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
import { BoundingSphere } from "../../Source/Cesium.js";
import { Cartesian3 } from "../../Source/Cesium.js";
import { Ellipsoid } from "../../Source/Cesium.js";
import { Math as CesiumMath } from "../../Source/Cesium.js";
import { Occluder } from "../../Source/Cesium.js";
import { Rectangle } from "../../Source/Cesium.js";
import { Visibility } from "../../Source/Cesium.js";
 
describe("Core/Occluder", function () {
  it("throws an exception during construction (1 of 3)", function () {
    expect(function () {
      return new Occluder();
    }).toThrowDeveloperError();
  });
 
  it("throws an exception during construction (2 of 3)", function () {
    expect(function () {
      return new Occluder(new BoundingSphere(new Cartesian3(0, 0, 0)));
    }).toThrowDeveloperError();
  });
 
  it("throws an exception during construction (3 of 3)", function () {
    expect(function () {
      return new Occluder(new Cartesian3(0, 0, 0));
    }).toThrowDeveloperError();
  });
 
  it("can entirely eclipse a smaller occludee", function () {
    var giantSphere = new BoundingSphere(new Cartesian3(0, 0, -1.5), 0.5);
    var littleSphere = new BoundingSphere(new Cartesian3(0, 0, -2.75), 0.25);
    var cameraPosition = Cartesian3.ZERO;
    var occluder = new Occluder(giantSphere, cameraPosition);
    expect(occluder.isBoundingSphereVisible(littleSphere)).toEqual(false);
    expect(occluder.computeVisibility(littleSphere)).toEqual(Visibility.NONE);
  });
 
  it("can have a fully visible occludee", function () {
    var bigSphere = new BoundingSphere(new Cartesian3(0, 0, -1.5), 0.5);
    var littleSphere = new BoundingSphere(new Cartesian3(0, 0, -2.75), 0.25);
    var cameraPosition = Cartesian3.ZERO;
    var occluder = new Occluder(littleSphere, cameraPosition);
    expect(occluder.radius).toBeLessThan(bigSphere.radius);
    expect(occluder.isBoundingSphereVisible(bigSphere)).toEqual(true);
    expect(occluder.computeVisibility(bigSphere)).toEqual(Visibility.FULL);
  });
 
  it("blocks the occludee when both are aligned and the same size", function () {
    var sphere1 = new BoundingSphere(new Cartesian3(0, 0, -1.5), 0.5);
    var sphere2 = new BoundingSphere(new Cartesian3(0, 0, -2.5), 0.5);
    var cameraPosition = Cartesian3.ZERO;
    var occluder = new Occluder(sphere1, cameraPosition);
    expect(occluder.isBoundingSphereVisible(sphere2)).toEqual(false);
    expect(occluder.computeVisibility(sphere2)).toEqual(Visibility.NONE);
  });
 
  it("can have a fully visible occludee", function () {
    var sphere1 = new BoundingSphere(new Cartesian3(-1.25, 0, -1.5), 0.5);
    var sphere2 = new BoundingSphere(new Cartesian3(1.25, 0, -1.5), 0.5);
    var cameraPosition = Cartesian3.ZERO;
    var occluder = new Occluder(sphere1, cameraPosition);
    expect(occluder.computeVisibility(sphere2)).toEqual(Visibility.FULL);
  });
 
  it("can partially block an occludee without intersecting", function () {
    var cameraPosition = Cartesian3.ZERO;
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -2), 1);
    var occluder = new Occluder(occluderBS, cameraPosition);
    var occludeeBS = new BoundingSphere(new Cartesian3(0.5, 0.5, -3), 1);
    expect(occluder.computeVisibility(occludeeBS)).toEqual(Visibility.PARTIAL);
  });
 
  it("can partially block an occludee when it intersects laterally", function () {
    var cameraPosition = Cartesian3.ZERO;
    var occluderBS = new BoundingSphere(new Cartesian3(-0.5, 0, -1), 1);
    var occluder = new Occluder(occluderBS, cameraPosition);
    var occludeeBS = new BoundingSphere(new Cartesian3(0.5, 0, -1), 1);
    expect(occluder.computeVisibility(occludeeBS)).toEqual(Visibility.PARTIAL);
  });
 
  it("can partially block an occludee when it intersects vertically", function () {
    var cameraPosition = Cartesian3.ZERO;
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -2), 1);
    var occluder = new Occluder(occluderBS, cameraPosition);
    var occludeeBS = new BoundingSphere(new Cartesian3(0, 0.5, -2.5), 1);
    expect(occluder.computeVisibility(occludeeBS)).toEqual(Visibility.PARTIAL);
  });
 
  it("reports full visibility when occludee is larger than occluder", function () {
    var littleSphere = new BoundingSphere(new Cartesian3(0, 0, -1.5), 0.5);
    var bigSphere = new BoundingSphere(new Cartesian3(0, 0, -3), 1);
    var cameraPosition = Cartesian3.ZERO;
    var occluder = new Occluder(littleSphere, cameraPosition);
    expect(occluder.computeVisibility(bigSphere)).toEqual(Visibility.FULL);
  });
 
  it("computeVisibility throws without a bounding sphere", function () {
    var sphere = new BoundingSphere(new Cartesian3(0, 0, -1.5), 0.5);
    var cameraPosition = Cartesian3.ZERO;
    var occluder = new Occluder(sphere, cameraPosition);
 
    expect(function () {
      occluder.computeVisibility();
    }).toThrowDeveloperError();
  });
 
  it("can throw errors during computeOccludeePoint (1 of 5)", function () {
    expect(function () {
      Occluder.computeOccludeePoint();
    }).toThrowDeveloperError();
  });
 
  it("can throw errors during computeOccludeePoint (2 of 5)", function () {
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -5), 1);
    var occludeePosition = new Cartesian3(0, 0, -5);
    var positions = [];
 
    expect(function () {
      Occluder.computeOccludeePoint(occluderBS, occludeePosition, positions);
    }).toThrowDeveloperError();
  });
 
  it("can throw errors during computeOccludeePoint (3 of 5)", function () {
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -5), 1);
    var positions = [];
 
    expect(function () {
      Occluder.computeOccludeePoint(
        occluderBS,
        new Cartesian3(0, 0, -3),
        positions
      );
    }).toThrowDeveloperError();
  });
 
  it("can throw errors during computeOccludeePoint (4 of 5)", function () {
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -5), 1);
 
    expect(function () {
      Occluder.computeOccludeePoint(occluderBS, new Cartesian3(0, 0, -3));
    }).toThrowDeveloperError();
  });
 
  it("can throw errors during computeOccludeePoint (5 of 5)", function () {
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -5), 1);
 
    expect(function () {
      Occluder.computeOccludeePoint(
        occluderBS,
        new Cartesian3(0, 0, -5),
        new Cartesian3(0, 0, -3)
      );
    }).toThrowDeveloperError();
  });
 
  it("can compute an occludee point", function () {
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -8), 2);
    var positions = [
      new Cartesian3(-1.085, 0, -6.221),
      new Cartesian3(1.085, 0, -6.221),
    ];
    var tileOccluderSphere = BoundingSphere.fromPoints(positions);
    var occludeePosition = tileOccluderSphere.center;
    var result = Occluder.computeOccludeePoint(
      occluderBS,
      occludeePosition,
      positions
    );
    expect(result).toEqualEpsilon(
      new Cartesian3(0, 0, -5),
      CesiumMath.EPSILON1
    );
  });
 
  it("can compute a rotation vector (major axis = 0)", function () {
    var cameraPosition = Cartesian3.ZERO;
    var occluderBS = new BoundingSphere(new Cartesian3(5, 0, 0), 2);
    var occluder = new Occluder(occluderBS, cameraPosition);
    var occludeeBS = new BoundingSphere(new Cartesian3(8, 0, 0), 1);
    var occludee = new Occluder(occludeeBS, cameraPosition);
 
    var occluderPosition = occluder.position;
    var occludeePosition = occludee.position;
    var occluderPlaneNormal = Cartesian3.normalize(
      Cartesian3.subtract(occludeePosition, occluderPosition, new Cartesian3()),
      new Cartesian3()
    );
    var occluderPlaneD = -Cartesian3.dot(occluderPlaneNormal, occluderPosition);
 
    var tempVec0 = Cartesian3.abs(
      Cartesian3.clone(occluderPlaneNormal),
      new Cartesian3()
    );
    var majorAxis = tempVec0.x > tempVec0.y ? 0 : 1;
    if (
      (majorAxis === 0 && tempVec0.z > tempVec0.x) ||
      (majorAxis === 1 && tempVec0.z > tempVec0.y)
    ) {
      majorAxis = 2;
    }
    expect(majorAxis).toEqual(0);
    var aRotationVector = Occluder._anyRotationVector(
      occluderPosition,
      occluderPlaneNormal,
      occluderPlaneD
    );
    expect(aRotationVector).toBeTruthy();
  });
 
  it("can compute a rotation vector (major axis = 1)", function () {
    var cameraPosition = Cartesian3.ZERO;
    var occluderBS = new BoundingSphere(new Cartesian3(5, 0, 0), 2);
    var occluder = new Occluder(occluderBS, cameraPosition);
    var occludeeBS = new BoundingSphere(new Cartesian3(7, 2, 0), 1);
    var occludee = new Occluder(occludeeBS, cameraPosition);
 
    var occluderPosition = occluder.position;
    var occludeePosition = occludee.position;
    var occluderPlaneNormal = Cartesian3.normalize(
      Cartesian3.subtract(occludeePosition, occluderPosition, new Cartesian3()),
      new Cartesian3()
    );
    var occluderPlaneD = -Cartesian3.dot(occluderPlaneNormal, occluderPosition);
 
    var tempVec0 = Cartesian3.abs(
      Cartesian3.clone(occluderPlaneNormal),
      new Cartesian3()
    );
    var majorAxis = tempVec0.x > tempVec0.y ? 0 : 1;
    if (
      (majorAxis === 0 && tempVec0.z > tempVec0.x) ||
      (majorAxis === 1 && tempVec0.z > tempVec0.y)
    ) {
      majorAxis = 2;
    }
    expect(majorAxis).toEqual(1);
    var aRotationVector = Occluder._anyRotationVector(
      occluderPosition,
      occluderPlaneNormal,
      occluderPlaneD
    );
    expect(aRotationVector).toBeTruthy();
  });
 
  it("can compute a rotation vector (major axis = 2)", function () {
    var cameraPosition = Cartesian3.ZERO;
    var occluderBS = new BoundingSphere(new Cartesian3(5, 0, 0), 2);
    var occluder = new Occluder(occluderBS, cameraPosition);
    var occludeeBS = new BoundingSphere(new Cartesian3(6, 0, 2), 1);
    var occludee = new Occluder(occludeeBS, cameraPosition);
 
    var occluderPosition = occluder.position;
    var occludeePosition = occludee.position;
    var occluderPlaneNormal = Cartesian3.normalize(
      Cartesian3.subtract(occludeePosition, occluderPosition, new Cartesian3()),
      new Cartesian3()
    );
    var occluderPlaneD = -Cartesian3.dot(occluderPlaneNormal, occluderPosition);
 
    var tempVec0 = Cartesian3.abs(
      Cartesian3.clone(occluderPlaneNormal),
      new Cartesian3()
    );
    var majorAxis = tempVec0.x > tempVec0.y ? 0 : 1;
    if (
      (majorAxis === 0 && tempVec0.z > tempVec0.x) ||
      (majorAxis === 1 && tempVec0.z > tempVec0.y)
    ) {
      majorAxis = 2;
    }
    expect(majorAxis).toEqual(2);
    var aRotationVector = Occluder._anyRotationVector(
      occluderPosition,
      occluderPlaneNormal,
      occluderPlaneD
    );
    expect(aRotationVector).toBeTruthy();
  });
 
  it("can  have an invisible occludee point", function () {
    var cameraPosition = new Cartesian3(0, 0, -8);
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -8), 2);
    var occluder = new Occluder(occluderBS, cameraPosition);
    var positions = [
      new Cartesian3(-0.25, 0, -5.3),
      new Cartesian3(0.25, 0, -5.3),
    ];
    var tileOccluderSphere = BoundingSphere.fromPoints(positions);
    var occludeePosition = tileOccluderSphere.center;
    var result = Occluder.computeOccludeePoint(
      occluderBS,
      occludeePosition,
      positions
    );
 
    var bs = new BoundingSphere(result, 0.0);
 
    expect(occluder.isBoundingSphereVisible(bs)).toEqual(false);
    expect(occluder.computeVisibility(bs)).toEqual(Visibility.NONE);
  });
 
  it("can have a visible occludee point", function () {
    var cameraPosition = new Cartesian3(3, 0, -8);
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -8), 2);
    var occluder = new Occluder(occluderBS, cameraPosition);
    var positions = [
      new Cartesian3(-0.25, 0, -5.3),
      new Cartesian3(0.25, 0, -5.3),
    ];
    var tileOccluderSphere = BoundingSphere.fromPoints(positions);
    var occludeePosition = tileOccluderSphere.center;
    var result = Occluder.computeOccludeePoint(
      occluderBS,
      occludeePosition,
      positions
    );
    expect(
      occluder.isBoundingSphereVisible(new BoundingSphere(result, 0.0))
    ).toEqual(true);
  });
 
  it("compute occludee point from rectangle throws without a rectangle", function () {
    expect(function () {
      return Occluder.computeOccludeePointFromRectangle();
    }).toThrowDeveloperError();
  });
 
  it("compute invalid occludee point from rectangle", function () {
    var rectangle = Rectangle.MAX_VALUE;
    expect(Occluder.computeOccludeePointFromRectangle(rectangle)).toEqual(
      undefined
    );
  });
 
  it("compute valid occludee point from rectangle", function () {
    var edge = Math.PI / 32.0;
    var rectangle = new Rectangle(-edge, -edge, edge, edge);
    var ellipsoid = Ellipsoid.WGS84;
    var positions = Rectangle.subsample(rectangle, ellipsoid);
    var bs = BoundingSphere.fromPoints(positions);
    var point = Occluder.computeOccludeePoint(
      new BoundingSphere(Cartesian3.ZERO, ellipsoid.minimumRadius),
      bs.center,
      positions
    );
    var actual = Occluder.computeOccludeePointFromRectangle(rectangle);
    expect(actual).toEqual(point);
  });
 
  it("fromBoundingSphere throws without a bounding sphere", function () {
    expect(function () {
      Occluder.fromBoundingSphere();
    }).toThrowDeveloperError();
  });
 
  it("fromBoundingSphere throws without camera position", function () {
    expect(function () {
      Occluder.fromBoundingSphere(new BoundingSphere());
    }).toThrowDeveloperError();
  });
 
  it("fromBoundingSphere without result parameter", function () {
    var cameraPosition = new Cartesian3(3, 0, -8);
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -8), 2);
    var occluder0 = new Occluder(occluderBS, cameraPosition);
    var occluder1 = Occluder.fromBoundingSphere(occluderBS, cameraPosition);
 
    expect(occluder1.position).toEqual(occluder0.position);
    expect(occluder1.radius).toEqual(occluder0.radius);
  });
 
  it("fromBoundingSphere with result parameter", function () {
    var cameraPosition = new Cartesian3(3, 0, -8);
    var occluderBS = new BoundingSphere(new Cartesian3(0, 0, -8), 2);
    var occluder0 = new Occluder(occluderBS, cameraPosition);
    var result = new Occluder(occluderBS, Cartesian3.ZERO);
    var occluder1 = Occluder.fromBoundingSphere(
      occluderBS,
      cameraPosition,
      result
    );
 
    expect(occluder1).toBe(result);
    expect(occluder1.position).toEqual(occluder0.position);
    expect(occluder1.radius).toEqual(occluder0.radius);
  });
});