@openglobus/og 0.11.8 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,742 +1,742 @@
1
- /**
2
- * @module og/entity/Entity
3
- */
4
-
5
- "use strict";
6
-
7
- import * as mercator from "../mercator.js";
8
- import * as utils from "../utils/shared.js";
9
- import { Billboard } from "./Billboard.js";
10
- import { Strip } from "./Strip.js";
11
- import { Extent } from "../Extent.js";
12
- import { Geometry } from "./Geometry.js";
13
- import { Label } from "./Label.js";
14
- import { LonLat } from "../LonLat.js";
15
- import { Polyline } from "./Polyline.js";
16
- import { Ray } from "./Ray.js";
17
- import { PointCloud } from "./PointCloud.js";
18
- import { Sphere } from "../shapes/Sphere.js";
19
- import { Vec3 } from "../math/Vec3.js";
20
- import { GeoObject } from "./GeoObject.js";
21
-
22
- /**
23
- * Entity instances aggregate multiple forms of visualization into a single high-level object.
24
- * They can be created manually and added to entity collection.
25
- *
26
- * @class
27
- * @param {Object} [options] - Entity options:
28
- * @param {string} [options.name] - A human readable name to display to users. It does not have to be unique.
29
- * @param {Vec3|Array.<number>} [options.cartesian] - Spatial entities like billboard, label, sphere etc. cartesian position.
30
- * @param {LonLat} [options.lonlat] - Geodetic coordiantes for an entities like billboard, label, sphere etc.
31
- * @param {boolean} [options.aground] - True for entities that have to be placed on the relief.
32
- * @param {boolean} [options.visibility] - Entity visibility.
33
- * @param {*} [options.billboard] - Billboard options(see {@link og.Billboard}).
34
- * @param {*} [options.label] - Label options(see {@link og.Label}).
35
- * @param {*} [options.sphere] - Sphere options(see {@link og.shape.Sphere}).
36
- * @param {*} [options.box] - Sphere options(see {@link og.shape.Box}).
37
- * @param {*} [options.polyline] - Polyline options(see {@link og.Polyline}).
38
- * @param {*} [options.ray] - Ray options(see {@link og.Ray}).
39
- * @param {*} [options.pointCloud] - Point cloud options(see {@link og.PointCloud}).
40
- * @param {*} [options.geometry] - Geometry options (see {@link og.Geometry}), available for vector layer only.
41
- * @param {*} [options.properties] - Entity custom properties.
42
- */
43
- class Entity {
44
- constructor(options) {
45
- options = options || {};
46
-
47
- options.properties = options.properties || {};
48
-
49
- /**
50
- * Unic identifier.
51
- * @public
52
- * @readonly
53
- */
54
- this.id = Entity._staticCounter++;
55
-
56
- /**
57
- * Entity user defined properties.
58
- * @public
59
- * @type {Object}
60
- */
61
- this.properties = options.properties || {};
62
-
63
- /**
64
- * Entity name.
65
- * @public
66
- * @type {string}
67
- */
68
- this.properties.name = this.properties.name || options.name || "";
69
-
70
- /**
71
- * Children entities.
72
- * @public
73
- * @type {Array.<Entity>}
74
- */
75
- this.childrenNodes = [];
76
-
77
- /**
78
- * Parent entity.
79
- * @public
80
- * @type {Entity}
81
- */
82
- this.parent = null;
83
-
84
- /**
85
- * Entity cartesian position.
86
- * @protected
87
- * @type {Vec3}
88
- */
89
- this._cartesian = utils.createVector3(options.cartesian);
90
-
91
- /**
92
- * Geodetic entity coordiantes.
93
- * @protected
94
- * @type {LonLat}
95
- */
96
- this._lonlat = utils.createLonLat(options.lonlat);
97
-
98
- /**
99
- * World Mercator entity coordinates.
100
- * @protected
101
- * @type {LonLat}
102
- */
103
- this._lonlatMerc = null;
104
-
105
- /**
106
- * Entity visible terrain altitude.
107
- * @protected
108
- * @type {number}
109
- */
110
- this._altitude = options.altitude || 0.0;
111
-
112
- /**
113
- * Visibility flag.
114
- * @protected
115
- * @type {boolean}
116
- */
117
- this._visibility = options.visibility != undefined ? options.visibility : true;
118
-
119
- /**
120
- * Entity collection that this entity belongs to.
121
- * @protected
122
- * @type {EntityCollection}
123
- */
124
- this._entityCollection = null;
125
-
126
- /**
127
- * Entity collection array store index.
128
- * @protected
129
- * @type {number}
130
- */
131
- this._entityCollectionIndex = -1;
132
-
133
- /**
134
- * Assigned vector layer pointer.
135
- * @protected
136
- * @type {layer.Vector}
137
- */
138
- this._layer = null;
139
-
140
- /**
141
- * Assigned vector layer entity array index.
142
- * @protected
143
- * @type {number}
144
- */
145
- this._layerIndex = -1;
146
-
147
- /**
148
- * Picking color.
149
- * @protected
150
- * @type {Vec3}
151
- */
152
- this._pickingColor = new Vec3(0, 0, 0);
153
-
154
- this._featureConstructorArray = {
155
- billboard: [Billboard, this.setBillboard],
156
- label: [Label, this.setLabel],
157
- sphere: [Sphere, this.setShape],
158
- // box: [Box, this.setShape],
159
- polyline: [Polyline, this.setPolyline],
160
- pointCloud: [PointCloud, this.setPointCloud],
161
- geometry: [Geometry, this.setGeometry],
162
- geoObject: [GeoObject, this.setGeoObject],
163
- strip: [Strip, this.setStrip],
164
- ray: [Ray, this.setRay]
165
- };
166
-
167
- /**
168
- * Billboard entity.
169
- * @public
170
- * @type {Billboard}
171
- */
172
- this.billboard = this._createOptionFeature("billboard", options.billboard);
173
-
174
- /**
175
- * Text label entity.
176
- * @public
177
- * @type {Label}
178
- */
179
- this.label = this._createOptionFeature("label", options.label);
180
-
181
- /**
182
- * Shape entity.
183
- * @public
184
- * @type {shape.BaseShape}
185
- */
186
- this.shape = this._createOptionFeature("sphere", options.sphere || options.box);
187
-
188
- /**
189
- * Polyline entity.
190
- * @public
191
- * @type {Polyline}
192
- */
193
- this.polyline = this._createOptionFeature("polyline", options.polyline);
194
-
195
- /**
196
- * Ray entity.
197
- * @public
198
- * @type {ray}
199
- */
200
- this.ray = this._createOptionFeature("ray", options.ray);
201
-
202
- /**
203
- * PointCloud entity.
204
- * @public
205
- * @type {PointCloud}
206
- */
207
- this.pointCloud = this._createOptionFeature("pointCloud", options.pointCloud);
208
-
209
- /**
210
- * Geometry entity(available for vector layer only).
211
- * @public
212
- * @type {Geometry}
213
- */
214
- this.geometry = this._createOptionFeature("geometry", options.geometry);
215
-
216
- /**
217
- * Geo object entity
218
- * @public
219
- * @type {og.Geometry}
220
- */
221
- this.geoObject = this._createOptionFeature("geoObject", options.geoObject);
222
-
223
- /**
224
- * Strip entity.
225
- * @public
226
- * @type {Strip}
227
- */
228
- this.strip = this._createOptionFeature("strip", options.strip);
229
- }
230
-
231
- static get _staticCounter() {
232
- if (!this._counter && this._counter !== 0) {
233
- this._counter = 0;
234
- }
235
- return this._counter;
236
- }
237
-
238
- static set _staticCounter(n) {
239
- this._counter = n;
240
- }
241
-
242
- get instanceName() {
243
- return "Entity";
244
- }
245
-
246
- _createOptionFeature(featureName, options) {
247
- if (options) {
248
- var c = this._featureConstructorArray[featureName];
249
- return c[1].call(this, new c[0](options));
250
- }
251
- return null;
252
- }
253
-
254
- getCollectionIndex() {
255
- return this._entityCollectionIndex;
256
- }
257
-
258
- /**
259
- * Adds current entity into the specified entity collection.
260
- * @public
261
- * @param {EntityCollection|Vector} collection - Specified entity collection or vector layer.
262
- * @param {Boolean} [rightNow=false] - Entity insertion option for vector layer.
263
- * @returns {Entity} - This object.
264
- */
265
- addTo(collection, rightNow) {
266
- collection.add(this, rightNow);
267
- return this;
268
- }
269
-
270
- /**
271
- * Removes current entity from collection and layer.
272
- * @public
273
- */
274
- remove() {
275
- this._layer && this._layer.removeEntity(this);
276
- this._entityCollection && this._entityCollection.removeEntity(this);
277
- }
278
-
279
- /**
280
- * Sets the entity visibility.
281
- * @public
282
- * @param {boolean} visibility - Entity visibility.
283
- */
284
- setVisibility(visibility) {
285
- this._visibility = visibility;
286
-
287
- // billboards
288
- this.billboard && this.billboard.setVisibility(visibility);
289
-
290
- // billboards
291
- this.geoObject && this.geoObject.setVisibility(visibility);
292
-
293
- // labels
294
- this.label && this.label.setVisibility(visibility);
295
-
296
- // shape
297
- this.shape && this.shape.setVisibility(visibility);
298
-
299
- // polyline
300
- this.polyline && this.polyline.setVisibility(visibility);
301
-
302
- // ray
303
- this.ray && this.ray.setVisibility(visibility);
304
-
305
- // geometry
306
- this.geometry && this.geometry.setVisibility(visibility);
307
-
308
- for (var i = 0; i < this.childrenNodes.length; i++) {
309
- this.childrenNodes[i].setVisibility(visibility);
310
- }
311
- }
312
-
313
- /**
314
- * Returns entity visibility.
315
- * @public
316
- * @returns {boolean} -
317
- */
318
- getVisibility() {
319
- return this._visibility;
320
- }
321
-
322
- /**
323
- * Sets entity cartesian position.
324
- * @public
325
- * @param {Vec3} cartesian - Cartesian position in 3d space.
326
- */
327
- setCartesian3v(cartesian) {
328
- this.setCartesian(cartesian.x, cartesian.y, cartesian.z);
329
- }
330
-
331
- /**
332
- * Sets entity cartesian position.
333
- * @public
334
- * @param {number} x - 3d space X - position.
335
- * @param {number} y - 3d space Y - position.
336
- * @param {number} z - 3d space Z - position.
337
- */
338
- setCartesian(x, y, z) {
339
- var p = this._cartesian;
340
-
341
- p.x = x;
342
- p.y = y;
343
- p.z = z;
344
-
345
- // billboards
346
- this.billboard && this.billboard.setPosition3v(p);
347
-
348
- // geoObject
349
- this.geoObject && this.geoObject.setPosition3v(p);
350
-
351
- // labels
352
- this.label && this.label.setPosition3v(p);
353
-
354
- // shape
355
- this.shape && this.shape.setPosition3v(p);
356
-
357
- for (var i = 0; i < this.childrenNodes.length; i++) {
358
- this.childrenNodes[i].setCartesian(x, y, z);
359
- }
360
-
361
- var ec = this._entityCollection;
362
-
363
- if (ec && ec.renderNode && ec.renderNode.ellipsoid) {
364
- this._lonlat = ec.renderNode.ellipsoid.cartesianToLonLat(p);
365
-
366
- if (Math.abs(this._lonlat.lat) < mercator.MAX_LAT) {
367
- this._lonlatMerc = this._lonlat.forwardMercator();
368
- } else {
369
- this._lonlatMerc = null;
370
- }
371
- }
372
-
373
- ec && ec.events.dispatch(ec.events.entitymove, this);
374
- }
375
-
376
- /**
377
- * Sets entity cartesian position without event dispatching.
378
- * @protected
379
- * @param {Vec3} cartesian - Cartesian position in 3d space.
380
- * @param {boolean} skipLonLat - skip geodetic calculation.
381
- */
382
- _setCartesian3vSilent(cartesian, skipLonLat) {
383
- var p = this._cartesian;
384
-
385
- p.x = cartesian.x;
386
- p.y = cartesian.y;
387
- p.z = cartesian.z;
388
-
389
- // billboards
390
- this.billboard && this.billboard.setPosition3v(p);
391
-
392
- // geoObject
393
- this.geoObject && this.geoObject.setPosition3v(p);
394
-
395
- // labels
396
- this.label && this.label.setPosition3v(p);
397
-
398
- // shape
399
- this.shape && this.shape.setPosition3v(p);
400
-
401
- for (var i = 0; i < this.childrenNodes.length; i++) {
402
- this.childrenNodes[i].setCartesian(p.x, p.y, p.z);
403
- }
404
-
405
- var ec = this._entityCollection;
406
-
407
- if (!skipLonLat && ec && ec.renderNode && ec.renderNode.ellipsoid) {
408
- this._lonlat = ec.renderNode.ellipsoid.cartesianToLonLat(p);
409
-
410
- if (Math.abs(this._lonlat.lat) < mercator.MAX_LAT) {
411
- this._lonlatMerc = this._lonlat.forwardMercator();
412
- } else {
413
- this._lonlatMerc = null;
414
- }
415
- }
416
- }
417
-
418
- /**
419
- * Gets entity geodetic coordinates.
420
- * @public
421
- * @returns {LonLat} -
422
- */
423
- getLonLat() {
424
- return this._lonlat.clone();
425
- }
426
-
427
- /**
428
- * Sets geodetic coordinates of the entity point object.
429
- * @public
430
- * @param {LonLat} lonlat - WGS84 coordinates.
431
- */
432
- setLonLat(lonlat) {
433
- var l = this._lonlat;
434
-
435
- l.lon = lonlat.lon;
436
- l.lat = lonlat.lat;
437
- l.height = lonlat.height;
438
-
439
- var ec = this._entityCollection;
440
- if (ec && ec.renderNode && ec.renderNode.ellipsoid) {
441
- if (Math.abs(lonlat.lat) < mercator.MAX_LAT) {
442
- this._lonlatMerc = lonlat.forwardMercator();
443
- } else {
444
- this._lonlatMerc = null;
445
- }
446
-
447
- ec.renderNode.ellipsoid.lonLatToCartesianRes(lonlat, this._cartesian);
448
- this.setCartesian3v(this._cartesian);
449
- }
450
- }
451
-
452
- /**
453
- * Sets entity altitude over the planet.
454
- * @public
455
- * @param {number} altitude - Altitude.
456
- */
457
- setAltitude(altitude) {
458
- this._altitude = altitude;
459
- }
460
-
461
- /**
462
- * Sets entity altitude over the planet.
463
- * @public
464
- * @return {number} Altitude.
465
- */
466
- getAltitude() {
467
- return this._altitude;
468
- }
469
-
470
- /**
471
- * Returns carteain position.
472
- * @public
473
- * @returns {Vec3} -
474
- */
475
- getCartesian() {
476
- return this._cartesian.clone();
477
- }
478
-
479
- /**
480
- * Sets entity billboard.
481
- * @public
482
- * @param {Billboard} billboard - Billboard object.
483
- * @returns {Billboard} -
484
- */
485
- setBillboard(billboard) {
486
- if (this.billboard) {
487
- this.billboard.remove();
488
- }
489
- this.billboard = billboard;
490
- this.billboard._entity = this;
491
- this.billboard.setPosition3v(this._cartesian);
492
- this.billboard.setVisibility(this._visibility);
493
- this._entityCollection && this._entityCollection._billboardHandler.add(billboard);
494
- return billboard;
495
- }
496
-
497
- /**
498
- * Sets entity label.
499
- * @public
500
- * @param {Label} label - Text label.
501
- * @returns {Label} -
502
- */
503
- setLabel(label) {
504
- if (this.label) {
505
- this.label.remove();
506
- }
507
- this.label = label;
508
- this.label._entity = this;
509
- this.label.setPosition3v(this._cartesian);
510
- this.label.setVisibility(this._visibility);
511
- this._entityCollection && this._entityCollection._labelHandler.add(label);
512
- return label;
513
- }
514
-
515
- /**
516
- * Sets entity ray.
517
- * @public
518
- * @param {Ray} ray - Ray object.
519
- * @returns {Ray} -
520
- */
521
- setRay(ray) {
522
- if (this.ray) {
523
- this.ray.remove();
524
- }
525
- this.ray = ray;
526
- this.ray._entity = this;
527
- this.ray.setVisibility(this._visibility);
528
- this._entityCollection && this._entityCollection._rayHandler.add(ray);
529
- return ray;
530
- }
531
-
532
- /**
533
- * Sets entity shape.
534
- * @public
535
- * @param {BaseShape} shape - Shape object.
536
- * @returns {Polyline} -
537
- */
538
- setShape(shape) {
539
- if (this.shape) {
540
- this.shape.remove();
541
- }
542
- this.shape = shape;
543
- this.shape._entity = this;
544
- this.shape.setPosition3v(this._cartesian);
545
- this.shape.setVisibility(this._visibility);
546
- this._entityCollection && this._entityCollection._shapeHandler.add(shape);
547
- return shape;
548
- }
549
-
550
- /**
551
- * Sets entity polyline.
552
- * @public
553
- * @param {Polyline} polyline - Polyline object.
554
- * @returns {Polyline} -
555
- */
556
- setPolyline(polyline) {
557
- if (this.polyline) {
558
- this.polyline.remove();
559
- }
560
- this.polyline = polyline;
561
- this.polyline._entity = this;
562
- this.polyline.setVisibility(this._visibility);
563
- this._entityCollection && this._entityCollection._polylineHandler.add(polyline);
564
- return polyline;
565
- }
566
-
567
- /**
568
- * Sets entity pointCloud.
569
- * @public
570
- * @param {PointCloud} pointCloud - PointCloud object.
571
- * @returns {PointCloud} -
572
- */
573
- setPointCloud(pointCloud) {
574
- if (this.pointCloud) {
575
- this.pointCloud.remove();
576
- }
577
- this.pointCloud = pointCloud;
578
- this.pointCloud._entity = this;
579
- this.pointCloud.setVisibility(this._visibility);
580
- this._entityCollection && this._entityCollection._pointCloudHandler.add(pointCloud);
581
- return pointCloud;
582
- }
583
-
584
- /**
585
- * Sets entity geometry.
586
- * @public
587
- * @param {Geometry} geometry - Geometry object.
588
- * @returns {Geometry} -
589
- */
590
- setGeometry(geometry) {
591
- if (this.geometry) {
592
- this.geometry.remove();
593
- }
594
- this.geometry = geometry;
595
- this.geometry._entity = this;
596
- this.geometry.setVisibility(this._visibility);
597
- this._layer && this._layer.add(this);
598
- return geometry;
599
- }
600
-
601
- /**
602
- * Sets entity geoObject.
603
- * @public
604
- * @param {GeoObject} geoObject - GeoObject.
605
- * @returns {GeoObject} -
606
- */
607
- setGeoObject(geoObject) {
608
- if (this.geoObject) {
609
- this.geoObject.remove();
610
- }
611
- this.geoObject = geoObject;
612
- this.geoObject._entity = this;
613
- this.geoObject.setPosition3v(this._cartesian);
614
- this.geoObject.setVisibility(this._visibility);
615
- this._entityCollection && this._entityCollection._geoObjectHandler.add(geoObject);
616
- return geoObject;
617
- }
618
-
619
- /**
620
- * Sets entity strip.
621
- * @public
622
- * @param {Strip} strip - Strip object.
623
- * @returns {Strip} -
624
- */
625
- setStrip(strip) {
626
- if (this.strip) {
627
- this.strip.remove();
628
- }
629
- this.strip = strip;
630
- this.strip._entity = this;
631
- this.strip.setVisibility(this._visibility);
632
- this._entityCollection && this._entityCollection._stripHandler.add(strip);
633
- return strip;
634
- }
635
-
636
- get layer() {
637
- return this._layer;
638
- }
639
-
640
- get rendererEvents() {
641
- if (this._layer) {
642
- return this._layer.events;
643
- } else if (this._entityCollection) {
644
- return this._entityCollection.events;
645
- }
646
- return null;
647
- }
648
-
649
- /**
650
- * Append child entity.
651
- * @public
652
- * @param {Entity} entity - Child entity.
653
- */
654
- appendChild(entity) {
655
- entity._entityCollection = this._entityCollection;
656
- entity._pickingColor = this._pickingColor;
657
- entity.parent = this;
658
- this.childrenNodes.push(entity);
659
- this._entityCollection && this._entityCollection._addRecursively(entity);
660
- }
661
-
662
- /**
663
- * Appends entity items(billboard, label etc.) picking color.
664
- * @public
665
- */
666
- setPickingColor() {
667
- var c = this._pickingColor;
668
-
669
- // billboard
670
- this.billboard && this.billboard.setPickingColor3v(c);
671
-
672
- // label
673
- this.label && this.label.setPickingColor3v(c);
674
-
675
- // shape
676
- this.shape && this.shape.setPickingColor3v(c);
677
-
678
- // polyline
679
- this.polyline && this.polyline.setPickingColor3v(c);
680
-
681
- // ray
682
- this.ray && this.ray.setPickingColor3v(c);
683
-
684
- // strip
685
- this.strip && this.strip.setPickingColor3v(c);
686
-
687
- // billboard
688
- this.geoObject && this.geoObject.setPickingColor3v(c);
689
- for (var i = 0; i < this.childrenNodes.length; i++) {
690
- this.childrenNodes[i].setPickingColor();
691
- }
692
- }
693
-
694
- /**
695
- * Return geodethic extent.
696
- * @returns {Extent} -
697
- */
698
- getExtent() {
699
- var res;
700
- var c = this._lonlat;
701
- if (this.billboard || this.label) {
702
- res = new Extent(new LonLat(c.lon, c.lat), new LonLat(c.lon, c.lat));
703
- } else {
704
- res = new Extent(new LonLat(180.0, 90.0), new LonLat(-180.0, -90.0));
705
- }
706
-
707
- var sw = res.southWest,
708
- ne = res.northEast;
709
-
710
- if (this.polyline) {
711
- let e = this.polyline.getExtent();
712
- if (e.southWest.lon < sw.lon) sw.lon = e.southWest.lon;
713
- if (e.southWest.lat < sw.lat) sw.lat = e.southWest.lat;
714
- if (e.northEast.lon > ne.lon) ne.lon = e.northEast.lon;
715
- if (e.northEast.lat > ne.lat) ne.lat = e.northEast.lat;
716
- }
717
-
718
- if (this.geometry) {
719
- let e = this.geometry.getExtent();
720
- if (e.southWest.lon < sw.lon) sw.lon = e.southWest.lon;
721
- if (e.southWest.lat < sw.lat) sw.lat = e.southWest.lat;
722
- if (e.northEast.lon > ne.lon) ne.lon = e.northEast.lon;
723
- if (e.northEast.lat > ne.lat) ne.lat = e.northEast.lat;
724
- }
725
-
726
- for (var i = 0; i < this.childrenNodes.length; i++) {
727
- let e = this.childrenNodes[i].getExtent();
728
- if (e.southWest.lon < sw.lon) sw.lon = e.southWest.lon;
729
- if (e.southWest.lat < sw.lat) sw.lat = e.southWest.lat;
730
- if (e.northEast.lon > ne.lon) ne.lon = e.northEast.lon;
731
- if (e.northEast.lat > ne.lat) ne.lat = e.northEast.lat;
732
- }
733
-
734
- return res;
735
- }
736
-
737
- isEqual(entity) {
738
- return this.id === entity.id;
739
- }
740
- }
741
-
742
- export { Entity };
1
+ /**
2
+ * @module og/entity/Entity
3
+ */
4
+
5
+ "use strict";
6
+
7
+ import * as mercator from "../mercator.js";
8
+ import * as utils from "../utils/shared.js";
9
+ import { Billboard } from "./Billboard.js";
10
+ import { Strip } from "./Strip.js";
11
+ import { Extent } from "../Extent.js";
12
+ import { Geometry } from "./Geometry.js";
13
+ import { Label } from "./Label.js";
14
+ import { LonLat } from "../LonLat.js";
15
+ import { Polyline } from "./Polyline.js";
16
+ import { Ray } from "./Ray.js";
17
+ import { PointCloud } from "./PointCloud.js";
18
+ import { Sphere } from "../shapes/Sphere.js";
19
+ import { Vec3 } from "../math/Vec3.js";
20
+ import { GeoObject } from "./GeoObject.js";
21
+
22
+ /**
23
+ * Entity instances aggregate multiple forms of visualization into a single high-level object.
24
+ * They can be created manually and added to entity collection.
25
+ *
26
+ * @class
27
+ * @param {Object} [options] - Entity options:
28
+ * @param {string} [options.name] - A human readable name to display to users. It does not have to be unique.
29
+ * @param {Vec3|Array.<number>} [options.cartesian] - Spatial entities like billboard, label, sphere etc. cartesian position.
30
+ * @param {LonLat} [options.lonlat] - Geodetic coordiantes for an entities like billboard, label, sphere etc.
31
+ * @param {boolean} [options.aground] - True for entities that have to be placed on the relief.
32
+ * @param {boolean} [options.visibility] - Entity visibility.
33
+ * @param {*} [options.billboard] - Billboard options(see {@link og.Billboard}).
34
+ * @param {*} [options.label] - Label options(see {@link og.Label}).
35
+ * @param {*} [options.sphere] - Sphere options(see {@link og.shape.Sphere}).
36
+ * @param {*} [options.box] - Sphere options(see {@link og.shape.Box}).
37
+ * @param {*} [options.polyline] - Polyline options(see {@link og.Polyline}).
38
+ * @param {*} [options.ray] - Ray options(see {@link og.Ray}).
39
+ * @param {*} [options.pointCloud] - Point cloud options(see {@link og.PointCloud}).
40
+ * @param {*} [options.geometry] - Geometry options (see {@link og.Geometry}), available for vector layer only.
41
+ * @param {*} [options.properties] - Entity custom properties.
42
+ */
43
+ class Entity {
44
+ constructor(options) {
45
+ options = options || {};
46
+
47
+ options.properties = options.properties || {};
48
+
49
+ /**
50
+ * Unic identifier.
51
+ * @public
52
+ * @readonly
53
+ */
54
+ this.id = Entity._staticCounter++;
55
+
56
+ /**
57
+ * Entity user defined properties.
58
+ * @public
59
+ * @type {Object}
60
+ */
61
+ this.properties = options.properties || {};
62
+
63
+ /**
64
+ * Entity name.
65
+ * @public
66
+ * @type {string}
67
+ */
68
+ this.properties.name = this.properties.name || options.name || "";
69
+
70
+ /**
71
+ * Children entities.
72
+ * @public
73
+ * @type {Array.<Entity>}
74
+ */
75
+ this.childrenNodes = [];
76
+
77
+ /**
78
+ * Parent entity.
79
+ * @public
80
+ * @type {Entity}
81
+ */
82
+ this.parent = null;
83
+
84
+ /**
85
+ * Entity cartesian position.
86
+ * @protected
87
+ * @type {Vec3}
88
+ */
89
+ this._cartesian = utils.createVector3(options.cartesian);
90
+
91
+ /**
92
+ * Geodetic entity coordiantes.
93
+ * @protected
94
+ * @type {LonLat}
95
+ */
96
+ this._lonlat = utils.createLonLat(options.lonlat);
97
+
98
+ /**
99
+ * World Mercator entity coordinates.
100
+ * @protected
101
+ * @type {LonLat}
102
+ */
103
+ this._lonlatMerc = null;
104
+
105
+ /**
106
+ * Entity visible terrain altitude.
107
+ * @protected
108
+ * @type {number}
109
+ */
110
+ this._altitude = options.altitude || 0.0;
111
+
112
+ /**
113
+ * Visibility flag.
114
+ * @protected
115
+ * @type {boolean}
116
+ */
117
+ this._visibility = options.visibility != undefined ? options.visibility : true;
118
+
119
+ /**
120
+ * Entity collection that this entity belongs to.
121
+ * @protected
122
+ * @type {EntityCollection}
123
+ */
124
+ this._entityCollection = null;
125
+
126
+ /**
127
+ * Entity collection array store index.
128
+ * @protected
129
+ * @type {number}
130
+ */
131
+ this._entityCollectionIndex = -1;
132
+
133
+ /**
134
+ * Assigned vector layer pointer.
135
+ * @protected
136
+ * @type {layer.Vector}
137
+ */
138
+ this._layer = null;
139
+
140
+ /**
141
+ * Assigned vector layer entity array index.
142
+ * @protected
143
+ * @type {number}
144
+ */
145
+ this._layerIndex = -1;
146
+
147
+ /**
148
+ * Picking color.
149
+ * @protected
150
+ * @type {Vec3}
151
+ */
152
+ this._pickingColor = new Vec3(0, 0, 0);
153
+
154
+ this._featureConstructorArray = {
155
+ billboard: [Billboard, this.setBillboard],
156
+ label: [Label, this.setLabel],
157
+ sphere: [Sphere, this.setShape],
158
+ // box: [Box, this.setShape],
159
+ polyline: [Polyline, this.setPolyline],
160
+ pointCloud: [PointCloud, this.setPointCloud],
161
+ geometry: [Geometry, this.setGeometry],
162
+ geoObject: [GeoObject, this.setGeoObject],
163
+ strip: [Strip, this.setStrip],
164
+ ray: [Ray, this.setRay]
165
+ };
166
+
167
+ /**
168
+ * Billboard entity.
169
+ * @public
170
+ * @type {Billboard}
171
+ */
172
+ this.billboard = this._createOptionFeature("billboard", options.billboard);
173
+
174
+ /**
175
+ * Text label entity.
176
+ * @public
177
+ * @type {Label}
178
+ */
179
+ this.label = this._createOptionFeature("label", options.label);
180
+
181
+ /**
182
+ * Shape entity.
183
+ * @public
184
+ * @type {shape.BaseShape}
185
+ */
186
+ this.shape = this._createOptionFeature("sphere", options.sphere || options.box);
187
+
188
+ /**
189
+ * Polyline entity.
190
+ * @public
191
+ * @type {Polyline}
192
+ */
193
+ this.polyline = this._createOptionFeature("polyline", options.polyline);
194
+
195
+ /**
196
+ * Ray entity.
197
+ * @public
198
+ * @type {ray}
199
+ */
200
+ this.ray = this._createOptionFeature("ray", options.ray);
201
+
202
+ /**
203
+ * PointCloud entity.
204
+ * @public
205
+ * @type {PointCloud}
206
+ */
207
+ this.pointCloud = this._createOptionFeature("pointCloud", options.pointCloud);
208
+
209
+ /**
210
+ * Geometry entity(available for vector layer only).
211
+ * @public
212
+ * @type {Geometry}
213
+ */
214
+ this.geometry = this._createOptionFeature("geometry", options.geometry);
215
+
216
+ /**
217
+ * Geo object entity
218
+ * @public
219
+ * @type {og.Geometry}
220
+ */
221
+ this.geoObject = this._createOptionFeature("geoObject", options.geoObject);
222
+
223
+ /**
224
+ * Strip entity.
225
+ * @public
226
+ * @type {Strip}
227
+ */
228
+ this.strip = this._createOptionFeature("strip", options.strip);
229
+ }
230
+
231
+ static get _staticCounter() {
232
+ if (!this._counter && this._counter !== 0) {
233
+ this._counter = 0;
234
+ }
235
+ return this._counter;
236
+ }
237
+
238
+ static set _staticCounter(n) {
239
+ this._counter = n;
240
+ }
241
+
242
+ get instanceName() {
243
+ return "Entity";
244
+ }
245
+
246
+ _createOptionFeature(featureName, options) {
247
+ if (options) {
248
+ let c = this._featureConstructorArray[featureName];
249
+ return c[1].call(this, new c[0](options));
250
+ }
251
+ return null;
252
+ }
253
+
254
+ getCollectionIndex() {
255
+ return this._entityCollectionIndex;
256
+ }
257
+
258
+ /**
259
+ * Adds current entity into the specified entity collection.
260
+ * @public
261
+ * @param {EntityCollection|Vector} collection - Specified entity collection or vector layer.
262
+ * @param {Boolean} [rightNow=false] - Entity insertion option for vector layer.
263
+ * @returns {Entity} - This object.
264
+ */
265
+ addTo(collection, rightNow) {
266
+ collection.add(this, rightNow);
267
+ return this;
268
+ }
269
+
270
+ /**
271
+ * Removes current entity from collection and layer.
272
+ * @public
273
+ */
274
+ remove() {
275
+ this._layer && this._layer.removeEntity(this);
276
+ this._entityCollection && this._entityCollection.removeEntity(this);
277
+ }
278
+
279
+ /**
280
+ * Sets the entity visibility.
281
+ * @public
282
+ * @param {boolean} visibility - Entity visibility.
283
+ */
284
+ setVisibility(visibility) {
285
+ this._visibility = visibility;
286
+
287
+ // billboards
288
+ this.billboard && this.billboard.setVisibility(visibility);
289
+
290
+ // geoObject
291
+ this.geoObject && this.geoObject.setVisibility(visibility);
292
+
293
+ // labels
294
+ this.label && this.label.setVisibility(visibility);
295
+
296
+ // shape
297
+ this.shape && this.shape.setVisibility(visibility);
298
+
299
+ // polyline
300
+ this.polyline && this.polyline.setVisibility(visibility);
301
+
302
+ // ray
303
+ this.ray && this.ray.setVisibility(visibility);
304
+
305
+ // geometry
306
+ this.geometry && this.geometry.setVisibility(visibility);
307
+
308
+ for (let i = 0; i < this.childrenNodes.length; i++) {
309
+ this.childrenNodes[i].setVisibility(visibility);
310
+ }
311
+ }
312
+
313
+ /**
314
+ * Returns entity visibility.
315
+ * @public
316
+ * @returns {boolean} -
317
+ */
318
+ getVisibility() {
319
+ return this._visibility;
320
+ }
321
+
322
+ /**
323
+ * Sets entity cartesian position.
324
+ * @public
325
+ * @param {Vec3} cartesian - Cartesian position in 3d space.
326
+ */
327
+ setCartesian3v(cartesian) {
328
+ this.setCartesian(cartesian.x, cartesian.y, cartesian.z);
329
+ }
330
+
331
+ /**
332
+ * Sets entity cartesian position.
333
+ * @public
334
+ * @param {number} x - 3d space X - position.
335
+ * @param {number} y - 3d space Y - position.
336
+ * @param {number} z - 3d space Z - position.
337
+ */
338
+ setCartesian(x, y, z) {
339
+ let p = this._cartesian;
340
+
341
+ p.x = x;
342
+ p.y = y;
343
+ p.z = z;
344
+
345
+ // billboards
346
+ this.billboard && this.billboard.setPosition3v(p);
347
+
348
+ // geoObject
349
+ this.geoObject && this.geoObject.setPosition3v(p);
350
+
351
+ // labels
352
+ this.label && this.label.setPosition3v(p);
353
+
354
+ // shape
355
+ this.shape && this.shape.setPosition3v(p);
356
+
357
+ for (let i = 0; i < this.childrenNodes.length; i++) {
358
+ this.childrenNodes[i].setCartesian(x, y, z);
359
+ }
360
+
361
+ let ec = this._entityCollection;
362
+
363
+ if (ec && ec.renderNode && ec.renderNode.ellipsoid) {
364
+ this._lonlat = ec.renderNode.ellipsoid.cartesianToLonLat(p);
365
+
366
+ if (Math.abs(this._lonlat.lat) < mercator.MAX_LAT) {
367
+ this._lonlatMerc = this._lonlat.forwardMercator();
368
+ } else {
369
+ this._lonlatMerc = null;
370
+ }
371
+ }
372
+
373
+ ec && ec.events.dispatch(ec.events.entitymove, this);
374
+ }
375
+
376
+ /**
377
+ * Sets entity cartesian position without event dispatching.
378
+ * @protected
379
+ * @param {Vec3} cartesian - Cartesian position in 3d space.
380
+ * @param {boolean} skipLonLat - skip geodetic calculation.
381
+ */
382
+ _setCartesian3vSilent(cartesian, skipLonLat) {
383
+ let p = this._cartesian;
384
+
385
+ p.x = cartesian.x;
386
+ p.y = cartesian.y;
387
+ p.z = cartesian.z;
388
+
389
+ // billboards
390
+ this.billboard && this.billboard.setPosition3v(p);
391
+
392
+ // geoObject
393
+ this.geoObject && this.geoObject.setPosition3v(p);
394
+
395
+ // labels
396
+ this.label && this.label.setPosition3v(p);
397
+
398
+ // shape
399
+ this.shape && this.shape.setPosition3v(p);
400
+
401
+ for (let i = 0; i < this.childrenNodes.length; i++) {
402
+ this.childrenNodes[i].setCartesian(p.x, p.y, p.z);
403
+ }
404
+
405
+ let ec = this._entityCollection;
406
+
407
+ if (!skipLonLat && ec && ec.renderNode && ec.renderNode.ellipsoid) {
408
+ this._lonlat = ec.renderNode.ellipsoid.cartesianToLonLat(p);
409
+
410
+ if (Math.abs(this._lonlat.lat) < mercator.MAX_LAT) {
411
+ this._lonlatMerc = this._lonlat.forwardMercator();
412
+ } else {
413
+ this._lonlatMerc = null;
414
+ }
415
+ }
416
+ }
417
+
418
+ /**
419
+ * Gets entity geodetic coordinates.
420
+ * @public
421
+ * @returns {LonLat} -
422
+ */
423
+ getLonLat() {
424
+ return this._lonlat.clone();
425
+ }
426
+
427
+ /**
428
+ * Sets geodetic coordinates of the entity point object.
429
+ * @public
430
+ * @param {LonLat} lonlat - WGS84 coordinates.
431
+ */
432
+ setLonLat(lonlat) {
433
+ let l = this._lonlat;
434
+
435
+ l.lon = lonlat.lon;
436
+ l.lat = lonlat.lat;
437
+ l.height = lonlat.height;
438
+
439
+ let ec = this._entityCollection;
440
+ if (ec && ec.renderNode && ec.renderNode.ellipsoid) {
441
+ if (Math.abs(lonlat.lat) < mercator.MAX_LAT) {
442
+ this._lonlatMerc = lonlat.forwardMercator();
443
+ } else {
444
+ this._lonlatMerc = null;
445
+ }
446
+
447
+ ec.renderNode.ellipsoid.lonLatToCartesianRes(lonlat, this._cartesian);
448
+ this.setCartesian3v(this._cartesian);
449
+ }
450
+ }
451
+
452
+ /**
453
+ * Sets entity altitude over the planet.
454
+ * @public
455
+ * @param {number} altitude - Altitude.
456
+ */
457
+ setAltitude(altitude) {
458
+ this._altitude = altitude;
459
+ }
460
+
461
+ /**
462
+ * Sets entity altitude over the planet.
463
+ * @public
464
+ * @return {number} Altitude.
465
+ */
466
+ getAltitude() {
467
+ return this._altitude;
468
+ }
469
+
470
+ /**
471
+ * Returns carteain position.
472
+ * @public
473
+ * @returns {Vec3} -
474
+ */
475
+ getCartesian() {
476
+ return this._cartesian.clone();
477
+ }
478
+
479
+ /**
480
+ * Sets entity billboard.
481
+ * @public
482
+ * @param {Billboard} billboard - Billboard object.
483
+ * @returns {Billboard} -
484
+ */
485
+ setBillboard(billboard) {
486
+ if (this.billboard) {
487
+ this.billboard.remove();
488
+ }
489
+ this.billboard = billboard;
490
+ this.billboard._entity = this;
491
+ this.billboard.setPosition3v(this._cartesian);
492
+ this.billboard.setVisibility(this._visibility);
493
+ this._entityCollection && this._entityCollection._billboardHandler.add(billboard);
494
+ return billboard;
495
+ }
496
+
497
+ /**
498
+ * Sets entity label.
499
+ * @public
500
+ * @param {Label} label - Text label.
501
+ * @returns {Label} -
502
+ */
503
+ setLabel(label) {
504
+ if (this.label) {
505
+ this.label.remove();
506
+ }
507
+ this.label = label;
508
+ this.label._entity = this;
509
+ this.label.setPosition3v(this._cartesian);
510
+ this.label.setVisibility(this._visibility);
511
+ this._entityCollection && this._entityCollection._labelHandler.add(label);
512
+ return label;
513
+ }
514
+
515
+ /**
516
+ * Sets entity ray.
517
+ * @public
518
+ * @param {Ray} ray - Ray object.
519
+ * @returns {Ray} -
520
+ */
521
+ setRay(ray) {
522
+ if (this.ray) {
523
+ this.ray.remove();
524
+ }
525
+ this.ray = ray;
526
+ this.ray._entity = this;
527
+ this.ray.setVisibility(this._visibility);
528
+ this._entityCollection && this._entityCollection._rayHandler.add(ray);
529
+ return ray;
530
+ }
531
+
532
+ /**
533
+ * Sets entity shape.
534
+ * @public
535
+ * @param {BaseShape} shape - Shape object.
536
+ * @returns {Polyline} -
537
+ */
538
+ setShape(shape) {
539
+ if (this.shape) {
540
+ this.shape.remove();
541
+ }
542
+ this.shape = shape;
543
+ this.shape._entity = this;
544
+ this.shape.setPosition3v(this._cartesian);
545
+ this.shape.setVisibility(this._visibility);
546
+ this._entityCollection && this._entityCollection._shapeHandler.add(shape);
547
+ return shape;
548
+ }
549
+
550
+ /**
551
+ * Sets entity polyline.
552
+ * @public
553
+ * @param {Polyline} polyline - Polyline object.
554
+ * @returns {Polyline} -
555
+ */
556
+ setPolyline(polyline) {
557
+ if (this.polyline) {
558
+ this.polyline.remove();
559
+ }
560
+ this.polyline = polyline;
561
+ this.polyline._entity = this;
562
+ this.polyline.setVisibility(this._visibility);
563
+ this._entityCollection && this._entityCollection._polylineHandler.add(polyline);
564
+ return polyline;
565
+ }
566
+
567
+ /**
568
+ * Sets entity pointCloud.
569
+ * @public
570
+ * @param {PointCloud} pointCloud - PointCloud object.
571
+ * @returns {PointCloud} -
572
+ */
573
+ setPointCloud(pointCloud) {
574
+ if (this.pointCloud) {
575
+ this.pointCloud.remove();
576
+ }
577
+ this.pointCloud = pointCloud;
578
+ this.pointCloud._entity = this;
579
+ this.pointCloud.setVisibility(this._visibility);
580
+ this._entityCollection && this._entityCollection._pointCloudHandler.add(pointCloud);
581
+ return pointCloud;
582
+ }
583
+
584
+ /**
585
+ * Sets entity geometry.
586
+ * @public
587
+ * @param {Geometry} geometry - Geometry object.
588
+ * @returns {Geometry} -
589
+ */
590
+ setGeometry(geometry) {
591
+ if (this.geometry) {
592
+ this.geometry.remove();
593
+ }
594
+ this.geometry = geometry;
595
+ this.geometry._entity = this;
596
+ this.geometry.setVisibility(this._visibility);
597
+ this._layer && this._layer.add(this);
598
+ return geometry;
599
+ }
600
+
601
+ /**
602
+ * Sets entity geoObject.
603
+ * @public
604
+ * @param {GeoObject} geoObject - GeoObject.
605
+ * @returns {GeoObject} -
606
+ */
607
+ setGeoObject(geoObject) {
608
+ if (this.geoObject) {
609
+ this.geoObject.remove();
610
+ }
611
+ this.geoObject = geoObject;
612
+ this.geoObject._entity = this;
613
+ this.geoObject.setPosition3v(this._cartesian);
614
+ this.geoObject.setVisibility(this._visibility);
615
+ this._entityCollection && this._entityCollection._geoObjectHandler.add(geoObject);
616
+ return geoObject;
617
+ }
618
+
619
+ /**
620
+ * Sets entity strip.
621
+ * @public
622
+ * @param {Strip} strip - Strip object.
623
+ * @returns {Strip} -
624
+ */
625
+ setStrip(strip) {
626
+ if (this.strip) {
627
+ this.strip.remove();
628
+ }
629
+ this.strip = strip;
630
+ this.strip._entity = this;
631
+ this.strip.setVisibility(this._visibility);
632
+ this._entityCollection && this._entityCollection._stripHandler.add(strip);
633
+ return strip;
634
+ }
635
+
636
+ get layer() {
637
+ return this._layer;
638
+ }
639
+
640
+ get rendererEvents() {
641
+ if (this._layer) {
642
+ return this._layer.events;
643
+ } else if (this._entityCollection) {
644
+ return this._entityCollection.events;
645
+ }
646
+ return null;
647
+ }
648
+
649
+ /**
650
+ * Append child entity.
651
+ * @public
652
+ * @param {Entity} entity - Child entity.
653
+ */
654
+ appendChild(entity) {
655
+ entity._entityCollection = this._entityCollection;
656
+ entity._pickingColor = this._pickingColor;
657
+ entity.parent = this;
658
+ this.childrenNodes.push(entity);
659
+ this._entityCollection && this._entityCollection._addRecursively(entity);
660
+ }
661
+
662
+ /**
663
+ * Appends entity items(billboard, label etc.) picking color.
664
+ * @public
665
+ */
666
+ setPickingColor() {
667
+ let c = this._pickingColor;
668
+
669
+ // billboard
670
+ this.billboard && this.billboard.setPickingColor3v(c);
671
+
672
+ // label
673
+ this.label && this.label.setPickingColor3v(c);
674
+
675
+ // shape
676
+ this.shape && this.shape.setPickingColor3v(c);
677
+
678
+ // polyline
679
+ this.polyline && this.polyline.setPickingColor3v(c);
680
+
681
+ // ray
682
+ this.ray && this.ray.setPickingColor3v(c);
683
+
684
+ // strip
685
+ this.strip && this.strip.setPickingColor3v(c);
686
+
687
+ // billboard
688
+ this.geoObject && this.geoObject.setPickingColor3v(c);
689
+ for (let i = 0; i < this.childrenNodes.length; i++) {
690
+ this.childrenNodes[i].setPickingColor();
691
+ }
692
+ }
693
+
694
+ /**
695
+ * Return geodethic extent.
696
+ * @returns {Extent} -
697
+ */
698
+ getExtent() {
699
+ let res;
700
+ let c = this._lonlat;
701
+ if (this.billboard || this.label) {
702
+ res = new Extent(new LonLat(c.lon, c.lat), new LonLat(c.lon, c.lat));
703
+ } else {
704
+ res = new Extent(new LonLat(180.0, 90.0), new LonLat(-180.0, -90.0));
705
+ }
706
+
707
+ let sw = res.southWest,
708
+ ne = res.northEast;
709
+
710
+ if (this.polyline) {
711
+ let e = this.polyline.getExtent();
712
+ if (e.southWest.lon < sw.lon) sw.lon = e.southWest.lon;
713
+ if (e.southWest.lat < sw.lat) sw.lat = e.southWest.lat;
714
+ if (e.northEast.lon > ne.lon) ne.lon = e.northEast.lon;
715
+ if (e.northEast.lat > ne.lat) ne.lat = e.northEast.lat;
716
+ }
717
+
718
+ if (this.geometry) {
719
+ let e = this.geometry.getExtent();
720
+ if (e.southWest.lon < sw.lon) sw.lon = e.southWest.lon;
721
+ if (e.southWest.lat < sw.lat) sw.lat = e.southWest.lat;
722
+ if (e.northEast.lon > ne.lon) ne.lon = e.northEast.lon;
723
+ if (e.northEast.lat > ne.lat) ne.lat = e.northEast.lat;
724
+ }
725
+
726
+ for (let i = 0; i < this.childrenNodes.length; i++) {
727
+ let e = this.childrenNodes[i].getExtent();
728
+ if (e.southWest.lon < sw.lon) sw.lon = e.southWest.lon;
729
+ if (e.southWest.lat < sw.lat) sw.lat = e.southWest.lat;
730
+ if (e.northEast.lon > ne.lon) ne.lon = e.northEast.lon;
731
+ if (e.northEast.lat > ne.lat) ne.lat = e.northEast.lat;
732
+ }
733
+
734
+ return res;
735
+ }
736
+
737
+ isEqual(entity) {
738
+ return this.id === entity.id;
739
+ }
740
+ }
741
+
742
+ export { Entity };