Cartesian2-33d2657c.js 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109
  1. /**
  2. * Cesium - https://github.com/CesiumGS/cesium
  3. *
  4. * Copyright 2011-2020 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/CesiumGS/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-54c2dc71', './Check-6c0211bc', './Math-1124a290'], function (exports, when, Check, _Math) { 'use strict';
  24. /**
  25. * A 3D Cartesian point.
  26. * @alias Cartesian3
  27. * @constructor
  28. *
  29. * @param {Number} [x=0.0] The X component.
  30. * @param {Number} [y=0.0] The Y component.
  31. * @param {Number} [z=0.0] The Z component.
  32. *
  33. * @see Cartesian2
  34. * @see Cartesian4
  35. * @see Packable
  36. */
  37. function Cartesian3(x, y, z) {
  38. /**
  39. * The X component.
  40. * @type {Number}
  41. * @default 0.0
  42. */
  43. this.x = when.defaultValue(x, 0.0);
  44. /**
  45. * The Y component.
  46. * @type {Number}
  47. * @default 0.0
  48. */
  49. this.y = when.defaultValue(y, 0.0);
  50. /**
  51. * The Z component.
  52. * @type {Number}
  53. * @default 0.0
  54. */
  55. this.z = when.defaultValue(z, 0.0);
  56. }
  57. /**
  58. * Converts the provided Spherical into Cartesian3 coordinates.
  59. *
  60. * @param {Spherical} spherical The Spherical to be converted to Cartesian3.
  61. * @param {Cartesian3} [result] The object onto which to store the result.
  62. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  63. */
  64. Cartesian3.fromSpherical = function (spherical, result) {
  65. //>>includeStart('debug', pragmas.debug);
  66. Check.Check.typeOf.object("spherical", spherical);
  67. //>>includeEnd('debug');
  68. if (!when.defined(result)) {
  69. result = new Cartesian3();
  70. }
  71. var clock = spherical.clock;
  72. var cone = spherical.cone;
  73. var magnitude = when.defaultValue(spherical.magnitude, 1.0);
  74. var radial = magnitude * Math.sin(cone);
  75. result.x = radial * Math.cos(clock);
  76. result.y = radial * Math.sin(clock);
  77. result.z = magnitude * Math.cos(cone);
  78. return result;
  79. };
  80. /**
  81. * Creates a Cartesian3 instance from x, y and z coordinates.
  82. *
  83. * @param {Number} x The x coordinate.
  84. * @param {Number} y The y coordinate.
  85. * @param {Number} z The z coordinate.
  86. * @param {Cartesian3} [result] The object onto which to store the result.
  87. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  88. */
  89. Cartesian3.fromElements = function (x, y, z, result) {
  90. if (!when.defined(result)) {
  91. return new Cartesian3(x, y, z);
  92. }
  93. result.x = x;
  94. result.y = y;
  95. result.z = z;
  96. return result;
  97. };
  98. /**
  99. * Duplicates a Cartesian3 instance.
  100. *
  101. * @param {Cartesian3} cartesian The Cartesian to duplicate.
  102. * @param {Cartesian3} [result] The object onto which to store the result.
  103. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided. (Returns undefined if cartesian is undefined)
  104. */
  105. Cartesian3.clone = function (cartesian, result) {
  106. if (!when.defined(cartesian)) {
  107. return undefined;
  108. }
  109. if (!when.defined(result)) {
  110. return new Cartesian3(cartesian.x, cartesian.y, cartesian.z);
  111. }
  112. result.x = cartesian.x;
  113. result.y = cartesian.y;
  114. result.z = cartesian.z;
  115. return result;
  116. };
  117. /**
  118. * Creates a Cartesian3 instance from an existing Cartesian4. This simply takes the
  119. * x, y, and z properties of the Cartesian4 and drops w.
  120. * @function
  121. *
  122. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian3 instance from.
  123. * @param {Cartesian3} [result] The object onto which to store the result.
  124. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  125. */
  126. Cartesian3.fromCartesian4 = Cartesian3.clone;
  127. /**
  128. * The number of elements used to pack the object into an array.
  129. * @type {Number}
  130. */
  131. Cartesian3.packedLength = 3;
  132. /**
  133. * Stores the provided instance into the provided array.
  134. *
  135. * @param {Cartesian3} value The value to pack.
  136. * @param {Number[]} array The array to pack into.
  137. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  138. *
  139. * @returns {Number[]} The array that was packed into
  140. */
  141. Cartesian3.pack = function (value, array, startingIndex) {
  142. //>>includeStart('debug', pragmas.debug);
  143. Check.Check.typeOf.object("value", value);
  144. Check.Check.defined("array", array);
  145. //>>includeEnd('debug');
  146. startingIndex = when.defaultValue(startingIndex, 0);
  147. array[startingIndex++] = value.x;
  148. array[startingIndex++] = value.y;
  149. array[startingIndex] = value.z;
  150. return array;
  151. };
  152. /**
  153. * Retrieves an instance from a packed array.
  154. *
  155. * @param {Number[]} array The packed array.
  156. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  157. * @param {Cartesian3} [result] The object into which to store the result.
  158. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  159. */
  160. Cartesian3.unpack = function (array, startingIndex, result) {
  161. //>>includeStart('debug', pragmas.debug);
  162. Check.Check.defined("array", array);
  163. //>>includeEnd('debug');
  164. startingIndex = when.defaultValue(startingIndex, 0);
  165. if (!when.defined(result)) {
  166. result = new Cartesian3();
  167. }
  168. result.x = array[startingIndex++];
  169. result.y = array[startingIndex++];
  170. result.z = array[startingIndex];
  171. return result;
  172. };
  173. /**
  174. * Flattens an array of Cartesian3s into an array of components.
  175. *
  176. * @param {Cartesian3[]} array The array of cartesians to pack.
  177. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 3 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 3) elements.
  178. * @returns {Number[]} The packed array.
  179. */
  180. Cartesian3.packArray = function (array, result) {
  181. //>>includeStart('debug', pragmas.debug);
  182. Check.Check.defined("array", array);
  183. //>>includeEnd('debug');
  184. var length = array.length;
  185. var resultLength = length * 3;
  186. if (!when.defined(result)) {
  187. result = new Array(resultLength);
  188. } else if (!Array.isArray(result) && result.length !== resultLength) {
  189. throw new Check.DeveloperError(
  190. "If result is a typed array, it must have exactly array.length * 3 elements"
  191. );
  192. } else if (result.length !== resultLength) {
  193. result.length = resultLength;
  194. }
  195. for (var i = 0; i < length; ++i) {
  196. Cartesian3.pack(array[i], result, i * 3);
  197. }
  198. return result;
  199. };
  200. /**
  201. * Unpacks an array of cartesian components into an array of Cartesian3s.
  202. *
  203. * @param {Number[]} array The array of components to unpack.
  204. * @param {Cartesian3[]} [result] The array onto which to store the result.
  205. * @returns {Cartesian3[]} The unpacked array.
  206. */
  207. Cartesian3.unpackArray = function (array, result) {
  208. //>>includeStart('debug', pragmas.debug);
  209. Check.Check.defined("array", array);
  210. Check.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 3);
  211. if (array.length % 3 !== 0) {
  212. throw new Check.DeveloperError("array length must be a multiple of 3.");
  213. }
  214. //>>includeEnd('debug');
  215. var length = array.length;
  216. if (!when.defined(result)) {
  217. result = new Array(length / 3);
  218. } else {
  219. result.length = length / 3;
  220. }
  221. for (var i = 0; i < length; i += 3) {
  222. var index = i / 3;
  223. result[index] = Cartesian3.unpack(array, i, result[index]);
  224. }
  225. return result;
  226. };
  227. /**
  228. * Creates a Cartesian3 from three consecutive elements in an array.
  229. * @function
  230. *
  231. * @param {Number[]} array The array whose three consecutive elements correspond to the x, y, and z components, respectively.
  232. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  233. * @param {Cartesian3} [result] The object onto which to store the result.
  234. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  235. *
  236. * @example
  237. * // Create a Cartesian3 with (1.0, 2.0, 3.0)
  238. * var v = [1.0, 2.0, 3.0];
  239. * var p = Cesium.Cartesian3.fromArray(v);
  240. *
  241. * // Create a Cartesian3 with (1.0, 2.0, 3.0) using an offset into an array
  242. * var v2 = [0.0, 0.0, 1.0, 2.0, 3.0];
  243. * var p2 = Cesium.Cartesian3.fromArray(v2, 2);
  244. */
  245. Cartesian3.fromArray = Cartesian3.unpack;
  246. /**
  247. * Computes the value of the maximum component for the supplied Cartesian.
  248. *
  249. * @param {Cartesian3} cartesian The cartesian to use.
  250. * @returns {Number} The value of the maximum component.
  251. */
  252. Cartesian3.maximumComponent = function (cartesian) {
  253. //>>includeStart('debug', pragmas.debug);
  254. Check.Check.typeOf.object("cartesian", cartesian);
  255. //>>includeEnd('debug');
  256. return Math.max(cartesian.x, cartesian.y, cartesian.z);
  257. };
  258. /**
  259. * Computes the value of the minimum component for the supplied Cartesian.
  260. *
  261. * @param {Cartesian3} cartesian The cartesian to use.
  262. * @returns {Number} The value of the minimum component.
  263. */
  264. Cartesian3.minimumComponent = function (cartesian) {
  265. //>>includeStart('debug', pragmas.debug);
  266. Check.Check.typeOf.object("cartesian", cartesian);
  267. //>>includeEnd('debug');
  268. return Math.min(cartesian.x, cartesian.y, cartesian.z);
  269. };
  270. /**
  271. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  272. *
  273. * @param {Cartesian3} first A cartesian to compare.
  274. * @param {Cartesian3} second A cartesian to compare.
  275. * @param {Cartesian3} result The object into which to store the result.
  276. * @returns {Cartesian3} A cartesian with the minimum components.
  277. */
  278. Cartesian3.minimumByComponent = function (first, second, result) {
  279. //>>includeStart('debug', pragmas.debug);
  280. Check.Check.typeOf.object("first", first);
  281. Check.Check.typeOf.object("second", second);
  282. Check.Check.typeOf.object("result", result);
  283. //>>includeEnd('debug');
  284. result.x = Math.min(first.x, second.x);
  285. result.y = Math.min(first.y, second.y);
  286. result.z = Math.min(first.z, second.z);
  287. return result;
  288. };
  289. /**
  290. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  291. *
  292. * @param {Cartesian3} first A cartesian to compare.
  293. * @param {Cartesian3} second A cartesian to compare.
  294. * @param {Cartesian3} result The object into which to store the result.
  295. * @returns {Cartesian3} A cartesian with the maximum components.
  296. */
  297. Cartesian3.maximumByComponent = function (first, second, result) {
  298. //>>includeStart('debug', pragmas.debug);
  299. Check.Check.typeOf.object("first", first);
  300. Check.Check.typeOf.object("second", second);
  301. Check.Check.typeOf.object("result", result);
  302. //>>includeEnd('debug');
  303. result.x = Math.max(first.x, second.x);
  304. result.y = Math.max(first.y, second.y);
  305. result.z = Math.max(first.z, second.z);
  306. return result;
  307. };
  308. /**
  309. * Computes the provided Cartesian's squared magnitude.
  310. *
  311. * @param {Cartesian3} cartesian The Cartesian instance whose squared magnitude is to be computed.
  312. * @returns {Number} The squared magnitude.
  313. */
  314. Cartesian3.magnitudeSquared = function (cartesian) {
  315. //>>includeStart('debug', pragmas.debug);
  316. Check.Check.typeOf.object("cartesian", cartesian);
  317. //>>includeEnd('debug');
  318. return (
  319. cartesian.x * cartesian.x +
  320. cartesian.y * cartesian.y +
  321. cartesian.z * cartesian.z
  322. );
  323. };
  324. /**
  325. * Computes the Cartesian's magnitude (length).
  326. *
  327. * @param {Cartesian3} cartesian The Cartesian instance whose magnitude is to be computed.
  328. * @returns {Number} The magnitude.
  329. */
  330. Cartesian3.magnitude = function (cartesian) {
  331. return Math.sqrt(Cartesian3.magnitudeSquared(cartesian));
  332. };
  333. var distanceScratch = new Cartesian3();
  334. /**
  335. * Computes the distance between two points.
  336. *
  337. * @param {Cartesian3} left The first point to compute the distance from.
  338. * @param {Cartesian3} right The second point to compute the distance to.
  339. * @returns {Number} The distance between two points.
  340. *
  341. * @example
  342. * // Returns 1.0
  343. * var d = Cesium.Cartesian3.distance(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(2.0, 0.0, 0.0));
  344. */
  345. Cartesian3.distance = function (left, right) {
  346. //>>includeStart('debug', pragmas.debug);
  347. Check.Check.typeOf.object("left", left);
  348. Check.Check.typeOf.object("right", right);
  349. //>>includeEnd('debug');
  350. Cartesian3.subtract(left, right, distanceScratch);
  351. return Cartesian3.magnitude(distanceScratch);
  352. };
  353. /**
  354. * Computes the squared distance between two points. Comparing squared distances
  355. * using this function is more efficient than comparing distances using {@link Cartesian3#distance}.
  356. *
  357. * @param {Cartesian3} left The first point to compute the distance from.
  358. * @param {Cartesian3} right The second point to compute the distance to.
  359. * @returns {Number} The distance between two points.
  360. *
  361. * @example
  362. * // Returns 4.0, not 2.0
  363. * var d = Cesium.Cartesian3.distanceSquared(new Cesium.Cartesian3(1.0, 0.0, 0.0), new Cesium.Cartesian3(3.0, 0.0, 0.0));
  364. */
  365. Cartesian3.distanceSquared = function (left, right) {
  366. //>>includeStart('debug', pragmas.debug);
  367. Check.Check.typeOf.object("left", left);
  368. Check.Check.typeOf.object("right", right);
  369. //>>includeEnd('debug');
  370. Cartesian3.subtract(left, right, distanceScratch);
  371. return Cartesian3.magnitudeSquared(distanceScratch);
  372. };
  373. /**
  374. * Computes the normalized form of the supplied Cartesian.
  375. *
  376. * @param {Cartesian3} cartesian The Cartesian to be normalized.
  377. * @param {Cartesian3} result The object onto which to store the result.
  378. * @returns {Cartesian3} The modified result parameter.
  379. */
  380. Cartesian3.normalize = function (cartesian, result) {
  381. //>>includeStart('debug', pragmas.debug);
  382. Check.Check.typeOf.object("cartesian", cartesian);
  383. Check.Check.typeOf.object("result", result);
  384. //>>includeEnd('debug');
  385. var magnitude = Cartesian3.magnitude(cartesian);
  386. result.x = cartesian.x / magnitude;
  387. result.y = cartesian.y / magnitude;
  388. result.z = cartesian.z / magnitude;
  389. //>>includeStart('debug', pragmas.debug);
  390. if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z)) {
  391. throw new Check.DeveloperError("normalized result is not a number");
  392. }
  393. //>>includeEnd('debug');
  394. return result;
  395. };
  396. /**
  397. * Computes the dot (scalar) product of two Cartesians.
  398. *
  399. * @param {Cartesian3} left The first Cartesian.
  400. * @param {Cartesian3} right The second Cartesian.
  401. * @returns {Number} The dot product.
  402. */
  403. Cartesian3.dot = function (left, right) {
  404. //>>includeStart('debug', pragmas.debug);
  405. Check.Check.typeOf.object("left", left);
  406. Check.Check.typeOf.object("right", right);
  407. //>>includeEnd('debug');
  408. return left.x * right.x + left.y * right.y + left.z * right.z;
  409. };
  410. /**
  411. * Computes the componentwise product of two Cartesians.
  412. *
  413. * @param {Cartesian3} left The first Cartesian.
  414. * @param {Cartesian3} right The second Cartesian.
  415. * @param {Cartesian3} result The object onto which to store the result.
  416. * @returns {Cartesian3} The modified result parameter.
  417. */
  418. Cartesian3.multiplyComponents = function (left, right, result) {
  419. //>>includeStart('debug', pragmas.debug);
  420. Check.Check.typeOf.object("left", left);
  421. Check.Check.typeOf.object("right", right);
  422. Check.Check.typeOf.object("result", result);
  423. //>>includeEnd('debug');
  424. result.x = left.x * right.x;
  425. result.y = left.y * right.y;
  426. result.z = left.z * right.z;
  427. return result;
  428. };
  429. /**
  430. * Computes the componentwise quotient of two Cartesians.
  431. *
  432. * @param {Cartesian3} left The first Cartesian.
  433. * @param {Cartesian3} right The second Cartesian.
  434. * @param {Cartesian3} result The object onto which to store the result.
  435. * @returns {Cartesian3} The modified result parameter.
  436. */
  437. Cartesian3.divideComponents = function (left, right, result) {
  438. //>>includeStart('debug', pragmas.debug);
  439. Check.Check.typeOf.object("left", left);
  440. Check.Check.typeOf.object("right", right);
  441. Check.Check.typeOf.object("result", result);
  442. //>>includeEnd('debug');
  443. result.x = left.x / right.x;
  444. result.y = left.y / right.y;
  445. result.z = left.z / right.z;
  446. return result;
  447. };
  448. /**
  449. * Computes the componentwise sum of two Cartesians.
  450. *
  451. * @param {Cartesian3} left The first Cartesian.
  452. * @param {Cartesian3} right The second Cartesian.
  453. * @param {Cartesian3} result The object onto which to store the result.
  454. * @returns {Cartesian3} The modified result parameter.
  455. */
  456. Cartesian3.add = function (left, right, result) {
  457. //>>includeStart('debug', pragmas.debug);
  458. Check.Check.typeOf.object("left", left);
  459. Check.Check.typeOf.object("right", right);
  460. Check.Check.typeOf.object("result", result);
  461. //>>includeEnd('debug');
  462. result.x = left.x + right.x;
  463. result.y = left.y + right.y;
  464. result.z = left.z + right.z;
  465. return result;
  466. };
  467. /**
  468. * Computes the componentwise difference of two Cartesians.
  469. *
  470. * @param {Cartesian3} left The first Cartesian.
  471. * @param {Cartesian3} right The second Cartesian.
  472. * @param {Cartesian3} result The object onto which to store the result.
  473. * @returns {Cartesian3} The modified result parameter.
  474. */
  475. Cartesian3.subtract = function (left, right, result) {
  476. //>>includeStart('debug', pragmas.debug);
  477. Check.Check.typeOf.object("left", left);
  478. Check.Check.typeOf.object("right", right);
  479. Check.Check.typeOf.object("result", result);
  480. //>>includeEnd('debug');
  481. result.x = left.x - right.x;
  482. result.y = left.y - right.y;
  483. result.z = left.z - right.z;
  484. return result;
  485. };
  486. /**
  487. * Multiplies the provided Cartesian componentwise by the provided scalar.
  488. *
  489. * @param {Cartesian3} cartesian The Cartesian to be scaled.
  490. * @param {Number} scalar The scalar to multiply with.
  491. * @param {Cartesian3} result The object onto which to store the result.
  492. * @returns {Cartesian3} The modified result parameter.
  493. */
  494. Cartesian3.multiplyByScalar = function (cartesian, scalar, result) {
  495. //>>includeStart('debug', pragmas.debug);
  496. Check.Check.typeOf.object("cartesian", cartesian);
  497. Check.Check.typeOf.number("scalar", scalar);
  498. Check.Check.typeOf.object("result", result);
  499. //>>includeEnd('debug');
  500. result.x = cartesian.x * scalar;
  501. result.y = cartesian.y * scalar;
  502. result.z = cartesian.z * scalar;
  503. return result;
  504. };
  505. /**
  506. * Divides the provided Cartesian componentwise by the provided scalar.
  507. *
  508. * @param {Cartesian3} cartesian The Cartesian to be divided.
  509. * @param {Number} scalar The scalar to divide by.
  510. * @param {Cartesian3} result The object onto which to store the result.
  511. * @returns {Cartesian3} The modified result parameter.
  512. */
  513. Cartesian3.divideByScalar = function (cartesian, scalar, result) {
  514. //>>includeStart('debug', pragmas.debug);
  515. Check.Check.typeOf.object("cartesian", cartesian);
  516. Check.Check.typeOf.number("scalar", scalar);
  517. Check.Check.typeOf.object("result", result);
  518. //>>includeEnd('debug');
  519. result.x = cartesian.x / scalar;
  520. result.y = cartesian.y / scalar;
  521. result.z = cartesian.z / scalar;
  522. return result;
  523. };
  524. /**
  525. * Negates the provided Cartesian.
  526. *
  527. * @param {Cartesian3} cartesian The Cartesian to be negated.
  528. * @param {Cartesian3} result The object onto which to store the result.
  529. * @returns {Cartesian3} The modified result parameter.
  530. */
  531. Cartesian3.negate = function (cartesian, result) {
  532. //>>includeStart('debug', pragmas.debug);
  533. Check.Check.typeOf.object("cartesian", cartesian);
  534. Check.Check.typeOf.object("result", result);
  535. //>>includeEnd('debug');
  536. result.x = -cartesian.x;
  537. result.y = -cartesian.y;
  538. result.z = -cartesian.z;
  539. return result;
  540. };
  541. /**
  542. * Computes the absolute value of the provided Cartesian.
  543. *
  544. * @param {Cartesian3} cartesian The Cartesian whose absolute value is to be computed.
  545. * @param {Cartesian3} result The object onto which to store the result.
  546. * @returns {Cartesian3} The modified result parameter.
  547. */
  548. Cartesian3.abs = function (cartesian, result) {
  549. //>>includeStart('debug', pragmas.debug);
  550. Check.Check.typeOf.object("cartesian", cartesian);
  551. Check.Check.typeOf.object("result", result);
  552. //>>includeEnd('debug');
  553. result.x = Math.abs(cartesian.x);
  554. result.y = Math.abs(cartesian.y);
  555. result.z = Math.abs(cartesian.z);
  556. return result;
  557. };
  558. var lerpScratch = new Cartesian3();
  559. /**
  560. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  561. *
  562. * @param {Cartesian3} start The value corresponding to t at 0.0.
  563. * @param {Cartesian3} end The value corresponding to t at 1.0.
  564. * @param {Number} t The point along t at which to interpolate.
  565. * @param {Cartesian3} result The object onto which to store the result.
  566. * @returns {Cartesian3} The modified result parameter.
  567. */
  568. Cartesian3.lerp = function (start, end, t, result) {
  569. //>>includeStart('debug', pragmas.debug);
  570. Check.Check.typeOf.object("start", start);
  571. Check.Check.typeOf.object("end", end);
  572. Check.Check.typeOf.number("t", t);
  573. Check.Check.typeOf.object("result", result);
  574. //>>includeEnd('debug');
  575. Cartesian3.multiplyByScalar(end, t, lerpScratch);
  576. result = Cartesian3.multiplyByScalar(start, 1.0 - t, result);
  577. return Cartesian3.add(lerpScratch, result, result);
  578. };
  579. var angleBetweenScratch = new Cartesian3();
  580. var angleBetweenScratch2 = new Cartesian3();
  581. /**
  582. * Returns the angle, in radians, between the provided Cartesians.
  583. *
  584. * @param {Cartesian3} left The first Cartesian.
  585. * @param {Cartesian3} right The second Cartesian.
  586. * @returns {Number} The angle between the Cartesians.
  587. */
  588. Cartesian3.angleBetween = function (left, right) {
  589. //>>includeStart('debug', pragmas.debug);
  590. Check.Check.typeOf.object("left", left);
  591. Check.Check.typeOf.object("right", right);
  592. //>>includeEnd('debug');
  593. Cartesian3.normalize(left, angleBetweenScratch);
  594. Cartesian3.normalize(right, angleBetweenScratch2);
  595. var cosine = Cartesian3.dot(angleBetweenScratch, angleBetweenScratch2);
  596. var sine = Cartesian3.magnitude(
  597. Cartesian3.cross(
  598. angleBetweenScratch,
  599. angleBetweenScratch2,
  600. angleBetweenScratch
  601. )
  602. );
  603. return Math.atan2(sine, cosine);
  604. };
  605. var mostOrthogonalAxisScratch = new Cartesian3();
  606. /**
  607. * Returns the axis that is most orthogonal to the provided Cartesian.
  608. *
  609. * @param {Cartesian3} cartesian The Cartesian on which to find the most orthogonal axis.
  610. * @param {Cartesian3} result The object onto which to store the result.
  611. * @returns {Cartesian3} The most orthogonal axis.
  612. */
  613. Cartesian3.mostOrthogonalAxis = function (cartesian, result) {
  614. //>>includeStart('debug', pragmas.debug);
  615. Check.Check.typeOf.object("cartesian", cartesian);
  616. Check.Check.typeOf.object("result", result);
  617. //>>includeEnd('debug');
  618. var f = Cartesian3.normalize(cartesian, mostOrthogonalAxisScratch);
  619. Cartesian3.abs(f, f);
  620. if (f.x <= f.y) {
  621. if (f.x <= f.z) {
  622. result = Cartesian3.clone(Cartesian3.UNIT_X, result);
  623. } else {
  624. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  625. }
  626. } else if (f.y <= f.z) {
  627. result = Cartesian3.clone(Cartesian3.UNIT_Y, result);
  628. } else {
  629. result = Cartesian3.clone(Cartesian3.UNIT_Z, result);
  630. }
  631. return result;
  632. };
  633. /**
  634. * Projects vector a onto vector b
  635. * @param {Cartesian3} a The vector that needs projecting
  636. * @param {Cartesian3} b The vector to project onto
  637. * @param {Cartesian3} result The result cartesian
  638. * @returns {Cartesian3} The modified result parameter
  639. */
  640. Cartesian3.projectVector = function (a, b, result) {
  641. //>>includeStart('debug', pragmas.debug);
  642. Check.Check.defined("a", a);
  643. Check.Check.defined("b", b);
  644. Check.Check.defined("result", result);
  645. //>>includeEnd('debug');
  646. var scalar = Cartesian3.dot(a, b) / Cartesian3.dot(b, b);
  647. return Cartesian3.multiplyByScalar(b, scalar, result);
  648. };
  649. /**
  650. * Compares the provided Cartesians componentwise and returns
  651. * <code>true</code> if they are equal, <code>false</code> otherwise.
  652. *
  653. * @param {Cartesian3} [left] The first Cartesian.
  654. * @param {Cartesian3} [right] The second Cartesian.
  655. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  656. */
  657. Cartesian3.equals = function (left, right) {
  658. return (
  659. left === right ||
  660. (when.defined(left) &&
  661. when.defined(right) &&
  662. left.x === right.x &&
  663. left.y === right.y &&
  664. left.z === right.z)
  665. );
  666. };
  667. /**
  668. * @private
  669. */
  670. Cartesian3.equalsArray = function (cartesian, array, offset) {
  671. return (
  672. cartesian.x === array[offset] &&
  673. cartesian.y === array[offset + 1] &&
  674. cartesian.z === array[offset + 2]
  675. );
  676. };
  677. /**
  678. * Compares the provided Cartesians componentwise and returns
  679. * <code>true</code> if they pass an absolute or relative tolerance test,
  680. * <code>false</code> otherwise.
  681. *
  682. * @param {Cartesian3} [left] The first Cartesian.
  683. * @param {Cartesian3} [right] The second Cartesian.
  684. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  685. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  686. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  687. */
  688. Cartesian3.equalsEpsilon = function (
  689. left,
  690. right,
  691. relativeEpsilon,
  692. absoluteEpsilon
  693. ) {
  694. return (
  695. left === right ||
  696. (when.defined(left) &&
  697. when.defined(right) &&
  698. _Math.CesiumMath.equalsEpsilon(
  699. left.x,
  700. right.x,
  701. relativeEpsilon,
  702. absoluteEpsilon
  703. ) &&
  704. _Math.CesiumMath.equalsEpsilon(
  705. left.y,
  706. right.y,
  707. relativeEpsilon,
  708. absoluteEpsilon
  709. ) &&
  710. _Math.CesiumMath.equalsEpsilon(
  711. left.z,
  712. right.z,
  713. relativeEpsilon,
  714. absoluteEpsilon
  715. ))
  716. );
  717. };
  718. /**
  719. * Computes the cross (outer) product of two Cartesians.
  720. *
  721. * @param {Cartesian3} left The first Cartesian.
  722. * @param {Cartesian3} right The second Cartesian.
  723. * @param {Cartesian3} result The object onto which to store the result.
  724. * @returns {Cartesian3} The cross product.
  725. */
  726. Cartesian3.cross = function (left, right, result) {
  727. //>>includeStart('debug', pragmas.debug);
  728. Check.Check.typeOf.object("left", left);
  729. Check.Check.typeOf.object("right", right);
  730. Check.Check.typeOf.object("result", result);
  731. //>>includeEnd('debug');
  732. var leftX = left.x;
  733. var leftY = left.y;
  734. var leftZ = left.z;
  735. var rightX = right.x;
  736. var rightY = right.y;
  737. var rightZ = right.z;
  738. var x = leftY * rightZ - leftZ * rightY;
  739. var y = leftZ * rightX - leftX * rightZ;
  740. var z = leftX * rightY - leftY * rightX;
  741. result.x = x;
  742. result.y = y;
  743. result.z = z;
  744. return result;
  745. };
  746. /**
  747. * Computes the midpoint between the right and left Cartesian.
  748. * @param {Cartesian3} left The first Cartesian.
  749. * @param {Cartesian3} right The second Cartesian.
  750. * @param {Cartesian3} result The object onto which to store the result.
  751. * @returns {Cartesian3} The midpoint.
  752. */
  753. Cartesian3.midpoint = function (left, right, result) {
  754. //>>includeStart('debug', pragmas.debug);
  755. Check.Check.typeOf.object("left", left);
  756. Check.Check.typeOf.object("right", right);
  757. Check.Check.typeOf.object("result", result);
  758. //>>includeEnd('debug');
  759. result.x = (left.x + right.x) * 0.5;
  760. result.y = (left.y + right.y) * 0.5;
  761. result.z = (left.z + right.z) * 0.5;
  762. return result;
  763. };
  764. /**
  765. * Returns a Cartesian3 position from longitude and latitude values given in degrees.
  766. *
  767. * @param {Number} longitude The longitude, in degrees
  768. * @param {Number} latitude The latitude, in degrees
  769. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  770. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  771. * @param {Cartesian3} [result] The object onto which to store the result.
  772. * @returns {Cartesian3} The position
  773. *
  774. * @example
  775. * var position = Cesium.Cartesian3.fromDegrees(-115.0, 37.0);
  776. */
  777. Cartesian3.fromDegrees = function (
  778. longitude,
  779. latitude,
  780. height,
  781. ellipsoid,
  782. result
  783. ) {
  784. //>>includeStart('debug', pragmas.debug);
  785. Check.Check.typeOf.number("longitude", longitude);
  786. Check.Check.typeOf.number("latitude", latitude);
  787. //>>includeEnd('debug');
  788. longitude = _Math.CesiumMath.toRadians(longitude);
  789. latitude = _Math.CesiumMath.toRadians(latitude);
  790. return Cartesian3.fromRadians(longitude, latitude, height, ellipsoid, result);
  791. };
  792. var scratchN = new Cartesian3();
  793. var scratchK = new Cartesian3();
  794. var wgs84RadiiSquared = new Cartesian3(
  795. 6378137.0 * 6378137.0,
  796. 6378137.0 * 6378137.0,
  797. 6356752.3142451793 * 6356752.3142451793
  798. );
  799. /**
  800. * Returns a Cartesian3 position from longitude and latitude values given in radians.
  801. *
  802. * @param {Number} longitude The longitude, in radians
  803. * @param {Number} latitude The latitude, in radians
  804. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  805. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  806. * @param {Cartesian3} [result] The object onto which to store the result.
  807. * @returns {Cartesian3} The position
  808. *
  809. * @example
  810. * var position = Cesium.Cartesian3.fromRadians(-2.007, 0.645);
  811. */
  812. Cartesian3.fromRadians = function (
  813. longitude,
  814. latitude,
  815. height,
  816. ellipsoid,
  817. result
  818. ) {
  819. //>>includeStart('debug', pragmas.debug);
  820. Check.Check.typeOf.number("longitude", longitude);
  821. Check.Check.typeOf.number("latitude", latitude);
  822. //>>includeEnd('debug');
  823. height = when.defaultValue(height, 0.0);
  824. var radiiSquared = when.defined(ellipsoid)
  825. ? ellipsoid.radiiSquared
  826. : wgs84RadiiSquared;
  827. var cosLatitude = Math.cos(latitude);
  828. scratchN.x = cosLatitude * Math.cos(longitude);
  829. scratchN.y = cosLatitude * Math.sin(longitude);
  830. scratchN.z = Math.sin(latitude);
  831. scratchN = Cartesian3.normalize(scratchN, scratchN);
  832. Cartesian3.multiplyComponents(radiiSquared, scratchN, scratchK);
  833. var gamma = Math.sqrt(Cartesian3.dot(scratchN, scratchK));
  834. scratchK = Cartesian3.divideByScalar(scratchK, gamma, scratchK);
  835. scratchN = Cartesian3.multiplyByScalar(scratchN, height, scratchN);
  836. if (!when.defined(result)) {
  837. result = new Cartesian3();
  838. }
  839. return Cartesian3.add(scratchK, scratchN, result);
  840. };
  841. /**
  842. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in degrees.
  843. *
  844. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  845. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  846. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  847. * @returns {Cartesian3[]} The array of positions.
  848. *
  849. * @example
  850. * var positions = Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0, -107.0, 33.0]);
  851. */
  852. Cartesian3.fromDegreesArray = function (coordinates, ellipsoid, result) {
  853. //>>includeStart('debug', pragmas.debug);
  854. Check.Check.defined("coordinates", coordinates);
  855. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  856. throw new Check.DeveloperError(
  857. "the number of coordinates must be a multiple of 2 and at least 2"
  858. );
  859. }
  860. //>>includeEnd('debug');
  861. var length = coordinates.length;
  862. if (!when.defined(result)) {
  863. result = new Array(length / 2);
  864. } else {
  865. result.length = length / 2;
  866. }
  867. for (var i = 0; i < length; i += 2) {
  868. var longitude = coordinates[i];
  869. var latitude = coordinates[i + 1];
  870. var index = i / 2;
  871. result[index] = Cartesian3.fromDegrees(
  872. longitude,
  873. latitude,
  874. 0,
  875. ellipsoid,
  876. result[index]
  877. );
  878. }
  879. return result;
  880. };
  881. /**
  882. * Returns an array of Cartesian3 positions given an array of longitude and latitude values given in radians.
  883. *
  884. * @param {Number[]} coordinates A list of longitude and latitude values. Values alternate [longitude, latitude, longitude, latitude...].
  885. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the coordinates lie.
  886. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  887. * @returns {Cartesian3[]} The array of positions.
  888. *
  889. * @example
  890. * var positions = Cesium.Cartesian3.fromRadiansArray([-2.007, 0.645, -1.867, .575]);
  891. */
  892. Cartesian3.fromRadiansArray = function (coordinates, ellipsoid, result) {
  893. //>>includeStart('debug', pragmas.debug);
  894. Check.Check.defined("coordinates", coordinates);
  895. if (coordinates.length < 2 || coordinates.length % 2 !== 0) {
  896. throw new Check.DeveloperError(
  897. "the number of coordinates must be a multiple of 2 and at least 2"
  898. );
  899. }
  900. //>>includeEnd('debug');
  901. var length = coordinates.length;
  902. if (!when.defined(result)) {
  903. result = new Array(length / 2);
  904. } else {
  905. result.length = length / 2;
  906. }
  907. for (var i = 0; i < length; i += 2) {
  908. var longitude = coordinates[i];
  909. var latitude = coordinates[i + 1];
  910. var index = i / 2;
  911. result[index] = Cartesian3.fromRadians(
  912. longitude,
  913. latitude,
  914. 0,
  915. ellipsoid,
  916. result[index]
  917. );
  918. }
  919. return result;
  920. };
  921. /**
  922. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in degrees.
  923. *
  924. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  925. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  926. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  927. * @returns {Cartesian3[]} The array of positions.
  928. *
  929. * @example
  930. * var positions = Cesium.Cartesian3.fromDegreesArrayHeights([-115.0, 37.0, 100000.0, -107.0, 33.0, 150000.0]);
  931. */
  932. Cartesian3.fromDegreesArrayHeights = function (coordinates, ellipsoid, result) {
  933. //>>includeStart('debug', pragmas.debug);
  934. Check.Check.defined("coordinates", coordinates);
  935. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  936. throw new Check.DeveloperError(
  937. "the number of coordinates must be a multiple of 3 and at least 3"
  938. );
  939. }
  940. //>>includeEnd('debug');
  941. var length = coordinates.length;
  942. if (!when.defined(result)) {
  943. result = new Array(length / 3);
  944. } else {
  945. result.length = length / 3;
  946. }
  947. for (var i = 0; i < length; i += 3) {
  948. var longitude = coordinates[i];
  949. var latitude = coordinates[i + 1];
  950. var height = coordinates[i + 2];
  951. var index = i / 3;
  952. result[index] = Cartesian3.fromDegrees(
  953. longitude,
  954. latitude,
  955. height,
  956. ellipsoid,
  957. result[index]
  958. );
  959. }
  960. return result;
  961. };
  962. /**
  963. * Returns an array of Cartesian3 positions given an array of longitude, latitude and height values where longitude and latitude are given in radians.
  964. *
  965. * @param {Number[]} coordinates A list of longitude, latitude and height values. Values alternate [longitude, latitude, height, longitude, latitude, height...].
  966. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  967. * @param {Cartesian3[]} [result] An array of Cartesian3 objects to store the result.
  968. * @returns {Cartesian3[]} The array of positions.
  969. *
  970. * @example
  971. * var positions = Cesium.Cartesian3.fromRadiansArrayHeights([-2.007, 0.645, 100000.0, -1.867, .575, 150000.0]);
  972. */
  973. Cartesian3.fromRadiansArrayHeights = function (coordinates, ellipsoid, result) {
  974. //>>includeStart('debug', pragmas.debug);
  975. Check.Check.defined("coordinates", coordinates);
  976. if (coordinates.length < 3 || coordinates.length % 3 !== 0) {
  977. throw new Check.DeveloperError(
  978. "the number of coordinates must be a multiple of 3 and at least 3"
  979. );
  980. }
  981. //>>includeEnd('debug');
  982. var length = coordinates.length;
  983. if (!when.defined(result)) {
  984. result = new Array(length / 3);
  985. } else {
  986. result.length = length / 3;
  987. }
  988. for (var i = 0; i < length; i += 3) {
  989. var longitude = coordinates[i];
  990. var latitude = coordinates[i + 1];
  991. var height = coordinates[i + 2];
  992. var index = i / 3;
  993. result[index] = Cartesian3.fromRadians(
  994. longitude,
  995. latitude,
  996. height,
  997. ellipsoid,
  998. result[index]
  999. );
  1000. }
  1001. return result;
  1002. };
  1003. /**
  1004. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 0.0).
  1005. *
  1006. * @type {Cartesian3}
  1007. * @constant
  1008. */
  1009. Cartesian3.ZERO = Object.freeze(new Cartesian3(0.0, 0.0, 0.0));
  1010. /**
  1011. * An immutable Cartesian3 instance initialized to (1.0, 0.0, 0.0).
  1012. *
  1013. * @type {Cartesian3}
  1014. * @constant
  1015. */
  1016. Cartesian3.UNIT_X = Object.freeze(new Cartesian3(1.0, 0.0, 0.0));
  1017. /**
  1018. * An immutable Cartesian3 instance initialized to (0.0, 1.0, 0.0).
  1019. *
  1020. * @type {Cartesian3}
  1021. * @constant
  1022. */
  1023. Cartesian3.UNIT_Y = Object.freeze(new Cartesian3(0.0, 1.0, 0.0));
  1024. /**
  1025. * An immutable Cartesian3 instance initialized to (0.0, 0.0, 1.0).
  1026. *
  1027. * @type {Cartesian3}
  1028. * @constant
  1029. */
  1030. Cartesian3.UNIT_Z = Object.freeze(new Cartesian3(0.0, 0.0, 1.0));
  1031. /**
  1032. * Duplicates this Cartesian3 instance.
  1033. *
  1034. * @param {Cartesian3} [result] The object onto which to store the result.
  1035. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if one was not provided.
  1036. */
  1037. Cartesian3.prototype.clone = function (result) {
  1038. return Cartesian3.clone(this, result);
  1039. };
  1040. /**
  1041. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1042. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1043. *
  1044. * @param {Cartesian3} [right] The right hand side Cartesian.
  1045. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1046. */
  1047. Cartesian3.prototype.equals = function (right) {
  1048. return Cartesian3.equals(this, right);
  1049. };
  1050. /**
  1051. * Compares this Cartesian against the provided Cartesian componentwise and returns
  1052. * <code>true</code> if they pass an absolute or relative tolerance test,
  1053. * <code>false</code> otherwise.
  1054. *
  1055. * @param {Cartesian3} [right] The right hand side Cartesian.
  1056. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  1057. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  1058. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1059. */
  1060. Cartesian3.prototype.equalsEpsilon = function (
  1061. right,
  1062. relativeEpsilon,
  1063. absoluteEpsilon
  1064. ) {
  1065. return Cartesian3.equalsEpsilon(
  1066. this,
  1067. right,
  1068. relativeEpsilon,
  1069. absoluteEpsilon
  1070. );
  1071. };
  1072. /**
  1073. * Creates a string representing this Cartesian in the format '(x, y, z)'.
  1074. *
  1075. * @returns {String} A string representing this Cartesian in the format '(x, y, z)'.
  1076. */
  1077. Cartesian3.prototype.toString = function () {
  1078. return "(" + this.x + ", " + this.y + ", " + this.z + ")";
  1079. };
  1080. var scaleToGeodeticSurfaceIntersection = new Cartesian3();
  1081. var scaleToGeodeticSurfaceGradient = new Cartesian3();
  1082. /**
  1083. * Scales the provided Cartesian position along the geodetic surface normal
  1084. * so that it is on the surface of this ellipsoid. If the position is
  1085. * at the center of the ellipsoid, this function returns undefined.
  1086. *
  1087. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1088. * @param {Cartesian3} oneOverRadii One over radii of the ellipsoid.
  1089. * @param {Cartesian3} oneOverRadiiSquared One over radii squared of the ellipsoid.
  1090. * @param {Number} centerToleranceSquared Tolerance for closeness to the center.
  1091. * @param {Cartesian3} [result] The object onto which to store the result.
  1092. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  1093. *
  1094. * @function scaleToGeodeticSurface
  1095. *
  1096. * @private
  1097. */
  1098. function scaleToGeodeticSurface(
  1099. cartesian,
  1100. oneOverRadii,
  1101. oneOverRadiiSquared,
  1102. centerToleranceSquared,
  1103. result
  1104. ) {
  1105. //>>includeStart('debug', pragmas.debug);
  1106. if (!when.defined(cartesian)) {
  1107. throw new Check.DeveloperError("cartesian is required.");
  1108. }
  1109. if (!when.defined(oneOverRadii)) {
  1110. throw new Check.DeveloperError("oneOverRadii is required.");
  1111. }
  1112. if (!when.defined(oneOverRadiiSquared)) {
  1113. throw new Check.DeveloperError("oneOverRadiiSquared is required.");
  1114. }
  1115. if (!when.defined(centerToleranceSquared)) {
  1116. throw new Check.DeveloperError("centerToleranceSquared is required.");
  1117. }
  1118. //>>includeEnd('debug');
  1119. var positionX = cartesian.x;
  1120. var positionY = cartesian.y;
  1121. var positionZ = cartesian.z;
  1122. var oneOverRadiiX = oneOverRadii.x;
  1123. var oneOverRadiiY = oneOverRadii.y;
  1124. var oneOverRadiiZ = oneOverRadii.z;
  1125. var x2 = positionX * positionX * oneOverRadiiX * oneOverRadiiX;
  1126. var y2 = positionY * positionY * oneOverRadiiY * oneOverRadiiY;
  1127. var z2 = positionZ * positionZ * oneOverRadiiZ * oneOverRadiiZ;
  1128. // Compute the squared ellipsoid norm.
  1129. var squaredNorm = x2 + y2 + z2;
  1130. var ratio = Math.sqrt(1.0 / squaredNorm);
  1131. // As an initial approximation, assume that the radial intersection is the projection point.
  1132. var intersection = Cartesian3.multiplyByScalar(
  1133. cartesian,
  1134. ratio,
  1135. scaleToGeodeticSurfaceIntersection
  1136. );
  1137. // If the position is near the center, the iteration will not converge.
  1138. if (squaredNorm < centerToleranceSquared) {
  1139. return !isFinite(ratio)
  1140. ? undefined
  1141. : Cartesian3.clone(intersection, result);
  1142. }
  1143. var oneOverRadiiSquaredX = oneOverRadiiSquared.x;
  1144. var oneOverRadiiSquaredY = oneOverRadiiSquared.y;
  1145. var oneOverRadiiSquaredZ = oneOverRadiiSquared.z;
  1146. // Use the gradient at the intersection point in place of the true unit normal.
  1147. // The difference in magnitude will be absorbed in the multiplier.
  1148. var gradient = scaleToGeodeticSurfaceGradient;
  1149. gradient.x = intersection.x * oneOverRadiiSquaredX * 2.0;
  1150. gradient.y = intersection.y * oneOverRadiiSquaredY * 2.0;
  1151. gradient.z = intersection.z * oneOverRadiiSquaredZ * 2.0;
  1152. // Compute the initial guess at the normal vector multiplier, lambda.
  1153. var lambda =
  1154. ((1.0 - ratio) * Cartesian3.magnitude(cartesian)) /
  1155. (0.5 * Cartesian3.magnitude(gradient));
  1156. var correction = 0.0;
  1157. var func;
  1158. var denominator;
  1159. var xMultiplier;
  1160. var yMultiplier;
  1161. var zMultiplier;
  1162. var xMultiplier2;
  1163. var yMultiplier2;
  1164. var zMultiplier2;
  1165. var xMultiplier3;
  1166. var yMultiplier3;
  1167. var zMultiplier3;
  1168. do {
  1169. lambda -= correction;
  1170. xMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredX);
  1171. yMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredY);
  1172. zMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredZ);
  1173. xMultiplier2 = xMultiplier * xMultiplier;
  1174. yMultiplier2 = yMultiplier * yMultiplier;
  1175. zMultiplier2 = zMultiplier * zMultiplier;
  1176. xMultiplier3 = xMultiplier2 * xMultiplier;
  1177. yMultiplier3 = yMultiplier2 * yMultiplier;
  1178. zMultiplier3 = zMultiplier2 * zMultiplier;
  1179. func = x2 * xMultiplier2 + y2 * yMultiplier2 + z2 * zMultiplier2 - 1.0;
  1180. // "denominator" here refers to the use of this expression in the velocity and acceleration
  1181. // computations in the sections to follow.
  1182. denominator =
  1183. x2 * xMultiplier3 * oneOverRadiiSquaredX +
  1184. y2 * yMultiplier3 * oneOverRadiiSquaredY +
  1185. z2 * zMultiplier3 * oneOverRadiiSquaredZ;
  1186. var derivative = -2.0 * denominator;
  1187. correction = func / derivative;
  1188. } while (Math.abs(func) > _Math.CesiumMath.EPSILON12);
  1189. if (!when.defined(result)) {
  1190. return new Cartesian3(
  1191. positionX * xMultiplier,
  1192. positionY * yMultiplier,
  1193. positionZ * zMultiplier
  1194. );
  1195. }
  1196. result.x = positionX * xMultiplier;
  1197. result.y = positionY * yMultiplier;
  1198. result.z = positionZ * zMultiplier;
  1199. return result;
  1200. }
  1201. /**
  1202. * A position defined by longitude, latitude, and height.
  1203. * @alias Cartographic
  1204. * @constructor
  1205. *
  1206. * @param {Number} [longitude=0.0] The longitude, in radians.
  1207. * @param {Number} [latitude=0.0] The latitude, in radians.
  1208. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1209. *
  1210. * @see Ellipsoid
  1211. */
  1212. function Cartographic(longitude, latitude, height) {
  1213. /**
  1214. * The longitude, in radians.
  1215. * @type {Number}
  1216. * @default 0.0
  1217. */
  1218. this.longitude = when.defaultValue(longitude, 0.0);
  1219. /**
  1220. * The latitude, in radians.
  1221. * @type {Number}
  1222. * @default 0.0
  1223. */
  1224. this.latitude = when.defaultValue(latitude, 0.0);
  1225. /**
  1226. * The height, in meters, above the ellipsoid.
  1227. * @type {Number}
  1228. * @default 0.0
  1229. */
  1230. this.height = when.defaultValue(height, 0.0);
  1231. }
  1232. /**
  1233. * Creates a new Cartographic instance from longitude and latitude
  1234. * specified in radians.
  1235. *
  1236. * @param {Number} longitude The longitude, in radians.
  1237. * @param {Number} latitude The latitude, in radians.
  1238. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1239. * @param {Cartographic} [result] The object onto which to store the result.
  1240. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1241. */
  1242. Cartographic.fromRadians = function (longitude, latitude, height, result) {
  1243. //>>includeStart('debug', pragmas.debug);
  1244. Check.Check.typeOf.number("longitude", longitude);
  1245. Check.Check.typeOf.number("latitude", latitude);
  1246. //>>includeEnd('debug');
  1247. height = when.defaultValue(height, 0.0);
  1248. if (!when.defined(result)) {
  1249. return new Cartographic(longitude, latitude, height);
  1250. }
  1251. result.longitude = longitude;
  1252. result.latitude = latitude;
  1253. result.height = height;
  1254. return result;
  1255. };
  1256. /**
  1257. * Creates a new Cartographic instance from longitude and latitude
  1258. * specified in degrees. The values in the resulting object will
  1259. * be in radians.
  1260. *
  1261. * @param {Number} longitude The longitude, in degrees.
  1262. * @param {Number} latitude The latitude, in degrees.
  1263. * @param {Number} [height=0.0] The height, in meters, above the ellipsoid.
  1264. * @param {Cartographic} [result] The object onto which to store the result.
  1265. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1266. */
  1267. Cartographic.fromDegrees = function (longitude, latitude, height, result) {
  1268. //>>includeStart('debug', pragmas.debug);
  1269. Check.Check.typeOf.number("longitude", longitude);
  1270. Check.Check.typeOf.number("latitude", latitude);
  1271. //>>includeEnd('debug');
  1272. longitude = _Math.CesiumMath.toRadians(longitude);
  1273. latitude = _Math.CesiumMath.toRadians(latitude);
  1274. return Cartographic.fromRadians(longitude, latitude, height, result);
  1275. };
  1276. var cartesianToCartographicN = new Cartesian3();
  1277. var cartesianToCartographicP = new Cartesian3();
  1278. var cartesianToCartographicH = new Cartesian3();
  1279. var wgs84OneOverRadii = new Cartesian3(
  1280. 1.0 / 6378137.0,
  1281. 1.0 / 6378137.0,
  1282. 1.0 / 6356752.3142451793
  1283. );
  1284. var wgs84OneOverRadiiSquared = new Cartesian3(
  1285. 1.0 / (6378137.0 * 6378137.0),
  1286. 1.0 / (6378137.0 * 6378137.0),
  1287. 1.0 / (6356752.3142451793 * 6356752.3142451793)
  1288. );
  1289. var wgs84CenterToleranceSquared = _Math.CesiumMath.EPSILON1;
  1290. /**
  1291. * Creates a new Cartographic instance from a Cartesian position. The values in the
  1292. * resulting object will be in radians.
  1293. *
  1294. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  1295. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1296. * @param {Cartographic} [result] The object onto which to store the result.
  1297. * @returns {Cartographic} The modified result parameter, new Cartographic instance if none was provided, or undefined if the cartesian is at the center of the ellipsoid.
  1298. */
  1299. Cartographic.fromCartesian = function (cartesian, ellipsoid, result) {
  1300. var oneOverRadii = when.defined(ellipsoid)
  1301. ? ellipsoid.oneOverRadii
  1302. : wgs84OneOverRadii;
  1303. var oneOverRadiiSquared = when.defined(ellipsoid)
  1304. ? ellipsoid.oneOverRadiiSquared
  1305. : wgs84OneOverRadiiSquared;
  1306. var centerToleranceSquared = when.defined(ellipsoid)
  1307. ? ellipsoid._centerToleranceSquared
  1308. : wgs84CenterToleranceSquared;
  1309. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  1310. var p = scaleToGeodeticSurface(
  1311. cartesian,
  1312. oneOverRadii,
  1313. oneOverRadiiSquared,
  1314. centerToleranceSquared,
  1315. cartesianToCartographicP
  1316. );
  1317. if (!when.defined(p)) {
  1318. return undefined;
  1319. }
  1320. var n = Cartesian3.multiplyComponents(
  1321. p,
  1322. oneOverRadiiSquared,
  1323. cartesianToCartographicN
  1324. );
  1325. n = Cartesian3.normalize(n, n);
  1326. var h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH);
  1327. var longitude = Math.atan2(n.y, n.x);
  1328. var latitude = Math.asin(n.z);
  1329. var height =
  1330. _Math.CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  1331. if (!when.defined(result)) {
  1332. return new Cartographic(longitude, latitude, height);
  1333. }
  1334. result.longitude = longitude;
  1335. result.latitude = latitude;
  1336. result.height = height;
  1337. return result;
  1338. };
  1339. /**
  1340. * Creates a new Cartesian3 instance from a Cartographic input. The values in the inputted
  1341. * object should be in radians.
  1342. *
  1343. * @param {Cartographic} cartographic Input to be converted into a Cartesian3 output.
  1344. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the position lies.
  1345. * @param {Cartesian3} [result] The object onto which to store the result.
  1346. * @returns {Cartesian3} The position
  1347. */
  1348. Cartographic.toCartesian = function (cartographic, ellipsoid, result) {
  1349. //>>includeStart('debug', pragmas.debug);
  1350. Check.Check.defined("cartographic", cartographic);
  1351. //>>includeEnd('debug');
  1352. return Cartesian3.fromRadians(
  1353. cartographic.longitude,
  1354. cartographic.latitude,
  1355. cartographic.height,
  1356. ellipsoid,
  1357. result
  1358. );
  1359. };
  1360. /**
  1361. * Duplicates a Cartographic instance.
  1362. *
  1363. * @param {Cartographic} cartographic The cartographic to duplicate.
  1364. * @param {Cartographic} [result] The object onto which to store the result.
  1365. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided. (Returns undefined if cartographic is undefined)
  1366. */
  1367. Cartographic.clone = function (cartographic, result) {
  1368. if (!when.defined(cartographic)) {
  1369. return undefined;
  1370. }
  1371. if (!when.defined(result)) {
  1372. return new Cartographic(
  1373. cartographic.longitude,
  1374. cartographic.latitude,
  1375. cartographic.height
  1376. );
  1377. }
  1378. result.longitude = cartographic.longitude;
  1379. result.latitude = cartographic.latitude;
  1380. result.height = cartographic.height;
  1381. return result;
  1382. };
  1383. /**
  1384. * Compares the provided cartographics componentwise and returns
  1385. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1386. *
  1387. * @param {Cartographic} [left] The first cartographic.
  1388. * @param {Cartographic} [right] The second cartographic.
  1389. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1390. */
  1391. Cartographic.equals = function (left, right) {
  1392. return (
  1393. left === right ||
  1394. (when.defined(left) &&
  1395. when.defined(right) &&
  1396. left.longitude === right.longitude &&
  1397. left.latitude === right.latitude &&
  1398. left.height === right.height)
  1399. );
  1400. };
  1401. /**
  1402. * Compares the provided cartographics componentwise and returns
  1403. * <code>true</code> if they are within the provided epsilon,
  1404. * <code>false</code> otherwise.
  1405. *
  1406. * @param {Cartographic} [left] The first cartographic.
  1407. * @param {Cartographic} [right] The second cartographic.
  1408. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1409. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1410. */
  1411. Cartographic.equalsEpsilon = function (left, right, epsilon) {
  1412. epsilon = when.defaultValue(epsilon, 0);
  1413. return (
  1414. left === right ||
  1415. (when.defined(left) &&
  1416. when.defined(right) &&
  1417. Math.abs(left.longitude - right.longitude) <= epsilon &&
  1418. Math.abs(left.latitude - right.latitude) <= epsilon &&
  1419. Math.abs(left.height - right.height) <= epsilon)
  1420. );
  1421. };
  1422. /**
  1423. * An immutable Cartographic instance initialized to (0.0, 0.0, 0.0).
  1424. *
  1425. * @type {Cartographic}
  1426. * @constant
  1427. */
  1428. Cartographic.ZERO = Object.freeze(new Cartographic(0.0, 0.0, 0.0));
  1429. /**
  1430. * Duplicates this instance.
  1431. *
  1432. * @param {Cartographic} [result] The object onto which to store the result.
  1433. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if one was not provided.
  1434. */
  1435. Cartographic.prototype.clone = function (result) {
  1436. return Cartographic.clone(this, result);
  1437. };
  1438. /**
  1439. * Compares the provided against this cartographic componentwise and returns
  1440. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1441. *
  1442. * @param {Cartographic} [right] The second cartographic.
  1443. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1444. */
  1445. Cartographic.prototype.equals = function (right) {
  1446. return Cartographic.equals(this, right);
  1447. };
  1448. /**
  1449. * Compares the provided against this cartographic componentwise and returns
  1450. * <code>true</code> if they are within the provided epsilon,
  1451. * <code>false</code> otherwise.
  1452. *
  1453. * @param {Cartographic} [right] The second cartographic.
  1454. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  1455. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1456. */
  1457. Cartographic.prototype.equalsEpsilon = function (right, epsilon) {
  1458. return Cartographic.equalsEpsilon(this, right, epsilon);
  1459. };
  1460. /**
  1461. * Creates a string representing this cartographic in the format '(longitude, latitude, height)'.
  1462. *
  1463. * @returns {String} A string representing the provided cartographic in the format '(longitude, latitude, height)'.
  1464. */
  1465. Cartographic.prototype.toString = function () {
  1466. return "(" + this.longitude + ", " + this.latitude + ", " + this.height + ")";
  1467. };
  1468. function initialize(ellipsoid, x, y, z) {
  1469. x = when.defaultValue(x, 0.0);
  1470. y = when.defaultValue(y, 0.0);
  1471. z = when.defaultValue(z, 0.0);
  1472. //>>includeStart('debug', pragmas.debug);
  1473. Check.Check.typeOf.number.greaterThanOrEquals("x", x, 0.0);
  1474. Check.Check.typeOf.number.greaterThanOrEquals("y", y, 0.0);
  1475. Check.Check.typeOf.number.greaterThanOrEquals("z", z, 0.0);
  1476. //>>includeEnd('debug');
  1477. ellipsoid._radii = new Cartesian3(x, y, z);
  1478. ellipsoid._radiiSquared = new Cartesian3(x * x, y * y, z * z);
  1479. ellipsoid._radiiToTheFourth = new Cartesian3(
  1480. x * x * x * x,
  1481. y * y * y * y,
  1482. z * z * z * z
  1483. );
  1484. ellipsoid._oneOverRadii = new Cartesian3(
  1485. x === 0.0 ? 0.0 : 1.0 / x,
  1486. y === 0.0 ? 0.0 : 1.0 / y,
  1487. z === 0.0 ? 0.0 : 1.0 / z
  1488. );
  1489. ellipsoid._oneOverRadiiSquared = new Cartesian3(
  1490. x === 0.0 ? 0.0 : 1.0 / (x * x),
  1491. y === 0.0 ? 0.0 : 1.0 / (y * y),
  1492. z === 0.0 ? 0.0 : 1.0 / (z * z)
  1493. );
  1494. ellipsoid._minimumRadius = Math.min(x, y, z);
  1495. ellipsoid._maximumRadius = Math.max(x, y, z);
  1496. ellipsoid._centerToleranceSquared = _Math.CesiumMath.EPSILON1;
  1497. if (ellipsoid._radiiSquared.z !== 0) {
  1498. ellipsoid._squaredXOverSquaredZ =
  1499. ellipsoid._radiiSquared.x / ellipsoid._radiiSquared.z;
  1500. }
  1501. }
  1502. /**
  1503. * A quadratic surface defined in Cartesian coordinates by the equation
  1504. * <code>(x / a)^2 + (y / b)^2 + (z / c)^2 = 1</code>. Primarily used
  1505. * by Cesium to represent the shape of planetary bodies.
  1506. *
  1507. * Rather than constructing this object directly, one of the provided
  1508. * constants is normally used.
  1509. * @alias Ellipsoid
  1510. * @constructor
  1511. *
  1512. * @param {Number} [x=0] The radius in the x direction.
  1513. * @param {Number} [y=0] The radius in the y direction.
  1514. * @param {Number} [z=0] The radius in the z direction.
  1515. *
  1516. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  1517. *
  1518. * @see Ellipsoid.fromCartesian3
  1519. * @see Ellipsoid.WGS84
  1520. * @see Ellipsoid.UNIT_SPHERE
  1521. */
  1522. function Ellipsoid(x, y, z) {
  1523. this._radii = undefined;
  1524. this._radiiSquared = undefined;
  1525. this._radiiToTheFourth = undefined;
  1526. this._oneOverRadii = undefined;
  1527. this._oneOverRadiiSquared = undefined;
  1528. this._minimumRadius = undefined;
  1529. this._maximumRadius = undefined;
  1530. this._centerToleranceSquared = undefined;
  1531. this._squaredXOverSquaredZ = undefined;
  1532. initialize(this, x, y, z);
  1533. }
  1534. Object.defineProperties(Ellipsoid.prototype, {
  1535. /**
  1536. * Gets the radii of the ellipsoid.
  1537. * @memberof Ellipsoid.prototype
  1538. * @type {Cartesian3}
  1539. * @readonly
  1540. */
  1541. radii: {
  1542. get: function () {
  1543. return this._radii;
  1544. },
  1545. },
  1546. /**
  1547. * Gets the squared radii of the ellipsoid.
  1548. * @memberof Ellipsoid.prototype
  1549. * @type {Cartesian3}
  1550. * @readonly
  1551. */
  1552. radiiSquared: {
  1553. get: function () {
  1554. return this._radiiSquared;
  1555. },
  1556. },
  1557. /**
  1558. * Gets the radii of the ellipsoid raise to the fourth power.
  1559. * @memberof Ellipsoid.prototype
  1560. * @type {Cartesian3}
  1561. * @readonly
  1562. */
  1563. radiiToTheFourth: {
  1564. get: function () {
  1565. return this._radiiToTheFourth;
  1566. },
  1567. },
  1568. /**
  1569. * Gets one over the radii of the ellipsoid.
  1570. * @memberof Ellipsoid.prototype
  1571. * @type {Cartesian3}
  1572. * @readonly
  1573. */
  1574. oneOverRadii: {
  1575. get: function () {
  1576. return this._oneOverRadii;
  1577. },
  1578. },
  1579. /**
  1580. * Gets one over the squared radii of the ellipsoid.
  1581. * @memberof Ellipsoid.prototype
  1582. * @type {Cartesian3}
  1583. * @readonly
  1584. */
  1585. oneOverRadiiSquared: {
  1586. get: function () {
  1587. return this._oneOverRadiiSquared;
  1588. },
  1589. },
  1590. /**
  1591. * Gets the minimum radius of the ellipsoid.
  1592. * @memberof Ellipsoid.prototype
  1593. * @type {Number}
  1594. * @readonly
  1595. */
  1596. minimumRadius: {
  1597. get: function () {
  1598. return this._minimumRadius;
  1599. },
  1600. },
  1601. /**
  1602. * Gets the maximum radius of the ellipsoid.
  1603. * @memberof Ellipsoid.prototype
  1604. * @type {Number}
  1605. * @readonly
  1606. */
  1607. maximumRadius: {
  1608. get: function () {
  1609. return this._maximumRadius;
  1610. },
  1611. },
  1612. });
  1613. /**
  1614. * Duplicates an Ellipsoid instance.
  1615. *
  1616. * @param {Ellipsoid} ellipsoid The ellipsoid to duplicate.
  1617. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1618. * instance should be created.
  1619. * @returns {Ellipsoid} The cloned Ellipsoid. (Returns undefined if ellipsoid is undefined)
  1620. */
  1621. Ellipsoid.clone = function (ellipsoid, result) {
  1622. if (!when.defined(ellipsoid)) {
  1623. return undefined;
  1624. }
  1625. var radii = ellipsoid._radii;
  1626. if (!when.defined(result)) {
  1627. return new Ellipsoid(radii.x, radii.y, radii.z);
  1628. }
  1629. Cartesian3.clone(radii, result._radii);
  1630. Cartesian3.clone(ellipsoid._radiiSquared, result._radiiSquared);
  1631. Cartesian3.clone(ellipsoid._radiiToTheFourth, result._radiiToTheFourth);
  1632. Cartesian3.clone(ellipsoid._oneOverRadii, result._oneOverRadii);
  1633. Cartesian3.clone(ellipsoid._oneOverRadiiSquared, result._oneOverRadiiSquared);
  1634. result._minimumRadius = ellipsoid._minimumRadius;
  1635. result._maximumRadius = ellipsoid._maximumRadius;
  1636. result._centerToleranceSquared = ellipsoid._centerToleranceSquared;
  1637. return result;
  1638. };
  1639. /**
  1640. * Computes an Ellipsoid from a Cartesian specifying the radii in x, y, and z directions.
  1641. *
  1642. * @param {Cartesian3} [cartesian=Cartesian3.ZERO] The ellipsoid's radius in the x, y, and z directions.
  1643. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1644. * instance should be created.
  1645. * @returns {Ellipsoid} A new Ellipsoid instance.
  1646. *
  1647. * @exception {DeveloperError} All radii components must be greater than or equal to zero.
  1648. *
  1649. * @see Ellipsoid.WGS84
  1650. * @see Ellipsoid.UNIT_SPHERE
  1651. */
  1652. Ellipsoid.fromCartesian3 = function (cartesian, result) {
  1653. if (!when.defined(result)) {
  1654. result = new Ellipsoid();
  1655. }
  1656. if (!when.defined(cartesian)) {
  1657. return result;
  1658. }
  1659. initialize(result, cartesian.x, cartesian.y, cartesian.z);
  1660. return result;
  1661. };
  1662. /**
  1663. * An Ellipsoid instance initialized to the WGS84 standard.
  1664. *
  1665. * @type {Ellipsoid}
  1666. * @constant
  1667. */
  1668. Ellipsoid.WGS84 = Object.freeze(
  1669. new Ellipsoid(6378137.0, 6378137.0, 6356752.3142451793)
  1670. );
  1671. /**
  1672. * An Ellipsoid instance initialized to radii of (1.0, 1.0, 1.0).
  1673. *
  1674. * @type {Ellipsoid}
  1675. * @constant
  1676. */
  1677. Ellipsoid.UNIT_SPHERE = Object.freeze(new Ellipsoid(1.0, 1.0, 1.0));
  1678. /**
  1679. * An Ellipsoid instance initialized to a sphere with the lunar radius.
  1680. *
  1681. * @type {Ellipsoid}
  1682. * @constant
  1683. */
  1684. Ellipsoid.MOON = Object.freeze(
  1685. new Ellipsoid(
  1686. _Math.CesiumMath.LUNAR_RADIUS,
  1687. _Math.CesiumMath.LUNAR_RADIUS,
  1688. _Math.CesiumMath.LUNAR_RADIUS
  1689. )
  1690. );
  1691. /**
  1692. * Duplicates an Ellipsoid instance.
  1693. *
  1694. * @param {Ellipsoid} [result] The object onto which to store the result, or undefined if a new
  1695. * instance should be created.
  1696. * @returns {Ellipsoid} The cloned Ellipsoid.
  1697. */
  1698. Ellipsoid.prototype.clone = function (result) {
  1699. return Ellipsoid.clone(this, result);
  1700. };
  1701. /**
  1702. * The number of elements used to pack the object into an array.
  1703. * @type {Number}
  1704. */
  1705. Ellipsoid.packedLength = Cartesian3.packedLength;
  1706. /**
  1707. * Stores the provided instance into the provided array.
  1708. *
  1709. * @param {Ellipsoid} value The value to pack.
  1710. * @param {Number[]} array The array to pack into.
  1711. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1712. *
  1713. * @returns {Number[]} The array that was packed into
  1714. */
  1715. Ellipsoid.pack = function (value, array, startingIndex) {
  1716. //>>includeStart('debug', pragmas.debug);
  1717. Check.Check.typeOf.object("value", value);
  1718. Check.Check.defined("array", array);
  1719. //>>includeEnd('debug');
  1720. startingIndex = when.defaultValue(startingIndex, 0);
  1721. Cartesian3.pack(value._radii, array, startingIndex);
  1722. return array;
  1723. };
  1724. /**
  1725. * Retrieves an instance from a packed array.
  1726. *
  1727. * @param {Number[]} array The packed array.
  1728. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1729. * @param {Ellipsoid} [result] The object into which to store the result.
  1730. * @returns {Ellipsoid} The modified result parameter or a new Ellipsoid instance if one was not provided.
  1731. */
  1732. Ellipsoid.unpack = function (array, startingIndex, result) {
  1733. //>>includeStart('debug', pragmas.debug);
  1734. Check.Check.defined("array", array);
  1735. //>>includeEnd('debug');
  1736. startingIndex = when.defaultValue(startingIndex, 0);
  1737. var radii = Cartesian3.unpack(array, startingIndex);
  1738. return Ellipsoid.fromCartesian3(radii, result);
  1739. };
  1740. /**
  1741. * Computes the unit vector directed from the center of this ellipsoid toward the provided Cartesian position.
  1742. * @function
  1743. *
  1744. * @param {Cartesian3} cartesian The Cartesian for which to to determine the geocentric normal.
  1745. * @param {Cartesian3} [result] The object onto which to store the result.
  1746. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1747. */
  1748. Ellipsoid.prototype.geocentricSurfaceNormal = Cartesian3.normalize;
  1749. /**
  1750. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  1751. *
  1752. * @param {Cartographic} cartographic The cartographic position for which to to determine the geodetic normal.
  1753. * @param {Cartesian3} [result] The object onto which to store the result.
  1754. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1755. */
  1756. Ellipsoid.prototype.geodeticSurfaceNormalCartographic = function (
  1757. cartographic,
  1758. result
  1759. ) {
  1760. //>>includeStart('debug', pragmas.debug);
  1761. Check.Check.typeOf.object("cartographic", cartographic);
  1762. //>>includeEnd('debug');
  1763. var longitude = cartographic.longitude;
  1764. var latitude = cartographic.latitude;
  1765. var cosLatitude = Math.cos(latitude);
  1766. var x = cosLatitude * Math.cos(longitude);
  1767. var y = cosLatitude * Math.sin(longitude);
  1768. var z = Math.sin(latitude);
  1769. if (!when.defined(result)) {
  1770. result = new Cartesian3();
  1771. }
  1772. result.x = x;
  1773. result.y = y;
  1774. result.z = z;
  1775. return Cartesian3.normalize(result, result);
  1776. };
  1777. /**
  1778. * Computes the normal of the plane tangent to the surface of the ellipsoid at the provided position.
  1779. *
  1780. * @param {Cartesian3} cartesian The Cartesian position for which to to determine the surface normal.
  1781. * @param {Cartesian3} [result] The object onto which to store the result.
  1782. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1783. */
  1784. Ellipsoid.prototype.geodeticSurfaceNormal = function (cartesian, result) {
  1785. if (
  1786. Cartesian3.equalsEpsilon(cartesian, Cartesian3.ZERO, _Math.CesiumMath.EPSILON14)
  1787. ) {
  1788. return undefined;
  1789. }
  1790. if (!when.defined(result)) {
  1791. result = new Cartesian3();
  1792. }
  1793. result = Cartesian3.multiplyComponents(
  1794. cartesian,
  1795. this._oneOverRadiiSquared,
  1796. result
  1797. );
  1798. return Cartesian3.normalize(result, result);
  1799. };
  1800. var cartographicToCartesianNormal = new Cartesian3();
  1801. var cartographicToCartesianK = new Cartesian3();
  1802. /**
  1803. * Converts the provided cartographic to Cartesian representation.
  1804. *
  1805. * @param {Cartographic} cartographic The cartographic position.
  1806. * @param {Cartesian3} [result] The object onto which to store the result.
  1807. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1808. *
  1809. * @example
  1810. * //Create a Cartographic and determine it's Cartesian representation on a WGS84 ellipsoid.
  1811. * var position = new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 5000);
  1812. * var cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(position);
  1813. */
  1814. Ellipsoid.prototype.cartographicToCartesian = function (cartographic, result) {
  1815. //`cartographic is required` is thrown from geodeticSurfaceNormalCartographic.
  1816. var n = cartographicToCartesianNormal;
  1817. var k = cartographicToCartesianK;
  1818. this.geodeticSurfaceNormalCartographic(cartographic, n);
  1819. Cartesian3.multiplyComponents(this._radiiSquared, n, k);
  1820. var gamma = Math.sqrt(Cartesian3.dot(n, k));
  1821. Cartesian3.divideByScalar(k, gamma, k);
  1822. Cartesian3.multiplyByScalar(n, cartographic.height, n);
  1823. if (!when.defined(result)) {
  1824. result = new Cartesian3();
  1825. }
  1826. return Cartesian3.add(k, n, result);
  1827. };
  1828. /**
  1829. * Converts the provided array of cartographics to an array of Cartesians.
  1830. *
  1831. * @param {Cartographic[]} cartographics An array of cartographic positions.
  1832. * @param {Cartesian3[]} [result] The object onto which to store the result.
  1833. * @returns {Cartesian3[]} The modified result parameter or a new Array instance if none was provided.
  1834. *
  1835. * @example
  1836. * //Convert an array of Cartographics and determine their Cartesian representation on a WGS84 ellipsoid.
  1837. * var positions = [new Cesium.Cartographic(Cesium.Math.toRadians(21), Cesium.Math.toRadians(78), 0),
  1838. * new Cesium.Cartographic(Cesium.Math.toRadians(21.321), Cesium.Math.toRadians(78.123), 100),
  1839. * new Cesium.Cartographic(Cesium.Math.toRadians(21.645), Cesium.Math.toRadians(78.456), 250)];
  1840. * var cartesianPositions = Cesium.Ellipsoid.WGS84.cartographicArrayToCartesianArray(positions);
  1841. */
  1842. Ellipsoid.prototype.cartographicArrayToCartesianArray = function (
  1843. cartographics,
  1844. result
  1845. ) {
  1846. //>>includeStart('debug', pragmas.debug);
  1847. Check.Check.defined("cartographics", cartographics);
  1848. //>>includeEnd('debug')
  1849. var length = cartographics.length;
  1850. if (!when.defined(result)) {
  1851. result = new Array(length);
  1852. } else {
  1853. result.length = length;
  1854. }
  1855. for (var i = 0; i < length; i++) {
  1856. result[i] = this.cartographicToCartesian(cartographics[i], result[i]);
  1857. }
  1858. return result;
  1859. };
  1860. var cartesianToCartographicN$1 = new Cartesian3();
  1861. var cartesianToCartographicP$1 = new Cartesian3();
  1862. var cartesianToCartographicH$1 = new Cartesian3();
  1863. /**
  1864. * Converts the provided cartesian to cartographic representation.
  1865. * The cartesian is undefined at the center of the ellipsoid.
  1866. *
  1867. * @param {Cartesian3} cartesian The Cartesian position to convert to cartographic representation.
  1868. * @param {Cartographic} [result] The object onto which to store the result.
  1869. * @returns {Cartographic} The modified result parameter, new Cartographic instance if none was provided, or undefined if the cartesian is at the center of the ellipsoid.
  1870. *
  1871. * @example
  1872. * //Create a Cartesian and determine it's Cartographic representation on a WGS84 ellipsoid.
  1873. * var position = new Cesium.Cartesian3(17832.12, 83234.52, 952313.73);
  1874. * var cartographicPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
  1875. */
  1876. Ellipsoid.prototype.cartesianToCartographic = function (cartesian, result) {
  1877. //`cartesian is required.` is thrown from scaleToGeodeticSurface
  1878. var p = this.scaleToGeodeticSurface(cartesian, cartesianToCartographicP$1);
  1879. if (!when.defined(p)) {
  1880. return undefined;
  1881. }
  1882. var n = this.geodeticSurfaceNormal(p, cartesianToCartographicN$1);
  1883. var h = Cartesian3.subtract(cartesian, p, cartesianToCartographicH$1);
  1884. var longitude = Math.atan2(n.y, n.x);
  1885. var latitude = Math.asin(n.z);
  1886. var height =
  1887. _Math.CesiumMath.sign(Cartesian3.dot(h, cartesian)) * Cartesian3.magnitude(h);
  1888. if (!when.defined(result)) {
  1889. return new Cartographic(longitude, latitude, height);
  1890. }
  1891. result.longitude = longitude;
  1892. result.latitude = latitude;
  1893. result.height = height;
  1894. return result;
  1895. };
  1896. /**
  1897. * Converts the provided array of cartesians to an array of cartographics.
  1898. *
  1899. * @param {Cartesian3[]} cartesians An array of Cartesian positions.
  1900. * @param {Cartographic[]} [result] The object onto which to store the result.
  1901. * @returns {Cartographic[]} The modified result parameter or a new Array instance if none was provided.
  1902. *
  1903. * @example
  1904. * //Create an array of Cartesians and determine their Cartographic representation on a WGS84 ellipsoid.
  1905. * var positions = [new Cesium.Cartesian3(17832.12, 83234.52, 952313.73),
  1906. * new Cesium.Cartesian3(17832.13, 83234.53, 952313.73),
  1907. * new Cesium.Cartesian3(17832.14, 83234.54, 952313.73)]
  1908. * var cartographicPositions = Cesium.Ellipsoid.WGS84.cartesianArrayToCartographicArray(positions);
  1909. */
  1910. Ellipsoid.prototype.cartesianArrayToCartographicArray = function (
  1911. cartesians,
  1912. result
  1913. ) {
  1914. //>>includeStart('debug', pragmas.debug);
  1915. Check.Check.defined("cartesians", cartesians);
  1916. //>>includeEnd('debug');
  1917. var length = cartesians.length;
  1918. if (!when.defined(result)) {
  1919. result = new Array(length);
  1920. } else {
  1921. result.length = length;
  1922. }
  1923. for (var i = 0; i < length; ++i) {
  1924. result[i] = this.cartesianToCartographic(cartesians[i], result[i]);
  1925. }
  1926. return result;
  1927. };
  1928. /**
  1929. * Scales the provided Cartesian position along the geodetic surface normal
  1930. * so that it is on the surface of this ellipsoid. If the position is
  1931. * at the center of the ellipsoid, this function returns undefined.
  1932. *
  1933. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1934. * @param {Cartesian3} [result] The object onto which to store the result.
  1935. * @returns {Cartesian3} The modified result parameter, a new Cartesian3 instance if none was provided, or undefined if the position is at the center.
  1936. */
  1937. Ellipsoid.prototype.scaleToGeodeticSurface = function (cartesian, result) {
  1938. return scaleToGeodeticSurface(
  1939. cartesian,
  1940. this._oneOverRadii,
  1941. this._oneOverRadiiSquared,
  1942. this._centerToleranceSquared,
  1943. result
  1944. );
  1945. };
  1946. /**
  1947. * Scales the provided Cartesian position along the geocentric surface normal
  1948. * so that it is on the surface of this ellipsoid.
  1949. *
  1950. * @param {Cartesian3} cartesian The Cartesian position to scale.
  1951. * @param {Cartesian3} [result] The object onto which to store the result.
  1952. * @returns {Cartesian3} The modified result parameter or a new Cartesian3 instance if none was provided.
  1953. */
  1954. Ellipsoid.prototype.scaleToGeocentricSurface = function (cartesian, result) {
  1955. //>>includeStart('debug', pragmas.debug);
  1956. Check.Check.typeOf.object("cartesian", cartesian);
  1957. //>>includeEnd('debug');
  1958. if (!when.defined(result)) {
  1959. result = new Cartesian3();
  1960. }
  1961. var positionX = cartesian.x;
  1962. var positionY = cartesian.y;
  1963. var positionZ = cartesian.z;
  1964. var oneOverRadiiSquared = this._oneOverRadiiSquared;
  1965. var beta =
  1966. 1.0 /
  1967. Math.sqrt(
  1968. positionX * positionX * oneOverRadiiSquared.x +
  1969. positionY * positionY * oneOverRadiiSquared.y +
  1970. positionZ * positionZ * oneOverRadiiSquared.z
  1971. );
  1972. return Cartesian3.multiplyByScalar(cartesian, beta, result);
  1973. };
  1974. /**
  1975. * Transforms a Cartesian X, Y, Z position to the ellipsoid-scaled space by multiplying
  1976. * its components by the result of {@link Ellipsoid#oneOverRadii}.
  1977. *
  1978. * @param {Cartesian3} position The position to transform.
  1979. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  1980. * return a new instance.
  1981. * @returns {Cartesian3} The position expressed in the scaled space. The returned instance is the
  1982. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  1983. */
  1984. Ellipsoid.prototype.transformPositionToScaledSpace = function (
  1985. position,
  1986. result
  1987. ) {
  1988. if (!when.defined(result)) {
  1989. result = new Cartesian3();
  1990. }
  1991. return Cartesian3.multiplyComponents(position, this._oneOverRadii, result);
  1992. };
  1993. /**
  1994. * Transforms a Cartesian X, Y, Z position from the ellipsoid-scaled space by multiplying
  1995. * its components by the result of {@link Ellipsoid#radii}.
  1996. *
  1997. * @param {Cartesian3} position The position to transform.
  1998. * @param {Cartesian3} [result] The position to which to copy the result, or undefined to create and
  1999. * return a new instance.
  2000. * @returns {Cartesian3} The position expressed in the unscaled space. The returned instance is the
  2001. * one passed as the result parameter if it is not undefined, or a new instance of it is.
  2002. */
  2003. Ellipsoid.prototype.transformPositionFromScaledSpace = function (
  2004. position,
  2005. result
  2006. ) {
  2007. if (!when.defined(result)) {
  2008. result = new Cartesian3();
  2009. }
  2010. return Cartesian3.multiplyComponents(position, this._radii, result);
  2011. };
  2012. /**
  2013. * Compares this Ellipsoid against the provided Ellipsoid componentwise and returns
  2014. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2015. *
  2016. * @param {Ellipsoid} [right] The other Ellipsoid.
  2017. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  2018. */
  2019. Ellipsoid.prototype.equals = function (right) {
  2020. return (
  2021. this === right ||
  2022. (when.defined(right) && Cartesian3.equals(this._radii, right._radii))
  2023. );
  2024. };
  2025. /**
  2026. * Creates a string representing this Ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  2027. *
  2028. * @returns {String} A string representing this ellipsoid in the format '(radii.x, radii.y, radii.z)'.
  2029. */
  2030. Ellipsoid.prototype.toString = function () {
  2031. return this._radii.toString();
  2032. };
  2033. /**
  2034. * Computes a point which is the intersection of the surface normal with the z-axis.
  2035. *
  2036. * @param {Cartesian3} position the position. must be on the surface of the ellipsoid.
  2037. * @param {Number} [buffer = 0.0] A buffer to subtract from the ellipsoid size when checking if the point is inside the ellipsoid.
  2038. * In earth case, with common earth datums, there is no need for this buffer since the intersection point is always (relatively) very close to the center.
  2039. * In WGS84 datum, intersection point is at max z = +-42841.31151331382 (0.673% of z-axis).
  2040. * Intersection point could be outside the ellipsoid if the ratio of MajorAxis / AxisOfRotation is bigger than the square root of 2
  2041. * @param {Cartesian3} [result] The cartesian to which to copy the result, or undefined to create and
  2042. * return a new instance.
  2043. * @returns {Cartesian3 | undefined} the intersection point if it's inside the ellipsoid, undefined otherwise
  2044. *
  2045. * @exception {DeveloperError} position is required.
  2046. * @exception {DeveloperError} Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y).
  2047. * @exception {DeveloperError} Ellipsoid.radii.z must be greater than 0.
  2048. */
  2049. Ellipsoid.prototype.getSurfaceNormalIntersectionWithZAxis = function (
  2050. position,
  2051. buffer,
  2052. result
  2053. ) {
  2054. //>>includeStart('debug', pragmas.debug);
  2055. Check.Check.typeOf.object("position", position);
  2056. if (
  2057. !_Math.CesiumMath.equalsEpsilon(
  2058. this._radii.x,
  2059. this._radii.y,
  2060. _Math.CesiumMath.EPSILON15
  2061. )
  2062. ) {
  2063. throw new Check.DeveloperError(
  2064. "Ellipsoid must be an ellipsoid of revolution (radii.x == radii.y)"
  2065. );
  2066. }
  2067. Check.Check.typeOf.number.greaterThan("Ellipsoid.radii.z", this._radii.z, 0);
  2068. //>>includeEnd('debug');
  2069. buffer = when.defaultValue(buffer, 0.0);
  2070. var squaredXOverSquaredZ = this._squaredXOverSquaredZ;
  2071. if (!when.defined(result)) {
  2072. result = new Cartesian3();
  2073. }
  2074. result.x = 0.0;
  2075. result.y = 0.0;
  2076. result.z = position.z * (1 - squaredXOverSquaredZ);
  2077. if (Math.abs(result.z) >= this._radii.z - buffer) {
  2078. return undefined;
  2079. }
  2080. return result;
  2081. };
  2082. var abscissas = [
  2083. 0.14887433898163,
  2084. 0.43339539412925,
  2085. 0.67940956829902,
  2086. 0.86506336668898,
  2087. 0.97390652851717,
  2088. 0.0,
  2089. ];
  2090. var weights = [
  2091. 0.29552422471475,
  2092. 0.26926671930999,
  2093. 0.21908636251598,
  2094. 0.14945134915058,
  2095. 0.066671344308684,
  2096. 0.0,
  2097. ];
  2098. /**
  2099. * Compute the 10th order Gauss-Legendre Quadrature of the given definite integral.
  2100. *
  2101. * @param {Number} a The lower bound for the integration.
  2102. * @param {Number} b The upper bound for the integration.
  2103. * @param {Ellipsoid~RealValuedScalarFunction} func The function to integrate.
  2104. * @returns {Number} The value of the integral of the given function over the given domain.
  2105. *
  2106. * @private
  2107. */
  2108. function gaussLegendreQuadrature(a, b, func) {
  2109. //>>includeStart('debug', pragmas.debug);
  2110. Check.Check.typeOf.number("a", a);
  2111. Check.Check.typeOf.number("b", b);
  2112. Check.Check.typeOf.func("func", func);
  2113. //>>includeEnd('debug');
  2114. // The range is half of the normal range since the five weights add to one (ten weights add to two).
  2115. // The values of the abscissas are multiplied by two to account for this.
  2116. var xMean = 0.5 * (b + a);
  2117. var xRange = 0.5 * (b - a);
  2118. var sum = 0.0;
  2119. for (var i = 0; i < 5; i++) {
  2120. var dx = xRange * abscissas[i];
  2121. sum += weights[i] * (func(xMean + dx) + func(xMean - dx));
  2122. }
  2123. // Scale the sum to the range of x.
  2124. sum *= xRange;
  2125. return sum;
  2126. }
  2127. /**
  2128. * A real valued scalar function.
  2129. * @callback Ellipsoid~RealValuedScalarFunction
  2130. *
  2131. * @param {Number} x The value used to evaluate the function.
  2132. * @returns {Number} The value of the function at x.
  2133. *
  2134. * @private
  2135. */
  2136. /**
  2137. * Computes an approximation of the surface area of a rectangle on the surface of an ellipsoid using
  2138. * Gauss-Legendre 10th order quadrature.
  2139. *
  2140. * @param {Rectangle} rectangle The rectangle used for computing the surface area.
  2141. * @returns {Number} The approximate area of the rectangle on the surface of this ellipsoid.
  2142. */
  2143. Ellipsoid.prototype.surfaceArea = function (rectangle) {
  2144. //>>includeStart('debug', pragmas.debug);
  2145. Check.Check.typeOf.object("rectangle", rectangle);
  2146. //>>includeEnd('debug');
  2147. var minLongitude = rectangle.west;
  2148. var maxLongitude = rectangle.east;
  2149. var minLatitude = rectangle.south;
  2150. var maxLatitude = rectangle.north;
  2151. while (maxLongitude < minLongitude) {
  2152. maxLongitude += _Math.CesiumMath.TWO_PI;
  2153. }
  2154. var radiiSquared = this._radiiSquared;
  2155. var a2 = radiiSquared.x;
  2156. var b2 = radiiSquared.y;
  2157. var c2 = radiiSquared.z;
  2158. var a2b2 = a2 * b2;
  2159. return gaussLegendreQuadrature(minLatitude, maxLatitude, function (lat) {
  2160. // phi represents the angle measured from the north pole
  2161. // sin(phi) = sin(pi / 2 - lat) = cos(lat), cos(phi) is similar
  2162. var sinPhi = Math.cos(lat);
  2163. var cosPhi = Math.sin(lat);
  2164. return (
  2165. Math.cos(lat) *
  2166. gaussLegendreQuadrature(minLongitude, maxLongitude, function (lon) {
  2167. var cosTheta = Math.cos(lon);
  2168. var sinTheta = Math.sin(lon);
  2169. return Math.sqrt(
  2170. a2b2 * cosPhi * cosPhi +
  2171. c2 *
  2172. (b2 * cosTheta * cosTheta + a2 * sinTheta * sinTheta) *
  2173. sinPhi *
  2174. sinPhi
  2175. );
  2176. })
  2177. );
  2178. });
  2179. };
  2180. /**
  2181. * A two dimensional region specified as longitude and latitude coordinates.
  2182. *
  2183. * @alias Rectangle
  2184. * @constructor
  2185. *
  2186. * @param {Number} [west=0.0] The westernmost longitude, in radians, in the range [-Pi, Pi].
  2187. * @param {Number} [south=0.0] The southernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  2188. * @param {Number} [east=0.0] The easternmost longitude, in radians, in the range [-Pi, Pi].
  2189. * @param {Number} [north=0.0] The northernmost latitude, in radians, in the range [-Pi/2, Pi/2].
  2190. *
  2191. * @see Packable
  2192. */
  2193. function Rectangle(west, south, east, north) {
  2194. /**
  2195. * The westernmost longitude in radians in the range [-Pi, Pi].
  2196. *
  2197. * @type {Number}
  2198. * @default 0.0
  2199. */
  2200. this.west = when.defaultValue(west, 0.0);
  2201. /**
  2202. * The southernmost latitude in radians in the range [-Pi/2, Pi/2].
  2203. *
  2204. * @type {Number}
  2205. * @default 0.0
  2206. */
  2207. this.south = when.defaultValue(south, 0.0);
  2208. /**
  2209. * The easternmost longitude in radians in the range [-Pi, Pi].
  2210. *
  2211. * @type {Number}
  2212. * @default 0.0
  2213. */
  2214. this.east = when.defaultValue(east, 0.0);
  2215. /**
  2216. * The northernmost latitude in radians in the range [-Pi/2, Pi/2].
  2217. *
  2218. * @type {Number}
  2219. * @default 0.0
  2220. */
  2221. this.north = when.defaultValue(north, 0.0);
  2222. }
  2223. Object.defineProperties(Rectangle.prototype, {
  2224. /**
  2225. * Gets the width of the rectangle in radians.
  2226. * @memberof Rectangle.prototype
  2227. * @type {Number}
  2228. * @readonly
  2229. */
  2230. width: {
  2231. get: function () {
  2232. return Rectangle.computeWidth(this);
  2233. },
  2234. },
  2235. /**
  2236. * Gets the height of the rectangle in radians.
  2237. * @memberof Rectangle.prototype
  2238. * @type {Number}
  2239. * @readonly
  2240. */
  2241. height: {
  2242. get: function () {
  2243. return Rectangle.computeHeight(this);
  2244. },
  2245. },
  2246. });
  2247. /**
  2248. * The number of elements used to pack the object into an array.
  2249. * @type {Number}
  2250. */
  2251. Rectangle.packedLength = 4;
  2252. /**
  2253. * Stores the provided instance into the provided array.
  2254. *
  2255. * @param {Rectangle} value The value to pack.
  2256. * @param {Number[]} array The array to pack into.
  2257. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  2258. *
  2259. * @returns {Number[]} The array that was packed into
  2260. */
  2261. Rectangle.pack = function (value, array, startingIndex) {
  2262. //>>includeStart('debug', pragmas.debug);
  2263. Check.Check.typeOf.object("value", value);
  2264. Check.Check.defined("array", array);
  2265. //>>includeEnd('debug');
  2266. startingIndex = when.defaultValue(startingIndex, 0);
  2267. array[startingIndex++] = value.west;
  2268. array[startingIndex++] = value.south;
  2269. array[startingIndex++] = value.east;
  2270. array[startingIndex] = value.north;
  2271. return array;
  2272. };
  2273. /**
  2274. * Retrieves an instance from a packed array.
  2275. *
  2276. * @param {Number[]} array The packed array.
  2277. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  2278. * @param {Rectangle} [result] The object into which to store the result.
  2279. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  2280. */
  2281. Rectangle.unpack = function (array, startingIndex, result) {
  2282. //>>includeStart('debug', pragmas.debug);
  2283. Check.Check.defined("array", array);
  2284. //>>includeEnd('debug');
  2285. startingIndex = when.defaultValue(startingIndex, 0);
  2286. if (!when.defined(result)) {
  2287. result = new Rectangle();
  2288. }
  2289. result.west = array[startingIndex++];
  2290. result.south = array[startingIndex++];
  2291. result.east = array[startingIndex++];
  2292. result.north = array[startingIndex];
  2293. return result;
  2294. };
  2295. /**
  2296. * Computes the width of a rectangle in radians.
  2297. * @param {Rectangle} rectangle The rectangle to compute the width of.
  2298. * @returns {Number} The width.
  2299. */
  2300. Rectangle.computeWidth = function (rectangle) {
  2301. //>>includeStart('debug', pragmas.debug);
  2302. Check.Check.typeOf.object("rectangle", rectangle);
  2303. //>>includeEnd('debug');
  2304. var east = rectangle.east;
  2305. var west = rectangle.west;
  2306. if (east < west) {
  2307. east += _Math.CesiumMath.TWO_PI;
  2308. }
  2309. return east - west;
  2310. };
  2311. /**
  2312. * Computes the height of a rectangle in radians.
  2313. * @param {Rectangle} rectangle The rectangle to compute the height of.
  2314. * @returns {Number} The height.
  2315. */
  2316. Rectangle.computeHeight = function (rectangle) {
  2317. //>>includeStart('debug', pragmas.debug);
  2318. Check.Check.typeOf.object("rectangle", rectangle);
  2319. //>>includeEnd('debug');
  2320. return rectangle.north - rectangle.south;
  2321. };
  2322. /**
  2323. * Creates a rectangle given the boundary longitude and latitude in degrees.
  2324. *
  2325. * @param {Number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
  2326. * @param {Number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
  2327. * @param {Number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
  2328. * @param {Number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
  2329. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  2330. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  2331. *
  2332. * @example
  2333. * var rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
  2334. */
  2335. Rectangle.fromDegrees = function (west, south, east, north, result) {
  2336. west = _Math.CesiumMath.toRadians(when.defaultValue(west, 0.0));
  2337. south = _Math.CesiumMath.toRadians(when.defaultValue(south, 0.0));
  2338. east = _Math.CesiumMath.toRadians(when.defaultValue(east, 0.0));
  2339. north = _Math.CesiumMath.toRadians(when.defaultValue(north, 0.0));
  2340. if (!when.defined(result)) {
  2341. return new Rectangle(west, south, east, north);
  2342. }
  2343. result.west = west;
  2344. result.south = south;
  2345. result.east = east;
  2346. result.north = north;
  2347. return result;
  2348. };
  2349. /**
  2350. * Creates a rectangle given the boundary longitude and latitude in radians.
  2351. *
  2352. * @param {Number} [west=0.0] The westernmost longitude in radians in the range [-Math.PI, Math.PI].
  2353. * @param {Number} [south=0.0] The southernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  2354. * @param {Number} [east=0.0] The easternmost longitude in radians in the range [-Math.PI, Math.PI].
  2355. * @param {Number} [north=0.0] The northernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
  2356. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  2357. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  2358. *
  2359. * @example
  2360. * var rectangle = Cesium.Rectangle.fromRadians(0.0, Math.PI/4, Math.PI/8, 3*Math.PI/4);
  2361. */
  2362. Rectangle.fromRadians = function (west, south, east, north, result) {
  2363. if (!when.defined(result)) {
  2364. return new Rectangle(west, south, east, north);
  2365. }
  2366. result.west = when.defaultValue(west, 0.0);
  2367. result.south = when.defaultValue(south, 0.0);
  2368. result.east = when.defaultValue(east, 0.0);
  2369. result.north = when.defaultValue(north, 0.0);
  2370. return result;
  2371. };
  2372. /**
  2373. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  2374. *
  2375. * @param {Cartographic[]} cartographics The list of Cartographic instances.
  2376. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  2377. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  2378. */
  2379. Rectangle.fromCartographicArray = function (cartographics, result) {
  2380. //>>includeStart('debug', pragmas.debug);
  2381. Check.Check.defined("cartographics", cartographics);
  2382. //>>includeEnd('debug');
  2383. var west = Number.MAX_VALUE;
  2384. var east = -Number.MAX_VALUE;
  2385. var westOverIDL = Number.MAX_VALUE;
  2386. var eastOverIDL = -Number.MAX_VALUE;
  2387. var south = Number.MAX_VALUE;
  2388. var north = -Number.MAX_VALUE;
  2389. for (var i = 0, len = cartographics.length; i < len; i++) {
  2390. var position = cartographics[i];
  2391. west = Math.min(west, position.longitude);
  2392. east = Math.max(east, position.longitude);
  2393. south = Math.min(south, position.latitude);
  2394. north = Math.max(north, position.latitude);
  2395. var lonAdjusted =
  2396. position.longitude >= 0
  2397. ? position.longitude
  2398. : position.longitude + _Math.CesiumMath.TWO_PI;
  2399. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  2400. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  2401. }
  2402. if (east - west > eastOverIDL - westOverIDL) {
  2403. west = westOverIDL;
  2404. east = eastOverIDL;
  2405. if (east > _Math.CesiumMath.PI) {
  2406. east = east - _Math.CesiumMath.TWO_PI;
  2407. }
  2408. if (west > _Math.CesiumMath.PI) {
  2409. west = west - _Math.CesiumMath.TWO_PI;
  2410. }
  2411. }
  2412. if (!when.defined(result)) {
  2413. return new Rectangle(west, south, east, north);
  2414. }
  2415. result.west = west;
  2416. result.south = south;
  2417. result.east = east;
  2418. result.north = north;
  2419. return result;
  2420. };
  2421. /**
  2422. * Creates the smallest possible Rectangle that encloses all positions in the provided array.
  2423. *
  2424. * @param {Cartesian3[]} cartesians The list of Cartesian instances.
  2425. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid the cartesians are on.
  2426. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  2427. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  2428. */
  2429. Rectangle.fromCartesianArray = function (cartesians, ellipsoid, result) {
  2430. //>>includeStart('debug', pragmas.debug);
  2431. Check.Check.defined("cartesians", cartesians);
  2432. //>>includeEnd('debug');
  2433. ellipsoid = when.defaultValue(ellipsoid, Ellipsoid.WGS84);
  2434. var west = Number.MAX_VALUE;
  2435. var east = -Number.MAX_VALUE;
  2436. var westOverIDL = Number.MAX_VALUE;
  2437. var eastOverIDL = -Number.MAX_VALUE;
  2438. var south = Number.MAX_VALUE;
  2439. var north = -Number.MAX_VALUE;
  2440. for (var i = 0, len = cartesians.length; i < len; i++) {
  2441. var position = ellipsoid.cartesianToCartographic(cartesians[i]);
  2442. west = Math.min(west, position.longitude);
  2443. east = Math.max(east, position.longitude);
  2444. south = Math.min(south, position.latitude);
  2445. north = Math.max(north, position.latitude);
  2446. var lonAdjusted =
  2447. position.longitude >= 0
  2448. ? position.longitude
  2449. : position.longitude + _Math.CesiumMath.TWO_PI;
  2450. westOverIDL = Math.min(westOverIDL, lonAdjusted);
  2451. eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  2452. }
  2453. if (east - west > eastOverIDL - westOverIDL) {
  2454. west = westOverIDL;
  2455. east = eastOverIDL;
  2456. if (east > _Math.CesiumMath.PI) {
  2457. east = east - _Math.CesiumMath.TWO_PI;
  2458. }
  2459. if (west > _Math.CesiumMath.PI) {
  2460. west = west - _Math.CesiumMath.TWO_PI;
  2461. }
  2462. }
  2463. if (!when.defined(result)) {
  2464. return new Rectangle(west, south, east, north);
  2465. }
  2466. result.west = west;
  2467. result.south = south;
  2468. result.east = east;
  2469. result.north = north;
  2470. return result;
  2471. };
  2472. /**
  2473. * Duplicates a Rectangle.
  2474. *
  2475. * @param {Rectangle} rectangle The rectangle to clone.
  2476. * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
  2477. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided. (Returns undefined if rectangle is undefined)
  2478. */
  2479. Rectangle.clone = function (rectangle, result) {
  2480. if (!when.defined(rectangle)) {
  2481. return undefined;
  2482. }
  2483. if (!when.defined(result)) {
  2484. return new Rectangle(
  2485. rectangle.west,
  2486. rectangle.south,
  2487. rectangle.east,
  2488. rectangle.north
  2489. );
  2490. }
  2491. result.west = rectangle.west;
  2492. result.south = rectangle.south;
  2493. result.east = rectangle.east;
  2494. result.north = rectangle.north;
  2495. return result;
  2496. };
  2497. /**
  2498. * Compares the provided Rectangles componentwise and returns
  2499. * <code>true</code> if they pass an absolute or relative tolerance test,
  2500. * <code>false</code> otherwise.
  2501. *
  2502. * @param {Rectangle} [left] The first Rectangle.
  2503. * @param {Rectangle} [right] The second Rectangle.
  2504. * @param {Number} [absoluteEpsilon=0] The absolute epsilon tolerance to use for equality testing.
  2505. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  2506. */
  2507. Rectangle.equalsEpsilon = function (left, right, absoluteEpsilon) {
  2508. absoluteEpsilon = when.defaultValue(absoluteEpsilon, 0);
  2509. return (
  2510. left === right ||
  2511. (when.defined(left) &&
  2512. when.defined(right) &&
  2513. Math.abs(left.west - right.west) <= absoluteEpsilon &&
  2514. Math.abs(left.south - right.south) <= absoluteEpsilon &&
  2515. Math.abs(left.east - right.east) <= absoluteEpsilon &&
  2516. Math.abs(left.north - right.north) <= absoluteEpsilon)
  2517. );
  2518. };
  2519. /**
  2520. * Duplicates this Rectangle.
  2521. *
  2522. * @param {Rectangle} [result] The object onto which to store the result.
  2523. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  2524. */
  2525. Rectangle.prototype.clone = function (result) {
  2526. return Rectangle.clone(this, result);
  2527. };
  2528. /**
  2529. * Compares the provided Rectangle with this Rectangle componentwise and returns
  2530. * <code>true</code> if they are equal, <code>false</code> otherwise.
  2531. *
  2532. * @param {Rectangle} [other] The Rectangle to compare.
  2533. * @returns {Boolean} <code>true</code> if the Rectangles are equal, <code>false</code> otherwise.
  2534. */
  2535. Rectangle.prototype.equals = function (other) {
  2536. return Rectangle.equals(this, other);
  2537. };
  2538. /**
  2539. * Compares the provided rectangles and returns <code>true</code> if they are equal,
  2540. * <code>false</code> otherwise.
  2541. *
  2542. * @param {Rectangle} [left] The first Rectangle.
  2543. * @param {Rectangle} [right] The second Rectangle.
  2544. * @returns {Boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
  2545. */
  2546. Rectangle.equals = function (left, right) {
  2547. return (
  2548. left === right ||
  2549. (when.defined(left) &&
  2550. when.defined(right) &&
  2551. left.west === right.west &&
  2552. left.south === right.south &&
  2553. left.east === right.east &&
  2554. left.north === right.north)
  2555. );
  2556. };
  2557. /**
  2558. * Compares the provided Rectangle with this Rectangle componentwise and returns
  2559. * <code>true</code> if they are within the provided epsilon,
  2560. * <code>false</code> otherwise.
  2561. *
  2562. * @param {Rectangle} [other] The Rectangle to compare.
  2563. * @param {Number} [epsilon=0] The epsilon to use for equality testing.
  2564. * @returns {Boolean} <code>true</code> if the Rectangles are within the provided epsilon, <code>false</code> otherwise.
  2565. */
  2566. Rectangle.prototype.equalsEpsilon = function (other, epsilon) {
  2567. return Rectangle.equalsEpsilon(this, other, epsilon);
  2568. };
  2569. /**
  2570. * Checks a Rectangle's properties and throws if they are not in valid ranges.
  2571. *
  2572. * @param {Rectangle} rectangle The rectangle to validate
  2573. *
  2574. * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  2575. * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  2576. * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  2577. * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  2578. */
  2579. Rectangle.validate = function (rectangle) {
  2580. //>>includeStart('debug', pragmas.debug);
  2581. Check.Check.typeOf.object("rectangle", rectangle);
  2582. var north = rectangle.north;
  2583. Check.Check.typeOf.number.greaterThanOrEquals(
  2584. "north",
  2585. north,
  2586. -_Math.CesiumMath.PI_OVER_TWO
  2587. );
  2588. Check.Check.typeOf.number.lessThanOrEquals("north", north, _Math.CesiumMath.PI_OVER_TWO);
  2589. var south = rectangle.south;
  2590. Check.Check.typeOf.number.greaterThanOrEquals(
  2591. "south",
  2592. south,
  2593. -_Math.CesiumMath.PI_OVER_TWO
  2594. );
  2595. Check.Check.typeOf.number.lessThanOrEquals("south", south, _Math.CesiumMath.PI_OVER_TWO);
  2596. var west = rectangle.west;
  2597. Check.Check.typeOf.number.greaterThanOrEquals("west", west, -Math.PI);
  2598. Check.Check.typeOf.number.lessThanOrEquals("west", west, Math.PI);
  2599. var east = rectangle.east;
  2600. Check.Check.typeOf.number.greaterThanOrEquals("east", east, -Math.PI);
  2601. Check.Check.typeOf.number.lessThanOrEquals("east", east, Math.PI);
  2602. //>>includeEnd('debug');
  2603. };
  2604. /**
  2605. * Computes the southwest corner of a rectangle.
  2606. *
  2607. * @param {Rectangle} rectangle The rectangle for which to find the corner
  2608. * @param {Cartographic} [result] The object onto which to store the result.
  2609. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  2610. */
  2611. Rectangle.southwest = function (rectangle, result) {
  2612. //>>includeStart('debug', pragmas.debug);
  2613. Check.Check.typeOf.object("rectangle", rectangle);
  2614. //>>includeEnd('debug');
  2615. if (!when.defined(result)) {
  2616. return new Cartographic(rectangle.west, rectangle.south);
  2617. }
  2618. result.longitude = rectangle.west;
  2619. result.latitude = rectangle.south;
  2620. result.height = 0.0;
  2621. return result;
  2622. };
  2623. /**
  2624. * Computes the northwest corner of a rectangle.
  2625. *
  2626. * @param {Rectangle} rectangle The rectangle for which to find the corner
  2627. * @param {Cartographic} [result] The object onto which to store the result.
  2628. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  2629. */
  2630. Rectangle.northwest = function (rectangle, result) {
  2631. //>>includeStart('debug', pragmas.debug);
  2632. Check.Check.typeOf.object("rectangle", rectangle);
  2633. //>>includeEnd('debug');
  2634. if (!when.defined(result)) {
  2635. return new Cartographic(rectangle.west, rectangle.north);
  2636. }
  2637. result.longitude = rectangle.west;
  2638. result.latitude = rectangle.north;
  2639. result.height = 0.0;
  2640. return result;
  2641. };
  2642. /**
  2643. * Computes the northeast corner of a rectangle.
  2644. *
  2645. * @param {Rectangle} rectangle The rectangle for which to find the corner
  2646. * @param {Cartographic} [result] The object onto which to store the result.
  2647. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  2648. */
  2649. Rectangle.northeast = function (rectangle, result) {
  2650. //>>includeStart('debug', pragmas.debug);
  2651. Check.Check.typeOf.object("rectangle", rectangle);
  2652. //>>includeEnd('debug');
  2653. if (!when.defined(result)) {
  2654. return new Cartographic(rectangle.east, rectangle.north);
  2655. }
  2656. result.longitude = rectangle.east;
  2657. result.latitude = rectangle.north;
  2658. result.height = 0.0;
  2659. return result;
  2660. };
  2661. /**
  2662. * Computes the southeast corner of a rectangle.
  2663. *
  2664. * @param {Rectangle} rectangle The rectangle for which to find the corner
  2665. * @param {Cartographic} [result] The object onto which to store the result.
  2666. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  2667. */
  2668. Rectangle.southeast = function (rectangle, result) {
  2669. //>>includeStart('debug', pragmas.debug);
  2670. Check.Check.typeOf.object("rectangle", rectangle);
  2671. //>>includeEnd('debug');
  2672. if (!when.defined(result)) {
  2673. return new Cartographic(rectangle.east, rectangle.south);
  2674. }
  2675. result.longitude = rectangle.east;
  2676. result.latitude = rectangle.south;
  2677. result.height = 0.0;
  2678. return result;
  2679. };
  2680. /**
  2681. * Computes the center of a rectangle.
  2682. *
  2683. * @param {Rectangle} rectangle The rectangle for which to find the center
  2684. * @param {Cartographic} [result] The object onto which to store the result.
  2685. * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
  2686. */
  2687. Rectangle.center = function (rectangle, result) {
  2688. //>>includeStart('debug', pragmas.debug);
  2689. Check.Check.typeOf.object("rectangle", rectangle);
  2690. //>>includeEnd('debug');
  2691. var east = rectangle.east;
  2692. var west = rectangle.west;
  2693. if (east < west) {
  2694. east += _Math.CesiumMath.TWO_PI;
  2695. }
  2696. var longitude = _Math.CesiumMath.negativePiToPi((west + east) * 0.5);
  2697. var latitude = (rectangle.south + rectangle.north) * 0.5;
  2698. if (!when.defined(result)) {
  2699. return new Cartographic(longitude, latitude);
  2700. }
  2701. result.longitude = longitude;
  2702. result.latitude = latitude;
  2703. result.height = 0.0;
  2704. return result;
  2705. };
  2706. /**
  2707. * Computes the intersection of two rectangles. This function assumes that the rectangle's coordinates are
  2708. * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
  2709. * the same angle can be represented with multiple values as well as the wrapping of longitude at the
  2710. * anti-meridian. For a simple intersection that ignores these factors and can be used with projected
  2711. * coordinates, see {@link Rectangle.simpleIntersection}.
  2712. *
  2713. * @param {Rectangle} rectangle On rectangle to find an intersection
  2714. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  2715. * @param {Rectangle} [result] The object onto which to store the result.
  2716. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  2717. */
  2718. Rectangle.intersection = function (rectangle, otherRectangle, result) {
  2719. //>>includeStart('debug', pragmas.debug);
  2720. Check.Check.typeOf.object("rectangle", rectangle);
  2721. Check.Check.typeOf.object("otherRectangle", otherRectangle);
  2722. //>>includeEnd('debug');
  2723. var rectangleEast = rectangle.east;
  2724. var rectangleWest = rectangle.west;
  2725. var otherRectangleEast = otherRectangle.east;
  2726. var otherRectangleWest = otherRectangle.west;
  2727. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  2728. rectangleEast += _Math.CesiumMath.TWO_PI;
  2729. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  2730. otherRectangleEast += _Math.CesiumMath.TWO_PI;
  2731. }
  2732. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  2733. otherRectangleWest += _Math.CesiumMath.TWO_PI;
  2734. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  2735. rectangleWest += _Math.CesiumMath.TWO_PI;
  2736. }
  2737. var west = _Math.CesiumMath.negativePiToPi(
  2738. Math.max(rectangleWest, otherRectangleWest)
  2739. );
  2740. var east = _Math.CesiumMath.negativePiToPi(
  2741. Math.min(rectangleEast, otherRectangleEast)
  2742. );
  2743. if (
  2744. (rectangle.west < rectangle.east ||
  2745. otherRectangle.west < otherRectangle.east) &&
  2746. east <= west
  2747. ) {
  2748. return undefined;
  2749. }
  2750. var south = Math.max(rectangle.south, otherRectangle.south);
  2751. var north = Math.min(rectangle.north, otherRectangle.north);
  2752. if (south >= north) {
  2753. return undefined;
  2754. }
  2755. if (!when.defined(result)) {
  2756. return new Rectangle(west, south, east, north);
  2757. }
  2758. result.west = west;
  2759. result.south = south;
  2760. result.east = east;
  2761. result.north = north;
  2762. return result;
  2763. };
  2764. /**
  2765. * Computes a simple intersection of two rectangles. Unlike {@link Rectangle.intersection}, this function
  2766. * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
  2767. * anti-meridian. As such, it can be used for rectangles where the coordinates are not simply latitude
  2768. * and longitude (i.e. projected coordinates).
  2769. *
  2770. * @param {Rectangle} rectangle On rectangle to find an intersection
  2771. * @param {Rectangle} otherRectangle Another rectangle to find an intersection
  2772. * @param {Rectangle} [result] The object onto which to store the result.
  2773. * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
  2774. */
  2775. Rectangle.simpleIntersection = function (rectangle, otherRectangle, result) {
  2776. //>>includeStart('debug', pragmas.debug);
  2777. Check.Check.typeOf.object("rectangle", rectangle);
  2778. Check.Check.typeOf.object("otherRectangle", otherRectangle);
  2779. //>>includeEnd('debug');
  2780. var west = Math.max(rectangle.west, otherRectangle.west);
  2781. var south = Math.max(rectangle.south, otherRectangle.south);
  2782. var east = Math.min(rectangle.east, otherRectangle.east);
  2783. var north = Math.min(rectangle.north, otherRectangle.north);
  2784. if (south >= north || west >= east) {
  2785. return undefined;
  2786. }
  2787. if (!when.defined(result)) {
  2788. return new Rectangle(west, south, east, north);
  2789. }
  2790. result.west = west;
  2791. result.south = south;
  2792. result.east = east;
  2793. result.north = north;
  2794. return result;
  2795. };
  2796. /**
  2797. * Computes a rectangle that is the union of two rectangles.
  2798. *
  2799. * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
  2800. * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
  2801. * @param {Rectangle} [result] The object onto which to store the result.
  2802. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
  2803. */
  2804. Rectangle.union = function (rectangle, otherRectangle, result) {
  2805. //>>includeStart('debug', pragmas.debug);
  2806. Check.Check.typeOf.object("rectangle", rectangle);
  2807. Check.Check.typeOf.object("otherRectangle", otherRectangle);
  2808. //>>includeEnd('debug');
  2809. if (!when.defined(result)) {
  2810. result = new Rectangle();
  2811. }
  2812. var rectangleEast = rectangle.east;
  2813. var rectangleWest = rectangle.west;
  2814. var otherRectangleEast = otherRectangle.east;
  2815. var otherRectangleWest = otherRectangle.west;
  2816. if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
  2817. rectangleEast += _Math.CesiumMath.TWO_PI;
  2818. } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
  2819. otherRectangleEast += _Math.CesiumMath.TWO_PI;
  2820. }
  2821. if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
  2822. otherRectangleWest += _Math.CesiumMath.TWO_PI;
  2823. } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
  2824. rectangleWest += _Math.CesiumMath.TWO_PI;
  2825. }
  2826. var west = _Math.CesiumMath.convertLongitudeRange(
  2827. Math.min(rectangleWest, otherRectangleWest)
  2828. );
  2829. var east = _Math.CesiumMath.convertLongitudeRange(
  2830. Math.max(rectangleEast, otherRectangleEast)
  2831. );
  2832. result.west = west;
  2833. result.south = Math.min(rectangle.south, otherRectangle.south);
  2834. result.east = east;
  2835. result.north = Math.max(rectangle.north, otherRectangle.north);
  2836. return result;
  2837. };
  2838. /**
  2839. * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
  2840. *
  2841. * @param {Rectangle} rectangle A rectangle to expand.
  2842. * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
  2843. * @param {Rectangle} [result] The object onto which to store the result.
  2844. * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
  2845. */
  2846. Rectangle.expand = function (rectangle, cartographic, result) {
  2847. //>>includeStart('debug', pragmas.debug);
  2848. Check.Check.typeOf.object("rectangle", rectangle);
  2849. Check.Check.typeOf.object("cartographic", cartographic);
  2850. //>>includeEnd('debug');
  2851. if (!when.defined(result)) {
  2852. result = new Rectangle();
  2853. }
  2854. result.west = Math.min(rectangle.west, cartographic.longitude);
  2855. result.south = Math.min(rectangle.south, cartographic.latitude);
  2856. result.east = Math.max(rectangle.east, cartographic.longitude);
  2857. result.north = Math.max(rectangle.north, cartographic.latitude);
  2858. return result;
  2859. };
  2860. /**
  2861. * Returns true if the cartographic is on or inside the rectangle, false otherwise.
  2862. *
  2863. * @param {Rectangle} rectangle The rectangle
  2864. * @param {Cartographic} cartographic The cartographic to test.
  2865. * @returns {Boolean} true if the provided cartographic is inside the rectangle, false otherwise.
  2866. */
  2867. Rectangle.contains = function (rectangle, cartographic) {
  2868. //>>includeStart('debug', pragmas.debug);
  2869. Check.Check.typeOf.object("rectangle", rectangle);
  2870. Check.Check.typeOf.object("cartographic", cartographic);
  2871. //>>includeEnd('debug');
  2872. var longitude = cartographic.longitude;
  2873. var latitude = cartographic.latitude;
  2874. var west = rectangle.west;
  2875. var east = rectangle.east;
  2876. if (east < west) {
  2877. east += _Math.CesiumMath.TWO_PI;
  2878. if (longitude < 0.0) {
  2879. longitude += _Math.CesiumMath.TWO_PI;
  2880. }
  2881. }
  2882. return (
  2883. (longitude > west ||
  2884. _Math.CesiumMath.equalsEpsilon(longitude, west, _Math.CesiumMath.EPSILON14)) &&
  2885. (longitude < east ||
  2886. _Math.CesiumMath.equalsEpsilon(longitude, east, _Math.CesiumMath.EPSILON14)) &&
  2887. latitude >= rectangle.south &&
  2888. latitude <= rectangle.north
  2889. );
  2890. };
  2891. var subsampleLlaScratch = new Cartographic();
  2892. /**
  2893. * Samples a rectangle so that it includes a list of Cartesian points suitable for passing to
  2894. * {@link BoundingSphere#fromPoints}. Sampling is necessary to account
  2895. * for rectangles that cover the poles or cross the equator.
  2896. *
  2897. * @param {Rectangle} rectangle The rectangle to subsample.
  2898. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid to use.
  2899. * @param {Number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
  2900. * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
  2901. * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
  2902. */
  2903. Rectangle.subsample = function (rectangle, ellipsoid, surfaceHeight, result) {
  2904. //>>includeStart('debug', pragmas.debug);
  2905. Check.Check.typeOf.object("rectangle", rectangle);
  2906. //>>includeEnd('debug');
  2907. ellipsoid = when.defaultValue(ellipsoid, Ellipsoid.WGS84);
  2908. surfaceHeight = when.defaultValue(surfaceHeight, 0.0);
  2909. if (!when.defined(result)) {
  2910. result = [];
  2911. }
  2912. var length = 0;
  2913. var north = rectangle.north;
  2914. var south = rectangle.south;
  2915. var east = rectangle.east;
  2916. var west = rectangle.west;
  2917. var lla = subsampleLlaScratch;
  2918. lla.height = surfaceHeight;
  2919. lla.longitude = west;
  2920. lla.latitude = north;
  2921. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  2922. length++;
  2923. lla.longitude = east;
  2924. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  2925. length++;
  2926. lla.latitude = south;
  2927. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  2928. length++;
  2929. lla.longitude = west;
  2930. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  2931. length++;
  2932. if (north < 0.0) {
  2933. lla.latitude = north;
  2934. } else if (south > 0.0) {
  2935. lla.latitude = south;
  2936. } else {
  2937. lla.latitude = 0.0;
  2938. }
  2939. for (var i = 1; i < 8; ++i) {
  2940. lla.longitude = -Math.PI + i * _Math.CesiumMath.PI_OVER_TWO;
  2941. if (Rectangle.contains(rectangle, lla)) {
  2942. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  2943. length++;
  2944. }
  2945. }
  2946. if (lla.latitude === 0.0) {
  2947. lla.longitude = west;
  2948. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  2949. length++;
  2950. lla.longitude = east;
  2951. result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  2952. length++;
  2953. }
  2954. result.length = length;
  2955. return result;
  2956. };
  2957. /**
  2958. * The largest possible rectangle.
  2959. *
  2960. * @type {Rectangle}
  2961. * @constant
  2962. */
  2963. Rectangle.MAX_VALUE = Object.freeze(
  2964. new Rectangle(
  2965. -Math.PI,
  2966. -_Math.CesiumMath.PI_OVER_TWO,
  2967. Math.PI,
  2968. _Math.CesiumMath.PI_OVER_TWO
  2969. )
  2970. );
  2971. /**
  2972. * A 2D Cartesian point.
  2973. * @alias Cartesian2
  2974. * @constructor
  2975. *
  2976. * @param {Number} [x=0.0] The X component.
  2977. * @param {Number} [y=0.0] The Y component.
  2978. *
  2979. * @see Cartesian3
  2980. * @see Cartesian4
  2981. * @see Packable
  2982. */
  2983. function Cartesian2(x, y) {
  2984. /**
  2985. * The X component.
  2986. * @type {Number}
  2987. * @default 0.0
  2988. */
  2989. this.x = when.defaultValue(x, 0.0);
  2990. /**
  2991. * The Y component.
  2992. * @type {Number}
  2993. * @default 0.0
  2994. */
  2995. this.y = when.defaultValue(y, 0.0);
  2996. }
  2997. /**
  2998. * Creates a Cartesian2 instance from x and y coordinates.
  2999. *
  3000. * @param {Number} x The x coordinate.
  3001. * @param {Number} y The y coordinate.
  3002. * @param {Cartesian2} [result] The object onto which to store the result.
  3003. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  3004. */
  3005. Cartesian2.fromElements = function (x, y, result) {
  3006. if (!when.defined(result)) {
  3007. return new Cartesian2(x, y);
  3008. }
  3009. result.x = x;
  3010. result.y = y;
  3011. return result;
  3012. };
  3013. /**
  3014. * Duplicates a Cartesian2 instance.
  3015. *
  3016. * @param {Cartesian2} cartesian The Cartesian to duplicate.
  3017. * @param {Cartesian2} [result] The object onto which to store the result.
  3018. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. (Returns undefined if cartesian is undefined)
  3019. */
  3020. Cartesian2.clone = function (cartesian, result) {
  3021. if (!when.defined(cartesian)) {
  3022. return undefined;
  3023. }
  3024. if (!when.defined(result)) {
  3025. return new Cartesian2(cartesian.x, cartesian.y);
  3026. }
  3027. result.x = cartesian.x;
  3028. result.y = cartesian.y;
  3029. return result;
  3030. };
  3031. /**
  3032. * Creates a Cartesian2 instance from an existing Cartesian3. This simply takes the
  3033. * x and y properties of the Cartesian3 and drops z.
  3034. * @function
  3035. *
  3036. * @param {Cartesian3} cartesian The Cartesian3 instance to create a Cartesian2 instance from.
  3037. * @param {Cartesian2} [result] The object onto which to store the result.
  3038. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  3039. */
  3040. Cartesian2.fromCartesian3 = Cartesian2.clone;
  3041. /**
  3042. * Creates a Cartesian2 instance from an existing Cartesian4. This simply takes the
  3043. * x and y properties of the Cartesian4 and drops z and w.
  3044. * @function
  3045. *
  3046. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian2 instance from.
  3047. * @param {Cartesian2} [result] The object onto which to store the result.
  3048. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  3049. */
  3050. Cartesian2.fromCartesian4 = Cartesian2.clone;
  3051. /**
  3052. * The number of elements used to pack the object into an array.
  3053. * @type {Number}
  3054. */
  3055. Cartesian2.packedLength = 2;
  3056. /**
  3057. * Stores the provided instance into the provided array.
  3058. *
  3059. * @param {Cartesian2} value The value to pack.
  3060. * @param {Number[]} array The array to pack into.
  3061. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  3062. *
  3063. * @returns {Number[]} The array that was packed into
  3064. */
  3065. Cartesian2.pack = function (value, array, startingIndex) {
  3066. //>>includeStart('debug', pragmas.debug);
  3067. Check.Check.typeOf.object("value", value);
  3068. Check.Check.defined("array", array);
  3069. //>>includeEnd('debug');
  3070. startingIndex = when.defaultValue(startingIndex, 0);
  3071. array[startingIndex++] = value.x;
  3072. array[startingIndex] = value.y;
  3073. return array;
  3074. };
  3075. /**
  3076. * Retrieves an instance from a packed array.
  3077. *
  3078. * @param {Number[]} array The packed array.
  3079. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  3080. * @param {Cartesian2} [result] The object into which to store the result.
  3081. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  3082. */
  3083. Cartesian2.unpack = function (array, startingIndex, result) {
  3084. //>>includeStart('debug', pragmas.debug);
  3085. Check.Check.defined("array", array);
  3086. //>>includeEnd('debug');
  3087. startingIndex = when.defaultValue(startingIndex, 0);
  3088. if (!when.defined(result)) {
  3089. result = new Cartesian2();
  3090. }
  3091. result.x = array[startingIndex++];
  3092. result.y = array[startingIndex];
  3093. return result;
  3094. };
  3095. /**
  3096. * Flattens an array of Cartesian2s into and array of components.
  3097. *
  3098. * @param {Cartesian2[]} array The array of cartesians to pack.
  3099. * @param {Number[]} [result] The array onto which to store the result. If this is a typed array, it must have array.length * 2 components, else a {@link DeveloperError} will be thrown. If it is a regular array, it will be resized to have (array.length * 2) elements.
  3100. * @returns {Number[]} The packed array.
  3101. */
  3102. Cartesian2.packArray = function (array, result) {
  3103. //>>includeStart('debug', pragmas.debug);
  3104. Check.Check.defined("array", array);
  3105. //>>includeEnd('debug');
  3106. var length = array.length;
  3107. var resultLength = length * 2;
  3108. if (!when.defined(result)) {
  3109. result = new Array(resultLength);
  3110. } else if (!Array.isArray(result) && result.length !== resultLength) {
  3111. throw new Check.DeveloperError(
  3112. "If result is a typed array, it must have exactly array.length * 2 elements"
  3113. );
  3114. } else if (result.length !== resultLength) {
  3115. result.length = resultLength;
  3116. }
  3117. for (var i = 0; i < length; ++i) {
  3118. Cartesian2.pack(array[i], result, i * 2);
  3119. }
  3120. return result;
  3121. };
  3122. /**
  3123. * Unpacks an array of cartesian components into and array of Cartesian2s.
  3124. *
  3125. * @param {Number[]} array The array of components to unpack.
  3126. * @param {Cartesian2[]} [result] The array onto which to store the result.
  3127. * @returns {Cartesian2[]} The unpacked array.
  3128. */
  3129. Cartesian2.unpackArray = function (array, result) {
  3130. //>>includeStart('debug', pragmas.debug);
  3131. Check.Check.defined("array", array);
  3132. Check.Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 2);
  3133. if (array.length % 2 !== 0) {
  3134. throw new Check.DeveloperError("array length must be a multiple of 2.");
  3135. }
  3136. //>>includeEnd('debug');
  3137. var length = array.length;
  3138. if (!when.defined(result)) {
  3139. result = new Array(length / 2);
  3140. } else {
  3141. result.length = length / 2;
  3142. }
  3143. for (var i = 0; i < length; i += 2) {
  3144. var index = i / 2;
  3145. result[index] = Cartesian2.unpack(array, i, result[index]);
  3146. }
  3147. return result;
  3148. };
  3149. /**
  3150. * Creates a Cartesian2 from two consecutive elements in an array.
  3151. * @function
  3152. *
  3153. * @param {Number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  3154. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  3155. * @param {Cartesian2} [result] The object onto which to store the result.
  3156. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  3157. *
  3158. * @example
  3159. * // Create a Cartesian2 with (1.0, 2.0)
  3160. * var v = [1.0, 2.0];
  3161. * var p = Cesium.Cartesian2.fromArray(v);
  3162. *
  3163. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  3164. * var v2 = [0.0, 0.0, 1.0, 2.0];
  3165. * var p2 = Cesium.Cartesian2.fromArray(v2, 2);
  3166. */
  3167. Cartesian2.fromArray = Cartesian2.unpack;
  3168. /**
  3169. * Computes the value of the maximum component for the supplied Cartesian.
  3170. *
  3171. * @param {Cartesian2} cartesian The cartesian to use.
  3172. * @returns {Number} The value of the maximum component.
  3173. */
  3174. Cartesian2.maximumComponent = function (cartesian) {
  3175. //>>includeStart('debug', pragmas.debug);
  3176. Check.Check.typeOf.object("cartesian", cartesian);
  3177. //>>includeEnd('debug');
  3178. return Math.max(cartesian.x, cartesian.y);
  3179. };
  3180. /**
  3181. * Computes the value of the minimum component for the supplied Cartesian.
  3182. *
  3183. * @param {Cartesian2} cartesian The cartesian to use.
  3184. * @returns {Number} The value of the minimum component.
  3185. */
  3186. Cartesian2.minimumComponent = function (cartesian) {
  3187. //>>includeStart('debug', pragmas.debug);
  3188. Check.Check.typeOf.object("cartesian", cartesian);
  3189. //>>includeEnd('debug');
  3190. return Math.min(cartesian.x, cartesian.y);
  3191. };
  3192. /**
  3193. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  3194. *
  3195. * @param {Cartesian2} first A cartesian to compare.
  3196. * @param {Cartesian2} second A cartesian to compare.
  3197. * @param {Cartesian2} result The object into which to store the result.
  3198. * @returns {Cartesian2} A cartesian with the minimum components.
  3199. */
  3200. Cartesian2.minimumByComponent = function (first, second, result) {
  3201. //>>includeStart('debug', pragmas.debug);
  3202. Check.Check.typeOf.object("first", first);
  3203. Check.Check.typeOf.object("second", second);
  3204. Check.Check.typeOf.object("result", result);
  3205. //>>includeEnd('debug');
  3206. result.x = Math.min(first.x, second.x);
  3207. result.y = Math.min(first.y, second.y);
  3208. return result;
  3209. };
  3210. /**
  3211. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  3212. *
  3213. * @param {Cartesian2} first A cartesian to compare.
  3214. * @param {Cartesian2} second A cartesian to compare.
  3215. * @param {Cartesian2} result The object into which to store the result.
  3216. * @returns {Cartesian2} A cartesian with the maximum components.
  3217. */
  3218. Cartesian2.maximumByComponent = function (first, second, result) {
  3219. //>>includeStart('debug', pragmas.debug);
  3220. Check.Check.typeOf.object("first", first);
  3221. Check.Check.typeOf.object("second", second);
  3222. Check.Check.typeOf.object("result", result);
  3223. //>>includeEnd('debug');
  3224. result.x = Math.max(first.x, second.x);
  3225. result.y = Math.max(first.y, second.y);
  3226. return result;
  3227. };
  3228. /**
  3229. * Computes the provided Cartesian's squared magnitude.
  3230. *
  3231. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  3232. * @returns {Number} The squared magnitude.
  3233. */
  3234. Cartesian2.magnitudeSquared = function (cartesian) {
  3235. //>>includeStart('debug', pragmas.debug);
  3236. Check.Check.typeOf.object("cartesian", cartesian);
  3237. //>>includeEnd('debug');
  3238. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  3239. };
  3240. /**
  3241. * Computes the Cartesian's magnitude (length).
  3242. *
  3243. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  3244. * @returns {Number} The magnitude.
  3245. */
  3246. Cartesian2.magnitude = function (cartesian) {
  3247. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  3248. };
  3249. var distanceScratch$1 = new Cartesian2();
  3250. /**
  3251. * Computes the distance between two points.
  3252. *
  3253. * @param {Cartesian2} left The first point to compute the distance from.
  3254. * @param {Cartesian2} right The second point to compute the distance to.
  3255. * @returns {Number} The distance between two points.
  3256. *
  3257. * @example
  3258. * // Returns 1.0
  3259. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  3260. */
  3261. Cartesian2.distance = function (left, right) {
  3262. //>>includeStart('debug', pragmas.debug);
  3263. Check.Check.typeOf.object("left", left);
  3264. Check.Check.typeOf.object("right", right);
  3265. //>>includeEnd('debug');
  3266. Cartesian2.subtract(left, right, distanceScratch$1);
  3267. return Cartesian2.magnitude(distanceScratch$1);
  3268. };
  3269. /**
  3270. * Computes the squared distance between two points. Comparing squared distances
  3271. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  3272. *
  3273. * @param {Cartesian2} left The first point to compute the distance from.
  3274. * @param {Cartesian2} right The second point to compute the distance to.
  3275. * @returns {Number} The distance between two points.
  3276. *
  3277. * @example
  3278. * // Returns 4.0, not 2.0
  3279. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  3280. */
  3281. Cartesian2.distanceSquared = function (left, right) {
  3282. //>>includeStart('debug', pragmas.debug);
  3283. Check.Check.typeOf.object("left", left);
  3284. Check.Check.typeOf.object("right", right);
  3285. //>>includeEnd('debug');
  3286. Cartesian2.subtract(left, right, distanceScratch$1);
  3287. return Cartesian2.magnitudeSquared(distanceScratch$1);
  3288. };
  3289. /**
  3290. * Computes the normalized form of the supplied Cartesian.
  3291. *
  3292. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  3293. * @param {Cartesian2} result The object onto which to store the result.
  3294. * @returns {Cartesian2} The modified result parameter.
  3295. */
  3296. Cartesian2.normalize = function (cartesian, result) {
  3297. //>>includeStart('debug', pragmas.debug);
  3298. Check.Check.typeOf.object("cartesian", cartesian);
  3299. Check.Check.typeOf.object("result", result);
  3300. //>>includeEnd('debug');
  3301. var magnitude = Cartesian2.magnitude(cartesian);
  3302. result.x = cartesian.x / magnitude;
  3303. result.y = cartesian.y / magnitude;
  3304. //>>includeStart('debug', pragmas.debug);
  3305. if (isNaN(result.x) || isNaN(result.y)) {
  3306. throw new Check.DeveloperError("normalized result is not a number");
  3307. }
  3308. //>>includeEnd('debug');
  3309. return result;
  3310. };
  3311. /**
  3312. * Computes the dot (scalar) product of two Cartesians.
  3313. *
  3314. * @param {Cartesian2} left The first Cartesian.
  3315. * @param {Cartesian2} right The second Cartesian.
  3316. * @returns {Number} The dot product.
  3317. */
  3318. Cartesian2.dot = function (left, right) {
  3319. //>>includeStart('debug', pragmas.debug);
  3320. Check.Check.typeOf.object("left", left);
  3321. Check.Check.typeOf.object("right", right);
  3322. //>>includeEnd('debug');
  3323. return left.x * right.x + left.y * right.y;
  3324. };
  3325. /**
  3326. * Computes the componentwise product of two Cartesians.
  3327. *
  3328. * @param {Cartesian2} left The first Cartesian.
  3329. * @param {Cartesian2} right The second Cartesian.
  3330. * @param {Cartesian2} result The object onto which to store the result.
  3331. * @returns {Cartesian2} The modified result parameter.
  3332. */
  3333. Cartesian2.multiplyComponents = function (left, right, result) {
  3334. //>>includeStart('debug', pragmas.debug);
  3335. Check.Check.typeOf.object("left", left);
  3336. Check.Check.typeOf.object("right", right);
  3337. Check.Check.typeOf.object("result", result);
  3338. //>>includeEnd('debug');
  3339. result.x = left.x * right.x;
  3340. result.y = left.y * right.y;
  3341. return result;
  3342. };
  3343. /**
  3344. * Computes the componentwise quotient of two Cartesians.
  3345. *
  3346. * @param {Cartesian2} left The first Cartesian.
  3347. * @param {Cartesian2} right The second Cartesian.
  3348. * @param {Cartesian2} result The object onto which to store the result.
  3349. * @returns {Cartesian2} The modified result parameter.
  3350. */
  3351. Cartesian2.divideComponents = function (left, right, result) {
  3352. //>>includeStart('debug', pragmas.debug);
  3353. Check.Check.typeOf.object("left", left);
  3354. Check.Check.typeOf.object("right", right);
  3355. Check.Check.typeOf.object("result", result);
  3356. //>>includeEnd('debug');
  3357. result.x = left.x / right.x;
  3358. result.y = left.y / right.y;
  3359. return result;
  3360. };
  3361. /**
  3362. * Computes the componentwise sum of two Cartesians.
  3363. *
  3364. * @param {Cartesian2} left The first Cartesian.
  3365. * @param {Cartesian2} right The second Cartesian.
  3366. * @param {Cartesian2} result The object onto which to store the result.
  3367. * @returns {Cartesian2} The modified result parameter.
  3368. */
  3369. Cartesian2.add = function (left, right, result) {
  3370. //>>includeStart('debug', pragmas.debug);
  3371. Check.Check.typeOf.object("left", left);
  3372. Check.Check.typeOf.object("right", right);
  3373. Check.Check.typeOf.object("result", result);
  3374. //>>includeEnd('debug');
  3375. result.x = left.x + right.x;
  3376. result.y = left.y + right.y;
  3377. return result;
  3378. };
  3379. /**
  3380. * Computes the componentwise difference of two Cartesians.
  3381. *
  3382. * @param {Cartesian2} left The first Cartesian.
  3383. * @param {Cartesian2} right The second Cartesian.
  3384. * @param {Cartesian2} result The object onto which to store the result.
  3385. * @returns {Cartesian2} The modified result parameter.
  3386. */
  3387. Cartesian2.subtract = function (left, right, result) {
  3388. //>>includeStart('debug', pragmas.debug);
  3389. Check.Check.typeOf.object("left", left);
  3390. Check.Check.typeOf.object("right", right);
  3391. Check.Check.typeOf.object("result", result);
  3392. //>>includeEnd('debug');
  3393. result.x = left.x - right.x;
  3394. result.y = left.y - right.y;
  3395. return result;
  3396. };
  3397. /**
  3398. * Multiplies the provided Cartesian componentwise by the provided scalar.
  3399. *
  3400. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  3401. * @param {Number} scalar The scalar to multiply with.
  3402. * @param {Cartesian2} result The object onto which to store the result.
  3403. * @returns {Cartesian2} The modified result parameter.
  3404. */
  3405. Cartesian2.multiplyByScalar = function (cartesian, scalar, result) {
  3406. //>>includeStart('debug', pragmas.debug);
  3407. Check.Check.typeOf.object("cartesian", cartesian);
  3408. Check.Check.typeOf.number("scalar", scalar);
  3409. Check.Check.typeOf.object("result", result);
  3410. //>>includeEnd('debug');
  3411. result.x = cartesian.x * scalar;
  3412. result.y = cartesian.y * scalar;
  3413. return result;
  3414. };
  3415. /**
  3416. * Divides the provided Cartesian componentwise by the provided scalar.
  3417. *
  3418. * @param {Cartesian2} cartesian The Cartesian to be divided.
  3419. * @param {Number} scalar The scalar to divide by.
  3420. * @param {Cartesian2} result The object onto which to store the result.
  3421. * @returns {Cartesian2} The modified result parameter.
  3422. */
  3423. Cartesian2.divideByScalar = function (cartesian, scalar, result) {
  3424. //>>includeStart('debug', pragmas.debug);
  3425. Check.Check.typeOf.object("cartesian", cartesian);
  3426. Check.Check.typeOf.number("scalar", scalar);
  3427. Check.Check.typeOf.object("result", result);
  3428. //>>includeEnd('debug');
  3429. result.x = cartesian.x / scalar;
  3430. result.y = cartesian.y / scalar;
  3431. return result;
  3432. };
  3433. /**
  3434. * Negates the provided Cartesian.
  3435. *
  3436. * @param {Cartesian2} cartesian The Cartesian to be negated.
  3437. * @param {Cartesian2} result The object onto which to store the result.
  3438. * @returns {Cartesian2} The modified result parameter.
  3439. */
  3440. Cartesian2.negate = function (cartesian, result) {
  3441. //>>includeStart('debug', pragmas.debug);
  3442. Check.Check.typeOf.object("cartesian", cartesian);
  3443. Check.Check.typeOf.object("result", result);
  3444. //>>includeEnd('debug');
  3445. result.x = -cartesian.x;
  3446. result.y = -cartesian.y;
  3447. return result;
  3448. };
  3449. /**
  3450. * Computes the absolute value of the provided Cartesian.
  3451. *
  3452. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  3453. * @param {Cartesian2} result The object onto which to store the result.
  3454. * @returns {Cartesian2} The modified result parameter.
  3455. */
  3456. Cartesian2.abs = function (cartesian, result) {
  3457. //>>includeStart('debug', pragmas.debug);
  3458. Check.Check.typeOf.object("cartesian", cartesian);
  3459. Check.Check.typeOf.object("result", result);
  3460. //>>includeEnd('debug');
  3461. result.x = Math.abs(cartesian.x);
  3462. result.y = Math.abs(cartesian.y);
  3463. return result;
  3464. };
  3465. var lerpScratch$1 = new Cartesian2();
  3466. /**
  3467. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  3468. *
  3469. * @param {Cartesian2} start The value corresponding to t at 0.0.
  3470. * @param {Cartesian2} end The value corresponding to t at 1.0.
  3471. * @param {Number} t The point along t at which to interpolate.
  3472. * @param {Cartesian2} result The object onto which to store the result.
  3473. * @returns {Cartesian2} The modified result parameter.
  3474. */
  3475. Cartesian2.lerp = function (start, end, t, result) {
  3476. //>>includeStart('debug', pragmas.debug);
  3477. Check.Check.typeOf.object("start", start);
  3478. Check.Check.typeOf.object("end", end);
  3479. Check.Check.typeOf.number("t", t);
  3480. Check.Check.typeOf.object("result", result);
  3481. //>>includeEnd('debug');
  3482. Cartesian2.multiplyByScalar(end, t, lerpScratch$1);
  3483. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  3484. return Cartesian2.add(lerpScratch$1, result, result);
  3485. };
  3486. var angleBetweenScratch$1 = new Cartesian2();
  3487. var angleBetweenScratch2$1 = new Cartesian2();
  3488. /**
  3489. * Returns the angle, in radians, between the provided Cartesians.
  3490. *
  3491. * @param {Cartesian2} left The first Cartesian.
  3492. * @param {Cartesian2} right The second Cartesian.
  3493. * @returns {Number} The angle between the Cartesians.
  3494. */
  3495. Cartesian2.angleBetween = function (left, right) {
  3496. //>>includeStart('debug', pragmas.debug);
  3497. Check.Check.typeOf.object("left", left);
  3498. Check.Check.typeOf.object("right", right);
  3499. //>>includeEnd('debug');
  3500. Cartesian2.normalize(left, angleBetweenScratch$1);
  3501. Cartesian2.normalize(right, angleBetweenScratch2$1);
  3502. return _Math.CesiumMath.acosClamped(
  3503. Cartesian2.dot(angleBetweenScratch$1, angleBetweenScratch2$1)
  3504. );
  3505. };
  3506. var mostOrthogonalAxisScratch$1 = new Cartesian2();
  3507. /**
  3508. * Returns the axis that is most orthogonal to the provided Cartesian.
  3509. *
  3510. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  3511. * @param {Cartesian2} result The object onto which to store the result.
  3512. * @returns {Cartesian2} The most orthogonal axis.
  3513. */
  3514. Cartesian2.mostOrthogonalAxis = function (cartesian, result) {
  3515. //>>includeStart('debug', pragmas.debug);
  3516. Check.Check.typeOf.object("cartesian", cartesian);
  3517. Check.Check.typeOf.object("result", result);
  3518. //>>includeEnd('debug');
  3519. var f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch$1);
  3520. Cartesian2.abs(f, f);
  3521. if (f.x <= f.y) {
  3522. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  3523. } else {
  3524. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  3525. }
  3526. return result;
  3527. };
  3528. /**
  3529. * Compares the provided Cartesians componentwise and returns
  3530. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3531. *
  3532. * @param {Cartesian2} [left] The first Cartesian.
  3533. * @param {Cartesian2} [right] The second Cartesian.
  3534. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  3535. */
  3536. Cartesian2.equals = function (left, right) {
  3537. return (
  3538. left === right ||
  3539. (when.defined(left) &&
  3540. when.defined(right) &&
  3541. left.x === right.x &&
  3542. left.y === right.y)
  3543. );
  3544. };
  3545. /**
  3546. * @private
  3547. */
  3548. Cartesian2.equalsArray = function (cartesian, array, offset) {
  3549. return cartesian.x === array[offset] && cartesian.y === array[offset + 1];
  3550. };
  3551. /**
  3552. * Compares the provided Cartesians componentwise and returns
  3553. * <code>true</code> if they pass an absolute or relative tolerance test,
  3554. * <code>false</code> otherwise.
  3555. *
  3556. * @param {Cartesian2} [left] The first Cartesian.
  3557. * @param {Cartesian2} [right] The second Cartesian.
  3558. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  3559. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  3560. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  3561. */
  3562. Cartesian2.equalsEpsilon = function (
  3563. left,
  3564. right,
  3565. relativeEpsilon,
  3566. absoluteEpsilon
  3567. ) {
  3568. return (
  3569. left === right ||
  3570. (when.defined(left) &&
  3571. when.defined(right) &&
  3572. _Math.CesiumMath.equalsEpsilon(
  3573. left.x,
  3574. right.x,
  3575. relativeEpsilon,
  3576. absoluteEpsilon
  3577. ) &&
  3578. _Math.CesiumMath.equalsEpsilon(
  3579. left.y,
  3580. right.y,
  3581. relativeEpsilon,
  3582. absoluteEpsilon
  3583. ))
  3584. );
  3585. };
  3586. /**
  3587. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  3588. *
  3589. * @type {Cartesian2}
  3590. * @constant
  3591. */
  3592. Cartesian2.ZERO = Object.freeze(new Cartesian2(0.0, 0.0));
  3593. /**
  3594. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  3595. *
  3596. * @type {Cartesian2}
  3597. * @constant
  3598. */
  3599. Cartesian2.UNIT_X = Object.freeze(new Cartesian2(1.0, 0.0));
  3600. /**
  3601. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  3602. *
  3603. * @type {Cartesian2}
  3604. * @constant
  3605. */
  3606. Cartesian2.UNIT_Y = Object.freeze(new Cartesian2(0.0, 1.0));
  3607. /**
  3608. * Duplicates this Cartesian2 instance.
  3609. *
  3610. * @param {Cartesian2} [result] The object onto which to store the result.
  3611. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  3612. */
  3613. Cartesian2.prototype.clone = function (result) {
  3614. return Cartesian2.clone(this, result);
  3615. };
  3616. /**
  3617. * Compares this Cartesian against the provided Cartesian componentwise and returns
  3618. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3619. *
  3620. * @param {Cartesian2} [right] The right hand side Cartesian.
  3621. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  3622. */
  3623. Cartesian2.prototype.equals = function (right) {
  3624. return Cartesian2.equals(this, right);
  3625. };
  3626. /**
  3627. * Compares this Cartesian against the provided Cartesian componentwise and returns
  3628. * <code>true</code> if they pass an absolute or relative tolerance test,
  3629. * <code>false</code> otherwise.
  3630. *
  3631. * @param {Cartesian2} [right] The right hand side Cartesian.
  3632. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  3633. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  3634. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  3635. */
  3636. Cartesian2.prototype.equalsEpsilon = function (
  3637. right,
  3638. relativeEpsilon,
  3639. absoluteEpsilon
  3640. ) {
  3641. return Cartesian2.equalsEpsilon(
  3642. this,
  3643. right,
  3644. relativeEpsilon,
  3645. absoluteEpsilon
  3646. );
  3647. };
  3648. /**
  3649. * Creates a string representing this Cartesian in the format '(x, y)'.
  3650. *
  3651. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  3652. */
  3653. Cartesian2.prototype.toString = function () {
  3654. return "(" + this.x + ", " + this.y + ")";
  3655. };
  3656. exports.Cartesian2 = Cartesian2;
  3657. exports.Cartesian3 = Cartesian3;
  3658. exports.Cartographic = Cartographic;
  3659. exports.Ellipsoid = Ellipsoid;
  3660. exports.Rectangle = Rectangle;
  3661. });
  3662. //# sourceMappingURL=Cartesian2-33d2657c.js.map