forked from CesiumGS/cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CorridorGeometryLibrary.js
501 lines (470 loc) · 14 KB
/
CorridorGeometryLibrary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import Cartesian3 from "./Cartesian3.js";
import CornerType from "./CornerType.js";
import defined from "./defined.js";
import CesiumMath from "./Math.js";
import Matrix3 from "./Matrix3.js";
import PolylinePipeline from "./PolylinePipeline.js";
import PolylineVolumeGeometryLibrary from "./PolylineVolumeGeometryLibrary.js";
import Quaternion from "./Quaternion.js";
/**
* @private
*/
const CorridorGeometryLibrary = {};
const scratch1 = new Cartesian3();
const scratch2 = new Cartesian3();
const scratch3 = new Cartesian3();
const scratch4 = new Cartesian3();
const scaleArray2 = [new Cartesian3(), new Cartesian3()];
const cartesian1 = new Cartesian3();
const cartesian2 = new Cartesian3();
const cartesian3 = new Cartesian3();
const cartesian4 = new Cartesian3();
const cartesian5 = new Cartesian3();
const cartesian6 = new Cartesian3();
const cartesian7 = new Cartesian3();
const cartesian8 = new Cartesian3();
const cartesian9 = new Cartesian3();
const cartesian10 = new Cartesian3();
const quaterion = new Quaternion();
const rotMatrix = new Matrix3();
function computeRoundCorner(
cornerPoint,
startPoint,
endPoint,
cornerType,
leftIsOutside
) {
const angle = Cartesian3.angleBetween(
Cartesian3.subtract(startPoint, cornerPoint, scratch1),
Cartesian3.subtract(endPoint, cornerPoint, scratch2)
);
const granularity =
cornerType === CornerType.BEVELED
? 1
: Math.ceil(angle / CesiumMath.toRadians(5)) + 1;
const size = granularity * 3;
const array = new Array(size);
array[size - 3] = endPoint.x;
array[size - 2] = endPoint.y;
array[size - 1] = endPoint.z;
let m;
if (leftIsOutside) {
m = Matrix3.fromQuaternion(
Quaternion.fromAxisAngle(
Cartesian3.negate(cornerPoint, scratch1),
angle / granularity,
quaterion
),
rotMatrix
);
} else {
m = Matrix3.fromQuaternion(
Quaternion.fromAxisAngle(cornerPoint, angle / granularity, quaterion),
rotMatrix
);
}
let index = 0;
startPoint = Cartesian3.clone(startPoint, scratch1);
for (let i = 0; i < granularity; i++) {
startPoint = Matrix3.multiplyByVector(m, startPoint, startPoint);
array[index++] = startPoint.x;
array[index++] = startPoint.y;
array[index++] = startPoint.z;
}
return array;
}
function addEndCaps(calculatedPositions) {
let cornerPoint = cartesian1;
let startPoint = cartesian2;
let endPoint = cartesian3;
let leftEdge = calculatedPositions[1];
startPoint = Cartesian3.fromArray(
calculatedPositions[1],
leftEdge.length - 3,
startPoint
);
endPoint = Cartesian3.fromArray(calculatedPositions[0], 0, endPoint);
cornerPoint = Cartesian3.midpoint(startPoint, endPoint, cornerPoint);
const firstEndCap = computeRoundCorner(
cornerPoint,
startPoint,
endPoint,
CornerType.ROUNDED,
false
);
const length = calculatedPositions.length - 1;
const rightEdge = calculatedPositions[length - 1];
leftEdge = calculatedPositions[length];
startPoint = Cartesian3.fromArray(
rightEdge,
rightEdge.length - 3,
startPoint
);
endPoint = Cartesian3.fromArray(leftEdge, 0, endPoint);
cornerPoint = Cartesian3.midpoint(startPoint, endPoint, cornerPoint);
const lastEndCap = computeRoundCorner(
cornerPoint,
startPoint,
endPoint,
CornerType.ROUNDED,
false
);
return [firstEndCap, lastEndCap];
}
function computeMiteredCorner(
position,
leftCornerDirection,
lastPoint,
leftIsOutside
) {
let cornerPoint = scratch1;
if (leftIsOutside) {
cornerPoint = Cartesian3.add(position, leftCornerDirection, cornerPoint);
} else {
leftCornerDirection = Cartesian3.negate(
leftCornerDirection,
leftCornerDirection
);
cornerPoint = Cartesian3.add(position, leftCornerDirection, cornerPoint);
}
return [
cornerPoint.x,
cornerPoint.y,
cornerPoint.z,
lastPoint.x,
lastPoint.y,
lastPoint.z,
];
}
function addShiftedPositions(positions, left, scalar, calculatedPositions) {
const rightPositions = new Array(positions.length);
const leftPositions = new Array(positions.length);
const scaledLeft = Cartesian3.multiplyByScalar(left, scalar, scratch1);
const scaledRight = Cartesian3.negate(scaledLeft, scratch2);
let rightIndex = 0;
let leftIndex = positions.length - 1;
for (let i = 0; i < positions.length; i += 3) {
const pos = Cartesian3.fromArray(positions, i, scratch3);
const rightPos = Cartesian3.add(pos, scaledRight, scratch4);
rightPositions[rightIndex++] = rightPos.x;
rightPositions[rightIndex++] = rightPos.y;
rightPositions[rightIndex++] = rightPos.z;
const leftPos = Cartesian3.add(pos, scaledLeft, scratch4);
leftPositions[leftIndex--] = leftPos.z;
leftPositions[leftIndex--] = leftPos.y;
leftPositions[leftIndex--] = leftPos.x;
}
calculatedPositions.push(rightPositions, leftPositions);
return calculatedPositions;
}
/**
* @private
*/
CorridorGeometryLibrary.addAttribute = function (
attribute,
value,
front,
back
) {
const x = value.x;
const y = value.y;
const z = value.z;
if (defined(front)) {
attribute[front] = x;
attribute[front + 1] = y;
attribute[front + 2] = z;
}
if (defined(back)) {
attribute[back] = z;
attribute[back - 1] = y;
attribute[back - 2] = x;
}
};
const scratchForwardProjection = new Cartesian3();
const scratchBackwardProjection = new Cartesian3();
/**
* @private
*/
CorridorGeometryLibrary.computePositions = function (params) {
const granularity = params.granularity;
const positions = params.positions;
const ellipsoid = params.ellipsoid;
const width = params.width / 2;
const cornerType = params.cornerType;
const saveAttributes = params.saveAttributes;
let normal = cartesian1;
let forward = cartesian2;
let backward = cartesian3;
let left = cartesian4;
let cornerDirection = cartesian5;
let startPoint = cartesian6;
let previousPos = cartesian7;
let rightPos = cartesian8;
let leftPos = cartesian9;
let center = cartesian10;
let calculatedPositions = [];
const calculatedLefts = saveAttributes ? [] : undefined;
const calculatedNormals = saveAttributes ? [] : undefined;
let position = positions[0]; //add first point
let nextPosition = positions[1];
forward = Cartesian3.normalize(
Cartesian3.subtract(nextPosition, position, forward),
forward
);
normal = ellipsoid.geodeticSurfaceNormal(position, normal);
left = Cartesian3.normalize(Cartesian3.cross(normal, forward, left), left);
if (saveAttributes) {
calculatedLefts.push(left.x, left.y, left.z);
calculatedNormals.push(normal.x, normal.y, normal.z);
}
previousPos = Cartesian3.clone(position, previousPos);
position = nextPosition;
backward = Cartesian3.negate(forward, backward);
let subdividedPositions;
const corners = [];
let i;
const length = positions.length;
for (i = 1; i < length - 1; i++) {
// add middle points and corners
normal = ellipsoid.geodeticSurfaceNormal(position, normal);
nextPosition = positions[i + 1];
forward = Cartesian3.normalize(
Cartesian3.subtract(nextPosition, position, forward),
forward
);
cornerDirection = Cartesian3.normalize(
Cartesian3.add(forward, backward, cornerDirection),
cornerDirection
);
const forwardProjection = Cartesian3.multiplyByScalar(
normal,
Cartesian3.dot(forward, normal),
scratchForwardProjection
);
Cartesian3.subtract(forward, forwardProjection, forwardProjection);
Cartesian3.normalize(forwardProjection, forwardProjection);
const backwardProjection = Cartesian3.multiplyByScalar(
normal,
Cartesian3.dot(backward, normal),
scratchBackwardProjection
);
Cartesian3.subtract(backward, backwardProjection, backwardProjection);
Cartesian3.normalize(backwardProjection, backwardProjection);
const doCorner = !CesiumMath.equalsEpsilon(
Math.abs(Cartesian3.dot(forwardProjection, backwardProjection)),
1.0,
CesiumMath.EPSILON7
);
if (doCorner) {
cornerDirection = Cartesian3.cross(
cornerDirection,
normal,
cornerDirection
);
cornerDirection = Cartesian3.cross(
normal,
cornerDirection,
cornerDirection
);
cornerDirection = Cartesian3.normalize(cornerDirection, cornerDirection);
const scalar =
width /
Math.max(
0.25,
Cartesian3.magnitude(
Cartesian3.cross(cornerDirection, backward, scratch1)
)
);
const leftIsOutside = PolylineVolumeGeometryLibrary.angleIsGreaterThanPi(
forward,
backward,
position,
ellipsoid
);
cornerDirection = Cartesian3.multiplyByScalar(
cornerDirection,
scalar,
cornerDirection
);
if (leftIsOutside) {
rightPos = Cartesian3.add(position, cornerDirection, rightPos);
center = Cartesian3.add(
rightPos,
Cartesian3.multiplyByScalar(left, width, center),
center
);
leftPos = Cartesian3.add(
rightPos,
Cartesian3.multiplyByScalar(left, width * 2, leftPos),
leftPos
);
scaleArray2[0] = Cartesian3.clone(previousPos, scaleArray2[0]);
scaleArray2[1] = Cartesian3.clone(center, scaleArray2[1]);
subdividedPositions = PolylinePipeline.generateArc({
positions: scaleArray2,
granularity: granularity,
ellipsoid: ellipsoid,
});
calculatedPositions = addShiftedPositions(
subdividedPositions,
left,
width,
calculatedPositions
);
if (saveAttributes) {
calculatedLefts.push(left.x, left.y, left.z);
calculatedNormals.push(normal.x, normal.y, normal.z);
}
startPoint = Cartesian3.clone(leftPos, startPoint);
left = Cartesian3.normalize(
Cartesian3.cross(normal, forward, left),
left
);
leftPos = Cartesian3.add(
rightPos,
Cartesian3.multiplyByScalar(left, width * 2, leftPos),
leftPos
);
previousPos = Cartesian3.add(
rightPos,
Cartesian3.multiplyByScalar(left, width, previousPos),
previousPos
);
if (
cornerType === CornerType.ROUNDED ||
cornerType === CornerType.BEVELED
) {
corners.push({
leftPositions: computeRoundCorner(
rightPos,
startPoint,
leftPos,
cornerType,
leftIsOutside
),
});
} else {
corners.push({
leftPositions: computeMiteredCorner(
position,
Cartesian3.negate(cornerDirection, cornerDirection),
leftPos,
leftIsOutside
),
});
}
} else {
leftPos = Cartesian3.add(position, cornerDirection, leftPos);
center = Cartesian3.add(
leftPos,
Cartesian3.negate(
Cartesian3.multiplyByScalar(left, width, center),
center
),
center
);
rightPos = Cartesian3.add(
leftPos,
Cartesian3.negate(
Cartesian3.multiplyByScalar(left, width * 2, rightPos),
rightPos
),
rightPos
);
scaleArray2[0] = Cartesian3.clone(previousPos, scaleArray2[0]);
scaleArray2[1] = Cartesian3.clone(center, scaleArray2[1]);
subdividedPositions = PolylinePipeline.generateArc({
positions: scaleArray2,
granularity: granularity,
ellipsoid: ellipsoid,
});
calculatedPositions = addShiftedPositions(
subdividedPositions,
left,
width,
calculatedPositions
);
if (saveAttributes) {
calculatedLefts.push(left.x, left.y, left.z);
calculatedNormals.push(normal.x, normal.y, normal.z);
}
startPoint = Cartesian3.clone(rightPos, startPoint);
left = Cartesian3.normalize(
Cartesian3.cross(normal, forward, left),
left
);
rightPos = Cartesian3.add(
leftPos,
Cartesian3.negate(
Cartesian3.multiplyByScalar(left, width * 2, rightPos),
rightPos
),
rightPos
);
previousPos = Cartesian3.add(
leftPos,
Cartesian3.negate(
Cartesian3.multiplyByScalar(left, width, previousPos),
previousPos
),
previousPos
);
if (
cornerType === CornerType.ROUNDED ||
cornerType === CornerType.BEVELED
) {
corners.push({
rightPositions: computeRoundCorner(
leftPos,
startPoint,
rightPos,
cornerType,
leftIsOutside
),
});
} else {
corners.push({
rightPositions: computeMiteredCorner(
position,
cornerDirection,
rightPos,
leftIsOutside
),
});
}
}
backward = Cartesian3.negate(forward, backward);
}
position = nextPosition;
}
normal = ellipsoid.geodeticSurfaceNormal(position, normal);
scaleArray2[0] = Cartesian3.clone(previousPos, scaleArray2[0]);
scaleArray2[1] = Cartesian3.clone(position, scaleArray2[1]);
subdividedPositions = PolylinePipeline.generateArc({
positions: scaleArray2,
granularity: granularity,
ellipsoid: ellipsoid,
});
calculatedPositions = addShiftedPositions(
subdividedPositions,
left,
width,
calculatedPositions
);
if (saveAttributes) {
calculatedLefts.push(left.x, left.y, left.z);
calculatedNormals.push(normal.x, normal.y, normal.z);
}
let endPositions;
if (cornerType === CornerType.ROUNDED) {
endPositions = addEndCaps(calculatedPositions);
}
return {
positions: calculatedPositions,
corners: corners,
lefts: calculatedLefts,
normals: calculatedNormals,
endPositions: endPositions,
};
};
export default CorridorGeometryLibrary;