createCoplanarPolygonGeometry.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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', './AttributeCompression-87682214', './GeometryPipeline-f0b16df6', './EncodedCartesian3-8b2b90d0', './IndexDatatype-3de60176', './IntersectionTests-c2360ffa', './Plane-a1a3fd52', './VertexFormat-fbb91dc7', './GeometryInstance-72fd4e35', './arrayRemoveDuplicates-dd708d81', './BoundingRectangle-36e6acca', './EllipsoidTangentPlane-10c6053a', './OrientedBoundingBox-a786ab5d', './CoplanarPolygonGeometryLibrary-602752f0', './ArcType-d521909b', './EllipsoidRhumbLine-c6cdbfd3', './PolygonPipeline-e486c11c', './PolygonGeometryLibrary-7f7b74b5'], function (defined, Check, freezeObject, defaultValue, _Math, Cartesian2, defineProperties, Transforms, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, when, GeometryAttributes, AttributeCompression, GeometryPipeline, EncodedCartesian3, IndexDatatype, IntersectionTests, Plane, VertexFormat, GeometryInstance, arrayRemoveDuplicates, BoundingRectangle, EllipsoidTangentPlane, OrientedBoundingBox, CoplanarPolygonGeometryLibrary, ArcType, EllipsoidRhumbLine, PolygonPipeline, PolygonGeometryLibrary) { 'use strict';
  3. var scratchPosition = new Cartesian2.Cartesian3();
  4. var scratchBR = new BoundingRectangle.BoundingRectangle();
  5. var stScratch = new Cartesian2.Cartesian2();
  6. var textureCoordinatesOrigin = new Cartesian2.Cartesian2();
  7. var scratchNormal = new Cartesian2.Cartesian3();
  8. var scratchTangent = new Cartesian2.Cartesian3();
  9. var scratchBitangent = new Cartesian2.Cartesian3();
  10. var centerScratch = new Cartesian2.Cartesian3();
  11. var axis1Scratch = new Cartesian2.Cartesian3();
  12. var axis2Scratch = new Cartesian2.Cartesian3();
  13. var quaternionScratch = new Transforms.Quaternion();
  14. var textureMatrixScratch = new Transforms.Matrix3();
  15. var tangentRotationScratch = new Transforms.Matrix3();
  16. var surfaceNormalScratch = new Cartesian2.Cartesian3();
  17. function createGeometryFromPolygon(polygon, vertexFormat, boundingRectangle, stRotation, projectPointTo2D, normal, tangent, bitangent) {
  18. var positions = polygon.positions;
  19. var indices = PolygonPipeline.PolygonPipeline.triangulate(polygon.positions2D, polygon.holes);
  20. /* If polygon is completely unrenderable, just use the first three vertices */
  21. if (indices.length < 3) {
  22. indices = [0, 1, 2];
  23. }
  24. var newIndices = IndexDatatype.IndexDatatype.createTypedArray(positions.length, indices.length);
  25. newIndices.set(indices);
  26. var textureMatrix = textureMatrixScratch;
  27. if (stRotation !== 0.0) {
  28. var rotation = Transforms.Quaternion.fromAxisAngle(normal, stRotation, quaternionScratch);
  29. textureMatrix = Transforms.Matrix3.fromQuaternion(rotation, textureMatrix);
  30. if (vertexFormat.tangent || vertexFormat.bitangent) {
  31. rotation = Transforms.Quaternion.fromAxisAngle(normal, -stRotation, quaternionScratch);
  32. var tangentRotation = Transforms.Matrix3.fromQuaternion(rotation, tangentRotationScratch);
  33. tangent = Cartesian2.Cartesian3.normalize(Transforms.Matrix3.multiplyByVector(tangentRotation, tangent, tangent), tangent);
  34. if (vertexFormat.bitangent) {
  35. bitangent = Cartesian2.Cartesian3.normalize(Cartesian2.Cartesian3.cross(normal, tangent, bitangent), bitangent);
  36. }
  37. }
  38. } else {
  39. textureMatrix = Transforms.Matrix3.clone(Transforms.Matrix3.IDENTITY, textureMatrix);
  40. }
  41. var stOrigin = textureCoordinatesOrigin;
  42. if (vertexFormat.st) {
  43. stOrigin.x = boundingRectangle.x;
  44. stOrigin.y = boundingRectangle.y;
  45. }
  46. var length = positions.length;
  47. var size = length * 3;
  48. var flatPositions = new Float64Array(size);
  49. var normals = vertexFormat.normal ? new Float32Array(size) : undefined;
  50. var tangents = vertexFormat.tangent ? new Float32Array(size) : undefined;
  51. var bitangents = vertexFormat.bitangent ? new Float32Array(size) : undefined;
  52. var textureCoordinates = vertexFormat.st ? new Float32Array(length * 2) : undefined;
  53. var positionIndex = 0;
  54. var normalIndex = 0;
  55. var bitangentIndex = 0;
  56. var tangentIndex = 0;
  57. var stIndex = 0;
  58. for (var i = 0; i < length; i++) {
  59. var position = positions[i];
  60. flatPositions[positionIndex++] = position.x;
  61. flatPositions[positionIndex++] = position.y;
  62. flatPositions[positionIndex++] = position.z;
  63. if (vertexFormat.st) {
  64. var p = Transforms.Matrix3.multiplyByVector(textureMatrix, position, scratchPosition);
  65. var st = projectPointTo2D(p, stScratch);
  66. Cartesian2.Cartesian2.subtract(st, stOrigin, st);
  67. var stx = _Math.CesiumMath.clamp(st.x / boundingRectangle.width, 0, 1);
  68. var sty = _Math.CesiumMath.clamp(st.y / boundingRectangle.height, 0, 1);
  69. textureCoordinates[stIndex++] = stx;
  70. textureCoordinates[stIndex++] = sty;
  71. }
  72. if (vertexFormat.normal) {
  73. normals[normalIndex++] = normal.x;
  74. normals[normalIndex++] = normal.y;
  75. normals[normalIndex++] = normal.z;
  76. }
  77. if (vertexFormat.tangent) {
  78. tangents[tangentIndex++] = tangent.x;
  79. tangents[tangentIndex++] = tangent.y;
  80. tangents[tangentIndex++] = tangent.z;
  81. }
  82. if (vertexFormat.bitangent) {
  83. bitangents[bitangentIndex++] = bitangent.x;
  84. bitangents[bitangentIndex++] = bitangent.y;
  85. bitangents[bitangentIndex++] = bitangent.z;
  86. }
  87. }
  88. var attributes = new GeometryAttributes.GeometryAttributes();
  89. if (vertexFormat.position) {
  90. attributes.position = new GeometryAttribute.GeometryAttribute({
  91. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  92. componentsPerAttribute : 3,
  93. values : flatPositions
  94. });
  95. }
  96. if (vertexFormat.normal) {
  97. attributes.normal = new GeometryAttribute.GeometryAttribute({
  98. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  99. componentsPerAttribute : 3,
  100. values : normals
  101. });
  102. }
  103. if (vertexFormat.tangent) {
  104. attributes.tangent = new GeometryAttribute.GeometryAttribute({
  105. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  106. componentsPerAttribute : 3,
  107. values : tangents
  108. });
  109. }
  110. if (vertexFormat.bitangent) {
  111. attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  112. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  113. componentsPerAttribute : 3,
  114. values : bitangents
  115. });
  116. }
  117. if (vertexFormat.st) {
  118. attributes.st = new GeometryAttribute.GeometryAttribute({
  119. componentDatatype : ComponentDatatype.ComponentDatatype.FLOAT,
  120. componentsPerAttribute : 2,
  121. values : textureCoordinates
  122. });
  123. }
  124. return new GeometryAttribute.Geometry({
  125. attributes : attributes,
  126. indices : newIndices,
  127. primitiveType : GeometryAttribute.PrimitiveType.TRIANGLES
  128. });
  129. }
  130. /**
  131. * A description of a polygon composed of arbitrary coplanar positions.
  132. *
  133. * @alias CoplanarPolygonGeometry
  134. * @constructor
  135. *
  136. * @param {Object} options Object with the following properties:
  137. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  138. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  139. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  140. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  141. *
  142. * @example
  143. * var polygon = new Cesium.CoplanarPolygonGeometry({
  144. * positions : Cesium.Cartesian3.fromDegreesArrayHeights([
  145. * -90.0, 30.0, 0.0,
  146. * -90.0, 30.0, 1000.0,
  147. * -80.0, 30.0, 1000.0,
  148. * -80.0, 30.0, 0.0
  149. * ])
  150. * });
  151. * var geometry = Cesium.CoplanarPolygonGeometry.createGeometry(polygon);
  152. *
  153. * @see CoplanarPolygonGeometry.createGeometry
  154. */
  155. function CoplanarPolygonGeometry(options) {
  156. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  157. var polygonHierarchy = options.polygonHierarchy;
  158. //>>includeStart('debug', pragmas.debug);
  159. Check.Check.defined('options.polygonHierarchy', polygonHierarchy);
  160. //>>includeEnd('debug');
  161. var vertexFormat = defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT);
  162. this._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat);
  163. this._polygonHierarchy = polygonHierarchy;
  164. this._stRotation = defaultValue.defaultValue(options.stRotation, 0.0);
  165. this._ellipsoid = Cartesian2.Ellipsoid.clone(defaultValue.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84));
  166. this._workerName = 'createCoplanarPolygonGeometry';
  167. /**
  168. * The number of elements used to pack the object into an array.
  169. * @type {Number}
  170. */
  171. this.packedLength = PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) + VertexFormat.VertexFormat.packedLength + Cartesian2.Ellipsoid.packedLength + 2;
  172. }
  173. /**
  174. * A description of a coplanar polygon from an array of positions.
  175. *
  176. * @param {Object} options Object with the following properties:
  177. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  178. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  179. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  180. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  181. * @returns {CoplanarPolygonGeometry}
  182. *
  183. * @example
  184. * // create a polygon from points
  185. * var polygon = Cesium.CoplanarPolygonGeometry.fromPositions({
  186. * positions : Cesium.Cartesian3.fromDegreesArray([
  187. * -72.0, 40.0,
  188. * -70.0, 35.0,
  189. * -75.0, 30.0,
  190. * -70.0, 30.0,
  191. * -68.0, 40.0
  192. * ])
  193. * });
  194. * var geometry = Cesium.PolygonGeometry.createGeometry(polygon);
  195. *
  196. * @see PolygonGeometry#createGeometry
  197. */
  198. CoplanarPolygonGeometry.fromPositions = function(options) {
  199. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  200. //>>includeStart('debug', pragmas.debug);
  201. Check.Check.defined('options.positions', options.positions);
  202. //>>includeEnd('debug');
  203. var newOptions = {
  204. polygonHierarchy : {
  205. positions : options.positions
  206. },
  207. vertexFormat : options.vertexFormat,
  208. stRotation : options.stRotation,
  209. ellipsoid : options.ellipsoid
  210. };
  211. return new CoplanarPolygonGeometry(newOptions);
  212. };
  213. /**
  214. * Stores the provided instance into the provided array.
  215. *
  216. * @param {CoplanarPolygonGeometry} value The value to pack.
  217. * @param {Number[]} array The array to pack into.
  218. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  219. *
  220. * @returns {Number[]} The array that was packed into
  221. */
  222. CoplanarPolygonGeometry.pack = function(value, array, startingIndex) {
  223. //>>includeStart('debug', pragmas.debug);
  224. Check.Check.typeOf.object('value', value);
  225. Check.Check.defined('array', array);
  226. //>>includeEnd('debug');
  227. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  228. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(value._polygonHierarchy, array, startingIndex);
  229. Cartesian2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  230. startingIndex += Cartesian2.Ellipsoid.packedLength;
  231. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  232. startingIndex += VertexFormat.VertexFormat.packedLength;
  233. array[startingIndex++] = value._stRotation;
  234. array[startingIndex] = value.packedLength;
  235. return array;
  236. };
  237. var scratchEllipsoid = Cartesian2.Ellipsoid.clone(Cartesian2.Ellipsoid.UNIT_SPHERE);
  238. var scratchVertexFormat = new VertexFormat.VertexFormat();
  239. var scratchOptions = {
  240. polygonHierarchy : {}
  241. };
  242. /**
  243. * Retrieves an instance from a packed array.
  244. *
  245. * @param {Number[]} array The packed array.
  246. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  247. * @param {CoplanarPolygonGeometry} [result] The object into which to store the result.
  248. * @returns {CoplanarPolygonGeometry} The modified result parameter or a new CoplanarPolygonGeometry instance if one was not provided.
  249. */
  250. CoplanarPolygonGeometry.unpack = function(array, startingIndex, result) {
  251. //>>includeStart('debug', pragmas.debug);
  252. Check.Check.defined('array', array);
  253. //>>includeEnd('debug');
  254. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  255. var polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(array, startingIndex);
  256. startingIndex = polygonHierarchy.startingIndex;
  257. delete polygonHierarchy.startingIndex;
  258. var ellipsoid = Cartesian2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  259. startingIndex += Cartesian2.Ellipsoid.packedLength;
  260. var vertexFormat = VertexFormat.VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
  261. startingIndex += VertexFormat.VertexFormat.packedLength;
  262. var stRotation = array[startingIndex++];
  263. var packedLength = array[startingIndex];
  264. if (!defined.defined(result)) {
  265. result = new CoplanarPolygonGeometry(scratchOptions);
  266. }
  267. result._polygonHierarchy = polygonHierarchy;
  268. result._ellipsoid = Cartesian2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  269. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  270. result._stRotation = stRotation;
  271. result.packedLength = packedLength;
  272. return result;
  273. };
  274. /**
  275. * Computes the geometric representation of an arbitrary coplanar polygon, including its vertices, indices, and a bounding sphere.
  276. *
  277. * @param {CoplanarPolygonGeometry} polygonGeometry A description of the polygon.
  278. * @returns {Geometry|undefined} The computed vertices and indices.
  279. */
  280. CoplanarPolygonGeometry.createGeometry = function(polygonGeometry) {
  281. var vertexFormat = polygonGeometry._vertexFormat;
  282. var polygonHierarchy = polygonGeometry._polygonHierarchy;
  283. var stRotation = polygonGeometry._stRotation;
  284. var outerPositions = polygonHierarchy.positions;
  285. outerPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(outerPositions, Cartesian2.Cartesian3.equalsEpsilon, true);
  286. if (outerPositions.length < 3) {
  287. return;
  288. }
  289. var normal = scratchNormal;
  290. var tangent = scratchTangent;
  291. var bitangent = scratchBitangent;
  292. var axis1 = axis1Scratch;
  293. var axis2 = axis2Scratch;
  294. var validGeometry = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments(outerPositions, centerScratch, axis1, axis2);
  295. if (!validGeometry) {
  296. return undefined;
  297. }
  298. normal = Cartesian2.Cartesian3.cross(axis1, axis2, normal);
  299. normal = Cartesian2.Cartesian3.normalize(normal, normal);
  300. if (!Cartesian2.Cartesian3.equalsEpsilon(centerScratch, Cartesian2.Cartesian3.ZERO, _Math.CesiumMath.EPSILON6)) {
  301. var surfaceNormal = polygonGeometry._ellipsoid.geodeticSurfaceNormal(centerScratch, surfaceNormalScratch);
  302. if (Cartesian2.Cartesian3.dot(normal, surfaceNormal) < 0) {
  303. normal = Cartesian2.Cartesian3.negate(normal, normal);
  304. axis1 = Cartesian2.Cartesian3.negate(axis1, axis1);
  305. }
  306. }
  307. var projectPoints = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction(centerScratch, axis1, axis2);
  308. var projectPoint = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.createProjectPointTo2DFunction(centerScratch, axis1, axis2);
  309. if (vertexFormat.tangent) {
  310. tangent = Cartesian2.Cartesian3.clone(axis1, tangent);
  311. }
  312. if (vertexFormat.bitangent) {
  313. bitangent = Cartesian2.Cartesian3.clone(axis2, bitangent);
  314. }
  315. var results = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonsFromHierarchy(polygonHierarchy, projectPoints, false);
  316. var hierarchy = results.hierarchy;
  317. var polygons = results.polygons;
  318. if (hierarchy.length === 0) {
  319. return;
  320. }
  321. outerPositions = hierarchy[0].outerRing;
  322. var boundingSphere = Transforms.BoundingSphere.fromPoints(outerPositions);
  323. var boundingRectangle = PolygonGeometryLibrary.PolygonGeometryLibrary.computeBoundingRectangle(normal, projectPoint, outerPositions, stRotation, scratchBR);
  324. var geometries = [];
  325. for (var i = 0; i < polygons.length; i++) {
  326. var geometryInstance = new GeometryInstance.GeometryInstance({
  327. geometry : createGeometryFromPolygon(polygons[i], vertexFormat, boundingRectangle, stRotation, projectPoint, normal, tangent, bitangent)
  328. });
  329. geometries.push(geometryInstance);
  330. }
  331. var geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
  332. geometry.attributes.position.values = new Float64Array(geometry.attributes.position.values);
  333. geometry.indices = IndexDatatype.IndexDatatype.createTypedArray(geometry.attributes.position.values.length / 3, geometry.indices);
  334. var attributes = geometry.attributes;
  335. if (!vertexFormat.position) {
  336. delete attributes.position;
  337. }
  338. return new GeometryAttribute.Geometry({
  339. attributes : attributes,
  340. indices : geometry.indices,
  341. primitiveType : geometry.primitiveType,
  342. boundingSphere : boundingSphere
  343. });
  344. };
  345. function createCoplanarPolygonGeometry(polygonGeometry, offset) {
  346. if (defined.defined(offset)) {
  347. polygonGeometry = CoplanarPolygonGeometry.unpack(polygonGeometry, offset);
  348. }
  349. return CoplanarPolygonGeometry.createGeometry(polygonGeometry);
  350. }
  351. return createCoplanarPolygonGeometry;
  352. });