-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathCorridorOutlineGeometry.js
405 lines (374 loc) · 15.3 KB
/
CorridorOutlineGeometry.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
/*global define*/
define([
'./BoundingSphere',
'./Cartesian3',
'./ComponentDatatype',
'./CornerType',
'./CorridorGeometryLibrary',
'./defaultValue',
'./defined',
'./DeveloperError',
'./Ellipsoid',
'./Geometry',
'./GeometryAttribute',
'./GeometryAttributes',
'./IndexDatatype',
'./Math',
'./PolylinePipeline',
'./PrimitiveType'
], function(
BoundingSphere,
Cartesian3,
ComponentDatatype,
CornerType,
CorridorGeometryLibrary,
defaultValue,
defined,
DeveloperError,
Ellipsoid,
Geometry,
GeometryAttribute,
GeometryAttributes,
IndexDatatype,
CesiumMath,
PolylinePipeline,
PrimitiveType) {
"use strict";
var cartesian1 = new Cartesian3();
var cartesian2 = new Cartesian3();
var cartesian3 = new Cartesian3();
function combine(computedPositions, cornerType) {
var wallIndices = [];
var positions = computedPositions.positions;
var corners = computedPositions.corners;
var endPositions = computedPositions.endPositions;
var attributes = new GeometryAttributes();
var corner;
var leftCount = 0;
var rightCount = 0;
var i;
var indicesLength = 0;
var length;
for (i = 0; i < positions.length; i += 2) {
length = positions[i].length - 3;
leftCount += length; //subtracting 3 to account for duplicate points at corners
indicesLength += length / 3 * 4;
rightCount += positions[i + 1].length - 3;
}
leftCount += 3; //add back count for end positions
rightCount += 3;
for (i = 0; i < corners.length; i++) {
corner = corners[i];
var leftSide = corners[i].leftPositions;
if (defined(leftSide)) {
length = leftSide.length;
leftCount += length;
indicesLength += length / 3 * 2;
} else {
length = corners[i].rightPositions.length;
rightCount += length;
indicesLength += length / 3 * 2;
}
}
var addEndPositions = defined(endPositions);
var endPositionLength;
if (addEndPositions) {
endPositionLength = endPositions[0].length - 3;
leftCount += endPositionLength;
rightCount += endPositionLength;
endPositionLength /= 3;
indicesLength += endPositionLength * 4;
}
var size = leftCount + rightCount;
var finalPositions = new Float64Array(size);
var front = 0;
var back = size - 1;
var UL, LL, UR, LR;
var rightPos, leftPos;
var halfLength = endPositionLength / 2;
var indices = IndexDatatype.createTypedArray(size / 3, indicesLength + 4);
var index = 0;
indices[index++] = front / 3;
indices[index++] = (back - 2) / 3;
if (addEndPositions) { // add rounded end
wallIndices.push(front / 3);
leftPos = cartesian1;
rightPos = cartesian2;
var firstEndPositions = endPositions[0];
for (i = 0; i < halfLength; i++) {
leftPos = Cartesian3.fromArray(firstEndPositions, (halfLength - 1 - i) * 3, leftPos);
rightPos = Cartesian3.fromArray(firstEndPositions, (halfLength + i) * 3, rightPos);
CorridorGeometryLibrary.addAttribute(finalPositions, rightPos, front);
CorridorGeometryLibrary.addAttribute(finalPositions, leftPos, undefined, back);
LL = front / 3;
LR = LL + 1;
UL = (back - 2) / 3;
UR = UL - 1;
indices[index++] = UL;
indices[index++] = UR;
indices[index++] = LL;
indices[index++] = LR;
front += 3;
back -= 3;
}
}
var posIndex = 0;
var rightEdge = positions[posIndex++]; //add first two edges
var leftEdge = positions[posIndex++];
finalPositions.set(rightEdge, front);
finalPositions.set(leftEdge, back - leftEdge.length + 1);
length = leftEdge.length - 3;
wallIndices.push(front / 3, (back - 2) / 3);
for (i = 0; i < length; i += 3) {
LL = front / 3;
LR = LL + 1;
UL = (back - 2) / 3;
UR = UL - 1;
indices[index++] = UL;
indices[index++] = UR;
indices[index++] = LL;
indices[index++] = LR;
front += 3;
back -= 3;
}
for (i = 0; i < corners.length; i++) {
var j;
corner = corners[i];
var l = corner.leftPositions;
var r = corner.rightPositions;
var start;
var outsidePoint = cartesian3;
if (defined(l)) {
back -= 3;
start = UR;
wallIndices.push(LR);
for (j = 0; j < l.length / 3; j++) {
outsidePoint = Cartesian3.fromArray(l, j * 3, outsidePoint);
indices[index++] = start - j - 1;
indices[index++] = start - j;
CorridorGeometryLibrary.addAttribute(finalPositions, outsidePoint, undefined, back);
back -= 3;
}
wallIndices.push(start - Math.floor(l.length / 6));
if (cornerType === CornerType.BEVELED) {
wallIndices.push((back - 2) / 3 + 1);
}
front += 3;
} else {
front += 3;
start = LR;
wallIndices.push(UR);
for (j = 0; j < r.length / 3; j++) {
outsidePoint = Cartesian3.fromArray(r, j * 3, outsidePoint);
indices[index++] = start + j;
indices[index++] = start + j + 1;
CorridorGeometryLibrary.addAttribute(finalPositions, outsidePoint, front);
front += 3;
}
wallIndices.push(start + Math.floor(r.length / 6));
if (cornerType === CornerType.BEVELED) {
wallIndices.push(front / 3 - 1);
}
back -= 3;
}
rightEdge = positions[posIndex++];
leftEdge = positions[posIndex++];
rightEdge.splice(0, 3); //remove duplicate points added by corner
leftEdge.splice(leftEdge.length - 3, 3);
finalPositions.set(rightEdge, front);
finalPositions.set(leftEdge, back - leftEdge.length + 1);
length = leftEdge.length - 3;
for (j = 0; j < leftEdge.length; j += 3) {
LR = front / 3;
LL = LR - 1;
UR = (back - 2) / 3;
UL = UR + 1;
indices[index++] = UL;
indices[index++] = UR;
indices[index++] = LL;
indices[index++] = LR;
front += 3;
back -= 3;
}
front -= 3;
back += 3;
wallIndices.push(front / 3, (back - 2) / 3);
}
if (addEndPositions) { // add rounded end
front += 3;
back -= 3;
leftPos = cartesian1;
rightPos = cartesian2;
var lastEndPositions = endPositions[1];
for (i = 0; i < halfLength; i++) {
leftPos = Cartesian3.fromArray(lastEndPositions, (endPositionLength - i - 1) * 3, leftPos);
rightPos = Cartesian3.fromArray(lastEndPositions, i * 3, rightPos);
CorridorGeometryLibrary.addAttribute(finalPositions, leftPos, undefined, back);
CorridorGeometryLibrary.addAttribute(finalPositions, rightPos, front);
LR = front / 3;
LL = LR - 1;
UR = (back - 2) / 3;
UL = UR + 1;
indices[index++] = UL;
indices[index++] = UR;
indices[index++] = LL;
indices[index++] = LR;
front += 3;
back -= 3;
}
wallIndices.push(front / 3);
} else {
wallIndices.push(front / 3, (back - 2) / 3);
}
indices[index++] = front / 3;
indices[index++] = (back - 2) / 3;
attributes.position = new GeometryAttribute({
componentDatatype : ComponentDatatype.DOUBLE,
componentsPerAttribute : 3,
values : finalPositions
});
return {
attributes : attributes,
indices : indices,
wallIndices : wallIndices
};
}
function computePositionsExtruded(params) {
var ellipsoid = params.ellipsoid;
var computedPositions = CorridorGeometryLibrary.computePositions(params);
var attr = combine(computedPositions, params.cornerType);
var wallIndices = attr.wallIndices;
var height = params.height;
var extrudedHeight = params.extrudedHeight;
var attributes = attr.attributes;
var indices = attr.indices;
var positions = attributes.position.values;
var length = positions.length;
var extrudedPositions = new Float64Array(length);
extrudedPositions.set(positions);
var newPositions = new Float64Array(length * 2);
positions = PolylinePipeline.scaleToGeodeticHeight(positions, height, ellipsoid, positions);
extrudedPositions = PolylinePipeline.scaleToGeodeticHeight(extrudedPositions, extrudedHeight, ellipsoid, extrudedPositions);
newPositions.set(positions);
newPositions.set(extrudedPositions, length);
attributes.position.values = newPositions;
length /= 3;
var i;
var iLength = indices.length;
var newIndices = IndexDatatype.createTypedArray(newPositions.length / 3, (iLength + wallIndices.length) * 2);
newIndices.set(indices);
var index = iLength;
for (i = 0; i < iLength; i += 2) { // bottom indices
var v0 = indices[i];
var v1 = indices[i + 1];
newIndices[index++] = v0 + length;
newIndices[index++] = v1 + length;
}
var UL, LL;
for (i = 0; i < wallIndices.length; i++) { //wall indices
UL = wallIndices[i];
LL = UL + length;
newIndices[index++] = UL;
newIndices[index++] = LL;
}
return {
attributes : attributes,
indices : newIndices
};
}
/**
* A description of a corridor outline.
*
* @alias CorridorOutlineGeometry
* @constructor
*
* @param {Object} options Object with the following properties:
* @param {Cartesian3[]} options.positions An array of positions that define the center of the corridor outline.
* @param {Number} options.width The distance between the edges of the corridor outline.
* @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
* @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
* @param {Number} [options.height=0] The distance between the ellipsoid surface and the positions.
* @param {Number} [options.extrudedHeight] The distance between the ellipsoid surface and the extrusion.
* @param {CornerType} [options.cornerType=CornerType.ROUNDED] Determines the style of the corners.
*
* @see CorridorOutlineGeometry.createGeometry
*
* @example
* var corridor = new Cesium.CorridorOutlineGeometry({
* positions : Cesium.Cartesian3.fromDegreesArray([-72.0, 40.0, -70.0, 35.0]),
* width : 100000
* });
*/
var CorridorOutlineGeometry = function(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
var positions = options.positions;
var width = options.width;
//>>includeStart('debug', pragmas.debug);
if (!defined(positions)) {
throw new DeveloperError('options.positions is required.');
}
if (!defined(width)) {
throw new DeveloperError('options.width is required.');
}
//>>includeEnd('debug');
this._positions = positions;
this._width = width;
this._ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
this._height = defaultValue(options.height, 0);
this._extrudedHeight = defaultValue(options.extrudedHeight, this._height);
this._cornerType = defaultValue(options.cornerType, CornerType.ROUNDED);
this._granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
this._workerName = 'createCorridorOutlineGeometry';
};
/**
* Computes the geometric representation of a corridor, including its vertices, indices, and a bounding sphere.
*
* @param {CorridorOutlineGeometry} corridorOutlineGeometry A description of the corridor.
* @returns {Geometry} The computed vertices and indices.
*
* @exception {DeveloperError} Count of unique positions must be greater than 1.
*/
CorridorOutlineGeometry.createGeometry = function(corridorOutlineGeometry) {
var positions = corridorOutlineGeometry._positions;
var height = corridorOutlineGeometry._height;
var extrudedHeight = corridorOutlineGeometry._extrudedHeight;
var extrude = (height !== extrudedHeight);
var cleanPositions = PolylinePipeline.removeDuplicates(positions);
//>>includeStart('debug', pragmas.debug);
if (cleanPositions.length < 2) {
throw new DeveloperError('Count of unique positions must be greater than 1.');
}
//>>includeEnd('debug');
var ellipsoid = corridorOutlineGeometry._ellipsoid;
var params = {
ellipsoid : ellipsoid,
positions : cleanPositions,
width : corridorOutlineGeometry._width,
cornerType : corridorOutlineGeometry._cornerType,
granularity : corridorOutlineGeometry._granularity,
saveAttributes : false
};
var attr;
if (extrude) {
var h = Math.max(height, extrudedHeight);
extrudedHeight = Math.min(height, extrudedHeight);
height = h;
params.height = height;
params.extrudedHeight = extrudedHeight;
attr = computePositionsExtruded(params);
} else {
var computedPositions = CorridorGeometryLibrary.computePositions(params);
attr = combine(computedPositions, params.cornerType);
attr.attributes.position.values = PolylinePipeline.scaleToGeodeticHeight(attr.attributes.position.values, height, ellipsoid, attr.attributes.position.values);
}
var attributes = attr.attributes;
var boundingSphere = BoundingSphere.fromVertices(attributes.position.values, undefined, 3);
return new Geometry({
attributes : attributes,
indices : attr.indices,
primitiveType : PrimitiveType.LINES,
boundingSphere : boundingSphere
});
};
return CorridorOutlineGeometry;
});