qvm.adoc 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098
  1. :last-update-label!:
  2. :icons: font
  3. :prewrap!:
  4. :source-highlighter: coderay
  5. :stylesheet: zajo.css
  6. = QVM
  7. Generic {CPP} library for working with Quaternions Vectors and Matrices
  8. :toclevels: 3
  9. :toc: left
  10. :sectnumlevels: 2
  11. :keywords: c++, boost, matrix, vector, quaternion
  12. [abstract]
  13. == Abstract
  14. QVM is a generic library for working with Quaternions, Vectors and Matrices of static size. Features:
  15. ====
  16. * Emphasis on 2, 3 and 4-dimensional operations needed in graphics, video games and simulation applications.
  17. * Free function templates operate on any compatible user-defined quaternion, vector or matrix type.
  18. * Quaternion, vector and matrix types from different libraries or subsystems can be safely mixed in the same expression.
  19. * Type-safe mapping between compatible lvalue types with no temporary objects; e.g. transpose remaps the elements, rather than transforming the matrix.
  20. [.text-right]
  21. https://github.com/boostorg/qvm[GitHub] | <<tutorial>> | <<reference>> | <<rationale>>
  22. ====
  23. [[tutorial]]
  24. == Tutorial
  25. === Quaternions, Vectors, Matrices
  26. Out of the box QVM defines generic yet simple <<quat,`quat`>>, <<vec,`vec`>> and <<mat,`mat`>> types. For example, the following snippet creates a quaternion object that rotates around the X axis:
  27. [source,c++]
  28. ----
  29. quat<float> rx = rotx_quat(3.14159f);
  30. ----
  31. Similarly, a matrix that translates by a given vector can be created as follows:
  32. [source,c++]
  33. ----
  34. vec<float,3> v = {0,0,7};
  35. mat<float,4,4> tr = translation_mat(v);
  36. ----
  37. The usual quaternion, vector and matrix operations work on these QVM types, however the operations are decoupled from any specific type: they work on any suitable type that has been registered by specializing the <<quat_traits,`quat_traits`>>, <<vec_traits,`vec_traits`>> and <<mat_traits,`mat_traits`>> templates.
  38. For example, a user-defined 3D vector type `float3` can be introduced to QVM as follows:
  39. [source,c++]
  40. ----
  41. struct float3 { float a[3]; };
  42. namespace boost { namespace qvm {
  43. template <>
  44. struct vec_traits<float3> {
  45. static int const dim=3;
  46. typedef float scalar_type;
  47. template <int I>
  48. static inline scalar_type & write_element( float3 & v ) {
  49. return v.a[I];
  50. }
  51. template <int I>
  52. static inline scalar_type read_element( float3 const & v ) {
  53. return v.a[I];
  54. }
  55. static inline scalar_type & write_element_idx( int i, float3 & v ) {
  56. return v.a[i];
  57. } //optional
  58. static inline scalar_type read_element_idx( int i, float3 const & v ) {
  59. return v.a[i];
  60. } //optional
  61. };
  62. } }
  63. ----
  64. Equivalently, using the <<vec_traits_defaults,`vec_traits_defaults`>> template the above can be shortened to:
  65. [source,c++]
  66. ----
  67. namespace boost { namespace qvm {
  68. template <>
  69. struct vec_traits<float3>: vec_traits_defaults<float3,float,3> {
  70. template <int I>
  71. static inline scalar_type & write_element( float3 & v ) {
  72. return v.a[I];
  73. }
  74. static inline scalar_type & write_element_idx( int i, float3 & v ) {
  75. return v.a[i];
  76. } //optional
  77. };
  78. } }
  79. ----
  80. After a similar specialization of the <<mat_traits,`mat_traits`>> template for a user-defined 3x3 matrix type `float33`, the full range of vector and matrix operations defined by QVM headers becomes available automatically:
  81. [source,c++]
  82. ----
  83. float3 v;
  84. X(v) = 0;
  85. Y(v) = 0;
  86. Z(v) = 7;
  87. float vmag = mag(v);
  88. float33 m = rotx_mat<3>(3.14159f);
  89. float3 vrot = m * v;
  90. ----
  91. User-defined quaternion types are similarly introduced to QVM by specializing the <<quat_traits,`quat_traits`>> template.
  92. '''
  93. === C Arrays
  94. In <<boost/qvm/quat_traits_array.hpp,`boost/qvm/quat_traits_array.hpp`>>, <<boost/qvm/vec_traits_array.hpp,`boost/qvm/vec_traits_array.hpp`>> and <<boost/qvm/mat_traits_array.hpp,`boost/qvm/mat_traits_array.hpp`>> QVM defines appropriate <<quat_traits,`quat_traits`>>, <<vec_traits,`vec_traits`>> and <<mat_traits,`mat_traits`>> specializations that allow QVM functions to operate directly on plain old C arrays:
  95. [source,c++]
  96. ----
  97. float v[3] = {0,0,7};
  98. float3 vrot = rotx_mat<3>(3.14159f) * v;
  99. ----
  100. Naturally, operator overloads cannot kick in if all elements of an expression are of built-in types. The following is still illegal:
  101. [source,c++]
  102. ----
  103. float v[3] = {0,0,7};
  104. v *= 42;
  105. ----
  106. The <<vref,`vref`>> and <<mref,`mref`>> function templates can be used to work around this issue:
  107. [source,c++]
  108. ----
  109. float v[3] = {0,0,7};
  110. vref(v) *= 42;
  111. ----
  112. '''
  113. [[view_proxy]]
  114. === View proxies
  115. QVM defines various function templates that provide static mapping between (possibly user-defined) quaternion, vector and matrix types. The example below multiplies column 1 (QVM indexes are always zero-based) of the matrix `m` by a scalar:
  116. [source,c++]
  117. ----
  118. void multiply_column1( float33 & m, float scalar ) {
  119. col<1>(m) *= scalar;
  120. }
  121. ----
  122. The expression <<col,`col<1>(m)`>> is an lvalue of an unspecified 3D vector type that refers to column 1 of `m`. Note however that this does not create any temporary objects; instead `operator*=` above works directly with a reference to `m`.
  123. Here is another example, multiplying a transposed view of a matrix by a vector of some user-defined type `float3`:
  124. [source,c++]
  125. ----
  126. float3 v = {0,0,7};
  127. float3 vrot = transposed(rotx_mat<3>(3.14159f)) * v;
  128. ----
  129. In general, the various view proxy functions return references of unspecified, non-copyable types that refer to the original object. They can be assigned from or converted to any compatible vector or matrix type.
  130. '''
  131. === Swizzling
  132. QVM allows accessing vector elements by swizzling, exposing vector views of different dimensions, and/or views with reordered elements. The example below rotates `v` around the X axis, and stores the resulting vector back in `v` but with the X and Y elements swapped:
  133. [source,c++]
  134. ----
  135. float3 v = {0,0,7};
  136. YXZ(v) = rotx_mat<3>(3.14159f) * v;
  137. ----
  138. A special case of swizzling provides next-dimension-view of a vector object, adding either 0 or 1 as its last component. Assuming `float3` is a 3D vector type, and `float4` is a 4D vector type, the following statements are valid:
  139. [source,c++]
  140. ----
  141. float3 v = {0,0,7};
  142. float4 point = XYZ1(v); //{0,0,7,1}
  143. float4 vector = XYZ0(v); //{0,0,7,0}
  144. ----
  145. It is also valid for swizzling to address vector elements more than once:
  146. [source,c++]
  147. ----
  148. float3 v = {0,0,7};
  149. float4 v1 = ZZZZ(v); //{7,7,7,7}
  150. ----
  151. QVM defines all permutations of `X`, `Y`, `Z`, `W` for 1D, 2D, 3D and 4D swizzling, plus each dimension defines variants with 0 or 1 used at any position (if 0 or 1 appear at the first position, the swizzling function name begins with underscore, e.g. `_1XY`).
  152. The swizzling syntax can also be used to bind scalars as vectors. For example:
  153. [source,c++]
  154. ----
  155. float3 v = _00X(42.0f); //{0,0,42}
  156. ----
  157. '''
  158. [[enable_if]]
  159. === SFINAE/enable_if
  160. SFINAE stands for Substitution Failure Is Not An Error. This refers to a situation in {CPP} where an invalid substitution of template parameters (including when those parameters are deduced implicitly as a result of an unqualified call) is not in itself an error.
  161. In absence of concepts support, SFINAE can be used to disable function template overloads that would otherwise present a signature that is too generic. More formally, this is supported by the Boost `enable_if` library.
  162. For example, QVM defines `operator*` overload which works with any user-defined matrix and vector types. The naive approach would be to declare this overload as follows:
  163. [source,c++]
  164. ----
  165. template <class Matrix,class Vector>
  166. Vector operator*( Matrix const & m, Vector const & v );
  167. ----
  168. Even if the function definition might contain code that would compile only for `Matrix` and `Vector` types, because the function declaration itself is valid, it will participate in overload rezolutions when multiplying objects of any two types whatsoever. This typically renders overload resolutions ambiguous and the compiler (correctly) issues an error.
  169. Using `enable_if`, QVM declares such overloads in a way that preserves their generic signature but only participate in overload resolutions if the passed parameters make sense depending on the semantics of the operation being defined:
  170. [source,c++]
  171. ----
  172. template <class A,class B>
  173. typename enable_if_c<
  174. is_mat<A>::value && is_vec<B>::value && mat_traits<A>::cols==vec_traits<B>::dim, //Condition
  175. B>::type //Return type
  176. operator*( A const & a, B const & b );
  177. ----
  178. For brevity, function declarations throughout this documentation specify the condition which controls whether they are enabled or not without specifying exactly what `enable_if` construct is used to achieve this effect.
  179. '''
  180. === Interoperability
  181. An important design goal of QVM is that it works seamlessly with 3rd-party quaternion, vector and matrix types and libraries. Even when such libraries overload the same {CPP} operators as QVM, it is safe to bring the entire `boost::qvm` namespace in scope by specifying:
  182. [source,c++]
  183. ----
  184. using namespace boost::qvm;
  185. ----
  186. The above using directive does not introduce ambiguities with function and operator overloads defined by a 3rd-party library because:
  187. - Most `boost::qvm` function overloads and all operator overloads use SFINAE/`enable_if`, which makes them "disappear" unless an expression uses types that have the appropriate QVM-specific type traits defined;
  188. - Whenever such overloads are compatible with a given expression, their signature is extremely generic, which means that any other (user-defined) compatible overload will be a better match in any overload resolution.
  189. NOTE: Bringing the entire boost::qvm namespace in scope may introduce ambiguities when accessing types (as opposed to functions) defined by 3rd-party libraries. In that case, you can safely bring namespace `boost::qvm::sfinae` in scope instead, which contains only function and operator overloads that use SFINAE/`enable_if`.
  190. ==== Specifying return types for binary operations
  191. Bringing the `boost::qvm` namespace in scope lets you mix vector and matrix types that come from different APIs into a common, type-safe framework. In this case however, it should be considered what types should be returned by binary operations that return an object by value. For example, if you multiply a 3x3 matrix `m1` of type `user_matrix1` by a 3x3 matrix `m2` of type `user_matrix2`, what type should that operation return?
  192. The answer is that by default, QVM returns some kind of compatible matrix type, so it is always safe to write:
  193. [source,c++]
  194. ----
  195. auto & m = m1 * m2;
  196. ----
  197. However, the type deduced by default converts implicitly to any compatible matrix type, so the following is also valid, at the cost of a temporary:
  198. [source,c++]
  199. ----
  200. user_matrix1 m = m1 * m2;
  201. ----
  202. While the temporary object can be optimized away by many compilers, it can be avoided altogether by specializing the <<deduce_mat2,`deduce_mat2`>> template. For example, to specify that multiplying a `user_matrix1` by a `user_matrix2` should always produce a `user_matrix1` object, you could write:
  203. [source,c++]
  204. ----
  205. namespace boost { namespace qvm {
  206. template <>
  207. struct deduce_mat2<user_matrix1,user_matrix2,3,3> {
  208. typedef user_matrix1 type;
  209. };
  210. template <>
  211. struct deduce_mat2<user_matrix2,user_matrix1,3,3> {
  212. typedef user_matrix1 type;
  213. };
  214. } }
  215. ----
  216. [WARNING]
  217. ====
  218. Be mindful of potential ODR violation when using <<deduce_quat2,`deduce_quat2`>>, <<deduce_vec2,`deduce_vec2`>> and <<deduce_mat2,`deduce_mat2`>> in independent libraries. For example, this could happen if `lib1` defines `deduce_vec2<lib1::vec,lib2::vec>::type` as `lib1::vec` and in the same program `lib2` defines `deduce_vec2<lib1::vec,lib2::vec>::type` as `lib2::vec`.
  219. It is best to keep such specializations out of `lib1` and `lib2`. Of course, it is always safe for `lib1` and `lib2` to use <<convert_to,`convert_to`>> to convert between the `lib1::vec` and `lib2::vec` types as needed.
  220. ====
  221. ==== Specifying return types for unary operations
  222. Perhaps surprisingly, unary operations that return an object by value have a similar, though simpler issue. That's because the argument they're called with may not be copyable, as in:
  223. [source,c++]
  224. ----
  225. float m[3][3];
  226. auto & inv = inverse(m);
  227. ----
  228. Above, the object returned by <<mat_inverse,`inverse`>> and captured by `inv` can not be of type `float[3][3]`, because that type isn't copyable. By default, QVM "just works", returning an object of suitable matrix type that is copyable. This deduction process can be controlled, by specializing the <<deduce_mat,`deduce_mat`>> template.
  229. ==== Converting between different quaternion, vector and matrix types
  230. Any time you need to create a matrix of a particular {CPP} type from any other compatible matrix type, you can use the <<convert_to,`convert_to`>> function:
  231. [source,c++]
  232. ----
  233. user_matrix2 m=convert_to<user_matrix2>(m1 * m2);
  234. ----
  235. [[reference]]
  236. == Reference
  237. === Header Files
  238. QVM is split into multiple headers to allow different compilation units to `#include` only the components they need. Each function in this document specifies the exact header that must be `#included` in order to use it.
  239. The tables below list commonly used components and the headers they're found in. Header names containing a number define functions that only work with objects of that dimension; e.g. `vec_operations2.hpp` contains only functions for working with 2D vectors.
  240. The header `boost/qvm/all.hpp` is provided for convenience. It includes all other QVM headers.
  241. .Quaternion header files
  242. [cols="1,2l"]
  243. |====
  244. | Quaternion traits |#include <boost/qvm/quat_traits.hpp>
  245. #include <boost/qvm/quat_traits_array.hpp>
  246. #include <boost/qvm/deduce_quat.hpp>
  247. | Quaternion element access |#include <boost/qvm/quat_access.hpp>
  248. | Quaternion operations |#include <boost/qvm/quat_operations.hpp>
  249. | <<quat,`quat`>> class template |#include <boost/qvm/quat.hpp>
  250. |====
  251. .Vector header files
  252. [cols="1,2l"]
  253. |====
  254. | Vector traits |#include <boost/qvm/vec_traits.hpp>
  255. #include <boost/qvm/vec_traits_array.hpp>
  256. #include <boost/qvm/deduce_vec.hpp>
  257. | Vector element access |#include <boost/qvm/vec_access.hpp>
  258. | Vector <<swizzling,swizzling>> |#include <boost/qvm/swizzle.hpp>
  259. #include <boost/qvm/swizzle2.hpp>
  260. #include <boost/qvm/swizzle3.hpp>
  261. #include <boost/qvm/swizzle4.hpp>
  262. | Vector operations | #include <boost/qvm/vec_operations.hpp>
  263. #include <boost/qvm/vec_operations2.hpp>
  264. #include <boost/qvm/vec_operations3.hpp>
  265. #include <boost/qvm/vec_operations4.hpp>
  266. | Quaternion-vector operations | #include <boost/qvm/quat_vec_operations.hpp>
  267. | Vector-matrix operations | #include <boost/qvm/vec_mat_operations.hpp>
  268. | Vector-matrix <<view_proxy,view proxies>> | #include <boost/qvm/map_vec_mat.hpp>
  269. | <<vec,`vec`>> class template | #include <boost/qvm/vec.hpp>
  270. |====
  271. .Matrix header files
  272. [cols="1,2l"]
  273. |====
  274. | Matrix traits |#include <boost/qvm/mat_traits.hpp>
  275. #include <boost/qvm/mat_traits_array.hpp>
  276. #include <boost/qvm/deduce_mat.hpp>
  277. | Matrix element access |#include <boost/qvm/mat_access.hpp>
  278. | Matrix operations |#include <boost/qvm/mat_operations.hpp>
  279. #include <boost/qvm/mat_operations2.hpp>
  280. #include <boost/qvm/mat_operations3.hpp>
  281. #include <boost/qvm/mat_operations4.hpp>
  282. | Matrix-matrix <<view_proxy,view proxies>> | #include <boost/qvm/map_mat_mat.hpp>
  283. | Matrix-vector <<view_proxy,view proxies>> | #include <boost/qvm/map_mat_vec.hpp>
  284. | <<mat,`mat`>> class template | #include <boost/qvm/mat.hpp>
  285. |====
  286. [[type_traits]]
  287. === Type Traits System
  288. QVM is designed to work with user-defined quaternion, vector and matrix types, as well as user-defined scalar types. This section formally defines the way such types can be integrated.
  289. '''
  290. [[scalar_requirements]]
  291. ==== Scalar Requirements
  292. A valid scalar type `S` must have accessible destructor, default constructor, copy constructor and assignment operator, and must support the following operations:
  293. [source,c++]
  294. ----
  295. S operator*( S, S );
  296. S operator/( S, S );
  297. S operator+( S, S );
  298. S operator-( S, S );
  299. S & operator*=( S &, S );
  300. S & operator/=( S &, S );
  301. S & operator+=( S &, S );
  302. S & operator-=( S &, S );
  303. bool operator==( S, S );
  304. bool operator!=( S, S );
  305. ----
  306. In addition, the expression `S(0)` should construct a scalar of value zero, and `S(1)` should construct a scalar of value one, or else the <<scalar_traits,`scalar_traits`>> template must be specialized appropriately.
  307. '''
  308. [[is_scalar]]
  309. ==== `is_scalar`
  310. .#include <boost/qvm/scalar_traits.hpp>
  311. [source,c++]
  312. ----
  313. namespace boost { namespace qvm {
  314. template <class T>
  315. struct is_scalar {
  316. static bool const value=false;
  317. };
  318. template <> struct is_scalar<char> { static bool const value=true; };
  319. template <> struct is_scalar<signed char> { static bool const value=true; };
  320. template <> struct is_scalar<unsigned char> { static bool const value=true; };
  321. template <> struct is_scalar<signed short> { static bool const value=true; };
  322. template <> struct is_scalar<unsigned short> { static bool const value=true; };
  323. template <> struct is_scalar<signed int> { static bool const value=true; };
  324. template <> struct is_scalar<unsigned int> { static bool const value=true; };
  325. template <> struct is_scalar<signed long> { static bool const value=true; };
  326. template <> struct is_scalar<unsigned long> { static bool const value=true; };
  327. template <> struct is_scalar<float> { static bool const value=true; };
  328. template <> struct is_scalar<double> { static bool const value=true; };
  329. template <> struct is_scalar<long double> { static bool const value=true; };
  330. } }
  331. ----
  332. This template defines a compile-time boolean constant value which can be used to determine whether a type `T` is a valid scalar type. It must be specialized together with the <<scalar_traits,`scalar_traits`>> template in order to introduce a user-defined scalar type to QVM. Such types must satisfy the <<scalar_requirements,scalar requirements>>.
  333. '''
  334. [[scalar_traits]]
  335. ==== `scalar_traits`
  336. .#include <boost/qvm/scalar_traits.hpp>
  337. [source,c++]
  338. ----
  339. namespace boost { namespace qvm {
  340. template <class Scalar>
  341. struct scalar_traits {
  342. BOOST_QVM_INLINE_CRITICAL
  343. static Scalar value( int v ) {
  344. return Scalar(v);
  345. }
  346. };
  347. } }
  348. ----
  349. This template may be specialized for user-defined scalar types to define the appropriate conversion from `int`; this is primarily used whenever QVM needs to deduce a zero or one value.
  350. '''
  351. [[deduce_scalar]]
  352. ==== `deduce_scalar`
  353. .#include <boost/qvm/deduce_scalar.hpp>
  354. [source,c++]
  355. ----
  356. namespace boost { namespace qvm {
  357. template <class A,class B>
  358. struct deduce_scalar
  359. {
  360. typedef typename impl<A,B>::type type;
  361. };
  362. } }
  363. ----
  364. Requirements: :: `A` and `B` satisfy the <<scalar_requirements,scalar requirements>>.
  365. Returns: ::
  366. If `A` and `B` are the same type, `impl<A,B>::type` returns that type. Otherwise, `impl<A,B>::type` is well defined for the following types only: `signed`/`unsigned char`, `signed`/`unsigned short`, `signed`/`unsigned int`, `signed`/`unsigned long`, `float` and `double`. The deduction logic is as follows:
  367. - if either of `A` and `B` is `double`, the result is `double`;
  368. - else, if one of `A` or `B` is an integer type and the other is `float`, the result is `float`;
  369. - else, if one of `A` or `B` is a signed integer and the other type is unsigned integer, the signed type is changed to unsigned, and then the lesser of the two integers is promoted to the other.
  370. NOTE: This template is used by generic binary operations that return a scalar, to deduce the return type based on the (possibly different) scalars of their arguments.
  371. '''
  372. [[scalar]]
  373. ==== `scalar`
  374. .#include <boost/qvm/scalar_traits.hpp>
  375. [source,c++]
  376. ----
  377. namespace boost { namespace qvm {
  378. template <class T>
  379. struct scalar {
  380. typedef /*exact definition unspecified*/ type;
  381. };
  382. } }
  383. ----
  384. The expression <<quat_traits,`quat_traits<T>::scalar_type`>> evaluates to the scalar type of the quaternion type `T` (if <<is_quat,`is_quat<T>::value`>> is `true`).
  385. The expression <<vec_traits,`vec_traits<T>::scalar_type`>> evaluates to the scalar type of the vector type `T` (if <<is_vec,`is_vec<T>::value`>> is `true`).
  386. The expression <<mat_traits,`mat_traits<T>::scalar_type`>> evaluates to the scalar type of the matrix type `T` (if <<is_mat,`is_mat<T>::value`>> is `true`).
  387. The expression `scalar<T>::type` is similar, except that it automatically detects whether `T` is a vector or a matrix or a quaternion type.
  388. '''
  389. [[is_quat]]
  390. ==== `is_quat`
  391. .#include <boost/qvm/quat_traits.hpp>
  392. [source,c++]
  393. ----
  394. namespace boost { namespace qvm {
  395. template <class T>
  396. struct is_quat {
  397. static bool const value = false;
  398. };
  399. } }
  400. ----
  401. This type template defines a compile-time boolean constant value which can be used to determine whether a type `T` is a quaternion type. For quaternion types, the <<quat_traits,`quat_traits`>> template can be used to access their elements generically, or to obtain their `scalar type`.
  402. '''
  403. [[quat_traits]]
  404. ==== `quat_traits`
  405. .#include <boost/qvm/quat_traits.hpp>
  406. [source,c++]
  407. ----
  408. namespace boost { namespace qvm {
  409. template <class Q>
  410. struct quat_traits {
  411. /*main template members unspecified*/
  412. };
  413. /*
  414. User-defined (possibly partial) specializations:
  415. template <>
  416. struct quat_traits<Q> {
  417. typedef <<user-defined>> scalar_type;
  418. template <int I>
  419. static inline scalar_type read_element( Quaternion const & q );
  420. template <int I>
  421. static inline scalar_type & write_element( Quaternion & q );
  422. };
  423. */
  424. } }
  425. ----
  426. The `quat_traits` template must be specialized for (user-defined) quaternion types in order to enable quaternion operations defined in QVM headers for objects of those types.
  427. NOTE: QVM quaternion operations do not require that quaternion types are copyable.
  428. The main `quat_traits` template members are not specified. Valid specializations are required to define the following members:
  429. - `scalar_type`: the expression `quat_traits<Quaternion>::scalar_type` must be a value type which satisfies the <<scalar_requirements,`scalar requirements`>>.
  430. In addition, valid specializations of the `quat_traits` template must define at least one of the following access functions as static members, where `q` is an object of type `Quaternion`, and `I` is compile-time integer constant:
  431. - `read_element`: the expression `quat_traits<Quaternion>::read_element<I>(q)` returns either a copy of or a `const` reference to the `I`-th element of `q`.
  432. - `write_element`: the expression `quat_traits<Quaternion>::write_element<I>(q)` returns mutable reference to the `I`-th element of `q`.
  433. NOTE: For the quaternion `a + bi + cj + dk`, the elements are assumed to be in the following order: `a`, `b`, `c`, `d`; that is, `I`=`0`/`1`/`2`/`3` would access `a`/`b`/`c`/`d`.
  434. It is illegal to call any of the above functions unless `is_quat<Quaternion>::value` is true. Even then, quaternion types are allowed to define only a subset of the access functions.
  435. Below is an example of a user-defined quaternion type, and its corresponding specialization of the quat_traits template:
  436. [source,c++]
  437. ----
  438. #include <boost/qvm/quat_traits.hpp>
  439. struct fquat { float a[4]; };
  440. namespace boost { namespace qvm {
  441. template <>
  442. struct quat_traits<fquat> {
  443. typedef float scalar_type;
  444. template <int I>
  445. static inline scalar_type & write_element( fquat & q ) {
  446. return q.a[I];
  447. }
  448. template <int I>
  449. static inline scalar_type read_element( fquat const & q ) {
  450. return q.a[I];
  451. }
  452. };
  453. } }
  454. ----
  455. Equivalently, using the <<quat_traits_defaults,`quat_traits_defaults`>> template the above can be shortened to:
  456. [source,c++]
  457. ----
  458. namespace boost { namespace qvm {
  459. template <>
  460. struct quat_traits<fquat>: quat_traits_defaults<fquat,float> {
  461. template <int I>
  462. static inline scalar_type & write_element( fquat & q ) {
  463. return q.a[I];
  464. }
  465. };
  466. } }
  467. ----
  468. '''
  469. [[quat_traits_defaults]]
  470. ==== `quat_traits_defaults`
  471. .#include <boost/qvm/quat_traits_defaults.hpp>
  472. [source,c++]
  473. ----
  474. namespace boost { namespace qvm {
  475. template <class QuatType,class ScalarType>
  476. struct quat_traits_defaults {
  477. typedef QuatType quat_type;
  478. typedef ScalarType scalar_type;
  479. template <int I>
  480. static BOOST_QVM_INLINE_CRITICAL
  481. scalar_type read_element( quat_type const & x ) {
  482. return quat_traits<quat_type>::template
  483. write_element<I>(const_cast<quat_type &>(x));
  484. }
  485. };
  486. } }
  487. ----
  488. The `quat_traits_defaults` template is designed to be used as a public base for user-defined specializations of the <<quat_traits,`quat_traits`>> template, to easily define the required members. If it is used, the only member that must be defined by the user in a `quat_traits` specialization is `write_element`; the `quat_traits_defaults` base will define `read_element`, as well as `scalar_type` automatically.
  489. '''
  490. [[deduce_quat]]
  491. ==== `deduce_quat`
  492. .#include <boost/qvm/deduce_quat.hpp>
  493. [source,c++]
  494. ----
  495. namespace boost { namespace qvm {
  496. template <class Q>
  497. struct deduce_quat {
  498. typedef Q type;
  499. };
  500. } }
  501. ----
  502. Requirements: ::
  503. - `<<is_quat,is_quat>><Q>::value` is `true`;
  504. - `<<is_quat,is_quat>><deduce_quat<Q>::type>::value` must be `true`;
  505. - `deduce_quat<Q>::type` must be copyable.
  506. This template is used by QVM whenever it needs to deduce a copyable quaternion type from a single user-supplied function parameter of quaternion type. Note that `Q` itself may be non-copyable.
  507. The main template definition returns `Q`, which means that it is suitable only for copyable quaternion types. QVM also defines (partial) specializations for the non-copyable quaternion types it produces. Users can define other (partial) specializations for their own types.
  508. A typical use of the `deduce_quat` template is for specifying the preferred quaternion type to be returned by the generic function template overloads in QVM depending on the type of their arguments.
  509. '''
  510. [[deduce_quat2]]
  511. ==== `deduce_quat2`
  512. .#include <boost/qvm/deduce_quat.hpp>
  513. [source,c++]
  514. ----
  515. namespace boost { namespace qvm {
  516. template <class A,class B>
  517. struct deduce_quat2 {
  518. typedef /*unspecified*/ type;
  519. };
  520. } }
  521. ----
  522. Requirements: ::
  523. - Both `<<scalar,scalar>><A>::type` and `scalar<B>::type` are well defined;
  524. - `<<is_quat,is_quat>><A>::value` || `is_quat<B>::value` is `true`;
  525. - `is_quat<deduce_quat2<A,B>::type>::value` must be `true`;
  526. - `deduce_quat2<A,B>::type` must be copyable.
  527. This template is used by QVM whenever it needs to deduce a quaternion type from the types of two user-supplied function parameters. The returned type must have accessible copy constructor (the `A` and `B` types themselves could be non-copyable, and either one of them may not be a quaternion type.)
  528. The main template definition returns an unspecified quaternion type with <<quat_traits,`scalar_type`>> obtained by `<<deduce_scalar,deduce_scalar>><A,B>::type`, except if `A` and `B` are the same quaternion type `Q`, in which case `Q` is returned, which is only suitable for copyable types. QVM also defines (partial) specializations for the non-copyable quaternion types it produces. Users can define other (partial) specializations for their own types.
  529. A typical use of the `deduce_quat2` template is for specifying the preferred quaternion type to be returned by the generic function template overloads in QVM depending on the type of their arguments.
  530. '''
  531. [[is_vec]]
  532. ==== `is_vec`
  533. .#include <boost/qvm/vec_traits.hpp>
  534. [source,c++]
  535. ----
  536. namespace boost { namespace qvm {
  537. template <class T>
  538. struct is_vec {
  539. static bool const value = false;
  540. };
  541. } }
  542. ----
  543. This type template defines a compile-time boolean constant value which can be used to determine whether a type `T` is a vector type. For vector types, the <<vec_traits,`vec_traits`>> template can be used to access their elements generically, or to obtain their dimension and `scalar type`.
  544. '''
  545. [[vec_traits]]
  546. ==== `vec_traits`
  547. .#include <boost/qvm/vec_traits.hpp>
  548. [source,c++]
  549. ----
  550. namespace boost { namespace qvm {
  551. template <class V>
  552. struct vec_traits {
  553. /*main template members unspecified*/
  554. };
  555. /*
  556. User-defined (possibly partial) specializations:
  557. template <>
  558. struct vec_traits<V> {
  559. static int const dim = <<user-defined>>;
  560. typedef <<user-defined>> scalar_type;
  561. template <int I>
  562. static inline scalar_type read_element( Vector const & v );
  563. template <int I>
  564. static inline scalar_type & write_element( Vector & v );
  565. static inline scalar_type read_element_idx( int i, Vector const & v );
  566. static inline scalar_type & write_element_idx( int i, Vector & v );
  567. };
  568. */
  569. } }
  570. ----
  571. The `vec_traits` template must be specialized for (user-defined) vector types in order to enable vector and matrix operations defined in QVM headers for objects of those types.
  572. NOTE: QVM vector operations do not require that vector types are copyable.
  573. The main `vec_traits` template members are not specified. Valid specializations are required to define the following members:
  574. - `dim`: the expression `vec_traits<Vector>::dim` must evaluate to a compile-time integer constant greater than 0 that specifies the vector size.
  575. - `scalar_type`: the expression `vec_traits<Vector>::scalar_type` must be a value type which satisfies the <<scalar_requirements,`scalar requirements`>>.
  576. In addition, valid specializations of the `vec_traits` template may define the following access functions as static members, where `v` is an object of type `Vector`, `I` is a compile-time integer constant, and `i` is a variable of type `int`:
  577. - `read_element`: the expression `vec_traits<Vector>::read_element<I>(v)` returns either a copy of or a const reference to the `I`-th element of `v`.
  578. - `write_element`: the expression `vec_traits<Vector>::write_element<I>(v)` returns mutable reference to the `I`-th element of `v`.
  579. - `read_element_idx`: the expression `vec_traits<Vector>::read_element_idx(i,v)` returns either a copy of or a `const` reference to the `i`-th element of `v`.
  580. - `write_element_idx`: the expression `vec_traits<Vector>::write_element_idx(i,v)` returns mutable reference to the `i`-th element of `v`.
  581. It is illegal to call any of the above functions unless `is_vec<Vector>::value` is true. Even then, vector types are allowed to define only a subset of the access functions. The general requirements are:
  582. - At least one of `read_element` or `write_element` must be defined;
  583. - If `read_element_idx` is defined, `read_element` must also be defined;
  584. - If `write_element_idx` is defined, `write_element` must also be defined.
  585. Below is an example of a user-defined 3D vector type, and its corresponding specialization of the `vec_traits` template:
  586. [source,c++]
  587. ----
  588. #include <boost/qvm/vec_traits.hpp>
  589. struct float3 { float a[3]; };
  590. namespace boost { namespace qvm {
  591. template <>
  592. struct vec_traits<float3> {
  593. static int const dim=3;
  594. typedef float scalar_type;
  595. template <int I>
  596. static inline scalar_type & write_element( float3 & v ) {
  597. return v.a[I];
  598. }
  599. template <int I>
  600. static inline scalar_type read_element( float3 const & v ) {
  601. return v.a[I];
  602. }
  603. static inline scalar_type & write_element_idx( int i, float3 & v ) {
  604. return v.a[i];
  605. } //optional
  606. static inline scalar_type read_element_idx( int i, float3 const & v ) {
  607. return v.a[i];
  608. } //optional
  609. };
  610. } }
  611. ----
  612. Equivalently, using the <<vec_traits_defaults,`vec_traits_defaults`>> template the above can be shortened to:
  613. [source,c++]
  614. ----
  615. namespace boost { namespace qvm {
  616. template <>
  617. struct vec_traits<float3>: vec_traits_defaults<float3,float,3>
  618. {
  619. template <int I>
  620. static inline scalar_type & write_element( float3 & v ) {
  621. return v.a[I];
  622. }
  623. static inline scalar_type & write_element_idx( int i, float3 & v ) {
  624. return v.a[i];
  625. } //optional
  626. };
  627. } }
  628. ----
  629. '''
  630. [[vec_traits_defaults]]
  631. ==== `vec_traits_defaults`
  632. .#include <boost/qvm/vec_traits_defaults.hpp>
  633. [source,c++]
  634. ----
  635. namespace boost { namespace qvm {
  636. template <class VecType,class ScalarType,int Dim>
  637. struct vec_traits_defaults {
  638. typedef VecType vec_type;
  639. typedef ScalarType scalar_type;
  640. static int const dim=Dim;
  641. template <int I>
  642. static BOOST_QVM_INLINE_CRITICAL
  643. scalar_type write_element( vec_type const & x ) {
  644. return vec_traits<vec_type>::template write_element<I>(const_cast<vec_type &>(x));
  645. }
  646. static BOOST_QVM_INLINE_CRITICAL
  647. scalar_type read_element_idx( int i, vec_type const & x ) {
  648. return vec_traits<vec_type>::write_element_idx(i,const_cast<vec_type &>(x));
  649. }
  650. protected:
  651. static BOOST_QVM_INLINE_TRIVIAL
  652. scalar_type & write_element_idx( int i, vec_type & m ) {
  653. /* unspecified */
  654. }
  655. };
  656. } }
  657. ----
  658. The `vec_traits_defaults` template is designed to be used as a public base for user-defined specializations of the <<vec_traits,`vec_traits`>> template, to easily define the required members. If it is used, the only member that must be defined by the user in a `vec_traits` specialization is `write_element`; the `vec_traits_defaults` base will define `read_element`, as well as `scalar_type` and `dim` automatically.
  659. Optionally, the user may also define `write_element_idx`, in which case the `vec_traits_defaults` base will provide a suitable `read_element_idx` definition automatically. If not, `vec_traits_defaults` defines a protected implementation of `write_element_idx` which may be made publicly available by the deriving `vec_traits` specialization in case the vector type for which it is being specialized can not be indexed efficiently. This `write_element_idx` function is less efficient (using meta-programming), implemented in terms of the required user-defined `write_element`.
  660. '''
  661. [[deduce_vec]]
  662. ==== `deduce_vec`
  663. .#include <boost/qvm/deduce_vec.hpp>
  664. [source,c++]
  665. ----
  666. namespace boost { namespace qvm {
  667. template <class V, int Dim=vec_traits<Vector>::dim>
  668. struct deduce_vec {
  669. typedef /*unspecified*/ type;
  670. };
  671. } }
  672. ----
  673. Requirements: ::
  674. - `<<is_vec,is_vec>><V>::value` is `true`;
  675. - `is_vec<deduce_vec<V>::type>::value` must be `true`;
  676. - `deduce_vec<V>::type` must be copyable;
  677. - `vec_traits<deduce_vec<V>::type>::dim==Dim`.
  678. This template is used by QVM whenever it needs to deduce a copyable vector type of certain dimension from a single user-supplied function parameter of vector type. The returned type must have accessible copy constructor. Note that `V` may be non-copyable.
  679. The main template definition returns an unspecified copyable vector type of size `Dim`, except if `<<vec_traits,vec_traits>><V>::dim==Dim`, in which case it returns `V`, which is suitable only if `V` is a copyable type. QVM also defines (partial) specializations for the non-copyable vector types it produces. Users can define other (partial) specializations for their own types.
  680. A typical use of the `deduce_vec` template is for specifying the preferred vector type to be returned by the generic function template overloads in QVM depending on the type of their arguments.
  681. '''
  682. [[deduce_vec2]]
  683. ==== `deduce_vec2`
  684. .#include <boost/qvm/deduce_vec.hpp>
  685. [source,c++]
  686. ----
  687. namespace boost { namespace qvm {
  688. template <class A,class B,int Dim>
  689. struct deduce_vec2 {
  690. typedef /*unspecified*/ type;
  691. };
  692. } }
  693. ----
  694. Requirements: ::
  695. - Both `<<scalar,scalar>><A>::type` and `scalar<B>::type` are well defined;
  696. - `<<is_vec,is_vec>><A>::value || is_vec<B>::value` is `true`;
  697. - `is_vec<deduce_vec2<A,B>::type>::value` must be `true`;
  698. - `deduce_vec2<A,B>::type` must be copyable;
  699. - `vec_traits<deduce_vec2<A,B>::type>::dim==Dim`.
  700. This template is used by QVM whenever it needs to deduce a vector type of certain dimension from the types of two user-supplied function parameters. The returned type must have accessible copy constructor (the `A` and `B` types themselves could be non-copyable, and either one of them may not be a vector type.)
  701. The main template definition returns an unspecified vector type of the requested dimension with <<vec_traits,`scalar_type`>> obtained by `<<deduce_scalar,deduce_scalar>><A,B>::type`, except if `A` and `B` are the same vector type `V` of dimension `Dim`, in which case `V` is returned, which is only suitable for copyable types. QVM also defines (partial) specializations for the non-copyable vector types it produces. Users can define other (partial) specializations for their own types.
  702. A typical use of the `deduce_vec2` template is for specifying the preferred vector type to be returned by the generic function template overloads in QVM depending on the type of their arguments.
  703. '''
  704. [[is_mat]]
  705. ==== `is_mat`
  706. .#include <boost/qvm/mat_traits.hpp>
  707. [source,c++]
  708. ----
  709. namespace boost { namespace qvm {
  710. template <class T>
  711. struct is_mat {
  712. static bool const value = false;
  713. };
  714. } }
  715. ----
  716. This type template defines a compile-time boolean constant value which can be used to determine whether a type `T` is a matrix type. For matrix types, the <<mat_traits,`mat_traits`>> template can be used to access their elements generically, or to obtain their dimensions and scalar type.
  717. '''
  718. [[mat_traits]]
  719. ==== `mat_traits`
  720. .#include <boost/qvm/mat_traits.hpp>
  721. [source,c++]
  722. ----
  723. namespace boost { namespace qvm {
  724. template <class M>
  725. struct mat_traits {
  726. /*main template members unspecified*/
  727. };
  728. /*
  729. User-defined (possibly partial) specializations:
  730. template <>
  731. struct mat_traits<M> {
  732. static int const rows = <<user-defined>>;
  733. static int const cols = <<user-defined>>;
  734. typedef <<user-defined>> scalar_type;
  735. template <int R,int C>
  736. static inline scalar_type read_element( Matrix const & m );
  737. template <int R,int C>
  738. static inline scalar_type & write_element( Matrix & m );
  739. static inline scalar_typeread_element_idx( int r, int c, Matrix const & m );
  740. static inline scalar_type & write_element_idx( int r, int c, Matrix & m );
  741. };
  742. */
  743. } }
  744. ----
  745. The `mat_traits` template must be specialized for (user-defined) matrix types in order to enable vector and matrix operations defined in QVM headers for objects of those types.
  746. NOTE: The matrix operations defined by QVM do not require matrix types to be copyable.
  747. The main `mat_traits` template members are not specified. Valid specializations are required to define the following members:
  748. - `rows`: the expression `mat_traits<Matrix>::rows` must evaluate to a compile-time integer constant greater than 0 that specifies the number of rows in a matrix.
  749. - `cols` must evaluate to a compile-time integer constant greater than 0 that specifies the number of columns in a matrix.
  750. - `scalar_type`: the expression `mat_traits<Matrix>::scalar_type` must be a value type which satisfies the scalar requirements.
  751. In addition, valid specializations of the `mat_traits` template may define the following access functions as static members, where `m` is an object of type `Matrix`, `R` and `C` are compile-time integer constants, and `r` and `c` are variables of type `int`:
  752. - `read_element`: the expression `mat_traits<Matrix>::read_element<R,C>(m)` returns either a copy of or a const reference to the element at row `R` and column `C` of `m`.
  753. - `write_element`: the expression `mat_traits<Matrix>::write_element<R,C>(m)` returns mutable reference to the element at row `R` and column `C` of `m`.
  754. - `read_element_idx`: the expression `mat_traits<Matrix>::read_element_idx(r,c,m)` returns either a copy of or a const reference to the element at row `r` and column `c` of `m`.
  755. - `write_element_idx`: the expression `mat_traits<Matrix>::write_element_idx(r,c,m)` returns mutable reference to the element at row `r` and column `c` of `m`.
  756. It is illegal to call any of the above functions unless `is_mat<Matrix>::value` is true. Even then, matrix types are allowed to define only a subset of the access functions. The general requirements are:
  757. - At least one of `read_element` or `write_element` must be defined;
  758. - If `read_element_idx` is defined, `read_element` must also be defined;
  759. - If `write_element_idx` is defined, `write_element` must also be defined.
  760. Below is an example of a user-defined 3x3 matrix type, and its corresponding specialization of the `mat_traits` template:
  761. [source,c++]
  762. ----
  763. #include <boost/qvm/mat_traits.hpp>
  764. struct float33 { float a[3][3]; };
  765. namespace boost { namespace qvm {
  766. template <>
  767. struct mat_traits<float33> {
  768. static int const rows=3;
  769. static int const cols=3;
  770. typedef float scalar_type;
  771. template <int R,int C>
  772. static inline scalar_type & write_element( float33 & m ) {
  773. return m.a[R][C];
  774. }
  775. template <int R,int C>
  776. static inline scalar_type read_element( float33 const & m ) {
  777. return m.a[R][C];
  778. }
  779. static inline scalar_type & write_element_idx( int r, int c, float33 & m ) {
  780. return m.a[r][c];
  781. }
  782. static inline scalar_type read_element_idx( int r, int c, float33 const & m ) {
  783. return m.a[r][c];
  784. }
  785. };
  786. } }
  787. ----
  788. Equivalently, we could use the <<mat_traits_defaults,`mat_traits_defaults` template to shorten the above to:
  789. [source,c++]
  790. ----
  791. namespace boost { namespace qvm {
  792. template <>
  793. struct mat_traits<float33>: mat_traits_defaults<float33,float,3,3> {
  794. template <int R,int C> static inline scalar_type & write_element( float33 & m ) { return m.a[R][C]; }
  795. static inline scalar_type & write_element_idx( int r, int c, float33 & m ) {
  796. return m.a[r][c];
  797. }
  798. };
  799. } }
  800. ----
  801. '''
  802. [[mat_traits_defaults]]
  803. ==== `mat_traits_defaults`
  804. .#include <boost/qvm/mat_traits_defaults.hpp>
  805. [source,c++]
  806. ----
  807. namespace boost { namespace qvm {
  808. template <class MatType,class ScalarType,int Rows,int Cols>
  809. struct mat_traits_defaults
  810. {
  811. typedef MatType mat_type;
  812. typedef ScalarType scalar_type;
  813. static int const rows=Rows;
  814. static int const cols=Cols;
  815. template <int Row,int Col>
  816. static BOOST_QVM_INLINE_CRITICAL
  817. scalar_type write_element( mat_type const & x ) {
  818. return mat_traits<mat_type>::template write_element<Row,Col>(const_cast<mat_type &>(x));
  819. }
  820. static BOOST_QVM_INLINE_CRITICAL
  821. scalar_type read_element_idx( int r, int c, mat_type const & x ) {
  822. return mat_traits<mat_type>::write_element_idx(r,c,const_cast<mat_type &>(x));
  823. }
  824. protected:
  825. static BOOST_QVM_INLINE_TRIVIAL
  826. scalar_type & write_element_idx( int r, int c, mat_type & m ) {
  827. /* unspecified */
  828. }
  829. };
  830. } }
  831. ----
  832. The `mat_traits_defaults` template is designed to be used as a public base for user-defined specializations of the <<mat_traits,`mat_traits`>> template, to easily define the required members. If it is used, the only member that must be defined by the user in a `mat_traits` specialization is `write_element`; the `mat_traits_defaults` base will define `read_element`, as well as `scalar_type`, `rows` and `cols` automatically.
  833. Optionally, the user may also define `write_element_idx`, in which case the `mat_traits_defaults` base will provide a suitable `read_element_idx` definition automatically. Otherwise, `mat_traits_defaults` defines a protected implementation of `write_element_idx` which may be made publicly available by the deriving `mat_traits` specialization in case the matrix type for which it is being specialized can not be indexed efficiently. This `write_element_idx` function is less efficient (using meta-programming), implemented in terms of the required user-defined `write_element`.
  834. '''
  835. [[deduce_mat]]
  836. ==== `deduce_mat`
  837. .#include <boost/qvm/deduce_mat.hpp>
  838. [source,c++]
  839. ----
  840. namespace boost { namespace qvm {
  841. template <
  842. class M,
  843. int Rows=mat_traits<Matrix>::rows,
  844. int Cols=mat_traits<Matrix>::cols>
  845. struct deduce_mat {
  846. typedef /*unspecified*/ type;
  847. };
  848. } }
  849. ----
  850. Requirements: ::
  851. - `<<is_mat,is_mat>><M>::value` is `true`;
  852. - `is_mat<deduce_mat<M>::type>::value` must be `true`;
  853. - `deduce_mat<M>::type` must be copyable;
  854. - `<<mat_traits,mat_traits>><deduce_mat<M>::type>::rows==Rows`;
  855. - `mat_traits<deduce_mat<M>::type>::cols==Cols`.
  856. This template is used by QVM whenever it needs to deduce a copyable matrix type of certain dimensions from a single user-supplied function parameter of matrix type. The returned type must have accessible copy constructor. Note that M itself may be non-copyable.
  857. The main template definition returns an unspecified copyable matrix type of size `Rows` x `Cols`, except if `<<mat_traits,mat_traits>><M>::rows==Rows && mat_traits<M>::cols==Cols`, in which case it returns `M`, which is suitable only if `M` is a copyable type. QVM also defines (partial) specializations for the non-copyable matrix types it produces. Users can define other (partial) specializations for their own types.
  858. A typical use of the deduce_mat template is for specifying the preferred matrix type to be returned by the generic function template overloads in QVM depending on the type of their arguments.
  859. '''
  860. [[deduce_mat2]]
  861. ==== `deduce_mat2`
  862. .#include <boost/qvm/deduce_mat.hpp>
  863. [source,c++]
  864. ----
  865. namespace boost { namespace qvm {
  866. template <class A,class B,int Rows,int Cols>
  867. struct deduce_mat2 {
  868. typedef /*unspecified*/ type;
  869. };
  870. } }
  871. ----
  872. Requirements: ::
  873. - Both `<<scalar,scalar>><A>::type` and `scalar<B>::type` are well defined;
  874. - `<<is_mat,is_mat>><A>::value || is_mat<B>::value` is `true`;
  875. - `is_mat<deduce_mat2<A,B>::type>::value` must be `true`;
  876. - `deduce_mat2<A,B>::type` must be copyable;
  877. - `<<mat_traits,mat_traits>><deduce_mat2<A,B>::type>::rows==Rows`;
  878. - `mat_traits<deduce_mat2<A,B>::type>::cols==Cols`.
  879. This template is used by QVM whenever it needs to deduce a matrix type of certain dimensions from the types of two user-supplied function parameters. The returned type must have accessible copy constructor (the `A` and `B` types themselves could be non-copyable, and either one of them may be a non-matrix type.)
  880. The main template definition returns an unspecified matrix type of the requested dimensions with <<mat_traits,`scalar_type`>> obtained by `<<deduce_scalar,deduce_scalar>><A,B>::type`, except if `A` and `B` are the same matrix type `M` of dimensions `Rows` x `Cols`, in which case `M` is returned, which is only suitable for copyable types. QVM also defines (partial) specializations for the non-copyable matrix types it produces. Users can define other (partial) specializations for their own types.
  881. A typical use of the `deduce_mat2` template is for specifying the preferred matrix type to be returned by the generic function template overloads in QVM depending on the type of their arguments.
  882. '''
  883. === Built-in Quaternion, Vector and Matrix Types
  884. QVM defines several class templates (together with appropriate specializations of <<quat_traits,`quat_traits`>>, <<vec_traits,`vec_traits`>> and <<mat_traits,`mat_traits`>> templates) which can be used as generic quaternion, vector and matrix types. Using these types directly wouldn't be typical though, the main design goal of QVM is to allow users to plug in their own quaternion, vector and matrix types.
  885. [[quat]]
  886. ==== `quat`
  887. .#include <boost/qvm/quat.hpp>
  888. [source,c++]
  889. ----
  890. namespace boost { namespace qvm {
  891. template <class T>
  892. struct quat {
  893. T a[4];
  894. template <class R>
  895. operator R() const {
  896. R r;
  897. assign(r,*this);
  898. return r;
  899. }
  900. };
  901. template <class Quaternion>
  902. struct quat_traits;
  903. template <class T>
  904. struct quat_traits< quat<T> > {
  905. typedef T scalar_type;
  906. template <int I>
  907. static scalar_type read_element( quat<T> const & x ) {
  908. return x.a[I];
  909. }
  910. template <int I>
  911. static scalar_type & write_element( quat<T> & x ) {
  912. return x.a[I];
  913. }
  914. };
  915. } }
  916. ----
  917. This is a simple quaternion type. It converts to any other quaternion type.
  918. The partial specialization of the <<quat_traits,`quat_traits`>> template makes the `quat` template compatible with the generic operations defined by QVM.
  919. '''
  920. [[vec]]
  921. ==== `vec`
  922. .#include <boost/qvm/vec.hpp>
  923. [source,c++]
  924. ----
  925. namespace boost { namespace qvm {
  926. template <class T,int Dim>
  927. struct vec {
  928. T a[Dim];
  929. template <class R>
  930. operator R() const {
  931. R r;
  932. assign(r,*this);
  933. return r;
  934. }
  935. };
  936. template <class Vector>
  937. struct vec_traits;
  938. template <class T,int Dim>
  939. struct vec_traits< vec<T,Dim> > {
  940. typedef T scalar_type;
  941. static int const dim=Dim;
  942. template <int I>
  943. static scalar_type read_element( vec<T,Dim> const & x ) {
  944. return x.a[I];
  945. }
  946. template <int I>
  947. static scalar_type & write_element( vec<T,Dim> & x ) {
  948. return x.a[I];
  949. }
  950. static scalar_type read_element_idx( int i, vec<T,Dim> const & x ) {
  951. return x.a[i];
  952. }
  953. static scalar_type & write_element_idx( int i, vec<T,Dim> & x ) {
  954. return x.a[i];
  955. }
  956. };
  957. } }
  958. ----
  959. This is a simple vector type. It converts to any other vector type of compatible size.
  960. The partial specialization of the <<vec_traits,`vec_traits`>> template makes the `vec` template compatible with the generic operations defined by QVM.
  961. '''
  962. [[mat]]
  963. ==== `mat`
  964. .#include <boost/qvm/mat.hpp>
  965. [source,c++]
  966. ----
  967. namespace boost { namespace qvm {
  968. template <class T,int Rows,int Cols>
  969. struct mat {
  970. T a[Rows][Cols];
  971. template <class R>
  972. operator R() const {
  973. R r;
  974. assign(r,*this);
  975. return r;
  976. }
  977. };
  978. template <class Matrix>
  979. struct mat_traits;
  980. template <class T,int Rows,int Cols>
  981. struct mat_traits< mat<T,Rows,Cols> > {
  982. typedef T scalar_type;
  983. static int const rows=Rows;
  984. static int const cols=Cols;
  985. template <int Row,int Col>
  986. static scalar_type read_element( mat<T,Rows,Cols> const & x ) {
  987. return x.a[Row][Col];
  988. }
  989. template <int Row,int Col>
  990. static scalar_type & write_element( mat<T,Rows,Cols> & x ) {
  991. return x.a[Row][Col];
  992. }
  993. static scalar_type read_element_idx( int row, int col, mat<T,Rows,Cols> const & x ) {
  994. return x.a[row][col];
  995. }
  996. static scalar_type & write_element_idx( int row, int col, mat<T,Rows,Cols> & x ) {
  997. return x.a[row][col];
  998. }
  999. };
  1000. } }
  1001. ----
  1002. This is a simple matrix type. It converts to any other matrix type of compatible size.
  1003. The partial specialization of the <<mat_traits,`mat_traits`>> template makes the `mat` template compatible with the generic operations defined by QVM.
  1004. '''
  1005. === Element Access
  1006. [[quat_access]]
  1007. ==== Quaternions
  1008. .#include <boost/qvm/quat_access.hpp>
  1009. [source,c++]
  1010. ----
  1011. namespace boost { namespace qvm {
  1012. //Only enabled if:
  1013. // is_quat<Q>::value
  1014. template <class Q> -unspecified-return-type- S( Q & q );
  1015. template <class Q> -unspecified-return-type- V( Q & q );
  1016. template <class Q> -unspecified-return-type- X( Q & q );
  1017. template <class Q> -unspecified-return-type- Y( Q & q );
  1018. template <class Q> -unspecified-return-type- Z( Q & q );
  1019. } }
  1020. ----
  1021. An expression of the form `S(q)` can be used to access the scalar component of the quaternion `q`. For example,
  1022. [source,c++]
  1023. ----
  1024. S(q) *= 42;
  1025. ----
  1026. multiplies the scalar component of `q` by the scalar 42.
  1027. An expression of the form `V(q)` can be used to access the vector component of the quaternion `q`. For example,
  1028. [source,c++]
  1029. ----
  1030. V(q) *= 42
  1031. ----
  1032. multiplies the vector component of `q` by the scalar 42.
  1033. The `X`, `Y` and `Z` elements of the vector component can also be accessed directly using `X(q)`, `Y(q)` and `Z(q)`.
  1034. TIP: The return types are lvalues.
  1035. [[vec_access]]
  1036. ==== Vectors
  1037. .#include <boost/qvm/vec_access.hpp>
  1038. [source,c++]
  1039. ----
  1040. namespace boost { namespace qvm {
  1041. //Only enabled if:
  1042. // is_vec<V>::value
  1043. template <int I,class V> -unspecified-return-type- A( V & v );
  1044. template <class V> -unspecified-return-type- A0( V & v );
  1045. template <class V> -unspecified-return-type- A1( V & v );
  1046. ...
  1047. template <class V> -unspecified-return-type- A9( V & v );
  1048. template <class V> -unspecified-return-type- X( V & v );
  1049. template <class V> -unspecified-return-type- Y( V & v );
  1050. template <class V> -unspecified-return-type- Z( V & v );
  1051. template <class V> -unspecified-return-type- W( V & v );
  1052. } }
  1053. ----
  1054. An expression of the form of `A<I>(v)` can be used to access the `I`-th element a vector object `v`. For example, the expression:
  1055. [source,c++]
  1056. ----
  1057. A<1>(v) *= 42;
  1058. ----
  1059. can be used to multiply the element at index 1 (indexing in QVM is always zero-based) of a vector `v` by 42.
  1060. For convenience, there are also non-template overloads for `I` from 0 to 9; an alternative way to write the above expression is:
  1061. [source,c++]
  1062. ----
  1063. A1(v) *= 42;
  1064. ----
  1065. `X`, `Y`, `Z` and `W` act the same as `A0`/`A1`/`A2`/`A3`; yet another alternative way to write the above expression is:
  1066. [source,c++]
  1067. ----
  1068. Y(v) *= 42;
  1069. ----
  1070. TIP: The return types are lvalues.
  1071. [[swizzling]]
  1072. ==== Vector Element Swizzling
  1073. .#include <boost/qvm/swizzle.hpp>
  1074. [source,c++]
  1075. ----
  1076. namespace boost { namespace qvm {
  1077. //*** Accessing vector elements by swizzling ***
  1078. //2D view proxies, only enabled if:
  1079. // is_vec<V>::value
  1080. template <class V> -unspecified-2D-vector-type- XX( V & v );
  1081. template <class V> -unspecified-2D-vector-type- XY( V & v );
  1082. template <class V> -unspecified-2D-vector-type- XZ( V & v );
  1083. template <class V> -unspecified-2D-vector-type- XW( V & v );
  1084. template <class V> -unspecified-2D-vector-type- X0( V & v );
  1085. template <class V> -unspecified-2D-vector-type- X1( V & v );
  1086. template <class V> -unspecified-2D-vector-type- YX( V & v );
  1087. template <class V> -unspecified-2D-vector-type- YY( V & v );
  1088. template <class V> -unspecified-2D-vector-type- YZ( V & v );
  1089. template <class V> -unspecified-2D-vector-type- YW( V & v );
  1090. template <class V> -unspecified-2D-vector-type- Y0( V & v );
  1091. template <class V> -unspecified-2D-vector-type- Y1( V & v );
  1092. template <class V> -unspecified-2D-vector-type- ZX( V & v );
  1093. template <class V> -unspecified-2D-vector-type- ZY( V & v );
  1094. template <class V> -unspecified-2D-vector-type- ZZ( V & v );
  1095. template <class V> -unspecified-2D-vector-type- ZW( V & v );
  1096. template <class V> -unspecified-2D-vector-type- Z0( V & v );
  1097. template <class V> -unspecified-2D-vector-type- Z1( V & v );
  1098. template <class V> -unspecified-2D-vector-type- WX( V & v );
  1099. template <class V> -unspecified-2D-vector-type- WY( V & v );
  1100. template <class V> -unspecified-2D-vector-type- WZ( V & v );
  1101. template <class V> -unspecified-2D-vector-type- WW( V & v );
  1102. template <class V> -unspecified-2D-vector-type- W0( V & v );
  1103. template <class V> -unspecified-2D-vector-type- W1( V & v );
  1104. ...
  1105. //2D view proxies, only enabled if:
  1106. // is_scalar<S>::value
  1107. template <class S> -unspecified-2D-vector-type- X0( S & s );
  1108. template <class S> -unspecified-2D-vector-type- X1( S & s );
  1109. template <class S> -unspecified-2D-vector-type- XX( S & s );
  1110. ...
  1111. -unspecified-2D-vector-type- _00();
  1112. -unspecified-2D-vector-type- _01();
  1113. -unspecified-2D-vector-type- _10();
  1114. -unspecified-2D-vector-type- _11();
  1115. //3D view proxies, only enabled if:
  1116. // is_vec<V>::value
  1117. template <class V> -unspecified-3D-vector-type- XXX( V & v );
  1118. ...
  1119. template <class V> -unspecified-3D-vector-type- XXW( V & v );
  1120. template <class V> -unspecified-3D-vector-type- XX0( V & v );
  1121. template <class V> -unspecified-3D-vector-type- XX1( V & v );
  1122. template <class V> -unspecified-3D-vector-type- XYX( V & v );
  1123. ...
  1124. template <class V> -unspecified-3D-vector-type- XY1( V & v );
  1125. ...
  1126. template <class V> -unspecified-3D-vector-type- WW1( V & v );
  1127. ...
  1128. //3D view proxies, only enabled if:
  1129. // is_scalar<S>::value
  1130. template <class S> -unspecified-3D-vector-type- X00( S & s );
  1131. template <class S> -unspecified-3D-vector-type- X01( S & s );
  1132. ...
  1133. template <class S> -unspecified-3D-vector-type- XXX( S & s );
  1134. template <class S> -unspecified-3D-vector-type- XX0( S & s );
  1135. ...
  1136. -unspecified-3D-vector-type- _000();
  1137. -unspecified-3D-vector-type- _001();
  1138. -unspecified-3D-vector-type- _010();
  1139. ...
  1140. -unspecified-3D-vector-type- _111();
  1141. //4D view proxies, only enabled if:
  1142. // is_vec<V>::value
  1143. template <class V> -unspecified-4D-vector-type- XXXX( V & v );
  1144. ...
  1145. template <class V> -unspecified-4D-vector-type- XXXW( V & v );
  1146. template <class V> -unspecified-4D-vector-type- XXX0( V & v );
  1147. template <class V> -unspecified-4D-vector-type- XXX1( V & v );
  1148. template <class V> -unspecified-4D-vector-type- XXYX( V & v );
  1149. ...
  1150. template <class V> -unspecified-4D-vector-type- XXY1( V & v );
  1151. ...
  1152. template <class V> -unspecified-4D-vector-type- WWW1( V & v );
  1153. ...
  1154. //4D view proxies, only enabled if:
  1155. // is_scalar<S>::value
  1156. template <class S> -unspecified-4D-vector-type- X000( S & s );
  1157. template <class S> -unspecified-4D-vector-type- X001( S & s );
  1158. ...
  1159. template <class S> -unspecified-4D-vector-type- XXXX( S & s );
  1160. template <class S> -unspecified-4D-vector-type- XX00( S & s );
  1161. ...
  1162. -unspecified-4D-vector-type- _0000();
  1163. -unspecified-4D-vector-type- _0001();
  1164. -unspecified-4D-vector-type- _0010();
  1165. ...
  1166. -unspecified-4D-vector-type- _1111();
  1167. } }
  1168. ----
  1169. Swizzling allows zero-overhead direct access to a (possibly rearranged) subset of the elements of 2D, 3D and 4D vectors. For example, if `v` is a 4D vector, the expression `YX(v) is a 2D view proxy whose `X` element refers to the `Y` element of `v`, and whose `Y` element refers to the `X` element of `v`. Like other view proxies `YX` is an lvalue, that is, if `v2` is a 2D vector, one could write:
  1170. [source,c++]
  1171. ----
  1172. YX(v) = v2;
  1173. ----
  1174. The above will leave the `Z` and `W` elements of `v` unchanged but assign the `Y` element of `v2` to the `X` element of `v` and the `X` element of `v2` to the `Y` element of `v`.
  1175. All permutations of `X`, `Y`, `Z`, `W`, `0`, `1` for 2D, 3D and 4D swizzling are available (if the first character of the swizzle identifier is `0` or `1`, it is preceded by a `_`, for example `_11XY`).
  1176. It is valid to use the same vector element more than once: the expression `ZZZ(v)` is a 3D vector whose `X`, `Y` and `Z` elements all refer to the `Z` element of `v`.
  1177. Finally, scalars can be "swizzled" to access them as vectors: the expression `_0X01(42.0f)` is a 4D vector with `X`=0, `Y`=42.0, `Z`=0, `W`=1.
  1178. [[mat_access]]
  1179. ==== Matrices
  1180. .#include <boost/qvm/mat_access.hpp>
  1181. [source,c++]
  1182. ----
  1183. namespace boost { namespace qvm {
  1184. //Only enabled if:
  1185. // is_quat<Q>::value
  1186. template <int R,int C,class M> -unspecified-return-type- A( M & m );
  1187. template <class M> -unspecified-return-type- A00( M & m );
  1188. template <class M> -unspecified-return-type- A01( M & m );
  1189. ...
  1190. template <class M> -unspecified-return-type- A09( M & m );
  1191. template <class M> -unspecified-return-type- A10( M & m );
  1192. ...
  1193. template <class M> -unspecified-return-type- A99( M & m );
  1194. } }
  1195. ----
  1196. An expression of the form `A<R,C>(m)` can be used to access the element at row `R` and column `C` of a matrix object `m`. For example, the expression:
  1197. [source,c++]
  1198. ----
  1199. A<4,2>(m) *= 42;
  1200. ----
  1201. can be used to multiply the element at row 4 and column 2 of a matrix `m` by 42.
  1202. For convenience, there are also non-template overloads for `R` from `0` to `9` and `C` from `0` to `9`; an alternative way to write the above expression is:
  1203. [source,c++]
  1204. ----
  1205. A42(m) *= 42;
  1206. ----
  1207. TIP: The return types are lvalues.
  1208. '''
  1209. === Quaternion Operations
  1210. [[quat_assign]]
  1211. ==== `assign`
  1212. .#include <boost/qvm/quat_operations.hpp>
  1213. [source,c++]
  1214. ----
  1215. namespace boost { namespace qvm {
  1216. //Only enabled if:
  1217. // is_quat<A>::value && is_quat<B>::value
  1218. template <class A,class B>
  1219. A & assign( A & a, B const & b );
  1220. } }
  1221. ----
  1222. Effects: :: Copies all elements of the quaternion `b` to the quaternion `a`.
  1223. Returns: :: `a`.
  1224. '''
  1225. [[quat_convert_to]]
  1226. ==== `convert_to`
  1227. .#include <boost/qvm/quat_operations.hpp>
  1228. [source,c++]
  1229. ----
  1230. namespace boost { namespace qvm {
  1231. //Only enabled if:
  1232. // is_quat<R>::value && is_quat<A>::value
  1233. template <class R,class A>
  1234. R convert_to( A const & a );
  1235. //Only enabled if:
  1236. // is_quat<R>::value && is_mat<A>::value &&
  1237. // mat_traits<A>::rows==3 && mat_traits<A>::cols==3
  1238. template <class R,class A>
  1239. R convert_to( A const & m );
  1240. } }
  1241. ----
  1242. Requirements: :: `R` must be copyable.
  1243. Effects: ::
  1244. - The first overload is equivalent to: `R r; assign(r,a); return r;`
  1245. - The second overload assumes that `m` is an orthonormal rotation matrix and converts it to a quaternion that performs the same rotation.
  1246. '''
  1247. [[quat_minus_eq]]
  1248. ==== `operator-=`
  1249. .#include <boost/qvm/quat_operations.hpp>
  1250. [source,c++]
  1251. ----
  1252. namespace boost { namespace qvm {
  1253. //Only enabled if:
  1254. // is_quat<A>::value && is_quat<B>::value
  1255. template <class A,class B>
  1256. A & operator-=( A & a, B const & b );
  1257. } }
  1258. ----
  1259. Effects: :: Subtracts the elements of `b` from the corresponding elements of `a`.
  1260. Returns: :: `a`.
  1261. '''
  1262. [[quat_minus_unary]]
  1263. ==== `operator-` (unary)
  1264. .#include <boost/qvm/quat_operations.hpp>
  1265. [source,c++]
  1266. ----
  1267. namespace boost { namespace qvm {
  1268. //Only enabled if: is_quat<A>::value
  1269. template <class A>
  1270. typename deduce_quat<A>::type
  1271. operator-( A const & a );
  1272. } }
  1273. ----
  1274. Returns: :: A quaternion of the negated elements of `a`.
  1275. NOTE: The <<deduce_quat,`deduce_quat`>> template can be specialized to deduce the desired return type from the type `A`.
  1276. '''
  1277. [[quat_minus]]
  1278. ==== `operator-` (binary)
  1279. .#include <boost/qvm/quat_operations.hpp>
  1280. [source,c++]
  1281. ----
  1282. namespace boost { namespace qvm {
  1283. //Only enabled if:
  1284. // is_quat<A>::value && is_quat<B>::value
  1285. template <class A,class B>
  1286. typename deduce_quat2<A,B>::type
  1287. operator-( A const & a, B const & b );
  1288. } }
  1289. ----
  1290. Returns: :: A quaternion with elements equal to the elements of `b` subtracted from the corresponding elements of `a`.
  1291. NOTE: The <<deduce_quat2,`deduce_quat2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  1292. '''
  1293. [[quat_plus_eq]]
  1294. ==== `operator+=`
  1295. .#include <boost/qvm/quat_operations.hpp>
  1296. [source,c++]
  1297. ----
  1298. namespace boost { namespace qvm {
  1299. //Only enabled if:
  1300. // is_quat<A>::value && is_quat<B>::value
  1301. template <class A,class B>
  1302. A & operator+=( A & a, B const & b );
  1303. } }
  1304. ----
  1305. Effects: :: Adds the elements of `b` to the corresponding elements of `a`.
  1306. Returns: :: `a`.
  1307. '''
  1308. [[quat_plus]]
  1309. ==== `operator+`
  1310. .#include <boost/qvm/quat_operations.hpp>
  1311. [source,c++]
  1312. ----
  1313. namespace boost { namespace qvm {
  1314. //Only enabled if:
  1315. // is_quat<A>::value && is_quat<B>::value &&
  1316. template <class A,class B>
  1317. typename deduce_quat2<A,B>::type
  1318. operator+( A const & a, B const & b );
  1319. } }
  1320. ----
  1321. Returns: :: A quaternion with elements equal to the elements of `a` added to the corresponding elements of `b`.
  1322. NOTE: The <<deduce_quat2,`deduce_quat2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  1323. '''
  1324. [[quat_div_eq_scalar]]
  1325. ==== `operator/=` (scalar)
  1326. .#include <boost/qvm/quat_operations.hpp>
  1327. [source,c++]
  1328. ----
  1329. namespace boost { namespace qvm {
  1330. //Only enabled if: is_quat<A>::value && is_scalar<B>::value
  1331. template <class A,class B>
  1332. A & operator/=( A & a, B b );
  1333. } }
  1334. ----
  1335. Effects: :: This operation divides a quaternion by a scalar.
  1336. Returns: :: `a`.
  1337. '''
  1338. [[quat_div_scalar]]
  1339. ==== `operator/` (scalar)
  1340. .#include <boost/qvm/quat_operations.hpp>
  1341. [source,c++]
  1342. ----
  1343. namespace boost { namespace qvm {
  1344. //Only enabled if: is_quat<A>::value && is_scalar<B>::value
  1345. template <class A,class B>
  1346. typename deduce_quat<A>::type
  1347. operator/( A const & a, B b );
  1348. } }
  1349. ----
  1350. Returns: :: A quaternion that is the result of dividing the quaternion `a` by the scalar `b`.
  1351. NOTE: The <<deduce_quat,`deduce_quat`>> template can be specialized to deduce the desired return type from the type `A`.
  1352. '''
  1353. [[quat_mul_eq_scalar]]
  1354. ==== `operator*=` (scalar)
  1355. .#include <boost/qvm/quat_operations.hpp>
  1356. [source,c++]
  1357. ----
  1358. namespace boost { namespace qvm {
  1359. //Only enabled if: is_quat<A>::value && is_scalar<B>::value
  1360. template <class A,class B>
  1361. A & operator*=( A & a, B b );
  1362. } }
  1363. ----
  1364. Effects: :: This operation multiplies the quaternion `a` by the scalar `b`.
  1365. Returns: :: `a`.
  1366. '''
  1367. [[quat_mul_eq]]
  1368. ==== `operator*=`
  1369. .#include <boost/qvm/quat_operations.hpp>
  1370. [source,c++]
  1371. ----
  1372. namespace boost { namespace qvm {
  1373. //Only enabled if:
  1374. // is_quat<A>::value && is_quat<B>::value
  1375. template <class A,class B>
  1376. A & operator*=( A & a, B const & b );
  1377. } }
  1378. ----
  1379. Effects: :: As if:
  1380. +
  1381. [source,c++]
  1382. ----
  1383. A tmp(a);
  1384. a = tmp * b;
  1385. return a;
  1386. ----
  1387. '''
  1388. [[quat_mul_scalar]]
  1389. ==== `operator*` (scalar)
  1390. .#include <boost/qvm/quat_operations.hpp>
  1391. [source,c++]
  1392. ----
  1393. namespace boost { namespace qvm {
  1394. //Only enabled if: is_quat<A>::value && is_scalar<B>::value
  1395. template <class A,class B>
  1396. typename deduce_quat<A>::type
  1397. operator*( A const & a, B b );
  1398. } }
  1399. ----
  1400. Returns: :: A quaternion that is the result of multiplying the quaternion `a` by the scalar `b`.
  1401. NOTE: The <<deduce_quat,`deduce_quat`>> template can be specialized to deduce the desired return type from the type `A`.
  1402. '''
  1403. [[quat_mul]]
  1404. ==== `operator*`
  1405. .#include <boost/qvm/quat_operations.hpp>
  1406. [source,c++]
  1407. ----
  1408. namespace boost { namespace qvm {
  1409. //Only enabled if:
  1410. // is_quat<A>::value && is_quat<B>::value
  1411. template <class A,class B>
  1412. typename deduce_quat2<A,B>::type
  1413. operator*( A const & a, B const & b );
  1414. } }
  1415. ----
  1416. Returns: :: The result of multiplying the quaternions `a` and `b`.
  1417. NOTE: The <<deduce_quat2,`deduce_quat2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  1418. '''
  1419. [[quat_eq]]
  1420. ==== `operator==`
  1421. .#include <boost/qvm/quat_operations.hpp>
  1422. [source,c++]
  1423. ----
  1424. namespace boost { namespace qvm {
  1425. //Only enabled if:
  1426. // is_quat<A>::value && is_quat<B>::value
  1427. template <class A,class B>
  1428. bool operator==( A const & a, B const & b );
  1429. } }
  1430. ----
  1431. Returns: :: `true` if each element of `a` compares equal to its corresponding element of `b`, `false` otherwise.
  1432. '''
  1433. [[quat_neq]]
  1434. ==== `operator!=`
  1435. .#include <boost/qvm/quat_operations.hpp>
  1436. [source,c++]
  1437. ----
  1438. namespace boost { namespace qvm {
  1439. //Only enabled if:
  1440. // is_quat<A>::value && is_quat<B>::value
  1441. template <class A,class B>
  1442. bool operator!=( A const & a, B const & b );
  1443. } }
  1444. ----
  1445. Returns: :: `!(a == b)`.
  1446. '''
  1447. [[quat_cmp]]
  1448. ==== `cmp`
  1449. .#include <boost/qvm/quat_operations.hpp>
  1450. [source,c++]
  1451. ----
  1452. namespace boost { namespace qvm {
  1453. //Only enabled if:
  1454. // is_quat<A>::value && is_quat<B>::value
  1455. template <class A,class B,class Cmp>
  1456. bool cmp( A const & a, B const & b, Cmp pred );
  1457. } }
  1458. ----
  1459. Returns: :: Similar to <<quat_eq,`operator==`>>, except that it uses the binary predicate `pred` to compare the individual quaternion elements.
  1460. '''
  1461. [[quat_mag_sqr]]
  1462. ==== `mag_sqr`
  1463. .#include <boost/qvm/quat_operations.hpp>
  1464. [source,c++]
  1465. ----
  1466. namespace boost { namespace qvm {
  1467. //Only enabled if: is_quat<A>::value
  1468. template <class A>
  1469. typename quat_traits<A>::scalar_type
  1470. mag_sqr( A const & a );
  1471. } }
  1472. ----
  1473. Returns: :: The squared magnitude of the quaternion `a`.
  1474. '''
  1475. [[quat_mag]]
  1476. ==== `mag`
  1477. .#include <boost/qvm/quat_operations.hpp>
  1478. [source,c++]
  1479. ----
  1480. namespace boost { namespace qvm {
  1481. //Only enabled if: is_quat<A>::value
  1482. template <class A>
  1483. typename quat_traits<A>::scalar_type
  1484. mag( A const & a );
  1485. } }
  1486. ----
  1487. Returns: :: The magnitude of the quaternion `a`.
  1488. '''
  1489. [[quat_normalized]]
  1490. ==== `normalized`
  1491. .#include <boost/qvm/quat_operations.hpp>
  1492. [source,c++]
  1493. ----
  1494. namespace boost { namespace qvm {
  1495. //Only enabled if: is_quat<A>::value
  1496. template <class A>
  1497. typename deduce_quat<A>::type
  1498. normalized( A const & a );
  1499. } }
  1500. ----
  1501. Effects: :: As if:
  1502. +
  1503. [source,c++]
  1504. ----
  1505. typename deduce_quat<A>::type tmp;
  1506. assign(tmp,a);
  1507. normalize(tmp);
  1508. return tmp;
  1509. ----
  1510. NOTE: The <<deduce_quat,`deduce_quat`>> template can be specialized to deduce the desired return type from the type `A`.
  1511. '''
  1512. [[quat_normalize]]
  1513. ==== `normalize`
  1514. .#include <boost/qvm/quat_operations.hpp>
  1515. [source,c++]
  1516. ----
  1517. namespace boost { namespace qvm {
  1518. //Only enabled if: is_quat<A>::value
  1519. template <class A>
  1520. void normalize( A & a );
  1521. } }
  1522. ----
  1523. Effects: :: Normalizes `a`.
  1524. Postcondition: :: `mag(a)==scalar_traits<typename quat_traits<A>::scalar_type>::value(1).`
  1525. Throws: :: If the magnitude of `a` is zero, throws <<zero_magnitude_error,`zero_magnitude_error`>>.
  1526. '''
  1527. [[quat_dot]]
  1528. ==== `dot`
  1529. .#include <boost/qvm/quat_operations.hpp>
  1530. [source,c++]
  1531. ----
  1532. namespace boost { namespace qvm {
  1533. //Only enabled if:
  1534. // is_quat<A>::value && is_quat<B>::value
  1535. template <class A,class B>
  1536. typename deduce_scalar<A,B>::type
  1537. dot( A const & a, B const & b );
  1538. } }
  1539. ----
  1540. Returns: :: The dot product of the quaternions `a` and `b`.
  1541. NOTE: The <<deduce_scalar,`deduce_scalar`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  1542. '''
  1543. [[conjugate]]
  1544. ==== `conjugate`
  1545. .#include <boost/qvm/quat_operations.hpp>
  1546. [source,c++]
  1547. ----
  1548. namespace boost { namespace qvm {
  1549. //Only enabled if: is_quat<A>::value
  1550. template <class A>
  1551. typename deduce_quat<A>::type
  1552. conjugate( A const & a );
  1553. } }
  1554. ----
  1555. Returns: :: Computes the conjugate of `a`.
  1556. NOTE: The <<deduce_quat,`deduce_quat`>> template can be specialized to deduce the desired return type from the type `A`.
  1557. '''
  1558. [[quat_inverse]]
  1559. ==== `inverse`
  1560. .#include <boost/qvm/quat_operations.hpp>
  1561. [source,c++]
  1562. ----
  1563. namespace boost { namespace qvm {
  1564. //Only enabled if: is_quat<A>::value
  1565. template <class A>
  1566. typename deduce_quat<A>::type
  1567. inverse( A const & a );
  1568. } }
  1569. ----
  1570. Returns: :: Computes the multiplicative inverse of `a`, or the conjugate-to-norm ratio.
  1571. Throws: :: If the magnitude of `a` is zero, throws <<zero_magnitude_error,`zero_magnitude_error`>>.
  1572. TIP: If `a` is known to be unit length, `conjugate` is equivalent to <<quat_inverse,`inverse`>>, yet it is faster to compute.
  1573. NOTE: The <<deduce_quat,`deduce_quat`>> template can be specialized to deduce the desired return type from the type `A`.
  1574. '''
  1575. [[slerp]]
  1576. ==== `slerp`
  1577. .#include <boost/qvm/quat_operations.hpp>
  1578. [source,c++]
  1579. ----
  1580. namespace boost { namespace qvm {
  1581. //Only enabled if:
  1582. // is_quat<A>::value && is_quat<B>::value && is_scalar<C>
  1583. template <class A,class B,class C>
  1584. typename deduce_quat2<A,B> >::type
  1585. slerp( A const & a, B const & b, C c );
  1586. } }
  1587. ----
  1588. Preconditions: :: `t>=0 && t\<=1`.
  1589. Returns: :: A quaternion that is the result of Spherical Linear Interpolation of the quaternions `a` and `b` and the interpolation parameter `c`. When `slerp` is applied to unit quaternions, the quaternion path maps to a path through 3D rotations in a standard way. The effect is a rotation with uniform angular velocity around a fixed rotation axis.
  1590. NOTE: The <<deduce_quat2,`deduce_quat2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  1591. '''
  1592. [[zero_quat]]
  1593. ==== `zero_quat`
  1594. .#include <boost/qvm/quat_operations.hpp>
  1595. [source,c++]
  1596. ----
  1597. namespace boost { namespace qvm {
  1598. template <class T>
  1599. -unspecified-return-type- zero_quat();
  1600. } }
  1601. ----
  1602. Returns: :: A read-only quaternion of unspecified type with <<scalar_traits,`scalar_type`>> `T`, with all elements equal to <<scalar_traits,`scalar_traits<T>::value(0)`>>.
  1603. '''
  1604. [[quat_set_zero]]
  1605. ==== `set_zero`
  1606. .#include <boost/qvm/quat_operations.hpp>
  1607. [source,c++]
  1608. ----
  1609. namespace boost { namespace qvm {
  1610. //Only enabled if: is_quat<A>::value
  1611. template <class A>
  1612. void set_zero( A & a );
  1613. } }
  1614. ----
  1615. Effects: :: As if:
  1616. +
  1617. [source,c++]
  1618. ----
  1619. assign(a,
  1620. zero_quat<typename quat_traits<A>::scalar_type>());
  1621. ----
  1622. '''
  1623. [[identity_quat]]
  1624. ==== `identity_quat`
  1625. .#include <boost/qvm/quat_operations.hpp>
  1626. [source,c++]
  1627. ----
  1628. namespace boost { namespace qvm {
  1629. template <class S>
  1630. -unspecified-return-type- identity_quat();
  1631. } }
  1632. ----
  1633. Returns: :: An identity quaternion with scalar type `S`.
  1634. '''
  1635. [[quat_set_identity]]
  1636. ==== `set_identity`
  1637. .#include <boost/qvm/quat_operations.hpp>
  1638. [source,c++]
  1639. ----
  1640. namespace boost { namespace qvm {
  1641. //Only enabled if: is_quat<A>::value
  1642. template <class A>
  1643. void set_identity( A & a );
  1644. } }
  1645. ----
  1646. Effects: :: As if:
  1647. +
  1648. [source,c++]
  1649. ----
  1650. assign(
  1651. a,
  1652. identity_quat<typename quat_traits<A>::scalar_type>());
  1653. ----
  1654. '''
  1655. [[rot_quat]]
  1656. ==== `rot_quat`
  1657. .#include <boost/qvm/quat_operations.hpp>
  1658. [source,c++]
  1659. ----
  1660. namespace boost { namespace qvm {
  1661. //Only enabled if:
  1662. // is_vec<A>::value && vec_traits<A>::dim==3
  1663. template <class A>
  1664. -unspecified-return-type- rot_quat( A const & axis, typename vec_traits<A>::scalar_type angle );
  1665. } }
  1666. ----
  1667. Returns: :: A quaternion of unspecified type which performs a rotation around the `axis` at `angle` radians.
  1668. Throws: :: In case the axis vector has zero magnitude, throws <<zero_magnitude_error,`zero_magnitude_error`>>.
  1669. NOTE: The `rot_quat` function is not a <<view_proxy,view proxy>>; it returns a temp object.
  1670. '''
  1671. [[quat_set_rot]]
  1672. ==== `set_rot`
  1673. .#include <boost/qvm/quat_operations.hpp>
  1674. [source,c++]
  1675. ----
  1676. namespace boost { namespace qvm {
  1677. //Only enabled if:
  1678. // is_quat<A>::value &&
  1679. // is_vec<B>::value && vec_traits<B>::dim==3
  1680. template <class A>
  1681. void set_rot( A & a, B const & axis, typename vec_traits<B>::scalar_type angle );
  1682. } }
  1683. ----
  1684. Effects: :: As if:
  1685. +
  1686. [source,c++]
  1687. ----
  1688. assign(
  1689. a,
  1690. rot_quat(axis,angle));
  1691. ----
  1692. '''
  1693. [[quat_rotate]]
  1694. ==== `rotate`
  1695. .#include <boost/qvm/quat_operations.hpp>
  1696. [source,c++]
  1697. ----
  1698. namespace boost { namespace qvm {
  1699. //Only enabled if:
  1700. // is_quat<A>::value &&
  1701. // is_vec<B>::value && vec_traits<B>::dim==3
  1702. template <class A,class B>
  1703. void rotate( A & a, B const & axis, typename quat_traits<A>::scalar_type angle );
  1704. } }
  1705. ----
  1706. Effects: :: As if: `a *= <<rot_quat,rot_quat>>(axis,angle)`.
  1707. '''
  1708. [[rotx_quat]]
  1709. ==== `rotx_quat`
  1710. .#include <boost/qvm/quat_operations.hpp>
  1711. [source,c++]
  1712. ----
  1713. namespace boost { namespace qvm {
  1714. template <class Angle>
  1715. -unspecified-return-type- rotx_quat( Angle const & angle );
  1716. } }
  1717. ----
  1718. Returns: :: A <<view_proxy,view proxy>> quaternion of unspecified type and scalar type `Angle`, which performs a rotation around the X axis at `angle` radians.
  1719. '''
  1720. [[quat_set_rotx]]
  1721. ==== `set_rotx`
  1722. .#include <boost/qvm/quat_operations.hpp>
  1723. [source,c++]
  1724. ----
  1725. namespace boost { namespace qvm {
  1726. //Only enabled if: is_quat<A>::value
  1727. template <class A>
  1728. void set_rotx( A & a, typename quat_traits<A>::scalar_type angle );
  1729. } }
  1730. ----
  1731. Effects: :: As if:
  1732. +
  1733. [source,c++]
  1734. ----
  1735. assign(
  1736. a,
  1737. rotx_quat(angle));
  1738. ----
  1739. '''
  1740. [[quat_rotate_x]]
  1741. ==== `rotate_x`
  1742. .#include <boost/qvm/quat_operations.hpp>
  1743. [source,c++]
  1744. ----
  1745. namespace boost { namespace qvm {
  1746. //Only enabled if: is_quat<A>::value
  1747. template <class A>
  1748. void rotate_x( A & a, typename quat_traits<A>::scalar_type angle );
  1749. } }
  1750. ----
  1751. Effects: :: As if: `a *= <<rotx_quat,rotx_quat>>(angle)`.
  1752. '''
  1753. [[roty_quat]]
  1754. ==== `roty_quat`
  1755. .#include <boost/qvm/quat_operations.hpp>
  1756. [source,c++]
  1757. ----
  1758. namespace boost { namespace qvm {
  1759. template <class Angle>
  1760. -unspecified-return-type- roty_quat( Angle const & angle );
  1761. } }
  1762. ----
  1763. Returns: :: A <<view_proxy,view proxy>> quaternion of unspecified type and scalar type `Angle`, which performs a rotation around the Y axis at `angle` radians.
  1764. '''
  1765. [[quat_set_roty]]
  1766. ==== `set_roty`
  1767. .#include <boost/qvm/quat_operations.hpp>
  1768. [source,c++]
  1769. ----
  1770. namespace boost { namespace qvm {
  1771. //Only enabled if: is_quat<A>::value
  1772. template <class A>
  1773. void set_rotz( A & a, typename quat_traits<A>::scalar_type angle );
  1774. } }
  1775. ----
  1776. Effects: :: As if:
  1777. +
  1778. [source,c++]
  1779. ----
  1780. assign(
  1781. a,
  1782. roty_quat(angle));
  1783. ----
  1784. '''
  1785. [[quat_rotate_y]]
  1786. ==== `rotate_y`
  1787. .#include <boost/qvm/quat_operations.hpp>
  1788. [source,c++]
  1789. ----
  1790. namespace boost { namespace qvm {
  1791. //Only enabled if: is_quat<A>::value
  1792. template <class A>
  1793. void rotate_y( A & a, typename quat_traits<A>::scalar_type angle );
  1794. } }
  1795. ----
  1796. Effects: :: As if: `a *= <<roty_quat,roty_quat>>(angle)`.
  1797. '''
  1798. [[rotz_quat]]
  1799. ==== `rotz_quat`
  1800. .#include <boost/qvm/quat_operations.hpp>
  1801. [source,c++]
  1802. ----
  1803. namespace boost { namespace qvm {
  1804. template <class Angle>
  1805. -unspecified-return-type- rotz_quat( Angle const & angle );
  1806. } }
  1807. ----
  1808. Returns: :: A <<view_proxy,view proxy>> quaternion of unspecified type and scalar type `Angle`, which performs a rotation around the Z axis at `angle` radians.
  1809. '''
  1810. [[quat_set_rotz]]
  1811. ==== `set_rotz`
  1812. .#include <boost/qvm/quat_operations.hpp>
  1813. [source,c++]
  1814. ----
  1815. namespace boost { namespace qvm {
  1816. //Only enabled if: is_quat<A>::value
  1817. template <class A>
  1818. void set_rotz( A & a, typename quat_traits<A>::scalar_type angle );
  1819. } }
  1820. ----
  1821. Effects: :: As if:
  1822. +
  1823. [source,c++]
  1824. ----
  1825. assign(
  1826. a,
  1827. rotz_quat(angle));
  1828. ----
  1829. '''
  1830. [[quat_rotate_z]]
  1831. ==== `rotate_z`
  1832. .#include <boost/qvm/quat_operations.hpp>
  1833. [source,c++]
  1834. ----
  1835. namespace boost { namespace qvm {
  1836. //Only enabled if: is_quat<A>::value
  1837. template <class A>
  1838. void rotate_z( A & a, typename quat_traits<A>::scalar_type angle );
  1839. } }
  1840. ----
  1841. Effects: :: As if: `a *= <<rotz_quat,rotz_quat>>(angle)`.
  1842. '''
  1843. [[quat_scalar_cast]]
  1844. ==== `scalar_cast`
  1845. .#include <boost/qvm/quat_operations.hpp>
  1846. [source,c++]
  1847. ----
  1848. namespace boost { namespace qvm {
  1849. //Only enabled if: is_quat<A>::value
  1850. template <class Scalar,class A>
  1851. -unspecified-return_type- scalar_cast( A const & a );
  1852. } }
  1853. ----
  1854. Returns: :: A read-only <<view_proxy,view proxy>> of `a` that looks like a quaternion of the same dimensions as `a`, but with <<quat_traits,`scalar_type`>> `Scalar` and elements constructed from the corresponding elements of `a`.
  1855. '''
  1856. [[qref]]
  1857. ==== `qref`
  1858. .#include <boost/qvm/quat_operations.hpp>
  1859. [source,c++]
  1860. ----
  1861. namespace boost { namespace qvm {
  1862. //Only enabled if: is_quat<A>::value
  1863. template <class A>
  1864. -unspecified-return-type- qref( A & a );
  1865. } }
  1866. ----
  1867. Returns: :: An identity view proxy of `a`; that is, it simply accesses the elements of `a`.
  1868. TIP: `qref` allows calling QVM operations when `a` is of built-in type, for example a plain old C array.
  1869. '''
  1870. === Vector Operations
  1871. [[vec_assign]]
  1872. ==== `assign`
  1873. .#include <boost/qvm/vec_operations.hpp>
  1874. [source,c++]
  1875. ----
  1876. namespace boost { namespace qvm {
  1877. //Only enabled if:
  1878. // is_vec<A>::value && is_vec<B>::value &&
  1879. // vec_traits<A>::dim==vec_traits<B>::dim
  1880. template <class A,class B>
  1881. A & assign( A & a, B const & b );
  1882. } }
  1883. ----
  1884. Effects: :: Copies all elements of the vector `b` to the vector `a`.
  1885. Returns: :: `a`.
  1886. '''
  1887. [[vec_convert_to]]
  1888. ==== `convert_to`
  1889. .#include <boost/qvm/vec_operations.hpp>
  1890. [source,c++]
  1891. ----
  1892. namespace boost { namespace qvm {
  1893. //Only enabled if:
  1894. // is_vec<R>::value && is_vec<A>::value &&
  1895. // vec_traits<R>::dim==vec_traits<A>::dim
  1896. template <class R,class A>
  1897. R convert_to( A const & a );
  1898. } }
  1899. ----
  1900. Requirements: :: `R` must be copyable.
  1901. Effects: :: As if: `R r; assign(r,a); return r;`
  1902. '''
  1903. [[vec_minus_eq]]
  1904. ==== `operator-=`
  1905. .#include <boost/qvm/vec_operations.hpp>
  1906. [source,c++]
  1907. ----
  1908. namespace boost { namespace qvm {
  1909. //Only enabled if:
  1910. // is_vec<A>::value && is_vec<B>::value &&
  1911. // vec_traits<A>::dim==vec_traits<B>::dim
  1912. template <class A,class B>
  1913. A & operator-=( A & a, B const & b );
  1914. } }
  1915. ----
  1916. Effects: :: Subtracts the elements of `b` from the corresponding elements of `a`.
  1917. Returns: :: `a`.
  1918. '''
  1919. [[vec_minus_unary]]
  1920. ==== `operator-` (unary)
  1921. operator-(vec)
  1922. .#include <boost/qvm/vec_operations.hpp>
  1923. [source,c++]
  1924. ----
  1925. namespace boost { namespace qvm {
  1926. //Only enabled if: is_vec<A>::value
  1927. template <class A>
  1928. typename deduce_vec<A>::type
  1929. operator-( A const & a );
  1930. } }
  1931. ----
  1932. Returns: :: A vector of the negated elements of `a`.
  1933. NOTE: The <<deduce_vec,`deduce_vec`>> template can be specialized to deduce the desired return type from the type `A`.
  1934. '''
  1935. [[vec_minus]]
  1936. ==== `operator-` (binary)
  1937. .#include <boost/qvm/vec_operations.hpp>
  1938. [source,c++]
  1939. ----
  1940. namespace boost { namespace qvm {
  1941. //Only enabled if:
  1942. // is_vec<A>::value && is_vec<B>::value &&
  1943. // vec_traits<A>::dim==vec_traits<B>::dim
  1944. template <class A,class B>
  1945. typename deduce_vec2<A,B,vec_traits<A>::dim>::type
  1946. operator-( A const & a, B const & b );
  1947. } }
  1948. ----
  1949. Returns: :: A vector of the same size as `a` and `b`, with elements the elements of `b` subtracted from the corresponding elements of `a`.
  1950. NOTE: The <<deduce_vec2,`deduce_vec2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  1951. '''
  1952. [[vec_plus_eq]]
  1953. ==== `operator+=`
  1954. .#include <boost/qvm/vec_operations.hpp>
  1955. [source,c++]
  1956. ----
  1957. namespace boost { namespace qvm {
  1958. //Only enabled if:
  1959. // is_vec<A>::value && is_vec<B>::value &&
  1960. // vec_traits<A>::dim==vec_traits<B>::dim
  1961. template <class A,class B>
  1962. A & operator+=( A & a, B const & b );
  1963. } }
  1964. ----
  1965. Effects: :: Adds the elements of `b` to the corresponding elements of `a`.
  1966. Returns: :: `a`.
  1967. '''
  1968. [[vec_plus]]
  1969. ==== `operator+`
  1970. .#include <boost/qvm/vec_operations.hpp>
  1971. [source,c++]
  1972. ----
  1973. namespace boost { namespace qvm {
  1974. //Only enabled if:
  1975. // is_vec<A>::value && is_vec<B>::value &&
  1976. // vec_traits<A>::dim==vec_traits<B>::dim
  1977. template <class A,class B>
  1978. typename deduce_vec2<A,B,vec_traits<A>::dim>::type
  1979. operator+( A const & a, B const & b );
  1980. } }
  1981. ----
  1982. Returns: :: A vector of the same size as `a` and `b`, with elements the elements of `b` added to the corresponding elements of `a`.
  1983. NOTE: The <<deduce_vec2,`deduce_vec2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  1984. '''
  1985. [[vec_div_eq_scalar]]
  1986. ==== `operator/=` (scalar)
  1987. .#include <boost/qvm/vec_operations.hpp>
  1988. [source,c++]
  1989. ----
  1990. namespace boost { namespace qvm {
  1991. //Only enabled if: is_vec<A>::value && is_scalar<B>::value
  1992. template <class A,class B>
  1993. A & operator/=( A & a, B b );
  1994. } }
  1995. ----
  1996. Effects: :: This operation divides a vector by a scalar.
  1997. Returns: :: `a`.
  1998. '''
  1999. [[vec_div_scalar]]
  2000. ==== `operator/`
  2001. .#include <boost/qvm/vec_operations.hpp>
  2002. [source,c++]
  2003. ----
  2004. namespace boost { namespace qvm {
  2005. //Only enabled if: is_vec<A>::value && is_scalar<B>::value
  2006. template <class A,class B>
  2007. typename deduce_vec<A>::type
  2008. operator/( A const & a, B b );
  2009. } }
  2010. ----
  2011. Returns: :: A vector that is the result of dividing the vector `a` by the scalar `b`.
  2012. NOTE: The <<deduce_vec,`deduce_vec`>> template can be specialized to deduce the desired return type from the type `A`.
  2013. '''
  2014. [[vec_mul_eq_scalar]]
  2015. ==== `operator*=`
  2016. .#include <boost/qvm/vec_operations.hpp>
  2017. [source,c++]
  2018. ----
  2019. namespace boost { namespace qvm {
  2020. //Only enabled if: is_vec<A>::value && is_scalar<B>::value
  2021. template <class A,class B>
  2022. A & operator*=( A & a, B b );
  2023. } }
  2024. ----
  2025. Effects: :: This operation multiplies the vector `a` by the scalar `b`.
  2026. Returns: :: `a`.
  2027. '''
  2028. [[vec_mul_scalar]]
  2029. ==== `operator*`
  2030. .#include <boost/qvm/vec_operations.hpp>
  2031. [source,c++]
  2032. ----
  2033. namespace boost { namespace qvm {
  2034. //Only enabled if: is_vec<A>::value && is_scalar<B>::value
  2035. template <class A>
  2036. typename deduce_vec<A>::type
  2037. operator*( A const & a, B b );
  2038. } }
  2039. ----
  2040. Returns: :: A vector that is the result of multiplying the vector `a` by the scalar `b`.
  2041. NOTE: The <<deduce_vec,`deduce_vec`>> template can be specialized to deduce the desired return type from the type `A`.
  2042. '''
  2043. [[vec_eq]]
  2044. ==== `operator==`
  2045. .#include <boost/qvm/vec_operations.hpp>
  2046. [source,c++]
  2047. ----
  2048. namespace boost { namespace qvm {
  2049. //Only enabled if:
  2050. // is_vec<A>::value && is_vec<B>::value &&
  2051. // vec_traits<A>::dim==vec_traits<B>::dim
  2052. template <class A,class B>
  2053. bool operator==( A const & a, B const & b );
  2054. } }
  2055. ----
  2056. Returns: :: `true` if each element of `a` compares equal to its corresponding element of `b`, `false` otherwise.
  2057. '''
  2058. [[vec_neq]]
  2059. ==== `operator!=`
  2060. .#include <boost/qvm/vec_operations.hpp>
  2061. [source,c++]
  2062. ----
  2063. namespace boost { namespace qvm {
  2064. //Only enabled if:
  2065. // is_vec<A>::value && is_vec<B>::value &&
  2066. // vec_traits<A>::dim==vec_traits<B>::dim
  2067. template <class A,class B>
  2068. bool operator!=( A const & a, B const & b );
  2069. } }
  2070. ----
  2071. Returns: :: `!(a == b)`.
  2072. '''
  2073. [[vec_cmp]]
  2074. ==== `cmp`
  2075. ----
  2076. .#include <boost/qvm/mat_operations.hpp>
  2077. namespace boost
  2078. {
  2079. namespace qvm
  2080. {
  2081. //Only enabled if:
  2082. // is_mat<A>::value && is_mat<B>::value &&
  2083. // mat_traits<A>::rows==mat_traits<B>::rows &&
  2084. // mat_traits<A>::cols==mat_traits<B>::cols
  2085. template <class A,class B,class Cmp>
  2086. bool cmp( A const & a, B const & b, Cmp pred );
  2087. } }
  2088. ----
  2089. Returns: :: Similar to <<vec_eq,`operator==`>>, except that the individual elements of `a` and `b` are passed to the binary predicate `pred` for comparison.
  2090. '''
  2091. [[vec_mag_sqr]]
  2092. ==== `mag_sqr`
  2093. .#include <boost/qvm/vec_operations.hpp>
  2094. [source,c++]
  2095. ----
  2096. namespace boost { namespace qvm {
  2097. //Only enabled if:
  2098. // is_vec<A>::value
  2099. template <class A>
  2100. typename vec_traits<A>::scalar_type
  2101. mag_sqr( A const & a );
  2102. } }
  2103. ----
  2104. Returns: :: The squared magnitude of the vector `a`.
  2105. '''
  2106. [[vec_mag]]
  2107. ==== `mag`
  2108. .#include <boost/qvm/vec_operations.hpp>
  2109. [source,c++]
  2110. ----
  2111. namespace boost { namespace qvm {
  2112. //Only enabled if:
  2113. // is_vec<A>::value
  2114. template <class A>
  2115. typename vec_traits<A>::scalar_type
  2116. mag( A const & a );
  2117. } }
  2118. ----
  2119. Returns: :: The magnitude of the vector `a`.
  2120. '''
  2121. [[vec_normalized]]
  2122. ==== `normalized`
  2123. .#include <boost/qvm/vec_operations.hpp>
  2124. [source,c++]
  2125. ----
  2126. namespace boost { namespace qvm {
  2127. //Only enabled if:
  2128. // is_vec<A>::value
  2129. template <class A>
  2130. typename deduce_vec<A>::type
  2131. normalized( A const & a );
  2132. } }
  2133. ----
  2134. Effects: :: As if:
  2135. +
  2136. [source,c++]
  2137. ----
  2138. typename deduce_vec<A>::type tmp;
  2139. assign(tmp,a);
  2140. normalize(tmp);
  2141. return tmp;
  2142. ----
  2143. NOTE: The <<deduce_vec,`deduce_vec`>> template can be specialized to deduce the desired return type from the type `A`.
  2144. '''
  2145. [[vec_normalize]]
  2146. ==== `normalize`
  2147. .#include <boost/qvm/vec_operations.hpp>
  2148. [source,c++]
  2149. ----
  2150. namespace boost { namespace qvm {
  2151. //Only enabled if:
  2152. // is_vec<A>::value
  2153. template <class A>
  2154. void normalize( A & a );
  2155. } }
  2156. ----
  2157. Effects: :: Normalizes `a`.
  2158. Postcondition:
  2159. `mag(a)==<<scalar_traits,scalar_traits>><typename <<vec_traits,vec_traits<A>::scalar_type>>>::value(1)`.
  2160. Throws: :: If the magnitude of `a` is zero, throws <<zero_magnitude_error,`zero_magnitude_error`>>.
  2161. '''
  2162. [[vec_dot]]
  2163. ==== `dot`
  2164. .#include <boost/qvm/vec_operations.hpp>
  2165. [source,c++]
  2166. ----
  2167. namespace boost { namespace qvm {
  2168. //Only enabled if:
  2169. // is_vec<A>::value && is_vec<B>::value &&
  2170. // vec_traits<A>::dim==vec_traits<B>::dim
  2171. template <class A,class B>
  2172. typename deduce_scalar<A,B>::type
  2173. dot( A const & a, B const & b );
  2174. } }
  2175. ----
  2176. Returns: :: The dot product of the vectors `a` and `b`.
  2177. NOTE: The <<deduce_scalar,`deduce_scalar`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  2178. '''
  2179. [[vec_cross]]
  2180. ==== `cross`
  2181. .#include <boost/qvm/vec_operations.hpp>
  2182. [source,c++]
  2183. ----
  2184. namespace boost { namespace qvm {
  2185. //Only enabled if:
  2186. // is_vec<A>::value && is_vec<B>::value &&
  2187. // vec_traits<A>::dim==3 && vec_traits<B>::dim==3
  2188. template <class A,class B>
  2189. typename deduce_vec2<A,B,3>::type
  2190. cross( A const & a, B const & b );
  2191. } }
  2192. ----
  2193. Returns: :: The cross product of the vectors `a` and `b`.
  2194. NOTE: The <<deduce_vec2,`deduce_vec2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  2195. '''
  2196. [[zero_vec]]
  2197. ==== `zero_vec`
  2198. .#include <boost/qvm/vec_operations.hpp>
  2199. [source,c++]
  2200. ----
  2201. namespace boost { namespace qvm {
  2202. template <class T,int S>
  2203. -unspecified-return-type- zero_vec();
  2204. } }
  2205. ----
  2206. Returns: :: A read-only vector of unspecified type with <<vec_traits,`scalar_type`>> `T` and size `S`, with all elements equal to <<scalar_traits,`scalar_traits<T>::value(0)`>>.
  2207. '''
  2208. [[vec_set_zero]]
  2209. ==== `set_zero`
  2210. .#include <boost/qvm/vec_operations.hpp>
  2211. [source,c++]
  2212. ----
  2213. namespace boost { namespace qvm {
  2214. //Only enabled if:
  2215. // is_vec<A>::value
  2216. template <class A>
  2217. void set_zero( A & a );
  2218. } }
  2219. ----
  2220. Effects: :: As if:
  2221. +
  2222. [source,c++]
  2223. ----
  2224. assign(a,
  2225. zero_vec<
  2226. typename vec_traits<A>::scalar_type,
  2227. vec_traits<A>::dim>());
  2228. ----
  2229. '''
  2230. [[vec_scalar_cast]]
  2231. ==== `scalar_cast`
  2232. .#include <boost/qvm/vec_operations.hpp>
  2233. [source,c++]
  2234. ----
  2235. namespace boost { namespace qvm {
  2236. //Only enabled if: is_vec<A>::value
  2237. template <class Scalar,class A>
  2238. -unspecified-return_type- scalar_cast( A const & a );
  2239. } }
  2240. ----
  2241. Returns: :: A read-only <<view_proxy,view proxy>> of `a` that looks like a vector of the same dimensions as `a`, but with <<vec_traits,`scalar_type`>> `Scalar` and elements constructed from the corresponding elements of `a`.
  2242. '''
  2243. [[vref]]
  2244. ==== `vref`
  2245. .#include <boost/qvm/vec_operations.hpp>
  2246. [source,c++]
  2247. ----
  2248. namespace boost { namespace qvm {
  2249. //Only enabled if: is_vec<A>::value
  2250. template <class A>
  2251. -unspecified-return-type- vref( A & a );
  2252. } }
  2253. ----
  2254. Returns: :: An identity <<view_proxy,view proxy>> of `a`; that is, it simply accesses the elements of `a`.
  2255. TIP: `vref` allows calling QVM operations when `a` is of built-in type, for example a plain old C array.
  2256. '''
  2257. === Matrix Operations
  2258. [[mat_assign]]
  2259. ==== `assign`
  2260. .#include <boost/qvm/mat_operations.hpp>
  2261. [source,c++]
  2262. ----
  2263. namespace boost { namespace qvm {
  2264. //Only enabled if:
  2265. // is_mat<A>::value && is_mat<B>::value &&
  2266. // mat_traits<A>::rows==mat_traits<B>::rows &&
  2267. // mat_traits<A>::cols==mat_traits<B>::cols
  2268. template <class A,class B>
  2269. A & assign( A & a, B const & b );
  2270. } }
  2271. ----
  2272. Effects: :: Copies all elements of the matrix `b` to the matrix `a`.
  2273. Returns: :: `a`.
  2274. '''
  2275. [[mat_convert_to]]
  2276. ==== `convert_to`
  2277. .#include <boost/qvm/mat_operations.hpp>
  2278. [source,c++]
  2279. ----
  2280. namespace boost { namespace qvm {
  2281. //Only enabled if:
  2282. // is_mat<R>::value && is_mat<A>::value &&
  2283. // mat_traits<R>::rows==mat_traits<A>::rows &&
  2284. // mat_traits<R>::cols==mat_traits<A>::cols
  2285. template <class R,class A>
  2286. R convert_to( A const & a );
  2287. } }
  2288. ----
  2289. Requirements: :: `R` must be copyable.
  2290. Effects:
  2291. As if: `R r; <<mat_assign,assign>>(r,a); return r;`
  2292. '''
  2293. [[mat_minus_eq_scalar]]
  2294. ==== `operator-=`
  2295. .#include <boost/qvm/mat_operations.hpp>
  2296. [source,c++]
  2297. ----
  2298. namespace boost { namespace qvm {
  2299. //Only enabled if:
  2300. // is_mat<A>::value && is_mat<B>::value &&
  2301. // mat_traits<A>::rows==mat_traits<B>::rows &&
  2302. // mat_traits<A>::cols==mat_traits<B>::cols
  2303. template <class A,class B>
  2304. A & operator-=( A & a, B const & b );
  2305. } }
  2306. ----
  2307. Effects: :: Subtracts the elements of `b` from the corresponding elements of `a`.
  2308. Returns: :: `a`.
  2309. '''
  2310. [[mat_minus_unary]]
  2311. ==== `operator-` (unary)
  2312. .#include <boost/qvm/mat_operations.hpp>
  2313. [source,c++]
  2314. ----
  2315. namespace boost { namespace qvm {
  2316. //Only enabled if: is_mat<A>::value
  2317. template <class A>
  2318. typename deduce_mat<A>::type
  2319. operator-( A const & a );
  2320. } }
  2321. ----
  2322. Returns: :: A matrix of the negated elements of `a`.
  2323. NOTE: The <<deduce_mat,`deduce_mat`>> template can be specialized to deduce the desired return type from the type `A`.
  2324. '''
  2325. [[mat_minus]]
  2326. ==== `operator-`
  2327. .#include <boost/qvm/mat_operations.hpp>
  2328. [source,c++]
  2329. ----
  2330. namespace boost { namespace qvm {
  2331. //Only enabled if:
  2332. // is_mat<A>::value && is_mat<B>::value &&
  2333. // mat_traits<A>::rows==mat_traits<B>::rows &&
  2334. // mat_traits<A>::cols==mat_traits<B>::cols
  2335. template <class A,class B>
  2336. typename deduce_mat2<A,B,mat_traits<A>::rows,mat_traits<A>::cols>::type
  2337. operator-( A const & a, B const & b );
  2338. } }
  2339. ----
  2340. Returns: :: A matrix of the same size as `a` and `b`, with elements the elements of `b` subtracted from the corresponding elements of `a`.
  2341. NOTE: The <<deduce_mat2,`deduce_mat2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  2342. '''
  2343. [[mat_plus_eq_scalar]]
  2344. ==== `operator+=`
  2345. .#include <boost/qvm/mat_operations.hpp>
  2346. [source,c++]
  2347. ----
  2348. namespace boost { namespace qvm {
  2349. //Only enabled if:
  2350. // is_mat<A>::value && is_mat<B>::value &&
  2351. // mat_traits<A>::rows==mat_traits<B>::rows &&
  2352. // mat_traits<A>::cols==mat_traits<B>::cols
  2353. template <class A,class B>
  2354. A & operator+=( A & a, B const & b );
  2355. } }
  2356. ----
  2357. Effects: :: Adds the elements of `b` to the corresponding elements of `a`.
  2358. Returns: :: `a`.
  2359. '''
  2360. [[mat_plus]]
  2361. ==== `operator+`
  2362. .#include <boost/qvm/mat_operations.hpp>
  2363. [source,c++]
  2364. ----
  2365. namespace boost { namespace qvm {
  2366. //Only enabled if:
  2367. // is_mat<A>::value && is_mat<B>::value &&
  2368. // mat_traits<A>::rows==mat_traits<B>::rows &&
  2369. // mat_traits<A>::cols==mat_traits<B>::cols
  2370. template <class A,class B>
  2371. typename deduce_mat2<A,B,mat_traits<A>::rows,mat_traits<A>::cols>::type
  2372. operator+( A const & a, B const & b );
  2373. } }
  2374. ----
  2375. Returns: :: A matrix of the same size as `a` and `b`, with elements the elements of `b` added to the corresponding elements of `a`.
  2376. NOTE: The <<deduce_mat2,`deduce_mat2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  2377. '''
  2378. [[mat_div_eq_scalar]]
  2379. ==== `operator/=` (scalar)
  2380. .#include <boost/qvm/mat_operations.hpp>
  2381. [source,c++]
  2382. ----
  2383. namespace boost { namespace qvm {
  2384. //Only enabled if: is_mat<A>::value && is_scalar<B>::value
  2385. template <class A,class B>
  2386. A & operator/=( A & a, B b );
  2387. } }
  2388. ----
  2389. Effects: :: This operation divides a matrix by a scalar.
  2390. Returns: :: `a`.
  2391. '''
  2392. [[mat_div_scalar]]
  2393. ==== `operator/` (scalar)
  2394. .#include <boost/qvm/mat_operations.hpp>
  2395. [source,c++]
  2396. ----
  2397. namespace boost { namespace qvm {
  2398. //Only enabled if: is_mat<A>::value && is_scalar<B>::value
  2399. template <class A,class B>
  2400. typename deduce_mat<A>::type
  2401. operator/( A const & a, B b );
  2402. } }
  2403. ----
  2404. Returns: :: A matrix that is the result of dividing the matrix `a` by the scalar `b`.
  2405. NOTE: The <<deduce_mat,`deduce_mat`>> template can be specialized to deduce the desired return type from the type `A`.
  2406. '''
  2407. [[mat_mul_eq]]
  2408. ==== `operator*=`
  2409. .#include <boost/qvm/mat_operations.hpp>
  2410. [source,c++]
  2411. ----
  2412. namespace boost { namespace qvm {
  2413. //Only enabled if:
  2414. // is_mat<A>::value && is_mat<B>::value &&
  2415. // mat_traits<A>::rows==mat_traits<A>::cols &&
  2416. // mat_traits<A>::rows==mat_traits<B>::rows &&
  2417. // mat_traits<A>::cols==mat_traits<B>::cols
  2418. template <class A,class B>
  2419. A & operator*=( A & a, B const & b );
  2420. } }
  2421. ----
  2422. Effects: :: As if:
  2423. +
  2424. [source,c++]
  2425. ----
  2426. A tmp(a);
  2427. a = tmp * b;
  2428. return a;
  2429. ----
  2430. '''
  2431. [[mat_mul_eq_scalar]]
  2432. ==== `operator*=` (scalar)
  2433. .#include <boost/qvm/mat_operations.hpp>
  2434. [source,c++]
  2435. ----
  2436. namespace boost { namespace qvm {
  2437. //Only enabled if: is_mat<A>::value && is_scalar<B>::value
  2438. template <class A,class B>
  2439. A & operator*=( A & a, B b );
  2440. } }
  2441. ----
  2442. Effects: :: This operation multiplies the matrix `a` matrix by the scalar `b`.
  2443. Returns: :: `a`.
  2444. '''
  2445. [[mat_mul]]
  2446. ==== `operator*`
  2447. .#include <boost/qvm/mat_operations.hpp>
  2448. [source,c++]
  2449. ----
  2450. namespace boost { namespace qvm {
  2451. //Only enabled if:
  2452. // is_mat<A>::value && is_mat<B>::value &&
  2453. // mat_traits<A>::cols==mat_traits<B>::rows
  2454. template <class A,class B>
  2455. typename deduce_mat2<A,B,mat_traits<A>::rows,mat_traits<B>::cols>::type
  2456. operator*( A const & a, B const & b );
  2457. } }
  2458. ----
  2459. Returns: :: The result of https://en.wikipedia.org/wiki/Matrix_multiplication[multiplying] the matrices `a` and `b`.
  2460. NOTE: The <<deduce_mat2,`deduce_mat2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  2461. '''
  2462. [[mat_mul_scalar]]
  2463. ==== `operator*` (scalar)
  2464. .#include <boost/qvm/mat_operations.hpp>
  2465. [source,c++]
  2466. ----
  2467. namespace boost { namespace qvm {
  2468. //Only enabled if: is_mat<A>::value && is_scalar<B>::value
  2469. template <class A,class B>
  2470. typename deduce_mat<A>::type
  2471. operator*( A const & a, B b );
  2472. //Only enabled if: is_scalar<B>::value && is_mat<A>::value
  2473. template <class B,class A>
  2474. typename deduce_mat<A>::type
  2475. operator*( B b, A const & a );
  2476. } }
  2477. ----
  2478. Returns: :: A matrix that is the result of multiplying the matrix `a` by the scalar `b`.
  2479. NOTE: The <<deduce_mat,`deduce_mat`>> template can be specialized to deduce the desired return type from the type `A`.
  2480. '''
  2481. [[mat_eq]]
  2482. ==== `operator==`
  2483. .#include <boost/qvm/mat_operations.hpp>
  2484. [source,c++]
  2485. ----
  2486. namespace boost { namespace qvm {
  2487. //Only enabled if:
  2488. // is_mat<A>::value && is_mat<B>::value &&
  2489. // mat_traits<A>::rows==mat_traits<B>::rows &&
  2490. // mat_traits<A>::cols==mat_traits<B>::cols
  2491. template <class A,class B>
  2492. bool operator==( A const & a, B const & b );
  2493. } }
  2494. ----
  2495. Returns: :: `true` if each element of `a` compares equal to its corresponding element of `b`, `false` otherwise.
  2496. '''
  2497. [[mat_neq]]
  2498. ==== `operator!=`
  2499. .#include <boost/qvm/mat_operations.hpp>
  2500. [source,c++]
  2501. ----
  2502. namespace boost { namespace qvm {
  2503. //Only enabled if:
  2504. // is_mat<A>::value && is_mat<B>::value &&
  2505. // mat_traits<A>::rows==mat_traits<B>::rows &&
  2506. // mat_traits<A>::cols==mat_traits<B>::cols
  2507. template <class A,class B>
  2508. bool operator!=( A const & a, B const & b );
  2509. } }
  2510. ----
  2511. Returns: :: `!( a <<mat_eq,=\=>> b )`.
  2512. '''
  2513. [[mat_cmp]]
  2514. ==== `cmp`
  2515. .#include <boost/qvm/mat_operations.hpp>
  2516. [source,c++]
  2517. ----
  2518. namespace boost { namespace qvm {
  2519. //Only enabled if:
  2520. // is_mat<A>::value && is_mat<B>::value &&
  2521. // mat_traits<A>::rows==mat_traits<B>::rows &&
  2522. // mat_traits<A>::cols==mat_traits<B>::cols
  2523. template <class A,class B,class Cmp>
  2524. bool cmp( A const & a, B const & b, Cmp pred );
  2525. } }
  2526. ----
  2527. Returns: :: Similar to <<mat_eq,`operator==`>>, except that the individual elements of `a` and `b` are passed to the binary predicate `pred` for comparison.
  2528. '''
  2529. [[mat_inverse]]
  2530. ==== `inverse`
  2531. .#include <boost/qvm/mat_operations.hpp>
  2532. [source,c++]
  2533. ----
  2534. namespace boost { namespace qvm {
  2535. //Only enabled if:
  2536. // is_mat<A>::value && is_scalar<B>::value
  2537. // mat_traits<A>::rows==mat_traits<A>::cols
  2538. template <class A,class B>
  2539. typename deduce_mat<A>::type
  2540. inverse( A const & a, B det );
  2541. template <class A>
  2542. typename deduce_mat<A>::type
  2543. inverse( A const & a );
  2544. } }
  2545. ----
  2546. Preconditions: :: `det!=<<scalar_traits,scalar_traits>><typename <<mat_traits,mat_traits<A>::scalar_type>>>::value(0)`
  2547. Returns: :: Both overloads compute the inverse of `a`. The first overload takes the pre-computed determinant of `a`.
  2548. Throws: :: The second overload computes the determinant automatically and throws <<zero_determinant_error,`zero_determinant_error`>> if the computed determinant is zero.
  2549. NOTE: The <<deduce_mat,`deduce_mat`>> template can be specialized to deduce the desired return type from the type `A`.
  2550. '''
  2551. [[zero_mat]]
  2552. ==== `zero_mat`
  2553. .#include <boost/qvm/mat_operations.hpp>
  2554. [source,c++]
  2555. ----
  2556. namespace boost { namespace qvm {
  2557. template <class T,int D>
  2558. -unspecified-return-type- zero_mat();
  2559. template <class T,int R,int C>
  2560. -unspecified-return-type- zero_mat();
  2561. } }
  2562. ----
  2563. Returns: :: A read-only matrix of unspecified type with <<mat_traits,`scalar_type`>> `T`, `R` rows and `C` columns (or `D` rows and `D` columns), with all elements equal to <<scalar_traits,`scalar_traits<T>::value(0)`>>.
  2564. '''
  2565. [[mat_set_zero]]
  2566. ==== `set_zero`
  2567. .#include <boost/qvm/mat_operations.hpp>
  2568. [source,c++]
  2569. ----
  2570. namespace boost { namespace qvm {
  2571. //Only enabled if:
  2572. // is_mat<A>::value
  2573. template <class A>
  2574. void set_zero( A & a );
  2575. } }
  2576. ----
  2577. Effects: :: As if:
  2578. +
  2579. [source,c++]
  2580. ----
  2581. assign(a,
  2582. zero_mat<
  2583. typename mat_traits<A>::scalar_type,
  2584. mat_traits<A>::rows,
  2585. mat_traits<A>::cols>());
  2586. ----
  2587. '''
  2588. [[identity_mat]]
  2589. ==== `identity_mat`
  2590. .#include <boost/qvm/mat_operations.hpp>
  2591. ----
  2592. namespace boost { namespace qvm {
  2593. template <class S,int D>
  2594. -unspecified-return-type- identity_mat();
  2595. } }
  2596. ----
  2597. Returns: :: An identity matrix of size `D` x `D` and scalar type `S`.
  2598. '''
  2599. [[mat_set_identity]]
  2600. ==== `set_identity`
  2601. .#include <boost/qvm/mat_operations.hpp>
  2602. [source,c++]
  2603. ----
  2604. namespace boost { namespace qvm {
  2605. //Only enabled if:
  2606. // is_mat<A>::value &&
  2607. // mat_traits<A>::cols==mat_traits<A>::rows
  2608. template <class A>
  2609. void set_identity( A & a );
  2610. } }
  2611. ----
  2612. Effects: :: As if:
  2613. +
  2614. [source,c++]
  2615. ----
  2616. assign(
  2617. a,
  2618. identity_mat<
  2619. typename mat_traits<A>::scalar_type,
  2620. mat_traits<A>::rows,
  2621. mat_traits<A>::cols>());
  2622. ----
  2623. '''
  2624. [[rot_mat]]
  2625. ==== `rot_mat` / Euler angles
  2626. .#include <boost/qvm/mat_operations.hpp>
  2627. [source,c++]
  2628. ----
  2629. namespace boost { namespace qvm {
  2630. //Only enabled if:
  2631. // is_vec<A>::value && vec_traits<A>::dim==3
  2632. template <int Dim,class A,class Angle>
  2633. -unspecified-return-type-
  2634. rot_mat( A const & axis, Angle angle );
  2635. template <int Dim,class Angle>
  2636. -unspecified-return-type-
  2637. rot_mat_xzy( Angle x1, Angle z2, Angle y3 );
  2638. template <int Dim,class Angle>
  2639. -unspecified-return-type-
  2640. rot_mat_xyz( Angle x1, Angle y2, Angle z3 );
  2641. template <int Dim,class Angle>
  2642. -unspecified-return-type-
  2643. rot_mat_yxz( Angle y1, Angle x2, Angle z3 );
  2644. template <int Dim,class Angle>
  2645. -unspecified-return-type-
  2646. rot_mat_yzx( Angle y1, Angle z2, Angle x3 );
  2647. template <int Dim,class Angle>
  2648. -unspecified-return-type-
  2649. rot_mat_zyx( Angle z1, Angle y2, Angle x3 );
  2650. template <int Dim,class Angle>
  2651. -unspecified-return-type-
  2652. rot_mat_zxy( Angle z1, Angle x2, Angle y3 );
  2653. template <int Dim,class Angle>
  2654. -unspecified-return-type-
  2655. rot_mat_xzx( Angle x1, Angle z2, Angle x3 );
  2656. template <int Dim,class Angle>
  2657. -unspecified-return-type-
  2658. rot_mat_xyx( Angle x1, Angle y2, Angle x3 );
  2659. template <int Dim,class Angle>
  2660. -unspecified-return-type-
  2661. rot_mat_yxy( Angle y1, Angle x2, Angle y3 );
  2662. template <int Dim,class Angle>
  2663. -unspecified-return-type-
  2664. rot_mat_yzy( Angle y1, Angle z2, Angle y3 );
  2665. template <int Dim,class Angle>
  2666. -unspecified-return-type-
  2667. rot_mat_zyz( Angle z1, Angle y2, Angle z3 );
  2668. template <int Dim,class Angle>
  2669. -unspecified-return-type-
  2670. rot_mat_zxz( Angle z1, Angle y2, Angle z3 );
  2671. } }
  2672. ----
  2673. Returns: :: A matrix of unspecified type, of `Dim` rows and `Dim` columns parameter, which performs a rotation around the `axis` at `angle` radians, or Tait–Bryan angles (x-y-z, y-z-x, z-x-y, x-z-y, z-y-x, y-x-z), or proper Euler angles (z-x-z, x-y-x, y-z-y, z-y-z, x-z-x, y-x-y). See https://en.wikipedia.org/wiki/Euler_angles[Euler angles].
  2674. Throws: :: In case the axis vector has zero magnitude, throws <<zero_magnitude_error,`zero_magnitude_error`>>.
  2675. NOTE: These functions are not view proxies; they return a temp object.
  2676. '''
  2677. [[mat_set_rot]]
  2678. ==== `set_rot` / Euler angles
  2679. .#include <boost/qvm/mat_operations.hpp>
  2680. [source,c++]
  2681. ----
  2682. namespace boost { namespace qvm {
  2683. //Only enabled if:
  2684. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2685. // mat_traits<A>::rows==mat_traits<A>::cols &&
  2686. // is_vec<B>::value && vec_traits<B>::dim==3
  2687. template <class A>
  2688. void set_rot( A & a, B const & axis, typename vec_traits<B>::scalar_type angle );
  2689. //Only enabled if:
  2690. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2691. // mat_traits<A>::rows==mat_traits<A>::cols
  2692. template <class A,class Angle>
  2693. void set_rot_xzy( A & a, Angle x1, Angle z2, Angle y3 );
  2694. //Only enabled if:
  2695. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2696. // mat_traits<A>::rows==mat_traits<A>::cols
  2697. template <class A,class Angle>
  2698. void set_rot_xyz( A & a, Angle x1, Angle y2, Angle z3 );
  2699. //Only enabled if:
  2700. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2701. // mat_traits<A>::rows==mat_traits<A>::cols
  2702. template <class A,class Angle>
  2703. void set_rot_yxz( A & a, Angle y1, Angle x2, Angle z3 );
  2704. //Only enabled if:
  2705. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2706. // mat_traits<A>::rows==mat_traits<A>::cols
  2707. template <class A,class Angle>
  2708. void set_rot_yzx( A & a, Angle y1, Angle z2, Angle x3 );
  2709. //Only enabled if:
  2710. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2711. // mat_traits<A>::rows==mat_traits<A>::cols
  2712. template <class A,class Angle>
  2713. void set_rot_zyx( A & a, Angle z1, Angle y2, Angle x3 );
  2714. //Only enabled if:
  2715. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2716. // mat_traits<A>::rows==mat_traits<A>::cols
  2717. template <class A,class Angle>
  2718. void set_rot_zxy( A & a, Angle z1, Angle x2, Angle y3 );
  2719. //Only enabled if:
  2720. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2721. // mat_traits<A>::rows==mat_traits<A>::cols
  2722. template <class A,class Angle>
  2723. void set_rot_xzx( A & a, Angle x1, Angle z2, Angle x3 );
  2724. //Only enabled if:
  2725. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2726. // mat_traits<A>::rows==mat_traits<A>::cols
  2727. template <class A,class Angle>
  2728. void set_rot_xyx( A & a, Angle x1, Angle y2, Angle x3 );
  2729. //Only enabled if:
  2730. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2731. // mat_traits<A>::rows==mat_traits<A>::cols
  2732. template <class A,class Angle>
  2733. void set_rot_yxy( A & a, Angle y1, Angle x2, Angle y3 );
  2734. //Only enabled if:
  2735. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2736. // mat_traits<A>::rows==mat_traits<A>::cols
  2737. template <class A,class Angle>
  2738. void set_rot_yzy( A & a, Angle y1, Angle z2, Angle y3 );
  2739. //Only enabled if:
  2740. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2741. // mat_traits<A>::rows==mat_traits<A>::cols
  2742. template <class A,class Angle>
  2743. void set_rot_zyz( A & a, Angle z1, Angle y2, Angle z3 );
  2744. //Only enabled if:
  2745. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2746. // mat_traits<A>::rows==mat_traits<A>::cols
  2747. template <class A,class Angle>
  2748. void set_rot_zxz( A & a, Angle z1, Angle x2, Angle z3 );
  2749. //Only enabled if:
  2750. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2751. // mat_traits<A>::rows==mat_traits<A>::cols
  2752. template <class A,class Angle>
  2753. void set_rot_xzy( A & a, Angle x1, Angle z2, Angle y3 );
  2754. } }
  2755. ----
  2756. Effects: :: Assigns the return value of the corresponding <<rot_mat,`rot_mat`>> function to `a`.
  2757. '''
  2758. [[mat_rotate]]
  2759. ==== `rotate` / Euler angles
  2760. .#include <boost/qvm/mat_operations.hpp>
  2761. [source,c++]
  2762. ----
  2763. namespace boost { namespace qvm {
  2764. //Only enabled if:
  2765. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2766. // mat_traits<A>::rows==mat_traits<A>::cols &&
  2767. // is_vec<B>::value && vec_traits<B>::dim==3
  2768. template <class A,class B>
  2769. void rotate( A & a, B const & axis, typename mat_traits<A>::scalar_type angle );
  2770. //Only enabled if:
  2771. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2772. // mat_traits<A>::rows==mat_traits<A>::cols
  2773. template <class A,class Angle>
  2774. void rotate_xzy( A & a, Angle x1, Angle z2, Angle y3 );
  2775. //Only enabled if:
  2776. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2777. // mat_traits<A>::rows==mat_traits<A>::cols
  2778. template <class A,class Angle>
  2779. void rotate_xyz( A & a, Angle x1, Angle y2, Angle z3 );
  2780. //Only enabled if:
  2781. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2782. // mat_traits<A>::rows==mat_traits<A>::cols
  2783. template <class A,class Angle>
  2784. void rotate_yxz( A & a, Angle y1, Angle x2, Angle z3 );
  2785. //Only enabled if:
  2786. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2787. // mat_traits<A>::rows==mat_traits<A>::cols
  2788. template <class A,class Angle>
  2789. void rotate_yzx( A & a, Angle y1, Angle z2, Angle x3 );
  2790. //Only enabled if:
  2791. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2792. // mat_traits<A>::rows==mat_traits<A>::cols
  2793. template <class A,class Angle>
  2794. void rotate_zyx( A & a, Angle z1, Angle y2, Angle x3 );
  2795. //Only enabled if:
  2796. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2797. // mat_traits<A>::rows==mat_traits<A>::cols
  2798. template <class A,class Angle>
  2799. void rotate_zxy( A & a, Angle z1, Angle x2, Angle y3 );
  2800. //Only enabled if:
  2801. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2802. // mat_traits<A>::rows==mat_traits<A>::cols
  2803. template <class A,class Angle>
  2804. void rotate_xzx( A & a, Angle x1, Angle z2, Angle x3 );
  2805. //Only enabled if:
  2806. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2807. // mat_traits<A>::rows==mat_traits<A>::cols
  2808. template <class A,class Angle>
  2809. void rotate_xyx( A & a, Angle x1, Angle y2, Angle x3 );
  2810. //Only enabled if:
  2811. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2812. // mat_traits<A>::rows==mat_traits<A>::cols
  2813. template <class A,class Angle>
  2814. void rotate_yxy( A & a, Angle y1, Angle x2, Angle y3 );
  2815. //Only enabled if:
  2816. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2817. // mat_traits<A>::rows==mat_traits<A>::cols
  2818. template <class A,class Angle>
  2819. void rotate_yzy( A & a, Angle y1, Angle z2, Angle y3 );
  2820. //Only enabled if:
  2821. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2822. // mat_traits<A>::rows==mat_traits<A>::cols
  2823. template <class A,class Angle>
  2824. void rotate_zyz( A & a, Angle z1, Angle y2, Angle z3 );
  2825. //Only enabled if:
  2826. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2827. // mat_traits<A>::rows==mat_traits<A>::cols
  2828. template <class A,class Angle>
  2829. void rotate_zxz( A & a, Angle z1, Angle x2, Angle z3 );
  2830. } }
  2831. ----
  2832. Effects: :: Multiplies the matrix `a` in-place by the return value of the corresponding <<rot_mat,`rot_mat`>> function.
  2833. '''
  2834. [[rotx_mat]]
  2835. ==== `rotx_mat`
  2836. .#include <boost/qvm/mat_operations.hpp>
  2837. [source,c++]
  2838. ----
  2839. namespace boost { namespace qvm {
  2840. template <int Dim,class Angle>
  2841. -unspecified-return-type- rotx_mat( Angle const & angle );
  2842. } }
  2843. ----
  2844. Returns: :: A <<view_proxy,view proxy>> matrix of unspecified type, of `Dim` rows and `Dim` columns and scalar type `Angle`, which performs a rotation around the `X` axis at `angle` radians.
  2845. '''
  2846. [[mat_set_rotx]]
  2847. ==== `set_rotx`
  2848. .#include <boost/qvm/mat_operations.hpp>
  2849. [source,c++]
  2850. ----
  2851. namespace boost { namespace qvm {
  2852. //Only enabled if:
  2853. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2854. // mat_traits<A>::rows==mat_traits<A>::cols
  2855. template <class A>
  2856. void set_rotx( A & a, typename mat_traits<A>::scalar_type angle );
  2857. } }
  2858. ----
  2859. Effects: :: As if:
  2860. +
  2861. [source,c++]
  2862. ----
  2863. assign(
  2864. a,
  2865. rotx_mat<mat_traits<A>::rows>(angle));
  2866. ----
  2867. '''
  2868. [[mat_rotate_x]]
  2869. ==== `rotate_x`
  2870. .#include <boost/qvm/mat_operations.hpp>
  2871. [source,c++]
  2872. ----
  2873. namespace boost { namespace qvm {
  2874. //Only enabled if:
  2875. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2876. // mat_traits<A>::rows==mat_traits<A>::cols
  2877. template <class A>
  2878. void rotate_x( A & a, typename mat_traits<A>::scalar_type angle );
  2879. } }
  2880. ----
  2881. Effects: :: As if: `a <<mat_mul_eq,*\=>> <<rotx_mat,rotx_mat>><<<mat_traits,mat_traits<A>::rows>>>(angle)`.
  2882. '''
  2883. [[roty_mat]]
  2884. ==== `roty_mat`
  2885. .#include <boost/qvm/mat_operations.hpp>
  2886. [source,c++]
  2887. ----
  2888. namespace boost { namespace qvm {
  2889. template <int Dim,class Angle>
  2890. -unspecified-return-type- roty_mat( Angle const & angle );
  2891. } }
  2892. ----
  2893. Returns: :: A <<view_proxy,view proxy>> matrix of unspecified type, of `Dim` rows and `Dim` columns and scalar type `Angle`, which performs a rotation around the `Y` axis at `angle` radians.
  2894. '''
  2895. [[mat_set_roty]]
  2896. ==== `set_roty`
  2897. .#include <boost/qvm/mat_operations.hpp>
  2898. [source,c++]
  2899. ----
  2900. namespace boost { namespace qvm {
  2901. //Only enabled if:
  2902. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2903. // mat_traits<A>::rows==mat_traits<A>::cols
  2904. template <class A>
  2905. void set_roty( A & a, typename mat_traits<A>::scalar_type angle );
  2906. } }
  2907. ----
  2908. Effects: :: As if:
  2909. +
  2910. [source,c++]
  2911. ----
  2912. assign(
  2913. a,
  2914. roty_mat<mat_traits<A>::rows>(angle));
  2915. ----
  2916. '''
  2917. [[mat_rotate_y]]
  2918. ==== `rotate_y`
  2919. .#include <boost/qvm/mat_operations.hpp>
  2920. [source,c++]
  2921. ----
  2922. namespace boost { namespace qvm {
  2923. //Only enabled if:
  2924. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2925. // mat_traits<A>::rows==mat_traits<A>::cols
  2926. template <class A>
  2927. void rotate_y( A & a, typename mat_traits<A>::scalar_type angle );
  2928. } }
  2929. ----
  2930. Effects: :: As if: `a <<mat_mul_eq,*\=>> <<roty_mat,roty_mat>><<<mat_traits,mat_traits<A>::rows>>>(angle)`.
  2931. '''
  2932. [[rotz_mat]]
  2933. ==== `rotz_mat`
  2934. .#include <boost/qvm/mat_operations.hpp>
  2935. [source,c++]
  2936. ----
  2937. namespace boost { namespace qvm {
  2938. template <int Dim,class Angle>
  2939. -unspecified-return-type- rotz_mat( Angle const & angle );
  2940. } }
  2941. ----
  2942. Returns: :: A <<view_proxy,view proxy>> matrix of unspecified type, of `Dim` rows and `Dim` columns and scalar type `Angle`, which performs a rotation around the `Z` axis at `angle` radians.
  2943. '''
  2944. [[mat_set_rotz]]
  2945. ==== `set_rotz`
  2946. .#include <boost/qvm/mat_operations.hpp>
  2947. [source,c++]
  2948. ----
  2949. namespace boost { namespace qvm {
  2950. //Only enabled if:
  2951. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2952. // mat_traits<A>::rows==mat_traits<A>::cols
  2953. template <class A>
  2954. void set_rotz( A & a, typename mat_traits<A>::scalar_type angle );
  2955. } }
  2956. ----
  2957. Effects: :: As if:
  2958. +
  2959. [source,c++]
  2960. ----
  2961. assign(
  2962. a,
  2963. rotz_mat<mat_traits<A>::rows>(angle));
  2964. ----
  2965. '''
  2966. [[mat_rotate_z]]
  2967. ==== `rotate_z`
  2968. .#include <boost/qvm/mat_operations.hpp>
  2969. [source,c++]
  2970. ----
  2971. namespace boost { namespace qvm {
  2972. //Only enabled if:
  2973. // is_mat<A>::value && mat_traits<A>::rows>=3 &&
  2974. // mat_traits<A>::rows==mat_traits<A>::cols
  2975. template <class A>
  2976. void rotate_z( A & a, typename mat_traits<A>::scalar_type angle );
  2977. } }
  2978. ----
  2979. Effects: :: As if: `a <<mat_mul_eq,*\=>> <<rotz_mat,rotz_mat>><<<mat_traits,mat_traits<A>::rows>>>(angle)`.
  2980. '''
  2981. [[determinant]]
  2982. ==== `determinant`
  2983. .#include <boost/qvm/mat_operations.hpp>
  2984. [source,c++]
  2985. ----
  2986. namespace boost { namespace qvm {
  2987. //Only enabled if:
  2988. // is_mat<A>::value && mat_traits<A>::rows==mat_traits<A>::cols
  2989. template <class A>
  2990. mat_traits<A>::scalar_type
  2991. determinant( A const & a );
  2992. } }
  2993. ----
  2994. This function computes the https://en.wikipedia.org/wiki/Determinant[determinant] of the square matrix `a`.
  2995. '''
  2996. [[perspective_lh]]
  2997. ==== `perspective_lh`
  2998. .#include <boost/qvm/mat_operations.hpp>
  2999. [source,c++]
  3000. ----
  3001. namespace boost { namespace qvm {
  3002. template <class T>
  3003. -unspecified-return-type-
  3004. perspective_lh( T fov_y, T aspect, T zn, T zf );
  3005. } }
  3006. ----
  3007. Returns: :: A 4x4 projection matrix of unspecified type of the following form:
  3008. +
  3009. [cols="^v,^v,^v,^v",width="50%"]
  3010. |====
  3011. | `xs` | 0 | 0 | 0
  3012. | 0 | `ys` | 0 | 0
  3013. | 0 | 0 | `zf`/(`zf`-`zn`) | -`zn`*`zf`/(`zf`-`zn`)
  3014. | 0 | 0 | 1 | 0
  3015. |====
  3016. +
  3017. where `ys` = cot(`fov_y`/2) and `xs` = `ys`/`aspect`.
  3018. '''
  3019. [[perspective_rh]]
  3020. ==== `perspective_rh`
  3021. .#include <boost/qvm/mat_operations.hpp>
  3022. [source,c++]
  3023. ----
  3024. namespace boost { namespace qvm {
  3025. template <class T>
  3026. -unspecified-return-type-
  3027. perspective_rh( T fov_y, T aspect, T zn, T zf );
  3028. } }
  3029. ----
  3030. Returns: :: A 4x4 projection matrix of unspecified type of the following form:
  3031. +
  3032. [cols="^v,^v,^v,^v",width="50%"]
  3033. |====
  3034. | `xs` | 0 | 0 | 0
  3035. | 0 | `ys` | 0 | 0
  3036. | 0 | 0 | `zf`/(`zn`-`zf`) | `zn`*`zf`/(`zn`-`zf`)
  3037. | 0 | 0 | -1 | 0
  3038. |====
  3039. +
  3040. where `ys` = cot(`fov_y`/2), and `xs` = `ys`/`aspect`.
  3041. '''
  3042. [[mat_scalar_cast]]
  3043. ==== `scalar_cast`
  3044. .#include <boost/qvm/mat_operations.hpp>
  3045. [source,c++]
  3046. ----
  3047. namespace boost { namespace qvm {
  3048. //Only enabled if: is_mat<A>::value
  3049. template <class Scalar,class A>
  3050. -unspecified-return_type- scalar_cast( A const & a );
  3051. } }
  3052. ----
  3053. Returns: :: A read-only <<view_proxy,view proxy>> of `a` that looks like a matrix of the same dimensions as `a`, but with <<mat_traits,`scalar_type`>> `Scalar` and elements constructed from the corresponding elements of `a`.
  3054. '''
  3055. [[mref]]
  3056. ==== `mref`
  3057. .#include <boost/qvm/mat_operations.hpp>
  3058. [source,c++]
  3059. ----
  3060. namespace boost { namespace qvm {
  3061. //Only enabled if: is_mat<A>::value
  3062. template <class A>
  3063. -unspecified-return-type- mref( A & a );
  3064. } }
  3065. ----
  3066. Returns: :: An identity view proxy of `a`; that is, it simply accesses the elements of `a`.
  3067. TIP: `mref` allows calling QVM operations when `a` is of built-in type, for example a plain old C array.
  3068. '''
  3069. === Quaternion-Vector Operations
  3070. [[quat_vec_mul]]
  3071. ==== `operator*`
  3072. .#include <boost/qvm/quat_vec_operations.hpp>
  3073. [source,c++]
  3074. ----
  3075. namespace boost { namespace qvm {
  3076. //Only enabled if:
  3077. // is_mat<A>::value && is_vec<B>::value &&
  3078. // mat_traits<A>::cols==vec_traits<B>::dim
  3079. template <class A,class B>
  3080. typename deduce_vec2<A,B,mat_traits<A>::rows>::type
  3081. operator*( A const & a, B const & b );
  3082. } }
  3083. ----
  3084. Returns: :: The result of transforming the vector `b` by the quaternion `a`.
  3085. NOTE: The <<deduce_vec2,`deduce_vec2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  3086. '''
  3087. === Matrix-Vector Operations
  3088. [[mat_vec_mul]]
  3089. ==== `operator*`
  3090. .#include <boost/qvm/vec_mat_operations.hpp>
  3091. [source,c++]
  3092. ----
  3093. namespace boost { namespace qvm {
  3094. //Only enabled if:
  3095. // is_mat<A>::value && is_vec<B>::value &&
  3096. // mat_traits<A>::cols==vec_traits<B>::dim
  3097. template <class A,class B>
  3098. typename deduce_vec2<A,B,mat_traits<A>::rows>::type
  3099. operator*( A const & a, B const & b );
  3100. } }
  3101. ----
  3102. Returns: :: The result of multiplying the matrix `a` and the vector `b`, where `b` is interpreted as a matrix-column. The resulting matrix-row is returned as a vector type.
  3103. NOTE: The <<deduce_vec2,`deduce_vec2`>> template can be specialized to deduce the desired return type, given the types `A` and `B`.
  3104. '''
  3105. [[transform_vector]]
  3106. ==== `transform_vector`
  3107. .#include <boost/qvm/vec_mat_operations.hpp>
  3108. [source,c++]
  3109. ----
  3110. namespace boost { namespace qvm {
  3111. //Only enabled if:
  3112. // is_mat<A>::value && is_vec<B>::value &&
  3113. // mat_traits<A>::rows==4 && mat_traits<A>::cols==4 &&
  3114. // vec_traits<B>::dim==3
  3115. template <class A,class B>
  3116. deduce_vec2<A,B,3> >::type
  3117. transform_vector( A const & a, B const & b );
  3118. } }
  3119. ----
  3120. Effects: :: As if: `return a <<mat_vec_mul,*>> <<swizzling,XYZ0>>(b)`.
  3121. '''
  3122. [[transform_point]]
  3123. ==== `transform_point`
  3124. .#include <boost/qvm/vec_mat_operations.hpp>
  3125. [source,c++]
  3126. ----
  3127. namespace boost { namespace qvm {
  3128. //Only enabled if:
  3129. // is_mat<A>::value && is_vec<B>::value &&
  3130. // mat_traits<A>::rows==4 && mat_traits<A>::cols==4 &&
  3131. // vec_traits<B>::dim==3
  3132. template <class A,class B>
  3133. deduce_vec2<A,B,3> >::type
  3134. transform_point( A const & a, B const & b );
  3135. } }
  3136. ----
  3137. Effects: :: As if: `return a <<mat_vec_mul,*>> <<swizzling,XYZ1>>(b)`.
  3138. '''
  3139. === Matrix-to-Matrix View Proxies
  3140. [[del_row]]
  3141. ==== `del_row`
  3142. .#include <boost/qvm/map_mat_mat.hpp>
  3143. [source,c++]
  3144. ----
  3145. namespace boost { namespace qvm {
  3146. template <int R>
  3147. -unspecified-return-type- del_row();
  3148. } }
  3149. ----
  3150. The expression `del_row<R>(m)` returns an lvalue <<view_proxy,view proxy>> that looks like the matrix `m` with row `R` deleted.
  3151. '''
  3152. [[del_col]]
  3153. ==== `del_col`
  3154. .#include <boost/qvm/map_mat_mat.hpp>
  3155. [source,c++]
  3156. ----
  3157. namespace boost { namespace qvm {
  3158. template <int C>
  3159. -unspecified-return-type- del_col();
  3160. } }
  3161. ----
  3162. The expression `del_col<C>(m)` returns an lvalue <<view_proxy,view proxy>> that looks like the matrix `m` with column `C` deleted.
  3163. '''
  3164. [[del_row_col]]
  3165. ==== `del_row_col`
  3166. .#include <boost/qvm/map_mat_mat.hpp>
  3167. [source,c++]
  3168. ----
  3169. namespace boost { namespace qvm {
  3170. template <int R,int C>
  3171. -unspecified-return-type- del_row_col();
  3172. } }
  3173. ----
  3174. The expression `del_row_col<R,C>(m)` returns an lvalue <<view_proxy,view proxy>> that looks like the matrix `m` with row `R` and column `C` deleted.
  3175. '''
  3176. [[neg_row]]
  3177. ==== `neg_row`
  3178. .#include <boost/qvm/map_mat_mat.hpp>
  3179. [source,c++]
  3180. ----
  3181. namespace boost { namespace qvm {
  3182. template <int R>
  3183. -unspecified-return-type- neg_row();
  3184. } }
  3185. ----
  3186. The expression `neg_row<R>(m)` returns a read-only <<view_proxy,view proxy>> that looks like the matrix `m` with row `R` negated.
  3187. '''
  3188. [[neg_col]]
  3189. ==== `neg_col`
  3190. .#include <boost/qvm/map_mat_mat.hpp>
  3191. [source,c++]
  3192. ----
  3193. namespace boost { namespace qvm {
  3194. template <int C>
  3195. -unspecified-return-type- neg_col();
  3196. } }
  3197. ----
  3198. The expression `neg_col<C>(m)` returns a read-only <<view_proxy,`view proxy`>> that looks like the matrix `m` with column `C` negated.
  3199. '''
  3200. [[swap_rows]]
  3201. ==== `swap_rows`
  3202. .#include <boost/qvm/map_mat_mat.hpp>
  3203. [source,c++]
  3204. ----
  3205. namespace boost { namespace qvm {
  3206. template <int R1,int R2>
  3207. -unspecified-return-type- swap_rows();
  3208. } }
  3209. ----
  3210. The expression `swap_rows<R1,R2>(m)` returns an lvalue <<view_proxy,view proxy>> that looks like the matrix `m` with rows `R1` and `R2` swapped.
  3211. '''
  3212. [[swap_cols]]
  3213. ==== `swap_cols`
  3214. .#include <boost/qvm/map_mat_mat.hpp>
  3215. [source,c++]
  3216. ----
  3217. namespace boost { namespace qvm {
  3218. template <int C1,int C2>
  3219. -unspecified-return-type- swap_cols();
  3220. } }
  3221. ----
  3222. The expression `swap_cols<C1,C2>(m)` returns an lvalue <<view_proxy,view proxy>> that looks like the matrix `m` with columns `C1` and `C2` swapped.
  3223. '''
  3224. [[transposed]]
  3225. ==== `transposed`
  3226. .#include <boost/qvm/map_mat_mat.hpp>
  3227. [source,c++]
  3228. ----
  3229. namespace boost { namespace qvm {
  3230. -unspecified-return-type- transposed();
  3231. } }
  3232. ----
  3233. The expression `transposed(m)` returns an lvalue <<view_proxy,view proxy>> that transposes the matrix `m`.
  3234. '''
  3235. === Vector-to-Matrix View Proxies
  3236. [[col_mat]]
  3237. ==== `col_mat`
  3238. .#include <boost/qvm/map_vec_mat.hpp>
  3239. [source,c++]
  3240. ----
  3241. namespace boost { namespace qvm {
  3242. //Only enabled if: is_vec<A>::value
  3243. template <iclass A>
  3244. -unspecified-return-type- col_mat( A & a );
  3245. } }
  3246. ----
  3247. The expression `col_mat(v)` returns an lvalue <<view_proxy,view proxy>> that accesses the vector `v` as a matrix-column.
  3248. '''
  3249. [[row_mat]]
  3250. ==== `row_mat`
  3251. .#include <boost/qvm/map_vec_mat.hpp>
  3252. [source,c++]
  3253. ----
  3254. namespace boost { namespace qvm {
  3255. //Only enabled if: is_vec<A>::value
  3256. template <iclass A>
  3257. -unspecified-return-type- row_mat( A & a );
  3258. } }
  3259. ----
  3260. The expression `row_mat(v)` returns an lvalue <<view_proxy,view proxy>> that accesses the vector `v` as a matrix-row.
  3261. '''
  3262. [[translation_mat]]
  3263. ==== `translation_mat`
  3264. .#include <boost/qvm/map_vec_mat.hpp>
  3265. [source,c++]
  3266. ----
  3267. namespace boost { namespace qvm {
  3268. //Only enabled if: is_vec<A>::value
  3269. template <iclass A>
  3270. -unspecified-return-type- translation_mat( A & a );
  3271. } }
  3272. ----
  3273. The expression `translation_mat(v)` returns an lvalue <<view_proxy,view proxy>> that accesses the vector `v` as translation matrix of size 1 + <<vec_traits,`vec_traits<A>::dim`>>.
  3274. '''
  3275. [[diag_mat]]
  3276. ==== `diag_mat`
  3277. .#include <boost/qvm/map_vec_mat.hpp>
  3278. [source,c++]
  3279. ----
  3280. namespace boost { namespace qvm {
  3281. //Only enabled if: is_vec<A>::value
  3282. template <iclass A>
  3283. -unspecified-return-type- diag_mat( A & a );
  3284. } }
  3285. ----
  3286. The expression `diag_mat(v)` returns an lvalue <<view_proxy,view proxy>> that accesses the vector `v` as a square matrix of the same dimensions in which the elements of `v` appear as the main diagonal and all other elements are zero.
  3287. TIP: If `v` is a 3D vector, the expression `diag_mat(XYZ1(v))` can be used as a scaling 4D matrix.
  3288. '''
  3289. === Matrix-to-Vector View Proxies
  3290. [[col]]
  3291. ==== `col`
  3292. .#include <boost/qvm/map_mat_vec.hpp>
  3293. [source,c++]
  3294. ----
  3295. namespace boost { namespace qvm {
  3296. //Only enabled if: is_mat<A>::value
  3297. template <int C,class A>
  3298. -unspecified-return-type- col( A & a );
  3299. } }
  3300. ----
  3301. The expression `col<C>(m)` returns an lvalue <<view_proxy,view proxy>> that accesses column `C` of the matrix `m` as a vector.
  3302. '''
  3303. [[row]]
  3304. ==== `row`
  3305. .#include <boost/qvm/map_mat_vec.hpp>
  3306. [source,c++]
  3307. ----
  3308. namespace boost { namespace qvm {
  3309. //Only enabled if: is_mat<A>::value
  3310. template <int C,class A>
  3311. -unspecified-return-type- row( A & a );
  3312. } }
  3313. ----
  3314. The expression `row<R>(m)` returns an lvalue <<view_proxy,view proxy>> that accesses row `R` of the matrix `m` as a vector.
  3315. '''
  3316. [[diag]]
  3317. ==== `diag`
  3318. .#include <boost/qvm/map_mat_vec.hpp>
  3319. [source,c++]
  3320. ----
  3321. namespace boost { namespace qvm {
  3322. //Only enabled if: is_mat<A>::value
  3323. template <class A>
  3324. -unspecified-return-type- diag( A & a );
  3325. } }
  3326. ----
  3327. The expression `diag(m)` returns an lvalue <<view_proxy,view proxy>> that accesses the main diagonal of the matrix `m` as a vector.
  3328. '''
  3329. [[translation]]
  3330. ==== `translation`
  3331. .#include <boost/qvm/map_mat_vec.hpp>
  3332. [source,c++]
  3333. ----
  3334. namespace boost { namespace qvm {
  3335. //Only enabled if:
  3336. // is_mat<A>::value &&
  3337. // mat_traits<A>::rows==mat_traits<A>::cols && mat_traits<A>::rows>=3
  3338. template <class A>
  3339. -unspecified-return-type- translation( A & a );
  3340. } }
  3341. ----
  3342. The expression `translation(m)` returns an lvalue <<view_proxy,view proxy>> that accesses the translation component of the square matrix `m`, which is a vector of size `D`-1, where `D` is the size of `m`.
  3343. '''
  3344. === Exceptions
  3345. [[error]]
  3346. ==== `error`
  3347. .#include <boost/qvm/error.hpp>
  3348. [source,c++]
  3349. ----
  3350. namespace boost { namespace qvm {
  3351. struct error: virtual boost::exception, virtual std::exception { };
  3352. } }
  3353. ----
  3354. This is the base for all exceptions thorwn by QVM.
  3355. '''
  3356. [[zero_magnitude_error]]
  3357. ==== `zero_magnitude_error`
  3358. .#include <boost/qvm/error.hpp>
  3359. [source,c++]
  3360. ----
  3361. namespace boost { namespace qvm {
  3362. struct zero_magnitude_error: virtual error { };
  3363. } }
  3364. ----
  3365. This exception indicates that an operation requires a vector or a quaternion with non-zero magnitude, but the computed magnitude is zero.
  3366. '''
  3367. [[zero_determinant_error]]
  3368. ==== `zero_determinant_error`
  3369. .#include <boost/qvm/error.hpp>
  3370. [source,c++]
  3371. ----
  3372. namespace boost { namespace qvm {
  3373. struct zero_determinant_error: virtual error { };
  3374. } }
  3375. ----
  3376. This exception indicates that an operation requires a matrix with non-zero determinant, but the computed determinant is zero.
  3377. '''
  3378. === Macros and Configuration: BOOST_QVM_
  3379. [[BOOST_QVM_INLINE]]
  3380. ==== `INLINE`
  3381. ===== `BOOST_QVM_INLINE`
  3382. .#include <boost/qvm/inline.hpp>
  3383. [source,c++]
  3384. ----
  3385. namespace boost { namespace qvm {
  3386. #ifndef BOOST_QVM_INLINE
  3387. #define BOOST_QVM_INLINE inline
  3388. #endif
  3389. } }
  3390. ----
  3391. This macro is not used directly by QVM, except as the default value of other macros from `<boost/qvm/inline.hpp>`. A user-defined `BOOST_QVM_INLINE` should expand to a value that is valid substitution of the `inline` keyword in function definitions.
  3392. '''
  3393. [[BOOST_QVM_FORCE_INLINE]]
  3394. ==== `FORCE_INLINE`
  3395. ===== `BOOST_QVM_FORCE_INLINE`
  3396. .#include <boost/qvm/inline.hpp>
  3397. [source,c++]
  3398. ----
  3399. namespace boost { namespace qvm {
  3400. #ifndef BOOST_QVM_FORCE_INLINE
  3401. #define BOOST_QVM_FORCE_INLINE /*platform-specific*/
  3402. #endif
  3403. } }
  3404. ----
  3405. This macro is not used directly by QVM, except as the default value of other macros from `<boost/qvm/inline.hpp>`. A user-defined `BOOST_QVM_FORCE_INLINE` should expand to a value that is valid substitution of the `inline` keyword in function definitions, to indicate that the compiler must inline the function. Of course, actual inlining may or may not occur.
  3406. '''
  3407. [[BOOST_QVM_INLINE_TRIVIAL]]
  3408. ==== `INLINE_TRIVIAL`
  3409. ===== `BOOST_QVM_INLINE_TRIVIAL`
  3410. .#include <boost/qvm/inline.hpp>
  3411. [source,c++]
  3412. ----
  3413. namespace boost { namespace qvm {
  3414. #ifndef BOOST_QVM_INLINE_TRIVIAL
  3415. #define BOOST_QVM_INLINE_TRIVIAL BOOST_QVM_FORCE_INLINE
  3416. #endif
  3417. } }
  3418. ----
  3419. QVM uses `BOOST_QVM_INLINE_TRIVIAL` in definitions of functions that are not critical for the overall performance of the library but are extremely simple (such as one-liners) and therefore should always be inlined.
  3420. '''
  3421. [[BOOST_QVM_INLINE_CRITICAL]]
  3422. ==== `INLINE_CRITICAL`
  3423. ===== `BOOST_QVM_INLINE_CRITICAL`
  3424. .#include <boost/qvm/inline.hpp>
  3425. [source,c++]
  3426. ----
  3427. namespace boost { namespace qvm {
  3428. #ifndef BOOST_QVM_INLINE_CRITICAL
  3429. #define BOOST_QVM_INLINE_CRITICAL BOOST_QVM_FORCE_INLINE
  3430. #endif
  3431. } }
  3432. ----
  3433. QVM uses `BOOST_QVM_INLINE_CRITICAL` in definitions of functions that are critical for the overall performance of the library, such as functions that access individual vector and matrix elements.
  3434. '''
  3435. [[BOOST_QVM_INLINE_OPERATIONS]]
  3436. ==== `INLINE_OPERATIONS`
  3437. ===== `BOOST_QVM_INLINE_OPERATIONS`
  3438. .#include <boost/qvm/inline.hpp>
  3439. [source,c++]
  3440. ----
  3441. namespace boost { namespace qvm {
  3442. #ifndef BOOST_QVM_INLINE_OPERATIONS
  3443. #define BOOST_QVM_INLINE_OPERATIONS BOOST_QVM_INLINE
  3444. #endif
  3445. } }
  3446. ----
  3447. QVM uses `BOOST_QVM_INLINE_OPERATIONS` in definitions of functions that implement various high-level operations, such as matrix multiplication, computing the magnitude of a vector, etc.
  3448. '''
  3449. [[BOOST_QVM_INLINE_RECURSION]]
  3450. ==== `INLINE_RECURSION`
  3451. ===== `BOOST_QVM_INLINE_RECURSION`
  3452. .#include <boost/qvm/inline.hpp>
  3453. [source,c++]
  3454. ----
  3455. namespace boost { namespace qvm {
  3456. #ifndef BOOST_QVM_INLINE_RECURSION
  3457. #define BOOST_QVM_INLINE_RECURSION BOOST_QVM_INLINE_OPERATIONS
  3458. #endif
  3459. } }
  3460. ----
  3461. QVM uses `BOOST_QVM_INLINE_RECURSION` in definitions of recursive functions that are not critical for the overall performance of the library (definitions of all critical functions, including critical recursive functions, use <<BOOST_QVM_INLINE_CRITICAL,`BOOST_QVM_INLINE_CRITICAL`>>).
  3462. '''
  3463. [[BOOST_QVM_ASSERT]]
  3464. ==== `ASSERT`
  3465. ===== `BOOST_QVM_ASSERT`
  3466. .#include <boost/qvm/assert.hpp>
  3467. [source,c++]
  3468. ----
  3469. namespace boost { namespace qvm {
  3470. #ifndef BOOST_QVM_ASSERT
  3471. #include <boost/assert.hpp>
  3472. #define BOOST_QVM_ASSERT BOOST_ASSERT
  3473. #endif
  3474. } }
  3475. ----
  3476. This is the macro QVM uses to assert on precondition violations and logic errors. A user-defined `BOOST_QVM_ASSERT` should have the semantics of the standard `assert`.
  3477. '''
  3478. [[BOOST_QVM_STATIC_ASSERT]]
  3479. ==== `STATIC_ASSERT`
  3480. ===== `BOOST_QVM_STATIC_ASSERT`
  3481. .#include <boost/qvm/static_assert.hpp>
  3482. [source,c++]
  3483. ----
  3484. namespace boost { namespace qvm {
  3485. #ifndef BOOST_QVM_STATIC_ASSERT
  3486. #include <boost/static_assert.hpp>
  3487. #define BOOST_QVM_STATIC_ASSERT BOOST_STATIC_ASSERT
  3488. #endif
  3489. } }
  3490. ----
  3491. All static assertions in QVM use the `BOOST_QVM_STATIC_ASSERT` macro.
  3492. '''
  3493. [[BOOST_QVM_THROW_EXCEPTION]]
  3494. ==== `THROW_EXCEPTION`
  3495. ===== `BOOST_QVM_THROW_EXCEPTION`
  3496. .#include <boost/qvm/throw_exception.hpp>
  3497. [source,c++]
  3498. ----
  3499. namespace boost { namespace qvm {
  3500. #ifndef BOOST_QVM_THROW_EXCEPTION
  3501. #include <boost/throw_exception.hpp>
  3502. #define BOOST_QVM_THROW_EXCEPTION BOOST_THROW_EXCEPTION
  3503. #endif
  3504. } }
  3505. ----
  3506. This macro is used whenever QVM throws an exception. Users who override the standard `BOOST_QVM_THROW_EXCEPTION` behavior must ensure that when invoked, the substituted implementation does not return control to the caller. Below is a list of all QVM functions that invoke `BOOST_QVM_THROW_EXCEPTION`:
  3507. * Quaternion operations:
  3508. ** <<quat_inverse,`inverse`>>
  3509. ** <<rot_quat,`rot_quat`>>
  3510. ** <<quat_normalize,`normalize`>>
  3511. ** <<quat_normalized,`normalized`>>
  3512. * Vector operations:
  3513. ** <<vec_normalize,`normalize`>>
  3514. ** <<vec_normalized,`normalized`>>
  3515. * Matrix operations:
  3516. ** <<mat_inverse,`inverse`>>
  3517. ** <<rot_mat,`rot_mat`>>
  3518. [[rationale]]
  3519. == Design Rationale
  3520. {CPP} is ideal for 3D graphics and other domains that require 3D transformations: define vector and matrix types and then overload the appropriate operators to implement the standard algebraic operations. Because this is relatively straight-forward, there are many libraries that do this, each providing custom vector and matrix types, and then defining the same operations (e.g. matrix multiply) for these types.
  3521. Often these libraries are part of a higher level system. For example, video game programmers typically use one set of vector/matrix types with the rendering engine, and another with the physics simulation engine.
  3522. QVM proides interoperability between all these different types and APIs by decoupling the standard algebraic functions from the types they operate on -- without compromising type safety. The operations work on any type for which proper traits have been specialized. Using QVM, there is no need to translate between the different quaternion, vector or matrix types; they can be mixed in the same expression safely and efficiently.
  3523. This design enables QVM to generate types and adaptors at compile time, compatible with any other QVM or user-defined type. For example, transposing a matrix needs not store the result: rather than modifying its argument or returning a new object, it simply binds the original matrix object through a generated type which remaps element access on the fly.
  3524. In addition, QVM can be helpful in selectively optimizing individual types or operations for maximum performance where that matters. For example, users can overload a specific operation for specific types, or define highly optimized, possibly platform-specific or for some reason cumbersome to use types, then mix and match them with more user-friendly types in parts of the program where performance isn't critical.
  3525. == Code Generator
  3526. While QVM defines generic functions that operate on matrix and vector types of arbitrary static dimensions, it also provides a code generator that can be used to create compatible header files that define much simpler specializations of these functions for specific dimensions. This is useful during debugging since the generated code is much easier to read than the template metaprogramming-heavy generic implementations. It is also potentially friendlier to the optimizer.
  3527. The code generator is a command-line utility program. Its source code can be found in the `boost/libs/qvm/gen` directory. It was used to generate the following headers that ship with QVM:
  3528. * 2D, 3D and 4D matrix operations:
  3529. ** `boost/qvm/gen/mat_operations2.hpp` (matrices of size 2x2, 2x1 and 1x2, included by `boost/qvm/mat_operations2.hpp`)
  3530. ** `boost/qvm/gen/mat_operations3.hpp` (matrices of size 3x3, 3x1 and 1x3, included by `boost/qvm/mat_operations3.hpp`)
  3531. ** `boost/qvm/gen/mat_operations4.hpp` (matrices of size 4x4, 4x1 and 1x4, included by `boost/qvm/mat_operations4.hpp`)
  3532. * 2D, 3D and 4D vector operations:
  3533. ** `boost/qvm/gen/v2.hpp` (included by `boost/qvm/vec_operations2.hpp`)
  3534. ** `boost/qvm/gen/v3.hpp` (included by `boost/qvm/vec_operations3.hpp`)
  3535. ** `boost/qvm/gen/v4.hpp` (included by `boost/qvm/vec_operations4.hpp`)
  3536. * 2D, 3D and 4D vector-matrix operations:
  3537. ** `boost/qvm/gen/vm2.hpp` (included by `boost/qvm/vec_mat_operations2.hpp`)
  3538. ** `boost/qvm/gen/vm3.hpp` (included by `boost/qvm/vec_mat_operations3.hpp`)
  3539. ** `boost/qvm/gen/vm4.hpp` (included by `boost/qvm/vec_mat_operations4.hpp`)
  3540. * 2D, 3D and 4D vector swizzling operations:
  3541. ** `boost/qvm/gen/sw2.hpp` (included by `boost/qvm/swizzle2.hpp`)
  3542. ** `boost/qvm/gen/sw3.hpp` (included by `boost/qvm/swizzle3.hpp`)
  3543. ** `boost/qvm/gen/sw4.hpp` (included by `boost/qvm/swizzle4.hpp`)
  3544. Any such generated headers must be included before the corresponding generic header file is included. For example, if one creates a header `boost/qvm/gen/m5.hpp`, it must be included before `boost/qvm/mat_operations.hpp` in included. However, the generic headers (`boost/qvm/mat_operations.hpp`, `boost/qvm/vec_operations.hpp`, `boost/qvm/vec_mat_operations.hpp` and `boost/qvm/swizzle.hpp`) already include the generated headers from the list above, so the generated headers don't need to be included manually.
  3545. NOTE: headers under `boost/qvm/gen` are not part of the public interface of QVM. For example, `boost/qvm/gen/mat_operations2.hpp` should not be included directly; `#include <boost/qvm/mat_operations2.hpp>` instead.
  3546. == Known Quirks and Issues
  3547. === Capturing View Proxies with `auto`
  3548. By design, <<view_proxy,view proxies>> must not return temporary objects. They return reference to an argument they take by (`const`) reference, cast to reference of unspecified type that is not copyable. Because of this, the return value of a view proxy can not be captured by value with `auto`:
  3549. [source,c++]
  3550. ----
  3551. auto tr = transposed(m); //Error: the return type of transposed can not be copied.
  3552. ----
  3553. The correct use of auto with view proxies is:
  3554. [source,c++]
  3555. ----
  3556. auto & tr = transposed(m);
  3557. ----
  3558. NOTE: Many view proxies are not read-only, that is, they're lvalues; changes made on the view proxy operate on the original object. This is another reason why they can not be captured by value with `auto`.
  3559. '''
  3560. === Binding QVM Overloads From an Unrelated Namespace
  3561. The operator overloads in namespace `boost::qvm` are designed to work with user-defined types. Typically it is sufficient to make these operators available in the namespace where the operator is used, by `using namespace boost::qvm`. A problem arises if the scope that uses the operator is not controlled by the user. For example:
  3562. [source,c++]
  3563. ----
  3564. namespace ns1 {
  3565. struct float2 { float x, y; };
  3566. }
  3567. namespace ns2 {
  3568. using namespace boost::qvm;
  3569. void f() {
  3570. ns1::float2 a, b;
  3571. a==b; //OK
  3572. ns1::float2 arr1[2], arr2[2];
  3573. std::equal(arr1,arr1+2,arr2); //Error: operator== is inaccessible from namespace std
  3574. }
  3575. }
  3576. ----
  3577. In the `std::equal` expression above, even though `boost::qvm::operator==` is made visible in namespace `ns2` by `using namespace boost::qvm`, the call originates from namespace `std`. In this case the compiler can't bind `boost::qvm::operator==` because only namespace `ns1` is visible through ADL, and it does not contain a suitable declaration. The solution is to declare `operator==` in namespace ns1, which can be done like this:
  3578. [source,c++]
  3579. ----
  3580. namespace ns1 {
  3581. using boost::qvm::operator==;
  3582. }
  3583. ----
  3584. '''
  3585. === Link Errors When Calling Math Functions with `int` Arguments
  3586. QVM does not call standard math functions (e.g. sin, cos, etc.) directly. Instead, it calls function templates declared in `boost/qvm/math.hpp` in namespace `boost::qvm`. This allows the user to specialize these templates for user-defined scalar types.
  3587. QVM itself defines specializations of the math function templates only for `float` and `double`, but it does not provide generic definitions. This is done to protect the user from unintentionally writing code that binds standard math functions that take `double` when passing arguments of lesser types, which would be suboptimal.
  3588. Because of this, a call to e.g. `<<rot_mat,rot_mat>>(axis,1)` will compile successfully but fail to link, since it calls e.g. `boost::qvm::sin<int>`, which is undefined. Because rotations by integer number of radians are rarely needed, in QVM there is no protection against such errors. In such cases the solution is to use `rot_mat(axis,1.0f)` instead.
  3589. == Distribution
  3590. QVM is part of https://www.boost.org/[Boost] and is distributed under the http://www.boost.org/LICENSE_1_0.txt[Boost Software License, Version 1.0].
  3591. The source code is available in https://github.com/boostorg/qvm[QVM GitHub repository].
  3592. (C) 2008-2018 Emil Dotchevski and Reverge Studios, Inc.
  3593. == Portability
  3594. See the link:https://travis-ci.org/boostorg/qvm[QVM Travis CI Builds].
  3595. == Feedback / Support
  3596. Please use the link:https://lists.boost.org/mailman/listinfo.cgi/boost[Boost Developers mailing list].
  3597. == Q&A
  3598. [qanda]
  3599. What is the motivation behind QVM? Why not just use uBLAS/Eigen/CML/GLM/etc?:: The primary domain of QVM is realtime graphics and simulation applications, so it is not a complete linear algebra library. While (naturally) there is some overlap with such libraries, QVM puts the emphasis on 2, 3 and 4 dimensional zero-overhead operations (hence domain-specific features like Swizzling).
  3600. How does the `qvm::<<vec,vec>>` (or `qvm::<<mat,mat>>`, or `qvm::<<quat,quat>>`) template compare to vector types from other libraries?:: The `qvm::vec` template is not in any way central to the vector operations defined by QVM. The operations are designed to work with any user-defined vector type or with 3rd-party vector types (e.g. `D3DVECTOR`), while the `qvm::vec` template is simply a default return type for expressions that use arguments of different types that would be incompatible outside of QVM. For example, if the <<deduce_mat2,`deduce_mat2`>> hasn't been specialized, calling <<cross,`cross`>> with a user-defined type `vec3` and a user-defined type `float3` returns a `qvm::vec`.
  3601. Why doesn't QVM use [] or () to access vector and matrix elements?:: Because it's designed to work with user-defined types, and the {CPP} standard requires these operators to be members. Of course if a user-defined type defines `operator[]` or `operator()` they are available for use with other QVM functions, but QVM defines its own mechanisms for <<quat_access,accessing quaternion elements>>, <<vec_access,accessing vector elements>> (as well as <<swizzling,swizzling>>), and <<mat_access,accessing matrix elements>>.
  3602. '''
  3603. [.text-right]
  3604. (C) 2008-2018 Emil Dotchevski and Reverge Studios, Inc.