@loaders.gl/terrain 4.0.0-alpha.5 → 4.0.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.js +2 -2
- package/dist/dist.min.js +100 -106
- package/dist/es5/bundle.js +6 -0
- package/dist/es5/bundle.js.map +1 -0
- package/dist/es5/index.js +87 -0
- package/dist/es5/index.js.map +1 -0
- package/dist/es5/lib/decode-quantized-mesh.js +250 -0
- package/dist/es5/lib/decode-quantized-mesh.js.map +1 -0
- package/dist/es5/lib/delatin/index.js +408 -0
- package/dist/es5/lib/delatin/index.js.map +1 -0
- package/dist/es5/lib/helpers/skirt.js +105 -0
- package/dist/es5/lib/helpers/skirt.js.map +1 -0
- package/dist/es5/lib/parse-quantized-mesh.js +95 -0
- package/dist/es5/lib/parse-quantized-mesh.js.map +1 -0
- package/dist/es5/lib/parse-terrain.js +152 -0
- package/dist/es5/lib/parse-terrain.js.map +1 -0
- package/dist/es5/lib/utils/version.js +9 -0
- package/dist/es5/lib/utils/version.js.map +1 -0
- package/dist/es5/quantized-mesh-loader.js +24 -0
- package/dist/es5/quantized-mesh-loader.js.map +1 -0
- package/dist/es5/terrain-loader.js +32 -0
- package/dist/es5/terrain-loader.js.map +1 -0
- package/dist/es5/workers/quantized-mesh-worker.js +6 -0
- package/dist/es5/workers/quantized-mesh-worker.js.map +1 -0
- package/dist/es5/workers/terrain-worker.js +6 -0
- package/dist/es5/workers/terrain-worker.js.map +1 -0
- package/dist/esm/bundle.js +4 -0
- package/dist/esm/bundle.js.map +1 -0
- package/dist/esm/index.js +32 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lib/decode-quantized-mesh.js +231 -0
- package/dist/esm/lib/decode-quantized-mesh.js.map +1 -0
- package/dist/esm/lib/delatin/index.js +355 -0
- package/dist/esm/lib/delatin/index.js.map +1 -0
- package/dist/esm/lib/helpers/skirt.js +89 -0
- package/dist/esm/lib/helpers/skirt.js.map +1 -0
- package/dist/esm/lib/parse-quantized-mesh.js +85 -0
- package/dist/esm/lib/parse-quantized-mesh.js.map +1 -0
- package/dist/esm/lib/parse-terrain.js +151 -0
- package/dist/esm/lib/parse-terrain.js.map +1 -0
- package/dist/esm/lib/utils/version.js +2 -0
- package/dist/esm/lib/utils/version.js.map +1 -0
- package/dist/esm/quantized-mesh-loader.js +17 -0
- package/dist/esm/quantized-mesh-loader.js.map +1 -0
- package/dist/esm/terrain-loader.js +25 -0
- package/dist/esm/terrain-loader.js.map +1 -0
- package/dist/esm/workers/quantized-mesh-worker.js +4 -0
- package/dist/esm/workers/quantized-mesh-worker.js.map +1 -0
- package/dist/esm/workers/terrain-worker.js +4 -0
- package/dist/esm/workers/terrain-worker.js.map +1 -0
- package/dist/index.d.ts +27 -69
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -14
- package/dist/lib/decode-quantized-mesh.d.ts +1 -1
- package/dist/lib/decode-quantized-mesh.d.ts.map +1 -1
- package/dist/lib/decode-quantized-mesh.js +200 -241
- package/dist/lib/delatin/index.js +397 -443
- package/dist/lib/helpers/skirt.d.ts +1 -1
- package/dist/lib/helpers/skirt.d.ts.map +1 -1
- package/dist/lib/helpers/skirt.js +119 -91
- package/dist/lib/parse-quantized-mesh.d.ts +6 -24
- package/dist/lib/parse-quantized-mesh.d.ts.map +1 -1
- package/dist/lib/parse-quantized-mesh.js +94 -93
- package/dist/lib/parse-terrain.d.ts +28 -2
- package/dist/lib/parse-terrain.d.ts.map +1 -1
- package/dist/lib/parse-terrain.js +146 -172
- package/dist/lib/utils/version.js +7 -2
- package/dist/quantized-mesh-loader.d.ts +8 -17
- package/dist/quantized-mesh-loader.d.ts.map +1 -1
- package/dist/quantized-mesh-loader.js +20 -16
- package/dist/quantized-mesh-worker.js +146 -117
- package/dist/terrain-loader.d.ts +7 -27
- package/dist/terrain-loader.d.ts.map +1 -1
- package/dist/terrain-loader.js +28 -24
- package/dist/terrain-worker.js +146 -117
- package/dist/workers/quantized-mesh-worker.js +5 -4
- package/dist/workers/terrain-worker.js +5 -4
- package/package.json +9 -8
- package/src/index.ts +29 -13
- package/src/lib/helpers/skirt.ts +7 -7
- package/src/lib/parse-quantized-mesh.ts +47 -43
- package/src/lib/parse-terrain.ts +83 -89
- package/src/quantized-mesh-loader.ts +9 -4
- package/src/terrain-loader.ts +14 -11
- package/dist/bundle.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/decode-quantized-mesh.js.map +0 -1
- package/dist/lib/delatin/index.js.map +0 -1
- package/dist/lib/helpers/skirt.js.map +0 -1
- package/dist/lib/parse-quantized-mesh.js.map +0 -1
- package/dist/lib/parse-terrain.js.map +0 -1
- package/dist/lib/utils/version.js.map +0 -1
- package/dist/quantized-mesh-loader.js.map +0 -1
- package/dist/terrain-loader.js.map +0 -1
- package/dist/workers/quantized-mesh-worker.js.map +0 -1
- package/dist/workers/terrain-worker.js.map +0 -1
- package/src/workers/quantized-mesh-worker.js +0 -3
- package/src/workers/terrain-worker.js +0 -3
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
const QUANTIZED_MESH_HEADER = new Map([['centerX', Float64Array.BYTES_PER_ELEMENT], ['centerY', Float64Array.BYTES_PER_ELEMENT], ['centerZ', Float64Array.BYTES_PER_ELEMENT], ['minHeight', Float32Array.BYTES_PER_ELEMENT], ['maxHeight', Float32Array.BYTES_PER_ELEMENT], ['boundingSphereCenterX', Float64Array.BYTES_PER_ELEMENT], ['boundingSphereCenterY', Float64Array.BYTES_PER_ELEMENT], ['boundingSphereCenterZ', Float64Array.BYTES_PER_ELEMENT], ['boundingSphereRadius', Float64Array.BYTES_PER_ELEMENT], ['horizonOcclusionPointX', Float64Array.BYTES_PER_ELEMENT], ['horizonOcclusionPointY', Float64Array.BYTES_PER_ELEMENT], ['horizonOcclusionPointZ', Float64Array.BYTES_PER_ELEMENT]]);
|
|
2
|
+
function decodeZigZag(value) {
|
|
3
|
+
return value >> 1 ^ -(value & 1);
|
|
4
|
+
}
|
|
5
|
+
function decodeHeader(dataView) {
|
|
6
|
+
let position = 0;
|
|
7
|
+
const header = {};
|
|
8
|
+
for (const [key, bytesCount] of QUANTIZED_MESH_HEADER) {
|
|
9
|
+
const getter = bytesCount === 8 ? dataView.getFloat64 : dataView.getFloat32;
|
|
10
|
+
header[key] = getter.call(dataView, position, true);
|
|
11
|
+
position += bytesCount;
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
header,
|
|
15
|
+
headerEndPosition: position
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function decodeVertexData(dataView, headerEndPosition) {
|
|
19
|
+
let position = headerEndPosition;
|
|
20
|
+
const elementsPerVertex = 3;
|
|
21
|
+
const vertexCount = dataView.getUint32(position, true);
|
|
22
|
+
const vertexData = new Uint16Array(vertexCount * elementsPerVertex);
|
|
23
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
24
|
+
const bytesPerArrayElement = Uint16Array.BYTES_PER_ELEMENT;
|
|
25
|
+
const elementArrayLength = vertexCount * bytesPerArrayElement;
|
|
26
|
+
const uArrayStartPosition = position;
|
|
27
|
+
const vArrayStartPosition = uArrayStartPosition + elementArrayLength;
|
|
28
|
+
const heightArrayStartPosition = vArrayStartPosition + elementArrayLength;
|
|
29
|
+
let u = 0;
|
|
30
|
+
let v = 0;
|
|
31
|
+
let height = 0;
|
|
32
|
+
for (let i = 0; i < vertexCount; i++) {
|
|
33
|
+
u += decodeZigZag(dataView.getUint16(uArrayStartPosition + bytesPerArrayElement * i, true));
|
|
34
|
+
v += decodeZigZag(dataView.getUint16(vArrayStartPosition + bytesPerArrayElement * i, true));
|
|
35
|
+
height += decodeZigZag(dataView.getUint16(heightArrayStartPosition + bytesPerArrayElement * i, true));
|
|
36
|
+
vertexData[i] = u;
|
|
37
|
+
vertexData[i + vertexCount] = v;
|
|
38
|
+
vertexData[i + vertexCount * 2] = height;
|
|
39
|
+
}
|
|
40
|
+
position += elementArrayLength * 3;
|
|
41
|
+
return {
|
|
42
|
+
vertexData,
|
|
43
|
+
vertexDataEndPosition: position
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function decodeIndex(buffer, position, indicesCount, bytesPerIndex) {
|
|
47
|
+
let encoded = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true;
|
|
48
|
+
let indices;
|
|
49
|
+
if (bytesPerIndex === 2) {
|
|
50
|
+
indices = new Uint16Array(buffer, position, indicesCount);
|
|
51
|
+
} else {
|
|
52
|
+
indices = new Uint32Array(buffer, position, indicesCount);
|
|
53
|
+
}
|
|
54
|
+
if (!encoded) {
|
|
55
|
+
return indices;
|
|
56
|
+
}
|
|
57
|
+
let highest = 0;
|
|
58
|
+
for (let i = 0; i < indices.length; ++i) {
|
|
59
|
+
const code = indices[i];
|
|
60
|
+
indices[i] = highest - code;
|
|
61
|
+
if (code === 0) {
|
|
62
|
+
++highest;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return indices;
|
|
66
|
+
}
|
|
67
|
+
function decodeTriangleIndices(dataView, vertexData, vertexDataEndPosition) {
|
|
68
|
+
let position = vertexDataEndPosition;
|
|
69
|
+
const elementsPerVertex = 3;
|
|
70
|
+
const vertexCount = vertexData.length / elementsPerVertex;
|
|
71
|
+
const bytesPerIndex = vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
|
|
72
|
+
if (position % bytesPerIndex !== 0) {
|
|
73
|
+
position += bytesPerIndex - position % bytesPerIndex;
|
|
74
|
+
}
|
|
75
|
+
const triangleCount = dataView.getUint32(position, true);
|
|
76
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
77
|
+
const triangleIndicesCount = triangleCount * 3;
|
|
78
|
+
const triangleIndices = decodeIndex(dataView.buffer, position, triangleIndicesCount, bytesPerIndex);
|
|
79
|
+
position += triangleIndicesCount * bytesPerIndex;
|
|
80
|
+
return {
|
|
81
|
+
triangleIndicesEndPosition: position,
|
|
82
|
+
triangleIndices
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function decodeEdgeIndices(dataView, vertexData, triangleIndicesEndPosition) {
|
|
86
|
+
let position = triangleIndicesEndPosition;
|
|
87
|
+
const elementsPerVertex = 3;
|
|
88
|
+
const vertexCount = vertexData.length / elementsPerVertex;
|
|
89
|
+
const bytesPerIndex = vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;
|
|
90
|
+
const westVertexCount = dataView.getUint32(position, true);
|
|
91
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
92
|
+
const westIndices = decodeIndex(dataView.buffer, position, westVertexCount, bytesPerIndex, false);
|
|
93
|
+
position += westVertexCount * bytesPerIndex;
|
|
94
|
+
const southVertexCount = dataView.getUint32(position, true);
|
|
95
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
96
|
+
const southIndices = decodeIndex(dataView.buffer, position, southVertexCount, bytesPerIndex, false);
|
|
97
|
+
position += southVertexCount * bytesPerIndex;
|
|
98
|
+
const eastVertexCount = dataView.getUint32(position, true);
|
|
99
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
100
|
+
const eastIndices = decodeIndex(dataView.buffer, position, eastVertexCount, bytesPerIndex, false);
|
|
101
|
+
position += eastVertexCount * bytesPerIndex;
|
|
102
|
+
const northVertexCount = dataView.getUint32(position, true);
|
|
103
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
104
|
+
const northIndices = decodeIndex(dataView.buffer, position, northVertexCount, bytesPerIndex, false);
|
|
105
|
+
position += northVertexCount * bytesPerIndex;
|
|
106
|
+
return {
|
|
107
|
+
edgeIndicesEndPosition: position,
|
|
108
|
+
westIndices,
|
|
109
|
+
southIndices,
|
|
110
|
+
eastIndices,
|
|
111
|
+
northIndices
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function decodeVertexNormalsExtension(extensionDataView) {
|
|
115
|
+
return new Uint8Array(extensionDataView.buffer, extensionDataView.byteOffset, extensionDataView.byteLength);
|
|
116
|
+
}
|
|
117
|
+
function decodeWaterMaskExtension(extensionDataView) {
|
|
118
|
+
return extensionDataView.buffer.slice(extensionDataView.byteOffset, extensionDataView.byteOffset + extensionDataView.byteLength);
|
|
119
|
+
}
|
|
120
|
+
function decodeExtensions(dataView, indicesEndPosition) {
|
|
121
|
+
const extensions = {};
|
|
122
|
+
if (dataView.byteLength <= indicesEndPosition) {
|
|
123
|
+
return {
|
|
124
|
+
extensions,
|
|
125
|
+
extensionsEndPosition: indicesEndPosition
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
let position = indicesEndPosition;
|
|
129
|
+
while (position < dataView.byteLength) {
|
|
130
|
+
const extensionId = dataView.getUint8(position, true);
|
|
131
|
+
position += Uint8Array.BYTES_PER_ELEMENT;
|
|
132
|
+
const extensionLength = dataView.getUint32(position, true);
|
|
133
|
+
position += Uint32Array.BYTES_PER_ELEMENT;
|
|
134
|
+
const extensionView = new DataView(dataView.buffer, position, extensionLength);
|
|
135
|
+
switch (extensionId) {
|
|
136
|
+
case 1:
|
|
137
|
+
{
|
|
138
|
+
extensions.vertexNormals = decodeVertexNormalsExtension(extensionView);
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
case 2:
|
|
142
|
+
{
|
|
143
|
+
extensions.waterMask = decodeWaterMaskExtension(extensionView);
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
default:
|
|
147
|
+
{}
|
|
148
|
+
}
|
|
149
|
+
position += extensionLength;
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
extensions,
|
|
153
|
+
extensionsEndPosition: position
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
export const DECODING_STEPS = {
|
|
157
|
+
header: 0,
|
|
158
|
+
vertices: 1,
|
|
159
|
+
triangleIndices: 2,
|
|
160
|
+
edgeIndices: 3,
|
|
161
|
+
extensions: 4
|
|
162
|
+
};
|
|
163
|
+
const DEFAULT_OPTIONS = {
|
|
164
|
+
maxDecodingStep: DECODING_STEPS.extensions
|
|
165
|
+
};
|
|
166
|
+
export default function decode(data, userOptions) {
|
|
167
|
+
const options = Object.assign({}, DEFAULT_OPTIONS, userOptions);
|
|
168
|
+
const view = new DataView(data);
|
|
169
|
+
const {
|
|
170
|
+
header,
|
|
171
|
+
headerEndPosition
|
|
172
|
+
} = decodeHeader(view);
|
|
173
|
+
if (options.maxDecodingStep < DECODING_STEPS.vertices) {
|
|
174
|
+
return {
|
|
175
|
+
header
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const {
|
|
179
|
+
vertexData,
|
|
180
|
+
vertexDataEndPosition
|
|
181
|
+
} = decodeVertexData(view, headerEndPosition);
|
|
182
|
+
if (options.maxDecodingStep < DECODING_STEPS.triangleIndices) {
|
|
183
|
+
return {
|
|
184
|
+
header,
|
|
185
|
+
vertexData
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const {
|
|
189
|
+
triangleIndices,
|
|
190
|
+
triangleIndicesEndPosition
|
|
191
|
+
} = decodeTriangleIndices(view, vertexData, vertexDataEndPosition);
|
|
192
|
+
if (options.maxDecodingStep < DECODING_STEPS.edgeIndices) {
|
|
193
|
+
return {
|
|
194
|
+
header,
|
|
195
|
+
vertexData,
|
|
196
|
+
triangleIndices
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
const {
|
|
200
|
+
westIndices,
|
|
201
|
+
southIndices,
|
|
202
|
+
eastIndices,
|
|
203
|
+
northIndices,
|
|
204
|
+
edgeIndicesEndPosition
|
|
205
|
+
} = decodeEdgeIndices(view, vertexData, triangleIndicesEndPosition);
|
|
206
|
+
if (options.maxDecodingStep < DECODING_STEPS.extensions) {
|
|
207
|
+
return {
|
|
208
|
+
header,
|
|
209
|
+
vertexData,
|
|
210
|
+
triangleIndices,
|
|
211
|
+
westIndices,
|
|
212
|
+
northIndices,
|
|
213
|
+
eastIndices,
|
|
214
|
+
southIndices
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const {
|
|
218
|
+
extensions
|
|
219
|
+
} = decodeExtensions(view, edgeIndicesEndPosition);
|
|
220
|
+
return {
|
|
221
|
+
header,
|
|
222
|
+
vertexData,
|
|
223
|
+
triangleIndices,
|
|
224
|
+
westIndices,
|
|
225
|
+
northIndices,
|
|
226
|
+
eastIndices,
|
|
227
|
+
southIndices,
|
|
228
|
+
extensions
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
//# sourceMappingURL=decode-quantized-mesh.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode-quantized-mesh.js","names":["QUANTIZED_MESH_HEADER","Map","Float64Array","BYTES_PER_ELEMENT","Float32Array","decodeZigZag","value","decodeHeader","dataView","position","header","key","bytesCount","getter","getFloat64","getFloat32","call","headerEndPosition","decodeVertexData","elementsPerVertex","vertexCount","getUint32","vertexData","Uint16Array","Uint32Array","bytesPerArrayElement","elementArrayLength","uArrayStartPosition","vArrayStartPosition","heightArrayStartPosition","u","v","height","i","getUint16","vertexDataEndPosition","decodeIndex","buffer","indicesCount","bytesPerIndex","encoded","arguments","length","undefined","indices","highest","code","decodeTriangleIndices","triangleCount","triangleIndicesCount","triangleIndices","triangleIndicesEndPosition","decodeEdgeIndices","westVertexCount","westIndices","southVertexCount","southIndices","eastVertexCount","eastIndices","northVertexCount","northIndices","edgeIndicesEndPosition","decodeVertexNormalsExtension","extensionDataView","Uint8Array","byteOffset","byteLength","decodeWaterMaskExtension","slice","decodeExtensions","indicesEndPosition","extensions","extensionsEndPosition","extensionId","getUint8","extensionLength","extensionView","DataView","vertexNormals","waterMask","DECODING_STEPS","vertices","edgeIndices","DEFAULT_OPTIONS","maxDecodingStep","decode","data","userOptions","options","Object","assign","view"],"sources":["../../../src/lib/decode-quantized-mesh.ts"],"sourcesContent":["// Copyright (C) 2018-2019 HERE Europe B.V.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\nconst QUANTIZED_MESH_HEADER = new Map([\n ['centerX', Float64Array.BYTES_PER_ELEMENT],\n ['centerY', Float64Array.BYTES_PER_ELEMENT],\n ['centerZ', Float64Array.BYTES_PER_ELEMENT],\n\n ['minHeight', Float32Array.BYTES_PER_ELEMENT],\n ['maxHeight', Float32Array.BYTES_PER_ELEMENT],\n\n ['boundingSphereCenterX', Float64Array.BYTES_PER_ELEMENT],\n ['boundingSphereCenterY', Float64Array.BYTES_PER_ELEMENT],\n ['boundingSphereCenterZ', Float64Array.BYTES_PER_ELEMENT],\n ['boundingSphereRadius', Float64Array.BYTES_PER_ELEMENT],\n\n ['horizonOcclusionPointX', Float64Array.BYTES_PER_ELEMENT],\n ['horizonOcclusionPointY', Float64Array.BYTES_PER_ELEMENT],\n ['horizonOcclusionPointZ', Float64Array.BYTES_PER_ELEMENT]\n]);\n\nfunction decodeZigZag(value) {\n return (value >> 1) ^ -(value & 1);\n}\n\nfunction decodeHeader(dataView) {\n let position = 0;\n const header = {};\n\n for (const [key, bytesCount] of QUANTIZED_MESH_HEADER) {\n const getter = bytesCount === 8 ? dataView.getFloat64 : dataView.getFloat32;\n\n header[key] = getter.call(dataView, position, true);\n position += bytesCount;\n }\n\n return {header, headerEndPosition: position};\n}\n\nfunction decodeVertexData(dataView, headerEndPosition) {\n let position = headerEndPosition;\n const elementsPerVertex = 3;\n const vertexCount = dataView.getUint32(position, true);\n const vertexData = new Uint16Array(vertexCount * elementsPerVertex);\n\n position += Uint32Array.BYTES_PER_ELEMENT;\n\n const bytesPerArrayElement = Uint16Array.BYTES_PER_ELEMENT;\n const elementArrayLength = vertexCount * bytesPerArrayElement;\n const uArrayStartPosition = position;\n const vArrayStartPosition = uArrayStartPosition + elementArrayLength;\n const heightArrayStartPosition = vArrayStartPosition + elementArrayLength;\n\n let u = 0;\n let v = 0;\n let height = 0;\n\n for (let i = 0; i < vertexCount; i++) {\n u += decodeZigZag(dataView.getUint16(uArrayStartPosition + bytesPerArrayElement * i, true));\n v += decodeZigZag(dataView.getUint16(vArrayStartPosition + bytesPerArrayElement * i, true));\n height += decodeZigZag(\n dataView.getUint16(heightArrayStartPosition + bytesPerArrayElement * i, true)\n );\n\n vertexData[i] = u;\n vertexData[i + vertexCount] = v;\n vertexData[i + vertexCount * 2] = height;\n }\n\n position += elementArrayLength * 3;\n\n return {vertexData, vertexDataEndPosition: position};\n}\n\nfunction decodeIndex(buffer, position, indicesCount, bytesPerIndex, encoded = true) {\n let indices;\n\n if (bytesPerIndex === 2) {\n indices = new Uint16Array(buffer, position, indicesCount);\n } else {\n indices = new Uint32Array(buffer, position, indicesCount);\n }\n\n if (!encoded) {\n return indices;\n }\n\n let highest = 0;\n\n for (let i = 0; i < indices.length; ++i) {\n const code = indices[i];\n\n indices[i] = highest - code;\n\n if (code === 0) {\n ++highest;\n }\n }\n\n return indices;\n}\n\nfunction decodeTriangleIndices(dataView, vertexData, vertexDataEndPosition) {\n let position = vertexDataEndPosition;\n const elementsPerVertex = 3;\n const vertexCount = vertexData.length / elementsPerVertex;\n const bytesPerIndex =\n vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;\n\n if (position % bytesPerIndex !== 0) {\n position += bytesPerIndex - (position % bytesPerIndex);\n }\n\n const triangleCount = dataView.getUint32(position, true);\n position += Uint32Array.BYTES_PER_ELEMENT;\n\n const triangleIndicesCount = triangleCount * 3;\n const triangleIndices = decodeIndex(\n dataView.buffer,\n position,\n triangleIndicesCount,\n bytesPerIndex\n );\n position += triangleIndicesCount * bytesPerIndex;\n\n return {\n triangleIndicesEndPosition: position,\n triangleIndices\n };\n}\n\nfunction decodeEdgeIndices(dataView, vertexData, triangleIndicesEndPosition) {\n let position = triangleIndicesEndPosition;\n const elementsPerVertex = 3;\n const vertexCount = vertexData.length / elementsPerVertex;\n const bytesPerIndex =\n vertexCount > 65536 ? Uint32Array.BYTES_PER_ELEMENT : Uint16Array.BYTES_PER_ELEMENT;\n\n const westVertexCount = dataView.getUint32(position, true);\n position += Uint32Array.BYTES_PER_ELEMENT;\n\n const westIndices = decodeIndex(dataView.buffer, position, westVertexCount, bytesPerIndex, false);\n position += westVertexCount * bytesPerIndex;\n\n const southVertexCount = dataView.getUint32(position, true);\n position += Uint32Array.BYTES_PER_ELEMENT;\n\n const southIndices = decodeIndex(\n dataView.buffer,\n position,\n southVertexCount,\n bytesPerIndex,\n false\n );\n position += southVertexCount * bytesPerIndex;\n\n const eastVertexCount = dataView.getUint32(position, true);\n position += Uint32Array.BYTES_PER_ELEMENT;\n\n const eastIndices = decodeIndex(dataView.buffer, position, eastVertexCount, bytesPerIndex, false);\n position += eastVertexCount * bytesPerIndex;\n\n const northVertexCount = dataView.getUint32(position, true);\n position += Uint32Array.BYTES_PER_ELEMENT;\n\n const northIndices = decodeIndex(\n dataView.buffer,\n position,\n northVertexCount,\n bytesPerIndex,\n false\n );\n position += northVertexCount * bytesPerIndex;\n\n return {\n edgeIndicesEndPosition: position,\n westIndices,\n southIndices,\n eastIndices,\n northIndices\n };\n}\n\nfunction decodeVertexNormalsExtension(extensionDataView) {\n return new Uint8Array(\n extensionDataView.buffer,\n extensionDataView.byteOffset,\n extensionDataView.byteLength\n );\n}\n\nfunction decodeWaterMaskExtension(extensionDataView) {\n return extensionDataView.buffer.slice(\n extensionDataView.byteOffset,\n extensionDataView.byteOffset + extensionDataView.byteLength\n );\n}\n\ntype Extensions = {\n vertexNormals?: any;\n waterMask?: any;\n};\n\nfunction decodeExtensions(dataView, indicesEndPosition) {\n const extensions: Extensions = {};\n\n if (dataView.byteLength <= indicesEndPosition) {\n return {extensions, extensionsEndPosition: indicesEndPosition};\n }\n\n let position = indicesEndPosition;\n\n while (position < dataView.byteLength) {\n const extensionId = dataView.getUint8(position, true);\n position += Uint8Array.BYTES_PER_ELEMENT;\n\n const extensionLength = dataView.getUint32(position, true);\n position += Uint32Array.BYTES_PER_ELEMENT;\n\n const extensionView = new DataView(dataView.buffer, position, extensionLength);\n\n switch (extensionId) {\n case 1: {\n extensions.vertexNormals = decodeVertexNormalsExtension(extensionView);\n\n break;\n }\n case 2: {\n extensions.waterMask = decodeWaterMaskExtension(extensionView);\n\n break;\n }\n default: {\n // console.warn(`Unknown extension with id ${extensionId}`)\n }\n }\n\n position += extensionLength;\n }\n\n return {extensions, extensionsEndPosition: position};\n}\n\nexport const DECODING_STEPS = {\n header: 0,\n vertices: 1,\n triangleIndices: 2,\n edgeIndices: 3,\n extensions: 4\n};\n\nconst DEFAULT_OPTIONS = {\n maxDecodingStep: DECODING_STEPS.extensions\n};\n\nexport default function decode(data, userOptions) {\n const options = Object.assign({}, DEFAULT_OPTIONS, userOptions);\n const view = new DataView(data);\n const {header, headerEndPosition} = decodeHeader(view);\n\n if (options.maxDecodingStep < DECODING_STEPS.vertices) {\n return {header};\n }\n\n const {vertexData, vertexDataEndPosition} = decodeVertexData(view, headerEndPosition);\n\n if (options.maxDecodingStep < DECODING_STEPS.triangleIndices) {\n return {header, vertexData};\n }\n\n const {triangleIndices, triangleIndicesEndPosition} = decodeTriangleIndices(\n view,\n vertexData,\n vertexDataEndPosition\n );\n\n if (options.maxDecodingStep < DECODING_STEPS.edgeIndices) {\n return {header, vertexData, triangleIndices};\n }\n\n const {westIndices, southIndices, eastIndices, northIndices, edgeIndicesEndPosition} =\n decodeEdgeIndices(view, vertexData, triangleIndicesEndPosition);\n\n if (options.maxDecodingStep < DECODING_STEPS.extensions) {\n return {\n header,\n vertexData,\n triangleIndices,\n westIndices,\n northIndices,\n eastIndices,\n southIndices\n };\n }\n\n const {extensions} = decodeExtensions(view, edgeIndicesEndPosition);\n\n return {\n header,\n vertexData,\n triangleIndices,\n westIndices,\n northIndices,\n eastIndices,\n southIndices,\n extensions\n };\n}\n"],"mappings":"AAoBA,MAAMA,qBAAqB,GAAG,IAAIC,GAAG,CAAC,CACpC,CAAC,SAAS,EAAEC,YAAY,CAACC,iBAAiB,CAAC,EAC3C,CAAC,SAAS,EAAED,YAAY,CAACC,iBAAiB,CAAC,EAC3C,CAAC,SAAS,EAAED,YAAY,CAACC,iBAAiB,CAAC,EAE3C,CAAC,WAAW,EAAEC,YAAY,CAACD,iBAAiB,CAAC,EAC7C,CAAC,WAAW,EAAEC,YAAY,CAACD,iBAAiB,CAAC,EAE7C,CAAC,uBAAuB,EAAED,YAAY,CAACC,iBAAiB,CAAC,EACzD,CAAC,uBAAuB,EAAED,YAAY,CAACC,iBAAiB,CAAC,EACzD,CAAC,uBAAuB,EAAED,YAAY,CAACC,iBAAiB,CAAC,EACzD,CAAC,sBAAsB,EAAED,YAAY,CAACC,iBAAiB,CAAC,EAExD,CAAC,wBAAwB,EAAED,YAAY,CAACC,iBAAiB,CAAC,EAC1D,CAAC,wBAAwB,EAAED,YAAY,CAACC,iBAAiB,CAAC,EAC1D,CAAC,wBAAwB,EAAED,YAAY,CAACC,iBAAiB,CAAC,CAC3D,CAAC;AAEF,SAASE,YAAYA,CAACC,KAAK,EAAE;EAC3B,OAAQA,KAAK,IAAI,CAAC,GAAI,EAAEA,KAAK,GAAG,CAAC,CAAC;AACpC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C7B;EAEA,MAAM;IAAC4B,eAAe;IAAEC;EAA0B,CAAC,GAAGJ,qBAAqB,CACzE4C,IAAI,EACJrE,UAAU,EACVa,qBACF,CAAC;EAED,IAAIqD,OAAO,CAACJ,eAAe,GAAGJ,cAAc,CAACE,WAAW,EAAE;IACxD,OAAO;MAACxE,MAAM;MAAEY,UAAU;MAAE4B;IAAe,CAAC;EAC9C;EAEA,MAAM;IAACI,WAAW;IAAEE,YAAY;IAAEE,WAAW;IAAEE,YAAY;IAAEC;EAAsB,CAAC,GAClFT,iBAAiB,CAACuC,IAAI,EAAErE,UAAU,EAAE6B,0BAA0B,CAAC;EAEjE,IAAIqC,OAAO,CAACJ,eAAe,GAAGJ,cAAc,CAACT,UAAU,EAAE;IACvD,OAAO;MACL7D,MAAM;MACNY,UAAU;MACV4B,eAAe;MACfI,WAAW;MACXM,YAAY;MACZF,WAAW;MACXF;IACF,CAAC;EACH;EAEA,MAAM;IAACe;EAAU,CAAC,GAAGF,gBAAgB,CAACsB,IAAI,EAAE9B,sBAAsB,CAAC;EAEnE,OAAO;IACLnD,MAAM;IACNY,UAAU;IACV4B,eAAe;IACfI,WAAW;IACXM,YAAY;IACZF,WAAW;IACXF,YAAY;IACZe;EACF,CAAC;AACH"}
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
export default class Delatin {
|
|
2
|
+
constructor(data, width) {
|
|
3
|
+
let height = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : width;
|
|
4
|
+
this.data = data;
|
|
5
|
+
this.width = width;
|
|
6
|
+
this.height = height;
|
|
7
|
+
this.coords = [];
|
|
8
|
+
this.triangles = [];
|
|
9
|
+
this._halfedges = [];
|
|
10
|
+
this._candidates = [];
|
|
11
|
+
this._queueIndices = [];
|
|
12
|
+
this._queue = [];
|
|
13
|
+
this._errors = [];
|
|
14
|
+
this._rms = [];
|
|
15
|
+
this._pending = [];
|
|
16
|
+
this._pendingLen = 0;
|
|
17
|
+
this._rmsSum = 0;
|
|
18
|
+
const x1 = width - 1;
|
|
19
|
+
const y1 = height - 1;
|
|
20
|
+
const p0 = this._addPoint(0, 0);
|
|
21
|
+
const p1 = this._addPoint(x1, 0);
|
|
22
|
+
const p2 = this._addPoint(0, y1);
|
|
23
|
+
const p3 = this._addPoint(x1, y1);
|
|
24
|
+
const t0 = this._addTriangle(p3, p0, p2, -1, -1, -1);
|
|
25
|
+
this._addTriangle(p0, p3, p1, t0, -1, -1);
|
|
26
|
+
this._flush();
|
|
27
|
+
}
|
|
28
|
+
run() {
|
|
29
|
+
let maxError = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
|
|
30
|
+
while (this.getMaxError() > maxError) {
|
|
31
|
+
this.refine();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
refine() {
|
|
35
|
+
this._step();
|
|
36
|
+
this._flush();
|
|
37
|
+
}
|
|
38
|
+
getMaxError() {
|
|
39
|
+
return this._errors[0];
|
|
40
|
+
}
|
|
41
|
+
getRMSD() {
|
|
42
|
+
return this._rmsSum > 0 ? Math.sqrt(this._rmsSum / (this.width * this.height)) : 0;
|
|
43
|
+
}
|
|
44
|
+
heightAt(x, y) {
|
|
45
|
+
return this.data[this.width * y + x];
|
|
46
|
+
}
|
|
47
|
+
_flush() {
|
|
48
|
+
const coords = this.coords;
|
|
49
|
+
for (let i = 0; i < this._pendingLen; i++) {
|
|
50
|
+
const t = this._pending[i];
|
|
51
|
+
const a = 2 * this.triangles[t * 3 + 0];
|
|
52
|
+
const b = 2 * this.triangles[t * 3 + 1];
|
|
53
|
+
const c = 2 * this.triangles[t * 3 + 2];
|
|
54
|
+
this._findCandidate(coords[a], coords[a + 1], coords[b], coords[b + 1], coords[c], coords[c + 1], t);
|
|
55
|
+
}
|
|
56
|
+
this._pendingLen = 0;
|
|
57
|
+
}
|
|
58
|
+
_findCandidate(p0x, p0y, p1x, p1y, p2x, p2y, t) {
|
|
59
|
+
const minX = Math.min(p0x, p1x, p2x);
|
|
60
|
+
const minY = Math.min(p0y, p1y, p2y);
|
|
61
|
+
const maxX = Math.max(p0x, p1x, p2x);
|
|
62
|
+
const maxY = Math.max(p0y, p1y, p2y);
|
|
63
|
+
let w00 = orient(p1x, p1y, p2x, p2y, minX, minY);
|
|
64
|
+
let w01 = orient(p2x, p2y, p0x, p0y, minX, minY);
|
|
65
|
+
let w02 = orient(p0x, p0y, p1x, p1y, minX, minY);
|
|
66
|
+
const a01 = p1y - p0y;
|
|
67
|
+
const b01 = p0x - p1x;
|
|
68
|
+
const a12 = p2y - p1y;
|
|
69
|
+
const b12 = p1x - p2x;
|
|
70
|
+
const a20 = p0y - p2y;
|
|
71
|
+
const b20 = p2x - p0x;
|
|
72
|
+
const a = orient(p0x, p0y, p1x, p1y, p2x, p2y);
|
|
73
|
+
const z0 = this.heightAt(p0x, p0y) / a;
|
|
74
|
+
const z1 = this.heightAt(p1x, p1y) / a;
|
|
75
|
+
const z2 = this.heightAt(p2x, p2y) / a;
|
|
76
|
+
let maxError = 0;
|
|
77
|
+
let mx = 0;
|
|
78
|
+
let my = 0;
|
|
79
|
+
let rms = 0;
|
|
80
|
+
for (let y = minY; y <= maxY; y++) {
|
|
81
|
+
let dx = 0;
|
|
82
|
+
if (w00 < 0 && a12 !== 0) {
|
|
83
|
+
dx = Math.max(dx, Math.floor(-w00 / a12));
|
|
84
|
+
}
|
|
85
|
+
if (w01 < 0 && a20 !== 0) {
|
|
86
|
+
dx = Math.max(dx, Math.floor(-w01 / a20));
|
|
87
|
+
}
|
|
88
|
+
if (w02 < 0 && a01 !== 0) {
|
|
89
|
+
dx = Math.max(dx, Math.floor(-w02 / a01));
|
|
90
|
+
}
|
|
91
|
+
let w0 = w00 + a12 * dx;
|
|
92
|
+
let w1 = w01 + a20 * dx;
|
|
93
|
+
let w2 = w02 + a01 * dx;
|
|
94
|
+
let wasInside = false;
|
|
95
|
+
for (let x = minX + dx; x <= maxX; x++) {
|
|
96
|
+
if (w0 >= 0 && w1 >= 0 && w2 >= 0) {
|
|
97
|
+
wasInside = true;
|
|
98
|
+
const z = z0 * w0 + z1 * w1 + z2 * w2;
|
|
99
|
+
const dz = Math.abs(z - this.heightAt(x, y));
|
|
100
|
+
rms += dz * dz;
|
|
101
|
+
if (dz > maxError) {
|
|
102
|
+
maxError = dz;
|
|
103
|
+
mx = x;
|
|
104
|
+
my = y;
|
|
105
|
+
}
|
|
106
|
+
} else if (wasInside) {
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
w0 += a12;
|
|
110
|
+
w1 += a20;
|
|
111
|
+
w2 += a01;
|
|
112
|
+
}
|
|
113
|
+
w00 += b12;
|
|
114
|
+
w01 += b20;
|
|
115
|
+
w02 += b01;
|
|
116
|
+
}
|
|
117
|
+
if (mx === p0x && my === p0y || mx === p1x && my === p1y || mx === p2x && my === p2y) {
|
|
118
|
+
maxError = 0;
|
|
119
|
+
}
|
|
120
|
+
this._candidates[2 * t] = mx;
|
|
121
|
+
this._candidates[2 * t + 1] = my;
|
|
122
|
+
this._rms[t] = rms;
|
|
123
|
+
this._queuePush(t, maxError, rms);
|
|
124
|
+
}
|
|
125
|
+
_step() {
|
|
126
|
+
const t = this._queuePop();
|
|
127
|
+
const e0 = t * 3 + 0;
|
|
128
|
+
const e1 = t * 3 + 1;
|
|
129
|
+
const e2 = t * 3 + 2;
|
|
130
|
+
const p0 = this.triangles[e0];
|
|
131
|
+
const p1 = this.triangles[e1];
|
|
132
|
+
const p2 = this.triangles[e2];
|
|
133
|
+
const ax = this.coords[2 * p0];
|
|
134
|
+
const ay = this.coords[2 * p0 + 1];
|
|
135
|
+
const bx = this.coords[2 * p1];
|
|
136
|
+
const by = this.coords[2 * p1 + 1];
|
|
137
|
+
const cx = this.coords[2 * p2];
|
|
138
|
+
const cy = this.coords[2 * p2 + 1];
|
|
139
|
+
const px = this._candidates[2 * t];
|
|
140
|
+
const py = this._candidates[2 * t + 1];
|
|
141
|
+
const pn = this._addPoint(px, py);
|
|
142
|
+
if (orient(ax, ay, bx, by, px, py) === 0) {
|
|
143
|
+
this._handleCollinear(pn, e0);
|
|
144
|
+
} else if (orient(bx, by, cx, cy, px, py) === 0) {
|
|
145
|
+
this._handleCollinear(pn, e1);
|
|
146
|
+
} else if (orient(cx, cy, ax, ay, px, py) === 0) {
|
|
147
|
+
this._handleCollinear(pn, e2);
|
|
148
|
+
} else {
|
|
149
|
+
const h0 = this._halfedges[e0];
|
|
150
|
+
const h1 = this._halfedges[e1];
|
|
151
|
+
const h2 = this._halfedges[e2];
|
|
152
|
+
const t0 = this._addTriangle(p0, p1, pn, h0, -1, -1, e0);
|
|
153
|
+
const t1 = this._addTriangle(p1, p2, pn, h1, -1, t0 + 1);
|
|
154
|
+
const t2 = this._addTriangle(p2, p0, pn, h2, t0 + 2, t1 + 1);
|
|
155
|
+
this._legalize(t0);
|
|
156
|
+
this._legalize(t1);
|
|
157
|
+
this._legalize(t2);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
_addPoint(x, y) {
|
|
161
|
+
const i = this.coords.length >> 1;
|
|
162
|
+
this.coords.push(x, y);
|
|
163
|
+
return i;
|
|
164
|
+
}
|
|
165
|
+
_addTriangle(a, b, c, ab, bc, ca) {
|
|
166
|
+
let e = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : this.triangles.length;
|
|
167
|
+
const t = e / 3;
|
|
168
|
+
this.triangles[e + 0] = a;
|
|
169
|
+
this.triangles[e + 1] = b;
|
|
170
|
+
this.triangles[e + 2] = c;
|
|
171
|
+
this._halfedges[e + 0] = ab;
|
|
172
|
+
this._halfedges[e + 1] = bc;
|
|
173
|
+
this._halfedges[e + 2] = ca;
|
|
174
|
+
if (ab >= 0) {
|
|
175
|
+
this._halfedges[ab] = e + 0;
|
|
176
|
+
}
|
|
177
|
+
if (bc >= 0) {
|
|
178
|
+
this._halfedges[bc] = e + 1;
|
|
179
|
+
}
|
|
180
|
+
if (ca >= 0) {
|
|
181
|
+
this._halfedges[ca] = e + 2;
|
|
182
|
+
}
|
|
183
|
+
this._candidates[2 * t + 0] = 0;
|
|
184
|
+
this._candidates[2 * t + 1] = 0;
|
|
185
|
+
this._queueIndices[t] = -1;
|
|
186
|
+
this._rms[t] = 0;
|
|
187
|
+
this._pending[this._pendingLen++] = t;
|
|
188
|
+
return e;
|
|
189
|
+
}
|
|
190
|
+
_legalize(a) {
|
|
191
|
+
const b = this._halfedges[a];
|
|
192
|
+
if (b < 0) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const a0 = a - a % 3;
|
|
196
|
+
const b0 = b - b % 3;
|
|
197
|
+
const al = a0 + (a + 1) % 3;
|
|
198
|
+
const ar = a0 + (a + 2) % 3;
|
|
199
|
+
const bl = b0 + (b + 2) % 3;
|
|
200
|
+
const br = b0 + (b + 1) % 3;
|
|
201
|
+
const p0 = this.triangles[ar];
|
|
202
|
+
const pr = this.triangles[a];
|
|
203
|
+
const pl = this.triangles[al];
|
|
204
|
+
const p1 = this.triangles[bl];
|
|
205
|
+
const coords = this.coords;
|
|
206
|
+
if (!inCircle(coords[2 * p0], coords[2 * p0 + 1], coords[2 * pr], coords[2 * pr + 1], coords[2 * pl], coords[2 * pl + 1], coords[2 * p1], coords[2 * p1 + 1])) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
const hal = this._halfedges[al];
|
|
210
|
+
const har = this._halfedges[ar];
|
|
211
|
+
const hbl = this._halfedges[bl];
|
|
212
|
+
const hbr = this._halfedges[br];
|
|
213
|
+
this._queueRemove(a0 / 3);
|
|
214
|
+
this._queueRemove(b0 / 3);
|
|
215
|
+
const t0 = this._addTriangle(p0, p1, pl, -1, hbl, hal, a0);
|
|
216
|
+
const t1 = this._addTriangle(p1, p0, pr, t0, har, hbr, b0);
|
|
217
|
+
this._legalize(t0 + 1);
|
|
218
|
+
this._legalize(t1 + 2);
|
|
219
|
+
}
|
|
220
|
+
_handleCollinear(pn, a) {
|
|
221
|
+
const a0 = a - a % 3;
|
|
222
|
+
const al = a0 + (a + 1) % 3;
|
|
223
|
+
const ar = a0 + (a + 2) % 3;
|
|
224
|
+
const p0 = this.triangles[ar];
|
|
225
|
+
const pr = this.triangles[a];
|
|
226
|
+
const pl = this.triangles[al];
|
|
227
|
+
const hal = this._halfedges[al];
|
|
228
|
+
const har = this._halfedges[ar];
|
|
229
|
+
const b = this._halfedges[a];
|
|
230
|
+
if (b < 0) {
|
|
231
|
+
const t0 = this._addTriangle(pn, p0, pr, -1, har, -1, a0);
|
|
232
|
+
const t1 = this._addTriangle(p0, pn, pl, t0, -1, hal);
|
|
233
|
+
this._legalize(t0 + 1);
|
|
234
|
+
this._legalize(t1 + 2);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const b0 = b - b % 3;
|
|
238
|
+
const bl = b0 + (b + 2) % 3;
|
|
239
|
+
const br = b0 + (b + 1) % 3;
|
|
240
|
+
const p1 = this.triangles[bl];
|
|
241
|
+
const hbl = this._halfedges[bl];
|
|
242
|
+
const hbr = this._halfedges[br];
|
|
243
|
+
this._queueRemove(b0 / 3);
|
|
244
|
+
const t0 = this._addTriangle(p0, pr, pn, har, -1, -1, a0);
|
|
245
|
+
const t1 = this._addTriangle(pr, p1, pn, hbr, -1, t0 + 1, b0);
|
|
246
|
+
const t2 = this._addTriangle(p1, pl, pn, hbl, -1, t1 + 1);
|
|
247
|
+
const t3 = this._addTriangle(pl, p0, pn, hal, t0 + 2, t2 + 1);
|
|
248
|
+
this._legalize(t0);
|
|
249
|
+
this._legalize(t1);
|
|
250
|
+
this._legalize(t2);
|
|
251
|
+
this._legalize(t3);
|
|
252
|
+
}
|
|
253
|
+
_queuePush(t, error, rms) {
|
|
254
|
+
const i = this._queue.length;
|
|
255
|
+
this._queueIndices[t] = i;
|
|
256
|
+
this._queue.push(t);
|
|
257
|
+
this._errors.push(error);
|
|
258
|
+
this._rmsSum += rms;
|
|
259
|
+
this._queueUp(i);
|
|
260
|
+
}
|
|
261
|
+
_queuePop() {
|
|
262
|
+
const n = this._queue.length - 1;
|
|
263
|
+
this._queueSwap(0, n);
|
|
264
|
+
this._queueDown(0, n);
|
|
265
|
+
return this._queuePopBack();
|
|
266
|
+
}
|
|
267
|
+
_queuePopBack() {
|
|
268
|
+
const t = this._queue.pop();
|
|
269
|
+
this._errors.pop();
|
|
270
|
+
this._rmsSum -= this._rms[t];
|
|
271
|
+
this._queueIndices[t] = -1;
|
|
272
|
+
return t;
|
|
273
|
+
}
|
|
274
|
+
_queueRemove(t) {
|
|
275
|
+
const i = this._queueIndices[t];
|
|
276
|
+
if (i < 0) {
|
|
277
|
+
const it = this._pending.indexOf(t);
|
|
278
|
+
if (it !== -1) {
|
|
279
|
+
this._pending[it] = this._pending[--this._pendingLen];
|
|
280
|
+
} else {
|
|
281
|
+
throw new Error('Broken triangulation (something went wrong).');
|
|
282
|
+
}
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const n = this._queue.length - 1;
|
|
286
|
+
if (n !== i) {
|
|
287
|
+
this._queueSwap(i, n);
|
|
288
|
+
if (!this._queueDown(i, n)) {
|
|
289
|
+
this._queueUp(i);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
this._queuePopBack();
|
|
293
|
+
}
|
|
294
|
+
_queueLess(i, j) {
|
|
295
|
+
return this._errors[i] > this._errors[j];
|
|
296
|
+
}
|
|
297
|
+
_queueSwap(i, j) {
|
|
298
|
+
const pi = this._queue[i];
|
|
299
|
+
const pj = this._queue[j];
|
|
300
|
+
this._queue[i] = pj;
|
|
301
|
+
this._queue[j] = pi;
|
|
302
|
+
this._queueIndices[pi] = j;
|
|
303
|
+
this._queueIndices[pj] = i;
|
|
304
|
+
const e = this._errors[i];
|
|
305
|
+
this._errors[i] = this._errors[j];
|
|
306
|
+
this._errors[j] = e;
|
|
307
|
+
}
|
|
308
|
+
_queueUp(j0) {
|
|
309
|
+
let j = j0;
|
|
310
|
+
while (true) {
|
|
311
|
+
const i = j - 1 >> 1;
|
|
312
|
+
if (i === j || !this._queueLess(j, i)) {
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
this._queueSwap(i, j);
|
|
316
|
+
j = i;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
_queueDown(i0, n) {
|
|
320
|
+
let i = i0;
|
|
321
|
+
while (true) {
|
|
322
|
+
const j1 = 2 * i + 1;
|
|
323
|
+
if (j1 >= n || j1 < 0) {
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
const j2 = j1 + 1;
|
|
327
|
+
let j = j1;
|
|
328
|
+
if (j2 < n && this._queueLess(j2, j1)) {
|
|
329
|
+
j = j2;
|
|
330
|
+
}
|
|
331
|
+
if (!this._queueLess(j, i)) {
|
|
332
|
+
break;
|
|
333
|
+
}
|
|
334
|
+
this._queueSwap(i, j);
|
|
335
|
+
i = j;
|
|
336
|
+
}
|
|
337
|
+
return i > i0;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function orient(ax, ay, bx, by, cx, cy) {
|
|
341
|
+
return (bx - cx) * (ay - cy) - (by - cy) * (ax - cx);
|
|
342
|
+
}
|
|
343
|
+
function inCircle(ax, ay, bx, by, cx, cy, px, py) {
|
|
344
|
+
const dx = ax - px;
|
|
345
|
+
const dy = ay - py;
|
|
346
|
+
const ex = bx - px;
|
|
347
|
+
const ey = by - py;
|
|
348
|
+
const fx = cx - px;
|
|
349
|
+
const fy = cy - py;
|
|
350
|
+
const ap = dx * dx + dy * dy;
|
|
351
|
+
const bp = ex * ex + ey * ey;
|
|
352
|
+
const cp = fx * fx + fy * fy;
|
|
353
|
+
return dx * (ey * cp - bp * fy) - dy * (ex * cp - bp * fx) + ap * (ex * fy - ey * fx) < 0;
|
|
354
|
+
}
|
|
355
|
+
//# sourceMappingURL=index.js.map
|