createPolygonGeometry.js 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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', './GeometryOffsetAttribute-9c46b133', './VertexFormat-2df57ea4', './GeometryInstance-252acf0f', './arrayRemoveDuplicates-1ded18d8', './BoundingRectangle-cb9ae01b', './EllipsoidTangentPlane-85280670', './ArcType-13a53523', './EllipsoidRhumbLine-5f1ab81f', './PolygonPipeline-197b7d5c', './PolygonGeometryLibrary-a0562073', './EllipsoidGeodesic-2a1a77d2'], function (when, Check, _Math, Cartesian2, Transforms, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, GeometryAttributes, AttributeCompression, GeometryPipeline, EncodedCartesian3, IndexDatatype, IntersectionTests, Plane, GeometryOffsetAttribute, VertexFormat, GeometryInstance, arrayRemoveDuplicates, BoundingRectangle, EllipsoidTangentPlane, ArcType, EllipsoidRhumbLine, PolygonPipeline, PolygonGeometryLibrary, EllipsoidGeodesic) { 'use strict';
  3. var scratchCarto1 = new Cartesian2.Cartographic();
  4. var scratchCarto2 = new Cartesian2.Cartographic();
  5. function adjustPosHeightsForNormal(position, p1, p2, ellipsoid) {
  6. var carto1 = ellipsoid.cartesianToCartographic(position, scratchCarto1);
  7. var height = carto1.height;
  8. var p1Carto = ellipsoid.cartesianToCartographic(p1, scratchCarto2);
  9. p1Carto.height = height;
  10. ellipsoid.cartographicToCartesian(p1Carto, p1);
  11. var p2Carto = ellipsoid.cartesianToCartographic(p2, scratchCarto2);
  12. p2Carto.height = height - 100;
  13. ellipsoid.cartographicToCartesian(p2Carto, p2);
  14. }
  15. var scratchBoundingRectangle = new BoundingRectangle.BoundingRectangle();
  16. var scratchPosition = new Cartesian2.Cartesian3();
  17. var scratchNormal = new Cartesian2.Cartesian3();
  18. var scratchTangent = new Cartesian2.Cartesian3();
  19. var scratchBitangent = new Cartesian2.Cartesian3();
  20. var p1Scratch = new Cartesian2.Cartesian3();
  21. var p2Scratch = new Cartesian2.Cartesian3();
  22. var scratchPerPosNormal = new Cartesian2.Cartesian3();
  23. var scratchPerPosTangent = new Cartesian2.Cartesian3();
  24. var scratchPerPosBitangent = new Cartesian2.Cartesian3();
  25. var appendTextureCoordinatesOrigin = new Cartesian2.Cartesian2();
  26. var appendTextureCoordinatesCartesian2 = new Cartesian2.Cartesian2();
  27. var appendTextureCoordinatesCartesian3 = new Cartesian2.Cartesian3();
  28. var appendTextureCoordinatesQuaternion = new Transforms.Quaternion();
  29. var appendTextureCoordinatesMatrix3 = new Transforms.Matrix3();
  30. var tangentMatrixScratch = new Transforms.Matrix3();
  31. function computeAttributes(options) {
  32. var vertexFormat = options.vertexFormat;
  33. var geometry = options.geometry;
  34. var shadowVolume = options.shadowVolume;
  35. var flatPositions = geometry.attributes.position.values;
  36. var length = flatPositions.length;
  37. var wall = options.wall;
  38. var top = options.top || wall;
  39. var bottom = options.bottom || wall;
  40. if (
  41. vertexFormat.st ||
  42. vertexFormat.normal ||
  43. vertexFormat.tangent ||
  44. vertexFormat.bitangent ||
  45. shadowVolume
  46. ) {
  47. // PERFORMANCE_IDEA: Compute before subdivision, then just interpolate during subdivision.
  48. // PERFORMANCE_IDEA: Compute with createGeometryFromPositions() for fast path when there's no holes.
  49. var boundingRectangle = options.boundingRectangle;
  50. var tangentPlane = options.tangentPlane;
  51. var ellipsoid = options.ellipsoid;
  52. var stRotation = options.stRotation;
  53. var perPositionHeight = options.perPositionHeight;
  54. var origin = appendTextureCoordinatesOrigin;
  55. origin.x = boundingRectangle.x;
  56. origin.y = boundingRectangle.y;
  57. var textureCoordinates = vertexFormat.st
  58. ? new Float32Array(2 * (length / 3))
  59. : undefined;
  60. var normals;
  61. if (vertexFormat.normal) {
  62. if (perPositionHeight && top && !wall) {
  63. normals = geometry.attributes.normal.values;
  64. } else {
  65. normals = new Float32Array(length);
  66. }
  67. }
  68. var tangents = vertexFormat.tangent ? new Float32Array(length) : undefined;
  69. var bitangents = vertexFormat.bitangent
  70. ? new Float32Array(length)
  71. : undefined;
  72. var extrudeNormals = shadowVolume ? new Float32Array(length) : undefined;
  73. var textureCoordIndex = 0;
  74. var attrIndex = 0;
  75. var normal = scratchNormal;
  76. var tangent = scratchTangent;
  77. var bitangent = scratchBitangent;
  78. var recomputeNormal = true;
  79. var textureMatrix = appendTextureCoordinatesMatrix3;
  80. var tangentRotationMatrix = tangentMatrixScratch;
  81. if (stRotation !== 0.0) {
  82. var rotation = Transforms.Quaternion.fromAxisAngle(
  83. tangentPlane._plane.normal,
  84. stRotation,
  85. appendTextureCoordinatesQuaternion
  86. );
  87. textureMatrix = Transforms.Matrix3.fromQuaternion(rotation, textureMatrix);
  88. rotation = Transforms.Quaternion.fromAxisAngle(
  89. tangentPlane._plane.normal,
  90. -stRotation,
  91. appendTextureCoordinatesQuaternion
  92. );
  93. tangentRotationMatrix = Transforms.Matrix3.fromQuaternion(
  94. rotation,
  95. tangentRotationMatrix
  96. );
  97. } else {
  98. textureMatrix = Transforms.Matrix3.clone(Transforms.Matrix3.IDENTITY, textureMatrix);
  99. tangentRotationMatrix = Transforms.Matrix3.clone(
  100. Transforms.Matrix3.IDENTITY,
  101. tangentRotationMatrix
  102. );
  103. }
  104. var bottomOffset = 0;
  105. var bottomOffset2 = 0;
  106. if (top && bottom) {
  107. bottomOffset = length / 2;
  108. bottomOffset2 = length / 3;
  109. length /= 2;
  110. }
  111. for (var i = 0; i < length; i += 3) {
  112. var position = Cartesian2.Cartesian3.fromArray(
  113. flatPositions,
  114. i,
  115. appendTextureCoordinatesCartesian3
  116. );
  117. if (vertexFormat.st) {
  118. var p = Transforms.Matrix3.multiplyByVector(
  119. textureMatrix,
  120. position,
  121. scratchPosition
  122. );
  123. p = ellipsoid.scaleToGeodeticSurface(p, p);
  124. var st = tangentPlane.projectPointOntoPlane(
  125. p,
  126. appendTextureCoordinatesCartesian2
  127. );
  128. Cartesian2.Cartesian2.subtract(st, origin, st);
  129. var stx = _Math.CesiumMath.clamp(st.x / boundingRectangle.width, 0, 1);
  130. var sty = _Math.CesiumMath.clamp(st.y / boundingRectangle.height, 0, 1);
  131. if (bottom) {
  132. textureCoordinates[textureCoordIndex + bottomOffset2] = stx;
  133. textureCoordinates[textureCoordIndex + 1 + bottomOffset2] = sty;
  134. }
  135. if (top) {
  136. textureCoordinates[textureCoordIndex] = stx;
  137. textureCoordinates[textureCoordIndex + 1] = sty;
  138. }
  139. textureCoordIndex += 2;
  140. }
  141. if (
  142. vertexFormat.normal ||
  143. vertexFormat.tangent ||
  144. vertexFormat.bitangent ||
  145. shadowVolume
  146. ) {
  147. var attrIndex1 = attrIndex + 1;
  148. var attrIndex2 = attrIndex + 2;
  149. if (wall) {
  150. if (i + 3 < length) {
  151. var p1 = Cartesian2.Cartesian3.fromArray(flatPositions, i + 3, p1Scratch);
  152. if (recomputeNormal) {
  153. var p2 = Cartesian2.Cartesian3.fromArray(
  154. flatPositions,
  155. i + length,
  156. p2Scratch
  157. );
  158. if (perPositionHeight) {
  159. adjustPosHeightsForNormal(position, p1, p2, ellipsoid);
  160. }
  161. Cartesian2.Cartesian3.subtract(p1, position, p1);
  162. Cartesian2.Cartesian3.subtract(p2, position, p2);
  163. normal = Cartesian2.Cartesian3.normalize(
  164. Cartesian2.Cartesian3.cross(p2, p1, normal),
  165. normal
  166. );
  167. recomputeNormal = false;
  168. }
  169. if (Cartesian2.Cartesian3.equalsEpsilon(p1, position, _Math.CesiumMath.EPSILON10)) {
  170. // if we've reached a corner
  171. recomputeNormal = true;
  172. }
  173. }
  174. if (vertexFormat.tangent || vertexFormat.bitangent) {
  175. bitangent = ellipsoid.geodeticSurfaceNormal(position, bitangent);
  176. if (vertexFormat.tangent) {
  177. tangent = Cartesian2.Cartesian3.normalize(
  178. Cartesian2.Cartesian3.cross(bitangent, normal, tangent),
  179. tangent
  180. );
  181. }
  182. }
  183. } else {
  184. normal = ellipsoid.geodeticSurfaceNormal(position, normal);
  185. if (vertexFormat.tangent || vertexFormat.bitangent) {
  186. if (perPositionHeight) {
  187. scratchPerPosNormal = Cartesian2.Cartesian3.fromArray(
  188. normals,
  189. attrIndex,
  190. scratchPerPosNormal
  191. );
  192. scratchPerPosTangent = Cartesian2.Cartesian3.cross(
  193. Cartesian2.Cartesian3.UNIT_Z,
  194. scratchPerPosNormal,
  195. scratchPerPosTangent
  196. );
  197. scratchPerPosTangent = Cartesian2.Cartesian3.normalize(
  198. Transforms.Matrix3.multiplyByVector(
  199. tangentRotationMatrix,
  200. scratchPerPosTangent,
  201. scratchPerPosTangent
  202. ),
  203. scratchPerPosTangent
  204. );
  205. if (vertexFormat.bitangent) {
  206. scratchPerPosBitangent = Cartesian2.Cartesian3.normalize(
  207. Cartesian2.Cartesian3.cross(
  208. scratchPerPosNormal,
  209. scratchPerPosTangent,
  210. scratchPerPosBitangent
  211. ),
  212. scratchPerPosBitangent
  213. );
  214. }
  215. }
  216. tangent = Cartesian2.Cartesian3.cross(Cartesian2.Cartesian3.UNIT_Z, normal, tangent);
  217. tangent = Cartesian2.Cartesian3.normalize(
  218. Transforms.Matrix3.multiplyByVector(tangentRotationMatrix, tangent, tangent),
  219. tangent
  220. );
  221. if (vertexFormat.bitangent) {
  222. bitangent = Cartesian2.Cartesian3.normalize(
  223. Cartesian2.Cartesian3.cross(normal, tangent, bitangent),
  224. bitangent
  225. );
  226. }
  227. }
  228. }
  229. if (vertexFormat.normal) {
  230. if (options.wall) {
  231. normals[attrIndex + bottomOffset] = normal.x;
  232. normals[attrIndex1 + bottomOffset] = normal.y;
  233. normals[attrIndex2 + bottomOffset] = normal.z;
  234. } else if (bottom) {
  235. normals[attrIndex + bottomOffset] = -normal.x;
  236. normals[attrIndex1 + bottomOffset] = -normal.y;
  237. normals[attrIndex2 + bottomOffset] = -normal.z;
  238. }
  239. if ((top && !perPositionHeight) || wall) {
  240. normals[attrIndex] = normal.x;
  241. normals[attrIndex1] = normal.y;
  242. normals[attrIndex2] = normal.z;
  243. }
  244. }
  245. if (shadowVolume) {
  246. if (wall) {
  247. normal = ellipsoid.geodeticSurfaceNormal(position, normal);
  248. }
  249. extrudeNormals[attrIndex + bottomOffset] = -normal.x;
  250. extrudeNormals[attrIndex1 + bottomOffset] = -normal.y;
  251. extrudeNormals[attrIndex2 + bottomOffset] = -normal.z;
  252. }
  253. if (vertexFormat.tangent) {
  254. if (options.wall) {
  255. tangents[attrIndex + bottomOffset] = tangent.x;
  256. tangents[attrIndex1 + bottomOffset] = tangent.y;
  257. tangents[attrIndex2 + bottomOffset] = tangent.z;
  258. } else if (bottom) {
  259. tangents[attrIndex + bottomOffset] = -tangent.x;
  260. tangents[attrIndex1 + bottomOffset] = -tangent.y;
  261. tangents[attrIndex2 + bottomOffset] = -tangent.z;
  262. }
  263. if (top) {
  264. if (perPositionHeight) {
  265. tangents[attrIndex] = scratchPerPosTangent.x;
  266. tangents[attrIndex1] = scratchPerPosTangent.y;
  267. tangents[attrIndex2] = scratchPerPosTangent.z;
  268. } else {
  269. tangents[attrIndex] = tangent.x;
  270. tangents[attrIndex1] = tangent.y;
  271. tangents[attrIndex2] = tangent.z;
  272. }
  273. }
  274. }
  275. if (vertexFormat.bitangent) {
  276. if (bottom) {
  277. bitangents[attrIndex + bottomOffset] = bitangent.x;
  278. bitangents[attrIndex1 + bottomOffset] = bitangent.y;
  279. bitangents[attrIndex2 + bottomOffset] = bitangent.z;
  280. }
  281. if (top) {
  282. if (perPositionHeight) {
  283. bitangents[attrIndex] = scratchPerPosBitangent.x;
  284. bitangents[attrIndex1] = scratchPerPosBitangent.y;
  285. bitangents[attrIndex2] = scratchPerPosBitangent.z;
  286. } else {
  287. bitangents[attrIndex] = bitangent.x;
  288. bitangents[attrIndex1] = bitangent.y;
  289. bitangents[attrIndex2] = bitangent.z;
  290. }
  291. }
  292. }
  293. attrIndex += 3;
  294. }
  295. }
  296. if (vertexFormat.st) {
  297. geometry.attributes.st = new GeometryAttribute.GeometryAttribute({
  298. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  299. componentsPerAttribute: 2,
  300. values: textureCoordinates,
  301. });
  302. }
  303. if (vertexFormat.normal) {
  304. geometry.attributes.normal = new GeometryAttribute.GeometryAttribute({
  305. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  306. componentsPerAttribute: 3,
  307. values: normals,
  308. });
  309. }
  310. if (vertexFormat.tangent) {
  311. geometry.attributes.tangent = new GeometryAttribute.GeometryAttribute({
  312. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  313. componentsPerAttribute: 3,
  314. values: tangents,
  315. });
  316. }
  317. if (vertexFormat.bitangent) {
  318. geometry.attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  319. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  320. componentsPerAttribute: 3,
  321. values: bitangents,
  322. });
  323. }
  324. if (shadowVolume) {
  325. geometry.attributes.extrudeDirection = new GeometryAttribute.GeometryAttribute({
  326. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  327. componentsPerAttribute: 3,
  328. values: extrudeNormals,
  329. });
  330. }
  331. }
  332. if (options.extrude && when.defined(options.offsetAttribute)) {
  333. var size = flatPositions.length / 3;
  334. var offsetAttribute = new Uint8Array(size);
  335. if (options.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP) {
  336. if ((top && bottom) || wall) {
  337. offsetAttribute = GeometryOffsetAttribute.arrayFill(offsetAttribute, 1, 0, size / 2);
  338. } else if (top) {
  339. offsetAttribute = GeometryOffsetAttribute.arrayFill(offsetAttribute, 1);
  340. }
  341. } else {
  342. var offsetValue =
  343. options.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE ? 0 : 1;
  344. offsetAttribute = GeometryOffsetAttribute.arrayFill(offsetAttribute, offsetValue);
  345. }
  346. geometry.attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  347. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  348. componentsPerAttribute: 1,
  349. values: offsetAttribute,
  350. });
  351. }
  352. return geometry;
  353. }
  354. var startCartographicScratch = new Cartesian2.Cartographic();
  355. var endCartographicScratch = new Cartesian2.Cartographic();
  356. var idlCross = {
  357. westOverIDL: 0.0,
  358. eastOverIDL: 0.0,
  359. };
  360. var ellipsoidGeodesic = new EllipsoidGeodesic.EllipsoidGeodesic();
  361. function computeRectangle(positions, ellipsoid, arcType, granularity, result) {
  362. result = when.defaultValue(result, new Cartesian2.Rectangle());
  363. if (!when.defined(positions) || positions.length < 3) {
  364. result.west = 0.0;
  365. result.north = 0.0;
  366. result.south = 0.0;
  367. result.east = 0.0;
  368. return result;
  369. }
  370. if (arcType === ArcType.ArcType.RHUMB) {
  371. return Cartesian2.Rectangle.fromCartesianArray(positions, ellipsoid, result);
  372. }
  373. if (!ellipsoidGeodesic.ellipsoid.equals(ellipsoid)) {
  374. ellipsoidGeodesic = new EllipsoidGeodesic.EllipsoidGeodesic(undefined, undefined, ellipsoid);
  375. }
  376. result.west = Number.POSITIVE_INFINITY;
  377. result.east = Number.NEGATIVE_INFINITY;
  378. result.south = Number.POSITIVE_INFINITY;
  379. result.north = Number.NEGATIVE_INFINITY;
  380. idlCross.westOverIDL = Number.POSITIVE_INFINITY;
  381. idlCross.eastOverIDL = Number.NEGATIVE_INFINITY;
  382. var inverseChordLength =
  383. 1.0 / _Math.CesiumMath.chordLength(granularity, ellipsoid.maximumRadius);
  384. var positionsLength = positions.length;
  385. var endCartographic = ellipsoid.cartesianToCartographic(
  386. positions[0],
  387. endCartographicScratch
  388. );
  389. var startCartographic = startCartographicScratch;
  390. var swap;
  391. for (var i = 1; i < positionsLength; i++) {
  392. swap = startCartographic;
  393. startCartographic = endCartographic;
  394. endCartographic = ellipsoid.cartesianToCartographic(positions[i], swap);
  395. ellipsoidGeodesic.setEndPoints(startCartographic, endCartographic);
  396. interpolateAndGrowRectangle(
  397. ellipsoidGeodesic,
  398. inverseChordLength,
  399. result,
  400. idlCross
  401. );
  402. }
  403. swap = startCartographic;
  404. startCartographic = endCartographic;
  405. endCartographic = ellipsoid.cartesianToCartographic(positions[0], swap);
  406. ellipsoidGeodesic.setEndPoints(startCartographic, endCartographic);
  407. interpolateAndGrowRectangle(
  408. ellipsoidGeodesic,
  409. inverseChordLength,
  410. result,
  411. idlCross
  412. );
  413. if (result.east - result.west > idlCross.eastOverIDL - idlCross.westOverIDL) {
  414. result.west = idlCross.westOverIDL;
  415. result.east = idlCross.eastOverIDL;
  416. if (result.east > _Math.CesiumMath.PI) {
  417. result.east = result.east - _Math.CesiumMath.TWO_PI;
  418. }
  419. if (result.west > _Math.CesiumMath.PI) {
  420. result.west = result.west - _Math.CesiumMath.TWO_PI;
  421. }
  422. }
  423. return result;
  424. }
  425. var interpolatedCartographicScratch = new Cartesian2.Cartographic();
  426. function interpolateAndGrowRectangle(
  427. ellipsoidGeodesic,
  428. inverseChordLength,
  429. result,
  430. idlCross
  431. ) {
  432. var segmentLength = ellipsoidGeodesic.surfaceDistance;
  433. var numPoints = Math.ceil(segmentLength * inverseChordLength);
  434. var subsegmentDistance =
  435. numPoints > 0 ? segmentLength / (numPoints - 1) : Number.POSITIVE_INFINITY;
  436. var interpolationDistance = 0.0;
  437. for (var i = 0; i < numPoints; i++) {
  438. var interpolatedCartographic = ellipsoidGeodesic.interpolateUsingSurfaceDistance(
  439. interpolationDistance,
  440. interpolatedCartographicScratch
  441. );
  442. interpolationDistance += subsegmentDistance;
  443. var longitude = interpolatedCartographic.longitude;
  444. var latitude = interpolatedCartographic.latitude;
  445. result.west = Math.min(result.west, longitude);
  446. result.east = Math.max(result.east, longitude);
  447. result.south = Math.min(result.south, latitude);
  448. result.north = Math.max(result.north, latitude);
  449. var lonAdjusted =
  450. longitude >= 0 ? longitude : longitude + _Math.CesiumMath.TWO_PI;
  451. idlCross.westOverIDL = Math.min(idlCross.westOverIDL, lonAdjusted);
  452. idlCross.eastOverIDL = Math.max(idlCross.eastOverIDL, lonAdjusted);
  453. }
  454. }
  455. var createGeometryFromPositionsExtrudedPositions = [];
  456. function createGeometryFromPositionsExtruded(
  457. ellipsoid,
  458. polygon,
  459. granularity,
  460. hierarchy,
  461. perPositionHeight,
  462. closeTop,
  463. closeBottom,
  464. vertexFormat,
  465. arcType
  466. ) {
  467. var geos = {
  468. walls: [],
  469. };
  470. var i;
  471. if (closeTop || closeBottom) {
  472. var topGeo = PolygonGeometryLibrary.PolygonGeometryLibrary.createGeometryFromPositions(
  473. ellipsoid,
  474. polygon,
  475. granularity,
  476. perPositionHeight,
  477. vertexFormat,
  478. arcType
  479. );
  480. var edgePoints = topGeo.attributes.position.values;
  481. var indices = topGeo.indices;
  482. var numPositions;
  483. var newIndices;
  484. if (closeTop && closeBottom) {
  485. var topBottomPositions = edgePoints.concat(edgePoints);
  486. numPositions = topBottomPositions.length / 3;
  487. newIndices = IndexDatatype.IndexDatatype.createTypedArray(
  488. numPositions,
  489. indices.length * 2
  490. );
  491. newIndices.set(indices);
  492. var ilength = indices.length;
  493. var length = numPositions / 2;
  494. for (i = 0; i < ilength; i += 3) {
  495. var i0 = newIndices[i] + length;
  496. var i1 = newIndices[i + 1] + length;
  497. var i2 = newIndices[i + 2] + length;
  498. newIndices[i + ilength] = i2;
  499. newIndices[i + 1 + ilength] = i1;
  500. newIndices[i + 2 + ilength] = i0;
  501. }
  502. topGeo.attributes.position.values = topBottomPositions;
  503. if (perPositionHeight && vertexFormat.normal) {
  504. var normals = topGeo.attributes.normal.values;
  505. topGeo.attributes.normal.values = new Float32Array(
  506. topBottomPositions.length
  507. );
  508. topGeo.attributes.normal.values.set(normals);
  509. }
  510. topGeo.indices = newIndices;
  511. } else if (closeBottom) {
  512. numPositions = edgePoints.length / 3;
  513. newIndices = IndexDatatype.IndexDatatype.createTypedArray(numPositions, indices.length);
  514. for (i = 0; i < indices.length; i += 3) {
  515. newIndices[i] = indices[i + 2];
  516. newIndices[i + 1] = indices[i + 1];
  517. newIndices[i + 2] = indices[i];
  518. }
  519. topGeo.indices = newIndices;
  520. }
  521. geos.topAndBottom = new GeometryInstance.GeometryInstance({
  522. geometry: topGeo,
  523. });
  524. }
  525. var outerRing = hierarchy.outerRing;
  526. var tangentPlane = EllipsoidTangentPlane.EllipsoidTangentPlane.fromPoints(outerRing, ellipsoid);
  527. var positions2D = tangentPlane.projectPointsOntoPlane(
  528. outerRing,
  529. createGeometryFromPositionsExtrudedPositions
  530. );
  531. var windingOrder = PolygonPipeline.PolygonPipeline.computeWindingOrder2D(positions2D);
  532. if (windingOrder === PolygonPipeline.WindingOrder.CLOCKWISE) {
  533. outerRing = outerRing.slice().reverse();
  534. }
  535. var wallGeo = PolygonGeometryLibrary.PolygonGeometryLibrary.computeWallGeometry(
  536. outerRing,
  537. ellipsoid,
  538. granularity,
  539. perPositionHeight,
  540. arcType
  541. );
  542. geos.walls.push(
  543. new GeometryInstance.GeometryInstance({
  544. geometry: wallGeo,
  545. })
  546. );
  547. var holes = hierarchy.holes;
  548. for (i = 0; i < holes.length; i++) {
  549. var hole = holes[i];
  550. tangentPlane = EllipsoidTangentPlane.EllipsoidTangentPlane.fromPoints(hole, ellipsoid);
  551. positions2D = tangentPlane.projectPointsOntoPlane(
  552. hole,
  553. createGeometryFromPositionsExtrudedPositions
  554. );
  555. windingOrder = PolygonPipeline.PolygonPipeline.computeWindingOrder2D(positions2D);
  556. if (windingOrder === PolygonPipeline.WindingOrder.COUNTER_CLOCKWISE) {
  557. hole = hole.slice().reverse();
  558. }
  559. wallGeo = PolygonGeometryLibrary.PolygonGeometryLibrary.computeWallGeometry(
  560. hole,
  561. ellipsoid,
  562. granularity,
  563. perPositionHeight,
  564. arcType
  565. );
  566. geos.walls.push(
  567. new GeometryInstance.GeometryInstance({
  568. geometry: wallGeo,
  569. })
  570. );
  571. }
  572. return geos;
  573. }
  574. /**
  575. * A description of a polygon on the ellipsoid. The polygon is defined by a polygon hierarchy. Polygon geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  576. *
  577. * @alias PolygonGeometry
  578. * @constructor
  579. *
  580. * @param {Object} options Object with the following properties:
  581. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  582. * @param {Number} [options.height=0.0] The distance in meters between the polygon and the ellipsoid surface.
  583. * @param {Number} [options.extrudedHeight] The distance in meters between the polygon's extruded face and the ellipsoid surface.
  584. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  585. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  586. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  587. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  588. * @param {Boolean} [options.perPositionHeight=false] Use the height of options.positions for each position instead of using options.height to determine the height.
  589. * @param {Boolean} [options.closeTop=true] When false, leaves off the top of an extruded polygon open.
  590. * @param {Boolean} [options.closeBottom=true] When false, leaves off the bottom of an extruded polygon open.
  591. * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of line the polygon edges must follow. Valid options are {@link ArcType.GEODESIC} and {@link ArcType.RHUMB}.
  592. *
  593. * @see PolygonGeometry#createGeometry
  594. * @see PolygonGeometry#fromPositions
  595. *
  596. * @demo {@link https://sandcastle.cesium.com/index.html?src=Polygon.html|Cesium Sandcastle Polygon Demo}
  597. *
  598. * @example
  599. * // 1. create a polygon from points
  600. * var polygon = new Cesium.PolygonGeometry({
  601. * polygonHierarchy : new Cesium.PolygonHierarchy(
  602. * Cesium.Cartesian3.fromDegreesArray([
  603. * -72.0, 40.0,
  604. * -70.0, 35.0,
  605. * -75.0, 30.0,
  606. * -70.0, 30.0,
  607. * -68.0, 40.0
  608. * ])
  609. * )
  610. * });
  611. * var geometry = Cesium.PolygonGeometry.createGeometry(polygon);
  612. *
  613. * // 2. create a nested polygon with holes
  614. * var polygonWithHole = new Cesium.PolygonGeometry({
  615. * polygonHierarchy : new Cesium.PolygonHierarchy(
  616. * Cesium.Cartesian3.fromDegreesArray([
  617. * -109.0, 30.0,
  618. * -95.0, 30.0,
  619. * -95.0, 40.0,
  620. * -109.0, 40.0
  621. * ]),
  622. * [new Cesium.PolygonHierarchy(
  623. * Cesium.Cartesian3.fromDegreesArray([
  624. * -107.0, 31.0,
  625. * -107.0, 39.0,
  626. * -97.0, 39.0,
  627. * -97.0, 31.0
  628. * ]),
  629. * [new Cesium.PolygonHierarchy(
  630. * Cesium.Cartesian3.fromDegreesArray([
  631. * -105.0, 33.0,
  632. * -99.0, 33.0,
  633. * -99.0, 37.0,
  634. * -105.0, 37.0
  635. * ]),
  636. * [new Cesium.PolygonHierarchy(
  637. * Cesium.Cartesian3.fromDegreesArray([
  638. * -103.0, 34.0,
  639. * -101.0, 34.0,
  640. * -101.0, 36.0,
  641. * -103.0, 36.0
  642. * ])
  643. * )]
  644. * )]
  645. * )]
  646. * )
  647. * });
  648. * var geometry = Cesium.PolygonGeometry.createGeometry(polygonWithHole);
  649. *
  650. * // 3. create extruded polygon
  651. * var extrudedPolygon = new Cesium.PolygonGeometry({
  652. * polygonHierarchy : new Cesium.PolygonHierarchy(
  653. * Cesium.Cartesian3.fromDegreesArray([
  654. * -72.0, 40.0,
  655. * -70.0, 35.0,
  656. * -75.0, 30.0,
  657. * -70.0, 30.0,
  658. * -68.0, 40.0
  659. * ])
  660. * ),
  661. * extrudedHeight: 300000
  662. * });
  663. * var geometry = Cesium.PolygonGeometry.createGeometry(extrudedPolygon);
  664. */
  665. function PolygonGeometry(options) {
  666. //>>includeStart('debug', pragmas.debug);
  667. Check.Check.typeOf.object("options", options);
  668. Check.Check.typeOf.object("options.polygonHierarchy", options.polygonHierarchy);
  669. if (
  670. when.defined(options.perPositionHeight) &&
  671. options.perPositionHeight &&
  672. when.defined(options.height)
  673. ) {
  674. throw new Check.DeveloperError(
  675. "Cannot use both options.perPositionHeight and options.height"
  676. );
  677. }
  678. if (
  679. when.defined(options.arcType) &&
  680. options.arcType !== ArcType.ArcType.GEODESIC &&
  681. options.arcType !== ArcType.ArcType.RHUMB
  682. ) {
  683. throw new Check.DeveloperError(
  684. "Invalid arcType. Valid options are ArcType.GEODESIC and ArcType.RHUMB."
  685. );
  686. }
  687. //>>includeEnd('debug');
  688. var polygonHierarchy = options.polygonHierarchy;
  689. var vertexFormat = when.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT);
  690. var ellipsoid = when.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84);
  691. var granularity = when.defaultValue(
  692. options.granularity,
  693. _Math.CesiumMath.RADIANS_PER_DEGREE
  694. );
  695. var stRotation = when.defaultValue(options.stRotation, 0.0);
  696. var perPositionHeight = when.defaultValue(options.perPositionHeight, false);
  697. var perPositionHeightExtrude =
  698. perPositionHeight && when.defined(options.extrudedHeight);
  699. var height = when.defaultValue(options.height, 0.0);
  700. var extrudedHeight = when.defaultValue(options.extrudedHeight, height);
  701. if (!perPositionHeightExtrude) {
  702. var h = Math.max(height, extrudedHeight);
  703. extrudedHeight = Math.min(height, extrudedHeight);
  704. height = h;
  705. }
  706. this._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat);
  707. this._ellipsoid = Cartesian2.Ellipsoid.clone(ellipsoid);
  708. this._granularity = granularity;
  709. this._stRotation = stRotation;
  710. this._height = height;
  711. this._extrudedHeight = extrudedHeight;
  712. this._closeTop = when.defaultValue(options.closeTop, true);
  713. this._closeBottom = when.defaultValue(options.closeBottom, true);
  714. this._polygonHierarchy = polygonHierarchy;
  715. this._perPositionHeight = perPositionHeight;
  716. this._perPositionHeightExtrude = perPositionHeightExtrude;
  717. this._shadowVolume = when.defaultValue(options.shadowVolume, false);
  718. this._workerName = "createPolygonGeometry";
  719. this._offsetAttribute = options.offsetAttribute;
  720. this._arcType = when.defaultValue(options.arcType, ArcType.ArcType.GEODESIC);
  721. this._rectangle = undefined;
  722. this._textureCoordinateRotationPoints = undefined;
  723. /**
  724. * The number of elements used to pack the object into an array.
  725. * @type {Number}
  726. */
  727. this.packedLength =
  728. PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) +
  729. Cartesian2.Ellipsoid.packedLength +
  730. VertexFormat.VertexFormat.packedLength +
  731. 12;
  732. }
  733. /**
  734. * A description of a polygon from an array of positions. Polygon geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  735. *
  736. * @param {Object} options Object with the following properties:
  737. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  738. * @param {Number} [options.height=0.0] The height of the polygon.
  739. * @param {Number} [options.extrudedHeight] The height of the polygon extrusion.
  740. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  741. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  742. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  743. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  744. * @param {Boolean} [options.perPositionHeight=false] Use the height of options.positions for each position instead of using options.height to determine the height.
  745. * @param {Boolean} [options.closeTop=true] When false, leaves off the top of an extruded polygon open.
  746. * @param {Boolean} [options.closeBottom=true] When false, leaves off the bottom of an extruded polygon open.
  747. * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of line the polygon edges must follow. Valid options are {@link ArcType.GEODESIC} and {@link ArcType.RHUMB}.
  748. * @returns {PolygonGeometry}
  749. *
  750. *
  751. * @example
  752. * // create a polygon from points
  753. * var polygon = Cesium.PolygonGeometry.fromPositions({
  754. * positions : Cesium.Cartesian3.fromDegreesArray([
  755. * -72.0, 40.0,
  756. * -70.0, 35.0,
  757. * -75.0, 30.0,
  758. * -70.0, 30.0,
  759. * -68.0, 40.0
  760. * ])
  761. * });
  762. * var geometry = Cesium.PolygonGeometry.createGeometry(polygon);
  763. *
  764. * @see PolygonGeometry#createGeometry
  765. */
  766. PolygonGeometry.fromPositions = function (options) {
  767. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  768. //>>includeStart('debug', pragmas.debug);
  769. Check.Check.defined("options.positions", options.positions);
  770. //>>includeEnd('debug');
  771. var newOptions = {
  772. polygonHierarchy: {
  773. positions: options.positions,
  774. },
  775. height: options.height,
  776. extrudedHeight: options.extrudedHeight,
  777. vertexFormat: options.vertexFormat,
  778. stRotation: options.stRotation,
  779. ellipsoid: options.ellipsoid,
  780. granularity: options.granularity,
  781. perPositionHeight: options.perPositionHeight,
  782. closeTop: options.closeTop,
  783. closeBottom: options.closeBottom,
  784. offsetAttribute: options.offsetAttribute,
  785. arcType: options.arcType,
  786. };
  787. return new PolygonGeometry(newOptions);
  788. };
  789. /**
  790. * Stores the provided instance into the provided array.
  791. *
  792. * @param {PolygonGeometry} value The value to pack.
  793. * @param {Number[]} array The array to pack into.
  794. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  795. *
  796. * @returns {Number[]} The array that was packed into
  797. */
  798. PolygonGeometry.pack = function (value, array, startingIndex) {
  799. //>>includeStart('debug', pragmas.debug);
  800. Check.Check.typeOf.object("value", value);
  801. Check.Check.defined("array", array);
  802. //>>includeEnd('debug');
  803. startingIndex = when.defaultValue(startingIndex, 0);
  804. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(
  805. value._polygonHierarchy,
  806. array,
  807. startingIndex
  808. );
  809. Cartesian2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  810. startingIndex += Cartesian2.Ellipsoid.packedLength;
  811. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  812. startingIndex += VertexFormat.VertexFormat.packedLength;
  813. array[startingIndex++] = value._height;
  814. array[startingIndex++] = value._extrudedHeight;
  815. array[startingIndex++] = value._granularity;
  816. array[startingIndex++] = value._stRotation;
  817. array[startingIndex++] = value._perPositionHeightExtrude ? 1.0 : 0.0;
  818. array[startingIndex++] = value._perPositionHeight ? 1.0 : 0.0;
  819. array[startingIndex++] = value._closeTop ? 1.0 : 0.0;
  820. array[startingIndex++] = value._closeBottom ? 1.0 : 0.0;
  821. array[startingIndex++] = value._shadowVolume ? 1.0 : 0.0;
  822. array[startingIndex++] = when.defaultValue(value._offsetAttribute, -1);
  823. array[startingIndex++] = value._arcType;
  824. array[startingIndex] = value.packedLength;
  825. return array;
  826. };
  827. var scratchEllipsoid = Cartesian2.Ellipsoid.clone(Cartesian2.Ellipsoid.UNIT_SPHERE);
  828. var scratchVertexFormat = new VertexFormat.VertexFormat();
  829. //Only used to avoid inability to default construct.
  830. var dummyOptions = {
  831. polygonHierarchy: {},
  832. };
  833. /**
  834. * Retrieves an instance from a packed array.
  835. *
  836. * @param {Number[]} array The packed array.
  837. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  838. * @param {PolygonGeometry} [result] The object into which to store the result.
  839. */
  840. PolygonGeometry.unpack = function (array, startingIndex, result) {
  841. //>>includeStart('debug', pragmas.debug);
  842. Check.Check.defined("array", array);
  843. //>>includeEnd('debug');
  844. startingIndex = when.defaultValue(startingIndex, 0);
  845. var polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(
  846. array,
  847. startingIndex
  848. );
  849. startingIndex = polygonHierarchy.startingIndex;
  850. delete polygonHierarchy.startingIndex;
  851. var ellipsoid = Cartesian2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  852. startingIndex += Cartesian2.Ellipsoid.packedLength;
  853. var vertexFormat = VertexFormat.VertexFormat.unpack(
  854. array,
  855. startingIndex,
  856. scratchVertexFormat
  857. );
  858. startingIndex += VertexFormat.VertexFormat.packedLength;
  859. var height = array[startingIndex++];
  860. var extrudedHeight = array[startingIndex++];
  861. var granularity = array[startingIndex++];
  862. var stRotation = array[startingIndex++];
  863. var perPositionHeightExtrude = array[startingIndex++] === 1.0;
  864. var perPositionHeight = array[startingIndex++] === 1.0;
  865. var closeTop = array[startingIndex++] === 1.0;
  866. var closeBottom = array[startingIndex++] === 1.0;
  867. var shadowVolume = array[startingIndex++] === 1.0;
  868. var offsetAttribute = array[startingIndex++];
  869. var arcType = array[startingIndex++];
  870. var packedLength = array[startingIndex];
  871. if (!when.defined(result)) {
  872. result = new PolygonGeometry(dummyOptions);
  873. }
  874. result._polygonHierarchy = polygonHierarchy;
  875. result._ellipsoid = Cartesian2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  876. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  877. result._height = height;
  878. result._extrudedHeight = extrudedHeight;
  879. result._granularity = granularity;
  880. result._stRotation = stRotation;
  881. result._perPositionHeightExtrude = perPositionHeightExtrude;
  882. result._perPositionHeight = perPositionHeight;
  883. result._closeTop = closeTop;
  884. result._closeBottom = closeBottom;
  885. result._shadowVolume = shadowVolume;
  886. result._offsetAttribute =
  887. offsetAttribute === -1 ? undefined : offsetAttribute;
  888. result._arcType = arcType;
  889. result.packedLength = packedLength;
  890. return result;
  891. };
  892. /**
  893. * Returns the bounding rectangle given the provided options
  894. *
  895. * @param {Object} options Object with the following properties:
  896. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  897. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions sampled.
  898. * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of line the polygon edges must follow. Valid options are {@link ArcType.GEODESIC} and {@link ArcType.RHUMB}.
  899. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  900. * @param {Rectangle} [result] An object in which to store the result.
  901. *
  902. * @returns {Rectangle} The result rectangle
  903. */
  904. PolygonGeometry.computeRectangle = function (options, result) {
  905. //>>includeStart('debug', pragmas.debug);
  906. Check.Check.typeOf.object("options", options);
  907. Check.Check.typeOf.object("options.polygonHierarchy", options.polygonHierarchy);
  908. //>>includeEnd('debug');
  909. var granularity = when.defaultValue(
  910. options.granularity,
  911. _Math.CesiumMath.RADIANS_PER_DEGREE
  912. );
  913. var arcType = when.defaultValue(options.arcType, ArcType.ArcType.GEODESIC);
  914. //>>includeStart('debug', pragmas.debug);
  915. if (arcType !== ArcType.ArcType.GEODESIC && arcType !== ArcType.ArcType.RHUMB) {
  916. throw new Check.DeveloperError(
  917. "Invalid arcType. Valid options are ArcType.GEODESIC and ArcType.RHUMB."
  918. );
  919. }
  920. //>>includeEnd('debug');
  921. var polygonHierarchy = options.polygonHierarchy;
  922. var ellipsoid = when.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84);
  923. return computeRectangle(
  924. polygonHierarchy.positions,
  925. ellipsoid,
  926. arcType,
  927. granularity,
  928. result
  929. );
  930. };
  931. /**
  932. * Computes the geometric representation of a polygon, including its vertices, indices, and a bounding sphere.
  933. *
  934. * @param {PolygonGeometry} polygonGeometry A description of the polygon.
  935. * @returns {Geometry|undefined} The computed vertices and indices.
  936. */
  937. PolygonGeometry.createGeometry = function (polygonGeometry) {
  938. var vertexFormat = polygonGeometry._vertexFormat;
  939. var ellipsoid = polygonGeometry._ellipsoid;
  940. var granularity = polygonGeometry._granularity;
  941. var stRotation = polygonGeometry._stRotation;
  942. var polygonHierarchy = polygonGeometry._polygonHierarchy;
  943. var perPositionHeight = polygonGeometry._perPositionHeight;
  944. var closeTop = polygonGeometry._closeTop;
  945. var closeBottom = polygonGeometry._closeBottom;
  946. var arcType = polygonGeometry._arcType;
  947. var outerPositions = polygonHierarchy.positions;
  948. if (outerPositions.length < 3) {
  949. return;
  950. }
  951. var tangentPlane = EllipsoidTangentPlane.EllipsoidTangentPlane.fromPoints(
  952. outerPositions,
  953. ellipsoid
  954. );
  955. var results = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonsFromHierarchy(
  956. polygonHierarchy,
  957. tangentPlane.projectPointsOntoPlane.bind(tangentPlane),
  958. !perPositionHeight,
  959. ellipsoid
  960. );
  961. var hierarchy = results.hierarchy;
  962. var polygons = results.polygons;
  963. if (hierarchy.length === 0) {
  964. return;
  965. }
  966. outerPositions = hierarchy[0].outerRing;
  967. var boundingRectangle = PolygonGeometryLibrary.PolygonGeometryLibrary.computeBoundingRectangle(
  968. tangentPlane.plane.normal,
  969. tangentPlane.projectPointOntoPlane.bind(tangentPlane),
  970. outerPositions,
  971. stRotation,
  972. scratchBoundingRectangle
  973. );
  974. var geometries = [];
  975. var height = polygonGeometry._height;
  976. var extrudedHeight = polygonGeometry._extrudedHeight;
  977. var extrude =
  978. polygonGeometry._perPositionHeightExtrude ||
  979. !_Math.CesiumMath.equalsEpsilon(height, extrudedHeight, 0, _Math.CesiumMath.EPSILON2);
  980. var options = {
  981. perPositionHeight: perPositionHeight,
  982. vertexFormat: vertexFormat,
  983. geometry: undefined,
  984. tangentPlane: tangentPlane,
  985. boundingRectangle: boundingRectangle,
  986. ellipsoid: ellipsoid,
  987. stRotation: stRotation,
  988. bottom: false,
  989. top: true,
  990. wall: false,
  991. extrude: false,
  992. arcType: arcType,
  993. };
  994. var i;
  995. if (extrude) {
  996. options.extrude = true;
  997. options.top = closeTop;
  998. options.bottom = closeBottom;
  999. options.shadowVolume = polygonGeometry._shadowVolume;
  1000. options.offsetAttribute = polygonGeometry._offsetAttribute;
  1001. for (i = 0; i < polygons.length; i++) {
  1002. var splitGeometry = createGeometryFromPositionsExtruded(
  1003. ellipsoid,
  1004. polygons[i],
  1005. granularity,
  1006. hierarchy[i],
  1007. perPositionHeight,
  1008. closeTop,
  1009. closeBottom,
  1010. vertexFormat,
  1011. arcType
  1012. );
  1013. var topAndBottom;
  1014. if (closeTop && closeBottom) {
  1015. topAndBottom = splitGeometry.topAndBottom;
  1016. options.geometry = PolygonGeometryLibrary.PolygonGeometryLibrary.scaleToGeodeticHeightExtruded(
  1017. topAndBottom.geometry,
  1018. height,
  1019. extrudedHeight,
  1020. ellipsoid,
  1021. perPositionHeight
  1022. );
  1023. } else if (closeTop) {
  1024. topAndBottom = splitGeometry.topAndBottom;
  1025. topAndBottom.geometry.attributes.position.values = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(
  1026. topAndBottom.geometry.attributes.position.values,
  1027. height,
  1028. ellipsoid,
  1029. !perPositionHeight
  1030. );
  1031. options.geometry = topAndBottom.geometry;
  1032. } else if (closeBottom) {
  1033. topAndBottom = splitGeometry.topAndBottom;
  1034. topAndBottom.geometry.attributes.position.values = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(
  1035. topAndBottom.geometry.attributes.position.values,
  1036. extrudedHeight,
  1037. ellipsoid,
  1038. true
  1039. );
  1040. options.geometry = topAndBottom.geometry;
  1041. }
  1042. if (closeTop || closeBottom) {
  1043. options.wall = false;
  1044. topAndBottom.geometry = computeAttributes(options);
  1045. geometries.push(topAndBottom);
  1046. }
  1047. var walls = splitGeometry.walls;
  1048. options.wall = true;
  1049. for (var k = 0; k < walls.length; k++) {
  1050. var wall = walls[k];
  1051. options.geometry = PolygonGeometryLibrary.PolygonGeometryLibrary.scaleToGeodeticHeightExtruded(
  1052. wall.geometry,
  1053. height,
  1054. extrudedHeight,
  1055. ellipsoid,
  1056. perPositionHeight
  1057. );
  1058. wall.geometry = computeAttributes(options);
  1059. geometries.push(wall);
  1060. }
  1061. }
  1062. } else {
  1063. for (i = 0; i < polygons.length; i++) {
  1064. var geometryInstance = new GeometryInstance.GeometryInstance({
  1065. geometry: PolygonGeometryLibrary.PolygonGeometryLibrary.createGeometryFromPositions(
  1066. ellipsoid,
  1067. polygons[i],
  1068. granularity,
  1069. perPositionHeight,
  1070. vertexFormat,
  1071. arcType
  1072. ),
  1073. });
  1074. geometryInstance.geometry.attributes.position.values = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(
  1075. geometryInstance.geometry.attributes.position.values,
  1076. height,
  1077. ellipsoid,
  1078. !perPositionHeight
  1079. );
  1080. options.geometry = geometryInstance.geometry;
  1081. geometryInstance.geometry = computeAttributes(options);
  1082. if (when.defined(polygonGeometry._offsetAttribute)) {
  1083. var length =
  1084. geometryInstance.geometry.attributes.position.values.length;
  1085. var applyOffset = new Uint8Array(length / 3);
  1086. var offsetValue =
  1087. polygonGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE
  1088. ? 0
  1089. : 1;
  1090. GeometryOffsetAttribute.arrayFill(applyOffset, offsetValue);
  1091. geometryInstance.geometry.attributes.applyOffset = new GeometryAttribute.GeometryAttribute(
  1092. {
  1093. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  1094. componentsPerAttribute: 1,
  1095. values: applyOffset,
  1096. }
  1097. );
  1098. }
  1099. geometries.push(geometryInstance);
  1100. }
  1101. }
  1102. var geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
  1103. geometry.attributes.position.values = new Float64Array(
  1104. geometry.attributes.position.values
  1105. );
  1106. geometry.indices = IndexDatatype.IndexDatatype.createTypedArray(
  1107. geometry.attributes.position.values.length / 3,
  1108. geometry.indices
  1109. );
  1110. var attributes = geometry.attributes;
  1111. var boundingSphere = Transforms.BoundingSphere.fromVertices(attributes.position.values);
  1112. if (!vertexFormat.position) {
  1113. delete attributes.position;
  1114. }
  1115. return new GeometryAttribute.Geometry({
  1116. attributes: attributes,
  1117. indices: geometry.indices,
  1118. primitiveType: geometry.primitiveType,
  1119. boundingSphere: boundingSphere,
  1120. offsetAttribute: polygonGeometry._offsetAttribute,
  1121. });
  1122. };
  1123. /**
  1124. * @private
  1125. */
  1126. PolygonGeometry.createShadowVolume = function (
  1127. polygonGeometry,
  1128. minHeightFunc,
  1129. maxHeightFunc
  1130. ) {
  1131. var granularity = polygonGeometry._granularity;
  1132. var ellipsoid = polygonGeometry._ellipsoid;
  1133. var minHeight = minHeightFunc(granularity, ellipsoid);
  1134. var maxHeight = maxHeightFunc(granularity, ellipsoid);
  1135. return new PolygonGeometry({
  1136. polygonHierarchy: polygonGeometry._polygonHierarchy,
  1137. ellipsoid: ellipsoid,
  1138. stRotation: polygonGeometry._stRotation,
  1139. granularity: granularity,
  1140. perPositionHeight: false,
  1141. extrudedHeight: minHeight,
  1142. height: maxHeight,
  1143. vertexFormat: VertexFormat.VertexFormat.POSITION_ONLY,
  1144. shadowVolume: true,
  1145. arcType: polygonGeometry._arcType,
  1146. });
  1147. };
  1148. function textureCoordinateRotationPoints(polygonGeometry) {
  1149. var stRotation = -polygonGeometry._stRotation;
  1150. if (stRotation === 0.0) {
  1151. return [0, 0, 0, 1, 1, 0];
  1152. }
  1153. var ellipsoid = polygonGeometry._ellipsoid;
  1154. var positions = polygonGeometry._polygonHierarchy.positions;
  1155. var boundingRectangle = polygonGeometry.rectangle;
  1156. return GeometryAttribute.Geometry._textureCoordinateRotationPoints(
  1157. positions,
  1158. stRotation,
  1159. ellipsoid,
  1160. boundingRectangle
  1161. );
  1162. }
  1163. Object.defineProperties(PolygonGeometry.prototype, {
  1164. /**
  1165. * @private
  1166. */
  1167. rectangle: {
  1168. get: function () {
  1169. if (!when.defined(this._rectangle)) {
  1170. var positions = this._polygonHierarchy.positions;
  1171. this._rectangle = computeRectangle(
  1172. positions,
  1173. this._ellipsoid,
  1174. this._arcType,
  1175. this._granularity
  1176. );
  1177. }
  1178. return this._rectangle;
  1179. },
  1180. },
  1181. /**
  1182. * For remapping texture coordinates when rendering PolygonGeometries as GroundPrimitives.
  1183. * @private
  1184. */
  1185. textureCoordinateRotationPoints: {
  1186. get: function () {
  1187. if (!when.defined(this._textureCoordinateRotationPoints)) {
  1188. this._textureCoordinateRotationPoints = textureCoordinateRotationPoints(
  1189. this
  1190. );
  1191. }
  1192. return this._textureCoordinateRotationPoints;
  1193. },
  1194. },
  1195. });
  1196. function createPolygonGeometry(polygonGeometry, offset) {
  1197. if (when.defined(offset)) {
  1198. polygonGeometry = PolygonGeometry.unpack(polygonGeometry, offset);
  1199. }
  1200. polygonGeometry._ellipsoid = Cartesian2.Ellipsoid.clone(polygonGeometry._ellipsoid);
  1201. return PolygonGeometry.createGeometry(polygonGeometry);
  1202. }
  1203. return createPolygonGeometry;
  1204. });