createCoplanarPolygonGeometry.js 18 KB

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