EncodedCartesian3-491ac596.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './Matrix2-9e1c22e2', './RuntimeError-4f8ec8a2', './defaultValue-97284df2'], (function (exports, Matrix2, RuntimeError, defaultValue) { 'use strict';
  26. /**
  27. * A fixed-point encoding of a {@link Cartesian3} with 64-bit floating-point components, as two {@link Cartesian3}
  28. * values that, when converted to 32-bit floating-point and added, approximate the original input.
  29. * <p>
  30. * This is used to encode positions in vertex buffers for rendering without jittering artifacts
  31. * as described in {@link http://help.agi.com/AGIComponents/html/BlogPrecisionsPrecisions.htm|Precisions, Precisions}.
  32. * </p>
  33. *
  34. * @alias EncodedCartesian3
  35. * @constructor
  36. *
  37. * @private
  38. */
  39. function EncodedCartesian3() {
  40. /**
  41. * The high bits for each component. Bits 0 to 22 store the whole value. Bits 23 to 31 are not used.
  42. *
  43. * @type {Cartesian3}
  44. * @default {@link Cartesian3.ZERO}
  45. */
  46. this.high = Matrix2.Cartesian3.clone(Matrix2.Cartesian3.ZERO);
  47. /**
  48. * The low bits for each component. Bits 7 to 22 store the whole value, and bits 0 to 6 store the fraction. Bits 23 to 31 are not used.
  49. *
  50. * @type {Cartesian3}
  51. * @default {@link Cartesian3.ZERO}
  52. */
  53. this.low = Matrix2.Cartesian3.clone(Matrix2.Cartesian3.ZERO);
  54. }
  55. /**
  56. * Encodes a 64-bit floating-point value as two floating-point values that, when converted to
  57. * 32-bit floating-point and added, approximate the original input. The returned object
  58. * has <code>high</code> and <code>low</code> properties for the high and low bits, respectively.
  59. * <p>
  60. * The fixed-point encoding follows {@link http://help.agi.com/AGIComponents/html/BlogPrecisionsPrecisions.htm|Precisions, Precisions}.
  61. * </p>
  62. *
  63. * @param {Number} value The floating-point value to encode.
  64. * @param {Object} [result] The object onto which to store the result.
  65. * @returns {Object} The modified result parameter or a new instance if one was not provided.
  66. *
  67. * @example
  68. * const value = 1234567.1234567;
  69. * const splitValue = Cesium.EncodedCartesian3.encode(value);
  70. */
  71. EncodedCartesian3.encode = function (value, result) {
  72. //>>includeStart('debug', pragmas.debug);
  73. RuntimeError.Check.typeOf.number("value", value);
  74. //>>includeEnd('debug');
  75. if (!defaultValue.defined(result)) {
  76. result = {
  77. high: 0.0,
  78. low: 0.0,
  79. };
  80. }
  81. let doubleHigh;
  82. if (value >= 0.0) {
  83. doubleHigh = Math.floor(value / 65536.0) * 65536.0;
  84. result.high = doubleHigh;
  85. result.low = value - doubleHigh;
  86. } else {
  87. doubleHigh = Math.floor(-value / 65536.0) * 65536.0;
  88. result.high = -doubleHigh;
  89. result.low = value + doubleHigh;
  90. }
  91. return result;
  92. };
  93. const scratchEncode = {
  94. high: 0.0,
  95. low: 0.0,
  96. };
  97. /**
  98. * Encodes a {@link Cartesian3} with 64-bit floating-point components as two {@link Cartesian3}
  99. * values that, when converted to 32-bit floating-point and added, approximate the original input.
  100. * <p>
  101. * The fixed-point encoding follows {@link https://help.agi.com/AGIComponents/html/BlogPrecisionsPrecisions.htm|Precisions, Precisions}.
  102. * </p>
  103. *
  104. * @param {Cartesian3} cartesian The cartesian to encode.
  105. * @param {EncodedCartesian3} [result] The object onto which to store the result.
  106. * @returns {EncodedCartesian3} The modified result parameter or a new EncodedCartesian3 instance if one was not provided.
  107. *
  108. * @example
  109. * const cart = new Cesium.Cartesian3(-10000000.0, 0.0, 10000000.0);
  110. * const encoded = Cesium.EncodedCartesian3.fromCartesian(cart);
  111. */
  112. EncodedCartesian3.fromCartesian = function (cartesian, result) {
  113. //>>includeStart('debug', pragmas.debug);
  114. RuntimeError.Check.typeOf.object("cartesian", cartesian);
  115. //>>includeEnd('debug');
  116. if (!defaultValue.defined(result)) {
  117. result = new EncodedCartesian3();
  118. }
  119. const high = result.high;
  120. const low = result.low;
  121. EncodedCartesian3.encode(cartesian.x, scratchEncode);
  122. high.x = scratchEncode.high;
  123. low.x = scratchEncode.low;
  124. EncodedCartesian3.encode(cartesian.y, scratchEncode);
  125. high.y = scratchEncode.high;
  126. low.y = scratchEncode.low;
  127. EncodedCartesian3.encode(cartesian.z, scratchEncode);
  128. high.z = scratchEncode.high;
  129. low.z = scratchEncode.low;
  130. return result;
  131. };
  132. const encodedP = new EncodedCartesian3();
  133. /**
  134. * Encodes the provided <code>cartesian</code>, and writes it to an array with <code>high</code>
  135. * components followed by <code>low</code> components, i.e. <code>[high.x, high.y, high.z, low.x, low.y, low.z]</code>.
  136. * <p>
  137. * This is used to create interleaved high-precision position vertex attributes.
  138. * </p>
  139. *
  140. * @param {Cartesian3} cartesian The cartesian to encode.
  141. * @param {Number[]} cartesianArray The array to write to.
  142. * @param {Number} index The index into the array to start writing. Six elements will be written.
  143. *
  144. * @exception {DeveloperError} index must be a number greater than or equal to 0.
  145. *
  146. * @example
  147. * const positions = [
  148. * new Cesium.Cartesian3(),
  149. * // ...
  150. * ];
  151. * const encodedPositions = new Float32Array(2 * 3 * positions.length);
  152. * let j = 0;
  153. * for (let i = 0; i < positions.length; ++i) {
  154. * Cesium.EncodedCartesian3.writeElement(positions[i], encodedPositions, j);
  155. * j += 6;
  156. * }
  157. */
  158. EncodedCartesian3.writeElements = function (cartesian, cartesianArray, index) {
  159. //>>includeStart('debug', pragmas.debug);
  160. RuntimeError.Check.defined("cartesianArray", cartesianArray);
  161. RuntimeError.Check.typeOf.number("index", index);
  162. RuntimeError.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  163. //>>includeEnd('debug');
  164. EncodedCartesian3.fromCartesian(cartesian, encodedP);
  165. const high = encodedP.high;
  166. const low = encodedP.low;
  167. cartesianArray[index] = high.x;
  168. cartesianArray[index + 1] = high.y;
  169. cartesianArray[index + 2] = high.z;
  170. cartesianArray[index + 3] = low.x;
  171. cartesianArray[index + 4] = low.y;
  172. cartesianArray[index + 5] = low.z;
  173. };
  174. exports.EncodedCartesian3 = EncodedCartesian3;
  175. }));
  176. //# sourceMappingURL=EncodedCartesian3-491ac596.js.map