EncodedCartesian3-57415c8a.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. define(['exports', './Matrix3-41c58dde', './Check-6ede7e26', './defaultValue-fe22d8c0'], (function (exports, Matrix3, Check, defaultValue) { 'use strict';
  2. /**
  3. * A fixed-point encoding of a {@link Cartesian3} with 64-bit floating-point components, as two {@link Cartesian3}
  4. * values that, when converted to 32-bit floating-point and added, approximate the original input.
  5. * <p>
  6. * This is used to encode positions in vertex buffers for rendering without jittering artifacts
  7. * as described in {@link http://help.agi.com/AGIComponents/html/BlogPrecisionsPrecisions.htm|Precisions, Precisions}.
  8. * </p>
  9. *
  10. * @alias EncodedCartesian3
  11. * @constructor
  12. *
  13. * @private
  14. */
  15. function EncodedCartesian3() {
  16. /**
  17. * The high bits for each component. Bits 0 to 22 store the whole value. Bits 23 to 31 are not used.
  18. *
  19. * @type {Cartesian3}
  20. * @default {@link Cartesian3.ZERO}
  21. */
  22. this.high = Matrix3.Cartesian3.clone(Matrix3.Cartesian3.ZERO);
  23. /**
  24. * 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.
  25. *
  26. * @type {Cartesian3}
  27. * @default {@link Cartesian3.ZERO}
  28. */
  29. this.low = Matrix3.Cartesian3.clone(Matrix3.Cartesian3.ZERO);
  30. }
  31. /**
  32. * Encodes a 64-bit floating-point value as two floating-point values that, when converted to
  33. * 32-bit floating-point and added, approximate the original input. The returned object
  34. * has <code>high</code> and <code>low</code> properties for the high and low bits, respectively.
  35. * <p>
  36. * The fixed-point encoding follows {@link http://help.agi.com/AGIComponents/html/BlogPrecisionsPrecisions.htm|Precisions, Precisions}.
  37. * </p>
  38. *
  39. * @param {number} value The floating-point value to encode.
  40. * @param {object} [result] The object onto which to store the result.
  41. * @returns {object} The modified result parameter or a new instance if one was not provided.
  42. *
  43. * @example
  44. * const value = 1234567.1234567;
  45. * const splitValue = Cesium.EncodedCartesian3.encode(value);
  46. */
  47. EncodedCartesian3.encode = function (value, result) {
  48. //>>includeStart('debug', pragmas.debug);
  49. Check.Check.typeOf.number("value", value);
  50. //>>includeEnd('debug');
  51. if (!defaultValue.defined(result)) {
  52. result = {
  53. high: 0.0,
  54. low: 0.0,
  55. };
  56. }
  57. let doubleHigh;
  58. if (value >= 0.0) {
  59. doubleHigh = Math.floor(value / 65536.0) * 65536.0;
  60. result.high = doubleHigh;
  61. result.low = value - doubleHigh;
  62. } else {
  63. doubleHigh = Math.floor(-value / 65536.0) * 65536.0;
  64. result.high = -doubleHigh;
  65. result.low = value + doubleHigh;
  66. }
  67. return result;
  68. };
  69. const scratchEncode = {
  70. high: 0.0,
  71. low: 0.0,
  72. };
  73. /**
  74. * Encodes a {@link Cartesian3} with 64-bit floating-point components as two {@link Cartesian3}
  75. * values that, when converted to 32-bit floating-point and added, approximate the original input.
  76. * <p>
  77. * The fixed-point encoding follows {@link https://help.agi.com/AGIComponents/html/BlogPrecisionsPrecisions.htm|Precisions, Precisions}.
  78. * </p>
  79. *
  80. * @param {Cartesian3} cartesian The cartesian to encode.
  81. * @param {EncodedCartesian3} [result] The object onto which to store the result.
  82. * @returns {EncodedCartesian3} The modified result parameter or a new EncodedCartesian3 instance if one was not provided.
  83. *
  84. * @example
  85. * const cart = new Cesium.Cartesian3(-10000000.0, 0.0, 10000000.0);
  86. * const encoded = Cesium.EncodedCartesian3.fromCartesian(cart);
  87. */
  88. EncodedCartesian3.fromCartesian = function (cartesian, result) {
  89. //>>includeStart('debug', pragmas.debug);
  90. Check.Check.typeOf.object("cartesian", cartesian);
  91. //>>includeEnd('debug');
  92. if (!defaultValue.defined(result)) {
  93. result = new EncodedCartesian3();
  94. }
  95. const high = result.high;
  96. const low = result.low;
  97. EncodedCartesian3.encode(cartesian.x, scratchEncode);
  98. high.x = scratchEncode.high;
  99. low.x = scratchEncode.low;
  100. EncodedCartesian3.encode(cartesian.y, scratchEncode);
  101. high.y = scratchEncode.high;
  102. low.y = scratchEncode.low;
  103. EncodedCartesian3.encode(cartesian.z, scratchEncode);
  104. high.z = scratchEncode.high;
  105. low.z = scratchEncode.low;
  106. return result;
  107. };
  108. const encodedP = new EncodedCartesian3();
  109. /**
  110. * Encodes the provided <code>cartesian</code>, and writes it to an array with <code>high</code>
  111. * components followed by <code>low</code> components, i.e. <code>[high.x, high.y, high.z, low.x, low.y, low.z]</code>.
  112. * <p>
  113. * This is used to create interleaved high-precision position vertex attributes.
  114. * </p>
  115. *
  116. * @param {Cartesian3} cartesian The cartesian to encode.
  117. * @param {number[]} cartesianArray The array to write to.
  118. * @param {number} index The index into the array to start writing. Six elements will be written.
  119. *
  120. * @exception {DeveloperError} index must be a number greater than or equal to 0.
  121. *
  122. * @example
  123. * const positions = [
  124. * new Cesium.Cartesian3(),
  125. * // ...
  126. * ];
  127. * const encodedPositions = new Float32Array(2 * 3 * positions.length);
  128. * let j = 0;
  129. * for (let i = 0; i < positions.length; ++i) {
  130. * Cesium.EncodedCartesian3.writeElement(positions[i], encodedPositions, j);
  131. * j += 6;
  132. * }
  133. */
  134. EncodedCartesian3.writeElements = function (cartesian, cartesianArray, index) {
  135. //>>includeStart('debug', pragmas.debug);
  136. Check.Check.defined("cartesianArray", cartesianArray);
  137. Check.Check.typeOf.number("index", index);
  138. Check.Check.typeOf.number.greaterThanOrEquals("index", index, 0);
  139. //>>includeEnd('debug');
  140. EncodedCartesian3.fromCartesian(cartesian, encodedP);
  141. const high = encodedP.high;
  142. const low = encodedP.low;
  143. cartesianArray[index] = high.x;
  144. cartesianArray[index + 1] = high.y;
  145. cartesianArray[index + 2] = high.z;
  146. cartesianArray[index + 3] = low.x;
  147. cartesianArray[index + 4] = low.y;
  148. cartesianArray[index + 5] = low.z;
  149. };
  150. exports.EncodedCartesian3 = EncodedCartesian3;
  151. }));