yzt
2023-05-05 4c558c77a6a9d23f057f094c4dc3e315eabef497
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
import { ArcGISTiledElevationTerrainProvider } from "../../Source/Cesium.js";
import { Cartographic } from "../../Source/Cesium.js";
import { CesiumTerrainProvider } from "../../Source/Cesium.js";
import { createWorldTerrain } from "../../Source/Cesium.js";
import { defined } from "../../Source/Cesium.js";
import { RequestScheduler } from "../../Source/Cesium.js";
import { Resource } from "../../Source/Cesium.js";
import { sampleTerrain } from "../../Source/Cesium.js";
 
describe("Core/sampleTerrain", function () {
  var worldTerrain;
  beforeAll(function () {
    worldTerrain = createWorldTerrain();
    return worldTerrain.readyPromise;
  });
 
  it("queries heights", function () {
    var positions = [
      Cartographic.fromDegrees(86.925145, 27.988257),
      Cartographic.fromDegrees(87.0, 28.0),
    ];
 
    return sampleTerrain(worldTerrain, 11, positions).then(function (
      passedPositions
    ) {
      expect(passedPositions).toBe(positions);
      expect(positions[0].height).toBeGreaterThan(5000);
      expect(positions[0].height).toBeLessThan(10000);
      expect(positions[1].height).toBeGreaterThan(5000);
      expect(positions[1].height).toBeLessThan(10000);
    });
  });
 
  it("queries heights from Small Terrain", function () {
    var terrainProvider = new CesiumTerrainProvider({
      url: "https://s3.amazonaws.com/cesiumjs/smallTerrain",
    });
 
    var positions = [
      Cartographic.fromDegrees(86.925145, 27.988257),
      Cartographic.fromDegrees(87.0, 28.0),
    ];
 
    return sampleTerrain(terrainProvider, 11, positions).then(function (
      passedPositions
    ) {
      expect(passedPositions).toBe(positions);
      expect(positions[0].height).toBeGreaterThan(5000);
      expect(positions[0].height).toBeLessThan(10000);
      expect(positions[1].height).toBeGreaterThan(5000);
      expect(positions[1].height).toBeLessThan(10000);
    });
  });
 
  it("sets height to undefined if terrain data is not available at the position and specified level", function () {
    var positions = [Cartographic.fromDegrees(0.0, 0.0, 0.0)];
 
    return sampleTerrain(worldTerrain, 18, positions).then(function () {
      expect(positions[0].height).toBeUndefined();
    });
  });
 
  it("fills in what it can when given a mix of positions with and without valid tiles", function () {
    var positions = [
      Cartographic.fromDegrees(86.925145, 27.988257),
      Cartographic.fromDegrees(0.0, 89.0, 0.0),
      Cartographic.fromDegrees(87.0, 28.0),
    ];
 
    return sampleTerrain(worldTerrain, 12, positions).then(function () {
      expect(positions[0].height).toBeGreaterThan(5000);
      expect(positions[0].height).toBeLessThan(10000);
      expect(positions[1].height).toBeUndefined();
      expect(positions[2].height).toBeGreaterThan(5000);
      expect(positions[2].height).toBeLessThan(10000);
    });
  });
 
  it("requires terrainProvider, level, and positions", function () {
    var positions = [
      Cartographic.fromDegrees(86.925145, 27.988257),
      Cartographic.fromDegrees(0.0, 0.0, 0.0),
      Cartographic.fromDegrees(87.0, 28.0),
    ];
 
    expect(function () {
      sampleTerrain(undefined, 11, positions);
    }).toThrowDeveloperError();
 
    expect(function () {
      sampleTerrain(worldTerrain, undefined, positions);
    }).toThrowDeveloperError();
 
    expect(function () {
      sampleTerrain(worldTerrain, 11, undefined);
    }).toThrowDeveloperError();
  });
 
  it("works for a dodgy point right near the edge of a tile", function () {
    var positions = [new Cartographic(0.33179290856829535, 0.7363107781851078)];
 
    return sampleTerrain(worldTerrain, 12, positions).then(function () {
      expect(positions[0].height).toBeDefined();
    });
  });
 
  describe("with terrain providers", function () {
    beforeEach(function () {
      RequestScheduler.clearForSpecs();
    });
 
    afterEach(function () {
      Resource._Implementations.loadWithXhr =
        Resource._DefaultImplementations.loadWithXhr;
    });
 
    function spyOnTerrainDataCreateMesh(terrainProvider) {
      // do some sneaky spying so we can check how many times createMesh is called
      var originalRequestTileGeometry = terrainProvider.requestTileGeometry;
      spyOn(terrainProvider, "requestTileGeometry").and.callFake(function (
        x,
        y,
        level,
        request
      ) {
        // Call the original function!
        return originalRequestTileGeometry
          .call(terrainProvider, x, y, level, request)
          .then(function (tile) {
            spyOn(tile, "createMesh").and.callThrough();
            // return the original tile - after we've spied on the createMesh method
            return tile;
          });
      });
    }
 
    function expectTileAndMeshCounts(
      terrainProvider,
      numberOfTilesRequested,
      wasFirstTileMeshCreated
    ) {
      // assert how many tiles were requested
      expect(terrainProvider.requestTileGeometry.calls.count()).toEqual(
        numberOfTilesRequested
      );
 
      // get the first tile that was requested
      return (
        terrainProvider.requestTileGeometry.calls
          .first()
          // the return value was the promise of the tile, so wait for that
          .returnValue.then(function (terrainData) {
            // assert if the mesh was created or not for this tile
            expect(terrainData.createMesh.calls.count()).toEqual(
              wasFirstTileMeshCreated ? 1 : 0
            );
          })
      );
    }
 
    function endsWith(value, suffix) {
      return value.indexOf(suffix, value.length - suffix.length) >= 0;
    }
 
    function patchXHRLoad(proxySpec) {
      Resource._Implementations.loadWithXhr = function (
        url,
        responseType,
        method,
        data,
        headers,
        deferred,
        overrideMimeType
      ) {
        // find a key (source path) path in the spec which matches (ends with) the requested url
        var availablePaths = Object.keys(proxySpec);
        var proxiedUrl;
 
        for (var i = 0; i < availablePaths.length; i++) {
          var srcPath = availablePaths[i];
          if (endsWith(url, srcPath)) {
            proxiedUrl = proxySpec[availablePaths[i]];
            break;
          }
        }
 
        // it's a whitelist - meaning you have to proxy every request explicitly
        if (!defined(proxiedUrl)) {
          throw new Error(
            "Unexpected XHR load to url: " +
              url +
              "; spec includes: " +
              availablePaths.join(", ")
          );
        }
 
        // make a real request to the proxied path for the matching source path
        return Resource._DefaultImplementations.loadWithXhr(
          proxiedUrl,
          responseType,
          method,
          data,
          headers,
          deferred,
          overrideMimeType
        );
      };
    }
 
    it("should work for Cesium World Terrain", function () {
      patchXHRLoad({
        "/layer.json": "Data/CesiumTerrainTileJson/9_759_335/layer.json",
        "/9/759/335.terrain?v=1.2.0":
          "Data/CesiumTerrainTileJson/9_759_335/9_759_335.terrain",
      });
      var terrainProvider = new CesiumTerrainProvider({
        url: "made/up/url",
      });
 
      spyOnTerrainDataCreateMesh(terrainProvider);
 
      var positionA = Cartographic.fromDegrees(
        86.93666235421982,
        27.97989963555095
      );
      var positionB = Cartographic.fromDegrees(
        86.9366623542198,
        27.9798996355509
      );
      var positionC = Cartographic.fromDegrees(
        86.936662354213,
        27.979899635557
      );
 
      var level = 9;
 
      return sampleTerrain(terrainProvider, level, [
        positionA,
        positionB,
        positionC,
      ]).then(function () {
        expect(positionA.height).toBeCloseTo(7780, 0);
        expect(positionB.height).toBeCloseTo(7780, 0);
        expect(positionC.height).toBeCloseTo(7780, 0);
        // 1 tile was requested (all positions were close enough on the same tile)
        //  and the mesh was not created because we're using CWT - which doesn't need the mesh for interpolation
        return expectTileAndMeshCounts(terrainProvider, 1, false);
      });
    });
 
    it("should work for ArcGIS terrain", function () {
      patchXHRLoad({
        "/?f=pjson": "Data/ArcGIS/9_214_379/root.json",
        "/tilemap/10/384/640/128/128":
          "Data/ArcGIS/9_214_379/tilemap_10_384_640_128_128.json",
        "/tile/9/214/379": "Data/ArcGIS/9_214_379/tile_9_214_379.tile",
      });
 
      var terrainProvider = new ArcGISTiledElevationTerrainProvider({
        url: "made/up/url",
      });
 
      spyOnTerrainDataCreateMesh(terrainProvider);
 
      var positionA = Cartographic.fromDegrees(
        86.93666235421982,
        27.97989963555095
      );
      var positionB = Cartographic.fromDegrees(
        86.9366623542198,
        27.9798996355509
      );
      var positionC = Cartographic.fromDegrees(
        86.936662354213,
        27.979899635557
      );
 
      var level = 9;
      return sampleTerrain(terrainProvider, level, [
        positionA,
        positionB,
        positionC,
      ]).then(function () {
        // 3 very similar positions
        expect(positionA.height).toBeCloseTo(7681, 0);
        expect(positionB.height).toBeCloseTo(7681, 0);
        expect(positionC.height).toBeCloseTo(7681, 0);
        // 1 tile was requested (all positions were close enough on the same tile)
        //  and the mesh was created once because we're using an ArcGIS tile
        return expectTileAndMeshCounts(terrainProvider, 1, true);
      });
    });
  });
});