DirectXCollision.inl 193 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784
  1. //-------------------------------------------------------------------------------------
  2. // DirectXCollision.inl -- C++ Collision Math library
  3. //
  4. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  5. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  6. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  7. // PARTICULAR PURPOSE.
  8. //
  9. // Copyright (c) Microsoft Corporation. All rights reserved.
  10. //
  11. // http://go.microsoft.com/fwlink/?LinkID=615560
  12. //-------------------------------------------------------------------------------------
  13. #pragma once
  14. XMGLOBALCONST XMVECTORF32 g_BoxOffset[8] =
  15. {
  16. { { { -1.0f, -1.0f, 1.0f, 0.0f } } },
  17. { { { 1.0f, -1.0f, 1.0f, 0.0f } } },
  18. { { { 1.0f, 1.0f, 1.0f, 0.0f } } },
  19. { { { -1.0f, 1.0f, 1.0f, 0.0f } } },
  20. { { { -1.0f, -1.0f, -1.0f, 0.0f } } },
  21. { { { 1.0f, -1.0f, -1.0f, 0.0f } } },
  22. { { { 1.0f, 1.0f, -1.0f, 0.0f } } },
  23. { { { -1.0f, 1.0f, -1.0f, 0.0f } } },
  24. };
  25. XMGLOBALCONST XMVECTORF32 g_RayEpsilon = { { { 1e-20f, 1e-20f, 1e-20f, 1e-20f } } };
  26. XMGLOBALCONST XMVECTORF32 g_RayNegEpsilon = { { { -1e-20f, -1e-20f, -1e-20f, -1e-20f } } };
  27. XMGLOBALCONST XMVECTORF32 g_FltMin = { { { -FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX } } };
  28. XMGLOBALCONST XMVECTORF32 g_FltMax = { { { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX } } };
  29. namespace Internal
  30. {
  31. //-----------------------------------------------------------------------------
  32. // Return true if any of the elements of a 3 vector are equal to 0xffffffff.
  33. // Slightly more efficient than using XMVector3EqualInt.
  34. //-----------------------------------------------------------------------------
  35. inline bool XMVector3AnyTrue( _In_ FXMVECTOR V )
  36. {
  37. // Duplicate the fourth element from the first element.
  38. XMVECTOR C = XMVectorSwizzle<XM_SWIZZLE_X, XM_SWIZZLE_Y, XM_SWIZZLE_Z, XM_SWIZZLE_X>(V);
  39. return XMComparisonAnyTrue( XMVector4EqualIntR( C, XMVectorTrueInt() ) );
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Return true if all of the elements of a 3 vector are equal to 0xffffffff.
  43. // Slightly more efficient than using XMVector3EqualInt.
  44. //-----------------------------------------------------------------------------
  45. inline bool XMVector3AllTrue( _In_ FXMVECTOR V )
  46. {
  47. // Duplicate the fourth element from the first element.
  48. XMVECTOR C = XMVectorSwizzle<XM_SWIZZLE_X, XM_SWIZZLE_Y, XM_SWIZZLE_Z, XM_SWIZZLE_X>( V );
  49. return XMComparisonAllTrue( XMVector4EqualIntR( C, XMVectorTrueInt() ) );
  50. }
  51. #if defined(_PREFAST_) || !defined(NDEBUG)
  52. XMGLOBALCONST XMVECTORF32 g_UnitVectorEpsilon = { { { 1.0e-4f, 1.0e-4f, 1.0e-4f, 1.0e-4f } } };
  53. XMGLOBALCONST XMVECTORF32 g_UnitQuaternionEpsilon = { { { 1.0e-4f, 1.0e-4f, 1.0e-4f, 1.0e-4f } } };
  54. XMGLOBALCONST XMVECTORF32 g_UnitPlaneEpsilon = { { { 1.0e-4f, 1.0e-4f, 1.0e-4f, 1.0e-4f } } };
  55. //-----------------------------------------------------------------------------
  56. // Return true if the vector is a unit vector (length == 1).
  57. //-----------------------------------------------------------------------------
  58. inline bool XMVector3IsUnit( _In_ FXMVECTOR V )
  59. {
  60. XMVECTOR Difference = XMVectorSubtract( XMVector3Length( V ), XMVectorSplatOne() );
  61. return XMVector4Less( XMVectorAbs( Difference ), g_UnitVectorEpsilon );
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Return true if the quaterion is a unit quaternion.
  65. //-----------------------------------------------------------------------------
  66. inline bool XMQuaternionIsUnit( _In_ FXMVECTOR Q )
  67. {
  68. XMVECTOR Difference = XMVectorSubtract( XMVector4Length( Q ), XMVectorSplatOne() );
  69. return XMVector4Less( XMVectorAbs( Difference ), g_UnitQuaternionEpsilon );
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Return true if the plane is a unit plane.
  73. //-----------------------------------------------------------------------------
  74. inline bool XMPlaneIsUnit( _In_ FXMVECTOR Plane )
  75. {
  76. XMVECTOR Difference = XMVectorSubtract( XMVector3Length( Plane ), XMVectorSplatOne() );
  77. return XMVector4Less( XMVectorAbs( Difference ), g_UnitPlaneEpsilon );
  78. }
  79. #endif // _PREFAST_ || !NDEBUG
  80. //-----------------------------------------------------------------------------
  81. inline XMVECTOR XMPlaneTransform( _In_ FXMVECTOR Plane, _In_ FXMVECTOR Rotation, _In_ FXMVECTOR Translation )
  82. {
  83. XMVECTOR vNormal = XMVector3Rotate( Plane, Rotation );
  84. XMVECTOR vD = XMVectorSubtract( XMVectorSplatW( Plane ), XMVector3Dot( vNormal, Translation ) );
  85. return XMVectorInsert<0, 0, 0, 0, 1>( vNormal, vD );
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Return the point on the line segement (S1, S2) nearest the point P.
  89. //-----------------------------------------------------------------------------
  90. inline XMVECTOR PointOnLineSegmentNearestPoint( _In_ FXMVECTOR S1, _In_ FXMVECTOR S2, _In_ FXMVECTOR P )
  91. {
  92. XMVECTOR Dir = XMVectorSubtract( S2, S1 );
  93. XMVECTOR Projection = XMVectorSubtract( XMVector3Dot( P, Dir ), XMVector3Dot( S1, Dir ) );
  94. XMVECTOR LengthSq = XMVector3Dot( Dir, Dir );
  95. XMVECTOR t = XMVectorMultiply( Projection, XMVectorReciprocal( LengthSq ) );
  96. XMVECTOR Point = XMVectorMultiplyAdd( t, Dir, S1 );
  97. // t < 0
  98. XMVECTOR SelectS1 = XMVectorLess( Projection, XMVectorZero() );
  99. Point = XMVectorSelect( Point, S1, SelectS1 );
  100. // t > 1
  101. XMVECTOR SelectS2 = XMVectorGreater( Projection, LengthSq );
  102. Point = XMVectorSelect( Point, S2, SelectS2 );
  103. return Point;
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Test if the point (P) on the plane of the triangle is inside the triangle
  107. // (V0, V1, V2).
  108. //-----------------------------------------------------------------------------
  109. inline XMVECTOR XM_CALLCONV PointOnPlaneInsideTriangle( _In_ FXMVECTOR P, _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ GXMVECTOR V2 )
  110. {
  111. // Compute the triangle normal.
  112. XMVECTOR N = XMVector3Cross( XMVectorSubtract( V2, V0 ), XMVectorSubtract( V1, V0 ) );
  113. // Compute the cross products of the vector from the base of each edge to
  114. // the point with each edge vector.
  115. XMVECTOR C0 = XMVector3Cross( XMVectorSubtract( P, V0 ), XMVectorSubtract( V1, V0 ) );
  116. XMVECTOR C1 = XMVector3Cross( XMVectorSubtract( P, V1 ), XMVectorSubtract( V2, V1 ) );
  117. XMVECTOR C2 = XMVector3Cross( XMVectorSubtract( P, V2 ), XMVectorSubtract( V0, V2 ) );
  118. // If the cross product points in the same direction as the normal the the
  119. // point is inside the edge (it is zero if is on the edge).
  120. XMVECTOR Zero = XMVectorZero();
  121. XMVECTOR Inside0 = XMVectorGreaterOrEqual( XMVector3Dot( C0, N ), Zero );
  122. XMVECTOR Inside1 = XMVectorGreaterOrEqual( XMVector3Dot( C1, N ), Zero );
  123. XMVECTOR Inside2 = XMVectorGreaterOrEqual( XMVector3Dot( C2, N ), Zero );
  124. // If the point inside all of the edges it is inside.
  125. return XMVectorAndInt( XMVectorAndInt( Inside0, Inside1 ), Inside2 );
  126. }
  127. //-----------------------------------------------------------------------------
  128. inline bool SolveCubic( _In_ float e, _In_ float f, _In_ float g, _Out_ float* t, _Out_ float* u, _Out_ float* v )
  129. {
  130. float p, q, h, rc, d, theta, costh3, sinth3;
  131. p = f - e * e / 3.0f;
  132. q = g - e * f / 3.0f + e * e * e * 2.0f / 27.0f;
  133. h = q * q / 4.0f + p * p * p / 27.0f;
  134. if( h > 0.0 )
  135. {
  136. *t = *u = *v = 0.f;
  137. return false; // only one real root
  138. }
  139. if( ( h == 0.0 ) && ( q == 0.0 ) ) // all the same root
  140. {
  141. *t = - e / 3;
  142. *u = - e / 3;
  143. *v = - e / 3;
  144. return true;
  145. }
  146. d = sqrtf( q * q / 4.0f - h );
  147. if( d < 0 )
  148. rc = -powf( -d, 1.0f / 3.0f );
  149. else
  150. rc = powf( d, 1.0f / 3.0f );
  151. theta = XMScalarACos( -q / ( 2.0f * d ) );
  152. costh3 = XMScalarCos( theta / 3.0f );
  153. sinth3 = sqrtf( 3.0f ) * XMScalarSin( theta / 3.0f );
  154. *t = 2.0f * rc * costh3 - e / 3.0f;
  155. *u = -rc * ( costh3 + sinth3 ) - e / 3.0f;
  156. *v = -rc * ( costh3 - sinth3 ) - e / 3.0f;
  157. return true;
  158. }
  159. //-----------------------------------------------------------------------------
  160. inline XMVECTOR CalculateEigenVector( _In_ float m11, _In_ float m12, _In_ float m13,
  161. _In_ float m22, _In_ float m23, _In_ float m33, _In_ float e )
  162. {
  163. float fTmp[3];
  164. fTmp[0] = ( float )( m12 * m23 - m13 * ( m22 - e ) );
  165. fTmp[1] = ( float )( m13 * m12 - m23 * ( m11 - e ) );
  166. fTmp[2] = ( float )( ( m11 - e ) * ( m22 - e ) - m12 * m12 );
  167. XMVECTOR vTmp = XMLoadFloat3( reinterpret_cast<const XMFLOAT3*>(fTmp) );
  168. if( XMVector3Equal( vTmp, XMVectorZero() ) ) // planar or linear
  169. {
  170. float f1, f2, f3;
  171. // we only have one equation - find a valid one
  172. if( ( m11 - e != 0.0 ) || ( m12 != 0.0 ) || ( m13 != 0.0 ) )
  173. {
  174. f1 = m11 - e; f2 = m12; f3 = m13;
  175. }
  176. else if( ( m12 != 0.0 ) || ( m22 - e != 0.0 ) || ( m23 != 0.0 ) )
  177. {
  178. f1 = m12; f2 = m22 - e; f3 = m23;
  179. }
  180. else if( ( m13 != 0.0 ) || ( m23 != 0.0 ) || ( m33 - e != 0.0 ) )
  181. {
  182. f1 = m13; f2 = m23; f3 = m33 - e;
  183. }
  184. else
  185. {
  186. // error, we'll just make something up - we have NO context
  187. f1 = 1.0; f2 = 0.0; f3 = 0.0;
  188. }
  189. if( f1 == 0.0 )
  190. vTmp = XMVectorSetX( vTmp, 0.0f );
  191. else
  192. vTmp = XMVectorSetX( vTmp, 1.0f );
  193. if( f2 == 0.0 )
  194. vTmp = XMVectorSetY( vTmp, 0.0f );
  195. else
  196. vTmp = XMVectorSetY( vTmp, 1.0f );
  197. if( f3 == 0.0 )
  198. {
  199. vTmp = XMVectorSetZ( vTmp, 0.0f );
  200. // recalculate y to make equation work
  201. if( m12 != 0.0 )
  202. vTmp = XMVectorSetY( vTmp, ( float )( -f1 / f2 ) );
  203. }
  204. else
  205. {
  206. vTmp = XMVectorSetZ( vTmp, ( float )( ( f2 - f1 ) / f3 ) );
  207. }
  208. }
  209. if( XMVectorGetX( XMVector3LengthSq( vTmp ) ) > 1e-5f )
  210. {
  211. return XMVector3Normalize( vTmp );
  212. }
  213. else
  214. {
  215. // Multiply by a value large enough to make the vector non-zero.
  216. vTmp = XMVectorScale( vTmp, 1e5f );
  217. return XMVector3Normalize( vTmp );
  218. }
  219. }
  220. //-----------------------------------------------------------------------------
  221. inline bool CalculateEigenVectors( _In_ float m11, _In_ float m12, _In_ float m13,
  222. _In_ float m22, _In_ float m23, _In_ float m33,
  223. _In_ float e1, _In_ float e2, _In_ float e3,
  224. _Out_ XMVECTOR* pV1, _Out_ XMVECTOR* pV2, _Out_ XMVECTOR* pV3 )
  225. {
  226. *pV1 = DirectX::Internal::CalculateEigenVector( m11, m12, m13, m22, m23, m33, e1 );
  227. *pV2 = DirectX::Internal::CalculateEigenVector( m11, m12, m13, m22, m23, m33, e2 );
  228. *pV3 = DirectX::Internal::CalculateEigenVector( m11, m12, m13, m22, m23, m33, e3 );
  229. bool v1z = false;
  230. bool v2z = false;
  231. bool v3z = false;
  232. XMVECTOR Zero = XMVectorZero();
  233. if ( XMVector3Equal( *pV1, Zero ) )
  234. v1z = true;
  235. if ( XMVector3Equal( *pV2, Zero ) )
  236. v2z = true;
  237. if ( XMVector3Equal( *pV3, Zero ))
  238. v3z = true;
  239. bool e12 = ( fabsf( XMVectorGetX( XMVector3Dot( *pV1, *pV2 ) ) ) > 0.1f ); // check for non-orthogonal vectors
  240. bool e13 = ( fabsf( XMVectorGetX( XMVector3Dot( *pV1, *pV3 ) ) ) > 0.1f );
  241. bool e23 = ( fabsf( XMVectorGetX( XMVector3Dot( *pV2, *pV3 ) ) ) > 0.1f );
  242. if( ( v1z && v2z && v3z ) || ( e12 && e13 && e23 ) ||
  243. ( e12 && v3z ) || ( e13 && v2z ) || ( e23 && v1z ) ) // all eigenvectors are 0- any basis set
  244. {
  245. *pV1 = g_XMIdentityR0.v;
  246. *pV2 = g_XMIdentityR1.v;
  247. *pV3 = g_XMIdentityR2.v;
  248. return true;
  249. }
  250. if( v1z && v2z )
  251. {
  252. XMVECTOR vTmp = XMVector3Cross( g_XMIdentityR1, *pV3 );
  253. if( XMVectorGetX( XMVector3LengthSq( vTmp ) ) < 1e-5f )
  254. {
  255. vTmp = XMVector3Cross( g_XMIdentityR0, *pV3 );
  256. }
  257. *pV1 = XMVector3Normalize( vTmp );
  258. *pV2 = XMVector3Cross( *pV3, *pV1 );
  259. return true;
  260. }
  261. if( v3z && v1z )
  262. {
  263. XMVECTOR vTmp = XMVector3Cross( g_XMIdentityR1, *pV2 );
  264. if( XMVectorGetX( XMVector3LengthSq( vTmp ) ) < 1e-5f )
  265. {
  266. vTmp = XMVector3Cross( g_XMIdentityR0, *pV2 );
  267. }
  268. *pV3 = XMVector3Normalize( vTmp );
  269. *pV1 = XMVector3Cross( *pV2, *pV3 );
  270. return true;
  271. }
  272. if( v2z && v3z )
  273. {
  274. XMVECTOR vTmp = XMVector3Cross( g_XMIdentityR1, *pV1 );
  275. if( XMVectorGetX( XMVector3LengthSq( vTmp ) ) < 1e-5f )
  276. {
  277. vTmp = XMVector3Cross( g_XMIdentityR0, *pV1 );
  278. }
  279. *pV2 = XMVector3Normalize( vTmp );
  280. *pV3 = XMVector3Cross( *pV1, *pV2 );
  281. return true;
  282. }
  283. if( ( v1z ) || e12 )
  284. {
  285. *pV1 = XMVector3Cross( *pV2, *pV3 );
  286. return true;
  287. }
  288. if( ( v2z ) || e23 )
  289. {
  290. *pV2 = XMVector3Cross( *pV3, *pV1 );
  291. return true;
  292. }
  293. if( ( v3z ) || e13 )
  294. {
  295. *pV3 = XMVector3Cross( *pV1, *pV2 );
  296. return true;
  297. }
  298. return true;
  299. }
  300. //-----------------------------------------------------------------------------
  301. inline bool CalculateEigenVectorsFromCovarianceMatrix( _In_ float Cxx, _In_ float Cyy, _In_ float Czz,
  302. _In_ float Cxy, _In_ float Cxz, _In_ float Cyz,
  303. _Out_ XMVECTOR* pV1, _Out_ XMVECTOR* pV2, _Out_ XMVECTOR* pV3 )
  304. {
  305. // Calculate the eigenvalues by solving a cubic equation.
  306. float e = -( Cxx + Cyy + Czz );
  307. float f = Cxx * Cyy + Cyy * Czz + Czz * Cxx - Cxy * Cxy - Cxz * Cxz - Cyz * Cyz;
  308. float g = Cxy * Cxy * Czz + Cxz * Cxz * Cyy + Cyz * Cyz * Cxx - Cxy * Cyz * Cxz * 2.0f - Cxx * Cyy * Czz;
  309. float ev1, ev2, ev3;
  310. if( !DirectX::Internal::SolveCubic( e, f, g, &ev1, &ev2, &ev3 ) )
  311. {
  312. // set them to arbitrary orthonormal basis set
  313. *pV1 = g_XMIdentityR0.v;
  314. *pV2 = g_XMIdentityR1.v;
  315. *pV3 = g_XMIdentityR2.v;
  316. return false;
  317. }
  318. return DirectX::Internal::CalculateEigenVectors( Cxx, Cxy, Cxz, Cyy, Cyz, Czz, ev1, ev2, ev3, pV1, pV2, pV3 );
  319. }
  320. //-----------------------------------------------------------------------------
  321. inline void XM_CALLCONV FastIntersectTrianglePlane( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2, GXMVECTOR Plane,
  322. XMVECTOR& Outside, XMVECTOR& Inside )
  323. {
  324. // Plane0
  325. XMVECTOR Dist0 = XMVector4Dot( V0, Plane );
  326. XMVECTOR Dist1 = XMVector4Dot( V1, Plane );
  327. XMVECTOR Dist2 = XMVector4Dot( V2, Plane );
  328. XMVECTOR MinDist = XMVectorMin( Dist0, Dist1 );
  329. MinDist = XMVectorMin( MinDist, Dist2 );
  330. XMVECTOR MaxDist = XMVectorMax( Dist0, Dist1 );
  331. MaxDist = XMVectorMax( MaxDist, Dist2 );
  332. XMVECTOR Zero = XMVectorZero();
  333. // Outside the plane?
  334. Outside = XMVectorGreater( MinDist, Zero );
  335. // Fully inside the plane?
  336. Inside = XMVectorLess( MaxDist, Zero );
  337. }
  338. //-----------------------------------------------------------------------------
  339. inline void FastIntersectSpherePlane( _In_ FXMVECTOR Center, _In_ FXMVECTOR Radius, _In_ FXMVECTOR Plane,
  340. _Out_ XMVECTOR& Outside, _Out_ XMVECTOR& Inside )
  341. {
  342. XMVECTOR Dist = XMVector4Dot( Center, Plane );
  343. // Outside the plane?
  344. Outside = XMVectorGreater( Dist, Radius );
  345. // Fully inside the plane?
  346. Inside = XMVectorLess( Dist, XMVectorNegate( Radius ) );
  347. }
  348. //-----------------------------------------------------------------------------
  349. inline void FastIntersectAxisAlignedBoxPlane( _In_ FXMVECTOR Center, _In_ FXMVECTOR Extents, _In_ FXMVECTOR Plane,
  350. _Out_ XMVECTOR& Outside, _Out_ XMVECTOR& Inside )
  351. {
  352. // Compute the distance to the center of the box.
  353. XMVECTOR Dist = XMVector4Dot( Center, Plane );
  354. // Project the axes of the box onto the normal of the plane. Half the
  355. // length of the projection (sometime called the "radius") is equal to
  356. // h(u) * abs(n dot b(u))) + h(v) * abs(n dot b(v)) + h(w) * abs(n dot b(w))
  357. // where h(i) are extents of the box, n is the plane normal, and b(i) are the
  358. // axes of the box. In this case b(i) = [(1,0,0), (0,1,0), (0,0,1)].
  359. XMVECTOR Radius = XMVector3Dot( Extents, XMVectorAbs( Plane ) );
  360. // Outside the plane?
  361. Outside = XMVectorGreater( Dist, Radius );
  362. // Fully inside the plane?
  363. Inside = XMVectorLess( Dist, XMVectorNegate( Radius ) );
  364. }
  365. //-----------------------------------------------------------------------------
  366. inline void XM_CALLCONV FastIntersectOrientedBoxPlane( _In_ FXMVECTOR Center, _In_ FXMVECTOR Extents, _In_ FXMVECTOR Axis0, _In_ GXMVECTOR Axis1,
  367. _In_ HXMVECTOR Axis2, _In_ HXMVECTOR Plane, _Out_ XMVECTOR& Outside, _Out_ XMVECTOR& Inside )
  368. {
  369. // Compute the distance to the center of the box.
  370. XMVECTOR Dist = XMVector4Dot( Center, Plane );
  371. // Project the axes of the box onto the normal of the plane. Half the
  372. // length of the projection (sometime called the "radius") is equal to
  373. // h(u) * abs(n dot b(u))) + h(v) * abs(n dot b(v)) + h(w) * abs(n dot b(w))
  374. // where h(i) are extents of the box, n is the plane normal, and b(i) are the
  375. // axes of the box.
  376. XMVECTOR Radius = XMVector3Dot( Plane, Axis0 );
  377. Radius = XMVectorInsert<0, 0, 1, 0, 0>( Radius, XMVector3Dot( Plane, Axis1 ) );
  378. Radius = XMVectorInsert<0, 0, 0, 1, 0>( Radius, XMVector3Dot( Plane, Axis2 ) );
  379. Radius = XMVector3Dot( Extents, XMVectorAbs( Radius ) );
  380. // Outside the plane?
  381. Outside = XMVectorGreater( Dist, Radius );
  382. // Fully inside the plane?
  383. Inside = XMVectorLess( Dist, XMVectorNegate( Radius ) );
  384. }
  385. //-----------------------------------------------------------------------------
  386. inline void XM_CALLCONV FastIntersectFrustumPlane( _In_ FXMVECTOR Point0, _In_ FXMVECTOR Point1, _In_ FXMVECTOR Point2, _In_ GXMVECTOR Point3,
  387. _In_ HXMVECTOR Point4, _In_ HXMVECTOR Point5, _In_ CXMVECTOR Point6, _In_ CXMVECTOR Point7,
  388. _In_ CXMVECTOR Plane, _Out_ XMVECTOR& Outside, _Out_ XMVECTOR& Inside )
  389. {
  390. // Find the min/max projection of the frustum onto the plane normal.
  391. XMVECTOR Min, Max, Dist;
  392. Min = Max = XMVector3Dot( Plane, Point0 );
  393. Dist = XMVector3Dot( Plane, Point1 );
  394. Min = XMVectorMin( Min, Dist );
  395. Max = XMVectorMax( Max, Dist );
  396. Dist = XMVector3Dot( Plane, Point2 );
  397. Min = XMVectorMin( Min, Dist );
  398. Max = XMVectorMax( Max, Dist );
  399. Dist = XMVector3Dot( Plane, Point3 );
  400. Min = XMVectorMin( Min, Dist );
  401. Max = XMVectorMax( Max, Dist );
  402. Dist = XMVector3Dot( Plane, Point4 );
  403. Min = XMVectorMin( Min, Dist );
  404. Max = XMVectorMax( Max, Dist );
  405. Dist = XMVector3Dot( Plane, Point5 );
  406. Min = XMVectorMin( Min, Dist );
  407. Max = XMVectorMax( Max, Dist );
  408. Dist = XMVector3Dot( Plane, Point6 );
  409. Min = XMVectorMin( Min, Dist );
  410. Max = XMVectorMax( Max, Dist );
  411. Dist = XMVector3Dot( Plane, Point7 );
  412. Min = XMVectorMin( Min, Dist );
  413. Max = XMVectorMax( Max, Dist );
  414. XMVECTOR PlaneDist = XMVectorNegate( XMVectorSplatW( Plane ) );
  415. // Outside the plane?
  416. Outside = XMVectorGreater( Min, PlaneDist );
  417. // Fully inside the plane?
  418. Inside = XMVectorLess( Max, PlaneDist );
  419. }
  420. }; // namespace Internal
  421. /****************************************************************************
  422. *
  423. * BoundingSphere
  424. *
  425. ****************************************************************************/
  426. //-----------------------------------------------------------------------------
  427. // Transform a sphere by an angle preserving transform.
  428. //-----------------------------------------------------------------------------
  429. _Use_decl_annotations_
  430. inline void XM_CALLCONV BoundingSphere::Transform( BoundingSphere& Out, FXMMATRIX M ) const
  431. {
  432. // Load the center of the sphere.
  433. XMVECTOR vCenter = XMLoadFloat3( &Center );
  434. // Transform the center of the sphere.
  435. XMVECTOR C = XMVector3Transform( vCenter, M );
  436. XMVECTOR dX = XMVector3Dot( M.r[0], M.r[0] );
  437. XMVECTOR dY = XMVector3Dot( M.r[1], M.r[1] );
  438. XMVECTOR dZ = XMVector3Dot( M.r[2], M.r[2] );
  439. XMVECTOR d = XMVectorMax( dX, XMVectorMax( dY, dZ ) );
  440. // Store the center sphere.
  441. XMStoreFloat3( &Out.Center, C );
  442. // Scale the radius of the pshere.
  443. float Scale = sqrtf( XMVectorGetX(d) );
  444. Out.Radius = Radius * Scale;
  445. }
  446. _Use_decl_annotations_
  447. inline void XM_CALLCONV BoundingSphere::Transform( BoundingSphere& Out, float Scale, FXMVECTOR Rotation, FXMVECTOR Translation ) const
  448. {
  449. // Load the center of the sphere.
  450. XMVECTOR vCenter = XMLoadFloat3( &Center );
  451. // Transform the center of the sphere.
  452. vCenter = XMVectorAdd( XMVector3Rotate( XMVectorScale( vCenter, Scale ), Rotation ), Translation );
  453. // Store the center sphere.
  454. XMStoreFloat3( &Out.Center, vCenter );
  455. // Scale the radius of the pshere.
  456. Out.Radius = Radius * Scale;
  457. }
  458. //-----------------------------------------------------------------------------
  459. // Point in sphere test.
  460. //-----------------------------------------------------------------------------
  461. _Use_decl_annotations_
  462. inline ContainmentType XM_CALLCONV BoundingSphere::Contains( FXMVECTOR Point ) const
  463. {
  464. XMVECTOR vCenter = XMLoadFloat3( &Center );
  465. XMVECTOR vRadius = XMVectorReplicatePtr( &Radius );
  466. XMVECTOR DistanceSquared = XMVector3LengthSq( XMVectorSubtract( Point, vCenter ) );
  467. XMVECTOR RadiusSquared = XMVectorMultiply( vRadius, vRadius );
  468. return XMVector3LessOrEqual( DistanceSquared, RadiusSquared ) ? CONTAINS : DISJOINT;
  469. }
  470. //-----------------------------------------------------------------------------
  471. // Triangle in sphere test
  472. //-----------------------------------------------------------------------------
  473. _Use_decl_annotations_
  474. inline ContainmentType XM_CALLCONV BoundingSphere::Contains( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2 ) const
  475. {
  476. if ( !Intersects(V0,V1,V2) )
  477. return DISJOINT;
  478. XMVECTOR vCenter = XMLoadFloat3( &Center );
  479. XMVECTOR vRadius = XMVectorReplicatePtr( &Radius );
  480. XMVECTOR RadiusSquared = XMVectorMultiply( vRadius, vRadius );
  481. XMVECTOR DistanceSquared = XMVector3LengthSq( XMVectorSubtract( V0, vCenter ) );
  482. XMVECTOR Inside = XMVectorLessOrEqual(DistanceSquared, RadiusSquared);
  483. DistanceSquared = XMVector3LengthSq( XMVectorSubtract( V1, vCenter ) );
  484. Inside = XMVectorAndInt( Inside, XMVectorLessOrEqual(DistanceSquared, RadiusSquared) );
  485. DistanceSquared = XMVector3LengthSq( XMVectorSubtract( V2, vCenter ) );
  486. Inside = XMVectorAndInt( Inside, XMVectorLessOrEqual(DistanceSquared, RadiusSquared) );
  487. return ( XMVector3EqualInt( Inside, XMVectorTrueInt() ) ) ? CONTAINS : INTERSECTS;
  488. }
  489. //-----------------------------------------------------------------------------
  490. // Sphere in sphere test.
  491. //-----------------------------------------------------------------------------
  492. _Use_decl_annotations_
  493. inline ContainmentType BoundingSphere::Contains( const BoundingSphere& sh ) const
  494. {
  495. XMVECTOR Center1 = XMLoadFloat3( &Center );
  496. float r1 = Radius;
  497. XMVECTOR Center2 = XMLoadFloat3( &sh.Center );
  498. float r2 = sh.Radius;
  499. XMVECTOR V = XMVectorSubtract( Center2, Center1 );
  500. XMVECTOR Dist = XMVector3Length( V );
  501. float d = XMVectorGetX( Dist );
  502. return (r1 + r2 >= d) ? ((r1 - r2 >= d) ? CONTAINS : INTERSECTS) : DISJOINT;
  503. }
  504. //-----------------------------------------------------------------------------
  505. // Axis-aligned box in sphere test
  506. //-----------------------------------------------------------------------------
  507. _Use_decl_annotations_
  508. inline ContainmentType BoundingSphere::Contains( const BoundingBox& box ) const
  509. {
  510. if ( !box.Intersects(*this) )
  511. return DISJOINT;
  512. XMVECTOR vCenter = XMLoadFloat3( &Center );
  513. XMVECTOR vRadius = XMVectorReplicatePtr( &Radius );
  514. XMVECTOR RadiusSq = XMVectorMultiply( vRadius, vRadius );
  515. XMVECTOR boxCenter = XMLoadFloat3( &box.Center );
  516. XMVECTOR boxExtents = XMLoadFloat3( &box.Extents );
  517. XMVECTOR InsideAll = XMVectorTrueInt();
  518. XMVECTOR offset = XMVectorSubtract( boxCenter, vCenter );
  519. for( size_t i = 0; i < BoundingBox::CORNER_COUNT; ++i )
  520. {
  521. XMVECTOR C = XMVectorMultiplyAdd( boxExtents, g_BoxOffset[i], offset );
  522. XMVECTOR d = XMVector3LengthSq( C );
  523. InsideAll = XMVectorAndInt( InsideAll, XMVectorLessOrEqual( d, RadiusSq ) );
  524. }
  525. return ( XMVector3EqualInt( InsideAll, XMVectorTrueInt() ) ) ? CONTAINS : INTERSECTS;
  526. }
  527. //-----------------------------------------------------------------------------
  528. // Oriented box in sphere test
  529. //-----------------------------------------------------------------------------
  530. _Use_decl_annotations_
  531. inline ContainmentType BoundingSphere::Contains( const BoundingOrientedBox& box ) const
  532. {
  533. if ( !box.Intersects(*this) )
  534. return DISJOINT;
  535. XMVECTOR vCenter = XMLoadFloat3( &Center );
  536. XMVECTOR vRadius = XMVectorReplicatePtr( &Radius );
  537. XMVECTOR RadiusSq = XMVectorMultiply( vRadius, vRadius );
  538. XMVECTOR boxCenter = XMLoadFloat3( &box.Center );
  539. XMVECTOR boxExtents = XMLoadFloat3( &box.Extents );
  540. XMVECTOR boxOrientation = XMLoadFloat4( &box.Orientation );
  541. assert( DirectX::Internal::XMQuaternionIsUnit( boxOrientation ) );
  542. XMVECTOR InsideAll = XMVectorTrueInt();
  543. for( size_t i = 0; i < BoundingOrientedBox::CORNER_COUNT; ++i )
  544. {
  545. XMVECTOR C = XMVectorAdd( XMVector3Rotate( XMVectorMultiply( boxExtents, g_BoxOffset[i] ), boxOrientation ), boxCenter );
  546. XMVECTOR d = XMVector3LengthSq( XMVectorSubtract( vCenter, C ) );
  547. InsideAll = XMVectorAndInt( InsideAll, XMVectorLessOrEqual( d, RadiusSq ) );
  548. }
  549. return ( XMVector3EqualInt( InsideAll, XMVectorTrueInt() ) ) ? CONTAINS : INTERSECTS;
  550. }
  551. //-----------------------------------------------------------------------------
  552. // Frustum in sphere test
  553. //-----------------------------------------------------------------------------
  554. _Use_decl_annotations_
  555. inline ContainmentType BoundingSphere::Contains( const BoundingFrustum& fr ) const
  556. {
  557. if ( !fr.Intersects(*this) )
  558. return DISJOINT;
  559. XMVECTOR vCenter = XMLoadFloat3( &Center );
  560. XMVECTOR vRadius = XMVectorReplicatePtr( &Radius );
  561. XMVECTOR RadiusSq = XMVectorMultiply( vRadius, vRadius );
  562. XMVECTOR vOrigin = XMLoadFloat3( &fr.Origin );
  563. XMVECTOR vOrientation = XMLoadFloat4( &fr.Orientation );
  564. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  565. // Build the corners of the frustum.
  566. XMVECTOR vRightTop = XMVectorSet( fr.RightSlope, fr.TopSlope, 1.0f, 0.0f );
  567. XMVECTOR vRightBottom = XMVectorSet( fr.RightSlope, fr.BottomSlope, 1.0f, 0.0f );
  568. XMVECTOR vLeftTop = XMVectorSet( fr.LeftSlope, fr.TopSlope, 1.0f, 0.0f );
  569. XMVECTOR vLeftBottom = XMVectorSet( fr.LeftSlope, fr.BottomSlope, 1.0f, 0.0f );
  570. XMVECTOR vNear = XMVectorReplicatePtr( &fr.Near );
  571. XMVECTOR vFar = XMVectorReplicatePtr( &fr.Far );
  572. XMVECTOR Corners[BoundingFrustum::CORNER_COUNT];
  573. Corners[0] = XMVectorMultiply( vRightTop, vNear );
  574. Corners[1] = XMVectorMultiply( vRightBottom, vNear );
  575. Corners[2] = XMVectorMultiply( vLeftTop, vNear );
  576. Corners[3] = XMVectorMultiply( vLeftBottom, vNear );
  577. Corners[4] = XMVectorMultiply( vRightTop, vFar );
  578. Corners[5] = XMVectorMultiply( vRightBottom, vFar );
  579. Corners[6] = XMVectorMultiply( vLeftTop, vFar );
  580. Corners[7] = XMVectorMultiply( vLeftBottom, vFar );
  581. XMVECTOR InsideAll = XMVectorTrueInt();
  582. for( size_t i = 0; i < BoundingFrustum::CORNER_COUNT; ++i )
  583. {
  584. XMVECTOR C = XMVectorAdd( XMVector3Rotate( Corners[i], vOrientation ), vOrigin );
  585. XMVECTOR d = XMVector3LengthSq( XMVectorSubtract( vCenter, C ) );
  586. InsideAll = XMVectorAndInt( InsideAll, XMVectorLessOrEqual( d, RadiusSq ) );
  587. }
  588. return ( XMVector3EqualInt( InsideAll, XMVectorTrueInt() ) ) ? CONTAINS : INTERSECTS;
  589. }
  590. //-----------------------------------------------------------------------------
  591. // Sphere vs. sphere test.
  592. //-----------------------------------------------------------------------------
  593. _Use_decl_annotations_
  594. inline bool BoundingSphere::Intersects( const BoundingSphere& sh ) const
  595. {
  596. // Load A.
  597. XMVECTOR vCenterA = XMLoadFloat3( &Center );
  598. XMVECTOR vRadiusA = XMVectorReplicatePtr( &Radius );
  599. // Load B.
  600. XMVECTOR vCenterB = XMLoadFloat3( &sh.Center );
  601. XMVECTOR vRadiusB = XMVectorReplicatePtr( &sh.Radius );
  602. // Distance squared between centers.
  603. XMVECTOR Delta = XMVectorSubtract( vCenterB, vCenterA );
  604. XMVECTOR DistanceSquared = XMVector3LengthSq( Delta );
  605. // Sum of the radii squared.
  606. XMVECTOR RadiusSquared = XMVectorAdd( vRadiusA, vRadiusB );
  607. RadiusSquared = XMVectorMultiply( RadiusSquared, RadiusSquared );
  608. return XMVector3LessOrEqual( DistanceSquared, RadiusSquared );
  609. }
  610. //-----------------------------------------------------------------------------
  611. // Box vs. sphere test.
  612. //-----------------------------------------------------------------------------
  613. _Use_decl_annotations_
  614. inline bool BoundingSphere::Intersects( const BoundingBox& box ) const
  615. {
  616. return box.Intersects( *this );
  617. }
  618. _Use_decl_annotations_
  619. inline bool BoundingSphere::Intersects( const BoundingOrientedBox& box ) const
  620. {
  621. return box.Intersects( *this );
  622. }
  623. //-----------------------------------------------------------------------------
  624. // Frustum vs. sphere test.
  625. //-----------------------------------------------------------------------------
  626. _Use_decl_annotations_
  627. inline bool BoundingSphere::Intersects( const BoundingFrustum& fr ) const
  628. {
  629. return fr.Intersects( *this );
  630. }
  631. //-----------------------------------------------------------------------------
  632. // Triangle vs sphere test
  633. //-----------------------------------------------------------------------------
  634. _Use_decl_annotations_
  635. inline bool XM_CALLCONV BoundingSphere::Intersects( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2 ) const
  636. {
  637. // Load the sphere.
  638. XMVECTOR vCenter = XMLoadFloat3( &Center );
  639. XMVECTOR vRadius = XMVectorReplicatePtr( &Radius );
  640. // Compute the plane of the triangle (has to be normalized).
  641. XMVECTOR N = XMVector3Normalize( XMVector3Cross( XMVectorSubtract( V1, V0 ), XMVectorSubtract( V2, V0 ) ) );
  642. // Assert that the triangle is not degenerate.
  643. assert( !XMVector3Equal( N, XMVectorZero() ) );
  644. // Find the nearest feature on the triangle to the sphere.
  645. XMVECTOR Dist = XMVector3Dot( XMVectorSubtract( vCenter, V0 ), N );
  646. // If the center of the sphere is farther from the plane of the triangle than
  647. // the radius of the sphere, then there cannot be an intersection.
  648. XMVECTOR NoIntersection = XMVectorLess( Dist, XMVectorNegate( vRadius ) );
  649. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( Dist, vRadius ) );
  650. // Project the center of the sphere onto the plane of the triangle.
  651. XMVECTOR Point = XMVectorNegativeMultiplySubtract( N, Dist, vCenter );
  652. // Is it inside all the edges? If so we intersect because the distance
  653. // to the plane is less than the radius.
  654. XMVECTOR Intersection = DirectX::Internal::PointOnPlaneInsideTriangle( Point, V0, V1, V2 );
  655. // Find the nearest point on each edge.
  656. XMVECTOR RadiusSq = XMVectorMultiply( vRadius, vRadius );
  657. // Edge 0,1
  658. Point = DirectX::Internal::PointOnLineSegmentNearestPoint( V0, V1, vCenter );
  659. // If the distance to the center of the sphere to the point is less than
  660. // the radius of the sphere then it must intersect.
  661. Intersection = XMVectorOrInt( Intersection, XMVectorLessOrEqual( XMVector3LengthSq( XMVectorSubtract( vCenter, Point ) ), RadiusSq ) );
  662. // Edge 1,2
  663. Point = DirectX::Internal::PointOnLineSegmentNearestPoint( V1, V2, vCenter );
  664. // If the distance to the center of the sphere to the point is less than
  665. // the radius of the sphere then it must intersect.
  666. Intersection = XMVectorOrInt( Intersection, XMVectorLessOrEqual( XMVector3LengthSq( XMVectorSubtract( vCenter, Point ) ), RadiusSq ) );
  667. // Edge 2,0
  668. Point = DirectX::Internal::PointOnLineSegmentNearestPoint( V2, V0, vCenter );
  669. // If the distance to the center of the sphere to the point is less than
  670. // the radius of the sphere then it must intersect.
  671. Intersection = XMVectorOrInt( Intersection, XMVectorLessOrEqual( XMVector3LengthSq( XMVectorSubtract( vCenter, Point ) ), RadiusSq ) );
  672. return XMVector4EqualInt( XMVectorAndCInt( Intersection, NoIntersection ), XMVectorTrueInt() );
  673. }
  674. //-----------------------------------------------------------------------------
  675. // Sphere-plane intersection
  676. //-----------------------------------------------------------------------------
  677. _Use_decl_annotations_
  678. inline PlaneIntersectionType XM_CALLCONV BoundingSphere::Intersects( FXMVECTOR Plane ) const
  679. {
  680. assert( DirectX::Internal::XMPlaneIsUnit( Plane ) );
  681. // Load the sphere.
  682. XMVECTOR vCenter = XMLoadFloat3( &Center );
  683. XMVECTOR vRadius = XMVectorReplicatePtr( &Radius );
  684. // Set w of the center to one so we can dot4 with a plane.
  685. vCenter = XMVectorInsert<0, 0, 0, 0, 1>( vCenter, XMVectorSplatOne() );
  686. XMVECTOR Outside, Inside;
  687. DirectX::Internal::FastIntersectSpherePlane( vCenter, vRadius, Plane, Outside, Inside );
  688. // If the sphere is outside any plane it is outside.
  689. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  690. return FRONT;
  691. // If the sphere is inside all planes it is inside.
  692. if ( XMVector4EqualInt( Inside, XMVectorTrueInt() ) )
  693. return BACK;
  694. // The sphere is not inside all planes or outside a plane it intersects.
  695. return INTERSECTING;
  696. }
  697. //-----------------------------------------------------------------------------
  698. // Compute the intersection of a ray (Origin, Direction) with a sphere.
  699. //-----------------------------------------------------------------------------
  700. _Use_decl_annotations_
  701. inline bool XM_CALLCONV BoundingSphere::Intersects( FXMVECTOR Origin, FXMVECTOR Direction, float& Dist ) const
  702. {
  703. assert( DirectX::Internal::XMVector3IsUnit( Direction ) );
  704. XMVECTOR vCenter = XMLoadFloat3( &Center );
  705. XMVECTOR vRadius = XMVectorReplicatePtr( &Radius );
  706. // l is the vector from the ray origin to the center of the sphere.
  707. XMVECTOR l = XMVectorSubtract( vCenter, Origin );
  708. // s is the projection of the l onto the ray direction.
  709. XMVECTOR s = XMVector3Dot( l, Direction );
  710. XMVECTOR l2 = XMVector3Dot( l, l );
  711. XMVECTOR r2 = XMVectorMultiply( vRadius, vRadius );
  712. // m2 is squared distance from the center of the sphere to the projection.
  713. XMVECTOR m2 = XMVectorNegativeMultiplySubtract( s, s, l2 );
  714. XMVECTOR NoIntersection;
  715. // If the ray origin is outside the sphere and the center of the sphere is
  716. // behind the ray origin there is no intersection.
  717. NoIntersection = XMVectorAndInt( XMVectorLess( s, XMVectorZero() ), XMVectorGreater( l2, r2 ) );
  718. // If the squared distance from the center of the sphere to the projection
  719. // is greater than the radius squared the ray will miss the sphere.
  720. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( m2, r2 ) );
  721. // The ray hits the sphere, compute the nearest intersection point.
  722. XMVECTOR q = XMVectorSqrt( XMVectorSubtract( r2, m2 ) );
  723. XMVECTOR t1 = XMVectorSubtract( s, q );
  724. XMVECTOR t2 = XMVectorAdd( s, q );
  725. XMVECTOR OriginInside = XMVectorLessOrEqual( l2, r2 );
  726. XMVECTOR t = XMVectorSelect( t1, t2, OriginInside );
  727. if( XMVector4NotEqualInt( NoIntersection, XMVectorTrueInt() ) )
  728. {
  729. // Store the x-component to *pDist.
  730. XMStoreFloat( &Dist, t );
  731. return true;
  732. }
  733. Dist = 0.f;
  734. return false;
  735. }
  736. //-----------------------------------------------------------------------------
  737. // Test a sphere vs 6 planes (typically forming a frustum).
  738. //-----------------------------------------------------------------------------
  739. _Use_decl_annotations_
  740. inline ContainmentType XM_CALLCONV BoundingSphere::ContainedBy( FXMVECTOR Plane0, FXMVECTOR Plane1, FXMVECTOR Plane2,
  741. GXMVECTOR Plane3, HXMVECTOR Plane4, HXMVECTOR Plane5 ) const
  742. {
  743. // Load the sphere.
  744. XMVECTOR vCenter = XMLoadFloat3( &Center );
  745. XMVECTOR vRadius = XMVectorReplicatePtr( &Radius );
  746. // Set w of the center to one so we can dot4 with a plane.
  747. vCenter = XMVectorInsert<0, 0, 0, 0, 1>( vCenter, XMVectorSplatOne() );
  748. XMVECTOR Outside, Inside;
  749. // Test against each plane.
  750. DirectX::Internal::FastIntersectSpherePlane( vCenter, vRadius, Plane0, Outside, Inside );
  751. XMVECTOR AnyOutside = Outside;
  752. XMVECTOR AllInside = Inside;
  753. DirectX::Internal::FastIntersectSpherePlane( vCenter, vRadius, Plane1, Outside, Inside );
  754. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  755. AllInside = XMVectorAndInt( AllInside, Inside );
  756. DirectX::Internal::FastIntersectSpherePlane( vCenter, vRadius, Plane2, Outside, Inside );
  757. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  758. AllInside = XMVectorAndInt( AllInside, Inside );
  759. DirectX::Internal::FastIntersectSpherePlane( vCenter, vRadius, Plane3, Outside, Inside );
  760. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  761. AllInside = XMVectorAndInt( AllInside, Inside );
  762. DirectX::Internal::FastIntersectSpherePlane( vCenter, vRadius, Plane4, Outside, Inside );
  763. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  764. AllInside = XMVectorAndInt( AllInside, Inside );
  765. DirectX::Internal::FastIntersectSpherePlane( vCenter, vRadius, Plane5, Outside, Inside );
  766. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  767. AllInside = XMVectorAndInt( AllInside, Inside );
  768. // If the sphere is outside any plane it is outside.
  769. if ( XMVector4EqualInt( AnyOutside, XMVectorTrueInt() ) )
  770. return DISJOINT;
  771. // If the sphere is inside all planes it is inside.
  772. if ( XMVector4EqualInt( AllInside, XMVectorTrueInt() ) )
  773. return CONTAINS;
  774. // The sphere is not inside all planes or outside a plane, it may intersect.
  775. return INTERSECTS;
  776. }
  777. //-----------------------------------------------------------------------------
  778. // Creates a bounding sphere that contains two other bounding spheres
  779. //-----------------------------------------------------------------------------
  780. _Use_decl_annotations_
  781. inline void BoundingSphere::CreateMerged( BoundingSphere& Out, const BoundingSphere& S1, const BoundingSphere& S2 )
  782. {
  783. XMVECTOR Center1 = XMLoadFloat3( &S1.Center );
  784. float r1 = S1.Radius;
  785. XMVECTOR Center2 = XMLoadFloat3( &S2.Center );
  786. float r2 = S2.Radius;
  787. XMVECTOR V = XMVectorSubtract( Center2, Center1 );
  788. XMVECTOR Dist = XMVector3Length( V );
  789. float d = XMVectorGetX(Dist);
  790. if ( r1 + r2 >= d )
  791. {
  792. if ( r1 - r2 >= d )
  793. {
  794. Out = S1;
  795. return;
  796. }
  797. else if ( r2 - r1 >= d )
  798. {
  799. Out = S2;
  800. return;
  801. }
  802. }
  803. XMVECTOR N = XMVectorDivide( V, Dist );
  804. float t1 = XMMin( -r1, d-r2 );
  805. float t2 = XMMax( r1, d+r2 );
  806. float t_5 = (t2 - t1) * 0.5f;
  807. XMVECTOR NCenter = XMVectorAdd( Center1, XMVectorMultiply( N, XMVectorReplicate( t_5 + t1 ) ) );
  808. XMStoreFloat3( &Out.Center, NCenter );
  809. Out.Radius = t_5;
  810. }
  811. //-----------------------------------------------------------------------------
  812. // Create sphere enscribing bounding box
  813. //-----------------------------------------------------------------------------
  814. _Use_decl_annotations_
  815. inline void BoundingSphere::CreateFromBoundingBox( BoundingSphere& Out, const BoundingBox& box )
  816. {
  817. Out.Center = box.Center;
  818. XMVECTOR vExtents = XMLoadFloat3( &box.Extents );
  819. Out.Radius = XMVectorGetX( XMVector3Length( vExtents ) );
  820. }
  821. _Use_decl_annotations_
  822. inline void BoundingSphere::CreateFromBoundingBox( BoundingSphere& Out, const BoundingOrientedBox& box )
  823. {
  824. // Bounding box orientation is irrelevant because a sphere is rotationally invariant
  825. Out.Center = box.Center;
  826. XMVECTOR vExtents = XMLoadFloat3( &box.Extents );
  827. Out.Radius = XMVectorGetX( XMVector3Length( vExtents ) );
  828. }
  829. //-----------------------------------------------------------------------------
  830. // Find the approximate smallest enclosing bounding sphere for a set of
  831. // points. Exact computation of the smallest enclosing bounding sphere is
  832. // possible but is slower and requires a more complex algorithm.
  833. // The algorithm is based on Jack Ritter, "An Efficient Bounding Sphere",
  834. // Graphics Gems.
  835. //-----------------------------------------------------------------------------
  836. _Use_decl_annotations_
  837. inline void BoundingSphere::CreateFromPoints( BoundingSphere& Out, size_t Count, const XMFLOAT3* pPoints, size_t Stride )
  838. {
  839. assert( Count > 0 );
  840. assert( pPoints );
  841. // Find the points with minimum and maximum x, y, and z
  842. XMVECTOR MinX, MaxX, MinY, MaxY, MinZ, MaxZ;
  843. MinX = MaxX = MinY = MaxY = MinZ = MaxZ = XMLoadFloat3( pPoints );
  844. for( size_t i = 1; i < Count; ++i )
  845. {
  846. XMVECTOR Point = XMLoadFloat3( reinterpret_cast<const XMFLOAT3*>( reinterpret_cast<const uint8_t*>(pPoints) + i * Stride ) );
  847. float px = XMVectorGetX( Point );
  848. float py = XMVectorGetY( Point );
  849. float pz = XMVectorGetZ( Point );
  850. if( px < XMVectorGetX( MinX ) )
  851. MinX = Point;
  852. if( px > XMVectorGetX( MaxX ) )
  853. MaxX = Point;
  854. if( py < XMVectorGetY( MinY ) )
  855. MinY = Point;
  856. if( py > XMVectorGetY( MaxY ) )
  857. MaxY = Point;
  858. if( pz < XMVectorGetZ( MinZ ) )
  859. MinZ = Point;
  860. if( pz > XMVectorGetZ( MaxZ ) )
  861. MaxZ = Point;
  862. }
  863. // Use the min/max pair that are farthest apart to form the initial sphere.
  864. XMVECTOR DeltaX = XMVectorSubtract( MaxX, MinX );
  865. XMVECTOR DistX = XMVector3Length( DeltaX );
  866. XMVECTOR DeltaY = XMVectorSubtract( MaxY, MinY );
  867. XMVECTOR DistY = XMVector3Length( DeltaY );
  868. XMVECTOR DeltaZ = XMVectorSubtract( MaxZ, MinZ );
  869. XMVECTOR DistZ = XMVector3Length( DeltaZ );
  870. XMVECTOR vCenter;
  871. XMVECTOR vRadius;
  872. if( XMVector3Greater( DistX, DistY ) )
  873. {
  874. if( XMVector3Greater( DistX, DistZ ) )
  875. {
  876. // Use min/max x.
  877. vCenter = XMVectorLerp(MaxX,MinX,0.5f);
  878. vRadius = XMVectorScale( DistX, 0.5f );
  879. }
  880. else
  881. {
  882. // Use min/max z.
  883. vCenter = XMVectorLerp(MaxZ,MinZ,0.5f);
  884. vRadius = XMVectorScale( DistZ, 0.5f );
  885. }
  886. }
  887. else // Y >= X
  888. {
  889. if( XMVector3Greater( DistY, DistZ ) )
  890. {
  891. // Use min/max y.
  892. vCenter = XMVectorLerp(MaxY,MinY,0.5f);
  893. vRadius = XMVectorScale( DistY, 0.5f );
  894. }
  895. else
  896. {
  897. // Use min/max z.
  898. vCenter = XMVectorLerp(MaxZ,MinZ,0.5f);
  899. vRadius = XMVectorScale( DistZ, 0.5f );
  900. }
  901. }
  902. // Add any points not inside the sphere.
  903. for( size_t i = 0; i < Count; ++i )
  904. {
  905. XMVECTOR Point = XMLoadFloat3( reinterpret_cast<const XMFLOAT3*>( reinterpret_cast<const uint8_t*>(pPoints) + i * Stride ) );
  906. XMVECTOR Delta = XMVectorSubtract( Point, vCenter );
  907. XMVECTOR Dist = XMVector3Length( Delta );
  908. if( XMVector3Greater( Dist, vRadius ) )
  909. {
  910. // Adjust sphere to include the new point.
  911. vRadius = XMVectorScale( XMVectorAdd( vRadius, Dist ), 0.5f );
  912. vCenter = XMVectorAdd( vCenter, XMVectorMultiply( XMVectorSubtract( XMVectorReplicate(1.0f), XMVectorDivide(vRadius, Dist) ), Delta ) );
  913. }
  914. }
  915. XMStoreFloat3( &Out.Center, vCenter );
  916. XMStoreFloat( &Out.Radius, vRadius );
  917. }
  918. //-----------------------------------------------------------------------------
  919. // Create sphere containing frustum
  920. //-----------------------------------------------------------------------------
  921. _Use_decl_annotations_
  922. inline void BoundingSphere::CreateFromFrustum( BoundingSphere& Out, const BoundingFrustum& fr )
  923. {
  924. XMFLOAT3 Corners[BoundingFrustum::CORNER_COUNT];
  925. fr.GetCorners( Corners );
  926. CreateFromPoints( Out, BoundingFrustum::CORNER_COUNT, Corners, sizeof(XMFLOAT3) );
  927. }
  928. /****************************************************************************
  929. *
  930. * BoundingBox
  931. *
  932. ****************************************************************************/
  933. //-----------------------------------------------------------------------------
  934. // Transform an axis aligned box by an angle preserving transform.
  935. //-----------------------------------------------------------------------------
  936. _Use_decl_annotations_
  937. inline void XM_CALLCONV BoundingBox::Transform( BoundingBox& Out, FXMMATRIX M ) const
  938. {
  939. // Load center and extents.
  940. XMVECTOR vCenter = XMLoadFloat3( &Center );
  941. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  942. // Compute and transform the corners and find new min/max bounds.
  943. XMVECTOR Corner = XMVectorMultiplyAdd( vExtents, g_BoxOffset[0], vCenter );
  944. Corner = XMVector3Transform( Corner, M );
  945. XMVECTOR Min, Max;
  946. Min = Max = Corner;
  947. for( size_t i = 1; i < CORNER_COUNT; ++i )
  948. {
  949. Corner = XMVectorMultiplyAdd( vExtents, g_BoxOffset[i], vCenter );
  950. Corner = XMVector3Transform( Corner, M );
  951. Min = XMVectorMin( Min, Corner );
  952. Max = XMVectorMax( Max, Corner );
  953. }
  954. // Store center and extents.
  955. XMStoreFloat3( &Out.Center, XMVectorScale( XMVectorAdd( Min, Max ), 0.5f ) );
  956. XMStoreFloat3( &Out.Extents, XMVectorScale( XMVectorSubtract( Max, Min ), 0.5f ) );
  957. }
  958. _Use_decl_annotations_
  959. inline void XM_CALLCONV BoundingBox::Transform( BoundingBox& Out, float Scale, FXMVECTOR Rotation, FXMVECTOR Translation ) const
  960. {
  961. assert( DirectX::Internal::XMQuaternionIsUnit( Rotation ) );
  962. // Load center and extents.
  963. XMVECTOR vCenter = XMLoadFloat3( &Center );
  964. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  965. XMVECTOR VectorScale = XMVectorReplicate( Scale );
  966. // Compute and transform the corners and find new min/max bounds.
  967. XMVECTOR Corner = XMVectorMultiplyAdd( vExtents, g_BoxOffset[0], vCenter );
  968. Corner = XMVectorAdd( XMVector3Rotate( XMVectorMultiply( Corner, VectorScale ), Rotation ), Translation );
  969. XMVECTOR Min, Max;
  970. Min = Max = Corner;
  971. for( size_t i = 1; i < CORNER_COUNT; ++i )
  972. {
  973. Corner = XMVectorMultiplyAdd( vExtents, g_BoxOffset[i], vCenter );
  974. Corner = XMVectorAdd( XMVector3Rotate( XMVectorMultiply( Corner, VectorScale ), Rotation ), Translation );
  975. Min = XMVectorMin( Min, Corner );
  976. Max = XMVectorMax( Max, Corner );
  977. }
  978. // Store center and extents.
  979. XMStoreFloat3( &Out.Center, XMVectorScale( XMVectorAdd( Min, Max ), 0.5f ) );
  980. XMStoreFloat3( &Out.Extents, XMVectorScale( XMVectorSubtract( Max, Min ), 0.5f ) );
  981. }
  982. //-----------------------------------------------------------------------------
  983. // Get the corner points of the box
  984. //-----------------------------------------------------------------------------
  985. _Use_decl_annotations_
  986. inline void BoundingBox::GetCorners( XMFLOAT3* Corners ) const
  987. {
  988. assert( Corners != nullptr );
  989. // Load the box
  990. XMVECTOR vCenter = XMLoadFloat3( &Center );
  991. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  992. for( size_t i = 0; i < CORNER_COUNT; ++i )
  993. {
  994. XMVECTOR C = XMVectorMultiplyAdd( vExtents, g_BoxOffset[i], vCenter );
  995. XMStoreFloat3( &Corners[i], C );
  996. }
  997. }
  998. //-----------------------------------------------------------------------------
  999. // Point in axis-aligned box test
  1000. //-----------------------------------------------------------------------------
  1001. _Use_decl_annotations_
  1002. inline ContainmentType XM_CALLCONV BoundingBox::Contains( FXMVECTOR Point ) const
  1003. {
  1004. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1005. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1006. return XMVector3InBounds( XMVectorSubtract( Point, vCenter ), vExtents ) ? CONTAINS : DISJOINT;
  1007. }
  1008. //-----------------------------------------------------------------------------
  1009. // Triangle in axis-aligned box test
  1010. //-----------------------------------------------------------------------------
  1011. _Use_decl_annotations_
  1012. inline ContainmentType XM_CALLCONV BoundingBox::Contains( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2 ) const
  1013. {
  1014. if ( !Intersects(V0,V1,V2) )
  1015. return DISJOINT;
  1016. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1017. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1018. XMVECTOR d = XMVectorAbs( XMVectorSubtract( V0, vCenter ) );
  1019. XMVECTOR Inside = XMVectorLessOrEqual( d, vExtents );
  1020. d = XMVectorAbs( XMVectorSubtract( V1, vCenter ) );
  1021. Inside = XMVectorAndInt( Inside, XMVectorLessOrEqual( d, vExtents ) );
  1022. d = XMVectorAbs( XMVectorSubtract( V2, vCenter ) );
  1023. Inside = XMVectorAndInt( Inside, XMVectorLessOrEqual( d, vExtents ) );
  1024. return ( XMVector3EqualInt( Inside, XMVectorTrueInt() ) ) ? CONTAINS : INTERSECTS;
  1025. }
  1026. //-----------------------------------------------------------------------------
  1027. // Sphere in axis-aligned box test
  1028. //-----------------------------------------------------------------------------
  1029. _Use_decl_annotations_
  1030. inline ContainmentType BoundingBox::Contains( const BoundingSphere& sh ) const
  1031. {
  1032. XMVECTOR SphereCenter = XMLoadFloat3( &sh.Center );
  1033. XMVECTOR SphereRadius = XMVectorReplicatePtr( &sh.Radius );
  1034. XMVECTOR BoxCenter = XMLoadFloat3( &Center );
  1035. XMVECTOR BoxExtents = XMLoadFloat3( &Extents );
  1036. XMVECTOR BoxMin = XMVectorSubtract( BoxCenter, BoxExtents );
  1037. XMVECTOR BoxMax = XMVectorAdd( BoxCenter, BoxExtents );
  1038. // Find the distance to the nearest point on the box.
  1039. // for each i in (x, y, z)
  1040. // if (SphereCenter(i) < BoxMin(i)) d2 += (SphereCenter(i) - BoxMin(i)) ^ 2
  1041. // else if (SphereCenter(i) > BoxMax(i)) d2 += (SphereCenter(i) - BoxMax(i)) ^ 2
  1042. XMVECTOR d = XMVectorZero();
  1043. // Compute d for each dimension.
  1044. XMVECTOR LessThanMin = XMVectorLess( SphereCenter, BoxMin );
  1045. XMVECTOR GreaterThanMax = XMVectorGreater( SphereCenter, BoxMax );
  1046. XMVECTOR MinDelta = XMVectorSubtract( SphereCenter, BoxMin );
  1047. XMVECTOR MaxDelta = XMVectorSubtract( SphereCenter, BoxMax );
  1048. // Choose value for each dimension based on the comparison.
  1049. d = XMVectorSelect( d, MinDelta, LessThanMin );
  1050. d = XMVectorSelect( d, MaxDelta, GreaterThanMax );
  1051. // Use a dot-product to square them and sum them together.
  1052. XMVECTOR d2 = XMVector3Dot( d, d );
  1053. if ( XMVector3Greater( d2, XMVectorMultiply( SphereRadius, SphereRadius ) ) )
  1054. return DISJOINT;
  1055. XMVECTOR InsideAll = XMVectorLessOrEqual( XMVectorAdd( BoxMin, SphereRadius ), SphereCenter );
  1056. InsideAll = XMVectorAndInt( InsideAll, XMVectorLessOrEqual( SphereCenter, XMVectorSubtract( BoxMax, SphereRadius ) ) );
  1057. InsideAll = XMVectorAndInt( InsideAll, XMVectorGreater( XMVectorSubtract( BoxMax, BoxMin ), SphereRadius ) );
  1058. return ( XMVector3EqualInt( InsideAll, XMVectorTrueInt() ) ) ? CONTAINS : INTERSECTS;
  1059. }
  1060. //-----------------------------------------------------------------------------
  1061. // Axis-aligned box in axis-aligned box test
  1062. //-----------------------------------------------------------------------------
  1063. _Use_decl_annotations_
  1064. inline ContainmentType BoundingBox::Contains( const BoundingBox& box ) const
  1065. {
  1066. XMVECTOR CenterA = XMLoadFloat3( &Center );
  1067. XMVECTOR ExtentsA = XMLoadFloat3( &Extents );
  1068. XMVECTOR CenterB = XMLoadFloat3( &box.Center );
  1069. XMVECTOR ExtentsB = XMLoadFloat3( &box.Extents );
  1070. XMVECTOR MinA = XMVectorSubtract( CenterA, ExtentsA );
  1071. XMVECTOR MaxA = XMVectorAdd( CenterA, ExtentsA );
  1072. XMVECTOR MinB = XMVectorSubtract( CenterB, ExtentsB );
  1073. XMVECTOR MaxB = XMVectorAdd( CenterB, ExtentsB );
  1074. // for each i in (x, y, z) if a_min(i) > b_max(i) or b_min(i) > a_max(i) then return false
  1075. XMVECTOR Disjoint = XMVectorOrInt( XMVectorGreater( MinA, MaxB ), XMVectorGreater( MinB, MaxA ) );
  1076. if ( DirectX::Internal::XMVector3AnyTrue( Disjoint ) )
  1077. return DISJOINT;
  1078. // for each i in (x, y, z) if a_min(i) <= b_min(i) and b_max(i) <= a_max(i) then A contains B
  1079. XMVECTOR Inside = XMVectorAndInt( XMVectorLessOrEqual( MinA, MinB ), XMVectorLessOrEqual( MaxB, MaxA ) );
  1080. return DirectX::Internal::XMVector3AllTrue( Inside ) ? CONTAINS : INTERSECTS;
  1081. }
  1082. //-----------------------------------------------------------------------------
  1083. // Oriented box in axis-aligned box test
  1084. //-----------------------------------------------------------------------------
  1085. _Use_decl_annotations_
  1086. inline ContainmentType BoundingBox::Contains( const BoundingOrientedBox& box ) const
  1087. {
  1088. if ( !box.Intersects( *this ) )
  1089. return DISJOINT;
  1090. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1091. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1092. // Subtract off the AABB center to remove a subtract below
  1093. XMVECTOR oCenter = XMVectorSubtract( XMLoadFloat3( &box.Center ), vCenter );
  1094. XMVECTOR oExtents = XMLoadFloat3( &box.Extents );
  1095. XMVECTOR oOrientation = XMLoadFloat4( &box.Orientation );
  1096. assert( DirectX::Internal::XMQuaternionIsUnit( oOrientation ) );
  1097. XMVECTOR Inside = XMVectorTrueInt();
  1098. for( size_t i=0; i < BoundingOrientedBox::CORNER_COUNT; ++i )
  1099. {
  1100. XMVECTOR C = XMVectorAdd( XMVector3Rotate( XMVectorMultiply( oExtents, g_BoxOffset[i] ), oOrientation ), oCenter );
  1101. XMVECTOR d = XMVectorAbs(C);
  1102. Inside = XMVectorAndInt( Inside, XMVectorLessOrEqual( d, vExtents ) );
  1103. }
  1104. return ( XMVector3EqualInt( Inside, XMVectorTrueInt() ) ) ? CONTAINS : INTERSECTS;
  1105. }
  1106. //-----------------------------------------------------------------------------
  1107. // Frustum in axis-aligned box test
  1108. //-----------------------------------------------------------------------------
  1109. _Use_decl_annotations_
  1110. inline ContainmentType BoundingBox::Contains( const BoundingFrustum& fr ) const
  1111. {
  1112. if ( !fr.Intersects( *this ) )
  1113. return DISJOINT;
  1114. XMFLOAT3 Corners[BoundingFrustum::CORNER_COUNT];
  1115. fr.GetCorners( Corners );
  1116. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1117. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1118. XMVECTOR Inside = XMVectorTrueInt();
  1119. for( size_t i=0; i < BoundingFrustum::CORNER_COUNT; ++i )
  1120. {
  1121. XMVECTOR Point = XMLoadFloat3( &Corners[i] );
  1122. XMVECTOR d = XMVectorAbs( XMVectorSubtract( Point, vCenter ) );
  1123. Inside = XMVectorAndInt( Inside, XMVectorLessOrEqual( d, vExtents ) );
  1124. }
  1125. return ( XMVector3EqualInt( Inside, XMVectorTrueInt() ) ) ? CONTAINS : INTERSECTS;
  1126. }
  1127. //-----------------------------------------------------------------------------
  1128. // Sphere vs axis-aligned box test
  1129. //-----------------------------------------------------------------------------
  1130. _Use_decl_annotations_
  1131. inline bool BoundingBox::Intersects( const BoundingSphere& sh ) const
  1132. {
  1133. XMVECTOR SphereCenter = XMLoadFloat3( &sh.Center );
  1134. XMVECTOR SphereRadius = XMVectorReplicatePtr( &sh.Radius );
  1135. XMVECTOR BoxCenter = XMLoadFloat3( &Center );
  1136. XMVECTOR BoxExtents = XMLoadFloat3( &Extents );
  1137. XMVECTOR BoxMin = XMVectorSubtract( BoxCenter, BoxExtents );
  1138. XMVECTOR BoxMax = XMVectorAdd( BoxCenter, BoxExtents );
  1139. // Find the distance to the nearest point on the box.
  1140. // for each i in (x, y, z)
  1141. // if (SphereCenter(i) < BoxMin(i)) d2 += (SphereCenter(i) - BoxMin(i)) ^ 2
  1142. // else if (SphereCenter(i) > BoxMax(i)) d2 += (SphereCenter(i) - BoxMax(i)) ^ 2
  1143. XMVECTOR d = XMVectorZero();
  1144. // Compute d for each dimension.
  1145. XMVECTOR LessThanMin = XMVectorLess( SphereCenter, BoxMin );
  1146. XMVECTOR GreaterThanMax = XMVectorGreater( SphereCenter, BoxMax );
  1147. XMVECTOR MinDelta = XMVectorSubtract( SphereCenter, BoxMin );
  1148. XMVECTOR MaxDelta = XMVectorSubtract( SphereCenter, BoxMax );
  1149. // Choose value for each dimension based on the comparison.
  1150. d = XMVectorSelect( d, MinDelta, LessThanMin );
  1151. d = XMVectorSelect( d, MaxDelta, GreaterThanMax );
  1152. // Use a dot-product to square them and sum them together.
  1153. XMVECTOR d2 = XMVector3Dot( d, d );
  1154. return XMVector3LessOrEqual( d2, XMVectorMultiply( SphereRadius, SphereRadius ) );
  1155. }
  1156. //-----------------------------------------------------------------------------
  1157. // Axis-aligned box vs. axis-aligned box test
  1158. //-----------------------------------------------------------------------------
  1159. _Use_decl_annotations_
  1160. inline bool BoundingBox::Intersects( const BoundingBox& box ) const
  1161. {
  1162. XMVECTOR CenterA = XMLoadFloat3( &Center );
  1163. XMVECTOR ExtentsA = XMLoadFloat3( &Extents );
  1164. XMVECTOR CenterB = XMLoadFloat3( &box.Center );
  1165. XMVECTOR ExtentsB = XMLoadFloat3( &box.Extents );
  1166. XMVECTOR MinA = XMVectorSubtract( CenterA, ExtentsA );
  1167. XMVECTOR MaxA = XMVectorAdd( CenterA, ExtentsA );
  1168. XMVECTOR MinB = XMVectorSubtract( CenterB, ExtentsB );
  1169. XMVECTOR MaxB = XMVectorAdd( CenterB, ExtentsB );
  1170. // for each i in (x, y, z) if a_min(i) > b_max(i) or b_min(i) > a_max(i) then return false
  1171. XMVECTOR Disjoint = XMVectorOrInt( XMVectorGreater( MinA, MaxB ), XMVectorGreater( MinB, MaxA ) );
  1172. return !DirectX::Internal::XMVector3AnyTrue( Disjoint );
  1173. }
  1174. //-----------------------------------------------------------------------------
  1175. // Oriented box vs. axis-aligned box test
  1176. //-----------------------------------------------------------------------------
  1177. _Use_decl_annotations_
  1178. inline bool BoundingBox::Intersects( const BoundingOrientedBox& box ) const
  1179. {
  1180. return box.Intersects( *this );
  1181. }
  1182. //-----------------------------------------------------------------------------
  1183. // Frustum vs. axis-aligned box test
  1184. //-----------------------------------------------------------------------------
  1185. _Use_decl_annotations_
  1186. inline bool BoundingBox::Intersects( const BoundingFrustum& fr ) const
  1187. {
  1188. return fr.Intersects( *this );
  1189. }
  1190. //-----------------------------------------------------------------------------
  1191. // Triangle vs. axis aligned box test
  1192. //-----------------------------------------------------------------------------
  1193. _Use_decl_annotations_
  1194. inline bool XM_CALLCONV BoundingBox::Intersects( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2 ) const
  1195. {
  1196. XMVECTOR Zero = XMVectorZero();
  1197. // Load the box.
  1198. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1199. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1200. XMVECTOR BoxMin = XMVectorSubtract( vCenter, vExtents );
  1201. XMVECTOR BoxMax = XMVectorAdd( vCenter, vExtents );
  1202. // Test the axes of the box (in effect test the AAB against the minimal AAB
  1203. // around the triangle).
  1204. XMVECTOR TriMin = XMVectorMin( XMVectorMin( V0, V1 ), V2 );
  1205. XMVECTOR TriMax = XMVectorMax( XMVectorMax( V0, V1 ), V2 );
  1206. // for each i in (x, y, z) if a_min(i) > b_max(i) or b_min(i) > a_max(i) then disjoint
  1207. XMVECTOR Disjoint = XMVectorOrInt( XMVectorGreater( TriMin, BoxMax ), XMVectorGreater( BoxMin, TriMax ) );
  1208. if( DirectX::Internal::XMVector3AnyTrue( Disjoint ) )
  1209. return false;
  1210. // Test the plane of the triangle.
  1211. XMVECTOR Normal = XMVector3Cross( XMVectorSubtract( V1, V0 ), XMVectorSubtract( V2, V0 ) );
  1212. XMVECTOR Dist = XMVector3Dot( Normal, V0 );
  1213. // Assert that the triangle is not degenerate.
  1214. assert( !XMVector3Equal( Normal, Zero ) );
  1215. // for each i in (x, y, z) if n(i) >= 0 then v_min(i)=b_min(i), v_max(i)=b_max(i)
  1216. // else v_min(i)=b_max(i), v_max(i)=b_min(i)
  1217. XMVECTOR NormalSelect = XMVectorGreater( Normal, Zero );
  1218. XMVECTOR V_Min = XMVectorSelect( BoxMax, BoxMin, NormalSelect );
  1219. XMVECTOR V_Max = XMVectorSelect( BoxMin, BoxMax, NormalSelect );
  1220. // if n dot v_min + d > 0 || n dot v_max + d < 0 then disjoint
  1221. XMVECTOR MinDist = XMVector3Dot( V_Min, Normal );
  1222. XMVECTOR MaxDist = XMVector3Dot( V_Max, Normal );
  1223. XMVECTOR NoIntersection = XMVectorGreater( MinDist, Dist );
  1224. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( MaxDist, Dist ) );
  1225. // Move the box center to zero to simplify the following tests.
  1226. XMVECTOR TV0 = XMVectorSubtract( V0, vCenter );
  1227. XMVECTOR TV1 = XMVectorSubtract( V1, vCenter );
  1228. XMVECTOR TV2 = XMVectorSubtract( V2, vCenter );
  1229. // Test the edge/edge axes (3*3).
  1230. XMVECTOR e0 = XMVectorSubtract( TV1, TV0 );
  1231. XMVECTOR e1 = XMVectorSubtract( TV2, TV1 );
  1232. XMVECTOR e2 = XMVectorSubtract( TV0, TV2 );
  1233. // Make w zero.
  1234. e0 = XMVectorInsert<0, 0, 0, 0, 1>( e0, Zero );
  1235. e1 = XMVectorInsert<0, 0, 0, 0, 1>( e1, Zero );
  1236. e2 = XMVectorInsert<0, 0, 0, 0, 1>( e2, Zero );
  1237. XMVECTOR Axis;
  1238. XMVECTOR p0, p1, p2;
  1239. XMVECTOR Min, Max;
  1240. XMVECTOR Radius;
  1241. // Axis == (1,0,0) x e0 = (0, -e0.z, e0.y)
  1242. Axis = XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>( e0, XMVectorNegate( e0 ) );
  1243. p0 = XMVector3Dot( TV0, Axis );
  1244. // p1 = XMVector3Dot( V1, Axis ); // p1 = p0;
  1245. p2 = XMVector3Dot( TV2, Axis );
  1246. Min = XMVectorMin( p0, p2 );
  1247. Max = XMVectorMax( p0, p2 );
  1248. Radius = XMVector3Dot( vExtents, XMVectorAbs( Axis ) );
  1249. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( Min, Radius ) );
  1250. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( Max, XMVectorNegate( Radius ) ) );
  1251. // Axis == (1,0,0) x e1 = (0, -e1.z, e1.y)
  1252. Axis = XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>( e1, XMVectorNegate( e1 ) );
  1253. p0 = XMVector3Dot( TV0, Axis );
  1254. p1 = XMVector3Dot( TV1, Axis );
  1255. // p2 = XMVector3Dot( V2, Axis ); // p2 = p1;
  1256. Min = XMVectorMin( p0, p1 );
  1257. Max = XMVectorMax( p0, p1 );
  1258. Radius = XMVector3Dot( vExtents, XMVectorAbs( Axis ) );
  1259. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( Min, Radius ) );
  1260. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( Max, XMVectorNegate( Radius ) ) );
  1261. // Axis == (1,0,0) x e2 = (0, -e2.z, e2.y)
  1262. Axis = XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>( e2, XMVectorNegate( e2 ) );
  1263. p0 = XMVector3Dot( TV0, Axis );
  1264. p1 = XMVector3Dot( TV1, Axis );
  1265. // p2 = XMVector3Dot( V2, Axis ); // p2 = p0;
  1266. Min = XMVectorMin( p0, p1 );
  1267. Max = XMVectorMax( p0, p1 );
  1268. Radius = XMVector3Dot( vExtents, XMVectorAbs( Axis ) );
  1269. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( Min, Radius ) );
  1270. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( Max, XMVectorNegate( Radius ) ) );
  1271. // Axis == (0,1,0) x e0 = (e0.z, 0, -e0.x)
  1272. Axis = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>( e0, XMVectorNegate( e0 ) );
  1273. p0 = XMVector3Dot( TV0, Axis );
  1274. // p1 = XMVector3Dot( V1, Axis ); // p1 = p0;
  1275. p2 = XMVector3Dot( TV2, Axis );
  1276. Min = XMVectorMin( p0, p2 );
  1277. Max = XMVectorMax( p0, p2 );
  1278. Radius = XMVector3Dot( vExtents, XMVectorAbs( Axis ) );
  1279. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( Min, Radius ) );
  1280. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( Max, XMVectorNegate( Radius ) ) );
  1281. // Axis == (0,1,0) x e1 = (e1.z, 0, -e1.x)
  1282. Axis = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>( e1, XMVectorNegate( e1 ) );
  1283. p0 = XMVector3Dot( TV0, Axis );
  1284. p1 = XMVector3Dot( TV1, Axis );
  1285. // p2 = XMVector3Dot( V2, Axis ); // p2 = p1;
  1286. Min = XMVectorMin( p0, p1 );
  1287. Max = XMVectorMax( p0, p1 );
  1288. Radius = XMVector3Dot( vExtents, XMVectorAbs( Axis ) );
  1289. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( Min, Radius ) );
  1290. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( Max, XMVectorNegate( Radius ) ) );
  1291. // Axis == (0,0,1) x e2 = (e2.z, 0, -e2.x)
  1292. Axis = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>( e2, XMVectorNegate( e2 ) );
  1293. p0 = XMVector3Dot( TV0, Axis );
  1294. p1 = XMVector3Dot( TV1, Axis );
  1295. // p2 = XMVector3Dot( V2, Axis ); // p2 = p0;
  1296. Min = XMVectorMin( p0, p1 );
  1297. Max = XMVectorMax( p0, p1 );
  1298. Radius = XMVector3Dot( vExtents, XMVectorAbs( Axis ) );
  1299. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( Min, Radius ) );
  1300. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( Max, XMVectorNegate( Radius ) ) );
  1301. // Axis == (0,0,1) x e0 = (-e0.y, e0.x, 0)
  1302. Axis = XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>( e0, XMVectorNegate( e0 ) );
  1303. p0 = XMVector3Dot( TV0, Axis );
  1304. // p1 = XMVector3Dot( V1, Axis ); // p1 = p0;
  1305. p2 = XMVector3Dot( TV2, Axis );
  1306. Min = XMVectorMin( p0, p2 );
  1307. Max = XMVectorMax( p0, p2 );
  1308. Radius = XMVector3Dot( vExtents, XMVectorAbs( Axis ) );
  1309. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( Min, Radius ) );
  1310. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( Max, XMVectorNegate( Radius ) ) );
  1311. // Axis == (0,0,1) x e1 = (-e1.y, e1.x, 0)
  1312. Axis = XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>( e1, XMVectorNegate( e1 ) );
  1313. p0 = XMVector3Dot( TV0, Axis );
  1314. p1 = XMVector3Dot( TV1, Axis );
  1315. // p2 = XMVector3Dot( V2, Axis ); // p2 = p1;
  1316. Min = XMVectorMin( p0, p1 );
  1317. Max = XMVectorMax( p0, p1 );
  1318. Radius = XMVector3Dot( vExtents, XMVectorAbs( Axis ) );
  1319. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( Min, Radius ) );
  1320. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( Max, XMVectorNegate( Radius ) ) );
  1321. // Axis == (0,0,1) x e2 = (-e2.y, e2.x, 0)
  1322. Axis = XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>( e2, XMVectorNegate( e2 ) );
  1323. p0 = XMVector3Dot( TV0, Axis );
  1324. p1 = XMVector3Dot( TV1, Axis );
  1325. // p2 = XMVector3Dot( V2, Axis ); // p2 = p0;
  1326. Min = XMVectorMin( p0, p1 );
  1327. Max = XMVectorMax( p0, p1 );
  1328. Radius = XMVector3Dot( vExtents, XMVectorAbs( Axis ) );
  1329. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( Min, Radius ) );
  1330. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( Max, XMVectorNegate( Radius ) ) );
  1331. return XMVector4NotEqualInt( NoIntersection, XMVectorTrueInt() );
  1332. }
  1333. //-----------------------------------------------------------------------------
  1334. _Use_decl_annotations_
  1335. inline PlaneIntersectionType XM_CALLCONV BoundingBox::Intersects( FXMVECTOR Plane ) const
  1336. {
  1337. assert( DirectX::Internal::XMPlaneIsUnit( Plane ) );
  1338. // Load the box.
  1339. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1340. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1341. // Set w of the center to one so we can dot4 with a plane.
  1342. vCenter = XMVectorInsert<0, 0, 0, 0, 1>( vCenter, XMVectorSplatOne() );
  1343. XMVECTOR Outside, Inside;
  1344. DirectX::Internal::FastIntersectAxisAlignedBoxPlane( vCenter, vExtents, Plane, Outside, Inside );
  1345. // If the box is outside any plane it is outside.
  1346. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  1347. return FRONT;
  1348. // If the box is inside all planes it is inside.
  1349. if ( XMVector4EqualInt( Inside, XMVectorTrueInt() ) )
  1350. return BACK;
  1351. // The box is not inside all planes or outside a plane it intersects.
  1352. return INTERSECTING;
  1353. }
  1354. //-----------------------------------------------------------------------------
  1355. // Compute the intersection of a ray (Origin, Direction) with an axis aligned
  1356. // box using the slabs method.
  1357. //-----------------------------------------------------------------------------
  1358. _Use_decl_annotations_
  1359. inline bool XM_CALLCONV BoundingBox::Intersects( FXMVECTOR Origin, FXMVECTOR Direction, float& Dist ) const
  1360. {
  1361. assert( DirectX::Internal::XMVector3IsUnit( Direction ) );
  1362. // Load the box.
  1363. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1364. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1365. // Adjust ray origin to be relative to center of the box.
  1366. XMVECTOR TOrigin = XMVectorSubtract( vCenter, Origin );
  1367. // Compute the dot product againt each axis of the box.
  1368. // Since the axii are (1,0,0), (0,1,0), (0,0,1) no computation is necessary.
  1369. XMVECTOR AxisDotOrigin = TOrigin;
  1370. XMVECTOR AxisDotDirection = Direction;
  1371. // if (fabs(AxisDotDirection) <= Epsilon) the ray is nearly parallel to the slab.
  1372. XMVECTOR IsParallel = XMVectorLessOrEqual( XMVectorAbs( AxisDotDirection ), g_RayEpsilon );
  1373. // Test against all three axii simultaneously.
  1374. XMVECTOR InverseAxisDotDirection = XMVectorReciprocal( AxisDotDirection );
  1375. XMVECTOR t1 = XMVectorMultiply( XMVectorSubtract( AxisDotOrigin, vExtents ), InverseAxisDotDirection );
  1376. XMVECTOR t2 = XMVectorMultiply( XMVectorAdd( AxisDotOrigin, vExtents ), InverseAxisDotDirection );
  1377. // Compute the max of min(t1,t2) and the min of max(t1,t2) ensuring we don't
  1378. // use the results from any directions parallel to the slab.
  1379. XMVECTOR t_min = XMVectorSelect( XMVectorMin( t1, t2 ), g_FltMin, IsParallel );
  1380. XMVECTOR t_max = XMVectorSelect( XMVectorMax( t1, t2 ), g_FltMax, IsParallel );
  1381. // t_min.x = maximum( t_min.x, t_min.y, t_min.z );
  1382. // t_max.x = minimum( t_max.x, t_max.y, t_max.z );
  1383. t_min = XMVectorMax( t_min, XMVectorSplatY( t_min ) ); // x = max(x,y)
  1384. t_min = XMVectorMax( t_min, XMVectorSplatZ( t_min ) ); // x = max(max(x,y),z)
  1385. t_max = XMVectorMin( t_max, XMVectorSplatY( t_max ) ); // x = min(x,y)
  1386. t_max = XMVectorMin( t_max, XMVectorSplatZ( t_max ) ); // x = min(min(x,y),z)
  1387. // if ( t_min > t_max ) return false;
  1388. XMVECTOR NoIntersection = XMVectorGreater( XMVectorSplatX( t_min ), XMVectorSplatX( t_max ) );
  1389. // if ( t_max < 0.0f ) return false;
  1390. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( XMVectorSplatX( t_max ), XMVectorZero() ) );
  1391. // if (IsParallel && (-Extents > AxisDotOrigin || Extents < AxisDotOrigin)) return false;
  1392. XMVECTOR ParallelOverlap = XMVectorInBounds( AxisDotOrigin, vExtents );
  1393. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorAndCInt( IsParallel, ParallelOverlap ) );
  1394. if( !DirectX::Internal::XMVector3AnyTrue( NoIntersection ) )
  1395. {
  1396. // Store the x-component to *pDist
  1397. XMStoreFloat( &Dist, t_min );
  1398. return true;
  1399. }
  1400. Dist = 0.f;
  1401. return false;
  1402. }
  1403. //-----------------------------------------------------------------------------
  1404. // Test an axis alinged box vs 6 planes (typically forming a frustum).
  1405. //-----------------------------------------------------------------------------
  1406. _Use_decl_annotations_
  1407. inline ContainmentType XM_CALLCONV BoundingBox::ContainedBy( FXMVECTOR Plane0, FXMVECTOR Plane1, FXMVECTOR Plane2,
  1408. GXMVECTOR Plane3, HXMVECTOR Plane4, HXMVECTOR Plane5 ) const
  1409. {
  1410. // Load the box.
  1411. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1412. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1413. // Set w of the center to one so we can dot4 with a plane.
  1414. vCenter = XMVectorInsert<0, 0, 0, 0, 1>( vCenter, XMVectorSplatOne() );
  1415. XMVECTOR Outside, Inside;
  1416. // Test against each plane.
  1417. DirectX::Internal::FastIntersectAxisAlignedBoxPlane( vCenter, vExtents, Plane0, Outside, Inside );
  1418. XMVECTOR AnyOutside = Outside;
  1419. XMVECTOR AllInside = Inside;
  1420. DirectX::Internal::FastIntersectAxisAlignedBoxPlane( vCenter, vExtents, Plane1, Outside, Inside );
  1421. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  1422. AllInside = XMVectorAndInt( AllInside, Inside );
  1423. DirectX::Internal::FastIntersectAxisAlignedBoxPlane( vCenter, vExtents, Plane2, Outside, Inside );
  1424. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  1425. AllInside = XMVectorAndInt( AllInside, Inside );
  1426. DirectX::Internal::FastIntersectAxisAlignedBoxPlane( vCenter, vExtents, Plane3, Outside, Inside );
  1427. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  1428. AllInside = XMVectorAndInt( AllInside, Inside );
  1429. DirectX::Internal::FastIntersectAxisAlignedBoxPlane( vCenter, vExtents, Plane4, Outside, Inside );
  1430. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  1431. AllInside = XMVectorAndInt( AllInside, Inside );
  1432. DirectX::Internal::FastIntersectAxisAlignedBoxPlane( vCenter, vExtents, Plane5, Outside, Inside );
  1433. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  1434. AllInside = XMVectorAndInt( AllInside, Inside );
  1435. // If the box is outside any plane it is outside.
  1436. if ( XMVector4EqualInt( AnyOutside, XMVectorTrueInt() ) )
  1437. return DISJOINT;
  1438. // If the box is inside all planes it is inside.
  1439. if ( XMVector4EqualInt( AllInside, XMVectorTrueInt() ) )
  1440. return CONTAINS;
  1441. // The box is not inside all planes or outside a plane, it may intersect.
  1442. return INTERSECTS;
  1443. }
  1444. //-----------------------------------------------------------------------------
  1445. // Create axis-aligned box that contains two other bounding boxes
  1446. //-----------------------------------------------------------------------------
  1447. _Use_decl_annotations_
  1448. inline void BoundingBox::CreateMerged( BoundingBox& Out, const BoundingBox& b1, const BoundingBox& b2 )
  1449. {
  1450. XMVECTOR b1Center = XMLoadFloat3( &b1.Center );
  1451. XMVECTOR b1Extents = XMLoadFloat3( &b1.Extents );
  1452. XMVECTOR b2Center = XMLoadFloat3( &b2.Center );
  1453. XMVECTOR b2Extents = XMLoadFloat3( &b2.Extents );
  1454. XMVECTOR Min = XMVectorSubtract( b1Center, b1Extents );
  1455. Min = XMVectorMin( Min, XMVectorSubtract( b2Center, b2Extents ) );
  1456. XMVECTOR Max = XMVectorAdd( b1Center, b1Extents );
  1457. Max = XMVectorMax( Max, XMVectorAdd( b2Center, b2Extents ) );
  1458. assert( XMVector3LessOrEqual( Min, Max ) );
  1459. XMStoreFloat3( &Out.Center, XMVectorScale( XMVectorAdd( Min, Max ), 0.5f ) );
  1460. XMStoreFloat3( &Out.Extents, XMVectorScale( XMVectorSubtract( Max, Min ), 0.5f ) );
  1461. }
  1462. //-----------------------------------------------------------------------------
  1463. // Create axis-aligned box that contains a bounding sphere
  1464. //-----------------------------------------------------------------------------
  1465. _Use_decl_annotations_
  1466. inline void BoundingBox::CreateFromSphere( BoundingBox& Out, const BoundingSphere& sh )
  1467. {
  1468. XMVECTOR spCenter = XMLoadFloat3( &sh.Center );
  1469. XMVECTOR shRadius = XMVectorReplicatePtr( &sh.Radius );
  1470. XMVECTOR Min = XMVectorSubtract( spCenter, shRadius );
  1471. XMVECTOR Max = XMVectorAdd( spCenter, shRadius );
  1472. assert( XMVector3LessOrEqual( Min, Max ) );
  1473. XMStoreFloat3( &Out.Center, XMVectorScale( XMVectorAdd( Min, Max ), 0.5f ) );
  1474. XMStoreFloat3( &Out.Extents, XMVectorScale( XMVectorSubtract( Max, Min ), 0.5f ) );
  1475. }
  1476. //-----------------------------------------------------------------------------
  1477. // Create axis-aligned box from min/max points
  1478. //-----------------------------------------------------------------------------
  1479. _Use_decl_annotations_
  1480. inline void XM_CALLCONV BoundingBox::CreateFromPoints( BoundingBox& Out, FXMVECTOR pt1, FXMVECTOR pt2 )
  1481. {
  1482. XMVECTOR Min = XMVectorMin( pt1, pt2 );
  1483. XMVECTOR Max = XMVectorMax( pt1, pt2 );
  1484. // Store center and extents.
  1485. XMStoreFloat3( &Out.Center, XMVectorScale( XMVectorAdd( Min, Max ), 0.5f ) );
  1486. XMStoreFloat3( &Out.Extents, XMVectorScale( XMVectorSubtract( Max, Min ), 0.5f ) );
  1487. }
  1488. //-----------------------------------------------------------------------------
  1489. // Find the minimum axis aligned bounding box containing a set of points.
  1490. //-----------------------------------------------------------------------------
  1491. _Use_decl_annotations_
  1492. inline void BoundingBox::CreateFromPoints( BoundingBox& Out, size_t Count, const XMFLOAT3* pPoints, size_t Stride )
  1493. {
  1494. assert( Count > 0 );
  1495. assert( pPoints );
  1496. // Find the minimum and maximum x, y, and z
  1497. XMVECTOR vMin, vMax;
  1498. vMin = vMax = XMLoadFloat3( pPoints );
  1499. for( size_t i = 1; i < Count; ++i )
  1500. {
  1501. XMVECTOR Point = XMLoadFloat3( reinterpret_cast<const XMFLOAT3*>( reinterpret_cast<const uint8_t*>(pPoints) + i * Stride ) );
  1502. vMin = XMVectorMin( vMin, Point );
  1503. vMax = XMVectorMax( vMax, Point );
  1504. }
  1505. // Store center and extents.
  1506. XMStoreFloat3( &Out.Center, XMVectorScale( XMVectorAdd( vMin, vMax ), 0.5f ) );
  1507. XMStoreFloat3( &Out.Extents, XMVectorScale( XMVectorSubtract( vMax, vMin ), 0.5f ) );
  1508. }
  1509. /****************************************************************************
  1510. *
  1511. * BoundingOrientedBox
  1512. *
  1513. ****************************************************************************/
  1514. //-----------------------------------------------------------------------------
  1515. // Transform an oriented box by an angle preserving transform.
  1516. //-----------------------------------------------------------------------------
  1517. _Use_decl_annotations_
  1518. inline void XM_CALLCONV BoundingOrientedBox::Transform( BoundingOrientedBox& Out, FXMMATRIX M ) const
  1519. {
  1520. // Load the box.
  1521. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1522. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1523. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  1524. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  1525. // Composite the box rotation and the transform rotation.
  1526. XMMATRIX nM;
  1527. nM.r[0] = XMVector3Normalize( M.r[0] );
  1528. nM.r[1] = XMVector3Normalize( M.r[1] );
  1529. nM.r[2] = XMVector3Normalize( M.r[2] );
  1530. nM.r[3] = g_XMIdentityR3;
  1531. XMVECTOR Rotation = XMQuaternionRotationMatrix( nM );
  1532. vOrientation = XMQuaternionMultiply( vOrientation, Rotation );
  1533. // Transform the center.
  1534. vCenter = XMVector3Transform( vCenter, M );
  1535. // Scale the box extents.
  1536. XMVECTOR dX = XMVector3Length( M.r[0] );
  1537. XMVECTOR dY = XMVector3Length( M.r[1] );
  1538. XMVECTOR dZ = XMVector3Length( M.r[2] );
  1539. XMVECTOR VectorScale = XMVectorSelect( dY, dX, g_XMSelect1000 );
  1540. VectorScale = XMVectorSelect( dZ, VectorScale, g_XMSelect1100 );
  1541. vExtents = XMVectorMultiply( vExtents, VectorScale );
  1542. // Store the box.
  1543. XMStoreFloat3( &Out.Center, vCenter );
  1544. XMStoreFloat3( &Out.Extents, vExtents );
  1545. XMStoreFloat4( &Out.Orientation, vOrientation );
  1546. }
  1547. _Use_decl_annotations_
  1548. inline void XM_CALLCONV BoundingOrientedBox::Transform( BoundingOrientedBox& Out, float Scale, FXMVECTOR Rotation, FXMVECTOR Translation ) const
  1549. {
  1550. assert( DirectX::Internal::XMQuaternionIsUnit( Rotation ) );
  1551. // Load the box.
  1552. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1553. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1554. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  1555. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  1556. // Composite the box rotation and the transform rotation.
  1557. vOrientation = XMQuaternionMultiply( vOrientation, Rotation );
  1558. // Transform the center.
  1559. XMVECTOR VectorScale = XMVectorReplicate( Scale );
  1560. vCenter = XMVectorAdd( XMVector3Rotate( XMVectorMultiply( vCenter, VectorScale ), Rotation ), Translation );
  1561. // Scale the box extents.
  1562. vExtents = XMVectorMultiply( vExtents, VectorScale );
  1563. // Store the box.
  1564. XMStoreFloat3( &Out.Center, vCenter );
  1565. XMStoreFloat3( &Out.Extents, vExtents );
  1566. XMStoreFloat4( &Out.Orientation, vOrientation );
  1567. }
  1568. //-----------------------------------------------------------------------------
  1569. // Get the corner points of the box
  1570. //-----------------------------------------------------------------------------
  1571. _Use_decl_annotations_
  1572. inline void BoundingOrientedBox::GetCorners( XMFLOAT3* Corners ) const
  1573. {
  1574. assert( Corners != 0 );
  1575. // Load the box
  1576. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1577. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1578. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  1579. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  1580. for( size_t i = 0; i < CORNER_COUNT; ++i )
  1581. {
  1582. XMVECTOR C = XMVectorAdd( XMVector3Rotate( XMVectorMultiply( vExtents, g_BoxOffset[i] ), vOrientation ), vCenter );
  1583. XMStoreFloat3( &Corners[i], C );
  1584. }
  1585. }
  1586. //-----------------------------------------------------------------------------
  1587. // Point in oriented box test.
  1588. //-----------------------------------------------------------------------------
  1589. _Use_decl_annotations_
  1590. inline ContainmentType XM_CALLCONV BoundingOrientedBox::Contains( FXMVECTOR Point ) const
  1591. {
  1592. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1593. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1594. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  1595. // Transform the point to be local to the box.
  1596. XMVECTOR TPoint = XMVector3InverseRotate( XMVectorSubtract( Point, vCenter ), vOrientation );
  1597. return XMVector3InBounds( TPoint, vExtents ) ? CONTAINS : DISJOINT;
  1598. }
  1599. //-----------------------------------------------------------------------------
  1600. // Triangle in oriented bounding box
  1601. //-----------------------------------------------------------------------------
  1602. _Use_decl_annotations_
  1603. inline ContainmentType XM_CALLCONV BoundingOrientedBox::Contains( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2 ) const
  1604. {
  1605. // Load the box center & orientation.
  1606. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1607. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  1608. // Transform the triangle vertices into the space of the box.
  1609. XMVECTOR TV0 = XMVector3InverseRotate( XMVectorSubtract( V0, vCenter ), vOrientation );
  1610. XMVECTOR TV1 = XMVector3InverseRotate( XMVectorSubtract( V1, vCenter ), vOrientation );
  1611. XMVECTOR TV2 = XMVector3InverseRotate( XMVectorSubtract( V2, vCenter ), vOrientation );
  1612. BoundingBox box;
  1613. box.Center = XMFLOAT3( 0.0f, 0.0f, 0.0f );
  1614. box.Extents = Extents;
  1615. // Use the triangle vs axis aligned box intersection routine.
  1616. return box.Contains( TV0, TV1, TV2 );
  1617. }
  1618. //-----------------------------------------------------------------------------
  1619. // Sphere in oriented bounding box
  1620. //-----------------------------------------------------------------------------
  1621. _Use_decl_annotations_
  1622. inline ContainmentType BoundingOrientedBox::Contains( const BoundingSphere& sh ) const
  1623. {
  1624. XMVECTOR SphereCenter = XMLoadFloat3( &sh.Center );
  1625. XMVECTOR SphereRadius = XMVectorReplicatePtr( &sh.Radius );
  1626. XMVECTOR BoxCenter = XMLoadFloat3( &Center );
  1627. XMVECTOR BoxExtents = XMLoadFloat3( &Extents );
  1628. XMVECTOR BoxOrientation = XMLoadFloat4( &Orientation );
  1629. assert( DirectX::Internal::XMQuaternionIsUnit( BoxOrientation ) );
  1630. // Transform the center of the sphere to be local to the box.
  1631. // BoxMin = -BoxExtents
  1632. // BoxMax = +BoxExtents
  1633. SphereCenter = XMVector3InverseRotate( XMVectorSubtract( SphereCenter, BoxCenter ), BoxOrientation );
  1634. // Find the distance to the nearest point on the box.
  1635. // for each i in (x, y, z)
  1636. // if (SphereCenter(i) < BoxMin(i)) d2 += (SphereCenter(i) - BoxMin(i)) ^ 2
  1637. // else if (SphereCenter(i) > BoxMax(i)) d2 += (SphereCenter(i) - BoxMax(i)) ^ 2
  1638. XMVECTOR d = XMVectorZero();
  1639. // Compute d for each dimension.
  1640. XMVECTOR LessThanMin = XMVectorLess( SphereCenter, XMVectorNegate( BoxExtents ) );
  1641. XMVECTOR GreaterThanMax = XMVectorGreater( SphereCenter, BoxExtents );
  1642. XMVECTOR MinDelta = XMVectorAdd( SphereCenter, BoxExtents );
  1643. XMVECTOR MaxDelta = XMVectorSubtract( SphereCenter, BoxExtents );
  1644. // Choose value for each dimension based on the comparison.
  1645. d = XMVectorSelect( d, MinDelta, LessThanMin );
  1646. d = XMVectorSelect( d, MaxDelta, GreaterThanMax );
  1647. // Use a dot-product to square them and sum them together.
  1648. XMVECTOR d2 = XMVector3Dot( d, d );
  1649. XMVECTOR SphereRadiusSq = XMVectorMultiply( SphereRadius, SphereRadius );
  1650. if ( XMVector4Greater( d2, SphereRadiusSq ) )
  1651. return DISJOINT;
  1652. // See if we are completely inside the box
  1653. XMVECTOR SMin = XMVectorSubtract( SphereCenter, SphereRadius );
  1654. XMVECTOR SMax = XMVectorAdd( SphereCenter, SphereRadius );
  1655. return ( XMVector3InBounds( SMin, BoxExtents ) && XMVector3InBounds( SMax, BoxExtents ) ) ? CONTAINS : INTERSECTS;
  1656. }
  1657. //-----------------------------------------------------------------------------
  1658. // Axis aligned box vs. oriented box. Constructs an oriented box and uses
  1659. // the oriented box vs. oriented box test.
  1660. //-----------------------------------------------------------------------------
  1661. _Use_decl_annotations_
  1662. inline ContainmentType BoundingOrientedBox::Contains( const BoundingBox& box ) const
  1663. {
  1664. // Make the axis aligned box oriented and do an OBB vs OBB test.
  1665. BoundingOrientedBox obox( box.Center, box.Extents, XMFLOAT4( 0.f, 0.f, 0.f, 1.f ) );
  1666. return Contains( obox );
  1667. }
  1668. //-----------------------------------------------------------------------------
  1669. // Oriented bounding box in oriented bounding box
  1670. //-----------------------------------------------------------------------------
  1671. _Use_decl_annotations_
  1672. inline ContainmentType BoundingOrientedBox::Contains( const BoundingOrientedBox& box ) const
  1673. {
  1674. if ( !Intersects(box) )
  1675. return DISJOINT;
  1676. // Load the boxes
  1677. XMVECTOR aCenter = XMLoadFloat3( &Center );
  1678. XMVECTOR aExtents = XMLoadFloat3( &Extents );
  1679. XMVECTOR aOrientation = XMLoadFloat4( &Orientation );
  1680. assert( DirectX::Internal::XMQuaternionIsUnit( aOrientation ) );
  1681. XMVECTOR bCenter = XMLoadFloat3( &box.Center );
  1682. XMVECTOR bExtents = XMLoadFloat3( &box.Extents );
  1683. XMVECTOR bOrientation = XMLoadFloat4( &box.Orientation );
  1684. assert( DirectX::Internal::XMQuaternionIsUnit( bOrientation ) );
  1685. XMVECTOR offset = XMVectorSubtract( bCenter, aCenter );
  1686. for( size_t i = 0; i < CORNER_COUNT; ++i )
  1687. {
  1688. // Cb = rotate( bExtents * corneroffset[i], bOrientation ) + bcenter
  1689. // Ca = invrotate( Cb - aCenter, aOrientation )
  1690. XMVECTOR C = XMVectorAdd( XMVector3Rotate( XMVectorMultiply( bExtents, g_BoxOffset[i] ), bOrientation ), offset );
  1691. C = XMVector3InverseRotate( C , aOrientation );
  1692. if ( !XMVector3InBounds( C, aExtents ) )
  1693. return INTERSECTS;
  1694. }
  1695. return CONTAINS;
  1696. }
  1697. //-----------------------------------------------------------------------------
  1698. // Frustum in oriented bounding box
  1699. //-----------------------------------------------------------------------------
  1700. _Use_decl_annotations_
  1701. inline ContainmentType BoundingOrientedBox::Contains( const BoundingFrustum& fr ) const
  1702. {
  1703. if ( !fr.Intersects(*this) )
  1704. return DISJOINT;
  1705. XMFLOAT3 Corners[BoundingFrustum::CORNER_COUNT];
  1706. fr.GetCorners( Corners );
  1707. // Load the box
  1708. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1709. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1710. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  1711. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  1712. for( size_t i = 0; i < BoundingFrustum::CORNER_COUNT; ++i )
  1713. {
  1714. XMVECTOR C = XMVector3InverseRotate( XMVectorSubtract( XMLoadFloat3( &Corners[i] ), vCenter ), vOrientation );
  1715. if ( !XMVector3InBounds( C, vExtents ) )
  1716. return INTERSECTS;
  1717. }
  1718. return CONTAINS;
  1719. }
  1720. //-----------------------------------------------------------------------------
  1721. // Sphere vs. oriented box test
  1722. //-----------------------------------------------------------------------------
  1723. _Use_decl_annotations_
  1724. inline bool BoundingOrientedBox::Intersects( const BoundingSphere& sh ) const
  1725. {
  1726. XMVECTOR SphereCenter = XMLoadFloat3( &sh.Center );
  1727. XMVECTOR SphereRadius = XMVectorReplicatePtr( &sh.Radius );
  1728. XMVECTOR BoxCenter = XMLoadFloat3( &Center );
  1729. XMVECTOR BoxExtents = XMLoadFloat3( &Extents );
  1730. XMVECTOR BoxOrientation = XMLoadFloat4( &Orientation );
  1731. assert( DirectX::Internal::XMQuaternionIsUnit( BoxOrientation ) );
  1732. // Transform the center of the sphere to be local to the box.
  1733. // BoxMin = -BoxExtents
  1734. // BoxMax = +BoxExtents
  1735. SphereCenter = XMVector3InverseRotate( XMVectorSubtract( SphereCenter, BoxCenter ), BoxOrientation );
  1736. // Find the distance to the nearest point on the box.
  1737. // for each i in (x, y, z)
  1738. // if (SphereCenter(i) < BoxMin(i)) d2 += (SphereCenter(i) - BoxMin(i)) ^ 2
  1739. // else if (SphereCenter(i) > BoxMax(i)) d2 += (SphereCenter(i) - BoxMax(i)) ^ 2
  1740. XMVECTOR d = XMVectorZero();
  1741. // Compute d for each dimension.
  1742. XMVECTOR LessThanMin = XMVectorLess( SphereCenter, XMVectorNegate( BoxExtents ) );
  1743. XMVECTOR GreaterThanMax = XMVectorGreater( SphereCenter, BoxExtents );
  1744. XMVECTOR MinDelta = XMVectorAdd( SphereCenter, BoxExtents );
  1745. XMVECTOR MaxDelta = XMVectorSubtract( SphereCenter, BoxExtents );
  1746. // Choose value for each dimension based on the comparison.
  1747. d = XMVectorSelect( d, MinDelta, LessThanMin );
  1748. d = XMVectorSelect( d, MaxDelta, GreaterThanMax );
  1749. // Use a dot-product to square them and sum them together.
  1750. XMVECTOR d2 = XMVector3Dot( d, d );
  1751. return XMVector4LessOrEqual( d2, XMVectorMultiply( SphereRadius, SphereRadius ) ) ? true : false;
  1752. }
  1753. //-----------------------------------------------------------------------------
  1754. // Axis aligned box vs. oriented box. Constructs an oriented box and uses
  1755. // the oriented box vs. oriented box test.
  1756. //-----------------------------------------------------------------------------
  1757. _Use_decl_annotations_
  1758. inline bool BoundingOrientedBox::Intersects( const BoundingBox& box ) const
  1759. {
  1760. // Make the axis aligned box oriented and do an OBB vs OBB test.
  1761. BoundingOrientedBox obox( box.Center, box.Extents, XMFLOAT4( 0.f, 0.f, 0.f, 1.f ) );
  1762. return Intersects( obox );
  1763. }
  1764. //-----------------------------------------------------------------------------
  1765. // Fast oriented box / oriented box intersection test using the separating axis
  1766. // theorem.
  1767. //-----------------------------------------------------------------------------
  1768. _Use_decl_annotations_
  1769. inline bool BoundingOrientedBox::Intersects( const BoundingOrientedBox& box ) const
  1770. {
  1771. // Build the 3x3 rotation matrix that defines the orientation of B relative to A.
  1772. XMVECTOR A_quat = XMLoadFloat4( &Orientation );
  1773. XMVECTOR B_quat = XMLoadFloat4( &box.Orientation );
  1774. assert( DirectX::Internal::XMQuaternionIsUnit( A_quat ) );
  1775. assert( DirectX::Internal::XMQuaternionIsUnit( B_quat ) );
  1776. XMVECTOR Q = XMQuaternionMultiply( A_quat, XMQuaternionConjugate( B_quat ) );
  1777. XMMATRIX R = XMMatrixRotationQuaternion( Q );
  1778. // Compute the translation of B relative to A.
  1779. XMVECTOR A_cent = XMLoadFloat3( &Center );
  1780. XMVECTOR B_cent = XMLoadFloat3( &box.Center );
  1781. XMVECTOR t = XMVector3InverseRotate( XMVectorSubtract( B_cent, A_cent ), A_quat );
  1782. //
  1783. // h(A) = extents of A.
  1784. // h(B) = extents of B.
  1785. //
  1786. // a(u) = axes of A = (1,0,0), (0,1,0), (0,0,1)
  1787. // b(u) = axes of B relative to A = (r00,r10,r20), (r01,r11,r21), (r02,r12,r22)
  1788. //
  1789. // For each possible separating axis l:
  1790. // d(A) = sum (for i = u,v,w) h(A)(i) * abs( a(i) dot l )
  1791. // d(B) = sum (for i = u,v,w) h(B)(i) * abs( b(i) dot l )
  1792. // if abs( t dot l ) > d(A) + d(B) then disjoint
  1793. //
  1794. // Load extents of A and B.
  1795. XMVECTOR h_A = XMLoadFloat3( &Extents );
  1796. XMVECTOR h_B = XMLoadFloat3( &box.Extents );
  1797. // Rows. Note R[0,1,2]X.w = 0.
  1798. XMVECTOR R0X = R.r[0];
  1799. XMVECTOR R1X = R.r[1];
  1800. XMVECTOR R2X = R.r[2];
  1801. R = XMMatrixTranspose( R );
  1802. // Columns. Note RX[0,1,2].w = 0.
  1803. XMVECTOR RX0 = R.r[0];
  1804. XMVECTOR RX1 = R.r[1];
  1805. XMVECTOR RX2 = R.r[2];
  1806. // Absolute value of rows.
  1807. XMVECTOR AR0X = XMVectorAbs( R0X );
  1808. XMVECTOR AR1X = XMVectorAbs( R1X );
  1809. XMVECTOR AR2X = XMVectorAbs( R2X );
  1810. // Absolute value of columns.
  1811. XMVECTOR ARX0 = XMVectorAbs( RX0 );
  1812. XMVECTOR ARX1 = XMVectorAbs( RX1 );
  1813. XMVECTOR ARX2 = XMVectorAbs( RX2 );
  1814. // Test each of the 15 possible seperating axii.
  1815. XMVECTOR d, d_A, d_B;
  1816. // l = a(u) = (1, 0, 0)
  1817. // t dot l = t.x
  1818. // d(A) = h(A).x
  1819. // d(B) = h(B) dot abs(r00, r01, r02)
  1820. d = XMVectorSplatX( t );
  1821. d_A = XMVectorSplatX( h_A );
  1822. d_B = XMVector3Dot( h_B, AR0X );
  1823. XMVECTOR NoIntersection = XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) );
  1824. // l = a(v) = (0, 1, 0)
  1825. // t dot l = t.y
  1826. // d(A) = h(A).y
  1827. // d(B) = h(B) dot abs(r10, r11, r12)
  1828. d = XMVectorSplatY( t );
  1829. d_A = XMVectorSplatY( h_A );
  1830. d_B = XMVector3Dot( h_B, AR1X );
  1831. NoIntersection = XMVectorOrInt( NoIntersection,
  1832. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1833. // l = a(w) = (0, 0, 1)
  1834. // t dot l = t.z
  1835. // d(A) = h(A).z
  1836. // d(B) = h(B) dot abs(r20, r21, r22)
  1837. d = XMVectorSplatZ( t );
  1838. d_A = XMVectorSplatZ( h_A );
  1839. d_B = XMVector3Dot( h_B, AR2X );
  1840. NoIntersection = XMVectorOrInt( NoIntersection,
  1841. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1842. // l = b(u) = (r00, r10, r20)
  1843. // d(A) = h(A) dot abs(r00, r10, r20)
  1844. // d(B) = h(B).x
  1845. d = XMVector3Dot( t, RX0 );
  1846. d_A = XMVector3Dot( h_A, ARX0 );
  1847. d_B = XMVectorSplatX( h_B );
  1848. NoIntersection = XMVectorOrInt( NoIntersection,
  1849. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1850. // l = b(v) = (r01, r11, r21)
  1851. // d(A) = h(A) dot abs(r01, r11, r21)
  1852. // d(B) = h(B).y
  1853. d = XMVector3Dot( t, RX1 );
  1854. d_A = XMVector3Dot( h_A, ARX1 );
  1855. d_B = XMVectorSplatY( h_B );
  1856. NoIntersection = XMVectorOrInt( NoIntersection,
  1857. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1858. // l = b(w) = (r02, r12, r22)
  1859. // d(A) = h(A) dot abs(r02, r12, r22)
  1860. // d(B) = h(B).z
  1861. d = XMVector3Dot( t, RX2 );
  1862. d_A = XMVector3Dot( h_A, ARX2 );
  1863. d_B = XMVectorSplatZ( h_B );
  1864. NoIntersection = XMVectorOrInt( NoIntersection,
  1865. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1866. // l = a(u) x b(u) = (0, -r20, r10)
  1867. // d(A) = h(A) dot abs(0, r20, r10)
  1868. // d(B) = h(B) dot abs(0, r02, r01)
  1869. d = XMVector3Dot( t, XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>( RX0, XMVectorNegate( RX0 ) ) );
  1870. d_A = XMVector3Dot( h_A, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>( ARX0 ) );
  1871. d_B = XMVector3Dot( h_B, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>( AR0X ) );
  1872. NoIntersection = XMVectorOrInt( NoIntersection,
  1873. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1874. // l = a(u) x b(v) = (0, -r21, r11)
  1875. // d(A) = h(A) dot abs(0, r21, r11)
  1876. // d(B) = h(B) dot abs(r02, 0, r00)
  1877. d = XMVector3Dot( t, XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>( RX1, XMVectorNegate( RX1 ) ) );
  1878. d_A = XMVector3Dot( h_A, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>( ARX1 ) );
  1879. d_B = XMVector3Dot( h_B, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>( AR0X ) );
  1880. NoIntersection = XMVectorOrInt( NoIntersection,
  1881. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1882. // l = a(u) x b(w) = (0, -r22, r12)
  1883. // d(A) = h(A) dot abs(0, r22, r12)
  1884. // d(B) = h(B) dot abs(r01, r00, 0)
  1885. d = XMVector3Dot( t, XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>( RX2, XMVectorNegate( RX2 ) ) );
  1886. d_A = XMVector3Dot( h_A, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>( ARX2 ) );
  1887. d_B = XMVector3Dot( h_B, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>( AR0X ) );
  1888. NoIntersection = XMVectorOrInt( NoIntersection,
  1889. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1890. // l = a(v) x b(u) = (r20, 0, -r00)
  1891. // d(A) = h(A) dot abs(r20, 0, r00)
  1892. // d(B) = h(B) dot abs(0, r12, r11)
  1893. d = XMVector3Dot( t, XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>( RX0, XMVectorNegate( RX0 ) ) );
  1894. d_A = XMVector3Dot( h_A, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>( ARX0 ) );
  1895. d_B = XMVector3Dot( h_B, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>( AR1X ) );
  1896. NoIntersection = XMVectorOrInt( NoIntersection,
  1897. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1898. // l = a(v) x b(v) = (r21, 0, -r01)
  1899. // d(A) = h(A) dot abs(r21, 0, r01)
  1900. // d(B) = h(B) dot abs(r12, 0, r10)
  1901. d = XMVector3Dot( t, XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>( RX1, XMVectorNegate( RX1 ) ) );
  1902. d_A = XMVector3Dot( h_A, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>( ARX1 ) );
  1903. d_B = XMVector3Dot( h_B, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>( AR1X ) );
  1904. NoIntersection = XMVectorOrInt( NoIntersection,
  1905. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1906. // l = a(v) x b(w) = (r22, 0, -r02)
  1907. // d(A) = h(A) dot abs(r22, 0, r02)
  1908. // d(B) = h(B) dot abs(r11, r10, 0)
  1909. d = XMVector3Dot( t, XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>( RX2, XMVectorNegate( RX2 ) ) );
  1910. d_A = XMVector3Dot( h_A, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>( ARX2 ) );
  1911. d_B = XMVector3Dot( h_B, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>( AR1X ) );
  1912. NoIntersection = XMVectorOrInt( NoIntersection,
  1913. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1914. // l = a(w) x b(u) = (-r10, r00, 0)
  1915. // d(A) = h(A) dot abs(r10, r00, 0)
  1916. // d(B) = h(B) dot abs(0, r22, r21)
  1917. d = XMVector3Dot( t, XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>( RX0, XMVectorNegate( RX0 ) ) );
  1918. d_A = XMVector3Dot( h_A, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>( ARX0 ) );
  1919. d_B = XMVector3Dot( h_B, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>( AR2X ) );
  1920. NoIntersection = XMVectorOrInt( NoIntersection,
  1921. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1922. // l = a(w) x b(v) = (-r11, r01, 0)
  1923. // d(A) = h(A) dot abs(r11, r01, 0)
  1924. // d(B) = h(B) dot abs(r22, 0, r20)
  1925. d = XMVector3Dot( t, XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>( RX1, XMVectorNegate( RX1 ) ) );
  1926. d_A = XMVector3Dot( h_A, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>( ARX1 ) );
  1927. d_B = XMVector3Dot( h_B, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>( AR2X ) );
  1928. NoIntersection = XMVectorOrInt( NoIntersection,
  1929. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1930. // l = a(w) x b(w) = (-r12, r02, 0)
  1931. // d(A) = h(A) dot abs(r12, r02, 0)
  1932. // d(B) = h(B) dot abs(r21, r20, 0)
  1933. d = XMVector3Dot( t, XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>( RX2, XMVectorNegate( RX2 ) ) );
  1934. d_A = XMVector3Dot( h_A, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>( ARX2 ) );
  1935. d_B = XMVector3Dot( h_B, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>( AR2X ) );
  1936. NoIntersection = XMVectorOrInt( NoIntersection,
  1937. XMVectorGreater( XMVectorAbs(d), XMVectorAdd( d_A, d_B ) ) );
  1938. // No seperating axis found, boxes must intersect.
  1939. return XMVector4NotEqualInt( NoIntersection, XMVectorTrueInt() ) ? true : false;
  1940. }
  1941. //-----------------------------------------------------------------------------
  1942. // Frustum vs. oriented box test
  1943. //-----------------------------------------------------------------------------
  1944. _Use_decl_annotations_
  1945. inline bool BoundingOrientedBox::Intersects( const BoundingFrustum& fr ) const
  1946. {
  1947. return fr.Intersects( *this );
  1948. }
  1949. //-----------------------------------------------------------------------------
  1950. // Triangle vs. oriented box test.
  1951. //-----------------------------------------------------------------------------
  1952. _Use_decl_annotations_
  1953. inline bool XM_CALLCONV BoundingOrientedBox::Intersects( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2 ) const
  1954. {
  1955. // Load the box center & orientation.
  1956. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1957. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  1958. // Transform the triangle vertices into the space of the box.
  1959. XMVECTOR TV0 = XMVector3InverseRotate( XMVectorSubtract( V0, vCenter ), vOrientation );
  1960. XMVECTOR TV1 = XMVector3InverseRotate( XMVectorSubtract( V1, vCenter ), vOrientation );
  1961. XMVECTOR TV2 = XMVector3InverseRotate( XMVectorSubtract( V2, vCenter ), vOrientation );
  1962. BoundingBox box;
  1963. box.Center = XMFLOAT3( 0.0f, 0.0f, 0.0f );
  1964. box.Extents = Extents;
  1965. // Use the triangle vs axis aligned box intersection routine.
  1966. return box.Intersects( TV0, TV1, TV2 );
  1967. }
  1968. //-----------------------------------------------------------------------------
  1969. _Use_decl_annotations_
  1970. inline PlaneIntersectionType XM_CALLCONV BoundingOrientedBox::Intersects( FXMVECTOR Plane ) const
  1971. {
  1972. assert( DirectX::Internal::XMPlaneIsUnit( Plane ) );
  1973. // Load the box.
  1974. XMVECTOR vCenter = XMLoadFloat3( &Center );
  1975. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  1976. XMVECTOR BoxOrientation = XMLoadFloat4( &Orientation );
  1977. assert( DirectX::Internal::XMQuaternionIsUnit( BoxOrientation ) );
  1978. // Set w of the center to one so we can dot4 with a plane.
  1979. vCenter = XMVectorInsert<0, 0, 0, 0, 1>( vCenter, XMVectorSplatOne() );
  1980. // Build the 3x3 rotation matrix that defines the box axes.
  1981. XMMATRIX R = XMMatrixRotationQuaternion( BoxOrientation );
  1982. XMVECTOR Outside, Inside;
  1983. DirectX::Internal::FastIntersectOrientedBoxPlane( vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane, Outside, Inside );
  1984. // If the box is outside any plane it is outside.
  1985. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  1986. return FRONT;
  1987. // If the box is inside all planes it is inside.
  1988. if ( XMVector4EqualInt( Inside, XMVectorTrueInt() ) )
  1989. return BACK;
  1990. // The box is not inside all planes or outside a plane it intersects.
  1991. return INTERSECTING;
  1992. }
  1993. //-----------------------------------------------------------------------------
  1994. // Compute the intersection of a ray (Origin, Direction) with an oriented box
  1995. // using the slabs method.
  1996. //-----------------------------------------------------------------------------
  1997. _Use_decl_annotations_
  1998. inline bool XM_CALLCONV BoundingOrientedBox::Intersects( FXMVECTOR Origin, FXMVECTOR Direction, float& Dist ) const
  1999. {
  2000. assert( DirectX::Internal::XMVector3IsUnit( Direction ) );
  2001. static const XMVECTORU32 SelectY = { { { XM_SELECT_0, XM_SELECT_1, XM_SELECT_0, XM_SELECT_0 } } };
  2002. static const XMVECTORU32 SelectZ = { { { XM_SELECT_0, XM_SELECT_0, XM_SELECT_1, XM_SELECT_0 } } };
  2003. // Load the box.
  2004. XMVECTOR vCenter = XMLoadFloat3( &Center );
  2005. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  2006. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2007. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  2008. // Get the boxes normalized side directions.
  2009. XMMATRIX R = XMMatrixRotationQuaternion( vOrientation );
  2010. // Adjust ray origin to be relative to center of the box.
  2011. XMVECTOR TOrigin = XMVectorSubtract( vCenter, Origin );
  2012. // Compute the dot product againt each axis of the box.
  2013. XMVECTOR AxisDotOrigin = XMVector3Dot( R.r[0], TOrigin );
  2014. AxisDotOrigin = XMVectorSelect( AxisDotOrigin, XMVector3Dot( R.r[1], TOrigin ), SelectY );
  2015. AxisDotOrigin = XMVectorSelect( AxisDotOrigin, XMVector3Dot( R.r[2], TOrigin ), SelectZ );
  2016. XMVECTOR AxisDotDirection = XMVector3Dot( R.r[0], Direction );
  2017. AxisDotDirection = XMVectorSelect( AxisDotDirection, XMVector3Dot( R.r[1], Direction ), SelectY );
  2018. AxisDotDirection = XMVectorSelect( AxisDotDirection, XMVector3Dot( R.r[2], Direction ), SelectZ );
  2019. // if (fabs(AxisDotDirection) <= Epsilon) the ray is nearly parallel to the slab.
  2020. XMVECTOR IsParallel = XMVectorLessOrEqual( XMVectorAbs( AxisDotDirection ), g_RayEpsilon );
  2021. // Test against all three axes simultaneously.
  2022. XMVECTOR InverseAxisDotDirection = XMVectorReciprocal( AxisDotDirection );
  2023. XMVECTOR t1 = XMVectorMultiply( XMVectorSubtract( AxisDotOrigin, vExtents ), InverseAxisDotDirection );
  2024. XMVECTOR t2 = XMVectorMultiply( XMVectorAdd( AxisDotOrigin, vExtents ), InverseAxisDotDirection );
  2025. // Compute the max of min(t1,t2) and the min of max(t1,t2) ensuring we don't
  2026. // use the results from any directions parallel to the slab.
  2027. XMVECTOR t_min = XMVectorSelect( XMVectorMin( t1, t2 ), g_FltMin, IsParallel );
  2028. XMVECTOR t_max = XMVectorSelect( XMVectorMax( t1, t2 ), g_FltMax, IsParallel );
  2029. // t_min.x = maximum( t_min.x, t_min.y, t_min.z );
  2030. // t_max.x = minimum( t_max.x, t_max.y, t_max.z );
  2031. t_min = XMVectorMax( t_min, XMVectorSplatY( t_min ) ); // x = max(x,y)
  2032. t_min = XMVectorMax( t_min, XMVectorSplatZ( t_min ) ); // x = max(max(x,y),z)
  2033. t_max = XMVectorMin( t_max, XMVectorSplatY( t_max ) ); // x = min(x,y)
  2034. t_max = XMVectorMin( t_max, XMVectorSplatZ( t_max ) ); // x = min(min(x,y),z)
  2035. // if ( t_min > t_max ) return false;
  2036. XMVECTOR NoIntersection = XMVectorGreater( XMVectorSplatX( t_min ), XMVectorSplatX( t_max ) );
  2037. // if ( t_max < 0.0f ) return false;
  2038. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( XMVectorSplatX( t_max ), XMVectorZero() ) );
  2039. // if (IsParallel && (-Extents > AxisDotOrigin || Extents < AxisDotOrigin)) return false;
  2040. XMVECTOR ParallelOverlap = XMVectorInBounds( AxisDotOrigin, vExtents );
  2041. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorAndCInt( IsParallel, ParallelOverlap ) );
  2042. if( !DirectX::Internal::XMVector3AnyTrue( NoIntersection ) )
  2043. {
  2044. // Store the x-component to *pDist
  2045. XMStoreFloat( &Dist, t_min );
  2046. return true;
  2047. }
  2048. Dist = 0.f;
  2049. return false;
  2050. }
  2051. //-----------------------------------------------------------------------------
  2052. // Test an oriented box vs 6 planes (typically forming a frustum).
  2053. //-----------------------------------------------------------------------------
  2054. _Use_decl_annotations_
  2055. inline ContainmentType XM_CALLCONV BoundingOrientedBox::ContainedBy( FXMVECTOR Plane0, FXMVECTOR Plane1, FXMVECTOR Plane2,
  2056. GXMVECTOR Plane3, HXMVECTOR Plane4, HXMVECTOR Plane5 ) const
  2057. {
  2058. // Load the box.
  2059. XMVECTOR vCenter = XMLoadFloat3( &Center );
  2060. XMVECTOR vExtents = XMLoadFloat3( &Extents );
  2061. XMVECTOR BoxOrientation = XMLoadFloat4( &Orientation );
  2062. assert( DirectX::Internal::XMQuaternionIsUnit( BoxOrientation ) );
  2063. // Set w of the center to one so we can dot4 with a plane.
  2064. vCenter = XMVectorInsert<0, 0, 0, 0, 1>( vCenter, XMVectorSplatOne() );
  2065. // Build the 3x3 rotation matrix that defines the box axes.
  2066. XMMATRIX R = XMMatrixRotationQuaternion( BoxOrientation );
  2067. XMVECTOR Outside, Inside;
  2068. // Test against each plane.
  2069. DirectX::Internal::FastIntersectOrientedBoxPlane( vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane0, Outside, Inside );
  2070. XMVECTOR AnyOutside = Outside;
  2071. XMVECTOR AllInside = Inside;
  2072. DirectX::Internal::FastIntersectOrientedBoxPlane( vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane1, Outside, Inside );
  2073. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  2074. AllInside = XMVectorAndInt( AllInside, Inside );
  2075. DirectX::Internal::FastIntersectOrientedBoxPlane( vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane2, Outside, Inside );
  2076. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  2077. AllInside = XMVectorAndInt( AllInside, Inside );
  2078. DirectX::Internal::FastIntersectOrientedBoxPlane( vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane3, Outside, Inside );
  2079. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  2080. AllInside = XMVectorAndInt( AllInside, Inside );
  2081. DirectX::Internal::FastIntersectOrientedBoxPlane( vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane4, Outside, Inside );
  2082. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  2083. AllInside = XMVectorAndInt( AllInside, Inside );
  2084. DirectX::Internal::FastIntersectOrientedBoxPlane( vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane5, Outside, Inside );
  2085. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  2086. AllInside = XMVectorAndInt( AllInside, Inside );
  2087. // If the box is outside any plane it is outside.
  2088. if ( XMVector4EqualInt( AnyOutside, XMVectorTrueInt() ) )
  2089. return DISJOINT;
  2090. // If the box is inside all planes it is inside.
  2091. if ( XMVector4EqualInt( AllInside, XMVectorTrueInt() ) )
  2092. return CONTAINS;
  2093. // The box is not inside all planes or outside a plane, it may intersect.
  2094. return INTERSECTS;
  2095. }
  2096. //-----------------------------------------------------------------------------
  2097. // Create oriented bounding box from axis-aligned bounding box
  2098. //-----------------------------------------------------------------------------
  2099. _Use_decl_annotations_
  2100. inline void BoundingOrientedBox::CreateFromBoundingBox( BoundingOrientedBox& Out, const BoundingBox& box )
  2101. {
  2102. Out.Center = box.Center;
  2103. Out.Extents = box.Extents;
  2104. Out.Orientation = XMFLOAT4( 0.f, 0.f, 0.f, 1.f );
  2105. }
  2106. //-----------------------------------------------------------------------------
  2107. // Find the approximate minimum oriented bounding box containing a set of
  2108. // points. Exact computation of minimum oriented bounding box is possible but
  2109. // is slower and requires a more complex algorithm.
  2110. // The algorithm works by computing the inertia tensor of the points and then
  2111. // using the eigenvectors of the intertia tensor as the axes of the box.
  2112. // Computing the intertia tensor of the convex hull of the points will usually
  2113. // result in better bounding box but the computation is more complex.
  2114. // Exact computation of the minimum oriented bounding box is possible but the
  2115. // best know algorithm is O(N^3) and is significanly more complex to implement.
  2116. //-----------------------------------------------------------------------------
  2117. _Use_decl_annotations_
  2118. inline void BoundingOrientedBox::CreateFromPoints( BoundingOrientedBox& Out, size_t Count, const XMFLOAT3* pPoints, size_t Stride )
  2119. {
  2120. assert( Count > 0 );
  2121. assert( pPoints != 0 );
  2122. XMVECTOR CenterOfMass = XMVectorZero();
  2123. // Compute the center of mass and inertia tensor of the points.
  2124. for( size_t i = 0; i < Count; ++i )
  2125. {
  2126. XMVECTOR Point = XMLoadFloat3( reinterpret_cast<const XMFLOAT3*>( reinterpret_cast<const uint8_t*>(pPoints) + i * Stride ) );
  2127. CenterOfMass = XMVectorAdd( CenterOfMass, Point );
  2128. }
  2129. CenterOfMass = XMVectorMultiply( CenterOfMass, XMVectorReciprocal( XMVectorReplicate( float( Count ) ) ) );
  2130. // Compute the inertia tensor of the points around the center of mass.
  2131. // Using the center of mass is not strictly necessary, but will hopefully
  2132. // improve the stability of finding the eigenvectors.
  2133. XMVECTOR XX_YY_ZZ = XMVectorZero();
  2134. XMVECTOR XY_XZ_YZ = XMVectorZero();
  2135. for( size_t i = 0; i < Count; ++i )
  2136. {
  2137. XMVECTOR Point = XMVectorSubtract( XMLoadFloat3( reinterpret_cast<const XMFLOAT3*>( reinterpret_cast<const uint8_t*>(pPoints) + i * Stride ) ), CenterOfMass );
  2138. XX_YY_ZZ = XMVectorAdd( XX_YY_ZZ, XMVectorMultiply( Point, Point ) );
  2139. XMVECTOR XXY = XMVectorSwizzle<XM_SWIZZLE_X, XM_SWIZZLE_X, XM_SWIZZLE_Y, XM_SWIZZLE_W>( Point );
  2140. XMVECTOR YZZ = XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_Z, XM_SWIZZLE_Z, XM_SWIZZLE_W>( Point );
  2141. XY_XZ_YZ = XMVectorAdd( XY_XZ_YZ, XMVectorMultiply( XXY, YZZ ) );
  2142. }
  2143. XMVECTOR v1, v2, v3;
  2144. // Compute the eigenvectors of the inertia tensor.
  2145. DirectX::Internal::CalculateEigenVectorsFromCovarianceMatrix( XMVectorGetX( XX_YY_ZZ ), XMVectorGetY( XX_YY_ZZ ),
  2146. XMVectorGetZ( XX_YY_ZZ ),
  2147. XMVectorGetX( XY_XZ_YZ ), XMVectorGetY( XY_XZ_YZ ),
  2148. XMVectorGetZ( XY_XZ_YZ ),
  2149. &v1, &v2, &v3 );
  2150. // Put them in a matrix.
  2151. XMMATRIX R;
  2152. R.r[0] = XMVectorSetW( v1, 0.f );
  2153. R.r[1] = XMVectorSetW( v2, 0.f );
  2154. R.r[2] = XMVectorSetW( v3, 0.f );
  2155. R.r[3] = g_XMIdentityR3.v;
  2156. // Multiply by -1 to convert the matrix into a right handed coordinate
  2157. // system (Det ~= 1) in case the eigenvectors form a left handed
  2158. // coordinate system (Det ~= -1) because XMQuaternionRotationMatrix only
  2159. // works on right handed matrices.
  2160. XMVECTOR Det = XMMatrixDeterminant( R );
  2161. if( XMVector4Less( Det, XMVectorZero() ) )
  2162. {
  2163. R.r[0] = XMVectorMultiply( R.r[0], g_XMNegativeOne.v );
  2164. R.r[1] = XMVectorMultiply( R.r[1], g_XMNegativeOne.v );
  2165. R.r[2] = XMVectorMultiply( R.r[2], g_XMNegativeOne.v );
  2166. }
  2167. // Get the rotation quaternion from the matrix.
  2168. XMVECTOR vOrientation = XMQuaternionRotationMatrix( R );
  2169. // Make sure it is normal (in case the vectors are slightly non-orthogonal).
  2170. vOrientation = XMQuaternionNormalize( vOrientation );
  2171. // Rebuild the rotation matrix from the quaternion.
  2172. R = XMMatrixRotationQuaternion( vOrientation );
  2173. // Build the rotation into the rotated space.
  2174. XMMATRIX InverseR = XMMatrixTranspose( R );
  2175. // Find the minimum OBB using the eigenvectors as the axes.
  2176. XMVECTOR vMin, vMax;
  2177. vMin = vMax = XMVector3TransformNormal( XMLoadFloat3( pPoints ), InverseR );
  2178. for( size_t i = 1; i < Count; ++i )
  2179. {
  2180. XMVECTOR Point = XMVector3TransformNormal( XMLoadFloat3( reinterpret_cast<const XMFLOAT3*>( reinterpret_cast<const uint8_t*>(pPoints) + i * Stride ) ),
  2181. InverseR );
  2182. vMin = XMVectorMin( vMin, Point );
  2183. vMax = XMVectorMax( vMax, Point );
  2184. }
  2185. // Rotate the center into world space.
  2186. XMVECTOR vCenter = XMVectorScale( XMVectorAdd( vMin, vMax ), 0.5f );
  2187. vCenter = XMVector3TransformNormal( vCenter, R );
  2188. // Store center, extents, and orientation.
  2189. XMStoreFloat3( &Out.Center, vCenter );
  2190. XMStoreFloat3( &Out.Extents, XMVectorScale( XMVectorSubtract( vMax, vMin ), 0.5f ) );
  2191. XMStoreFloat4( &Out.Orientation, vOrientation );
  2192. }
  2193. /****************************************************************************
  2194. *
  2195. * BoundingFrustum
  2196. *
  2197. ****************************************************************************/
  2198. //-----------------------------------------------------------------------------
  2199. // Transform a frustum by an angle preserving transform.
  2200. //-----------------------------------------------------------------------------
  2201. _Use_decl_annotations_
  2202. inline void XM_CALLCONV BoundingFrustum::Transform( BoundingFrustum& Out, FXMMATRIX M ) const
  2203. {
  2204. // Load the frustum.
  2205. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2206. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2207. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  2208. // Composite the frustum rotation and the transform rotation
  2209. XMMATRIX nM;
  2210. nM.r[0] = XMVector3Normalize( M.r[0] );
  2211. nM.r[1] = XMVector3Normalize( M.r[1] );
  2212. nM.r[2] = XMVector3Normalize( M.r[2] );
  2213. nM.r[3] = g_XMIdentityR3;
  2214. XMVECTOR Rotation = XMQuaternionRotationMatrix( nM );
  2215. vOrientation = XMQuaternionMultiply( vOrientation, Rotation );
  2216. // Transform the center.
  2217. vOrigin = XMVector3Transform( vOrigin, M );
  2218. // Store the frustum.
  2219. XMStoreFloat3( &Out.Origin, vOrigin );
  2220. XMStoreFloat4( &Out.Orientation, vOrientation );
  2221. // Scale the near and far distances (the slopes remain the same).
  2222. XMVECTOR dX = XMVector3Dot( M.r[0], M.r[0] );
  2223. XMVECTOR dY = XMVector3Dot( M.r[1], M.r[1] );
  2224. XMVECTOR dZ = XMVector3Dot( M.r[2], M.r[2] );
  2225. XMVECTOR d = XMVectorMax( dX, XMVectorMax( dY, dZ ) );
  2226. float Scale = sqrtf( XMVectorGetX(d) );
  2227. Out.Near = Near * Scale;
  2228. Out.Far = Far * Scale;
  2229. // Copy the slopes.
  2230. Out.RightSlope = RightSlope;
  2231. Out.LeftSlope = LeftSlope;
  2232. Out.TopSlope = TopSlope;
  2233. Out.BottomSlope = BottomSlope;
  2234. }
  2235. _Use_decl_annotations_
  2236. inline void XM_CALLCONV BoundingFrustum::Transform( BoundingFrustum& Out, float Scale, FXMVECTOR Rotation, FXMVECTOR Translation ) const
  2237. {
  2238. assert( DirectX::Internal::XMQuaternionIsUnit( Rotation ) );
  2239. // Load the frustum.
  2240. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2241. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2242. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  2243. // Composite the frustum rotation and the transform rotation.
  2244. vOrientation = XMQuaternionMultiply( vOrientation, Rotation );
  2245. // Transform the origin.
  2246. vOrigin = XMVectorAdd( XMVector3Rotate( XMVectorScale( vOrigin, Scale ), Rotation ), Translation );
  2247. // Store the frustum.
  2248. XMStoreFloat3( &Out.Origin, vOrigin );
  2249. XMStoreFloat4( &Out.Orientation, vOrientation );
  2250. // Scale the near and far distances (the slopes remain the same).
  2251. Out.Near = Near * Scale;
  2252. Out.Far = Far * Scale;
  2253. // Copy the slopes.
  2254. Out.RightSlope = RightSlope;
  2255. Out.LeftSlope = LeftSlope;
  2256. Out.TopSlope = TopSlope;
  2257. Out.BottomSlope = BottomSlope;
  2258. }
  2259. //-----------------------------------------------------------------------------
  2260. // Get the corner points of the frustum
  2261. //-----------------------------------------------------------------------------
  2262. _Use_decl_annotations_
  2263. inline void BoundingFrustum::GetCorners( XMFLOAT3* Corners ) const
  2264. {
  2265. assert( Corners != 0 );
  2266. // Load origin and orientation of the frustum.
  2267. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2268. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2269. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  2270. // Build the corners of the frustum.
  2271. XMVECTOR vRightTop = XMVectorSet( RightSlope, TopSlope, 1.0f, 0.0f );
  2272. XMVECTOR vRightBottom = XMVectorSet( RightSlope, BottomSlope, 1.0f, 0.0f );
  2273. XMVECTOR vLeftTop = XMVectorSet( LeftSlope, TopSlope, 1.0f, 0.0f );
  2274. XMVECTOR vLeftBottom = XMVectorSet( LeftSlope, BottomSlope, 1.0f, 0.0f );
  2275. XMVECTOR vNear = XMVectorReplicatePtr( &Near );
  2276. XMVECTOR vFar = XMVectorReplicatePtr( &Far );
  2277. // Returns 8 corners position of bounding frustum.
  2278. // Near Far
  2279. // 0----1 4----5
  2280. // | | | |
  2281. // | | | |
  2282. // 3----2 7----6
  2283. XMVECTOR vCorners[CORNER_COUNT];
  2284. vCorners[0] = XMVectorMultiply( vLeftTop, vNear );
  2285. vCorners[1] = XMVectorMultiply( vRightTop, vNear );
  2286. vCorners[2] = XMVectorMultiply( vRightBottom, vNear );
  2287. vCorners[3] = XMVectorMultiply( vLeftBottom, vNear );
  2288. vCorners[4] = XMVectorMultiply( vLeftTop, vFar );
  2289. vCorners[5] = XMVectorMultiply( vRightTop, vFar );
  2290. vCorners[6] = XMVectorMultiply( vRightBottom, vFar );
  2291. vCorners[7] = XMVectorMultiply( vLeftBottom, vFar );
  2292. for( size_t i=0; i < CORNER_COUNT; ++i )
  2293. {
  2294. XMVECTOR C = XMVectorAdd( XMVector3Rotate( vCorners[i], vOrientation ), vOrigin );
  2295. XMStoreFloat3( &Corners[i], C );
  2296. }
  2297. }
  2298. //-----------------------------------------------------------------------------
  2299. // Point in frustum test.
  2300. //-----------------------------------------------------------------------------
  2301. _Use_decl_annotations_
  2302. inline ContainmentType XM_CALLCONV BoundingFrustum::Contains( FXMVECTOR Point ) const
  2303. {
  2304. // Build frustum planes.
  2305. XMVECTOR Planes[6];
  2306. Planes[0] = XMVectorSet( 0.0f, 0.0f, -1.0f, Near );
  2307. Planes[1] = XMVectorSet( 0.0f, 0.0f, 1.0f, -Far );
  2308. Planes[2] = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  2309. Planes[3] = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  2310. Planes[4] = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  2311. Planes[5] = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  2312. // Load origin and orientation.
  2313. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2314. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2315. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  2316. // Transform point into local space of frustum.
  2317. XMVECTOR TPoint = XMVector3InverseRotate( XMVectorSubtract( Point, vOrigin ), vOrientation );
  2318. // Set w to one.
  2319. TPoint = XMVectorInsert<0, 0, 0, 0, 1>( TPoint, XMVectorSplatOne() );
  2320. XMVECTOR Zero = XMVectorZero();
  2321. XMVECTOR Outside = Zero;
  2322. // Test point against each plane of the frustum.
  2323. for( size_t i = 0; i < 6; ++i )
  2324. {
  2325. XMVECTOR Dot = XMVector4Dot( TPoint, Planes[i] );
  2326. Outside = XMVectorOrInt( Outside, XMVectorGreater( Dot, Zero ) );
  2327. }
  2328. return XMVector4NotEqualInt( Outside, XMVectorTrueInt() ) ? CONTAINS : DISJOINT;
  2329. }
  2330. //-----------------------------------------------------------------------------
  2331. // Triangle vs frustum test.
  2332. //-----------------------------------------------------------------------------
  2333. _Use_decl_annotations_
  2334. inline ContainmentType XM_CALLCONV BoundingFrustum::Contains( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2 ) const
  2335. {
  2336. // Load origin and orientation of the frustum.
  2337. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2338. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2339. // Create 6 planes (do it inline to encourage use of registers)
  2340. XMVECTOR NearPlane = XMVectorSet( 0.0f, 0.0f, -1.0f, Near );
  2341. NearPlane = DirectX::Internal::XMPlaneTransform( NearPlane, vOrientation, vOrigin );
  2342. NearPlane = XMPlaneNormalize( NearPlane );
  2343. XMVECTOR FarPlane = XMVectorSet( 0.0f, 0.0f, 1.0f, -Far );
  2344. FarPlane = DirectX::Internal::XMPlaneTransform( FarPlane, vOrientation, vOrigin );
  2345. FarPlane = XMPlaneNormalize( FarPlane );
  2346. XMVECTOR RightPlane = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  2347. RightPlane = DirectX::Internal::XMPlaneTransform( RightPlane, vOrientation, vOrigin );
  2348. RightPlane = XMPlaneNormalize( RightPlane );
  2349. XMVECTOR LeftPlane = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  2350. LeftPlane = DirectX::Internal::XMPlaneTransform( LeftPlane, vOrientation, vOrigin );
  2351. LeftPlane = XMPlaneNormalize( LeftPlane );
  2352. XMVECTOR TopPlane = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  2353. TopPlane = DirectX::Internal::XMPlaneTransform( TopPlane, vOrientation, vOrigin );
  2354. TopPlane = XMPlaneNormalize( TopPlane );
  2355. XMVECTOR BottomPlane = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  2356. BottomPlane = DirectX::Internal::XMPlaneTransform( BottomPlane, vOrientation, vOrigin );
  2357. BottomPlane = XMPlaneNormalize( BottomPlane );
  2358. return TriangleTests::ContainedBy( V0, V1, V2, NearPlane, FarPlane, RightPlane, LeftPlane, TopPlane, BottomPlane );
  2359. }
  2360. //-----------------------------------------------------------------------------
  2361. _Use_decl_annotations_
  2362. inline ContainmentType BoundingFrustum::Contains( const BoundingSphere& sh ) const
  2363. {
  2364. // Load origin and orientation of the frustum.
  2365. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2366. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2367. // Create 6 planes (do it inline to encourage use of registers)
  2368. XMVECTOR NearPlane = XMVectorSet( 0.0f, 0.0f, -1.0f, Near );
  2369. NearPlane = DirectX::Internal::XMPlaneTransform( NearPlane, vOrientation, vOrigin );
  2370. NearPlane = XMPlaneNormalize( NearPlane );
  2371. XMVECTOR FarPlane = XMVectorSet( 0.0f, 0.0f, 1.0f, -Far );
  2372. FarPlane = DirectX::Internal::XMPlaneTransform( FarPlane, vOrientation, vOrigin );
  2373. FarPlane = XMPlaneNormalize( FarPlane );
  2374. XMVECTOR RightPlane = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  2375. RightPlane = DirectX::Internal::XMPlaneTransform( RightPlane, vOrientation, vOrigin );
  2376. RightPlane = XMPlaneNormalize( RightPlane );
  2377. XMVECTOR LeftPlane = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  2378. LeftPlane = DirectX::Internal::XMPlaneTransform( LeftPlane, vOrientation, vOrigin );
  2379. LeftPlane = XMPlaneNormalize( LeftPlane );
  2380. XMVECTOR TopPlane = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  2381. TopPlane = DirectX::Internal::XMPlaneTransform( TopPlane, vOrientation, vOrigin );
  2382. TopPlane = XMPlaneNormalize( TopPlane );
  2383. XMVECTOR BottomPlane = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  2384. BottomPlane = DirectX::Internal::XMPlaneTransform( BottomPlane, vOrientation, vOrigin );
  2385. BottomPlane = XMPlaneNormalize( BottomPlane );
  2386. return sh.ContainedBy( NearPlane, FarPlane, RightPlane, LeftPlane, TopPlane, BottomPlane );
  2387. }
  2388. //-----------------------------------------------------------------------------
  2389. _Use_decl_annotations_
  2390. inline ContainmentType BoundingFrustum::Contains( const BoundingBox& box ) const
  2391. {
  2392. // Load origin and orientation of the frustum.
  2393. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2394. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2395. // Create 6 planes (do it inline to encourage use of registers)
  2396. XMVECTOR NearPlane = XMVectorSet( 0.0f, 0.0f, -1.0f, Near );
  2397. NearPlane = DirectX::Internal::XMPlaneTransform( NearPlane, vOrientation, vOrigin );
  2398. NearPlane = XMPlaneNormalize( NearPlane );
  2399. XMVECTOR FarPlane = XMVectorSet( 0.0f, 0.0f, 1.0f, -Far );
  2400. FarPlane = DirectX::Internal::XMPlaneTransform( FarPlane, vOrientation, vOrigin );
  2401. FarPlane = XMPlaneNormalize( FarPlane );
  2402. XMVECTOR RightPlane = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  2403. RightPlane = DirectX::Internal::XMPlaneTransform( RightPlane, vOrientation, vOrigin );
  2404. RightPlane = XMPlaneNormalize( RightPlane );
  2405. XMVECTOR LeftPlane = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  2406. LeftPlane = DirectX::Internal::XMPlaneTransform( LeftPlane, vOrientation, vOrigin );
  2407. LeftPlane = XMPlaneNormalize( LeftPlane );
  2408. XMVECTOR TopPlane = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  2409. TopPlane = DirectX::Internal::XMPlaneTransform( TopPlane, vOrientation, vOrigin );
  2410. TopPlane = XMPlaneNormalize( TopPlane );
  2411. XMVECTOR BottomPlane = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  2412. BottomPlane = DirectX::Internal::XMPlaneTransform( BottomPlane, vOrientation, vOrigin );
  2413. BottomPlane = XMPlaneNormalize( BottomPlane );
  2414. return box.ContainedBy( NearPlane, FarPlane, RightPlane, LeftPlane, TopPlane, BottomPlane );
  2415. }
  2416. //-----------------------------------------------------------------------------
  2417. _Use_decl_annotations_
  2418. inline ContainmentType BoundingFrustum::Contains( const BoundingOrientedBox& box ) const
  2419. {
  2420. // Load origin and orientation of the frustum.
  2421. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2422. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2423. // Create 6 planes (do it inline to encourage use of registers)
  2424. XMVECTOR NearPlane = XMVectorSet( 0.0f, 0.0f, -1.0f, Near );
  2425. NearPlane = DirectX::Internal::XMPlaneTransform( NearPlane, vOrientation, vOrigin );
  2426. NearPlane = XMPlaneNormalize( NearPlane );
  2427. XMVECTOR FarPlane = XMVectorSet( 0.0f, 0.0f, 1.0f, -Far );
  2428. FarPlane = DirectX::Internal::XMPlaneTransform( FarPlane, vOrientation, vOrigin );
  2429. FarPlane = XMPlaneNormalize( FarPlane );
  2430. XMVECTOR RightPlane = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  2431. RightPlane = DirectX::Internal::XMPlaneTransform( RightPlane, vOrientation, vOrigin );
  2432. RightPlane = XMPlaneNormalize( RightPlane );
  2433. XMVECTOR LeftPlane = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  2434. LeftPlane = DirectX::Internal::XMPlaneTransform( LeftPlane, vOrientation, vOrigin );
  2435. LeftPlane = XMPlaneNormalize( LeftPlane );
  2436. XMVECTOR TopPlane = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  2437. TopPlane = DirectX::Internal::XMPlaneTransform( TopPlane, vOrientation, vOrigin );
  2438. TopPlane = XMPlaneNormalize( TopPlane );
  2439. XMVECTOR BottomPlane = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  2440. BottomPlane = DirectX::Internal::XMPlaneTransform( BottomPlane, vOrientation, vOrigin );
  2441. BottomPlane = XMPlaneNormalize( BottomPlane );
  2442. return box.ContainedBy( NearPlane, FarPlane, RightPlane, LeftPlane, TopPlane, BottomPlane );
  2443. }
  2444. //-----------------------------------------------------------------------------
  2445. _Use_decl_annotations_
  2446. inline ContainmentType BoundingFrustum::Contains( const BoundingFrustum& fr ) const
  2447. {
  2448. // Load origin and orientation of the frustum.
  2449. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2450. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2451. // Create 6 planes (do it inline to encourage use of registers)
  2452. XMVECTOR NearPlane = XMVectorSet( 0.0f, 0.0f, -1.0f, Near );
  2453. NearPlane = DirectX::Internal::XMPlaneTransform( NearPlane, vOrientation, vOrigin );
  2454. NearPlane = XMPlaneNormalize( NearPlane );
  2455. XMVECTOR FarPlane = XMVectorSet( 0.0f, 0.0f, 1.0f, -Far );
  2456. FarPlane = DirectX::Internal::XMPlaneTransform( FarPlane, vOrientation, vOrigin );
  2457. FarPlane = XMPlaneNormalize( FarPlane );
  2458. XMVECTOR RightPlane = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  2459. RightPlane = DirectX::Internal::XMPlaneTransform( RightPlane, vOrientation, vOrigin );
  2460. RightPlane = XMPlaneNormalize( RightPlane );
  2461. XMVECTOR LeftPlane = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  2462. LeftPlane = DirectX::Internal::XMPlaneTransform( LeftPlane, vOrientation, vOrigin );
  2463. LeftPlane = XMPlaneNormalize( LeftPlane );
  2464. XMVECTOR TopPlane = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  2465. TopPlane = DirectX::Internal::XMPlaneTransform( TopPlane, vOrientation, vOrigin );
  2466. TopPlane = XMPlaneNormalize( TopPlane );
  2467. XMVECTOR BottomPlane = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  2468. BottomPlane = DirectX::Internal::XMPlaneTransform( BottomPlane, vOrientation, vOrigin );
  2469. BottomPlane = XMPlaneNormalize( BottomPlane );
  2470. return fr.ContainedBy( NearPlane, FarPlane, RightPlane, LeftPlane, TopPlane, BottomPlane );
  2471. }
  2472. //-----------------------------------------------------------------------------
  2473. // Exact sphere vs frustum test. The algorithm first checks the sphere against
  2474. // the planes of the frustum, then if the plane checks were indeterminate finds
  2475. // the nearest feature (plane, line, point) on the frustum to the center of the
  2476. // sphere and compares the distance to the nearest feature to the radius of the
  2477. // sphere
  2478. //-----------------------------------------------------------------------------
  2479. _Use_decl_annotations_
  2480. inline bool BoundingFrustum::Intersects( const BoundingSphere& sh ) const
  2481. {
  2482. XMVECTOR Zero = XMVectorZero();
  2483. // Build the frustum planes.
  2484. XMVECTOR Planes[6];
  2485. Planes[0] = XMVectorSet( 0.0f, 0.0f, -1.0f, Near );
  2486. Planes[1] = XMVectorSet( 0.0f, 0.0f, 1.0f, -Far );
  2487. Planes[2] = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  2488. Planes[3] = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  2489. Planes[4] = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  2490. Planes[5] = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  2491. // Normalize the planes so we can compare to the sphere radius.
  2492. Planes[2] = XMVector3Normalize( Planes[2] );
  2493. Planes[3] = XMVector3Normalize( Planes[3] );
  2494. Planes[4] = XMVector3Normalize( Planes[4] );
  2495. Planes[5] = XMVector3Normalize( Planes[5] );
  2496. // Load origin and orientation of the frustum.
  2497. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2498. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2499. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  2500. // Load the sphere.
  2501. XMVECTOR vCenter = XMLoadFloat3( &sh.Center );
  2502. XMVECTOR vRadius = XMVectorReplicatePtr( &sh.Radius );
  2503. // Transform the center of the sphere into the local space of frustum.
  2504. vCenter = XMVector3InverseRotate( XMVectorSubtract( vCenter, vOrigin ), vOrientation );
  2505. // Set w of the center to one so we can dot4 with the plane.
  2506. vCenter = XMVectorInsert<0, 0, 0, 0, 1>( vCenter, XMVectorSplatOne() );
  2507. // Check against each plane of the frustum.
  2508. XMVECTOR Outside = XMVectorFalseInt();
  2509. XMVECTOR InsideAll = XMVectorTrueInt();
  2510. XMVECTOR CenterInsideAll = XMVectorTrueInt();
  2511. XMVECTOR Dist[6];
  2512. for( size_t i = 0; i < 6; ++i )
  2513. {
  2514. Dist[i] = XMVector4Dot( vCenter, Planes[i] );
  2515. // Outside the plane?
  2516. Outside = XMVectorOrInt( Outside, XMVectorGreater( Dist[i], vRadius ) );
  2517. // Fully inside the plane?
  2518. InsideAll = XMVectorAndInt( InsideAll, XMVectorLessOrEqual( Dist[i], XMVectorNegate( vRadius ) ) );
  2519. // Check if the center is inside the plane.
  2520. CenterInsideAll = XMVectorAndInt( CenterInsideAll, XMVectorLessOrEqual( Dist[i], Zero ) );
  2521. }
  2522. // If the sphere is outside any of the planes it is outside.
  2523. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  2524. return false;
  2525. // If the sphere is inside all planes it is fully inside.
  2526. if ( XMVector4EqualInt( InsideAll, XMVectorTrueInt() ) )
  2527. return true;
  2528. // If the center of the sphere is inside all planes and the sphere intersects
  2529. // one or more planes then it must intersect.
  2530. if ( XMVector4EqualInt( CenterInsideAll, XMVectorTrueInt() ) )
  2531. return true;
  2532. // The sphere may be outside the frustum or intersecting the frustum.
  2533. // Find the nearest feature (face, edge, or corner) on the frustum
  2534. // to the sphere.
  2535. // The faces adjacent to each face are:
  2536. static const size_t adjacent_faces[6][4] =
  2537. {
  2538. { 2, 3, 4, 5 }, // 0
  2539. { 2, 3, 4, 5 }, // 1
  2540. { 0, 1, 4, 5 }, // 2
  2541. { 0, 1, 4, 5 }, // 3
  2542. { 0, 1, 2, 3 }, // 4
  2543. { 0, 1, 2, 3 }
  2544. }; // 5
  2545. XMVECTOR Intersects = XMVectorFalseInt();
  2546. // Check to see if the nearest feature is one of the planes.
  2547. for( size_t i = 0; i < 6; ++i )
  2548. {
  2549. // Find the nearest point on the plane to the center of the sphere.
  2550. XMVECTOR Point = XMVectorNegativeMultiplySubtract( Planes[i], Dist[i], vCenter );
  2551. // Set w of the point to one.
  2552. Point = XMVectorInsert<0, 0, 0, 0, 1>( Point, XMVectorSplatOne() );
  2553. // If the point is inside the face (inside the adjacent planes) then
  2554. // this plane is the nearest feature.
  2555. XMVECTOR InsideFace = XMVectorTrueInt();
  2556. for ( size_t j = 0; j < 4; j++ )
  2557. {
  2558. size_t plane_index = adjacent_faces[i][j];
  2559. InsideFace = XMVectorAndInt( InsideFace,
  2560. XMVectorLessOrEqual( XMVector4Dot( Point, Planes[plane_index] ), Zero ) );
  2561. }
  2562. // Since we have already checked distance from the plane we know that the
  2563. // sphere must intersect if this plane is the nearest feature.
  2564. Intersects = XMVectorOrInt( Intersects,
  2565. XMVectorAndInt( XMVectorGreater( Dist[i], Zero ), InsideFace ) );
  2566. }
  2567. if ( XMVector4EqualInt( Intersects, XMVectorTrueInt() ) )
  2568. return true;
  2569. // Build the corners of the frustum.
  2570. XMVECTOR vRightTop = XMVectorSet( RightSlope, TopSlope, 1.0f, 0.0f );
  2571. XMVECTOR vRightBottom = XMVectorSet( RightSlope, BottomSlope, 1.0f, 0.0f );
  2572. XMVECTOR vLeftTop = XMVectorSet( LeftSlope, TopSlope, 1.0f, 0.0f );
  2573. XMVECTOR vLeftBottom = XMVectorSet( LeftSlope, BottomSlope, 1.0f, 0.0f );
  2574. XMVECTOR vNear = XMVectorReplicatePtr( &Near );
  2575. XMVECTOR vFar = XMVectorReplicatePtr( &Far );
  2576. XMVECTOR Corners[CORNER_COUNT];
  2577. Corners[0] = XMVectorMultiply( vRightTop, vNear );
  2578. Corners[1] = XMVectorMultiply( vRightBottom, vNear );
  2579. Corners[2] = XMVectorMultiply( vLeftTop, vNear );
  2580. Corners[3] = XMVectorMultiply( vLeftBottom, vNear );
  2581. Corners[4] = XMVectorMultiply( vRightTop, vFar );
  2582. Corners[5] = XMVectorMultiply( vRightBottom, vFar );
  2583. Corners[6] = XMVectorMultiply( vLeftTop, vFar );
  2584. Corners[7] = XMVectorMultiply( vLeftBottom, vFar );
  2585. // The Edges are:
  2586. static const size_t edges[12][2] =
  2587. {
  2588. { 0, 1 }, { 2, 3 }, { 0, 2 }, { 1, 3 }, // Near plane
  2589. { 4, 5 }, { 6, 7 }, { 4, 6 }, { 5, 7 }, // Far plane
  2590. { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 },
  2591. }; // Near to far
  2592. XMVECTOR RadiusSq = XMVectorMultiply( vRadius, vRadius );
  2593. // Check to see if the nearest feature is one of the edges (or corners).
  2594. for( size_t i = 0; i < 12; ++i )
  2595. {
  2596. size_t ei0 = edges[i][0];
  2597. size_t ei1 = edges[i][1];
  2598. // Find the nearest point on the edge to the center of the sphere.
  2599. // The corners of the frustum are included as the endpoints of the edges.
  2600. XMVECTOR Point = DirectX::Internal::PointOnLineSegmentNearestPoint( Corners[ei0], Corners[ei1], vCenter );
  2601. XMVECTOR Delta = XMVectorSubtract( vCenter, Point );
  2602. XMVECTOR DistSq = XMVector3Dot( Delta, Delta );
  2603. // If the distance to the center of the sphere to the point is less than
  2604. // the radius of the sphere then it must intersect.
  2605. Intersects = XMVectorOrInt( Intersects, XMVectorLessOrEqual( DistSq, RadiusSq ) );
  2606. }
  2607. if ( XMVector4EqualInt( Intersects, XMVectorTrueInt() ) )
  2608. return true;
  2609. // The sphere must be outside the frustum.
  2610. return false;
  2611. }
  2612. //-----------------------------------------------------------------------------
  2613. // Exact axis aligned box vs frustum test. Constructs an oriented box and uses
  2614. // the oriented box vs frustum test.
  2615. //-----------------------------------------------------------------------------
  2616. _Use_decl_annotations_
  2617. inline bool BoundingFrustum::Intersects( const BoundingBox& box ) const
  2618. {
  2619. // Make the axis aligned box oriented and do an OBB vs frustum test.
  2620. BoundingOrientedBox obox( box.Center, box.Extents, XMFLOAT4( 0.f, 0.f, 0.f, 1.f ) );
  2621. return Intersects( obox );
  2622. }
  2623. //-----------------------------------------------------------------------------
  2624. // Exact oriented box vs frustum test.
  2625. //-----------------------------------------------------------------------------
  2626. _Use_decl_annotations_
  2627. inline bool BoundingFrustum::Intersects( const BoundingOrientedBox& box ) const
  2628. {
  2629. static const XMVECTORU32 SelectY = { { { XM_SELECT_0, XM_SELECT_1, XM_SELECT_0, XM_SELECT_0 } } };
  2630. static const XMVECTORU32 SelectZ = { { { XM_SELECT_0, XM_SELECT_0, XM_SELECT_1, XM_SELECT_0 } } };
  2631. XMVECTOR Zero = XMVectorZero();
  2632. // Build the frustum planes.
  2633. XMVECTOR Planes[6];
  2634. Planes[0] = XMVectorSet( 0.0f, 0.0f, -1.0f, Near );
  2635. Planes[1] = XMVectorSet( 0.0f, 0.0f, 1.0f, -Far );
  2636. Planes[2] = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  2637. Planes[3] = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  2638. Planes[4] = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  2639. Planes[5] = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  2640. // Load origin and orientation of the frustum.
  2641. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2642. XMVECTOR FrustumOrientation = XMLoadFloat4( &Orientation );
  2643. assert( DirectX::Internal::XMQuaternionIsUnit( FrustumOrientation ) );
  2644. // Load the box.
  2645. XMVECTOR Center = XMLoadFloat3( &box.Center );
  2646. XMVECTOR Extents = XMLoadFloat3( &box.Extents );
  2647. XMVECTOR BoxOrientation = XMLoadFloat4( &box.Orientation );
  2648. assert( DirectX::Internal::XMQuaternionIsUnit( BoxOrientation ) );
  2649. // Transform the oriented box into the space of the frustum in order to
  2650. // minimize the number of transforms we have to do.
  2651. Center = XMVector3InverseRotate( XMVectorSubtract( Center, vOrigin ), FrustumOrientation );
  2652. BoxOrientation = XMQuaternionMultiply( BoxOrientation, XMQuaternionConjugate( FrustumOrientation ) );
  2653. // Set w of the center to one so we can dot4 with the plane.
  2654. Center = XMVectorInsert<0, 0, 0, 0, 1>( Center, XMVectorSplatOne() );
  2655. // Build the 3x3 rotation matrix that defines the box axes.
  2656. XMMATRIX R = XMMatrixRotationQuaternion( BoxOrientation );
  2657. // Check against each plane of the frustum.
  2658. XMVECTOR Outside = XMVectorFalseInt();
  2659. XMVECTOR InsideAll = XMVectorTrueInt();
  2660. XMVECTOR CenterInsideAll = XMVectorTrueInt();
  2661. for( size_t i = 0; i < 6; ++i )
  2662. {
  2663. // Compute the distance to the center of the box.
  2664. XMVECTOR Dist = XMVector4Dot( Center, Planes[i] );
  2665. // Project the axes of the box onto the normal of the plane. Half the
  2666. // length of the projection (sometime called the "radius") is equal to
  2667. // h(u) * abs(n dot b(u))) + h(v) * abs(n dot b(v)) + h(w) * abs(n dot b(w))
  2668. // where h(i) are extents of the box, n is the plane normal, and b(i) are the
  2669. // axes of the box.
  2670. XMVECTOR Radius = XMVector3Dot( Planes[i], R.r[0] );
  2671. Radius = XMVectorSelect( Radius, XMVector3Dot( Planes[i], R.r[1] ), SelectY );
  2672. Radius = XMVectorSelect( Radius, XMVector3Dot( Planes[i], R.r[2] ), SelectZ );
  2673. Radius = XMVector3Dot( Extents, XMVectorAbs( Radius ) );
  2674. // Outside the plane?
  2675. Outside = XMVectorOrInt( Outside, XMVectorGreater( Dist, Radius ) );
  2676. // Fully inside the plane?
  2677. InsideAll = XMVectorAndInt( InsideAll, XMVectorLessOrEqual( Dist, XMVectorNegate( Radius ) ) );
  2678. // Check if the center is inside the plane.
  2679. CenterInsideAll = XMVectorAndInt( CenterInsideAll, XMVectorLessOrEqual( Dist, Zero ) );
  2680. }
  2681. // If the box is outside any of the planes it is outside.
  2682. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  2683. return false;
  2684. // If the box is inside all planes it is fully inside.
  2685. if ( XMVector4EqualInt( InsideAll, XMVectorTrueInt() ) )
  2686. return true;
  2687. // If the center of the box is inside all planes and the box intersects
  2688. // one or more planes then it must intersect.
  2689. if ( XMVector4EqualInt( CenterInsideAll, XMVectorTrueInt() ) )
  2690. return true;
  2691. // Build the corners of the frustum.
  2692. XMVECTOR vRightTop = XMVectorSet( RightSlope, TopSlope, 1.0f, 0.0f );
  2693. XMVECTOR vRightBottom = XMVectorSet( RightSlope, BottomSlope, 1.0f, 0.0f );
  2694. XMVECTOR vLeftTop = XMVectorSet( LeftSlope, TopSlope, 1.0f, 0.0f );
  2695. XMVECTOR vLeftBottom = XMVectorSet( LeftSlope, BottomSlope, 1.0f, 0.0f );
  2696. XMVECTOR vNear = XMVectorReplicatePtr( &Near );
  2697. XMVECTOR vFar = XMVectorReplicatePtr( &Far );
  2698. XMVECTOR Corners[CORNER_COUNT];
  2699. Corners[0] = XMVectorMultiply( vRightTop, vNear );
  2700. Corners[1] = XMVectorMultiply( vRightBottom, vNear );
  2701. Corners[2] = XMVectorMultiply( vLeftTop, vNear );
  2702. Corners[3] = XMVectorMultiply( vLeftBottom, vNear );
  2703. Corners[4] = XMVectorMultiply( vRightTop, vFar );
  2704. Corners[5] = XMVectorMultiply( vRightBottom, vFar );
  2705. Corners[6] = XMVectorMultiply( vLeftTop, vFar );
  2706. Corners[7] = XMVectorMultiply( vLeftBottom, vFar );
  2707. // Test against box axes (3)
  2708. {
  2709. // Find the min/max values of the projection of the frustum onto each axis.
  2710. XMVECTOR FrustumMin, FrustumMax;
  2711. FrustumMin = XMVector3Dot( Corners[0], R.r[0] );
  2712. FrustumMin = XMVectorSelect( FrustumMin, XMVector3Dot( Corners[0], R.r[1] ), SelectY );
  2713. FrustumMin = XMVectorSelect( FrustumMin, XMVector3Dot( Corners[0], R.r[2] ), SelectZ );
  2714. FrustumMax = FrustumMin;
  2715. for( size_t i = 1; i < BoundingOrientedBox::CORNER_COUNT; ++i )
  2716. {
  2717. XMVECTOR Temp = XMVector3Dot( Corners[i], R.r[0] );
  2718. Temp = XMVectorSelect( Temp, XMVector3Dot( Corners[i], R.r[1] ), SelectY );
  2719. Temp = XMVectorSelect( Temp, XMVector3Dot( Corners[i], R.r[2] ), SelectZ );
  2720. FrustumMin = XMVectorMin( FrustumMin, Temp );
  2721. FrustumMax = XMVectorMax( FrustumMax, Temp );
  2722. }
  2723. // Project the center of the box onto the axes.
  2724. XMVECTOR BoxDist = XMVector3Dot( Center, R.r[0] );
  2725. BoxDist = XMVectorSelect( BoxDist, XMVector3Dot( Center, R.r[1] ), SelectY );
  2726. BoxDist = XMVectorSelect( BoxDist, XMVector3Dot( Center, R.r[2] ), SelectZ );
  2727. // The projection of the box onto the axis is just its Center and Extents.
  2728. // if (min > box_max || max < box_min) reject;
  2729. XMVECTOR Result = XMVectorOrInt( XMVectorGreater( FrustumMin, XMVectorAdd( BoxDist, Extents ) ),
  2730. XMVectorLess( FrustumMax, XMVectorSubtract( BoxDist, Extents ) ) );
  2731. if( DirectX::Internal::XMVector3AnyTrue( Result ) )
  2732. return false;
  2733. }
  2734. // Test against edge/edge axes (3*6).
  2735. XMVECTOR FrustumEdgeAxis[6];
  2736. FrustumEdgeAxis[0] = vRightTop;
  2737. FrustumEdgeAxis[1] = vRightBottom;
  2738. FrustumEdgeAxis[2] = vLeftTop;
  2739. FrustumEdgeAxis[3] = vLeftBottom;
  2740. FrustumEdgeAxis[4] = XMVectorSubtract( vRightTop, vLeftTop );
  2741. FrustumEdgeAxis[5] = XMVectorSubtract( vLeftBottom, vLeftTop );
  2742. for( size_t i = 0; i < 3; ++i )
  2743. {
  2744. for( size_t j = 0; j < 6; j++ )
  2745. {
  2746. // Compute the axis we are going to test.
  2747. XMVECTOR Axis = XMVector3Cross( R.r[i], FrustumEdgeAxis[j] );
  2748. // Find the min/max values of the projection of the frustum onto the axis.
  2749. XMVECTOR FrustumMin, FrustumMax;
  2750. FrustumMin = FrustumMax = XMVector3Dot( Axis, Corners[0] );
  2751. for( size_t k = 1; k < CORNER_COUNT; k++ )
  2752. {
  2753. XMVECTOR Temp = XMVector3Dot( Axis, Corners[k] );
  2754. FrustumMin = XMVectorMin( FrustumMin, Temp );
  2755. FrustumMax = XMVectorMax( FrustumMax, Temp );
  2756. }
  2757. // Project the center of the box onto the axis.
  2758. XMVECTOR Dist = XMVector3Dot( Center, Axis );
  2759. // Project the axes of the box onto the axis to find the "radius" of the box.
  2760. XMVECTOR Radius = XMVector3Dot( Axis, R.r[0] );
  2761. Radius = XMVectorSelect( Radius, XMVector3Dot( Axis, R.r[1] ), SelectY );
  2762. Radius = XMVectorSelect( Radius, XMVector3Dot( Axis, R.r[2] ), SelectZ );
  2763. Radius = XMVector3Dot( Extents, XMVectorAbs( Radius ) );
  2764. // if (center > max + radius || center < min - radius) reject;
  2765. Outside = XMVectorOrInt( Outside, XMVectorGreater( Dist, XMVectorAdd( FrustumMax, Radius ) ) );
  2766. Outside = XMVectorOrInt( Outside, XMVectorLess( Dist, XMVectorSubtract( FrustumMin, Radius ) ) );
  2767. }
  2768. }
  2769. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  2770. return false;
  2771. // If we did not find a separating plane then the box must intersect the frustum.
  2772. return true;
  2773. }
  2774. //-----------------------------------------------------------------------------
  2775. // Exact frustum vs frustum test.
  2776. //-----------------------------------------------------------------------------
  2777. _Use_decl_annotations_
  2778. inline bool BoundingFrustum::Intersects( const BoundingFrustum& fr ) const
  2779. {
  2780. // Load origin and orientation of frustum B.
  2781. XMVECTOR OriginB = XMLoadFloat3( &Origin );
  2782. XMVECTOR OrientationB = XMLoadFloat4( &Orientation );
  2783. assert( DirectX::Internal::XMQuaternionIsUnit( OrientationB ) );
  2784. // Build the planes of frustum B.
  2785. XMVECTOR AxisB[6];
  2786. AxisB[0] = XMVectorSet( 0.0f, 0.0f, -1.0f, 0.0f );
  2787. AxisB[1] = XMVectorSet( 0.0f, 0.0f, 1.0f, 0.0f );
  2788. AxisB[2] = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  2789. AxisB[3] = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  2790. AxisB[4] = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  2791. AxisB[5] = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  2792. XMVECTOR PlaneDistB[6];
  2793. PlaneDistB[0] = XMVectorNegate( XMVectorReplicatePtr( &Near ) );
  2794. PlaneDistB[1] = XMVectorReplicatePtr( &Far );
  2795. PlaneDistB[2] = XMVectorZero();
  2796. PlaneDistB[3] = XMVectorZero();
  2797. PlaneDistB[4] = XMVectorZero();
  2798. PlaneDistB[5] = XMVectorZero();
  2799. // Load origin and orientation of frustum A.
  2800. XMVECTOR OriginA = XMLoadFloat3( &fr.Origin );
  2801. XMVECTOR OrientationA = XMLoadFloat4( &fr.Orientation );
  2802. assert( DirectX::Internal::XMQuaternionIsUnit( OrientationA ) );
  2803. // Transform frustum A into the space of the frustum B in order to
  2804. // minimize the number of transforms we have to do.
  2805. OriginA = XMVector3InverseRotate( XMVectorSubtract( OriginA, OriginB ), OrientationB );
  2806. OrientationA = XMQuaternionMultiply( OrientationA, XMQuaternionConjugate( OrientationB ) );
  2807. // Build the corners of frustum A (in the local space of B).
  2808. XMVECTOR RightTopA = XMVectorSet( fr.RightSlope, fr.TopSlope, 1.0f, 0.0f );
  2809. XMVECTOR RightBottomA = XMVectorSet( fr.RightSlope, fr.BottomSlope, 1.0f, 0.0f );
  2810. XMVECTOR LeftTopA = XMVectorSet(fr.LeftSlope,fr.TopSlope, 1.0f, 0.0f );
  2811. XMVECTOR LeftBottomA = XMVectorSet( fr.LeftSlope, fr.BottomSlope, 1.0f, 0.0f );
  2812. XMVECTOR NearA = XMVectorReplicatePtr( &fr.Near );
  2813. XMVECTOR FarA = XMVectorReplicatePtr( &fr.Far );
  2814. RightTopA = XMVector3Rotate( RightTopA, OrientationA );
  2815. RightBottomA = XMVector3Rotate( RightBottomA, OrientationA );
  2816. LeftTopA = XMVector3Rotate( LeftTopA, OrientationA );
  2817. LeftBottomA = XMVector3Rotate( LeftBottomA, OrientationA );
  2818. XMVECTOR CornersA[CORNER_COUNT];
  2819. CornersA[0] = XMVectorMultiplyAdd( RightTopA, NearA, OriginA );
  2820. CornersA[1] = XMVectorMultiplyAdd( RightBottomA, NearA, OriginA );
  2821. CornersA[2] = XMVectorMultiplyAdd( LeftTopA, NearA, OriginA );
  2822. CornersA[3] = XMVectorMultiplyAdd( LeftBottomA, NearA, OriginA );
  2823. CornersA[4] = XMVectorMultiplyAdd( RightTopA, FarA, OriginA );
  2824. CornersA[5] = XMVectorMultiplyAdd( RightBottomA, FarA, OriginA );
  2825. CornersA[6] = XMVectorMultiplyAdd( LeftTopA, FarA, OriginA );
  2826. CornersA[7] = XMVectorMultiplyAdd( LeftBottomA, FarA, OriginA );
  2827. // Check frustum A against each plane of frustum B.
  2828. XMVECTOR Outside = XMVectorFalseInt();
  2829. XMVECTOR InsideAll = XMVectorTrueInt();
  2830. for( size_t i = 0; i < 6; ++i )
  2831. {
  2832. // Find the min/max projection of the frustum onto the plane normal.
  2833. XMVECTOR Min, Max;
  2834. Min = Max = XMVector3Dot( AxisB[i], CornersA[0] );
  2835. for( size_t j = 1; j < CORNER_COUNT; j++ )
  2836. {
  2837. XMVECTOR Temp = XMVector3Dot( AxisB[i], CornersA[j] );
  2838. Min = XMVectorMin( Min, Temp );
  2839. Max = XMVectorMax( Max, Temp );
  2840. }
  2841. // Outside the plane?
  2842. Outside = XMVectorOrInt( Outside, XMVectorGreater( Min, PlaneDistB[i] ) );
  2843. // Fully inside the plane?
  2844. InsideAll = XMVectorAndInt( InsideAll, XMVectorLessOrEqual( Max, PlaneDistB[i] ) );
  2845. }
  2846. // If the frustum A is outside any of the planes of frustum B it is outside.
  2847. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  2848. return false;
  2849. // If frustum A is inside all planes of frustum B it is fully inside.
  2850. if ( XMVector4EqualInt( InsideAll, XMVectorTrueInt() ) )
  2851. return true;
  2852. // Build the corners of frustum B.
  2853. XMVECTOR RightTopB = XMVectorSet( RightSlope, TopSlope, 1.0f, 0.0f );
  2854. XMVECTOR RightBottomB = XMVectorSet( RightSlope, BottomSlope, 1.0f, 0.0f );
  2855. XMVECTOR LeftTopB = XMVectorSet( LeftSlope, TopSlope, 1.0f, 0.0f );
  2856. XMVECTOR LeftBottomB = XMVectorSet( LeftSlope, BottomSlope, 1.0f, 0.0f );
  2857. XMVECTOR NearB = XMVectorReplicatePtr( &Near );
  2858. XMVECTOR FarB = XMVectorReplicatePtr( &Far );
  2859. XMVECTOR CornersB[BoundingFrustum::CORNER_COUNT];
  2860. CornersB[0] = XMVectorMultiply( RightTopB, NearB );
  2861. CornersB[1] = XMVectorMultiply( RightBottomB, NearB );
  2862. CornersB[2] = XMVectorMultiply( LeftTopB, NearB );
  2863. CornersB[3] = XMVectorMultiply( LeftBottomB, NearB );
  2864. CornersB[4] = XMVectorMultiply( RightTopB, FarB );
  2865. CornersB[5] = XMVectorMultiply( RightBottomB, FarB );
  2866. CornersB[6] = XMVectorMultiply( LeftTopB, FarB );
  2867. CornersB[7] = XMVectorMultiply( LeftBottomB, FarB );
  2868. // Build the planes of frustum A (in the local space of B).
  2869. XMVECTOR AxisA[6];
  2870. XMVECTOR PlaneDistA[6];
  2871. AxisA[0] = XMVectorSet( 0.0f, 0.0f, -1.0f, 0.0f );
  2872. AxisA[1] = XMVectorSet( 0.0f, 0.0f, 1.0f, 0.0f );
  2873. AxisA[2] = XMVectorSet( 1.0f, 0.0f, -fr.RightSlope, 0.0f );
  2874. AxisA[3] = XMVectorSet( -1.0f, 0.0f, fr.LeftSlope, 0.0f );
  2875. AxisA[4] = XMVectorSet( 0.0f, 1.0f, -fr.TopSlope, 0.0f );
  2876. AxisA[5] = XMVectorSet( 0.0f, -1.0f, fr.BottomSlope, 0.0f );
  2877. AxisA[0] = XMVector3Rotate( AxisA[0], OrientationA );
  2878. AxisA[1] = XMVectorNegate( AxisA[0] );
  2879. AxisA[2] = XMVector3Rotate( AxisA[2], OrientationA );
  2880. AxisA[3] = XMVector3Rotate( AxisA[3], OrientationA );
  2881. AxisA[4] = XMVector3Rotate( AxisA[4], OrientationA );
  2882. AxisA[5] = XMVector3Rotate( AxisA[5], OrientationA );
  2883. PlaneDistA[0] = XMVector3Dot( AxisA[0], CornersA[0] ); // Re-use corner on near plane.
  2884. PlaneDistA[1] = XMVector3Dot( AxisA[1], CornersA[4] ); // Re-use corner on far plane.
  2885. PlaneDistA[2] = XMVector3Dot( AxisA[2], OriginA );
  2886. PlaneDistA[3] = XMVector3Dot( AxisA[3], OriginA );
  2887. PlaneDistA[4] = XMVector3Dot( AxisA[4], OriginA );
  2888. PlaneDistA[5] = XMVector3Dot( AxisA[5], OriginA );
  2889. // Check each axis of frustum A for a seperating plane (5).
  2890. for( size_t i = 0; i < 6; ++i )
  2891. {
  2892. // Find the minimum projection of the frustum onto the plane normal.
  2893. XMVECTOR Min;
  2894. Min = XMVector3Dot( AxisA[i], CornersB[0] );
  2895. for( size_t j = 1; j < CORNER_COUNT; j++ )
  2896. {
  2897. XMVECTOR Temp = XMVector3Dot( AxisA[i], CornersB[j] );
  2898. Min = XMVectorMin( Min, Temp );
  2899. }
  2900. // Outside the plane?
  2901. Outside = XMVectorOrInt( Outside, XMVectorGreater( Min, PlaneDistA[i] ) );
  2902. }
  2903. // If the frustum B is outside any of the planes of frustum A it is outside.
  2904. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  2905. return false;
  2906. // Check edge/edge axes (6 * 6).
  2907. XMVECTOR FrustumEdgeAxisA[6];
  2908. FrustumEdgeAxisA[0] = RightTopA;
  2909. FrustumEdgeAxisA[1] = RightBottomA;
  2910. FrustumEdgeAxisA[2] = LeftTopA;
  2911. FrustumEdgeAxisA[3] = LeftBottomA;
  2912. FrustumEdgeAxisA[4] = XMVectorSubtract( RightTopA, LeftTopA );
  2913. FrustumEdgeAxisA[5] = XMVectorSubtract( LeftBottomA, LeftTopA );
  2914. XMVECTOR FrustumEdgeAxisB[6];
  2915. FrustumEdgeAxisB[0] = RightTopB;
  2916. FrustumEdgeAxisB[1] = RightBottomB;
  2917. FrustumEdgeAxisB[2] = LeftTopB;
  2918. FrustumEdgeAxisB[3] = LeftBottomB;
  2919. FrustumEdgeAxisB[4] = XMVectorSubtract( RightTopB, LeftTopB );
  2920. FrustumEdgeAxisB[5] = XMVectorSubtract( LeftBottomB, LeftTopB );
  2921. for( size_t i = 0; i < 6; ++i )
  2922. {
  2923. for( size_t j = 0; j < 6; j++ )
  2924. {
  2925. // Compute the axis we are going to test.
  2926. XMVECTOR Axis = XMVector3Cross( FrustumEdgeAxisA[i], FrustumEdgeAxisB[j] );
  2927. // Find the min/max values of the projection of both frustums onto the axis.
  2928. XMVECTOR MinA, MaxA;
  2929. XMVECTOR MinB, MaxB;
  2930. MinA = MaxA = XMVector3Dot( Axis, CornersA[0] );
  2931. MinB = MaxB = XMVector3Dot( Axis, CornersB[0] );
  2932. for( size_t k = 1; k < CORNER_COUNT; k++ )
  2933. {
  2934. XMVECTOR TempA = XMVector3Dot( Axis, CornersA[k] );
  2935. MinA = XMVectorMin( MinA, TempA );
  2936. MaxA = XMVectorMax( MaxA, TempA );
  2937. XMVECTOR TempB = XMVector3Dot( Axis, CornersB[k] );
  2938. MinB = XMVectorMin( MinB, TempB );
  2939. MaxB = XMVectorMax( MaxB, TempB );
  2940. }
  2941. // if (MinA > MaxB || MinB > MaxA) reject
  2942. Outside = XMVectorOrInt( Outside, XMVectorGreater( MinA, MaxB ) );
  2943. Outside = XMVectorOrInt( Outside, XMVectorGreater( MinB, MaxA ) );
  2944. }
  2945. }
  2946. // If there is a seperating plane, then the frustums do not intersect.
  2947. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  2948. return false;
  2949. // If we did not find a separating plane then the frustums intersect.
  2950. return true;
  2951. }
  2952. //-----------------------------------------------------------------------------
  2953. // Triangle vs frustum test.
  2954. //-----------------------------------------------------------------------------
  2955. _Use_decl_annotations_
  2956. inline bool XM_CALLCONV BoundingFrustum::Intersects( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2 ) const
  2957. {
  2958. // Build the frustum planes (NOTE: D is negated from the usual).
  2959. XMVECTOR Planes[6];
  2960. Planes[0] = XMVectorSet( 0.0f, 0.0f, -1.0f, -Near );
  2961. Planes[1] = XMVectorSet( 0.0f, 0.0f, 1.0f, Far );
  2962. Planes[2] = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  2963. Planes[3] = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  2964. Planes[4] = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  2965. Planes[5] = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  2966. // Load origin and orientation of the frustum.
  2967. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  2968. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  2969. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  2970. // Transform triangle into the local space of frustum.
  2971. XMVECTOR TV0 = XMVector3InverseRotate( XMVectorSubtract( V0, vOrigin ), vOrientation );
  2972. XMVECTOR TV1 = XMVector3InverseRotate( XMVectorSubtract( V1, vOrigin ), vOrientation );
  2973. XMVECTOR TV2 = XMVector3InverseRotate( XMVectorSubtract( V2, vOrigin ), vOrientation );
  2974. // Test each vertex of the triangle against the frustum planes.
  2975. XMVECTOR Outside = XMVectorFalseInt();
  2976. XMVECTOR InsideAll = XMVectorTrueInt();
  2977. for( size_t i = 0; i < 6; ++i )
  2978. {
  2979. XMVECTOR Dist0 = XMVector3Dot( TV0, Planes[i] );
  2980. XMVECTOR Dist1 = XMVector3Dot( TV1, Planes[i] );
  2981. XMVECTOR Dist2 = XMVector3Dot( TV2, Planes[i] );
  2982. XMVECTOR MinDist = XMVectorMin( Dist0, Dist1 );
  2983. MinDist = XMVectorMin( MinDist, Dist2 );
  2984. XMVECTOR MaxDist = XMVectorMax( Dist0, Dist1 );
  2985. MaxDist = XMVectorMax( MaxDist, Dist2 );
  2986. XMVECTOR PlaneDist = XMVectorSplatW( Planes[i] );
  2987. // Outside the plane?
  2988. Outside = XMVectorOrInt( Outside, XMVectorGreater( MinDist, PlaneDist ) );
  2989. // Fully inside the plane?
  2990. InsideAll = XMVectorAndInt( InsideAll, XMVectorLessOrEqual( MaxDist, PlaneDist ) );
  2991. }
  2992. // If the triangle is outside any of the planes it is outside.
  2993. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  2994. return false;
  2995. // If the triangle is inside all planes it is fully inside.
  2996. if ( XMVector4EqualInt( InsideAll, XMVectorTrueInt() ) )
  2997. return true;
  2998. // Build the corners of the frustum.
  2999. XMVECTOR vRightTop = XMVectorSet( RightSlope, TopSlope, 1.0f, 0.0f );
  3000. XMVECTOR vRightBottom = XMVectorSet( RightSlope, BottomSlope, 1.0f, 0.0f );
  3001. XMVECTOR vLeftTop = XMVectorSet( LeftSlope, TopSlope, 1.0f, 0.0f );
  3002. XMVECTOR vLeftBottom = XMVectorSet( LeftSlope, BottomSlope, 1.0f, 0.0f );
  3003. XMVECTOR vNear = XMVectorReplicatePtr( &Near );
  3004. XMVECTOR vFar = XMVectorReplicatePtr( &Far );
  3005. XMVECTOR Corners[CORNER_COUNT];
  3006. Corners[0] = XMVectorMultiply( vRightTop, vNear );
  3007. Corners[1] = XMVectorMultiply( vRightBottom, vNear );
  3008. Corners[2] = XMVectorMultiply( vLeftTop, vNear );
  3009. Corners[3] = XMVectorMultiply( vLeftBottom, vNear );
  3010. Corners[4] = XMVectorMultiply( vRightTop, vFar );
  3011. Corners[5] = XMVectorMultiply( vRightBottom, vFar );
  3012. Corners[6] = XMVectorMultiply( vLeftTop, vFar );
  3013. Corners[7] = XMVectorMultiply( vLeftBottom, vFar );
  3014. // Test the plane of the triangle.
  3015. XMVECTOR Normal = XMVector3Cross( XMVectorSubtract( V1, V0 ), XMVectorSubtract( V2, V0 ) );
  3016. XMVECTOR Dist = XMVector3Dot( Normal, V0 );
  3017. XMVECTOR MinDist, MaxDist;
  3018. MinDist = MaxDist = XMVector3Dot( Corners[0], Normal );
  3019. for( size_t i = 1; i < CORNER_COUNT; ++i )
  3020. {
  3021. XMVECTOR Temp = XMVector3Dot( Corners[i], Normal );
  3022. MinDist = XMVectorMin( MinDist, Temp );
  3023. MaxDist = XMVectorMax( MaxDist, Temp );
  3024. }
  3025. Outside = XMVectorOrInt( XMVectorGreater( MinDist, Dist ), XMVectorLess( MaxDist, Dist ) );
  3026. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  3027. return false;
  3028. // Check the edge/edge axes (3*6).
  3029. XMVECTOR TriangleEdgeAxis[3];
  3030. TriangleEdgeAxis[0] = XMVectorSubtract( V1, V0 );
  3031. TriangleEdgeAxis[1] = XMVectorSubtract( V2, V1 );
  3032. TriangleEdgeAxis[2] = XMVectorSubtract( V0, V2 );
  3033. XMVECTOR FrustumEdgeAxis[6];
  3034. FrustumEdgeAxis[0] = vRightTop;
  3035. FrustumEdgeAxis[1] = vRightBottom;
  3036. FrustumEdgeAxis[2] = vLeftTop;
  3037. FrustumEdgeAxis[3] = vLeftBottom;
  3038. FrustumEdgeAxis[4] = XMVectorSubtract( vRightTop, vLeftTop );
  3039. FrustumEdgeAxis[5] = XMVectorSubtract( vLeftBottom, vLeftTop );
  3040. for( size_t i = 0; i < 3; ++i )
  3041. {
  3042. for( size_t j = 0; j < 6; j++ )
  3043. {
  3044. // Compute the axis we are going to test.
  3045. XMVECTOR Axis = XMVector3Cross( TriangleEdgeAxis[i], FrustumEdgeAxis[j] );
  3046. // Find the min/max of the projection of the triangle onto the axis.
  3047. XMVECTOR MinA, MaxA;
  3048. XMVECTOR Dist0 = XMVector3Dot( V0, Axis );
  3049. XMVECTOR Dist1 = XMVector3Dot( V1, Axis );
  3050. XMVECTOR Dist2 = XMVector3Dot( V2, Axis );
  3051. MinA = XMVectorMin( Dist0, Dist1 );
  3052. MinA = XMVectorMin( MinA, Dist2 );
  3053. MaxA = XMVectorMax( Dist0, Dist1 );
  3054. MaxA = XMVectorMax( MaxA, Dist2 );
  3055. // Find the min/max of the projection of the frustum onto the axis.
  3056. XMVECTOR MinB, MaxB;
  3057. MinB = MaxB = XMVector3Dot( Axis, Corners[0] );
  3058. for( size_t k = 1; k < CORNER_COUNT; k++ )
  3059. {
  3060. XMVECTOR Temp = XMVector3Dot( Axis, Corners[k] );
  3061. MinB = XMVectorMin( MinB, Temp );
  3062. MaxB = XMVectorMax( MaxB, Temp );
  3063. }
  3064. // if (MinA > MaxB || MinB > MaxA) reject;
  3065. Outside = XMVectorOrInt( Outside, XMVectorGreater( MinA, MaxB ) );
  3066. Outside = XMVectorOrInt( Outside, XMVectorGreater( MinB, MaxA ) );
  3067. }
  3068. }
  3069. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  3070. return false;
  3071. // If we did not find a separating plane then the triangle must intersect the frustum.
  3072. return true;
  3073. }
  3074. //-----------------------------------------------------------------------------
  3075. _Use_decl_annotations_
  3076. inline PlaneIntersectionType XM_CALLCONV BoundingFrustum::Intersects( FXMVECTOR Plane ) const
  3077. {
  3078. assert( DirectX::Internal::XMPlaneIsUnit( Plane ) );
  3079. // Load origin and orientation of the frustum.
  3080. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  3081. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  3082. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  3083. // Set w of the origin to one so we can dot4 with a plane.
  3084. vOrigin = XMVectorInsert<0, 0, 0, 0, 1>( vOrigin, XMVectorSplatOne() );
  3085. // Build the corners of the frustum (in world space).
  3086. XMVECTOR RightTop = XMVectorSet( RightSlope, TopSlope, 1.0f, 0.0f );
  3087. XMVECTOR RightBottom = XMVectorSet( RightSlope, BottomSlope, 1.0f, 0.0f );
  3088. XMVECTOR LeftTop = XMVectorSet( LeftSlope, TopSlope, 1.0f, 0.0f );
  3089. XMVECTOR LeftBottom = XMVectorSet( LeftSlope, BottomSlope, 1.0f, 0.0f );
  3090. XMVECTOR vNear = XMVectorReplicatePtr( &Near );
  3091. XMVECTOR vFar = XMVectorReplicatePtr( &Far );
  3092. RightTop = XMVector3Rotate( RightTop, vOrientation );
  3093. RightBottom = XMVector3Rotate( RightBottom, vOrientation );
  3094. LeftTop = XMVector3Rotate( LeftTop, vOrientation );
  3095. LeftBottom = XMVector3Rotate( LeftBottom, vOrientation );
  3096. XMVECTOR Corners0 = XMVectorMultiplyAdd( RightTop, vNear, vOrigin );
  3097. XMVECTOR Corners1 = XMVectorMultiplyAdd( RightBottom, vNear, vOrigin );
  3098. XMVECTOR Corners2 = XMVectorMultiplyAdd( LeftTop, vNear, vOrigin );
  3099. XMVECTOR Corners3 = XMVectorMultiplyAdd( LeftBottom, vNear, vOrigin );
  3100. XMVECTOR Corners4 = XMVectorMultiplyAdd( RightTop, vFar, vOrigin );
  3101. XMVECTOR Corners5 = XMVectorMultiplyAdd( RightBottom, vFar, vOrigin );
  3102. XMVECTOR Corners6 = XMVectorMultiplyAdd( LeftTop, vFar, vOrigin );
  3103. XMVECTOR Corners7 = XMVectorMultiplyAdd( LeftBottom, vFar, vOrigin );
  3104. XMVECTOR Outside, Inside;
  3105. DirectX::Internal::FastIntersectFrustumPlane( Corners0, Corners1, Corners2, Corners3,
  3106. Corners4, Corners5, Corners6, Corners7,
  3107. Plane, Outside, Inside );
  3108. // If the frustum is outside any plane it is outside.
  3109. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  3110. return FRONT;
  3111. // If the frustum is inside all planes it is inside.
  3112. if ( XMVector4EqualInt( Inside, XMVectorTrueInt() ) )
  3113. return BACK;
  3114. // The frustum is not inside all planes or outside a plane it intersects.
  3115. return INTERSECTING;
  3116. }
  3117. //-----------------------------------------------------------------------------
  3118. // Ray vs. frustum test
  3119. //-----------------------------------------------------------------------------
  3120. _Use_decl_annotations_
  3121. inline bool XM_CALLCONV BoundingFrustum::Intersects( FXMVECTOR rayOrigin, FXMVECTOR Direction, float& Dist ) const
  3122. {
  3123. // If ray starts inside the frustum, return a distance of 0 for the hit
  3124. if ( Contains(rayOrigin) == CONTAINS )
  3125. {
  3126. Dist = 0.0f;
  3127. return true;
  3128. }
  3129. // Build the frustum planes.
  3130. XMVECTOR Planes[6];
  3131. Planes[0] = XMVectorSet( 0.0f, 0.0f, -1.0f, Near );
  3132. Planes[1] = XMVectorSet( 0.0f, 0.0f, 1.0f, -Far );
  3133. Planes[2] = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  3134. Planes[3] = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  3135. Planes[4] = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  3136. Planes[5] = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  3137. // Load origin and orientation of the frustum.
  3138. XMVECTOR frOrigin = XMLoadFloat3( &Origin );
  3139. XMVECTOR frOrientation = XMLoadFloat4( &Orientation );
  3140. // This algorithm based on "Fast Ray-Convex Polyhedron Intersectin," in James Arvo, ed., Graphics Gems II pp. 247-250
  3141. float tnear = -FLT_MAX;
  3142. float tfar = FLT_MAX;
  3143. for( size_t i=0; i < 6; ++i )
  3144. {
  3145. XMVECTOR Plane = DirectX::Internal::XMPlaneTransform( Planes[i], frOrientation, frOrigin );
  3146. Plane = XMPlaneNormalize( Plane );
  3147. XMVECTOR AxisDotOrigin = XMPlaneDotCoord( Plane, rayOrigin );
  3148. XMVECTOR AxisDotDirection = XMVector3Dot( Plane, Direction );
  3149. if ( XMVector3LessOrEqual( XMVectorAbs( AxisDotDirection ), g_RayEpsilon ) )
  3150. {
  3151. // Ray is parallel to plane - check if ray origin is inside plane's
  3152. if ( XMVector3Greater( AxisDotOrigin, g_XMZero ) )
  3153. {
  3154. // Ray origin is outside half-space.
  3155. Dist = 0.f;
  3156. return false;
  3157. }
  3158. }
  3159. else
  3160. {
  3161. // Ray not parallel - get distance to plane.
  3162. float vd = XMVectorGetX( AxisDotDirection );
  3163. float vn = XMVectorGetX( AxisDotOrigin );
  3164. float t = -vn / vd;
  3165. if (vd < 0.0f)
  3166. {
  3167. // Front face - T is a near point.
  3168. if (t > tfar)
  3169. {
  3170. Dist = 0.f;
  3171. return false;
  3172. }
  3173. if (t > tnear)
  3174. {
  3175. // Hit near face.
  3176. tnear = t;
  3177. }
  3178. }
  3179. else
  3180. {
  3181. // back face - T is far point.
  3182. if (t < tnear)
  3183. {
  3184. Dist = 0.f;
  3185. return false;
  3186. }
  3187. if (t < tfar)
  3188. {
  3189. // Hit far face.
  3190. tfar = t;
  3191. }
  3192. }
  3193. }
  3194. }
  3195. // Survived all tests.
  3196. // Note: if ray originates on polyhedron, may want to change 0.0f to some
  3197. // epsilon to avoid intersecting the originating face.
  3198. float distance = ( tnear >= 0.0f ) ? tnear : tfar;
  3199. if (distance >= 0.0f)
  3200. {
  3201. Dist = distance;
  3202. return true;
  3203. }
  3204. Dist = 0.f;
  3205. return false;
  3206. }
  3207. //-----------------------------------------------------------------------------
  3208. // Test a frustum vs 6 planes (typically forming another frustum).
  3209. //-----------------------------------------------------------------------------
  3210. _Use_decl_annotations_
  3211. inline ContainmentType XM_CALLCONV BoundingFrustum::ContainedBy( FXMVECTOR Plane0, FXMVECTOR Plane1, FXMVECTOR Plane2,
  3212. GXMVECTOR Plane3, HXMVECTOR Plane4, HXMVECTOR Plane5 ) const
  3213. {
  3214. // Load origin and orientation of the frustum.
  3215. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  3216. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  3217. assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );
  3218. // Set w of the origin to one so we can dot4 with a plane.
  3219. vOrigin = XMVectorInsert<0, 0, 0, 0, 1>( vOrigin, XMVectorSplatOne() );
  3220. // Build the corners of the frustum (in world space).
  3221. XMVECTOR RightTop = XMVectorSet( RightSlope, TopSlope, 1.0f, 0.0f );
  3222. XMVECTOR RightBottom = XMVectorSet( RightSlope, BottomSlope, 1.0f, 0.0f );
  3223. XMVECTOR LeftTop = XMVectorSet( LeftSlope, TopSlope, 1.0f, 0.0f );
  3224. XMVECTOR LeftBottom = XMVectorSet( LeftSlope, BottomSlope, 1.0f, 0.0f );
  3225. XMVECTOR vNear = XMVectorReplicatePtr( &Near );
  3226. XMVECTOR vFar = XMVectorReplicatePtr( &Far );
  3227. RightTop = XMVector3Rotate( RightTop, vOrientation );
  3228. RightBottom = XMVector3Rotate( RightBottom, vOrientation );
  3229. LeftTop = XMVector3Rotate( LeftTop, vOrientation );
  3230. LeftBottom = XMVector3Rotate( LeftBottom, vOrientation );
  3231. XMVECTOR Corners0 = XMVectorMultiplyAdd( RightTop, vNear, vOrigin );
  3232. XMVECTOR Corners1 = XMVectorMultiplyAdd( RightBottom, vNear, vOrigin );
  3233. XMVECTOR Corners2 = XMVectorMultiplyAdd( LeftTop, vNear, vOrigin );
  3234. XMVECTOR Corners3 = XMVectorMultiplyAdd( LeftBottom, vNear, vOrigin );
  3235. XMVECTOR Corners4 = XMVectorMultiplyAdd( RightTop, vFar, vOrigin );
  3236. XMVECTOR Corners5 = XMVectorMultiplyAdd( RightBottom, vFar, vOrigin );
  3237. XMVECTOR Corners6 = XMVectorMultiplyAdd( LeftTop, vFar, vOrigin );
  3238. XMVECTOR Corners7 = XMVectorMultiplyAdd( LeftBottom, vFar, vOrigin );
  3239. XMVECTOR Outside, Inside;
  3240. // Test against each plane.
  3241. DirectX::Internal::FastIntersectFrustumPlane( Corners0, Corners1, Corners2, Corners3,
  3242. Corners4, Corners5, Corners6, Corners7,
  3243. Plane0, Outside, Inside );
  3244. XMVECTOR AnyOutside = Outside;
  3245. XMVECTOR AllInside = Inside;
  3246. DirectX::Internal::FastIntersectFrustumPlane( Corners0, Corners1, Corners2, Corners3,
  3247. Corners4, Corners5, Corners6, Corners7,
  3248. Plane1, Outside, Inside );
  3249. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  3250. AllInside = XMVectorAndInt( AllInside, Inside );
  3251. DirectX::Internal::FastIntersectFrustumPlane( Corners0, Corners1, Corners2, Corners3,
  3252. Corners4, Corners5, Corners6, Corners7,
  3253. Plane2, Outside, Inside );
  3254. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  3255. AllInside = XMVectorAndInt( AllInside, Inside );
  3256. DirectX::Internal::FastIntersectFrustumPlane( Corners0, Corners1, Corners2, Corners3,
  3257. Corners4, Corners5, Corners6, Corners7,
  3258. Plane3, Outside, Inside );
  3259. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  3260. AllInside = XMVectorAndInt( AllInside, Inside );
  3261. DirectX::Internal::FastIntersectFrustumPlane( Corners0, Corners1, Corners2, Corners3,
  3262. Corners4, Corners5, Corners6, Corners7,
  3263. Plane4, Outside, Inside );
  3264. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  3265. AllInside = XMVectorAndInt( AllInside, Inside );
  3266. DirectX::Internal::FastIntersectFrustumPlane( Corners0, Corners1, Corners2, Corners3,
  3267. Corners4, Corners5, Corners6, Corners7,
  3268. Plane5, Outside, Inside );
  3269. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  3270. AllInside = XMVectorAndInt( AllInside, Inside );
  3271. // If the frustum is outside any plane it is outside.
  3272. if ( XMVector4EqualInt( AnyOutside, XMVectorTrueInt() ) )
  3273. return DISJOINT;
  3274. // If the frustum is inside all planes it is inside.
  3275. if ( XMVector4EqualInt( AllInside, XMVectorTrueInt() ) )
  3276. return CONTAINS;
  3277. // The frustum is not inside all planes or outside a plane, it may intersect.
  3278. return INTERSECTS;
  3279. }
  3280. //-----------------------------------------------------------------------------
  3281. // Build the 6 frustum planes from a frustum.
  3282. //
  3283. // The intended use for these routines is for fast culling to a view frustum.
  3284. // When the volume being tested against a view frustum is small relative to the
  3285. // view frustum it is usually either inside all six planes of the frustum
  3286. // (CONTAINS) or outside one of the planes of the frustum (DISJOINT). If neither
  3287. // of these cases is true then it may or may not be intersecting the frustum
  3288. // (INTERSECTS)
  3289. //-----------------------------------------------------------------------------
  3290. _Use_decl_annotations_
  3291. inline void BoundingFrustum::GetPlanes( XMVECTOR* NearPlane, XMVECTOR* FarPlane, XMVECTOR* RightPlane,
  3292. XMVECTOR* LeftPlane, XMVECTOR* TopPlane, XMVECTOR* BottomPlane ) const
  3293. {
  3294. // Load origin and orientation of the frustum.
  3295. XMVECTOR vOrigin = XMLoadFloat3( &Origin );
  3296. XMVECTOR vOrientation = XMLoadFloat4( &Orientation );
  3297. if (NearPlane)
  3298. {
  3299. XMVECTOR vNearPlane = XMVectorSet( 0.0f, 0.0f, -1.0f, Near );
  3300. vNearPlane = DirectX::Internal::XMPlaneTransform( vNearPlane, vOrientation, vOrigin );
  3301. *NearPlane = XMPlaneNormalize( vNearPlane );
  3302. }
  3303. if (FarPlane)
  3304. {
  3305. XMVECTOR vFarPlane = XMVectorSet( 0.0f, 0.0f, 1.0f, -Far );
  3306. vFarPlane = DirectX::Internal::XMPlaneTransform( vFarPlane, vOrientation, vOrigin );
  3307. *FarPlane = XMPlaneNormalize( vFarPlane );
  3308. }
  3309. if (RightPlane)
  3310. {
  3311. XMVECTOR vRightPlane = XMVectorSet( 1.0f, 0.0f, -RightSlope, 0.0f );
  3312. vRightPlane = DirectX::Internal::XMPlaneTransform( vRightPlane, vOrientation, vOrigin );
  3313. *RightPlane = XMPlaneNormalize( vRightPlane );
  3314. }
  3315. if (LeftPlane)
  3316. {
  3317. XMVECTOR vLeftPlane = XMVectorSet( -1.0f, 0.0f, LeftSlope, 0.0f );
  3318. vLeftPlane = DirectX::Internal::XMPlaneTransform( vLeftPlane, vOrientation, vOrigin );
  3319. *LeftPlane = XMPlaneNormalize( vLeftPlane );
  3320. }
  3321. if (TopPlane)
  3322. {
  3323. XMVECTOR vTopPlane = XMVectorSet( 0.0f, 1.0f, -TopSlope, 0.0f );
  3324. vTopPlane = DirectX::Internal::XMPlaneTransform( vTopPlane, vOrientation, vOrigin );
  3325. *TopPlane = XMPlaneNormalize( vTopPlane );
  3326. }
  3327. if (BottomPlane)
  3328. {
  3329. XMVECTOR vBottomPlane = XMVectorSet( 0.0f, -1.0f, BottomSlope, 0.0f );
  3330. vBottomPlane = DirectX::Internal::XMPlaneTransform( vBottomPlane, vOrientation, vOrigin );
  3331. *BottomPlane = XMPlaneNormalize( vBottomPlane );
  3332. }
  3333. }
  3334. //-----------------------------------------------------------------------------
  3335. // Build a frustum from a persepective projection matrix. The matrix may only
  3336. // contain a projection; any rotation, translation or scale will cause the
  3337. // constructed frustum to be incorrect.
  3338. //-----------------------------------------------------------------------------
  3339. _Use_decl_annotations_
  3340. inline void XM_CALLCONV BoundingFrustum::CreateFromMatrix( BoundingFrustum& Out, FXMMATRIX Projection )
  3341. {
  3342. // Corners of the projection frustum in homogenous space.
  3343. static XMVECTORF32 HomogenousPoints[6] =
  3344. {
  3345. { { { 1.0f, 0.0f, 1.0f, 1.0f } } }, // right (at far plane)
  3346. { { { -1.0f, 0.0f, 1.0f, 1.0f } } }, // left
  3347. { { { 0.0f, 1.0f, 1.0f, 1.0f } } }, // top
  3348. { { { 0.0f, -1.0f, 1.0f, 1.0f } } }, // bottom
  3349. { { { 0.0f, 0.0f, 0.0f, 1.0f } } }, // near
  3350. { { { 0.0f, 0.0f, 1.0f, 1.0f } } } // far
  3351. };
  3352. XMVECTOR Determinant;
  3353. XMMATRIX matInverse = XMMatrixInverse( &Determinant, Projection );
  3354. // Compute the frustum corners in world space.
  3355. XMVECTOR Points[6];
  3356. for( size_t i = 0; i < 6; ++i )
  3357. {
  3358. // Transform point.
  3359. Points[i] = XMVector4Transform( HomogenousPoints[i], matInverse );
  3360. }
  3361. Out.Origin = XMFLOAT3( 0.0f, 0.0f, 0.0f );
  3362. Out.Orientation = XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f );
  3363. // Compute the slopes.
  3364. Points[0] = XMVectorMultiply( Points[0], XMVectorReciprocal( XMVectorSplatZ( Points[0] ) ) );
  3365. Points[1] = XMVectorMultiply( Points[1], XMVectorReciprocal( XMVectorSplatZ( Points[1] ) ) );
  3366. Points[2] = XMVectorMultiply( Points[2], XMVectorReciprocal( XMVectorSplatZ( Points[2] ) ) );
  3367. Points[3] = XMVectorMultiply( Points[3], XMVectorReciprocal( XMVectorSplatZ( Points[3] ) ) );
  3368. Out.RightSlope = XMVectorGetX( Points[0] );
  3369. Out.LeftSlope = XMVectorGetX( Points[1] );
  3370. Out.TopSlope = XMVectorGetY( Points[2] );
  3371. Out.BottomSlope = XMVectorGetY( Points[3] );
  3372. // Compute near and far.
  3373. Points[4] = XMVectorMultiply( Points[4], XMVectorReciprocal( XMVectorSplatW( Points[4] ) ) );
  3374. Points[5] = XMVectorMultiply( Points[5], XMVectorReciprocal( XMVectorSplatW( Points[5] ) ) );
  3375. Out.Near = XMVectorGetZ( Points[4] );
  3376. Out.Far = XMVectorGetZ( Points[5] );
  3377. }
  3378. /****************************************************************************
  3379. *
  3380. * TriangleTests
  3381. *
  3382. ****************************************************************************/
  3383. namespace TriangleTests
  3384. {
  3385. //-----------------------------------------------------------------------------
  3386. // Compute the intersection of a ray (Origin, Direction) with a triangle
  3387. // (V0, V1, V2). Return true if there is an intersection and also set *pDist
  3388. // to the distance along the ray to the intersection.
  3389. //
  3390. // The algorithm is based on Moller, Tomas and Trumbore, "Fast, Minimum Storage
  3391. // Ray-Triangle Intersection", Journal of Graphics Tools, vol. 2, no. 1,
  3392. // pp 21-28, 1997.
  3393. //-----------------------------------------------------------------------------
  3394. _Use_decl_annotations_
  3395. inline bool XM_CALLCONV Intersects( FXMVECTOR Origin, FXMVECTOR Direction, FXMVECTOR V0, GXMVECTOR V1, HXMVECTOR V2, float& Dist )
  3396. {
  3397. assert( DirectX::Internal::XMVector3IsUnit( Direction ) );
  3398. XMVECTOR Zero = XMVectorZero();
  3399. XMVECTOR e1 = XMVectorSubtract( V1, V0 );
  3400. XMVECTOR e2 = XMVectorSubtract( V2, V0 );
  3401. // p = Direction ^ e2;
  3402. XMVECTOR p = XMVector3Cross( Direction, e2 );
  3403. // det = e1 * p;
  3404. XMVECTOR det = XMVector3Dot( e1, p );
  3405. XMVECTOR u, v, t;
  3406. if( XMVector3GreaterOrEqual( det, g_RayEpsilon ) )
  3407. {
  3408. // Determinate is positive (front side of the triangle).
  3409. XMVECTOR s = XMVectorSubtract( Origin, V0 );
  3410. // u = s * p;
  3411. u = XMVector3Dot( s, p );
  3412. XMVECTOR NoIntersection = XMVectorLess( u, Zero );
  3413. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( u, det ) );
  3414. // q = s ^ e1;
  3415. XMVECTOR q = XMVector3Cross( s, e1 );
  3416. // v = Direction * q;
  3417. v = XMVector3Dot( Direction, q );
  3418. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( v, Zero ) );
  3419. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( XMVectorAdd( u, v ), det ) );
  3420. // t = e2 * q;
  3421. t = XMVector3Dot( e2, q );
  3422. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( t, Zero ) );
  3423. if( XMVector4EqualInt( NoIntersection, XMVectorTrueInt() ) )
  3424. {
  3425. Dist = 0.f;
  3426. return false;
  3427. }
  3428. }
  3429. else if( XMVector3LessOrEqual( det, g_RayNegEpsilon ) )
  3430. {
  3431. // Determinate is negative (back side of the triangle).
  3432. XMVECTOR s = XMVectorSubtract( Origin, V0 );
  3433. // u = s * p;
  3434. u = XMVector3Dot( s, p );
  3435. XMVECTOR NoIntersection = XMVectorGreater( u, Zero );
  3436. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( u, det ) );
  3437. // q = s ^ e1;
  3438. XMVECTOR q = XMVector3Cross( s, e1 );
  3439. // v = Direction * q;
  3440. v = XMVector3Dot( Direction, q );
  3441. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( v, Zero ) );
  3442. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorLess( XMVectorAdd( u, v ), det ) );
  3443. // t = e2 * q;
  3444. t = XMVector3Dot( e2, q );
  3445. NoIntersection = XMVectorOrInt( NoIntersection, XMVectorGreater( t, Zero ) );
  3446. if ( XMVector4EqualInt( NoIntersection, XMVectorTrueInt() ) )
  3447. {
  3448. Dist = 0.f;
  3449. return false;
  3450. }
  3451. }
  3452. else
  3453. {
  3454. // Parallel ray.
  3455. Dist = 0.f;
  3456. return false;
  3457. }
  3458. t = XMVectorDivide ( t, det );
  3459. // (u / det) and (v / dev) are the barycentric cooridinates of the intersection.
  3460. // Store the x-component to *pDist
  3461. XMStoreFloat( &Dist, t );
  3462. return true;
  3463. }
  3464. //-----------------------------------------------------------------------------
  3465. // Test if two triangles intersect.
  3466. //
  3467. // The final test of algorithm is based on Shen, Heng, and Tang, "A Fast
  3468. // Triangle-Triangle Overlap Test Using Signed Distances", Journal of Graphics
  3469. // Tools, vol. 8, no. 1, pp 17-23, 2003 and Guigue and Devillers, "Fast and
  3470. // Robust Triangle-Triangle Overlap Test Using Orientation Predicates", Journal
  3471. // of Graphics Tools, vol. 8, no. 1, pp 25-32, 2003.
  3472. //
  3473. // The final test could be considered an edge-edge separating plane test with
  3474. // the 9 possible cases narrowed down to the only two pairs of edges that can
  3475. // actaully result in a seperation.
  3476. //-----------------------------------------------------------------------------
  3477. _Use_decl_annotations_
  3478. inline bool XM_CALLCONV Intersects( FXMVECTOR A0, FXMVECTOR A1, FXMVECTOR A2, GXMVECTOR B0, HXMVECTOR B1, HXMVECTOR B2 )
  3479. {
  3480. static const XMVECTORU32 SelectY = { { { XM_SELECT_0, XM_SELECT_1, XM_SELECT_0, XM_SELECT_0 } } };
  3481. static const XMVECTORU32 SelectZ = { { { XM_SELECT_0, XM_SELECT_0, XM_SELECT_1, XM_SELECT_0 } } };
  3482. static const XMVECTORU32 Select0111 = { { { XM_SELECT_0, XM_SELECT_1, XM_SELECT_1, XM_SELECT_1 } } };
  3483. static const XMVECTORU32 Select1011 = { { { XM_SELECT_1, XM_SELECT_0, XM_SELECT_1, XM_SELECT_1 } } };
  3484. static const XMVECTORU32 Select1101 = { { { XM_SELECT_1, XM_SELECT_1, XM_SELECT_0, XM_SELECT_1 } } };
  3485. XMVECTOR Zero = XMVectorZero();
  3486. // Compute the normal of triangle A.
  3487. XMVECTOR N1 = XMVector3Cross( XMVectorSubtract( A1, A0 ), XMVectorSubtract( A2, A0 ) );
  3488. // Assert that the triangle is not degenerate.
  3489. assert( !XMVector3Equal( N1, Zero ) );
  3490. // Test points of B against the plane of A.
  3491. XMVECTOR BDist = XMVector3Dot( N1, XMVectorSubtract( B0, A0 ) );
  3492. BDist = XMVectorSelect( BDist, XMVector3Dot( N1, XMVectorSubtract( B1, A0 ) ), SelectY );
  3493. BDist = XMVectorSelect( BDist, XMVector3Dot( N1, XMVectorSubtract( B2, A0 ) ), SelectZ );
  3494. // Ensure robustness with co-planar triangles by zeroing small distances.
  3495. uint32_t BDistIsZeroCR;
  3496. XMVECTOR BDistIsZero = XMVectorGreaterR( &BDistIsZeroCR, g_RayEpsilon, XMVectorAbs( BDist ) );
  3497. BDist = XMVectorSelect( BDist, Zero, BDistIsZero );
  3498. uint32_t BDistIsLessCR;
  3499. XMVECTOR BDistIsLess = XMVectorGreaterR( &BDistIsLessCR, Zero, BDist );
  3500. uint32_t BDistIsGreaterCR;
  3501. XMVECTOR BDistIsGreater = XMVectorGreaterR( &BDistIsGreaterCR, BDist, Zero );
  3502. // If all the points are on the same side we don't intersect.
  3503. if( XMComparisonAllTrue( BDistIsLessCR ) || XMComparisonAllTrue( BDistIsGreaterCR ) )
  3504. return false;
  3505. // Compute the normal of triangle B.
  3506. XMVECTOR N2 = XMVector3Cross( XMVectorSubtract( B1, B0 ), XMVectorSubtract( B2, B0 ) );
  3507. // Assert that the triangle is not degenerate.
  3508. assert( !XMVector3Equal( N2, Zero ) );
  3509. // Test points of A against the plane of B.
  3510. XMVECTOR ADist = XMVector3Dot( N2, XMVectorSubtract( A0, B0 ) );
  3511. ADist = XMVectorSelect( ADist, XMVector3Dot( N2, XMVectorSubtract( A1, B0 ) ), SelectY );
  3512. ADist = XMVectorSelect( ADist, XMVector3Dot( N2, XMVectorSubtract( A2, B0 ) ), SelectZ );
  3513. // Ensure robustness with co-planar triangles by zeroing small distances.
  3514. uint32_t ADistIsZeroCR;
  3515. XMVECTOR ADistIsZero = XMVectorGreaterR( &ADistIsZeroCR, g_RayEpsilon, XMVectorAbs( BDist ) );
  3516. ADist = XMVectorSelect( ADist, Zero, ADistIsZero );
  3517. uint32_t ADistIsLessCR;
  3518. XMVECTOR ADistIsLess = XMVectorGreaterR( &ADistIsLessCR, Zero, ADist );
  3519. uint32_t ADistIsGreaterCR;
  3520. XMVECTOR ADistIsGreater = XMVectorGreaterR( &ADistIsGreaterCR, ADist, Zero );
  3521. // If all the points are on the same side we don't intersect.
  3522. if( XMComparisonAllTrue( ADistIsLessCR ) || XMComparisonAllTrue( ADistIsGreaterCR ) )
  3523. return false;
  3524. // Special case for co-planar triangles.
  3525. if( XMComparisonAllTrue( ADistIsZeroCR ) || XMComparisonAllTrue( BDistIsZeroCR ) )
  3526. {
  3527. XMVECTOR Axis, Dist, MinDist;
  3528. // Compute an axis perpindicular to the edge (points out).
  3529. Axis = XMVector3Cross( N1, XMVectorSubtract( A1, A0 ) );
  3530. Dist = XMVector3Dot( Axis, A0 );
  3531. // Test points of B against the axis.
  3532. MinDist = XMVector3Dot( B0, Axis );
  3533. MinDist = XMVectorMin( MinDist, XMVector3Dot( B1, Axis ) );
  3534. MinDist = XMVectorMin( MinDist, XMVector3Dot( B2, Axis ) );
  3535. if( XMVector4GreaterOrEqual( MinDist, Dist ) )
  3536. return false;
  3537. // Edge (A1, A2)
  3538. Axis = XMVector3Cross( N1, XMVectorSubtract( A2, A1 ) );
  3539. Dist = XMVector3Dot( Axis, A1 );
  3540. MinDist = XMVector3Dot( B0, Axis );
  3541. MinDist = XMVectorMin( MinDist, XMVector3Dot( B1, Axis ) );
  3542. MinDist = XMVectorMin( MinDist, XMVector3Dot( B2, Axis ) );
  3543. if( XMVector4GreaterOrEqual( MinDist, Dist ) )
  3544. return false;
  3545. // Edge (A2, A0)
  3546. Axis = XMVector3Cross( N1, XMVectorSubtract( A0, A2 ) );
  3547. Dist = XMVector3Dot( Axis, A2 );
  3548. MinDist = XMVector3Dot( B0, Axis );
  3549. MinDist = XMVectorMin( MinDist, XMVector3Dot( B1, Axis ) );
  3550. MinDist = XMVectorMin( MinDist, XMVector3Dot( B2, Axis ) );
  3551. if( XMVector4GreaterOrEqual( MinDist, Dist ) )
  3552. return false;
  3553. // Edge (B0, B1)
  3554. Axis = XMVector3Cross( N2, XMVectorSubtract( B1, B0 ) );
  3555. Dist = XMVector3Dot( Axis, B0 );
  3556. MinDist = XMVector3Dot( A0, Axis );
  3557. MinDist = XMVectorMin( MinDist, XMVector3Dot( A1, Axis ) );
  3558. MinDist = XMVectorMin( MinDist, XMVector3Dot( A2, Axis ) );
  3559. if( XMVector4GreaterOrEqual( MinDist, Dist ) )
  3560. return false;
  3561. // Edge (B1, B2)
  3562. Axis = XMVector3Cross( N2, XMVectorSubtract( B2, B1 ) );
  3563. Dist = XMVector3Dot( Axis, B1 );
  3564. MinDist = XMVector3Dot( A0, Axis );
  3565. MinDist = XMVectorMin( MinDist, XMVector3Dot( A1, Axis ) );
  3566. MinDist = XMVectorMin( MinDist, XMVector3Dot( A2, Axis ) );
  3567. if( XMVector4GreaterOrEqual( MinDist, Dist ) )
  3568. return false;
  3569. // Edge (B2,B0)
  3570. Axis = XMVector3Cross( N2, XMVectorSubtract( B0, B2 ) );
  3571. Dist = XMVector3Dot( Axis, B2 );
  3572. MinDist = XMVector3Dot( A0, Axis );
  3573. MinDist = XMVectorMin( MinDist, XMVector3Dot( A1, Axis ) );
  3574. MinDist = XMVectorMin( MinDist, XMVector3Dot( A2, Axis ) );
  3575. if( XMVector4GreaterOrEqual( MinDist, Dist ) )
  3576. return false;
  3577. return true;
  3578. }
  3579. //
  3580. // Find the single vertex of A and B (ie the vertex on the opposite side
  3581. // of the plane from the other two) and reorder the edges so we can compute
  3582. // the signed edge/edge distances.
  3583. //
  3584. // if ( (V0 >= 0 && V1 < 0 && V2 < 0) ||
  3585. // (V0 > 0 && V1 <= 0 && V2 <= 0) ||
  3586. // (V0 <= 0 && V1 > 0 && V2 > 0) ||
  3587. // (V0 < 0 && V1 >= 0 && V2 >= 0) ) then V0 is singular;
  3588. //
  3589. // If our singular vertex is not on the positive side of the plane we reverse
  3590. // the triangle winding so that the overlap comparisons will compare the
  3591. // correct edges with the correct signs.
  3592. //
  3593. XMVECTOR ADistIsLessEqual = XMVectorOrInt( ADistIsLess, ADistIsZero );
  3594. XMVECTOR ADistIsGreaterEqual = XMVectorOrInt( ADistIsGreater, ADistIsZero );
  3595. XMVECTOR AA0, AA1, AA2;
  3596. bool bPositiveA;
  3597. if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsGreaterEqual, ADistIsLess, Select0111 ) ) ||
  3598. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsGreater, ADistIsLessEqual, Select0111 ) ) )
  3599. {
  3600. // A0 is singular, crossing from positive to negative.
  3601. AA0 = A0; AA1 = A1; AA2 = A2;
  3602. bPositiveA = true;
  3603. }
  3604. else if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsLessEqual, ADistIsGreater, Select0111 ) ) ||
  3605. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsLess, ADistIsGreaterEqual, Select0111 ) ) )
  3606. {
  3607. // A0 is singular, crossing from negative to positive.
  3608. AA0 = A0; AA1 = A2; AA2 = A1;
  3609. bPositiveA = false;
  3610. }
  3611. else if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsGreaterEqual, ADistIsLess, Select1011 ) ) ||
  3612. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsGreater, ADistIsLessEqual, Select1011 ) ) )
  3613. {
  3614. // A1 is singular, crossing from positive to negative.
  3615. AA0 = A1; AA1 = A2; AA2 = A0;
  3616. bPositiveA = true;
  3617. }
  3618. else if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsLessEqual, ADistIsGreater, Select1011 ) ) ||
  3619. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsLess, ADistIsGreaterEqual, Select1011 ) ) )
  3620. {
  3621. // A1 is singular, crossing from negative to positive.
  3622. AA0 = A1; AA1 = A0; AA2 = A2;
  3623. bPositiveA = false;
  3624. }
  3625. else if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsGreaterEqual, ADistIsLess, Select1101 ) ) ||
  3626. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsGreater, ADistIsLessEqual, Select1101 ) ) )
  3627. {
  3628. // A2 is singular, crossing from positive to negative.
  3629. AA0 = A2; AA1 = A0; AA2 = A1;
  3630. bPositiveA = true;
  3631. }
  3632. else if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsLessEqual, ADistIsGreater, Select1101 ) ) ||
  3633. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( ADistIsLess, ADistIsGreaterEqual, Select1101 ) ) )
  3634. {
  3635. // A2 is singular, crossing from negative to positive.
  3636. AA0 = A2; AA1 = A1; AA2 = A0;
  3637. bPositiveA = false;
  3638. }
  3639. else
  3640. {
  3641. assert( false );
  3642. return false;
  3643. }
  3644. XMVECTOR BDistIsLessEqual = XMVectorOrInt( BDistIsLess, BDistIsZero );
  3645. XMVECTOR BDistIsGreaterEqual = XMVectorOrInt( BDistIsGreater, BDistIsZero );
  3646. XMVECTOR BB0, BB1, BB2;
  3647. bool bPositiveB;
  3648. if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsGreaterEqual, BDistIsLess, Select0111 ) ) ||
  3649. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsGreater, BDistIsLessEqual, Select0111 ) ) )
  3650. {
  3651. // B0 is singular, crossing from positive to negative.
  3652. BB0 = B0; BB1 = B1; BB2 = B2;
  3653. bPositiveB = true;
  3654. }
  3655. else if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsLessEqual, BDistIsGreater, Select0111 ) ) ||
  3656. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsLess, BDistIsGreaterEqual, Select0111 ) ) )
  3657. {
  3658. // B0 is singular, crossing from negative to positive.
  3659. BB0 = B0; BB1 = B2; BB2 = B1;
  3660. bPositiveB = false;
  3661. }
  3662. else if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsGreaterEqual, BDistIsLess, Select1011 ) ) ||
  3663. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsGreater, BDistIsLessEqual, Select1011 ) ) )
  3664. {
  3665. // B1 is singular, crossing from positive to negative.
  3666. BB0 = B1; BB1 = B2; BB2 = B0;
  3667. bPositiveB = true;
  3668. }
  3669. else if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsLessEqual, BDistIsGreater, Select1011 ) ) ||
  3670. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsLess, BDistIsGreaterEqual, Select1011 ) ) )
  3671. {
  3672. // B1 is singular, crossing from negative to positive.
  3673. BB0 = B1; BB1 = B0; BB2 = B2;
  3674. bPositiveB = false;
  3675. }
  3676. else if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsGreaterEqual, BDistIsLess, Select1101 ) ) ||
  3677. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsGreater, BDistIsLessEqual, Select1101 ) ) )
  3678. {
  3679. // B2 is singular, crossing from positive to negative.
  3680. BB0 = B2; BB1 = B0; BB2 = B1;
  3681. bPositiveB = true;
  3682. }
  3683. else if( DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsLessEqual, BDistIsGreater, Select1101 ) ) ||
  3684. DirectX::Internal::XMVector3AllTrue( XMVectorSelect( BDistIsLess, BDistIsGreaterEqual, Select1101 ) ) )
  3685. {
  3686. // B2 is singular, crossing from negative to positive.
  3687. BB0 = B2; BB1 = B1; BB2 = B0;
  3688. bPositiveB = false;
  3689. }
  3690. else
  3691. {
  3692. assert( false );
  3693. return false;
  3694. }
  3695. XMVECTOR Delta0, Delta1;
  3696. // Reverse the direction of the test depending on whether the singular vertices are
  3697. // the same sign or different signs.
  3698. if( bPositiveA ^ bPositiveB )
  3699. {
  3700. Delta0 = XMVectorSubtract( BB0, AA0 );
  3701. Delta1 = XMVectorSubtract( AA0, BB0 );
  3702. }
  3703. else
  3704. {
  3705. Delta0 = XMVectorSubtract( AA0, BB0 );
  3706. Delta1 = XMVectorSubtract( BB0, AA0 );
  3707. }
  3708. // Check if the triangles overlap on the line of intersection between the
  3709. // planes of the two triangles by finding the signed line distances.
  3710. XMVECTOR Dist0 = XMVector3Dot( Delta0, XMVector3Cross( XMVectorSubtract( BB2, BB0 ), XMVectorSubtract( AA2, AA0 ) ) );
  3711. if( XMVector4Greater( Dist0, Zero ) )
  3712. return false;
  3713. XMVECTOR Dist1 = XMVector3Dot( Delta1, XMVector3Cross( XMVectorSubtract( BB1, BB0 ), XMVectorSubtract( AA1, AA0 ) ) );
  3714. if( XMVector4Greater( Dist1, Zero ) )
  3715. return false;
  3716. return true;
  3717. }
  3718. //-----------------------------------------------------------------------------
  3719. // Ray-triangle test
  3720. //-----------------------------------------------------------------------------
  3721. _Use_decl_annotations_
  3722. inline PlaneIntersectionType XM_CALLCONV Intersects( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2, GXMVECTOR Plane )
  3723. {
  3724. XMVECTOR One = XMVectorSplatOne();
  3725. assert( DirectX::Internal::XMPlaneIsUnit( Plane ) );
  3726. // Set w of the points to one so we can dot4 with a plane.
  3727. XMVECTOR TV0 = XMVectorInsert<0, 0, 0, 0, 1>(V0, One);
  3728. XMVECTOR TV1 = XMVectorInsert<0, 0, 0, 0, 1>(V1, One);
  3729. XMVECTOR TV2 = XMVectorInsert<0, 0, 0, 0, 1>(V2, One);
  3730. XMVECTOR Outside, Inside;
  3731. DirectX::Internal::FastIntersectTrianglePlane( TV0, TV1, TV2, Plane, Outside, Inside );
  3732. // If the triangle is outside any plane it is outside.
  3733. if ( XMVector4EqualInt( Outside, XMVectorTrueInt() ) )
  3734. return FRONT;
  3735. // If the triangle is inside all planes it is inside.
  3736. if ( XMVector4EqualInt( Inside, XMVectorTrueInt() ) )
  3737. return BACK;
  3738. // The triangle is not inside all planes or outside a plane it intersects.
  3739. return INTERSECTING;
  3740. }
  3741. //-----------------------------------------------------------------------------
  3742. // Test a triangle vs 6 planes (typically forming a frustum).
  3743. //-----------------------------------------------------------------------------
  3744. _Use_decl_annotations_
  3745. inline ContainmentType XM_CALLCONV ContainedBy( FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2,
  3746. GXMVECTOR Plane0, HXMVECTOR Plane1, HXMVECTOR Plane2,
  3747. CXMVECTOR Plane3, CXMVECTOR Plane4, CXMVECTOR Plane5 )
  3748. {
  3749. XMVECTOR One = XMVectorSplatOne();
  3750. // Set w of the points to one so we can dot4 with a plane.
  3751. XMVECTOR TV0 = XMVectorInsert<0, 0, 0, 0, 1>(V0, One);
  3752. XMVECTOR TV1 = XMVectorInsert<0, 0, 0, 0, 1>(V1, One);
  3753. XMVECTOR TV2 = XMVectorInsert<0, 0, 0, 0, 1>(V2, One);
  3754. XMVECTOR Outside, Inside;
  3755. // Test against each plane.
  3756. DirectX::Internal::FastIntersectTrianglePlane( TV0, TV1, TV2, Plane0, Outside, Inside );
  3757. XMVECTOR AnyOutside = Outside;
  3758. XMVECTOR AllInside = Inside;
  3759. DirectX::Internal::FastIntersectTrianglePlane( TV0, TV1, TV2, Plane1, Outside, Inside );
  3760. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  3761. AllInside = XMVectorAndInt( AllInside, Inside );
  3762. DirectX::Internal::FastIntersectTrianglePlane( TV0, TV1, TV2, Plane2, Outside, Inside );
  3763. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  3764. AllInside = XMVectorAndInt( AllInside, Inside );
  3765. DirectX::Internal::FastIntersectTrianglePlane( TV0, TV1, TV2, Plane3, Outside, Inside );
  3766. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  3767. AllInside = XMVectorAndInt( AllInside, Inside );
  3768. DirectX::Internal::FastIntersectTrianglePlane( TV0, TV1, TV2, Plane4, Outside, Inside );
  3769. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  3770. AllInside = XMVectorAndInt( AllInside, Inside );
  3771. DirectX::Internal::FastIntersectTrianglePlane( TV0, TV1, TV2, Plane5, Outside, Inside );
  3772. AnyOutside = XMVectorOrInt( AnyOutside, Outside );
  3773. AllInside = XMVectorAndInt( AllInside, Inside );
  3774. // If the triangle is outside any plane it is outside.
  3775. if ( XMVector4EqualInt( AnyOutside, XMVectorTrueInt() ) )
  3776. return DISJOINT;
  3777. // If the triangle is inside all planes it is inside.
  3778. if ( XMVector4EqualInt( AllInside, XMVectorTrueInt() ) )
  3779. return CONTAINS;
  3780. // The triangle is not inside all planes or outside a plane, it may intersect.
  3781. return INTERSECTS;
  3782. }
  3783. }; // namespace TriangleTests