createCoplanarPolygonGeometry.js 19 KB

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