15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
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
import { Cartesian2 } from "../../Source/Cesium.js";
import { Cartesian3 } from "../../Source/Cesium.js";
import { Cartographic } from "../../Source/Cesium.js";
import { Color } from "../../Source/Cesium.js";
import { Ellipsoid } from "../../Source/Cesium.js";
import { GeographicTilingScheme } from "../../Source/Cesium.js";
import { Intersect } from "../../Source/Cesium.js";
import { Math as CesiumMath } from "../../Source/Cesium.js";
import { Plane } from "../../Source/Cesium.js";
import { Rectangle } from "../../Source/Cesium.js";
import { SceneMode } from "../../Source/Cesium.js";
import { TileBoundingRegion } from "../../Source/Cesium.js";
import createFrameState from "../createFrameState.js";
 
describe("Scene/TileBoundingRegion", function () {
  var boundingVolumeRegion = [0.0, 0.0, 1.0, 1.0, 0, 1];
  var regionBox = boundingVolumeRegion.slice(0, 4);
  var rectangle = new Rectangle(
    regionBox[0],
    regionBox[1],
    regionBox[2],
    regionBox[3]
  );
  var tileBoundingRegion = new TileBoundingRegion({
    maximumHeight: boundingVolumeRegion[5],
    minimumHeight: boundingVolumeRegion[4],
    rectangle: rectangle,
  });
 
  var frameState;
  var camera;
 
  beforeEach(function () {
    frameState = createFrameState();
    camera = frameState.camera;
  });
 
  it("throws when options.rectangle is undefined", function () {
    expect(function () {
      return new TileBoundingRegion();
    }).toThrowDeveloperError();
  });
 
  it("can be instantiated with rectangle and heights", function () {
    var minimumHeight = boundingVolumeRegion[4];
    var maximumHeight = boundingVolumeRegion[5];
    var tbr = new TileBoundingRegion({
      maximumHeight: maximumHeight,
      minimumHeight: minimumHeight,
      rectangle: rectangle,
    });
    expect(tbr).toBeDefined();
    expect(tbr.boundingVolume).toBeDefined();
    expect(tbr.boundingSphere).toBeDefined();
    expect(tbr.rectangle).toEqual(rectangle);
    expect(tbr.minimumHeight).toEqual(minimumHeight);
    expect(tbr.maximumHeight).toEqual(maximumHeight);
  });
 
  it("can be instantiated with only a rectangle", function () {
    var tbr = new TileBoundingRegion({ rectangle: rectangle });
    expect(tbr).toBeDefined();
    expect(tbr.boundingVolume).toBeDefined();
    expect(tbr.boundingSphere).toBeDefined();
    expect(tbr.rectangle).toEqual(rectangle);
    expect(tbr.minimumHeight).toBeDefined();
    expect(tbr.maximumHeight).toBeDefined();
  });
 
  it("distanceToCamera throws when frameState is undefined", function () {
    expect(function () {
      return tileBoundingRegion.distanceToCamera();
    }).toThrowDeveloperError();
  });
 
  it("distance to camera is 0 when camera is inside bounding region", function () {
    camera.position = Cartesian3.fromRadians(
      regionBox[0] + CesiumMath.EPSILON6,
      regionBox[1],
      0
    );
    expect(tileBoundingRegion.distanceToCamera(frameState)).toEqual(0.0);
  });
 
  it("distance to camera is correct when camera is outside bounding region", function () {
    camera.position = Cartesian3.fromRadians(regionBox[0], regionBox[1], 2.0);
    expect(tileBoundingRegion.distanceToCamera(frameState)).toEqualEpsilon(
      1.0,
      CesiumMath.EPSILON6
    );
  });
 
  it("distanceToCamera", function () {
    var offset = 0.0001;
    var west = -0.001;
    var south = -0.001;
    var east = 0.001;
    var north = 0.001;
 
    var tile = new TileBoundingRegion({
      rectangle: new Rectangle(west, south, east, north),
      minimumHeight: 0.0,
      maximumHeight: 10.0,
    });
 
    // Inside rectangle, above height
    camera.position = Cartesian3.fromRadians(0.0, 0.0, 20.0);
    expect(tile.distanceToCamera(frameState)).toEqualEpsilon(
      10.0,
      CesiumMath.EPSILON3
    );
 
    // Inside rectangle, below height
    camera.position = Cartesian3.fromRadians(0.0, 0.0, 5.0);
    expect(tile.distanceToCamera(frameState)).toEqual(0.0);
 
    // From southwest
    camera.position = Cartesian3.fromRadians(
      west - offset,
      south - offset,
      0.0
    );
    var southwestPosition = Cartesian3.fromRadians(west, south);
    var expectedDistance = Cartesian3.distance(
      camera.position,
      southwestPosition
    );
    expect(tile.distanceToCamera(frameState)).toEqualEpsilon(
      expectedDistance,
      CesiumMath.EPSILON1
    );
 
    // From northeast
    camera.position = Cartesian3.fromRadians(
      east + offset,
      north + offset,
      0.0
    );
    var northeastPosition = Cartesian3.fromRadians(east, north);
    expectedDistance = Cartesian3.distance(camera.position, northeastPosition);
    expect(tile.distanceToCamera(frameState)).toEqualEpsilon(
      expectedDistance,
      CesiumMath.EPSILON1
    );
  });
 
  it("distanceToCamera close to south plane at the northern hemisphere", function () {
    var ellipsoid = Ellipsoid.WGS84;
    var tilingScheme = new GeographicTilingScheme({ ellipsoid: ellipsoid });
 
    // Tile on the northern hemisphere
    var rectangle = tilingScheme.tileXYToRectangle(5, 0, 2);
    var cameraPositionCartographic = new Cartographic(
      (rectangle.west + rectangle.east) * 0.5,
      rectangle.south,
      0.0
    );
 
    cameraPositionCartographic.south -= CesiumMath.EPSILON8;
 
    var tile = new TileBoundingRegion({
      rectangle: rectangle,
      minimumHeight: 0.0,
      maximumHeight: 10.0,
    });
 
    camera.position = ellipsoid.cartographicToCartesian(
      cameraPositionCartographic,
      new Cartesian3()
    );
    expect(tile.distanceToCamera(frameState)).toBeLessThan(
      CesiumMath.EPSILON8 * ellipsoid.maximumRadius
    );
  });
 
  it("distanceToCamera close to north plane at the southern hemisphere", function () {
    var ellipsoid = Ellipsoid.WGS84;
    var tilingScheme = new GeographicTilingScheme({ ellipsoid: ellipsoid });
 
    // Tile on the southern hemisphere
    var rectangle = tilingScheme.tileXYToRectangle(4, 3, 2);
    var cameraPositionCartographic = new Cartographic(
      (rectangle.west + rectangle.east) * 0.5,
      rectangle.north,
      0.0
    );
 
    cameraPositionCartographic.north += CesiumMath.EPSILON8;
 
    var tile = new TileBoundingRegion({
      rectangle: rectangle,
      minimumHeight: 0.0,
      maximumHeight: 10.0,
    });
 
    camera.position = ellipsoid.cartographicToCartesian(
      cameraPositionCartographic,
      new Cartesian3()
    );
    expect(tile.distanceToCamera(frameState)).toBeLessThan(
      CesiumMath.EPSILON8 * ellipsoid.maximumRadius
    );
  });
 
  it("distanceToCamera in 2D", function () {
    frameState.mode = SceneMode.SCENE2D;
 
    var offset = 0.0001;
    var west = -0.001;
    var south = -0.001;
    var east = 0.001;
    var north = 0.001;
 
    var tile = new TileBoundingRegion({
      rectangle: new Rectangle(west, south, east, north),
      minimumHeight: 0.0,
      maximumHeight: 10.0,
    });
 
    // Inside rectangle
    camera.position = Cartesian3.fromRadians(0.0, 0.0, 0.0);
    expect(tile.distanceToCamera(frameState)).toEqual(Ellipsoid.WGS84.radii.x);
 
    // From southwest
    var southwest3D = new Cartographic(west, south, 0.0);
    var southwest2D = frameState.mapProjection.project(southwest3D);
    var position3D = new Cartographic(west - offset, south - offset, 0.0);
    var position2D = frameState.mapProjection.project(position3D);
    var distance2D = Cartesian2.distance(southwest2D, position2D);
    var height = Ellipsoid.WGS84.radii.x;
    var expectedDistance = Math.sqrt(distance2D * distance2D + height * height);
 
    camera.position = Cartesian3.fromRadians(
      position3D.longitude,
      position3D.latitude
    );
    expect(tile.distanceToCamera(frameState)).toEqualEpsilon(
      expectedDistance,
      10.0
    );
  });
 
  it("createDebugVolume throws when color is undefined", function () {
    expect(function () {
      return tileBoundingRegion.createDebugVolume();
    }).toThrowDeveloperError();
  });
 
  it("can create a debug volume", function () {
    var debugVolume = tileBoundingRegion.createDebugVolume(Color.BLUE);
    expect(debugVolume).toBeDefined();
  });
 
  it("intersectPlane throws when plane is undefined", function () {
    expect(function () {
      return tileBoundingRegion.intersectPlane();
    }).toThrowDeveloperError();
  });
 
  it("intersects plane", function () {
    var normal = new Cartesian3();
    Cartesian3.normalize(Cartesian3.fromRadians(0.0, 0.0, 1.0), normal);
    var distanceFromCenter = Cartesian3.distance(
      new Cartesian3(0.0, 0.0, 0.0),
      Cartesian3.fromRadians(0.0, 0.0, 0.0)
    );
    var plane = new Plane(normal, -distanceFromCenter);
    expect(tileBoundingRegion.intersectPlane(plane)).toEqual(
      Intersect.INTERSECTING
    );
  });
});