CoplanarPolygonGeometry.js 17 KB

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