EllipseGeometry.js 41 KB

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