EllipseGeometry.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. import arrayFill from "./arrayFill.js";
  2. import BoundingSphere from "./BoundingSphere.js";
  3. import Cartesian2 from "./Cartesian2.js";
  4. import Cartesian3 from "./Cartesian3.js";
  5. import Cartographic from "./Cartographic.js";
  6. import Check from "./Check.js";
  7. import ComponentDatatype from "./ComponentDatatype.js";
  8. import defaultValue from "./defaultValue.js";
  9. import defined from "./defined.js";
  10. import DeveloperError from "./DeveloperError.js";
  11. import EllipseGeometryLibrary from "./EllipseGeometryLibrary.js";
  12. import Ellipsoid from "./Ellipsoid.js";
  13. import GeographicProjection from "./GeographicProjection.js";
  14. import Geometry from "./Geometry.js";
  15. import GeometryAttribute from "./GeometryAttribute.js";
  16. import GeometryAttributes from "./GeometryAttributes.js";
  17. import GeometryInstance from "./GeometryInstance.js";
  18. import GeometryOffsetAttribute from "./GeometryOffsetAttribute.js";
  19. import GeometryPipeline from "./GeometryPipeline.js";
  20. import IndexDatatype from "./IndexDatatype.js";
  21. import CesiumMath from "./Math.js";
  22. import Matrix3 from "./Matrix3.js";
  23. import PrimitiveType from "./PrimitiveType.js";
  24. import Quaternion from "./Quaternion.js";
  25. import Rectangle from "./Rectangle.js";
  26. import VertexFormat from "./VertexFormat.js";
  27. const scratchCartesian1 = new Cartesian3();
  28. const scratchCartesian2 = new Cartesian3();
  29. const scratchCartesian3 = new Cartesian3();
  30. const scratchCartesian4 = new Cartesian3();
  31. const texCoordScratch = new Cartesian2();
  32. const textureMatrixScratch = new Matrix3();
  33. const tangentMatrixScratch = new Matrix3();
  34. const quaternionScratch = new Quaternion();
  35. const scratchNormal = new Cartesian3();
  36. const scratchTangent = new Cartesian3();
  37. const scratchBitangent = new Cartesian3();
  38. const scratchCartographic = new Cartographic();
  39. const projectedCenterScratch = new Cartesian3();
  40. const scratchMinTexCoord = new Cartesian2();
  41. const scratchMaxTexCoord = new Cartesian2();
  42. function computeTopBottomAttributes(positions, options, extrude) {
  43. const vertexFormat = options.vertexFormat;
  44. const center = options.center;
  45. const semiMajorAxis = options.semiMajorAxis;
  46. const semiMinorAxis = options.semiMinorAxis;
  47. const ellipsoid = options.ellipsoid;
  48. const stRotation = options.stRotation;
  49. const size = extrude ? (positions.length / 3) * 2 : positions.length / 3;
  50. const shadowVolume = options.shadowVolume;
  51. const textureCoordinates = vertexFormat.st
  52. ? new Float32Array(size * 2)
  53. : undefined;
  54. const normals = vertexFormat.normal ? new Float32Array(size * 3) : undefined;
  55. const tangents = vertexFormat.tangent
  56. ? new Float32Array(size * 3)
  57. : undefined;
  58. const bitangents = vertexFormat.bitangent
  59. ? new Float32Array(size * 3)
  60. : undefined;
  61. const extrudeNormals = shadowVolume ? new Float32Array(size * 3) : undefined;
  62. let textureCoordIndex = 0;
  63. // Raise positions to a height above the ellipsoid and compute the
  64. // texture coordinates, normals, tangents, and bitangents.
  65. let normal = scratchNormal;
  66. let tangent = scratchTangent;
  67. let bitangent = scratchBitangent;
  68. const projection = new GeographicProjection(ellipsoid);
  69. const projectedCenter = projection.project(
  70. ellipsoid.cartesianToCartographic(center, scratchCartographic),
  71. projectedCenterScratch
  72. );
  73. const geodeticNormal = ellipsoid.scaleToGeodeticSurface(
  74. center,
  75. scratchCartesian1
  76. );
  77. ellipsoid.geodeticSurfaceNormal(geodeticNormal, geodeticNormal);
  78. let textureMatrix = textureMatrixScratch;
  79. let tangentMatrix = tangentMatrixScratch;
  80. if (stRotation !== 0) {
  81. let rotation = Quaternion.fromAxisAngle(
  82. geodeticNormal,
  83. stRotation,
  84. quaternionScratch
  85. );
  86. textureMatrix = Matrix3.fromQuaternion(rotation, textureMatrix);
  87. rotation = Quaternion.fromAxisAngle(
  88. geodeticNormal,
  89. -stRotation,
  90. quaternionScratch
  91. );
  92. tangentMatrix = Matrix3.fromQuaternion(rotation, tangentMatrix);
  93. } else {
  94. textureMatrix = Matrix3.clone(Matrix3.IDENTITY, textureMatrix);
  95. tangentMatrix = Matrix3.clone(Matrix3.IDENTITY, tangentMatrix);
  96. }
  97. const minTexCoord = Cartesian2.fromElements(
  98. Number.POSITIVE_INFINITY,
  99. Number.POSITIVE_INFINITY,
  100. scratchMinTexCoord
  101. );
  102. const maxTexCoord = Cartesian2.fromElements(
  103. Number.NEGATIVE_INFINITY,
  104. Number.NEGATIVE_INFINITY,
  105. scratchMaxTexCoord
  106. );
  107. let length = positions.length;
  108. const bottomOffset = extrude ? length : 0;
  109. const stOffset = (bottomOffset / 3) * 2;
  110. for (let i = 0; i < length; i += 3) {
  111. const i1 = i + 1;
  112. const i2 = i + 2;
  113. const position = Cartesian3.fromArray(positions, i, scratchCartesian1);
  114. if (vertexFormat.st) {
  115. const rotatedPoint = Matrix3.multiplyByVector(
  116. textureMatrix,
  117. position,
  118. scratchCartesian2
  119. );
  120. const projectedPoint = projection.project(
  121. ellipsoid.cartesianToCartographic(rotatedPoint, scratchCartographic),
  122. scratchCartesian3
  123. );
  124. Cartesian3.subtract(projectedPoint, projectedCenter, projectedPoint);
  125. texCoordScratch.x =
  126. (projectedPoint.x + semiMajorAxis) / (2.0 * semiMajorAxis);
  127. texCoordScratch.y =
  128. (projectedPoint.y + semiMinorAxis) / (2.0 * semiMinorAxis);
  129. minTexCoord.x = Math.min(texCoordScratch.x, minTexCoord.x);
  130. minTexCoord.y = Math.min(texCoordScratch.y, minTexCoord.y);
  131. maxTexCoord.x = Math.max(texCoordScratch.x, maxTexCoord.x);
  132. maxTexCoord.y = Math.max(texCoordScratch.y, maxTexCoord.y);
  133. if (extrude) {
  134. textureCoordinates[textureCoordIndex + stOffset] = texCoordScratch.x;
  135. textureCoordinates[textureCoordIndex + 1 + stOffset] =
  136. texCoordScratch.y;
  137. }
  138. textureCoordinates[textureCoordIndex++] = texCoordScratch.x;
  139. textureCoordinates[textureCoordIndex++] = texCoordScratch.y;
  140. }
  141. if (
  142. vertexFormat.normal ||
  143. vertexFormat.tangent ||
  144. vertexFormat.bitangent ||
  145. shadowVolume
  146. ) {
  147. normal = ellipsoid.geodeticSurfaceNormal(position, normal);
  148. if (shadowVolume) {
  149. extrudeNormals[i + bottomOffset] = -normal.x;
  150. extrudeNormals[i1 + bottomOffset] = -normal.y;
  151. extrudeNormals[i2 + bottomOffset] = -normal.z;
  152. }
  153. if (
  154. vertexFormat.normal ||
  155. vertexFormat.tangent ||
  156. vertexFormat.bitangent
  157. ) {
  158. if (vertexFormat.tangent || vertexFormat.bitangent) {
  159. tangent = Cartesian3.normalize(
  160. Cartesian3.cross(Cartesian3.UNIT_Z, normal, tangent),
  161. tangent
  162. );
  163. Matrix3.multiplyByVector(tangentMatrix, tangent, tangent);
  164. }
  165. if (vertexFormat.normal) {
  166. normals[i] = normal.x;
  167. normals[i1] = normal.y;
  168. normals[i2] = normal.z;
  169. if (extrude) {
  170. normals[i + bottomOffset] = -normal.x;
  171. normals[i1 + bottomOffset] = -normal.y;
  172. normals[i2 + bottomOffset] = -normal.z;
  173. }
  174. }
  175. if (vertexFormat.tangent) {
  176. tangents[i] = tangent.x;
  177. tangents[i1] = tangent.y;
  178. tangents[i2] = tangent.z;
  179. if (extrude) {
  180. tangents[i + bottomOffset] = -tangent.x;
  181. tangents[i1 + bottomOffset] = -tangent.y;
  182. tangents[i2 + bottomOffset] = -tangent.z;
  183. }
  184. }
  185. if (vertexFormat.bitangent) {
  186. bitangent = Cartesian3.normalize(
  187. Cartesian3.cross(normal, tangent, bitangent),
  188. bitangent
  189. );
  190. bitangents[i] = bitangent.x;
  191. bitangents[i1] = bitangent.y;
  192. bitangents[i2] = bitangent.z;
  193. if (extrude) {
  194. bitangents[i + bottomOffset] = bitangent.x;
  195. bitangents[i1 + bottomOffset] = bitangent.y;
  196. bitangents[i2 + bottomOffset] = bitangent.z;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. if (vertexFormat.st) {
  203. length = textureCoordinates.length;
  204. for (let k = 0; k < length; k += 2) {
  205. textureCoordinates[k] =
  206. (textureCoordinates[k] - minTexCoord.x) /
  207. (maxTexCoord.x - minTexCoord.x);
  208. textureCoordinates[k + 1] =
  209. (textureCoordinates[k + 1] - minTexCoord.y) /
  210. (maxTexCoord.y - minTexCoord.y);
  211. }
  212. }
  213. const attributes = new GeometryAttributes();
  214. if (vertexFormat.position) {
  215. const finalPositions = EllipseGeometryLibrary.raisePositionsToHeight(
  216. positions,
  217. options,
  218. extrude
  219. );
  220. attributes.position = new GeometryAttribute({
  221. componentDatatype: ComponentDatatype.DOUBLE,
  222. componentsPerAttribute: 3,
  223. values: finalPositions,
  224. });
  225. }
  226. if (vertexFormat.st) {
  227. attributes.st = new GeometryAttribute({
  228. componentDatatype: ComponentDatatype.FLOAT,
  229. componentsPerAttribute: 2,
  230. values: textureCoordinates,
  231. });
  232. }
  233. if (vertexFormat.normal) {
  234. attributes.normal = new GeometryAttribute({
  235. componentDatatype: ComponentDatatype.FLOAT,
  236. componentsPerAttribute: 3,
  237. values: normals,
  238. });
  239. }
  240. if (vertexFormat.tangent) {
  241. attributes.tangent = new GeometryAttribute({
  242. componentDatatype: ComponentDatatype.FLOAT,
  243. componentsPerAttribute: 3,
  244. values: tangents,
  245. });
  246. }
  247. if (vertexFormat.bitangent) {
  248. attributes.bitangent = new GeometryAttribute({
  249. componentDatatype: ComponentDatatype.FLOAT,
  250. componentsPerAttribute: 3,
  251. values: bitangents,
  252. });
  253. }
  254. if (shadowVolume) {
  255. attributes.extrudeDirection = new GeometryAttribute({
  256. componentDatatype: ComponentDatatype.FLOAT,
  257. componentsPerAttribute: 3,
  258. values: extrudeNormals,
  259. });
  260. }
  261. if (extrude && defined(options.offsetAttribute)) {
  262. let offsetAttribute = new Uint8Array(size);
  263. if (options.offsetAttribute === GeometryOffsetAttribute.TOP) {
  264. offsetAttribute = arrayFill(offsetAttribute, 1, 0, size / 2);
  265. } else {
  266. const offsetValue =
  267. options.offsetAttribute === GeometryOffsetAttribute.NONE ? 0 : 1;
  268. offsetAttribute = arrayFill(offsetAttribute, offsetValue);
  269. }
  270. attributes.applyOffset = new GeometryAttribute({
  271. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  272. componentsPerAttribute: 1,
  273. values: offsetAttribute,
  274. });
  275. }
  276. return attributes;
  277. }
  278. function topIndices(numPts) {
  279. // numTriangles in half = 3 + 8 + 12 + ... = -1 + 4 + (4 + 4) + (4 + 4 + 4) + ... = -1 + 4 * (1 + 2 + 3 + ...)
  280. // = -1 + 4 * ((n * ( n + 1)) / 2)
  281. // total triangles = 2 * numTrangles in half
  282. // indices = total triangles * 3;
  283. // Substitute numPts for n above
  284. const indices = new Array(12 * (numPts * (numPts + 1)) - 6);
  285. let indicesIndex = 0;
  286. let prevIndex;
  287. let numInterior;
  288. let positionIndex;
  289. let i;
  290. let j;
  291. // Indices triangles to the 'right' of the north vector
  292. prevIndex = 0;
  293. positionIndex = 1;
  294. for (i = 0; i < 3; i++) {
  295. indices[indicesIndex++] = positionIndex++;
  296. indices[indicesIndex++] = prevIndex;
  297. indices[indicesIndex++] = positionIndex;
  298. }
  299. for (i = 2; i < numPts + 1; ++i) {
  300. positionIndex = i * (i + 1) - 1;
  301. prevIndex = (i - 1) * i - 1;
  302. indices[indicesIndex++] = positionIndex++;
  303. indices[indicesIndex++] = prevIndex;
  304. indices[indicesIndex++] = positionIndex;
  305. numInterior = 2 * i;
  306. for (j = 0; j < numInterior - 1; ++j) {
  307. indices[indicesIndex++] = positionIndex;
  308. indices[indicesIndex++] = prevIndex++;
  309. indices[indicesIndex++] = prevIndex;
  310. indices[indicesIndex++] = positionIndex++;
  311. indices[indicesIndex++] = prevIndex;
  312. indices[indicesIndex++] = positionIndex;
  313. }
  314. indices[indicesIndex++] = positionIndex++;
  315. indices[indicesIndex++] = prevIndex;
  316. indices[indicesIndex++] = positionIndex;
  317. }
  318. // Indices for center column of triangles
  319. numInterior = numPts * 2;
  320. ++positionIndex;
  321. ++prevIndex;
  322. for (i = 0; i < numInterior - 1; ++i) {
  323. indices[indicesIndex++] = positionIndex;
  324. indices[indicesIndex++] = prevIndex++;
  325. indices[indicesIndex++] = prevIndex;
  326. indices[indicesIndex++] = positionIndex++;
  327. indices[indicesIndex++] = prevIndex;
  328. indices[indicesIndex++] = positionIndex;
  329. }
  330. indices[indicesIndex++] = positionIndex;
  331. indices[indicesIndex++] = prevIndex++;
  332. indices[indicesIndex++] = prevIndex;
  333. indices[indicesIndex++] = positionIndex++;
  334. indices[indicesIndex++] = prevIndex++;
  335. indices[indicesIndex++] = prevIndex;
  336. // Reverse the process creating indices to the 'left' of the north vector
  337. ++prevIndex;
  338. for (i = numPts - 1; i > 1; --i) {
  339. indices[indicesIndex++] = prevIndex++;
  340. indices[indicesIndex++] = prevIndex;
  341. indices[indicesIndex++] = positionIndex;
  342. numInterior = 2 * i;
  343. for (j = 0; j < numInterior - 1; ++j) {
  344. indices[indicesIndex++] = positionIndex;
  345. indices[indicesIndex++] = prevIndex++;
  346. indices[indicesIndex++] = prevIndex;
  347. indices[indicesIndex++] = positionIndex++;
  348. indices[indicesIndex++] = prevIndex;
  349. indices[indicesIndex++] = positionIndex;
  350. }
  351. indices[indicesIndex++] = prevIndex++;
  352. indices[indicesIndex++] = prevIndex++;
  353. indices[indicesIndex++] = positionIndex++;
  354. }
  355. for (i = 0; i < 3; i++) {
  356. indices[indicesIndex++] = prevIndex++;
  357. indices[indicesIndex++] = prevIndex;
  358. indices[indicesIndex++] = positionIndex;
  359. }
  360. return indices;
  361. }
  362. let boundingSphereCenter = new Cartesian3();
  363. function computeEllipse(options) {
  364. const center = options.center;
  365. boundingSphereCenter = Cartesian3.multiplyByScalar(
  366. options.ellipsoid.geodeticSurfaceNormal(center, boundingSphereCenter),
  367. options.height,
  368. boundingSphereCenter
  369. );
  370. boundingSphereCenter = Cartesian3.add(
  371. center,
  372. boundingSphereCenter,
  373. boundingSphereCenter
  374. );
  375. const boundingSphere = new BoundingSphere(
  376. boundingSphereCenter,
  377. options.semiMajorAxis
  378. );
  379. const cep = EllipseGeometryLibrary.computeEllipsePositions(
  380. options,
  381. true,
  382. false
  383. );
  384. const positions = cep.positions;
  385. const numPts = cep.numPts;
  386. const attributes = computeTopBottomAttributes(positions, options, false);
  387. let indices = topIndices(numPts);
  388. indices = IndexDatatype.createTypedArray(positions.length / 3, indices);
  389. return {
  390. boundingSphere: boundingSphere,
  391. attributes: attributes,
  392. indices: indices,
  393. };
  394. }
  395. function computeWallAttributes(positions, options) {
  396. const vertexFormat = options.vertexFormat;
  397. const center = options.center;
  398. const semiMajorAxis = options.semiMajorAxis;
  399. const semiMinorAxis = options.semiMinorAxis;
  400. const ellipsoid = options.ellipsoid;
  401. const height = options.height;
  402. const extrudedHeight = options.extrudedHeight;
  403. const stRotation = options.stRotation;
  404. const size = (positions.length / 3) * 2;
  405. const finalPositions = new Float64Array(size * 3);
  406. const textureCoordinates = vertexFormat.st
  407. ? new Float32Array(size * 2)
  408. : undefined;
  409. const normals = vertexFormat.normal ? new Float32Array(size * 3) : undefined;
  410. const tangents = vertexFormat.tangent
  411. ? new Float32Array(size * 3)
  412. : undefined;
  413. const bitangents = vertexFormat.bitangent
  414. ? new Float32Array(size * 3)
  415. : undefined;
  416. const shadowVolume = options.shadowVolume;
  417. const extrudeNormals = shadowVolume ? new Float32Array(size * 3) : undefined;
  418. let textureCoordIndex = 0;
  419. // Raise positions to a height above the ellipsoid and compute the
  420. // texture coordinates, normals, tangents, and bitangents.
  421. let normal = scratchNormal;
  422. let tangent = scratchTangent;
  423. let bitangent = scratchBitangent;
  424. const projection = new GeographicProjection(ellipsoid);
  425. const projectedCenter = projection.project(
  426. ellipsoid.cartesianToCartographic(center, scratchCartographic),
  427. projectedCenterScratch
  428. );
  429. const geodeticNormal = ellipsoid.scaleToGeodeticSurface(
  430. center,
  431. scratchCartesian1
  432. );
  433. ellipsoid.geodeticSurfaceNormal(geodeticNormal, geodeticNormal);
  434. const rotation = Quaternion.fromAxisAngle(
  435. geodeticNormal,
  436. stRotation,
  437. quaternionScratch
  438. );
  439. const textureMatrix = Matrix3.fromQuaternion(rotation, textureMatrixScratch);
  440. const minTexCoord = Cartesian2.fromElements(
  441. Number.POSITIVE_INFINITY,
  442. Number.POSITIVE_INFINITY,
  443. scratchMinTexCoord
  444. );
  445. const maxTexCoord = Cartesian2.fromElements(
  446. Number.NEGATIVE_INFINITY,
  447. Number.NEGATIVE_INFINITY,
  448. scratchMaxTexCoord
  449. );
  450. let length = positions.length;
  451. const stOffset = (length / 3) * 2;
  452. for (let i = 0; i < length; i += 3) {
  453. const i1 = i + 1;
  454. const i2 = i + 2;
  455. let position = Cartesian3.fromArray(positions, i, scratchCartesian1);
  456. let extrudedPosition;
  457. if (vertexFormat.st) {
  458. const rotatedPoint = Matrix3.multiplyByVector(
  459. textureMatrix,
  460. position,
  461. scratchCartesian2
  462. );
  463. const projectedPoint = projection.project(
  464. ellipsoid.cartesianToCartographic(rotatedPoint, scratchCartographic),
  465. scratchCartesian3
  466. );
  467. Cartesian3.subtract(projectedPoint, projectedCenter, projectedPoint);
  468. texCoordScratch.x =
  469. (projectedPoint.x + semiMajorAxis) / (2.0 * semiMajorAxis);
  470. texCoordScratch.y =
  471. (projectedPoint.y + semiMinorAxis) / (2.0 * semiMinorAxis);
  472. minTexCoord.x = Math.min(texCoordScratch.x, minTexCoord.x);
  473. minTexCoord.y = Math.min(texCoordScratch.y, minTexCoord.y);
  474. maxTexCoord.x = Math.max(texCoordScratch.x, maxTexCoord.x);
  475. maxTexCoord.y = Math.max(texCoordScratch.y, maxTexCoord.y);
  476. textureCoordinates[textureCoordIndex + stOffset] = texCoordScratch.x;
  477. textureCoordinates[textureCoordIndex + 1 + stOffset] = texCoordScratch.y;
  478. textureCoordinates[textureCoordIndex++] = texCoordScratch.x;
  479. textureCoordinates[textureCoordIndex++] = texCoordScratch.y;
  480. }
  481. position = ellipsoid.scaleToGeodeticSurface(position, position);
  482. extrudedPosition = Cartesian3.clone(position, scratchCartesian2);
  483. normal = ellipsoid.geodeticSurfaceNormal(position, normal);
  484. if (shadowVolume) {
  485. extrudeNormals[i + length] = -normal.x;
  486. extrudeNormals[i1 + length] = -normal.y;
  487. extrudeNormals[i2 + length] = -normal.z;
  488. }
  489. let scaledNormal = Cartesian3.multiplyByScalar(
  490. normal,
  491. height,
  492. scratchCartesian4
  493. );
  494. position = Cartesian3.add(position, scaledNormal, position);
  495. scaledNormal = Cartesian3.multiplyByScalar(
  496. normal,
  497. extrudedHeight,
  498. scaledNormal
  499. );
  500. extrudedPosition = Cartesian3.add(
  501. extrudedPosition,
  502. scaledNormal,
  503. extrudedPosition
  504. );
  505. if (vertexFormat.position) {
  506. finalPositions[i + length] = extrudedPosition.x;
  507. finalPositions[i1 + length] = extrudedPosition.y;
  508. finalPositions[i2 + length] = extrudedPosition.z;
  509. finalPositions[i] = position.x;
  510. finalPositions[i1] = position.y;
  511. finalPositions[i2] = position.z;
  512. }
  513. if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent) {
  514. bitangent = Cartesian3.clone(normal, bitangent);
  515. const next = Cartesian3.fromArray(
  516. positions,
  517. (i + 3) % length,
  518. scratchCartesian4
  519. );
  520. Cartesian3.subtract(next, position, next);
  521. const bottom = Cartesian3.subtract(
  522. extrudedPosition,
  523. position,
  524. scratchCartesian3
  525. );
  526. normal = Cartesian3.normalize(
  527. Cartesian3.cross(bottom, next, normal),
  528. normal
  529. );
  530. if (vertexFormat.normal) {
  531. normals[i] = normal.x;
  532. normals[i1] = normal.y;
  533. normals[i2] = normal.z;
  534. normals[i + length] = normal.x;
  535. normals[i1 + length] = normal.y;
  536. normals[i2 + length] = normal.z;
  537. }
  538. if (vertexFormat.tangent) {
  539. tangent = Cartesian3.normalize(
  540. Cartesian3.cross(bitangent, normal, tangent),
  541. tangent
  542. );
  543. tangents[i] = tangent.x;
  544. tangents[i1] = tangent.y;
  545. tangents[i2] = tangent.z;
  546. tangents[i + length] = tangent.x;
  547. tangents[i + 1 + length] = tangent.y;
  548. tangents[i + 2 + length] = tangent.z;
  549. }
  550. if (vertexFormat.bitangent) {
  551. bitangents[i] = bitangent.x;
  552. bitangents[i1] = bitangent.y;
  553. bitangents[i2] = bitangent.z;
  554. bitangents[i + length] = bitangent.x;
  555. bitangents[i1 + length] = bitangent.y;
  556. bitangents[i2 + length] = bitangent.z;
  557. }
  558. }
  559. }
  560. if (vertexFormat.st) {
  561. length = textureCoordinates.length;
  562. for (let k = 0; k < length; k += 2) {
  563. textureCoordinates[k] =
  564. (textureCoordinates[k] - minTexCoord.x) /
  565. (maxTexCoord.x - minTexCoord.x);
  566. textureCoordinates[k + 1] =
  567. (textureCoordinates[k + 1] - minTexCoord.y) /
  568. (maxTexCoord.y - minTexCoord.y);
  569. }
  570. }
  571. const attributes = new GeometryAttributes();
  572. if (vertexFormat.position) {
  573. attributes.position = new GeometryAttribute({
  574. componentDatatype: ComponentDatatype.DOUBLE,
  575. componentsPerAttribute: 3,
  576. values: finalPositions,
  577. });
  578. }
  579. if (vertexFormat.st) {
  580. attributes.st = new GeometryAttribute({
  581. componentDatatype: ComponentDatatype.FLOAT,
  582. componentsPerAttribute: 2,
  583. values: textureCoordinates,
  584. });
  585. }
  586. if (vertexFormat.normal) {
  587. attributes.normal = new GeometryAttribute({
  588. componentDatatype: ComponentDatatype.FLOAT,
  589. componentsPerAttribute: 3,
  590. values: normals,
  591. });
  592. }
  593. if (vertexFormat.tangent) {
  594. attributes.tangent = new GeometryAttribute({
  595. componentDatatype: ComponentDatatype.FLOAT,
  596. componentsPerAttribute: 3,
  597. values: tangents,
  598. });
  599. }
  600. if (vertexFormat.bitangent) {
  601. attributes.bitangent = new GeometryAttribute({
  602. componentDatatype: ComponentDatatype.FLOAT,
  603. componentsPerAttribute: 3,
  604. values: bitangents,
  605. });
  606. }
  607. if (shadowVolume) {
  608. attributes.extrudeDirection = new GeometryAttribute({
  609. componentDatatype: ComponentDatatype.FLOAT,
  610. componentsPerAttribute: 3,
  611. values: extrudeNormals,
  612. });
  613. }
  614. if (defined(options.offsetAttribute)) {
  615. let offsetAttribute = new Uint8Array(size);
  616. if (options.offsetAttribute === GeometryOffsetAttribute.TOP) {
  617. offsetAttribute = arrayFill(offsetAttribute, 1, 0, size / 2);
  618. } else {
  619. const offsetValue =
  620. options.offsetAttribute === GeometryOffsetAttribute.NONE ? 0 : 1;
  621. offsetAttribute = arrayFill(offsetAttribute, offsetValue);
  622. }
  623. attributes.applyOffset = new GeometryAttribute({
  624. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  625. componentsPerAttribute: 1,
  626. values: offsetAttribute,
  627. });
  628. }
  629. return attributes;
  630. }
  631. function computeWallIndices(positions) {
  632. const length = positions.length / 3;
  633. const indices = IndexDatatype.createTypedArray(length, length * 6);
  634. let index = 0;
  635. for (let i = 0; i < length; i++) {
  636. const UL = i;
  637. const LL = i + length;
  638. const UR = (UL + 1) % length;
  639. const LR = UR + length;
  640. indices[index++] = UL;
  641. indices[index++] = LL;
  642. indices[index++] = UR;
  643. indices[index++] = UR;
  644. indices[index++] = LL;
  645. indices[index++] = LR;
  646. }
  647. return indices;
  648. }
  649. const topBoundingSphere = new BoundingSphere();
  650. const bottomBoundingSphere = new BoundingSphere();
  651. function computeExtrudedEllipse(options) {
  652. const center = options.center;
  653. const ellipsoid = options.ellipsoid;
  654. const semiMajorAxis = options.semiMajorAxis;
  655. let scaledNormal = Cartesian3.multiplyByScalar(
  656. ellipsoid.geodeticSurfaceNormal(center, scratchCartesian1),
  657. options.height,
  658. scratchCartesian1
  659. );
  660. topBoundingSphere.center = Cartesian3.add(
  661. center,
  662. scaledNormal,
  663. topBoundingSphere.center
  664. );
  665. topBoundingSphere.radius = semiMajorAxis;
  666. scaledNormal = Cartesian3.multiplyByScalar(
  667. ellipsoid.geodeticSurfaceNormal(center, scaledNormal),
  668. options.extrudedHeight,
  669. scaledNormal
  670. );
  671. bottomBoundingSphere.center = Cartesian3.add(
  672. center,
  673. scaledNormal,
  674. bottomBoundingSphere.center
  675. );
  676. bottomBoundingSphere.radius = semiMajorAxis;
  677. const cep = EllipseGeometryLibrary.computeEllipsePositions(
  678. options,
  679. true,
  680. true
  681. );
  682. const positions = cep.positions;
  683. const numPts = cep.numPts;
  684. const outerPositions = cep.outerPositions;
  685. const boundingSphere = BoundingSphere.union(
  686. topBoundingSphere,
  687. bottomBoundingSphere
  688. );
  689. const topBottomAttributes = computeTopBottomAttributes(
  690. positions,
  691. options,
  692. true
  693. );
  694. let indices = topIndices(numPts);
  695. const length = indices.length;
  696. indices.length = length * 2;
  697. const posLength = positions.length / 3;
  698. for (let i = 0; i < length; i += 3) {
  699. indices[i + length] = indices[i + 2] + posLength;
  700. indices[i + 1 + length] = indices[i + 1] + posLength;
  701. indices[i + 2 + length] = indices[i] + posLength;
  702. }
  703. const topBottomIndices = IndexDatatype.createTypedArray(
  704. (posLength * 2) / 3,
  705. indices
  706. );
  707. const topBottomGeo = new Geometry({
  708. attributes: topBottomAttributes,
  709. indices: topBottomIndices,
  710. primitiveType: PrimitiveType.TRIANGLES,
  711. });
  712. const wallAttributes = computeWallAttributes(outerPositions, options);
  713. indices = computeWallIndices(outerPositions);
  714. const wallIndices = IndexDatatype.createTypedArray(
  715. (outerPositions.length * 2) / 3,
  716. indices
  717. );
  718. const wallGeo = new Geometry({
  719. attributes: wallAttributes,
  720. indices: wallIndices,
  721. primitiveType: PrimitiveType.TRIANGLES,
  722. });
  723. const geo = GeometryPipeline.combineInstances([
  724. new GeometryInstance({
  725. geometry: topBottomGeo,
  726. }),
  727. new GeometryInstance({
  728. geometry: wallGeo,
  729. }),
  730. ]);
  731. return {
  732. boundingSphere: boundingSphere,
  733. attributes: geo[0].attributes,
  734. indices: geo[0].indices,
  735. };
  736. }
  737. function computeRectangle(
  738. center,
  739. semiMajorAxis,
  740. semiMinorAxis,
  741. rotation,
  742. granularity,
  743. ellipsoid,
  744. result
  745. ) {
  746. const cep = EllipseGeometryLibrary.computeEllipsePositions(
  747. {
  748. center: center,
  749. semiMajorAxis: semiMajorAxis,
  750. semiMinorAxis: semiMinorAxis,
  751. rotation: rotation,
  752. granularity: granularity,
  753. },
  754. false,
  755. true
  756. );
  757. const positionsFlat = cep.outerPositions;
  758. const positionsCount = positionsFlat.length / 3;
  759. const positions = new Array(positionsCount);
  760. for (let i = 0; i < positionsCount; ++i) {
  761. positions[i] = Cartesian3.fromArray(positionsFlat, i * 3);
  762. }
  763. const rectangle = Rectangle.fromCartesianArray(positions, ellipsoid, result);
  764. // Rectangle width goes beyond 180 degrees when the ellipse crosses a pole.
  765. // When this happens, make the rectangle into a "circle" around the pole
  766. if (rectangle.width > CesiumMath.PI) {
  767. rectangle.north =
  768. rectangle.north > 0.0
  769. ? CesiumMath.PI_OVER_TWO - CesiumMath.EPSILON7
  770. : rectangle.north;
  771. rectangle.south =
  772. rectangle.south < 0.0
  773. ? CesiumMath.EPSILON7 - CesiumMath.PI_OVER_TWO
  774. : rectangle.south;
  775. rectangle.east = CesiumMath.PI;
  776. rectangle.west = -CesiumMath.PI;
  777. }
  778. return rectangle;
  779. }
  780. /**
  781. * A description of an ellipse on an ellipsoid. Ellipse geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  782. *
  783. * @alias EllipseGeometry
  784. * @constructor
  785. *
  786. * @param {Object} options Object with the following properties:
  787. * @param {Cartesian3} options.center The ellipse's center point in the fixed frame.
  788. * @param {Number} options.semiMajorAxis The length of the ellipse's semi-major axis in meters.
  789. * @param {Number} options.semiMinorAxis The length of the ellipse's semi-minor axis in meters.
  790. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid the ellipse will be on.
  791. * @param {Number} [options.height=0.0] The distance in meters between the ellipse and the ellipsoid surface.
  792. * @param {Number} [options.extrudedHeight] The distance in meters between the ellipse's extruded face and the ellipsoid surface.
  793. * @param {Number} [options.rotation=0.0] The angle of rotation counter-clockwise from north.
  794. * @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates counter-clockwise from north.
  795. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The angular distance between points on the ellipse in radians.
  796. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  797. *
  798. * @exception {DeveloperError} semiMajorAxis and semiMinorAxis must be greater than zero.
  799. * @exception {DeveloperError} semiMajorAxis must be greater than or equal to the semiMinorAxis.
  800. * @exception {DeveloperError} granularity must be greater than zero.
  801. *
  802. *
  803. * @example
  804. * // Create an ellipse.
  805. * const ellipse = new Cesium.EllipseGeometry({
  806. * center : Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883),
  807. * semiMajorAxis : 500000.0,
  808. * semiMinorAxis : 300000.0,
  809. * rotation : Cesium.Math.toRadians(60.0)
  810. * });
  811. * const geometry = Cesium.EllipseGeometry.createGeometry(ellipse);
  812. *
  813. * @see EllipseGeometry.createGeometry
  814. */
  815. function EllipseGeometry(options) {
  816. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  817. const center = options.center;
  818. const ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  819. const semiMajorAxis = options.semiMajorAxis;
  820. const semiMinorAxis = options.semiMinorAxis;
  821. const granularity = defaultValue(
  822. options.granularity,
  823. CesiumMath.RADIANS_PER_DEGREE
  824. );
  825. const vertexFormat = defaultValue(options.vertexFormat, VertexFormat.DEFAULT);
  826. //>>includeStart('debug', pragmas.debug);
  827. Check.defined("options.center", center);
  828. Check.typeOf.number("options.semiMajorAxis", semiMajorAxis);
  829. Check.typeOf.number("options.semiMinorAxis", semiMinorAxis);
  830. if (semiMajorAxis < semiMinorAxis) {
  831. throw new DeveloperError(
  832. "semiMajorAxis must be greater than or equal to the semiMinorAxis."
  833. );
  834. }
  835. if (granularity <= 0.0) {
  836. throw new DeveloperError("granularity must be greater than zero.");
  837. }
  838. //>>includeEnd('debug');
  839. const height = defaultValue(options.height, 0.0);
  840. const extrudedHeight = defaultValue(options.extrudedHeight, height);
  841. this._center = Cartesian3.clone(center);
  842. this._semiMajorAxis = semiMajorAxis;
  843. this._semiMinorAxis = semiMinorAxis;
  844. this._ellipsoid = Ellipsoid.clone(ellipsoid);
  845. this._rotation = defaultValue(options.rotation, 0.0);
  846. this._stRotation = defaultValue(options.stRotation, 0.0);
  847. this._height = Math.max(extrudedHeight, height);
  848. this._granularity = granularity;
  849. this._vertexFormat = VertexFormat.clone(vertexFormat);
  850. this._extrudedHeight = Math.min(extrudedHeight, height);
  851. this._shadowVolume = defaultValue(options.shadowVolume, false);
  852. this._workerName = "createEllipseGeometry";
  853. this._offsetAttribute = options.offsetAttribute;
  854. this._rectangle = undefined;
  855. this._textureCoordinateRotationPoints = undefined;
  856. }
  857. /**
  858. * The number of elements used to pack the object into an array.
  859. * @type {Number}
  860. */
  861. EllipseGeometry.packedLength =
  862. Cartesian3.packedLength +
  863. Ellipsoid.packedLength +
  864. VertexFormat.packedLength +
  865. 9;
  866. /**
  867. * Stores the provided instance into the provided array.
  868. *
  869. * @param {EllipseGeometry} value The value to pack.
  870. * @param {Number[]} array The array to pack into.
  871. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  872. *
  873. * @returns {Number[]} The array that was packed into
  874. */
  875. EllipseGeometry.pack = function (value, array, startingIndex) {
  876. //>>includeStart('debug', pragmas.debug);
  877. Check.defined("value", value);
  878. Check.defined("array", array);
  879. //>>includeEnd('debug');
  880. startingIndex = defaultValue(startingIndex, 0);
  881. Cartesian3.pack(value._center, array, startingIndex);
  882. startingIndex += Cartesian3.packedLength;
  883. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  884. startingIndex += Ellipsoid.packedLength;
  885. VertexFormat.pack(value._vertexFormat, array, startingIndex);
  886. startingIndex += VertexFormat.packedLength;
  887. array[startingIndex++] = value._semiMajorAxis;
  888. array[startingIndex++] = value._semiMinorAxis;
  889. array[startingIndex++] = value._rotation;
  890. array[startingIndex++] = value._stRotation;
  891. array[startingIndex++] = value._height;
  892. array[startingIndex++] = value._granularity;
  893. array[startingIndex++] = value._extrudedHeight;
  894. array[startingIndex++] = value._shadowVolume ? 1.0 : 0.0;
  895. array[startingIndex] = defaultValue(value._offsetAttribute, -1);
  896. return array;
  897. };
  898. const scratchCenter = new Cartesian3();
  899. const scratchEllipsoid = new Ellipsoid();
  900. const scratchVertexFormat = new VertexFormat();
  901. const scratchOptions = {
  902. center: scratchCenter,
  903. ellipsoid: scratchEllipsoid,
  904. vertexFormat: scratchVertexFormat,
  905. semiMajorAxis: undefined,
  906. semiMinorAxis: undefined,
  907. rotation: undefined,
  908. stRotation: undefined,
  909. height: undefined,
  910. granularity: undefined,
  911. extrudedHeight: undefined,
  912. shadowVolume: undefined,
  913. offsetAttribute: undefined,
  914. };
  915. /**
  916. * Retrieves an instance from a packed array.
  917. *
  918. * @param {Number[]} array The packed array.
  919. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  920. * @param {EllipseGeometry} [result] The object into which to store the result.
  921. * @returns {EllipseGeometry} The modified result parameter or a new EllipseGeometry instance if one was not provided.
  922. */
  923. EllipseGeometry.unpack = function (array, startingIndex, result) {
  924. //>>includeStart('debug', pragmas.debug);
  925. Check.defined("array", array);
  926. //>>includeEnd('debug');
  927. startingIndex = defaultValue(startingIndex, 0);
  928. const center = Cartesian3.unpack(array, startingIndex, scratchCenter);
  929. startingIndex += Cartesian3.packedLength;
  930. const ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  931. startingIndex += Ellipsoid.packedLength;
  932. const vertexFormat = VertexFormat.unpack(
  933. array,
  934. startingIndex,
  935. scratchVertexFormat
  936. );
  937. startingIndex += VertexFormat.packedLength;
  938. const semiMajorAxis = array[startingIndex++];
  939. const semiMinorAxis = array[startingIndex++];
  940. const rotation = array[startingIndex++];
  941. const stRotation = array[startingIndex++];
  942. const height = array[startingIndex++];
  943. const granularity = array[startingIndex++];
  944. const extrudedHeight = array[startingIndex++];
  945. const shadowVolume = array[startingIndex++] === 1.0;
  946. const offsetAttribute = array[startingIndex];
  947. if (!defined(result)) {
  948. scratchOptions.height = height;
  949. scratchOptions.extrudedHeight = extrudedHeight;
  950. scratchOptions.granularity = granularity;
  951. scratchOptions.stRotation = stRotation;
  952. scratchOptions.rotation = rotation;
  953. scratchOptions.semiMajorAxis = semiMajorAxis;
  954. scratchOptions.semiMinorAxis = semiMinorAxis;
  955. scratchOptions.shadowVolume = shadowVolume;
  956. scratchOptions.offsetAttribute =
  957. offsetAttribute === -1 ? undefined : offsetAttribute;
  958. return new EllipseGeometry(scratchOptions);
  959. }
  960. result._center = Cartesian3.clone(center, result._center);
  961. result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
  962. result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
  963. result._semiMajorAxis = semiMajorAxis;
  964. result._semiMinorAxis = semiMinorAxis;
  965. result._rotation = rotation;
  966. result._stRotation = stRotation;
  967. result._height = height;
  968. result._granularity = granularity;
  969. result._extrudedHeight = extrudedHeight;
  970. result._shadowVolume = shadowVolume;
  971. result._offsetAttribute =
  972. offsetAttribute === -1 ? undefined : offsetAttribute;
  973. return result;
  974. };
  975. /**
  976. * Computes the bounding rectangle based on the provided options
  977. *
  978. * @param {Object} options Object with the following properties:
  979. * @param {Cartesian3} options.center The ellipse's center point in the fixed frame.
  980. * @param {Number} options.semiMajorAxis The length of the ellipse's semi-major axis in meters.
  981. * @param {Number} options.semiMinorAxis The length of the ellipse's semi-minor axis in meters.
  982. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid the ellipse will be on.
  983. * @param {Number} [options.rotation=0.0] The angle of rotation counter-clockwise from north.
  984. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The angular distance between points on the ellipse in radians.
  985. * @param {Rectangle} [result] An object in which to store the result
  986. *
  987. * @returns {Rectangle} The result rectangle
  988. */
  989. EllipseGeometry.computeRectangle = function (options, result) {
  990. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  991. const center = options.center;
  992. const ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  993. const semiMajorAxis = options.semiMajorAxis;
  994. const semiMinorAxis = options.semiMinorAxis;
  995. const granularity = defaultValue(
  996. options.granularity,
  997. CesiumMath.RADIANS_PER_DEGREE
  998. );
  999. const rotation = defaultValue(options.rotation, 0.0);
  1000. //>>includeStart('debug', pragmas.debug);
  1001. Check.defined("options.center", center);
  1002. Check.typeOf.number("options.semiMajorAxis", semiMajorAxis);
  1003. Check.typeOf.number("options.semiMinorAxis", semiMinorAxis);
  1004. if (semiMajorAxis < semiMinorAxis) {
  1005. throw new DeveloperError(
  1006. "semiMajorAxis must be greater than or equal to the semiMinorAxis."
  1007. );
  1008. }
  1009. if (granularity <= 0.0) {
  1010. throw new DeveloperError("granularity must be greater than zero.");
  1011. }
  1012. //>>includeEnd('debug');
  1013. return computeRectangle(
  1014. center,
  1015. semiMajorAxis,
  1016. semiMinorAxis,
  1017. rotation,
  1018. granularity,
  1019. ellipsoid,
  1020. result
  1021. );
  1022. };
  1023. /**
  1024. * Computes the geometric representation of a ellipse on an ellipsoid, including its vertices, indices, and a bounding sphere.
  1025. *
  1026. * @param {EllipseGeometry} ellipseGeometry A description of the ellipse.
  1027. * @returns {Geometry|undefined} The computed vertices and indices.
  1028. */
  1029. EllipseGeometry.createGeometry = function (ellipseGeometry) {
  1030. if (
  1031. ellipseGeometry._semiMajorAxis <= 0.0 ||
  1032. ellipseGeometry._semiMinorAxis <= 0.0
  1033. ) {
  1034. return;
  1035. }
  1036. const height = ellipseGeometry._height;
  1037. const extrudedHeight = ellipseGeometry._extrudedHeight;
  1038. const extrude = !CesiumMath.equalsEpsilon(
  1039. height,
  1040. extrudedHeight,
  1041. 0,
  1042. CesiumMath.EPSILON2
  1043. );
  1044. ellipseGeometry._center = ellipseGeometry._ellipsoid.scaleToGeodeticSurface(
  1045. ellipseGeometry._center,
  1046. ellipseGeometry._center
  1047. );
  1048. const options = {
  1049. center: ellipseGeometry._center,
  1050. semiMajorAxis: ellipseGeometry._semiMajorAxis,
  1051. semiMinorAxis: ellipseGeometry._semiMinorAxis,
  1052. ellipsoid: ellipseGeometry._ellipsoid,
  1053. rotation: ellipseGeometry._rotation,
  1054. height: height,
  1055. granularity: ellipseGeometry._granularity,
  1056. vertexFormat: ellipseGeometry._vertexFormat,
  1057. stRotation: ellipseGeometry._stRotation,
  1058. };
  1059. let geometry;
  1060. if (extrude) {
  1061. options.extrudedHeight = extrudedHeight;
  1062. options.shadowVolume = ellipseGeometry._shadowVolume;
  1063. options.offsetAttribute = ellipseGeometry._offsetAttribute;
  1064. geometry = computeExtrudedEllipse(options);
  1065. } else {
  1066. geometry = computeEllipse(options);
  1067. if (defined(ellipseGeometry._offsetAttribute)) {
  1068. const length = geometry.attributes.position.values.length;
  1069. const applyOffset = new Uint8Array(length / 3);
  1070. const offsetValue =
  1071. ellipseGeometry._offsetAttribute === GeometryOffsetAttribute.NONE
  1072. ? 0
  1073. : 1;
  1074. arrayFill(applyOffset, offsetValue);
  1075. geometry.attributes.applyOffset = new GeometryAttribute({
  1076. componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
  1077. componentsPerAttribute: 1,
  1078. values: applyOffset,
  1079. });
  1080. }
  1081. }
  1082. return new Geometry({
  1083. attributes: geometry.attributes,
  1084. indices: geometry.indices,
  1085. primitiveType: PrimitiveType.TRIANGLES,
  1086. boundingSphere: geometry.boundingSphere,
  1087. offsetAttribute: ellipseGeometry._offsetAttribute,
  1088. });
  1089. };
  1090. /**
  1091. * @private
  1092. */
  1093. EllipseGeometry.createShadowVolume = function (
  1094. ellipseGeometry,
  1095. minHeightFunc,
  1096. maxHeightFunc
  1097. ) {
  1098. const granularity = ellipseGeometry._granularity;
  1099. const ellipsoid = ellipseGeometry._ellipsoid;
  1100. const minHeight = minHeightFunc(granularity, ellipsoid);
  1101. const maxHeight = maxHeightFunc(granularity, ellipsoid);
  1102. return new EllipseGeometry({
  1103. center: ellipseGeometry._center,
  1104. semiMajorAxis: ellipseGeometry._semiMajorAxis,
  1105. semiMinorAxis: ellipseGeometry._semiMinorAxis,
  1106. ellipsoid: ellipsoid,
  1107. rotation: ellipseGeometry._rotation,
  1108. stRotation: ellipseGeometry._stRotation,
  1109. granularity: granularity,
  1110. extrudedHeight: minHeight,
  1111. height: maxHeight,
  1112. vertexFormat: VertexFormat.POSITION_ONLY,
  1113. shadowVolume: true,
  1114. });
  1115. };
  1116. function textureCoordinateRotationPoints(ellipseGeometry) {
  1117. const stRotation = -ellipseGeometry._stRotation;
  1118. if (stRotation === 0.0) {
  1119. return [0, 0, 0, 1, 1, 0];
  1120. }
  1121. const cep = EllipseGeometryLibrary.computeEllipsePositions(
  1122. {
  1123. center: ellipseGeometry._center,
  1124. semiMajorAxis: ellipseGeometry._semiMajorAxis,
  1125. semiMinorAxis: ellipseGeometry._semiMinorAxis,
  1126. rotation: ellipseGeometry._rotation,
  1127. granularity: ellipseGeometry._granularity,
  1128. },
  1129. false,
  1130. true
  1131. );
  1132. const positionsFlat = cep.outerPositions;
  1133. const positionsCount = positionsFlat.length / 3;
  1134. const positions = new Array(positionsCount);
  1135. for (let i = 0; i < positionsCount; ++i) {
  1136. positions[i] = Cartesian3.fromArray(positionsFlat, i * 3);
  1137. }
  1138. const ellipsoid = ellipseGeometry._ellipsoid;
  1139. const boundingRectangle = ellipseGeometry.rectangle;
  1140. return Geometry._textureCoordinateRotationPoints(
  1141. positions,
  1142. stRotation,
  1143. ellipsoid,
  1144. boundingRectangle
  1145. );
  1146. }
  1147. Object.defineProperties(EllipseGeometry.prototype, {
  1148. /**
  1149. * @private
  1150. */
  1151. rectangle: {
  1152. get: function () {
  1153. if (!defined(this._rectangle)) {
  1154. this._rectangle = computeRectangle(
  1155. this._center,
  1156. this._semiMajorAxis,
  1157. this._semiMinorAxis,
  1158. this._rotation,
  1159. this._granularity,
  1160. this._ellipsoid
  1161. );
  1162. }
  1163. return this._rectangle;
  1164. },
  1165. },
  1166. /**
  1167. * For remapping texture coordinates when rendering EllipseGeometries as GroundPrimitives.
  1168. * @private
  1169. */
  1170. textureCoordinateRotationPoints: {
  1171. get: function () {
  1172. if (!defined(this._textureCoordinateRotationPoints)) {
  1173. this._textureCoordinateRotationPoints = textureCoordinateRotationPoints(
  1174. this
  1175. );
  1176. }
  1177. return this._textureCoordinateRotationPoints;
  1178. },
  1179. },
  1180. });
  1181. export default EllipseGeometry;