/
Polyline.js
355 lines (316 loc) · 12.5 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
/*global define*/
define([
'../Core/arrayRemoveDuplicates',
'../Core/BoundingSphere',
'../Core/Cartesian3',
'../Core/Color',
'../Core/defaultValue',
'../Core/defined',
'../Core/defineProperties',
'../Core/DeveloperError',
'../Core/Matrix4',
'../Core/PolylinePipeline',
'./Material'
], function(
arrayRemoveDuplicates,
BoundingSphere,
Cartesian3,
Color,
defaultValue,
defined,
defineProperties,
DeveloperError,
Matrix4,
PolylinePipeline,
Material) {
'use strict';
/**
* A renderable polyline. Create this by calling {@link PolylineCollection#add}
*
* @alias Polyline
* @internalConstructor
*
* @param {Object} [options] Object with the following properties:
* @param {Boolean} [options.show=true] <code>true</code> if this polyline will be shown; otherwise, <code>false</code>.
* @param {Number} [options.width=1.0] The width of the polyline in pixels.
* @param {Boolean} [options.loop=false] Whether a line segment will be added between the last and first line positions to make this line a loop.
* @param {Material} [options.material=Material.ColorType] The material.
* @param {Cartesian3[]} [options.positions] The positions.
* @param {Object} [options.id] The user-defined object to be returned when this polyline is picked.
*
* @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._material = options.material;
if (!defined(this._material)) {
this._material = Material.fromType(Material.ColorType, {
color : new Color(1.0, 1.0, 1.0, 1.0)
});
}
var 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;
var modelMatrix;
if (defined(polylineCollection)) {
modelMatrix = Matrix4.clone(polylineCollection.modelMatrix);
}
this._modelMatrix = modelMatrix;
this._segments = PolylinePipeline.wrapLongitude(this._actualPositions, modelMatrix);
this._actualLength = undefined;
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
}
var SHOW_INDEX = Polyline.SHOW_INDEX = 0;
var WIDTH_INDEX = Polyline.WIDTH_INDEX = 1;
var POSITION_INDEX = Polyline.POSITION_INDEX = 2;
var MATERIAL_INDEX = Polyline.MATERIAL_INDEX = 3;
var POSITION_SIZE_INDEX = Polyline.POSITION_SIZE_INDEX = 4;
var NUMBER_OF_PROPERTIES = Polyline.NUMBER_OF_PROPERTIES = 5;
function makeDirty(polyline, propertyChanged) {
++polyline._propertiesChanged[propertyChanged];
var polylineCollection = polyline._polylineCollection;
if (defined(polylineCollection)) {
polylineCollection._updatePolyline(polyline, propertyChanged);
polyline._dirty = true;
}
}
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');
var 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/AnalyticalGraphicsInc/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');
var 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) {
var 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 object returned when the polyline is picked.
* @memberof Polyline.prototype
* @type {Object}
*/
id : {
get : function() {
return this._id;
},
set : function(value) {
this._id = value;
if (defined(this._pickId)) {
this._pickId.object.id = value;
}
}
}
});
/**
* @private
*/
Polyline.prototype.update = function() {
var modelMatrix = Matrix4.IDENTITY;
if (defined(this._polylineCollection)) {
modelMatrix = this._polylineCollection.modelMatrix;
}
var segmentPositionsLength = this._segments.positions.length;
var segmentLengths = this._segments.lengths;
var 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 = modelMatrix;
if (this._segments.positions.length !== segmentPositionsLength) {
// number of positions changed
makeDirty(this, POSITION_SIZE_INDEX);
} else {
var length = segmentLengths.length;
for (var 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;
var properties = this._propertiesChanged;
for ( var 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;
};
return Polyline;
});