PageRenderTime 4082ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 1ms

/extern/prmath/vector4float.hpp

https://github.com/joeld42/luddite
C++ Header | 581 lines | 442 code | 85 blank | 54 comment | 25 complexity | b2b7b1cb863b4974a95f295f41bce204 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. Twilight Prophecy SDK
  3. A multi-platform development system for virtual reality and multimedia.
  4. Copyright (C) 1997-2003 Twilight 3D Finland Oy Ltd.
  5. */
  6. /*
  7. Abstract:
  8. Meta Expression Template based specialization of prmath::Vector4<float>
  9. Reference material:
  10. Jim Blinn's Corner: Notation, Notation, Notation ISBN 1-55860-860-5
  11. Tomas Arce: Faster Vector Math Using Templates http://www.flipcode.com/tutorials/tut_fastmath.shtml
  12. */
  13. #ifndef PRMATH_VECTOR4FLOAT_HPP
  14. #define PRMATH_VECTOR4FLOAT_HPP
  15. #ifndef PRMATH_VECTOR4_HPP
  16. #error prmath: "do not include this header yourself!"
  17. #endif
  18. #include "metavector.hpp"
  19. #ifdef PRMATH_METAVECTOR_ENABLE
  20. namespace prmath
  21. {
  22. // expression
  23. template <typename E>
  24. class MetaExpV4F
  25. {
  26. E exp;
  27. public:
  28. metainline MetaExpV4F(const E& e)
  29. : exp(e)
  30. {
  31. }
  32. metainline float operator [] (const int index) const
  33. {
  34. return exp[index];
  35. }
  36. metainline MetaExpV4F operator + () const
  37. {
  38. return *this;
  39. }
  40. metainline const MetaExpV4F< MetaNEG<float,E> > operator - () const
  41. {
  42. return MetaExpV4F< MetaNEG<float,E> >(exp);
  43. }
  44. };
  45. template <>
  46. class MetaExpV4F<float>
  47. {
  48. float exp;
  49. public:
  50. metainline MetaExpV4F(const float& e)
  51. : exp(e)
  52. {
  53. }
  54. metainline float operator [] (const int index) const
  55. {
  56. return exp;
  57. }
  58. };
  59. // vector4<float>
  60. template <>
  61. struct Vector4<float> : BaseVector<float,4>
  62. {
  63. // members
  64. union { float x,r; };
  65. union { float y,g; };
  66. union { float z,b; };
  67. union { float w,a; };
  68. // constructors
  69. metainline Vector4()
  70. {
  71. }
  72. metainline Vector4(float sx, float sy, float sz, float sw)
  73. : x(sx), y(sy), z(sz), w(sw)
  74. {
  75. }
  76. metainline Vector4(const float v[])
  77. : x(v[0]), y(v[1]), z(v[2]), w(v[3])
  78. {
  79. }
  80. metainline Vector4(const Vector4& v)
  81. : x(v.x), y(v.y), z(v.z), w(v.w)
  82. {
  83. }
  84. metainline Vector4(float v)
  85. : x(v), y(v), z(v), w(v)
  86. {
  87. }
  88. template <typename E>
  89. metainline Vector4(const MetaExpV4F<E>& exp)
  90. {
  91. x = exp[0];
  92. y = exp[1];
  93. z = exp[2];
  94. w = exp[3];
  95. }
  96. // operators
  97. template <typename E>
  98. metainline Vector4<float>& operator += (const MetaExpV4F<E>& exp)
  99. {
  100. x += exp[0];
  101. y += exp[1];
  102. z += exp[2];
  103. w += exp[3];
  104. return *this;
  105. }
  106. template <typename E>
  107. metainline Vector4<float>& operator -= (const MetaExpV4F<E>& exp)
  108. {
  109. x -= exp[0];
  110. y -= exp[1];
  111. z -= exp[2];
  112. w -= exp[3];
  113. return *this;
  114. }
  115. template <typename E>
  116. metainline Vector4<float>& operator *= (const MetaExpV4F<E>& exp)
  117. {
  118. x *= exp[0];
  119. y *= exp[1];
  120. z *= exp[2];
  121. w *= exp[3];
  122. return *this;
  123. }
  124. template <typename E>
  125. metainline Vector4<float>& operator = (const MetaExpV4F<E>& exp)
  126. {
  127. x = exp[0];
  128. y = exp[1];
  129. z = exp[2];
  130. w = exp[3];
  131. return *this;
  132. }
  133. metainline Vector4 operator + () const
  134. {
  135. return *this;
  136. }
  137. metainline Vector4 operator - () const
  138. {
  139. return Vector4(-x,-y,-z,-w);
  140. }
  141. metainline Vector4& operator += (const Vector4& v)
  142. {
  143. x += v.x;
  144. y += v.y;
  145. z += v.z;
  146. w += v.w;
  147. return *this;
  148. }
  149. metainline Vector4& operator -= (const Vector4& v)
  150. {
  151. x -= v.x;
  152. y -= v.y;
  153. z -= v.z;
  154. w -= v.w;
  155. return *this;
  156. }
  157. metainline Vector4& operator *= (const Vector4& v)
  158. {
  159. x *= v.x;
  160. y *= v.y;
  161. z *= v.z;
  162. w *= v.w;
  163. return *this;
  164. }
  165. metainline Vector4& operator = (const Vector4& v)
  166. {
  167. x = v.x;
  168. y = v.y;
  169. z = v.z;
  170. w = v.w;
  171. return *this;
  172. }
  173. metainline Vector4& operator = (float v)
  174. {
  175. x = v;
  176. y = v;
  177. z = v;
  178. w = v;
  179. return *this;
  180. }
  181. metainline Vector4& operator *= (float s)
  182. {
  183. x *= s;
  184. y *= s;
  185. z *= s;
  186. w *= s;
  187. return *this;
  188. }
  189. metainline Vector4& operator /= (float s)
  190. {
  191. assert( s != 0 );
  192. float is = 1 / s;
  193. x *= is;
  194. y *= is;
  195. z *= is;
  196. w *= is;
  197. return *this;
  198. }
  199. // methods
  200. void SetVector(float sx, float sy, float sz, float sw)
  201. {
  202. x = sx;
  203. y = sy;
  204. z = sz;
  205. w = sw;
  206. }
  207. void SetVector(float v)
  208. {
  209. x = v;
  210. y = v;
  211. z = v;
  212. w = v;
  213. }
  214. };
  215. // meta operators
  216. // exp + exp
  217. template <typename A, typename B>
  218. metainline const MetaExpV4F< MetaADD< float,MetaExpV4F<A>,MetaExpV4F<B> > >
  219. operator + (const MetaExpV4F<A>& a, const MetaExpV4F<B>& b)
  220. {
  221. typedef MetaADD< float,MetaExpV4F<A>,MetaExpV4F<B> > exp;
  222. return MetaExpV4F<exp>(exp(a,b));
  223. }
  224. // exp + vec4f
  225. template <typename A>
  226. metainline const MetaExpV4F< MetaADD< float,MetaExpV4F<A>,MetaExpV4F< Vector4<float> > > >
  227. operator + (const MetaExpV4F<A>& a, const Vector4<float>& b)
  228. {
  229. typedef MetaADD< float,MetaExpV4F<A>,MetaExpV4F< Vector4<float> > > exp;
  230. return MetaExpV4F<exp>(exp(a,b));
  231. }
  232. // vec4f + exp
  233. template <typename B>
  234. metainline const MetaExpV4F< MetaADD< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<B> > >
  235. operator + (const Vector4<float>& a, const MetaExpV4F<B>& b)
  236. {
  237. typedef MetaADD< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<B> > exp;
  238. return MetaExpV4F<exp>(exp(a,b));
  239. }
  240. // vec4f + vec4f
  241. metainline const MetaExpV4F< MetaADD< float,MetaExpV4F< Vector4<float> >,MetaExpV4F< Vector4<float> > > >
  242. operator + (const Vector4<float>& a, const Vector4<float>& b)
  243. {
  244. typedef MetaADD< float,MetaExpV4F< Vector4<float> >,MetaExpV4F< Vector4<float> > > exp;
  245. return MetaExpV4F<exp>(exp(a,b));
  246. }
  247. // exp - exp
  248. template <typename A, typename B>
  249. metainline const MetaExpV4F< MetaSUB< float,MetaExpV4F<A>,MetaExpV4F<B> > >
  250. operator - (const MetaExpV4F<A>& a, const MetaExpV4F<B>& b)
  251. {
  252. typedef MetaSUB< float,MetaExpV4F<A>,MetaExpV4F<B> > exp;
  253. return MetaExpV4F<exp>(exp(a,b));
  254. }
  255. // exp - vec4f
  256. template <typename A>
  257. metainline const MetaExpV4F< MetaSUB< float,MetaExpV4F<A>,MetaExpV4F< Vector4<float> > > >
  258. operator - (const MetaExpV4F<A>& a, const Vector4<float>& b)
  259. {
  260. typedef MetaSUB< float,MetaExpV4F<A>,MetaExpV4F< Vector4<float> > > exp;
  261. return MetaExpV4F<exp>(exp(a,b));
  262. }
  263. // vec4f - exp
  264. template <typename B>
  265. metainline const MetaExpV4F< MetaSUB< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<B> > >
  266. operator - (const Vector4<float>& a, const MetaExpV4F<B>& b)
  267. {
  268. typedef MetaSUB< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<B> > exp;
  269. return MetaExpV4F<exp>(exp(a,b));
  270. }
  271. // vec4f - vec4f
  272. metainline const MetaExpV4F< MetaSUB< float,MetaExpV4F< Vector4<float> >,MetaExpV4F< Vector4<float> > > >
  273. operator - (const Vector4<float>& a, const Vector4<float>& b)
  274. {
  275. typedef MetaSUB< float,MetaExpV4F< Vector4<float> >,MetaExpV4F< Vector4<float> > > exp;
  276. return MetaExpV4F<exp>(exp(a,b));
  277. }
  278. // exp * exp
  279. template <typename A, typename B>
  280. metainline const MetaExpV4F< MetaMUL< float,MetaExpV4F<A>,MetaExpV4F<B> > >
  281. operator * (const MetaExpV4F<A>& a, const MetaExpV4F<B>& b)
  282. {
  283. typedef MetaMUL< float,MetaExpV4F<A>,MetaExpV4F<B> > exp;
  284. return MetaExpV4F<exp>(exp(a,b));
  285. }
  286. // exp * vec4f
  287. template <typename A>
  288. metainline const MetaExpV4F< MetaMUL< float,MetaExpV4F<A>,MetaExpV4F< Vector4<float> > > >
  289. operator * (const MetaExpV4F<A>& a, const Vector4<float>& b)
  290. {
  291. typedef MetaMUL< float,MetaExpV4F<A>,MetaExpV4F< Vector4<float> > > exp;
  292. return MetaExpV4F<exp>(exp(a,b));
  293. }
  294. // exp * float
  295. template <typename A>
  296. metainline const MetaExpV4F< MetaMUL< float,MetaExpV4F<A>,MetaExpV4F<float> > >
  297. operator * (const MetaExpV4F<A>& a, const float& b)
  298. {
  299. typedef MetaMUL< float,MetaExpV4F<A>,MetaExpV4F<float> > exp;
  300. return MetaExpV4F<exp>(exp(a,b));
  301. }
  302. // vec4f * exp
  303. template <typename B>
  304. metainline const MetaExpV4F< MetaMUL< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<B> > >
  305. operator * (const Vector4<float>& a, const MetaExpV4F<B>& b)
  306. {
  307. typedef MetaMUL< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<B> > exp;
  308. return MetaExpV4F<exp>(exp(a,b));
  309. }
  310. // vec4f * vec4f
  311. metainline const MetaExpV4F< MetaMUL< float,MetaExpV4F< Vector4<float> >,MetaExpV4F< Vector4<float> > > >
  312. operator * (const Vector4<float>& a, const Vector4<float>& b)
  313. {
  314. typedef MetaMUL< float,MetaExpV4F< Vector4<float> >,MetaExpV4F< Vector4<float> > > exp;
  315. return MetaExpV4F<exp>(exp(a,b));
  316. }
  317. // vec4f * float
  318. metainline const MetaExpV4F< MetaMUL< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<float> > >
  319. operator * (const Vector4<float>& a, const float& b)
  320. {
  321. typedef MetaMUL< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<float> > exp;
  322. return MetaExpV4F<exp>(exp(a,b));
  323. }
  324. // float * exp
  325. template <typename B>
  326. metainline const MetaExpV4F< MetaMUL< float,MetaExpV4F<float>,MetaExpV4F<B> > >
  327. operator * (const float& a, const MetaExpV4F<B>& b)
  328. {
  329. typedef MetaMUL< float,MetaExpV4F<float>,MetaExpV4F<B> > exp;
  330. return MetaExpV4F<exp>(exp(a,b));
  331. }
  332. // float * vec4f
  333. metainline const MetaExpV4F< MetaMUL< float,MetaExpV4F<float>,MetaExpV4F< Vector4<float> > > >
  334. operator * (const float& a, Vector4<float>& b)
  335. {
  336. typedef MetaMUL< float,MetaExpV4F<float>,MetaExpV4F< Vector4<float> > > exp;
  337. return MetaExpV4F<exp>(exp(a,b));
  338. }
  339. // exp / exp
  340. template <typename A, typename B>
  341. metainline const MetaExpV4F< MetaDIV< float,MetaExpV4F<A>,MetaExpV4F<B> > >
  342. operator / (const MetaExpV4F<A>& a, const MetaExpV4F<B>& b)
  343. {
  344. typedef MetaDIV< float,MetaExpV4F<A>,MetaExpV4F<B> > exp;
  345. return MetaExpV4F<exp>(exp(a,b));
  346. }
  347. // exp / vec4f
  348. template <typename A>
  349. metainline const MetaExpV4F< MetaDIV< float,MetaExpV4F<A>,MetaExpV4F< Vector4<float> > > >
  350. operator / (const MetaExpV4F<A>& a, const Vector4<float>& b)
  351. {
  352. typedef MetaDIV< float,MetaExpV4F<A>,MetaExpV4F< Vector4<float> > > exp;
  353. return MetaExpV4F<exp>(exp(a,b));
  354. }
  355. // exp / float
  356. template <typename A>
  357. metainline const MetaExpV4F< MetaDIV< float,MetaExpV4F<A>,MetaExpV4F<float> > >
  358. operator / (const MetaExpV4F<A>& a, const float& b)
  359. {
  360. typedef MetaDIV< float,MetaExpV4F<A>,MetaExpV4F<float> > exp;
  361. return MetaExpV4F<exp>(exp(a,b));
  362. }
  363. // vec4f / exp
  364. template <typename B>
  365. metainline const MetaExpV4F< MetaDIV< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<B> > >
  366. operator / (const Vector4<float>& a, const MetaExpV4F<B>& b)
  367. {
  368. typedef MetaDIV< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<B> > exp;
  369. return MetaExpV4F<exp>(exp(a,b));
  370. }
  371. // vec4f / vec4f
  372. metainline const MetaExpV4F< MetaDIV< float,MetaExpV4F< Vector4<float> >,MetaExpV4F< Vector4<float> > > >
  373. operator / (const Vector4<float>& a, const Vector4<float>& b)
  374. {
  375. typedef MetaDIV< float,MetaExpV4F< Vector4<float> >,MetaExpV4F< Vector4<float> > > exp;
  376. return MetaExpV4F<exp>(exp(a,b));
  377. }
  378. // vec4f / float
  379. metainline const MetaExpV4F< MetaDIV< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<float> > >
  380. operator / (const Vector4<float>& a, const float& b)
  381. {
  382. typedef MetaDIV< float,MetaExpV4F< Vector4<float> >,MetaExpV4F<float> > exp;
  383. return MetaExpV4F<exp>(exp(a,b));
  384. }
  385. // exp == exp
  386. template <typename A, typename B>
  387. metainline bool operator == (const MetaExpV4F<A>& a, const MetaExpV4F<B>& b)
  388. {
  389. for ( int i=0; i<4; ++i )
  390. {
  391. if ( a[i] != b[i] )
  392. return false;
  393. }
  394. return true;
  395. }
  396. // vec4f == exp
  397. template <typename B>
  398. metainline bool operator == (const Vector4<float>& a, const MetaExpV4F<B>& b)
  399. {
  400. for ( int i=0; i<4; ++i )
  401. {
  402. if ( a[i] != b[i] )
  403. return false;
  404. }
  405. return true;
  406. }
  407. // exp == vec4f
  408. template <typename A>
  409. metainline bool operator == (const MetaExpV4F<A>& a, const Vector4<float>& b)
  410. {
  411. for ( int i=0; i<4; ++i )
  412. {
  413. if ( a[i] != b[i] )
  414. return false;
  415. }
  416. return true;
  417. }
  418. // exp != exp
  419. template <typename A, typename B>
  420. metainline bool operator != (const MetaExpV4F<A>& a, const MetaExpV4F<B>& b)
  421. {
  422. for ( int i=0; i<4; ++i )
  423. {
  424. if ( a[i] != b[i] )
  425. return true;
  426. }
  427. return false;
  428. }
  429. // vec4f != exp
  430. template <typename B>
  431. metainline bool operator != (const Vector4<float>& a, const MetaExpV4F<B>& b)
  432. {
  433. for ( int i=0; i<4; ++i )
  434. {
  435. if ( a[i] != b[i] )
  436. return true;
  437. }
  438. return false;
  439. }
  440. // exp != vec4f
  441. template <typename A>
  442. metainline bool operator != (const MetaExpV4F<A>& a, const Vector4<float>& b)
  443. {
  444. for ( int i=0; i<4; ++i )
  445. {
  446. if ( a[i] != b[i] )
  447. return true;
  448. }
  449. return false;
  450. }
  451. // meta functions
  452. // exp . exp
  453. template <typename A, typename B>
  454. metainline float DotProduct(const MetaExpV4F<A>& a, const MetaExpV4F<B>& b)
  455. {
  456. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
  457. }
  458. // exp . vec4f
  459. template <typename A>
  460. metainline float DotProduct(const MetaExpV4F<A>& a, const Vector4<float>& b)
  461. {
  462. return a[0] * b.x + a[1] * b.y + a[2] * b.z + a[3] * b.w;
  463. }
  464. // vec4f . exp
  465. template <typename B>
  466. metainline float DotProduct(const Vector4<float>& a, const MetaExpV4F<B>& b)
  467. {
  468. return a.x * b[0] + a.y * b[1] + a.z * b[2] + a.w * b[3];
  469. }
  470. // vec4f . vec4f
  471. metainline float DotProduct(const Vector4<float>& a, const Vector4<float>& b)
  472. {
  473. return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  474. }
  475. template <typename A, typename B>
  476. metainline Vector4<float> Lerp(const MetaExpV4F<A>& a, const MetaExpV4F<B>& b, float time)
  477. {
  478. return Vector4<float>(a + (b - a) * time);
  479. }
  480. template <typename B>
  481. metainline Vector4<float> Lerp(const Vector4<float>& a, const MetaExpV4F<B>& b, float time)
  482. {
  483. return Vector4<float>(a + (b - a) * time);
  484. }
  485. template <typename A>
  486. metainline Vector4<float> Lerp(const MetaExpV4F<A>& a, const Vector4<float>& b, float time)
  487. {
  488. return Vector4<float>(a + (b - a) * time);
  489. }
  490. } // namespace prmath
  491. #undef metainline
  492. #undef PRMATH_METAVECTOR_ENABLE
  493. #endif
  494. #endif