createCorridorGeometry.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./GeometryOffsetAttribute-3e8c299c', './arrayRemoveDuplicates-06991c15', './Transforms-a076dbe6', './Matrix2-fc7e9822', './RuntimeError-c581ca93', './ComponentDatatype-4a60b8d6', './PolylineVolumeGeometryLibrary-759a8d46', './CorridorGeometryLibrary-3d47380e', './defaultValue-94c3e563', './GeometryAttribute-2ecf73f6', './GeometryAttributes-7df9bef6', './IndexDatatype-db156785', './PolygonPipeline-cc031b9f', './VertexFormat-e46f29d6', './_commonjsHelpers-3aae1032-f55dc0c4', './combine-761d9c3f', './WebGLConstants-7dccdc96', './EllipsoidTangentPlane-d2c0c530', './AxisAlignedBoundingBox-8103739f', './IntersectionTests-5deed78b', './Plane-e20fba8c', './PolylinePipeline-7608e667', './EllipsoidGeodesic-dc94f381', './EllipsoidRhumbLine-daebc75b'], (function (GeometryOffsetAttribute, arrayRemoveDuplicates, Transforms, Matrix2, RuntimeError, ComponentDatatype, PolylineVolumeGeometryLibrary, CorridorGeometryLibrary, defaultValue, GeometryAttribute, GeometryAttributes, IndexDatatype, PolygonPipeline, VertexFormat, _commonjsHelpers3aae1032, combine$1, WebGLConstants, EllipsoidTangentPlane, AxisAlignedBoundingBox, IntersectionTests, Plane, PolylinePipeline, EllipsoidGeodesic, EllipsoidRhumbLine) { 'use strict';
  3. const cartesian1 = new Matrix2.Cartesian3();
  4. const cartesian2 = new Matrix2.Cartesian3();
  5. const cartesian3 = new Matrix2.Cartesian3();
  6. const cartesian4 = new Matrix2.Cartesian3();
  7. const cartesian5 = new Matrix2.Cartesian3();
  8. const cartesian6 = new Matrix2.Cartesian3();
  9. const scratch1 = new Matrix2.Cartesian3();
  10. const scratch2 = new Matrix2.Cartesian3();
  11. function scaleToSurface(positions, ellipsoid) {
  12. for (let i = 0; i < positions.length; i++) {
  13. positions[i] = ellipsoid.scaleToGeodeticSurface(positions[i], positions[i]);
  14. }
  15. return positions;
  16. }
  17. function addNormals(attr, normal, left, front, back, vertexFormat) {
  18. const normals = attr.normals;
  19. const tangents = attr.tangents;
  20. const bitangents = attr.bitangents;
  21. const forward = Matrix2.Cartesian3.normalize(
  22. Matrix2.Cartesian3.cross(left, normal, scratch1),
  23. scratch1
  24. );
  25. if (vertexFormat.normal) {
  26. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(normals, normal, front, back);
  27. }
  28. if (vertexFormat.tangent) {
  29. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(tangents, forward, front, back);
  30. }
  31. if (vertexFormat.bitangent) {
  32. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(bitangents, left, front, back);
  33. }
  34. }
  35. function combine(computedPositions, vertexFormat, ellipsoid) {
  36. const positions = computedPositions.positions;
  37. const corners = computedPositions.corners;
  38. const endPositions = computedPositions.endPositions;
  39. const computedLefts = computedPositions.lefts;
  40. const computedNormals = computedPositions.normals;
  41. const attributes = new GeometryAttributes.GeometryAttributes();
  42. let corner;
  43. let leftCount = 0;
  44. let rightCount = 0;
  45. let i;
  46. let indicesLength = 0;
  47. let length;
  48. for (i = 0; i < positions.length; i += 2) {
  49. length = positions[i].length - 3;
  50. leftCount += length; //subtracting 3 to account for duplicate points at corners
  51. indicesLength += length * 2;
  52. rightCount += positions[i + 1].length - 3;
  53. }
  54. leftCount += 3; //add back count for end positions
  55. rightCount += 3;
  56. for (i = 0; i < corners.length; i++) {
  57. corner = corners[i];
  58. const leftSide = corners[i].leftPositions;
  59. if (defaultValue.defined(leftSide)) {
  60. length = leftSide.length;
  61. leftCount += length;
  62. indicesLength += length;
  63. } else {
  64. length = corners[i].rightPositions.length;
  65. rightCount += length;
  66. indicesLength += length;
  67. }
  68. }
  69. const addEndPositions = defaultValue.defined(endPositions);
  70. let endPositionLength;
  71. if (addEndPositions) {
  72. endPositionLength = endPositions[0].length - 3;
  73. leftCount += endPositionLength;
  74. rightCount += endPositionLength;
  75. endPositionLength /= 3;
  76. indicesLength += endPositionLength * 6;
  77. }
  78. const size = leftCount + rightCount;
  79. const finalPositions = new Float64Array(size);
  80. const normals = vertexFormat.normal ? new Float32Array(size) : undefined;
  81. const tangents = vertexFormat.tangent ? new Float32Array(size) : undefined;
  82. const bitangents = vertexFormat.bitangent
  83. ? new Float32Array(size)
  84. : undefined;
  85. const attr = {
  86. normals: normals,
  87. tangents: tangents,
  88. bitangents: bitangents,
  89. };
  90. let front = 0;
  91. let back = size - 1;
  92. let UL, LL, UR, LR;
  93. let normal = cartesian1;
  94. let left = cartesian2;
  95. let rightPos, leftPos;
  96. const halfLength = endPositionLength / 2;
  97. const indices = IndexDatatype.IndexDatatype.createTypedArray(size / 3, indicesLength);
  98. let index = 0;
  99. if (addEndPositions) {
  100. // add rounded end
  101. leftPos = cartesian3;
  102. rightPos = cartesian4;
  103. const firstEndPositions = endPositions[0];
  104. normal = Matrix2.Cartesian3.fromArray(computedNormals, 0, normal);
  105. left = Matrix2.Cartesian3.fromArray(computedLefts, 0, left);
  106. for (i = 0; i < halfLength; i++) {
  107. leftPos = Matrix2.Cartesian3.fromArray(
  108. firstEndPositions,
  109. (halfLength - 1 - i) * 3,
  110. leftPos
  111. );
  112. rightPos = Matrix2.Cartesian3.fromArray(
  113. firstEndPositions,
  114. (halfLength + i) * 3,
  115. rightPos
  116. );
  117. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(finalPositions, rightPos, front);
  118. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  119. finalPositions,
  120. leftPos,
  121. undefined,
  122. back
  123. );
  124. addNormals(attr, normal, left, front, back, vertexFormat);
  125. LL = front / 3;
  126. LR = LL + 1;
  127. UL = (back - 2) / 3;
  128. UR = UL - 1;
  129. indices[index++] = UL;
  130. indices[index++] = LL;
  131. indices[index++] = UR;
  132. indices[index++] = UR;
  133. indices[index++] = LL;
  134. indices[index++] = LR;
  135. front += 3;
  136. back -= 3;
  137. }
  138. }
  139. let posIndex = 0;
  140. let compIndex = 0;
  141. let rightEdge = positions[posIndex++]; //add first two edges
  142. let leftEdge = positions[posIndex++];
  143. finalPositions.set(rightEdge, front);
  144. finalPositions.set(leftEdge, back - leftEdge.length + 1);
  145. left = Matrix2.Cartesian3.fromArray(computedLefts, compIndex, left);
  146. let rightNormal;
  147. let leftNormal;
  148. length = leftEdge.length - 3;
  149. for (i = 0; i < length; i += 3) {
  150. rightNormal = ellipsoid.geodeticSurfaceNormal(
  151. Matrix2.Cartesian3.fromArray(rightEdge, i, scratch1),
  152. scratch1
  153. );
  154. leftNormal = ellipsoid.geodeticSurfaceNormal(
  155. Matrix2.Cartesian3.fromArray(leftEdge, length - i, scratch2),
  156. scratch2
  157. );
  158. normal = Matrix2.Cartesian3.normalize(
  159. Matrix2.Cartesian3.add(rightNormal, leftNormal, normal),
  160. normal
  161. );
  162. addNormals(attr, normal, left, front, back, vertexFormat);
  163. LL = front / 3;
  164. LR = LL + 1;
  165. UL = (back - 2) / 3;
  166. UR = UL - 1;
  167. indices[index++] = UL;
  168. indices[index++] = LL;
  169. indices[index++] = UR;
  170. indices[index++] = UR;
  171. indices[index++] = LL;
  172. indices[index++] = LR;
  173. front += 3;
  174. back -= 3;
  175. }
  176. rightNormal = ellipsoid.geodeticSurfaceNormal(
  177. Matrix2.Cartesian3.fromArray(rightEdge, length, scratch1),
  178. scratch1
  179. );
  180. leftNormal = ellipsoid.geodeticSurfaceNormal(
  181. Matrix2.Cartesian3.fromArray(leftEdge, length, scratch2),
  182. scratch2
  183. );
  184. normal = Matrix2.Cartesian3.normalize(
  185. Matrix2.Cartesian3.add(rightNormal, leftNormal, normal),
  186. normal
  187. );
  188. compIndex += 3;
  189. for (i = 0; i < corners.length; i++) {
  190. let j;
  191. corner = corners[i];
  192. const l = corner.leftPositions;
  193. const r = corner.rightPositions;
  194. let pivot;
  195. let start;
  196. let outsidePoint = cartesian6;
  197. let previousPoint = cartesian3;
  198. let nextPoint = cartesian4;
  199. normal = Matrix2.Cartesian3.fromArray(computedNormals, compIndex, normal);
  200. if (defaultValue.defined(l)) {
  201. addNormals(attr, normal, left, undefined, back, vertexFormat);
  202. back -= 3;
  203. pivot = LR;
  204. start = UR;
  205. for (j = 0; j < l.length / 3; j++) {
  206. outsidePoint = Matrix2.Cartesian3.fromArray(l, j * 3, outsidePoint);
  207. indices[index++] = pivot;
  208. indices[index++] = start - j - 1;
  209. indices[index++] = start - j;
  210. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  211. finalPositions,
  212. outsidePoint,
  213. undefined,
  214. back
  215. );
  216. previousPoint = Matrix2.Cartesian3.fromArray(
  217. finalPositions,
  218. (start - j - 1) * 3,
  219. previousPoint
  220. );
  221. nextPoint = Matrix2.Cartesian3.fromArray(finalPositions, pivot * 3, nextPoint);
  222. left = Matrix2.Cartesian3.normalize(
  223. Matrix2.Cartesian3.subtract(previousPoint, nextPoint, left),
  224. left
  225. );
  226. addNormals(attr, normal, left, undefined, back, vertexFormat);
  227. back -= 3;
  228. }
  229. outsidePoint = Matrix2.Cartesian3.fromArray(
  230. finalPositions,
  231. pivot * 3,
  232. outsidePoint
  233. );
  234. previousPoint = Matrix2.Cartesian3.subtract(
  235. Matrix2.Cartesian3.fromArray(finalPositions, start * 3, previousPoint),
  236. outsidePoint,
  237. previousPoint
  238. );
  239. nextPoint = Matrix2.Cartesian3.subtract(
  240. Matrix2.Cartesian3.fromArray(finalPositions, (start - j) * 3, nextPoint),
  241. outsidePoint,
  242. nextPoint
  243. );
  244. left = Matrix2.Cartesian3.normalize(
  245. Matrix2.Cartesian3.add(previousPoint, nextPoint, left),
  246. left
  247. );
  248. addNormals(attr, normal, left, front, undefined, vertexFormat);
  249. front += 3;
  250. } else {
  251. addNormals(attr, normal, left, front, undefined, vertexFormat);
  252. front += 3;
  253. pivot = UR;
  254. start = LR;
  255. for (j = 0; j < r.length / 3; j++) {
  256. outsidePoint = Matrix2.Cartesian3.fromArray(r, j * 3, outsidePoint);
  257. indices[index++] = pivot;
  258. indices[index++] = start + j;
  259. indices[index++] = start + j + 1;
  260. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  261. finalPositions,
  262. outsidePoint,
  263. front
  264. );
  265. previousPoint = Matrix2.Cartesian3.fromArray(
  266. finalPositions,
  267. pivot * 3,
  268. previousPoint
  269. );
  270. nextPoint = Matrix2.Cartesian3.fromArray(
  271. finalPositions,
  272. (start + j) * 3,
  273. nextPoint
  274. );
  275. left = Matrix2.Cartesian3.normalize(
  276. Matrix2.Cartesian3.subtract(previousPoint, nextPoint, left),
  277. left
  278. );
  279. addNormals(attr, normal, left, front, undefined, vertexFormat);
  280. front += 3;
  281. }
  282. outsidePoint = Matrix2.Cartesian3.fromArray(
  283. finalPositions,
  284. pivot * 3,
  285. outsidePoint
  286. );
  287. previousPoint = Matrix2.Cartesian3.subtract(
  288. Matrix2.Cartesian3.fromArray(finalPositions, (start + j) * 3, previousPoint),
  289. outsidePoint,
  290. previousPoint
  291. );
  292. nextPoint = Matrix2.Cartesian3.subtract(
  293. Matrix2.Cartesian3.fromArray(finalPositions, start * 3, nextPoint),
  294. outsidePoint,
  295. nextPoint
  296. );
  297. left = Matrix2.Cartesian3.normalize(
  298. Matrix2.Cartesian3.negate(Matrix2.Cartesian3.add(nextPoint, previousPoint, left), left),
  299. left
  300. );
  301. addNormals(attr, normal, left, undefined, back, vertexFormat);
  302. back -= 3;
  303. }
  304. rightEdge = positions[posIndex++];
  305. leftEdge = positions[posIndex++];
  306. rightEdge.splice(0, 3); //remove duplicate points added by corner
  307. leftEdge.splice(leftEdge.length - 3, 3);
  308. finalPositions.set(rightEdge, front);
  309. finalPositions.set(leftEdge, back - leftEdge.length + 1);
  310. length = leftEdge.length - 3;
  311. compIndex += 3;
  312. left = Matrix2.Cartesian3.fromArray(computedLefts, compIndex, left);
  313. for (j = 0; j < leftEdge.length; j += 3) {
  314. rightNormal = ellipsoid.geodeticSurfaceNormal(
  315. Matrix2.Cartesian3.fromArray(rightEdge, j, scratch1),
  316. scratch1
  317. );
  318. leftNormal = ellipsoid.geodeticSurfaceNormal(
  319. Matrix2.Cartesian3.fromArray(leftEdge, length - j, scratch2),
  320. scratch2
  321. );
  322. normal = Matrix2.Cartesian3.normalize(
  323. Matrix2.Cartesian3.add(rightNormal, leftNormal, normal),
  324. normal
  325. );
  326. addNormals(attr, normal, left, front, back, vertexFormat);
  327. LR = front / 3;
  328. LL = LR - 1;
  329. UR = (back - 2) / 3;
  330. UL = UR + 1;
  331. indices[index++] = UL;
  332. indices[index++] = LL;
  333. indices[index++] = UR;
  334. indices[index++] = UR;
  335. indices[index++] = LL;
  336. indices[index++] = LR;
  337. front += 3;
  338. back -= 3;
  339. }
  340. front -= 3;
  341. back += 3;
  342. }
  343. normal = Matrix2.Cartesian3.fromArray(
  344. computedNormals,
  345. computedNormals.length - 3,
  346. normal
  347. );
  348. addNormals(attr, normal, left, front, back, vertexFormat);
  349. if (addEndPositions) {
  350. // add rounded end
  351. front += 3;
  352. back -= 3;
  353. leftPos = cartesian3;
  354. rightPos = cartesian4;
  355. const lastEndPositions = endPositions[1];
  356. for (i = 0; i < halfLength; i++) {
  357. leftPos = Matrix2.Cartesian3.fromArray(
  358. lastEndPositions,
  359. (endPositionLength - i - 1) * 3,
  360. leftPos
  361. );
  362. rightPos = Matrix2.Cartesian3.fromArray(lastEndPositions, i * 3, rightPos);
  363. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  364. finalPositions,
  365. leftPos,
  366. undefined,
  367. back
  368. );
  369. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(finalPositions, rightPos, front);
  370. addNormals(attr, normal, left, front, back, vertexFormat);
  371. LR = front / 3;
  372. LL = LR - 1;
  373. UR = (back - 2) / 3;
  374. UL = UR + 1;
  375. indices[index++] = UL;
  376. indices[index++] = LL;
  377. indices[index++] = UR;
  378. indices[index++] = UR;
  379. indices[index++] = LL;
  380. indices[index++] = LR;
  381. front += 3;
  382. back -= 3;
  383. }
  384. }
  385. attributes.position = new GeometryAttribute.GeometryAttribute({
  386. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  387. componentsPerAttribute: 3,
  388. values: finalPositions,
  389. });
  390. if (vertexFormat.st) {
  391. const st = new Float32Array((size / 3) * 2);
  392. let rightSt;
  393. let leftSt;
  394. let stIndex = 0;
  395. if (addEndPositions) {
  396. leftCount /= 3;
  397. rightCount /= 3;
  398. const theta = Math.PI / (endPositionLength + 1);
  399. leftSt = 1 / (leftCount - endPositionLength + 1);
  400. rightSt = 1 / (rightCount - endPositionLength + 1);
  401. let a;
  402. const halfEndPos = endPositionLength / 2;
  403. for (i = halfEndPos + 1; i < endPositionLength + 1; i++) {
  404. // lower left rounded end
  405. a = ComponentDatatype.CesiumMath.PI_OVER_TWO + theta * i;
  406. st[stIndex++] = rightSt * (1 + Math.cos(a));
  407. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  408. }
  409. for (i = 1; i < rightCount - endPositionLength + 1; i++) {
  410. // bottom edge
  411. st[stIndex++] = i * rightSt;
  412. st[stIndex++] = 0;
  413. }
  414. for (i = endPositionLength; i > halfEndPos; i--) {
  415. // lower right rounded end
  416. a = ComponentDatatype.CesiumMath.PI_OVER_TWO - i * theta;
  417. st[stIndex++] = 1 - rightSt * (1 + Math.cos(a));
  418. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  419. }
  420. for (i = halfEndPos; i > 0; i--) {
  421. // upper right rounded end
  422. a = ComponentDatatype.CesiumMath.PI_OVER_TWO - theta * i;
  423. st[stIndex++] = 1 - leftSt * (1 + Math.cos(a));
  424. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  425. }
  426. for (i = leftCount - endPositionLength; i > 0; i--) {
  427. // top edge
  428. st[stIndex++] = i * leftSt;
  429. st[stIndex++] = 1;
  430. }
  431. for (i = 1; i < halfEndPos + 1; i++) {
  432. // upper left rounded end
  433. a = ComponentDatatype.CesiumMath.PI_OVER_TWO + theta * i;
  434. st[stIndex++] = leftSt * (1 + Math.cos(a));
  435. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  436. }
  437. } else {
  438. leftCount /= 3;
  439. rightCount /= 3;
  440. leftSt = 1 / (leftCount - 1);
  441. rightSt = 1 / (rightCount - 1);
  442. for (i = 0; i < rightCount; i++) {
  443. // bottom edge
  444. st[stIndex++] = i * rightSt;
  445. st[stIndex++] = 0;
  446. }
  447. for (i = leftCount; i > 0; i--) {
  448. // top edge
  449. st[stIndex++] = (i - 1) * leftSt;
  450. st[stIndex++] = 1;
  451. }
  452. }
  453. attributes.st = new GeometryAttribute.GeometryAttribute({
  454. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  455. componentsPerAttribute: 2,
  456. values: st,
  457. });
  458. }
  459. if (vertexFormat.normal) {
  460. attributes.normal = new GeometryAttribute.GeometryAttribute({
  461. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  462. componentsPerAttribute: 3,
  463. values: attr.normals,
  464. });
  465. }
  466. if (vertexFormat.tangent) {
  467. attributes.tangent = new GeometryAttribute.GeometryAttribute({
  468. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  469. componentsPerAttribute: 3,
  470. values: attr.tangents,
  471. });
  472. }
  473. if (vertexFormat.bitangent) {
  474. attributes.bitangent = new GeometryAttribute.GeometryAttribute({
  475. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  476. componentsPerAttribute: 3,
  477. values: attr.bitangents,
  478. });
  479. }
  480. return {
  481. attributes: attributes,
  482. indices: indices,
  483. };
  484. }
  485. function extrudedAttributes(attributes, vertexFormat) {
  486. if (
  487. !vertexFormat.normal &&
  488. !vertexFormat.tangent &&
  489. !vertexFormat.bitangent &&
  490. !vertexFormat.st
  491. ) {
  492. return attributes;
  493. }
  494. const positions = attributes.position.values;
  495. let topNormals;
  496. let topBitangents;
  497. if (vertexFormat.normal || vertexFormat.bitangent) {
  498. topNormals = attributes.normal.values;
  499. topBitangents = attributes.bitangent.values;
  500. }
  501. const size = attributes.position.values.length / 18;
  502. const threeSize = size * 3;
  503. const twoSize = size * 2;
  504. const sixSize = threeSize * 2;
  505. let i;
  506. if (vertexFormat.normal || vertexFormat.bitangent || vertexFormat.tangent) {
  507. const normals = vertexFormat.normal
  508. ? new Float32Array(threeSize * 6)
  509. : undefined;
  510. const tangents = vertexFormat.tangent
  511. ? new Float32Array(threeSize * 6)
  512. : undefined;
  513. const bitangents = vertexFormat.bitangent
  514. ? new Float32Array(threeSize * 6)
  515. : undefined;
  516. let topPosition = cartesian1;
  517. let bottomPosition = cartesian2;
  518. let previousPosition = cartesian3;
  519. let normal = cartesian4;
  520. let tangent = cartesian5;
  521. let bitangent = cartesian6;
  522. let attrIndex = sixSize;
  523. for (i = 0; i < threeSize; i += 3) {
  524. const attrIndexOffset = attrIndex + sixSize;
  525. topPosition = Matrix2.Cartesian3.fromArray(positions, i, topPosition);
  526. bottomPosition = Matrix2.Cartesian3.fromArray(
  527. positions,
  528. i + threeSize,
  529. bottomPosition
  530. );
  531. previousPosition = Matrix2.Cartesian3.fromArray(
  532. positions,
  533. (i + 3) % threeSize,
  534. previousPosition
  535. );
  536. bottomPosition = Matrix2.Cartesian3.subtract(
  537. bottomPosition,
  538. topPosition,
  539. bottomPosition
  540. );
  541. previousPosition = Matrix2.Cartesian3.subtract(
  542. previousPosition,
  543. topPosition,
  544. previousPosition
  545. );
  546. normal = Matrix2.Cartesian3.normalize(
  547. Matrix2.Cartesian3.cross(bottomPosition, previousPosition, normal),
  548. normal
  549. );
  550. if (vertexFormat.normal) {
  551. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(normals, normal, attrIndexOffset);
  552. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  553. normals,
  554. normal,
  555. attrIndexOffset + 3
  556. );
  557. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(normals, normal, attrIndex);
  558. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(normals, normal, attrIndex + 3);
  559. }
  560. if (vertexFormat.tangent || vertexFormat.bitangent) {
  561. bitangent = Matrix2.Cartesian3.fromArray(topNormals, i, bitangent);
  562. if (vertexFormat.bitangent) {
  563. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  564. bitangents,
  565. bitangent,
  566. attrIndexOffset
  567. );
  568. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  569. bitangents,
  570. bitangent,
  571. attrIndexOffset + 3
  572. );
  573. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  574. bitangents,
  575. bitangent,
  576. attrIndex
  577. );
  578. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  579. bitangents,
  580. bitangent,
  581. attrIndex + 3
  582. );
  583. }
  584. if (vertexFormat.tangent) {
  585. tangent = Matrix2.Cartesian3.normalize(
  586. Matrix2.Cartesian3.cross(bitangent, normal, tangent),
  587. tangent
  588. );
  589. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  590. tangents,
  591. tangent,
  592. attrIndexOffset
  593. );
  594. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  595. tangents,
  596. tangent,
  597. attrIndexOffset + 3
  598. );
  599. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(tangents, tangent, attrIndex);
  600. CorridorGeometryLibrary.CorridorGeometryLibrary.addAttribute(
  601. tangents,
  602. tangent,
  603. attrIndex + 3
  604. );
  605. }
  606. }
  607. attrIndex += 6;
  608. }
  609. if (vertexFormat.normal) {
  610. normals.set(topNormals); //top
  611. for (i = 0; i < threeSize; i += 3) {
  612. //bottom normals
  613. normals[i + threeSize] = -topNormals[i];
  614. normals[i + threeSize + 1] = -topNormals[i + 1];
  615. normals[i + threeSize + 2] = -topNormals[i + 2];
  616. }
  617. attributes.normal.values = normals;
  618. } else {
  619. attributes.normal = undefined;
  620. }
  621. if (vertexFormat.bitangent) {
  622. bitangents.set(topBitangents); //top
  623. bitangents.set(topBitangents, threeSize); //bottom
  624. attributes.bitangent.values = bitangents;
  625. } else {
  626. attributes.bitangent = undefined;
  627. }
  628. if (vertexFormat.tangent) {
  629. const topTangents = attributes.tangent.values;
  630. tangents.set(topTangents); //top
  631. tangents.set(topTangents, threeSize); //bottom
  632. attributes.tangent.values = tangents;
  633. }
  634. }
  635. if (vertexFormat.st) {
  636. const topSt = attributes.st.values;
  637. const st = new Float32Array(twoSize * 6);
  638. st.set(topSt); //top
  639. st.set(topSt, twoSize); //bottom
  640. let index = twoSize * 2;
  641. for (let j = 0; j < 2; j++) {
  642. st[index++] = topSt[0];
  643. st[index++] = topSt[1];
  644. for (i = 2; i < twoSize; i += 2) {
  645. const s = topSt[i];
  646. const t = topSt[i + 1];
  647. st[index++] = s;
  648. st[index++] = t;
  649. st[index++] = s;
  650. st[index++] = t;
  651. }
  652. st[index++] = topSt[0];
  653. st[index++] = topSt[1];
  654. }
  655. attributes.st.values = st;
  656. }
  657. return attributes;
  658. }
  659. function addWallPositions(positions, index, wallPositions) {
  660. wallPositions[index++] = positions[0];
  661. wallPositions[index++] = positions[1];
  662. wallPositions[index++] = positions[2];
  663. for (let i = 3; i < positions.length; i += 3) {
  664. const x = positions[i];
  665. const y = positions[i + 1];
  666. const z = positions[i + 2];
  667. wallPositions[index++] = x;
  668. wallPositions[index++] = y;
  669. wallPositions[index++] = z;
  670. wallPositions[index++] = x;
  671. wallPositions[index++] = y;
  672. wallPositions[index++] = z;
  673. }
  674. wallPositions[index++] = positions[0];
  675. wallPositions[index++] = positions[1];
  676. wallPositions[index++] = positions[2];
  677. return wallPositions;
  678. }
  679. function computePositionsExtruded(params, vertexFormat) {
  680. const topVertexFormat = new VertexFormat.VertexFormat({
  681. position: vertexFormat.position,
  682. normal:
  683. vertexFormat.normal || vertexFormat.bitangent || params.shadowVolume,
  684. tangent: vertexFormat.tangent,
  685. bitangent: vertexFormat.normal || vertexFormat.bitangent,
  686. st: vertexFormat.st,
  687. });
  688. const ellipsoid = params.ellipsoid;
  689. const computedPositions = CorridorGeometryLibrary.CorridorGeometryLibrary.computePositions(params);
  690. const attr = combine(computedPositions, topVertexFormat, ellipsoid);
  691. const height = params.height;
  692. const extrudedHeight = params.extrudedHeight;
  693. let attributes = attr.attributes;
  694. const indices = attr.indices;
  695. let positions = attributes.position.values;
  696. let length = positions.length;
  697. const newPositions = new Float64Array(length * 6);
  698. let extrudedPositions = new Float64Array(length);
  699. extrudedPositions.set(positions);
  700. let wallPositions = new Float64Array(length * 4);
  701. positions = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(
  702. positions,
  703. height,
  704. ellipsoid
  705. );
  706. wallPositions = addWallPositions(positions, 0, wallPositions);
  707. extrudedPositions = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(
  708. extrudedPositions,
  709. extrudedHeight,
  710. ellipsoid
  711. );
  712. wallPositions = addWallPositions(
  713. extrudedPositions,
  714. length * 2,
  715. wallPositions
  716. );
  717. newPositions.set(positions);
  718. newPositions.set(extrudedPositions, length);
  719. newPositions.set(wallPositions, length * 2);
  720. attributes.position.values = newPositions;
  721. attributes = extrudedAttributes(attributes, vertexFormat);
  722. let i;
  723. const size = length / 3;
  724. if (params.shadowVolume) {
  725. const topNormals = attributes.normal.values;
  726. length = topNormals.length;
  727. let extrudeNormals = new Float32Array(length * 6);
  728. for (i = 0; i < length; i++) {
  729. topNormals[i] = -topNormals[i];
  730. }
  731. //only get normals for bottom layer that's going to be pushed down
  732. extrudeNormals.set(topNormals, length); //bottom face
  733. extrudeNormals = addWallPositions(topNormals, length * 4, extrudeNormals); //bottom wall
  734. attributes.extrudeDirection = new GeometryAttribute.GeometryAttribute({
  735. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  736. componentsPerAttribute: 3,
  737. values: extrudeNormals,
  738. });
  739. if (!vertexFormat.normal) {
  740. attributes.normal = undefined;
  741. }
  742. }
  743. if (defaultValue.defined(params.offsetAttribute)) {
  744. let applyOffset = new Uint8Array(size * 6);
  745. if (params.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP) {
  746. applyOffset = GeometryOffsetAttribute.arrayFill(applyOffset, 1, 0, size); // top face
  747. applyOffset = GeometryOffsetAttribute.arrayFill(applyOffset, 1, size * 2, size * 4); // top wall
  748. } else {
  749. const applyOffsetValue =
  750. params.offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE ? 0 : 1;
  751. applyOffset = GeometryOffsetAttribute.arrayFill(applyOffset, applyOffsetValue);
  752. }
  753. attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  754. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  755. componentsPerAttribute: 1,
  756. values: applyOffset,
  757. });
  758. }
  759. const iLength = indices.length;
  760. const twoSize = size + size;
  761. const newIndices = IndexDatatype.IndexDatatype.createTypedArray(
  762. newPositions.length / 3,
  763. iLength * 2 + twoSize * 3
  764. );
  765. newIndices.set(indices);
  766. let index = iLength;
  767. for (i = 0; i < iLength; i += 3) {
  768. // bottom indices
  769. const v0 = indices[i];
  770. const v1 = indices[i + 1];
  771. const v2 = indices[i + 2];
  772. newIndices[index++] = v2 + size;
  773. newIndices[index++] = v1 + size;
  774. newIndices[index++] = v0 + size;
  775. }
  776. let UL, LL, UR, LR;
  777. for (i = 0; i < twoSize; i += 2) {
  778. //wall indices
  779. UL = i + twoSize;
  780. LL = UL + twoSize;
  781. UR = UL + 1;
  782. LR = LL + 1;
  783. newIndices[index++] = UL;
  784. newIndices[index++] = LL;
  785. newIndices[index++] = UR;
  786. newIndices[index++] = UR;
  787. newIndices[index++] = LL;
  788. newIndices[index++] = LR;
  789. }
  790. return {
  791. attributes: attributes,
  792. indices: newIndices,
  793. };
  794. }
  795. const scratchCartesian1 = new Matrix2.Cartesian3();
  796. const scratchCartesian2 = new Matrix2.Cartesian3();
  797. const scratchCartographic = new Matrix2.Cartographic();
  798. function computeOffsetPoints(
  799. position1,
  800. position2,
  801. ellipsoid,
  802. halfWidth,
  803. min,
  804. max
  805. ) {
  806. // Compute direction of offset the point
  807. const direction = Matrix2.Cartesian3.subtract(
  808. position2,
  809. position1,
  810. scratchCartesian1
  811. );
  812. Matrix2.Cartesian3.normalize(direction, direction);
  813. const normal = ellipsoid.geodeticSurfaceNormal(position1, scratchCartesian2);
  814. const offsetDirection = Matrix2.Cartesian3.cross(
  815. direction,
  816. normal,
  817. scratchCartesian1
  818. );
  819. Matrix2.Cartesian3.multiplyByScalar(offsetDirection, halfWidth, offsetDirection);
  820. let minLat = min.latitude;
  821. let minLon = min.longitude;
  822. let maxLat = max.latitude;
  823. let maxLon = max.longitude;
  824. // Compute 2 offset points
  825. Matrix2.Cartesian3.add(position1, offsetDirection, scratchCartesian2);
  826. ellipsoid.cartesianToCartographic(scratchCartesian2, scratchCartographic);
  827. let lat = scratchCartographic.latitude;
  828. let lon = scratchCartographic.longitude;
  829. minLat = Math.min(minLat, lat);
  830. minLon = Math.min(minLon, lon);
  831. maxLat = Math.max(maxLat, lat);
  832. maxLon = Math.max(maxLon, lon);
  833. Matrix2.Cartesian3.subtract(position1, offsetDirection, scratchCartesian2);
  834. ellipsoid.cartesianToCartographic(scratchCartesian2, scratchCartographic);
  835. lat = scratchCartographic.latitude;
  836. lon = scratchCartographic.longitude;
  837. minLat = Math.min(minLat, lat);
  838. minLon = Math.min(minLon, lon);
  839. maxLat = Math.max(maxLat, lat);
  840. maxLon = Math.max(maxLon, lon);
  841. min.latitude = minLat;
  842. min.longitude = minLon;
  843. max.latitude = maxLat;
  844. max.longitude = maxLon;
  845. }
  846. const scratchCartesianOffset = new Matrix2.Cartesian3();
  847. const scratchCartesianEnds = new Matrix2.Cartesian3();
  848. const scratchCartographicMin = new Matrix2.Cartographic();
  849. const scratchCartographicMax = new Matrix2.Cartographic();
  850. function computeRectangle(positions, ellipsoid, width, cornerType, result) {
  851. positions = scaleToSurface(positions, ellipsoid);
  852. const cleanPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  853. positions,
  854. Matrix2.Cartesian3.equalsEpsilon
  855. );
  856. const length = cleanPositions.length;
  857. if (length < 2 || width <= 0) {
  858. return new Matrix2.Rectangle();
  859. }
  860. const halfWidth = width * 0.5;
  861. scratchCartographicMin.latitude = Number.POSITIVE_INFINITY;
  862. scratchCartographicMin.longitude = Number.POSITIVE_INFINITY;
  863. scratchCartographicMax.latitude = Number.NEGATIVE_INFINITY;
  864. scratchCartographicMax.longitude = Number.NEGATIVE_INFINITY;
  865. let lat, lon;
  866. if (cornerType === PolylineVolumeGeometryLibrary.CornerType.ROUNDED) {
  867. // Compute start cap
  868. const first = cleanPositions[0];
  869. Matrix2.Cartesian3.subtract(first, cleanPositions[1], scratchCartesianOffset);
  870. Matrix2.Cartesian3.normalize(scratchCartesianOffset, scratchCartesianOffset);
  871. Matrix2.Cartesian3.multiplyByScalar(
  872. scratchCartesianOffset,
  873. halfWidth,
  874. scratchCartesianOffset
  875. );
  876. Matrix2.Cartesian3.add(first, scratchCartesianOffset, scratchCartesianEnds);
  877. ellipsoid.cartesianToCartographic(
  878. scratchCartesianEnds,
  879. scratchCartographic
  880. );
  881. lat = scratchCartographic.latitude;
  882. lon = scratchCartographic.longitude;
  883. scratchCartographicMin.latitude = Math.min(
  884. scratchCartographicMin.latitude,
  885. lat
  886. );
  887. scratchCartographicMin.longitude = Math.min(
  888. scratchCartographicMin.longitude,
  889. lon
  890. );
  891. scratchCartographicMax.latitude = Math.max(
  892. scratchCartographicMax.latitude,
  893. lat
  894. );
  895. scratchCartographicMax.longitude = Math.max(
  896. scratchCartographicMax.longitude,
  897. lon
  898. );
  899. }
  900. // Compute the rest
  901. for (let i = 0; i < length - 1; ++i) {
  902. computeOffsetPoints(
  903. cleanPositions[i],
  904. cleanPositions[i + 1],
  905. ellipsoid,
  906. halfWidth,
  907. scratchCartographicMin,
  908. scratchCartographicMax
  909. );
  910. }
  911. // Compute ending point
  912. const last = cleanPositions[length - 1];
  913. Matrix2.Cartesian3.subtract(last, cleanPositions[length - 2], scratchCartesianOffset);
  914. Matrix2.Cartesian3.normalize(scratchCartesianOffset, scratchCartesianOffset);
  915. Matrix2.Cartesian3.multiplyByScalar(
  916. scratchCartesianOffset,
  917. halfWidth,
  918. scratchCartesianOffset
  919. );
  920. Matrix2.Cartesian3.add(last, scratchCartesianOffset, scratchCartesianEnds);
  921. computeOffsetPoints(
  922. last,
  923. scratchCartesianEnds,
  924. ellipsoid,
  925. halfWidth,
  926. scratchCartographicMin,
  927. scratchCartographicMax
  928. );
  929. if (cornerType === PolylineVolumeGeometryLibrary.CornerType.ROUNDED) {
  930. // Compute end cap
  931. ellipsoid.cartesianToCartographic(
  932. scratchCartesianEnds,
  933. scratchCartographic
  934. );
  935. lat = scratchCartographic.latitude;
  936. lon = scratchCartographic.longitude;
  937. scratchCartographicMin.latitude = Math.min(
  938. scratchCartographicMin.latitude,
  939. lat
  940. );
  941. scratchCartographicMin.longitude = Math.min(
  942. scratchCartographicMin.longitude,
  943. lon
  944. );
  945. scratchCartographicMax.latitude = Math.max(
  946. scratchCartographicMax.latitude,
  947. lat
  948. );
  949. scratchCartographicMax.longitude = Math.max(
  950. scratchCartographicMax.longitude,
  951. lon
  952. );
  953. }
  954. const rectangle = defaultValue.defined(result) ? result : new Matrix2.Rectangle();
  955. rectangle.north = scratchCartographicMax.latitude;
  956. rectangle.south = scratchCartographicMin.latitude;
  957. rectangle.east = scratchCartographicMax.longitude;
  958. rectangle.west = scratchCartographicMin.longitude;
  959. return rectangle;
  960. }
  961. /**
  962. * A description of a corridor. Corridor geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  963. *
  964. * @alias CorridorGeometry
  965. * @constructor
  966. *
  967. * @param {Object} options Object with the following properties:
  968. * @param {Cartesian3[]} options.positions An array of positions that define the center of the corridor.
  969. * @param {Number} options.width The distance between the edges of the corridor in meters.
  970. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  971. * @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.
  972. * @param {Number} [options.height=0] The distance in meters between the ellipsoid surface and the positions.
  973. * @param {Number} [options.extrudedHeight] The distance in meters between the ellipsoid surface and the extruded face.
  974. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  975. * @param {CornerType} [options.cornerType=CornerType.ROUNDED] Determines the style of the corners.
  976. *
  977. * @see CorridorGeometry.createGeometry
  978. * @see Packable
  979. *
  980. * @demo {@link https://sandcastle.cesium.com/index.html?src=Corridor.html|Cesium Sandcastle Corridor Demo}
  981. *
  982. * @example
  983. * const corridor = new Cesium.CorridorGeometry({
  984. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY,
  985. * positions : Cesium.Cartesian3.fromDegreesArray([-72.0, 40.0, -70.0, 35.0]),
  986. * width : 100000
  987. * });
  988. */
  989. function CorridorGeometry(options) {
  990. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  991. const positions = options.positions;
  992. const width = options.width;
  993. //>>includeStart('debug', pragmas.debug);
  994. RuntimeError.Check.defined("options.positions", positions);
  995. RuntimeError.Check.defined("options.width", width);
  996. //>>includeEnd('debug');
  997. const height = defaultValue.defaultValue(options.height, 0.0);
  998. const extrudedHeight = defaultValue.defaultValue(options.extrudedHeight, height);
  999. this._positions = positions;
  1000. this._ellipsoid = Matrix2.Ellipsoid.clone(
  1001. defaultValue.defaultValue(options.ellipsoid, Matrix2.Ellipsoid.WGS84)
  1002. );
  1003. this._vertexFormat = VertexFormat.VertexFormat.clone(
  1004. defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT)
  1005. );
  1006. this._width = width;
  1007. this._height = Math.max(height, extrudedHeight);
  1008. this._extrudedHeight = Math.min(height, extrudedHeight);
  1009. this._cornerType = defaultValue.defaultValue(options.cornerType, PolylineVolumeGeometryLibrary.CornerType.ROUNDED);
  1010. this._granularity = defaultValue.defaultValue(
  1011. options.granularity,
  1012. ComponentDatatype.CesiumMath.RADIANS_PER_DEGREE
  1013. );
  1014. this._shadowVolume = defaultValue.defaultValue(options.shadowVolume, false);
  1015. this._workerName = "createCorridorGeometry";
  1016. this._offsetAttribute = options.offsetAttribute;
  1017. this._rectangle = undefined;
  1018. /**
  1019. * The number of elements used to pack the object into an array.
  1020. * @type {Number}
  1021. */
  1022. this.packedLength =
  1023. 1 +
  1024. positions.length * Matrix2.Cartesian3.packedLength +
  1025. Matrix2.Ellipsoid.packedLength +
  1026. VertexFormat.VertexFormat.packedLength +
  1027. 7;
  1028. }
  1029. /**
  1030. * Stores the provided instance into the provided array.
  1031. *
  1032. * @param {CorridorGeometry} value The value to pack.
  1033. * @param {Number[]} array The array to pack into.
  1034. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1035. *
  1036. * @returns {Number[]} The array that was packed into
  1037. */
  1038. CorridorGeometry.pack = function (value, array, startingIndex) {
  1039. //>>includeStart('debug', pragmas.debug);
  1040. RuntimeError.Check.defined("value", value);
  1041. RuntimeError.Check.defined("array", array);
  1042. //>>includeEnd('debug');
  1043. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1044. const positions = value._positions;
  1045. const length = positions.length;
  1046. array[startingIndex++] = length;
  1047. for (let i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian3.packedLength) {
  1048. Matrix2.Cartesian3.pack(positions[i], array, startingIndex);
  1049. }
  1050. Matrix2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  1051. startingIndex += Matrix2.Ellipsoid.packedLength;
  1052. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  1053. startingIndex += VertexFormat.VertexFormat.packedLength;
  1054. array[startingIndex++] = value._width;
  1055. array[startingIndex++] = value._height;
  1056. array[startingIndex++] = value._extrudedHeight;
  1057. array[startingIndex++] = value._cornerType;
  1058. array[startingIndex++] = value._granularity;
  1059. array[startingIndex++] = value._shadowVolume ? 1.0 : 0.0;
  1060. array[startingIndex] = defaultValue.defaultValue(value._offsetAttribute, -1);
  1061. return array;
  1062. };
  1063. const scratchEllipsoid = Matrix2.Ellipsoid.clone(Matrix2.Ellipsoid.UNIT_SPHERE);
  1064. const scratchVertexFormat = new VertexFormat.VertexFormat();
  1065. const scratchOptions = {
  1066. positions: undefined,
  1067. ellipsoid: scratchEllipsoid,
  1068. vertexFormat: scratchVertexFormat,
  1069. width: undefined,
  1070. height: undefined,
  1071. extrudedHeight: undefined,
  1072. cornerType: undefined,
  1073. granularity: undefined,
  1074. shadowVolume: undefined,
  1075. offsetAttribute: undefined,
  1076. };
  1077. /**
  1078. * Retrieves an instance from a packed array.
  1079. *
  1080. * @param {Number[]} array The packed array.
  1081. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1082. * @param {CorridorGeometry} [result] The object into which to store the result.
  1083. * @returns {CorridorGeometry} The modified result parameter or a new CorridorGeometry instance if one was not provided.
  1084. */
  1085. CorridorGeometry.unpack = function (array, startingIndex, result) {
  1086. //>>includeStart('debug', pragmas.debug);
  1087. RuntimeError.Check.defined("array", array);
  1088. //>>includeEnd('debug');
  1089. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  1090. const length = array[startingIndex++];
  1091. const positions = new Array(length);
  1092. for (let i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian3.packedLength) {
  1093. positions[i] = Matrix2.Cartesian3.unpack(array, startingIndex);
  1094. }
  1095. const ellipsoid = Matrix2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  1096. startingIndex += Matrix2.Ellipsoid.packedLength;
  1097. const vertexFormat = VertexFormat.VertexFormat.unpack(
  1098. array,
  1099. startingIndex,
  1100. scratchVertexFormat
  1101. );
  1102. startingIndex += VertexFormat.VertexFormat.packedLength;
  1103. const width = array[startingIndex++];
  1104. const height = array[startingIndex++];
  1105. const extrudedHeight = array[startingIndex++];
  1106. const cornerType = array[startingIndex++];
  1107. const granularity = array[startingIndex++];
  1108. const shadowVolume = array[startingIndex++] === 1.0;
  1109. const offsetAttribute = array[startingIndex];
  1110. if (!defaultValue.defined(result)) {
  1111. scratchOptions.positions = positions;
  1112. scratchOptions.width = width;
  1113. scratchOptions.height = height;
  1114. scratchOptions.extrudedHeight = extrudedHeight;
  1115. scratchOptions.cornerType = cornerType;
  1116. scratchOptions.granularity = granularity;
  1117. scratchOptions.shadowVolume = shadowVolume;
  1118. scratchOptions.offsetAttribute =
  1119. offsetAttribute === -1 ? undefined : offsetAttribute;
  1120. return new CorridorGeometry(scratchOptions);
  1121. }
  1122. result._positions = positions;
  1123. result._ellipsoid = Matrix2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  1124. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  1125. result._width = width;
  1126. result._height = height;
  1127. result._extrudedHeight = extrudedHeight;
  1128. result._cornerType = cornerType;
  1129. result._granularity = granularity;
  1130. result._shadowVolume = shadowVolume;
  1131. result._offsetAttribute =
  1132. offsetAttribute === -1 ? undefined : offsetAttribute;
  1133. return result;
  1134. };
  1135. /**
  1136. * Computes the bounding rectangle given the provided options
  1137. *
  1138. * @param {Object} options Object with the following properties:
  1139. * @param {Cartesian3[]} options.positions An array of positions that define the center of the corridor.
  1140. * @param {Number} options.width The distance between the edges of the corridor in meters.
  1141. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  1142. * @param {CornerType} [options.cornerType=CornerType.ROUNDED] Determines the style of the corners.
  1143. * @param {Rectangle} [result] An object in which to store the result.
  1144. *
  1145. * @returns {Rectangle} The result rectangle.
  1146. */
  1147. CorridorGeometry.computeRectangle = function (options, result) {
  1148. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  1149. const positions = options.positions;
  1150. const width = options.width;
  1151. //>>includeStart('debug', pragmas.debug);
  1152. RuntimeError.Check.defined("options.positions", positions);
  1153. RuntimeError.Check.defined("options.width", width);
  1154. //>>includeEnd('debug');
  1155. const ellipsoid = defaultValue.defaultValue(options.ellipsoid, Matrix2.Ellipsoid.WGS84);
  1156. const cornerType = defaultValue.defaultValue(options.cornerType, PolylineVolumeGeometryLibrary.CornerType.ROUNDED);
  1157. return computeRectangle(positions, ellipsoid, width, cornerType, result);
  1158. };
  1159. /**
  1160. * Computes the geometric representation of a corridor, including its vertices, indices, and a bounding sphere.
  1161. *
  1162. * @param {CorridorGeometry} corridorGeometry A description of the corridor.
  1163. * @returns {Geometry|undefined} The computed vertices and indices.
  1164. */
  1165. CorridorGeometry.createGeometry = function (corridorGeometry) {
  1166. let positions = corridorGeometry._positions;
  1167. const width = corridorGeometry._width;
  1168. const ellipsoid = corridorGeometry._ellipsoid;
  1169. positions = scaleToSurface(positions, ellipsoid);
  1170. const cleanPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  1171. positions,
  1172. Matrix2.Cartesian3.equalsEpsilon
  1173. );
  1174. if (cleanPositions.length < 2 || width <= 0) {
  1175. return;
  1176. }
  1177. const height = corridorGeometry._height;
  1178. const extrudedHeight = corridorGeometry._extrudedHeight;
  1179. const extrude = !ComponentDatatype.CesiumMath.equalsEpsilon(
  1180. height,
  1181. extrudedHeight,
  1182. 0,
  1183. ComponentDatatype.CesiumMath.EPSILON2
  1184. );
  1185. const vertexFormat = corridorGeometry._vertexFormat;
  1186. const params = {
  1187. ellipsoid: ellipsoid,
  1188. positions: cleanPositions,
  1189. width: width,
  1190. cornerType: corridorGeometry._cornerType,
  1191. granularity: corridorGeometry._granularity,
  1192. saveAttributes: true,
  1193. };
  1194. let attr;
  1195. if (extrude) {
  1196. params.height = height;
  1197. params.extrudedHeight = extrudedHeight;
  1198. params.shadowVolume = corridorGeometry._shadowVolume;
  1199. params.offsetAttribute = corridorGeometry._offsetAttribute;
  1200. attr = computePositionsExtruded(params, vertexFormat);
  1201. } else {
  1202. const computedPositions = CorridorGeometryLibrary.CorridorGeometryLibrary.computePositions(params);
  1203. attr = combine(computedPositions, vertexFormat, ellipsoid);
  1204. attr.attributes.position.values = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(
  1205. attr.attributes.position.values,
  1206. height,
  1207. ellipsoid
  1208. );
  1209. if (defaultValue.defined(corridorGeometry._offsetAttribute)) {
  1210. const applyOffsetValue =
  1211. corridorGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE
  1212. ? 0
  1213. : 1;
  1214. const length = attr.attributes.position.values.length;
  1215. const applyOffset = new Uint8Array(length / 3);
  1216. GeometryOffsetAttribute.arrayFill(applyOffset, applyOffsetValue);
  1217. attr.attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  1218. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  1219. componentsPerAttribute: 1,
  1220. values: applyOffset,
  1221. });
  1222. }
  1223. }
  1224. const attributes = attr.attributes;
  1225. const boundingSphere = Transforms.BoundingSphere.fromVertices(
  1226. attributes.position.values,
  1227. undefined,
  1228. 3
  1229. );
  1230. if (!vertexFormat.position) {
  1231. attr.attributes.position.values = undefined;
  1232. }
  1233. return new GeometryAttribute.Geometry({
  1234. attributes: attributes,
  1235. indices: attr.indices,
  1236. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  1237. boundingSphere: boundingSphere,
  1238. offsetAttribute: corridorGeometry._offsetAttribute,
  1239. });
  1240. };
  1241. /**
  1242. * @private
  1243. */
  1244. CorridorGeometry.createShadowVolume = function (
  1245. corridorGeometry,
  1246. minHeightFunc,
  1247. maxHeightFunc
  1248. ) {
  1249. const granularity = corridorGeometry._granularity;
  1250. const ellipsoid = corridorGeometry._ellipsoid;
  1251. const minHeight = minHeightFunc(granularity, ellipsoid);
  1252. const maxHeight = maxHeightFunc(granularity, ellipsoid);
  1253. return new CorridorGeometry({
  1254. positions: corridorGeometry._positions,
  1255. width: corridorGeometry._width,
  1256. cornerType: corridorGeometry._cornerType,
  1257. ellipsoid: ellipsoid,
  1258. granularity: granularity,
  1259. extrudedHeight: minHeight,
  1260. height: maxHeight,
  1261. vertexFormat: VertexFormat.VertexFormat.POSITION_ONLY,
  1262. shadowVolume: true,
  1263. });
  1264. };
  1265. Object.defineProperties(CorridorGeometry.prototype, {
  1266. /**
  1267. * @private
  1268. */
  1269. rectangle: {
  1270. get: function () {
  1271. if (!defaultValue.defined(this._rectangle)) {
  1272. this._rectangle = computeRectangle(
  1273. this._positions,
  1274. this._ellipsoid,
  1275. this._width,
  1276. this._cornerType
  1277. );
  1278. }
  1279. return this._rectangle;
  1280. },
  1281. },
  1282. /**
  1283. * For remapping texture coordinates when rendering CorridorGeometries as GroundPrimitives.
  1284. *
  1285. * Corridors don't support stRotation,
  1286. * so just return the corners of the original system.
  1287. * @private
  1288. */
  1289. textureCoordinateRotationPoints: {
  1290. get: function () {
  1291. return [0, 0, 0, 1, 1, 0];
  1292. },
  1293. },
  1294. });
  1295. function createCorridorGeometry(corridorGeometry, offset) {
  1296. if (defaultValue.defined(offset)) {
  1297. corridorGeometry = CorridorGeometry.unpack(corridorGeometry, offset);
  1298. }
  1299. corridorGeometry._ellipsoid = Matrix2.Ellipsoid.clone(corridorGeometry._ellipsoid);
  1300. return CorridorGeometry.createGeometry(corridorGeometry);
  1301. }
  1302. return createCorridorGeometry;
  1303. }));