EllipseGeometry.js 41 KB

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