createBoxOutlineGeometry.js 13 KB

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