bruce-cesium 2.9.4 → 2.9.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.
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -42,6 +53,7 @@ var Cesium = require("cesium");
42
53
  var drawing_utils_1 = require("./drawing-utils");
43
54
  var measure_utils_1 = require("./measure-utils");
44
55
  var js_utils_1 = require("../internal/js-utils");
56
+ var view_utils_1 = require("./view-utils");
45
57
  /**
46
58
  * Returns if a given visual is alive and in the scene.
47
59
  * @param viewer
@@ -206,10 +218,522 @@ function GetValue(viewer, obj) {
206
218
  }
207
219
  var EntityUtils;
208
220
  (function (EntityUtils) {
221
+ /**
222
+ * Returns the location for given entity or entities.
223
+ * If available it will also compute a rectangle.
224
+ * It is recommended to fly-to a rectangle if available as it will ensure the full geometry is in view.
225
+ * @param params
226
+ */
227
+ function GetLocation(params) {
228
+ var _a;
229
+ return __awaiter(this, void 0, void 0, function () {
230
+ var api, viewer, samples, visualRegister, minimumAlt, paddingAlt, MAX_DISTANCE_BETWEEN_SAMPLES, MAX_TERRAIN_SAMPLES, terrainSamples, MAX_API_CALLS, apiCalls, MAX_VALID_SAMPLES, validSamples, MIN_RECT_DIAGONAL_LENGTH, data, processPosHeight, ensureHeightRefs, getEntityPositions, allPosses, i, sample, samplePosses, valid, j, samplePos, k, allPos, distance, rect, factor, marginX, marginY, diagonalLen, terrData, posCarto, height, MIN_POSSES_LEN;
231
+ var _this = this;
232
+ return __generator(this, function (_b) {
233
+ switch (_b.label) {
234
+ case 0:
235
+ api = params.api, viewer = params.viewer, samples = params.samples, visualRegister = params.visualRegister, minimumAlt = params.minimumAlt, paddingAlt = params.paddingAlt;
236
+ if (!paddingAlt) {
237
+ paddingAlt = 0;
238
+ }
239
+ MAX_DISTANCE_BETWEEN_SAMPLES = 5000;
240
+ MAX_TERRAIN_SAMPLES = 25;
241
+ terrainSamples = 0;
242
+ MAX_API_CALLS = 10;
243
+ apiCalls = 0;
244
+ MAX_VALID_SAMPLES = 20;
245
+ validSamples = 0;
246
+ MIN_RECT_DIAGONAL_LENGTH = isNaN(minimumAlt) || minimumAlt == null ? 0 : minimumAlt * 10;
247
+ if (MIN_RECT_DIAGONAL_LENGTH < 1500) {
248
+ MIN_RECT_DIAGONAL_LENGTH = 1500;
249
+ }
250
+ data = {
251
+ pos3d: null,
252
+ rectangle: null
253
+ };
254
+ processPosHeight = function (pos3d, heightRef) { return __awaiter(_this, void 0, void 0, function () {
255
+ var carto, baseHeight, terrData, height, carto;
256
+ return __generator(this, function (_a) {
257
+ switch (_a.label) {
258
+ case 0:
259
+ if (!(!isNaN(minimumAlt) && minimumAlt != null)) return [3 /*break*/, 5];
260
+ carto = Cesium.Cartographic.fromCartesian(pos3d);
261
+ if (!(carto === null || carto === void 0 ? void 0 : carto.latitude)) return [3 /*break*/, 4];
262
+ baseHeight = 0;
263
+ if (!(heightRef == null || heightRef == Cesium.HeightReference.RELATIVE_TO_GROUND || heightRef == Cesium.HeightReference.NONE)) return [3 /*break*/, 3];
264
+ if (!(terrainSamples > MAX_TERRAIN_SAMPLES)) return [3 /*break*/, 1];
265
+ baseHeight = viewer.scene.globe.getHeight(carto);
266
+ return [3 /*break*/, 3];
267
+ case 1: return [4 /*yield*/, drawing_utils_1.DrawingUtils.GetTerrainHeight({
268
+ pos3d: pos3d,
269
+ viewer: viewer
270
+ })];
271
+ case 2:
272
+ terrData = _a.sent();
273
+ baseHeight = terrData === null || terrData === void 0 ? void 0 : terrData.height;
274
+ terrainSamples += 1;
275
+ _a.label = 3;
276
+ case 3:
277
+ if (baseHeight != undefined && !isNaN(baseHeight)) {
278
+ height = baseHeight + minimumAlt;
279
+ if (height > carto.height) {
280
+ carto.height = height;
281
+ }
282
+ return [2 /*return*/, Cesium.Cartesian3.fromRadians(carto.longitude, carto.latitude, carto.height + paddingAlt)];
283
+ }
284
+ _a.label = 4;
285
+ case 4: return [3 /*break*/, 6];
286
+ case 5:
287
+ if (paddingAlt) {
288
+ carto = Cesium.Cartographic.fromCartesian(pos3d);
289
+ if (carto === null || carto === void 0 ? void 0 : carto.latitude) {
290
+ return [2 /*return*/, Cesium.Cartesian3.fromRadians(carto.longitude, carto.latitude, carto.height + paddingAlt)];
291
+ }
292
+ }
293
+ _a.label = 6;
294
+ case 6: return [2 /*return*/, pos3d];
295
+ }
296
+ });
297
+ }); };
298
+ ensureHeightRefs = function (posses, sample) { return __awaiter(_this, void 0, void 0, function () {
299
+ var heightRef, returnHeightRef, i, _a, _b;
300
+ return __generator(this, function (_c) {
301
+ switch (_c.label) {
302
+ case 0:
303
+ if (!(posses === null || posses === void 0 ? void 0 : posses.length)) return [3 /*break*/, 4];
304
+ heightRef = sample.heightRef;
305
+ returnHeightRef = sample.returnHeightRef;
306
+ i = 0;
307
+ _c.label = 1;
308
+ case 1:
309
+ if (!(i < posses.length)) return [3 /*break*/, 4];
310
+ if (heightRef != null && returnHeightRef != null && heightRef != returnHeightRef) {
311
+ posses[i] = drawing_utils_1.DrawingUtils.EnsurePosHeight({
312
+ desiredHeightRef: returnHeightRef,
313
+ heightRef: heightRef,
314
+ pos3d: posses[i],
315
+ viewer: viewer
316
+ });
317
+ }
318
+ _a = posses;
319
+ _b = i;
320
+ return [4 /*yield*/, processPosHeight(posses[i], returnHeightRef)];
321
+ case 2:
322
+ _a[_b] = _c.sent();
323
+ _c.label = 3;
324
+ case 3:
325
+ i++;
326
+ return [3 /*break*/, 1];
327
+ case 4: return [2 /*return*/];
328
+ }
329
+ });
330
+ }); };
331
+ getEntityPositions = function (sample) { return __awaiter(_this, void 0, void 0, function () {
332
+ var entityId, entity, tileset, tilesetId, lat, lon, e_1, e_2, evaluateRendered, evaluateRecord, renderedPosses, recordPosses, tSettings, pos3d, alt, alt, heading, pitch, roll, matrix4, offset, m1, hpr, transform, transformedOffset;
333
+ var _this = this;
334
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
335
+ return __generator(this, function (_o) {
336
+ switch (_o.label) {
337
+ case 0:
338
+ entityId = sample.entityId, entity = sample.entity, tileset = sample.tileset, tilesetId = sample.tilesetId;
339
+ if (!!entity) return [3 /*break*/, 9];
340
+ lat = 0;
341
+ lon = 0;
342
+ if (!(apiCalls < MAX_API_CALLS)) return [3 /*break*/, 9];
343
+ // Counting this whole area as a single call because it has a single purpose.
344
+ apiCalls += 1;
345
+ _o.label = 1;
346
+ case 1:
347
+ _o.trys.push([1, 3, , 4]);
348
+ return [4 /*yield*/, bruce_models_1.Entity.Get({
349
+ api: api,
350
+ entityId: entityId,
351
+ expandLocation: false
352
+ })];
353
+ case 2:
354
+ entity = (_o.sent()).entity;
355
+ lat = +((_a = entity === null || entity === void 0 ? void 0 : entity.location) === null || _a === void 0 ? void 0 : _a.latitude);
356
+ lon = +((_b = entity === null || entity === void 0 ? void 0 : entity.location) === null || _b === void 0 ? void 0 : _b.longitude);
357
+ return [3 /*break*/, 4];
358
+ case 3:
359
+ e_1 = _o.sent();
360
+ console.error(e_1);
361
+ return [3 /*break*/, 4];
362
+ case 4:
363
+ if (!(entity && (isNaN(lat) || isNaN(lon) || (lat == 0 && lon == 0)))) return [3 /*break*/, 9];
364
+ _o.label = 5;
365
+ case 5:
366
+ _o.trys.push([5, 7, , 9]);
367
+ return [4 /*yield*/, bruce_models_1.Entity.Get({
368
+ api: api,
369
+ entityId: entityId,
370
+ expandLocation: true
371
+ })];
372
+ case 6:
373
+ entity = (_o.sent()).entity;
374
+ return [3 /*break*/, 9];
375
+ case 7:
376
+ e_2 = _o.sent();
377
+ console.warn(e_2);
378
+ return [4 /*yield*/, bruce_models_1.Entity.Get({
379
+ api: api,
380
+ entityId: entityId,
381
+ expandLocation: false
382
+ })];
383
+ case 8:
384
+ entity = (_o.sent()).entity;
385
+ return [3 /*break*/, 9];
386
+ case 9:
387
+ if (!entity) {
388
+ return [2 /*return*/, []];
389
+ }
390
+ if (!tilesetId) {
391
+ tilesetId = ((_c = entity.tilesetID) === null || _c === void 0 ? void 0 : _c.length) ? entity.tilesetID[0] : tilesetId;
392
+ }
393
+ evaluateRendered = function () { return __awaiter(_this, void 0, void 0, function () {
394
+ var rego, posses, visual, visualHeightRef, hierarchy, hPosses, hPossesSample, step, i, pPosses, pPossesSample, step, i, pPosses, pPossesSample, step, i, pos3d_1;
395
+ return __generator(this, function (_a) {
396
+ switch (_a.label) {
397
+ case 0:
398
+ rego = visualRegister ? visualRegister.GetRego({
399
+ entityId: entity.Bruce.ID,
400
+ }) : null;
401
+ posses = [];
402
+ if (!((rego === null || rego === void 0 ? void 0 : rego.visual) instanceof Cesium.Entity)) return [3 /*break*/, 14];
403
+ visual = rego.visual;
404
+ visualHeightRef = Cesium.HeightReference.RELATIVE_TO_GROUND;
405
+ if (!visual.model) return [3 /*break*/, 1];
406
+ visualHeightRef = GetValue(viewer, visual.model.heightReference);
407
+ return [3 /*break*/, 12];
408
+ case 1:
409
+ if (!visual.polygon) return [3 /*break*/, 4];
410
+ visualHeightRef = GetValue(viewer, visual.polygon.heightReference);
411
+ hierarchy = GetValue(viewer, visual.polygon.hierarchy);
412
+ if (!(hierarchy === null || hierarchy === void 0 ? void 0 : hierarchy.positions)) return [3 /*break*/, 3];
413
+ hPosses = hierarchy.positions.map(function (x) { return x.clone ? x.clone() : x; });
414
+ hPossesSample = [];
415
+ step = Math.floor(hPosses.length / 5);
416
+ for (i = 0; i < hPosses.length; i += step) {
417
+ hPossesSample.push(hPosses[i]);
418
+ }
419
+ return [4 /*yield*/, ensureHeightRefs(hPossesSample, __assign(__assign({}, sample), { heightRef: visualHeightRef }))];
420
+ case 2:
421
+ _a.sent();
422
+ posses = posses.concat(hPossesSample);
423
+ _a.label = 3;
424
+ case 3: return [3 /*break*/, 12];
425
+ case 4:
426
+ if (!visual.polyline) return [3 /*break*/, 7];
427
+ visualHeightRef = GetValue(viewer, visual.polyline.clampToGround) ?
428
+ Cesium.HeightReference.CLAMP_TO_GROUND : Cesium.HeightReference.NONE;
429
+ pPosses = GetValue(viewer, visual.polyline.positions);
430
+ if (!(pPosses === null || pPosses === void 0 ? void 0 : pPosses.length)) return [3 /*break*/, 6];
431
+ // Grab 5 positions from the polyline at varied indexes.
432
+ pPosses = pPosses.map(function (x) { return x.clone ? x.clone() : x; });
433
+ pPossesSample = [];
434
+ step = Math.floor(pPosses.length / 5);
435
+ for (i = 0; i < pPosses.length; i += step) {
436
+ pPossesSample.push(pPosses[i]);
437
+ }
438
+ return [4 /*yield*/, ensureHeightRefs(pPossesSample, __assign(__assign({}, sample), { heightRef: visualHeightRef }))];
439
+ case 5:
440
+ _a.sent();
441
+ posses = posses.concat(pPossesSample);
442
+ _a.label = 6;
443
+ case 6: return [3 /*break*/, 12];
444
+ case 7:
445
+ if (!visual.billboard) return [3 /*break*/, 8];
446
+ visualHeightRef = GetValue(viewer, visual.billboard.heightReference);
447
+ return [3 /*break*/, 12];
448
+ case 8:
449
+ if (!visual.corridor) return [3 /*break*/, 11];
450
+ visualHeightRef = GetValue(viewer, visual.corridor.heightReference);
451
+ pPosses = GetValue(viewer, visual.corridor.positions);
452
+ if (!(pPosses === null || pPosses === void 0 ? void 0 : pPosses.length)) return [3 /*break*/, 10];
453
+ // Grab 5 positions from the corridor at varied indexes.
454
+ pPosses = pPosses.map(function (x) { return x.clone ? x.clone() : x; });
455
+ pPossesSample = [];
456
+ step = Math.floor(pPosses.length / 5);
457
+ for (i = 0; i < pPosses.length; i += step) {
458
+ pPossesSample.push(pPosses[i]);
459
+ }
460
+ return [4 /*yield*/, ensureHeightRefs(pPossesSample, __assign(__assign({}, sample), { heightRef: visualHeightRef }))];
461
+ case 9:
462
+ _a.sent();
463
+ posses = posses.concat(pPossesSample);
464
+ _a.label = 10;
465
+ case 10: return [3 /*break*/, 12];
466
+ case 11:
467
+ if (visual.ellipse) {
468
+ visualHeightRef = GetValue(viewer, visual.ellipse.heightReference);
469
+ }
470
+ else if (visual.point) {
471
+ visualHeightRef = GetValue(viewer, visual.point.heightReference);
472
+ }
473
+ _a.label = 12;
474
+ case 12:
475
+ pos3d_1 = GetValue(viewer, visual.position);
476
+ if (!(pos3d_1 === null || pos3d_1 === void 0 ? void 0 : pos3d_1.x)) return [3 /*break*/, 14];
477
+ pos3d_1 = drawing_utils_1.DrawingUtils.EnsurePosHeight({
478
+ desiredHeightRef: sample.returnHeightRef,
479
+ heightRef: visualHeightRef,
480
+ pos3d: pos3d_1,
481
+ viewer: viewer
482
+ });
483
+ return [4 /*yield*/, processPosHeight(pos3d_1, sample.returnHeightRef)];
484
+ case 13:
485
+ pos3d_1 = _a.sent();
486
+ posses.push(pos3d_1);
487
+ _a.label = 14;
488
+ case 14: return [2 /*return*/, posses];
489
+ }
490
+ });
491
+ }); };
492
+ evaluateRecord = function () { return __awaiter(_this, void 0, void 0, function () {
493
+ var posses, location_1, latitude, longitude, pos3d_2, pointStr, points, point, pos3d_3, lineStr, points, pPosses, pRings, boundary, points, pPosses, point, bPosses;
494
+ return __generator(this, function (_a) {
495
+ switch (_a.label) {
496
+ case 0:
497
+ posses = [];
498
+ if (entity.location && bruce_models_1.Carto.ValidateCarto(entity.location)) {
499
+ location_1 = entity.location;
500
+ latitude = (0, js_utils_1.EnsureNumber)(location_1.latitude);
501
+ longitude = (0, js_utils_1.EnsureNumber)(location_1.longitude);
502
+ // Disallowing exact 0.
503
+ if (latitude || longitude) {
504
+ pos3d_2 = Cesium.Cartesian3.fromDegrees(longitude, latitude, (0, js_utils_1.EnsureNumber)(location_1.altitude));
505
+ posses.push(pos3d_2);
506
+ }
507
+ }
508
+ if (!(entity.geometry && typeof entity.geometry == "object")) return [3 /*break*/, 4];
509
+ pointStr = entity.geometry.Point;
510
+ if (pointStr && typeof pointStr == "string") {
511
+ points = bruce_models_1.Geometry.ParsePoints(pointStr);
512
+ point = points.length > 0 ? points[0] : null;
513
+ if (point && bruce_models_1.Carto.ValidateCarto(point)) {
514
+ pos3d_3 = Cesium.Cartesian3.fromDegrees((0, js_utils_1.EnsureNumber)(point.longitude), (0, js_utils_1.EnsureNumber)(point.latitude), (0, js_utils_1.EnsureNumber)(point.altitude));
515
+ posses.push(pos3d_3);
516
+ }
517
+ }
518
+ lineStr = entity.geometry.LineString;
519
+ if (!(lineStr && typeof lineStr == "string")) return [3 /*break*/, 2];
520
+ points = bruce_models_1.Geometry.ParsePoints(lineStr);
521
+ if (!(points.length > 0)) return [3 /*break*/, 2];
522
+ pPosses = points.map(function (x) { return Cesium.Cartesian3.fromDegrees((0, js_utils_1.EnsureNumber)(x.longitude), (0, js_utils_1.EnsureNumber)(x.latitude), (0, js_utils_1.EnsureNumber)(x.altitude)); });
523
+ return [4 /*yield*/, ensureHeightRefs(pPosses, sample)];
524
+ case 1:
525
+ _a.sent();
526
+ posses = posses.concat(pPosses);
527
+ _a.label = 2;
528
+ case 2:
529
+ pRings = entity.geometry.Polygon;
530
+ if (!(pRings && typeof pRings == "object")) return [3 /*break*/, 4];
531
+ boundary = pRings.find(function (x) { return x.Facing == bruce_models_1.Geometry.EPolygonRingType.Boundaries; });
532
+ if (!(boundary === null || boundary === void 0 ? void 0 : boundary.LinearRing)) return [3 /*break*/, 4];
533
+ points = bruce_models_1.Geometry.ParsePoints(boundary.LinearRing);
534
+ pPosses = points.map(function (x) { return Cesium.Cartesian3.fromDegrees((0, js_utils_1.EnsureNumber)(x.longitude), (0, js_utils_1.EnsureNumber)(x.latitude), (0, js_utils_1.EnsureNumber)(x.altitude)); });
535
+ return [4 /*yield*/, ensureHeightRefs(pPosses, sample)];
536
+ case 3:
537
+ _a.sent();
538
+ posses = posses.concat(pPosses);
539
+ _a.label = 4;
540
+ case 4:
541
+ if (!entity.boundaries) return [3 /*break*/, 6];
542
+ point = {
543
+ latitude: ((0, js_utils_1.EnsureNumber)(entity.boundaries.minLatitude) + (0, js_utils_1.EnsureNumber)(entity.boundaries.maxLatitude)) / 2,
544
+ longitude: ((0, js_utils_1.EnsureNumber)(entity.boundaries.minLongitude) + (0, js_utils_1.EnsureNumber)(entity.boundaries.maxLongitude)) / 2,
545
+ altitude: 0
546
+ };
547
+ if (!(point.latitude || point.longitude)) return [3 /*break*/, 6];
548
+ bPosses = [
549
+ Cesium.Cartesian3.fromDegrees((0, js_utils_1.EnsureNumber)(entity.boundaries.minLongitude), (0, js_utils_1.EnsureNumber)(entity.boundaries.minLatitude), (0, js_utils_1.EnsureNumber)(entity.boundaries.minAltitude)),
550
+ Cesium.Cartesian3.fromDegrees((0, js_utils_1.EnsureNumber)(entity.boundaries.maxLongitude), (0, js_utils_1.EnsureNumber)(entity.boundaries.maxLatitude), (0, js_utils_1.EnsureNumber)(entity.boundaries.maxAltitude))
551
+ ];
552
+ return [4 /*yield*/, ensureHeightRefs(bPosses, sample)];
553
+ case 5:
554
+ _a.sent();
555
+ posses = posses.concat(bPosses);
556
+ _a.label = 6;
557
+ case 6: return [2 /*return*/, posses];
558
+ }
559
+ });
560
+ }); };
561
+ return [4 /*yield*/, evaluateRendered()];
562
+ case 10:
563
+ renderedPosses = _o.sent();
564
+ if (renderedPosses === null || renderedPosses === void 0 ? void 0 : renderedPosses.length) {
565
+ return [2 /*return*/, renderedPosses];
566
+ }
567
+ return [4 /*yield*/, evaluateRecord()];
568
+ case 11:
569
+ recordPosses = _o.sent();
570
+ if (recordPosses === null || recordPosses === void 0 ? void 0 : recordPosses.length) {
571
+ return [2 /*return*/, recordPosses];
572
+ }
573
+ if (!!tileset) return [3 /*break*/, 13];
574
+ if (!(tilesetId && apiCalls < MAX_API_CALLS)) return [3 /*break*/, 13];
575
+ apiCalls += 1;
576
+ return [4 /*yield*/, bruce_models_1.Tileset.Get({
577
+ api: api,
578
+ tilesetId: tilesetId
579
+ })];
580
+ case 12:
581
+ tileset = (_o.sent()).tileset;
582
+ _o.label = 13;
583
+ case 13:
584
+ tSettings = tileset === null || tileset === void 0 ? void 0 : tileset.settings;
585
+ pos3d = null;
586
+ if (!(((_d = entity.location) === null || _d === void 0 ? void 0 : _d.longitude) || ((_e = tSettings === null || tSettings === void 0 ? void 0 : tSettings.location) === null || _e === void 0 ? void 0 : _e.longitude))) return [3 /*break*/, 15];
587
+ if ((_f = entity === null || entity === void 0 ? void 0 : entity.location) === null || _f === void 0 ? void 0 : _f.longitude) {
588
+ alt = +entity.location.altitude;
589
+ if (isNaN(alt)) {
590
+ alt = 0;
591
+ }
592
+ pos3d = Cesium.Cartesian3.fromDegrees(+entity.location.longitude, +entity.location.latitude, alt);
593
+ }
594
+ else {
595
+ alt = +tSettings.location.altitude;
596
+ if (isNaN(alt)) {
597
+ alt = 0;
598
+ }
599
+ pos3d = Cesium.Cartesian3.fromDegrees(+tSettings.location.longitude, +tSettings.location.latitude, alt);
600
+ }
601
+ if (!(entity === null || entity === void 0 ? void 0 : entity.worldPosition)) return [3 /*break*/, 15];
602
+ heading = 0;
603
+ pitch = 0;
604
+ roll = 0;
605
+ if ((_g = entity === null || entity === void 0 ? void 0 : entity.transform) === null || _g === void 0 ? void 0 : _g.heading) {
606
+ heading = entity.transform.heading;
607
+ }
608
+ else if ((_h = tSettings === null || tSettings === void 0 ? void 0 : tSettings.transform) === null || _h === void 0 ? void 0 : _h.heading) {
609
+ heading = tSettings.transform.heading;
610
+ }
611
+ if ((_j = entity === null || entity === void 0 ? void 0 : entity.transform) === null || _j === void 0 ? void 0 : _j.pitch) {
612
+ pitch = entity.transform.pitch;
613
+ }
614
+ else if ((_k = tSettings === null || tSettings === void 0 ? void 0 : tSettings.transform) === null || _k === void 0 ? void 0 : _k.pitch) {
615
+ pitch = tSettings.transform.pitch;
616
+ }
617
+ if ((_l = entity === null || entity === void 0 ? void 0 : entity.transform) === null || _l === void 0 ? void 0 : _l.roll) {
618
+ roll = entity.transform.roll;
619
+ }
620
+ else if ((_m = tSettings === null || tSettings === void 0 ? void 0 : tSettings.transform) === null || _m === void 0 ? void 0 : _m.roll) {
621
+ roll = tSettings.transform.roll;
622
+ }
623
+ heading = +heading;
624
+ if (isNaN(heading)) {
625
+ heading = 0;
626
+ }
627
+ pitch = +pitch;
628
+ if (isNaN(pitch)) {
629
+ pitch = 0;
630
+ }
631
+ roll = +roll;
632
+ if (isNaN(roll)) {
633
+ roll = 0;
634
+ }
635
+ matrix4 = entity.worldPosition;
636
+ offset = new Cesium.Cartesian3(+matrix4[0][3], +matrix4[1][3], +matrix4[2][3]);
637
+ if (entity.worldPivot) //the position from worldMatrix + center of geometry offset
638
+ {
639
+ offset = new Cesium.Cartesian3(entity.worldPivot[0], entity.worldPivot[1], entity.worldPivot[2]);
640
+ }
641
+ m1 = Cesium.Transforms.eastNorthUpToFixedFrame(pos3d);
642
+ hpr = Cesium.HeadingPitchRoll.fromDegrees(heading, pitch, roll, new Cesium.HeadingPitchRoll());
643
+ transform = Cesium.Matrix3.fromHeadingPitchRoll(hpr);
644
+ transformedOffset = Cesium.Matrix3.multiplyByVector(transform, offset, new Cesium.Cartesian3());
645
+ pos3d = Cesium.Matrix4.multiplyByPoint(m1, transformedOffset, new Cesium.Cartesian3());
646
+ if (!(pos3d === null || pos3d === void 0 ? void 0 : pos3d.x)) return [3 /*break*/, 15];
647
+ return [4 /*yield*/, processPosHeight(pos3d, Cesium.HeightReference.NONE)];
648
+ case 14:
649
+ pos3d = _o.sent();
650
+ _o.label = 15;
651
+ case 15:
652
+ if (pos3d) {
653
+ return [2 /*return*/, [pos3d]];
654
+ }
655
+ return [2 /*return*/, []];
656
+ }
657
+ });
658
+ }); };
659
+ allPosses = [];
660
+ i = 0;
661
+ _b.label = 1;
662
+ case 1:
663
+ if (!(i < samples.length)) return [3 /*break*/, 4];
664
+ sample = samples[i];
665
+ return [4 /*yield*/, getEntityPositions(sample)];
666
+ case 2:
667
+ samplePosses = _b.sent();
668
+ if (samplePosses === null || samplePosses === void 0 ? void 0 : samplePosses.length) {
669
+ valid = !(allPosses === null || allPosses === void 0 ? void 0 : allPosses.length);
670
+ if (!valid) {
671
+ for (j = 0; j < samplePosses.length; j++) {
672
+ samplePos = samplePosses[j];
673
+ for (k = 0; k < allPosses.length; k++) {
674
+ allPos = allPosses[k];
675
+ distance = Cesium.Cartesian3.distance(samplePos, allPos);
676
+ if (distance < MAX_DISTANCE_BETWEEN_SAMPLES) {
677
+ valid = true;
678
+ break;
679
+ }
680
+ }
681
+ if (valid) {
682
+ break;
683
+ }
684
+ }
685
+ }
686
+ if (valid) {
687
+ allPosses = allPosses.concat(samplePosses);
688
+ validSamples += 1;
689
+ if (validSamples >= MAX_VALID_SAMPLES) {
690
+ return [3 /*break*/, 4];
691
+ }
692
+ }
693
+ }
694
+ _b.label = 3;
695
+ case 3:
696
+ i++;
697
+ return [3 /*break*/, 1];
698
+ case 4:
699
+ if (allPosses.length > 1) {
700
+ rect = allPosses.length == 1 ? null : Cesium.Rectangle.fromCartesianArray(allPosses);
701
+ factor = 0.1;
702
+ marginX = rect.width * factor / 2;
703
+ marginY = rect.height * factor / 2;
704
+ rect.east += marginX;
705
+ rect.west -= marginX;
706
+ rect.north += marginY;
707
+ rect.south -= marginY;
708
+ diagonalLen = Cesium.Cartesian3.distance(Cesium.Cartesian3.fromRadians(rect.west, rect.north), Cesium.Cartesian3.fromRadians(rect.east, rect.south));
709
+ terrData = view_utils_1.ViewUtils.GatherTerrainTile({
710
+ viewer: viewer
711
+ });
712
+ if (((_a = terrData === null || terrData === void 0 ? void 0 : terrData.terrain) === null || _a === void 0 ? void 0 : _a.tilesetId) == bruce_models_1.ProjectViewTile.EDefaultTerrain.FlatTerrain) {
713
+ MIN_RECT_DIAGONAL_LENGTH = 0;
714
+ }
715
+ posCarto = Cesium.Cartographic.fromCartesian(allPosses[0]);
716
+ height = posCarto.height + (diagonalLen * 1.5);
717
+ data.pos3d = Cesium.Cartesian3.fromRadians(posCarto.longitude, posCarto.latitude, height);
718
+ MIN_POSSES_LEN = 10;
719
+ if (diagonalLen >= MIN_RECT_DIAGONAL_LENGTH && allPosses.length >= MIN_POSSES_LEN) {
720
+ data.rectangle = rect;
721
+ }
722
+ }
723
+ else if (allPosses.length == 1) {
724
+ data.pos3d = allPosses[0];
725
+ }
726
+ return [2 /*return*/, data];
727
+ }
728
+ });
729
+ });
730
+ }
731
+ EntityUtils.GetLocation = GetLocation;
209
732
  /**
210
733
  * Returns an entity's position.
211
734
  * This will attempt to calculate it from multiple sources of data.
212
735
  * @param params
736
+ * @deprecated use GetLocation instead.
213
737
  * @returns
214
738
  */
215
739
  function GetPos(params) {
@@ -221,9 +745,9 @@ var EntityUtils;
221
745
  if ((rego === null || rego === void 0 ? void 0 : rego.visual) instanceof Cesium.Entity) {
222
746
  var visual = rego.visual;
223
747
  if (visual.position) {
224
- var pos3d_1 = GetValue(viewer, visual.position);
748
+ var pos3d_4 = GetValue(viewer, visual.position);
225
749
  // Fix height reference.
226
- if (pos3d_1 === null || pos3d_1 === void 0 ? void 0 : pos3d_1.x) {
750
+ if (pos3d_4 === null || pos3d_4 === void 0 ? void 0 : pos3d_4.x) {
227
751
  var visualHeightRef = Cesium.HeightReference.RELATIVE_TO_GROUND;
228
752
  if (visual.model) {
229
753
  visualHeightRef = GetValue(viewer, visual.model.heightReference);
@@ -250,7 +774,7 @@ var EntityUtils;
250
774
  return drawing_utils_1.DrawingUtils.EnsurePosHeight({
251
775
  desiredHeightRef: params.returnHeightRef,
252
776
  heightRef: visualHeightRef,
253
- pos3d: pos3d_1,
777
+ pos3d: pos3d_4,
254
778
  viewer: viewer
255
779
  });
256
780
  }
@@ -260,12 +784,12 @@ var EntityUtils;
260
784
  }
261
785
  function evaluateRecord() {
262
786
  if (entity.location && bruce_models_1.Carto.ValidateCarto(entity.location)) {
263
- var location_1 = entity.location;
264
- var latitude = (0, js_utils_1.EnsureNumber)(location_1.latitude);
265
- var longitude = (0, js_utils_1.EnsureNumber)(location_1.longitude);
787
+ var location_2 = entity.location;
788
+ var latitude = (0, js_utils_1.EnsureNumber)(location_2.latitude);
789
+ var longitude = (0, js_utils_1.EnsureNumber)(location_2.longitude);
266
790
  // Disallowing exact 0.
267
791
  if (latitude || longitude) {
268
- return Cesium.Cartesian3.fromDegrees(longitude, latitude, (0, js_utils_1.EnsureNumber)(location_1.altitude));
792
+ return Cesium.Cartesian3.fromDegrees(longitude, latitude, (0, js_utils_1.EnsureNumber)(location_2.altitude));
269
793
  }
270
794
  }
271
795
  if (entity.geometry && typeof entity.geometry == "object") {
@@ -343,12 +867,13 @@ var EntityUtils;
343
867
  * Returns the position of the entity.
344
868
  * This is an async variant that checks for related tilesets.
345
869
  * @param params
870
+ * @deprecated use GetLocation instead.
346
871
  * @returns
347
872
  */
348
873
  function GetPosAsync(params) {
349
874
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
350
875
  return __awaiter(this, void 0, void 0, function () {
351
- var viewer, entityId, entity, tileset, tilesetId, visualRegister, returnHeightRef, recordHeightRef, api, lat, lon, e_1, e_2, pos3d, tSettings, alt, alt, heading, pitch, roll, matrix4, offset, m1, hpr, transform, transformedOffset;
876
+ var viewer, entityId, entity, tileset, tilesetId, visualRegister, returnHeightRef, recordHeightRef, api, lat, lon, e_3, e_4, pos3d, tSettings, alt, alt, heading, pitch, roll, matrix4, offset, m1, hpr, transform, transformedOffset;
352
877
  return __generator(this, function (_o) {
353
878
  switch (_o.label) {
354
879
  case 0:
@@ -370,8 +895,8 @@ var EntityUtils;
370
895
  lon = +((_b = entity === null || entity === void 0 ? void 0 : entity.location) === null || _b === void 0 ? void 0 : _b.longitude);
371
896
  return [3 /*break*/, 4];
372
897
  case 3:
373
- e_1 = _o.sent();
374
- console.error(e_1);
898
+ e_3 = _o.sent();
899
+ console.error(e_3);
375
900
  return [3 /*break*/, 4];
376
901
  case 4:
377
902
  if (!(entity && (isNaN(lat) || isNaN(lon) || (lat == 0 && lon == 0)))) return [3 /*break*/, 9];
@@ -387,8 +912,8 @@ var EntityUtils;
387
912
  entity = (_o.sent()).entity;
388
913
  return [3 /*break*/, 9];
389
914
  case 7:
390
- e_2 = _o.sent();
391
- console.warn(e_2);
915
+ e_4 = _o.sent();
916
+ console.warn(e_4);
392
917
  return [4 /*yield*/, bruce_models_1.Entity.Get({
393
918
  api: api,
394
919
  entityId: entityId,
@@ -520,90 +1045,113 @@ var EntityUtils;
520
1045
  * @param entities
521
1046
  */
522
1047
  function LookAtEntities(params) {
523
- var viewer = params.viewer, entities = params.entities;
524
- // Removing cesium tileset features for now as it's not accurate.
525
- entities = [].concat(entities).filter(function (x) { return !(x instanceof Cesium.Cesium3DTileFeature); });
526
- if (!entities.length) {
527
- return;
528
- }
529
- var _a = [Cesium.Rectangle, Cesium.Cartographic, Cesium.Cartesian3, Cesium.Matrix3, Cesium.Matrix4, Cesium.EasingFunction], R = _a[0], CT = _a[1], C3 = _a[2], M3 = _a[3], M4 = _a[4], E = _a[5];
530
- var cutoffDistance = 2000;
531
- var hoverHeight = 2000;
532
- var duration = 0.5;
533
- var centers = function (p) {
534
- var PM = p.modelMatrix;
535
- var instances = (function (i) { return (i instanceof Cesium.GeometryInstance ? [i] : i); })(p.geometryInstances);
536
- return instances.map(function (g) {
537
- var _M = M4.multiply(PM, g.modelMatrix, new M4);
538
- var G = g.geometry;
539
- var _p = G.attributes.position;
540
- var b = G.boundingSphere;
541
- return [b.center, b.radius];
542
- });
543
- };
544
- var currentTime = Cesium.JulianDate.now();
545
- var target = entities.flatMap(function (e) {
546
- var _a;
547
- if (e instanceof Cesium.Cartesian3) {
548
- return [[e, 0]];
549
- }
550
- if (e instanceof Cesium.Entity) {
551
- return [[(_a = e.position) === null || _a === void 0 ? void 0 : _a.getValue(currentTime), 0]];
552
- }
553
- if (e instanceof Cesium.Primitive) {
554
- return centers(e);
555
- }
556
- if (e instanceof Cesium.Cesium3DTileFeature) {
557
- var s = e.tileset.boundingSphere;
558
- return [[s.center, s.radius]];
559
- }
560
- });
561
- var bounds = function (list) {
562
- var min = list[0][0];
563
- var max = min;
564
- for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
565
- var _a = list_1[_i], p_1 = _a[0], r = _a[1];
566
- var pMin = C3.subtract(p_1, new C3(r), new C3);
567
- var pMax = C3.add(p_1, new C3(r), new C3);
568
- min = C3.minimumByComponent(pMin, min, new C3);
569
- max = C3.maximumByComponent(pMax, max, new C3);
570
- }
571
- var c = C3.multiplyByScalar(C3.add(max, min, new C3), 0.5, new C3);
572
- return c;
573
- };
574
- var center = bounds(target);
575
- var orient = function (p) {
576
- var d = C3.subtract(center, p, new C3);
577
- var dir = C3.normalize(d, new C3);
578
- var up = viewer.scene.globe.ellipsoid.geodeticSurfaceNormal(center);
579
- return [p, dir, up];
580
- };
581
- var shorten = function (v, l) {
582
- var c = l / C3.magnitude(v);
583
- return C3.multiplyByScalar(v, c, new C3);
584
- };
585
- var camera = viewer.camera;
586
- var _b = orient(camera.position), p = _b[0], direction = _b[1], up = _b[2];
587
- var orientation = { direction: direction, up: up };
588
- camera.flyTo({
589
- destination: p,
590
- orientation: orientation,
591
- duration: duration,
592
- complete: function () {
593
- if (C3.distance(p, center) > cutoffDistance) {
594
- var d = C3.subtract(p, center, new C3);
595
- d = shorten(d, cutoffDistance);
596
- var h = C3.multiplyByScalar(C3.normalize(up, new C3), hoverHeight, new C3);
597
- d = C3.add(d, h, new C3);
598
- d = shorten(d, cutoffDistance);
599
- d = C3.add(d, center, new C3);
600
- var _a = orient(d), _p = _a[0], direction_1 = _a[1], u = _a[2];
601
- camera.flyTo({
602
- destination: d,
603
- orientation: { direction: direction_1, up: u }
604
- });
1048
+ return __awaiter(this, void 0, void 0, function () {
1049
+ var viewer, entities, _a, R, CT, C3, M3, M4, E, cutoffDistance, hoverHeight, duration, centers, currentTime, target, bounds, center, orient, shorten, camera, _b, p, direction, up, orientation, terrData, MIN_HEIGHT, carto;
1050
+ return __generator(this, function (_c) {
1051
+ switch (_c.label) {
1052
+ case 0:
1053
+ viewer = params.viewer, entities = params.entities;
1054
+ // Removing cesium tileset features for now as it's not accurate.
1055
+ entities = [].concat(entities).filter(function (x) { return !(x instanceof Cesium.Cesium3DTileFeature); });
1056
+ if (!entities.length) {
1057
+ return [2 /*return*/];
1058
+ }
1059
+ _a = [Cesium.Rectangle, Cesium.Cartographic, Cesium.Cartesian3, Cesium.Matrix3, Cesium.Matrix4, Cesium.EasingFunction], R = _a[0], CT = _a[1], C3 = _a[2], M3 = _a[3], M4 = _a[4], E = _a[5];
1060
+ cutoffDistance = 2000;
1061
+ hoverHeight = 2000;
1062
+ duration = 0.5;
1063
+ centers = function (p) {
1064
+ var PM = p.modelMatrix;
1065
+ var instances = (function (i) { return (i instanceof Cesium.GeometryInstance ? [i] : i); })(p.geometryInstances);
1066
+ return instances.map(function (g) {
1067
+ var _M = M4.multiply(PM, g.modelMatrix, new M4);
1068
+ var G = g.geometry;
1069
+ var _p = G.attributes.position;
1070
+ var b = G.boundingSphere;
1071
+ return [b.center, b.radius];
1072
+ });
1073
+ };
1074
+ currentTime = Cesium.JulianDate.now();
1075
+ target = entities.flatMap(function (e) {
1076
+ var _a;
1077
+ if (e instanceof Cesium.Cartesian3) {
1078
+ return [[e, 0]];
1079
+ }
1080
+ if (e instanceof Cesium.Entity) {
1081
+ return [[(_a = e.position) === null || _a === void 0 ? void 0 : _a.getValue(currentTime), 0]];
1082
+ }
1083
+ if (e instanceof Cesium.Primitive) {
1084
+ return centers(e);
1085
+ }
1086
+ if (e instanceof Cesium.Cesium3DTileFeature) {
1087
+ var s = e.tileset.boundingSphere;
1088
+ return [[s.center, s.radius]];
1089
+ }
1090
+ });
1091
+ bounds = function (list) {
1092
+ var min = list[0][0];
1093
+ var max = min;
1094
+ for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
1095
+ var _a = list_1[_i], p_1 = _a[0], r = _a[1];
1096
+ var pMin = C3.subtract(p_1, new C3(r), new C3);
1097
+ var pMax = C3.add(p_1, new C3(r), new C3);
1098
+ min = C3.minimumByComponent(pMin, min, new C3);
1099
+ max = C3.maximumByComponent(pMax, max, new C3);
1100
+ }
1101
+ var c = C3.multiplyByScalar(C3.add(max, min, new C3), 0.5, new C3);
1102
+ return c;
1103
+ };
1104
+ center = bounds(target);
1105
+ orient = function (p) {
1106
+ var d = C3.subtract(center, p, new C3);
1107
+ var dir = C3.normalize(d, new C3);
1108
+ var up = viewer.scene.globe.ellipsoid.geodeticSurfaceNormal(center);
1109
+ return [p, dir, up];
1110
+ };
1111
+ shorten = function (v, l) {
1112
+ var c = l / C3.magnitude(v);
1113
+ return C3.multiplyByScalar(v, c, new C3);
1114
+ };
1115
+ camera = viewer.camera;
1116
+ _b = orient(camera.position), p = _b[0], direction = _b[1], up = _b[2];
1117
+ orientation = { direction: direction, up: up };
1118
+ if (!(p === null || p === void 0 ? void 0 : p.x)) return [3 /*break*/, 2];
1119
+ return [4 /*yield*/, drawing_utils_1.DrawingUtils.GetTerrainHeight({
1120
+ pos3d: p,
1121
+ viewer: viewer
1122
+ })];
1123
+ case 1:
1124
+ terrData = _c.sent();
1125
+ MIN_HEIGHT = terrData.height + 300;
1126
+ carto = Cesium.Cartographic.fromCartesian(p);
1127
+ if (carto.height < MIN_HEIGHT) {
1128
+ carto.height = MIN_HEIGHT;
1129
+ p = Cesium.Cartesian3.fromRadians(carto.longitude, carto.latitude, carto.height);
1130
+ }
1131
+ camera.flyTo({
1132
+ destination: p,
1133
+ orientation: orientation,
1134
+ duration: duration,
1135
+ complete: function () {
1136
+ // if (C3.distance(p, center) > cutoffDistance) {
1137
+ // let d = C3.subtract(p, center, new C3);
1138
+ // d = shorten(d, cutoffDistance);
1139
+ // const h = C3.multiplyByScalar(C3.normalize(up, new C3), hoverHeight, new C3);
1140
+ // d = C3.add(d, h, new C3);
1141
+ // d = shorten(d, cutoffDistance);
1142
+ // d = C3.add(d, center, new C3);
1143
+ // const [ _p, direction, u ] = orient(d);
1144
+ // camera.flyTo({
1145
+ // destination: d,
1146
+ // orientation: { direction, up: u }
1147
+ // });
1148
+ // }
1149
+ }
1150
+ });
1151
+ _c.label = 2;
1152
+ case 2: return [2 /*return*/];
605
1153
  }
606
- }
1154
+ });
607
1155
  });
608
1156
  }
609
1157
  EntityUtils.LookAtEntities = LookAtEntities;