|
1 | 1 | import type { Feature, FeatureCollection, LineString, Position } from "geojson";
|
2 | 2 | import type { ParsedSurface } from "./parse-xml";
|
| 3 | +import { createEasyWebWorker } from "easy-web-worker"; |
| 4 | + |
| 5 | +const contoursWorker = createEasyWebWorker< |
| 6 | + { |
| 7 | + triangles: [x: number, y: number, z: number][][]; |
| 8 | + elevation: number; |
| 9 | + }, |
| 10 | + { |
| 11 | + elevation: number; |
| 12 | + polylines: [number, number][][]; |
| 13 | + } |
| 14 | +>( |
| 15 | + ({ onMessage }) => { |
| 16 | + const contourLineOnFace = (face: [x: number, y: number, z: number][], z: number) => { |
| 17 | + let vertsAtElevation = 0; |
| 18 | + let line: [x: number, z: number][] = []; |
| 19 | + for (let i = 0; i < face.length; i++) { |
| 20 | + let vertex1 = face[i] as [x: number, y: number, z: number]; |
| 21 | + let vertex2 = face[(i + 1) % face.length] as [x: number, y: number, z: number]; |
| 22 | + if (vertex1[2] === z) vertsAtElevation++; |
| 23 | + |
| 24 | + if ( |
| 25 | + ((vertex1[2] <= z && vertex2[2] >= z) || (vertex1[2] >= z && vertex2[2] <= z)) && |
| 26 | + !Number.isNaN((z - vertex1[2]) / (vertex2[2] - vertex1[2])) |
| 27 | + ) { |
| 28 | + let t = (z - vertex1[2]) / (vertex2[2] - vertex1[2]); |
| 29 | + line.push([vertex1[0] + t * (vertex2[0] - vertex1[0]), vertex1[1] + t * (vertex2[1] - vertex1[1])]); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // If an edge is going to be detected by two triangles, prioritize the triangle with 3rd vertex at lower elevation |
| 34 | + if (vertsAtElevation >= 2 && face.map((f) => f[2]).reduce((a, b) => a + b) > z * face.length) return undefined; |
| 35 | + |
| 36 | + // Prevent zero length lines |
| 37 | + if ( |
| 38 | + line.length === 2 && |
| 39 | + (line[0] as any)[0] === (line[1] as any)[0] && |
| 40 | + (line[0] as any)[1] === (line[1] as any)[1] |
| 41 | + ) |
| 42 | + return undefined; |
| 43 | + |
| 44 | + if (line.length > 2) { |
| 45 | + line = [...new Set(line.map((v) => JSON.stringify(v)))].map((s) => JSON.parse(s)); |
| 46 | + } |
| 47 | + return line.length > 0 ? (line as [[x: number, z: number], [x: number, z: number]]) : undefined; |
| 48 | + }; |
| 49 | + |
| 50 | + const linesToPolyLines = (lineSegments: [[number, number], [number, number]][]) => { |
| 51 | + if (!Array.isArray(lineSegments) || lineSegments?.length === 0) { |
| 52 | + return []; |
| 53 | + // throw new Error("Invalid input: Please provide a non-empty array of line segments."); |
| 54 | + } |
| 55 | + |
| 56 | + const segmentsMapIndexes: { [coordinateKey: string]: number[] } = {}; |
| 57 | + const polylines: [number, number][][] = []; |
| 58 | + const parsedSegmentIndexes: number[] = []; |
| 59 | + |
| 60 | + const lineSegmentStrings = lineSegments.map((v) => v.map((c) => c.join(","))) as [string, string][]; |
| 61 | + |
| 62 | + lineSegmentStrings.forEach(([start, end], i) => { |
| 63 | + segmentsMapIndexes[start] = segmentsMapIndexes[start] ? [...(segmentsMapIndexes[start] || []), i] : [i]; |
| 64 | + segmentsMapIndexes[end] = segmentsMapIndexes[end] ? [...(segmentsMapIndexes[end] || []), i] : [i]; |
| 65 | + }); |
| 66 | + |
| 67 | + for (let i = 0; i < lineSegmentStrings.length; i++) { |
| 68 | + if (parsedSegmentIndexes.includes(i)) continue; |
| 69 | + |
| 70 | + parsedSegmentIndexes.push(i); |
| 71 | + |
| 72 | + let [start, end]: (string | null)[] = lineSegmentStrings[i] as [string, string]; |
| 73 | + let polyline = [start, end]; |
| 74 | + |
| 75 | + while (start && segmentsMapIndexes[start]) { |
| 76 | + const nextLineIndex: number | undefined = segmentsMapIndexes[start]?.find( |
| 77 | + (lineIndex) => !parsedSegmentIndexes.includes(lineIndex) |
| 78 | + ); |
| 79 | + if (nextLineIndex) { |
| 80 | + parsedSegmentIndexes.push(nextLineIndex); |
| 81 | + const nextLineSegment = lineSegmentStrings[nextLineIndex] as [string, string]; |
| 82 | + const nextLineSegmentPointIndex: number = nextLineSegment[0] === start ? 1 : 0; |
| 83 | + const newPoint = nextLineSegment[nextLineSegmentPointIndex] as string; |
| 84 | + polyline.unshift(newPoint); |
| 85 | + start = newPoint; |
| 86 | + } else { |
| 87 | + start = null; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + while (end && segmentsMapIndexes[end]) { |
| 92 | + const nextLineIndex: number | undefined = segmentsMapIndexes[end]?.find( |
| 93 | + (lineIndex) => !parsedSegmentIndexes.includes(lineIndex) |
| 94 | + ); |
| 95 | + if (nextLineIndex) { |
| 96 | + parsedSegmentIndexes.push(nextLineIndex); |
| 97 | + const nextLineSegment = lineSegmentStrings[nextLineIndex] as [string, string]; |
| 98 | + const nextLineSegmentPointIndex: number = nextLineSegment[0] === end ? 1 : 0; |
| 99 | + const newPoint = nextLineSegment[nextLineSegmentPointIndex] as string; |
| 100 | + polyline.push(newPoint); |
| 101 | + end = newPoint; |
| 102 | + } else { |
| 103 | + end = null; |
| 104 | + } |
| 105 | + } |
| 106 | + polylines.push(polyline.map((coord) => coord.split(",").map((v) => parseFloat(v)) as [number, number])); |
| 107 | + } |
| 108 | + return polylines; |
| 109 | + }; |
| 110 | + |
| 111 | + onMessage((message) => { |
| 112 | + const { triangles, elevation } = message.payload; |
| 113 | + const linesAtElevationE = triangles.reduce((prev, curr) => { |
| 114 | + const line = contourLineOnFace(curr, elevation); |
| 115 | + if (line) prev.push(line); |
| 116 | + return prev; |
| 117 | + }, [] as [[x: number, z: number], [x: number, z: number]][]); |
| 118 | + |
| 119 | + message.resolve({ |
| 120 | + elevation, |
| 121 | + polylines: linesToPolyLines(linesAtElevationE), |
| 122 | + }); |
| 123 | + }); |
| 124 | + }, |
| 125 | + { maxWorkers: 10 } |
| 126 | +); |
3 | 127 |
|
4 | 128 | const contourLineOnFace = (face: [x: number, y: number, z: number][], z: number) => {
|
5 | 129 | let vertsAtElevation = 0;
|
@@ -32,8 +156,9 @@ const contourLineOnFace = (face: [x: number, y: number, z: number][], z: number)
|
32 | 156 | };
|
33 | 157 |
|
34 | 158 | const linesToPolyLines = (lineSegments: [[number, number], [number, number]][]) => {
|
35 |
| - if (!Array.isArray(lineSegments) || lineSegments.length === 0) { |
36 |
| - throw new Error("Invalid input: Please provide a non-empty array of line segments."); |
| 159 | + if (!Array.isArray(lineSegments) || lineSegments?.length === 0) { |
| 160 | + return []; |
| 161 | + // throw new Error("Invalid input: Please provide a non-empty array of line segments."); |
37 | 162 | }
|
38 | 163 |
|
39 | 164 | const segmentsMapIndexes: { [coordinateKey: string]: number[] } = {};
|
@@ -150,23 +275,10 @@ const getContours = async (data: ParsedSurface, interval: number = 2) => {
|
150 | 275 |
|
151 | 276 | const elevations = contourElevations(minElevation, maxElevation, interval);
|
152 | 277 |
|
153 |
| - const elevationPolylines = elevations.map((e) => { |
154 |
| - const linesAtElevationE = triangles.reduce((prev, curr) => { |
155 |
| - const line = contourLineOnFace(curr, e); |
156 |
| - if (line) prev.push(line); |
157 |
| - return prev; |
158 |
| - }, [] as [[x: number, z: number], [x: number, z: number]][]); |
159 |
| - |
160 |
| - const polylinesAtElevationE = linesToPolyLines(linesAtElevationE); |
161 |
| - if (e === 442) { |
162 |
| - console.log("linesAtElevationE", JSON.stringify(linesAtElevationE)); |
163 |
| - console.log("polylinesAtElevationE", JSON.stringify(polylinesAtElevationE)); |
164 |
| - } |
165 |
| - return { |
166 |
| - elevation: e, |
167 |
| - polylines: polylinesAtElevationE, |
168 |
| - }; |
169 |
| - }); |
| 278 | + const elevationPolylines: { |
| 279 | + elevation: number; |
| 280 | + polylines: [number, number][][]; |
| 281 | + }[] = await Promise.all(elevations.map((elevation) => (contoursWorker.send as any)({ triangles, elevation }))); |
170 | 282 |
|
171 | 283 | return constructGeojson(elevationPolylines);
|
172 | 284 | };
|
|
0 commit comments