HeightmapTerrainData.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. import when from '../ThirdParty/when.js';
  2. import BoundingSphere from './BoundingSphere.js';
  3. import Cartesian3 from './Cartesian3.js';
  4. import defaultValue from './defaultValue.js';
  5. import defined from './defined.js';
  6. import defineProperties from './defineProperties.js';
  7. import DeveloperError from './DeveloperError.js';
  8. import GeographicProjection from './GeographicProjection.js';
  9. import HeightmapEncoding from './HeightmapEncoding.js';
  10. import HeightmapTessellator from './HeightmapTessellator.js';
  11. import CesiumMath from './Math.js';
  12. import OrientedBoundingBox from './OrientedBoundingBox.js';
  13. import Rectangle from './Rectangle.js';
  14. import TaskProcessor from './TaskProcessor.js';
  15. import TerrainEncoding from './TerrainEncoding.js';
  16. import TerrainMesh from './TerrainMesh.js';
  17. import TerrainProvider from './TerrainProvider.js';
  18. /**
  19. * Terrain data for a single tile where the terrain data is represented as a heightmap. A heightmap
  20. * is a rectangular array of heights in row-major order from north to south and west to east.
  21. *
  22. * @alias HeightmapTerrainData
  23. * @constructor
  24. *
  25. * @param {Object} options Object with the following properties:
  26. * @param {TypedArray} options.buffer The buffer containing height data.
  27. * @param {Number} options.width The width (longitude direction) of the heightmap, in samples.
  28. * @param {Number} options.height The height (latitude direction) of the heightmap, in samples.
  29. * @param {Number} [options.childTileMask=15] A bit mask indicating which of this tile's four children exist.
  30. * If a child's bit is set, geometry will be requested for that tile as well when it
  31. * is needed. If the bit is cleared, the child tile is not requested and geometry is
  32. * instead upsampled from the parent. The bit values are as follows:
  33. * <table>
  34. * <tr><th>Bit Position</th><th>Bit Value</th><th>Child Tile</th></tr>
  35. * <tr><td>0</td><td>1</td><td>Southwest</td></tr>
  36. * <tr><td>1</td><td>2</td><td>Southeast</td></tr>
  37. * <tr><td>2</td><td>4</td><td>Northwest</td></tr>
  38. * <tr><td>3</td><td>8</td><td>Northeast</td></tr>
  39. * </table>
  40. * @param {Uint8Array} [options.waterMask] The water mask included in this terrain data, if any. A water mask is a square
  41. * Uint8Array or image where a value of 255 indicates water and a value of 0 indicates land.
  42. * Values in between 0 and 255 are allowed as well to smoothly blend between land and water.
  43. * @param {Object} [options.structure] An object describing the structure of the height data.
  44. * @param {Number} [options.structure.heightScale=1.0] The factor by which to multiply height samples in order to obtain
  45. * the height above the heightOffset, in meters. The heightOffset is added to the resulting
  46. * height after multiplying by the scale.
  47. * @param {Number} [options.structure.heightOffset=0.0] The offset to add to the scaled height to obtain the final
  48. * height in meters. The offset is added after the height sample is multiplied by the
  49. * heightScale.
  50. * @param {Number} [options.structure.elementsPerHeight=1] The number of elements in the buffer that make up a single height
  51. * sample. This is usually 1, indicating that each element is a separate height sample. If
  52. * it is greater than 1, that number of elements together form the height sample, which is
  53. * computed according to the structure.elementMultiplier and structure.isBigEndian properties.
  54. * @param {Number} [options.structure.stride=1] The number of elements to skip to get from the first element of
  55. * one height to the first element of the next height.
  56. * @param {Number} [options.structure.elementMultiplier=256.0] The multiplier used to compute the height value when the
  57. * stride property is greater than 1. For example, if the stride is 4 and the strideMultiplier
  58. * is 256, the height is computed as follows:
  59. * `height = buffer[index] + buffer[index + 1] * 256 + buffer[index + 2] * 256 * 256 + buffer[index + 3] * 256 * 256 * 256`
  60. * This is assuming that the isBigEndian property is false. If it is true, the order of the
  61. * elements is reversed.
  62. * @param {Boolean} [options.structure.isBigEndian=false] Indicates endianness of the elements in the buffer when the
  63. * stride property is greater than 1. If this property is false, the first element is the
  64. * low-order element. If it is true, the first element is the high-order element.
  65. * @param {Number} [options.structure.lowestEncodedHeight] The lowest value that can be stored in the height buffer. Any heights that are lower
  66. * than this value after encoding with the `heightScale` and `heightOffset` are clamped to this value. For example, if the height
  67. * buffer is a `Uint16Array`, this value should be 0 because a `Uint16Array` cannot store negative numbers. If this parameter is
  68. * not specified, no minimum value is enforced.
  69. * @param {Number} [options.structure.highestEncodedHeight] The highest value that can be stored in the height buffer. Any heights that are higher
  70. * than this value after encoding with the `heightScale` and `heightOffset` are clamped to this value. For example, if the height
  71. * buffer is a `Uint16Array`, this value should be `256 * 256 - 1` or 65535 because a `Uint16Array` cannot store numbers larger
  72. * than 65535. If this parameter is not specified, no maximum value is enforced.
  73. * @param {HeightmapEncoding} [options.encoding=HeightmapEncoding.NONE] The encoding that is used on the buffer.
  74. * @param {Boolean} [options.createdByUpsampling=false] True if this instance was created by upsampling another instance;
  75. * otherwise, false.
  76. *
  77. *
  78. * @example
  79. * var buffer = ...
  80. * var heightBuffer = new Uint16Array(buffer, 0, that._heightmapWidth * that._heightmapWidth);
  81. * var childTileMask = new Uint8Array(buffer, heightBuffer.byteLength, 1)[0];
  82. * var waterMask = new Uint8Array(buffer, heightBuffer.byteLength + 1, buffer.byteLength - heightBuffer.byteLength - 1);
  83. * var terrainData = new Cesium.HeightmapTerrainData({
  84. * buffer : heightBuffer,
  85. * width : 65,
  86. * height : 65,
  87. * childTileMask : childTileMask,
  88. * waterMask : waterMask
  89. * });
  90. *
  91. * @see TerrainData
  92. * @see QuantizedMeshTerrainData
  93. */
  94. function HeightmapTerrainData(options) {
  95. //>>includeStart('debug', pragmas.debug);
  96. if (!defined(options) || !defined(options.buffer)) {
  97. throw new DeveloperError('options.buffer is required.');
  98. }
  99. if (!defined(options.width)) {
  100. throw new DeveloperError('options.width is required.');
  101. }
  102. if (!defined(options.height)) {
  103. throw new DeveloperError('options.height is required.');
  104. }
  105. //>>includeEnd('debug');
  106. this._buffer = options.buffer;
  107. this._width = options.width;
  108. this._height = options.height;
  109. this._childTileMask = defaultValue(options.childTileMask, 15);
  110. this._encoding = defaultValue(options.encoding, HeightmapEncoding.NONE);
  111. var defaultStructure = HeightmapTessellator.DEFAULT_STRUCTURE;
  112. var structure = options.structure;
  113. if (!defined(structure)) {
  114. structure = defaultStructure;
  115. } else if (structure !== defaultStructure) {
  116. structure.heightScale = defaultValue(structure.heightScale, defaultStructure.heightScale);
  117. structure.heightOffset = defaultValue(structure.heightOffset, defaultStructure.heightOffset);
  118. structure.elementsPerHeight = defaultValue(structure.elementsPerHeight, defaultStructure.elementsPerHeight);
  119. structure.stride = defaultValue(structure.stride, defaultStructure.stride);
  120. structure.elementMultiplier = defaultValue(structure.elementMultiplier, defaultStructure.elementMultiplier);
  121. structure.isBigEndian = defaultValue(structure.isBigEndian, defaultStructure.isBigEndian);
  122. }
  123. this._structure = structure;
  124. this._createdByUpsampling = defaultValue(options.createdByUpsampling, false);
  125. this._waterMask = options.waterMask;
  126. this._skirtHeight = undefined;
  127. this._bufferType = (this._encoding === HeightmapEncoding.LERC) ? Float32Array : this._buffer.constructor;
  128. this._mesh = undefined;
  129. }
  130. defineProperties(HeightmapTerrainData.prototype, {
  131. /**
  132. * An array of credits for this tile.
  133. * @memberof HeightmapTerrainData.prototype
  134. * @type {Credit[]}
  135. */
  136. credits : {
  137. get : function() {
  138. return undefined;
  139. }
  140. },
  141. /**
  142. * The water mask included in this terrain data, if any. A water mask is a square
  143. * Uint8Array or image where a value of 255 indicates water and a value of 0 indicates land.
  144. * Values in between 0 and 255 are allowed as well to smoothly blend between land and water.
  145. * @memberof HeightmapTerrainData.prototype
  146. * @type {Uint8Array|Image|Canvas}
  147. */
  148. waterMask : {
  149. get : function() {
  150. return this._waterMask;
  151. }
  152. },
  153. childTileMask : {
  154. get : function() {
  155. return this._childTileMask;
  156. }
  157. }
  158. });
  159. var taskProcessor = new TaskProcessor('createVerticesFromHeightmap');
  160. /**
  161. * Creates a {@link TerrainMesh} from this terrain data.
  162. *
  163. * @private
  164. *
  165. * @param {TilingScheme} tilingScheme The tiling scheme to which this tile belongs.
  166. * @param {Number} x The X coordinate of the tile for which to create the terrain data.
  167. * @param {Number} y The Y coordinate of the tile for which to create the terrain data.
  168. * @param {Number} level The level of the tile for which to create the terrain data.
  169. * @param {Number} [exaggeration=1.0] The scale used to exaggerate the terrain.
  170. * @returns {Promise.<TerrainMesh>|undefined} A promise for the terrain mesh, or undefined if too many
  171. * asynchronous mesh creations are already in progress and the operation should
  172. * be retried later.
  173. */
  174. HeightmapTerrainData.prototype.createMesh = function(tilingScheme, x, y, level, exaggeration) {
  175. //>>includeStart('debug', pragmas.debug);
  176. if (!defined(tilingScheme)) {
  177. throw new DeveloperError('tilingScheme is required.');
  178. }
  179. if (!defined(x)) {
  180. throw new DeveloperError('x is required.');
  181. }
  182. if (!defined(y)) {
  183. throw new DeveloperError('y is required.');
  184. }
  185. if (!defined(level)) {
  186. throw new DeveloperError('level is required.');
  187. }
  188. //>>includeEnd('debug');
  189. var ellipsoid = tilingScheme.ellipsoid;
  190. var nativeRectangle = tilingScheme.tileXYToNativeRectangle(x, y, level);
  191. var rectangle = tilingScheme.tileXYToRectangle(x, y, level);
  192. exaggeration = defaultValue(exaggeration, 1.0);
  193. // Compute the center of the tile for RTC rendering.
  194. var center = ellipsoid.cartographicToCartesian(Rectangle.center(rectangle));
  195. var structure = this._structure;
  196. var levelZeroMaxError = TerrainProvider.getEstimatedLevelZeroGeometricErrorForAHeightmap(ellipsoid, this._width, tilingScheme.getNumberOfXTilesAtLevel(0));
  197. var thisLevelMaxError = levelZeroMaxError / (1 << level);
  198. this._skirtHeight = Math.min(thisLevelMaxError * 4.0, 1000.0);
  199. var verticesPromise = taskProcessor.scheduleTask({
  200. heightmap : this._buffer,
  201. structure : structure,
  202. includeWebMercatorT : true,
  203. width : this._width,
  204. height : this._height,
  205. nativeRectangle : nativeRectangle,
  206. rectangle : rectangle,
  207. relativeToCenter : center,
  208. ellipsoid : ellipsoid,
  209. skirtHeight : this._skirtHeight,
  210. isGeographic : tilingScheme.projection instanceof GeographicProjection,
  211. exaggeration : exaggeration,
  212. encoding : this._encoding
  213. });
  214. if (!defined(verticesPromise)) {
  215. // Postponed
  216. return undefined;
  217. }
  218. var that = this;
  219. return when(verticesPromise, function(result) {
  220. // Clone complex result objects because the transfer from the web worker
  221. // has stripped them down to JSON-style objects.
  222. that._mesh = new TerrainMesh(
  223. center,
  224. new Float32Array(result.vertices),
  225. TerrainProvider.getRegularGridIndices(result.gridWidth, result.gridHeight),
  226. result.minimumHeight,
  227. result.maximumHeight,
  228. BoundingSphere.clone(result.boundingSphere3D),
  229. Cartesian3.clone(result.occludeePointInScaledSpace),
  230. result.numberOfAttributes,
  231. OrientedBoundingBox.clone(result.orientedBoundingBox),
  232. TerrainEncoding.clone(result.encoding),
  233. exaggeration,
  234. result.westIndicesSouthToNorth,
  235. result.southIndicesEastToWest,
  236. result.eastIndicesNorthToSouth,
  237. result.northIndicesWestToEast);
  238. // Free memory received from server after mesh is created.
  239. that._buffer = undefined;
  240. return that._mesh;
  241. });
  242. };
  243. /**
  244. * @private
  245. */
  246. HeightmapTerrainData.prototype._createMeshSync = function(tilingScheme, x, y, level, exaggeration) {
  247. //>>includeStart('debug', pragmas.debug);
  248. if (!defined(tilingScheme)) {
  249. throw new DeveloperError('tilingScheme is required.');
  250. }
  251. if (!defined(x)) {
  252. throw new DeveloperError('x is required.');
  253. }
  254. if (!defined(y)) {
  255. throw new DeveloperError('y is required.');
  256. }
  257. if (!defined(level)) {
  258. throw new DeveloperError('level is required.');
  259. }
  260. //>>includeEnd('debug');
  261. var ellipsoid = tilingScheme.ellipsoid;
  262. var nativeRectangle = tilingScheme.tileXYToNativeRectangle(x, y, level);
  263. var rectangle = tilingScheme.tileXYToRectangle(x, y, level);
  264. exaggeration = defaultValue(exaggeration, 1.0);
  265. // Compute the center of the tile for RTC rendering.
  266. var center = ellipsoid.cartographicToCartesian(Rectangle.center(rectangle));
  267. var structure = this._structure;
  268. var levelZeroMaxError = TerrainProvider.getEstimatedLevelZeroGeometricErrorForAHeightmap(ellipsoid, this._width, tilingScheme.getNumberOfXTilesAtLevel(0));
  269. var thisLevelMaxError = levelZeroMaxError / (1 << level);
  270. this._skirtHeight = Math.min(thisLevelMaxError * 4.0, 1000.0);
  271. var result = HeightmapTessellator.computeVertices({
  272. heightmap : this._buffer,
  273. structure : structure,
  274. includeWebMercatorT : true,
  275. width : this._width,
  276. height : this._height,
  277. nativeRectangle : nativeRectangle,
  278. rectangle : rectangle,
  279. relativeToCenter : center,
  280. ellipsoid : ellipsoid,
  281. skirtHeight : this._skirtHeight,
  282. isGeographic : tilingScheme.projection instanceof GeographicProjection,
  283. exaggeration : exaggeration
  284. });
  285. // Free memory received from server after mesh is created.
  286. this._buffer = undefined;
  287. var arrayWidth = this._width;
  288. var arrayHeight = this._height;
  289. if (this._skirtHeight > 0.0) {
  290. arrayWidth += 2;
  291. arrayHeight += 2;
  292. }
  293. // No need to clone here (as we do in the async version) because the result
  294. // is not coming from a web worker.
  295. return new TerrainMesh(
  296. center,
  297. result.vertices,
  298. TerrainProvider.getRegularGridIndices(arrayWidth, arrayHeight),
  299. result.minimumHeight,
  300. result.maximumHeight,
  301. result.boundingSphere3D,
  302. result.occludeePointInScaledSpace,
  303. result.encoding.getStride(),
  304. result.orientedBoundingBox,
  305. result.encoding,
  306. exaggeration,
  307. result.westIndicesSouthToNorth,
  308. result.southIndicesEastToWest,
  309. result.eastIndicesNorthToSouth,
  310. result.northIndicesWestToEast);
  311. };
  312. /**
  313. * Computes the terrain height at a specified longitude and latitude.
  314. *
  315. * @param {Rectangle} rectangle The rectangle covered by this terrain data.
  316. * @param {Number} longitude The longitude in radians.
  317. * @param {Number} latitude The latitude in radians.
  318. * @returns {Number} The terrain height at the specified position. If the position
  319. * is outside the rectangle, this method will extrapolate the height, which is likely to be wildly
  320. * incorrect for positions far outside the rectangle.
  321. */
  322. HeightmapTerrainData.prototype.interpolateHeight = function(rectangle, longitude, latitude) {
  323. var width = this._width;
  324. var height = this._height;
  325. var structure = this._structure;
  326. var stride = structure.stride;
  327. var elementsPerHeight = structure.elementsPerHeight;
  328. var elementMultiplier = structure.elementMultiplier;
  329. var isBigEndian = structure.isBigEndian;
  330. var heightOffset = structure.heightOffset;
  331. var heightScale = structure.heightScale;
  332. var heightSample;
  333. if (defined(this._mesh)) {
  334. var buffer = this._mesh.vertices;
  335. var encoding = this._mesh.encoding;
  336. var skirtHeight = this._skirtHeight;
  337. var exaggeration = this._mesh.exaggeration;
  338. heightSample = interpolateMeshHeight(buffer, encoding, heightOffset, heightScale, skirtHeight, rectangle, width, height, longitude, latitude, exaggeration);
  339. } else {
  340. heightSample = interpolateHeight(this._buffer, elementsPerHeight, elementMultiplier, stride, isBigEndian, rectangle, width, height, longitude, latitude);
  341. heightSample = heightSample * heightScale + heightOffset;
  342. }
  343. return heightSample;
  344. };
  345. /**
  346. * Upsamples this terrain data for use by a descendant tile. The resulting instance will contain a subset of the
  347. * height samples in this instance, interpolated if necessary.
  348. *
  349. * @param {TilingScheme} tilingScheme The tiling scheme of this terrain data.
  350. * @param {Number} thisX The X coordinate of this tile in the tiling scheme.
  351. * @param {Number} thisY The Y coordinate of this tile in the tiling scheme.
  352. * @param {Number} thisLevel The level of this tile in the tiling scheme.
  353. * @param {Number} descendantX The X coordinate within the tiling scheme of the descendant tile for which we are upsampling.
  354. * @param {Number} descendantY The Y coordinate within the tiling scheme of the descendant tile for which we are upsampling.
  355. * @param {Number} descendantLevel The level within the tiling scheme of the descendant tile for which we are upsampling.
  356. * @returns {Promise.<HeightmapTerrainData>|undefined} A promise for upsampled heightmap terrain data for the descendant tile,
  357. * or undefined if too many asynchronous upsample operations are in progress and the request has been
  358. * deferred.
  359. */
  360. HeightmapTerrainData.prototype.upsample = function(tilingScheme, thisX, thisY, thisLevel, descendantX, descendantY, descendantLevel) {
  361. //>>includeStart('debug', pragmas.debug);
  362. if (!defined(tilingScheme)) {
  363. throw new DeveloperError('tilingScheme is required.');
  364. }
  365. if (!defined(thisX)) {
  366. throw new DeveloperError('thisX is required.');
  367. }
  368. if (!defined(thisY)) {
  369. throw new DeveloperError('thisY is required.');
  370. }
  371. if (!defined(thisLevel)) {
  372. throw new DeveloperError('thisLevel is required.');
  373. }
  374. if (!defined(descendantX)) {
  375. throw new DeveloperError('descendantX is required.');
  376. }
  377. if (!defined(descendantY)) {
  378. throw new DeveloperError('descendantY is required.');
  379. }
  380. if (!defined(descendantLevel)) {
  381. throw new DeveloperError('descendantLevel is required.');
  382. }
  383. var levelDifference = descendantLevel - thisLevel;
  384. if (levelDifference > 1) {
  385. throw new DeveloperError('Upsampling through more than one level at a time is not currently supported.');
  386. }
  387. //>>includeEnd('debug');
  388. var meshData = this._mesh;
  389. if (!defined(meshData)) {
  390. return undefined;
  391. }
  392. var width = this._width;
  393. var height = this._height;
  394. var structure = this._structure;
  395. var skirtHeight = this._skirtHeight;
  396. var stride = structure.stride;
  397. var heights = new this._bufferType(width * height * stride);
  398. var buffer = meshData.vertices;
  399. var encoding = meshData.encoding;
  400. // PERFORMANCE_IDEA: don't recompute these rectangles - the caller already knows them.
  401. var sourceRectangle = tilingScheme.tileXYToRectangle(thisX, thisY, thisLevel);
  402. var destinationRectangle = tilingScheme.tileXYToRectangle(descendantX, descendantY, descendantLevel);
  403. var heightOffset = structure.heightOffset;
  404. var heightScale = structure.heightScale;
  405. var exaggeration = meshData.exaggeration;
  406. var elementsPerHeight = structure.elementsPerHeight;
  407. var elementMultiplier = structure.elementMultiplier;
  408. var isBigEndian = structure.isBigEndian;
  409. var divisor = Math.pow(elementMultiplier, elementsPerHeight - 1);
  410. for (var j = 0; j < height; ++j) {
  411. var latitude = CesiumMath.lerp(destinationRectangle.north, destinationRectangle.south, j / (height - 1));
  412. for (var i = 0; i < width; ++i) {
  413. var longitude = CesiumMath.lerp(destinationRectangle.west, destinationRectangle.east, i / (width - 1));
  414. var heightSample = interpolateMeshHeight(buffer, encoding, heightOffset, heightScale, skirtHeight, sourceRectangle, width, height, longitude, latitude, exaggeration);
  415. // Use conditionals here instead of Math.min and Math.max so that an undefined
  416. // lowestEncodedHeight or highestEncodedHeight has no effect.
  417. heightSample = heightSample < structure.lowestEncodedHeight ? structure.lowestEncodedHeight : heightSample;
  418. heightSample = heightSample > structure.highestEncodedHeight ? structure.highestEncodedHeight : heightSample;
  419. setHeight(heights, elementsPerHeight, elementMultiplier, divisor, stride, isBigEndian, j * width + i, heightSample);
  420. }
  421. }
  422. return new HeightmapTerrainData({
  423. buffer : heights,
  424. width : width,
  425. height : height,
  426. childTileMask : 0,
  427. structure : this._structure,
  428. createdByUpsampling : true
  429. });
  430. };
  431. /**
  432. * Determines if a given child tile is available, based on the
  433. * {@link HeightmapTerrainData.childTileMask}. The given child tile coordinates are assumed
  434. * to be one of the four children of this tile. If non-child tile coordinates are
  435. * given, the availability of the southeast child tile is returned.
  436. *
  437. * @param {Number} thisX The tile X coordinate of this (the parent) tile.
  438. * @param {Number} thisY The tile Y coordinate of this (the parent) tile.
  439. * @param {Number} childX The tile X coordinate of the child tile to check for availability.
  440. * @param {Number} childY The tile Y coordinate of the child tile to check for availability.
  441. * @returns {Boolean} True if the child tile is available; otherwise, false.
  442. */
  443. HeightmapTerrainData.prototype.isChildAvailable = function(thisX, thisY, childX, childY) {
  444. //>>includeStart('debug', pragmas.debug);
  445. if (!defined(thisX)) {
  446. throw new DeveloperError('thisX is required.');
  447. }
  448. if (!defined(thisY)) {
  449. throw new DeveloperError('thisY is required.');
  450. }
  451. if (!defined(childX)) {
  452. throw new DeveloperError('childX is required.');
  453. }
  454. if (!defined(childY)) {
  455. throw new DeveloperError('childY is required.');
  456. }
  457. //>>includeEnd('debug');
  458. var bitNumber = 2; // northwest child
  459. if (childX !== thisX * 2) {
  460. ++bitNumber; // east child
  461. }
  462. if (childY !== thisY * 2) {
  463. bitNumber -= 2; // south child
  464. }
  465. return (this._childTileMask & (1 << bitNumber)) !== 0;
  466. };
  467. /**
  468. * Gets a value indicating whether or not this terrain data was created by upsampling lower resolution
  469. * terrain data. If this value is false, the data was obtained from some other source, such
  470. * as by downloading it from a remote server. This method should return true for instances
  471. * returned from a call to {@link HeightmapTerrainData#upsample}.
  472. *
  473. * @returns {Boolean} True if this instance was created by upsampling; otherwise, false.
  474. */
  475. HeightmapTerrainData.prototype.wasCreatedByUpsampling = function() {
  476. return this._createdByUpsampling;
  477. };
  478. function interpolateHeight(sourceHeights, elementsPerHeight, elementMultiplier, stride, isBigEndian, sourceRectangle, width, height, longitude, latitude) {
  479. var fromWest = (longitude - sourceRectangle.west) * (width - 1) / (sourceRectangle.east - sourceRectangle.west);
  480. var fromSouth = (latitude - sourceRectangle.south) * (height - 1) / (sourceRectangle.north - sourceRectangle.south);
  481. var westInteger = fromWest | 0;
  482. var eastInteger = westInteger + 1;
  483. if (eastInteger >= width) {
  484. eastInteger = width - 1;
  485. westInteger = width - 2;
  486. }
  487. var southInteger = fromSouth | 0;
  488. var northInteger = southInteger + 1;
  489. if (northInteger >= height) {
  490. northInteger = height - 1;
  491. southInteger = height - 2;
  492. }
  493. var dx = fromWest - westInteger;
  494. var dy = fromSouth - southInteger;
  495. southInteger = height - 1 - southInteger;
  496. northInteger = height - 1 - northInteger;
  497. var southwestHeight = getHeight(sourceHeights, elementsPerHeight, elementMultiplier, stride, isBigEndian, southInteger * width + westInteger);
  498. var southeastHeight = getHeight(sourceHeights, elementsPerHeight, elementMultiplier, stride, isBigEndian, southInteger * width + eastInteger);
  499. var northwestHeight = getHeight(sourceHeights, elementsPerHeight, elementMultiplier, stride, isBigEndian, northInteger * width + westInteger);
  500. var northeastHeight = getHeight(sourceHeights, elementsPerHeight, elementMultiplier, stride, isBigEndian, northInteger * width + eastInteger);
  501. return triangleInterpolateHeight(dx, dy, southwestHeight, southeastHeight, northwestHeight, northeastHeight);
  502. }
  503. function interpolateMeshHeight(buffer, encoding, heightOffset, heightScale, skirtHeight, sourceRectangle, width, height, longitude, latitude, exaggeration) {
  504. // returns a height encoded according to the structure's heightScale and heightOffset.
  505. var fromWest = (longitude - sourceRectangle.west) * (width - 1) / (sourceRectangle.east - sourceRectangle.west);
  506. var fromSouth = (latitude - sourceRectangle.south) * (height - 1) / (sourceRectangle.north - sourceRectangle.south);
  507. if (skirtHeight > 0) {
  508. fromWest += 1.0;
  509. fromSouth += 1.0;
  510. width += 2;
  511. height += 2;
  512. }
  513. var widthEdge = (skirtHeight > 0) ? width - 1 : width;
  514. var westInteger = fromWest | 0;
  515. var eastInteger = westInteger + 1;
  516. if (eastInteger >= widthEdge) {
  517. eastInteger = width - 1;
  518. westInteger = width - 2;
  519. }
  520. var heightEdge = (skirtHeight > 0) ? height - 1 : height;
  521. var southInteger = fromSouth | 0;
  522. var northInteger = southInteger + 1;
  523. if (northInteger >= heightEdge) {
  524. northInteger = height - 1;
  525. southInteger = height - 2;
  526. }
  527. var dx = fromWest - westInteger;
  528. var dy = fromSouth - southInteger;
  529. southInteger = height - 1 - southInteger;
  530. northInteger = height - 1 - northInteger;
  531. var southwestHeight = (encoding.decodeHeight(buffer, southInteger * width + westInteger) / exaggeration - heightOffset) / heightScale;
  532. var southeastHeight = (encoding.decodeHeight(buffer, southInteger * width + eastInteger) / exaggeration - heightOffset) / heightScale;
  533. var northwestHeight = (encoding.decodeHeight(buffer, northInteger * width + westInteger) / exaggeration - heightOffset) / heightScale;
  534. var northeastHeight = (encoding.decodeHeight(buffer, northInteger * width + eastInteger) / exaggeration - heightOffset) / heightScale;
  535. return triangleInterpolateHeight(dx, dy, southwestHeight, southeastHeight, northwestHeight, northeastHeight);
  536. }
  537. function triangleInterpolateHeight(dX, dY, southwestHeight, southeastHeight, northwestHeight, northeastHeight) {
  538. // The HeightmapTessellator bisects the quad from southwest to northeast.
  539. if (dY < dX) {
  540. // Lower right triangle
  541. return southwestHeight + (dX * (southeastHeight - southwestHeight)) + (dY * (northeastHeight - southeastHeight));
  542. }
  543. // Upper left triangle
  544. return southwestHeight + (dX * (northeastHeight - northwestHeight)) + (dY * (northwestHeight - southwestHeight));
  545. }
  546. function getHeight(heights, elementsPerHeight, elementMultiplier, stride, isBigEndian, index) {
  547. index *= stride;
  548. var height = 0;
  549. var i;
  550. if (isBigEndian) {
  551. for (i = 0; i < elementsPerHeight; ++i) {
  552. height = (height * elementMultiplier) + heights[index + i];
  553. }
  554. } else {
  555. for (i = elementsPerHeight - 1; i >= 0; --i) {
  556. height = (height * elementMultiplier) + heights[index + i];
  557. }
  558. }
  559. return height;
  560. }
  561. function setHeight(heights, elementsPerHeight, elementMultiplier, divisor, stride, isBigEndian, index, height) {
  562. index *= stride;
  563. var i;
  564. if (isBigEndian) {
  565. for (i = 0; i < elementsPerHeight - 1; ++i) {
  566. heights[index + i] = (height / divisor) | 0;
  567. height -= heights[index + i] * divisor;
  568. divisor /= elementMultiplier;
  569. }
  570. } else {
  571. for (i = elementsPerHeight - 1; i > 0; --i) {
  572. heights[index + i] = (height / divisor) | 0;
  573. height -= heights[index + i] * divisor;
  574. divisor /= elementMultiplier;
  575. }
  576. }
  577. heights[index + i] = height;
  578. }
  579. export default HeightmapTerrainData;