createBoxOutlineGeometry.js 13 KB

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