createCylinderOutlineGeometry.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./GeometryOffsetAttribute-3e8c299c', './Transforms-323408fe', './Matrix2-69c32d33', './RuntimeError-c581ca93', './ComponentDatatype-b1ea011a', './CylinderGeometryLibrary-1ace08dc', './defaultValue-94c3e563', './GeometryAttribute-cb73bb3f', './GeometryAttributes-7df9bef6', './IndexDatatype-c4099fe9', './_commonjsHelpers-3aae1032-f55dc0c4', './combine-761d9c3f', './WebGLConstants-7dccdc96'], (function (GeometryOffsetAttribute, Transforms, Matrix2, RuntimeError, ComponentDatatype, CylinderGeometryLibrary, defaultValue, GeometryAttribute, GeometryAttributes, IndexDatatype, _commonjsHelpers3aae1032, combine, WebGLConstants) { 'use strict';
  3. const radiusScratch = new Matrix2.Cartesian2();
  4. /**
  5. * A description of the outline of a cylinder.
  6. *
  7. * @alias CylinderOutlineGeometry
  8. * @constructor
  9. *
  10. * @param {Object} options Object with the following properties:
  11. * @param {Number} options.length The length of the cylinder.
  12. * @param {Number} options.topRadius The radius of the top of the cylinder.
  13. * @param {Number} options.bottomRadius The radius of the bottom of the cylinder.
  14. * @param {Number} [options.slices=128] The number of edges around the perimeter of the cylinder.
  15. * @param {Number} [options.numberOfVerticalLines=16] Number of lines to draw between the top and bottom surfaces of the cylinder.
  16. *
  17. * @exception {DeveloperError} options.length must be greater than 0.
  18. * @exception {DeveloperError} options.topRadius must be greater than 0.
  19. * @exception {DeveloperError} options.bottomRadius must be greater than 0.
  20. * @exception {DeveloperError} bottomRadius and topRadius cannot both equal 0.
  21. * @exception {DeveloperError} options.slices must be greater than or equal to 3.
  22. *
  23. * @see CylinderOutlineGeometry.createGeometry
  24. *
  25. * @example
  26. * // create cylinder geometry
  27. * const cylinder = new Cesium.CylinderOutlineGeometry({
  28. * length: 200000,
  29. * topRadius: 80000,
  30. * bottomRadius: 200000,
  31. * });
  32. * const geometry = Cesium.CylinderOutlineGeometry.createGeometry(cylinder);
  33. */
  34. function CylinderOutlineGeometry(options) {
  35. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  36. const length = options.length;
  37. const topRadius = options.topRadius;
  38. const bottomRadius = options.bottomRadius;
  39. const slices = defaultValue.defaultValue(options.slices, 128);
  40. const numberOfVerticalLines = Math.max(
  41. defaultValue.defaultValue(options.numberOfVerticalLines, 16),
  42. 0
  43. );
  44. //>>includeStart('debug', pragmas.debug);
  45. RuntimeError.Check.typeOf.number("options.positions", length);
  46. RuntimeError.Check.typeOf.number("options.topRadius", topRadius);
  47. RuntimeError.Check.typeOf.number("options.bottomRadius", bottomRadius);
  48. RuntimeError.Check.typeOf.number.greaterThanOrEquals("options.slices", slices, 3);
  49. if (
  50. defaultValue.defined(options.offsetAttribute) &&
  51. options.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP
  52. ) {
  53. throw new RuntimeError.DeveloperError(
  54. "GeometryOffsetAttribute.TOP is not a supported options.offsetAttribute for this geometry."
  55. );
  56. }
  57. //>>includeEnd('debug');
  58. this._length = length;
  59. this._topRadius = topRadius;
  60. this._bottomRadius = bottomRadius;
  61. this._slices = slices;
  62. this._numberOfVerticalLines = numberOfVerticalLines;
  63. this._offsetAttribute = options.offsetAttribute;
  64. this._workerName = "createCylinderOutlineGeometry";
  65. }
  66. /**
  67. * The number of elements used to pack the object into an array.
  68. * @type {Number}
  69. */
  70. CylinderOutlineGeometry.packedLength = 6;
  71. /**
  72. * Stores the provided instance into the provided array.
  73. *
  74. * @param {CylinderOutlineGeometry} value The value to pack.
  75. * @param {Number[]} array The array to pack into.
  76. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  77. *
  78. * @returns {Number[]} The array that was packed into
  79. */
  80. CylinderOutlineGeometry.pack = function (value, array, startingIndex) {
  81. //>>includeStart('debug', pragmas.debug);
  82. RuntimeError.Check.typeOf.object("value", value);
  83. RuntimeError.Check.defined("array", array);
  84. //>>includeEnd('debug');
  85. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  86. array[startingIndex++] = value._length;
  87. array[startingIndex++] = value._topRadius;
  88. array[startingIndex++] = value._bottomRadius;
  89. array[startingIndex++] = value._slices;
  90. array[startingIndex++] = value._numberOfVerticalLines;
  91. array[startingIndex] = defaultValue.defaultValue(value._offsetAttribute, -1);
  92. return array;
  93. };
  94. const scratchOptions = {
  95. length: undefined,
  96. topRadius: undefined,
  97. bottomRadius: undefined,
  98. slices: undefined,
  99. numberOfVerticalLines: undefined,
  100. offsetAttribute: undefined,
  101. };
  102. /**
  103. * Retrieves an instance from a packed array.
  104. *
  105. * @param {Number[]} array The packed array.
  106. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  107. * @param {CylinderOutlineGeometry} [result] The object into which to store the result.
  108. * @returns {CylinderOutlineGeometry} The modified result parameter or a new CylinderOutlineGeometry instance if one was not provided.
  109. */
  110. CylinderOutlineGeometry.unpack = function (array, startingIndex, result) {
  111. //>>includeStart('debug', pragmas.debug);
  112. RuntimeError.Check.defined("array", array);
  113. //>>includeEnd('debug');
  114. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  115. const length = array[startingIndex++];
  116. const topRadius = array[startingIndex++];
  117. const bottomRadius = array[startingIndex++];
  118. const slices = array[startingIndex++];
  119. const numberOfVerticalLines = array[startingIndex++];
  120. const offsetAttribute = array[startingIndex];
  121. if (!defaultValue.defined(result)) {
  122. scratchOptions.length = length;
  123. scratchOptions.topRadius = topRadius;
  124. scratchOptions.bottomRadius = bottomRadius;
  125. scratchOptions.slices = slices;
  126. scratchOptions.numberOfVerticalLines = numberOfVerticalLines;
  127. scratchOptions.offsetAttribute =
  128. offsetAttribute === -1 ? undefined : offsetAttribute;
  129. return new CylinderOutlineGeometry(scratchOptions);
  130. }
  131. result._length = length;
  132. result._topRadius = topRadius;
  133. result._bottomRadius = bottomRadius;
  134. result._slices = slices;
  135. result._numberOfVerticalLines = numberOfVerticalLines;
  136. result._offsetAttribute =
  137. offsetAttribute === -1 ? undefined : offsetAttribute;
  138. return result;
  139. };
  140. /**
  141. * Computes the geometric representation of an outline of a cylinder, including its vertices, indices, and a bounding sphere.
  142. *
  143. * @param {CylinderOutlineGeometry} cylinderGeometry A description of the cylinder outline.
  144. * @returns {Geometry|undefined} The computed vertices and indices.
  145. */
  146. CylinderOutlineGeometry.createGeometry = function (cylinderGeometry) {
  147. let length = cylinderGeometry._length;
  148. const topRadius = cylinderGeometry._topRadius;
  149. const bottomRadius = cylinderGeometry._bottomRadius;
  150. const slices = cylinderGeometry._slices;
  151. const numberOfVerticalLines = cylinderGeometry._numberOfVerticalLines;
  152. if (
  153. length <= 0 ||
  154. topRadius < 0 ||
  155. bottomRadius < 0 ||
  156. (topRadius === 0 && bottomRadius === 0)
  157. ) {
  158. return;
  159. }
  160. const numVertices = slices * 2;
  161. const positions = CylinderGeometryLibrary.CylinderGeometryLibrary.computePositions(
  162. length,
  163. topRadius,
  164. bottomRadius,
  165. slices,
  166. false
  167. );
  168. let numIndices = slices * 2;
  169. let numSide;
  170. if (numberOfVerticalLines > 0) {
  171. const numSideLines = Math.min(numberOfVerticalLines, slices);
  172. numSide = Math.round(slices / numSideLines);
  173. numIndices += numSideLines;
  174. }
  175. const indices = IndexDatatype.IndexDatatype.createTypedArray(numVertices, numIndices * 2);
  176. let index = 0;
  177. let i;
  178. for (i = 0; i < slices - 1; i++) {
  179. indices[index++] = i;
  180. indices[index++] = i + 1;
  181. indices[index++] = i + slices;
  182. indices[index++] = i + 1 + slices;
  183. }
  184. indices[index++] = slices - 1;
  185. indices[index++] = 0;
  186. indices[index++] = slices + slices - 1;
  187. indices[index++] = slices;
  188. if (numberOfVerticalLines > 0) {
  189. for (i = 0; i < slices; i += numSide) {
  190. indices[index++] = i;
  191. indices[index++] = i + slices;
  192. }
  193. }
  194. const attributes = new GeometryAttributes.GeometryAttributes();
  195. attributes.position = new GeometryAttribute.GeometryAttribute({
  196. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  197. componentsPerAttribute: 3,
  198. values: positions,
  199. });
  200. radiusScratch.x = length * 0.5;
  201. radiusScratch.y = Math.max(bottomRadius, topRadius);
  202. const boundingSphere = new Transforms.BoundingSphere(
  203. Matrix2.Cartesian3.ZERO,
  204. Matrix2.Cartesian2.magnitude(radiusScratch)
  205. );
  206. if (defaultValue.defined(cylinderGeometry._offsetAttribute)) {
  207. length = positions.length;
  208. const applyOffset = new Uint8Array(length / 3);
  209. const offsetValue =
  210. cylinderGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE
  211. ? 0
  212. : 1;
  213. GeometryOffsetAttribute.arrayFill(applyOffset, offsetValue);
  214. attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  215. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  216. componentsPerAttribute: 1,
  217. values: applyOffset,
  218. });
  219. }
  220. return new GeometryAttribute.Geometry({
  221. attributes: attributes,
  222. indices: indices,
  223. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  224. boundingSphere: boundingSphere,
  225. offsetAttribute: cylinderGeometry._offsetAttribute,
  226. });
  227. };
  228. function createCylinderOutlineGeometry(cylinderGeometry, offset) {
  229. if (defaultValue.defined(offset)) {
  230. cylinderGeometry = CylinderOutlineGeometry.unpack(cylinderGeometry, offset);
  231. }
  232. return CylinderOutlineGeometry.createGeometry(cylinderGeometry);
  233. }
  234. return createCylinderOutlineGeometry;
  235. }));