createCoplanarPolygonOutlineGeometry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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', './GeometryInstance-93a01b5d', './arrayRemoveDuplicates-f0b089b1', './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, GeometryInstance, arrayRemoveDuplicates, EllipsoidTangentPlane, OrientedBoundingBox, CoplanarPolygonGeometryLibrary, ArcType, EllipsoidRhumbLine, earcut2_2_1, PolygonPipeline, PolygonGeometryLibrary) { 'use strict';
  24. function createGeometryFromPositions(positions){
  25. var length = positions.length;
  26. var flatPositions = new Float64Array(length * 3);
  27. var indices = IndexDatatype.IndexDatatype.createTypedArray(length, length * 2);
  28. var positionIndex = 0;
  29. var index = 0;
  30. for (var i = 0; i < length; i++) {
  31. var position = positions[i];
  32. flatPositions[positionIndex++] = position.x;
  33. flatPositions[positionIndex++] = position.y;
  34. flatPositions[positionIndex++] = position.z;
  35. indices[index++] = i;
  36. indices[index++] = (i + 1) % length;
  37. }
  38. var attributes = new GeometryAttributes.GeometryAttributes({
  39. position: new GeometryAttribute.GeometryAttribute({
  40. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  41. componentsPerAttribute : 3,
  42. values : flatPositions
  43. })
  44. });
  45. return new GeometryAttribute.Geometry({
  46. attributes : attributes,
  47. indices : indices,
  48. primitiveType : PrimitiveType.PrimitiveType.LINES
  49. });
  50. }
  51. /**
  52. * A description of the outline of a polygon composed of arbitrary coplanar positions.
  53. *
  54. * @alias CoplanarPolygonOutlineGeometry
  55. * @constructor
  56. *
  57. * @param {Object} options Object with the following properties:
  58. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  59. *
  60. * @see CoplanarPolygonOutlineGeometry.createGeometry
  61. *
  62. * @example
  63. * var polygonOutline = new Cesium.CoplanarPolygonOutlineGeometry({
  64. * positions : Cesium.Cartesian3.fromDegreesArrayHeights([
  65. * -90.0, 30.0, 0.0,
  66. * -90.0, 30.0, 1000.0,
  67. * -80.0, 30.0, 1000.0,
  68. * -80.0, 30.0, 0.0
  69. * ])
  70. * });
  71. * var geometry = Cesium.CoplanarPolygonOutlineGeometry.createGeometry(polygonOutline);
  72. */
  73. function CoplanarPolygonOutlineGeometry(options) {
  74. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  75. var polygonHierarchy = options.polygonHierarchy;
  76. //>>includeStart('debug', pragmas.debug);
  77. Check.Check.defined('options.polygonHierarchy', polygonHierarchy);
  78. //>>includeEnd('debug');
  79. this._polygonHierarchy = polygonHierarchy;
  80. this._workerName = 'createCoplanarPolygonOutlineGeometry';
  81. /**
  82. * The number of elements used to pack the object into an array.
  83. * @type {Number}
  84. */
  85. this.packedLength = PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) + 1;
  86. }
  87. /**
  88. * A description of a coplanar polygon outline from an array of positions.
  89. *
  90. * @param {Object} options Object with the following properties:
  91. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  92. * @returns {CoplanarPolygonOutlineGeometry}
  93. */
  94. CoplanarPolygonOutlineGeometry.fromPositions = function(options) {
  95. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  96. //>>includeStart('debug', pragmas.debug);
  97. Check.Check.defined('options.positions', options.positions);
  98. //>>includeEnd('debug');
  99. var newOptions = {
  100. polygonHierarchy : {
  101. positions : options.positions
  102. }
  103. };
  104. return new CoplanarPolygonOutlineGeometry(newOptions);
  105. };
  106. /**
  107. * Stores the provided instance into the provided array.
  108. *
  109. * @param {CoplanarPolygonOutlineGeometry} value The value to pack.
  110. * @param {Number[]} array The array to pack into.
  111. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  112. *
  113. * @returns {Number[]} The array that was packed into
  114. */
  115. CoplanarPolygonOutlineGeometry.pack = function(value, array, startingIndex) {
  116. //>>includeStart('debug', pragmas.debug);
  117. Check.Check.typeOf.object('value', value);
  118. Check.Check.defined('array', array);
  119. //>>includeEnd('debug');
  120. startingIndex = when.defaultValue(startingIndex, 0);
  121. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(value._polygonHierarchy, array, startingIndex);
  122. array[startingIndex] = value.packedLength;
  123. return array;
  124. };
  125. var scratchOptions = {
  126. polygonHierarchy : {}
  127. };
  128. /**
  129. * Retrieves an instance from a packed array.
  130. *
  131. * @param {Number[]} array The packed array.
  132. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  133. * @param {CoplanarPolygonOutlineGeometry} [result] The object into which to store the result.
  134. * @returns {CoplanarPolygonOutlineGeometry} The modified result parameter or a new CoplanarPolygonOutlineGeometry instance if one was not provided.
  135. */
  136. CoplanarPolygonOutlineGeometry.unpack = function(array, startingIndex, result) {
  137. //>>includeStart('debug', pragmas.debug);
  138. Check.Check.defined('array', array);
  139. //>>includeEnd('debug');
  140. startingIndex = when.defaultValue(startingIndex, 0);
  141. var polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(array, startingIndex);
  142. startingIndex = polygonHierarchy.startingIndex;
  143. delete polygonHierarchy.startingIndex;
  144. var packedLength = array[startingIndex];
  145. if (!when.defined(result)) {
  146. result = new CoplanarPolygonOutlineGeometry(scratchOptions);
  147. }
  148. result._polygonHierarchy = polygonHierarchy;
  149. result.packedLength = packedLength;
  150. return result;
  151. };
  152. /**
  153. * Computes the geometric representation of an arbitrary coplanar polygon, including its vertices, indices, and a bounding sphere.
  154. *
  155. * @param {CoplanarPolygonOutlineGeometry} polygonGeometry A description of the polygon.
  156. * @returns {Geometry|undefined} The computed vertices and indices.
  157. */
  158. CoplanarPolygonOutlineGeometry.createGeometry = function(polygonGeometry) {
  159. var polygonHierarchy = polygonGeometry._polygonHierarchy;
  160. var outerPositions = polygonHierarchy.positions;
  161. outerPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(outerPositions, Cartographic.Cartesian3.equalsEpsilon, true);
  162. if (outerPositions.length < 3) {
  163. return;
  164. }
  165. var isValid = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.validOutline(outerPositions);
  166. if (!isValid) {
  167. return undefined;
  168. }
  169. var polygons = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonOutlinesFromHierarchy(polygonHierarchy, false);
  170. if (polygons.length === 0) {
  171. return undefined;
  172. }
  173. var geometries = [];
  174. for (var i = 0; i < polygons.length; i++) {
  175. var geometryInstance = new GeometryInstance.GeometryInstance({
  176. geometry : createGeometryFromPositions(polygons[i])
  177. });
  178. geometries.push(geometryInstance);
  179. }
  180. var geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
  181. var boundingSphere = BoundingSphere.BoundingSphere.fromPoints(polygonHierarchy.positions);
  182. return new GeometryAttribute.Geometry({
  183. attributes : geometry.attributes,
  184. indices : geometry.indices,
  185. primitiveType : geometry.primitiveType,
  186. boundingSphere : boundingSphere
  187. });
  188. };
  189. function createCoplanarPolygonOutlineGeometry(polygonGeometry, offset) {
  190. if (when.defined(offset)) {
  191. polygonGeometry = CoplanarPolygonOutlineGeometry.unpack(polygonGeometry, offset);
  192. }
  193. polygonGeometry._ellipsoid = Cartesian2.Ellipsoid.clone(polygonGeometry._ellipsoid);
  194. return CoplanarPolygonOutlineGeometry.createGeometry(polygonGeometry);
  195. }
  196. return createCoplanarPolygonOutlineGeometry;
  197. });