BoxOutlineGeometry.js 11 KB

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