GoogleEarthEnterpriseTerrainData.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. import BoundingSphere from "./BoundingSphere.js";
  2. import Cartesian2 from "./Cartesian2.js";
  3. import Cartesian3 from "./Cartesian3.js";
  4. import Check from "./Check.js";
  5. import defaultValue from "./defaultValue.js";
  6. import defined from "./defined.js";
  7. import DeveloperError from "./DeveloperError.js";
  8. import IndexDatatype from "./IndexDatatype.js";
  9. import Intersections2D from "./Intersections2D.js";
  10. import CesiumMath from "./Math.js";
  11. import OrientedBoundingBox from "./OrientedBoundingBox.js";
  12. import QuantizedMeshTerrainData from "./QuantizedMeshTerrainData.js";
  13. import Rectangle from "./Rectangle.js";
  14. import TaskProcessor from "./TaskProcessor.js";
  15. import TerrainData from "./TerrainData.js";
  16. import TerrainEncoding from "./TerrainEncoding.js";
  17. import TerrainMesh from "./TerrainMesh.js";
  18. /**
  19. * Terrain data for a single tile from a Google Earth Enterprise server.
  20. *
  21. * @alias GoogleEarthEnterpriseTerrainData
  22. * @constructor
  23. *
  24. * @param {Object} options Object with the following properties:
  25. * @param {ArrayBuffer} options.buffer The buffer containing terrain data.
  26. * @param {Number} options.negativeAltitudeExponentBias Multiplier for negative terrain heights that are encoded as very small positive values.
  27. * @param {Number} options.negativeElevationThreshold Threshold for negative values
  28. * @param {Number} [options.childTileMask=15] A bit mask indicating which of this tile's four children exist.
  29. * If a child's bit is set, geometry will be requested for that tile as well when it
  30. * is needed. If the bit is cleared, the child tile is not requested and geometry is
  31. * instead upsampled from the parent. The bit values are as follows:
  32. * <table>
  33. * <tr><th>Bit Position</th><th>Bit Value</th><th>Child Tile</th></tr>
  34. * <tr><td>0</td><td>1</td><td>Southwest</td></tr>
  35. * <tr><td>1</td><td>2</td><td>Southeast</td></tr>
  36. * <tr><td>2</td><td>4</td><td>Northeast</td></tr>
  37. * <tr><td>3</td><td>8</td><td>Northwest</td></tr>
  38. * </table>
  39. * @param {Boolean} [options.createdByUpsampling=false] True if this instance was created by upsampling another instance;
  40. * otherwise, false.
  41. * @param {Credit[]} [options.credits] Array of credits for this tile.
  42. *
  43. *
  44. * @example
  45. * const buffer = ...
  46. * const childTileMask = ...
  47. * const terrainData = new Cesium.GoogleEarthEnterpriseTerrainData({
  48. * buffer : heightBuffer,
  49. * childTileMask : childTileMask
  50. * });
  51. *
  52. * @see TerrainData
  53. * @see HeightmapTerrainData
  54. * @see QuantizedMeshTerrainData
  55. */
  56. function GoogleEarthEnterpriseTerrainData(options) {
  57. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  58. //>>includeStart('debug', pragmas.debug);
  59. Check.typeOf.object("options.buffer", options.buffer);
  60. Check.typeOf.number(
  61. "options.negativeAltitudeExponentBias",
  62. options.negativeAltitudeExponentBias
  63. );
  64. Check.typeOf.number(
  65. "options.negativeElevationThreshold",
  66. options.negativeElevationThreshold
  67. );
  68. //>>includeEnd('debug');
  69. this._buffer = options.buffer;
  70. this._credits = options.credits;
  71. this._negativeAltitudeExponentBias = options.negativeAltitudeExponentBias;
  72. this._negativeElevationThreshold = options.negativeElevationThreshold;
  73. // Convert from google layout to layout of other providers
  74. // 3 2 -> 2 3
  75. // 0 1 -> 0 1
  76. const googleChildTileMask = defaultValue(options.childTileMask, 15);
  77. let childTileMask = googleChildTileMask & 3; // Bottom row is identical
  78. childTileMask |= googleChildTileMask & 4 ? 8 : 0; // NE
  79. childTileMask |= googleChildTileMask & 8 ? 4 : 0; // NW
  80. this._childTileMask = childTileMask;
  81. this._createdByUpsampling = defaultValue(options.createdByUpsampling, false);
  82. this._skirtHeight = undefined;
  83. this._bufferType = this._buffer.constructor;
  84. this._mesh = undefined;
  85. this._minimumHeight = undefined;
  86. this._maximumHeight = undefined;
  87. }
  88. Object.defineProperties(GoogleEarthEnterpriseTerrainData.prototype, {
  89. /**
  90. * An array of credits for this tile
  91. * @memberof GoogleEarthEnterpriseTerrainData.prototype
  92. * @type {Credit[]}
  93. */
  94. credits: {
  95. get: function () {
  96. return this._credits;
  97. },
  98. },
  99. /**
  100. * The water mask included in this terrain data, if any. A water mask is a rectangular
  101. * Uint8Array or image where a value of 255 indicates water and a value of 0 indicates land.
  102. * Values in between 0 and 255 are allowed as well to smoothly blend between land and water.
  103. * @memberof GoogleEarthEnterpriseTerrainData.prototype
  104. * @type {Uint8Array|HTMLImageElement|HTMLCanvasElement}
  105. */
  106. waterMask: {
  107. get: function () {
  108. return undefined;
  109. },
  110. },
  111. });
  112. const createMeshTaskName = "createVerticesFromGoogleEarthEnterpriseBuffer";
  113. const createMeshTaskProcessorNoThrottle = new TaskProcessor(createMeshTaskName);
  114. const createMeshTaskProcessorThrottle = new TaskProcessor(
  115. createMeshTaskName,
  116. TerrainData.maximumAsynchronousTasks
  117. );
  118. const nativeRectangleScratch = new Rectangle();
  119. const rectangleScratch = new Rectangle();
  120. /**
  121. * Creates a {@link TerrainMesh} from this terrain data.
  122. *
  123. * @private
  124. *
  125. * @param {Object} options Object with the following properties:
  126. * @param {TilingScheme} options.tilingScheme The tiling scheme to which this tile belongs.
  127. * @param {Number} options.x The X coordinate of the tile for which to create the terrain data.
  128. * @param {Number} options.y The Y coordinate of the tile for which to create the terrain data.
  129. * @param {Number} options.level The level of the tile for which to create the terrain data.
  130. * @param {Number} [options.exaggeration=1.0] The scale used to exaggerate the terrain.
  131. * @param {Number} [options.exaggerationRelativeHeight=0.0] The height from which terrain is exaggerated.
  132. * @param {Boolean} [options.throttle=true] If true, indicates that this operation will need to be retried if too many asynchronous mesh creations are already in progress.
  133. * @returns {Promise.<TerrainMesh>|undefined} A promise for the terrain mesh, or undefined if too many
  134. * asynchronous mesh creations are already in progress and the operation should
  135. * be retried later.
  136. */
  137. GoogleEarthEnterpriseTerrainData.prototype.createMesh = function (options) {
  138. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  139. //>>includeStart('debug', pragmas.debug);
  140. Check.typeOf.object("options.tilingScheme", options.tilingScheme);
  141. Check.typeOf.number("options.x", options.x);
  142. Check.typeOf.number("options.y", options.y);
  143. Check.typeOf.number("options.level", options.level);
  144. //>>includeEnd('debug');
  145. const tilingScheme = options.tilingScheme;
  146. const x = options.x;
  147. const y = options.y;
  148. const level = options.level;
  149. const exaggeration = defaultValue(options.exaggeration, 1.0);
  150. const exaggerationRelativeHeight = defaultValue(
  151. options.exaggerationRelativeHeight,
  152. 0.0
  153. );
  154. const throttle = defaultValue(options.throttle, true);
  155. const ellipsoid = tilingScheme.ellipsoid;
  156. tilingScheme.tileXYToNativeRectangle(x, y, level, nativeRectangleScratch);
  157. tilingScheme.tileXYToRectangle(x, y, level, rectangleScratch);
  158. // Compute the center of the tile for RTC rendering.
  159. const center = ellipsoid.cartographicToCartesian(
  160. Rectangle.center(rectangleScratch)
  161. );
  162. const levelZeroMaxError = 40075.16; // From Google's Doc
  163. const thisLevelMaxError = levelZeroMaxError / (1 << level);
  164. this._skirtHeight = Math.min(thisLevelMaxError * 8.0, 1000.0);
  165. const createMeshTaskProcessor = throttle
  166. ? createMeshTaskProcessorThrottle
  167. : createMeshTaskProcessorNoThrottle;
  168. const verticesPromise = createMeshTaskProcessor.scheduleTask({
  169. buffer: this._buffer,
  170. nativeRectangle: nativeRectangleScratch,
  171. rectangle: rectangleScratch,
  172. relativeToCenter: center,
  173. ellipsoid: ellipsoid,
  174. skirtHeight: this._skirtHeight,
  175. exaggeration: exaggeration,
  176. exaggerationRelativeHeight: exaggerationRelativeHeight,
  177. includeWebMercatorT: true,
  178. negativeAltitudeExponentBias: this._negativeAltitudeExponentBias,
  179. negativeElevationThreshold: this._negativeElevationThreshold,
  180. });
  181. if (!defined(verticesPromise)) {
  182. // Postponed
  183. return undefined;
  184. }
  185. const that = this;
  186. return verticesPromise.then(function (result) {
  187. // Clone complex result objects because the transfer from the web worker
  188. // has stripped them down to JSON-style objects.
  189. that._mesh = new TerrainMesh(
  190. center,
  191. new Float32Array(result.vertices),
  192. new Uint16Array(result.indices),
  193. result.indexCountWithoutSkirts,
  194. result.vertexCountWithoutSkirts,
  195. result.minimumHeight,
  196. result.maximumHeight,
  197. BoundingSphere.clone(result.boundingSphere3D),
  198. Cartesian3.clone(result.occludeePointInScaledSpace),
  199. result.numberOfAttributes,
  200. OrientedBoundingBox.clone(result.orientedBoundingBox),
  201. TerrainEncoding.clone(result.encoding),
  202. result.westIndicesSouthToNorth,
  203. result.southIndicesEastToWest,
  204. result.eastIndicesNorthToSouth,
  205. result.northIndicesWestToEast
  206. );
  207. that._minimumHeight = result.minimumHeight;
  208. that._maximumHeight = result.maximumHeight;
  209. // Free memory received from server after mesh is created.
  210. that._buffer = undefined;
  211. return that._mesh;
  212. });
  213. };
  214. /**
  215. * Computes the terrain height at a specified longitude and latitude.
  216. *
  217. * @param {Rectangle} rectangle The rectangle covered by this terrain data.
  218. * @param {Number} longitude The longitude in radians.
  219. * @param {Number} latitude The latitude in radians.
  220. * @returns {Number} The terrain height at the specified position. If the position
  221. * is outside the rectangle, this method will extrapolate the height, which is likely to be wildly
  222. * incorrect for positions far outside the rectangle.
  223. */
  224. GoogleEarthEnterpriseTerrainData.prototype.interpolateHeight = function (
  225. rectangle,
  226. longitude,
  227. latitude
  228. ) {
  229. const u = CesiumMath.clamp(
  230. (longitude - rectangle.west) / rectangle.width,
  231. 0.0,
  232. 1.0
  233. );
  234. const v = CesiumMath.clamp(
  235. (latitude - rectangle.south) / rectangle.height,
  236. 0.0,
  237. 1.0
  238. );
  239. if (!defined(this._mesh)) {
  240. return interpolateHeight(this, u, v, rectangle);
  241. }
  242. return interpolateMeshHeight(this, u, v);
  243. };
  244. const upsampleTaskProcessor = new TaskProcessor(
  245. "upsampleQuantizedTerrainMesh",
  246. TerrainData.maximumAsynchronousTasks
  247. );
  248. /**
  249. * Upsamples this terrain data for use by a descendant tile. The resulting instance will contain a subset of the
  250. * height samples in this instance, interpolated if necessary.
  251. *
  252. * @param {TilingScheme} tilingScheme The tiling scheme of this terrain data.
  253. * @param {Number} thisX The X coordinate of this tile in the tiling scheme.
  254. * @param {Number} thisY The Y coordinate of this tile in the tiling scheme.
  255. * @param {Number} thisLevel The level of this tile in the tiling scheme.
  256. * @param {Number} descendantX The X coordinate within the tiling scheme of the descendant tile for which we are upsampling.
  257. * @param {Number} descendantY The Y coordinate within the tiling scheme of the descendant tile for which we are upsampling.
  258. * @param {Number} descendantLevel The level within the tiling scheme of the descendant tile for which we are upsampling.
  259. * @returns {Promise.<HeightmapTerrainData>|undefined} A promise for upsampled heightmap terrain data for the descendant tile,
  260. * or undefined if too many asynchronous upsample operations are in progress and the request has been
  261. * deferred.
  262. */
  263. GoogleEarthEnterpriseTerrainData.prototype.upsample = function (
  264. tilingScheme,
  265. thisX,
  266. thisY,
  267. thisLevel,
  268. descendantX,
  269. descendantY,
  270. descendantLevel
  271. ) {
  272. //>>includeStart('debug', pragmas.debug);
  273. Check.typeOf.object("tilingScheme", tilingScheme);
  274. Check.typeOf.number("thisX", thisX);
  275. Check.typeOf.number("thisY", thisY);
  276. Check.typeOf.number("thisLevel", thisLevel);
  277. Check.typeOf.number("descendantX", descendantX);
  278. Check.typeOf.number("descendantY", descendantY);
  279. Check.typeOf.number("descendantLevel", descendantLevel);
  280. const levelDifference = descendantLevel - thisLevel;
  281. if (levelDifference > 1) {
  282. throw new DeveloperError(
  283. "Upsampling through more than one level at a time is not currently supported."
  284. );
  285. }
  286. //>>includeEnd('debug');
  287. const mesh = this._mesh;
  288. if (!defined(this._mesh)) {
  289. return undefined;
  290. }
  291. const isEastChild = thisX * 2 !== descendantX;
  292. const isNorthChild = thisY * 2 === descendantY;
  293. const ellipsoid = tilingScheme.ellipsoid;
  294. const childRectangle = tilingScheme.tileXYToRectangle(
  295. descendantX,
  296. descendantY,
  297. descendantLevel
  298. );
  299. const upsamplePromise = upsampleTaskProcessor.scheduleTask({
  300. vertices: mesh.vertices,
  301. indices: mesh.indices,
  302. indexCountWithoutSkirts: mesh.indexCountWithoutSkirts,
  303. vertexCountWithoutSkirts: mesh.vertexCountWithoutSkirts,
  304. encoding: mesh.encoding,
  305. minimumHeight: this._minimumHeight,
  306. maximumHeight: this._maximumHeight,
  307. isEastChild: isEastChild,
  308. isNorthChild: isNorthChild,
  309. childRectangle: childRectangle,
  310. ellipsoid: ellipsoid,
  311. });
  312. if (!defined(upsamplePromise)) {
  313. // Postponed
  314. return undefined;
  315. }
  316. const that = this;
  317. return upsamplePromise.then(function (result) {
  318. const quantizedVertices = new Uint16Array(result.vertices);
  319. const indicesTypedArray = IndexDatatype.createTypedArray(
  320. quantizedVertices.length / 3,
  321. result.indices
  322. );
  323. const skirtHeight = that._skirtHeight;
  324. // Use QuantizedMeshTerrainData since we have what we need already parsed
  325. return new QuantizedMeshTerrainData({
  326. quantizedVertices: quantizedVertices,
  327. indices: indicesTypedArray,
  328. minimumHeight: result.minimumHeight,
  329. maximumHeight: result.maximumHeight,
  330. boundingSphere: BoundingSphere.clone(result.boundingSphere),
  331. orientedBoundingBox: OrientedBoundingBox.clone(
  332. result.orientedBoundingBox
  333. ),
  334. horizonOcclusionPoint: Cartesian3.clone(result.horizonOcclusionPoint),
  335. westIndices: result.westIndices,
  336. southIndices: result.southIndices,
  337. eastIndices: result.eastIndices,
  338. northIndices: result.northIndices,
  339. westSkirtHeight: skirtHeight,
  340. southSkirtHeight: skirtHeight,
  341. eastSkirtHeight: skirtHeight,
  342. northSkirtHeight: skirtHeight,
  343. childTileMask: 0,
  344. createdByUpsampling: true,
  345. credits: that._credits,
  346. });
  347. });
  348. };
  349. /**
  350. * Determines if a given child tile is available, based on the
  351. * {@link HeightmapTerrainData.childTileMask}. The given child tile coordinates are assumed
  352. * to be one of the four children of this tile. If non-child tile coordinates are
  353. * given, the availability of the southeast child tile is returned.
  354. *
  355. * @param {Number} thisX The tile X coordinate of this (the parent) tile.
  356. * @param {Number} thisY The tile Y coordinate of this (the parent) tile.
  357. * @param {Number} childX The tile X coordinate of the child tile to check for availability.
  358. * @param {Number} childY The tile Y coordinate of the child tile to check for availability.
  359. * @returns {Boolean} True if the child tile is available; otherwise, false.
  360. */
  361. GoogleEarthEnterpriseTerrainData.prototype.isChildAvailable = function (
  362. thisX,
  363. thisY,
  364. childX,
  365. childY
  366. ) {
  367. //>>includeStart('debug', pragmas.debug);
  368. Check.typeOf.number("thisX", thisX);
  369. Check.typeOf.number("thisY", thisY);
  370. Check.typeOf.number("childX", childX);
  371. Check.typeOf.number("childY", childY);
  372. //>>includeEnd('debug');
  373. let bitNumber = 2; // northwest child
  374. if (childX !== thisX * 2) {
  375. ++bitNumber; // east child
  376. }
  377. if (childY !== thisY * 2) {
  378. bitNumber -= 2; // south child
  379. }
  380. return (this._childTileMask & (1 << bitNumber)) !== 0;
  381. };
  382. /**
  383. * Gets a value indicating whether or not this terrain data was created by upsampling lower resolution
  384. * terrain data. If this value is false, the data was obtained from some other source, such
  385. * as by downloading it from a remote server. This method should return true for instances
  386. * returned from a call to {@link HeightmapTerrainData#upsample}.
  387. *
  388. * @returns {Boolean} True if this instance was created by upsampling; otherwise, false.
  389. */
  390. GoogleEarthEnterpriseTerrainData.prototype.wasCreatedByUpsampling = function () {
  391. return this._createdByUpsampling;
  392. };
  393. const texCoordScratch0 = new Cartesian2();
  394. const texCoordScratch1 = new Cartesian2();
  395. const texCoordScratch2 = new Cartesian2();
  396. const barycentricCoordinateScratch = new Cartesian3();
  397. function interpolateMeshHeight(terrainData, u, v) {
  398. const mesh = terrainData._mesh;
  399. const vertices = mesh.vertices;
  400. const encoding = mesh.encoding;
  401. const indices = mesh.indices;
  402. for (let i = 0, len = indices.length; i < len; i += 3) {
  403. const i0 = indices[i];
  404. const i1 = indices[i + 1];
  405. const i2 = indices[i + 2];
  406. const uv0 = encoding.decodeTextureCoordinates(
  407. vertices,
  408. i0,
  409. texCoordScratch0
  410. );
  411. const uv1 = encoding.decodeTextureCoordinates(
  412. vertices,
  413. i1,
  414. texCoordScratch1
  415. );
  416. const uv2 = encoding.decodeTextureCoordinates(
  417. vertices,
  418. i2,
  419. texCoordScratch2
  420. );
  421. const barycentric = Intersections2D.computeBarycentricCoordinates(
  422. u,
  423. v,
  424. uv0.x,
  425. uv0.y,
  426. uv1.x,
  427. uv1.y,
  428. uv2.x,
  429. uv2.y,
  430. barycentricCoordinateScratch
  431. );
  432. if (
  433. barycentric.x >= -1e-15 &&
  434. barycentric.y >= -1e-15 &&
  435. barycentric.z >= -1e-15
  436. ) {
  437. const h0 = encoding.decodeHeight(vertices, i0);
  438. const h1 = encoding.decodeHeight(vertices, i1);
  439. const h2 = encoding.decodeHeight(vertices, i2);
  440. return barycentric.x * h0 + barycentric.y * h1 + barycentric.z * h2;
  441. }
  442. }
  443. // Position does not lie in any triangle in this mesh.
  444. return undefined;
  445. }
  446. const sizeOfUint16 = Uint16Array.BYTES_PER_ELEMENT;
  447. const sizeOfUint32 = Uint32Array.BYTES_PER_ELEMENT;
  448. const sizeOfInt32 = Int32Array.BYTES_PER_ELEMENT;
  449. const sizeOfFloat = Float32Array.BYTES_PER_ELEMENT;
  450. const sizeOfDouble = Float64Array.BYTES_PER_ELEMENT;
  451. function interpolateHeight(terrainData, u, v, rectangle) {
  452. const buffer = terrainData._buffer;
  453. let quad = 0; // SW
  454. let uStart = 0.0;
  455. let vStart = 0.0;
  456. if (v > 0.5) {
  457. // Upper row
  458. if (u > 0.5) {
  459. // NE
  460. quad = 2;
  461. uStart = 0.5;
  462. } else {
  463. // NW
  464. quad = 3;
  465. }
  466. vStart = 0.5;
  467. } else if (u > 0.5) {
  468. // SE
  469. quad = 1;
  470. uStart = 0.5;
  471. }
  472. const dv = new DataView(buffer);
  473. let offset = 0;
  474. for (let q = 0; q < quad; ++q) {
  475. offset += dv.getUint32(offset, true);
  476. offset += sizeOfUint32;
  477. }
  478. offset += sizeOfUint32; // Skip length of quad
  479. offset += 2 * sizeOfDouble; // Skip origin
  480. // Read sizes
  481. const xSize = CesiumMath.toRadians(dv.getFloat64(offset, true) * 180.0);
  482. offset += sizeOfDouble;
  483. const ySize = CesiumMath.toRadians(dv.getFloat64(offset, true) * 180.0);
  484. offset += sizeOfDouble;
  485. // Samples per quad
  486. const xScale = rectangle.width / xSize / 2;
  487. const yScale = rectangle.height / ySize / 2;
  488. // Number of points
  489. const numPoints = dv.getInt32(offset, true);
  490. offset += sizeOfInt32;
  491. // Number of faces
  492. const numIndices = dv.getInt32(offset, true) * 3;
  493. offset += sizeOfInt32;
  494. offset += sizeOfInt32; // Skip Level
  495. const uBuffer = new Array(numPoints);
  496. const vBuffer = new Array(numPoints);
  497. const heights = new Array(numPoints);
  498. let i;
  499. for (i = 0; i < numPoints; ++i) {
  500. uBuffer[i] = uStart + dv.getUint8(offset++) * xScale;
  501. vBuffer[i] = vStart + dv.getUint8(offset++) * yScale;
  502. // Height is stored in units of (1/EarthRadius) or (1/6371010.0)
  503. heights[i] = dv.getFloat32(offset, true) * 6371010.0;
  504. offset += sizeOfFloat;
  505. }
  506. const indices = new Array(numIndices);
  507. for (i = 0; i < numIndices; ++i) {
  508. indices[i] = dv.getUint16(offset, true);
  509. offset += sizeOfUint16;
  510. }
  511. for (i = 0; i < numIndices; i += 3) {
  512. const i0 = indices[i];
  513. const i1 = indices[i + 1];
  514. const i2 = indices[i + 2];
  515. const u0 = uBuffer[i0];
  516. const u1 = uBuffer[i1];
  517. const u2 = uBuffer[i2];
  518. const v0 = vBuffer[i0];
  519. const v1 = vBuffer[i1];
  520. const v2 = vBuffer[i2];
  521. const barycentric = Intersections2D.computeBarycentricCoordinates(
  522. u,
  523. v,
  524. u0,
  525. v0,
  526. u1,
  527. v1,
  528. u2,
  529. v2,
  530. barycentricCoordinateScratch
  531. );
  532. if (
  533. barycentric.x >= -1e-15 &&
  534. barycentric.y >= -1e-15 &&
  535. barycentric.z >= -1e-15
  536. ) {
  537. return (
  538. barycentric.x * heights[i0] +
  539. barycentric.y * heights[i1] +
  540. barycentric.z * heights[i2]
  541. );
  542. }
  543. }
  544. // Position does not lie in any triangle in this mesh.
  545. return undefined;
  546. }
  547. export default GoogleEarthEnterpriseTerrainData;