createBoxOutlineGeometry.js 11 KB

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