/
PointVisualizer.js
231 lines (208 loc) · 8.73 KB
/
PointVisualizer.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
/*global define*/
define([
'../Core/AssociativeArray',
'../Core/Cartesian3',
'../Core/Color',
'../Core/defined',
'../Core/destroyObject',
'../Core/DeveloperError',
'../Core/NearFarScalar',
'../Scene/PointPrimitiveCollection',
'./BoundingSphereState',
'./Property'
], function(
AssociativeArray,
Cartesian3,
Color,
defined,
destroyObject,
DeveloperError,
NearFarScalar,
PointPrimitiveCollection,
BoundingSphereState,
Property) {
"use strict";
var defaultColor = Color.WHITE;
var defaultOutlineColor = Color.BLACK;
var defaultOutlineWidth = 0.0;
var defaultPixelSize = 1.0;
var color = new Color();
var position = new Cartesian3();
var outlineColor = new Color();
var scaleByDistance = new NearFarScalar();
var translucencyByDistance = new NearFarScalar();
var EntityData = function(entity) {
this.entity = entity;
this.pointPrimitive = undefined;
this.color = undefined;
this.outlineColor = undefined;
this.pixelSize = undefined;
this.outlineWidth = undefined;
};
/**
* A {@link Visualizer} which maps {@link Entity#point} to a {@link PointPrimitive}.
* @alias PointVisualizer
* @constructor
*
* @param {Scene} scene The scene the primitives will be rendered in.
* @param {EntityCollection} entityCollection The entityCollection to visualize.
*/
var PointVisualizer = function(scene, entityCollection) {
//>>includeStart('debug', pragmas.debug);
if (!defined(scene)) {
throw new DeveloperError('scene is required.');
}
if (!defined(entityCollection)) {
throw new DeveloperError('entityCollection is required.');
}
//>>includeEnd('debug');
entityCollection.collectionChanged.addEventListener(PointVisualizer.prototype._onCollectionChanged, this);
this._scene = scene;
this._unusedIndexes = [];
this._entityCollection = entityCollection;
this._pointPrimitiveCollection = undefined;
this._items = new AssociativeArray();
this._onCollectionChanged(entityCollection, entityCollection.values, [], []);
};
/**
* Updates the primitives created by this visualizer to match their
* Entity counterpart at the given time.
*
* @param {JulianDate} time The time to update to.
* @returns {Boolean} This function always returns true.
*/
PointVisualizer.prototype.update = function(time) {
//>>includeStart('debug', pragmas.debug);
if (!defined(time)) {
throw new DeveloperError('time is required.');
}
//>>includeEnd('debug');
var items = this._items.values;
var unusedIndexes = this._unusedIndexes;
for (var i = 0, len = items.length; i < len; i++) {
var item = items[i];
var entity = item.entity;
var pointGraphics = entity._point;
var pointPrimitive = item.pointPrimitive;
var show = entity.isShowing && entity.isAvailable(time) && Property.getValueOrDefault(pointGraphics._show, time, true);
if (show) {
position = Property.getValueOrUndefined(entity._position, time, position);
show = defined(position);
}
if (!show) {
returnPointPrimitive(item, unusedIndexes);
continue;
}
if (!defined(pointPrimitive)) {
var pointPrimitiveCollection = this._pointPrimitiveCollection;
if (!defined(pointPrimitiveCollection)) {
pointPrimitiveCollection = new PointPrimitiveCollection();
this._pointPrimitiveCollection = pointPrimitiveCollection;
this._scene.primitives.add(pointPrimitiveCollection);
}
var length = unusedIndexes.length;
if (length > 0) {
pointPrimitive = pointPrimitiveCollection.get(unusedIndexes.pop());
} else {
pointPrimitive = pointPrimitiveCollection.add();
}
pointPrimitive.id = entity;
item.pointPrimitive = pointPrimitive;
}
pointPrimitive.show = true;
pointPrimitive.position = position;
pointPrimitive.scaleByDistance = Property.getValueOrUndefined(pointGraphics._scaleByDistance, time, scaleByDistance);
pointPrimitive.translucencyByDistance = Property.getValueOrUndefined(pointGraphics._translucencyByDistance, time, translucencyByDistance);
pointPrimitive.color = Property.getValueOrDefault(pointGraphics._color, time, defaultColor, color);
pointPrimitive.outlineColor = Property.getValueOrDefault(pointGraphics._outlineColor, time, defaultOutlineColor, outlineColor);
pointPrimitive.outlineWidth = Property.getValueOrDefault(pointGraphics._outlineWidth, time, defaultOutlineWidth);
pointPrimitive.pixelSize = Property.getValueOrDefault(pointGraphics._pixelSize, time, defaultPixelSize);
}
return true;
};
/**
* Computes a bounding sphere which encloses the visualization produced for the specified entity.
* The bounding sphere is in the fixed frame of the scene's globe.
*
* @param {Entity} entity The entity whose bounding sphere to compute.
* @param {BoundingSphere} result The bounding sphere onto which to store the result.
* @returns {BoundingSphereState} BoundingSphereState.DONE if the result contains the bounding sphere,
* BoundingSphereState.PENDING if the result is still being computed, or
* BoundingSphereState.FAILED if the entity has no visualization in the current scene.
* @private
*/
PointVisualizer.prototype.getBoundingSphere = function(entity, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(entity)) {
throw new DeveloperError('entity is required.');
}
if (!defined(result)) {
throw new DeveloperError('result is required.');
}
//>>includeEnd('debug');
var item = this._items.get(entity.id);
if (!defined(item) || !defined(item.pointPrimitive)) {
return BoundingSphereState.FAILED;
}
result.center = Cartesian3.clone(item.pointPrimitive.position, result.center);
result.radius = 0;
return BoundingSphereState.DONE;
};
/**
* Returns true if this object was destroyed; otherwise, false.
*
* @returns {Boolean} True if this object was destroyed; otherwise, false.
*/
PointVisualizer.prototype.isDestroyed = function() {
return false;
};
/**
* Removes and destroys all primitives created by this instance.
*/
PointVisualizer.prototype.destroy = function() {
this._entityCollection.collectionChanged.removeEventListener(PointVisualizer.prototype._onCollectionChanged, this);
if (defined(this._pointPrimitiveCollection)) {
this._scene.primitives.remove(this._pointPrimitiveCollection);
}
return destroyObject(this);
};
PointVisualizer.prototype._onCollectionChanged = function(entityCollection, added, removed, changed) {
var i;
var entity;
var unusedIndexes = this._unusedIndexes;
var items = this._items;
for (i = added.length - 1; i > -1; i--) {
entity = added[i];
if (defined(entity._point) && defined(entity._position)) {
items.set(entity.id, new EntityData(entity));
}
}
for (i = changed.length - 1; i > -1; i--) {
entity = changed[i];
if (defined(entity._point) && defined(entity._position)) {
if (!items.contains(entity.id)) {
items.set(entity.id, new EntityData(entity));
}
} else {
returnPointPrimitive(items.get(entity.id), unusedIndexes);
items.remove(entity.id);
}
}
for (i = removed.length - 1; i > -1; i--) {
entity = removed[i];
returnPointPrimitive(items.get(entity.id), unusedIndexes);
items.remove(entity.id);
}
};
function returnPointPrimitive(item, unusedIndexes) {
if (defined(item)) {
var pointPrimitive = item.pointPrimitive;
if (defined(pointPrimitive)) {
item.pointPrimitive = undefined;
pointPrimitive.show = false;
unusedIndexes.push(pointPrimitive._index);
}
}
}
return PointVisualizer;
});