BoxOutlineGeometry.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import arrayFill from "./arrayFill.js";
  2. import BoundingSphere from "./BoundingSphere.js";
  3. import Cartesian3 from "./Cartesian3.js";
  4. import Check from "./Check.js";
  5. import ComponentDatatype from "./ComponentDatatype.js";
  6. import defaultValue from "./defaultValue.js";
  7. import defined from "./defined.js";
  8. import DeveloperError from "./DeveloperError.js";
  9. import Geometry from "./Geometry.js";
  10. import GeometryAttribute from "./GeometryAttribute.js";
  11. import GeometryAttributes from "./GeometryAttributes.js";
  12. import GeometryOffsetAttribute from "./GeometryOffsetAttribute.js";
  13. import PrimitiveType from "./PrimitiveType.js";
  14. const diffScratch = new Cartesian3();
  15. /**
  16. * A description of the outline of a cube centered at the origin.
  17. *
  18. * @alias BoxOutlineGeometry
  19. * @constructor
  20. *
  21. * @param {Object} options Object with the following properties:
  22. * @param {Cartesian3} options.minimum The minimum x, y, and z coordinates of the box.
  23. * @param {Cartesian3} options.maximum The maximum x, y, and z coordinates of the box.
  24. *
  25. * @see BoxOutlineGeometry.fromDimensions
  26. * @see BoxOutlineGeometry.createGeometry
  27. * @see Packable
  28. *
  29. * @example
  30. * const box = new Cesium.BoxOutlineGeometry({
  31. * maximum : new Cesium.Cartesian3(250000.0, 250000.0, 250000.0),
  32. * minimum : new Cesium.Cartesian3(-250000.0, -250000.0, -250000.0)
  33. * });
  34. * const geometry = Cesium.BoxOutlineGeometry.createGeometry(box);
  35. */
  36. function BoxOutlineGeometry(options) {
  37. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  38. const min = options.minimum;
  39. const max = options.maximum;
  40. //>>includeStart('debug', pragmas.debug);
  41. Check.typeOf.object("min", min);
  42. Check.typeOf.object("max", max);
  43. if (
  44. defined(options.offsetAttribute) &&
  45. options.offsetAttribute === GeometryOffsetAttribute.TOP
  46. ) {
  47. throw new DeveloperError(
  48. "GeometryOffsetAttribute.TOP is not a supported options.offsetAttribute for this geometry."
  49. );
  50. }
  51. //>>includeEnd('debug');
  52. this._min = Cartesian3.clone(min);
  53. this._max = Cartesian3.clone(max);
  54. this._offsetAttribute = options.offsetAttribute;
  55. this._workerName = "createBoxOutlineGeometry";
  56. }
  57. /**
  58. * Creates an outline of a cube centered at the origin given its dimensions.
  59. *
  60. * @param {Object} options Object with the following properties:
  61. * @param {Cartesian3} options.dimensions The width, depth, and height of the box stored in the x, y, and z coordinates of the <code>Cartesian3</code>, respectively.
  62. * @returns {BoxOutlineGeometry}
  63. *
  64. * @exception {DeveloperError} All dimensions components must be greater than or equal to zero.
  65. *
  66. *
  67. * @example
  68. * const box = Cesium.BoxOutlineGeometry.fromDimensions({
  69. * dimensions : new Cesium.Cartesian3(500000.0, 500000.0, 500000.0)
  70. * });
  71. * const geometry = Cesium.BoxOutlineGeometry.createGeometry(box);
  72. *
  73. * @see BoxOutlineGeometry.createGeometry
  74. */
  75. BoxOutlineGeometry.fromDimensions = function (options) {
  76. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  77. const dimensions = options.dimensions;
  78. //>>includeStart('debug', pragmas.debug);
  79. Check.typeOf.object("dimensions", dimensions);
  80. Check.typeOf.number.greaterThanOrEquals("dimensions.x", dimensions.x, 0);
  81. Check.typeOf.number.greaterThanOrEquals("dimensions.y", dimensions.y, 0);
  82. Check.typeOf.number.greaterThanOrEquals("dimensions.z", dimensions.z, 0);
  83. //>>includeEnd('debug');
  84. const corner = Cartesian3.multiplyByScalar(dimensions, 0.5, new Cartesian3());
  85. return new BoxOutlineGeometry({
  86. minimum: Cartesian3.negate(corner, new Cartesian3()),
  87. maximum: corner,
  88. offsetAttribute: options.offsetAttribute,
  89. });
  90. };
  91. /**
  92. * Creates an outline of a cube from the dimensions of an AxisAlignedBoundingBox.
  93. *
  94. * @param {AxisAlignedBoundingBox} boundingBox A description of the AxisAlignedBoundingBox.
  95. * @returns {BoxOutlineGeometry}
  96. *
  97. *
  98. *
  99. * @example
  100. * const aabb = Cesium.AxisAlignedBoundingBox.fromPoints(Cesium.Cartesian3.fromDegreesArray([
  101. * -72.0, 40.0,
  102. * -70.0, 35.0,
  103. * -75.0, 30.0,
  104. * -70.0, 30.0,
  105. * -68.0, 40.0
  106. * ]));
  107. * const box = Cesium.BoxOutlineGeometry.fromAxisAlignedBoundingBox(aabb);
  108. *
  109. * @see BoxOutlineGeometry.createGeometry
  110. */
  111. BoxOutlineGeometry.fromAxisAlignedBoundingBox = function (boundingBox) {
  112. //>>includeStart('debug', pragmas.debug);
  113. Check.typeOf.object("boundindBox", boundingBox);
  114. //>>includeEnd('debug');
  115. return new BoxOutlineGeometry({
  116. minimum: boundingBox.minimum,
  117. maximum: boundingBox.maximum,
  118. });
  119. };
  120. /**
  121. * The number of elements used to pack the object into an array.
  122. * @type {Number}
  123. */
  124. BoxOutlineGeometry.packedLength = 2 * Cartesian3.packedLength + 1;
  125. /**
  126. * Stores the provided instance into the provided array.
  127. *
  128. * @param {BoxOutlineGeometry} value The value to pack.
  129. * @param {Number[]} array The array to pack into.
  130. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  131. *
  132. * @returns {Number[]} The array that was packed into
  133. */
  134. BoxOutlineGeometry.pack = function (value, array, startingIndex) {
  135. //>>includeStart('debug', pragmas.debug);
  136. Check.typeOf.object("value", value);
  137. Check.defined("array", array);
  138. //>>includeEnd('debug');
  139. startingIndex = defaultValue(startingIndex, 0);
  140. Cartesian3.pack(value._min, array, startingIndex);
  141. Cartesian3.pack(value._max, array, startingIndex + Cartesian3.packedLength);
  142. array[startingIndex + Cartesian3.packedLength * 2] = defaultValue(
  143. value._offsetAttribute,
  144. -1
  145. );
  146. return array;
  147. };
  148. const scratchMin = new Cartesian3();
  149. const scratchMax = new Cartesian3();
  150. const scratchOptions = {
  151. minimum: scratchMin,
  152. maximum: scratchMax,
  153. offsetAttribute: undefined,
  154. };
  155. /**
  156. * Retrieves an instance from a packed array.
  157. *
  158. * @param {Number[]} array The packed array.
  159. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  160. * @param {BoxOutlineGeometry} [result] The object into which to store the result.
  161. * @returns {BoxOutlineGeometry} The modified result parameter or a new BoxOutlineGeometry instance if one was not provided.
  162. */
  163. BoxOutlineGeometry.unpack = function (array, startingIndex, result) {
  164. //>>includeStart('debug', pragmas.debug);
  165. Check.defined("array", array);
  166. //>>includeEnd('debug');
  167. startingIndex = defaultValue(startingIndex, 0);
  168. const min = Cartesian3.unpack(array, startingIndex, scratchMin);
  169. const max = Cartesian3.unpack(
  170. array,
  171. startingIndex + Cartesian3.packedLength,
  172. scratchMax
  173. );
  174. const offsetAttribute = array[startingIndex + Cartesian3.packedLength * 2];
  175. if (!defined(result)) {
  176. scratchOptions.offsetAttribute =
  177. offsetAttribute === -1 ? undefined : offsetAttribute;
  178. return new BoxOutlineGeometry(scratchOptions);
  179. }
  180. result._min = Cartesian3.clone(min, result._min);
  181. result._max = Cartesian3.clone(max, result._max);
  182. result._offsetAttribute =
  183. offsetAttribute === -1 ? undefined : offsetAttribute;
  184. return result;
  185. };
  186. /**
  187. * Computes the geometric representation of an outline of a box, including its vertices, indices, and a bounding sphere.
  188. *
  189. * @param {BoxOutlineGeometry} boxGeometry A description of the box outline.
  190. * @returns {Geometry|undefined} The computed vertices and indices.
  191. */
  192. BoxOutlineGeometry.createGeometry = function (boxGeometry) {
  193. const min = boxGeometry._min;
  194. const max = boxGeometry._max;
  195. if (Cartesian3.equals(min, max)) {
  196. return;
  197. }
  198. const attributes = new GeometryAttributes();
  199. const indices = new Uint16Array(12 * 2);
  200. const positions = new Float64Array(8 * 3);
  201. positions[0] = min.x;
  202. positions[1] = min.y;
  203. positions[2] = min.z;
  204. positions[3] = max.x;
  205. positions[4] = min.y;
  206. positions[5] = min.z;
  207. positions[6] = max.x;
  208. positions[7] = max.y;
  209. positions[8] = min.z;
  210. positions[9] = min.x;
  211. positions[10] = max.y;
  212. positions[11] = min.z;
  213. positions[12] = min.x;
  214. positions[13] = min.y;
  215. positions[14] = max.z;
  216. positions[15] = max.x;
  217. positions[16] = min.y;
  218. positions[17] = max.z;
  219. positions[18] = max.x;
  220. positions[19] = max.y;
  221. positions[20] = max.z;
  222. positions[21] = min.x;
  223. positions[22] = max.y;
  224. positions[23] = max.z;
  225. attributes.position = new GeometryAttribute({
  226. componentDatatype: ComponentDatatype.DOUBLE,
  227. componentsPerAttribute: 3,
  228. values: positions,
  229. });
  230. // top
  231. indices[0] = 4;
  232. indices[1] = 5;
  233. indices[2] = 5;
  234. indices[3] = 6;
  235. indices[4] = 6;
  236. indices[5] = 7;
  237. indices[6] = 7;
  238. indices[7] = 4;
  239. // bottom
  240. indices[8] = 0;
  241. indices[9] = 1;
  242. indices[10] = 1;
  243. indices[11] = 2;
  244. indices[12] = 2;
  245. indices[13] = 3;
  246. indices[14] = 3;
  247. indices[15] = 0;
  248. // left
  249. indices[16] = 0;
  250. indices[17] = 4;
  251. indices[18] = 1;
  252. indices[19] = 5;
  253. //right
  254. indices[20] = 2;
  255. indices[21] = 6;
  256. indices[22] = 3;
  257. indices[23] = 7;
  258. const diff = Cartesian3.subtract(max, min, diffScratch);
  259. const radius = Cartesian3.magnitude(diff) * 0.5;
  260. if (defined(boxGeometry._offsetAttribute)) {
  261. const length = positions.length;
  262. const applyOffset = new Uint8Array(length / 3);
  263. const offsetValue =
  264. boxGeometry._offsetAttribute === GeometryOffsetAttribute.NONE ? 0 : 1;
  265. arrayFill(applyOffset, offsetValue);
  266. attributes.applyOffset = new GeometryAttribute({
  267. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  268. componentsPerAttribute: 1,
  269. values: applyOffset,
  270. });
  271. }
  272. return new Geometry({
  273. attributes: attributes,
  274. indices: indices,
  275. primitiveType: PrimitiveType.LINES,
  276. boundingSphere: new BoundingSphere(Cartesian3.ZERO, radius),
  277. offsetAttribute: boxGeometry._offsetAttribute,
  278. });
  279. };
  280. export default BoxOutlineGeometry;