createBoxOutlineGeometry.js 12 KB

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