createBoxOutlineGeometry.js 11 KB

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