EncodedCartesian3-d3e254ea.js 6.1 KB

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