From c70e95d95ba692bdefb5f2ddc851e06bc61aa3fd Mon Sep 17 00:00:00 2001 From: Roy Mdr Date: Sat, 16 Apr 2022 17:49:43 -0500 Subject: [PATCH] Profile DXF download Added feature: download flat DXF (2D) of the profile view --- resources/icons/file_dxf_2d.svg | 66 +++++++++++++ src/exporter/DXFProfileExporter.js | 145 +++++++++++++++++++++++++++++ src/viewer/profile.html | 4 + src/viewer/profile.js | 14 +++ 4 files changed, 229 insertions(+) create mode 100644 resources/icons/file_dxf_2d.svg create mode 100644 src/exporter/DXFProfileExporter.js diff --git a/resources/icons/file_dxf_2d.svg b/resources/icons/file_dxf_2d.svg new file mode 100644 index 00000000..81ccc062 --- /dev/null +++ b/resources/icons/file_dxf_2d.svg @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/exporter/DXFProfileExporter.js b/src/exporter/DXFProfileExporter.js new file mode 100644 index 00000000..c8d832ee --- /dev/null +++ b/src/exporter/DXFProfileExporter.js @@ -0,0 +1,145 @@ +/** + * + * @author roy.mdr / http://... + * + */ + +export class DXFProfileExporter { + + static toXYZ(points) { + + /* + points: { + ... + data: { + mileage: [0, 1, 2...], -> one per point + position: [0, 0, 0, 1, 1, 1, 2, 2, 2...], -> X, Y, Z + rgba: [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2...] -> R, G, B, A + }, + numPoints: Int + } + */ + + const pointsXYZ = { + x: [], + y: [], + z: [], + numPoints: 0 + }; + + const pData = points.data; + const pMileage = pData.mileage; + const pCoords = pData.position; + const pColor = pData.rgba; + + for (let pIx = 0; pIx < points.numPoints; pIx++) { + + const poMileage = pMileage[pIx]; + // const poCoordX = pCoords[ ((pIx * 3) + 0) ]; + // const poCoordY = pCoords[ ((pIx * 3) + 1) ]; + const poCoordZ = pCoords[ ((pIx * 3) + 2) ]; + // const poColorR = pColor[ ((pIx * 4) + 0) ]; + // const poColorG = pColor[ ((pIx * 4) + 1) ]; + // const poColorB = pColor[ ((pIx * 4) + 2) ]; + // const poColorA = pColor[ ((pIx * 4) + 3) ]; + + + pointsXYZ.x.push(poMileage); + pointsXYZ.y.push(0); + pointsXYZ.z.push(poCoordZ); + + } + + pointsXYZ.numPoints = points.numPoints; + + return pointsXYZ; + } + + static plotPCloudPoint(x, y, z) { + + const dxfSection = `0 +POINT +8 +layer_pointCloud +10 +${x} +20 +${y} +30 +${z} +`; + + return dxfSection; + } + + static toString(points) { + + const pCloud = DXFProfileExporter.toXYZ(points); + + const minX = Math.min(...pCloud.x); + const minY = Math.min(...pCloud.y); + const minZ = Math.min(...pCloud.z); + + const maxX = Math.max(...pCloud.x); + const maxY = Math.max(...pCloud.y); + const maxZ = Math.max(...pCloud.z); + + + const dxfHeader = `999 +DXF created from potree +0 +SECTION +2 +HEADER +9 +$ACADVER +1 +AC1006 +9 +$INSBASE +10 +0.0 +20 +0.0 +30 +0.0 +9 +$EXTMIN +10 +${minX} +20 +${minY} +30 +${minZ} +9 +$EXTMAX +10 +${maxX} +20 +${maxY} +30 +${maxZ} +0 +ENDSEC +`; + + let dxfBody = `0 +SECTION +2 +ENTITIES +`; + + for (let i = 0; i < pCloud.numPoints; i++) { + dxfBody += DXFProfileExporter.plotPCloudPoint(pCloud.x[i], pCloud.y[i], pCloud.z[i]); + } + + dxfBody += `0 +ENDSEC +`; + + const dxf = dxfHeader + dxfBody + '0\nEOF'; + + return dxf; + } + +} diff --git a/src/viewer/profile.html b/src/viewer/profile.html index 3eead5a3..75d75354 100644 --- a/src/viewer/profile.html +++ b/src/viewer/profile.html @@ -48,6 +48,10 @@ + + + + diff --git a/src/viewer/profile.js b/src/viewer/profile.js index 4fb60cfe..cf6c2d59 100644 --- a/src/viewer/profile.js +++ b/src/viewer/profile.js @@ -2,6 +2,7 @@ import * as THREE from "../../libs/three.js/build/three.module.js"; import {Utils} from "../utils.js"; import {Points} from "../Points.js"; +import {DXFProfileExporter} from "../exporter/DXFProfileExporter.js"; import {CSVExporter} from "../exporter/CSVExporter.js"; import {LASExporter} from "../exporter/LASExporter.js"; import { EventDispatcher } from "../EventDispatcher.js"; @@ -262,6 +263,9 @@ export class ProfileWindow extends EventDispatcher { let backwardIcon = `${exports.resourcePath}/icons/arrow_down.svg`; $('#potree_profile_move_backward').attr('src', backwardIcon); + let dxfIcon = `${exports.resourcePath}/icons/file_dxf_2d.svg`; + $('#potree_download_dxf_icon').attr('src', dxfIcon); + let csvIcon = `${exports.resourcePath}/icons/file_csv_2d.svg`; $('#potree_download_csv_icon').attr('src', csvIcon); @@ -499,6 +503,16 @@ export class ProfileWindow extends EventDispatcher { return points; }; + $('#potree_download_dxf_icon').click(() => { + + const points = getProfilePoints(); + + const string = DXFProfileExporter.toString(points); + + const blob = new Blob([string], {type: "text/string"}); + $('#potree_download_profile_dxf_link').attr('href', URL.createObjectURL(blob)); + }); + $('#potree_download_csv_icon').click(() => { let points = getProfilePoints();