BoundingSphere-775c5788.js 227 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 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/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './Check-70bec281', './Math-61ede240', './Cartographic-fe4be337', './Cartesian2-85064f09', './Cartesian4-5af5bb24', './RuntimeError-ba10bc3e'], function (exports, when, Check, _Math, Cartographic, Cartesian2, Cartesian4, RuntimeError) { 'use strict';
  24. /**
  25. * A simple map projection where longitude and latitude are linearly mapped to X and Y by multiplying
  26. * them by the {@link Ellipsoid#maximumRadius}. This projection
  27. * is commonly known as geographic, equirectangular, equidistant cylindrical, or plate carrée. It
  28. * is also known as EPSG:4326.
  29. *
  30. * @alias GeographicProjection
  31. * @constructor
  32. *
  33. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  34. *
  35. * @see WebMercatorProjection
  36. */
  37. function GeographicProjection(ellipsoid) {
  38. this._ellipsoid = when.defaultValue(ellipsoid, Cartesian2.Ellipsoid.WGS84);
  39. this._semimajorAxis = this._ellipsoid.maximumRadius;
  40. this._oneOverSemimajorAxis = 1.0 / this._semimajorAxis;
  41. }
  42. Object.defineProperties(GeographicProjection.prototype, {
  43. /**
  44. * Gets the {@link Ellipsoid}.
  45. *
  46. * @memberof GeographicProjection.prototype
  47. *
  48. * @type {Ellipsoid}
  49. * @readonly
  50. */
  51. ellipsoid : {
  52. get : function() {
  53. return this._ellipsoid;
  54. }
  55. }
  56. });
  57. /**
  58. * Projects a set of {@link Cartographic} coordinates, in radians, to map coordinates, in meters.
  59. * X and Y are the longitude and latitude, respectively, multiplied by the maximum radius of the
  60. * ellipsoid. Z is the unmodified height.
  61. *
  62. * @param {Cartographic} cartographic The coordinates to project.
  63. * @param {Cartesian3} [result] An instance into which to copy the result. If this parameter is
  64. * undefined, a new instance is created and returned.
  65. * @returns {Cartesian3} The projected coordinates. If the result parameter is not undefined, the
  66. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  67. * created and returned.
  68. */
  69. GeographicProjection.prototype.project = function(cartographic, result) {
  70. // Actually this is the special case of equidistant cylindrical called the plate carree
  71. var semimajorAxis = this._semimajorAxis;
  72. var x = cartographic.longitude * semimajorAxis;
  73. var y = cartographic.latitude * semimajorAxis;
  74. var z = cartographic.height;
  75. if (!when.defined(result)) {
  76. return new Cartographic.Cartesian3(x, y, z);
  77. }
  78. result.x = x;
  79. result.y = y;
  80. result.z = z;
  81. return result;
  82. };
  83. /**
  84. * Unprojects a set of projected {@link Cartesian3} coordinates, in meters, to {@link Cartographic}
  85. * coordinates, in radians. Longitude and Latitude are the X and Y coordinates, respectively,
  86. * divided by the maximum radius of the ellipsoid. Height is the unmodified Z coordinate.
  87. *
  88. * @param {Cartesian3} cartesian The Cartesian position to unproject with height (z) in meters.
  89. * @param {Cartographic} [result] An instance into which to copy the result. If this parameter is
  90. * undefined, a new instance is created and returned.
  91. * @returns {Cartographic} The unprojected coordinates. If the result parameter is not undefined, the
  92. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  93. * created and returned.
  94. */
  95. GeographicProjection.prototype.unproject = function(cartesian, result) {
  96. //>>includeStart('debug', pragmas.debug);
  97. if (!when.defined(cartesian)) {
  98. throw new Check.DeveloperError('cartesian is required');
  99. }
  100. //>>includeEnd('debug');
  101. var oneOverEarthSemimajorAxis = this._oneOverSemimajorAxis;
  102. var longitude = cartesian.x * oneOverEarthSemimajorAxis;
  103. var latitude = cartesian.y * oneOverEarthSemimajorAxis;
  104. var height = cartesian.z;
  105. if (!when.defined(result)) {
  106. return new Cartographic.Cartographic(longitude, latitude, height);
  107. }
  108. result.longitude = longitude;
  109. result.latitude = latitude;
  110. result.height = height;
  111. return result;
  112. };
  113. /**
  114. * This enumerated type is used in determining where, relative to the frustum, an
  115. * object is located. The object can either be fully contained within the frustum (INSIDE),
  116. * partially inside the frustum and partially outside (INTERSECTING), or somwhere entirely
  117. * outside of the frustum's 6 planes (OUTSIDE).
  118. *
  119. * @exports Intersect
  120. */
  121. var Intersect = {
  122. /**
  123. * Represents that an object is not contained within the frustum.
  124. *
  125. * @type {Number}
  126. * @constant
  127. */
  128. OUTSIDE : -1,
  129. /**
  130. * Represents that an object intersects one of the frustum's planes.
  131. *
  132. * @type {Number}
  133. * @constant
  134. */
  135. INTERSECTING : 0,
  136. /**
  137. * Represents that an object is fully within the frustum.
  138. *
  139. * @type {Number}
  140. * @constant
  141. */
  142. INSIDE : 1
  143. };
  144. var Intersect$1 = Object.freeze(Intersect);
  145. /**
  146. * Represents the closed interval [start, stop].
  147. * @alias Interval
  148. * @constructor
  149. *
  150. * @param {Number} [start=0.0] The beginning of the interval.
  151. * @param {Number} [stop=0.0] The end of the interval.
  152. */
  153. function Interval(start, stop) {
  154. /**
  155. * The beginning of the interval.
  156. * @type {Number}
  157. * @default 0.0
  158. */
  159. this.start = when.defaultValue(start, 0.0);
  160. /**
  161. * The end of the interval.
  162. * @type {Number}
  163. * @default 0.0
  164. */
  165. this.stop = when.defaultValue(stop, 0.0);
  166. }
  167. /**
  168. * A 3x3 matrix, indexable as a column-major order array.
  169. * Constructor parameters are in row-major order for code readability.
  170. * @alias Matrix3
  171. * @constructor
  172. *
  173. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  174. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  175. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  176. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  177. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  178. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  179. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  180. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  181. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  182. *
  183. * @see Matrix3.fromColumnMajorArray
  184. * @see Matrix3.fromRowMajorArray
  185. * @see Matrix3.fromQuaternion
  186. * @see Matrix3.fromScale
  187. * @see Matrix3.fromUniformScale
  188. * @see Matrix2
  189. * @see Matrix4
  190. */
  191. function Matrix3(column0Row0, column1Row0, column2Row0,
  192. column0Row1, column1Row1, column2Row1,
  193. column0Row2, column1Row2, column2Row2) {
  194. this[0] = when.defaultValue(column0Row0, 0.0);
  195. this[1] = when.defaultValue(column0Row1, 0.0);
  196. this[2] = when.defaultValue(column0Row2, 0.0);
  197. this[3] = when.defaultValue(column1Row0, 0.0);
  198. this[4] = when.defaultValue(column1Row1, 0.0);
  199. this[5] = when.defaultValue(column1Row2, 0.0);
  200. this[6] = when.defaultValue(column2Row0, 0.0);
  201. this[7] = when.defaultValue(column2Row1, 0.0);
  202. this[8] = when.defaultValue(column2Row2, 0.0);
  203. }
  204. /**
  205. * The number of elements used to pack the object into an array.
  206. * @type {Number}
  207. */
  208. Matrix3.packedLength = 9;
  209. /**
  210. * Stores the provided instance into the provided array.
  211. *
  212. * @param {Matrix3} value The value to pack.
  213. * @param {Number[]} array The array to pack into.
  214. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  215. *
  216. * @returns {Number[]} The array that was packed into
  217. */
  218. Matrix3.pack = function(value, array, startingIndex) {
  219. //>>includeStart('debug', pragmas.debug);
  220. Check.Check.typeOf.object('value', value);
  221. Check.Check.defined('array', array);
  222. //>>includeEnd('debug');
  223. startingIndex = when.defaultValue(startingIndex, 0);
  224. array[startingIndex++] = value[0];
  225. array[startingIndex++] = value[1];
  226. array[startingIndex++] = value[2];
  227. array[startingIndex++] = value[3];
  228. array[startingIndex++] = value[4];
  229. array[startingIndex++] = value[5];
  230. array[startingIndex++] = value[6];
  231. array[startingIndex++] = value[7];
  232. array[startingIndex++] = value[8];
  233. return array;
  234. };
  235. /**
  236. * Retrieves an instance from a packed array.
  237. *
  238. * @param {Number[]} array The packed array.
  239. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  240. * @param {Matrix3} [result] The object into which to store the result.
  241. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  242. */
  243. Matrix3.unpack = function(array, startingIndex, result) {
  244. //>>includeStart('debug', pragmas.debug);
  245. Check.Check.defined('array', array);
  246. //>>includeEnd('debug');
  247. startingIndex = when.defaultValue(startingIndex, 0);
  248. if (!when.defined(result)) {
  249. result = new Matrix3();
  250. }
  251. result[0] = array[startingIndex++];
  252. result[1] = array[startingIndex++];
  253. result[2] = array[startingIndex++];
  254. result[3] = array[startingIndex++];
  255. result[4] = array[startingIndex++];
  256. result[5] = array[startingIndex++];
  257. result[6] = array[startingIndex++];
  258. result[7] = array[startingIndex++];
  259. result[8] = array[startingIndex++];
  260. return result;
  261. };
  262. /**
  263. * Duplicates a Matrix3 instance.
  264. *
  265. * @param {Matrix3} matrix The matrix to duplicate.
  266. * @param {Matrix3} [result] The object onto which to store the result.
  267. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided. (Returns undefined if matrix is undefined)
  268. */
  269. Matrix3.clone = function(matrix, result) {
  270. if (!when.defined(matrix)) {
  271. return undefined;
  272. }
  273. if (!when.defined(result)) {
  274. return new Matrix3(matrix[0], matrix[3], matrix[6],
  275. matrix[1], matrix[4], matrix[7],
  276. matrix[2], matrix[5], matrix[8]);
  277. }
  278. result[0] = matrix[0];
  279. result[1] = matrix[1];
  280. result[2] = matrix[2];
  281. result[3] = matrix[3];
  282. result[4] = matrix[4];
  283. result[5] = matrix[5];
  284. result[6] = matrix[6];
  285. result[7] = matrix[7];
  286. result[8] = matrix[8];
  287. return result;
  288. };
  289. /**
  290. * Creates a Matrix3 from 9 consecutive elements in an array.
  291. *
  292. * @param {Number[]} array The array whose 9 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  293. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  294. * @param {Matrix3} [result] The object onto which to store the result.
  295. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  296. *
  297. * @example
  298. * // Create the Matrix3:
  299. * // [1.0, 2.0, 3.0]
  300. * // [1.0, 2.0, 3.0]
  301. * // [1.0, 2.0, 3.0]
  302. *
  303. * var v = [1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  304. * var m = Cesium.Matrix3.fromArray(v);
  305. *
  306. * // Create same Matrix3 with using an offset into an array
  307. * var v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0];
  308. * var m2 = Cesium.Matrix3.fromArray(v2, 2);
  309. */
  310. Matrix3.fromArray = function(array, startingIndex, result) {
  311. //>>includeStart('debug', pragmas.debug);
  312. Check.Check.defined('array', array);
  313. //>>includeEnd('debug');
  314. startingIndex = when.defaultValue(startingIndex, 0);
  315. if (!when.defined(result)) {
  316. result = new Matrix3();
  317. }
  318. result[0] = array[startingIndex];
  319. result[1] = array[startingIndex + 1];
  320. result[2] = array[startingIndex + 2];
  321. result[3] = array[startingIndex + 3];
  322. result[4] = array[startingIndex + 4];
  323. result[5] = array[startingIndex + 5];
  324. result[6] = array[startingIndex + 6];
  325. result[7] = array[startingIndex + 7];
  326. result[8] = array[startingIndex + 8];
  327. return result;
  328. };
  329. /**
  330. * Creates a Matrix3 instance from a column-major order array.
  331. *
  332. * @param {Number[]} values The column-major order array.
  333. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  334. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  335. */
  336. Matrix3.fromColumnMajorArray = function(values, result) {
  337. //>>includeStart('debug', pragmas.debug);
  338. Check.Check.defined('values', values);
  339. //>>includeEnd('debug');
  340. return Matrix3.clone(values, result);
  341. };
  342. /**
  343. * Creates a Matrix3 instance from a row-major order array.
  344. * The resulting matrix will be in column-major order.
  345. *
  346. * @param {Number[]} values The row-major order array.
  347. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  348. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  349. */
  350. Matrix3.fromRowMajorArray = function(values, result) {
  351. //>>includeStart('debug', pragmas.debug);
  352. Check.Check.defined('values', values);
  353. //>>includeEnd('debug');
  354. if (!when.defined(result)) {
  355. return new Matrix3(values[0], values[1], values[2],
  356. values[3], values[4], values[5],
  357. values[6], values[7], values[8]);
  358. }
  359. result[0] = values[0];
  360. result[1] = values[3];
  361. result[2] = values[6];
  362. result[3] = values[1];
  363. result[4] = values[4];
  364. result[5] = values[7];
  365. result[6] = values[2];
  366. result[7] = values[5];
  367. result[8] = values[8];
  368. return result;
  369. };
  370. /**
  371. * Computes a 3x3 rotation matrix from the provided quaternion.
  372. *
  373. * @param {Quaternion} quaternion the quaternion to use.
  374. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  375. * @returns {Matrix3} The 3x3 rotation matrix from this quaternion.
  376. */
  377. Matrix3.fromQuaternion = function(quaternion, result) {
  378. //>>includeStart('debug', pragmas.debug);
  379. Check.Check.typeOf.object('quaternion', quaternion);
  380. //>>includeEnd('debug');
  381. var x2 = quaternion.x * quaternion.x;
  382. var xy = quaternion.x * quaternion.y;
  383. var xz = quaternion.x * quaternion.z;
  384. var xw = quaternion.x * quaternion.w;
  385. var y2 = quaternion.y * quaternion.y;
  386. var yz = quaternion.y * quaternion.z;
  387. var yw = quaternion.y * quaternion.w;
  388. var z2 = quaternion.z * quaternion.z;
  389. var zw = quaternion.z * quaternion.w;
  390. var w2 = quaternion.w * quaternion.w;
  391. var m00 = x2 - y2 - z2 + w2;
  392. var m01 = 2.0 * (xy - zw);
  393. var m02 = 2.0 * (xz + yw);
  394. var m10 = 2.0 * (xy + zw);
  395. var m11 = -x2 + y2 - z2 + w2;
  396. var m12 = 2.0 * (yz - xw);
  397. var m20 = 2.0 * (xz - yw);
  398. var m21 = 2.0 * (yz + xw);
  399. var m22 = -x2 - y2 + z2 + w2;
  400. if (!when.defined(result)) {
  401. return new Matrix3(m00, m01, m02,
  402. m10, m11, m12,
  403. m20, m21, m22);
  404. }
  405. result[0] = m00;
  406. result[1] = m10;
  407. result[2] = m20;
  408. result[3] = m01;
  409. result[4] = m11;
  410. result[5] = m21;
  411. result[6] = m02;
  412. result[7] = m12;
  413. result[8] = m22;
  414. return result;
  415. };
  416. /**
  417. * Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
  418. *
  419. * @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
  420. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  421. * @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
  422. */
  423. Matrix3.fromHeadingPitchRoll = function(headingPitchRoll, result) {
  424. //>>includeStart('debug', pragmas.debug);
  425. Check.Check.typeOf.object('headingPitchRoll', headingPitchRoll);
  426. //>>includeEnd('debug');
  427. var cosTheta = Math.cos(-headingPitchRoll.pitch);
  428. var cosPsi = Math.cos(-headingPitchRoll.heading);
  429. var cosPhi = Math.cos(headingPitchRoll.roll);
  430. var sinTheta = Math.sin(-headingPitchRoll.pitch);
  431. var sinPsi = Math.sin(-headingPitchRoll.heading);
  432. var sinPhi = Math.sin(headingPitchRoll.roll);
  433. var m00 = cosTheta * cosPsi;
  434. var m01 = -cosPhi * sinPsi + sinPhi * sinTheta * cosPsi;
  435. var m02 = sinPhi * sinPsi + cosPhi * sinTheta * cosPsi;
  436. var m10 = cosTheta * sinPsi;
  437. var m11 = cosPhi * cosPsi + sinPhi * sinTheta * sinPsi;
  438. var m12 = -sinPhi * cosPsi + cosPhi * sinTheta * sinPsi;
  439. var m20 = -sinTheta;
  440. var m21 = sinPhi * cosTheta;
  441. var m22 = cosPhi * cosTheta;
  442. if (!when.defined(result)) {
  443. return new Matrix3(m00, m01, m02,
  444. m10, m11, m12,
  445. m20, m21, m22);
  446. }
  447. result[0] = m00;
  448. result[1] = m10;
  449. result[2] = m20;
  450. result[3] = m01;
  451. result[4] = m11;
  452. result[5] = m21;
  453. result[6] = m02;
  454. result[7] = m12;
  455. result[8] = m22;
  456. return result;
  457. };
  458. /**
  459. * Computes a Matrix3 instance representing a non-uniform scale.
  460. *
  461. * @param {Cartesian3} scale The x, y, and z scale factors.
  462. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  463. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  464. *
  465. * @example
  466. * // Creates
  467. * // [7.0, 0.0, 0.0]
  468. * // [0.0, 8.0, 0.0]
  469. * // [0.0, 0.0, 9.0]
  470. * var m = Cesium.Matrix3.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  471. */
  472. Matrix3.fromScale = function(scale, result) {
  473. //>>includeStart('debug', pragmas.debug);
  474. Check.Check.typeOf.object('scale', scale);
  475. //>>includeEnd('debug');
  476. if (!when.defined(result)) {
  477. return new Matrix3(
  478. scale.x, 0.0, 0.0,
  479. 0.0, scale.y, 0.0,
  480. 0.0, 0.0, scale.z);
  481. }
  482. result[0] = scale.x;
  483. result[1] = 0.0;
  484. result[2] = 0.0;
  485. result[3] = 0.0;
  486. result[4] = scale.y;
  487. result[5] = 0.0;
  488. result[6] = 0.0;
  489. result[7] = 0.0;
  490. result[8] = scale.z;
  491. return result;
  492. };
  493. /**
  494. * Computes a Matrix3 instance representing a uniform scale.
  495. *
  496. * @param {Number} scale The uniform scale factor.
  497. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  498. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  499. *
  500. * @example
  501. * // Creates
  502. * // [2.0, 0.0, 0.0]
  503. * // [0.0, 2.0, 0.0]
  504. * // [0.0, 0.0, 2.0]
  505. * var m = Cesium.Matrix3.fromUniformScale(2.0);
  506. */
  507. Matrix3.fromUniformScale = function(scale, result) {
  508. //>>includeStart('debug', pragmas.debug);
  509. Check.Check.typeOf.number('scale', scale);
  510. //>>includeEnd('debug');
  511. if (!when.defined(result)) {
  512. return new Matrix3(
  513. scale, 0.0, 0.0,
  514. 0.0, scale, 0.0,
  515. 0.0, 0.0, scale);
  516. }
  517. result[0] = scale;
  518. result[1] = 0.0;
  519. result[2] = 0.0;
  520. result[3] = 0.0;
  521. result[4] = scale;
  522. result[5] = 0.0;
  523. result[6] = 0.0;
  524. result[7] = 0.0;
  525. result[8] = scale;
  526. return result;
  527. };
  528. /**
  529. * Computes a Matrix3 instance representing the cross product equivalent matrix of a Cartesian3 vector.
  530. *
  531. * @param {Cartesian3} vector the vector on the left hand side of the cross product operation.
  532. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  533. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  534. *
  535. * @example
  536. * // Creates
  537. * // [0.0, -9.0, 8.0]
  538. * // [9.0, 0.0, -7.0]
  539. * // [-8.0, 7.0, 0.0]
  540. * var m = Cesium.Matrix3.fromCrossProduct(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  541. */
  542. Matrix3.fromCrossProduct = function(vector, result) {
  543. //>>includeStart('debug', pragmas.debug);
  544. Check.Check.typeOf.object('vector', vector);
  545. //>>includeEnd('debug');
  546. if (!when.defined(result)) {
  547. return new Matrix3(
  548. 0.0, -vector.z, vector.y,
  549. vector.z, 0.0, -vector.x,
  550. -vector.y, vector.x, 0.0);
  551. }
  552. result[0] = 0.0;
  553. result[1] = vector.z;
  554. result[2] = -vector.y;
  555. result[3] = -vector.z;
  556. result[4] = 0.0;
  557. result[5] = vector.x;
  558. result[6] = vector.y;
  559. result[7] = -vector.x;
  560. result[8] = 0.0;
  561. return result;
  562. };
  563. /**
  564. * Creates a rotation matrix around the x-axis.
  565. *
  566. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  567. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  568. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  569. *
  570. * @example
  571. * // Rotate a point 45 degrees counterclockwise around the x-axis.
  572. * var p = new Cesium.Cartesian3(5, 6, 7);
  573. * var m = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(45.0));
  574. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  575. */
  576. Matrix3.fromRotationX = function(angle, result) {
  577. //>>includeStart('debug', pragmas.debug);
  578. Check.Check.typeOf.number('angle', angle);
  579. //>>includeEnd('debug');
  580. var cosAngle = Math.cos(angle);
  581. var sinAngle = Math.sin(angle);
  582. if (!when.defined(result)) {
  583. return new Matrix3(
  584. 1.0, 0.0, 0.0,
  585. 0.0, cosAngle, -sinAngle,
  586. 0.0, sinAngle, cosAngle);
  587. }
  588. result[0] = 1.0;
  589. result[1] = 0.0;
  590. result[2] = 0.0;
  591. result[3] = 0.0;
  592. result[4] = cosAngle;
  593. result[5] = sinAngle;
  594. result[6] = 0.0;
  595. result[7] = -sinAngle;
  596. result[8] = cosAngle;
  597. return result;
  598. };
  599. /**
  600. * Creates a rotation matrix around the y-axis.
  601. *
  602. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  603. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  604. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  605. *
  606. * @example
  607. * // Rotate a point 45 degrees counterclockwise around the y-axis.
  608. * var p = new Cesium.Cartesian3(5, 6, 7);
  609. * var m = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(45.0));
  610. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  611. */
  612. Matrix3.fromRotationY = function(angle, result) {
  613. //>>includeStart('debug', pragmas.debug);
  614. Check.Check.typeOf.number('angle', angle);
  615. //>>includeEnd('debug');
  616. var cosAngle = Math.cos(angle);
  617. var sinAngle = Math.sin(angle);
  618. if (!when.defined(result)) {
  619. return new Matrix3(
  620. cosAngle, 0.0, sinAngle,
  621. 0.0, 1.0, 0.0,
  622. -sinAngle, 0.0, cosAngle);
  623. }
  624. result[0] = cosAngle;
  625. result[1] = 0.0;
  626. result[2] = -sinAngle;
  627. result[3] = 0.0;
  628. result[4] = 1.0;
  629. result[5] = 0.0;
  630. result[6] = sinAngle;
  631. result[7] = 0.0;
  632. result[8] = cosAngle;
  633. return result;
  634. };
  635. /**
  636. * Creates a rotation matrix around the z-axis.
  637. *
  638. * @param {Number} angle The angle, in radians, of the rotation. Positive angles are counterclockwise.
  639. * @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
  640. * @returns {Matrix3} The modified result parameter, or a new Matrix3 instance if one was not provided.
  641. *
  642. * @example
  643. * // Rotate a point 45 degrees counterclockwise around the z-axis.
  644. * var p = new Cesium.Cartesian3(5, 6, 7);
  645. * var m = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(45.0));
  646. * var rotated = Cesium.Matrix3.multiplyByVector(m, p, new Cesium.Cartesian3());
  647. */
  648. Matrix3.fromRotationZ = function(angle, result) {
  649. //>>includeStart('debug', pragmas.debug);
  650. Check.Check.typeOf.number('angle', angle);
  651. //>>includeEnd('debug');
  652. var cosAngle = Math.cos(angle);
  653. var sinAngle = Math.sin(angle);
  654. if (!when.defined(result)) {
  655. return new Matrix3(
  656. cosAngle, -sinAngle, 0.0,
  657. sinAngle, cosAngle, 0.0,
  658. 0.0, 0.0, 1.0);
  659. }
  660. result[0] = cosAngle;
  661. result[1] = sinAngle;
  662. result[2] = 0.0;
  663. result[3] = -sinAngle;
  664. result[4] = cosAngle;
  665. result[5] = 0.0;
  666. result[6] = 0.0;
  667. result[7] = 0.0;
  668. result[8] = 1.0;
  669. return result;
  670. };
  671. /**
  672. * Creates an Array from the provided Matrix3 instance.
  673. * The array will be in column-major order.
  674. *
  675. * @param {Matrix3} matrix The matrix to use..
  676. * @param {Number[]} [result] The Array onto which to store the result.
  677. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  678. */
  679. Matrix3.toArray = function(matrix, result) {
  680. //>>includeStart('debug', pragmas.debug);
  681. Check.Check.typeOf.object('matrix', matrix);
  682. //>>includeEnd('debug');
  683. if (!when.defined(result)) {
  684. return [matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6], matrix[7], matrix[8]];
  685. }
  686. result[0] = matrix[0];
  687. result[1] = matrix[1];
  688. result[2] = matrix[2];
  689. result[3] = matrix[3];
  690. result[4] = matrix[4];
  691. result[5] = matrix[5];
  692. result[6] = matrix[6];
  693. result[7] = matrix[7];
  694. result[8] = matrix[8];
  695. return result;
  696. };
  697. /**
  698. * Computes the array index of the element at the provided row and column.
  699. *
  700. * @param {Number} row The zero-based index of the row.
  701. * @param {Number} column The zero-based index of the column.
  702. * @returns {Number} The index of the element at the provided row and column.
  703. *
  704. * @exception {DeveloperError} row must be 0, 1, or 2.
  705. * @exception {DeveloperError} column must be 0, 1, or 2.
  706. *
  707. * @example
  708. * var myMatrix = new Cesium.Matrix3();
  709. * var column1Row0Index = Cesium.Matrix3.getElementIndex(1, 0);
  710. * var column1Row0 = myMatrix[column1Row0Index]
  711. * myMatrix[column1Row0Index] = 10.0;
  712. */
  713. Matrix3.getElementIndex = function(column, row) {
  714. //>>includeStart('debug', pragmas.debug);
  715. Check.Check.typeOf.number.greaterThanOrEquals('row', row, 0);
  716. Check.Check.typeOf.number.lessThanOrEquals('row', row, 2);
  717. Check.Check.typeOf.number.greaterThanOrEquals('column', column, 0);
  718. Check.Check.typeOf.number.lessThanOrEquals('column', column, 2);
  719. //>>includeEnd('debug');
  720. return column * 3 + row;
  721. };
  722. /**
  723. * Retrieves a copy of the matrix column at the provided index as a Cartesian3 instance.
  724. *
  725. * @param {Matrix3} matrix The matrix to use.
  726. * @param {Number} index The zero-based index of the column to retrieve.
  727. * @param {Cartesian3} result The object onto which to store the result.
  728. * @returns {Cartesian3} The modified result parameter.
  729. *
  730. * @exception {DeveloperError} index must be 0, 1, or 2.
  731. */
  732. Matrix3.getColumn = function(matrix, index, result) {
  733. //>>includeStart('debug', pragmas.debug);
  734. Check.Check.typeOf.object('matrix', matrix);
  735. Check.Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  736. Check.Check.typeOf.number.lessThanOrEquals('index', index, 2);
  737. Check.Check.typeOf.object('result', result);
  738. //>>includeEnd('debug');
  739. var startIndex = index * 3;
  740. var x = matrix[startIndex];
  741. var y = matrix[startIndex + 1];
  742. var z = matrix[startIndex + 2];
  743. result.x = x;
  744. result.y = y;
  745. result.z = z;
  746. return result;
  747. };
  748. /**
  749. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian3 instance.
  750. *
  751. * @param {Matrix3} matrix The matrix to use.
  752. * @param {Number} index The zero-based index of the column to set.
  753. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified column.
  754. * @param {Matrix3} result The object onto which to store the result.
  755. * @returns {Matrix3} The modified result parameter.
  756. *
  757. * @exception {DeveloperError} index must be 0, 1, or 2.
  758. */
  759. Matrix3.setColumn = function(matrix, index, cartesian, result) {
  760. //>>includeStart('debug', pragmas.debug);
  761. Check.Check.typeOf.object('matrix', matrix);
  762. Check.Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  763. Check.Check.typeOf.number.lessThanOrEquals('index', index, 2);
  764. Check.Check.typeOf.object('cartesian', cartesian);
  765. Check.Check.typeOf.object('result', result);
  766. //>>includeEnd('debug');
  767. result = Matrix3.clone(matrix, result);
  768. var startIndex = index * 3;
  769. result[startIndex] = cartesian.x;
  770. result[startIndex + 1] = cartesian.y;
  771. result[startIndex + 2] = cartesian.z;
  772. return result;
  773. };
  774. /**
  775. * Retrieves a copy of the matrix row at the provided index as a Cartesian3 instance.
  776. *
  777. * @param {Matrix3} matrix The matrix to use.
  778. * @param {Number} index The zero-based index of the row to retrieve.
  779. * @param {Cartesian3} result The object onto which to store the result.
  780. * @returns {Cartesian3} The modified result parameter.
  781. *
  782. * @exception {DeveloperError} index must be 0, 1, or 2.
  783. */
  784. Matrix3.getRow = function(matrix, index, result) {
  785. //>>includeStart('debug', pragmas.debug);
  786. Check.Check.typeOf.object('matrix', matrix);
  787. Check.Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  788. Check.Check.typeOf.number.lessThanOrEquals('index', index, 2);
  789. Check.Check.typeOf.object('result', result);
  790. //>>includeEnd('debug');
  791. var x = matrix[index];
  792. var y = matrix[index + 3];
  793. var z = matrix[index + 6];
  794. result.x = x;
  795. result.y = y;
  796. result.z = z;
  797. return result;
  798. };
  799. /**
  800. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian3 instance.
  801. *
  802. * @param {Matrix3} matrix The matrix to use.
  803. * @param {Number} index The zero-based index of the row to set.
  804. * @param {Cartesian3} cartesian The Cartesian whose values will be assigned to the specified row.
  805. * @param {Matrix3} result The object onto which to store the result.
  806. * @returns {Matrix3} The modified result parameter.
  807. *
  808. * @exception {DeveloperError} index must be 0, 1, or 2.
  809. */
  810. Matrix3.setRow = function(matrix, index, cartesian, result) {
  811. //>>includeStart('debug', pragmas.debug);
  812. Check.Check.typeOf.object('matrix', matrix);
  813. Check.Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  814. Check.Check.typeOf.number.lessThanOrEquals('index', index, 2);
  815. Check.Check.typeOf.object('cartesian', cartesian);
  816. Check.Check.typeOf.object('result', result);
  817. //>>includeEnd('debug');
  818. result = Matrix3.clone(matrix, result);
  819. result[index] = cartesian.x;
  820. result[index + 3] = cartesian.y;
  821. result[index + 6] = cartesian.z;
  822. return result;
  823. };
  824. var scratchColumn = new Cartographic.Cartesian3();
  825. /**
  826. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  827. *
  828. * @param {Matrix3} matrix The matrix.
  829. * @param {Cartesian3} result The object onto which to store the result.
  830. * @returns {Cartesian3} The modified result parameter.
  831. */
  832. Matrix3.getScale = function(matrix, result) {
  833. //>>includeStart('debug', pragmas.debug);
  834. Check.Check.typeOf.object('matrix', matrix);
  835. Check.Check.typeOf.object('result', result);
  836. //>>includeEnd('debug');
  837. result.x = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn));
  838. result.y = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.fromElements(matrix[3], matrix[4], matrix[5], scratchColumn));
  839. result.z = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.fromElements(matrix[6], matrix[7], matrix[8], scratchColumn));
  840. return result;
  841. };
  842. var scratchScale = new Cartographic.Cartesian3();
  843. /**
  844. * Computes the maximum scale assuming the matrix is an affine transformation.
  845. * The maximum scale is the maximum length of the column vectors.
  846. *
  847. * @param {Matrix3} matrix The matrix.
  848. * @returns {Number} The maximum scale.
  849. */
  850. Matrix3.getMaximumScale = function(matrix) {
  851. Matrix3.getScale(matrix, scratchScale);
  852. return Cartographic.Cartesian3.maximumComponent(scratchScale);
  853. };
  854. /**
  855. * Computes the product of two matrices.
  856. *
  857. * @param {Matrix3} left The first matrix.
  858. * @param {Matrix3} right The second matrix.
  859. * @param {Matrix3} result The object onto which to store the result.
  860. * @returns {Matrix3} The modified result parameter.
  861. */
  862. Matrix3.multiply = function(left, right, result) {
  863. //>>includeStart('debug', pragmas.debug);
  864. Check.Check.typeOf.object('left', left);
  865. Check.Check.typeOf.object('right', right);
  866. Check.Check.typeOf.object('result', result);
  867. //>>includeEnd('debug');
  868. var column0Row0 = left[0] * right[0] + left[3] * right[1] + left[6] * right[2];
  869. var column0Row1 = left[1] * right[0] + left[4] * right[1] + left[7] * right[2];
  870. var column0Row2 = left[2] * right[0] + left[5] * right[1] + left[8] * right[2];
  871. var column1Row0 = left[0] * right[3] + left[3] * right[4] + left[6] * right[5];
  872. var column1Row1 = left[1] * right[3] + left[4] * right[4] + left[7] * right[5];
  873. var column1Row2 = left[2] * right[3] + left[5] * right[4] + left[8] * right[5];
  874. var column2Row0 = left[0] * right[6] + left[3] * right[7] + left[6] * right[8];
  875. var column2Row1 = left[1] * right[6] + left[4] * right[7] + left[7] * right[8];
  876. var column2Row2 = left[2] * right[6] + left[5] * right[7] + left[8] * right[8];
  877. result[0] = column0Row0;
  878. result[1] = column0Row1;
  879. result[2] = column0Row2;
  880. result[3] = column1Row0;
  881. result[4] = column1Row1;
  882. result[5] = column1Row2;
  883. result[6] = column2Row0;
  884. result[7] = column2Row1;
  885. result[8] = column2Row2;
  886. return result;
  887. };
  888. /**
  889. * Computes the sum of two matrices.
  890. *
  891. * @param {Matrix3} left The first matrix.
  892. * @param {Matrix3} right The second matrix.
  893. * @param {Matrix3} result The object onto which to store the result.
  894. * @returns {Matrix3} The modified result parameter.
  895. */
  896. Matrix3.add = function(left, right, result) {
  897. //>>includeStart('debug', pragmas.debug);
  898. Check.Check.typeOf.object('left', left);
  899. Check.Check.typeOf.object('right', right);
  900. Check.Check.typeOf.object('result', result);
  901. //>>includeEnd('debug');
  902. result[0] = left[0] + right[0];
  903. result[1] = left[1] + right[1];
  904. result[2] = left[2] + right[2];
  905. result[3] = left[3] + right[3];
  906. result[4] = left[4] + right[4];
  907. result[5] = left[5] + right[5];
  908. result[6] = left[6] + right[6];
  909. result[7] = left[7] + right[7];
  910. result[8] = left[8] + right[8];
  911. return result;
  912. };
  913. /**
  914. * Computes the difference of two matrices.
  915. *
  916. * @param {Matrix3} left The first matrix.
  917. * @param {Matrix3} right The second matrix.
  918. * @param {Matrix3} result The object onto which to store the result.
  919. * @returns {Matrix3} The modified result parameter.
  920. */
  921. Matrix3.subtract = function(left, right, result) {
  922. //>>includeStart('debug', pragmas.debug);
  923. Check.Check.typeOf.object('left', left);
  924. Check.Check.typeOf.object('right', right);
  925. Check.Check.typeOf.object('result', result);
  926. //>>includeEnd('debug');
  927. result[0] = left[0] - right[0];
  928. result[1] = left[1] - right[1];
  929. result[2] = left[2] - right[2];
  930. result[3] = left[3] - right[3];
  931. result[4] = left[4] - right[4];
  932. result[5] = left[5] - right[5];
  933. result[6] = left[6] - right[6];
  934. result[7] = left[7] - right[7];
  935. result[8] = left[8] - right[8];
  936. return result;
  937. };
  938. /**
  939. * Computes the product of a matrix and a column vector.
  940. *
  941. * @param {Matrix3} matrix The matrix.
  942. * @param {Cartesian3} cartesian The column.
  943. * @param {Cartesian3} result The object onto which to store the result.
  944. * @returns {Cartesian3} The modified result parameter.
  945. */
  946. Matrix3.multiplyByVector = function(matrix, cartesian, result) {
  947. //>>includeStart('debug', pragmas.debug);
  948. Check.Check.typeOf.object('matrix', matrix);
  949. Check.Check.typeOf.object('cartesian', cartesian);
  950. Check.Check.typeOf.object('result', result);
  951. //>>includeEnd('debug');
  952. var vX = cartesian.x;
  953. var vY = cartesian.y;
  954. var vZ = cartesian.z;
  955. var x = matrix[0] * vX + matrix[3] * vY + matrix[6] * vZ;
  956. var y = matrix[1] * vX + matrix[4] * vY + matrix[7] * vZ;
  957. var z = matrix[2] * vX + matrix[5] * vY + matrix[8] * vZ;
  958. result.x = x;
  959. result.y = y;
  960. result.z = z;
  961. return result;
  962. };
  963. /**
  964. * Computes the product of a matrix and a scalar.
  965. *
  966. * @param {Matrix3} matrix The matrix.
  967. * @param {Number} scalar The number to multiply by.
  968. * @param {Matrix3} result The object onto which to store the result.
  969. * @returns {Matrix3} The modified result parameter.
  970. */
  971. Matrix3.multiplyByScalar = function(matrix, scalar, result) {
  972. //>>includeStart('debug', pragmas.debug);
  973. Check.Check.typeOf.object('matrix', matrix);
  974. Check.Check.typeOf.number('scalar', scalar);
  975. Check.Check.typeOf.object('result', result);
  976. //>>includeEnd('debug');
  977. result[0] = matrix[0] * scalar;
  978. result[1] = matrix[1] * scalar;
  979. result[2] = matrix[2] * scalar;
  980. result[3] = matrix[3] * scalar;
  981. result[4] = matrix[4] * scalar;
  982. result[5] = matrix[5] * scalar;
  983. result[6] = matrix[6] * scalar;
  984. result[7] = matrix[7] * scalar;
  985. result[8] = matrix[8] * scalar;
  986. return result;
  987. };
  988. /**
  989. * Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
  990. *
  991. * @param {Matrix3} matrix The matrix on the left-hand side.
  992. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  993. * @param {Matrix3} result The object onto which to store the result.
  994. * @returns {Matrix3} The modified result parameter.
  995. *
  996. *
  997. * @example
  998. * // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
  999. * Cesium.Matrix3.multiplyByScale(m, scale, m);
  1000. *
  1001. * @see Matrix3.fromScale
  1002. * @see Matrix3.multiplyByUniformScale
  1003. */
  1004. Matrix3.multiplyByScale = function(matrix, scale, result) {
  1005. //>>includeStart('debug', pragmas.debug);
  1006. Check.Check.typeOf.object('matrix', matrix);
  1007. Check.Check.typeOf.object('scale', scale);
  1008. Check.Check.typeOf.object('result', result);
  1009. //>>includeEnd('debug');
  1010. result[0] = matrix[0] * scale.x;
  1011. result[1] = matrix[1] * scale.x;
  1012. result[2] = matrix[2] * scale.x;
  1013. result[3] = matrix[3] * scale.y;
  1014. result[4] = matrix[4] * scale.y;
  1015. result[5] = matrix[5] * scale.y;
  1016. result[6] = matrix[6] * scale.z;
  1017. result[7] = matrix[7] * scale.z;
  1018. result[8] = matrix[8] * scale.z;
  1019. return result;
  1020. };
  1021. /**
  1022. * Creates a negated copy of the provided matrix.
  1023. *
  1024. * @param {Matrix3} matrix The matrix to negate.
  1025. * @param {Matrix3} result The object onto which to store the result.
  1026. * @returns {Matrix3} The modified result parameter.
  1027. */
  1028. Matrix3.negate = function(matrix, result) {
  1029. //>>includeStart('debug', pragmas.debug);
  1030. Check.Check.typeOf.object('matrix', matrix);
  1031. Check.Check.typeOf.object('result', result);
  1032. //>>includeEnd('debug');
  1033. result[0] = -matrix[0];
  1034. result[1] = -matrix[1];
  1035. result[2] = -matrix[2];
  1036. result[3] = -matrix[3];
  1037. result[4] = -matrix[4];
  1038. result[5] = -matrix[5];
  1039. result[6] = -matrix[6];
  1040. result[7] = -matrix[7];
  1041. result[8] = -matrix[8];
  1042. return result;
  1043. };
  1044. /**
  1045. * Computes the transpose of the provided matrix.
  1046. *
  1047. * @param {Matrix3} matrix The matrix to transpose.
  1048. * @param {Matrix3} result The object onto which to store the result.
  1049. * @returns {Matrix3} The modified result parameter.
  1050. */
  1051. Matrix3.transpose = function(matrix, result) {
  1052. //>>includeStart('debug', pragmas.debug);
  1053. Check.Check.typeOf.object('matrix', matrix);
  1054. Check.Check.typeOf.object('result', result);
  1055. //>>includeEnd('debug');
  1056. var column0Row0 = matrix[0];
  1057. var column0Row1 = matrix[3];
  1058. var column0Row2 = matrix[6];
  1059. var column1Row0 = matrix[1];
  1060. var column1Row1 = matrix[4];
  1061. var column1Row2 = matrix[7];
  1062. var column2Row0 = matrix[2];
  1063. var column2Row1 = matrix[5];
  1064. var column2Row2 = matrix[8];
  1065. result[0] = column0Row0;
  1066. result[1] = column0Row1;
  1067. result[2] = column0Row2;
  1068. result[3] = column1Row0;
  1069. result[4] = column1Row1;
  1070. result[5] = column1Row2;
  1071. result[6] = column2Row0;
  1072. result[7] = column2Row1;
  1073. result[8] = column2Row2;
  1074. return result;
  1075. };
  1076. var UNIT = new Cartographic.Cartesian3(1, 1, 1);
  1077. /**
  1078. * Extracts the rotation assuming the matrix is an affine transformation.
  1079. *
  1080. * @param {Matrix3} matrix The matrix.
  1081. * @param {Matrix3} result The object onto which to store the result.
  1082. * @returns {Matrix3} The modified result parameter
  1083. */
  1084. Matrix3.getRotation = function(matrix, result) {
  1085. //>>includeStart('debug', pragmas.debug);
  1086. Check.Check.typeOf.object('matrix', matrix);
  1087. Check.Check.typeOf.object('result', result);
  1088. //>>includeEnd('debug');
  1089. var inverseScale = Cartographic.Cartesian3.divideComponents(UNIT, Matrix3.getScale(matrix, scratchScale), scratchScale);
  1090. result = Matrix3.multiplyByScale(matrix, inverseScale, result);
  1091. return result;
  1092. };
  1093. function computeFrobeniusNorm(matrix) {
  1094. var norm = 0.0;
  1095. for (var i = 0; i < 9; ++i) {
  1096. var temp = matrix[i];
  1097. norm += temp * temp;
  1098. }
  1099. return Math.sqrt(norm);
  1100. }
  1101. var rowVal = [1, 0, 0];
  1102. var colVal = [2, 2, 1];
  1103. function offDiagonalFrobeniusNorm(matrix) {
  1104. // Computes the "off-diagonal" Frobenius norm.
  1105. // Assumes matrix is symmetric.
  1106. var norm = 0.0;
  1107. for (var i = 0; i < 3; ++i) {
  1108. var temp = matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])];
  1109. norm += 2.0 * temp * temp;
  1110. }
  1111. return Math.sqrt(norm);
  1112. }
  1113. function shurDecomposition(matrix, result) {
  1114. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  1115. // section 8.4.2 The 2by2 Symmetric Schur Decomposition.
  1116. //
  1117. // The routine takes a matrix, which is assumed to be symmetric, and
  1118. // finds the largest off-diagonal term, and then creates
  1119. // a matrix (result) which can be used to help reduce it
  1120. var tolerance = _Math.CesiumMath.EPSILON15;
  1121. var maxDiagonal = 0.0;
  1122. var rotAxis = 1;
  1123. // find pivot (rotAxis) based on max diagonal of matrix
  1124. for (var i = 0; i < 3; ++i) {
  1125. var temp = Math.abs(matrix[Matrix3.getElementIndex(colVal[i], rowVal[i])]);
  1126. if (temp > maxDiagonal) {
  1127. rotAxis = i;
  1128. maxDiagonal = temp;
  1129. }
  1130. }
  1131. var c = 1.0;
  1132. var s = 0.0;
  1133. var p = rowVal[rotAxis];
  1134. var q = colVal[rotAxis];
  1135. if (Math.abs(matrix[Matrix3.getElementIndex(q, p)]) > tolerance) {
  1136. var qq = matrix[Matrix3.getElementIndex(q, q)];
  1137. var pp = matrix[Matrix3.getElementIndex(p, p)];
  1138. var qp = matrix[Matrix3.getElementIndex(q, p)];
  1139. var tau = (qq - pp) / 2.0 / qp;
  1140. var t;
  1141. if (tau < 0.0) {
  1142. t = -1.0 / (-tau + Math.sqrt(1.0 + tau * tau));
  1143. } else {
  1144. t = 1.0 / (tau + Math.sqrt(1.0 + tau * tau));
  1145. }
  1146. c = 1.0 / Math.sqrt(1.0 + t * t);
  1147. s = t * c;
  1148. }
  1149. result = Matrix3.clone(Matrix3.IDENTITY, result);
  1150. result[Matrix3.getElementIndex(p, p)] = result[Matrix3.getElementIndex(q, q)] = c;
  1151. result[Matrix3.getElementIndex(q, p)] = s;
  1152. result[Matrix3.getElementIndex(p, q)] = -s;
  1153. return result;
  1154. }
  1155. var jMatrix = new Matrix3();
  1156. var jMatrixTranspose = new Matrix3();
  1157. /**
  1158. * Computes the eigenvectors and eigenvalues of a symmetric matrix.
  1159. * <p>
  1160. * Returns a diagonal matrix and unitary matrix such that:
  1161. * <code>matrix = unitary matrix * diagonal matrix * transpose(unitary matrix)</code>
  1162. * </p>
  1163. * <p>
  1164. * The values along the diagonal of the diagonal matrix are the eigenvalues. The columns
  1165. * of the unitary matrix are the corresponding eigenvectors.
  1166. * </p>
  1167. *
  1168. * @param {Matrix3} matrix The matrix to decompose into diagonal and unitary matrix. Expected to be symmetric.
  1169. * @param {Object} [result] An object with unitary and diagonal properties which are matrices onto which to store the result.
  1170. * @returns {Object} An object with unitary and diagonal properties which are the unitary and diagonal matrices, respectively.
  1171. *
  1172. * @example
  1173. * var a = //... symetric matrix
  1174. * var result = {
  1175. * unitary : new Cesium.Matrix3(),
  1176. * diagonal : new Cesium.Matrix3()
  1177. * };
  1178. * Cesium.Matrix3.computeEigenDecomposition(a, result);
  1179. *
  1180. * var unitaryTranspose = Cesium.Matrix3.transpose(result.unitary, new Cesium.Matrix3());
  1181. * var b = Cesium.Matrix3.multiply(result.unitary, result.diagonal, new Cesium.Matrix3());
  1182. * Cesium.Matrix3.multiply(b, unitaryTranspose, b); // b is now equal to a
  1183. *
  1184. * var lambda = Cesium.Matrix3.getColumn(result.diagonal, 0, new Cesium.Cartesian3()).x; // first eigenvalue
  1185. * var v = Cesium.Matrix3.getColumn(result.unitary, 0, new Cesium.Cartesian3()); // first eigenvector
  1186. * var c = Cesium.Cartesian3.multiplyByScalar(v, lambda, new Cesium.Cartesian3()); // equal to Cesium.Matrix3.multiplyByVector(a, v)
  1187. */
  1188. Matrix3.computeEigenDecomposition = function(matrix, result) {
  1189. //>>includeStart('debug', pragmas.debug);
  1190. Check.Check.typeOf.object('matrix', matrix);
  1191. //>>includeEnd('debug');
  1192. // This routine was created based upon Matrix Computations, 3rd ed., by Golub and Van Loan,
  1193. // section 8.4.3 The Classical Jacobi Algorithm
  1194. var tolerance = _Math.CesiumMath.EPSILON20;
  1195. var maxSweeps = 10;
  1196. var count = 0;
  1197. var sweep = 0;
  1198. if (!when.defined(result)) {
  1199. result = {};
  1200. }
  1201. var unitaryMatrix = result.unitary = Matrix3.clone(Matrix3.IDENTITY, result.unitary);
  1202. var diagMatrix = result.diagonal = Matrix3.clone(matrix, result.diagonal);
  1203. var epsilon = tolerance * computeFrobeniusNorm(diagMatrix);
  1204. while (sweep < maxSweeps && offDiagonalFrobeniusNorm(diagMatrix) > epsilon) {
  1205. shurDecomposition(diagMatrix, jMatrix);
  1206. Matrix3.transpose(jMatrix, jMatrixTranspose);
  1207. Matrix3.multiply(diagMatrix, jMatrix, diagMatrix);
  1208. Matrix3.multiply(jMatrixTranspose, diagMatrix, diagMatrix);
  1209. Matrix3.multiply(unitaryMatrix, jMatrix, unitaryMatrix);
  1210. if (++count > 2) {
  1211. ++sweep;
  1212. count = 0;
  1213. }
  1214. }
  1215. return result;
  1216. };
  1217. /**
  1218. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  1219. *
  1220. * @param {Matrix3} matrix The matrix with signed elements.
  1221. * @param {Matrix3} result The object onto which to store the result.
  1222. * @returns {Matrix3} The modified result parameter.
  1223. */
  1224. Matrix3.abs = function(matrix, result) {
  1225. //>>includeStart('debug', pragmas.debug);
  1226. Check.Check.typeOf.object('matrix', matrix);
  1227. Check.Check.typeOf.object('result', result);
  1228. //>>includeEnd('debug');
  1229. result[0] = Math.abs(matrix[0]);
  1230. result[1] = Math.abs(matrix[1]);
  1231. result[2] = Math.abs(matrix[2]);
  1232. result[3] = Math.abs(matrix[3]);
  1233. result[4] = Math.abs(matrix[4]);
  1234. result[5] = Math.abs(matrix[5]);
  1235. result[6] = Math.abs(matrix[6]);
  1236. result[7] = Math.abs(matrix[7]);
  1237. result[8] = Math.abs(matrix[8]);
  1238. return result;
  1239. };
  1240. /**
  1241. * Computes the determinant of the provided matrix.
  1242. *
  1243. * @param {Matrix3} matrix The matrix to use.
  1244. * @returns {Number} The value of the determinant of the matrix.
  1245. */
  1246. Matrix3.determinant = function(matrix) {
  1247. //>>includeStart('debug', pragmas.debug);
  1248. Check.Check.typeOf.object('matrix', matrix);
  1249. //>>includeEnd('debug');
  1250. var m11 = matrix[0];
  1251. var m21 = matrix[3];
  1252. var m31 = matrix[6];
  1253. var m12 = matrix[1];
  1254. var m22 = matrix[4];
  1255. var m32 = matrix[7];
  1256. var m13 = matrix[2];
  1257. var m23 = matrix[5];
  1258. var m33 = matrix[8];
  1259. return m11 * (m22 * m33 - m23 * m32) + m12 * (m23 * m31 - m21 * m33) + m13 * (m21 * m32 - m22 * m31);
  1260. };
  1261. /**
  1262. * Computes the inverse of the provided matrix.
  1263. *
  1264. * @param {Matrix3} matrix The matrix to invert.
  1265. * @param {Matrix3} result The object onto which to store the result.
  1266. * @returns {Matrix3} The modified result parameter.
  1267. *
  1268. * @exception {DeveloperError} matrix is not invertible.
  1269. */
  1270. Matrix3.inverse = function(matrix, result) {
  1271. //>>includeStart('debug', pragmas.debug);
  1272. Check.Check.typeOf.object('matrix', matrix);
  1273. Check.Check.typeOf.object('result', result);
  1274. //>>includeEnd('debug');
  1275. var m11 = matrix[0];
  1276. var m21 = matrix[1];
  1277. var m31 = matrix[2];
  1278. var m12 = matrix[3];
  1279. var m22 = matrix[4];
  1280. var m32 = matrix[5];
  1281. var m13 = matrix[6];
  1282. var m23 = matrix[7];
  1283. var m33 = matrix[8];
  1284. var determinant = Matrix3.determinant(matrix);
  1285. //>>includeStart('debug', pragmas.debug);
  1286. if (Math.abs(determinant) <= _Math.CesiumMath.EPSILON15) {
  1287. throw new Check.DeveloperError('matrix is not invertible');
  1288. }
  1289. //>>includeEnd('debug');
  1290. result[0] = m22 * m33 - m23 * m32;
  1291. result[1] = m23 * m31 - m21 * m33;
  1292. result[2] = m21 * m32 - m22 * m31;
  1293. result[3] = m13 * m32 - m12 * m33;
  1294. result[4] = m11 * m33 - m13 * m31;
  1295. result[5] = m12 * m31 - m11 * m32;
  1296. result[6] = m12 * m23 - m13 * m22;
  1297. result[7] = m13 * m21 - m11 * m23;
  1298. result[8] = m11 * m22 - m12 * m21;
  1299. var scale = 1.0 / determinant;
  1300. return Matrix3.multiplyByScalar(result, scale, result);
  1301. };
  1302. /**
  1303. * Compares the provided matrices componentwise and returns
  1304. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1305. *
  1306. * @param {Matrix3} [left] The first matrix.
  1307. * @param {Matrix3} [right] The second matrix.
  1308. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  1309. */
  1310. Matrix3.equals = function(left, right) {
  1311. return (left === right) ||
  1312. (when.defined(left) &&
  1313. when.defined(right) &&
  1314. left[0] === right[0] &&
  1315. left[1] === right[1] &&
  1316. left[2] === right[2] &&
  1317. left[3] === right[3] &&
  1318. left[4] === right[4] &&
  1319. left[5] === right[5] &&
  1320. left[6] === right[6] &&
  1321. left[7] === right[7] &&
  1322. left[8] === right[8]);
  1323. };
  1324. /**
  1325. * Compares the provided matrices componentwise and returns
  1326. * <code>true</code> if they are within the provided epsilon,
  1327. * <code>false</code> otherwise.
  1328. *
  1329. * @param {Matrix3} [left] The first matrix.
  1330. * @param {Matrix3} [right] The second matrix.
  1331. * @param {Number} epsilon The epsilon to use for equality testing.
  1332. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  1333. */
  1334. Matrix3.equalsEpsilon = function(left, right, epsilon) {
  1335. //>>includeStart('debug', pragmas.debug);
  1336. Check.Check.typeOf.number('epsilon', epsilon);
  1337. //>>includeEnd('debug');
  1338. return (left === right) ||
  1339. (when.defined(left) &&
  1340. when.defined(right) &&
  1341. Math.abs(left[0] - right[0]) <= epsilon &&
  1342. Math.abs(left[1] - right[1]) <= epsilon &&
  1343. Math.abs(left[2] - right[2]) <= epsilon &&
  1344. Math.abs(left[3] - right[3]) <= epsilon &&
  1345. Math.abs(left[4] - right[4]) <= epsilon &&
  1346. Math.abs(left[5] - right[5]) <= epsilon &&
  1347. Math.abs(left[6] - right[6]) <= epsilon &&
  1348. Math.abs(left[7] - right[7]) <= epsilon &&
  1349. Math.abs(left[8] - right[8]) <= epsilon);
  1350. };
  1351. /**
  1352. * An immutable Matrix3 instance initialized to the identity matrix.
  1353. *
  1354. * @type {Matrix3}
  1355. * @constant
  1356. */
  1357. Matrix3.IDENTITY = Object.freeze(new Matrix3(1.0, 0.0, 0.0,
  1358. 0.0, 1.0, 0.0,
  1359. 0.0, 0.0, 1.0));
  1360. /**
  1361. * An immutable Matrix3 instance initialized to the zero matrix.
  1362. *
  1363. * @type {Matrix3}
  1364. * @constant
  1365. */
  1366. Matrix3.ZERO = Object.freeze(new Matrix3(0.0, 0.0, 0.0,
  1367. 0.0, 0.0, 0.0,
  1368. 0.0, 0.0, 0.0));
  1369. /**
  1370. * The index into Matrix3 for column 0, row 0.
  1371. *
  1372. * @type {Number}
  1373. * @constant
  1374. */
  1375. Matrix3.COLUMN0ROW0 = 0;
  1376. /**
  1377. * The index into Matrix3 for column 0, row 1.
  1378. *
  1379. * @type {Number}
  1380. * @constant
  1381. */
  1382. Matrix3.COLUMN0ROW1 = 1;
  1383. /**
  1384. * The index into Matrix3 for column 0, row 2.
  1385. *
  1386. * @type {Number}
  1387. * @constant
  1388. */
  1389. Matrix3.COLUMN0ROW2 = 2;
  1390. /**
  1391. * The index into Matrix3 for column 1, row 0.
  1392. *
  1393. * @type {Number}
  1394. * @constant
  1395. */
  1396. Matrix3.COLUMN1ROW0 = 3;
  1397. /**
  1398. * The index into Matrix3 for column 1, row 1.
  1399. *
  1400. * @type {Number}
  1401. * @constant
  1402. */
  1403. Matrix3.COLUMN1ROW1 = 4;
  1404. /**
  1405. * The index into Matrix3 for column 1, row 2.
  1406. *
  1407. * @type {Number}
  1408. * @constant
  1409. */
  1410. Matrix3.COLUMN1ROW2 = 5;
  1411. /**
  1412. * The index into Matrix3 for column 2, row 0.
  1413. *
  1414. * @type {Number}
  1415. * @constant
  1416. */
  1417. Matrix3.COLUMN2ROW0 = 6;
  1418. /**
  1419. * The index into Matrix3 for column 2, row 1.
  1420. *
  1421. * @type {Number}
  1422. * @constant
  1423. */
  1424. Matrix3.COLUMN2ROW1 = 7;
  1425. /**
  1426. * The index into Matrix3 for column 2, row 2.
  1427. *
  1428. * @type {Number}
  1429. * @constant
  1430. */
  1431. Matrix3.COLUMN2ROW2 = 8;
  1432. Object.defineProperties(Matrix3.prototype, {
  1433. /**
  1434. * Gets the number of items in the collection.
  1435. * @memberof Matrix3.prototype
  1436. *
  1437. * @type {Number}
  1438. */
  1439. length : {
  1440. get : function() {
  1441. return Matrix3.packedLength;
  1442. }
  1443. }
  1444. });
  1445. /**
  1446. * Duplicates the provided Matrix3 instance.
  1447. *
  1448. * @param {Matrix3} [result] The object onto which to store the result.
  1449. * @returns {Matrix3} The modified result parameter or a new Matrix3 instance if one was not provided.
  1450. */
  1451. Matrix3.prototype.clone = function(result) {
  1452. return Matrix3.clone(this, result);
  1453. };
  1454. /**
  1455. * Compares this matrix to the provided matrix componentwise and returns
  1456. * <code>true</code> if they are equal, <code>false</code> otherwise.
  1457. *
  1458. * @param {Matrix3} [right] The right hand side matrix.
  1459. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  1460. */
  1461. Matrix3.prototype.equals = function(right) {
  1462. return Matrix3.equals(this, right);
  1463. };
  1464. /**
  1465. * @private
  1466. */
  1467. Matrix3.equalsArray = function(matrix, array, offset) {
  1468. return matrix[0] === array[offset] &&
  1469. matrix[1] === array[offset + 1] &&
  1470. matrix[2] === array[offset + 2] &&
  1471. matrix[3] === array[offset + 3] &&
  1472. matrix[4] === array[offset + 4] &&
  1473. matrix[5] === array[offset + 5] &&
  1474. matrix[6] === array[offset + 6] &&
  1475. matrix[7] === array[offset + 7] &&
  1476. matrix[8] === array[offset + 8];
  1477. };
  1478. /**
  1479. * Compares this matrix to the provided matrix componentwise and returns
  1480. * <code>true</code> if they are within the provided epsilon,
  1481. * <code>false</code> otherwise.
  1482. *
  1483. * @param {Matrix3} [right] The right hand side matrix.
  1484. * @param {Number} epsilon The epsilon to use for equality testing.
  1485. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  1486. */
  1487. Matrix3.prototype.equalsEpsilon = function(right, epsilon) {
  1488. return Matrix3.equalsEpsilon(this, right, epsilon);
  1489. };
  1490. /**
  1491. * Creates a string representing this Matrix with each row being
  1492. * on a separate line and in the format '(column0, column1, column2)'.
  1493. *
  1494. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2)'.
  1495. */
  1496. Matrix3.prototype.toString = function() {
  1497. return '(' + this[0] + ', ' + this[3] + ', ' + this[6] + ')\n' +
  1498. '(' + this[1] + ', ' + this[4] + ', ' + this[7] + ')\n' +
  1499. '(' + this[2] + ', ' + this[5] + ', ' + this[8] + ')';
  1500. };
  1501. /**
  1502. * A 4x4 matrix, indexable as a column-major order array.
  1503. * Constructor parameters are in row-major order for code readability.
  1504. * @alias Matrix4
  1505. * @constructor
  1506. *
  1507. * @param {Number} [column0Row0=0.0] The value for column 0, row 0.
  1508. * @param {Number} [column1Row0=0.0] The value for column 1, row 0.
  1509. * @param {Number} [column2Row0=0.0] The value for column 2, row 0.
  1510. * @param {Number} [column3Row0=0.0] The value for column 3, row 0.
  1511. * @param {Number} [column0Row1=0.0] The value for column 0, row 1.
  1512. * @param {Number} [column1Row1=0.0] The value for column 1, row 1.
  1513. * @param {Number} [column2Row1=0.0] The value for column 2, row 1.
  1514. * @param {Number} [column3Row1=0.0] The value for column 3, row 1.
  1515. * @param {Number} [column0Row2=0.0] The value for column 0, row 2.
  1516. * @param {Number} [column1Row2=0.0] The value for column 1, row 2.
  1517. * @param {Number} [column2Row2=0.0] The value for column 2, row 2.
  1518. * @param {Number} [column3Row2=0.0] The value for column 3, row 2.
  1519. * @param {Number} [column0Row3=0.0] The value for column 0, row 3.
  1520. * @param {Number} [column1Row3=0.0] The value for column 1, row 3.
  1521. * @param {Number} [column2Row3=0.0] The value for column 2, row 3.
  1522. * @param {Number} [column3Row3=0.0] The value for column 3, row 3.
  1523. *
  1524. * @see Matrix4.fromColumnMajorArray
  1525. * @see Matrix4.fromRowMajorArray
  1526. * @see Matrix4.fromRotationTranslation
  1527. * @see Matrix4.fromTranslationRotationScale
  1528. * @see Matrix4.fromTranslationQuaternionRotationScale
  1529. * @see Matrix4.fromTranslation
  1530. * @see Matrix4.fromScale
  1531. * @see Matrix4.fromUniformScale
  1532. * @see Matrix4.fromCamera
  1533. * @see Matrix4.computePerspectiveFieldOfView
  1534. * @see Matrix4.computeOrthographicOffCenter
  1535. * @see Matrix4.computePerspectiveOffCenter
  1536. * @see Matrix4.computeInfinitePerspectiveOffCenter
  1537. * @see Matrix4.computeViewportTransformation
  1538. * @see Matrix4.computeView
  1539. * @see Matrix2
  1540. * @see Matrix3
  1541. * @see Packable
  1542. */
  1543. function Matrix4(column0Row0, column1Row0, column2Row0, column3Row0,
  1544. column0Row1, column1Row1, column2Row1, column3Row1,
  1545. column0Row2, column1Row2, column2Row2, column3Row2,
  1546. column0Row3, column1Row3, column2Row3, column3Row3) {
  1547. this[0] = when.defaultValue(column0Row0, 0.0);
  1548. this[1] = when.defaultValue(column0Row1, 0.0);
  1549. this[2] = when.defaultValue(column0Row2, 0.0);
  1550. this[3] = when.defaultValue(column0Row3, 0.0);
  1551. this[4] = when.defaultValue(column1Row0, 0.0);
  1552. this[5] = when.defaultValue(column1Row1, 0.0);
  1553. this[6] = when.defaultValue(column1Row2, 0.0);
  1554. this[7] = when.defaultValue(column1Row3, 0.0);
  1555. this[8] = when.defaultValue(column2Row0, 0.0);
  1556. this[9] = when.defaultValue(column2Row1, 0.0);
  1557. this[10] = when.defaultValue(column2Row2, 0.0);
  1558. this[11] = when.defaultValue(column2Row3, 0.0);
  1559. this[12] = when.defaultValue(column3Row0, 0.0);
  1560. this[13] = when.defaultValue(column3Row1, 0.0);
  1561. this[14] = when.defaultValue(column3Row2, 0.0);
  1562. this[15] = when.defaultValue(column3Row3, 0.0);
  1563. }
  1564. /**
  1565. * The number of elements used to pack the object into an array.
  1566. * @type {Number}
  1567. */
  1568. Matrix4.packedLength = 16;
  1569. /**
  1570. * Stores the provided instance into the provided array.
  1571. *
  1572. * @param {Matrix4} value The value to pack.
  1573. * @param {Number[]} array The array to pack into.
  1574. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  1575. *
  1576. * @returns {Number[]} The array that was packed into
  1577. */
  1578. Matrix4.pack = function(value, array, startingIndex) {
  1579. //>>includeStart('debug', pragmas.debug);
  1580. Check.Check.typeOf.object('value', value);
  1581. Check.Check.defined('array', array);
  1582. //>>includeEnd('debug');
  1583. startingIndex = when.defaultValue(startingIndex, 0);
  1584. array[startingIndex++] = value[0];
  1585. array[startingIndex++] = value[1];
  1586. array[startingIndex++] = value[2];
  1587. array[startingIndex++] = value[3];
  1588. array[startingIndex++] = value[4];
  1589. array[startingIndex++] = value[5];
  1590. array[startingIndex++] = value[6];
  1591. array[startingIndex++] = value[7];
  1592. array[startingIndex++] = value[8];
  1593. array[startingIndex++] = value[9];
  1594. array[startingIndex++] = value[10];
  1595. array[startingIndex++] = value[11];
  1596. array[startingIndex++] = value[12];
  1597. array[startingIndex++] = value[13];
  1598. array[startingIndex++] = value[14];
  1599. array[startingIndex] = value[15];
  1600. return array;
  1601. };
  1602. /**
  1603. * Retrieves an instance from a packed array.
  1604. *
  1605. * @param {Number[]} array The packed array.
  1606. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  1607. * @param {Matrix4} [result] The object into which to store the result.
  1608. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  1609. */
  1610. Matrix4.unpack = function(array, startingIndex, result) {
  1611. //>>includeStart('debug', pragmas.debug);
  1612. Check.Check.defined('array', array);
  1613. //>>includeEnd('debug');
  1614. startingIndex = when.defaultValue(startingIndex, 0);
  1615. if (!when.defined(result)) {
  1616. result = new Matrix4();
  1617. }
  1618. result[0] = array[startingIndex++];
  1619. result[1] = array[startingIndex++];
  1620. result[2] = array[startingIndex++];
  1621. result[3] = array[startingIndex++];
  1622. result[4] = array[startingIndex++];
  1623. result[5] = array[startingIndex++];
  1624. result[6] = array[startingIndex++];
  1625. result[7] = array[startingIndex++];
  1626. result[8] = array[startingIndex++];
  1627. result[9] = array[startingIndex++];
  1628. result[10] = array[startingIndex++];
  1629. result[11] = array[startingIndex++];
  1630. result[12] = array[startingIndex++];
  1631. result[13] = array[startingIndex++];
  1632. result[14] = array[startingIndex++];
  1633. result[15] = array[startingIndex];
  1634. return result;
  1635. };
  1636. /**
  1637. * Duplicates a Matrix4 instance.
  1638. *
  1639. * @param {Matrix4} matrix The matrix to duplicate.
  1640. * @param {Matrix4} [result] The object onto which to store the result.
  1641. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided. (Returns undefined if matrix is undefined)
  1642. */
  1643. Matrix4.clone = function(matrix, result) {
  1644. if (!when.defined(matrix)) {
  1645. return undefined;
  1646. }
  1647. if (!when.defined(result)) {
  1648. return new Matrix4(matrix[0], matrix[4], matrix[8], matrix[12],
  1649. matrix[1], matrix[5], matrix[9], matrix[13],
  1650. matrix[2], matrix[6], matrix[10], matrix[14],
  1651. matrix[3], matrix[7], matrix[11], matrix[15]);
  1652. }
  1653. result[0] = matrix[0];
  1654. result[1] = matrix[1];
  1655. result[2] = matrix[2];
  1656. result[3] = matrix[3];
  1657. result[4] = matrix[4];
  1658. result[5] = matrix[5];
  1659. result[6] = matrix[6];
  1660. result[7] = matrix[7];
  1661. result[8] = matrix[8];
  1662. result[9] = matrix[9];
  1663. result[10] = matrix[10];
  1664. result[11] = matrix[11];
  1665. result[12] = matrix[12];
  1666. result[13] = matrix[13];
  1667. result[14] = matrix[14];
  1668. result[15] = matrix[15];
  1669. return result;
  1670. };
  1671. /**
  1672. * Creates a Matrix4 from 16 consecutive elements in an array.
  1673. * @function
  1674. *
  1675. * @param {Number[]} array The array whose 16 consecutive elements correspond to the positions of the matrix. Assumes column-major order.
  1676. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to first column first row position in the matrix.
  1677. * @param {Matrix4} [result] The object onto which to store the result.
  1678. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  1679. *
  1680. * @example
  1681. * // Create the Matrix4:
  1682. * // [1.0, 2.0, 3.0, 4.0]
  1683. * // [1.0, 2.0, 3.0, 4.0]
  1684. * // [1.0, 2.0, 3.0, 4.0]
  1685. * // [1.0, 2.0, 3.0, 4.0]
  1686. *
  1687. * var v = [1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0];
  1688. * var m = Cesium.Matrix4.fromArray(v);
  1689. *
  1690. * // Create same Matrix4 with using an offset into an array
  1691. * var v2 = [0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0];
  1692. * var m2 = Cesium.Matrix4.fromArray(v2, 2);
  1693. */
  1694. Matrix4.fromArray = Matrix4.unpack;
  1695. /**
  1696. * Computes a Matrix4 instance from a column-major order array.
  1697. *
  1698. * @param {Number[]} values The column-major order array.
  1699. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1700. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1701. */
  1702. Matrix4.fromColumnMajorArray = function(values, result) {
  1703. //>>includeStart('debug', pragmas.debug);
  1704. Check.Check.defined('values', values);
  1705. //>>includeEnd('debug');
  1706. return Matrix4.clone(values, result);
  1707. };
  1708. /**
  1709. * Computes a Matrix4 instance from a row-major order array.
  1710. * The resulting matrix will be in column-major order.
  1711. *
  1712. * @param {Number[]} values The row-major order array.
  1713. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1714. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1715. */
  1716. Matrix4.fromRowMajorArray = function(values, result) {
  1717. //>>includeStart('debug', pragmas.debug);
  1718. Check.Check.defined('values', values);
  1719. //>>includeEnd('debug');
  1720. if (!when.defined(result)) {
  1721. return new Matrix4(values[0], values[1], values[2], values[3],
  1722. values[4], values[5], values[6], values[7],
  1723. values[8], values[9], values[10], values[11],
  1724. values[12], values[13], values[14], values[15]);
  1725. }
  1726. result[0] = values[0];
  1727. result[1] = values[4];
  1728. result[2] = values[8];
  1729. result[3] = values[12];
  1730. result[4] = values[1];
  1731. result[5] = values[5];
  1732. result[6] = values[9];
  1733. result[7] = values[13];
  1734. result[8] = values[2];
  1735. result[9] = values[6];
  1736. result[10] = values[10];
  1737. result[11] = values[14];
  1738. result[12] = values[3];
  1739. result[13] = values[7];
  1740. result[14] = values[11];
  1741. result[15] = values[15];
  1742. return result;
  1743. };
  1744. /**
  1745. * Computes a Matrix4 instance from a Matrix3 representing the rotation
  1746. * and a Cartesian3 representing the translation.
  1747. *
  1748. * @param {Matrix3} rotation The upper left portion of the matrix representing the rotation.
  1749. * @param {Cartesian3} [translation=Cartesian3.ZERO] The upper right portion of the matrix representing the translation.
  1750. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1751. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1752. */
  1753. Matrix4.fromRotationTranslation = function(rotation, translation, result) {
  1754. //>>includeStart('debug', pragmas.debug);
  1755. Check.Check.typeOf.object('rotation', rotation);
  1756. //>>includeEnd('debug');
  1757. translation = when.defaultValue(translation, Cartographic.Cartesian3.ZERO);
  1758. if (!when.defined(result)) {
  1759. return new Matrix4(rotation[0], rotation[3], rotation[6], translation.x,
  1760. rotation[1], rotation[4], rotation[7], translation.y,
  1761. rotation[2], rotation[5], rotation[8], translation.z,
  1762. 0.0, 0.0, 0.0, 1.0);
  1763. }
  1764. result[0] = rotation[0];
  1765. result[1] = rotation[1];
  1766. result[2] = rotation[2];
  1767. result[3] = 0.0;
  1768. result[4] = rotation[3];
  1769. result[5] = rotation[4];
  1770. result[6] = rotation[5];
  1771. result[7] = 0.0;
  1772. result[8] = rotation[6];
  1773. result[9] = rotation[7];
  1774. result[10] = rotation[8];
  1775. result[11] = 0.0;
  1776. result[12] = translation.x;
  1777. result[13] = translation.y;
  1778. result[14] = translation.z;
  1779. result[15] = 1.0;
  1780. return result;
  1781. };
  1782. /**
  1783. * Computes a Matrix4 instance from a translation, rotation, and scale (TRS)
  1784. * representation with the rotation represented as a quaternion.
  1785. *
  1786. * @param {Cartesian3} translation The translation transformation.
  1787. * @param {Quaternion} rotation The rotation transformation.
  1788. * @param {Cartesian3} scale The non-uniform scale transformation.
  1789. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1790. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1791. *
  1792. * @example
  1793. * var result = Cesium.Matrix4.fromTranslationQuaternionRotationScale(
  1794. * new Cesium.Cartesian3(1.0, 2.0, 3.0), // translation
  1795. * Cesium.Quaternion.IDENTITY, // rotation
  1796. * new Cesium.Cartesian3(7.0, 8.0, 9.0), // scale
  1797. * result);
  1798. */
  1799. Matrix4.fromTranslationQuaternionRotationScale = function(translation, rotation, scale, result) {
  1800. //>>includeStart('debug', pragmas.debug);
  1801. Check.Check.typeOf.object('translation', translation);
  1802. Check.Check.typeOf.object('rotation', rotation);
  1803. Check.Check.typeOf.object('scale', scale);
  1804. //>>includeEnd('debug');
  1805. if (!when.defined(result)) {
  1806. result = new Matrix4();
  1807. }
  1808. var scaleX = scale.x;
  1809. var scaleY = scale.y;
  1810. var scaleZ = scale.z;
  1811. var x2 = rotation.x * rotation.x;
  1812. var xy = rotation.x * rotation.y;
  1813. var xz = rotation.x * rotation.z;
  1814. var xw = rotation.x * rotation.w;
  1815. var y2 = rotation.y * rotation.y;
  1816. var yz = rotation.y * rotation.z;
  1817. var yw = rotation.y * rotation.w;
  1818. var z2 = rotation.z * rotation.z;
  1819. var zw = rotation.z * rotation.w;
  1820. var w2 = rotation.w * rotation.w;
  1821. var m00 = x2 - y2 - z2 + w2;
  1822. var m01 = 2.0 * (xy - zw);
  1823. var m02 = 2.0 * (xz + yw);
  1824. var m10 = 2.0 * (xy + zw);
  1825. var m11 = -x2 + y2 - z2 + w2;
  1826. var m12 = 2.0 * (yz - xw);
  1827. var m20 = 2.0 * (xz - yw);
  1828. var m21 = 2.0 * (yz + xw);
  1829. var m22 = -x2 - y2 + z2 + w2;
  1830. result[0] = m00 * scaleX;
  1831. result[1] = m10 * scaleX;
  1832. result[2] = m20 * scaleX;
  1833. result[3] = 0.0;
  1834. result[4] = m01 * scaleY;
  1835. result[5] = m11 * scaleY;
  1836. result[6] = m21 * scaleY;
  1837. result[7] = 0.0;
  1838. result[8] = m02 * scaleZ;
  1839. result[9] = m12 * scaleZ;
  1840. result[10] = m22 * scaleZ;
  1841. result[11] = 0.0;
  1842. result[12] = translation.x;
  1843. result[13] = translation.y;
  1844. result[14] = translation.z;
  1845. result[15] = 1.0;
  1846. return result;
  1847. };
  1848. /**
  1849. * Creates a Matrix4 instance from a {@link TranslationRotationScale} instance.
  1850. *
  1851. * @param {TranslationRotationScale} translationRotationScale The instance.
  1852. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1853. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1854. */
  1855. Matrix4.fromTranslationRotationScale = function(translationRotationScale, result) {
  1856. //>>includeStart('debug', pragmas.debug);
  1857. Check.Check.typeOf.object('translationRotationScale', translationRotationScale);
  1858. //>>includeEnd('debug');
  1859. return Matrix4.fromTranslationQuaternionRotationScale(translationRotationScale.translation, translationRotationScale.rotation, translationRotationScale.scale, result);
  1860. };
  1861. /**
  1862. * Creates a Matrix4 instance from a Cartesian3 representing the translation.
  1863. *
  1864. * @param {Cartesian3} translation The upper right portion of the matrix representing the translation.
  1865. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1866. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1867. *
  1868. * @see Matrix4.multiplyByTranslation
  1869. */
  1870. Matrix4.fromTranslation = function(translation, result) {
  1871. //>>includeStart('debug', pragmas.debug);
  1872. Check.Check.typeOf.object('translation', translation);
  1873. //>>includeEnd('debug');
  1874. return Matrix4.fromRotationTranslation(Matrix3.IDENTITY, translation, result);
  1875. };
  1876. /**
  1877. * Computes a Matrix4 instance representing a non-uniform scale.
  1878. *
  1879. * @param {Cartesian3} scale The x, y, and z scale factors.
  1880. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1881. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1882. *
  1883. * @example
  1884. * // Creates
  1885. * // [7.0, 0.0, 0.0, 0.0]
  1886. * // [0.0, 8.0, 0.0, 0.0]
  1887. * // [0.0, 0.0, 9.0, 0.0]
  1888. * // [0.0, 0.0, 0.0, 1.0]
  1889. * var m = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(7.0, 8.0, 9.0));
  1890. */
  1891. Matrix4.fromScale = function(scale, result) {
  1892. //>>includeStart('debug', pragmas.debug);
  1893. Check.Check.typeOf.object('scale', scale);
  1894. //>>includeEnd('debug');
  1895. if (!when.defined(result)) {
  1896. return new Matrix4(
  1897. scale.x, 0.0, 0.0, 0.0,
  1898. 0.0, scale.y, 0.0, 0.0,
  1899. 0.0, 0.0, scale.z, 0.0,
  1900. 0.0, 0.0, 0.0, 1.0);
  1901. }
  1902. result[0] = scale.x;
  1903. result[1] = 0.0;
  1904. result[2] = 0.0;
  1905. result[3] = 0.0;
  1906. result[4] = 0.0;
  1907. result[5] = scale.y;
  1908. result[6] = 0.0;
  1909. result[7] = 0.0;
  1910. result[8] = 0.0;
  1911. result[9] = 0.0;
  1912. result[10] = scale.z;
  1913. result[11] = 0.0;
  1914. result[12] = 0.0;
  1915. result[13] = 0.0;
  1916. result[14] = 0.0;
  1917. result[15] = 1.0;
  1918. return result;
  1919. };
  1920. /**
  1921. * Computes a Matrix4 instance representing a uniform scale.
  1922. *
  1923. * @param {Number} scale The uniform scale factor.
  1924. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1925. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1926. *
  1927. * @example
  1928. * // Creates
  1929. * // [2.0, 0.0, 0.0, 0.0]
  1930. * // [0.0, 2.0, 0.0, 0.0]
  1931. * // [0.0, 0.0, 2.0, 0.0]
  1932. * // [0.0, 0.0, 0.0, 1.0]
  1933. * var m = Cesium.Matrix4.fromUniformScale(2.0);
  1934. */
  1935. Matrix4.fromUniformScale = function(scale, result) {
  1936. //>>includeStart('debug', pragmas.debug);
  1937. Check.Check.typeOf.number('scale', scale);
  1938. //>>includeEnd('debug');
  1939. if (!when.defined(result)) {
  1940. return new Matrix4(scale, 0.0, 0.0, 0.0,
  1941. 0.0, scale, 0.0, 0.0,
  1942. 0.0, 0.0, scale, 0.0,
  1943. 0.0, 0.0, 0.0, 1.0);
  1944. }
  1945. result[0] = scale;
  1946. result[1] = 0.0;
  1947. result[2] = 0.0;
  1948. result[3] = 0.0;
  1949. result[4] = 0.0;
  1950. result[5] = scale;
  1951. result[6] = 0.0;
  1952. result[7] = 0.0;
  1953. result[8] = 0.0;
  1954. result[9] = 0.0;
  1955. result[10] = scale;
  1956. result[11] = 0.0;
  1957. result[12] = 0.0;
  1958. result[13] = 0.0;
  1959. result[14] = 0.0;
  1960. result[15] = 1.0;
  1961. return result;
  1962. };
  1963. var fromCameraF = new Cartographic.Cartesian3();
  1964. var fromCameraR = new Cartographic.Cartesian3();
  1965. var fromCameraU = new Cartographic.Cartesian3();
  1966. /**
  1967. * Computes a Matrix4 instance from a Camera.
  1968. *
  1969. * @param {Camera} camera The camera to use.
  1970. * @param {Matrix4} [result] The object in which the result will be stored, if undefined a new instance will be created.
  1971. * @returns {Matrix4} The modified result parameter, or a new Matrix4 instance if one was not provided.
  1972. */
  1973. Matrix4.fromCamera = function(camera, result) {
  1974. //>>includeStart('debug', pragmas.debug);
  1975. Check.Check.typeOf.object('camera', camera);
  1976. //>>includeEnd('debug');
  1977. var position = camera.position;
  1978. var direction = camera.direction;
  1979. var up = camera.up;
  1980. //>>includeStart('debug', pragmas.debug);
  1981. Check.Check.typeOf.object('camera.position', position);
  1982. Check.Check.typeOf.object('camera.direction', direction);
  1983. Check.Check.typeOf.object('camera.up', up);
  1984. //>>includeEnd('debug');
  1985. Cartographic.Cartesian3.normalize(direction, fromCameraF);
  1986. Cartographic.Cartesian3.normalize(Cartographic.Cartesian3.cross(fromCameraF, up, fromCameraR), fromCameraR);
  1987. Cartographic.Cartesian3.normalize(Cartographic.Cartesian3.cross(fromCameraR, fromCameraF, fromCameraU), fromCameraU);
  1988. var sX = fromCameraR.x;
  1989. var sY = fromCameraR.y;
  1990. var sZ = fromCameraR.z;
  1991. var fX = fromCameraF.x;
  1992. var fY = fromCameraF.y;
  1993. var fZ = fromCameraF.z;
  1994. var uX = fromCameraU.x;
  1995. var uY = fromCameraU.y;
  1996. var uZ = fromCameraU.z;
  1997. var positionX = position.x;
  1998. var positionY = position.y;
  1999. var positionZ = position.z;
  2000. var t0 = sX * -positionX + sY * -positionY+ sZ * -positionZ;
  2001. var t1 = uX * -positionX + uY * -positionY+ uZ * -positionZ;
  2002. var t2 = fX * positionX + fY * positionY + fZ * positionZ;
  2003. // The code below this comment is an optimized
  2004. // version of the commented lines.
  2005. // Rather that create two matrices and then multiply,
  2006. // we just bake in the multiplcation as part of creation.
  2007. // var rotation = new Matrix4(
  2008. // sX, sY, sZ, 0.0,
  2009. // uX, uY, uZ, 0.0,
  2010. // -fX, -fY, -fZ, 0.0,
  2011. // 0.0, 0.0, 0.0, 1.0);
  2012. // var translation = new Matrix4(
  2013. // 1.0, 0.0, 0.0, -position.x,
  2014. // 0.0, 1.0, 0.0, -position.y,
  2015. // 0.0, 0.0, 1.0, -position.z,
  2016. // 0.0, 0.0, 0.0, 1.0);
  2017. // return rotation.multiply(translation);
  2018. if (!when.defined(result)) {
  2019. return new Matrix4(
  2020. sX, sY, sZ, t0,
  2021. uX, uY, uZ, t1,
  2022. -fX, -fY, -fZ, t2,
  2023. 0.0, 0.0, 0.0, 1.0);
  2024. }
  2025. result[0] = sX;
  2026. result[1] = uX;
  2027. result[2] = -fX;
  2028. result[3] = 0.0;
  2029. result[4] = sY;
  2030. result[5] = uY;
  2031. result[6] = -fY;
  2032. result[7] = 0.0;
  2033. result[8] = sZ;
  2034. result[9] = uZ;
  2035. result[10] = -fZ;
  2036. result[11] = 0.0;
  2037. result[12] = t0;
  2038. result[13] = t1;
  2039. result[14] = t2;
  2040. result[15] = 1.0;
  2041. return result;
  2042. };
  2043. /**
  2044. * Computes a Matrix4 instance representing a perspective transformation matrix.
  2045. *
  2046. * @param {Number} fovY The field of view along the Y axis in radians.
  2047. * @param {Number} aspectRatio The aspect ratio.
  2048. * @param {Number} near The distance to the near plane in meters.
  2049. * @param {Number} far The distance to the far plane in meters.
  2050. * @param {Matrix4} result The object in which the result will be stored.
  2051. * @returns {Matrix4} The modified result parameter.
  2052. *
  2053. * @exception {DeveloperError} fovY must be in (0, PI].
  2054. * @exception {DeveloperError} aspectRatio must be greater than zero.
  2055. * @exception {DeveloperError} near must be greater than zero.
  2056. * @exception {DeveloperError} far must be greater than zero.
  2057. */
  2058. Matrix4.computePerspectiveFieldOfView = function(fovY, aspectRatio, near, far, result) {
  2059. //>>includeStart('debug', pragmas.debug);
  2060. Check.Check.typeOf.number.greaterThan('fovY', fovY, 0.0);
  2061. Check.Check.typeOf.number.lessThan('fovY', fovY, Math.PI);
  2062. Check.Check.typeOf.number.greaterThan('near', near, 0.0);
  2063. Check.Check.typeOf.number.greaterThan('far', far, 0.0);
  2064. Check.Check.typeOf.object('result', result);
  2065. //>>includeEnd('debug');
  2066. var bottom = Math.tan(fovY * 0.5);
  2067. var column1Row1 = 1.0 / bottom;
  2068. var column0Row0 = column1Row1 / aspectRatio;
  2069. var column2Row2 = (far + near) / (near - far);
  2070. var column3Row2 = (2.0 * far * near) / (near - far);
  2071. result[0] = column0Row0;
  2072. result[1] = 0.0;
  2073. result[2] = 0.0;
  2074. result[3] = 0.0;
  2075. result[4] = 0.0;
  2076. result[5] = column1Row1;
  2077. result[6] = 0.0;
  2078. result[7] = 0.0;
  2079. result[8] = 0.0;
  2080. result[9] = 0.0;
  2081. result[10] = column2Row2;
  2082. result[11] = -1.0;
  2083. result[12] = 0.0;
  2084. result[13] = 0.0;
  2085. result[14] = column3Row2;
  2086. result[15] = 0.0;
  2087. return result;
  2088. };
  2089. /**
  2090. * Computes a Matrix4 instance representing an orthographic transformation matrix.
  2091. *
  2092. * @param {Number} left The number of meters to the left of the camera that will be in view.
  2093. * @param {Number} right The number of meters to the right of the camera that will be in view.
  2094. * @param {Number} bottom The number of meters below of the camera that will be in view.
  2095. * @param {Number} top The number of meters above of the camera that will be in view.
  2096. * @param {Number} near The distance to the near plane in meters.
  2097. * @param {Number} far The distance to the far plane in meters.
  2098. * @param {Matrix4} result The object in which the result will be stored.
  2099. * @returns {Matrix4} The modified result parameter.
  2100. */
  2101. Matrix4.computeOrthographicOffCenter = function(left, right, bottom, top, near, far, result) {
  2102. //>>includeStart('debug', pragmas.debug);
  2103. Check.Check.typeOf.number('left', left);
  2104. Check.Check.typeOf.number('right', right);
  2105. Check.Check.typeOf.number('bottom', bottom);
  2106. Check.Check.typeOf.number('top', top);
  2107. Check.Check.typeOf.number('near', near);
  2108. Check.Check.typeOf.number('far', far);
  2109. Check.Check.typeOf.object('result', result);
  2110. //>>includeEnd('debug');
  2111. var a = 1.0 / (right - left);
  2112. var b = 1.0 / (top - bottom);
  2113. var c = 1.0 / (far - near);
  2114. var tx = -(right + left) * a;
  2115. var ty = -(top + bottom) * b;
  2116. var tz = -(far + near) * c;
  2117. a *= 2.0;
  2118. b *= 2.0;
  2119. c *= -2.0;
  2120. result[0] = a;
  2121. result[1] = 0.0;
  2122. result[2] = 0.0;
  2123. result[3] = 0.0;
  2124. result[4] = 0.0;
  2125. result[5] = b;
  2126. result[6] = 0.0;
  2127. result[7] = 0.0;
  2128. result[8] = 0.0;
  2129. result[9] = 0.0;
  2130. result[10] = c;
  2131. result[11] = 0.0;
  2132. result[12] = tx;
  2133. result[13] = ty;
  2134. result[14] = tz;
  2135. result[15] = 1.0;
  2136. return result;
  2137. };
  2138. /**
  2139. * Computes a Matrix4 instance representing an off center perspective transformation.
  2140. *
  2141. * @param {Number} left The number of meters to the left of the camera that will be in view.
  2142. * @param {Number} right The number of meters to the right of the camera that will be in view.
  2143. * @param {Number} bottom The number of meters below of the camera that will be in view.
  2144. * @param {Number} top The number of meters above of the camera that will be in view.
  2145. * @param {Number} near The distance to the near plane in meters.
  2146. * @param {Number} far The distance to the far plane in meters.
  2147. * @param {Matrix4} result The object in which the result will be stored.
  2148. * @returns {Matrix4} The modified result parameter.
  2149. */
  2150. Matrix4.computePerspectiveOffCenter = function(left, right, bottom, top, near, far, result) {
  2151. //>>includeStart('debug', pragmas.debug);
  2152. Check.Check.typeOf.number('left', left);
  2153. Check.Check.typeOf.number('right', right);
  2154. Check.Check.typeOf.number('bottom', bottom);
  2155. Check.Check.typeOf.number('top', top);
  2156. Check.Check.typeOf.number('near', near);
  2157. Check.Check.typeOf.number('far', far);
  2158. Check.Check.typeOf.object('result', result);
  2159. //>>includeEnd('debug');
  2160. var column0Row0 = 2.0 * near / (right - left);
  2161. var column1Row1 = 2.0 * near / (top - bottom);
  2162. var column2Row0 = (right + left) / (right - left);
  2163. var column2Row1 = (top + bottom) / (top - bottom);
  2164. var column2Row2 = -(far + near) / (far - near);
  2165. var column2Row3 = -1.0;
  2166. var column3Row2 = -2.0 * far * near / (far - near);
  2167. result[0] = column0Row0;
  2168. result[1] = 0.0;
  2169. result[2] = 0.0;
  2170. result[3] = 0.0;
  2171. result[4] = 0.0;
  2172. result[5] = column1Row1;
  2173. result[6] = 0.0;
  2174. result[7] = 0.0;
  2175. result[8] = column2Row0;
  2176. result[9] = column2Row1;
  2177. result[10] = column2Row2;
  2178. result[11] = column2Row3;
  2179. result[12] = 0.0;
  2180. result[13] = 0.0;
  2181. result[14] = column3Row2;
  2182. result[15] = 0.0;
  2183. return result;
  2184. };
  2185. /**
  2186. * Computes a Matrix4 instance representing an infinite off center perspective transformation.
  2187. *
  2188. * @param {Number} left The number of meters to the left of the camera that will be in view.
  2189. * @param {Number} right The number of meters to the right of the camera that will be in view.
  2190. * @param {Number} bottom The number of meters below of the camera that will be in view.
  2191. * @param {Number} top The number of meters above of the camera that will be in view.
  2192. * @param {Number} near The distance to the near plane in meters.
  2193. * @param {Matrix4} result The object in which the result will be stored.
  2194. * @returns {Matrix4} The modified result parameter.
  2195. */
  2196. Matrix4.computeInfinitePerspectiveOffCenter = function(left, right, bottom, top, near, result) {
  2197. //>>includeStart('debug', pragmas.debug);
  2198. Check.Check.typeOf.number('left', left);
  2199. Check.Check.typeOf.number('right', right);
  2200. Check.Check.typeOf.number('bottom', bottom);
  2201. Check.Check.typeOf.number('top', top);
  2202. Check.Check.typeOf.number('near', near);
  2203. Check.Check.typeOf.object('result', result);
  2204. //>>includeEnd('debug');
  2205. var column0Row0 = 2.0 * near / (right - left);
  2206. var column1Row1 = 2.0 * near / (top - bottom);
  2207. var column2Row0 = (right + left) / (right - left);
  2208. var column2Row1 = (top + bottom) / (top - bottom);
  2209. var column2Row2 = -1.0;
  2210. var column2Row3 = -1.0;
  2211. var column3Row2 = -2.0 * near;
  2212. result[0] = column0Row0;
  2213. result[1] = 0.0;
  2214. result[2] = 0.0;
  2215. result[3] = 0.0;
  2216. result[4] = 0.0;
  2217. result[5] = column1Row1;
  2218. result[6] = 0.0;
  2219. result[7] = 0.0;
  2220. result[8] = column2Row0;
  2221. result[9] = column2Row1;
  2222. result[10] = column2Row2;
  2223. result[11] = column2Row3;
  2224. result[12] = 0.0;
  2225. result[13] = 0.0;
  2226. result[14] = column3Row2;
  2227. result[15] = 0.0;
  2228. return result;
  2229. };
  2230. /**
  2231. * Computes a Matrix4 instance that transforms from normalized device coordinates to window coordinates.
  2232. *
  2233. * @param {Object}[viewport = { x : 0.0, y : 0.0, width : 0.0, height : 0.0 }] The viewport's corners as shown in Example 1.
  2234. * @param {Number}[nearDepthRange=0.0] The near plane distance in window coordinates.
  2235. * @param {Number}[farDepthRange=1.0] The far plane distance in window coordinates.
  2236. * @param {Matrix4} result The object in which the result will be stored.
  2237. * @returns {Matrix4} The modified result parameter.
  2238. *
  2239. * @example
  2240. * // Create viewport transformation using an explicit viewport and depth range.
  2241. * var m = Cesium.Matrix4.computeViewportTransformation({
  2242. * x : 0.0,
  2243. * y : 0.0,
  2244. * width : 1024.0,
  2245. * height : 768.0
  2246. * }, 0.0, 1.0, new Cesium.Matrix4());
  2247. */
  2248. Matrix4.computeViewportTransformation = function(viewport, nearDepthRange, farDepthRange, result) {
  2249. //>>includeStart('debug', pragmas.debug);
  2250. Check.Check.typeOf.object('result', result);
  2251. //>>includeEnd('debug');
  2252. viewport = when.defaultValue(viewport, when.defaultValue.EMPTY_OBJECT);
  2253. var x = when.defaultValue(viewport.x, 0.0);
  2254. var y = when.defaultValue(viewport.y, 0.0);
  2255. var width = when.defaultValue(viewport.width, 0.0);
  2256. var height = when.defaultValue(viewport.height, 0.0);
  2257. nearDepthRange = when.defaultValue(nearDepthRange, 0.0);
  2258. farDepthRange = when.defaultValue(farDepthRange, 1.0);
  2259. var halfWidth = width * 0.5;
  2260. var halfHeight = height * 0.5;
  2261. var halfDepth = (farDepthRange - nearDepthRange) * 0.5;
  2262. var column0Row0 = halfWidth;
  2263. var column1Row1 = halfHeight;
  2264. var column2Row2 = halfDepth;
  2265. var column3Row0 = x + halfWidth;
  2266. var column3Row1 = y + halfHeight;
  2267. var column3Row2 = nearDepthRange + halfDepth;
  2268. var column3Row3 = 1.0;
  2269. result[0] = column0Row0;
  2270. result[1] = 0.0;
  2271. result[2] = 0.0;
  2272. result[3] = 0.0;
  2273. result[4] = 0.0;
  2274. result[5] = column1Row1;
  2275. result[6] = 0.0;
  2276. result[7] = 0.0;
  2277. result[8] = 0.0;
  2278. result[9] = 0.0;
  2279. result[10] = column2Row2;
  2280. result[11] = 0.0;
  2281. result[12] = column3Row0;
  2282. result[13] = column3Row1;
  2283. result[14] = column3Row2;
  2284. result[15] = column3Row3;
  2285. return result;
  2286. };
  2287. /**
  2288. * Computes a Matrix4 instance that transforms from world space to view space.
  2289. *
  2290. * @param {Cartesian3} position The position of the camera.
  2291. * @param {Cartesian3} direction The forward direction.
  2292. * @param {Cartesian3} up The up direction.
  2293. * @param {Cartesian3} right The right direction.
  2294. * @param {Matrix4} result The object in which the result will be stored.
  2295. * @returns {Matrix4} The modified result parameter.
  2296. */
  2297. Matrix4.computeView = function(position, direction, up, right, result) {
  2298. //>>includeStart('debug', pragmas.debug);
  2299. Check.Check.typeOf.object('position', position);
  2300. Check.Check.typeOf.object('direction', direction);
  2301. Check.Check.typeOf.object('up', up);
  2302. Check.Check.typeOf.object('right', right);
  2303. Check.Check.typeOf.object('result', result);
  2304. //>>includeEnd('debug');
  2305. result[0] = right.x;
  2306. result[1] = up.x;
  2307. result[2] = -direction.x;
  2308. result[3] = 0.0;
  2309. result[4] = right.y;
  2310. result[5] = up.y;
  2311. result[6] = -direction.y;
  2312. result[7] = 0.0;
  2313. result[8] = right.z;
  2314. result[9] = up.z;
  2315. result[10] = -direction.z;
  2316. result[11] = 0.0;
  2317. result[12] = -Cartographic.Cartesian3.dot(right, position);
  2318. result[13] = -Cartographic.Cartesian3.dot(up, position);
  2319. result[14] = Cartographic.Cartesian3.dot(direction, position);
  2320. result[15] = 1.0;
  2321. return result;
  2322. };
  2323. /**
  2324. * Computes an Array from the provided Matrix4 instance.
  2325. * The array will be in column-major order.
  2326. *
  2327. * @param {Matrix4} matrix The matrix to use..
  2328. * @param {Number[]} [result] The Array onto which to store the result.
  2329. * @returns {Number[]} The modified Array parameter or a new Array instance if one was not provided.
  2330. *
  2331. * @example
  2332. * //create an array from an instance of Matrix4
  2333. * // m = [10.0, 14.0, 18.0, 22.0]
  2334. * // [11.0, 15.0, 19.0, 23.0]
  2335. * // [12.0, 16.0, 20.0, 24.0]
  2336. * // [13.0, 17.0, 21.0, 25.0]
  2337. * var a = Cesium.Matrix4.toArray(m);
  2338. *
  2339. * // m remains the same
  2340. * //creates a = [10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0]
  2341. */
  2342. Matrix4.toArray = function(matrix, result) {
  2343. //>>includeStart('debug', pragmas.debug);
  2344. Check.Check.typeOf.object('matrix', matrix);
  2345. //>>includeEnd('debug');
  2346. if (!when.defined(result)) {
  2347. return [matrix[0], matrix[1], matrix[2], matrix[3],
  2348. matrix[4], matrix[5], matrix[6], matrix[7],
  2349. matrix[8], matrix[9], matrix[10], matrix[11],
  2350. matrix[12], matrix[13], matrix[14], matrix[15]];
  2351. }
  2352. result[0] = matrix[0];
  2353. result[1] = matrix[1];
  2354. result[2] = matrix[2];
  2355. result[3] = matrix[3];
  2356. result[4] = matrix[4];
  2357. result[5] = matrix[5];
  2358. result[6] = matrix[6];
  2359. result[7] = matrix[7];
  2360. result[8] = matrix[8];
  2361. result[9] = matrix[9];
  2362. result[10] = matrix[10];
  2363. result[11] = matrix[11];
  2364. result[12] = matrix[12];
  2365. result[13] = matrix[13];
  2366. result[14] = matrix[14];
  2367. result[15] = matrix[15];
  2368. return result;
  2369. };
  2370. /**
  2371. * Computes the array index of the element at the provided row and column.
  2372. *
  2373. * @param {Number} row The zero-based index of the row.
  2374. * @param {Number} column The zero-based index of the column.
  2375. * @returns {Number} The index of the element at the provided row and column.
  2376. *
  2377. * @exception {DeveloperError} row must be 0, 1, 2, or 3.
  2378. * @exception {DeveloperError} column must be 0, 1, 2, or 3.
  2379. *
  2380. * @example
  2381. * var myMatrix = new Cesium.Matrix4();
  2382. * var column1Row0Index = Cesium.Matrix4.getElementIndex(1, 0);
  2383. * var column1Row0 = myMatrix[column1Row0Index];
  2384. * myMatrix[column1Row0Index] = 10.0;
  2385. */
  2386. Matrix4.getElementIndex = function(column, row) {
  2387. //>>includeStart('debug', pragmas.debug);
  2388. Check.Check.typeOf.number.greaterThanOrEquals('row', row, 0);
  2389. Check.Check.typeOf.number.lessThanOrEquals('row', row, 3);
  2390. Check.Check.typeOf.number.greaterThanOrEquals('column', column, 0);
  2391. Check.Check.typeOf.number.lessThanOrEquals('column', column, 3);
  2392. //>>includeEnd('debug');
  2393. return column * 4 + row;
  2394. };
  2395. /**
  2396. * Retrieves a copy of the matrix column at the provided index as a Cartesian4 instance.
  2397. *
  2398. * @param {Matrix4} matrix The matrix to use.
  2399. * @param {Number} index The zero-based index of the column to retrieve.
  2400. * @param {Cartesian4} result The object onto which to store the result.
  2401. * @returns {Cartesian4} The modified result parameter.
  2402. *
  2403. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  2404. *
  2405. * @example
  2406. * //returns a Cartesian4 instance with values from the specified column
  2407. * // m = [10.0, 11.0, 12.0, 13.0]
  2408. * // [14.0, 15.0, 16.0, 17.0]
  2409. * // [18.0, 19.0, 20.0, 21.0]
  2410. * // [22.0, 23.0, 24.0, 25.0]
  2411. *
  2412. * //Example 1: Creates an instance of Cartesian
  2413. * var a = Cesium.Matrix4.getColumn(m, 2, new Cesium.Cartesian4());
  2414. *
  2415. * @example
  2416. * //Example 2: Sets values for Cartesian instance
  2417. * var a = new Cesium.Cartesian4();
  2418. * Cesium.Matrix4.getColumn(m, 2, a);
  2419. *
  2420. * // a.x = 12.0; a.y = 16.0; a.z = 20.0; a.w = 24.0;
  2421. */
  2422. Matrix4.getColumn = function(matrix, index, result) {
  2423. //>>includeStart('debug', pragmas.debug);
  2424. Check.Check.typeOf.object('matrix', matrix);
  2425. Check.Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  2426. Check.Check.typeOf.number.lessThanOrEquals('index', index, 3);
  2427. Check.Check.typeOf.object('result', result);
  2428. //>>includeEnd('debug');
  2429. var startIndex = index * 4;
  2430. var x = matrix[startIndex];
  2431. var y = matrix[startIndex + 1];
  2432. var z = matrix[startIndex + 2];
  2433. var w = matrix[startIndex + 3];
  2434. result.x = x;
  2435. result.y = y;
  2436. result.z = z;
  2437. result.w = w;
  2438. return result;
  2439. };
  2440. /**
  2441. * Computes a new matrix that replaces the specified column in the provided matrix with the provided Cartesian4 instance.
  2442. *
  2443. * @param {Matrix4} matrix The matrix to use.
  2444. * @param {Number} index The zero-based index of the column to set.
  2445. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified column.
  2446. * @param {Matrix4} result The object onto which to store the result.
  2447. * @returns {Matrix4} The modified result parameter.
  2448. *
  2449. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  2450. *
  2451. * @example
  2452. * //creates a new Matrix4 instance with new column values from the Cartesian4 instance
  2453. * // m = [10.0, 11.0, 12.0, 13.0]
  2454. * // [14.0, 15.0, 16.0, 17.0]
  2455. * // [18.0, 19.0, 20.0, 21.0]
  2456. * // [22.0, 23.0, 24.0, 25.0]
  2457. *
  2458. * var a = Cesium.Matrix4.setColumn(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  2459. *
  2460. * // m remains the same
  2461. * // a = [10.0, 11.0, 99.0, 13.0]
  2462. * // [14.0, 15.0, 98.0, 17.0]
  2463. * // [18.0, 19.0, 97.0, 21.0]
  2464. * // [22.0, 23.0, 96.0, 25.0]
  2465. */
  2466. Matrix4.setColumn = function(matrix, index, cartesian, result) {
  2467. //>>includeStart('debug', pragmas.debug);
  2468. Check.Check.typeOf.object('matrix', matrix);
  2469. Check.Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  2470. Check.Check.typeOf.number.lessThanOrEquals('index', index, 3);
  2471. Check.Check.typeOf.object('cartesian', cartesian);
  2472. Check.Check.typeOf.object('result', result);
  2473. //>>includeEnd('debug');
  2474. result = Matrix4.clone(matrix, result);
  2475. var startIndex = index * 4;
  2476. result[startIndex] = cartesian.x;
  2477. result[startIndex + 1] = cartesian.y;
  2478. result[startIndex + 2] = cartesian.z;
  2479. result[startIndex + 3] = cartesian.w;
  2480. return result;
  2481. };
  2482. /**
  2483. * Computes a new matrix that replaces the translation in the rightmost column of the provided
  2484. * matrix with the provided translation. This assumes the matrix is an affine transformation
  2485. *
  2486. * @param {Matrix4} matrix The matrix to use.
  2487. * @param {Cartesian3} translation The translation that replaces the translation of the provided matrix.
  2488. * @param {Matrix4} result The object onto which to store the result.
  2489. * @returns {Matrix4} The modified result parameter.
  2490. */
  2491. Matrix4.setTranslation = function(matrix, translation, result) {
  2492. //>>includeStart('debug', pragmas.debug);
  2493. Check.Check.typeOf.object('matrix', matrix);
  2494. Check.Check.typeOf.object('translation', translation);
  2495. Check.Check.typeOf.object('result', result);
  2496. //>>includeEnd('debug');
  2497. result[0] = matrix[0];
  2498. result[1] = matrix[1];
  2499. result[2] = matrix[2];
  2500. result[3] = matrix[3];
  2501. result[4] = matrix[4];
  2502. result[5] = matrix[5];
  2503. result[6] = matrix[6];
  2504. result[7] = matrix[7];
  2505. result[8] = matrix[8];
  2506. result[9] = matrix[9];
  2507. result[10] = matrix[10];
  2508. result[11] = matrix[11];
  2509. result[12] = translation.x;
  2510. result[13] = translation.y;
  2511. result[14] = translation.z;
  2512. result[15] = matrix[15];
  2513. return result;
  2514. };
  2515. var scaleScratch = new Cartographic.Cartesian3();
  2516. /**
  2517. * Computes a new matrix that replaces the scale with the provided scale. This assumes the matrix is an affine transformation
  2518. *
  2519. * @param {Matrix4} matrix The matrix to use.
  2520. * @param {Cartesian3} scale The scale that replaces the scale of the provided matrix.
  2521. * @param {Matrix4} result The object onto which to store the result.
  2522. * @returns {Matrix4} The modified result parameter.
  2523. */
  2524. Matrix4.setScale = function(matrix, scale, result) {
  2525. //>>includeStart('debug', pragmas.debug);
  2526. Check.Check.typeOf.object('matrix', matrix);
  2527. Check.Check.typeOf.object('scale', scale);
  2528. Check.Check.typeOf.object('result', result);
  2529. //>>includeEnd('debug');
  2530. var existingScale = Matrix4.getScale(matrix, scaleScratch);
  2531. var newScale = Cartographic.Cartesian3.divideComponents(scale, existingScale, scaleScratch);
  2532. return Matrix4.multiplyByScale(matrix, newScale, result);
  2533. };
  2534. /**
  2535. * Retrieves a copy of the matrix row at the provided index as a Cartesian4 instance.
  2536. *
  2537. * @param {Matrix4} matrix The matrix to use.
  2538. * @param {Number} index The zero-based index of the row to retrieve.
  2539. * @param {Cartesian4} result The object onto which to store the result.
  2540. * @returns {Cartesian4} The modified result parameter.
  2541. *
  2542. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  2543. *
  2544. * @example
  2545. * //returns a Cartesian4 instance with values from the specified column
  2546. * // m = [10.0, 11.0, 12.0, 13.0]
  2547. * // [14.0, 15.0, 16.0, 17.0]
  2548. * // [18.0, 19.0, 20.0, 21.0]
  2549. * // [22.0, 23.0, 24.0, 25.0]
  2550. *
  2551. * //Example 1: Returns an instance of Cartesian
  2552. * var a = Cesium.Matrix4.getRow(m, 2, new Cesium.Cartesian4());
  2553. *
  2554. * @example
  2555. * //Example 2: Sets values for a Cartesian instance
  2556. * var a = new Cesium.Cartesian4();
  2557. * Cesium.Matrix4.getRow(m, 2, a);
  2558. *
  2559. * // a.x = 18.0; a.y = 19.0; a.z = 20.0; a.w = 21.0;
  2560. */
  2561. Matrix4.getRow = function(matrix, index, result) {
  2562. //>>includeStart('debug', pragmas.debug);
  2563. Check.Check.typeOf.object('matrix', matrix);
  2564. Check.Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  2565. Check.Check.typeOf.number.lessThanOrEquals('index', index, 3);
  2566. Check.Check.typeOf.object('result', result);
  2567. //>>includeEnd('debug');
  2568. var x = matrix[index];
  2569. var y = matrix[index + 4];
  2570. var z = matrix[index + 8];
  2571. var w = matrix[index + 12];
  2572. result.x = x;
  2573. result.y = y;
  2574. result.z = z;
  2575. result.w = w;
  2576. return result;
  2577. };
  2578. /**
  2579. * Computes a new matrix that replaces the specified row in the provided matrix with the provided Cartesian4 instance.
  2580. *
  2581. * @param {Matrix4} matrix The matrix to use.
  2582. * @param {Number} index The zero-based index of the row to set.
  2583. * @param {Cartesian4} cartesian The Cartesian whose values will be assigned to the specified row.
  2584. * @param {Matrix4} result The object onto which to store the result.
  2585. * @returns {Matrix4} The modified result parameter.
  2586. *
  2587. * @exception {DeveloperError} index must be 0, 1, 2, or 3.
  2588. *
  2589. * @example
  2590. * //create a new Matrix4 instance with new row values from the Cartesian4 instance
  2591. * // m = [10.0, 11.0, 12.0, 13.0]
  2592. * // [14.0, 15.0, 16.0, 17.0]
  2593. * // [18.0, 19.0, 20.0, 21.0]
  2594. * // [22.0, 23.0, 24.0, 25.0]
  2595. *
  2596. * var a = Cesium.Matrix4.setRow(m, 2, new Cesium.Cartesian4(99.0, 98.0, 97.0, 96.0), new Cesium.Matrix4());
  2597. *
  2598. * // m remains the same
  2599. * // a = [10.0, 11.0, 12.0, 13.0]
  2600. * // [14.0, 15.0, 16.0, 17.0]
  2601. * // [99.0, 98.0, 97.0, 96.0]
  2602. * // [22.0, 23.0, 24.0, 25.0]
  2603. */
  2604. Matrix4.setRow = function(matrix, index, cartesian, result) {
  2605. //>>includeStart('debug', pragmas.debug);
  2606. Check.Check.typeOf.object('matrix', matrix);
  2607. Check.Check.typeOf.number.greaterThanOrEquals('index', index, 0);
  2608. Check.Check.typeOf.number.lessThanOrEquals('index', index, 3);
  2609. Check.Check.typeOf.object('cartesian', cartesian);
  2610. Check.Check.typeOf.object('result', result);
  2611. //>>includeEnd('debug');
  2612. result = Matrix4.clone(matrix, result);
  2613. result[index] = cartesian.x;
  2614. result[index + 4] = cartesian.y;
  2615. result[index + 8] = cartesian.z;
  2616. result[index + 12] = cartesian.w;
  2617. return result;
  2618. };
  2619. var scratchColumn$1 = new Cartographic.Cartesian3();
  2620. /**
  2621. * Extracts the non-uniform scale assuming the matrix is an affine transformation.
  2622. *
  2623. * @param {Matrix4} matrix The matrix.
  2624. * @param {Cartesian3} result The object onto which to store the result.
  2625. * @returns {Cartesian3} The modified result parameter
  2626. */
  2627. Matrix4.getScale = function(matrix, result) {
  2628. //>>includeStart('debug', pragmas.debug);
  2629. Check.Check.typeOf.object('matrix', matrix);
  2630. Check.Check.typeOf.object('result', result);
  2631. //>>includeEnd('debug');
  2632. result.x = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.fromElements(matrix[0], matrix[1], matrix[2], scratchColumn$1));
  2633. result.y = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.fromElements(matrix[4], matrix[5], matrix[6], scratchColumn$1));
  2634. result.z = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.fromElements(matrix[8], matrix[9], matrix[10], scratchColumn$1));
  2635. return result;
  2636. };
  2637. var scratchScale$1 = new Cartographic.Cartesian3();
  2638. /**
  2639. * Computes the maximum scale assuming the matrix is an affine transformation.
  2640. * The maximum scale is the maximum length of the column vectors in the upper-left
  2641. * 3x3 matrix.
  2642. *
  2643. * @param {Matrix4} matrix The matrix.
  2644. * @returns {Number} The maximum scale.
  2645. */
  2646. Matrix4.getMaximumScale = function(matrix) {
  2647. Matrix4.getScale(matrix, scratchScale$1);
  2648. return Cartographic.Cartesian3.maximumComponent(scratchScale$1);
  2649. };
  2650. /**
  2651. * Computes the product of two matrices.
  2652. *
  2653. * @param {Matrix4} left The first matrix.
  2654. * @param {Matrix4} right The second matrix.
  2655. * @param {Matrix4} result The object onto which to store the result.
  2656. * @returns {Matrix4} The modified result parameter.
  2657. */
  2658. Matrix4.multiply = function(left, right, result) {
  2659. //>>includeStart('debug', pragmas.debug);
  2660. Check.Check.typeOf.object('left', left);
  2661. Check.Check.typeOf.object('right', right);
  2662. Check.Check.typeOf.object('result', result);
  2663. //>>includeEnd('debug');
  2664. var left0 = left[0];
  2665. var left1 = left[1];
  2666. var left2 = left[2];
  2667. var left3 = left[3];
  2668. var left4 = left[4];
  2669. var left5 = left[5];
  2670. var left6 = left[6];
  2671. var left7 = left[7];
  2672. var left8 = left[8];
  2673. var left9 = left[9];
  2674. var left10 = left[10];
  2675. var left11 = left[11];
  2676. var left12 = left[12];
  2677. var left13 = left[13];
  2678. var left14 = left[14];
  2679. var left15 = left[15];
  2680. var right0 = right[0];
  2681. var right1 = right[1];
  2682. var right2 = right[2];
  2683. var right3 = right[3];
  2684. var right4 = right[4];
  2685. var right5 = right[5];
  2686. var right6 = right[6];
  2687. var right7 = right[7];
  2688. var right8 = right[8];
  2689. var right9 = right[9];
  2690. var right10 = right[10];
  2691. var right11 = right[11];
  2692. var right12 = right[12];
  2693. var right13 = right[13];
  2694. var right14 = right[14];
  2695. var right15 = right[15];
  2696. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2 + left12 * right3;
  2697. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2 + left13 * right3;
  2698. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2 + left14 * right3;
  2699. var column0Row3 = left3 * right0 + left7 * right1 + left11 * right2 + left15 * right3;
  2700. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6 + left12 * right7;
  2701. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6 + left13 * right7;
  2702. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6 + left14 * right7;
  2703. var column1Row3 = left3 * right4 + left7 * right5 + left11 * right6 + left15 * right7;
  2704. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10 + left12 * right11;
  2705. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10 + left13 * right11;
  2706. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10 + left14 * right11;
  2707. var column2Row3 = left3 * right8 + left7 * right9 + left11 * right10 + left15 * right11;
  2708. var column3Row0 = left0 * right12 + left4 * right13 + left8 * right14 + left12 * right15;
  2709. var column3Row1 = left1 * right12 + left5 * right13 + left9 * right14 + left13 * right15;
  2710. var column3Row2 = left2 * right12 + left6 * right13 + left10 * right14 + left14 * right15;
  2711. var column3Row3 = left3 * right12 + left7 * right13 + left11 * right14 + left15 * right15;
  2712. result[0] = column0Row0;
  2713. result[1] = column0Row1;
  2714. result[2] = column0Row2;
  2715. result[3] = column0Row3;
  2716. result[4] = column1Row0;
  2717. result[5] = column1Row1;
  2718. result[6] = column1Row2;
  2719. result[7] = column1Row3;
  2720. result[8] = column2Row0;
  2721. result[9] = column2Row1;
  2722. result[10] = column2Row2;
  2723. result[11] = column2Row3;
  2724. result[12] = column3Row0;
  2725. result[13] = column3Row1;
  2726. result[14] = column3Row2;
  2727. result[15] = column3Row3;
  2728. return result;
  2729. };
  2730. /**
  2731. * Computes the sum of two matrices.
  2732. *
  2733. * @param {Matrix4} left The first matrix.
  2734. * @param {Matrix4} right The second matrix.
  2735. * @param {Matrix4} result The object onto which to store the result.
  2736. * @returns {Matrix4} The modified result parameter.
  2737. */
  2738. Matrix4.add = function(left, right, result) {
  2739. //>>includeStart('debug', pragmas.debug);
  2740. Check.Check.typeOf.object('left', left);
  2741. Check.Check.typeOf.object('right', right);
  2742. Check.Check.typeOf.object('result', result);
  2743. //>>includeEnd('debug');
  2744. result[0] = left[0] + right[0];
  2745. result[1] = left[1] + right[1];
  2746. result[2] = left[2] + right[2];
  2747. result[3] = left[3] + right[3];
  2748. result[4] = left[4] + right[4];
  2749. result[5] = left[5] + right[5];
  2750. result[6] = left[6] + right[6];
  2751. result[7] = left[7] + right[7];
  2752. result[8] = left[8] + right[8];
  2753. result[9] = left[9] + right[9];
  2754. result[10] = left[10] + right[10];
  2755. result[11] = left[11] + right[11];
  2756. result[12] = left[12] + right[12];
  2757. result[13] = left[13] + right[13];
  2758. result[14] = left[14] + right[14];
  2759. result[15] = left[15] + right[15];
  2760. return result;
  2761. };
  2762. /**
  2763. * Computes the difference of two matrices.
  2764. *
  2765. * @param {Matrix4} left The first matrix.
  2766. * @param {Matrix4} right The second matrix.
  2767. * @param {Matrix4} result The object onto which to store the result.
  2768. * @returns {Matrix4} The modified result parameter.
  2769. */
  2770. Matrix4.subtract = function(left, right, result) {
  2771. //>>includeStart('debug', pragmas.debug);
  2772. Check.Check.typeOf.object('left', left);
  2773. Check.Check.typeOf.object('right', right);
  2774. Check.Check.typeOf.object('result', result);
  2775. //>>includeEnd('debug');
  2776. result[0] = left[0] - right[0];
  2777. result[1] = left[1] - right[1];
  2778. result[2] = left[2] - right[2];
  2779. result[3] = left[3] - right[3];
  2780. result[4] = left[4] - right[4];
  2781. result[5] = left[5] - right[5];
  2782. result[6] = left[6] - right[6];
  2783. result[7] = left[7] - right[7];
  2784. result[8] = left[8] - right[8];
  2785. result[9] = left[9] - right[9];
  2786. result[10] = left[10] - right[10];
  2787. result[11] = left[11] - right[11];
  2788. result[12] = left[12] - right[12];
  2789. result[13] = left[13] - right[13];
  2790. result[14] = left[14] - right[14];
  2791. result[15] = left[15] - right[15];
  2792. return result;
  2793. };
  2794. /**
  2795. * Computes the product of two matrices assuming the matrices are
  2796. * affine transformation matrices, where the upper left 3x3 elements
  2797. * are a rotation matrix, and the upper three elements in the fourth
  2798. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  2799. * The matrix is not verified to be in the proper form.
  2800. * This method is faster than computing the product for general 4x4
  2801. * matrices using {@link Matrix4.multiply}.
  2802. *
  2803. * @param {Matrix4} left The first matrix.
  2804. * @param {Matrix4} right The second matrix.
  2805. * @param {Matrix4} result The object onto which to store the result.
  2806. * @returns {Matrix4} The modified result parameter.
  2807. *
  2808. * @example
  2809. * var m1 = new Cesium.Matrix4(1.0, 6.0, 7.0, 0.0, 2.0, 5.0, 8.0, 0.0, 3.0, 4.0, 9.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  2810. * var m2 = Cesium.Transforms.eastNorthUpToFixedFrame(new Cesium.Cartesian3(1.0, 1.0, 1.0));
  2811. * var m3 = Cesium.Matrix4.multiplyTransformation(m1, m2, new Cesium.Matrix4());
  2812. */
  2813. Matrix4.multiplyTransformation = function(left, right, result) {
  2814. //>>includeStart('debug', pragmas.debug);
  2815. Check.Check.typeOf.object('left', left);
  2816. Check.Check.typeOf.object('right', right);
  2817. Check.Check.typeOf.object('result', result);
  2818. //>>includeEnd('debug');
  2819. var left0 = left[0];
  2820. var left1 = left[1];
  2821. var left2 = left[2];
  2822. var left4 = left[4];
  2823. var left5 = left[5];
  2824. var left6 = left[6];
  2825. var left8 = left[8];
  2826. var left9 = left[9];
  2827. var left10 = left[10];
  2828. var left12 = left[12];
  2829. var left13 = left[13];
  2830. var left14 = left[14];
  2831. var right0 = right[0];
  2832. var right1 = right[1];
  2833. var right2 = right[2];
  2834. var right4 = right[4];
  2835. var right5 = right[5];
  2836. var right6 = right[6];
  2837. var right8 = right[8];
  2838. var right9 = right[9];
  2839. var right10 = right[10];
  2840. var right12 = right[12];
  2841. var right13 = right[13];
  2842. var right14 = right[14];
  2843. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  2844. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  2845. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  2846. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  2847. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  2848. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  2849. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  2850. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  2851. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  2852. var column3Row0 = left0 * right12 + left4 * right13 + left8 * right14 + left12;
  2853. var column3Row1 = left1 * right12 + left5 * right13 + left9 * right14 + left13;
  2854. var column3Row2 = left2 * right12 + left6 * right13 + left10 * right14 + left14;
  2855. result[0] = column0Row0;
  2856. result[1] = column0Row1;
  2857. result[2] = column0Row2;
  2858. result[3] = 0.0;
  2859. result[4] = column1Row0;
  2860. result[5] = column1Row1;
  2861. result[6] = column1Row2;
  2862. result[7] = 0.0;
  2863. result[8] = column2Row0;
  2864. result[9] = column2Row1;
  2865. result[10] = column2Row2;
  2866. result[11] = 0.0;
  2867. result[12] = column3Row0;
  2868. result[13] = column3Row1;
  2869. result[14] = column3Row2;
  2870. result[15] = 1.0;
  2871. return result;
  2872. };
  2873. /**
  2874. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  2875. * by a 3x3 rotation matrix. This is an optimization
  2876. * for <code>Matrix4.multiply(m, Matrix4.fromRotationTranslation(rotation), m);</code> with less allocations and arithmetic operations.
  2877. *
  2878. * @param {Matrix4} matrix The matrix on the left-hand side.
  2879. * @param {Matrix3} rotation The 3x3 rotation matrix on the right-hand side.
  2880. * @param {Matrix4} result The object onto which to store the result.
  2881. * @returns {Matrix4} The modified result parameter.
  2882. *
  2883. * @example
  2884. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromRotationTranslation(rotation), m);
  2885. * Cesium.Matrix4.multiplyByMatrix3(m, rotation, m);
  2886. */
  2887. Matrix4.multiplyByMatrix3 = function(matrix, rotation, result) {
  2888. //>>includeStart('debug', pragmas.debug);
  2889. Check.Check.typeOf.object('matrix', matrix);
  2890. Check.Check.typeOf.object('rotation', rotation);
  2891. Check.Check.typeOf.object('result', result);
  2892. //>>includeEnd('debug');
  2893. var left0 = matrix[0];
  2894. var left1 = matrix[1];
  2895. var left2 = matrix[2];
  2896. var left4 = matrix[4];
  2897. var left5 = matrix[5];
  2898. var left6 = matrix[6];
  2899. var left8 = matrix[8];
  2900. var left9 = matrix[9];
  2901. var left10 = matrix[10];
  2902. var right0 = rotation[0];
  2903. var right1 = rotation[1];
  2904. var right2 = rotation[2];
  2905. var right4 = rotation[3];
  2906. var right5 = rotation[4];
  2907. var right6 = rotation[5];
  2908. var right8 = rotation[6];
  2909. var right9 = rotation[7];
  2910. var right10 = rotation[8];
  2911. var column0Row0 = left0 * right0 + left4 * right1 + left8 * right2;
  2912. var column0Row1 = left1 * right0 + left5 * right1 + left9 * right2;
  2913. var column0Row2 = left2 * right0 + left6 * right1 + left10 * right2;
  2914. var column1Row0 = left0 * right4 + left4 * right5 + left8 * right6;
  2915. var column1Row1 = left1 * right4 + left5 * right5 + left9 * right6;
  2916. var column1Row2 = left2 * right4 + left6 * right5 + left10 * right6;
  2917. var column2Row0 = left0 * right8 + left4 * right9 + left8 * right10;
  2918. var column2Row1 = left1 * right8 + left5 * right9 + left9 * right10;
  2919. var column2Row2 = left2 * right8 + left6 * right9 + left10 * right10;
  2920. result[0] = column0Row0;
  2921. result[1] = column0Row1;
  2922. result[2] = column0Row2;
  2923. result[3] = 0.0;
  2924. result[4] = column1Row0;
  2925. result[5] = column1Row1;
  2926. result[6] = column1Row2;
  2927. result[7] = 0.0;
  2928. result[8] = column2Row0;
  2929. result[9] = column2Row1;
  2930. result[10] = column2Row2;
  2931. result[11] = 0.0;
  2932. result[12] = matrix[12];
  2933. result[13] = matrix[13];
  2934. result[14] = matrix[14];
  2935. result[15] = matrix[15];
  2936. return result;
  2937. };
  2938. /**
  2939. * Multiplies a transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  2940. * by an implicit translation matrix defined by a {@link Cartesian3}. This is an optimization
  2941. * for <code>Matrix4.multiply(m, Matrix4.fromTranslation(position), m);</code> with less allocations and arithmetic operations.
  2942. *
  2943. * @param {Matrix4} matrix The matrix on the left-hand side.
  2944. * @param {Cartesian3} translation The translation on the right-hand side.
  2945. * @param {Matrix4} result The object onto which to store the result.
  2946. * @returns {Matrix4} The modified result parameter.
  2947. *
  2948. * @example
  2949. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromTranslation(position), m);
  2950. * Cesium.Matrix4.multiplyByTranslation(m, position, m);
  2951. */
  2952. Matrix4.multiplyByTranslation = function(matrix, translation, result) {
  2953. //>>includeStart('debug', pragmas.debug);
  2954. Check.Check.typeOf.object('matrix', matrix);
  2955. Check.Check.typeOf.object('translation', translation);
  2956. Check.Check.typeOf.object('result', result);
  2957. //>>includeEnd('debug');
  2958. var x = translation.x;
  2959. var y = translation.y;
  2960. var z = translation.z;
  2961. var tx = (x * matrix[0]) + (y * matrix[4]) + (z * matrix[8]) + matrix[12];
  2962. var ty = (x * matrix[1]) + (y * matrix[5]) + (z * matrix[9]) + matrix[13];
  2963. var tz = (x * matrix[2]) + (y * matrix[6]) + (z * matrix[10]) + matrix[14];
  2964. result[0] = matrix[0];
  2965. result[1] = matrix[1];
  2966. result[2] = matrix[2];
  2967. result[3] = matrix[3];
  2968. result[4] = matrix[4];
  2969. result[5] = matrix[5];
  2970. result[6] = matrix[6];
  2971. result[7] = matrix[7];
  2972. result[8] = matrix[8];
  2973. result[9] = matrix[9];
  2974. result[10] = matrix[10];
  2975. result[11] = matrix[11];
  2976. result[12] = tx;
  2977. result[13] = ty;
  2978. result[14] = tz;
  2979. result[15] = matrix[15];
  2980. return result;
  2981. };
  2982. var uniformScaleScratch = new Cartographic.Cartesian3();
  2983. /**
  2984. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  2985. * by an implicit uniform scale matrix. This is an optimization
  2986. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  2987. * <code>m</code> must be an affine matrix.
  2988. * This function performs fewer allocations and arithmetic operations.
  2989. *
  2990. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  2991. * @param {Number} scale The uniform scale on the right-hand side.
  2992. * @param {Matrix4} result The object onto which to store the result.
  2993. * @returns {Matrix4} The modified result parameter.
  2994. *
  2995. *
  2996. * @example
  2997. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromUniformScale(scale), m);
  2998. * Cesium.Matrix4.multiplyByUniformScale(m, scale, m);
  2999. *
  3000. * @see Matrix4.fromUniformScale
  3001. * @see Matrix4.multiplyByScale
  3002. */
  3003. Matrix4.multiplyByUniformScale = function(matrix, scale, result) {
  3004. //>>includeStart('debug', pragmas.debug);
  3005. Check.Check.typeOf.object('matrix', matrix);
  3006. Check.Check.typeOf.number('scale', scale);
  3007. Check.Check.typeOf.object('result', result);
  3008. //>>includeEnd('debug');
  3009. uniformScaleScratch.x = scale;
  3010. uniformScaleScratch.y = scale;
  3011. uniformScaleScratch.z = scale;
  3012. return Matrix4.multiplyByScale(matrix, uniformScaleScratch, result);
  3013. };
  3014. /**
  3015. * Multiplies an affine transformation matrix (with a bottom row of <code>[0.0, 0.0, 0.0, 1.0]</code>)
  3016. * by an implicit non-uniform scale matrix. This is an optimization
  3017. * for <code>Matrix4.multiply(m, Matrix4.fromUniformScale(scale), m);</code>, where
  3018. * <code>m</code> must be an affine matrix.
  3019. * This function performs fewer allocations and arithmetic operations.
  3020. *
  3021. * @param {Matrix4} matrix The affine matrix on the left-hand side.
  3022. * @param {Cartesian3} scale The non-uniform scale on the right-hand side.
  3023. * @param {Matrix4} result The object onto which to store the result.
  3024. * @returns {Matrix4} The modified result parameter.
  3025. *
  3026. *
  3027. * @example
  3028. * // Instead of Cesium.Matrix4.multiply(m, Cesium.Matrix4.fromScale(scale), m);
  3029. * Cesium.Matrix4.multiplyByScale(m, scale, m);
  3030. *
  3031. * @see Matrix4.fromScale
  3032. * @see Matrix4.multiplyByUniformScale
  3033. */
  3034. Matrix4.multiplyByScale = function(matrix, scale, result) {
  3035. //>>includeStart('debug', pragmas.debug);
  3036. Check.Check.typeOf.object('matrix', matrix);
  3037. Check.Check.typeOf.object('scale', scale);
  3038. Check.Check.typeOf.object('result', result);
  3039. //>>includeEnd('debug');
  3040. var scaleX = scale.x;
  3041. var scaleY = scale.y;
  3042. var scaleZ = scale.z;
  3043. // Faster than Cartesian3.equals
  3044. if ((scaleX === 1.0) && (scaleY === 1.0) && (scaleZ === 1.0)) {
  3045. return Matrix4.clone(matrix, result);
  3046. }
  3047. result[0] = scaleX * matrix[0];
  3048. result[1] = scaleX * matrix[1];
  3049. result[2] = scaleX * matrix[2];
  3050. result[3] = 0.0;
  3051. result[4] = scaleY * matrix[4];
  3052. result[5] = scaleY * matrix[5];
  3053. result[6] = scaleY * matrix[6];
  3054. result[7] = 0.0;
  3055. result[8] = scaleZ * matrix[8];
  3056. result[9] = scaleZ * matrix[9];
  3057. result[10] = scaleZ * matrix[10];
  3058. result[11] = 0.0;
  3059. result[12] = matrix[12];
  3060. result[13] = matrix[13];
  3061. result[14] = matrix[14];
  3062. result[15] = 1.0;
  3063. return result;
  3064. };
  3065. /**
  3066. * Computes the product of a matrix and a column vector.
  3067. *
  3068. * @param {Matrix4} matrix The matrix.
  3069. * @param {Cartesian4} cartesian The vector.
  3070. * @param {Cartesian4} result The object onto which to store the result.
  3071. * @returns {Cartesian4} The modified result parameter.
  3072. */
  3073. Matrix4.multiplyByVector = function(matrix, cartesian, result) {
  3074. //>>includeStart('debug', pragmas.debug);
  3075. Check.Check.typeOf.object('matrix', matrix);
  3076. Check.Check.typeOf.object('cartesian', cartesian);
  3077. Check.Check.typeOf.object('result', result);
  3078. //>>includeEnd('debug');
  3079. var vX = cartesian.x;
  3080. var vY = cartesian.y;
  3081. var vZ = cartesian.z;
  3082. var vW = cartesian.w;
  3083. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12] * vW;
  3084. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13] * vW;
  3085. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14] * vW;
  3086. var w = matrix[3] * vX + matrix[7] * vY + matrix[11] * vZ + matrix[15] * vW;
  3087. result.x = x;
  3088. result.y = y;
  3089. result.z = z;
  3090. result.w = w;
  3091. return result;
  3092. };
  3093. /**
  3094. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  3095. * with a {@link Cartesian4} with a <code>w</code> component of zero.
  3096. *
  3097. * @param {Matrix4} matrix The matrix.
  3098. * @param {Cartesian3} cartesian The point.
  3099. * @param {Cartesian3} result The object onto which to store the result.
  3100. * @returns {Cartesian3} The modified result parameter.
  3101. *
  3102. * @example
  3103. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  3104. * var result = Cesium.Matrix4.multiplyByPointAsVector(matrix, p, new Cesium.Cartesian3());
  3105. * // A shortcut for
  3106. * // Cartesian3 p = ...
  3107. * // Cesium.Matrix4.multiplyByVector(matrix, new Cesium.Cartesian4(p.x, p.y, p.z, 0.0), result);
  3108. */
  3109. Matrix4.multiplyByPointAsVector = function(matrix, cartesian, result) {
  3110. //>>includeStart('debug', pragmas.debug);
  3111. Check.Check.typeOf.object('matrix', matrix);
  3112. Check.Check.typeOf.object('cartesian', cartesian);
  3113. Check.Check.typeOf.object('result', result);
  3114. //>>includeEnd('debug');
  3115. var vX = cartesian.x;
  3116. var vY = cartesian.y;
  3117. var vZ = cartesian.z;
  3118. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ;
  3119. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ;
  3120. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ;
  3121. result.x = x;
  3122. result.y = y;
  3123. result.z = z;
  3124. return result;
  3125. };
  3126. /**
  3127. * Computes the product of a matrix and a {@link Cartesian3}. This is equivalent to calling {@link Matrix4.multiplyByVector}
  3128. * with a {@link Cartesian4} with a <code>w</code> component of 1, but returns a {@link Cartesian3} instead of a {@link Cartesian4}.
  3129. *
  3130. * @param {Matrix4} matrix The matrix.
  3131. * @param {Cartesian3} cartesian The point.
  3132. * @param {Cartesian3} result The object onto which to store the result.
  3133. * @returns {Cartesian3} The modified result parameter.
  3134. *
  3135. * @example
  3136. * var p = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  3137. * var result = Cesium.Matrix4.multiplyByPoint(matrix, p, new Cesium.Cartesian3());
  3138. */
  3139. Matrix4.multiplyByPoint = function(matrix, cartesian, result) {
  3140. //>>includeStart('debug', pragmas.debug);
  3141. Check.Check.typeOf.object('matrix', matrix);
  3142. Check.Check.typeOf.object('cartesian', cartesian);
  3143. Check.Check.typeOf.object('result', result);
  3144. //>>includeEnd('debug');
  3145. var vX = cartesian.x;
  3146. var vY = cartesian.y;
  3147. var vZ = cartesian.z;
  3148. var x = matrix[0] * vX + matrix[4] * vY + matrix[8] * vZ + matrix[12];
  3149. var y = matrix[1] * vX + matrix[5] * vY + matrix[9] * vZ + matrix[13];
  3150. var z = matrix[2] * vX + matrix[6] * vY + matrix[10] * vZ + matrix[14];
  3151. result.x = x;
  3152. result.y = y;
  3153. result.z = z;
  3154. return result;
  3155. };
  3156. /**
  3157. * Computes the product of a matrix and a scalar.
  3158. *
  3159. * @param {Matrix4} matrix The matrix.
  3160. * @param {Number} scalar The number to multiply by.
  3161. * @param {Matrix4} result The object onto which to store the result.
  3162. * @returns {Matrix4} The modified result parameter.
  3163. *
  3164. * @example
  3165. * //create a Matrix4 instance which is a scaled version of the supplied Matrix4
  3166. * // m = [10.0, 11.0, 12.0, 13.0]
  3167. * // [14.0, 15.0, 16.0, 17.0]
  3168. * // [18.0, 19.0, 20.0, 21.0]
  3169. * // [22.0, 23.0, 24.0, 25.0]
  3170. *
  3171. * var a = Cesium.Matrix4.multiplyByScalar(m, -2, new Cesium.Matrix4());
  3172. *
  3173. * // m remains the same
  3174. * // a = [-20.0, -22.0, -24.0, -26.0]
  3175. * // [-28.0, -30.0, -32.0, -34.0]
  3176. * // [-36.0, -38.0, -40.0, -42.0]
  3177. * // [-44.0, -46.0, -48.0, -50.0]
  3178. */
  3179. Matrix4.multiplyByScalar = function(matrix, scalar, result) {
  3180. //>>includeStart('debug', pragmas.debug);
  3181. Check.Check.typeOf.object('matrix', matrix);
  3182. Check.Check.typeOf.number('scalar', scalar);
  3183. Check.Check.typeOf.object('result', result);
  3184. //>>includeEnd('debug');
  3185. result[0] = matrix[0] * scalar;
  3186. result[1] = matrix[1] * scalar;
  3187. result[2] = matrix[2] * scalar;
  3188. result[3] = matrix[3] * scalar;
  3189. result[4] = matrix[4] * scalar;
  3190. result[5] = matrix[5] * scalar;
  3191. result[6] = matrix[6] * scalar;
  3192. result[7] = matrix[7] * scalar;
  3193. result[8] = matrix[8] * scalar;
  3194. result[9] = matrix[9] * scalar;
  3195. result[10] = matrix[10] * scalar;
  3196. result[11] = matrix[11] * scalar;
  3197. result[12] = matrix[12] * scalar;
  3198. result[13] = matrix[13] * scalar;
  3199. result[14] = matrix[14] * scalar;
  3200. result[15] = matrix[15] * scalar;
  3201. return result;
  3202. };
  3203. /**
  3204. * Compute the product of a matrix and a plane.
  3205. * @param matrix
  3206. * @param plane
  3207. * @param result
  3208. * @returns {*}
  3209. */
  3210. Matrix4.multiplyByPlane = function(matrix, plane, result) {
  3211. //>>includeStart('debug', pragmas.debug);
  3212. Check.Check.typeOf.object('matrix', matrix);
  3213. Check.Check.typeOf.object('plane', plane);
  3214. Check.Check.typeOf.object('result', result);
  3215. //>>includeEnd('debug');
  3216. var inverseMat = new Matrix4();
  3217. var invTrans = new Matrix4();
  3218. Matrix4.inverse(matrix, inverseMat);
  3219. Matrix4.transpose(inverseMat, invTrans);
  3220. var v4 = new Cartesian4.Cartesian4(plane.normal.x, plane.normal.y, plane.normal.z, plane.distance);
  3221. Matrix4.multiplyByVector(invTrans, v4, v4);
  3222. result.normal.x = v4.x;
  3223. result.normal.y = v4.y;
  3224. result.normal.z = v4.z;
  3225. var length = Cartographic.Cartesian3.magnitude(result.normal);
  3226. Cartographic.Cartesian3.normalize(result.normal, result.normal);
  3227. result.distance = v4.w / length;
  3228. return result;
  3229. };
  3230. /**
  3231. * Computes a negated copy of the provided matrix.
  3232. *
  3233. * @param {Matrix4} matrix The matrix to negate.
  3234. * @param {Matrix4} result The object onto which to store the result.
  3235. * @returns {Matrix4} The modified result parameter.
  3236. *
  3237. * @example
  3238. * //create a new Matrix4 instance which is a negation of a Matrix4
  3239. * // m = [10.0, 11.0, 12.0, 13.0]
  3240. * // [14.0, 15.0, 16.0, 17.0]
  3241. * // [18.0, 19.0, 20.0, 21.0]
  3242. * // [22.0, 23.0, 24.0, 25.0]
  3243. *
  3244. * var a = Cesium.Matrix4.negate(m, new Cesium.Matrix4());
  3245. *
  3246. * // m remains the same
  3247. * // a = [-10.0, -11.0, -12.0, -13.0]
  3248. * // [-14.0, -15.0, -16.0, -17.0]
  3249. * // [-18.0, -19.0, -20.0, -21.0]
  3250. * // [-22.0, -23.0, -24.0, -25.0]
  3251. */
  3252. Matrix4.negate = function(matrix, result) {
  3253. //>>includeStart('debug', pragmas.debug);
  3254. Check.Check.typeOf.object('matrix', matrix);
  3255. Check.Check.typeOf.object('result', result);
  3256. //>>includeEnd('debug');
  3257. result[0] = -matrix[0];
  3258. result[1] = -matrix[1];
  3259. result[2] = -matrix[2];
  3260. result[3] = -matrix[3];
  3261. result[4] = -matrix[4];
  3262. result[5] = -matrix[5];
  3263. result[6] = -matrix[6];
  3264. result[7] = -matrix[7];
  3265. result[8] = -matrix[8];
  3266. result[9] = -matrix[9];
  3267. result[10] = -matrix[10];
  3268. result[11] = -matrix[11];
  3269. result[12] = -matrix[12];
  3270. result[13] = -matrix[13];
  3271. result[14] = -matrix[14];
  3272. result[15] = -matrix[15];
  3273. return result;
  3274. };
  3275. /**
  3276. * Computes the transpose of the provided matrix.
  3277. *
  3278. * @param {Matrix4} matrix The matrix to transpose.
  3279. * @param {Matrix4} result The object onto which to store the result.
  3280. * @returns {Matrix4} The modified result parameter.
  3281. *
  3282. * @example
  3283. * //returns transpose of a Matrix4
  3284. * // m = [10.0, 11.0, 12.0, 13.0]
  3285. * // [14.0, 15.0, 16.0, 17.0]
  3286. * // [18.0, 19.0, 20.0, 21.0]
  3287. * // [22.0, 23.0, 24.0, 25.0]
  3288. *
  3289. * var a = Cesium.Matrix4.transpose(m, new Cesium.Matrix4());
  3290. *
  3291. * // m remains the same
  3292. * // a = [10.0, 14.0, 18.0, 22.0]
  3293. * // [11.0, 15.0, 19.0, 23.0]
  3294. * // [12.0, 16.0, 20.0, 24.0]
  3295. * // [13.0, 17.0, 21.0, 25.0]
  3296. */
  3297. Matrix4.transpose = function(matrix, result) {
  3298. //>>includeStart('debug', pragmas.debug);
  3299. Check.Check.typeOf.object('matrix', matrix);
  3300. Check.Check.typeOf.object('result', result);
  3301. //>>includeEnd('debug');
  3302. var matrix1 = matrix[1];
  3303. var matrix2 = matrix[2];
  3304. var matrix3 = matrix[3];
  3305. var matrix6 = matrix[6];
  3306. var matrix7 = matrix[7];
  3307. var matrix11 = matrix[11];
  3308. result[0] = matrix[0];
  3309. result[1] = matrix[4];
  3310. result[2] = matrix[8];
  3311. result[3] = matrix[12];
  3312. result[4] = matrix1;
  3313. result[5] = matrix[5];
  3314. result[6] = matrix[9];
  3315. result[7] = matrix[13];
  3316. result[8] = matrix2;
  3317. result[9] = matrix6;
  3318. result[10] = matrix[10];
  3319. result[11] = matrix[14];
  3320. result[12] = matrix3;
  3321. result[13] = matrix7;
  3322. result[14] = matrix11;
  3323. result[15] = matrix[15];
  3324. return result;
  3325. };
  3326. /**
  3327. * Computes a matrix, which contains the absolute (unsigned) values of the provided matrix's elements.
  3328. *
  3329. * @param {Matrix4} matrix The matrix with signed elements.
  3330. * @param {Matrix4} result The object onto which to store the result.
  3331. * @returns {Matrix4} The modified result parameter.
  3332. */
  3333. Matrix4.abs = function(matrix, result) {
  3334. //>>includeStart('debug', pragmas.debug);
  3335. Check.Check.typeOf.object('matrix', matrix);
  3336. Check.Check.typeOf.object('result', result);
  3337. //>>includeEnd('debug');
  3338. result[0] = Math.abs(matrix[0]);
  3339. result[1] = Math.abs(matrix[1]);
  3340. result[2] = Math.abs(matrix[2]);
  3341. result[3] = Math.abs(matrix[3]);
  3342. result[4] = Math.abs(matrix[4]);
  3343. result[5] = Math.abs(matrix[5]);
  3344. result[6] = Math.abs(matrix[6]);
  3345. result[7] = Math.abs(matrix[7]);
  3346. result[8] = Math.abs(matrix[8]);
  3347. result[9] = Math.abs(matrix[9]);
  3348. result[10] = Math.abs(matrix[10]);
  3349. result[11] = Math.abs(matrix[11]);
  3350. result[12] = Math.abs(matrix[12]);
  3351. result[13] = Math.abs(matrix[13]);
  3352. result[14] = Math.abs(matrix[14]);
  3353. result[15] = Math.abs(matrix[15]);
  3354. return result;
  3355. };
  3356. /**
  3357. * Compares the provided matrices componentwise and returns
  3358. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3359. *
  3360. * @param {Matrix4} [left] The first matrix.
  3361. * @param {Matrix4} [right] The second matrix.
  3362. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  3363. *
  3364. * @example
  3365. * //compares two Matrix4 instances
  3366. *
  3367. * // a = [10.0, 14.0, 18.0, 22.0]
  3368. * // [11.0, 15.0, 19.0, 23.0]
  3369. * // [12.0, 16.0, 20.0, 24.0]
  3370. * // [13.0, 17.0, 21.0, 25.0]
  3371. *
  3372. * // b = [10.0, 14.0, 18.0, 22.0]
  3373. * // [11.0, 15.0, 19.0, 23.0]
  3374. * // [12.0, 16.0, 20.0, 24.0]
  3375. * // [13.0, 17.0, 21.0, 25.0]
  3376. *
  3377. * if(Cesium.Matrix4.equals(a,b)) {
  3378. * console.log("Both matrices are equal");
  3379. * } else {
  3380. * console.log("They are not equal");
  3381. * }
  3382. *
  3383. * //Prints "Both matrices are equal" on the console
  3384. */
  3385. Matrix4.equals = function(left, right) {
  3386. // Given that most matrices will be transformation matrices, the elements
  3387. // are tested in order such that the test is likely to fail as early
  3388. // as possible. I _think_ this is just as friendly to the L1 cache
  3389. // as testing in index order. It is certainty faster in practice.
  3390. return (left === right) ||
  3391. (when.defined(left) &&
  3392. when.defined(right) &&
  3393. // Translation
  3394. left[12] === right[12] &&
  3395. left[13] === right[13] &&
  3396. left[14] === right[14] &&
  3397. // Rotation/scale
  3398. left[0] === right[0] &&
  3399. left[1] === right[1] &&
  3400. left[2] === right[2] &&
  3401. left[4] === right[4] &&
  3402. left[5] === right[5] &&
  3403. left[6] === right[6] &&
  3404. left[8] === right[8] &&
  3405. left[9] === right[9] &&
  3406. left[10] === right[10] &&
  3407. // Bottom row
  3408. left[3] === right[3] &&
  3409. left[7] === right[7] &&
  3410. left[11] === right[11] &&
  3411. left[15] === right[15]);
  3412. };
  3413. /**
  3414. * Compares the provided matrices componentwise and returns
  3415. * <code>true</code> if they are within the provided epsilon,
  3416. * <code>false</code> otherwise.
  3417. *
  3418. * @param {Matrix4} [left] The first matrix.
  3419. * @param {Matrix4} [right] The second matrix.
  3420. * @param {Number} epsilon The epsilon to use for equality testing.
  3421. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  3422. *
  3423. * @example
  3424. * //compares two Matrix4 instances
  3425. *
  3426. * // a = [10.5, 14.5, 18.5, 22.5]
  3427. * // [11.5, 15.5, 19.5, 23.5]
  3428. * // [12.5, 16.5, 20.5, 24.5]
  3429. * // [13.5, 17.5, 21.5, 25.5]
  3430. *
  3431. * // b = [10.0, 14.0, 18.0, 22.0]
  3432. * // [11.0, 15.0, 19.0, 23.0]
  3433. * // [12.0, 16.0, 20.0, 24.0]
  3434. * // [13.0, 17.0, 21.0, 25.0]
  3435. *
  3436. * if(Cesium.Matrix4.equalsEpsilon(a,b,0.1)){
  3437. * console.log("Difference between both the matrices is less than 0.1");
  3438. * } else {
  3439. * console.log("Difference between both the matrices is not less than 0.1");
  3440. * }
  3441. *
  3442. * //Prints "Difference between both the matrices is not less than 0.1" on the console
  3443. */
  3444. Matrix4.equalsEpsilon = function(left, right, epsilon) {
  3445. //>>includeStart('debug', pragmas.debug);
  3446. Check.Check.typeOf.number('epsilon', epsilon);
  3447. //>>includeEnd('debug');
  3448. return (left === right) ||
  3449. (when.defined(left) &&
  3450. when.defined(right) &&
  3451. Math.abs(left[0] - right[0]) <= epsilon &&
  3452. Math.abs(left[1] - right[1]) <= epsilon &&
  3453. Math.abs(left[2] - right[2]) <= epsilon &&
  3454. Math.abs(left[3] - right[3]) <= epsilon &&
  3455. Math.abs(left[4] - right[4]) <= epsilon &&
  3456. Math.abs(left[5] - right[5]) <= epsilon &&
  3457. Math.abs(left[6] - right[6]) <= epsilon &&
  3458. Math.abs(left[7] - right[7]) <= epsilon &&
  3459. Math.abs(left[8] - right[8]) <= epsilon &&
  3460. Math.abs(left[9] - right[9]) <= epsilon &&
  3461. Math.abs(left[10] - right[10]) <= epsilon &&
  3462. Math.abs(left[11] - right[11]) <= epsilon &&
  3463. Math.abs(left[12] - right[12]) <= epsilon &&
  3464. Math.abs(left[13] - right[13]) <= epsilon &&
  3465. Math.abs(left[14] - right[14]) <= epsilon &&
  3466. Math.abs(left[15] - right[15]) <= epsilon);
  3467. };
  3468. /**
  3469. * Gets the translation portion of the provided matrix, assuming the matrix is a affine transformation matrix.
  3470. *
  3471. * @param {Matrix4} matrix The matrix to use.
  3472. * @param {Cartesian3} result The object onto which to store the result.
  3473. * @returns {Cartesian3} The modified result parameter.
  3474. */
  3475. Matrix4.getTranslation = function(matrix, result) {
  3476. //>>includeStart('debug', pragmas.debug);
  3477. Check.Check.typeOf.object('matrix', matrix);
  3478. Check.Check.typeOf.object('result', result);
  3479. //>>includeEnd('debug');
  3480. result.x = matrix[12];
  3481. result.y = matrix[13];
  3482. result.z = matrix[14];
  3483. return result;
  3484. };
  3485. /**
  3486. * Gets the upper left 3x3 rotation matrix of the provided matrix, assuming the matrix is an affine transformation matrix.
  3487. *
  3488. * @param {Matrix4} matrix The matrix to use.
  3489. * @param {Matrix3} result The object onto which to store the result.
  3490. * @returns {Matrix3} The modified result parameter.
  3491. *
  3492. * @example
  3493. * // returns a Matrix3 instance from a Matrix4 instance
  3494. *
  3495. * // m = [10.0, 14.0, 18.0, 22.0]
  3496. * // [11.0, 15.0, 19.0, 23.0]
  3497. * // [12.0, 16.0, 20.0, 24.0]
  3498. * // [13.0, 17.0, 21.0, 25.0]
  3499. *
  3500. * var b = new Cesium.Matrix3();
  3501. * Cesium.Matrix4.getMatrix3(m,b);
  3502. *
  3503. * // b = [10.0, 14.0, 18.0]
  3504. * // [11.0, 15.0, 19.0]
  3505. * // [12.0, 16.0, 20.0]
  3506. */
  3507. Matrix4.getMatrix3 = function(matrix, result) {
  3508. //>>includeStart('debug', pragmas.debug);
  3509. Check.Check.typeOf.object('matrix', matrix);
  3510. Check.Check.typeOf.object('result', result);
  3511. //>>includeEnd('debug');
  3512. result[0] = matrix[0];
  3513. result[1] = matrix[1];
  3514. result[2] = matrix[2];
  3515. result[3] = matrix[4];
  3516. result[4] = matrix[5];
  3517. result[5] = matrix[6];
  3518. result[6] = matrix[8];
  3519. result[7] = matrix[9];
  3520. result[8] = matrix[10];
  3521. return result;
  3522. };
  3523. /**
  3524. * Gets the upper left 3x3 rotation matrix of the provided matrix, assuming the matrix is a affine transformation matrix.
  3525. *
  3526. * @param {Matrix4} matrix The matrix to use.
  3527. * @param {Matrix3} result The object onto which to store the result.
  3528. * @returns {Matrix3} The modified result parameter.
  3529. *
  3530. * @example
  3531. * // returns a Matrix3 instance from a Matrix4 instance
  3532. *
  3533. * // m = [10.0, 14.0, 18.0, 22.0]
  3534. * // [11.0, 15.0, 19.0, 23.0]
  3535. * // [12.0, 16.0, 20.0, 24.0]
  3536. * // [13.0, 17.0, 21.0, 25.0]
  3537. *
  3538. * var b = new Cesium.Matrix3();
  3539. * Cesium.Matrix4.getRotation(m,b);
  3540. *
  3541. * // b = [10.0, 14.0, 18.0]
  3542. * // [11.0, 15.0, 19.0]
  3543. * // [12.0, 16.0, 20.0]
  3544. */
  3545. Matrix4.getRotation = function(matrix, result) {
  3546. //>>includeStart('debug', pragmas.debug);
  3547. Check.Check.typeOf.object('matrix', matrix);
  3548. Check.Check.typeOf.object('result', result);
  3549. //>>includeEnd('debug');
  3550. result[0] = matrix[0];
  3551. result[1] = matrix[1];
  3552. result[2] = matrix[2];
  3553. result[3] = matrix[4];
  3554. result[4] = matrix[5];
  3555. result[5] = matrix[6];
  3556. result[6] = matrix[8];
  3557. result[7] = matrix[9];
  3558. result[8] = matrix[10];
  3559. return result;
  3560. };
  3561. var scratchInverseRotation = new Matrix3();
  3562. var scratchMatrix3Zero = new Matrix3();
  3563. var scratchBottomRow = new Cartesian4.Cartesian4();
  3564. var scratchExpectedBottomRow = new Cartesian4.Cartesian4(0.0, 0.0, 0.0, 1.0);
  3565. /**
  3566. * Computes the inverse of the provided matrix using Cramers Rule.
  3567. * If the determinant is zero, the matrix can not be inverted, and an exception is thrown.
  3568. * If the matrix is an affine transformation matrix, it is more efficient
  3569. * to invert it with {@link Matrix4.inverseTransformation}.
  3570. *
  3571. * @param {Matrix4} matrix The matrix to invert.
  3572. * @param {Matrix4} result The object onto which to store the result.
  3573. * @returns {Matrix4} The modified result parameter.
  3574. *
  3575. * @exception {RuntimeError} matrix is not invertible because its determinate is zero.
  3576. */
  3577. Matrix4.inverse = function(matrix, result) {
  3578. //>>includeStart('debug', pragmas.debug);
  3579. Check.Check.typeOf.object('matrix', matrix);
  3580. Check.Check.typeOf.object('result', result);
  3581. //>>includeEnd('debug');
  3582. //
  3583. // Ported from:
  3584. // ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
  3585. //
  3586. var src0 = matrix[0];
  3587. var src1 = matrix[4];
  3588. var src2 = matrix[8];
  3589. var src3 = matrix[12];
  3590. var src4 = matrix[1];
  3591. var src5 = matrix[5];
  3592. var src6 = matrix[9];
  3593. var src7 = matrix[13];
  3594. var src8 = matrix[2];
  3595. var src9 = matrix[6];
  3596. var src10 = matrix[10];
  3597. var src11 = matrix[14];
  3598. var src12 = matrix[3];
  3599. var src13 = matrix[7];
  3600. var src14 = matrix[11];
  3601. var src15 = matrix[15];
  3602. // calculate pairs for first 8 elements (cofactors)
  3603. var tmp0 = src10 * src15;
  3604. var tmp1 = src11 * src14;
  3605. var tmp2 = src9 * src15;
  3606. var tmp3 = src11 * src13;
  3607. var tmp4 = src9 * src14;
  3608. var tmp5 = src10 * src13;
  3609. var tmp6 = src8 * src15;
  3610. var tmp7 = src11 * src12;
  3611. var tmp8 = src8 * src14;
  3612. var tmp9 = src10 * src12;
  3613. var tmp10 = src8 * src13;
  3614. var tmp11 = src9 * src12;
  3615. // calculate first 8 elements (cofactors)
  3616. var dst0 = (tmp0 * src5 + tmp3 * src6 + tmp4 * src7) - (tmp1 * src5 + tmp2 * src6 + tmp5 * src7);
  3617. var dst1 = (tmp1 * src4 + tmp6 * src6 + tmp9 * src7) - (tmp0 * src4 + tmp7 * src6 + tmp8 * src7);
  3618. var dst2 = (tmp2 * src4 + tmp7 * src5 + tmp10 * src7) - (tmp3 * src4 + tmp6 * src5 + tmp11 * src7);
  3619. var dst3 = (tmp5 * src4 + tmp8 * src5 + tmp11 * src6) - (tmp4 * src4 + tmp9 * src5 + tmp10 * src6);
  3620. var dst4 = (tmp1 * src1 + tmp2 * src2 + tmp5 * src3) - (tmp0 * src1 + tmp3 * src2 + tmp4 * src3);
  3621. var dst5 = (tmp0 * src0 + tmp7 * src2 + tmp8 * src3) - (tmp1 * src0 + tmp6 * src2 + tmp9 * src3);
  3622. var dst6 = (tmp3 * src0 + tmp6 * src1 + tmp11 * src3) - (tmp2 * src0 + tmp7 * src1 + tmp10 * src3);
  3623. var dst7 = (tmp4 * src0 + tmp9 * src1 + tmp10 * src2) - (tmp5 * src0 + tmp8 * src1 + tmp11 * src2);
  3624. // calculate pairs for second 8 elements (cofactors)
  3625. tmp0 = src2 * src7;
  3626. tmp1 = src3 * src6;
  3627. tmp2 = src1 * src7;
  3628. tmp3 = src3 * src5;
  3629. tmp4 = src1 * src6;
  3630. tmp5 = src2 * src5;
  3631. tmp6 = src0 * src7;
  3632. tmp7 = src3 * src4;
  3633. tmp8 = src0 * src6;
  3634. tmp9 = src2 * src4;
  3635. tmp10 = src0 * src5;
  3636. tmp11 = src1 * src4;
  3637. // calculate second 8 elements (cofactors)
  3638. var dst8 = (tmp0 * src13 + tmp3 * src14 + tmp4 * src15) - (tmp1 * src13 + tmp2 * src14 + tmp5 * src15);
  3639. var dst9 = (tmp1 * src12 + tmp6 * src14 + tmp9 * src15) - (tmp0 * src12 + tmp7 * src14 + tmp8 * src15);
  3640. var dst10 = (tmp2 * src12 + tmp7 * src13 + tmp10 * src15) - (tmp3 * src12 + tmp6 * src13 + tmp11 * src15);
  3641. var dst11 = (tmp5 * src12 + tmp8 * src13 + tmp11 * src14) - (tmp4 * src12 + tmp9 * src13 + tmp10 * src14);
  3642. var dst12 = (tmp2 * src10 + tmp5 * src11 + tmp1 * src9) - (tmp4 * src11 + tmp0 * src9 + tmp3 * src10);
  3643. var dst13 = (tmp8 * src11 + tmp0 * src8 + tmp7 * src10) - (tmp6 * src10 + tmp9 * src11 + tmp1 * src8);
  3644. var dst14 = (tmp6 * src9 + tmp11 * src11 + tmp3 * src8) - (tmp10 * src11 + tmp2 * src8 + tmp7 * src9);
  3645. var dst15 = (tmp10 * src10 + tmp4 * src8 + tmp9 * src9) - (tmp8 * src9 + tmp11 * src10 + tmp5 * src8);
  3646. // calculate determinant
  3647. var det = src0 * dst0 + src1 * dst1 + src2 * dst2 + src3 * dst3;
  3648. if (Math.abs(det) < _Math.CesiumMath.EPSILON21) {
  3649. // Special case for a zero scale matrix that can occur, for example,
  3650. // when a model's node has a [0, 0, 0] scale.
  3651. if (Matrix3.equalsEpsilon(Matrix4.getRotation(matrix, scratchInverseRotation), scratchMatrix3Zero, _Math.CesiumMath.EPSILON7) &&
  3652. Cartesian4.Cartesian4.equals(Matrix4.getRow(matrix, 3, scratchBottomRow), scratchExpectedBottomRow)) {
  3653. result[0] = 0.0;
  3654. result[1] = 0.0;
  3655. result[2] = 0.0;
  3656. result[3] = 0.0;
  3657. result[4] = 0.0;
  3658. result[5] = 0.0;
  3659. result[6] = 0.0;
  3660. result[7] = 0.0;
  3661. result[8] = 0.0;
  3662. result[9] = 0.0;
  3663. result[10] = 0.0;
  3664. result[11] = 0.0;
  3665. result[12] = -matrix[12];
  3666. result[13] = -matrix[13];
  3667. result[14] = -matrix[14];
  3668. result[15] = 1.0;
  3669. return result;
  3670. }
  3671. throw new RuntimeError.RuntimeError('matrix is not invertible because its determinate is zero.');
  3672. }
  3673. // calculate matrix inverse
  3674. det = 1.0 / det;
  3675. result[0] = dst0 * det;
  3676. result[1] = dst1 * det;
  3677. result[2] = dst2 * det;
  3678. result[3] = dst3 * det;
  3679. result[4] = dst4 * det;
  3680. result[5] = dst5 * det;
  3681. result[6] = dst6 * det;
  3682. result[7] = dst7 * det;
  3683. result[8] = dst8 * det;
  3684. result[9] = dst9 * det;
  3685. result[10] = dst10 * det;
  3686. result[11] = dst11 * det;
  3687. result[12] = dst12 * det;
  3688. result[13] = dst13 * det;
  3689. result[14] = dst14 * det;
  3690. result[15] = dst15 * det;
  3691. return result;
  3692. };
  3693. /**
  3694. * Computes the inverse of the provided matrix assuming it is
  3695. * an affine transformation matrix, where the upper left 3x3 elements
  3696. * are a rotation matrix, and the upper three elements in the fourth
  3697. * column are the translation. The bottom row is assumed to be [0, 0, 0, 1].
  3698. * The matrix is not verified to be in the proper form.
  3699. * This method is faster than computing the inverse for a general 4x4
  3700. * matrix using {@link Matrix4.inverse}.
  3701. *
  3702. * @param {Matrix4} matrix The matrix to invert.
  3703. * @param {Matrix4} result The object onto which to store the result.
  3704. * @returns {Matrix4} The modified result parameter.
  3705. */
  3706. Matrix4.inverseTransformation = function(matrix, result) {
  3707. //>>includeStart('debug', pragmas.debug);
  3708. Check.Check.typeOf.object('matrix', matrix);
  3709. Check.Check.typeOf.object('result', result);
  3710. //>>includeEnd('debug');
  3711. //This function is an optimized version of the below 4 lines.
  3712. //var rT = Matrix3.transpose(Matrix4.getRotation(matrix));
  3713. //var rTN = Matrix3.negate(rT);
  3714. //var rTT = Matrix3.multiplyByVector(rTN, Matrix4.getTranslation(matrix));
  3715. //return Matrix4.fromRotationTranslation(rT, rTT, result);
  3716. var matrix0 = matrix[0];
  3717. var matrix1 = matrix[1];
  3718. var matrix2 = matrix[2];
  3719. var matrix4 = matrix[4];
  3720. var matrix5 = matrix[5];
  3721. var matrix6 = matrix[6];
  3722. var matrix8 = matrix[8];
  3723. var matrix9 = matrix[9];
  3724. var matrix10 = matrix[10];
  3725. var vX = matrix[12];
  3726. var vY = matrix[13];
  3727. var vZ = matrix[14];
  3728. var x = -matrix0 * vX - matrix1 * vY - matrix2 * vZ;
  3729. var y = -matrix4 * vX - matrix5 * vY - matrix6 * vZ;
  3730. var z = -matrix8 * vX - matrix9 * vY - matrix10 * vZ;
  3731. result[0] = matrix0;
  3732. result[1] = matrix4;
  3733. result[2] = matrix8;
  3734. result[3] = 0.0;
  3735. result[4] = matrix1;
  3736. result[5] = matrix5;
  3737. result[6] = matrix9;
  3738. result[7] = 0.0;
  3739. result[8] = matrix2;
  3740. result[9] = matrix6;
  3741. result[10] = matrix10;
  3742. result[11] = 0.0;
  3743. result[12] = x;
  3744. result[13] = y;
  3745. result[14] = z;
  3746. result[15] = 1.0;
  3747. return result;
  3748. };
  3749. /**
  3750. * An immutable Matrix4 instance initialized to the identity matrix.
  3751. *
  3752. * @type {Matrix4}
  3753. * @constant
  3754. */
  3755. Matrix4.IDENTITY = Object.freeze(new Matrix4(1.0, 0.0, 0.0, 0.0,
  3756. 0.0, 1.0, 0.0, 0.0,
  3757. 0.0, 0.0, 1.0, 0.0,
  3758. 0.0, 0.0, 0.0, 1.0));
  3759. /**
  3760. * An immutable Matrix4 instance initialized to the zero matrix.
  3761. *
  3762. * @type {Matrix4}
  3763. * @constant
  3764. */
  3765. Matrix4.ZERO = Object.freeze(new Matrix4(0.0, 0.0, 0.0, 0.0,
  3766. 0.0, 0.0, 0.0, 0.0,
  3767. 0.0, 0.0, 0.0, 0.0,
  3768. 0.0, 0.0, 0.0, 0.0));
  3769. /**
  3770. * The index into Matrix4 for column 0, row 0.
  3771. *
  3772. * @type {Number}
  3773. * @constant
  3774. */
  3775. Matrix4.COLUMN0ROW0 = 0;
  3776. /**
  3777. * The index into Matrix4 for column 0, row 1.
  3778. *
  3779. * @type {Number}
  3780. * @constant
  3781. */
  3782. Matrix4.COLUMN0ROW1 = 1;
  3783. /**
  3784. * The index into Matrix4 for column 0, row 2.
  3785. *
  3786. * @type {Number}
  3787. * @constant
  3788. */
  3789. Matrix4.COLUMN0ROW2 = 2;
  3790. /**
  3791. * The index into Matrix4 for column 0, row 3.
  3792. *
  3793. * @type {Number}
  3794. * @constant
  3795. */
  3796. Matrix4.COLUMN0ROW3 = 3;
  3797. /**
  3798. * The index into Matrix4 for column 1, row 0.
  3799. *
  3800. * @type {Number}
  3801. * @constant
  3802. */
  3803. Matrix4.COLUMN1ROW0 = 4;
  3804. /**
  3805. * The index into Matrix4 for column 1, row 1.
  3806. *
  3807. * @type {Number}
  3808. * @constant
  3809. */
  3810. Matrix4.COLUMN1ROW1 = 5;
  3811. /**
  3812. * The index into Matrix4 for column 1, row 2.
  3813. *
  3814. * @type {Number}
  3815. * @constant
  3816. */
  3817. Matrix4.COLUMN1ROW2 = 6;
  3818. /**
  3819. * The index into Matrix4 for column 1, row 3.
  3820. *
  3821. * @type {Number}
  3822. * @constant
  3823. */
  3824. Matrix4.COLUMN1ROW3 = 7;
  3825. /**
  3826. * The index into Matrix4 for column 2, row 0.
  3827. *
  3828. * @type {Number}
  3829. * @constant
  3830. */
  3831. Matrix4.COLUMN2ROW0 = 8;
  3832. /**
  3833. * The index into Matrix4 for column 2, row 1.
  3834. *
  3835. * @type {Number}
  3836. * @constant
  3837. */
  3838. Matrix4.COLUMN2ROW1 = 9;
  3839. /**
  3840. * The index into Matrix4 for column 2, row 2.
  3841. *
  3842. * @type {Number}
  3843. * @constant
  3844. */
  3845. Matrix4.COLUMN2ROW2 = 10;
  3846. /**
  3847. * The index into Matrix4 for column 2, row 3.
  3848. *
  3849. * @type {Number}
  3850. * @constant
  3851. */
  3852. Matrix4.COLUMN2ROW3 = 11;
  3853. /**
  3854. * The index into Matrix4 for column 3, row 0.
  3855. *
  3856. * @type {Number}
  3857. * @constant
  3858. */
  3859. Matrix4.COLUMN3ROW0 = 12;
  3860. /**
  3861. * The index into Matrix4 for column 3, row 1.
  3862. *
  3863. * @type {Number}
  3864. * @constant
  3865. */
  3866. Matrix4.COLUMN3ROW1 = 13;
  3867. /**
  3868. * The index into Matrix4 for column 3, row 2.
  3869. *
  3870. * @type {Number}
  3871. * @constant
  3872. */
  3873. Matrix4.COLUMN3ROW2 = 14;
  3874. /**
  3875. * The index into Matrix4 for column 3, row 3.
  3876. *
  3877. * @type {Number}
  3878. * @constant
  3879. */
  3880. Matrix4.COLUMN3ROW3 = 15;
  3881. Object.defineProperties(Matrix4.prototype, {
  3882. /**
  3883. * Gets the number of items in the collection.
  3884. * @memberof Matrix4.prototype
  3885. *
  3886. * @type {Number}
  3887. */
  3888. length : {
  3889. get : function() {
  3890. return Matrix4.packedLength;
  3891. }
  3892. }
  3893. });
  3894. /**
  3895. * Duplicates the provided Matrix4 instance.
  3896. *
  3897. * @param {Matrix4} [result] The object onto which to store the result.
  3898. * @returns {Matrix4} The modified result parameter or a new Matrix4 instance if one was not provided.
  3899. */
  3900. Matrix4.prototype.clone = function(result) {
  3901. return Matrix4.clone(this, result);
  3902. };
  3903. /**
  3904. * Compares this matrix to the provided matrix componentwise and returns
  3905. * <code>true</code> if they are equal, <code>false</code> otherwise.
  3906. *
  3907. * @param {Matrix4} [right] The right hand side matrix.
  3908. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  3909. */
  3910. Matrix4.prototype.equals = function(right) {
  3911. return Matrix4.equals(this, right);
  3912. };
  3913. /**
  3914. * @private
  3915. */
  3916. Matrix4.equalsArray = function(matrix, array, offset) {
  3917. return matrix[0] === array[offset] &&
  3918. matrix[1] === array[offset + 1] &&
  3919. matrix[2] === array[offset + 2] &&
  3920. matrix[3] === array[offset + 3] &&
  3921. matrix[4] === array[offset + 4] &&
  3922. matrix[5] === array[offset + 5] &&
  3923. matrix[6] === array[offset + 6] &&
  3924. matrix[7] === array[offset + 7] &&
  3925. matrix[8] === array[offset + 8] &&
  3926. matrix[9] === array[offset + 9] &&
  3927. matrix[10] === array[offset + 10] &&
  3928. matrix[11] === array[offset + 11] &&
  3929. matrix[12] === array[offset + 12] &&
  3930. matrix[13] === array[offset + 13] &&
  3931. matrix[14] === array[offset + 14] &&
  3932. matrix[15] === array[offset + 15];
  3933. };
  3934. /**
  3935. * Compares this matrix to the provided matrix componentwise and returns
  3936. * <code>true</code> if they are within the provided epsilon,
  3937. * <code>false</code> otherwise.
  3938. *
  3939. * @param {Matrix4} [right] The right hand side matrix.
  3940. * @param {Number} epsilon The epsilon to use for equality testing.
  3941. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  3942. */
  3943. Matrix4.prototype.equalsEpsilon = function(right, epsilon) {
  3944. return Matrix4.equalsEpsilon(this, right, epsilon);
  3945. };
  3946. /**
  3947. * Computes a string representing this Matrix with each row being
  3948. * on a separate line and in the format '(column0, column1, column2, column3)'.
  3949. *
  3950. * @returns {String} A string representing the provided Matrix with each row being on a separate line and in the format '(column0, column1, column2, column3)'.
  3951. */
  3952. Matrix4.prototype.toString = function() {
  3953. return '(' + this[0] + ', ' + this[4] + ', ' + this[8] + ', ' + this[12] +')\n' +
  3954. '(' + this[1] + ', ' + this[5] + ', ' + this[9] + ', ' + this[13] +')\n' +
  3955. '(' + this[2] + ', ' + this[6] + ', ' + this[10] + ', ' + this[14] +')\n' +
  3956. '(' + this[3] + ', ' + this[7] + ', ' + this[11] + ', ' + this[15] +')';
  3957. };
  3958. /**
  3959. * A bounding sphere with a center and a radius.
  3960. * @alias BoundingSphere
  3961. * @constructor
  3962. *
  3963. * @param {Cartesian3} [center=Cartesian3.ZERO] The center of the bounding sphere.
  3964. * @param {Number} [radius=0.0] The radius of the bounding sphere.
  3965. *
  3966. * @see AxisAlignedBoundingBox
  3967. * @see BoundingRectangle
  3968. * @see Packable
  3969. */
  3970. function BoundingSphere(center, radius) {
  3971. /**
  3972. * The center point of the sphere.
  3973. * @type {Cartesian3}
  3974. * @default {@link Cartesian3.ZERO}
  3975. */
  3976. this.center = Cartographic.Cartesian3.clone(when.defaultValue(center, Cartographic.Cartesian3.ZERO));
  3977. /**
  3978. * The radius of the sphere.
  3979. * @type {Number}
  3980. * @default 0.0
  3981. */
  3982. this.radius = when.defaultValue(radius, 0.0);
  3983. }
  3984. var fromPointsXMin = new Cartographic.Cartesian3();
  3985. var fromPointsYMin = new Cartographic.Cartesian3();
  3986. var fromPointsZMin = new Cartographic.Cartesian3();
  3987. var fromPointsXMax = new Cartographic.Cartesian3();
  3988. var fromPointsYMax = new Cartographic.Cartesian3();
  3989. var fromPointsZMax = new Cartographic.Cartesian3();
  3990. var fromPointsCurrentPos = new Cartographic.Cartesian3();
  3991. var fromPointsScratch = new Cartographic.Cartesian3();
  3992. var fromPointsRitterCenter = new Cartographic.Cartesian3();
  3993. var fromPointsMinBoxPt = new Cartographic.Cartesian3();
  3994. var fromPointsMaxBoxPt = new Cartographic.Cartesian3();
  3995. var fromPointsNaiveCenterScratch = new Cartographic.Cartesian3();
  3996. var volumeConstant = (4.0 / 3.0) * _Math.CesiumMath.PI;
  3997. /**
  3998. * Computes a tight-fitting bounding sphere enclosing a list of 3D Cartesian points.
  3999. * The bounding sphere is computed by running two algorithms, a naive algorithm and
  4000. * Ritter's algorithm. The smaller of the two spheres is used to ensure a tight fit.
  4001. *
  4002. * @param {Cartesian3[]} [positions] An array of points that the bounding sphere will enclose. Each point must have <code>x</code>, <code>y</code>, and <code>z</code> properties.
  4003. * @param {BoundingSphere} [result] The object onto which to store the result.
  4004. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  4005. *
  4006. * @see {@link http://help.agi.com/AGIComponents/html/BlogBoundingSphere.htm|Bounding Sphere computation article}
  4007. */
  4008. BoundingSphere.fromPoints = function(positions, result) {
  4009. if (!when.defined(result)) {
  4010. result = new BoundingSphere();
  4011. }
  4012. if (!when.defined(positions) || positions.length === 0) {
  4013. result.center = Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, result.center);
  4014. result.radius = 0.0;
  4015. return result;
  4016. }
  4017. var currentPos = Cartographic.Cartesian3.clone(positions[0], fromPointsCurrentPos);
  4018. var xMin = Cartographic.Cartesian3.clone(currentPos, fromPointsXMin);
  4019. var yMin = Cartographic.Cartesian3.clone(currentPos, fromPointsYMin);
  4020. var zMin = Cartographic.Cartesian3.clone(currentPos, fromPointsZMin);
  4021. var xMax = Cartographic.Cartesian3.clone(currentPos, fromPointsXMax);
  4022. var yMax = Cartographic.Cartesian3.clone(currentPos, fromPointsYMax);
  4023. var zMax = Cartographic.Cartesian3.clone(currentPos, fromPointsZMax);
  4024. var numPositions = positions.length;
  4025. var i;
  4026. for (i = 1; i < numPositions; i++) {
  4027. Cartographic.Cartesian3.clone(positions[i], currentPos);
  4028. var x = currentPos.x;
  4029. var y = currentPos.y;
  4030. var z = currentPos.z;
  4031. // Store points containing the the smallest and largest components
  4032. if (x < xMin.x) {
  4033. Cartographic.Cartesian3.clone(currentPos, xMin);
  4034. }
  4035. if (x > xMax.x) {
  4036. Cartographic.Cartesian3.clone(currentPos, xMax);
  4037. }
  4038. if (y < yMin.y) {
  4039. Cartographic.Cartesian3.clone(currentPos, yMin);
  4040. }
  4041. if (y > yMax.y) {
  4042. Cartographic.Cartesian3.clone(currentPos, yMax);
  4043. }
  4044. if (z < zMin.z) {
  4045. Cartographic.Cartesian3.clone(currentPos, zMin);
  4046. }
  4047. if (z > zMax.z) {
  4048. Cartographic.Cartesian3.clone(currentPos, zMax);
  4049. }
  4050. }
  4051. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  4052. var xSpan = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  4053. var ySpan = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  4054. var zSpan = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  4055. // Set the diameter endpoints to the largest span.
  4056. var diameter1 = xMin;
  4057. var diameter2 = xMax;
  4058. var maxSpan = xSpan;
  4059. if (ySpan > maxSpan) {
  4060. maxSpan = ySpan;
  4061. diameter1 = yMin;
  4062. diameter2 = yMax;
  4063. }
  4064. if (zSpan > maxSpan) {
  4065. maxSpan = zSpan;
  4066. diameter1 = zMin;
  4067. diameter2 = zMax;
  4068. }
  4069. // Calculate the center of the initial sphere found by Ritter's algorithm
  4070. var ritterCenter = fromPointsRitterCenter;
  4071. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  4072. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  4073. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  4074. // Calculate the radius of the initial sphere found by Ritter's algorithm
  4075. var radiusSquared = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  4076. var ritterRadius = Math.sqrt(radiusSquared);
  4077. // Find the center of the sphere found using the Naive method.
  4078. var minBoxPt = fromPointsMinBoxPt;
  4079. minBoxPt.x = xMin.x;
  4080. minBoxPt.y = yMin.y;
  4081. minBoxPt.z = zMin.z;
  4082. var maxBoxPt = fromPointsMaxBoxPt;
  4083. maxBoxPt.x = xMax.x;
  4084. maxBoxPt.y = yMax.y;
  4085. maxBoxPt.z = zMax.z;
  4086. var naiveCenter = Cartographic.Cartesian3.midpoint(minBoxPt, maxBoxPt, fromPointsNaiveCenterScratch);
  4087. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  4088. var naiveRadius = 0;
  4089. for (i = 0; i < numPositions; i++) {
  4090. Cartographic.Cartesian3.clone(positions[i], currentPos);
  4091. // Find the furthest point from the naive center to calculate the naive radius.
  4092. var r = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  4093. if (r > naiveRadius) {
  4094. naiveRadius = r;
  4095. }
  4096. // Make adjustments to the Ritter Sphere to include all points.
  4097. var oldCenterToPointSquared = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  4098. if (oldCenterToPointSquared > radiusSquared) {
  4099. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  4100. // Calculate new radius to include the point that lies outside
  4101. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  4102. radiusSquared = ritterRadius * ritterRadius;
  4103. // Calculate center of new Ritter sphere
  4104. var oldToNew = oldCenterToPoint - ritterRadius;
  4105. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  4106. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  4107. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  4108. }
  4109. }
  4110. if (ritterRadius < naiveRadius) {
  4111. Cartographic.Cartesian3.clone(ritterCenter, result.center);
  4112. result.radius = ritterRadius;
  4113. } else {
  4114. Cartographic.Cartesian3.clone(naiveCenter, result.center);
  4115. result.radius = naiveRadius;
  4116. }
  4117. return result;
  4118. };
  4119. var defaultProjection = new GeographicProjection();
  4120. var fromRectangle2DLowerLeft = new Cartographic.Cartesian3();
  4121. var fromRectangle2DUpperRight = new Cartographic.Cartesian3();
  4122. var fromRectangle2DSouthwest = new Cartographic.Cartographic();
  4123. var fromRectangle2DNortheast = new Cartographic.Cartographic();
  4124. /**
  4125. * Computes a bounding sphere from a rectangle projected in 2D.
  4126. *
  4127. * @param {Rectangle} [rectangle] The rectangle around which to create a bounding sphere.
  4128. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  4129. * @param {BoundingSphere} [result] The object onto which to store the result.
  4130. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4131. */
  4132. BoundingSphere.fromRectangle2D = function(rectangle, projection, result) {
  4133. return BoundingSphere.fromRectangleWithHeights2D(rectangle, projection, 0.0, 0.0, result);
  4134. };
  4135. /**
  4136. * Computes a bounding sphere from a rectangle projected in 2D. The bounding sphere accounts for the
  4137. * object's minimum and maximum heights over the rectangle.
  4138. *
  4139. * @param {Rectangle} [rectangle] The rectangle around which to create a bounding sphere.
  4140. * @param {Object} [projection=GeographicProjection] The projection used to project the rectangle into 2D.
  4141. * @param {Number} [minimumHeight=0.0] The minimum height over the rectangle.
  4142. * @param {Number} [maximumHeight=0.0] The maximum height over the rectangle.
  4143. * @param {BoundingSphere} [result] The object onto which to store the result.
  4144. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4145. */
  4146. BoundingSphere.fromRectangleWithHeights2D = function(rectangle, projection, minimumHeight, maximumHeight, result) {
  4147. if (!when.defined(result)) {
  4148. result = new BoundingSphere();
  4149. }
  4150. if (!when.defined(rectangle)) {
  4151. result.center = Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, result.center);
  4152. result.radius = 0.0;
  4153. return result;
  4154. }
  4155. projection = when.defaultValue(projection, defaultProjection);
  4156. Cartesian2.Rectangle.southwest(rectangle, fromRectangle2DSouthwest);
  4157. fromRectangle2DSouthwest.height = minimumHeight;
  4158. Cartesian2.Rectangle.northeast(rectangle, fromRectangle2DNortheast);
  4159. fromRectangle2DNortheast.height = maximumHeight;
  4160. var lowerLeft = projection.project(fromRectangle2DSouthwest, fromRectangle2DLowerLeft);
  4161. var upperRight = projection.project(fromRectangle2DNortheast, fromRectangle2DUpperRight);
  4162. var width = upperRight.x - lowerLeft.x;
  4163. var height = upperRight.y - lowerLeft.y;
  4164. var elevation = upperRight.z - lowerLeft.z;
  4165. result.radius = Math.sqrt(width * width + height * height + elevation * elevation) * 0.5;
  4166. var center = result.center;
  4167. center.x = lowerLeft.x + width * 0.5;
  4168. center.y = lowerLeft.y + height * 0.5;
  4169. center.z = lowerLeft.z + elevation * 0.5;
  4170. return result;
  4171. };
  4172. var fromRectangle3DScratch = [];
  4173. /**
  4174. * Computes a bounding sphere from a rectangle in 3D. The bounding sphere is created using a subsample of points
  4175. * on the ellipsoid and contained in the rectangle. It may not be accurate for all rectangles on all types of ellipsoids.
  4176. *
  4177. * @param {Rectangle} [rectangle] The valid rectangle used to create a bounding sphere.
  4178. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid used to determine positions of the rectangle.
  4179. * @param {Number} [surfaceHeight=0.0] The height above the surface of the ellipsoid.
  4180. * @param {BoundingSphere} [result] The object onto which to store the result.
  4181. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4182. */
  4183. BoundingSphere.fromRectangle3D = function(rectangle, ellipsoid, surfaceHeight, result) {
  4184. ellipsoid = when.defaultValue(ellipsoid, Cartesian2.Ellipsoid.WGS84);
  4185. surfaceHeight = when.defaultValue(surfaceHeight, 0.0);
  4186. if (!when.defined(result)) {
  4187. result = new BoundingSphere();
  4188. }
  4189. if (!when.defined(rectangle)) {
  4190. result.center = Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, result.center);
  4191. result.radius = 0.0;
  4192. return result;
  4193. }
  4194. var positions = Cartesian2.Rectangle.subsample(rectangle, ellipsoid, surfaceHeight, fromRectangle3DScratch);
  4195. return BoundingSphere.fromPoints(positions, result);
  4196. };
  4197. /**
  4198. * Computes a tight-fitting bounding sphere enclosing a list of 3D points, where the points are
  4199. * stored in a flat array in X, Y, Z, order. The bounding sphere is computed by running two
  4200. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  4201. * ensure a tight fit.
  4202. *
  4203. * @param {Number[]} [positions] An array of points that the bounding sphere will enclose. Each point
  4204. * is formed from three elements in the array in the order X, Y, Z.
  4205. * @param {Cartesian3} [center=Cartesian3.ZERO] The position to which the positions are relative, which need not be the
  4206. * origin of the coordinate system. This is useful when the positions are to be used for
  4207. * relative-to-center (RTC) rendering.
  4208. * @param {Number} [stride=3] The number of array elements per vertex. It must be at least 3, but it may
  4209. * be higher. Regardless of the value of this parameter, the X coordinate of the first position
  4210. * is at array index 0, the Y coordinate is at array index 1, and the Z coordinate is at array index
  4211. * 2. When stride is 3, the X coordinate of the next position then begins at array index 3. If
  4212. * the stride is 5, however, two array elements are skipped and the next position begins at array
  4213. * index 5.
  4214. * @param {BoundingSphere} [result] The object onto which to store the result.
  4215. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  4216. *
  4217. * @example
  4218. * // Compute the bounding sphere from 3 positions, each specified relative to a center.
  4219. * // In addition to the X, Y, and Z coordinates, the points array contains two additional
  4220. * // elements per point which are ignored for the purpose of computing the bounding sphere.
  4221. * var center = new Cesium.Cartesian3(1.0, 2.0, 3.0);
  4222. * var points = [1.0, 2.0, 3.0, 0.1, 0.2,
  4223. * 4.0, 5.0, 6.0, 0.1, 0.2,
  4224. * 7.0, 8.0, 9.0, 0.1, 0.2];
  4225. * var sphere = Cesium.BoundingSphere.fromVertices(points, center, 5);
  4226. *
  4227. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  4228. */
  4229. BoundingSphere.fromVertices = function(positions, center, stride, result) {
  4230. if (!when.defined(result)) {
  4231. result = new BoundingSphere();
  4232. }
  4233. if (!when.defined(positions) || positions.length === 0) {
  4234. result.center = Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, result.center);
  4235. result.radius = 0.0;
  4236. return result;
  4237. }
  4238. center = when.defaultValue(center, Cartographic.Cartesian3.ZERO);
  4239. stride = when.defaultValue(stride, 3);
  4240. //>>includeStart('debug', pragmas.debug);
  4241. Check.Check.typeOf.number.greaterThanOrEquals('stride', stride, 3);
  4242. //>>includeEnd('debug');
  4243. var currentPos = fromPointsCurrentPos;
  4244. currentPos.x = positions[0] + center.x;
  4245. currentPos.y = positions[1] + center.y;
  4246. currentPos.z = positions[2] + center.z;
  4247. var xMin = Cartographic.Cartesian3.clone(currentPos, fromPointsXMin);
  4248. var yMin = Cartographic.Cartesian3.clone(currentPos, fromPointsYMin);
  4249. var zMin = Cartographic.Cartesian3.clone(currentPos, fromPointsZMin);
  4250. var xMax = Cartographic.Cartesian3.clone(currentPos, fromPointsXMax);
  4251. var yMax = Cartographic.Cartesian3.clone(currentPos, fromPointsYMax);
  4252. var zMax = Cartographic.Cartesian3.clone(currentPos, fromPointsZMax);
  4253. var numElements = positions.length;
  4254. var i;
  4255. for (i = 0; i < numElements; i += stride) {
  4256. var x = positions[i] + center.x;
  4257. var y = positions[i + 1] + center.y;
  4258. var z = positions[i + 2] + center.z;
  4259. currentPos.x = x;
  4260. currentPos.y = y;
  4261. currentPos.z = z;
  4262. // Store points containing the the smallest and largest components
  4263. if (x < xMin.x) {
  4264. Cartographic.Cartesian3.clone(currentPos, xMin);
  4265. }
  4266. if (x > xMax.x) {
  4267. Cartographic.Cartesian3.clone(currentPos, xMax);
  4268. }
  4269. if (y < yMin.y) {
  4270. Cartographic.Cartesian3.clone(currentPos, yMin);
  4271. }
  4272. if (y > yMax.y) {
  4273. Cartographic.Cartesian3.clone(currentPos, yMax);
  4274. }
  4275. if (z < zMin.z) {
  4276. Cartographic.Cartesian3.clone(currentPos, zMin);
  4277. }
  4278. if (z > zMax.z) {
  4279. Cartographic.Cartesian3.clone(currentPos, zMax);
  4280. }
  4281. }
  4282. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  4283. var xSpan = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  4284. var ySpan = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  4285. var zSpan = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  4286. // Set the diameter endpoints to the largest span.
  4287. var diameter1 = xMin;
  4288. var diameter2 = xMax;
  4289. var maxSpan = xSpan;
  4290. if (ySpan > maxSpan) {
  4291. maxSpan = ySpan;
  4292. diameter1 = yMin;
  4293. diameter2 = yMax;
  4294. }
  4295. if (zSpan > maxSpan) {
  4296. maxSpan = zSpan;
  4297. diameter1 = zMin;
  4298. diameter2 = zMax;
  4299. }
  4300. // Calculate the center of the initial sphere found by Ritter's algorithm
  4301. var ritterCenter = fromPointsRitterCenter;
  4302. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  4303. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  4304. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  4305. // Calculate the radius of the initial sphere found by Ritter's algorithm
  4306. var radiusSquared = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  4307. var ritterRadius = Math.sqrt(radiusSquared);
  4308. // Find the center of the sphere found using the Naive method.
  4309. var minBoxPt = fromPointsMinBoxPt;
  4310. minBoxPt.x = xMin.x;
  4311. minBoxPt.y = yMin.y;
  4312. minBoxPt.z = zMin.z;
  4313. var maxBoxPt = fromPointsMaxBoxPt;
  4314. maxBoxPt.x = xMax.x;
  4315. maxBoxPt.y = yMax.y;
  4316. maxBoxPt.z = zMax.z;
  4317. var naiveCenter = Cartographic.Cartesian3.midpoint(minBoxPt, maxBoxPt, fromPointsNaiveCenterScratch);
  4318. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  4319. var naiveRadius = 0;
  4320. for (i = 0; i < numElements; i += stride) {
  4321. currentPos.x = positions[i] + center.x;
  4322. currentPos.y = positions[i + 1] + center.y;
  4323. currentPos.z = positions[i + 2] + center.z;
  4324. // Find the furthest point from the naive center to calculate the naive radius.
  4325. var r = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  4326. if (r > naiveRadius) {
  4327. naiveRadius = r;
  4328. }
  4329. // Make adjustments to the Ritter Sphere to include all points.
  4330. var oldCenterToPointSquared = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  4331. if (oldCenterToPointSquared > radiusSquared) {
  4332. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  4333. // Calculate new radius to include the point that lies outside
  4334. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  4335. radiusSquared = ritterRadius * ritterRadius;
  4336. // Calculate center of new Ritter sphere
  4337. var oldToNew = oldCenterToPoint - ritterRadius;
  4338. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  4339. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  4340. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  4341. }
  4342. }
  4343. if (ritterRadius < naiveRadius) {
  4344. Cartographic.Cartesian3.clone(ritterCenter, result.center);
  4345. result.radius = ritterRadius;
  4346. } else {
  4347. Cartographic.Cartesian3.clone(naiveCenter, result.center);
  4348. result.radius = naiveRadius;
  4349. }
  4350. return result;
  4351. };
  4352. /**
  4353. * Computes a tight-fitting bounding sphere enclosing a list of EncodedCartesian3s, where the points are
  4354. * stored in parallel flat arrays in X, Y, Z, order. The bounding sphere is computed by running two
  4355. * algorithms, a naive algorithm and Ritter's algorithm. The smaller of the two spheres is used to
  4356. * ensure a tight fit.
  4357. *
  4358. * @param {Number[]} [positionsHigh] An array of high bits of the encoded cartesians that the bounding sphere will enclose. Each point
  4359. * is formed from three elements in the array in the order X, Y, Z.
  4360. * @param {Number[]} [positionsLow] An array of low bits of the encoded cartesians that the bounding sphere will enclose. Each point
  4361. * is formed from three elements in the array in the order X, Y, Z.
  4362. * @param {BoundingSphere} [result] The object onto which to store the result.
  4363. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  4364. *
  4365. * @see {@link http://blogs.agi.com/insight3d/index.php/2008/02/04/a-bounding/|Bounding Sphere computation article}
  4366. */
  4367. BoundingSphere.fromEncodedCartesianVertices = function(positionsHigh, positionsLow, result) {
  4368. if (!when.defined(result)) {
  4369. result = new BoundingSphere();
  4370. }
  4371. if (!when.defined(positionsHigh) || !when.defined(positionsLow) || positionsHigh.length !== positionsLow.length || positionsHigh.length === 0) {
  4372. result.center = Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, result.center);
  4373. result.radius = 0.0;
  4374. return result;
  4375. }
  4376. var currentPos = fromPointsCurrentPos;
  4377. currentPos.x = positionsHigh[0] + positionsLow[0];
  4378. currentPos.y = positionsHigh[1] + positionsLow[1];
  4379. currentPos.z = positionsHigh[2] + positionsLow[2];
  4380. var xMin = Cartographic.Cartesian3.clone(currentPos, fromPointsXMin);
  4381. var yMin = Cartographic.Cartesian3.clone(currentPos, fromPointsYMin);
  4382. var zMin = Cartographic.Cartesian3.clone(currentPos, fromPointsZMin);
  4383. var xMax = Cartographic.Cartesian3.clone(currentPos, fromPointsXMax);
  4384. var yMax = Cartographic.Cartesian3.clone(currentPos, fromPointsYMax);
  4385. var zMax = Cartographic.Cartesian3.clone(currentPos, fromPointsZMax);
  4386. var numElements = positionsHigh.length;
  4387. var i;
  4388. for (i = 0; i < numElements; i += 3) {
  4389. var x = positionsHigh[i] + positionsLow[i];
  4390. var y = positionsHigh[i + 1] + positionsLow[i + 1];
  4391. var z = positionsHigh[i + 2] + positionsLow[i + 2];
  4392. currentPos.x = x;
  4393. currentPos.y = y;
  4394. currentPos.z = z;
  4395. // Store points containing the the smallest and largest components
  4396. if (x < xMin.x) {
  4397. Cartographic.Cartesian3.clone(currentPos, xMin);
  4398. }
  4399. if (x > xMax.x) {
  4400. Cartographic.Cartesian3.clone(currentPos, xMax);
  4401. }
  4402. if (y < yMin.y) {
  4403. Cartographic.Cartesian3.clone(currentPos, yMin);
  4404. }
  4405. if (y > yMax.y) {
  4406. Cartographic.Cartesian3.clone(currentPos, yMax);
  4407. }
  4408. if (z < zMin.z) {
  4409. Cartographic.Cartesian3.clone(currentPos, zMin);
  4410. }
  4411. if (z > zMax.z) {
  4412. Cartographic.Cartesian3.clone(currentPos, zMax);
  4413. }
  4414. }
  4415. // Compute x-, y-, and z-spans (Squared distances b/n each component's min. and max.).
  4416. var xSpan = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(xMax, xMin, fromPointsScratch));
  4417. var ySpan = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(yMax, yMin, fromPointsScratch));
  4418. var zSpan = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(zMax, zMin, fromPointsScratch));
  4419. // Set the diameter endpoints to the largest span.
  4420. var diameter1 = xMin;
  4421. var diameter2 = xMax;
  4422. var maxSpan = xSpan;
  4423. if (ySpan > maxSpan) {
  4424. maxSpan = ySpan;
  4425. diameter1 = yMin;
  4426. diameter2 = yMax;
  4427. }
  4428. if (zSpan > maxSpan) {
  4429. maxSpan = zSpan;
  4430. diameter1 = zMin;
  4431. diameter2 = zMax;
  4432. }
  4433. // Calculate the center of the initial sphere found by Ritter's algorithm
  4434. var ritterCenter = fromPointsRitterCenter;
  4435. ritterCenter.x = (diameter1.x + diameter2.x) * 0.5;
  4436. ritterCenter.y = (diameter1.y + diameter2.y) * 0.5;
  4437. ritterCenter.z = (diameter1.z + diameter2.z) * 0.5;
  4438. // Calculate the radius of the initial sphere found by Ritter's algorithm
  4439. var radiusSquared = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(diameter2, ritterCenter, fromPointsScratch));
  4440. var ritterRadius = Math.sqrt(radiusSquared);
  4441. // Find the center of the sphere found using the Naive method.
  4442. var minBoxPt = fromPointsMinBoxPt;
  4443. minBoxPt.x = xMin.x;
  4444. minBoxPt.y = yMin.y;
  4445. minBoxPt.z = zMin.z;
  4446. var maxBoxPt = fromPointsMaxBoxPt;
  4447. maxBoxPt.x = xMax.x;
  4448. maxBoxPt.y = yMax.y;
  4449. maxBoxPt.z = zMax.z;
  4450. var naiveCenter = Cartographic.Cartesian3.midpoint(minBoxPt, maxBoxPt, fromPointsNaiveCenterScratch);
  4451. // Begin 2nd pass to find naive radius and modify the ritter sphere.
  4452. var naiveRadius = 0;
  4453. for (i = 0; i < numElements; i += 3) {
  4454. currentPos.x = positionsHigh[i] + positionsLow[i];
  4455. currentPos.y = positionsHigh[i + 1] + positionsLow[i + 1];
  4456. currentPos.z = positionsHigh[i + 2] + positionsLow[i + 2];
  4457. // Find the furthest point from the naive center to calculate the naive radius.
  4458. var r = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.subtract(currentPos, naiveCenter, fromPointsScratch));
  4459. if (r > naiveRadius) {
  4460. naiveRadius = r;
  4461. }
  4462. // Make adjustments to the Ritter Sphere to include all points.
  4463. var oldCenterToPointSquared = Cartographic.Cartesian3.magnitudeSquared(Cartographic.Cartesian3.subtract(currentPos, ritterCenter, fromPointsScratch));
  4464. if (oldCenterToPointSquared > radiusSquared) {
  4465. var oldCenterToPoint = Math.sqrt(oldCenterToPointSquared);
  4466. // Calculate new radius to include the point that lies outside
  4467. ritterRadius = (ritterRadius + oldCenterToPoint) * 0.5;
  4468. radiusSquared = ritterRadius * ritterRadius;
  4469. // Calculate center of new Ritter sphere
  4470. var oldToNew = oldCenterToPoint - ritterRadius;
  4471. ritterCenter.x = (ritterRadius * ritterCenter.x + oldToNew * currentPos.x) / oldCenterToPoint;
  4472. ritterCenter.y = (ritterRadius * ritterCenter.y + oldToNew * currentPos.y) / oldCenterToPoint;
  4473. ritterCenter.z = (ritterRadius * ritterCenter.z + oldToNew * currentPos.z) / oldCenterToPoint;
  4474. }
  4475. }
  4476. if (ritterRadius < naiveRadius) {
  4477. Cartographic.Cartesian3.clone(ritterCenter, result.center);
  4478. result.radius = ritterRadius;
  4479. } else {
  4480. Cartographic.Cartesian3.clone(naiveCenter, result.center);
  4481. result.radius = naiveRadius;
  4482. }
  4483. return result;
  4484. };
  4485. /**
  4486. * Computes a bounding sphere from the corner points of an axis-aligned bounding box. The sphere
  4487. * tighly and fully encompases the box.
  4488. *
  4489. * @param {Cartesian3} [corner] The minimum height over the rectangle.
  4490. * @param {Cartesian3} [oppositeCorner] The maximum height over the rectangle.
  4491. * @param {BoundingSphere} [result] The object onto which to store the result.
  4492. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4493. *
  4494. * @example
  4495. * // Create a bounding sphere around the unit cube
  4496. * var sphere = Cesium.BoundingSphere.fromCornerPoints(new Cesium.Cartesian3(-0.5, -0.5, -0.5), new Cesium.Cartesian3(0.5, 0.5, 0.5));
  4497. */
  4498. BoundingSphere.fromCornerPoints = function(corner, oppositeCorner, result) {
  4499. //>>includeStart('debug', pragmas.debug);
  4500. Check.Check.typeOf.object('corner', corner);
  4501. Check.Check.typeOf.object('oppositeCorner', oppositeCorner);
  4502. //>>includeEnd('debug');
  4503. if (!when.defined(result)) {
  4504. result = new BoundingSphere();
  4505. }
  4506. var center = Cartographic.Cartesian3.midpoint(corner, oppositeCorner, result.center);
  4507. result.radius = Cartographic.Cartesian3.distance(center, oppositeCorner);
  4508. return result;
  4509. };
  4510. /**
  4511. * Creates a bounding sphere encompassing an ellipsoid.
  4512. *
  4513. * @param {Ellipsoid} ellipsoid The ellipsoid around which to create a bounding sphere.
  4514. * @param {BoundingSphere} [result] The object onto which to store the result.
  4515. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4516. *
  4517. * @example
  4518. * var boundingSphere = Cesium.BoundingSphere.fromEllipsoid(ellipsoid);
  4519. */
  4520. BoundingSphere.fromEllipsoid = function(ellipsoid, result) {
  4521. //>>includeStart('debug', pragmas.debug);
  4522. Check.Check.typeOf.object('ellipsoid', ellipsoid);
  4523. //>>includeEnd('debug');
  4524. if (!when.defined(result)) {
  4525. result = new BoundingSphere();
  4526. }
  4527. Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, result.center);
  4528. result.radius = ellipsoid.maximumRadius;
  4529. return result;
  4530. };
  4531. var fromBoundingSpheresScratch = new Cartographic.Cartesian3();
  4532. /**
  4533. * Computes a tight-fitting bounding sphere enclosing the provided array of bounding spheres.
  4534. *
  4535. * @param {BoundingSphere[]} [boundingSpheres] The array of bounding spheres.
  4536. * @param {BoundingSphere} [result] The object onto which to store the result.
  4537. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4538. */
  4539. BoundingSphere.fromBoundingSpheres = function(boundingSpheres, result) {
  4540. if (!when.defined(result)) {
  4541. result = new BoundingSphere();
  4542. }
  4543. if (!when.defined(boundingSpheres) || boundingSpheres.length === 0) {
  4544. result.center = Cartographic.Cartesian3.clone(Cartographic.Cartesian3.ZERO, result.center);
  4545. result.radius = 0.0;
  4546. return result;
  4547. }
  4548. var length = boundingSpheres.length;
  4549. if (length === 1) {
  4550. return BoundingSphere.clone(boundingSpheres[0], result);
  4551. }
  4552. if (length === 2) {
  4553. return BoundingSphere.union(boundingSpheres[0], boundingSpheres[1], result);
  4554. }
  4555. var positions = [];
  4556. var i;
  4557. for (i = 0; i < length; i++) {
  4558. positions.push(boundingSpheres[i].center);
  4559. }
  4560. result = BoundingSphere.fromPoints(positions, result);
  4561. var center = result.center;
  4562. var radius = result.radius;
  4563. for (i = 0; i < length; i++) {
  4564. var tmp = boundingSpheres[i];
  4565. radius = Math.max(radius, Cartographic.Cartesian3.distance(center, tmp.center, fromBoundingSpheresScratch) + tmp.radius);
  4566. }
  4567. result.radius = radius;
  4568. return result;
  4569. };
  4570. var fromOrientedBoundingBoxScratchU = new Cartographic.Cartesian3();
  4571. var fromOrientedBoundingBoxScratchV = new Cartographic.Cartesian3();
  4572. var fromOrientedBoundingBoxScratchW = new Cartographic.Cartesian3();
  4573. /**
  4574. * Computes a tight-fitting bounding sphere enclosing the provided oriented bounding box.
  4575. *
  4576. * @param {OrientedBoundingBox} orientedBoundingBox The oriented bounding box.
  4577. * @param {BoundingSphere} [result] The object onto which to store the result.
  4578. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4579. */
  4580. BoundingSphere.fromOrientedBoundingBox = function(orientedBoundingBox, result) {
  4581. //>>includeStart('debug', pragmas.debug);
  4582. Check.Check.defined('orientedBoundingBox', orientedBoundingBox);
  4583. //>>includeEnd('debug');
  4584. if (!when.defined(result)) {
  4585. result = new BoundingSphere();
  4586. }
  4587. var halfAxes = orientedBoundingBox.halfAxes;
  4588. var u = Matrix3.getColumn(halfAxes, 0, fromOrientedBoundingBoxScratchU);
  4589. var v = Matrix3.getColumn(halfAxes, 1, fromOrientedBoundingBoxScratchV);
  4590. var w = Matrix3.getColumn(halfAxes, 2, fromOrientedBoundingBoxScratchW);
  4591. Cartographic.Cartesian3.add(u, v, u);
  4592. Cartographic.Cartesian3.add(u, w, u);
  4593. result.center = Cartographic.Cartesian3.clone(orientedBoundingBox.center, result.center);
  4594. result.radius = Cartographic.Cartesian3.magnitude(u);
  4595. return result;
  4596. };
  4597. /**
  4598. * Duplicates a BoundingSphere instance.
  4599. *
  4600. * @param {BoundingSphere} sphere The bounding sphere to duplicate.
  4601. * @param {BoundingSphere} [result] The object onto which to store the result.
  4602. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided. (Returns undefined if sphere is undefined)
  4603. */
  4604. BoundingSphere.clone = function(sphere, result) {
  4605. if (!when.defined(sphere)) {
  4606. return undefined;
  4607. }
  4608. if (!when.defined(result)) {
  4609. return new BoundingSphere(sphere.center, sphere.radius);
  4610. }
  4611. result.center = Cartographic.Cartesian3.clone(sphere.center, result.center);
  4612. result.radius = sphere.radius;
  4613. return result;
  4614. };
  4615. /**
  4616. * The number of elements used to pack the object into an array.
  4617. * @type {Number}
  4618. */
  4619. BoundingSphere.packedLength = 4;
  4620. /**
  4621. * Stores the provided instance into the provided array.
  4622. *
  4623. * @param {BoundingSphere} value The value to pack.
  4624. * @param {Number[]} array The array to pack into.
  4625. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  4626. *
  4627. * @returns {Number[]} The array that was packed into
  4628. */
  4629. BoundingSphere.pack = function(value, array, startingIndex) {
  4630. //>>includeStart('debug', pragmas.debug);
  4631. Check.Check.typeOf.object('value', value);
  4632. Check.Check.defined('array', array);
  4633. //>>includeEnd('debug');
  4634. startingIndex = when.defaultValue(startingIndex, 0);
  4635. var center = value.center;
  4636. array[startingIndex++] = center.x;
  4637. array[startingIndex++] = center.y;
  4638. array[startingIndex++] = center.z;
  4639. array[startingIndex] = value.radius;
  4640. return array;
  4641. };
  4642. /**
  4643. * Retrieves an instance from a packed array.
  4644. *
  4645. * @param {Number[]} array The packed array.
  4646. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  4647. * @param {BoundingSphere} [result] The object into which to store the result.
  4648. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if one was not provided.
  4649. */
  4650. BoundingSphere.unpack = function(array, startingIndex, result) {
  4651. //>>includeStart('debug', pragmas.debug);
  4652. Check.Check.defined('array', array);
  4653. //>>includeEnd('debug');
  4654. startingIndex = when.defaultValue(startingIndex, 0);
  4655. if (!when.defined(result)) {
  4656. result = new BoundingSphere();
  4657. }
  4658. var center = result.center;
  4659. center.x = array[startingIndex++];
  4660. center.y = array[startingIndex++];
  4661. center.z = array[startingIndex++];
  4662. result.radius = array[startingIndex];
  4663. return result;
  4664. };
  4665. var unionScratch = new Cartographic.Cartesian3();
  4666. var unionScratchCenter = new Cartographic.Cartesian3();
  4667. /**
  4668. * Computes a bounding sphere that contains both the left and right bounding spheres.
  4669. *
  4670. * @param {BoundingSphere} left A sphere to enclose in a bounding sphere.
  4671. * @param {BoundingSphere} right A sphere to enclose in a bounding sphere.
  4672. * @param {BoundingSphere} [result] The object onto which to store the result.
  4673. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4674. */
  4675. BoundingSphere.union = function(left, right, result) {
  4676. //>>includeStart('debug', pragmas.debug);
  4677. Check.Check.typeOf.object('left', left);
  4678. Check.Check.typeOf.object('right', right);
  4679. //>>includeEnd('debug');
  4680. if (!when.defined(result)) {
  4681. result = new BoundingSphere();
  4682. }
  4683. var leftCenter = left.center;
  4684. var leftRadius = left.radius;
  4685. var rightCenter = right.center;
  4686. var rightRadius = right.radius;
  4687. var toRightCenter = Cartographic.Cartesian3.subtract(rightCenter, leftCenter, unionScratch);
  4688. var centerSeparation = Cartographic.Cartesian3.magnitude(toRightCenter);
  4689. if (leftRadius >= (centerSeparation + rightRadius)) {
  4690. // Left sphere wins.
  4691. left.clone(result);
  4692. return result;
  4693. }
  4694. if (rightRadius >= (centerSeparation + leftRadius)) {
  4695. // Right sphere wins.
  4696. right.clone(result);
  4697. return result;
  4698. }
  4699. // There are two tangent points, one on far side of each sphere.
  4700. var halfDistanceBetweenTangentPoints = (leftRadius + centerSeparation + rightRadius) * 0.5;
  4701. // Compute the center point halfway between the two tangent points.
  4702. var center = Cartographic.Cartesian3.multiplyByScalar(toRightCenter,
  4703. (-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation, unionScratchCenter);
  4704. Cartographic.Cartesian3.add(center, leftCenter, center);
  4705. Cartographic.Cartesian3.clone(center, result.center);
  4706. result.radius = halfDistanceBetweenTangentPoints;
  4707. return result;
  4708. };
  4709. var expandScratch = new Cartographic.Cartesian3();
  4710. /**
  4711. * Computes a bounding sphere by enlarging the provided sphere to contain the provided point.
  4712. *
  4713. * @param {BoundingSphere} sphere A sphere to expand.
  4714. * @param {Cartesian3} point A point to enclose in a bounding sphere.
  4715. * @param {BoundingSphere} [result] The object onto which to store the result.
  4716. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4717. */
  4718. BoundingSphere.expand = function(sphere, point, result) {
  4719. //>>includeStart('debug', pragmas.debug);
  4720. Check.Check.typeOf.object('sphere', sphere);
  4721. Check.Check.typeOf.object('point', point);
  4722. //>>includeEnd('debug');
  4723. result = BoundingSphere.clone(sphere, result);
  4724. var radius = Cartographic.Cartesian3.magnitude(Cartographic.Cartesian3.subtract(point, result.center, expandScratch));
  4725. if (radius > result.radius) {
  4726. result.radius = radius;
  4727. }
  4728. return result;
  4729. };
  4730. /**
  4731. * Determines which side of a plane a sphere is located.
  4732. *
  4733. * @param {BoundingSphere} sphere The bounding sphere to test.
  4734. * @param {Plane} plane The plane to test against.
  4735. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  4736. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  4737. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  4738. * intersects the plane.
  4739. */
  4740. BoundingSphere.intersectPlane = function(sphere, plane) {
  4741. //>>includeStart('debug', pragmas.debug);
  4742. Check.Check.typeOf.object('sphere', sphere);
  4743. Check.Check.typeOf.object('plane', plane);
  4744. //>>includeEnd('debug');
  4745. var center = sphere.center;
  4746. var radius = sphere.radius;
  4747. var normal = plane.normal;
  4748. var distanceToPlane = Cartographic.Cartesian3.dot(normal, center) + plane.distance;
  4749. if (distanceToPlane < -radius) {
  4750. // The center point is negative side of the plane normal
  4751. return Intersect$1.OUTSIDE;
  4752. } else if (distanceToPlane < radius) {
  4753. // The center point is positive side of the plane, but radius extends beyond it; partial overlap
  4754. return Intersect$1.INTERSECTING;
  4755. }
  4756. return Intersect$1.INSIDE;
  4757. };
  4758. /**
  4759. * Applies a 4x4 affine transformation matrix to a bounding sphere.
  4760. *
  4761. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  4762. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  4763. * @param {BoundingSphere} [result] The object onto which to store the result.
  4764. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4765. */
  4766. BoundingSphere.transform = function(sphere, transform, result) {
  4767. //>>includeStart('debug', pragmas.debug);
  4768. Check.Check.typeOf.object('sphere', sphere);
  4769. Check.Check.typeOf.object('transform', transform);
  4770. //>>includeEnd('debug');
  4771. if (!when.defined(result)) {
  4772. result = new BoundingSphere();
  4773. }
  4774. result.center = Matrix4.multiplyByPoint(transform, sphere.center, result.center);
  4775. result.radius = Matrix4.getMaximumScale(transform) * sphere.radius;
  4776. return result;
  4777. };
  4778. var distanceSquaredToScratch = new Cartographic.Cartesian3();
  4779. /**
  4780. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  4781. *
  4782. * @param {BoundingSphere} sphere The sphere.
  4783. * @param {Cartesian3} cartesian The point
  4784. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  4785. *
  4786. * @example
  4787. * // Sort bounding spheres from back to front
  4788. * spheres.sort(function(a, b) {
  4789. * return Cesium.BoundingSphere.distanceSquaredTo(b, camera.positionWC) - Cesium.BoundingSphere.distanceSquaredTo(a, camera.positionWC);
  4790. * });
  4791. */
  4792. BoundingSphere.distanceSquaredTo = function(sphere, cartesian) {
  4793. //>>includeStart('debug', pragmas.debug);
  4794. Check.Check.typeOf.object('sphere', sphere);
  4795. Check.Check.typeOf.object('cartesian', cartesian);
  4796. //>>includeEnd('debug');
  4797. var diff = Cartographic.Cartesian3.subtract(sphere.center, cartesian, distanceSquaredToScratch);
  4798. return Cartographic.Cartesian3.magnitudeSquared(diff) - sphere.radius * sphere.radius;
  4799. };
  4800. /**
  4801. * Applies a 4x4 affine transformation matrix to a bounding sphere where there is no scale
  4802. * The transformation matrix is not verified to have a uniform scale of 1.
  4803. * This method is faster than computing the general bounding sphere transform using {@link BoundingSphere.transform}.
  4804. *
  4805. * @param {BoundingSphere} sphere The bounding sphere to apply the transformation to.
  4806. * @param {Matrix4} transform The transformation matrix to apply to the bounding sphere.
  4807. * @param {BoundingSphere} [result] The object onto which to store the result.
  4808. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4809. *
  4810. * @example
  4811. * var modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(positionOnEllipsoid);
  4812. * var boundingSphere = new Cesium.BoundingSphere();
  4813. * var newBoundingSphere = Cesium.BoundingSphere.transformWithoutScale(boundingSphere, modelMatrix);
  4814. */
  4815. BoundingSphere.transformWithoutScale = function(sphere, transform, result) {
  4816. //>>includeStart('debug', pragmas.debug);
  4817. Check.Check.typeOf.object('sphere', sphere);
  4818. Check.Check.typeOf.object('transform', transform);
  4819. //>>includeEnd('debug');
  4820. if (!when.defined(result)) {
  4821. result = new BoundingSphere();
  4822. }
  4823. result.center = Matrix4.multiplyByPoint(transform, sphere.center, result.center);
  4824. result.radius = sphere.radius;
  4825. return result;
  4826. };
  4827. var scratchCartesian3 = new Cartographic.Cartesian3();
  4828. /**
  4829. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  4830. * plus/minus the radius of the bounding sphere.
  4831. * <br>
  4832. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  4833. * closest and farthest planes from position that intersect the bounding sphere.
  4834. *
  4835. * @param {BoundingSphere} sphere The bounding sphere to calculate the distance to.
  4836. * @param {Cartesian3} position The position to calculate the distance from.
  4837. * @param {Cartesian3} direction The direction from position.
  4838. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  4839. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  4840. */
  4841. BoundingSphere.computePlaneDistances = function(sphere, position, direction, result) {
  4842. //>>includeStart('debug', pragmas.debug);
  4843. Check.Check.typeOf.object('sphere', sphere);
  4844. Check.Check.typeOf.object('position', position);
  4845. Check.Check.typeOf.object('direction', direction);
  4846. //>>includeEnd('debug');
  4847. if (!when.defined(result)) {
  4848. result = new Interval();
  4849. }
  4850. var toCenter = Cartographic.Cartesian3.subtract(sphere.center, position, scratchCartesian3);
  4851. var mag = Cartographic.Cartesian3.dot(direction, toCenter);
  4852. result.start = mag - sphere.radius;
  4853. result.stop = mag + sphere.radius;
  4854. return result;
  4855. };
  4856. var projectTo2DNormalScratch = new Cartographic.Cartesian3();
  4857. var projectTo2DEastScratch = new Cartographic.Cartesian3();
  4858. var projectTo2DNorthScratch = new Cartographic.Cartesian3();
  4859. var projectTo2DWestScratch = new Cartographic.Cartesian3();
  4860. var projectTo2DSouthScratch = new Cartographic.Cartesian3();
  4861. var projectTo2DCartographicScratch = new Cartographic.Cartographic();
  4862. var projectTo2DPositionsScratch = new Array(8);
  4863. for (var n = 0; n < 8; ++n) {
  4864. projectTo2DPositionsScratch[n] = new Cartographic.Cartesian3();
  4865. }
  4866. var projectTo2DProjection = new GeographicProjection();
  4867. /**
  4868. * Creates a bounding sphere in 2D from a bounding sphere in 3D world coordinates.
  4869. *
  4870. * @param {BoundingSphere} sphere The bounding sphere to transform to 2D.
  4871. * @param {Object} [projection=GeographicProjection] The projection to 2D.
  4872. * @param {BoundingSphere} [result] The object onto which to store the result.
  4873. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  4874. */
  4875. BoundingSphere.projectTo2D = function(sphere, projection, result) {
  4876. //>>includeStart('debug', pragmas.debug);
  4877. Check.Check.typeOf.object('sphere', sphere);
  4878. //>>includeEnd('debug');
  4879. projection = when.defaultValue(projection, projectTo2DProjection);
  4880. var ellipsoid = projection.ellipsoid;
  4881. var center = sphere.center;
  4882. var radius = sphere.radius;
  4883. var normal;
  4884. if (Cartographic.Cartesian3.equals(center, Cartographic.Cartesian3.ZERO)) {
  4885. // Bounding sphere is at the center. The geodetic surface normal is not
  4886. // defined here so pick the x-axis as a fallback.
  4887. normal = Cartographic.Cartesian3.clone(Cartographic.Cartesian3.UNIT_X, projectTo2DNormalScratch);
  4888. } else {
  4889. normal = ellipsoid.geodeticSurfaceNormal(center, projectTo2DNormalScratch);
  4890. }
  4891. var east = Cartographic.Cartesian3.cross(Cartographic.Cartesian3.UNIT_Z, normal, projectTo2DEastScratch);
  4892. Cartographic.Cartesian3.normalize(east, east);
  4893. var north = Cartographic.Cartesian3.cross(normal, east, projectTo2DNorthScratch);
  4894. Cartographic.Cartesian3.normalize(north, north);
  4895. Cartographic.Cartesian3.multiplyByScalar(normal, radius, normal);
  4896. Cartographic.Cartesian3.multiplyByScalar(north, radius, north);
  4897. Cartographic.Cartesian3.multiplyByScalar(east, radius, east);
  4898. var south = Cartographic.Cartesian3.negate(north, projectTo2DSouthScratch);
  4899. var west = Cartographic.Cartesian3.negate(east, projectTo2DWestScratch);
  4900. var positions = projectTo2DPositionsScratch;
  4901. // top NE corner
  4902. var corner = positions[0];
  4903. Cartographic.Cartesian3.add(normal, north, corner);
  4904. Cartographic.Cartesian3.add(corner, east, corner);
  4905. // top NW corner
  4906. corner = positions[1];
  4907. Cartographic.Cartesian3.add(normal, north, corner);
  4908. Cartographic.Cartesian3.add(corner, west, corner);
  4909. // top SW corner
  4910. corner = positions[2];
  4911. Cartographic.Cartesian3.add(normal, south, corner);
  4912. Cartographic.Cartesian3.add(corner, west, corner);
  4913. // top SE corner
  4914. corner = positions[3];
  4915. Cartographic.Cartesian3.add(normal, south, corner);
  4916. Cartographic.Cartesian3.add(corner, east, corner);
  4917. Cartographic.Cartesian3.negate(normal, normal);
  4918. // bottom NE corner
  4919. corner = positions[4];
  4920. Cartographic.Cartesian3.add(normal, north, corner);
  4921. Cartographic.Cartesian3.add(corner, east, corner);
  4922. // bottom NW corner
  4923. corner = positions[5];
  4924. Cartographic.Cartesian3.add(normal, north, corner);
  4925. Cartographic.Cartesian3.add(corner, west, corner);
  4926. // bottom SW corner
  4927. corner = positions[6];
  4928. Cartographic.Cartesian3.add(normal, south, corner);
  4929. Cartographic.Cartesian3.add(corner, west, corner);
  4930. // bottom SE corner
  4931. corner = positions[7];
  4932. Cartographic.Cartesian3.add(normal, south, corner);
  4933. Cartographic.Cartesian3.add(corner, east, corner);
  4934. var length = positions.length;
  4935. for (var i = 0; i < length; ++i) {
  4936. var position = positions[i];
  4937. Cartographic.Cartesian3.add(center, position, position);
  4938. var cartographic = ellipsoid.cartesianToCartographic(position, projectTo2DCartographicScratch);
  4939. projection.project(cartographic, position);
  4940. }
  4941. result = BoundingSphere.fromPoints(positions, result);
  4942. // swizzle center components
  4943. center = result.center;
  4944. var x = center.x;
  4945. var y = center.y;
  4946. var z = center.z;
  4947. center.x = z;
  4948. center.y = x;
  4949. center.z = y;
  4950. return result;
  4951. };
  4952. /**
  4953. * Determines whether or not a sphere is hidden from view by the occluder.
  4954. *
  4955. * @param {BoundingSphere} sphere The bounding sphere surrounding the occludee object.
  4956. * @param {Occluder} occluder The occluder.
  4957. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  4958. */
  4959. BoundingSphere.isOccluded = function(sphere, occluder) {
  4960. //>>includeStart('debug', pragmas.debug);
  4961. Check.Check.typeOf.object('sphere', sphere);
  4962. Check.Check.typeOf.object('occluder', occluder);
  4963. //>>includeEnd('debug');
  4964. return !occluder.isBoundingSphereVisible(sphere);
  4965. };
  4966. /**
  4967. * Compares the provided BoundingSphere componentwise and returns
  4968. * <code>true</code> if they are equal, <code>false</code> otherwise.
  4969. *
  4970. * @param {BoundingSphere} [left] The first BoundingSphere.
  4971. * @param {BoundingSphere} [right] The second BoundingSphere.
  4972. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  4973. */
  4974. BoundingSphere.equals = function(left, right) {
  4975. return (left === right) ||
  4976. ((when.defined(left)) &&
  4977. (when.defined(right)) &&
  4978. Cartographic.Cartesian3.equals(left.center, right.center) &&
  4979. left.radius === right.radius);
  4980. };
  4981. /**
  4982. * Determines which side of a plane the sphere is located.
  4983. *
  4984. * @param {Plane} plane The plane to test against.
  4985. * @returns {Intersect} {@link Intersect.INSIDE} if the entire sphere is on the side of the plane
  4986. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire sphere is
  4987. * on the opposite side, and {@link Intersect.INTERSECTING} if the sphere
  4988. * intersects the plane.
  4989. */
  4990. BoundingSphere.prototype.intersectPlane = function(plane) {
  4991. return BoundingSphere.intersectPlane(this, plane);
  4992. };
  4993. /**
  4994. * Computes the estimated distance squared from the closest point on a bounding sphere to a point.
  4995. *
  4996. * @param {Cartesian3} cartesian The point
  4997. * @returns {Number} The estimated distance squared from the bounding sphere to the point.
  4998. *
  4999. * @example
  5000. * // Sort bounding spheres from back to front
  5001. * spheres.sort(function(a, b) {
  5002. * return b.distanceSquaredTo(camera.positionWC) - a.distanceSquaredTo(camera.positionWC);
  5003. * });
  5004. */
  5005. BoundingSphere.prototype.distanceSquaredTo = function(cartesian) {
  5006. return BoundingSphere.distanceSquaredTo(this, cartesian);
  5007. };
  5008. /**
  5009. * The distances calculated by the vector from the center of the bounding sphere to position projected onto direction
  5010. * plus/minus the radius of the bounding sphere.
  5011. * <br>
  5012. * If you imagine the infinite number of planes with normal direction, this computes the smallest distance to the
  5013. * closest and farthest planes from position that intersect the bounding sphere.
  5014. *
  5015. * @param {Cartesian3} position The position to calculate the distance from.
  5016. * @param {Cartesian3} direction The direction from position.
  5017. * @param {Interval} [result] A Interval to store the nearest and farthest distances.
  5018. * @returns {Interval} The nearest and farthest distances on the bounding sphere from position in direction.
  5019. */
  5020. BoundingSphere.prototype.computePlaneDistances = function(position, direction, result) {
  5021. return BoundingSphere.computePlaneDistances(this, position, direction, result);
  5022. };
  5023. /**
  5024. * Determines whether or not a sphere is hidden from view by the occluder.
  5025. *
  5026. * @param {Occluder} occluder The occluder.
  5027. * @returns {Boolean} <code>true</code> if the sphere is not visible; otherwise <code>false</code>.
  5028. */
  5029. BoundingSphere.prototype.isOccluded = function(occluder) {
  5030. return BoundingSphere.isOccluded(this, occluder);
  5031. };
  5032. /**
  5033. * Compares this BoundingSphere against the provided BoundingSphere componentwise and returns
  5034. * <code>true</code> if they are equal, <code>false</code> otherwise.
  5035. *
  5036. * @param {BoundingSphere} [right] The right hand side BoundingSphere.
  5037. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  5038. */
  5039. BoundingSphere.prototype.equals = function(right) {
  5040. return BoundingSphere.equals(this, right);
  5041. };
  5042. /**
  5043. * Duplicates this BoundingSphere instance.
  5044. *
  5045. * @param {BoundingSphere} [result] The object onto which to store the result.
  5046. * @returns {BoundingSphere} The modified result parameter or a new BoundingSphere instance if none was provided.
  5047. */
  5048. BoundingSphere.prototype.clone = function(result) {
  5049. return BoundingSphere.clone(this, result);
  5050. };
  5051. /**
  5052. * Computes the radius of the BoundingSphere.
  5053. * @returns {Number} The radius of the BoundingSphere.
  5054. */
  5055. BoundingSphere.prototype.volume = function() {
  5056. var radius = this.radius;
  5057. return volumeConstant * radius * radius * radius;
  5058. };
  5059. exports.BoundingSphere = BoundingSphere;
  5060. exports.GeographicProjection = GeographicProjection;
  5061. exports.Intersect = Intersect$1;
  5062. exports.Interval = Interval;
  5063. exports.Matrix3 = Matrix3;
  5064. exports.Matrix4 = Matrix4;
  5065. });