createBoxOutlineGeometry.js 12 KB

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