createCylinderOutlineGeometry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.97
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['./Transforms-c78c4637', './Matrix2-ab676047', './RuntimeError-1088cc64', './ComponentDatatype-e06f4e16', './CylinderGeometryLibrary-4e1a4cee', './defaultValue-a6eb9f34', './GeometryAttribute-4f02e2ad', './GeometryAttributes-aff51037', './GeometryOffsetAttribute-102da468', './IndexDatatype-c2232ebd', './_commonjsHelpers-89c9b271', './combine-7cf28d88', './WebGLConstants-d81b330d'], (function (Transforms, Matrix2, RuntimeError, ComponentDatatype, CylinderGeometryLibrary, defaultValue, GeometryAttribute, GeometryAttributes, GeometryOffsetAttribute, IndexDatatype, _commonjsHelpers, combine, WebGLConstants) { 'use strict';
  26. const radiusScratch = new Matrix2.Cartesian2();
  27. /**
  28. * A description of the outline of a cylinder.
  29. *
  30. * @alias CylinderOutlineGeometry
  31. * @constructor
  32. *
  33. * @param {Object} options Object with the following properties:
  34. * @param {Number} options.length The length of the cylinder.
  35. * @param {Number} options.topRadius The radius of the top of the cylinder.
  36. * @param {Number} options.bottomRadius The radius of the bottom of the cylinder.
  37. * @param {Number} [options.slices=128] The number of edges around the perimeter of the cylinder.
  38. * @param {Number} [options.numberOfVerticalLines=16] Number of lines to draw between the top and bottom surfaces of the cylinder.
  39. *
  40. * @exception {DeveloperError} options.length must be greater than 0.
  41. * @exception {DeveloperError} options.topRadius must be greater than 0.
  42. * @exception {DeveloperError} options.bottomRadius must be greater than 0.
  43. * @exception {DeveloperError} bottomRadius and topRadius cannot both equal 0.
  44. * @exception {DeveloperError} options.slices must be greater than or equal to 3.
  45. *
  46. * @see CylinderOutlineGeometry.createGeometry
  47. *
  48. * @example
  49. * // create cylinder geometry
  50. * const cylinder = new Cesium.CylinderOutlineGeometry({
  51. * length: 200000,
  52. * topRadius: 80000,
  53. * bottomRadius: 200000,
  54. * });
  55. * const geometry = Cesium.CylinderOutlineGeometry.createGeometry(cylinder);
  56. */
  57. function CylinderOutlineGeometry(options) {
  58. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  59. const length = options.length;
  60. const topRadius = options.topRadius;
  61. const bottomRadius = options.bottomRadius;
  62. const slices = defaultValue.defaultValue(options.slices, 128);
  63. const numberOfVerticalLines = Math.max(
  64. defaultValue.defaultValue(options.numberOfVerticalLines, 16),
  65. 0
  66. );
  67. //>>includeStart('debug', pragmas.debug);
  68. RuntimeError.Check.typeOf.number("options.positions", length);
  69. RuntimeError.Check.typeOf.number("options.topRadius", topRadius);
  70. RuntimeError.Check.typeOf.number("options.bottomRadius", bottomRadius);
  71. RuntimeError.Check.typeOf.number.greaterThanOrEquals("options.slices", slices, 3);
  72. if (
  73. defaultValue.defined(options.offsetAttribute) &&
  74. options.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP
  75. ) {
  76. throw new RuntimeError.DeveloperError(
  77. "GeometryOffsetAttribute.TOP is not a supported options.offsetAttribute for this geometry."
  78. );
  79. }
  80. //>>includeEnd('debug');
  81. this._length = length;
  82. this._topRadius = topRadius;
  83. this._bottomRadius = bottomRadius;
  84. this._slices = slices;
  85. this._numberOfVerticalLines = numberOfVerticalLines;
  86. this._offsetAttribute = options.offsetAttribute;
  87. this._workerName = "createCylinderOutlineGeometry";
  88. }
  89. /**
  90. * The number of elements used to pack the object into an array.
  91. * @type {Number}
  92. */
  93. CylinderOutlineGeometry.packedLength = 6;
  94. /**
  95. * Stores the provided instance into the provided array.
  96. *
  97. * @param {CylinderOutlineGeometry} value The value to pack.
  98. * @param {Number[]} array The array to pack into.
  99. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  100. *
  101. * @returns {Number[]} The array that was packed into
  102. */
  103. CylinderOutlineGeometry.pack = function (value, array, startingIndex) {
  104. //>>includeStart('debug', pragmas.debug);
  105. RuntimeError.Check.typeOf.object("value", value);
  106. RuntimeError.Check.defined("array", array);
  107. //>>includeEnd('debug');
  108. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  109. array[startingIndex++] = value._length;
  110. array[startingIndex++] = value._topRadius;
  111. array[startingIndex++] = value._bottomRadius;
  112. array[startingIndex++] = value._slices;
  113. array[startingIndex++] = value._numberOfVerticalLines;
  114. array[startingIndex] = defaultValue.defaultValue(value._offsetAttribute, -1);
  115. return array;
  116. };
  117. const scratchOptions = {
  118. length: undefined,
  119. topRadius: undefined,
  120. bottomRadius: undefined,
  121. slices: undefined,
  122. numberOfVerticalLines: undefined,
  123. offsetAttribute: undefined,
  124. };
  125. /**
  126. * Retrieves an instance from a packed array.
  127. *
  128. * @param {Number[]} array The packed array.
  129. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  130. * @param {CylinderOutlineGeometry} [result] The object into which to store the result.
  131. * @returns {CylinderOutlineGeometry} The modified result parameter or a new CylinderOutlineGeometry instance if one was not provided.
  132. */
  133. CylinderOutlineGeometry.unpack = function (array, startingIndex, result) {
  134. //>>includeStart('debug', pragmas.debug);
  135. RuntimeError.Check.defined("array", array);
  136. //>>includeEnd('debug');
  137. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  138. const length = array[startingIndex++];
  139. const topRadius = array[startingIndex++];
  140. const bottomRadius = array[startingIndex++];
  141. const slices = array[startingIndex++];
  142. const numberOfVerticalLines = array[startingIndex++];
  143. const offsetAttribute = array[startingIndex];
  144. if (!defaultValue.defined(result)) {
  145. scratchOptions.length = length;
  146. scratchOptions.topRadius = topRadius;
  147. scratchOptions.bottomRadius = bottomRadius;
  148. scratchOptions.slices = slices;
  149. scratchOptions.numberOfVerticalLines = numberOfVerticalLines;
  150. scratchOptions.offsetAttribute =
  151. offsetAttribute === -1 ? undefined : offsetAttribute;
  152. return new CylinderOutlineGeometry(scratchOptions);
  153. }
  154. result._length = length;
  155. result._topRadius = topRadius;
  156. result._bottomRadius = bottomRadius;
  157. result._slices = slices;
  158. result._numberOfVerticalLines = numberOfVerticalLines;
  159. result._offsetAttribute =
  160. offsetAttribute === -1 ? undefined : offsetAttribute;
  161. return result;
  162. };
  163. /**
  164. * Computes the geometric representation of an outline of a cylinder, including its vertices, indices, and a bounding sphere.
  165. *
  166. * @param {CylinderOutlineGeometry} cylinderGeometry A description of the cylinder outline.
  167. * @returns {Geometry|undefined} The computed vertices and indices.
  168. */
  169. CylinderOutlineGeometry.createGeometry = function (cylinderGeometry) {
  170. let length = cylinderGeometry._length;
  171. const topRadius = cylinderGeometry._topRadius;
  172. const bottomRadius = cylinderGeometry._bottomRadius;
  173. const slices = cylinderGeometry._slices;
  174. const numberOfVerticalLines = cylinderGeometry._numberOfVerticalLines;
  175. if (
  176. length <= 0 ||
  177. topRadius < 0 ||
  178. bottomRadius < 0 ||
  179. (topRadius === 0 && bottomRadius === 0)
  180. ) {
  181. return;
  182. }
  183. const numVertices = slices * 2;
  184. const positions = CylinderGeometryLibrary.CylinderGeometryLibrary.computePositions(
  185. length,
  186. topRadius,
  187. bottomRadius,
  188. slices,
  189. false
  190. );
  191. let numIndices = slices * 2;
  192. let numSide;
  193. if (numberOfVerticalLines > 0) {
  194. const numSideLines = Math.min(numberOfVerticalLines, slices);
  195. numSide = Math.round(slices / numSideLines);
  196. numIndices += numSideLines;
  197. }
  198. const indices = IndexDatatype.IndexDatatype.createTypedArray(numVertices, numIndices * 2);
  199. let index = 0;
  200. let i;
  201. for (i = 0; i < slices - 1; i++) {
  202. indices[index++] = i;
  203. indices[index++] = i + 1;
  204. indices[index++] = i + slices;
  205. indices[index++] = i + 1 + slices;
  206. }
  207. indices[index++] = slices - 1;
  208. indices[index++] = 0;
  209. indices[index++] = slices + slices - 1;
  210. indices[index++] = slices;
  211. if (numberOfVerticalLines > 0) {
  212. for (i = 0; i < slices; i += numSide) {
  213. indices[index++] = i;
  214. indices[index++] = i + slices;
  215. }
  216. }
  217. const attributes = new GeometryAttributes.GeometryAttributes();
  218. attributes.position = new GeometryAttribute.GeometryAttribute({
  219. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  220. componentsPerAttribute: 3,
  221. values: positions,
  222. });
  223. radiusScratch.x = length * 0.5;
  224. radiusScratch.y = Math.max(bottomRadius, topRadius);
  225. const boundingSphere = new Transforms.BoundingSphere(
  226. Matrix2.Cartesian3.ZERO,
  227. Matrix2.Cartesian2.magnitude(radiusScratch)
  228. );
  229. if (defaultValue.defined(cylinderGeometry._offsetAttribute)) {
  230. length = positions.length;
  231. const offsetValue =
  232. cylinderGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE
  233. ? 0
  234. : 1;
  235. const applyOffset = new Uint8Array(length / 3).fill(offsetValue);
  236. attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  237. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  238. componentsPerAttribute: 1,
  239. values: applyOffset,
  240. });
  241. }
  242. return new GeometryAttribute.Geometry({
  243. attributes: attributes,
  244. indices: indices,
  245. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  246. boundingSphere: boundingSphere,
  247. offsetAttribute: cylinderGeometry._offsetAttribute,
  248. });
  249. };
  250. function createCylinderOutlineGeometry(cylinderGeometry, offset) {
  251. if (defaultValue.defined(offset)) {
  252. cylinderGeometry = CylinderOutlineGeometry.unpack(cylinderGeometry, offset);
  253. }
  254. return CylinderOutlineGeometry.createGeometry(cylinderGeometry);
  255. }
  256. return createCylinderOutlineGeometry;
  257. }));