bruce-cesium 2.8.4 → 2.8.6

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.
@@ -3732,28 +3732,27 @@
3732
3732
  EntityLabel.ATTACH_KEY = "nextspace-navigator-entity-label";
3733
3733
  var Label = /** @class */ (function () {
3734
3734
  function Label(params) {
3735
- var _this = this;
3735
+ var _a;
3736
+ // Counter to help differentiate between createLabel calls.
3737
+ this.counter = 0;
3736
3738
  // The label element itself.
3737
3739
  this.label = null;
3738
3740
  // Callback to kill render listener.
3739
3741
  this.renderRemoval = null;
3742
+ // Callback to kill terrain listener.
3743
+ this.terrChangedRemoval = null;
3740
3744
  // Indicates the label was disposed and any actions will be ignored.
3741
3745
  this.disposed = false;
3742
- this.creating = false;
3743
3746
  // Calculated pos3d for the label.
3744
3747
  // Override to make it draw at a different position.
3745
- this.pos3d = null;
3748
+ this._pos3d = null;
3749
+ this.prePositioned = false;
3746
3750
  this.rego = params.rego;
3747
3751
  this.viewer = params.viewer;
3748
3752
  this.api = params.api;
3749
3753
  this.visualRegister = params.visualRegister;
3750
- this.creating = true;
3751
- this.createCLabel(params.pos3d).then(function () {
3752
- _this.creating = false;
3753
- }).catch(function (e) {
3754
- console.error("Failed to create label", e);
3755
- _this.creating = false;
3756
- });
3754
+ this.prePositioned = !isNaN((_a = params.pos3d) === null || _a === void 0 ? void 0 : _a.x);
3755
+ this.createLabel(params.pos3d);
3757
3756
  }
3758
3757
  Object.defineProperty(Label.prototype, "Rego", {
3759
3758
  get: function () {
@@ -3769,9 +3768,14 @@
3769
3768
  enumerable: false,
3770
3769
  configurable: true
3771
3770
  });
3772
- Object.defineProperty(Label.prototype, "Creating", {
3771
+ Object.defineProperty(Label.prototype, "pos3d", {
3773
3772
  get: function () {
3774
- return this.creating;
3773
+ return this._pos3d;
3774
+ },
3775
+ set: function (val) {
3776
+ this._pos3d = val;
3777
+ this.prePositioned = !isNaN(val === null || val === void 0 ? void 0 : val.x);
3778
+ this.createLabel(val);
3775
3779
  },
3776
3780
  enumerable: false,
3777
3781
  configurable: true
@@ -3785,49 +3789,107 @@
3785
3789
  return;
3786
3790
  }
3787
3791
  this.disposed = true;
3788
- if (this.label &&
3789
- this.label.parentElement) {
3790
- this.label.parentElement.removeChild(this.label);
3791
- this.label = null;
3792
- }
3793
- if (this.renderRemoval) {
3794
- this.renderRemoval();
3795
- this.renderRemoval = null;
3796
- }
3792
+ this.removeLabel();
3797
3793
  };
3798
3794
  /**
3799
3795
  * Creates cesium entity for the label if one does not exist.
3800
- * @param pos23 optional position to create label at.
3796
+ * @param starterPos3d optional position to create label at. Ignored for billboard labels.
3801
3797
  */
3802
- Label.prototype.createCLabel = function (pos3d) {
3803
- var _a, _b, _c, _d, _e, _f;
3798
+ Label.prototype.createLabel = function (starterPos3d) {
3799
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
3804
3800
  return __awaiter(this, void 0, void 0, function () {
3805
- var heightRef, heightOffset, pixelOffset, visual, isClamped, height, scale, featureAny, propertyNames, latProp, lonProp, lat, lon, latIsRad, lonIsRad, latIsDeg, lonIsDeg, carto, text, ele, _lastDistance, _lastCameraPos, getDistance, MAX_DISTANCE, updateLabel;
3801
+ var pos3d, counter, sizeInM, heightRef, pixelOffset, visual, hierarchy, posses, centerIndex, pos1, pos2, pos3, posses, isClamped, centerIndex, size, height, scale, posses, centerIndex, text, type, e_1, ele, _lastDistance, _lastCameraPos, getDistance, MAX_DISTANCE, updateLabel, terrTimeout;
3806
3802
  var _this = this;
3807
- return __generator(this, function (_g) {
3808
- switch (_g.label) {
3803
+ return __generator(this, function (_l) {
3804
+ switch (_l.label) {
3809
3805
  case 0:
3810
- if (this.label || this.disposed) {
3806
+ pos3d = starterPos3d ? starterPos3d.clone() : null;
3807
+ counter = ++this.counter;
3808
+ if (this.disposed) {
3811
3809
  return [2 /*return*/];
3812
3810
  }
3811
+ this.removeLabel();
3812
+ sizeInM = 0;
3813
3813
  heightRef = null;
3814
- heightOffset = 10;
3815
- pixelOffset = new Cesium.Cartesian2(0, 0);
3816
- if (!isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) return [3 /*break*/, 3];
3814
+ pixelOffset = new Cesium.Cartesian2(0, -5);
3815
+ if (!isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
3816
+ heightRef = Cesium.HeightReference.NONE;
3817
+ pixelOffset.y = -15;
3818
+ }
3817
3819
  visual = (_a = this.rego) === null || _a === void 0 ? void 0 : _a.visual;
3820
+ // If pos3d is already set we'll run through the visuals to figure out the entity's size.
3821
+ // If unset then we'll use the entity's position to draw the label.
3818
3822
  if (visual && visual instanceof Cesium.Entity) {
3819
- if (visual.polygon) {
3820
- heightRef = getValue$1(this.viewer, visual.polygon.heightReference);
3823
+ if (!this.prePositioned && visual.polygon) {
3824
+ hierarchy = getValue$1(this.viewer, visual.polygon.hierarchy);
3825
+ if ((_b = hierarchy === null || hierarchy === void 0 ? void 0 : hierarchy.positions) === null || _b === void 0 ? void 0 : _b.length) {
3826
+ posses = [].concat(hierarchy.positions);
3827
+ try {
3828
+ sizeInM = (_c = exports.MeasureUtils.MeasurePolygon({
3829
+ posses: posses,
3830
+ })) === null || _c === void 0 ? void 0 : _c.area;
3831
+ if (isNaN(sizeInM)) {
3832
+ sizeInM = 0;
3833
+ }
3834
+ }
3835
+ catch (e) {
3836
+ console.error("Failed to measure polygon", e);
3837
+ }
3838
+ if (isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
3839
+ heightRef = getValue$1(this.viewer, visual.polygon.heightReference);
3840
+ if (bruceModels.Cartes.IsRing3Closed(posses)) {
3841
+ posses.pop();
3842
+ }
3843
+ centerIndex = Math.floor(posses.length / 2);
3844
+ if (posses.length >= 3) {
3845
+ pos1 = posses[centerIndex];
3846
+ pos2 = posses[0];
3847
+ pos3 = posses[posses.length - 1];
3848
+ if (pos1 && pos2 && pos3) {
3849
+ pos3d = Cesium.Cartesian3.midpoint(pos1, pos2, new Cesium.Cartesian3());
3850
+ pos3d = Cesium.Cartesian3.midpoint(pos3d, pos3, new Cesium.Cartesian3());
3851
+ }
3852
+ }
3853
+ if (isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
3854
+ pos3d = posses[centerIndex];
3855
+ }
3856
+ }
3857
+ }
3821
3858
  }
3822
- else if (visual.polyline) {
3823
- isClamped = getValue$1(this.viewer, visual.polyline.clampToGround);
3824
- heightRef = isClamped == false ? Cesium.HeightReference.NONE : Cesium.HeightReference.CLAMP_TO_GROUND;
3859
+ else if (!this.prePositioned && visual.polyline) {
3860
+ posses = getValue$1(this.viewer, visual.polyline.positions);
3861
+ if (posses === null || posses === void 0 ? void 0 : posses.length) {
3862
+ if (isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
3863
+ isClamped = getValue$1(this.viewer, visual.polyline.clampToGround);
3864
+ heightRef = isClamped == false ? Cesium.HeightReference.NONE : Cesium.HeightReference.CLAMP_TO_GROUND;
3865
+ centerIndex = Math.floor(posses.length / 2);
3866
+ pos3d = posses[centerIndex];
3867
+ }
3868
+ try {
3869
+ sizeInM = (_d = exports.MeasureUtils.MeasurePolyline({
3870
+ posses: posses,
3871
+ })) === null || _d === void 0 ? void 0 : _d.totalLength;
3872
+ if (isNaN(sizeInM)) {
3873
+ sizeInM = 0;
3874
+ }
3875
+ }
3876
+ catch (e) {
3877
+ console.error("Failed to measure polyline", e);
3878
+ }
3879
+ }
3825
3880
  }
3826
3881
  else if (visual.model) {
3827
- heightRef = getValue$1(this.viewer, visual.model.heightReference);
3882
+ if (isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
3883
+ heightRef = getValue$1(this.viewer, visual.model.heightReference);
3884
+ }
3828
3885
  }
3829
3886
  else if (visual.point) {
3830
3887
  heightRef = getValue$1(this.viewer, visual.point.heightReference);
3888
+ size = getValue$1(this.viewer, visual.point.pixelSize);
3889
+ if (size) {
3890
+ pixelOffset.y = -(size / 2);
3891
+ pixelOffset.y -= 5;
3892
+ }
3831
3893
  }
3832
3894
  else if (visual.billboard) {
3833
3895
  heightRef = getValue$1(this.viewer, visual.billboard.heightReference);
@@ -3835,63 +3897,57 @@
3835
3897
  if (height) {
3836
3898
  scale = EnsureNumber(getValue$1(this.viewer, visual.billboard.scale), 1);
3837
3899
  pixelOffset.y = -(height * scale);
3900
+ pixelOffset.y -= 5;
3838
3901
  }
3839
- // Flat padding.
3840
- pixelOffset.y -= 5;
3841
- heightOffset = 0;
3842
3902
  }
3843
- else if (visual.corridor) {
3844
- heightRef = getValue$1(this.viewer, visual.corridor.heightReference);
3845
- }
3846
- else if (visual.ellipse) {
3847
- heightRef = getValue$1(this.viewer, visual.ellipse.heightReference);
3848
- }
3849
- pos3d = getValue$1(this.viewer, visual.position);
3850
- }
3851
- else if (visual instanceof Cesium.Cesium3DTileFeature) {
3852
- featureAny = visual;
3853
- propertyNames = featureAny.getPropertyNames ? featureAny.getPropertyNames() : (_b = featureAny.getPropertyIds) === null || _b === void 0 ? void 0 : _b.call(featureAny);
3854
- if (propertyNames === null || propertyNames === void 0 ? void 0 : propertyNames.length) {
3855
- latProp = propertyNames.find(function (x) { return x.toLowerCase().includes("lat"); });
3856
- lonProp = propertyNames.find(function (x) { return x.toLowerCase().includes("lon"); });
3857
- lat = latProp ? featureAny.getProperty(latProp) : null;
3858
- lon = lonProp ? featureAny.getProperty(lonProp) : null;
3859
- if (lat && lon) {
3860
- latIsRad = typeof lat == "number" && lat > -Math.PI && lat < Math.PI;
3861
- lonIsRad = typeof lon == "number" && lon > -Math.PI && lon < Math.PI;
3862
- latIsDeg = typeof lat == "number" && lat > -180 && lat < 180;
3863
- lonIsDeg = typeof lon == "number" && lon > -180 && lon < 180;
3864
- if (latIsRad && lonIsRad) {
3865
- pos3d = Cesium.Cartographic.toCartesian(Cesium.Cartographic.fromRadians(lon, lat, 0));
3903
+ else if (!this.prePositioned && visual.corridor) {
3904
+ posses = getValue$1(this.viewer, visual.corridor.positions);
3905
+ if (posses === null || posses === void 0 ? void 0 : posses.length) {
3906
+ if (isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
3907
+ heightRef = getValue$1(this.viewer, visual.corridor.heightReference);
3908
+ centerIndex = Math.floor(posses.length / 2);
3909
+ pos3d = posses[centerIndex];
3866
3910
  }
3867
- else if (latIsDeg && lonIsDeg) {
3868
- pos3d = Cesium.Cartographic.toCartesian(Cesium.Cartographic.fromDegrees(lon, lat, 0));
3911
+ try {
3912
+ sizeInM = (_e = exports.MeasureUtils.MeasurePolyline({
3913
+ posses: posses,
3914
+ })) === null || _e === void 0 ? void 0 : _e.totalLength;
3915
+ if (isNaN(sizeInM)) {
3916
+ sizeInM = 0;
3917
+ }
3918
+ }
3919
+ catch (e) {
3920
+ console.error("Failed to measure polyline", e);
3869
3921
  }
3870
- heightRef = Cesium.HeightReference.CLAMP_TO_GROUND;
3871
3922
  }
3872
3923
  }
3924
+ else if (visual.ellipse) {
3925
+ if (isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
3926
+ heightRef = getValue$1(this.viewer, visual.ellipse.heightReference);
3927
+ }
3928
+ }
3929
+ if (visual.billboard || visual.point || isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
3930
+ pos3d = getValue$1(this.viewer, visual.position);
3931
+ }
3873
3932
  }
3874
3933
  if (!isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) return [3 /*break*/, 2];
3875
3934
  return [4 /*yield*/, exports.EntityUtils.GetPosAsync({
3876
3935
  viewer: this.viewer,
3877
3936
  api: this.api,
3878
- entityId: (_c = this.rego) === null || _c === void 0 ? void 0 : _c.entityId,
3937
+ entityId: (_f = this.rego) === null || _f === void 0 ? void 0 : _f.entityId,
3879
3938
  returnHeightRef: Cesium.HeightReference.NONE,
3880
- tilesetId: (_d = this.rego) === null || _d === void 0 ? void 0 : _d.tilesetId,
3939
+ recordHeightRef: heightRef == null || isNaN(heightRef) ? Cesium.HeightReference.NONE : heightRef,
3940
+ tilesetId: (_g = this.rego) === null || _g === void 0 ? void 0 : _g.tilesetId,
3881
3941
  visualRegister: this.visualRegister
3882
3942
  })];
3883
3943
  case 1:
3884
- pos3d = _g.sent();
3944
+ pos3d = _l.sent();
3885
3945
  heightRef = Cesium.HeightReference.NONE;
3886
- _g.label = 2;
3887
- case 2: return [3 /*break*/, 4];
3888
- case 3:
3889
- heightRef = Cesium.HeightReference.NONE;
3890
- // Flat padding.
3891
- heightOffset = 0;
3892
- pixelOffset.y = -15;
3893
- _g.label = 4;
3894
- case 4:
3946
+ if (this.counter != counter) {
3947
+ return [2 /*return*/];
3948
+ }
3949
+ _l.label = 2;
3950
+ case 2:
3895
3951
  if (isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
3896
3952
  return [2 /*return*/];
3897
3953
  }
@@ -3904,18 +3960,42 @@
3904
3960
  if (isNaN(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) {
3905
3961
  return [2 /*return*/];
3906
3962
  }
3907
- if (heightOffset) {
3908
- carto = Cesium.Cartographic.fromCartesian(pos3d);
3909
- // TODO: Check if we need to use heightRef and adjust to absolute height.
3910
- // TODO: We should be smarter and not just add 10m to the height. Eg: use polygon extrusion.
3911
- carto.height += heightOffset;
3912
- pos3d = Cesium.Cartographic.toCartesian(carto);
3963
+ this._pos3d = pos3d;
3964
+ text = "Unknown Entity";
3965
+ if (!((_h = this.rego) === null || _h === void 0 ? void 0 : _h.relation)) return [3 /*break*/, 7];
3966
+ type = this.rego.relation.RelationType;
3967
+ if (!(!type && this.rego.relation["Related.Entity.ID"])) return [3 /*break*/, 6];
3968
+ _l.label = 3;
3969
+ case 3:
3970
+ _l.trys.push([3, 5, , 6]);
3971
+ return [4 /*yield*/, bruceModels.EntityRelationType.Get({
3972
+ api: this.api,
3973
+ relationTypeId: this.rego.relation["Related.Entity.ID"]
3974
+ })];
3975
+ case 4:
3976
+ type = (_j = (_l.sent())) === null || _j === void 0 ? void 0 : _j.relationType;
3977
+ return [3 /*break*/, 6];
3978
+ case 5:
3979
+ e_1 = _l.sent();
3980
+ console.error(e_1);
3981
+ return [3 /*break*/, 6];
3982
+ case 6:
3983
+ if (type) {
3984
+ text = type.Name ? type.Name : (type.ForwardName + "/" + type.ReverseName);
3985
+ }
3986
+ return [3 /*break*/, 8];
3987
+ case 7:
3988
+ if ((_k = this.rego) === null || _k === void 0 ? void 0 : _k.name) {
3989
+ text = this.rego.name;
3913
3990
  }
3914
- this.pos3d = pos3d;
3915
- text = (_f = (_e = this.rego) === null || _e === void 0 ? void 0 : _e.name) !== null && _f !== void 0 ? _f : "Unnamed Entity";
3991
+ else {
3992
+ text = "Unnamed Entity";
3993
+ }
3994
+ _l.label = 8;
3995
+ case 8:
3916
3996
  ele = document.createElement("div");
3917
3997
  ele.innerHTML = text;
3918
- ele.setAttribute("style", "\n position: absolute;\n z-index: 1;\n display: none;\n pointer-events: none;\n padding: 6px 8px;\n border-radius: 6px;\n font-family: Arial;\n font-size: 12px;\n -webkit-backdrop-filter: blur(20px);\n backdrop-filter: blur(20px);\n background: rgba(33,39,42,.8);\n border-radius: 9px;\n box-shadow: 0 0 1px rgba(18,22,25,.36),0 18px 36px -4px rgba(18,22,25,.36);\n color: #ffffff;\n }");
3998
+ ele.setAttribute("style", "\n position: absolute;\n z-index: 0;\n display: none;\n pointer-events: none;\n padding: 6px 8px;\n border-radius: 6px;\n font-family: Arial;\n font-size: 12px;\n -webkit-backdrop-filter: blur(20px);\n backdrop-filter: blur(20px);\n background: rgba(33,39,42,.8);\n border-radius: 9px;\n box-shadow: 0 0 1px rgba(18,22,25,.36),0 18px 36px -4px rgba(18,22,25,.36);\n color: #ffffff;\n }");
3919
3999
  this.label = ele;
3920
4000
  this.viewer.container.appendChild(ele);
3921
4001
  _lastDistance = null;
@@ -3965,23 +4045,30 @@
3965
4045
  // }
3966
4046
  // }
3967
4047
  // }
3968
- var distance = Cesium.Cartesian3.distance(cameraPos, _this.pos3d);
4048
+ var distance = Cesium.Cartesian3.distance(cameraPos, _this._pos3d);
3969
4049
  _lastDistance = distance;
3970
4050
  _lastCameraPos = cameraPos.clone();
3971
4051
  return distance;
3972
4052
  };
3973
4053
  MAX_DISTANCE = 5000;
4054
+ if (sizeInM > 30) {
4055
+ MAX_DISTANCE = Math.max(MAX_DISTANCE, sizeInM / 2);
4056
+ }
3974
4057
  updateLabel = function () {
3975
4058
  var _a;
3976
- if (_this.disposed || isNaN((_a = _this.pos3d) === null || _a === void 0 ? void 0 : _a.x)) {
4059
+ if (_this.disposed || isNaN((_a = _this._pos3d) === null || _a === void 0 ? void 0 : _a.x)) {
4060
+ return;
4061
+ }
4062
+ if (!_this.viewer || _this.viewer.isDestroyed()) {
4063
+ _this.Dispose();
3977
4064
  return;
3978
4065
  }
3979
4066
  var distance = getDistance();
3980
- if (isNaN(distance) || distance >= MAX_DISTANCE) {
4067
+ if (isNaN(distance) || (!_this.prePositioned && distance >= MAX_DISTANCE)) {
3981
4068
  ele.style.display = "none";
3982
4069
  return;
3983
4070
  }
3984
- var pos2d = Cesium.SceneTransforms.wgs84ToWindowCoordinates(_this.viewer.scene, _this.pos3d);
4071
+ var pos2d = Cesium.SceneTransforms.wgs84ToWindowCoordinates(_this.viewer.scene, _this._pos3d);
3985
4072
  if (isNaN(pos2d === null || pos2d === void 0 ? void 0 : pos2d.x)) {
3986
4073
  ele.style.display = "none";
3987
4074
  return;
@@ -3995,7 +4082,7 @@
3995
4082
  ele.style.display = "block";
3996
4083
  // Check scale to apply based on distance between camera and pos3d.
3997
4084
  // We'll make the label smaller and less transparent when further away.
3998
- var scale = 1 - (distance / MAX_DISTANCE);
4085
+ var scale = _this.prePositioned ? 1 : 1 - (distance / MAX_DISTANCE);
3999
4086
  if (scale && scale > 0.15) {
4000
4087
  ele.style.transform = "scale(".concat(scale, ")");
4001
4088
  ele.style.opacity = "".concat(scale);
@@ -4011,11 +4098,42 @@
4011
4098
  };
4012
4099
  this.renderRemoval = this.viewer.scene.postUpdate.addEventListener(updateLabel);
4013
4100
  updateLabel();
4101
+ terrTimeout = null;
4102
+ this.terrChangedRemoval = this.viewer.scene.terrainProviderChanged.addEventListener(function () {
4103
+ if (_this.disposed) {
4104
+ return;
4105
+ }
4106
+ clearTimeout(terrTimeout);
4107
+ terrTimeout = setTimeout(function () {
4108
+ if (counter != _this.counter) {
4109
+ return;
4110
+ }
4111
+ _this.createLabel(starterPos3d);
4112
+ }, 2500);
4113
+ });
4014
4114
  return [2 /*return*/];
4015
4115
  }
4016
4116
  });
4017
4117
  });
4018
4118
  };
4119
+ /**
4120
+ * Removes label from the viewer and unsubscribes from events.
4121
+ */
4122
+ Label.prototype.removeLabel = function () {
4123
+ if (this.label &&
4124
+ this.label.parentElement) {
4125
+ this.label.parentElement.removeChild(this.label);
4126
+ this.label = null;
4127
+ }
4128
+ if (this.renderRemoval) {
4129
+ this.renderRemoval();
4130
+ this.renderRemoval = null;
4131
+ }
4132
+ if (this.terrChangedRemoval) {
4133
+ this.terrChangedRemoval();
4134
+ this.terrChangedRemoval = null;
4135
+ }
4136
+ };
4019
4137
  return Label;
4020
4138
  }());
4021
4139
  EntityLabel.Label = Label;
@@ -15950,7 +16068,7 @@
15950
16068
  ViewerUtils.CreateWidgets = CreateWidgets;
15951
16069
  })(exports.ViewerUtils || (exports.ViewerUtils = {}));
15952
16070
 
15953
- var VERSION$1 = "2.8.4";
16071
+ var VERSION$1 = "2.8.6";
15954
16072
 
15955
16073
  exports.VERSION = VERSION$1;
15956
16074
  exports.CesiumViewMonitor = CesiumViewMonitor;