BoxOutlineGeometry.js 9.3 KB

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