yzt
2023-05-26 2f70f6727314edd84d8ec2bfe3ce832803f1ea77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import { Cartesian3 } from "../../Source/Cesium.js";
import { clone } from "../../Source/Cesium.js";
import { HeadingPitchRoll } from "../../Source/Cesium.js";
import { Math as CesiumMath } from "../../Source/Cesium.js";
import { Matrix3 } from "../../Source/Cesium.js";
import { Matrix4 } from "../../Source/Cesium.js";
import { Rectangle } from "../../Source/Cesium.js";
import { Transforms } from "../../Source/Cesium.js";
import { Cesium3DTile } from "../../Source/Cesium.js";
import { Cesium3DTilePass } from "../../Source/Cesium.js";
import { Cesium3DTileRefine } from "../../Source/Cesium.js";
import { Cesium3DTilesetHeatmap } from "../../Source/Cesium.js";
import { TileBoundingRegion } from "../../Source/Cesium.js";
import { TileOrientedBoundingBox } from "../../Source/Cesium.js";
import createScene from "../createScene.js";
 
describe(
  "Scene/Cesium3DTile",
  function () {
    var tileWithBoundingSphere = {
      geometricError: 1,
      refine: "REPLACE",
      children: [],
      boundingVolume: {
        sphere: [0.0, 0.0, 0.0, 5.0],
      },
    };
 
    var tileWithContentBoundingSphere = {
      geometricError: 1,
      refine: "REPLACE",
      content: {
        url: "0/0.b3dm",
        boundingVolume: {
          sphere: [0.0, 0.0, 1.0, 5.0],
        },
      },
      children: [],
      boundingVolume: {
        sphere: [0.0, 0.0, 1.0, 5.0],
      },
    };
 
    var tileWithBoundingRegion = {
      geometricError: 1,
      refine: "REPLACE",
      children: [],
      boundingVolume: {
        region: [-1.2, -1.2, 0.0, 0.0, -30, -34],
      },
    };
 
    var tileWithContentBoundingRegion = {
      geometricError: 1,
      refine: "REPLACE",
      children: [],
      content: {
        url: "0/0.b3dm",
        boundingVolume: {
          region: [-1.2, -1.2, 0, 0, -30, -34],
        },
      },
      boundingVolume: {
        region: [-1.2, -1.2, 0, 0, -30, -34],
      },
    };
 
    var tileWithBoundingBox = {
      geometricError: 1,
      refine: "REPLACE",
      children: [],
      boundingVolume: {
        box: [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0],
      },
    };
 
    var tileWithContentBoundingBox = {
      geometricError: 1,
      refine: "REPLACE",
      children: [],
      content: {
        url: "0/0.b3dm",
        boundingVolume: {
          box: [0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 2.0],
        },
      },
      boundingVolume: {
        box: [0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 2.0],
      },
    };
 
    var tileWithViewerRequestVolume = {
      geometricError: 1,
      refine: "REPLACE",
      children: [],
      boundingVolume: {
        box: [0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 2.0],
      },
      viewerRequestVolume: {
        box: [0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 2.0],
      },
    };
 
    var mockTileset = {
      debugShowBoundingVolume: true,
      debugShowViewerRequestVolume: true,
      modelMatrix: Matrix4.IDENTITY,
      _geometricError: 2,
      _heatmap: new Cesium3DTilesetHeatmap(),
    };
 
    var centerLongitude = -1.31968;
    var centerLatitude = 0.698874;
 
    function getTileTransform(longitude, latitude) {
      var transformCenter = Cartesian3.fromRadians(longitude, latitude, 0.0);
      var hpr = new HeadingPitchRoll();
      var transformMatrix = Transforms.headingPitchRollToFixedFrame(
        transformCenter,
        hpr
      );
      return Matrix4.pack(transformMatrix, new Array(16));
    }
 
    it("destroys", function () {
      var tile = new Cesium3DTile(
        mockTileset,
        "/some_url",
        tileWithBoundingSphere,
        undefined
      );
      expect(tile.isDestroyed()).toEqual(false);
      tile.destroy();
      expect(tile.isDestroyed()).toEqual(true);
    });
 
    it("throws if boundingVolume is undefined", function () {
      var tileWithoutBoundingVolume = clone(tileWithBoundingSphere, true);
      delete tileWithoutBoundingVolume.boundingVolume;
      expect(function () {
        return new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithoutBoundingVolume,
          undefined
        );
      }).toThrowRuntimeError();
    });
 
    it("throws if boundingVolume does not contain a sphere, region, or box", function () {
      var tileWithoutBoundingVolume = clone(tileWithBoundingSphere, true);
      delete tileWithoutBoundingVolume.boundingVolume.sphere;
      expect(function () {
        return new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithoutBoundingVolume,
          undefined
        );
      }).toThrowRuntimeError();
    });
 
    it("logs deprecation warning if refine is lowercase", function () {
      spyOn(Cesium3DTile, "_deprecationWarning");
      var header = clone(tileWithBoundingSphere, true);
      header.refine = "replace";
      var tile = new Cesium3DTile(mockTileset, "/some_url", header, undefined);
      expect(tile.refine).toBe(Cesium3DTileRefine.REPLACE);
      expect(Cesium3DTile._deprecationWarning).toHaveBeenCalled();
    });
 
    it("logs deprecation warning if geometric error is undefined", function () {
      spyOn(Cesium3DTile, "_deprecationWarning");
 
      var geometricErrorMissing = clone(tileWithBoundingSphere, true);
      delete geometricErrorMissing.geometricError;
 
      var parent = new Cesium3DTile(
        mockTileset,
        "/some_url",
        tileWithBoundingSphere,
        undefined
      );
      var child = new Cesium3DTile(
        mockTileset,
        "/some_url",
        geometricErrorMissing,
        parent
      );
      expect(child.geometricError).toBe(parent.geometricError);
      expect(child.geometricError).toBe(1);
 
      var tile = new Cesium3DTile(
        mockTileset,
        "/some_url",
        geometricErrorMissing,
        undefined
      );
      expect(tile.geometricError).toBe(mockTileset._geometricError);
      expect(tile.geometricError).toBe(2);
 
      expect(Cesium3DTile._deprecationWarning.calls.count()).toBe(2);
    });
 
    describe("bounding volumes", function () {
      it("returns the tile bounding volume if the content bounding volume is undefined", function () {
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithBoundingSphere,
          undefined
        );
        expect(tile.boundingVolume).toBeDefined();
        expect(tile.contentBoundingVolume).toBe(tile.boundingVolume);
      });
 
      it("can have a bounding sphere", function () {
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithBoundingSphere,
          undefined
        );
        var radius = tileWithBoundingSphere.boundingVolume.sphere[3];
        expect(tile.boundingVolume).toBeDefined();
        expect(tile.boundingVolume.boundingVolume.radius).toEqual(radius);
        expect(tile.boundingVolume.boundingVolume.center).toEqual(
          Cartesian3.ZERO
        );
      });
 
      it("can have a content bounding sphere", function () {
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithContentBoundingSphere,
          undefined
        );
        var radius =
          tileWithContentBoundingSphere.content.boundingVolume.sphere[3];
        expect(tile.contentBoundingVolume).toBeDefined();
        expect(tile.contentBoundingVolume.boundingVolume.radius).toEqual(
          radius
        );
        expect(tile.contentBoundingVolume.boundingVolume.center).toEqual(
          new Cartesian3(0.0, 0.0, 1.0)
        );
      });
 
      it("can have a bounding region", function () {
        var box = tileWithBoundingRegion.boundingVolume.region;
        var rectangle = new Rectangle(box[0], box[1], box[2], box[3]);
        var minimumHeight = tileWithBoundingRegion.boundingVolume.region[4];
        var maximumHeight = tileWithBoundingRegion.boundingVolume.region[5];
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithBoundingRegion,
          undefined
        );
        var tbr = new TileBoundingRegion({
          rectangle: rectangle,
          minimumHeight: minimumHeight,
          maximumHeight: maximumHeight,
        });
        expect(tile.boundingVolume).toBeDefined();
        expect(tile.boundingVolume).toEqual(tbr);
      });
 
      it("can have a content bounding region", function () {
        var region =
          tileWithContentBoundingRegion.content.boundingVolume.region;
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithContentBoundingRegion,
          undefined
        );
        expect(tile.contentBoundingVolume).toBeDefined();
        var tbb = new TileBoundingRegion({
          rectangle: new Rectangle(region[0], region[1], region[2], region[3]),
          minimumHeight: region[4],
          maximumHeight: region[5],
        });
        expect(tile.contentBoundingVolume).toEqual(tbb);
      });
 
      it("can have an oriented bounding box", function () {
        var box = tileWithBoundingBox.boundingVolume.box;
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithBoundingBox,
          undefined
        );
        expect(tile.boundingVolume).toBeDefined();
        var center = new Cartesian3(box[0], box[1], box[2]);
        var halfAxes = Matrix3.fromArray(box, 3);
        var obb = new TileOrientedBoundingBox(center, halfAxes);
        expect(tile.boundingVolume).toEqual(obb);
      });
 
      it("does not crash for bounding box with 0 volume", function () {
        // Create a copy of the tile with bounding box.
        var tileWithBoundingBox0Volume = JSON.parse(
          JSON.stringify(tileWithBoundingBox)
        );
        // Generate all the combinations of missing axes.
        var boxes = [];
        for (var x = 0; x < 2; ++x) {
          for (var y = 0; y < 2; ++y) {
            for (var z = 0; z < 2; ++z) {
              boxes.push([
                0.0,
                0.0,
                0.0,
                x,
                0.0,
                0.0,
                0.0,
                y,
                0.0,
                0.0,
                0.0,
                z,
              ]);
            }
          }
        }
 
        for (var i = 0; i < boxes.length; ++i) {
          var box = boxes[i];
 
          tileWithBoundingBox0Volume.boundingVolume.box = box;
 
          var tile = new Cesium3DTile(
            mockTileset,
            "/some_url",
            tileWithBoundingBox0Volume,
            undefined
          );
          expect(tile.boundingVolume).toBeDefined();
          var center = new Cartesian3(box[0], box[1], box[2]);
          var halfAxes = Matrix3.fromArray(box, 3);
          var obb = new TileOrientedBoundingBox(center, halfAxes);
          expect(tile.boundingVolume).toEqual(obb);
        }
      });
 
      it("can have a content oriented bounding box", function () {
        var box = tileWithContentBoundingBox.boundingVolume.box;
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithContentBoundingBox,
          undefined
        );
        expect(tile.contentBoundingVolume).toBeDefined();
        var center = new Cartesian3(box[0], box[1], box[2]);
        var halfAxes = Matrix3.fromArray(box, 3);
        var obb = new TileOrientedBoundingBox(center, halfAxes);
        expect(tile.contentBoundingVolume).toEqual(obb);
      });
 
      it("tile transform affects bounding sphere", function () {
        var header = clone(tileWithContentBoundingSphere, true);
        header.transform = getTileTransform(centerLongitude, centerLatitude);
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          header,
          undefined
        );
        var boundingSphere = tile.boundingVolume.boundingVolume;
        var contentBoundingSphere = tile.contentBoundingVolume.boundingVolume;
 
        var boundingVolumeCenter = Cartesian3.fromRadians(
          centerLongitude,
          centerLatitude,
          1.0
        );
        expect(boundingSphere.center).toEqualEpsilon(
          boundingVolumeCenter,
          CesiumMath.EPSILON4
        );
        expect(boundingSphere.radius).toEqual(5.0); // No change
 
        expect(contentBoundingSphere.center).toEqualEpsilon(
          boundingVolumeCenter,
          CesiumMath.EPSILON4
        );
        expect(contentBoundingSphere.radius).toEqual(5.0); // No change
      });
 
      it("tile transform affects oriented bounding box", function () {
        var header = clone(tileWithContentBoundingBox, true);
        header.transform = getTileTransform(centerLongitude, centerLatitude);
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          header,
          undefined
        );
        var boundingBox = tile.boundingVolume.boundingVolume;
        var contentBoundingBox = tile.contentBoundingVolume.boundingVolume;
 
        var boundingVolumeCenter = Cartesian3.fromRadians(
          centerLongitude,
          centerLatitude,
          1.0
        );
        expect(boundingBox.center).toEqualEpsilon(
          boundingVolumeCenter,
          CesiumMath.EPSILON7
        );
        expect(contentBoundingBox.center).toEqualEpsilon(
          boundingVolumeCenter,
          CesiumMath.EPSILON7
        );
      });
 
      it("tile transform does not affect bounding region", function () {
        var header = clone(tileWithContentBoundingRegion, true);
        header.transform = getTileTransform(centerLongitude, centerLatitude);
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          header,
          undefined
        );
        var boundingRegion = tile.boundingVolume;
        var contentBoundingRegion = tile.contentBoundingVolume;
 
        var region = header.boundingVolume.region;
        var rectangle = Rectangle.unpack(region);
        expect(boundingRegion.rectangle).toEqual(rectangle);
        expect(contentBoundingRegion.rectangle).toEqual(rectangle);
      });
 
      it("tile transform affects viewer request volume", function () {
        var header = clone(tileWithViewerRequestVolume, true);
        header.transform = getTileTransform(centerLongitude, centerLatitude);
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          header,
          undefined
        );
        var requestVolume = tile._viewerRequestVolume.boundingVolume;
        var requestVolumeCenter = Cartesian3.fromRadians(
          centerLongitude,
          centerLatitude,
          1.0
        );
        expect(requestVolume.center).toEqualEpsilon(
          requestVolumeCenter,
          CesiumMath.EPSILON7
        );
      });
 
      it("tile transform changes", function () {
        var mockTileset = {
          modelMatrix: Matrix4.IDENTITY,
        };
        var header = clone(tileWithBoundingSphere, true);
        header.transform = getTileTransform(centerLongitude, centerLatitude);
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          header,
          undefined
        );
        var boundingSphere = tile.boundingVolume.boundingVolume;
 
        // Check the original transform
        var boundingVolumeCenter = Cartesian3.fromRadians(
          centerLongitude,
          centerLatitude
        );
        expect(boundingSphere.center).toEqualEpsilon(
          boundingVolumeCenter,
          CesiumMath.EPSILON7
        );
 
        // Change the transform
        var newLongitude = -1.012;
        var newLatitude = 0.698874;
        tile.transform = getTileTransform(newLongitude, newLatitude);
        tile.updateTransform();
 
        // Check the new transform
        var newCenter = Cartesian3.fromRadians(newLongitude, newLatitude);
        expect(boundingSphere.center).toEqualEpsilon(
          newCenter,
          CesiumMath.EPSILON7
        );
      });
    });
 
    describe("debug bounding volumes", function () {
      var scene;
      beforeEach(function () {
        scene = createScene();
        scene.frameState.passes.render = true;
      });
 
      afterEach(function () {
        scene.destroyForSpecs();
      });
 
      it("can be a bounding region", function () {
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithBoundingRegion,
          undefined
        );
        var passOptions = Cesium3DTilePass.getPassOptions(
          Cesium3DTilePass.RENDER
        );
        tile.update(mockTileset, scene.frameState, passOptions);
        expect(tile._debugBoundingVolume).toBeDefined();
      });
 
      it("can be an oriented bounding box", function () {
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithBoundingBox,
          undefined
        );
        var passOptions = Cesium3DTilePass.getPassOptions(
          Cesium3DTilePass.RENDER
        );
        tile.update(mockTileset, scene.frameState, passOptions);
        expect(tile._debugBoundingVolume).toBeDefined();
      });
 
      it("can be a bounding sphere", function () {
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithBoundingSphere,
          undefined
        );
        var passOptions = Cesium3DTilePass.getPassOptions(
          Cesium3DTilePass.RENDER
        );
        tile.update(mockTileset, scene.frameState, passOptions);
        expect(tile._debugBoundingVolume).toBeDefined();
      });
 
      it("creates debug bounding volume for viewer request volume", function () {
        var tile = new Cesium3DTile(
          mockTileset,
          "/some_url",
          tileWithViewerRequestVolume,
          undefined
        );
        var passOptions = Cesium3DTilePass.getPassOptions(
          Cesium3DTilePass.RENDER
        );
        tile.update(mockTileset, scene.frameState, passOptions);
        expect(tile._debugViewerRequestVolume).toBeDefined();
      });
    });
 
    it("updates priority", function () {
      var tile1 = new Cesium3DTile(
        mockTileset,
        "/some_url",
        tileWithBoundingSphere,
        undefined
      );
      tile1._priorityHolder = tile1;
      tile1._foveatedFactor = 0.0;
      tile1._distanceToCamera = 1.0;
      tile1._depth = 0.0;
      tile1._priorityProgressiveResolution = true;
 
      var tile2 = new Cesium3DTile(
        mockTileset,
        "/some_url",
        tileWithBoundingSphere,
        undefined
      );
      tile2._priorityHolder = tile1;
      tile2._foveatedFactor = 1.0; // foveatedFactor (when considered for priority in certain modes) is actually 0 since its linked up to tile1
      tile2._distanceToCamera = 0.0;
      tile2._depth = 1.0;
      tile2._priorityProgressiveResolution = true;
 
      mockTileset._minimumPriority = {
        depth: 0.0,
        distance: 0.0,
        foveatedFactor: 0.0,
      };
      mockTileset._maximumPriority = {
        depth: 1.0,
        distance: 1.0,
        foveatedFactor: 1.0,
      };
 
      tile1.updatePriority();
      tile2.updatePriority();
 
      var nonPreloadFlightPenalty = 10000000000.0;
      var tile1ExpectedPriority = nonPreloadFlightPenalty + 0.0;
      var tile2ExpectedPriority = nonPreloadFlightPenalty + 1.0;
      expect(
        CesiumMath.equalsEpsilon(
          tile1._priority,
          tile1ExpectedPriority,
          CesiumMath.EPSILON2
        )
      ).toBe(true);
      expect(
        CesiumMath.equalsEpsilon(
          tile2._priority,
          tile2ExpectedPriority,
          CesiumMath.EPSILON2
        )
      ).toBe(true);
 
      // Penalty for not being a progressive resolution
      tile2._priorityProgressiveResolution = false;
      tile2.updatePriority();
      var nonProgressiveResoutionPenalty = 100000000.0;
      expect(tile2._priority).toBeGreaterThan(nonProgressiveResoutionPenalty);
      tile2._priorityProgressiveResolution = true;
 
      // Penalty for being a foveated deferral
      tile2.priorityDeferred = true;
      tile2.updatePriority();
      var foveatedDeferralPenalty = 10000000.0;
      expect(tile2._priority).toBeGreaterThanOrEqualTo(foveatedDeferralPenalty);
      tile2._priorityDeferred = false;
    });
  },
  "WebGL"
);