ImathVec.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002-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. //----------------------------------------------------------------------------
  35. //
  36. // Specializations of the Vec2<T> and Vec3<T> templates.
  37. //
  38. //----------------------------------------------------------------------------
  39. #include "ImathVec.h"
  40. #include "ImathExport.h"
  41. #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
  42. // suppress exception specification warnings
  43. #pragma warning(disable:4290)
  44. #endif
  45. IMATH_INTERNAL_NAMESPACE_SOURCE_ENTER
  46. namespace
  47. {
  48. template<class T>
  49. bool
  50. normalizeOrThrow(Vec2<T> &v)
  51. {
  52. int axis = -1;
  53. for (int i = 0; i < 2; i ++)
  54. {
  55. if (v[i] != 0)
  56. {
  57. if (axis != -1)
  58. {
  59. throw IntVecNormalizeExc ("Cannot normalize an integer "
  60. "vector unless it is parallel "
  61. "to a principal axis");
  62. }
  63. axis = i;
  64. }
  65. }
  66. v[axis] = (v[axis] > 0) ? 1 : -1;
  67. return true;
  68. }
  69. template<class T>
  70. bool
  71. normalizeOrThrow(Vec3<T> &v)
  72. {
  73. int axis = -1;
  74. for (int i = 0; i < 3; i ++)
  75. {
  76. if (v[i] != 0)
  77. {
  78. if (axis != -1)
  79. {
  80. throw IntVecNormalizeExc ("Cannot normalize an integer "
  81. "vector unless it is parallel "
  82. "to a principal axis");
  83. }
  84. axis = i;
  85. }
  86. }
  87. v[axis] = (v[axis] > 0) ? 1 : -1;
  88. return true;
  89. }
  90. template<class T>
  91. bool
  92. normalizeOrThrow(Vec4<T> &v)
  93. {
  94. int axis = -1;
  95. for (int i = 0; i < 4; i ++)
  96. {
  97. if (v[i] != 0)
  98. {
  99. if (axis != -1)
  100. {
  101. throw IntVecNormalizeExc ("Cannot normalize an integer "
  102. "vector unless it is parallel "
  103. "to a principal axis");
  104. }
  105. axis = i;
  106. }
  107. }
  108. v[axis] = (v[axis] > 0) ? 1 : -1;
  109. return true;
  110. }
  111. }
  112. // Vec2<short>
  113. template <>
  114. IMATH_EXPORT
  115. short
  116. Vec2<short>::length () const
  117. {
  118. float lenF = Math<float>::sqrt ((float)dot (*this));
  119. short lenS = (short) (lenF + 0.5f);
  120. return lenS;
  121. }
  122. template <>
  123. IMATH_EXPORT
  124. const Vec2<short> &
  125. Vec2<short>::normalize ()
  126. {
  127. normalizeOrThrow<short>(*this);
  128. return *this;
  129. }
  130. template <>
  131. IMATH_EXPORT
  132. const Vec2<short> &
  133. Vec2<short>::normalizeExc ()
  134. {
  135. if ((x == 0) && (y == 0))
  136. throw NullVecExc ("Cannot normalize null vector.");
  137. normalizeOrThrow<short>(*this);
  138. return *this;
  139. }
  140. template <>
  141. IMATH_EXPORT
  142. const Vec2<short> &
  143. Vec2<short>::normalizeNonNull ()
  144. {
  145. normalizeOrThrow<short>(*this);
  146. return *this;
  147. }
  148. template <>
  149. IMATH_EXPORT
  150. Vec2<short>
  151. Vec2<short>::normalized () const
  152. {
  153. Vec2<short> v(*this);
  154. normalizeOrThrow<short>(v);
  155. return v;
  156. }
  157. template <>
  158. IMATH_EXPORT
  159. Vec2<short>
  160. Vec2<short>::normalizedExc () const
  161. {
  162. if ((x == 0) && (y == 0))
  163. throw NullVecExc ("Cannot normalize null vector.");
  164. Vec2<short> v(*this);
  165. normalizeOrThrow<short>(v);
  166. return v;
  167. }
  168. template <>
  169. IMATH_EXPORT
  170. Vec2<short>
  171. Vec2<short>::normalizedNonNull () const
  172. {
  173. Vec2<short> v(*this);
  174. normalizeOrThrow<short>(v);
  175. return v;
  176. }
  177. // Vec2<int>
  178. template <>
  179. IMATH_EXPORT
  180. int
  181. Vec2<int>::length () const
  182. {
  183. float lenF = Math<float>::sqrt ((float)dot (*this));
  184. int lenI = (int) (lenF + 0.5f);
  185. return lenI;
  186. }
  187. template <>
  188. IMATH_EXPORT
  189. const Vec2<int> &
  190. Vec2<int>::normalize ()
  191. {
  192. normalizeOrThrow<int>(*this);
  193. return *this;
  194. }
  195. template <>
  196. IMATH_EXPORT
  197. const Vec2<int> &
  198. Vec2<int>::normalizeExc ()
  199. {
  200. if ((x == 0) && (y == 0))
  201. throw NullVecExc ("Cannot normalize null vector.");
  202. normalizeOrThrow<int>(*this);
  203. return *this;
  204. }
  205. template <>
  206. IMATH_EXPORT
  207. const Vec2<int> &
  208. Vec2<int>::normalizeNonNull ()
  209. {
  210. normalizeOrThrow<int>(*this);
  211. return *this;
  212. }
  213. template <>
  214. IMATH_EXPORT
  215. Vec2<int>
  216. Vec2<int>::normalized () const
  217. {
  218. Vec2<int> v(*this);
  219. normalizeOrThrow<int>(v);
  220. return v;
  221. }
  222. template <>
  223. IMATH_EXPORT
  224. Vec2<int>
  225. Vec2<int>::normalizedExc () const
  226. {
  227. if ((x == 0) && (y == 0))
  228. throw NullVecExc ("Cannot normalize null vector.");
  229. Vec2<int> v(*this);
  230. normalizeOrThrow<int>(v);
  231. return v;
  232. }
  233. template <>
  234. IMATH_EXPORT
  235. Vec2<int>
  236. Vec2<int>::normalizedNonNull () const
  237. {
  238. Vec2<int> v(*this);
  239. normalizeOrThrow<int>(v);
  240. return v;
  241. }
  242. // Vec3<short>
  243. template <>
  244. IMATH_EXPORT
  245. short
  246. Vec3<short>::length () const
  247. {
  248. float lenF = Math<float>::sqrt ((float)dot (*this));
  249. short lenS = (short) (lenF + 0.5f);
  250. return lenS;
  251. }
  252. template <>
  253. IMATH_EXPORT
  254. const Vec3<short> &
  255. Vec3<short>::normalize ()
  256. {
  257. normalizeOrThrow<short>(*this);
  258. return *this;
  259. }
  260. template <>
  261. IMATH_EXPORT
  262. const Vec3<short> &
  263. Vec3<short>::normalizeExc ()
  264. {
  265. if ((x == 0) && (y == 0) && (z == 0))
  266. throw NullVecExc ("Cannot normalize null vector.");
  267. normalizeOrThrow<short>(*this);
  268. return *this;
  269. }
  270. template <>
  271. IMATH_EXPORT
  272. const Vec3<short> &
  273. Vec3<short>::normalizeNonNull ()
  274. {
  275. normalizeOrThrow<short>(*this);
  276. return *this;
  277. }
  278. template <>
  279. IMATH_EXPORT
  280. Vec3<short>
  281. Vec3<short>::normalized () const
  282. {
  283. Vec3<short> v(*this);
  284. normalizeOrThrow<short>(v);
  285. return v;
  286. }
  287. template <>
  288. IMATH_EXPORT
  289. Vec3<short>
  290. Vec3<short>::normalizedExc () const
  291. {
  292. if ((x == 0) && (y == 0) && (z == 0))
  293. throw NullVecExc ("Cannot normalize null vector.");
  294. Vec3<short> v(*this);
  295. normalizeOrThrow<short>(v);
  296. return v;
  297. }
  298. template <>
  299. IMATH_EXPORT
  300. Vec3<short>
  301. Vec3<short>::normalizedNonNull () const
  302. {
  303. Vec3<short> v(*this);
  304. normalizeOrThrow<short>(v);
  305. return v;
  306. }
  307. // Vec3<int>
  308. template <>
  309. IMATH_EXPORT
  310. int
  311. Vec3<int>::length () const
  312. {
  313. float lenF = Math<float>::sqrt ((float)dot (*this));
  314. int lenI = (int) (lenF + 0.5f);
  315. return lenI;
  316. }
  317. template <>
  318. IMATH_EXPORT
  319. const Vec3<int> &
  320. Vec3<int>::normalize ()
  321. {
  322. normalizeOrThrow<int>(*this);
  323. return *this;
  324. }
  325. template <>
  326. IMATH_EXPORT
  327. const Vec3<int> &
  328. Vec3<int>::normalizeExc ()
  329. {
  330. if ((x == 0) && (y == 0) && (z == 0))
  331. throw NullVecExc ("Cannot normalize null vector.");
  332. normalizeOrThrow<int>(*this);
  333. return *this;
  334. }
  335. template <>
  336. IMATH_EXPORT
  337. const Vec3<int> &
  338. Vec3<int>::normalizeNonNull ()
  339. {
  340. normalizeOrThrow<int>(*this);
  341. return *this;
  342. }
  343. template <>
  344. IMATH_EXPORT
  345. Vec3<int>
  346. Vec3<int>::normalized () const
  347. {
  348. Vec3<int> v(*this);
  349. normalizeOrThrow<int>(v);
  350. return v;
  351. }
  352. template <>
  353. IMATH_EXPORT
  354. Vec3<int>
  355. Vec3<int>::normalizedExc () const
  356. {
  357. if ((x == 0) && (y == 0) && (z == 0))
  358. throw NullVecExc ("Cannot normalize null vector.");
  359. Vec3<int> v(*this);
  360. normalizeOrThrow<int>(v);
  361. return v;
  362. }
  363. template <>
  364. IMATH_EXPORT
  365. Vec3<int>
  366. Vec3<int>::normalizedNonNull () const
  367. {
  368. Vec3<int> v(*this);
  369. normalizeOrThrow<int>(v);
  370. return v;
  371. }
  372. // Vec4<short>
  373. template <>
  374. IMATH_EXPORT
  375. short
  376. Vec4<short>::length () const
  377. {
  378. float lenF = Math<float>::sqrt ((float)dot (*this));
  379. short lenS = (short) (lenF + 0.5f);
  380. return lenS;
  381. }
  382. template <>
  383. IMATH_EXPORT
  384. const Vec4<short> &
  385. Vec4<short>::normalize ()
  386. {
  387. normalizeOrThrow<short>(*this);
  388. return *this;
  389. }
  390. template <>
  391. IMATH_EXPORT
  392. const Vec4<short> &
  393. Vec4<short>::normalizeExc ()
  394. {
  395. if ((x == 0) && (y == 0) && (z == 0) && (w == 0))
  396. throw NullVecExc ("Cannot normalize null vector.");
  397. normalizeOrThrow<short>(*this);
  398. return *this;
  399. }
  400. template <>
  401. IMATH_EXPORT
  402. const Vec4<short> &
  403. Vec4<short>::normalizeNonNull ()
  404. {
  405. normalizeOrThrow<short>(*this);
  406. return *this;
  407. }
  408. template <>
  409. IMATH_EXPORT
  410. Vec4<short>
  411. Vec4<short>::normalized () const
  412. {
  413. Vec4<short> v(*this);
  414. normalizeOrThrow<short>(v);
  415. return v;
  416. }
  417. template <>
  418. IMATH_EXPORT
  419. Vec4<short>
  420. Vec4<short>::normalizedExc () const
  421. {
  422. if ((x == 0) && (y == 0) && (z == 0) && (w == 0))
  423. throw NullVecExc ("Cannot normalize null vector.");
  424. Vec4<short> v(*this);
  425. normalizeOrThrow<short>(v);
  426. return v;
  427. }
  428. template <>
  429. IMATH_EXPORT
  430. Vec4<short>
  431. Vec4<short>::normalizedNonNull () const
  432. {
  433. Vec4<short> v(*this);
  434. normalizeOrThrow<short>(v);
  435. return v;
  436. }
  437. // Vec4<int>
  438. template <>
  439. IMATH_EXPORT
  440. int
  441. Vec4<int>::length () const
  442. {
  443. float lenF = Math<float>::sqrt ((float)dot (*this));
  444. int lenI = (int) (lenF + 0.5f);
  445. return lenI;
  446. }
  447. template <>
  448. IMATH_EXPORT
  449. const Vec4<int> &
  450. Vec4<int>::normalize ()
  451. {
  452. normalizeOrThrow<int>(*this);
  453. return *this;
  454. }
  455. template <>
  456. IMATH_EXPORT
  457. const Vec4<int> &
  458. Vec4<int>::normalizeExc ()
  459. {
  460. if ((x == 0) && (y == 0) && (z == 0) && (w == 0))
  461. throw NullVecExc ("Cannot normalize null vector.");
  462. normalizeOrThrow<int>(*this);
  463. return *this;
  464. }
  465. template <>
  466. IMATH_EXPORT
  467. const Vec4<int> &
  468. Vec4<int>::normalizeNonNull ()
  469. {
  470. normalizeOrThrow<int>(*this);
  471. return *this;
  472. }
  473. template <>
  474. IMATH_EXPORT
  475. Vec4<int>
  476. Vec4<int>::normalized () const
  477. {
  478. Vec4<int> v(*this);
  479. normalizeOrThrow<int>(v);
  480. return v;
  481. }
  482. template <>
  483. IMATH_EXPORT
  484. Vec4<int>
  485. Vec4<int>::normalizedExc () const
  486. {
  487. if ((x == 0) && (y == 0) && (z == 0) && (w == 0))
  488. throw NullVecExc ("Cannot normalize null vector.");
  489. Vec4<int> v(*this);
  490. normalizeOrThrow<int>(v);
  491. return v;
  492. }
  493. template <>
  494. IMATH_EXPORT
  495. Vec4<int>
  496. Vec4<int>::normalizedNonNull () const
  497. {
  498. Vec4<int> v(*this);
  499. normalizeOrThrow<int>(v);
  500. return v;
  501. }
  502. IMATH_INTERNAL_NAMESPACE_SOURCE_EXIT