ImathShear.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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_IMATHSHEAR_H
  35. #define INCLUDED_IMATHSHEAR_H
  36. //----------------------------------------------------
  37. //
  38. // Shear6 class template.
  39. //
  40. //----------------------------------------------------
  41. #include "ImathExc.h"
  42. #include "ImathLimits.h"
  43. #include "ImathMath.h"
  44. #include "ImathVec.h"
  45. #include "ImathNamespace.h"
  46. #include <iostream>
  47. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  48. template <class T> class Shear6
  49. {
  50. public:
  51. //-------------------
  52. // Access to elements
  53. //-------------------
  54. T xy, xz, yz, yx, zx, zy;
  55. T & operator [] (int i);
  56. const T & operator [] (int i) const;
  57. //-------------
  58. // Constructors
  59. //-------------
  60. Shear6 (); // (0 0 0 0 0 0)
  61. Shear6 (T XY, T XZ, T YZ); // (XY XZ YZ 0 0 0)
  62. Shear6 (const Vec3<T> &v); // (v.x v.y v.z 0 0 0)
  63. template <class S> // (v.x v.y v.z 0 0 0)
  64. Shear6 (const Vec3<S> &v);
  65. Shear6 (T XY, T XZ, T YZ, // (XY XZ YZ YX ZX ZY)
  66. T YX, T ZX, T ZY);
  67. //---------------------------------
  68. // Copy constructors and assignment
  69. //---------------------------------
  70. Shear6 (const Shear6 &h);
  71. template <class S> Shear6 (const Shear6<S> &h);
  72. const Shear6 & operator = (const Shear6 &h);
  73. template <class S>
  74. const Shear6 & operator = (const Vec3<S> &v);
  75. //----------------------
  76. // Compatibility with Sb
  77. //----------------------
  78. template <class S>
  79. void setValue (S XY, S XZ, S YZ, S YX, S ZX, S ZY);
  80. template <class S>
  81. void setValue (const Shear6<S> &h);
  82. template <class S>
  83. void getValue (S &XY, S &XZ, S &YZ,
  84. S &YX, S &ZX, S &ZY) const;
  85. template <class S>
  86. void getValue (Shear6<S> &h) const;
  87. T * getValue();
  88. const T * getValue() const;
  89. //---------
  90. // Equality
  91. //---------
  92. template <class S>
  93. bool operator == (const Shear6<S> &h) const;
  94. template <class S>
  95. bool operator != (const Shear6<S> &h) const;
  96. //-----------------------------------------------------------------------
  97. // Compare two shears and test if they are "approximately equal":
  98. //
  99. // equalWithAbsError (h, e)
  100. //
  101. // Returns true if the coefficients of this and h are the same with
  102. // an absolute error of no more than e, i.e., for all i
  103. //
  104. // abs (this[i] - h[i]) <= e
  105. //
  106. // equalWithRelError (h, e)
  107. //
  108. // Returns true if the coefficients of this and h are the same with
  109. // a relative error of no more than e, i.e., for all i
  110. //
  111. // abs (this[i] - h[i]) <= e * abs (this[i])
  112. //-----------------------------------------------------------------------
  113. bool equalWithAbsError (const Shear6<T> &h, T e) const;
  114. bool equalWithRelError (const Shear6<T> &h, T e) const;
  115. //------------------------
  116. // Component-wise addition
  117. //------------------------
  118. const Shear6 & operator += (const Shear6 &h);
  119. Shear6 operator + (const Shear6 &h) const;
  120. //---------------------------
  121. // Component-wise subtraction
  122. //---------------------------
  123. const Shear6 & operator -= (const Shear6 &h);
  124. Shear6 operator - (const Shear6 &h) const;
  125. //------------------------------------
  126. // Component-wise multiplication by -1
  127. //------------------------------------
  128. Shear6 operator - () const;
  129. const Shear6 & negate ();
  130. //------------------------------
  131. // Component-wise multiplication
  132. //------------------------------
  133. const Shear6 & operator *= (const Shear6 &h);
  134. const Shear6 & operator *= (T a);
  135. Shear6 operator * (const Shear6 &h) const;
  136. Shear6 operator * (T a) const;
  137. //------------------------
  138. // Component-wise division
  139. //------------------------
  140. const Shear6 & operator /= (const Shear6 &h);
  141. const Shear6 & operator /= (T a);
  142. Shear6 operator / (const Shear6 &h) const;
  143. Shear6 operator / (T a) const;
  144. //----------------------------------------------------------
  145. // Number of dimensions, i.e. number of elements in a Shear6
  146. //----------------------------------------------------------
  147. static unsigned int dimensions() {return 6;}
  148. //-------------------------------------------------
  149. // Limitations of type T (see also class limits<T>)
  150. //-------------------------------------------------
  151. static T baseTypeMin() {return limits<T>::min();}
  152. static T baseTypeMax() {return limits<T>::max();}
  153. static T baseTypeSmallest() {return limits<T>::smallest();}
  154. static T baseTypeEpsilon() {return limits<T>::epsilon();}
  155. //--------------------------------------------------------------
  156. // Base type -- in templates, which accept a parameter, V, which
  157. // could be either a Vec2<T> or a Shear6<T>, you can refer to T as
  158. // V::BaseType
  159. //--------------------------------------------------------------
  160. typedef T BaseType;
  161. };
  162. //--------------
  163. // Stream output
  164. //--------------
  165. template <class T>
  166. std::ostream & operator << (std::ostream &s, const Shear6<T> &h);
  167. //----------------------------------------------------
  168. // Reverse multiplication: scalar * Shear6<T>
  169. //----------------------------------------------------
  170. template <class S, class T> Shear6<T> operator * (S a, const Shear6<T> &h);
  171. //-------------------------
  172. // Typedefs for convenience
  173. //-------------------------
  174. typedef Vec3 <float> Shear3f;
  175. typedef Vec3 <double> Shear3d;
  176. typedef Shear6 <float> Shear6f;
  177. typedef Shear6 <double> Shear6d;
  178. //-----------------------
  179. // Implementation of Shear6
  180. //-----------------------
  181. template <class T>
  182. inline T &
  183. Shear6<T>::operator [] (int i)
  184. {
  185. return (&xy)[i];
  186. }
  187. template <class T>
  188. inline const T &
  189. Shear6<T>::operator [] (int i) const
  190. {
  191. return (&xy)[i];
  192. }
  193. template <class T>
  194. inline
  195. Shear6<T>::Shear6 ()
  196. {
  197. xy = xz = yz = yx = zx = zy = 0;
  198. }
  199. template <class T>
  200. inline
  201. Shear6<T>::Shear6 (T XY, T XZ, T YZ)
  202. {
  203. xy = XY;
  204. xz = XZ;
  205. yz = YZ;
  206. yx = 0;
  207. zx = 0;
  208. zy = 0;
  209. }
  210. template <class T>
  211. inline
  212. Shear6<T>::Shear6 (const Vec3<T> &v)
  213. {
  214. xy = v.x;
  215. xz = v.y;
  216. yz = v.z;
  217. yx = 0;
  218. zx = 0;
  219. zy = 0;
  220. }
  221. template <class T>
  222. template <class S>
  223. inline
  224. Shear6<T>::Shear6 (const Vec3<S> &v)
  225. {
  226. xy = T (v.x);
  227. xz = T (v.y);
  228. yz = T (v.z);
  229. yx = 0;
  230. zx = 0;
  231. zy = 0;
  232. }
  233. template <class T>
  234. inline
  235. Shear6<T>::Shear6 (T XY, T XZ, T YZ, T YX, T ZX, T ZY)
  236. {
  237. xy = XY;
  238. xz = XZ;
  239. yz = YZ;
  240. yx = YX;
  241. zx = ZX;
  242. zy = ZY;
  243. }
  244. template <class T>
  245. inline
  246. Shear6<T>::Shear6 (const Shear6 &h)
  247. {
  248. xy = h.xy;
  249. xz = h.xz;
  250. yz = h.yz;
  251. yx = h.yx;
  252. zx = h.zx;
  253. zy = h.zy;
  254. }
  255. template <class T>
  256. template <class S>
  257. inline
  258. Shear6<T>::Shear6 (const Shear6<S> &h)
  259. {
  260. xy = T (h.xy);
  261. xz = T (h.xz);
  262. yz = T (h.yz);
  263. yx = T (h.yx);
  264. zx = T (h.zx);
  265. zy = T (h.zy);
  266. }
  267. template <class T>
  268. inline const Shear6<T> &
  269. Shear6<T>::operator = (const Shear6 &h)
  270. {
  271. xy = h.xy;
  272. xz = h.xz;
  273. yz = h.yz;
  274. yx = h.yx;
  275. zx = h.zx;
  276. zy = h.zy;
  277. return *this;
  278. }
  279. template <class T>
  280. template <class S>
  281. inline const Shear6<T> &
  282. Shear6<T>::operator = (const Vec3<S> &v)
  283. {
  284. xy = T (v.x);
  285. xz = T (v.y);
  286. yz = T (v.z);
  287. yx = 0;
  288. zx = 0;
  289. zy = 0;
  290. return *this;
  291. }
  292. template <class T>
  293. template <class S>
  294. inline void
  295. Shear6<T>::setValue (S XY, S XZ, S YZ, S YX, S ZX, S ZY)
  296. {
  297. xy = T (XY);
  298. xz = T (XZ);
  299. yz = T (YZ);
  300. yx = T (YX);
  301. zx = T (ZX);
  302. zy = T (ZY);
  303. }
  304. template <class T>
  305. template <class S>
  306. inline void
  307. Shear6<T>::setValue (const Shear6<S> &h)
  308. {
  309. xy = T (h.xy);
  310. xz = T (h.xz);
  311. yz = T (h.yz);
  312. yx = T (h.yx);
  313. zx = T (h.zx);
  314. zy = T (h.zy);
  315. }
  316. template <class T>
  317. template <class S>
  318. inline void
  319. Shear6<T>::getValue (S &XY, S &XZ, S &YZ, S &YX, S &ZX, S &ZY) const
  320. {
  321. XY = S (xy);
  322. XZ = S (xz);
  323. YZ = S (yz);
  324. YX = S (yx);
  325. ZX = S (zx);
  326. ZY = S (zy);
  327. }
  328. template <class T>
  329. template <class S>
  330. inline void
  331. Shear6<T>::getValue (Shear6<S> &h) const
  332. {
  333. h.xy = S (xy);
  334. h.xz = S (xz);
  335. h.yz = S (yz);
  336. h.yx = S (yx);
  337. h.zx = S (zx);
  338. h.zy = S (zy);
  339. }
  340. template <class T>
  341. inline T *
  342. Shear6<T>::getValue()
  343. {
  344. return (T *) &xy;
  345. }
  346. template <class T>
  347. inline const T *
  348. Shear6<T>::getValue() const
  349. {
  350. return (const T *) &xy;
  351. }
  352. template <class T>
  353. template <class S>
  354. inline bool
  355. Shear6<T>::operator == (const Shear6<S> &h) const
  356. {
  357. return xy == h.xy && xz == h.xz && yz == h.yz &&
  358. yx == h.yx && zx == h.zx && zy == h.zy;
  359. }
  360. template <class T>
  361. template <class S>
  362. inline bool
  363. Shear6<T>::operator != (const Shear6<S> &h) const
  364. {
  365. return xy != h.xy || xz != h.xz || yz != h.yz ||
  366. yx != h.yx || zx != h.zx || zy != h.zy;
  367. }
  368. template <class T>
  369. bool
  370. Shear6<T>::equalWithAbsError (const Shear6<T> &h, T e) const
  371. {
  372. for (int i = 0; i < 6; i++)
  373. if (!IMATH_INTERNAL_NAMESPACE::equalWithAbsError ((*this)[i], h[i], e))
  374. return false;
  375. return true;
  376. }
  377. template <class T>
  378. bool
  379. Shear6<T>::equalWithRelError (const Shear6<T> &h, T e) const
  380. {
  381. for (int i = 0; i < 6; i++)
  382. if (!IMATH_INTERNAL_NAMESPACE::equalWithRelError ((*this)[i], h[i], e))
  383. return false;
  384. return true;
  385. }
  386. template <class T>
  387. inline const Shear6<T> &
  388. Shear6<T>::operator += (const Shear6 &h)
  389. {
  390. xy += h.xy;
  391. xz += h.xz;
  392. yz += h.yz;
  393. yx += h.yx;
  394. zx += h.zx;
  395. zy += h.zy;
  396. return *this;
  397. }
  398. template <class T>
  399. inline Shear6<T>
  400. Shear6<T>::operator + (const Shear6 &h) const
  401. {
  402. return Shear6 (xy + h.xy, xz + h.xz, yz + h.yz,
  403. yx + h.yx, zx + h.zx, zy + h.zy);
  404. }
  405. template <class T>
  406. inline const Shear6<T> &
  407. Shear6<T>::operator -= (const Shear6 &h)
  408. {
  409. xy -= h.xy;
  410. xz -= h.xz;
  411. yz -= h.yz;
  412. yx -= h.yx;
  413. zx -= h.zx;
  414. zy -= h.zy;
  415. return *this;
  416. }
  417. template <class T>
  418. inline Shear6<T>
  419. Shear6<T>::operator - (const Shear6 &h) const
  420. {
  421. return Shear6 (xy - h.xy, xz - h.xz, yz - h.yz,
  422. yx - h.yx, zx - h.zx, zy - h.zy);
  423. }
  424. template <class T>
  425. inline Shear6<T>
  426. Shear6<T>::operator - () const
  427. {
  428. return Shear6 (-xy, -xz, -yz, -yx, -zx, -zy);
  429. }
  430. template <class T>
  431. inline const Shear6<T> &
  432. Shear6<T>::negate ()
  433. {
  434. xy = -xy;
  435. xz = -xz;
  436. yz = -yz;
  437. yx = -yx;
  438. zx = -zx;
  439. zy = -zy;
  440. return *this;
  441. }
  442. template <class T>
  443. inline const Shear6<T> &
  444. Shear6<T>::operator *= (const Shear6 &h)
  445. {
  446. xy *= h.xy;
  447. xz *= h.xz;
  448. yz *= h.yz;
  449. yx *= h.yx;
  450. zx *= h.zx;
  451. zy *= h.zy;
  452. return *this;
  453. }
  454. template <class T>
  455. inline const Shear6<T> &
  456. Shear6<T>::operator *= (T a)
  457. {
  458. xy *= a;
  459. xz *= a;
  460. yz *= a;
  461. yx *= a;
  462. zx *= a;
  463. zy *= a;
  464. return *this;
  465. }
  466. template <class T>
  467. inline Shear6<T>
  468. Shear6<T>::operator * (const Shear6 &h) const
  469. {
  470. return Shear6 (xy * h.xy, xz * h.xz, yz * h.yz,
  471. yx * h.yx, zx * h.zx, zy * h.zy);
  472. }
  473. template <class T>
  474. inline Shear6<T>
  475. Shear6<T>::operator * (T a) const
  476. {
  477. return Shear6 (xy * a, xz * a, yz * a,
  478. yx * a, zx * a, zy * a);
  479. }
  480. template <class T>
  481. inline const Shear6<T> &
  482. Shear6<T>::operator /= (const Shear6 &h)
  483. {
  484. xy /= h.xy;
  485. xz /= h.xz;
  486. yz /= h.yz;
  487. yx /= h.yx;
  488. zx /= h.zx;
  489. zy /= h.zy;
  490. return *this;
  491. }
  492. template <class T>
  493. inline const Shear6<T> &
  494. Shear6<T>::operator /= (T a)
  495. {
  496. xy /= a;
  497. xz /= a;
  498. yz /= a;
  499. yx /= a;
  500. zx /= a;
  501. zy /= a;
  502. return *this;
  503. }
  504. template <class T>
  505. inline Shear6<T>
  506. Shear6<T>::operator / (const Shear6 &h) const
  507. {
  508. return Shear6 (xy / h.xy, xz / h.xz, yz / h.yz,
  509. yx / h.yx, zx / h.zx, zy / h.zy);
  510. }
  511. template <class T>
  512. inline Shear6<T>
  513. Shear6<T>::operator / (T a) const
  514. {
  515. return Shear6 (xy / a, xz / a, yz / a,
  516. yx / a, zx / a, zy / a);
  517. }
  518. //-----------------------------
  519. // Stream output implementation
  520. //-----------------------------
  521. template <class T>
  522. std::ostream &
  523. operator << (std::ostream &s, const Shear6<T> &h)
  524. {
  525. return s << '('
  526. << h.xy << ' ' << h.xz << ' ' << h.yz
  527. << h.yx << ' ' << h.zx << ' ' << h.zy
  528. << ')';
  529. }
  530. //-----------------------------------------
  531. // Implementation of reverse multiplication
  532. //-----------------------------------------
  533. template <class S, class T>
  534. inline Shear6<T>
  535. operator * (S a, const Shear6<T> &h)
  536. {
  537. return Shear6<T> (a * h.xy, a * h.xz, a * h.yz,
  538. a * h.yx, a * h.zx, a * h.zy);
  539. }
  540. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  541. #endif // INCLUDED_IMATHSHEAR_H