ImathVec.h 43 KB

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