createCoplanarPolygonGeometry.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./arrayRemoveDuplicates-87160c89', './BoundingRectangle-41078ce4', './Transforms-323408fe', './Matrix2-69c32d33', './RuntimeError-c581ca93', './ComponentDatatype-b1ea011a', './CoplanarPolygonGeometryLibrary-757a06dc', './defaultValue-94c3e563', './GeometryAttribute-cb73bb3f', './GeometryAttributes-7df9bef6', './GeometryInstance-f69fd420', './GeometryPipeline-e27e35f8', './IndexDatatype-c4099fe9', './PolygonGeometryLibrary-515b324f', './PolygonPipeline-d1884135', './VertexFormat-e46f29d6', './_commonjsHelpers-3aae1032-f55dc0c4', './combine-761d9c3f', './WebGLConstants-7dccdc96', './OrientedBoundingBox-a5e39a7d', './EllipsoidTangentPlane-1e8d1fc2', './AxisAlignedBoundingBox-df2331b2', './IntersectionTests-d5d945ac', './Plane-069b6800', './AttributeCompression-3cfab808', './EncodedCartesian3-b1f97f8a', './ArcType-0cf52f8c', './EllipsoidRhumbLine-5cb6da82'], (function (arrayRemoveDuplicates, BoundingRectangle, Transforms, Matrix2, RuntimeError, ComponentDatatype, CoplanarPolygonGeometryLibrary, defaultValue, GeometryAttribute, GeometryAttributes, GeometryInstance, GeometryPipeline, IndexDatatype, PolygonGeometryLibrary, PolygonPipeline, VertexFormat, _commonjsHelpers3aae1032, combine, WebGLConstants, OrientedBoundingBox, EllipsoidTangentPlane, AxisAlignedBoundingBox, IntersectionTests, Plane, AttributeCompression, EncodedCartesian3, ArcType, EllipsoidRhumbLine) { 'use strict';
  3. const scratchPosition = new Matrix2.Cartesian3();
  4. const scratchBR = new BoundingRectangle.BoundingRectangle();
  5. const stScratch = new Matrix2.Cartesian2();
  6. const textureCoordinatesOrigin = new Matrix2.Cartesian2();
  7. const scratchNormal = new Matrix2.Cartesian3();
  8. const scratchTangent = new Matrix2.Cartesian3();
  9. const scratchBitangent = new Matrix2.Cartesian3();
  10. const centerScratch = new Matrix2.Cartesian3();
  11. const axis1Scratch = new Matrix2.Cartesian3();
  12. const axis2Scratch = new Matrix2.Cartesian3();
  13. const quaternionScratch = new Transforms.Quaternion();
  14. const textureMatrixScratch = new Matrix2.Matrix3();
  15. const tangentRotationScratch = new Matrix2.Matrix3();
  16. const surfaceNormalScratch = new Matrix2.Cartesian3();
  17. function createGeometryFromPolygon(
  18. polygon,
  19. vertexFormat,
  20. boundingRectangle,
  21. stRotation,
  22. projectPointTo2D,
  23. normal,
  24. tangent,
  25. bitangent
  26. ) {
  27. const positions = polygon.positions;
  28. let indices = PolygonPipeline.PolygonPipeline.triangulate(polygon.positions2D, polygon.holes);
  29. /* If polygon is completely unrenderable, just use the first three vertices */
  30. if (indices.length < 3) {
  31. indices = [0, 1, 2];
  32. }
  33. const newIndices = IndexDatatype.IndexDatatype.createTypedArray(
  34. positions.length,
  35. indices.length
  36. );
  37. newIndices.set(indices);
  38. let textureMatrix = textureMatrixScratch;
  39. if (stRotation !== 0.0) {
  40. let rotation = Transforms.Quaternion.fromAxisAngle(
  41. normal,
  42. stRotation,
  43. quaternionScratch
  44. );
  45. textureMatrix = Matrix2.Matrix3.fromQuaternion(rotation, textureMatrix);
  46. if (vertexFormat.tangent || vertexFormat.bitangent) {
  47. rotation = Transforms.Quaternion.fromAxisAngle(
  48. normal,
  49. -stRotation,
  50. quaternionScratch
  51. );
  52. const tangentRotation = Matrix2.Matrix3.fromQuaternion(
  53. rotation,
  54. tangentRotationScratch
  55. );
  56. tangent = Matrix2.Cartesian3.normalize(
  57. Matrix2.Matrix3.multiplyByVector(tangentRotation, tangent, tangent),
  58. tangent
  59. );
  60. if (vertexFormat.bitangent) {
  61. bitangent = Matrix2.Cartesian3.normalize(
  62. Matrix2.Cartesian3.cross(normal, tangent, bitangent),
  63. bitangent
  64. );
  65. }
  66. }
  67. } else {
  68. textureMatrix = Matrix2.Matrix3.clone(Matrix2.Matrix3.IDENTITY, textureMatrix);
  69. }
  70. const stOrigin = textureCoordinatesOrigin;
  71. if (vertexFormat.st) {
  72. stOrigin.x = boundingRectangle.x;
  73. stOrigin.y = boundingRectangle.y;
  74. }
  75. const length = positions.length;
  76. const size = length * 3;
  77. const flatPositions = new Float64Array(size);
  78. const normals = vertexFormat.normal ? new Float32Array(size) : undefined;
  79. const tangents = vertexFormat.tangent ? new Float32Array(size) : undefined;
  80. const bitangents = vertexFormat.bitangent
  81. ? new Float32Array(size)
  82. : undefined;
  83. const textureCoordinates = vertexFormat.st
  84. ? new Float32Array(length * 2)
  85. : undefined;
  86. let positionIndex = 0;
  87. let normalIndex = 0;
  88. let bitangentIndex = 0;
  89. let tangentIndex = 0;
  90. let stIndex = 0;
  91. for (let i = 0; i < length; i++) {
  92. const position = positions[i];
  93. flatPositions[positionIndex++] = position.x;
  94. flatPositions[positionIndex++] = position.y;
  95. flatPositions[positionIndex++] = position.z;
  96. if (vertexFormat.st) {
  97. const p = Matrix2.Matrix3.multiplyByVector(
  98. textureMatrix,
  99. position,
  100. scratchPosition
  101. );
  102. const st = projectPointTo2D(p, stScratch);
  103. Matrix2.Cartesian2.subtract(st, stOrigin, st);
  104. const stx = ComponentDatatype.CesiumMath.clamp(st.x / boundingRectangle.width, 0, 1);
  105. const sty = ComponentDatatype.CesiumMath.clamp(st.y / boundingRectangle.height, 0, 1);
  106. textureCoordinates[stIndex++] = stx;
  107. textureCoordinates[stIndex++] = sty;
  108. }
  109. if (vertexFormat.normal) {
  110. normals[normalIndex++] = normal.x;
  111. normals[normalIndex++] = normal.y;
  112. normals[normalIndex++] = normal.z;
  113. }
  114. if (vertexFormat.tangent) {
  115. tangents[tangentIndex++] = tangent.x;
  116. tangents[tangentIndex++] = tangent.y;
  117. tangents[tangentIndex++] = tangent.z;
  118. }
  119. if (vertexFormat.bitangent) {
  120. bitangents[bitangentIndex++] = bitangent.x;
  121. bitangents[bitangentIndex++] = bitangent.y;
  122. bitangents[bitangentIndex++] = bitangent.z;
  123. }
  124. }
  125. const attributes = new GeometryAttributes.GeometryAttributes();
  126. if (vertexFormat.position) {
  127. attributes.position = new GeometryAttribute.GeometryAttribute({
  128. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  129. componentsPerAttribute: 3,
  130. values: flatPositions,
  131. });
  132. }
  133. if (vertexFormat.normal) {
  134. attributes.normal = new GeometryAttribute.GeometryAttribute({
  135. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  136. componentsPerAttribute: 3,
  137. values: normals,
  138. });
  139. }
  140. if (vertexFormat.tangent) {
  141. attributes.tangent = new GeometryAttribute.GeometryAttribute({
  142. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  143. componentsPerAttribute: 3,
  144. values: tangents,
  145. });
  146. }
  147. if (vertexFormat.bitangent) {
  148. attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  149. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  150. componentsPerAttribute: 3,
  151. values: bitangents,
  152. });
  153. }
  154. if (vertexFormat.st) {
  155. attributes.st = new GeometryAttribute.GeometryAttribute({
  156. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  157. componentsPerAttribute: 2,
  158. values: textureCoordinates,
  159. });
  160. }
  161. return new GeometryAttribute.Geometry({
  162. attributes: attributes,
  163. indices: newIndices,
  164. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  165. });
  166. }
  167. /**
  168. * A description of a polygon composed of arbitrary coplanar positions.
  169. *
  170. * @alias CoplanarPolygonGeometry
  171. * @constructor
  172. *
  173. * @param {Object} options Object with the following properties:
  174. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  175. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  176. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  177. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  178. *
  179. * @example
  180. * const polygonGeometry = new Cesium.CoplanarPolygonGeometry({
  181. * polygonHierarchy: new Cesium.PolygonHierarchy(
  182. * Cesium.Cartesian3.fromDegreesArrayHeights([
  183. * -90.0, 30.0, 0.0,
  184. * -90.0, 30.0, 300000.0,
  185. * -80.0, 30.0, 300000.0,
  186. * -80.0, 30.0, 0.0
  187. * ]))
  188. * });
  189. *
  190. */
  191. function CoplanarPolygonGeometry(options) {
  192. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  193. const polygonHierarchy = options.polygonHierarchy;
  194. //>>includeStart('debug', pragmas.debug);
  195. RuntimeError.Check.defined("options.polygonHierarchy", polygonHierarchy);
  196. //>>includeEnd('debug');
  197. const vertexFormat = defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT);
  198. this._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat);
  199. this._polygonHierarchy = polygonHierarchy;
  200. this._stRotation = defaultValue.defaultValue(options.stRotation, 0.0);
  201. this._ellipsoid = Matrix2.Ellipsoid.clone(
  202. defaultValue.defaultValue(options.ellipsoid, Matrix2.Ellipsoid.WGS84)
  203. );
  204. this._workerName = "createCoplanarPolygonGeometry";
  205. /**
  206. * The number of elements used to pack the object into an array.
  207. * @type {Number}
  208. */
  209. this.packedLength =
  210. PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) +
  211. VertexFormat.VertexFormat.packedLength +
  212. Matrix2.Ellipsoid.packedLength +
  213. 2;
  214. }
  215. /**
  216. * A description of a coplanar polygon from an array of positions.
  217. *
  218. * @param {Object} options Object with the following properties:
  219. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  220. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  221. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  222. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  223. * @returns {CoplanarPolygonGeometry}
  224. *
  225. * @example
  226. * // create a polygon from points
  227. * const polygon = Cesium.CoplanarPolygonGeometry.fromPositions({
  228. * positions : Cesium.Cartesian3.fromDegreesArray([
  229. * -72.0, 40.0,
  230. * -70.0, 35.0,
  231. * -75.0, 30.0,
  232. * -70.0, 30.0,
  233. * -68.0, 40.0
  234. * ])
  235. * });
  236. * const geometry = Cesium.PolygonGeometry.createGeometry(polygon);
  237. *
  238. * @see PolygonGeometry#createGeometry
  239. */
  240. CoplanarPolygonGeometry.fromPositions = function (options) {
  241. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  242. //>>includeStart('debug', pragmas.debug);
  243. RuntimeError.Check.defined("options.positions", options.positions);
  244. //>>includeEnd('debug');
  245. const newOptions = {
  246. polygonHierarchy: {
  247. positions: options.positions,
  248. },
  249. vertexFormat: options.vertexFormat,
  250. stRotation: options.stRotation,
  251. ellipsoid: options.ellipsoid,
  252. };
  253. return new CoplanarPolygonGeometry(newOptions);
  254. };
  255. /**
  256. * Stores the provided instance into the provided array.
  257. *
  258. * @param {CoplanarPolygonGeometry} value The value to pack.
  259. * @param {Number[]} array The array to pack into.
  260. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  261. *
  262. * @returns {Number[]} The array that was packed into
  263. */
  264. CoplanarPolygonGeometry.pack = function (value, array, startingIndex) {
  265. //>>includeStart('debug', pragmas.debug);
  266. RuntimeError.Check.typeOf.object("value", value);
  267. RuntimeError.Check.defined("array", array);
  268. //>>includeEnd('debug');
  269. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  270. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(
  271. value._polygonHierarchy,
  272. array,
  273. startingIndex
  274. );
  275. Matrix2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  276. startingIndex += Matrix2.Ellipsoid.packedLength;
  277. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  278. startingIndex += VertexFormat.VertexFormat.packedLength;
  279. array[startingIndex++] = value._stRotation;
  280. array[startingIndex] = value.packedLength;
  281. return array;
  282. };
  283. const scratchEllipsoid = Matrix2.Ellipsoid.clone(Matrix2.Ellipsoid.UNIT_SPHERE);
  284. const scratchVertexFormat = new VertexFormat.VertexFormat();
  285. const scratchOptions = {
  286. polygonHierarchy: {},
  287. };
  288. /**
  289. * Retrieves an instance from a packed array.
  290. *
  291. * @param {Number[]} array The packed array.
  292. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  293. * @param {CoplanarPolygonGeometry} [result] The object into which to store the result.
  294. * @returns {CoplanarPolygonGeometry} The modified result parameter or a new CoplanarPolygonGeometry instance if one was not provided.
  295. */
  296. CoplanarPolygonGeometry.unpack = function (array, startingIndex, result) {
  297. //>>includeStart('debug', pragmas.debug);
  298. RuntimeError.Check.defined("array", array);
  299. //>>includeEnd('debug');
  300. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  301. const polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(
  302. array,
  303. startingIndex
  304. );
  305. startingIndex = polygonHierarchy.startingIndex;
  306. delete polygonHierarchy.startingIndex;
  307. const ellipsoid = Matrix2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  308. startingIndex += Matrix2.Ellipsoid.packedLength;
  309. const vertexFormat = VertexFormat.VertexFormat.unpack(
  310. array,
  311. startingIndex,
  312. scratchVertexFormat
  313. );
  314. startingIndex += VertexFormat.VertexFormat.packedLength;
  315. const stRotation = array[startingIndex++];
  316. const packedLength = array[startingIndex];
  317. if (!defaultValue.defined(result)) {
  318. result = new CoplanarPolygonGeometry(scratchOptions);
  319. }
  320. result._polygonHierarchy = polygonHierarchy;
  321. result._ellipsoid = Matrix2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  322. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  323. result._stRotation = stRotation;
  324. result.packedLength = packedLength;
  325. return result;
  326. };
  327. /**
  328. * Computes the geometric representation of an arbitrary coplanar polygon, including its vertices, indices, and a bounding sphere.
  329. *
  330. * @param {CoplanarPolygonGeometry} polygonGeometry A description of the polygon.
  331. * @returns {Geometry|undefined} The computed vertices and indices.
  332. */
  333. CoplanarPolygonGeometry.createGeometry = function (polygonGeometry) {
  334. const vertexFormat = polygonGeometry._vertexFormat;
  335. const polygonHierarchy = polygonGeometry._polygonHierarchy;
  336. const stRotation = polygonGeometry._stRotation;
  337. let outerPositions = polygonHierarchy.positions;
  338. outerPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  339. outerPositions,
  340. Matrix2.Cartesian3.equalsEpsilon,
  341. true
  342. );
  343. if (outerPositions.length < 3) {
  344. return;
  345. }
  346. let normal = scratchNormal;
  347. let tangent = scratchTangent;
  348. let bitangent = scratchBitangent;
  349. let axis1 = axis1Scratch;
  350. const axis2 = axis2Scratch;
  351. const validGeometry = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments(
  352. outerPositions,
  353. centerScratch,
  354. axis1,
  355. axis2
  356. );
  357. if (!validGeometry) {
  358. return undefined;
  359. }
  360. normal = Matrix2.Cartesian3.cross(axis1, axis2, normal);
  361. normal = Matrix2.Cartesian3.normalize(normal, normal);
  362. if (
  363. !Matrix2.Cartesian3.equalsEpsilon(
  364. centerScratch,
  365. Matrix2.Cartesian3.ZERO,
  366. ComponentDatatype.CesiumMath.EPSILON6
  367. )
  368. ) {
  369. const surfaceNormal = polygonGeometry._ellipsoid.geodeticSurfaceNormal(
  370. centerScratch,
  371. surfaceNormalScratch
  372. );
  373. if (Matrix2.Cartesian3.dot(normal, surfaceNormal) < 0) {
  374. normal = Matrix2.Cartesian3.negate(normal, normal);
  375. axis1 = Matrix2.Cartesian3.negate(axis1, axis1);
  376. }
  377. }
  378. const projectPoints = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction(
  379. centerScratch,
  380. axis1,
  381. axis2
  382. );
  383. const projectPoint = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.createProjectPointTo2DFunction(
  384. centerScratch,
  385. axis1,
  386. axis2
  387. );
  388. if (vertexFormat.tangent) {
  389. tangent = Matrix2.Cartesian3.clone(axis1, tangent);
  390. }
  391. if (vertexFormat.bitangent) {
  392. bitangent = Matrix2.Cartesian3.clone(axis2, bitangent);
  393. }
  394. const results = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonsFromHierarchy(
  395. polygonHierarchy,
  396. projectPoints,
  397. false
  398. );
  399. const hierarchy = results.hierarchy;
  400. const polygons = results.polygons;
  401. if (hierarchy.length === 0) {
  402. return;
  403. }
  404. outerPositions = hierarchy[0].outerRing;
  405. const boundingSphere = Transforms.BoundingSphere.fromPoints(outerPositions);
  406. const boundingRectangle = PolygonGeometryLibrary.PolygonGeometryLibrary.computeBoundingRectangle(
  407. normal,
  408. projectPoint,
  409. outerPositions,
  410. stRotation,
  411. scratchBR
  412. );
  413. const geometries = [];
  414. for (let i = 0; i < polygons.length; i++) {
  415. const geometryInstance = new GeometryInstance.GeometryInstance({
  416. geometry: createGeometryFromPolygon(
  417. polygons[i],
  418. vertexFormat,
  419. boundingRectangle,
  420. stRotation,
  421. projectPoint,
  422. normal,
  423. tangent,
  424. bitangent
  425. ),
  426. });
  427. geometries.push(geometryInstance);
  428. }
  429. const geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
  430. geometry.attributes.position.values = new Float64Array(
  431. geometry.attributes.position.values
  432. );
  433. geometry.indices = IndexDatatype.IndexDatatype.createTypedArray(
  434. geometry.attributes.position.values.length / 3,
  435. geometry.indices
  436. );
  437. const attributes = geometry.attributes;
  438. if (!vertexFormat.position) {
  439. delete attributes.position;
  440. }
  441. return new GeometryAttribute.Geometry({
  442. attributes: attributes,
  443. indices: geometry.indices,
  444. primitiveType: geometry.primitiveType,
  445. boundingSphere: boundingSphere,
  446. });
  447. };
  448. function createCoplanarPolygonGeometry(polygonGeometry, offset) {
  449. if (defaultValue.defined(offset)) {
  450. polygonGeometry = CoplanarPolygonGeometry.unpack(polygonGeometry, offset);
  451. }
  452. return CoplanarPolygonGeometry.createGeometry(polygonGeometry);
  453. }
  454. return createCoplanarPolygonGeometry;
  455. }));