/
Polyline.js
431 lines (391 loc) · 12.3 KB
/
Polyline.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
import arrayRemoveDuplicates from "../Core/arrayRemoveDuplicates.js";
import BoundingSphere from "../Core/BoundingSphere.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Color from "../Core/Color.js";
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
import DeveloperError from "../Core/DeveloperError.js";
import DistanceDisplayCondition from "../Core/DistanceDisplayCondition.js";
import Matrix4 from "../Core/Matrix4.js";
import PolylinePipeline from "../Core/PolylinePipeline.js";
import Material from "./Material.js";
/**
* <div class="notice">
* Create this by calling {@link PolylineCollection#add}. Do not call the constructor directly.
* </div>
*
* A renderable polyline.
*
* @alias Polyline
* @internalConstructor
* @class
*
* @privateParam {Object} options Object with the following properties:
* @privateParam {Boolean} [options.show=true] <code>true</code> if this polyline will be shown; otherwise, <code>false</code>.
* @privateParam {Number} [options.width=1.0] The width of the polyline in pixels.
* @privateParam {Boolean} [options.loop=false] Whether a line segment will be added between the last and first line positions to make this line a loop.
* @privateParam {Material} [options.material=Material.ColorType] The material.
* @privateParam {Cartesian3[]} [options.positions] The positions.
* @privateParam {Object} [options.id] The user-defined object to be returned when this polyline is picked.
* @privateParam {DistanceDisplayCondition} [options.distanceDisplayCondition] The condition specifying at what distance from the camera that this polyline will be displayed.
* @privateParam {PolylineCollection} polylineCollection The renderable polyline collection.
*
* @see PolylineCollection
*
*/
function Polyline(options, polylineCollection) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
this._show = defaultValue(options.show, true);
this._width = defaultValue(options.width, 1.0);
this._loop = defaultValue(options.loop, false);
this._distanceDisplayCondition = options.distanceDisplayCondition;
this._material = options.material;
if (!defined(this._material)) {
this._material = Material.fromType(Material.ColorType, {
color: new Color(1.0, 1.0, 1.0, 1.0),
});
}
let positions = options.positions;
if (!defined(positions)) {
positions = [];
}
this._positions = positions;
this._actualPositions = arrayRemoveDuplicates(
positions,
Cartesian3.equalsEpsilon
);
if (this._loop && this._actualPositions.length > 2) {
if (this._actualPositions === this._positions) {
this._actualPositions = positions.slice();
}
this._actualPositions.push(Cartesian3.clone(this._actualPositions[0]));
}
this._length = this._actualPositions.length;
this._id = options.id;
let modelMatrix;
if (defined(polylineCollection)) {
modelMatrix = Matrix4.clone(polylineCollection.modelMatrix);
}
this._modelMatrix = modelMatrix;
this._segments = PolylinePipeline.wrapLongitude(
this._actualPositions,
modelMatrix
);
this._actualLength = undefined;
// eslint-disable-next-line no-use-before-define
this._propertiesChanged = new Uint32Array(NUMBER_OF_PROPERTIES);
this._polylineCollection = polylineCollection;
this._dirty = false;
this._pickId = undefined;
this._boundingVolume = BoundingSphere.fromPoints(this._actualPositions);
this._boundingVolumeWC = BoundingSphere.transform(
this._boundingVolume,
this._modelMatrix
);
this._boundingVolume2D = new BoundingSphere(); // modified in PolylineCollection
}
const POSITION_INDEX = (Polyline.POSITION_INDEX = 0);
const SHOW_INDEX = (Polyline.SHOW_INDEX = 1);
const WIDTH_INDEX = (Polyline.WIDTH_INDEX = 2);
const MATERIAL_INDEX = (Polyline.MATERIAL_INDEX = 3);
const POSITION_SIZE_INDEX = (Polyline.POSITION_SIZE_INDEX = 4);
const DISTANCE_DISPLAY_CONDITION = (Polyline.DISTANCE_DISPLAY_CONDITION = 5);
const NUMBER_OF_PROPERTIES = (Polyline.NUMBER_OF_PROPERTIES = 6);
function makeDirty(polyline, propertyChanged) {
++polyline._propertiesChanged[propertyChanged];
const polylineCollection = polyline._polylineCollection;
if (defined(polylineCollection)) {
polylineCollection._updatePolyline(polyline, propertyChanged);
polyline._dirty = true;
}
}
Object.defineProperties(Polyline.prototype, {
/**
* Determines if this polyline will be shown. Use this to hide or show a polyline, instead
* of removing it and re-adding it to the collection.
* @memberof Polyline.prototype
* @type {Boolean}
*/
show: {
get: function () {
return this._show;
},
set: function (value) {
//>>includeStart('debug', pragmas.debug);
if (!defined(value)) {
throw new DeveloperError("value is required.");
}
//>>includeEnd('debug');
if (value !== this._show) {
this._show = value;
makeDirty(this, SHOW_INDEX);
}
},
},
/**
* Gets or sets the positions of the polyline.
* @memberof Polyline.prototype
* @type {Cartesian3[]}
* @example
* polyline.positions = Cesium.Cartesian3.fromDegreesArray([
* 0.0, 0.0,
* 10.0, 0.0,
* 0.0, 20.0
* ]);
*/
positions: {
get: function () {
return this._positions;
},
set: function (value) {
//>>includeStart('debug', pragmas.debug);
if (!defined(value)) {
throw new DeveloperError("value is required.");
}
//>>includeEnd('debug');
let positions = arrayRemoveDuplicates(value, Cartesian3.equalsEpsilon);
if (this._loop && positions.length > 2) {
if (positions === value) {
positions = value.slice();
}
positions.push(Cartesian3.clone(positions[0]));
}
if (
this._actualPositions.length !== positions.length ||
this._actualPositions.length !== this._length
) {
makeDirty(this, POSITION_SIZE_INDEX);
}
this._positions = value;
this._actualPositions = positions;
this._length = positions.length;
this._boundingVolume = BoundingSphere.fromPoints(
this._actualPositions,
this._boundingVolume
);
this._boundingVolumeWC = BoundingSphere.transform(
this._boundingVolume,
this._modelMatrix,
this._boundingVolumeWC
);
makeDirty(this, POSITION_INDEX);
this.update();
},
},
/**
* Gets or sets the surface appearance of the polyline. This can be one of several built-in {@link Material} objects or a custom material, scripted with
* {@link https://github.com/CesiumGS/cesium/wiki/Fabric|Fabric}.
* @memberof Polyline.prototype
* @type {Material}
*/
material: {
get: function () {
return this._material;
},
set: function (material) {
//>>includeStart('debug', pragmas.debug);
if (!defined(material)) {
throw new DeveloperError("material is required.");
}
//>>includeEnd('debug');
if (this._material !== material) {
this._material = material;
makeDirty(this, MATERIAL_INDEX);
}
},
},
/**
* Gets or sets the width of the polyline.
* @memberof Polyline.prototype
* @type {Number}
*/
width: {
get: function () {
return this._width;
},
set: function (value) {
//>>includeStart('debug', pragmas.debug)
if (!defined(value)) {
throw new DeveloperError("value is required.");
}
//>>includeEnd('debug');
const width = this._width;
if (value !== width) {
this._width = value;
makeDirty(this, WIDTH_INDEX);
}
},
},
/**
* Gets or sets whether a line segment will be added between the first and last polyline positions.
* @memberof Polyline.prototype
* @type {Boolean}
*/
loop: {
get: function () {
return this._loop;
},
set: function (value) {
//>>includeStart('debug', pragmas.debug)
if (!defined(value)) {
throw new DeveloperError("value is required.");
}
//>>includeEnd('debug');
if (value !== this._loop) {
let positions = this._actualPositions;
if (value) {
if (
positions.length > 2 &&
!Cartesian3.equals(positions[0], positions[positions.length - 1])
) {
if (positions.length === this._positions.length) {
this._actualPositions = positions = this._positions.slice();
}
positions.push(Cartesian3.clone(positions[0]));
}
} else if (
positions.length > 2 &&
Cartesian3.equals(positions[0], positions[positions.length - 1])
) {
if (positions.length - 1 === this._positions.length) {
this._actualPositions = this._positions;
} else {
positions.pop();
}
}
this._loop = value;
makeDirty(this, POSITION_SIZE_INDEX);
}
},
},
/**
* Gets or sets the user-defined value returned when the polyline is picked.
* @memberof Polyline.prototype
* @type {*}
*/
id: {
get: function () {
return this._id;
},
set: function (value) {
this._id = value;
if (defined(this._pickId)) {
this._pickId.object.id = value;
}
},
},
/**
* @private
*/
pickId: {
get: function () {
return this._pickId;
},
},
/**
* Gets the destruction status of this polyline
* @memberof Polyline.prototype
* @type {Boolean}
* @default false
* @private
*/
isDestroyed: {
get: function () {
return !defined(this._polylineCollection);
},
},
/**
* Gets or sets the condition specifying at what distance from the camera that this polyline will be displayed.
* @memberof Polyline.prototype
* @type {DistanceDisplayCondition}
* @default undefined
*/
distanceDisplayCondition: {
get: function () {
return this._distanceDisplayCondition;
},
set: function (value) {
//>>includeStart('debug', pragmas.debug);
if (defined(value) && value.far <= value.near) {
throw new DeveloperError(
"far distance must be greater than near distance."
);
}
//>>includeEnd('debug');
if (
!DistanceDisplayCondition.equals(value, this._distanceDisplayCondition)
) {
this._distanceDisplayCondition = DistanceDisplayCondition.clone(
value,
this._distanceDisplayCondition
);
makeDirty(this, DISTANCE_DISPLAY_CONDITION);
}
},
},
});
/**
* @private
*/
Polyline.prototype.update = function () {
let modelMatrix = Matrix4.IDENTITY;
if (defined(this._polylineCollection)) {
modelMatrix = this._polylineCollection.modelMatrix;
}
const segmentPositionsLength = this._segments.positions.length;
const segmentLengths = this._segments.lengths;
const positionsChanged =
this._propertiesChanged[POSITION_INDEX] > 0 ||
this._propertiesChanged[POSITION_SIZE_INDEX] > 0;
if (!Matrix4.equals(modelMatrix, this._modelMatrix) || positionsChanged) {
this._segments = PolylinePipeline.wrapLongitude(
this._actualPositions,
modelMatrix
);
this._boundingVolumeWC = BoundingSphere.transform(
this._boundingVolume,
modelMatrix,
this._boundingVolumeWC
);
}
this._modelMatrix = Matrix4.clone(modelMatrix, this._modelMatrix);
if (this._segments.positions.length !== segmentPositionsLength) {
// number of positions changed
makeDirty(this, POSITION_SIZE_INDEX);
} else {
const length = segmentLengths.length;
for (let i = 0; i < length; ++i) {
if (segmentLengths[i] !== this._segments.lengths[i]) {
// indices changed
makeDirty(this, POSITION_SIZE_INDEX);
break;
}
}
}
};
/**
* @private
*/
Polyline.prototype.getPickId = function (context) {
if (!defined(this._pickId)) {
this._pickId = context.createPickId({
primitive: this,
collection: this._polylineCollection,
id: this._id,
});
}
return this._pickId;
};
Polyline.prototype._clean = function () {
this._dirty = false;
const properties = this._propertiesChanged;
for (let k = 0; k < NUMBER_OF_PROPERTIES - 1; ++k) {
properties[k] = 0;
}
};
Polyline.prototype._destroy = function () {
this._pickId = this._pickId && this._pickId.destroy();
this._material = this._material && this._material.destroy();
this._polylineCollection = undefined;
};
export default Polyline;