Geometry.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import Cartesian2 from './Cartesian2.js';
  2. import Cartesian3 from './Cartesian3.js';
  3. import Cartographic from './Cartographic.js';
  4. import Check from './Check.js';
  5. import defaultValue from './defaultValue.js';
  6. import defined from './defined.js';
  7. import DeveloperError from './DeveloperError.js';
  8. import GeometryType from './GeometryType.js';
  9. import Matrix2 from './Matrix2.js';
  10. import Matrix3 from './Matrix3.js';
  11. import Matrix4 from './Matrix4.js';
  12. import PrimitiveType from './PrimitiveType.js';
  13. import Quaternion from './Quaternion.js';
  14. import Rectangle from './Rectangle.js';
  15. import Transforms from './Transforms.js';
  16. /**
  17. * A geometry representation with attributes forming vertices and optional index data
  18. * defining primitives. Geometries and an {@link Appearance}, which describes the shading,
  19. * can be assigned to a {@link Primitive} for visualization. A <code>Primitive</code> can
  20. * be created from many heterogeneous - in many cases - geometries for performance.
  21. * <p>
  22. * Geometries can be transformed and optimized using functions in {@link GeometryPipeline}.
  23. * </p>
  24. *
  25. * @alias Geometry
  26. * @constructor
  27. *
  28. * @param {Object} options Object with the following properties:
  29. * @param {GeometryAttributes} options.attributes Attributes, which make up the geometry's vertices.
  30. * @param {PrimitiveType} [options.primitiveType=PrimitiveType.TRIANGLES] The type of primitives in the geometry.
  31. * @param {Uint16Array|Uint32Array} [options.indices] Optional index data that determines the primitives in the geometry.
  32. * @param {BoundingSphere} [options.boundingSphere] An optional bounding sphere that fully enclosed the geometry.
  33. *
  34. * @see PolygonGeometry
  35. * @see RectangleGeometry
  36. * @see EllipseGeometry
  37. * @see CircleGeometry
  38. * @see WallGeometry
  39. * @see SimplePolylineGeometry
  40. * @see BoxGeometry
  41. * @see EllipsoidGeometry
  42. *
  43. * @demo {@link https://sandcastle.cesium.com/index.html?src=Geometry%20and%20Appearances.html|Geometry and Appearances Demo}
  44. *
  45. * @example
  46. * // Create geometry with a position attribute and indexed lines.
  47. * var positions = new Float64Array([
  48. * 0.0, 0.0, 0.0,
  49. * 7500000.0, 0.0, 0.0,
  50. * 0.0, 7500000.0, 0.0
  51. * ]);
  52. *
  53. * var geometry = new Cesium.Geometry({
  54. * attributes : {
  55. * position : new Cesium.GeometryAttribute({
  56. * componentDatatype : Cesium.ComponentDatatype.DOUBLE,
  57. * componentsPerAttribute : 3,
  58. * values : positions
  59. * })
  60. * },
  61. * indices : new Uint16Array([0, 1, 1, 2, 2, 0]),
  62. * primitiveType : Cesium.PrimitiveType.LINES,
  63. * boundingSphere : Cesium.BoundingSphere.fromVertices(positions)
  64. * });
  65. */
  66. function Geometry(options) {
  67. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  68. //>>includeStart('debug', pragmas.debug);
  69. Check.typeOf.object('options.attributes', options.attributes);
  70. //>>includeEnd('debug');
  71. /**
  72. * Attributes, which make up the geometry's vertices. Each property in this object corresponds to a
  73. * {@link GeometryAttribute} containing the attribute's data.
  74. * <p>
  75. * Attributes are always stored non-interleaved in a Geometry.
  76. * </p>
  77. * <p>
  78. * There are reserved attribute names with well-known semantics. The following attributes
  79. * are created by a Geometry (depending on the provided {@link VertexFormat}.
  80. * <ul>
  81. * <li><code>position</code> - 3D vertex position. 64-bit floating-point (for precision). 3 components per attribute. See {@link VertexFormat#position}.</li>
  82. * <li><code>normal</code> - Normal (normalized), commonly used for lighting. 32-bit floating-point. 3 components per attribute. See {@link VertexFormat#normal}.</li>
  83. * <li><code>st</code> - 2D texture coordinate. 32-bit floating-point. 2 components per attribute. See {@link VertexFormat#st}.</li>
  84. * <li><code>bitangent</code> - Bitangent (normalized), used for tangent-space effects like bump mapping. 32-bit floating-point. 3 components per attribute. See {@link VertexFormat#bitangent}.</li>
  85. * <li><code>tangent</code> - Tangent (normalized), used for tangent-space effects like bump mapping. 32-bit floating-point. 3 components per attribute. See {@link VertexFormat#tangent}.</li>
  86. * </ul>
  87. * </p>
  88. * <p>
  89. * The following attribute names are generally not created by a Geometry, but are added
  90. * to a Geometry by a {@link Primitive} or {@link GeometryPipeline} functions to prepare
  91. * the geometry for rendering.
  92. * <ul>
  93. * <li><code>position3DHigh</code> - High 32 bits for encoded 64-bit position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  94. * <li><code>position3DLow</code> - Low 32 bits for encoded 64-bit position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  95. * <li><code>position3DHigh</code> - High 32 bits for encoded 64-bit 2D (Columbus view) position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  96. * <li><code>position2DLow</code> - Low 32 bits for encoded 64-bit 2D (Columbus view) position computed with {@link GeometryPipeline.encodeAttribute}. 32-bit floating-point. 4 components per attribute.</li>
  97. * <li><code>color</code> - RGBA color (normalized) usually from {@link GeometryInstance#color}. 32-bit floating-point. 4 components per attribute.</li>
  98. * <li><code>pickColor</code> - RGBA color used for picking. 32-bit floating-point. 4 components per attribute.</li>
  99. * </ul>
  100. * </p>
  101. *
  102. * @type GeometryAttributes
  103. *
  104. * @default undefined
  105. *
  106. *
  107. * @example
  108. * geometry.attributes.position = new Cesium.GeometryAttribute({
  109. * componentDatatype : Cesium.ComponentDatatype.FLOAT,
  110. * componentsPerAttribute : 3,
  111. * values : new Float32Array(0)
  112. * });
  113. *
  114. * @see GeometryAttribute
  115. * @see VertexFormat
  116. */
  117. this.attributes = options.attributes;
  118. /**
  119. * Optional index data that - along with {@link Geometry#primitiveType} -
  120. * determines the primitives in the geometry.
  121. *
  122. * @type Array
  123. *
  124. * @default undefined
  125. */
  126. this.indices = options.indices;
  127. /**
  128. * The type of primitives in the geometry. This is most often {@link PrimitiveType.TRIANGLES},
  129. * but can varying based on the specific geometry.
  130. *
  131. * @type PrimitiveType
  132. *
  133. * @default undefined
  134. */
  135. this.primitiveType = defaultValue(options.primitiveType, PrimitiveType.TRIANGLES);
  136. /**
  137. * An optional bounding sphere that fully encloses the geometry. This is
  138. * commonly used for culling.
  139. *
  140. * @type BoundingSphere
  141. *
  142. * @default undefined
  143. */
  144. this.boundingSphere = options.boundingSphere;
  145. /**
  146. * @private
  147. */
  148. this.geometryType = defaultValue(options.geometryType, GeometryType.NONE);
  149. /**
  150. * @private
  151. */
  152. this.boundingSphereCV = options.boundingSphereCV;
  153. /**
  154. * @private
  155. * Used for computing the bounding sphere for geometry using the applyOffset vertex attribute
  156. */
  157. this.offsetAttribute = options.offsetAttribute;
  158. }
  159. /**
  160. * Computes the number of vertices in a geometry. The runtime is linear with
  161. * respect to the number of attributes in a vertex, not the number of vertices.
  162. *
  163. * @param {Geometry} geometry The geometry.
  164. * @returns {Number} The number of vertices in the geometry.
  165. *
  166. * @example
  167. * var numVertices = Cesium.Geometry.computeNumberOfVertices(geometry);
  168. */
  169. Geometry.computeNumberOfVertices = function(geometry) {
  170. //>>includeStart('debug', pragmas.debug);
  171. Check.typeOf.object('geometry', geometry);
  172. //>>includeEnd('debug');
  173. var numberOfVertices = -1;
  174. for ( var property in geometry.attributes) {
  175. if (geometry.attributes.hasOwnProperty(property) &&
  176. defined(geometry.attributes[property]) &&
  177. defined(geometry.attributes[property].values)) {
  178. var attribute = geometry.attributes[property];
  179. var num = attribute.values.length / attribute.componentsPerAttribute;
  180. //>>includeStart('debug', pragmas.debug);
  181. if ((numberOfVertices !== num) && (numberOfVertices !== -1)) {
  182. throw new DeveloperError('All attribute lists must have the same number of attributes.');
  183. }
  184. //>>includeEnd('debug');
  185. numberOfVertices = num;
  186. }
  187. }
  188. return numberOfVertices;
  189. };
  190. var rectangleCenterScratch = new Cartographic();
  191. var enuCenterScratch = new Cartesian3();
  192. var fixedFrameToEnuScratch = new Matrix4();
  193. var boundingRectanglePointsCartographicScratch = [new Cartographic(), new Cartographic(), new Cartographic()];
  194. var boundingRectanglePointsEnuScratch = [new Cartesian2(), new Cartesian2(), new Cartesian2()];
  195. var points2DScratch = [new Cartesian2(), new Cartesian2(), new Cartesian2()];
  196. var pointEnuScratch = new Cartesian3();
  197. var enuRotationScratch = new Quaternion();
  198. var enuRotationMatrixScratch = new Matrix4();
  199. var rotation2DScratch = new Matrix2();
  200. /**
  201. * For remapping texture coordinates when rendering GroundPrimitives with materials.
  202. * GroundPrimitive texture coordinates are computed to align with the cartographic coordinate system on the globe.
  203. * However, EllipseGeometry, RectangleGeometry, and PolygonGeometry all bake rotations to per-vertex texture coordinates
  204. * using different strategies.
  205. *
  206. * This method is used by EllipseGeometry and PolygonGeometry to approximate the same visual effect.
  207. * We encapsulate rotation and scale by computing a "transformed" texture coordinate system and computing
  208. * a set of reference points from which "cartographic" texture coordinates can be remapped to the "transformed"
  209. * system using distances to lines in 2D.
  210. *
  211. * This approximation becomes less accurate as the covered area increases, especially for GroundPrimitives near the poles,
  212. * but is generally reasonable for polygons and ellipses around the size of USA states.
  213. *
  214. * RectangleGeometry has its own version of this method that computes remapping coordinates using cartographic space
  215. * as an intermediary instead of local ENU, which is more accurate for large-area rectangles.
  216. *
  217. * @param {Cartesian3[]} positions Array of positions outlining the geometry
  218. * @param {Number} stRotation Texture coordinate rotation.
  219. * @param {Ellipsoid} ellipsoid Ellipsoid for projecting and generating local vectors.
  220. * @param {Rectangle} boundingRectangle Bounding rectangle around the positions.
  221. * @returns {Number[]} An array of 6 numbers specifying [minimum point, u extent, v extent] as points in the "cartographic" system.
  222. * @private
  223. */
  224. Geometry._textureCoordinateRotationPoints = function(positions, stRotation, ellipsoid, boundingRectangle) {
  225. var i;
  226. // Create a local east-north-up coordinate system centered on the polygon's bounding rectangle.
  227. // Project the southwest, northwest, and southeast corners of the bounding rectangle into the plane of ENU as 2D points.
  228. // These are the equivalents of (0,0), (0,1), and (1,0) in the texture coordiante system computed in ShadowVolumeAppearanceFS,
  229. // aka "ENU texture space."
  230. var rectangleCenter = Rectangle.center(boundingRectangle, rectangleCenterScratch);
  231. var enuCenter = Cartographic.toCartesian(rectangleCenter, ellipsoid, enuCenterScratch);
  232. var enuToFixedFrame = Transforms.eastNorthUpToFixedFrame(enuCenter, ellipsoid, fixedFrameToEnuScratch);
  233. var fixedFrameToEnu = Matrix4.inverse(enuToFixedFrame, fixedFrameToEnuScratch);
  234. var boundingPointsEnu = boundingRectanglePointsEnuScratch;
  235. var boundingPointsCarto = boundingRectanglePointsCartographicScratch;
  236. boundingPointsCarto[0].longitude = boundingRectangle.west;
  237. boundingPointsCarto[0].latitude = boundingRectangle.south;
  238. boundingPointsCarto[1].longitude = boundingRectangle.west;
  239. boundingPointsCarto[1].latitude = boundingRectangle.north;
  240. boundingPointsCarto[2].longitude = boundingRectangle.east;
  241. boundingPointsCarto[2].latitude = boundingRectangle.south;
  242. var posEnu = pointEnuScratch;
  243. for (i = 0; i < 3; i++) {
  244. Cartographic.toCartesian(boundingPointsCarto[i], ellipsoid, posEnu);
  245. posEnu = Matrix4.multiplyByPointAsVector(fixedFrameToEnu, posEnu, posEnu);
  246. boundingPointsEnu[i].x = posEnu.x;
  247. boundingPointsEnu[i].y = posEnu.y;
  248. }
  249. // Rotate each point in the polygon around the up vector in the ENU by -stRotation and project into ENU as 2D.
  250. // Compute the bounding box of these rotated points in the 2D ENU plane.
  251. // Rotate the corners back by stRotation, then compute their equivalents in the ENU texture space using the corners computed earlier.
  252. var rotation = Quaternion.fromAxisAngle(Cartesian3.UNIT_Z, -stRotation, enuRotationScratch);
  253. var textureMatrix = Matrix3.fromQuaternion(rotation, enuRotationMatrixScratch);
  254. var positionsLength = positions.length;
  255. var enuMinX = Number.POSITIVE_INFINITY;
  256. var enuMinY = Number.POSITIVE_INFINITY;
  257. var enuMaxX = Number.NEGATIVE_INFINITY;
  258. var enuMaxY = Number.NEGATIVE_INFINITY;
  259. for (i = 0; i < positionsLength; i++) {
  260. posEnu = Matrix4.multiplyByPointAsVector(fixedFrameToEnu, positions[i], posEnu);
  261. posEnu = Matrix3.multiplyByVector(textureMatrix, posEnu, posEnu);
  262. enuMinX = Math.min(enuMinX, posEnu.x);
  263. enuMinY = Math.min(enuMinY, posEnu.y);
  264. enuMaxX = Math.max(enuMaxX, posEnu.x);
  265. enuMaxY = Math.max(enuMaxY, posEnu.y);
  266. }
  267. var toDesiredInComputed = Matrix2.fromRotation(stRotation, rotation2DScratch);
  268. var points2D = points2DScratch;
  269. points2D[0].x = enuMinX;
  270. points2D[0].y = enuMinY;
  271. points2D[1].x = enuMinX;
  272. points2D[1].y = enuMaxY;
  273. points2D[2].x = enuMaxX;
  274. points2D[2].y = enuMinY;
  275. var boundingEnuMin = boundingPointsEnu[0];
  276. var boundingPointsWidth = boundingPointsEnu[2].x - boundingEnuMin.x;
  277. var boundingPointsHeight = boundingPointsEnu[1].y - boundingEnuMin.y;
  278. for (i = 0; i < 3; i++) {
  279. var point2D = points2D[i];
  280. // rotate back
  281. Matrix2.multiplyByVector(toDesiredInComputed, point2D, point2D);
  282. // Convert point into east-north texture coordinate space
  283. point2D.x = (point2D.x - boundingEnuMin.x) / boundingPointsWidth;
  284. point2D.y = (point2D.y - boundingEnuMin.y) / boundingPointsHeight;
  285. }
  286. var minXYCorner = points2D[0];
  287. var maxYCorner = points2D[1];
  288. var maxXCorner = points2D[2];
  289. var result = new Array(6);
  290. Cartesian2.pack(minXYCorner, result);
  291. Cartesian2.pack(maxYCorner, result, 2);
  292. Cartesian2.pack(maxXCorner, result, 4);
  293. return result;
  294. };
  295. export default Geometry;