ImathVec.h 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright Contributors to the OpenEXR Project.
  4. //
  5. //
  6. // 2D, 3D and 4D point/vector class templates
  7. //
  8. #ifndef INCLUDED_IMATHVEC_H
  9. #define INCLUDED_IMATHVEC_H
  10. #include "ImathExport.h"
  11. #include "ImathNamespace.h"
  12. #include "ImathTypeTraits.h"
  13. #include "ImathMath.h"
  14. #include <iostream>
  15. #include <limits>
  16. #include <cstdint>
  17. #include <stdexcept>
  18. #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
  19. // suppress exception specification warnings
  20. # pragma warning(push)
  21. # pragma warning(disable : 4290)
  22. #endif
  23. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  24. template <class T> class Vec2;
  25. template <class T> class Vec3;
  26. template <class T> class Vec4;
  27. /// Enum for the Vec4 to Vec3 conversion constructor
  28. enum IMATH_EXPORT_ENUM InfException
  29. {
  30. INF_EXCEPTION
  31. };
  32. ///
  33. /// 2-element vector
  34. ///
  35. template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Vec2
  36. {
  37. public:
  38. /// @{
  39. /// @name Direct access to elements
  40. T x, y;
  41. /// @}
  42. /// Element access by index.
  43. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T& operator[] (int i) IMATH_NOEXCEPT;
  44. /// Element access by index.
  45. IMATH_HOSTDEVICE constexpr const T& operator[] (int i) const IMATH_NOEXCEPT;
  46. /// @{
  47. /// @name Constructors and Assignment
  48. /// Uninitialized by default
  49. IMATH_HOSTDEVICE Vec2() IMATH_NOEXCEPT;
  50. /// Initialize to a scalar `(a,a)`
  51. IMATH_HOSTDEVICE constexpr explicit Vec2 (T a) IMATH_NOEXCEPT;
  52. /// Initialize to given elements `(a,b)`
  53. IMATH_HOSTDEVICE constexpr Vec2 (T a, T b) IMATH_NOEXCEPT;
  54. /// Copy constructor
  55. IMATH_HOSTDEVICE constexpr Vec2 (const Vec2& v) IMATH_NOEXCEPT;
  56. /// Construct from Vec2 of another base type
  57. template <class S> IMATH_HOSTDEVICE constexpr Vec2 (const Vec2<S>& v) IMATH_NOEXCEPT;
  58. /// Assignment
  59. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec2& operator= (const Vec2& v) IMATH_NOEXCEPT;
  60. /// Destructor
  61. ~Vec2() IMATH_NOEXCEPT = default;
  62. /// @}
  63. #if IMATH_FOREIGN_VECTOR_INTEROP
  64. /// @{
  65. /// @name Interoperability with other vector types
  66. ///
  67. /// Construction and assignment are allowed from other classes that
  68. /// appear to be equivalent vector types, provided that they have either
  69. /// a subscripting operator, or data members .x and .y, that are of the
  70. /// same type as the elements of this vector, and their size appears to
  71. /// be the right number of elements.
  72. ///
  73. /// This functionality is disabled for gcc 4.x, which seems to have a
  74. /// compiler bug that results in spurious errors. It can also be
  75. /// disabled by defining IMATH_FOREIGN_VECTOR_INTEROP to be 0 prior to
  76. /// including any Imath header files.
  77. ///
  78. template<typename V, IMATH_ENABLE_IF(has_xy<V,T>::value)>
  79. IMATH_HOSTDEVICE explicit constexpr Vec2 (const V& v) IMATH_NOEXCEPT
  80. : Vec2(T(v.x), T(v.y)) { }
  81. template<typename V, IMATH_ENABLE_IF(has_subscript<V,T,2>::value
  82. && !has_xy<V,T>::value)>
  83. IMATH_HOSTDEVICE explicit Vec2 (const V& v) : Vec2(T(v[0]), T(v[1])) { }
  84. template<typename V, IMATH_ENABLE_IF(has_xy<V,T>::value)>
  85. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec2& operator= (const V& v) IMATH_NOEXCEPT {
  86. x = T(v.x);
  87. y = T(v.y);
  88. return *this;
  89. }
  90. template<typename V, IMATH_ENABLE_IF(has_subscript<V,T,2>::value
  91. && !has_xy<V,T>::value)>
  92. IMATH_HOSTDEVICE const Vec2& operator= (const V& v) {
  93. x = T(v[0]);
  94. y = T(v[1]);
  95. return *this;
  96. }
  97. #endif
  98. /// @{
  99. /// @name Compatibility with Sb
  100. /// Set the value
  101. template <class S> IMATH_HOSTDEVICE void setValue (S a, S b) IMATH_NOEXCEPT;
  102. /// Set the value
  103. template <class S> IMATH_HOSTDEVICE void setValue (const Vec2<S>& v) IMATH_NOEXCEPT;
  104. /// Return the value in `a` and `b`
  105. template <class S> IMATH_HOSTDEVICE void getValue (S& a, S& b) const IMATH_NOEXCEPT;
  106. /// Return the value in `v`
  107. template <class S> IMATH_HOSTDEVICE void getValue (Vec2<S>& v) const IMATH_NOEXCEPT;
  108. /// Return a raw pointer to the array of values
  109. IMATH_HOSTDEVICE T* getValue() IMATH_NOEXCEPT;
  110. /// Return a raw pointer to the array of values
  111. IMATH_HOSTDEVICE const T* getValue() const IMATH_NOEXCEPT;
  112. /// @}
  113. /// @{
  114. /// @name Arithmetic and Comparison
  115. /// Equality
  116. template <class S> IMATH_HOSTDEVICE constexpr bool operator== (const Vec2<S>& v) const IMATH_NOEXCEPT;
  117. /// Inequality
  118. template <class S> IMATH_HOSTDEVICE constexpr bool operator!= (const Vec2<S>& v) const IMATH_NOEXCEPT;
  119. /// Compare two matrices and test if they are "approximately equal":
  120. /// @return True if the coefficients of this and `m` are the same
  121. /// with an absolute error of no more than e, i.e., for all i, j:
  122. ///
  123. /// abs (this[i][j] - m[i][j]) <= e
  124. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithAbsError (const Vec2<T>& v, T e) const IMATH_NOEXCEPT;
  125. /// Compare two matrices and test if they are "approximately equal":
  126. /// @return True if the coefficients of this and m are the same with
  127. /// a relative error of no more than e, i.e., for all i, j:
  128. ///
  129. /// abs (this[i] - v[i][j]) <= e * abs (this[i][j])
  130. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithRelError (const Vec2<T>& v, T e) const IMATH_NOEXCEPT;
  131. /// Dot product
  132. IMATH_HOSTDEVICE constexpr T dot (const Vec2& v) const IMATH_NOEXCEPT;
  133. /// Dot product
  134. IMATH_HOSTDEVICE constexpr T operator^ (const Vec2& v) const IMATH_NOEXCEPT;
  135. /// Right-handed cross product, i.e. z component of
  136. /// Vec3 (this->x, this->y, 0) % Vec3 (v.x, v.y, 0)
  137. IMATH_HOSTDEVICE constexpr T cross (const Vec2& v) const IMATH_NOEXCEPT;
  138. /// Right-handed cross product, i.e. z component of
  139. /// Vec3 (this->x, this->y, 0) % Vec3 (v.x, v.y, 0)
  140. IMATH_HOSTDEVICE constexpr T operator% (const Vec2& v) const IMATH_NOEXCEPT;
  141. /// Component-wise addition
  142. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec2& operator+= (const Vec2& v) IMATH_NOEXCEPT;
  143. /// Component-wise addition
  144. IMATH_HOSTDEVICE constexpr Vec2 operator+ (const Vec2& v) const IMATH_NOEXCEPT;
  145. /// Component-wise subtraction
  146. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec2& operator-= (const Vec2& v) IMATH_NOEXCEPT;
  147. /// Component-wise subtraction
  148. IMATH_HOSTDEVICE constexpr Vec2 operator- (const Vec2& v) const IMATH_NOEXCEPT;
  149. /// Component-wise multiplication by -1
  150. IMATH_HOSTDEVICE constexpr Vec2 operator-() const IMATH_NOEXCEPT;
  151. /// Component-wise multiplication by -1
  152. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec2& negate() IMATH_NOEXCEPT;
  153. /// Component-wise multiplication
  154. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec2& operator*= (const Vec2& v) IMATH_NOEXCEPT;
  155. /// Component-wise multiplication
  156. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec2& operator*= (T a) IMATH_NOEXCEPT;
  157. /// Component-wise multiplication
  158. IMATH_HOSTDEVICE constexpr Vec2 operator* (const Vec2& v) const IMATH_NOEXCEPT;
  159. /// Component-wise multiplication
  160. IMATH_HOSTDEVICE constexpr Vec2 operator* (T a) const IMATH_NOEXCEPT;
  161. /// Component-wise division
  162. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec2& operator/= (const Vec2& v) IMATH_NOEXCEPT;
  163. /// Component-wise division
  164. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec2& operator/= (T a) IMATH_NOEXCEPT;
  165. /// Component-wise division
  166. IMATH_HOSTDEVICE constexpr Vec2 operator/ (const Vec2& v) const IMATH_NOEXCEPT;
  167. /// Component-wise division
  168. IMATH_HOSTDEVICE constexpr Vec2 operator/ (T a) const IMATH_NOEXCEPT;
  169. /// @}
  170. /// @{
  171. /// @name Query and Manipulation
  172. /// Return the Euclidean norm
  173. IMATH_HOSTDEVICE T length() const IMATH_NOEXCEPT;
  174. /// Return the square of the Euclidean norm, i.e. the dot product
  175. /// with itself.
  176. IMATH_HOSTDEVICE constexpr T length2() const IMATH_NOEXCEPT;
  177. /// Normalize in place. If length()==0, return a null vector.
  178. IMATH_HOSTDEVICE const Vec2& normalize() IMATH_NOEXCEPT;
  179. /// Normalize in place. If length()==0, throw an exception.
  180. const Vec2& normalizeExc();
  181. /// Normalize without any checks for length()==0. Slightly faster
  182. /// than the other normalization routines, but if v.length() is
  183. /// 0.0, the result is undefined.
  184. IMATH_HOSTDEVICE const Vec2& normalizeNonNull() IMATH_NOEXCEPT;
  185. /// Return a normalized vector. Does not modify *this.
  186. IMATH_HOSTDEVICE Vec2<T> normalized() const IMATH_NOEXCEPT;
  187. /// Return a normalized vector. Does not modify *this. Throw an
  188. /// exception if length()==0.
  189. Vec2<T> normalizedExc() const;
  190. /// Return a normalized vector. Does not modify *this, and does
  191. /// not check for length()==0. Slightly faster than the other
  192. /// normalization routines, but if v.length() is 0.0, the result
  193. /// is undefined.
  194. IMATH_HOSTDEVICE Vec2<T> normalizedNonNull() const IMATH_NOEXCEPT;
  195. /// @}
  196. /// @{
  197. /// @name Numeric Limits
  198. /// Largest possible negative value
  199. IMATH_HOSTDEVICE constexpr static T baseTypeLowest() IMATH_NOEXCEPT { return std::numeric_limits<T>::lowest(); }
  200. /// Largest possible positive value
  201. IMATH_HOSTDEVICE constexpr static T baseTypeMax() IMATH_NOEXCEPT { return std::numeric_limits<T>::max(); }
  202. /// Smallest possible positive value
  203. IMATH_HOSTDEVICE constexpr static T baseTypeSmallest() IMATH_NOEXCEPT { return std::numeric_limits<T>::min(); }
  204. /// Smallest possible e for which 1+e != 1
  205. IMATH_HOSTDEVICE constexpr static T baseTypeEpsilon() IMATH_NOEXCEPT { return std::numeric_limits<T>::epsilon(); }
  206. /// @}
  207. /// Return the number of dimensions, i.e. 2
  208. IMATH_HOSTDEVICE constexpr static unsigned int dimensions() IMATH_NOEXCEPT { return 2; }
  209. /// The base type: In templates that accept a parameter `V`, you
  210. /// can refer to `T` as `V::BaseType`
  211. typedef T BaseType;
  212. private:
  213. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T lengthTiny() const IMATH_NOEXCEPT;
  214. };
  215. ///
  216. /// 3-element vector
  217. ///
  218. template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Vec3
  219. {
  220. public:
  221. /// @{
  222. /// @name Direct access to elements
  223. T x, y, z;
  224. /// @}
  225. /// Element access by index.
  226. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T& operator[] (int i) IMATH_NOEXCEPT;
  227. /// Element access by index.
  228. IMATH_HOSTDEVICE constexpr const T& operator[] (int i) const IMATH_NOEXCEPT;
  229. /// @{
  230. /// @name Constructors and Assignment
  231. /// Uninitialized by default
  232. IMATH_HOSTDEVICE Vec3() IMATH_NOEXCEPT;
  233. /// Initialize to a scalar `(a,a,a)`
  234. IMATH_HOSTDEVICE constexpr explicit Vec3 (T a) IMATH_NOEXCEPT;
  235. /// Initialize to given elements `(a,b,c)`
  236. IMATH_HOSTDEVICE constexpr Vec3 (T a, T b, T c) IMATH_NOEXCEPT;
  237. /// Copy constructor
  238. IMATH_HOSTDEVICE constexpr Vec3 (const Vec3& v) IMATH_NOEXCEPT;
  239. /// Construct from Vec3 of another base type
  240. template <class S> IMATH_HOSTDEVICE constexpr Vec3 (const Vec3<S>& v) IMATH_NOEXCEPT;
  241. /// Vec4 to Vec3 conversion: divide x, y and z by w, even if w is
  242. /// 0. The result depends on how the environment handles
  243. /// floating-point exceptions.
  244. template <class S> IMATH_HOSTDEVICE explicit constexpr Vec3 (const Vec4<S>& v) IMATH_NOEXCEPT;
  245. /// Vec4 to Vec3 conversion: divide x, y and z by w. Throws an
  246. /// exception if w is zero or if division by w would overflow.
  247. template <class S>
  248. explicit IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3 (const Vec4<S>& v, InfException);
  249. /// Assignment
  250. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec3& operator= (const Vec3& v) IMATH_NOEXCEPT;
  251. /// Destructor
  252. ~Vec3() IMATH_NOEXCEPT = default;
  253. /// @}
  254. #if IMATH_FOREIGN_VECTOR_INTEROP
  255. /// @{
  256. /// @name Interoperability with other vector types
  257. ///
  258. /// Construction and assignment are allowed from other classes that
  259. /// appear to be equivalent vector types, provided that they have either
  260. /// a subscripting operator, or data members .x, .y, .z, that are of the
  261. /// same type as the elements of this vector, and their size appears to
  262. /// be the right number of elements.
  263. ///
  264. /// This functionality is disabled for gcc 4.x, which seems to have a
  265. /// compiler bug that results in spurious errors. It can also be
  266. /// disabled by defining IMATH_FOREIGN_VECTOR_INTEROP to be 0 prior to
  267. /// including any Imath header files.
  268. ///
  269. template<typename V, IMATH_ENABLE_IF(has_xyz<V,T>::value)>
  270. IMATH_HOSTDEVICE explicit constexpr Vec3 (const V& v) IMATH_NOEXCEPT
  271. : Vec3(T(v.x), T(v.y), T(v.z)) { }
  272. template<typename V, IMATH_ENABLE_IF(has_subscript<V,T,3>::value
  273. && !has_xyz<V,T>::value)>
  274. IMATH_HOSTDEVICE explicit Vec3 (const V& v) : Vec3(T(v[0]), T(v[1]), T(v[2])) { }
  275. /// Interoperability assignment from another type that behaves as if it
  276. /// were an equivalent vector.
  277. template<typename V, IMATH_ENABLE_IF(has_xyz<V,T>::value)>
  278. IMATH_HOSTDEVICE const Vec3& operator= (const V& v) IMATH_NOEXCEPT {
  279. x = T(v.x);
  280. y = T(v.y);
  281. z = T(v.z);
  282. return *this;
  283. }
  284. template<typename V, IMATH_ENABLE_IF(has_subscript<V,T,3>::value
  285. && !has_xyz<V,T>::value)>
  286. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec3& operator= (const V& v) {
  287. x = T(v[0]);
  288. y = T(v[1]);
  289. z = T(v[2]);
  290. return *this;
  291. }
  292. /// @}
  293. #endif
  294. /// @{
  295. /// @name Compatibility with Sb
  296. /// Set the value
  297. template <class S> IMATH_HOSTDEVICE void setValue (S a, S b, S c) IMATH_NOEXCEPT;
  298. /// Set the value
  299. template <class S> IMATH_HOSTDEVICE void setValue (const Vec3<S>& v) IMATH_NOEXCEPT;
  300. /// Return the value in `a`, `b`, and `c`
  301. template <class S> IMATH_HOSTDEVICE void getValue (S& a, S& b, S& c) const IMATH_NOEXCEPT;
  302. /// Return the value in `v`
  303. template <class S> IMATH_HOSTDEVICE void getValue (Vec3<S>& v) const IMATH_NOEXCEPT;
  304. /// Return a raw pointer to the array of values
  305. IMATH_HOSTDEVICE T* getValue() IMATH_NOEXCEPT;
  306. /// Return a raw pointer to the array of values
  307. IMATH_HOSTDEVICE const T* getValue() const IMATH_NOEXCEPT;
  308. /// @}
  309. /// @{
  310. /// @name Arithmetic and Comparison
  311. /// Equality
  312. template <class S> IMATH_HOSTDEVICE constexpr bool operator== (const Vec3<S>& v) const IMATH_NOEXCEPT;
  313. /// Inequality
  314. template <class S> IMATH_HOSTDEVICE constexpr bool operator!= (const Vec3<S>& v) const IMATH_NOEXCEPT;
  315. /// Compare two matrices and test if they are "approximately equal":
  316. /// @return True if the coefficients of this and `m` are the same
  317. /// with an absolute error of no more than e, i.e., for all i, j:
  318. ///
  319. /// abs (this[i][j] - m[i][j]) <= e
  320. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithAbsError (const Vec3<T>& v, T e) const IMATH_NOEXCEPT;
  321. /// Compare two matrices and test if they are "approximately equal":
  322. /// @return True if the coefficients of this and m are the same with
  323. /// a relative error of no more than e, i.e., for all i, j:
  324. ///
  325. /// abs (this[i] - v[i][j]) <= e * abs (this[i][j])
  326. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithRelError (const Vec3<T>& v, T e) const IMATH_NOEXCEPT;
  327. /// Dot product
  328. IMATH_HOSTDEVICE constexpr T dot (const Vec3& v) const IMATH_NOEXCEPT;
  329. /// Dot product
  330. IMATH_HOSTDEVICE constexpr T operator^ (const Vec3& v) const IMATH_NOEXCEPT;
  331. /// Right-handed cross product
  332. IMATH_HOSTDEVICE constexpr Vec3 cross (const Vec3& v) const IMATH_NOEXCEPT;
  333. /// Right-handed cross product
  334. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec3& operator%= (const Vec3& v) IMATH_NOEXCEPT;
  335. /// Right-handed cross product
  336. IMATH_HOSTDEVICE constexpr Vec3 operator% (const Vec3& v) const IMATH_NOEXCEPT;
  337. /// Component-wise addition
  338. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec3& operator+= (const Vec3& v) IMATH_NOEXCEPT;
  339. /// Component-wise addition
  340. IMATH_HOSTDEVICE constexpr Vec3 operator+ (const Vec3& v) const IMATH_NOEXCEPT;
  341. /// Component-wise subtraction
  342. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec3& operator-= (const Vec3& v) IMATH_NOEXCEPT;
  343. /// Component-wise subtraction
  344. IMATH_HOSTDEVICE constexpr Vec3 operator- (const Vec3& v) const IMATH_NOEXCEPT;
  345. /// Component-wise multiplication by -1
  346. IMATH_HOSTDEVICE constexpr Vec3 operator-() const IMATH_NOEXCEPT;
  347. /// Component-wise multiplication by -1
  348. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec3& negate() IMATH_NOEXCEPT;
  349. /// Component-wise multiplication
  350. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec3& operator*= (const Vec3& v) IMATH_NOEXCEPT;
  351. /// Component-wise multiplication
  352. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec3& operator*= (T a) IMATH_NOEXCEPT;
  353. /// Component-wise multiplication
  354. IMATH_HOSTDEVICE constexpr Vec3 operator* (const Vec3& v) const IMATH_NOEXCEPT;
  355. /// Component-wise multiplication
  356. IMATH_HOSTDEVICE constexpr Vec3 operator* (T a) const IMATH_NOEXCEPT;
  357. /// Component-wise division
  358. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec3& operator/= (const Vec3& v) IMATH_NOEXCEPT;
  359. /// Component-wise division
  360. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec3& operator/= (T a) IMATH_NOEXCEPT;
  361. /// Component-wise division
  362. IMATH_HOSTDEVICE constexpr Vec3 operator/ (const Vec3& v) const IMATH_NOEXCEPT;
  363. /// Component-wise division
  364. IMATH_HOSTDEVICE constexpr Vec3 operator/ (T a) const IMATH_NOEXCEPT;
  365. /// @}
  366. /// @{
  367. /// @name Query and Manipulation
  368. /// Return the Euclidean norm
  369. IMATH_HOSTDEVICE T length() const IMATH_NOEXCEPT;
  370. /// Return the square of the Euclidean norm, i.e. the dot product
  371. /// with itself.
  372. IMATH_HOSTDEVICE constexpr T length2() const IMATH_NOEXCEPT;
  373. /// Normalize in place. If length()==0, return a null vector.
  374. IMATH_HOSTDEVICE const Vec3& normalize() IMATH_NOEXCEPT;
  375. /// Normalize in place. If length()==0, throw an exception.
  376. const Vec3& normalizeExc();
  377. /// Normalize without any checks for length()==0. Slightly faster
  378. /// than the other normalization routines, but if v.length() is
  379. /// 0.0, the result is undefined.
  380. IMATH_HOSTDEVICE const Vec3& normalizeNonNull() IMATH_NOEXCEPT;
  381. /// Return a normalized vector. Does not modify *this.
  382. IMATH_HOSTDEVICE Vec3<T> normalized() const IMATH_NOEXCEPT; // does not modify *this
  383. /// Return a normalized vector. Does not modify *this. Throw an
  384. /// exception if length()==0.
  385. Vec3<T> normalizedExc() const;
  386. /// Return a normalized vector. Does not modify *this, and does
  387. /// not check for length()==0. Slightly faster than the other
  388. /// normalization routines, but if v.length() is 0.0, the result
  389. /// is undefined.
  390. IMATH_HOSTDEVICE Vec3<T> normalizedNonNull() const IMATH_NOEXCEPT;
  391. /// @}
  392. /// @{
  393. /// @name Numeric Limits
  394. /// Largest possible negative value
  395. IMATH_HOSTDEVICE constexpr static T baseTypeLowest() IMATH_NOEXCEPT { return std::numeric_limits<T>::lowest(); }
  396. /// Largest possible positive value
  397. IMATH_HOSTDEVICE constexpr static T baseTypeMax() IMATH_NOEXCEPT { return std::numeric_limits<T>::max(); }
  398. /// Smallest possible positive value
  399. IMATH_HOSTDEVICE constexpr static T baseTypeSmallest() IMATH_NOEXCEPT { return std::numeric_limits<T>::min(); }
  400. /// Smallest possible e for which 1+e != 1
  401. IMATH_HOSTDEVICE constexpr static T baseTypeEpsilon() IMATH_NOEXCEPT { return std::numeric_limits<T>::epsilon(); }
  402. /// @}
  403. /// Return the number of dimensions, i.e. 3
  404. IMATH_HOSTDEVICE constexpr static unsigned int dimensions() IMATH_NOEXCEPT { return 3; }
  405. /// The base type: In templates that accept a parameter `V`, you
  406. /// can refer to `T` as `V::BaseType`
  407. typedef T BaseType;
  408. private:
  409. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T lengthTiny() const IMATH_NOEXCEPT;
  410. };
  411. ///
  412. /// 4-element vector
  413. ///
  414. template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Vec4
  415. {
  416. public:
  417. /// @{
  418. /// @name Direct access to elements
  419. T x, y, z, w;
  420. /// @}
  421. /// Element access by index.
  422. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T& operator[] (int i) IMATH_NOEXCEPT;
  423. /// Element access by index.
  424. IMATH_HOSTDEVICE constexpr const T& operator[] (int i) const IMATH_NOEXCEPT;
  425. /// @{
  426. /// @name Constructors and Assignment
  427. /// Uninitialized by default
  428. IMATH_HOSTDEVICE Vec4() IMATH_NOEXCEPT; // no initialization
  429. /// Initialize to a scalar `(a,a,a,a)`
  430. IMATH_HOSTDEVICE constexpr explicit Vec4 (T a) IMATH_NOEXCEPT;
  431. /// Initialize to given elements `(a,b,c,d)`
  432. IMATH_HOSTDEVICE constexpr Vec4 (T a, T b, T c, T d) IMATH_NOEXCEPT;
  433. /// Copy constructor
  434. IMATH_HOSTDEVICE constexpr Vec4 (const Vec4& v) IMATH_NOEXCEPT;
  435. /// Construct from Vec4 of another base type
  436. template <class S> IMATH_HOSTDEVICE constexpr Vec4 (const Vec4<S>& v) IMATH_NOEXCEPT;
  437. /// Vec3 to Vec4 conversion, sets w to 1.
  438. template <class S> IMATH_HOSTDEVICE explicit constexpr Vec4 (const Vec3<S>& v) IMATH_NOEXCEPT;
  439. /// Assignment
  440. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec4& operator= (const Vec4& v) IMATH_NOEXCEPT;
  441. /// Destructor
  442. ~Vec4() IMATH_NOEXCEPT = default;
  443. /// @}
  444. #if IMATH_FOREIGN_VECTOR_INTEROP
  445. /// @{
  446. /// @name Interoperability with other vector types
  447. ///
  448. /// Construction and assignment are allowed from other classes that
  449. /// appear to be equivalent vector types, provided that they have either
  450. /// a subscripting operator, or data members .x, .y, .z, .w that are of
  451. /// the same type as the elements of this vector, and their size appears
  452. /// to be the right number of elements.
  453. ///
  454. /// This functionality is disabled for gcc 4.x, which seems to have a
  455. /// compiler bug that results in spurious errors. It can also be
  456. /// disabled by defining IMATH_FOREIGN_VECTOR_INTEROP to be 0 prior to
  457. /// including any Imath header files.
  458. ///
  459. template<typename V, IMATH_ENABLE_IF(has_xyzw<V,T>::value)>
  460. IMATH_HOSTDEVICE explicit constexpr Vec4 (const V& v) IMATH_NOEXCEPT
  461. : Vec4(T(v.x), T(v.y), T(v.z), T(v.w)) { }
  462. template<typename V, IMATH_ENABLE_IF(has_subscript<V,T,4>::value
  463. && !has_xyzw<V,T>::value)>
  464. IMATH_HOSTDEVICE explicit Vec4 (const V& v) : Vec4(T(v[0]), T(v[1]), T(v[2]), T(v[3])) { }
  465. template<typename V, IMATH_ENABLE_IF(has_xyzw<V,T>::value)>
  466. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec4& operator= (const V& v) IMATH_NOEXCEPT {
  467. x = T(v.x);
  468. y = T(v.y);
  469. z = T(v.z);
  470. w = T(v.w);
  471. return *this;
  472. }
  473. template<typename V, IMATH_ENABLE_IF(has_subscript<V,T,4>::value
  474. && !has_xyzw<V,T>::value)>
  475. IMATH_HOSTDEVICE const Vec4& operator= (const V& v) {
  476. x = T(v[0]);
  477. y = T(v[1]);
  478. z = T(v[2]);
  479. w = T(v[3]);
  480. return *this;
  481. }
  482. /// @}
  483. #endif
  484. /// @{
  485. /// @name Arithmetic and Comparison
  486. /// Equality
  487. template <class S> IMATH_HOSTDEVICE constexpr bool operator== (const Vec4<S>& v) const IMATH_NOEXCEPT;
  488. /// Inequality
  489. template <class S> IMATH_HOSTDEVICE constexpr bool operator!= (const Vec4<S>& v) const IMATH_NOEXCEPT;
  490. /// Compare two matrices and test if they are "approximately equal":
  491. /// @return True if the coefficients of this and `m` are the same
  492. /// with an absolute error of no more than e, i.e., for all i, j:
  493. ///
  494. /// abs (this[i][j] - m[i][j]) <= e
  495. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithAbsError (const Vec4<T>& v, T e) const IMATH_NOEXCEPT;
  496. /// Compare two matrices and test if they are "approximately equal":
  497. /// @return True if the coefficients of this and m are the same with
  498. /// a relative error of no more than e, i.e., for all i, j:
  499. ///
  500. /// abs (this[i] - v[i][j]) <= e * abs (this[i][j])
  501. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithRelError (const Vec4<T>& v, T e) const IMATH_NOEXCEPT;
  502. /// Dot product
  503. IMATH_HOSTDEVICE constexpr T dot (const Vec4& v) const IMATH_NOEXCEPT;
  504. /// Dot product
  505. IMATH_HOSTDEVICE constexpr T operator^ (const Vec4& v) const IMATH_NOEXCEPT;
  506. /// Component-wise addition
  507. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec4& operator+= (const Vec4& v) IMATH_NOEXCEPT;
  508. /// Component-wise addition
  509. IMATH_HOSTDEVICE constexpr Vec4 operator+ (const Vec4& v) const IMATH_NOEXCEPT;
  510. /// Component-wise subtraction
  511. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec4& operator-= (const Vec4& v) IMATH_NOEXCEPT;
  512. /// Component-wise subtraction
  513. IMATH_HOSTDEVICE constexpr Vec4 operator- (const Vec4& v) const IMATH_NOEXCEPT;
  514. /// Component-wise multiplication by -1
  515. IMATH_HOSTDEVICE constexpr Vec4 operator-() const IMATH_NOEXCEPT;
  516. /// Component-wise multiplication by -1
  517. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec4& negate() IMATH_NOEXCEPT;
  518. /// Component-wise multiplication
  519. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec4& operator*= (const Vec4& v) IMATH_NOEXCEPT;
  520. /// Component-wise multiplication
  521. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec4& operator*= (T a) IMATH_NOEXCEPT;
  522. /// Component-wise multiplication
  523. IMATH_HOSTDEVICE constexpr Vec4 operator* (const Vec4& v) const IMATH_NOEXCEPT;
  524. /// Component-wise multiplication
  525. IMATH_HOSTDEVICE constexpr Vec4 operator* (T a) const IMATH_NOEXCEPT;
  526. /// Component-wise division
  527. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec4& operator/= (const Vec4& v) IMATH_NOEXCEPT;
  528. /// Component-wise division
  529. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Vec4& operator/= (T a) IMATH_NOEXCEPT;
  530. /// Component-wise division
  531. IMATH_HOSTDEVICE constexpr Vec4 operator/ (const Vec4& v) const IMATH_NOEXCEPT;
  532. /// Component-wise division
  533. IMATH_HOSTDEVICE constexpr Vec4 operator/ (T a) const IMATH_NOEXCEPT;
  534. /// @}
  535. /// @{
  536. /// @name Query and Manipulation
  537. /// Return the Euclidean norm
  538. IMATH_HOSTDEVICE T length() const IMATH_NOEXCEPT;
  539. /// Return the square of the Euclidean norm, i.e. the dot product
  540. /// with itself.
  541. IMATH_HOSTDEVICE constexpr T length2() const IMATH_NOEXCEPT;
  542. /// Normalize in place. If length()==0, return a null vector.
  543. IMATH_HOSTDEVICE const Vec4& normalize() IMATH_NOEXCEPT; // modifies *this
  544. /// Normalize in place. If length()==0, throw an exception.
  545. const Vec4& normalizeExc();
  546. /// Normalize without any checks for length()==0. Slightly faster
  547. /// than the other normalization routines, but if v.length() is
  548. /// 0.0, the result is undefined.
  549. IMATH_HOSTDEVICE const Vec4& normalizeNonNull() IMATH_NOEXCEPT;
  550. /// Return a normalized vector. Does not modify *this.
  551. IMATH_HOSTDEVICE Vec4<T> normalized() const IMATH_NOEXCEPT; // does not modify *this
  552. /// Return a normalized vector. Does not modify *this. Throw an
  553. /// exception if length()==0.
  554. Vec4<T> normalizedExc() const;
  555. /// Return a normalized vector. Does not modify *this, and does
  556. /// not check for length()==0. Slightly faster than the other
  557. /// normalization routines, but if v.length() is 0.0, the result
  558. /// is undefined.
  559. IMATH_HOSTDEVICE Vec4<T> normalizedNonNull() const IMATH_NOEXCEPT;
  560. /// @}
  561. /// @{
  562. /// @name Numeric Limits
  563. /// Largest possible negative value
  564. IMATH_HOSTDEVICE constexpr static T baseTypeLowest() IMATH_NOEXCEPT { return std::numeric_limits<T>::lowest(); }
  565. /// Largest possible positive value
  566. IMATH_HOSTDEVICE constexpr static T baseTypeMax() IMATH_NOEXCEPT { return std::numeric_limits<T>::max(); }
  567. /// Smallest possible positive value
  568. IMATH_HOSTDEVICE constexpr static T baseTypeSmallest() IMATH_NOEXCEPT { return std::numeric_limits<T>::min(); }
  569. /// Smallest possible e for which 1+e != 1
  570. IMATH_HOSTDEVICE constexpr static T baseTypeEpsilon() IMATH_NOEXCEPT { return std::numeric_limits<T>::epsilon(); }
  571. /// @}
  572. /// Return the number of dimensions, i.e. 4
  573. IMATH_HOSTDEVICE constexpr static unsigned int dimensions() IMATH_NOEXCEPT { return 4; }
  574. /// The base type: In templates that accept a parameter `V`, you
  575. /// can refer to `T` as `V::BaseType`
  576. typedef T BaseType;
  577. private:
  578. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T lengthTiny() const IMATH_NOEXCEPT;
  579. };
  580. /// Stream output, as "(x y)"
  581. template <class T> std::ostream& operator<< (std::ostream& s, const Vec2<T>& v);
  582. /// Stream output, as "(x y z)"
  583. template <class T> std::ostream& operator<< (std::ostream& s, const Vec3<T>& v);
  584. /// Stream output, as "(x y z w)"
  585. template <class T> std::ostream& operator<< (std::ostream& s, const Vec4<T>& v);
  586. /// Reverse multiplication: S * Vec2<T>
  587. template <class T> IMATH_HOSTDEVICE constexpr Vec2<T> operator* (T a, const Vec2<T>& v) IMATH_NOEXCEPT;
  588. /// Reverse multiplication: S * Vec3<T>
  589. template <class T> IMATH_HOSTDEVICE constexpr Vec3<T> operator* (T a, const Vec3<T>& v) IMATH_NOEXCEPT;
  590. /// Reverse multiplication: S * Vec4<T>
  591. template <class T> IMATH_HOSTDEVICE constexpr Vec4<T> operator* (T a, const Vec4<T>& v) IMATH_NOEXCEPT;
  592. //-------------------------
  593. // Typedefs for convenience
  594. //-------------------------
  595. /// Vec2 of short
  596. typedef Vec2<short> V2s;
  597. /// Vec2 of integer
  598. typedef Vec2<int> V2i;
  599. /// Vec2 of int64_t
  600. typedef Vec2<int64_t> V2i64;
  601. /// Vec2 of float
  602. typedef Vec2<float> V2f;
  603. /// Vec2 of double
  604. typedef Vec2<double> V2d;
  605. /// Vec3 of short
  606. typedef Vec3<short> V3s;
  607. /// Vec3 of integer
  608. typedef Vec3<int> V3i;
  609. /// Vec3 of int64_t
  610. typedef Vec3<int64_t> V3i64;
  611. /// Vec3 of float
  612. typedef Vec3<float> V3f;
  613. /// Vec3 of double
  614. typedef Vec3<double> V3d;
  615. /// Vec4 of short
  616. typedef Vec4<short> V4s;
  617. /// Vec4 of integer
  618. typedef Vec4<int> V4i;
  619. /// Vec4 of int64_t
  620. typedef Vec4<int64_t> V4i64;
  621. /// Vec4 of float
  622. typedef Vec4<float> V4f;
  623. /// Vec4 of double
  624. typedef Vec4<double> V4d;
  625. //----------------------------------------------------------------------------
  626. // Specializations for VecN<short>, VecN<int>
  627. //
  628. // Normalize and length don't make sense for integer vectors, so disable them.
  629. //----------------------------------------------------------------------------
  630. /// @cond Doxygen_Suppress
  631. // Vec2<short>
  632. template <> IMATH_HOSTDEVICE short Vec2<short>::length() const IMATH_NOEXCEPT = delete;
  633. template <> IMATH_HOSTDEVICE const Vec2<short>& Vec2<short>::normalize() IMATH_NOEXCEPT = delete;
  634. template <> const Vec2<short>& Vec2<short>::normalizeExc() = delete;
  635. template <> IMATH_HOSTDEVICE const Vec2<short>& Vec2<short>::normalizeNonNull() IMATH_NOEXCEPT = delete;
  636. template <> IMATH_HOSTDEVICE Vec2<short> Vec2<short>::normalized() const IMATH_NOEXCEPT = delete;
  637. template <> Vec2<short> Vec2<short>::normalizedExc() const = delete;
  638. template <> IMATH_HOSTDEVICE Vec2<short> Vec2<short>::normalizedNonNull() const IMATH_NOEXCEPT = delete;
  639. // Vec2<int>
  640. template <> IMATH_HOSTDEVICE int Vec2<int>::length() const IMATH_NOEXCEPT = delete;
  641. template <> IMATH_HOSTDEVICE const Vec2<int>& Vec2<int>::normalize() IMATH_NOEXCEPT = delete;
  642. template <> const Vec2<int>& Vec2<int>::normalizeExc() = delete;
  643. template <> IMATH_HOSTDEVICE const Vec2<int>& Vec2<int>::normalizeNonNull() IMATH_NOEXCEPT = delete;
  644. template <> IMATH_HOSTDEVICE Vec2<int> Vec2<int>::normalized() const IMATH_NOEXCEPT = delete;
  645. template <> Vec2<int> Vec2<int>::normalizedExc() const = delete;
  646. template <> IMATH_HOSTDEVICE Vec2<int> Vec2<int>::normalizedNonNull() const IMATH_NOEXCEPT = delete;
  647. // Vec2<int64_t>
  648. template <> IMATH_HOSTDEVICE int64_t Vec2<int64_t>::length() const IMATH_NOEXCEPT = delete;
  649. template <> IMATH_HOSTDEVICE const Vec2<int64_t>& Vec2<int64_t>::normalize() IMATH_NOEXCEPT = delete;
  650. template <> const Vec2<int64_t>& Vec2<int64_t>::normalizeExc() = delete;
  651. template <> IMATH_HOSTDEVICE const Vec2<int64_t>& Vec2<int64_t>::normalizeNonNull() IMATH_NOEXCEPT = delete;
  652. template <> IMATH_HOSTDEVICE Vec2<int64_t> Vec2<int64_t>::normalized() const IMATH_NOEXCEPT = delete;
  653. template <> Vec2<int64_t> Vec2<int64_t>::normalizedExc() const = delete;
  654. template <> IMATH_HOSTDEVICE Vec2<int64_t> Vec2<int64_t>::normalizedNonNull() const IMATH_NOEXCEPT = delete;
  655. // Vec3<short>
  656. template <> IMATH_HOSTDEVICE short Vec3<short>::length() const IMATH_NOEXCEPT = delete;
  657. template <> IMATH_HOSTDEVICE const Vec3<short>& Vec3<short>::normalize() IMATH_NOEXCEPT = delete;
  658. template <> const Vec3<short>& Vec3<short>::normalizeExc() = delete;
  659. template <> IMATH_HOSTDEVICE const Vec3<short>& Vec3<short>::normalizeNonNull() IMATH_NOEXCEPT = delete;
  660. template <> IMATH_HOSTDEVICE Vec3<short> Vec3<short>::normalized() const IMATH_NOEXCEPT = delete;
  661. template <> Vec3<short> Vec3<short>::normalizedExc() const = delete;
  662. template <> IMATH_HOSTDEVICE Vec3<short> Vec3<short>::normalizedNonNull() const IMATH_NOEXCEPT = delete;
  663. // Vec3<int>
  664. template <> IMATH_HOSTDEVICE int Vec3<int>::length() const IMATH_NOEXCEPT = delete;
  665. template <> IMATH_HOSTDEVICE const Vec3<int>& Vec3<int>::normalize() IMATH_NOEXCEPT = delete;
  666. template <> const Vec3<int>& Vec3<int>::normalizeExc() = delete;
  667. template <> IMATH_HOSTDEVICE const Vec3<int>& Vec3<int>::normalizeNonNull() IMATH_NOEXCEPT = delete;
  668. template <> IMATH_HOSTDEVICE Vec3<int> Vec3<int>::normalized() const IMATH_NOEXCEPT = delete;
  669. template <> Vec3<int> Vec3<int>::normalizedExc() const = delete;
  670. template <> IMATH_HOSTDEVICE Vec3<int> Vec3<int>::normalizedNonNull() const IMATH_NOEXCEPT = delete;
  671. // Vec3<int64_t>
  672. template <> IMATH_HOSTDEVICE int64_t Vec3<int64_t>::length() const IMATH_NOEXCEPT = delete;
  673. template <> IMATH_HOSTDEVICE const Vec3<int64_t>& Vec3<int64_t>::normalize() IMATH_NOEXCEPT = delete;
  674. template <> const Vec3<int64_t>& Vec3<int64_t>::normalizeExc() = delete;
  675. template <> IMATH_HOSTDEVICE const Vec3<int64_t>& Vec3<int64_t>::normalizeNonNull() IMATH_NOEXCEPT = delete;
  676. template <> IMATH_HOSTDEVICE Vec3<int64_t> Vec3<int64_t>::normalized() const IMATH_NOEXCEPT = delete;
  677. template <> Vec3<int64_t> Vec3<int64_t>::normalizedExc() const = delete;
  678. template <> IMATH_HOSTDEVICE Vec3<int64_t> Vec3<int64_t>::normalizedNonNull() const IMATH_NOEXCEPT = delete;
  679. // Vec4<short>
  680. template <> IMATH_HOSTDEVICE short Vec4<short>::length() const IMATH_NOEXCEPT = delete;
  681. template <> IMATH_HOSTDEVICE const Vec4<short>& Vec4<short>::normalize() IMATH_NOEXCEPT = delete;
  682. template <> const Vec4<short>& Vec4<short>::normalizeExc() = delete;
  683. template <> IMATH_HOSTDEVICE const Vec4<short>& Vec4<short>::normalizeNonNull() IMATH_NOEXCEPT = delete;
  684. template <> IMATH_HOSTDEVICE Vec4<short> Vec4<short>::normalized() const IMATH_NOEXCEPT = delete;
  685. template <> Vec4<short> Vec4<short>::normalizedExc() const = delete;
  686. template <> IMATH_HOSTDEVICE Vec4<short> Vec4<short>::normalizedNonNull() const IMATH_NOEXCEPT = delete;
  687. // Vec4<int>
  688. template <> IMATH_HOSTDEVICE int Vec4<int>::length() const IMATH_NOEXCEPT = delete;
  689. template <> IMATH_HOSTDEVICE const Vec4<int>& Vec4<int>::normalize() IMATH_NOEXCEPT = delete;
  690. template <> const Vec4<int>& Vec4<int>::normalizeExc() = delete;
  691. template <> IMATH_HOSTDEVICE const Vec4<int>& Vec4<int>::normalizeNonNull() IMATH_NOEXCEPT = delete;
  692. template <> IMATH_HOSTDEVICE Vec4<int> Vec4<int>::normalized() const IMATH_NOEXCEPT = delete;
  693. template <> Vec4<int> Vec4<int>::normalizedExc() const = delete;
  694. template <> IMATH_HOSTDEVICE Vec4<int> Vec4<int>::normalizedNonNull() const IMATH_NOEXCEPT = delete;
  695. // Vec4<int64_t>
  696. template <> IMATH_HOSTDEVICE int64_t Vec4<int64_t>::length() const IMATH_NOEXCEPT = delete;
  697. template <> IMATH_HOSTDEVICE const Vec4<int64_t>& Vec4<int64_t>::normalize() IMATH_NOEXCEPT = delete;
  698. template <> const Vec4<int64_t>& Vec4<int64_t>::normalizeExc() = delete;
  699. template <> IMATH_HOSTDEVICE const Vec4<int64_t>& Vec4<int64_t>::normalizeNonNull() IMATH_NOEXCEPT = delete;
  700. template <> IMATH_HOSTDEVICE Vec4<int64_t> Vec4<int64_t>::normalized() const IMATH_NOEXCEPT = delete;
  701. template <> Vec4<int64_t> Vec4<int64_t>::normalizedExc() const = delete;
  702. template <> IMATH_HOSTDEVICE Vec4<int64_t> Vec4<int64_t>::normalizedNonNull() const IMATH_NOEXCEPT = delete;
  703. /// @endcond Doxygen_Suppress
  704. //------------------------
  705. // Implementation of Vec2:
  706. //------------------------
  707. template <class T>
  708. IMATH_CONSTEXPR14 IMATH_HOSTDEVICE inline T&
  709. Vec2<T>::operator[] (int i) IMATH_NOEXCEPT
  710. {
  711. return (&x)[i]; // NOSONAR - suppress SonarCloud bug report.
  712. }
  713. template <class T>
  714. constexpr IMATH_HOSTDEVICE inline const T&
  715. Vec2<T>::operator[] (int i) const IMATH_NOEXCEPT
  716. {
  717. return (&x)[i]; // NOSONAR - suppress SonarCloud bug report.
  718. }
  719. template <class T> IMATH_HOSTDEVICE inline Vec2<T>::Vec2() IMATH_NOEXCEPT
  720. {
  721. // empty, and not constexpr because data is uninitialized.
  722. }
  723. template <class T> IMATH_HOSTDEVICE constexpr inline Vec2<T>::Vec2 (T a) IMATH_NOEXCEPT
  724. : x(a), y(a)
  725. {
  726. }
  727. template <class T> IMATH_HOSTDEVICE constexpr inline Vec2<T>::Vec2 (T a, T b) IMATH_NOEXCEPT
  728. : x(a), y(b)
  729. {
  730. }
  731. template <class T> IMATH_HOSTDEVICE constexpr inline Vec2<T>::Vec2 (const Vec2& v) IMATH_NOEXCEPT
  732. : x(v.x), y(v.y)
  733. {
  734. }
  735. template <class T> template <class S> IMATH_HOSTDEVICE constexpr inline Vec2<T>::Vec2 (const Vec2<S>& v) IMATH_NOEXCEPT
  736. : x(T(v.x)), y(T(v.y))
  737. {
  738. }
  739. template <class T>
  740. IMATH_CONSTEXPR14 IMATH_HOSTDEVICE inline const Vec2<T>&
  741. Vec2<T>::operator= (const Vec2& v) IMATH_NOEXCEPT
  742. {
  743. x = v.x;
  744. y = v.y;
  745. return *this;
  746. }
  747. template <class T>
  748. template <class S>
  749. IMATH_HOSTDEVICE inline void
  750. Vec2<T>::setValue (S a, S b) IMATH_NOEXCEPT
  751. {
  752. x = T (a);
  753. y = T (b);
  754. }
  755. template <class T>
  756. template <class S>
  757. IMATH_HOSTDEVICE inline void
  758. Vec2<T>::setValue (const Vec2<S>& v) IMATH_NOEXCEPT
  759. {
  760. x = T (v.x);
  761. y = T (v.y);
  762. }
  763. template <class T>
  764. template <class S>
  765. IMATH_HOSTDEVICE inline void
  766. Vec2<T>::getValue (S& a, S& b) const IMATH_NOEXCEPT
  767. {
  768. a = S (x);
  769. b = S (y);
  770. }
  771. template <class T>
  772. template <class S>
  773. IMATH_HOSTDEVICE inline void
  774. Vec2<T>::getValue (Vec2<S>& v) const IMATH_NOEXCEPT
  775. {
  776. v.x = S (x);
  777. v.y = S (y);
  778. }
  779. template <class T>
  780. IMATH_HOSTDEVICE inline T*
  781. Vec2<T>::getValue() IMATH_NOEXCEPT
  782. {
  783. return (T*) &x;
  784. }
  785. template <class T>
  786. IMATH_HOSTDEVICE inline const T*
  787. Vec2<T>::getValue() const IMATH_NOEXCEPT
  788. {
  789. return (const T*) &x;
  790. }
  791. template <class T>
  792. template <class S>
  793. IMATH_HOSTDEVICE constexpr inline bool
  794. Vec2<T>::operator== (const Vec2<S>& v) const IMATH_NOEXCEPT
  795. {
  796. return x == v.x && y == v.y;
  797. }
  798. template <class T>
  799. template <class S>
  800. IMATH_HOSTDEVICE constexpr inline bool
  801. Vec2<T>::operator!= (const Vec2<S>& v) const IMATH_NOEXCEPT
  802. {
  803. return x != v.x || y != v.y;
  804. }
  805. template <class T>
  806. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  807. Vec2<T>::equalWithAbsError (const Vec2<T>& v, T e) const IMATH_NOEXCEPT
  808. {
  809. for (int i = 0; i < 2; i++)
  810. if (!IMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*this)[i], v[i], e))
  811. return false;
  812. return true;
  813. }
  814. template <class T>
  815. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  816. Vec2<T>::equalWithRelError (const Vec2<T>& v, T e) const IMATH_NOEXCEPT
  817. {
  818. for (int i = 0; i < 2; i++)
  819. if (!IMATH_INTERNAL_NAMESPACE::equalWithRelError ((*this)[i], v[i], e))
  820. return false;
  821. return true;
  822. }
  823. template <class T>
  824. IMATH_HOSTDEVICE constexpr inline T
  825. Vec2<T>::dot (const Vec2& v) const IMATH_NOEXCEPT
  826. {
  827. return x * v.x + y * v.y;
  828. }
  829. template <class T>
  830. IMATH_HOSTDEVICE constexpr inline T
  831. Vec2<T>::operator^ (const Vec2& v) const IMATH_NOEXCEPT
  832. {
  833. return dot (v);
  834. }
  835. template <class T>
  836. IMATH_HOSTDEVICE constexpr inline T
  837. Vec2<T>::cross (const Vec2& v) const IMATH_NOEXCEPT
  838. {
  839. return x * v.y - y * v.x;
  840. }
  841. template <class T>
  842. IMATH_HOSTDEVICE constexpr inline T
  843. Vec2<T>::operator% (const Vec2& v) const IMATH_NOEXCEPT
  844. {
  845. return x * v.y - y * v.x;
  846. }
  847. template <class T>
  848. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec2<T>&
  849. Vec2<T>::operator+= (const Vec2& v) IMATH_NOEXCEPT
  850. {
  851. x += v.x;
  852. y += v.y;
  853. return *this;
  854. }
  855. template <class T>
  856. IMATH_HOSTDEVICE constexpr inline Vec2<T>
  857. Vec2<T>::operator+ (const Vec2& v) const IMATH_NOEXCEPT
  858. {
  859. return Vec2 (x + v.x, y + v.y);
  860. }
  861. template <class T>
  862. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec2<T>&
  863. Vec2<T>::operator-= (const Vec2& v) IMATH_NOEXCEPT
  864. {
  865. x -= v.x;
  866. y -= v.y;
  867. return *this;
  868. }
  869. template <class T>
  870. IMATH_HOSTDEVICE constexpr inline Vec2<T>
  871. Vec2<T>::operator- (const Vec2& v) const IMATH_NOEXCEPT
  872. {
  873. return Vec2 (x - v.x, y - v.y);
  874. }
  875. template <class T>
  876. IMATH_HOSTDEVICE constexpr inline Vec2<T>
  877. Vec2<T>::operator-() const IMATH_NOEXCEPT
  878. {
  879. return Vec2 (-x, -y);
  880. }
  881. template <class T>
  882. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec2<T>&
  883. Vec2<T>::negate() IMATH_NOEXCEPT
  884. {
  885. x = -x;
  886. y = -y;
  887. return *this;
  888. }
  889. template <class T>
  890. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec2<T>&
  891. Vec2<T>::operator*= (const Vec2& v) IMATH_NOEXCEPT
  892. {
  893. x *= v.x;
  894. y *= v.y;
  895. return *this;
  896. }
  897. template <class T>
  898. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec2<T>&
  899. Vec2<T>::operator*= (T a) IMATH_NOEXCEPT
  900. {
  901. x *= a;
  902. y *= a;
  903. return *this;
  904. }
  905. template <class T>
  906. IMATH_HOSTDEVICE constexpr inline Vec2<T>
  907. Vec2<T>::operator* (const Vec2& v) const IMATH_NOEXCEPT
  908. {
  909. return Vec2 (x * v.x, y * v.y);
  910. }
  911. template <class T>
  912. IMATH_HOSTDEVICE constexpr inline Vec2<T>
  913. Vec2<T>::operator* (T a) const IMATH_NOEXCEPT
  914. {
  915. return Vec2 (x * a, y * a);
  916. }
  917. template <class T>
  918. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec2<T>&
  919. Vec2<T>::operator/= (const Vec2& v) IMATH_NOEXCEPT
  920. {
  921. x /= v.x;
  922. y /= v.y;
  923. return *this;
  924. }
  925. template <class T>
  926. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec2<T>&
  927. Vec2<T>::operator/= (T a) IMATH_NOEXCEPT
  928. {
  929. x /= a;
  930. y /= a;
  931. return *this;
  932. }
  933. template <class T>
  934. IMATH_HOSTDEVICE constexpr inline Vec2<T>
  935. Vec2<T>::operator/ (const Vec2& v) const IMATH_NOEXCEPT
  936. {
  937. return Vec2 (x / v.x, y / v.y);
  938. }
  939. template <class T>
  940. IMATH_HOSTDEVICE constexpr inline Vec2<T>
  941. Vec2<T>::operator/ (T a) const IMATH_NOEXCEPT
  942. {
  943. return Vec2 (x / a, y / a);
  944. }
  945. template <class T>
  946. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
  947. Vec2<T>::lengthTiny() const IMATH_NOEXCEPT
  948. {
  949. T absX = std::abs(x);
  950. T absY = std::abs(y);
  951. T max = absX;
  952. if (max < absY)
  953. max = absY;
  954. if (IMATH_UNLIKELY(max == T (0)))
  955. return T (0);
  956. //
  957. // Do not replace the divisions by max with multiplications by 1/max.
  958. // Computing 1/max can overflow but the divisions below will always
  959. // produce results less than or equal to 1.
  960. //
  961. absX /= max;
  962. absY /= max;
  963. return max * std::sqrt (absX * absX + absY * absY);
  964. }
  965. template <class T>
  966. IMATH_HOSTDEVICE inline T
  967. Vec2<T>::length() const IMATH_NOEXCEPT
  968. {
  969. T length2 = dot (*this);
  970. if (IMATH_UNLIKELY(length2 < T (2) * std::numeric_limits<T>::min()))
  971. return lengthTiny();
  972. return std::sqrt (length2);
  973. }
  974. template <class T>
  975. IMATH_HOSTDEVICE constexpr inline T
  976. Vec2<T>::length2() const IMATH_NOEXCEPT
  977. {
  978. return dot (*this);
  979. }
  980. template <class T>
  981. IMATH_HOSTDEVICE inline const Vec2<T>&
  982. Vec2<T>::normalize() IMATH_NOEXCEPT
  983. {
  984. T l = length();
  985. if (IMATH_LIKELY(l != T (0)))
  986. {
  987. //
  988. // Do not replace the divisions by l with multiplications by 1/l.
  989. // Computing 1/l can overflow but the divisions below will always
  990. // produce results less than or equal to 1.
  991. //
  992. x /= l;
  993. y /= l;
  994. }
  995. return *this;
  996. }
  997. template <class T>
  998. inline const Vec2<T>&
  999. Vec2<T>::normalizeExc()
  1000. {
  1001. T l = length();
  1002. if (IMATH_UNLIKELY(l == T (0)))
  1003. throw std::domain_error ("Cannot normalize null vector.");
  1004. x /= l;
  1005. y /= l;
  1006. return *this;
  1007. }
  1008. template <class T>
  1009. IMATH_HOSTDEVICE inline const Vec2<T>&
  1010. Vec2<T>::normalizeNonNull() IMATH_NOEXCEPT
  1011. {
  1012. T l = length();
  1013. x /= l;
  1014. y /= l;
  1015. return *this;
  1016. }
  1017. template <class T>
  1018. IMATH_HOSTDEVICE inline Vec2<T>
  1019. Vec2<T>::normalized() const IMATH_NOEXCEPT
  1020. {
  1021. T l = length();
  1022. if (IMATH_UNLIKELY(l == T (0)))
  1023. return Vec2 (T (0));
  1024. return Vec2 (x / l, y / l);
  1025. }
  1026. template <class T>
  1027. inline Vec2<T>
  1028. Vec2<T>::normalizedExc() const
  1029. {
  1030. T l = length();
  1031. if (IMATH_UNLIKELY(l == T (0)))
  1032. throw std::domain_error ("Cannot normalize null vector.");
  1033. return Vec2 (x / l, y / l);
  1034. }
  1035. template <class T>
  1036. IMATH_HOSTDEVICE inline Vec2<T>
  1037. Vec2<T>::normalizedNonNull() const IMATH_NOEXCEPT
  1038. {
  1039. T l = length();
  1040. return Vec2 (x / l, y / l);
  1041. }
  1042. //-----------------------
  1043. // Implementation of Vec3
  1044. //-----------------------
  1045. template <class T>
  1046. IMATH_HOSTDEVICE
  1047. IMATH_CONSTEXPR14 inline T&
  1048. Vec3<T>::operator[] (int i) IMATH_NOEXCEPT
  1049. {
  1050. return (&x)[i]; // NOSONAR - suppress SonarCloud bug report.
  1051. }
  1052. template <class T>
  1053. IMATH_HOSTDEVICE constexpr inline const T&
  1054. Vec3<T>::operator[] (int i) const IMATH_NOEXCEPT
  1055. {
  1056. return (&x)[i]; // NOSONAR - suppress SonarCloud bug report.
  1057. }
  1058. template <class T> IMATH_HOSTDEVICE inline Vec3<T>::Vec3() IMATH_NOEXCEPT
  1059. {
  1060. // empty, and not constexpr because data is uninitialized.
  1061. }
  1062. template <class T> IMATH_HOSTDEVICE constexpr inline Vec3<T>::Vec3 (T a) IMATH_NOEXCEPT
  1063. : x(a), y(a), z(a)
  1064. {
  1065. }
  1066. template <class T> IMATH_HOSTDEVICE constexpr inline Vec3<T>::Vec3 (T a, T b, T c) IMATH_NOEXCEPT
  1067. : x(a), y(b), z(c)
  1068. {
  1069. }
  1070. template <class T> IMATH_HOSTDEVICE constexpr inline Vec3<T>::Vec3 (const Vec3& v) IMATH_NOEXCEPT
  1071. : x(v.x), y(v.y), z(v.z)
  1072. {
  1073. }
  1074. template <class T> template <class S>
  1075. IMATH_HOSTDEVICE constexpr inline Vec3<T>::Vec3 (const Vec3<S>& v) IMATH_NOEXCEPT
  1076. : x(T(v.x)), y(T(v.y)), z(T(v.z))
  1077. {
  1078. }
  1079. template <class T>
  1080. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec3<T>&
  1081. Vec3<T>::operator= (const Vec3& v) IMATH_NOEXCEPT
  1082. {
  1083. x = v.x;
  1084. y = v.y;
  1085. z = v.z;
  1086. return *this;
  1087. }
  1088. template <class T> template <class S>
  1089. IMATH_HOSTDEVICE constexpr inline Vec3<T>::Vec3 (const Vec4<S>& v) IMATH_NOEXCEPT
  1090. : x(T(v.x/v.w)), y(T(v.y/v.w)), z(T(v.z/v.w))
  1091. {
  1092. }
  1093. template <class T>
  1094. template <class S>
  1095. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline Vec3<T>::Vec3 (const Vec4<S>& v, InfException)
  1096. {
  1097. T vx = T (v.x);
  1098. T vy = T (v.y);
  1099. T vz = T (v.z);
  1100. T vw = T (v.w);
  1101. T absW = (vw >= T (0)) ? vw : -vw;
  1102. if (absW < 1)
  1103. {
  1104. T m = baseTypeMax() * absW;
  1105. if (vx <= -m || vx >= m || vy <= -m || vy >= m || vz <= -m || vz >= m)
  1106. throw std::domain_error ("Cannot normalize point at infinity.");
  1107. }
  1108. x = vx / vw;
  1109. y = vy / vw;
  1110. z = vz / vw;
  1111. }
  1112. template <class T>
  1113. template <class S>
  1114. IMATH_HOSTDEVICE inline void
  1115. Vec3<T>::setValue (S a, S b, S c) IMATH_NOEXCEPT
  1116. {
  1117. x = T (a);
  1118. y = T (b);
  1119. z = T (c);
  1120. }
  1121. template <class T>
  1122. template <class S>
  1123. IMATH_HOSTDEVICE inline void
  1124. Vec3<T>::setValue (const Vec3<S>& v) IMATH_NOEXCEPT
  1125. {
  1126. x = T (v.x);
  1127. y = T (v.y);
  1128. z = T (v.z);
  1129. }
  1130. template <class T>
  1131. template <class S>
  1132. IMATH_HOSTDEVICE inline void
  1133. Vec3<T>::getValue (S& a, S& b, S& c) const IMATH_NOEXCEPT
  1134. {
  1135. a = S (x);
  1136. b = S (y);
  1137. c = S (z);
  1138. }
  1139. template <class T>
  1140. template <class S>
  1141. IMATH_HOSTDEVICE inline void
  1142. Vec3<T>::getValue (Vec3<S>& v) const IMATH_NOEXCEPT
  1143. {
  1144. v.x = S (x);
  1145. v.y = S (y);
  1146. v.z = S (z);
  1147. }
  1148. template <class T>
  1149. IMATH_HOSTDEVICE inline T*
  1150. Vec3<T>::getValue() IMATH_NOEXCEPT
  1151. {
  1152. return (T*) &x;
  1153. }
  1154. template <class T>
  1155. IMATH_HOSTDEVICE inline const T*
  1156. Vec3<T>::getValue() const IMATH_NOEXCEPT
  1157. {
  1158. return (const T*) &x;
  1159. }
  1160. template <class T>
  1161. template <class S>
  1162. IMATH_HOSTDEVICE constexpr inline bool
  1163. Vec3<T>::operator== (const Vec3<S>& v) const IMATH_NOEXCEPT
  1164. {
  1165. return x == v.x && y == v.y && z == v.z;
  1166. }
  1167. template <class T>
  1168. template <class S>
  1169. IMATH_HOSTDEVICE constexpr inline bool
  1170. Vec3<T>::operator!= (const Vec3<S>& v) const IMATH_NOEXCEPT
  1171. {
  1172. return x != v.x || y != v.y || z != v.z;
  1173. }
  1174. template <class T>
  1175. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  1176. Vec3<T>::equalWithAbsError (const Vec3<T>& v, T e) const IMATH_NOEXCEPT
  1177. {
  1178. for (int i = 0; i < 3; i++)
  1179. if (!IMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*this)[i], v[i], e))
  1180. return false;
  1181. return true;
  1182. }
  1183. template <class T>
  1184. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  1185. Vec3<T>::equalWithRelError (const Vec3<T>& v, T e) const IMATH_NOEXCEPT
  1186. {
  1187. for (int i = 0; i < 3; i++)
  1188. if (!IMATH_INTERNAL_NAMESPACE::equalWithRelError ((*this)[i], v[i], e))
  1189. return false;
  1190. return true;
  1191. }
  1192. template <class T>
  1193. IMATH_HOSTDEVICE constexpr inline T
  1194. Vec3<T>::dot (const Vec3& v) const IMATH_NOEXCEPT
  1195. {
  1196. return x * v.x + y * v.y + z * v.z;
  1197. }
  1198. template <class T>
  1199. IMATH_HOSTDEVICE constexpr inline T
  1200. Vec3<T>::operator^ (const Vec3& v) const IMATH_NOEXCEPT
  1201. {
  1202. return dot (v);
  1203. }
  1204. template <class T>
  1205. IMATH_HOSTDEVICE constexpr inline Vec3<T>
  1206. Vec3<T>::cross (const Vec3& v) const IMATH_NOEXCEPT
  1207. {
  1208. return Vec3 (y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
  1209. }
  1210. template <class T>
  1211. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec3<T>&
  1212. Vec3<T>::operator%= (const Vec3& v) IMATH_NOEXCEPT
  1213. {
  1214. T a = y * v.z - z * v.y;
  1215. T b = z * v.x - x * v.z;
  1216. T c = x * v.y - y * v.x;
  1217. x = a;
  1218. y = b;
  1219. z = c;
  1220. return *this;
  1221. }
  1222. template <class T>
  1223. IMATH_HOSTDEVICE constexpr inline Vec3<T>
  1224. Vec3<T>::operator% (const Vec3& v) const IMATH_NOEXCEPT
  1225. {
  1226. return Vec3 (y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
  1227. }
  1228. template <class T>
  1229. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec3<T>&
  1230. Vec3<T>::operator+= (const Vec3& v) IMATH_NOEXCEPT
  1231. {
  1232. x += v.x;
  1233. y += v.y;
  1234. z += v.z;
  1235. return *this;
  1236. }
  1237. template <class T>
  1238. IMATH_HOSTDEVICE constexpr inline Vec3<T>
  1239. Vec3<T>::operator+ (const Vec3& v) const IMATH_NOEXCEPT
  1240. {
  1241. return Vec3 (x + v.x, y + v.y, z + v.z);
  1242. }
  1243. template <class T>
  1244. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec3<T>&
  1245. Vec3<T>::operator-= (const Vec3& v) IMATH_NOEXCEPT
  1246. {
  1247. x -= v.x;
  1248. y -= v.y;
  1249. z -= v.z;
  1250. return *this;
  1251. }
  1252. template <class T>
  1253. IMATH_HOSTDEVICE constexpr inline Vec3<T>
  1254. Vec3<T>::operator- (const Vec3& v) const IMATH_NOEXCEPT
  1255. {
  1256. return Vec3 (x - v.x, y - v.y, z - v.z);
  1257. }
  1258. template <class T>
  1259. IMATH_HOSTDEVICE constexpr inline Vec3<T>
  1260. Vec3<T>::operator-() const IMATH_NOEXCEPT
  1261. {
  1262. return Vec3 (-x, -y, -z);
  1263. }
  1264. template <class T>
  1265. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec3<T>&
  1266. Vec3<T>::negate() IMATH_NOEXCEPT
  1267. {
  1268. x = -x;
  1269. y = -y;
  1270. z = -z;
  1271. return *this;
  1272. }
  1273. template <class T>
  1274. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec3<T>&
  1275. Vec3<T>::operator*= (const Vec3& v) IMATH_NOEXCEPT
  1276. {
  1277. x *= v.x;
  1278. y *= v.y;
  1279. z *= v.z;
  1280. return *this;
  1281. }
  1282. template <class T>
  1283. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec3<T>&
  1284. Vec3<T>::operator*= (T a) IMATH_NOEXCEPT
  1285. {
  1286. x *= a;
  1287. y *= a;
  1288. z *= a;
  1289. return *this;
  1290. }
  1291. template <class T>
  1292. IMATH_HOSTDEVICE constexpr inline Vec3<T>
  1293. Vec3<T>::operator* (const Vec3& v) const IMATH_NOEXCEPT
  1294. {
  1295. return Vec3 (x * v.x, y * v.y, z * v.z);
  1296. }
  1297. template <class T>
  1298. IMATH_HOSTDEVICE constexpr inline Vec3<T>
  1299. Vec3<T>::operator* (T a) const IMATH_NOEXCEPT
  1300. {
  1301. return Vec3 (x * a, y * a, z * a);
  1302. }
  1303. template <class T>
  1304. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec3<T>&
  1305. Vec3<T>::operator/= (const Vec3& v) IMATH_NOEXCEPT
  1306. {
  1307. x /= v.x;
  1308. y /= v.y;
  1309. z /= v.z;
  1310. return *this;
  1311. }
  1312. template <class T>
  1313. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec3<T>&
  1314. Vec3<T>::operator/= (T a) IMATH_NOEXCEPT
  1315. {
  1316. x /= a;
  1317. y /= a;
  1318. z /= a;
  1319. return *this;
  1320. }
  1321. template <class T>
  1322. IMATH_HOSTDEVICE constexpr inline Vec3<T>
  1323. Vec3<T>::operator/ (const Vec3& v) const IMATH_NOEXCEPT
  1324. {
  1325. return Vec3 (x / v.x, y / v.y, z / v.z);
  1326. }
  1327. template <class T>
  1328. IMATH_HOSTDEVICE constexpr inline Vec3<T>
  1329. Vec3<T>::operator/ (T a) const IMATH_NOEXCEPT
  1330. {
  1331. return Vec3 (x / a, y / a, z / a);
  1332. }
  1333. template <class T>
  1334. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
  1335. Vec3<T>::lengthTiny() const IMATH_NOEXCEPT
  1336. {
  1337. T absX = (x >= T (0)) ? x : -x;
  1338. T absY = (y >= T (0)) ? y : -y;
  1339. T absZ = (z >= T (0)) ? z : -z;
  1340. T max = absX;
  1341. if (max < absY)
  1342. max = absY;
  1343. if (max < absZ)
  1344. max = absZ;
  1345. if (IMATH_UNLIKELY(max == T (0)))
  1346. return T (0);
  1347. //
  1348. // Do not replace the divisions by max with multiplications by 1/max.
  1349. // Computing 1/max can overflow but the divisions below will always
  1350. // produce results less than or equal to 1.
  1351. //
  1352. absX /= max;
  1353. absY /= max;
  1354. absZ /= max;
  1355. return max * std::sqrt (absX * absX + absY * absY + absZ * absZ);
  1356. }
  1357. template <class T>
  1358. IMATH_HOSTDEVICE inline T
  1359. Vec3<T>::length() const IMATH_NOEXCEPT
  1360. {
  1361. T length2 = dot (*this);
  1362. if (IMATH_UNLIKELY(length2 < T (2) * std::numeric_limits<T>::min()))
  1363. return lengthTiny();
  1364. return std::sqrt (length2);
  1365. }
  1366. template <class T>
  1367. IMATH_HOSTDEVICE constexpr inline T
  1368. Vec3<T>::length2() const IMATH_NOEXCEPT
  1369. {
  1370. return dot (*this);
  1371. }
  1372. template <class T>
  1373. IMATH_HOSTDEVICE inline const Vec3<T>&
  1374. Vec3<T>::normalize() IMATH_NOEXCEPT
  1375. {
  1376. T l = length();
  1377. if (IMATH_LIKELY(l != T (0)))
  1378. {
  1379. //
  1380. // Do not replace the divisions by l with multiplications by 1/l.
  1381. // Computing 1/l can overflow but the divisions below will always
  1382. // produce results less than or equal to 1.
  1383. //
  1384. x /= l;
  1385. y /= l;
  1386. z /= l;
  1387. }
  1388. return *this;
  1389. }
  1390. template <class T>
  1391. inline const Vec3<T>&
  1392. Vec3<T>::normalizeExc()
  1393. {
  1394. T l = length();
  1395. if (IMATH_UNLIKELY(l == T (0)))
  1396. throw std::domain_error ("Cannot normalize null vector.");
  1397. x /= l;
  1398. y /= l;
  1399. z /= l;
  1400. return *this;
  1401. }
  1402. template <class T>
  1403. IMATH_HOSTDEVICE inline const Vec3<T>&
  1404. Vec3<T>::normalizeNonNull() IMATH_NOEXCEPT
  1405. {
  1406. T l = length();
  1407. x /= l;
  1408. y /= l;
  1409. z /= l;
  1410. return *this;
  1411. }
  1412. template <class T>
  1413. IMATH_HOSTDEVICE inline Vec3<T>
  1414. Vec3<T>::normalized() const IMATH_NOEXCEPT
  1415. {
  1416. T l = length();
  1417. if (IMATH_UNLIKELY((l == T (0))))
  1418. return Vec3 (T (0));
  1419. return Vec3 (x / l, y / l, z / l);
  1420. }
  1421. template <class T>
  1422. inline Vec3<T>
  1423. Vec3<T>::normalizedExc() const
  1424. {
  1425. T l = length();
  1426. if (IMATH_UNLIKELY(l == T (0)))
  1427. throw std::domain_error ("Cannot normalize null vector.");
  1428. return Vec3 (x / l, y / l, z / l);
  1429. }
  1430. template <class T>
  1431. IMATH_HOSTDEVICE inline Vec3<T>
  1432. Vec3<T>::normalizedNonNull() const IMATH_NOEXCEPT
  1433. {
  1434. T l = length();
  1435. return Vec3 (x / l, y / l, z / l);
  1436. }
  1437. //-----------------------
  1438. // Implementation of Vec4
  1439. //-----------------------
  1440. template <class T>
  1441. IMATH_HOSTDEVICE
  1442. IMATH_CONSTEXPR14 inline T&
  1443. Vec4<T>::operator[] (int i) IMATH_NOEXCEPT
  1444. {
  1445. return (&x)[i]; // NOSONAR - suppress SonarCloud bug report.
  1446. }
  1447. template <class T>
  1448. IMATH_HOSTDEVICE constexpr inline const T&
  1449. Vec4<T>::operator[] (int i) const IMATH_NOEXCEPT
  1450. {
  1451. return (&x)[i]; // NOSONAR - suppress SonarCloud bug report.
  1452. }
  1453. template <class T> IMATH_HOSTDEVICE inline Vec4<T>::Vec4() IMATH_NOEXCEPT
  1454. {
  1455. // empty, and not constexpr because data is uninitialized.
  1456. }
  1457. template <class T> IMATH_HOSTDEVICE constexpr inline Vec4<T>::Vec4 (T a) IMATH_NOEXCEPT
  1458. : x(a), y(a), z(a), w(a)
  1459. {
  1460. }
  1461. template <class T> IMATH_HOSTDEVICE constexpr inline Vec4<T>::Vec4 (T a, T b, T c, T d) IMATH_NOEXCEPT
  1462. : x(a), y(b), z(c), w(d)
  1463. {
  1464. }
  1465. template <class T> IMATH_HOSTDEVICE constexpr inline Vec4<T>::Vec4 (const Vec4& v) IMATH_NOEXCEPT
  1466. : x(v.x), y(v.y), z(v.z), w(v.w)
  1467. {
  1468. }
  1469. template <class T> template <class S>
  1470. IMATH_HOSTDEVICE constexpr inline Vec4<T>::Vec4 (const Vec4<S>& v) IMATH_NOEXCEPT
  1471. : x(T(v.x)), y(T(v.y)), z(T(v.z)), w(T(v.w))
  1472. {
  1473. }
  1474. template <class T>
  1475. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec4<T>&
  1476. Vec4<T>::operator= (const Vec4& v) IMATH_NOEXCEPT
  1477. {
  1478. x = v.x;
  1479. y = v.y;
  1480. z = v.z;
  1481. w = v.w;
  1482. return *this;
  1483. }
  1484. template <class T> template <class S>
  1485. IMATH_HOSTDEVICE constexpr inline Vec4<T>::Vec4 (const Vec3<S>& v) IMATH_NOEXCEPT
  1486. : x(T(v.x)), y(T(v.y)), z(T(v.z)), w(T(1))
  1487. {
  1488. }
  1489. template <class T>
  1490. template <class S>
  1491. IMATH_HOSTDEVICE constexpr inline bool
  1492. Vec4<T>::operator== (const Vec4<S>& v) const IMATH_NOEXCEPT
  1493. {
  1494. return x == v.x && y == v.y && z == v.z && w == v.w;
  1495. }
  1496. template <class T>
  1497. template <class S>
  1498. IMATH_HOSTDEVICE constexpr inline bool
  1499. Vec4<T>::operator!= (const Vec4<S>& v) const IMATH_NOEXCEPT
  1500. {
  1501. return x != v.x || y != v.y || z != v.z || w != v.w;
  1502. }
  1503. template <class T>
  1504. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  1505. Vec4<T>::equalWithAbsError (const Vec4<T>& v, T e) const IMATH_NOEXCEPT
  1506. {
  1507. for (int i = 0; i < 4; i++)
  1508. if (!IMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*this)[i], v[i], e))
  1509. return false;
  1510. return true;
  1511. }
  1512. template <class T>
  1513. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  1514. Vec4<T>::equalWithRelError (const Vec4<T>& v, T e) const IMATH_NOEXCEPT
  1515. {
  1516. for (int i = 0; i < 4; i++)
  1517. if (!IMATH_INTERNAL_NAMESPACE::equalWithRelError ((*this)[i], v[i], e))
  1518. return false;
  1519. return true;
  1520. }
  1521. template <class T>
  1522. IMATH_HOSTDEVICE constexpr inline T
  1523. Vec4<T>::dot (const Vec4& v) const IMATH_NOEXCEPT
  1524. {
  1525. return x * v.x + y * v.y + z * v.z + w * v.w;
  1526. }
  1527. template <class T>
  1528. IMATH_HOSTDEVICE constexpr inline T
  1529. Vec4<T>::operator^ (const Vec4& v) const IMATH_NOEXCEPT
  1530. {
  1531. return dot (v);
  1532. }
  1533. template <class T>
  1534. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec4<T>&
  1535. Vec4<T>::operator+= (const Vec4& v) IMATH_NOEXCEPT
  1536. {
  1537. x += v.x;
  1538. y += v.y;
  1539. z += v.z;
  1540. w += v.w;
  1541. return *this;
  1542. }
  1543. template <class T>
  1544. IMATH_HOSTDEVICE constexpr inline Vec4<T>
  1545. Vec4<T>::operator+ (const Vec4& v) const IMATH_NOEXCEPT
  1546. {
  1547. return Vec4 (x + v.x, y + v.y, z + v.z, w + v.w);
  1548. }
  1549. template <class T>
  1550. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec4<T>&
  1551. Vec4<T>::operator-= (const Vec4& v) IMATH_NOEXCEPT
  1552. {
  1553. x -= v.x;
  1554. y -= v.y;
  1555. z -= v.z;
  1556. w -= v.w;
  1557. return *this;
  1558. }
  1559. template <class T>
  1560. IMATH_HOSTDEVICE constexpr inline Vec4<T>
  1561. Vec4<T>::operator- (const Vec4& v) const IMATH_NOEXCEPT
  1562. {
  1563. return Vec4 (x - v.x, y - v.y, z - v.z, w - v.w);
  1564. }
  1565. template <class T>
  1566. IMATH_HOSTDEVICE constexpr inline Vec4<T>
  1567. Vec4<T>::operator-() const IMATH_NOEXCEPT
  1568. {
  1569. return Vec4 (-x, -y, -z, -w);
  1570. }
  1571. template <class T>
  1572. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec4<T>&
  1573. Vec4<T>::negate() IMATH_NOEXCEPT
  1574. {
  1575. x = -x;
  1576. y = -y;
  1577. z = -z;
  1578. w = -w;
  1579. return *this;
  1580. }
  1581. template <class T>
  1582. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec4<T>&
  1583. Vec4<T>::operator*= (const Vec4& v) IMATH_NOEXCEPT
  1584. {
  1585. x *= v.x;
  1586. y *= v.y;
  1587. z *= v.z;
  1588. w *= v.w;
  1589. return *this;
  1590. }
  1591. template <class T>
  1592. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec4<T>&
  1593. Vec4<T>::operator*= (T a) IMATH_NOEXCEPT
  1594. {
  1595. x *= a;
  1596. y *= a;
  1597. z *= a;
  1598. w *= a;
  1599. return *this;
  1600. }
  1601. template <class T>
  1602. IMATH_HOSTDEVICE constexpr inline Vec4<T>
  1603. Vec4<T>::operator* (const Vec4& v) const IMATH_NOEXCEPT
  1604. {
  1605. return Vec4 (x * v.x, y * v.y, z * v.z, w * v.w);
  1606. }
  1607. template <class T>
  1608. IMATH_HOSTDEVICE constexpr inline Vec4<T>
  1609. Vec4<T>::operator* (T a) const IMATH_NOEXCEPT
  1610. {
  1611. return Vec4 (x * a, y * a, z * a, w * a);
  1612. }
  1613. template <class T>
  1614. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec4<T>&
  1615. Vec4<T>::operator/= (const Vec4& v) IMATH_NOEXCEPT
  1616. {
  1617. x /= v.x;
  1618. y /= v.y;
  1619. z /= v.z;
  1620. w /= v.w;
  1621. return *this;
  1622. }
  1623. template <class T>
  1624. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline const Vec4<T>&
  1625. Vec4<T>::operator/= (T a) IMATH_NOEXCEPT
  1626. {
  1627. x /= a;
  1628. y /= a;
  1629. z /= a;
  1630. w /= a;
  1631. return *this;
  1632. }
  1633. template <class T>
  1634. IMATH_HOSTDEVICE constexpr inline Vec4<T>
  1635. Vec4<T>::operator/ (const Vec4& v) const IMATH_NOEXCEPT
  1636. {
  1637. return Vec4 (x / v.x, y / v.y, z / v.z, w / v.w);
  1638. }
  1639. template <class T>
  1640. IMATH_HOSTDEVICE constexpr inline Vec4<T>
  1641. Vec4<T>::operator/ (T a) const IMATH_NOEXCEPT
  1642. {
  1643. return Vec4 (x / a, y / a, z / a, w / a);
  1644. }
  1645. template <class T>
  1646. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline T
  1647. Vec4<T>::lengthTiny() const IMATH_NOEXCEPT
  1648. {
  1649. T absX = (x >= T (0)) ? x : -x;
  1650. T absY = (y >= T (0)) ? y : -y;
  1651. T absZ = (z >= T (0)) ? z : -z;
  1652. T absW = (w >= T (0)) ? w : -w;
  1653. T max = absX;
  1654. if (max < absY)
  1655. max = absY;
  1656. if (max < absZ)
  1657. max = absZ;
  1658. if (max < absW)
  1659. max = absW;
  1660. if (IMATH_UNLIKELY(max == T (0)))
  1661. return T (0);
  1662. //
  1663. // Do not replace the divisions by max with multiplications by 1/max.
  1664. // Computing 1/max can overflow but the divisions below will always
  1665. // produce results less than or equal to 1.
  1666. //
  1667. absX /= max;
  1668. absY /= max;
  1669. absZ /= max;
  1670. absW /= max;
  1671. return max * std::sqrt (absX * absX + absY * absY + absZ * absZ + absW * absW);
  1672. }
  1673. template <class T>
  1674. IMATH_HOSTDEVICE inline T
  1675. Vec4<T>::length() const IMATH_NOEXCEPT
  1676. {
  1677. T length2 = dot (*this);
  1678. if (IMATH_UNLIKELY(length2 < T (2) * std::numeric_limits<T>::min()))
  1679. return lengthTiny();
  1680. return std::sqrt (length2);
  1681. }
  1682. template <class T>
  1683. IMATH_HOSTDEVICE constexpr inline T
  1684. Vec4<T>::length2() const IMATH_NOEXCEPT
  1685. {
  1686. return dot (*this);
  1687. }
  1688. template <class T>
  1689. IMATH_HOSTDEVICE const inline Vec4<T>&
  1690. Vec4<T>::normalize() IMATH_NOEXCEPT
  1691. {
  1692. T l = length();
  1693. if (IMATH_LIKELY(l != T (0)))
  1694. {
  1695. //
  1696. // Do not replace the divisions by l with multiplications by 1/l.
  1697. // Computing 1/l can overflow but the divisions below will always
  1698. // produce results less than or equal to 1.
  1699. //
  1700. x /= l;
  1701. y /= l;
  1702. z /= l;
  1703. w /= l;
  1704. }
  1705. return *this;
  1706. }
  1707. template <class T>
  1708. const inline Vec4<T>&
  1709. Vec4<T>::normalizeExc()
  1710. {
  1711. T l = length();
  1712. if (IMATH_UNLIKELY(l == T (0)))
  1713. throw std::domain_error ("Cannot normalize null vector.");
  1714. x /= l;
  1715. y /= l;
  1716. z /= l;
  1717. w /= l;
  1718. return *this;
  1719. }
  1720. template <class T>
  1721. IMATH_HOSTDEVICE inline const Vec4<T>&
  1722. Vec4<T>::normalizeNonNull() IMATH_NOEXCEPT
  1723. {
  1724. T l = length();
  1725. x /= l;
  1726. y /= l;
  1727. z /= l;
  1728. w /= l;
  1729. return *this;
  1730. }
  1731. template <class T>
  1732. IMATH_HOSTDEVICE inline Vec4<T>
  1733. Vec4<T>::normalized() const IMATH_NOEXCEPT
  1734. {
  1735. T l = length();
  1736. if (IMATH_UNLIKELY(l == T (0)))
  1737. return Vec4 (T (0));
  1738. return Vec4 (x / l, y / l, z / l, w / l);
  1739. }
  1740. template <class T>
  1741. inline Vec4<T>
  1742. Vec4<T>::normalizedExc() const
  1743. {
  1744. T l = length();
  1745. if (IMATH_UNLIKELY(l == T (0)))
  1746. throw std::domain_error ("Cannot normalize null vector.");
  1747. return Vec4 (x / l, y / l, z / l, w / l);
  1748. }
  1749. template <class T>
  1750. IMATH_HOSTDEVICE inline Vec4<T>
  1751. Vec4<T>::normalizedNonNull() const IMATH_NOEXCEPT
  1752. {
  1753. T l = length();
  1754. return Vec4 (x / l, y / l, z / l, w / l);
  1755. }
  1756. //-----------------------------
  1757. // Stream output implementation
  1758. //-----------------------------
  1759. template <class T>
  1760. std::ostream&
  1761. operator<< (std::ostream& s, const Vec2<T>& v)
  1762. {
  1763. return s << '(' << v.x << ' ' << v.y << ')';
  1764. }
  1765. template <class T>
  1766. std::ostream&
  1767. operator<< (std::ostream& s, const Vec3<T>& v)
  1768. {
  1769. return s << '(' << v.x << ' ' << v.y << ' ' << v.z << ')';
  1770. }
  1771. template <class T>
  1772. std::ostream&
  1773. operator<< (std::ostream& s, const Vec4<T>& v)
  1774. {
  1775. return s << '(' << v.x << ' ' << v.y << ' ' << v.z << ' ' << v.w << ')';
  1776. }
  1777. //-----------------------------------------
  1778. // Implementation of reverse multiplication
  1779. //-----------------------------------------
  1780. template <class T>
  1781. IMATH_HOSTDEVICE constexpr inline Vec2<T>
  1782. operator* (T a, const Vec2<T>& v) IMATH_NOEXCEPT
  1783. {
  1784. return Vec2<T> (a * v.x, a * v.y);
  1785. }
  1786. template <class T>
  1787. IMATH_HOSTDEVICE constexpr inline Vec3<T>
  1788. operator* (T a, const Vec3<T>& v) IMATH_NOEXCEPT
  1789. {
  1790. return Vec3<T> (a * v.x, a * v.y, a * v.z);
  1791. }
  1792. template <class T>
  1793. IMATH_HOSTDEVICE constexpr inline Vec4<T>
  1794. operator* (T a, const Vec4<T>& v) IMATH_NOEXCEPT
  1795. {
  1796. return Vec4<T> (a * v.x, a * v.y, a * v.z, a * v.w);
  1797. }
  1798. #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
  1799. # pragma warning(pop)
  1800. #endif
  1801. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  1802. #endif // INCLUDED_IMATHVEC_H