createCylinderOutlineGeometry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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', './IndexDatatype-3de60176', './GeometryOffsetAttribute-cb30cd97', './CylinderGeometryLibrary-1a22ba0e'], function (defined, Check, freezeObject, defaultValue, _Math, Cartesian2, defineProperties, Transforms, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, when, GeometryAttributes, IndexDatatype, GeometryOffsetAttribute, CylinderGeometryLibrary) { 'use strict';
  3. var radiusScratch = new Cartesian2.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. * var cylinder = new Cesium.CylinderOutlineGeometry({
  28. * length: 200000,
  29. * topRadius: 80000,
  30. * bottomRadius: 200000,
  31. * });
  32. * var geometry = Cesium.CylinderOutlineGeometry.createGeometry(cylinder);
  33. */
  34. function CylinderOutlineGeometry(options) {
  35. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  36. var length = options.length;
  37. var topRadius = options.topRadius;
  38. var bottomRadius = options.bottomRadius;
  39. var slices = defaultValue.defaultValue(options.slices, 128);
  40. var numberOfVerticalLines = Math.max(defaultValue.defaultValue(options.numberOfVerticalLines, 16), 0);
  41. //>>includeStart('debug', pragmas.debug);
  42. Check.Check.typeOf.number('options.positions', length);
  43. Check.Check.typeOf.number('options.topRadius', topRadius);
  44. Check.Check.typeOf.number('options.bottomRadius', bottomRadius);
  45. Check.Check.typeOf.number.greaterThanOrEquals('options.slices', slices, 3);
  46. if (defined.defined(options.offsetAttribute) && options.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP) {
  47. throw new Check.DeveloperError('GeometryOffsetAttribute.TOP is not a supported options.offsetAttribute for this geometry.');
  48. }
  49. //>>includeEnd('debug');
  50. this._length = length;
  51. this._topRadius = topRadius;
  52. this._bottomRadius = bottomRadius;
  53. this._slices = slices;
  54. this._numberOfVerticalLines = numberOfVerticalLines;
  55. this._offsetAttribute = options.offsetAttribute;
  56. this._workerName = 'createCylinderOutlineGeometry';
  57. }
  58. /**
  59. * The number of elements used to pack the object into an array.
  60. * @type {Number}
  61. */
  62. CylinderOutlineGeometry.packedLength = 6;
  63. /**
  64. * Stores the provided instance into the provided array.
  65. *
  66. * @param {CylinderOutlineGeometry} value The value to pack.
  67. * @param {Number[]} array The array to pack into.
  68. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  69. *
  70. * @returns {Number[]} The array that was packed into
  71. */
  72. CylinderOutlineGeometry.pack = function(value, array, startingIndex) {
  73. //>>includeStart('debug', pragmas.debug);
  74. Check.Check.typeOf.object('value', value);
  75. Check.Check.defined('array', array);
  76. //>>includeEnd('debug');
  77. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  78. array[startingIndex++] = value._length;
  79. array[startingIndex++] = value._topRadius;
  80. array[startingIndex++] = value._bottomRadius;
  81. array[startingIndex++] = value._slices;
  82. array[startingIndex++] = value._numberOfVerticalLines;
  83. array[startingIndex] = defaultValue.defaultValue(value._offsetAttribute, -1);
  84. return array;
  85. };
  86. var scratchOptions = {
  87. length : undefined,
  88. topRadius : undefined,
  89. bottomRadius : undefined,
  90. slices : undefined,
  91. numberOfVerticalLines : undefined,
  92. offsetAttribute : undefined
  93. };
  94. /**
  95. * Retrieves an instance from a packed array.
  96. *
  97. * @param {Number[]} array The packed array.
  98. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  99. * @param {CylinderOutlineGeometry} [result] The object into which to store the result.
  100. * @returns {CylinderOutlineGeometry} The modified result parameter or a new CylinderOutlineGeometry instance if one was not provided.
  101. */
  102. CylinderOutlineGeometry.unpack = function(array, startingIndex, result) {
  103. //>>includeStart('debug', pragmas.debug);
  104. Check.Check.defined('array', array);
  105. //>>includeEnd('debug');
  106. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  107. var length = array[startingIndex++];
  108. var topRadius = array[startingIndex++];
  109. var bottomRadius = array[startingIndex++];
  110. var slices = array[startingIndex++];
  111. var numberOfVerticalLines = array[startingIndex++];
  112. var offsetAttribute = array[startingIndex];
  113. if (!defined.defined(result)) {
  114. scratchOptions.length = length;
  115. scratchOptions.topRadius = topRadius;
  116. scratchOptions.bottomRadius = bottomRadius;
  117. scratchOptions.slices = slices;
  118. scratchOptions.numberOfVerticalLines = numberOfVerticalLines;
  119. scratchOptions.offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  120. return new CylinderOutlineGeometry(scratchOptions);
  121. }
  122. result._length = length;
  123. result._topRadius = topRadius;
  124. result._bottomRadius = bottomRadius;
  125. result._slices = slices;
  126. result._numberOfVerticalLines = numberOfVerticalLines;
  127. result._offsetAttribute = offsetAttribute === -1 ? undefined : offsetAttribute;
  128. return result;
  129. };
  130. /**
  131. * Computes the geometric representation of an outline of a cylinder, including its vertices, indices, and a bounding sphere.
  132. *
  133. * @param {CylinderOutlineGeometry} cylinderGeometry A description of the cylinder outline.
  134. * @returns {Geometry|undefined} The computed vertices and indices.
  135. */
  136. CylinderOutlineGeometry.createGeometry = function(cylinderGeometry) {
  137. var length = cylinderGeometry._length;
  138. var topRadius = cylinderGeometry._topRadius;
  139. var bottomRadius = cylinderGeometry._bottomRadius;
  140. var slices = cylinderGeometry._slices;
  141. var numberOfVerticalLines = cylinderGeometry._numberOfVerticalLines;
  142. if ((length <= 0) || (topRadius < 0) || (bottomRadius < 0) || ((topRadius === 0) && (bottomRadius === 0))) {
  143. return;
  144. }
  145. var numVertices = slices * 2;
  146. var positions = CylinderGeometryLibrary.CylinderGeometryLibrary.computePositions(length, topRadius, bottomRadius, slices, false);
  147. var numIndices = slices * 2;
  148. var numSide;
  149. if (numberOfVerticalLines > 0) {
  150. var numSideLines = Math.min(numberOfVerticalLines, slices);
  151. numSide = Math.round(slices / numSideLines);
  152. numIndices += numSideLines;
  153. }
  154. var indices = IndexDatatype.IndexDatatype.createTypedArray(numVertices, numIndices * 2);
  155. var index = 0;
  156. var i;
  157. for (i = 0; i < slices - 1; i++) {
  158. indices[index++] = i;
  159. indices[index++] = i + 1;
  160. indices[index++] = i + slices;
  161. indices[index++] = i + 1 + slices;
  162. }
  163. indices[index++] = slices - 1;
  164. indices[index++] = 0;
  165. indices[index++] = slices + slices - 1;
  166. indices[index++] = slices;
  167. if (numberOfVerticalLines > 0) {
  168. for (i = 0; i < slices; i += numSide) {
  169. indices[index++] = i;
  170. indices[index++] = i + slices;
  171. }
  172. }
  173. var attributes = new GeometryAttributes.GeometryAttributes();
  174. attributes.position = new GeometryAttribute.GeometryAttribute({
  175. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  176. componentsPerAttribute : 3,
  177. values : positions
  178. });
  179. radiusScratch.x = length * 0.5;
  180. radiusScratch.y = Math.max(bottomRadius, topRadius);
  181. var boundingSphere = new Transforms.BoundingSphere(Cartesian2.Cartesian3.ZERO, Cartesian2.Cartesian2.magnitude(radiusScratch));
  182. if (defined.defined(cylinderGeometry._offsetAttribute)) {
  183. length = positions.length;
  184. var applyOffset = new Uint8Array(length / 3);
  185. var offsetValue = cylinderGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE ? 0 : 1;
  186. GeometryOffsetAttribute.arrayFill(applyOffset, offsetValue);
  187. attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  188. componentDatatype : ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  189. componentsPerAttribute : 1,
  190. values: applyOffset
  191. });
  192. }
  193. return new GeometryAttribute.Geometry({
  194. attributes : attributes,
  195. indices : indices,
  196. primitiveType : GeometryAttribute.PrimitiveType.LINES,
  197. boundingSphere : boundingSphere,
  198. offsetAttribute : cylinderGeometry._offsetAttribute
  199. });
  200. };
  201. function createCylinderOutlineGeometry(cylinderGeometry, offset) {
  202. if (defined.defined(offset)) {
  203. cylinderGeometry = CylinderOutlineGeometry.unpack(cylinderGeometry, offset);
  204. }
  205. return CylinderOutlineGeometry.createGeometry(cylinderGeometry);
  206. }
  207. return createCylinderOutlineGeometry;
  208. });