createCoplanarPolygonGeometry.js 22 KB

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