You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5978 lines
173 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #pragma once
  2. #include <cstdio>
  3. #include <cassert>
  4. #include <cmath>
  5. #include <stdlib.h>
  6. #include <vector>
  7. #include <map>
  8. #include <limits>
  9. namespace CELL
  10. {
  11. #define PI 3.14159265358979323
  12. #define TWO_PI 6.28318530717958647
  13. #define HALF_PI 1.57079632679489661
  14. #define DEG2RAD(theta) (0.01745329251994329 * (theta))
  15. #define RAD2DEG 57.2957795130823208
  16. #define LOG2 0.69314718055994529
  17. #define WGS_84_RADIUS_EQUATOR 6378137.0
  18. #define WGS_84_RADIUS_POLAR 6356752.3142
  19. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  20. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  21. #ifndef FLT_MAX
  22. #define FLT_MAX 3.402823466e+38F
  23. #endif
  24. #ifndef FLT_MIN
  25. #define FLT_MIN 1.175494351e-38F
  26. #endif
  27. #define MAKE_INT(a, b) ((int)(((short)(((int)(a)) & 0xffff)) | ((int)((short)(((int)(b)) & 0xffff))) << 16))
  28. typedef unsigned char byte;
  29. typedef long long int64;
  30. typedef unsigned short ushort;
  31. typedef unsigned int uint;
  32. typedef unsigned long ulong;
  33. template<class T> inline T tmin(T a,T b)
  34. {
  35. return a < b ? a:b;
  36. }
  37. template<class T> inline T tmax(T a,T b)
  38. {
  39. return a > b ? a:b;
  40. }
  41. union LargeInt
  42. {
  43. struct __LARGE_INT
  44. {
  45. unsigned int LowPart;
  46. unsigned int HighPart;
  47. }_largeInt;
  48. int64 int64Data;
  49. } ;
  50. inline float unitRandom ()
  51. {
  52. return float(rand()) / float( RAND_MAX );
  53. }
  54. //-----------------------------------------------------------------------
  55. inline float rangeRandom (float fLow, float fHigh)
  56. {
  57. return (fHigh-fLow)*unitRandom() + fLow;
  58. }
  59. /**
  60. * 64λ
  61. */
  62. inline int64 makeInt64(unsigned low,unsigned hi)
  63. {
  64. LargeInt intdata;
  65. intdata._largeInt.HighPart = low;
  66. intdata._largeInt.LowPart = hi;
  67. return intdata.int64Data;
  68. }
  69. template <typename T>
  70. struct tvec2
  71. {
  72. typedef T value_type;
  73. typedef std::size_t size_type;
  74. typedef tvec2<T> type;
  75. value_type x;
  76. value_type y;
  77. value_type & operator[](size_type i)
  78. {
  79. assert(i < this->length());
  80. return (&x)[i];
  81. }
  82. value_type const & operator[]( size_type i ) const
  83. {
  84. assert(i < this->length());
  85. return (&x)[i];
  86. }
  87. tvec2() :
  88. x(value_type(0)),
  89. y(value_type(0))
  90. {}
  91. tvec2(tvec2<T> const & v) :
  92. x(v.x),
  93. y(v.y)
  94. {}
  95. tvec2(value_type const & s) :
  96. x(s),
  97. y(s)
  98. {}
  99. tvec2(value_type const & s1, value_type const & s2) :
  100. x(s1),
  101. y(s2)
  102. {}
  103. template <typename U>
  104. tvec2(U const & x) :
  105. x(value_type(x)),
  106. y(value_type(x))
  107. {}
  108. template <typename U, typename V>
  109. tvec2(U const & a, V b) :
  110. x(value_type(a)),
  111. y(value_type(b))
  112. {}
  113. template <typename U>
  114. tvec2(tvec2<U> const & v) :
  115. x(value_type(v.x)),
  116. y(value_type(v.y))
  117. {}
  118. tvec2<T> & operator= (tvec2<T> const & v)
  119. {
  120. this->x = v.x;
  121. this->y = v.y;
  122. return *this;
  123. }
  124. template <typename U>
  125. tvec2<T> & operator= (tvec2<U> const & v)
  126. {
  127. this->x = T(v.x);
  128. this->y = T(v.y);
  129. return *this;
  130. }
  131. template <typename U>
  132. tvec2<T> & operator+=(U const & s)
  133. {
  134. this->x += T(s);
  135. this->y += T(s);
  136. return *this;
  137. }
  138. template <typename U>
  139. tvec2<T> & operator+=(tvec2<U> const & v)
  140. {
  141. this->x += T(v.x);
  142. this->y += T(v.y);
  143. return *this;
  144. }
  145. template <typename U>
  146. tvec2<T> & operator-=(U const & s)
  147. {
  148. this->x -= T(s);
  149. this->y -= T(s);
  150. return *this;
  151. }
  152. template <typename U>
  153. tvec2<T> & operator-=(tvec2<U> const & v)
  154. {
  155. this->x -= T(v.x);
  156. this->y -= T(v.y);
  157. return *this;
  158. }
  159. template <typename U>
  160. tvec2<T> & operator*=(U s)
  161. {
  162. this->x *= T(s);
  163. this->y *= T(s);
  164. return *this;
  165. }
  166. template <typename U>
  167. tvec2<T> & operator*=(tvec2<U> const & v)
  168. {
  169. this->x *= T(v.x);
  170. this->y *= T(v.y);
  171. return *this;
  172. }
  173. template <typename U>
  174. tvec2<T> & operator/=(U s)
  175. {
  176. this->x /= T(s);
  177. this->y /= T(s);
  178. return *this;
  179. }
  180. template <typename U>
  181. tvec2<T> & operator/=(tvec2<U> const & v)
  182. {
  183. this->x /= T(v.x);
  184. this->y /= T(v.y);
  185. return *this;
  186. }
  187. tvec2<T> & operator++()
  188. {
  189. ++ this->x;
  190. ++ this->y;
  191. return *this;
  192. }
  193. tvec2<T> & operator--()
  194. {
  195. --this->x;
  196. --this->y;
  197. return *this;
  198. }
  199. void makeCeil( tvec2<T> cmp )
  200. {
  201. if( cmp.x > x ) x = cmp.x;
  202. if( cmp.y > y ) y = cmp.y;
  203. }
  204. void makeFloor( tvec2<T> cmp )
  205. {
  206. if( cmp.x < x ) x = cmp.x;
  207. if( cmp.y < y ) y = cmp.y;
  208. }
  209. };
  210. template <typename T>
  211. tvec2<T> rotate(tvec2<T> const & v, T angle)
  212. {
  213. tvec2<T> res;
  214. T const c(cos(DEG2RAD(angle)));
  215. T const s(sin(DEG2RAD(angle)));
  216. res.x = v.x * c - v.y * s;
  217. res.y = v.x * s + v.y * c;
  218. return res;
  219. }
  220. template <typename T>
  221. bool operator==(tvec2<T> const & v1, tvec2<T> const & v2)
  222. {
  223. return (v1.x == v2.x) && (v1.y == v2.y);
  224. }
  225. template <typename T>
  226. bool operator!=(tvec2<T> const & v1, tvec2<T> const & v2)
  227. {
  228. return (v1.x != v2.x) || (v1.y != v2.y);
  229. }
  230. template <typename T>
  231. tvec2<T> operator+ (tvec2<T> const & v, T const & s)
  232. {
  233. return tvec2<T>(
  234. v.x + T(s),
  235. v.y + T(s));
  236. }
  237. template <typename T>
  238. tvec2<T> operator+ (T const & s, tvec2<T> const & v)
  239. {
  240. return tvec2<T>(
  241. T(s) + v.x,
  242. T(s) + v.y);
  243. }
  244. template <typename T>
  245. tvec2<T> operator+ (tvec2<T> const & v1, tvec2<T> const & v2)
  246. {
  247. return tvec2<T>(
  248. v1.x + T(v2.x),
  249. v1.y + T(v2.y));
  250. }
  251. template <typename T>
  252. tvec2<T> operator-(tvec2<T> const & v, T const & s)
  253. {
  254. return tvec2<T>(
  255. v.x - T(s),
  256. v.y - T(s));
  257. }
  258. template <typename T>
  259. tvec2<T> operator- (T const & s, tvec2<T> const & v)
  260. {
  261. return tvec2<T>(
  262. T(s) - v.x,
  263. T(s) - v.y);
  264. }
  265. template <typename T>
  266. tvec2<T> operator- (tvec2<T> const & v1, tvec2<T> const & v2)
  267. {
  268. return tvec2<T>(
  269. v1.x - T(v2.x),
  270. v1.y - T(v2.y));
  271. }
  272. template <typename T>
  273. tvec2<T> operator* (tvec2<T> const & v, T const & s)
  274. {
  275. return tvec2<T>(
  276. v.x * T(s),
  277. v.y * T(s));
  278. }
  279. template <typename T>
  280. tvec2<T> operator* (T const & s, tvec2<T> const & v)
  281. {
  282. return tvec2<T>(
  283. T(s) * v.x,
  284. T(s) * v.y);
  285. }
  286. template <typename T>
  287. tvec2<T> operator*(tvec2<T> const & v1, tvec2<T> const & v2)
  288. {
  289. return tvec2<T>(
  290. v1.x * T(v2.x),
  291. v1.y * T(v2.y));
  292. }
  293. template <typename T>
  294. tvec2<T> operator/(tvec2<T> const & v, T const & s)
  295. {
  296. return tvec2<T>(
  297. v.x / T(s),
  298. v.y / T(s));
  299. }
  300. template <typename T>
  301. tvec2<T> operator/(T const & s, tvec2<T> const & v)
  302. {
  303. return tvec2<T>(
  304. T(s) / v.x,
  305. T(s) / v.y);
  306. }
  307. template <typename T>
  308. tvec2<T> operator/ (tvec2<T> const & v1, tvec2<T> const & v2 )
  309. {
  310. return tvec2<T>(
  311. v1.x / T(v2.x),
  312. v1.y / T(v2.y)
  313. );
  314. }
  315. template <typename T>
  316. tvec2<T> operator- (tvec2<T> const & v)
  317. {
  318. return tvec2<T> (
  319. -v.x,
  320. -v.y
  321. );
  322. }
  323. template <typename T>
  324. tvec2<T> operator++ (tvec2<T> const & v, int)
  325. {
  326. return tvec2<T>(
  327. v.x + T(1),
  328. v.y + T(1)
  329. );
  330. }
  331. template <typename T>
  332. tvec2<T> operator-- (tvec2<T> const & v, int)
  333. {
  334. return tvec2<T>(
  335. v.x - T(1),
  336. v.y - T(1)
  337. );
  338. }
  339. template <typename T>
  340. struct tvec3
  341. {
  342. typedef T value_type;
  343. typedef std::size_t size_type;
  344. typedef tvec3<T> type;
  345. value_type x;
  346. value_type y;
  347. value_type z;
  348. size_type length() const
  349. {
  350. return 3;
  351. }
  352. value_type & operator[](size_type i)
  353. {
  354. assert(i < this->length());
  355. return (&x)[i];
  356. }
  357. value_type const & operator[](size_type i) const
  358. {
  359. assert(i < this->length());
  360. return (&x)[i];
  361. }
  362. inline tvec3() :
  363. x(value_type(0)),
  364. y(value_type(0)),
  365. z(value_type(0))
  366. {}
  367. inline tvec3(tvec3<T> const & v) :
  368. x(v.x),
  369. y(v.y),
  370. z(v.z)
  371. {}
  372. inline tvec3(value_type s) :
  373. x(s),
  374. y(s),
  375. z(s)
  376. {}
  377. inline tvec3(value_type s0, value_type s1, value_type s2) :
  378. x(s0),
  379. y(s1),
  380. z(s2)
  381. {}
  382. template <typename U>
  383. tvec3(U s) :
  384. x(value_type(s)),
  385. y(value_type(s)),
  386. z(value_type(s))
  387. {}
  388. template <typename A, typename B, typename C>
  389. tvec3(A x, B y, C z) :
  390. x(value_type(x)),
  391. y(value_type(y)),
  392. z(value_type(z))
  393. {}
  394. template <typename A, typename B>
  395. tvec3(tvec2<A> const& v, B s) :
  396. x(value_type(v.x)),
  397. y(value_type(v.y)),
  398. z(value_type(s))
  399. {}
  400. template <typename A, typename B>
  401. tvec3(A s,tvec2<B> const& v) :
  402. x(value_type(s)),
  403. y(value_type(v.x)),
  404. z(value_type(v.y))
  405. {}
  406. template <typename U>
  407. tvec3(tvec3<U> const & v) :
  408. x(value_type(v.x)),
  409. y(value_type(v.y)),
  410. z(value_type(v.z))
  411. {}
  412. tvec3<T>& operator= (tvec3<T> const & v)
  413. {
  414. this->x = v.x;
  415. this->y = v.y;
  416. this->z = v.z;
  417. return *this;
  418. }
  419. template <typename U>
  420. tvec3<T>& operator= (tvec3<U> const & v)
  421. {
  422. this->x = T(v.x);
  423. this->y = T(v.y);
  424. this->z = T(v.z);
  425. return *this;
  426. }
  427. template <typename U>
  428. tvec3<T> & operator+=(U const & s)
  429. {
  430. this->x += T(s);
  431. this->y += T(s);
  432. this->z += T(s);
  433. return *this;
  434. }
  435. template <typename U>
  436. tvec3<T> & operator+=(tvec3<U> const & v)
  437. {
  438. this->x += T(v.x);
  439. this->y += T(v.y);
  440. this->z += T(v.z);
  441. return *this;
  442. }
  443. template <typename U>
  444. tvec3<T> & operator-=(U const & s)
  445. {
  446. this->x -= T(s);
  447. this->y -= T(s);
  448. this->z -= T(s);
  449. return *this;
  450. }
  451. template <typename U>
  452. tvec3<T> & operator-=(tvec3<U> const & v)
  453. {
  454. this->x -= T(v.x);
  455. this->y -= T(v.y);
  456. this->z -= T(v.z);
  457. return *this;
  458. }
  459. template <typename U>
  460. tvec3<T> & operator*=(U const & s)
  461. {
  462. this->x *= T(s);
  463. this->y *= T(s);
  464. this->z *= T(s);
  465. return *this;
  466. }
  467. template <typename U>
  468. tvec3<T> & operator*=(tvec3<U> const & v)
  469. {
  470. this->x *= T(v.x);
  471. this->y *= T(v.y);
  472. this->z *= T(v.z);
  473. return *this;
  474. }
  475. template <typename U>
  476. tvec3<T> & operator/=(U const & s)
  477. {
  478. this->x /= T(s);
  479. this->y /= T(s);
  480. this->z /= T(s);
  481. return *this;
  482. }
  483. template <typename U>
  484. tvec3<T> & operator/=(tvec3<U> const & v)
  485. {
  486. this->x /= T(v.x);
  487. this->y /= T(v.y);
  488. this->z /= T(v.z);
  489. return *this;
  490. }
  491. tvec3<T> & operator++()
  492. {
  493. ++this->x;
  494. ++this->y;
  495. ++this->z;
  496. return *this;
  497. }
  498. tvec3<T> & operator--()
  499. {
  500. --this->x;
  501. --this->y;
  502. --this->z;
  503. return *this;
  504. }
  505. void makeFloor( const tvec3<T>& cmp )
  506. {
  507. if( cmp.x < x ) x = cmp.x;
  508. if( cmp.y < y ) y = cmp.y;
  509. if( cmp.z < z ) z = cmp.z;
  510. }
  511. void makeCeil( const tvec3<T>& cmp )
  512. {
  513. if( cmp.x > x ) x = cmp.x;
  514. if( cmp.y > y ) y = cmp.y;
  515. if( cmp.z > z ) z = cmp.z;
  516. }
  517. T lengthf() const
  518. {
  519. return (T)sqrtf( x * x + y * y + z * z );
  520. }
  521. };
  522. template<typename T>
  523. bool operator >(const tvec3<T>& left ,const tvec3<T>& right)
  524. {
  525. return left.x > right.x && left.y > right.y && left.z > right.z;
  526. }
  527. template<typename T>
  528. bool operator <(const tvec3<T>& left ,const tvec3<T>& right)
  529. {
  530. return left.x < right.x && left.y < right.y && left.z < right.z;
  531. }
  532. template <typename T>
  533. tvec3<T> rotateX(const tvec3<T>& v, T angle)
  534. {
  535. tvec3<T> res(v);
  536. T c = cos(T(DEG2RAD(angle)));
  537. T s = sin(T(DEG2RAD(angle)));
  538. res.y = v.y * c - v.z * s;
  539. res.z = v.y * s + v.z * c;
  540. return res;
  541. }
  542. template <typename T>
  543. tvec3<T> rotateY(tvec3<T> const & v, T angle)
  544. {
  545. tvec3<T> res = v;
  546. T c = cos(T(DEG2RAD(angle)));
  547. T s = sin(T(DEG2RAD(angle)));
  548. res.x = v.x * c + v.z * s;
  549. res.z = -v.x * s + v.z * c;
  550. return res;
  551. }
  552. template <typename T>
  553. tvec3<T> rotateZ(tvec3<T> const & v, T angle)
  554. {
  555. tvec3<T> res = v;
  556. T c = cos(DEG2RAD(angle));
  557. T s = sin(DEG2RAD(angle));
  558. res.x = v.x * c - v.y * s;
  559. res.y = v.x * s + v.y * c;
  560. return res;
  561. }
  562. /**
  563. * ļн
  564. * A,B
  565. * AB = |A|*|B|*cos(@)
  566. * cos(@) = AB/|A|*|B|
  567. * @ = acos(@)
  568. */
  569. template <typename T>
  570. T angleBetweenVector(const tvec3<T>& a, const tvec3<T>& b)
  571. {
  572. #define Mag(V) (sqrtf(V.x*V.x + V.y*V.y + V.z*V.z))
  573. T dotProduct = dot(a, b);
  574. T vectorsMagnitude = Mag(a) * Mag(b) ;
  575. T angle = acos( dotProduct / vectorsMagnitude );
  576. T result = angle * T(RAD2DEG);
  577. if(_isnan(result))
  578. {
  579. return T(0);
  580. }
  581. else
  582. {
  583. return result;
  584. }
  585. }
  586. template<typename T>
  587. inline bool _isnan(T t)
  588. {
  589. return t == t;
  590. }
  591. template <typename T>
  592. T angleBetweenVector(const tvec2<T>& a, const tvec2<T>& b)
  593. {
  594. #define Mag2D(V) (sqrtf(V.x*V.x + V.y*V.y))
  595. T dotProduct = dot(a, b);
  596. T vectorsMagnitude = Mag2D(a) * Mag2D(b) ;
  597. T angle = acos( dotProduct / vectorsMagnitude );
  598. T result = angle * T(RAD2DEG);
  599. if(_isnan(result))
  600. {
  601. return T(0);
  602. }
  603. else
  604. {
  605. return result;
  606. }
  607. }
  608. template <typename T>
  609. static T clamp(T val, T minval, T maxval)
  610. {
  611. assert (minval < maxval && "Invalid clamp range");
  612. return MAX(MIN(val, maxval), minval);
  613. }
  614. template <typename T>
  615. inline T acosEx (T val)
  616. {
  617. if ( T(-1.0f) < val )
  618. {
  619. if ( val < 1.0f )
  620. return T(acos(val));
  621. else
  622. return T(0);
  623. }
  624. else
  625. {
  626. return T(PI);
  627. }
  628. }
  629. template<typename T>
  630. inline T angleBetween(const tvec3<T>& a, const tvec3<T>& b)
  631. {
  632. T lenProduct = a.lengthf() * b.lengthf();
  633. // Divide by zero check
  634. if(lenProduct < 1e-6f)
  635. lenProduct = 1e-6f;
  636. float f = dot(a,b) / lenProduct;
  637. f = clamp(f, T(-1.0), T(1.0));
  638. return acosEx(f);
  639. }
  640. /**
  641. * ڶ
  642. * ڶУ򣬵ߵļн֮ == 360
  643. */
  644. template<typename T>
  645. bool insidePolyon( const tvec3<T>& point, const tvec3<T> polygon[], size_t count)
  646. {
  647. tvec3<T> vA, vB;
  648. T angle = T(0.0);
  649. for (size_t i = 0; i < count; ++i)
  650. {
  651. vA = polygon[i] - point;
  652. vB = polygon[(i + 1) % count] - point;
  653. angle += angleBetweenVector(vA, vB);
  654. }
  655. if( abs(angle - 360 ) >= 0.5f)
  656. {
  657. return true;
  658. }
  659. return false;
  660. }
  661. template<typename T>
  662. bool insidePolyon( const tvec2<T>& point, const tvec2<T> polygon[], size_t count)
  663. {
  664. T angle = T(0.0);
  665. tvec2<T> vA, vB;
  666. for (size_t i = 0; i < count; ++i)
  667. {
  668. vA = polygon[i] - point;
  669. vB = polygon[(i + 1) % count] - point;
  670. tvec3<T> a(vA.x,vA.y,0);
  671. tvec3<T> b(vB.x,vB.y,0);
  672. angle += angleBetweenVector<T>(a, b);
  673. }
  674. if( abs(angle - 360 ) >= 0.5f)
  675. {
  676. return true;
  677. }
  678. return false;
  679. }
  680. template<typename T>
  681. bool pointinTriangle(tvec3<T> A, tvec3<T> B, tvec3<T> C, tvec3<T> P)
  682. {
  683. tvec3<T> v0 = C - A ;
  684. tvec3<T> v1 = B - A ;
  685. tvec3<T> v2 = P - A ;
  686. float dot00 = dot(v0,v0) ;
  687. float dot01 = dot(v0,v1) ;
  688. float dot02 = dot(v0,v2) ;
  689. float dot11 = dot(v1,v1) ;
  690. float dot12 = dot(v1,v2) ;
  691. float inverDeno = 1 / (dot00 * dot11 - dot01 * dot01) ;
  692. float u = (dot11 * dot02 - dot01 * dot12) * inverDeno ;
  693. if (u < 0 || u > 1) // if u out of range, return directly
  694. {
  695. return false ;
  696. }
  697. float v = (dot00 * dot12 - dot01 * dot02) * inverDeno ;
  698. if (v < 0 || v > 1) // if v out of range, return directly
  699. {
  700. return false ;
  701. }
  702. return u + v <= 1 ;
  703. }
  704. template<typename T>
  705. bool pointinTriangle(tvec2<T> A, tvec2<T> B, tvec2<T> C, tvec2<T> P)
  706. {
  707. return pointinTriangle(
  708. tvec3<T>(A.x,A.y,0),
  709. tvec3<T>(B.x,B.y,0),
  710. tvec3<T>(C.x,C.y,0),
  711. tvec3<T>(P.x,P.y,0));
  712. }
  713. /**
  714. *
  715. */
  716. template<typename T>
  717. bool intersectTriangle(
  718. const tvec3<T>& orig,
  719. const tvec3<T>& dir,
  720. tvec3<T>& v0,
  721. tvec3<T>& v1,
  722. tvec3<T>& v2,
  723. T* t,
  724. T* u,
  725. T* v
  726. )
  727. {
  728. // Find vectors for two edges sharing vert0
  729. tvec3<T> edge1 = v1 - v0;
  730. tvec3<T> edge2 = v2 - v0;
  731. // Begin calculating determinant - also used to calculate U parameter
  732. tvec3<T> pvec;
  733. pvec = cross(dir, edge2 );
  734. // If determinant is near zero, ray lies in plane of triangle
  735. T det = dot( edge1,pvec );
  736. tvec3<T> tvec;
  737. if( det > 0 )
  738. {
  739. tvec = orig - v0;
  740. }
  741. else
  742. {
  743. tvec = v0 - orig;
  744. det = -det;
  745. }
  746. if( det < 0.0001f )
  747. return false;
  748. // Calculate U parameter and test bounds
  749. *u = dot( tvec, pvec );
  750. if( *u < 0.0f || *u > det )
  751. return false;
  752. // Prepare to test V parameter
  753. tvec3<T> qvec;
  754. qvec = cross(tvec, edge1 );
  755. // Calculate V parameter and test bounds
  756. *v = dot( dir, qvec );
  757. if( *v < T(0.0f) || *u + *v > det )
  758. return false;
  759. *t = dot( edge2,qvec );
  760. T fInvDet = T(1.0) / det;
  761. *t *= fInvDet;
  762. *u *= fInvDet;
  763. *v *= fInvDet;
  764. return true;
  765. }
  766. /**
  767. *
  768. */
  769. template<typename T> T calcTriangleArea(const tvec3<T>& pt1,const tvec3<T>& pt2,const tvec3<T>& pt3)
  770. {
  771. tvec3<T> e1 = pt2 - pt1;
  772. tvec3<T> e2 = pt3 - pt1;
  773. tvec3<T> e3 = cross(e1,e2);
  774. return length(e3) * T(0.5);
  775. }
  776. template <typename T>
  777. bool operator==(tvec3<T> const & v1, tvec3<T> const & v2)
  778. {
  779. return (v1.x == v2.x) && (v1.y == v2.y) && (v1.z == v2.z);
  780. }
  781. template <typename T>
  782. bool operator!=(tvec3<T> const & v1, tvec3<T> const & v2)
  783. {
  784. return (v1.x != v2.x) || (v1.y != v2.y) || (v1.z != v2.z);
  785. }
  786. template <typename T>
  787. tvec3<T> operator+(tvec3<T> const & v, T const & s)
  788. {
  789. return tvec3<T>(
  790. v.x + T(s),
  791. v.y + T(s),
  792. v.z + T(s));
  793. }
  794. template <typename T>
  795. tvec3<T> operator+ ( T const & s, tvec3<T> const & v)
  796. {
  797. return tvec3<T>(
  798. T(s) + v.x,
  799. T(s) + v.y,
  800. T(s) + v.z);
  801. }
  802. template <typename T>
  803. tvec3<T> operator+ (tvec3<T> const & v1, tvec3<T> const & v2)
  804. {
  805. return tvec3<T>(
  806. v1.x + T(v2.x),
  807. v1.y + T(v2.y),
  808. v1.z + T(v2.z));
  809. }
  810. template <typename T>
  811. tvec3<T> operator- (tvec3<T> const & v, T const & s)
  812. {
  813. return tvec3<T>(
  814. v.x - T(s),
  815. v.y - T(s),
  816. v.z - T(s));
  817. }
  818. template <typename T>
  819. tvec3<T> operator- (T const & s, tvec3<T> const & v)
  820. {
  821. return tvec3<T>(
  822. T(s) - v.x,
  823. T(s) - v.y,
  824. T(s) - v.z);
  825. }
  826. template <typename T>
  827. tvec3<T> operator- (tvec3<T> const & v1, tvec3<T> const & v2)
  828. {
  829. return tvec3<T>(
  830. v1.x - T(v2.x),
  831. v1.y - T(v2.y),
  832. v1.z - T(v2.z));
  833. }
  834. template <typename T>
  835. tvec3<T> operator*(tvec3<T> const & v, T const & s)
  836. {
  837. return tvec3<T>(
  838. v.x * T(s),
  839. v.y * T(s),
  840. v.z * T(s));
  841. }
  842. template <typename T>
  843. tvec3<T> operator* (T const & s, tvec3<T> const & v)
  844. {
  845. return tvec3<T>(
  846. T(s) * v.x,
  847. T(s) * v.y,
  848. T(s) * v.z);
  849. }
  850. template <typename T>
  851. tvec3<T> operator* (tvec3<T> const & v1, tvec3<T> const & v2)
  852. {
  853. return tvec3<T>(
  854. v1.x * T(v2.x),
  855. v1.y * T(v2.y),
  856. v1.z * T(v2.z));
  857. }
  858. template <typename T>
  859. tvec3<T> operator/ (tvec3<T> const & v, T const & s)
  860. {
  861. return tvec3<T>(
  862. v.x / T(s),
  863. v.y / T(s),
  864. v.z / T(s));
  865. }
  866. template <typename T>
  867. tvec3<T> operator/ (T const & s, tvec3<T> const & v)
  868. {
  869. return tvec3<T>(
  870. T(s) / v.x,
  871. T(s) / v.y,
  872. T(s) / v.z);
  873. }
  874. template <typename T>
  875. tvec3<T> operator/ (tvec3<T> const & v1, tvec3<T> const & v2)
  876. {
  877. return tvec3<T>(
  878. v1.x / T(v2.x),
  879. v1.y / T(v2.y),
  880. v1.z / T(v2.z));
  881. }
  882. template <typename T>
  883. tvec3<T> operator- (tvec3<T> const & v)
  884. {
  885. return tvec3<T>(
  886. -v.x,
  887. -v.y,
  888. -v.z);
  889. }
  890. template <typename T>
  891. tvec3<T> operator++ (tvec3<T> const & v, int)
  892. {
  893. return tvec3<T>(
  894. v.x + T(1),
  895. v.y + T(1),
  896. v.z + T(1));
  897. }
  898. template <typename T>
  899. tvec3<T> operator-- (tvec3<T> const & v, int)
  900. {
  901. return tvec3<T>(
  902. v.x - T(1),
  903. v.y - T(1),
  904. v.z - T(1));
  905. }
  906. template <typename T>
  907. struct tvec4
  908. {
  909. typedef T value_type;
  910. typedef std::size_t size_type;
  911. typedef tvec4<T> type;
  912. value_type x, y, z, w;
  913. size_type length() const
  914. {
  915. return 4;
  916. }
  917. value_type & operator[](size_type i)
  918. {
  919. assert(i < this->length());
  920. return (&x)[i];
  921. }
  922. value_type const & operator[](size_type i) const
  923. {
  924. assert(i < this->length());
  925. return (&x)[i];
  926. }
  927. tvec4() :
  928. x(value_type(0)),
  929. y(value_type(0)),
  930. z(value_type(0)),
  931. w(value_type(0))
  932. {}
  933. tvec4(tvec3<T> const& v, T s) :
  934. x(v.x),
  935. y(v.y),
  936. z(v.z),
  937. w(s)
  938. {}
  939. tvec4(T s) :
  940. x(s),
  941. y(s),
  942. z(s),
  943. w(s)
  944. {}
  945. tvec4(tvec4<T> const & v) :
  946. x(v.x),
  947. y(v.y),
  948. z(v.z),
  949. w(v.w)
  950. {}
  951. template <typename A, typename B>
  952. tvec4(tvec3<A> const & v, B s):
  953. x(value_type(v.x)),
  954. y(value_type(v.y)),
  955. z(value_type(v.z)),
  956. w(value_type(s))
  957. {}
  958. template <typename A, typename B>
  959. tvec4(A s,tvec3<B> const & v):
  960. x(value_type(s)),
  961. y(value_type(v.x)),
  962. z(value_type(v.y)),
  963. w(value_type(v.z))
  964. {}
  965. template<typename U>
  966. tvec4(tvec4<U> const & v) :
  967. x(value_type(v.x)),
  968. y(value_type(v.y)),
  969. z(value_type(v.z)),
  970. w(value_type(v.w))
  971. {}
  972. tvec4
  973. (
  974. value_type s1,
  975. value_type s2,
  976. value_type s3,
  977. value_type s4
  978. ) :
  979. x(s1),
  980. y(s2),
  981. z(s3),
  982. w(s4)
  983. {}
  984. tvec4<T> & operator=(tvec4<T> const & v)
  985. {
  986. this->x = v.x;
  987. this->y = v.y;
  988. this->z = v.z;
  989. this->w = v.w;
  990. return *this;
  991. }
  992. template <typename U>
  993. tvec4<T> & operator= (tvec4<U> const & v)
  994. {
  995. this->x = T(v.x);
  996. this->y = T(v.y);
  997. this->z = T(v.z);
  998. this->w = T(v.w);
  999. return *this;
  1000. }
  1001. template <typename U>
  1002. tvec4<T> & operator+=(U const & s)
  1003. {
  1004. this->x += T(s);
  1005. this->y += T(s);
  1006. this->z += T(s);
  1007. this->w += T(s);
  1008. return *this;
  1009. }
  1010. template <typename U>
  1011. tvec4<T> & operator+=(tvec4<U> const & v)
  1012. {
  1013. this->x += T(v.x);
  1014. this->y += T(v.y);
  1015. this->z += T(v.z);
  1016. this->w += T(v.w);
  1017. return *this;
  1018. }
  1019. template <typename U>
  1020. tvec4<T> & operator-=(U const & s)
  1021. {
  1022. this->x -= T(s);
  1023. this->y -= T(s);
  1024. this->z -= T(s);
  1025. this->w -= T(s);
  1026. return *this;
  1027. }
  1028. template <typename U>
  1029. tvec4<T> & operator-=(tvec4<U> const & v)
  1030. {
  1031. this->x -= T(v.x);
  1032. this->y -= T(v.y);
  1033. this->z -= T(v.z);
  1034. this->w -= T(v.w);
  1035. return *this;
  1036. }
  1037. template <typename U>
  1038. tvec4<T> & operator*= (U const & s)
  1039. {
  1040. this->x *= T(s);
  1041. this->y *= T(s);
  1042. this->z *= T(s);
  1043. this->w *= T(s);
  1044. return *this;
  1045. }
  1046. template <typename U>
  1047. tvec4<T> & operator*=( tvec4<U> const & v)
  1048. {
  1049. this->x *= T(v.x);
  1050. this->y *= T(v.y);
  1051. this->z *= T(v.z);
  1052. this->w *= T(v.w);
  1053. return *this;
  1054. }
  1055. template <typename U>
  1056. tvec4<T> & operator/=(U const & s)
  1057. {
  1058. this->x /= T(s);
  1059. this->y /= T(s);
  1060. this->z /= T(s);
  1061. this->w /= T(s);
  1062. return *this;
  1063. }
  1064. template <typename U>
  1065. tvec4<T> & operator/=(tvec4<U> const & v)
  1066. {
  1067. this->x /= T(v.x);
  1068. this->y /= T(v.y);
  1069. this->z /= T(v.z);
  1070. this->w /= T(v.w);
  1071. return *this;
  1072. }
  1073. tvec4<T> & operator++()
  1074. {
  1075. ++this->x;
  1076. ++this->y;
  1077. ++this->z;
  1078. ++this->w;
  1079. return *this;
  1080. }
  1081. tvec4<T> & operator--()
  1082. {
  1083. --this->x;
  1084. --this->y;
  1085. --this->z;
  1086. --this->w;
  1087. return *this;
  1088. }
  1089. };
  1090. template <typename T>
  1091. tvec4<T> rotateX(const tvec4<T>& v, T angle)
  1092. {
  1093. tvec4<T> res(v);
  1094. T c = cos(DEG2RAD(angle));
  1095. T s = sin(DEG2RAD(angle));
  1096. res.y = v.y * c - v.z * s;
  1097. res.z = v.y * s + v.z * c;
  1098. return res;
  1099. }
  1100. template <typename T>
  1101. tvec4<T> rotateY(tvec4<T> const & v, T angle)
  1102. {
  1103. tvec4<T> res = v;
  1104. T c = cos(DEG2RAD(angle));
  1105. T s = sin(DEG2RAD(angle));
  1106. res.x = v.x * c + v.z * s;
  1107. res.z = -v.x * s + v.z * c;
  1108. return res;
  1109. }
  1110. template <typename T>
  1111. tvec4<T> rotateZ(tvec4<T> const & v, T angle)
  1112. {
  1113. tvec4<T> res = v;
  1114. T c = cos(DEG2RAD(angle));
  1115. T s = sin(DEG2RAD(angle));
  1116. res.x = v.x * c - v.y * s;
  1117. res.y = v.x * s + v.y * c;
  1118. return res;
  1119. }
  1120. template <typename T>
  1121. tvec4<T> operator+ (tvec4<T> const & v, T const & s)
  1122. {
  1123. return tvec4<T>(
  1124. v.x + s,
  1125. v.y + s,
  1126. v.z + s,
  1127. v.w + s);
  1128. }
  1129. template <typename T>
  1130. tvec4<T> operator+ (T const & s, tvec4<T> const & v)
  1131. {
  1132. return tvec4<T>(
  1133. s + v.x,
  1134. s + v.y,
  1135. s + v.z,
  1136. s + v.w);
  1137. }
  1138. template <typename T>
  1139. tvec4<T> operator+ (tvec4<T> const & v1, tvec4<T> const & v2)
  1140. {
  1141. return tvec4<T>(
  1142. v1.x + v2.x,
  1143. v1.y + v2.y,
  1144. v1.z + v2.z,
  1145. v1.w + v2.w);
  1146. }
  1147. template <typename T>
  1148. tvec4<T> operator- (tvec4<T> const & v, T const & s)
  1149. {
  1150. return tvec4<T>(
  1151. v.x - s,
  1152. v.y - s,
  1153. v.z - s,
  1154. v.w - s);
  1155. }
  1156. template <typename T>
  1157. tvec4<T> operator- (T const & s, tvec4<T> const & v)
  1158. {
  1159. return tvec4<T>(
  1160. s - v.x,
  1161. s - v.y,
  1162. s - v.z,
  1163. s - v.w);
  1164. }
  1165. template <typename T>
  1166. tvec4<T> operator-
  1167. (
  1168. tvec4<T> const & v1,
  1169. tvec4<T> const & v2
  1170. )
  1171. {
  1172. return tvec4<T>(
  1173. v1.x - v2.x,
  1174. v1.y - v2.y,
  1175. v1.z - v2.z,
  1176. v1.w - v2.w);
  1177. }
  1178. template <typename T>
  1179. tvec4<T> operator* (tvec4<T> const & v, T const & s)
  1180. {
  1181. return tvec4<T>(
  1182. v.x * s,
  1183. v.y * s,
  1184. v.z * s,
  1185. v.w * s);
  1186. }
  1187. template <typename T>
  1188. tvec4<T> operator* (T const & s, tvec4<T> const & v)
  1189. {
  1190. return tvec4<T>(
  1191. s * v.x,
  1192. s * v.y,
  1193. s * v.z,
  1194. s * v.w);
  1195. }
  1196. template <typename T>
  1197. tvec4<T> operator*(tvec4<T> const & v1, tvec4<T> const & v2)
  1198. {
  1199. return tvec4<T>(
  1200. v1.x * v2.x,
  1201. v1.y * v2.y,
  1202. v1.z * v2.z,
  1203. v1.w * v2.w);
  1204. }
  1205. template <typename T>
  1206. tvec4<T> operator/ (tvec4<T> const & v, T const & s)
  1207. {
  1208. return tvec4<T>(
  1209. v.x / s,
  1210. v.y / s,
  1211. v.z / s,
  1212. v.w / s);
  1213. }
  1214. template <typename T>
  1215. tvec4<T> operator/ (T const & s, tvec4<T> const & v)
  1216. {
  1217. return tvec4<T>(
  1218. s / v.x,
  1219. s / v.y,
  1220. s / v.z,
  1221. s / v.w);
  1222. }
  1223. template <typename T>
  1224. tvec4<T> operator/ ( tvec4<T> const & v1, tvec4<T> const & v2)
  1225. {
  1226. return tvec4<T>(
  1227. v1.x / v2.x,
  1228. v1.y / v2.y,
  1229. v1.z / v2.z,
  1230. v1.w / v2.w);
  1231. }
  1232. template <typename T>
  1233. tvec4<T> operator- ( tvec4<T> const & v)
  1234. {
  1235. return tvec4<T>(
  1236. -v.x,
  1237. -v.y,
  1238. -v.z,
  1239. -v.w);
  1240. }
  1241. template <typename T>
  1242. bool operator==
  1243. (
  1244. tvec4<T> const & v1,
  1245. tvec4<T> const & v2
  1246. )
  1247. {
  1248. return (v1.x == v2.x) && (v1.y == v2.y) && (v1.z == v2.z) && (v1.w == v2.w);
  1249. }
  1250. template <typename T>
  1251. bool operator!=(tvec4<T> const & v1, tvec4<T> const & v2)
  1252. {
  1253. return (v1.x != v2.x) || (v1.y != v2.y) || (v1.z != v2.z) || (v1.w != v2.w);
  1254. }
  1255. template<typename T>
  1256. class trect
  1257. {
  1258. public:
  1259. trect(T left = 0,T top = 0,T right = 0,T bottom = 0)
  1260. {
  1261. _left = left;
  1262. _top = top;
  1263. _right = right;
  1264. _bottom = bottom;
  1265. }
  1266. void fromCenter(T x,T y,T size)
  1267. {
  1268. _left = x - size * T(0.5f);
  1269. _top = y - size * T(0.5f);
  1270. _right = x + size * T(0.5f);
  1271. _bottom = y + size * T(0.5f);
  1272. }
  1273. void fromCenter(T x,T y,T sizeX,T sizeY)
  1274. {
  1275. _left = x - sizeX * T(0.5f);
  1276. _top = y - sizeY * T(0.5f);
  1277. _right = x + sizeX * T(0.5f);
  1278. _bottom = y + sizeY * T(0.5f);
  1279. }
  1280. bool ptInRect(T x,T y)
  1281. {
  1282. return x >= _left && x <= _right && y >= _top && y <= _bottom;
  1283. }
  1284. tvec2<T>center() const
  1285. {
  1286. return tvec2<T>((_left + _right) * T(0.5f),(_bottom + _top) * T(0.5f));
  1287. }
  1288. tvec2<T>halSize() const
  1289. {
  1290. return tvec2<T>((_right - _left) * T(0.5f),(_bottom - _top) * T(0.5f));
  1291. }
  1292. public:
  1293. T _left;
  1294. T _top;
  1295. T _right;
  1296. T _bottom;
  1297. };
  1298. template <typename T>
  1299. struct tmat2x2
  1300. {
  1301. typedef T value_type;
  1302. typedef std::size_t size_type;
  1303. typedef tvec2<T> col_type;
  1304. typedef tvec2<T> row_type;
  1305. typedef tmat2x2<T> type;
  1306. typedef tmat2x2<T> transpose_type;
  1307. public:
  1308. tmat2x2<T> _inverse() const
  1309. {
  1310. value_type Determinant = this->value[0][0] * this->value[1][1] - this->value[1][0] * this->value[0][1];
  1311. tmat2x2<T> Inverse(
  1312. + this->value[1][1] / Determinant,
  1313. - this->value[0][1] / Determinant,
  1314. - this->value[1][0] / Determinant,
  1315. + this->value[0][0] / Determinant);
  1316. return Inverse;
  1317. }
  1318. private:
  1319. col_type value[2];
  1320. public:
  1321. size_type length() const
  1322. {
  1323. return 2;
  1324. }
  1325. static size_type col_size()
  1326. {
  1327. return 2;
  1328. }
  1329. static size_type row_size()
  1330. {
  1331. return 2;
  1332. }
  1333. col_type &operator[](size_type i)
  1334. {
  1335. assert(i < this->length());
  1336. return this->value[i];
  1337. }
  1338. col_type const &operator[](size_type i) const
  1339. {
  1340. assert(i < this->length());
  1341. return this->value[i];
  1342. }
  1343. tmat2x2()
  1344. {
  1345. this->value[0] = col_type(1, 0);
  1346. this->value[1] = col_type(0, 1);
  1347. }
  1348. tmat2x2(tmat2x2<T> const & m)
  1349. {
  1350. this->value[0] = m.value[0];
  1351. this->value[1] = m.value[1];
  1352. }
  1353. tmat2x2(value_type s)
  1354. {
  1355. value_type const Zero(0);
  1356. this->value[0] = col_type(s, Zero);
  1357. this->value[1] = col_type(Zero, s);
  1358. }
  1359. tmat2x2(value_type x0, value_type y0, value_type x1, value_type y1)
  1360. {
  1361. this->value[0] = col_type(x0, y0);
  1362. this->value[1] = col_type(x1, y1);
  1363. }
  1364. tmat2x2(col_type const & v0, col_type const & v1)
  1365. {
  1366. this->value[0] = v0;
  1367. this->value[1] = v1;
  1368. }
  1369. template <typename U>
  1370. tmat2x2(U s)
  1371. {
  1372. value_type const Zero(0);
  1373. this->value[0] = tvec2<T>(value_type(s), Zero);
  1374. this->value[1] = tvec2<T>(Zero, value_type(s));
  1375. }
  1376. template <typename X1, typename Y1, typename X2, typename Y2>
  1377. tmat2x2(X1 x1, Y1 y1, X2 x2, Y2 y2)
  1378. {
  1379. this->value[0] = col_type(value_type(x1), value_type(y1));
  1380. this->value[1] = col_type(value_type(x2), value_type(y2));
  1381. }
  1382. template <typename V1, typename V2>
  1383. tmat2x2
  1384. (
  1385. tvec2<V1> const & v1,
  1386. tvec2<V2> const & v2
  1387. )
  1388. {
  1389. this->value[0] = col_type(v1);
  1390. this->value[1] = col_type(v2);
  1391. }
  1392. template <typename U>
  1393. tmat2x2(tmat2x2<U> const & m)
  1394. {
  1395. this->value[0] = col_type(m[0]);
  1396. this->value[1] = col_type(m[1]);
  1397. }
  1398. tmat2x2<T>& operator=(tmat2x2<T> const & m)
  1399. {
  1400. this->value[0] = m[0];
  1401. this->value[1] = m[1];
  1402. return *this;
  1403. }
  1404. template <typename U>
  1405. tmat2x2<T>& operator=
  1406. (
  1407. tmat2x2<U> const & m
  1408. )
  1409. {
  1410. this->value[0] = m[0];
  1411. this->value[1] = m[1];
  1412. return *this;
  1413. }
  1414. template <typename U>
  1415. tmat2x2<T>& operator+=(U const & s)
  1416. {
  1417. this->value[0] += s;
  1418. this->value[1] += s;
  1419. return *this;
  1420. }
  1421. template <typename U>
  1422. tmat2x2<T>& operator+=
  1423. (
  1424. tmat2x2<U> const & m
  1425. )
  1426. {
  1427. this->value[0] += m[0];
  1428. this->value[1] += m[1];
  1429. return *this;
  1430. }
  1431. template <typename U>
  1432. tmat2x2<T>& operator-=(U const & s)
  1433. {
  1434. this->value[0] -= s;
  1435. this->value[1] -= s;
  1436. return *this;
  1437. }
  1438. template <typename U>
  1439. tmat2x2<T>& operator-=(tmat2x2<U> const & m)
  1440. {
  1441. this->value[0] -= m[0];
  1442. this->value[1] -= m[1];
  1443. return *this;
  1444. }
  1445. template <typename U>
  1446. tmat2x2<T>& operator*= ( U const & s)
  1447. {
  1448. this->value[0] *= s;
  1449. this->value[1] *= s;
  1450. return *this;
  1451. }
  1452. template <typename U>
  1453. tmat2x2<T>& operator*= (tmat2x2<U> const & m)
  1454. {
  1455. return (*this = *this * m);
  1456. }
  1457. template <typename U>
  1458. tmat2x2<T>& operator/= (U const & s)
  1459. {
  1460. this->value[0] /= s;
  1461. this->value[1] /= s;
  1462. return *this;
  1463. }
  1464. template <typename U>
  1465. tmat2x2<T>& operator/= (tmat2x2<U> const & m)
  1466. {
  1467. return (*this = *this / m);
  1468. }
  1469. tmat2x2<T>& operator++ ()
  1470. {
  1471. ++this->value[0];
  1472. ++this->value[1];
  1473. return *this;
  1474. }
  1475. tmat2x2<T>& operator-- ()
  1476. {
  1477. --this->value[0];
  1478. --this->value[1];
  1479. return *this;
  1480. };
  1481. };
  1482. template <typename T>
  1483. tmat2x2<T> rotate(T angle)
  1484. {
  1485. T c = cos(DEG2RAD(angle));
  1486. T s = sin(DEG2RAD(angle));
  1487. return tmat2x2<T>(c,s,-s,c);
  1488. }
  1489. template <typename T>
  1490. tmat2x2<T> operator+ (tmat2x2<T> const & m, T const & s)
  1491. {
  1492. return tmat2x2<T>(m[0] + s,m[1] + s);
  1493. }
  1494. template <typename T>
  1495. tmat2x2<T> operator+ (T const & s, tmat2x2<T> const & m)
  1496. {
  1497. return tmat2x2<T>(m[0] + s,m[1] + s);
  1498. }
  1499. template <typename T>
  1500. tmat2x2<T> operator+ (tmat2x2<T> const & m1, tmat2x2<T> const & m2)
  1501. {
  1502. return tmat2x2<T>(m1[0] + m2[0],m1[1] + m2[1]);
  1503. }
  1504. template <typename T>
  1505. tmat2x2<T> operator- (tmat2x2<T> const & m, T const & s)
  1506. {
  1507. return tmat2x2<T>(m[0] - s,m[1] - s);
  1508. }
  1509. template <typename T>
  1510. tmat2x2<T> operator- (T const & s, tmat2x2<T> const & m)
  1511. {
  1512. return tmat2x2<T>(s - m[0],s - m[1]);
  1513. }
  1514. template <typename T>
  1515. tmat2x2<T> operator- (tmat2x2<T> const & m1, tmat2x2<T> const & m2)
  1516. {
  1517. return tmat2x2<T>(m1[0] - m2[0],m1[1] - m2[1]);
  1518. }
  1519. template <typename T>
  1520. tmat2x2<T> operator* (tmat2x2<T> const & m, T const & s)
  1521. {
  1522. return tmat2x2<T>(m[0] * s,m[1] * s);
  1523. }
  1524. template <typename T>
  1525. tmat2x2<T> operator* ( T const & s, tmat2x2<T> const & m)
  1526. {
  1527. return tmat2x2<T>(m[0] * s,m[1] * s);
  1528. }
  1529. template <typename T>
  1530. tvec2<T> operator*(tmat2x2<T> const & m, tvec2<T> const & v)
  1531. {
  1532. return tvec2<T>(
  1533. m[0][0] * v.x + m[1][0] * v.y,
  1534. m[0][1] * v.x + m[1][1] * v.y);
  1535. }
  1536. template <typename T>
  1537. tvec2<T> operator*(tvec2<T> const & v, tmat2x2<T> const & m)
  1538. {
  1539. return tvec2<T>(
  1540. v.x * m[0][0] + v.y * m[0][1],
  1541. v.x * m[1][0] + v.y * m[1][1]);
  1542. }
  1543. template <typename T>
  1544. tmat2x2<T> operator*(tmat2x2<T> const & m1,tmat2x2<T> const & m2)
  1545. {
  1546. return tmat2x2<T>(
  1547. m1[0][0] * m2[0][0] + m1[1][0] * m2[0][1],
  1548. m1[0][1] * m2[0][0] + m1[1][1] * m2[0][1],
  1549. m1[0][0] * m2[1][0] + m1[1][0] * m2[1][1],
  1550. m1[0][1] * m2[1][0] + m1[1][1] * m2[1][1]);
  1551. }
  1552. template <typename T>
  1553. struct tmat3x3
  1554. {
  1555. typedef T value_type;
  1556. typedef std::size_t size_type;
  1557. typedef tvec3<T> col_type;
  1558. typedef tvec3<T> row_type;
  1559. typedef tmat3x3<T> type;
  1560. typedef tmat3x3<T> transpose_type;
  1561. private:
  1562. // Data
  1563. col_type value[3];
  1564. public:
  1565. size_type length() const
  1566. {
  1567. return 3;
  1568. }
  1569. size_type col_size()
  1570. {
  1571. return 3;
  1572. }
  1573. size_type row_size()
  1574. {
  1575. return 3;
  1576. }
  1577. tmat3x3()
  1578. {
  1579. value_type const Zero(0);
  1580. value_type const One(1);
  1581. this->value[0] = col_type(One, Zero, Zero);
  1582. this->value[1] = col_type(Zero, One, Zero);
  1583. this->value[2] = col_type(Zero, Zero, One);
  1584. }
  1585. tmat3x3
  1586. (
  1587. tmat3x3<T> const & m
  1588. )
  1589. {
  1590. this->value[0] = m.value[0];
  1591. this->value[1] = m.value[1];
  1592. this->value[2] = m.value[2];
  1593. }
  1594. tmat3x3(value_type const & s)
  1595. {
  1596. value_type const Zero(0);
  1597. this->value[0] = col_type(s, Zero, Zero);
  1598. this->value[1] = col_type(Zero, s, Zero);
  1599. this->value[2] = col_type(Zero, Zero, s);
  1600. }
  1601. tmat3x3
  1602. (
  1603. value_type const & x0, value_type const & y0, value_type const & z0,
  1604. value_type const & x1, value_type const & y1, value_type const & z1,
  1605. value_type const & x2, value_type const & y2, value_type const & z2
  1606. )
  1607. {
  1608. this->value[0] = col_type(x0, y0, z0);
  1609. this->value[1] = col_type(x1, y1, z1);
  1610. this->value[2] = col_type(x2, y2, z2);
  1611. }
  1612. tmat3x3
  1613. (
  1614. col_type const & v0,
  1615. col_type const & v1,
  1616. col_type const & v2
  1617. )
  1618. {
  1619. this->value[0] = v0;
  1620. this->value[1] = v1;
  1621. this->value[2] = v2;
  1622. }
  1623. template <typename U>
  1624. tmat3x3(U const & s)
  1625. {
  1626. value_type const Zero(0);
  1627. this->value[0] = tvec3<T>(value_type(s), Zero, Zero);
  1628. this->value[1] = tvec3<T>(Zero, value_type(s), Zero);
  1629. this->value[2] = tvec3<T>(Zero, Zero, value_type(s));
  1630. }
  1631. template <
  1632. typename X1, typename Y1, typename Z1,
  1633. typename X2, typename Y2, typename Z2,
  1634. typename X3, typename Y3, typename Z3>
  1635. tmat3x3
  1636. (
  1637. X1 const & x1, Y1 const & y1, Z1 const & z1,
  1638. X2 const & x2, Y2 const & y2, Z2 const & z2,
  1639. X3 const & x3, Y3 const & y3, Z3 const & z3
  1640. )
  1641. {
  1642. this->value[0] = col_type(value_type(x1), value_type(y1), value_type(z1));
  1643. this->value[1] = col_type(value_type(x2), value_type(y2), value_type(z2));
  1644. this->value[2] = col_type(value_type(x3), value_type(y3), value_type(z3));
  1645. }
  1646. template <typename V1, typename V2, typename V3>
  1647. tmat3x3
  1648. (
  1649. tvec3<V1> const & v1,
  1650. tvec3<V2> const & v2,
  1651. tvec3<V3> const & v3
  1652. )
  1653. {
  1654. this->value[0] = col_type(v1);
  1655. this->value[1] = col_type(v2);
  1656. this->value[2] = col_type(v3);
  1657. }
  1658. template <typename U>
  1659. tmat3x3(tmat3x3<U> const & m)
  1660. {
  1661. this->value[0] = col_type(m[0]);
  1662. this->value[1] = col_type(m[1]);
  1663. this->value[2] = col_type(m[2]);
  1664. }
  1665. tmat3x3<T> _inverse() const
  1666. {
  1667. T S00 = value[0][0];
  1668. T S01 = value[0][1];
  1669. T S02 = value[0][2];
  1670. T S10 = value[1][0];
  1671. T S11 = value[1][1];
  1672. T S12 = value[1][2];
  1673. T S20 = value[2][0];
  1674. T S21 = value[2][1];
  1675. T S22 = value[2][2];
  1676. tmat3x3<T> Inverse(
  1677. S11 * S22 - S21 * S12,
  1678. S12 * S20 - S22 * S10,
  1679. S10 * S21 - S20 * S11,
  1680. S02 * S21 - S01 * S22,
  1681. S00 * S22 - S02 * S20,
  1682. S01 * S20 - S00 * S21,
  1683. S12 * S01 - S11 * S02,
  1684. S10 * S02 - S12 * S00,
  1685. S11 * S00 - S10 * S01);
  1686. T Determinant = S00 * (S11 * S22 - S21 * S12)
  1687. - S10 * (S01 * S22 - S21 * S02)
  1688. + S20 * (S01 * S12 - S11 * S02);
  1689. Inverse /= Determinant;
  1690. return Inverse;
  1691. }
  1692. col_type & operator[](size_type i)
  1693. {
  1694. assert(i < this->length());
  1695. return this->value[i];
  1696. }
  1697. col_type const & operator[](size_type i) const
  1698. {
  1699. assert(i < this->length());
  1700. return this->value[i];
  1701. }
  1702. tmat3x3<T> & operator=(tmat3x3<T> const & m)
  1703. {
  1704. this->value[0] = m[0];
  1705. this->value[1] = m[1];
  1706. this->value[2] = m[2];
  1707. return *this;
  1708. }
  1709. template <typename U>
  1710. tmat3x3<T> & operator=(tmat3x3<U> const & m)
  1711. {
  1712. this->value[0] = m[0];
  1713. this->value[1] = m[1];
  1714. this->value[2] = m[2];
  1715. return *this;
  1716. }
  1717. template <typename U>
  1718. tmat3x3<T> & operator+= (U const & s)
  1719. {
  1720. this->value[0] += s;
  1721. this->value[1] += s;
  1722. this->value[2] += s;
  1723. return *this;
  1724. }
  1725. template <typename U>
  1726. tmat3x3<T> & operator+=(tmat3x3<U> const & m)
  1727. {
  1728. this->value[0] += m[0];
  1729. this->value[1] += m[1];
  1730. this->value[2] += m[2];
  1731. return *this;
  1732. }
  1733. template <typename U>
  1734. tmat3x3<T> & operator-= (U const & s)
  1735. {
  1736. this->value[0] -= s;
  1737. this->value[1] -= s;
  1738. this->value[2] -= s;
  1739. return *this;
  1740. }
  1741. template <typename U>
  1742. tmat3x3<T> & operator-= (tmat3x3<U> const & m)
  1743. {
  1744. this->value[0] -= m[0];
  1745. this->value[1] -= m[1];
  1746. this->value[2] -= m[2];
  1747. return *this;
  1748. }
  1749. template <typename U>
  1750. tmat3x3<T> & operator*= (U const & s)
  1751. {
  1752. this->value[0] *= s;
  1753. this->value[1] *= s;
  1754. this->value[2] *= s;
  1755. return *this;
  1756. }
  1757. template <typename U>
  1758. tmat3x3<T> & operator*= (tmat3x3<U> const & m)
  1759. {
  1760. return (*this = *this * m);
  1761. }
  1762. template <typename U>
  1763. tmat3x3<T> & operator/= (U const & s)
  1764. {
  1765. this->value[0] /= s;
  1766. this->value[1] /= s;
  1767. this->value[2] /= s;
  1768. return *this;
  1769. }
  1770. template <typename U>
  1771. tmat3x3<T> & operator/= (tmat3x3<U> const & m)
  1772. {
  1773. return (*this = *this / m);
  1774. }
  1775. tmat3x3<T> & operator++ ()
  1776. {
  1777. ++this->value[0];
  1778. ++this->value[1];
  1779. ++this->value[2];
  1780. return *this;
  1781. }
  1782. tmat3x3<T> & operator-- ()
  1783. {
  1784. --this->value[0];
  1785. --this->value[1];
  1786. --this->value[2];
  1787. return *this;
  1788. }
  1789. tvec3<T> operator*(const tvec3<T> &v) const
  1790. {
  1791. return tvec3<T>(
  1792. value[0][0] * v[0] + value[1][0] * v[1] + value[2][0] * v[2]
  1793. ,value[0][1] * v[0] + value[1][1] * v[1] + value[2][1] * v[2]
  1794. ,value[0][2] * v[0] + value[1][2] * v[1] + value[2][2] * v[2]
  1795. );
  1796. }
  1797. tvec2<T> operator*(const tvec2<T> &v) const
  1798. {
  1799. return tvec2<T>(
  1800. value[0][0] * v[0] + value[1][0] * v[1] + value[2][0]
  1801. ,value[0][1] * v[0] + value[1][1] * v[1] + value[2][1]
  1802. );
  1803. }
  1804. void scale(T x,T y)
  1805. {
  1806. this->value[0] = col_type(value_type(x), value_type(0), value_type(0));
  1807. this->value[1] = col_type(value_type(0), value_type(y), value_type(0));
  1808. this->value[2] = col_type(value_type(0), value_type(0), value_type(1));
  1809. }
  1810. void rotate(T angle)
  1811. {
  1812. T rad = DEG2RAD(angle);
  1813. T c = cos(rad);
  1814. T s = sin(rad);
  1815. this->value[0] = col_type(value_type(c), value_type(-s), value_type(0));
  1816. this->value[1] = col_type(value_type(s), value_type(c), value_type(0));
  1817. this->value[2] = col_type(value_type(0), value_type(0), value_type(1));
  1818. }
  1819. void translate(T x,T y)
  1820. {
  1821. this->value[0] = col_type(value_type(1), value_type(0), value_type(0));
  1822. this->value[1] = col_type(value_type(0), value_type(1), value_type(0));
  1823. this->value[2] = col_type(value_type(x), value_type(y), value_type(1));
  1824. }
  1825. };
  1826. template <typename T>
  1827. tmat3x3<T> operator+ (tmat3x3<T> const & m, T const & s)
  1828. {
  1829. return tmat3x3<T>(
  1830. m[0] + s,
  1831. m[1] + s,
  1832. m[2] + s);
  1833. }
  1834. template <typename T>
  1835. tmat3x3<T> operator+ (T const & s, tmat3x3<T> const & m)
  1836. {
  1837. return tmat3x3<T>(
  1838. m[0] + s,
  1839. m[1] + s,
  1840. m[2] + s);
  1841. }
  1842. template <typename T>
  1843. tmat3x3<T> operator+ (tmat3x3<T> const & m1, tmat3x3<T> const & m2)
  1844. {
  1845. return tmat3x3<T>(
  1846. m1[0] + m2[0],
  1847. m1[1] + m2[1],
  1848. m1[2] + m2[2]);
  1849. }
  1850. template <typename T>
  1851. tmat3x3<T> operator- (tmat3x3<T> const & m, T const & s)
  1852. {
  1853. return tmat3x3<T>(
  1854. m[0] - s,
  1855. m[1] - s,
  1856. m[2] - s);
  1857. }
  1858. template <typename T>
  1859. tmat3x3<T> operator- (T const & s, tmat3x3<T> const & m)
  1860. {
  1861. return tmat3x3<T>(
  1862. s - m[0],
  1863. s - m[1],
  1864. s - m[2]);
  1865. }
  1866. template <typename T>
  1867. tmat3x3<T> operator- (tmat3x3<T> const & m1, tmat3x3<T> const & m2)
  1868. {
  1869. return tmat3x3<T>(
  1870. m1[0] - m2[0],
  1871. m1[1] - m2[1],
  1872. m1[2] - m2[2]);
  1873. }
  1874. template <typename T>
  1875. tmat3x3<T> operator* (tmat3x3<T> const & m, T const & s)
  1876. {
  1877. return tmat3x3<T>(
  1878. m[0] * s,
  1879. m[1] * s,
  1880. m[2] * s);
  1881. }
  1882. template <typename T>
  1883. tmat3x3<T> operator* (T const & s, tmat3x3<T> const & m)
  1884. {
  1885. return tmat3x3<T>(
  1886. m[0] * s,
  1887. m[1] * s,
  1888. m[2] * s);
  1889. }
  1890. template <typename T>
  1891. tvec3<T> operator* (tmat3x3<T> const & m, tvec3<T> const & v)
  1892. {
  1893. return tvec3<T>(
  1894. m[0][0] * v.x + m[1][0] * v.y + m[2][0] * v.z,
  1895. m[0][1] * v.x + m[1][1] * v.y + m[2][1] * v.z,
  1896. m[0][2] * v.x + m[1][2] * v.y + m[2][2] * v.z);
  1897. }
  1898. template <typename T>
  1899. tvec3<T> operator* (tvec3<T> const & v, tmat3x3<T> const & m)
  1900. {
  1901. return tvec3<T>(
  1902. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z,
  1903. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z,
  1904. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z);
  1905. }
  1906. template <typename T>
  1907. tmat3x3<T> operator* (tmat3x3<T> const & m1, tmat3x3<T> const & m2)
  1908. {
  1909. T const srcA00 = m1[0][0];
  1910. T const srcA01 = m1[0][1];
  1911. T const srcA02 = m1[0][2];
  1912. T const srcA10 = m1[1][0];
  1913. T const srcA11 = m1[1][1];
  1914. T const srcA12 = m1[1][2];
  1915. T const srcA20 = m1[2][0];
  1916. T const srcA21 = m1[2][1];
  1917. T const srcA22 = m1[2][2];
  1918. T const srcB00 = m2[0][0];
  1919. T const srcB01 = m2[0][1];
  1920. T const srcB02 = m2[0][2];
  1921. T const srcB10 = m2[1][0];
  1922. T const srcB11 = m2[1][1];
  1923. T const srcB12 = m2[1][2];
  1924. T const srcB20 = m2[2][0];
  1925. T const srcB21 = m2[2][1];
  1926. T const srcB22 = m2[2][2];
  1927. tmat3x3<T> res;
  1928. res[0][0] = srcA00 * srcB00 + srcA10 * srcB01 + srcA20 * srcB02;
  1929. res[0][1] = srcA01 * srcB00 + srcA11 * srcB01 + srcA21 * srcB02;
  1930. res[0][2] = srcA02 * srcB00 + srcA12 * srcB01 + srcA22 * srcB02;
  1931. res[1][0] = srcA00 * srcB10 + srcA10 * srcB11 + srcA20 * srcB12;
  1932. res[1][1] = srcA01 * srcB10 + srcA11 * srcB11 + srcA21 * srcB12;
  1933. res[1][2] = srcA02 * srcB10 + srcA12 * srcB11 + srcA22 * srcB12;
  1934. res[2][0] = srcA00 * srcB20 + srcA10 * srcB21 + srcA20 * srcB22;
  1935. res[2][1] = srcA01 * srcB20 + srcA11 * srcB21 + srcA21 * srcB22;
  1936. res[2][2] = srcA02 * srcB20 + srcA12 * srcB21 + srcA22 * srcB22;
  1937. return res;
  1938. }
  1939. template <typename T>
  1940. tmat3x3<T> operator/ (tmat3x3<T> const & m, T const & s)
  1941. {
  1942. return tmat3x3<T>(
  1943. m[0] / s,
  1944. m[1] / s,
  1945. m[2] / s);
  1946. }
  1947. template <typename T>
  1948. tmat3x3<T> operator/ (T const & s, tmat3x3<T> const & m)
  1949. {
  1950. return tmat3x3<T>(
  1951. s / m[0],
  1952. s / m[1],
  1953. s / m[2]
  1954. );
  1955. }
  1956. template <typename T>
  1957. tvec3<T> operator/ (tmat3x3<T> const & m, tvec3<T> const & v)
  1958. {
  1959. return m._inverse() * v;
  1960. }
  1961. template <typename T>
  1962. tvec3<T> operator/ (tvec3<T> const & v, tmat3x3<T> const & m)
  1963. {
  1964. return v * m._inverse();
  1965. }
  1966. template <typename T>
  1967. tmat3x3<T> operator/ (tmat3x3<T> const & m1, tmat3x3<T> const & m2)
  1968. {
  1969. return m1 * m2._inverse();
  1970. }
  1971. template <typename T>
  1972. tmat3x3<T> const operator- (tmat3x3<T> const & m)
  1973. {
  1974. return tmat3x3<T>(
  1975. -m[0],
  1976. -m[1],
  1977. -m[2]);
  1978. }
  1979. template <typename T>
  1980. tmat3x3<T> const operator++ (tmat3x3<T> const & m, int)
  1981. {
  1982. return tmat3x3<T>(
  1983. m[0] + T(1),
  1984. m[1] + T(1),
  1985. m[2] + T(1));
  1986. }
  1987. template <typename T>
  1988. tmat3x3<T> const operator-- (tmat3x3<T> const & m, int)
  1989. {
  1990. return tmat3x3<T>(
  1991. m[0] - T(1),
  1992. m[1] - T(1),
  1993. m[2] - T(1));
  1994. }
  1995. template <typename T>
  1996. bool operator==(tmat3x3<T> const & m1, tmat3x3<T> const & m2)
  1997. {
  1998. return (m1[0] == m2[0]) && (m1[1] == m2[1]) && (m1[2] == m2[2]);
  1999. }
  2000. template <typename T>
  2001. bool operator!=(tmat3x3<T> const & m1, tmat3x3<T> const & m2)
  2002. {
  2003. return (m1[0] != m2[0]) || (m1[1] != m2[1]) || (m1[2] != m2[2]);
  2004. }
  2005. template <typename T>
  2006. struct tmat4x4
  2007. {
  2008. typedef T value_type;
  2009. typedef std::size_t size_type;
  2010. typedef tvec4<T> col_type;
  2011. typedef tvec4<T> row_type;
  2012. typedef tmat4x4<T> type;
  2013. typedef tmat4x4<T> transpose_type;
  2014. public:
  2015. tmat4x4<T> inverse() const
  2016. {
  2017. value_type subFactor00 = this->value[2][2] * this->value[3][3] - this->value[3][2] * this->value[2][3];
  2018. value_type subFactor01 = this->value[2][1] * this->value[3][3] - this->value[3][1] * this->value[2][3];
  2019. value_type subFactor02 = this->value[2][1] * this->value[3][2] - this->value[3][1] * this->value[2][2];
  2020. value_type subFactor03 = this->value[2][0] * this->value[3][3] - this->value[3][0] * this->value[2][3];
  2021. value_type subFactor04 = this->value[2][0] * this->value[3][2] - this->value[3][0] * this->value[2][2];
  2022. value_type subFactor05 = this->value[2][0] * this->value[3][1] - this->value[3][0] * this->value[2][1];
  2023. value_type subFactor06 = this->value[1][2] * this->value[3][3] - this->value[3][2] * this->value[1][3];
  2024. value_type subFactor07 = this->value[1][1] * this->value[3][3] - this->value[3][1] * this->value[1][3];
  2025. value_type subFactor08 = this->value[1][1] * this->value[3][2] - this->value[3][1] * this->value[1][2];
  2026. value_type subFactor09 = this->value[1][0] * this->value[3][3] - this->value[3][0] * this->value[1][3];
  2027. value_type subFactor10 = this->value[1][0] * this->value[3][2] - this->value[3][0] * this->value[1][2];
  2028. value_type subFactor11 = this->value[1][1] * this->value[3][3] - this->value[3][1] * this->value[1][3];
  2029. value_type SubFactor12 = this->value[1][0] * this->value[3][1] - this->value[3][0] * this->value[1][1];
  2030. value_type subFactor13 = this->value[1][2] * this->value[2][3] - this->value[2][2] * this->value[1][3];
  2031. value_type subFactor14 = this->value[1][1] * this->value[2][3] - this->value[2][1] * this->value[1][3];
  2032. value_type subFactor15 = this->value[1][1] * this->value[2][2] - this->value[2][1] * this->value[1][2];
  2033. value_type subFactor16 = this->value[1][0] * this->value[2][3] - this->value[2][0] * this->value[1][3];
  2034. value_type subFactor17 = this->value[1][0] * this->value[2][2] - this->value[2][0] * this->value[1][2];
  2035. value_type subFactor18 = this->value[1][0] * this->value[2][1] - this->value[2][0] * this->value[1][1];
  2036. tmat4x4<T> res(
  2037. + this->value[1][1] * subFactor00 - this->value[1][2] * subFactor01 + this->value[1][3] * subFactor02,
  2038. - this->value[1][0] * subFactor00 + this->value[1][2] * subFactor03 - this->value[1][3] * subFactor04,
  2039. + this->value[1][0] * subFactor01 - this->value[1][1] * subFactor03 + this->value[1][3] * subFactor05,
  2040. - this->value[1][0] * subFactor02 + this->value[1][1] * subFactor04 - this->value[1][2] * subFactor05,
  2041. - this->value[0][1] * subFactor00 + this->value[0][2] * subFactor01 - this->value[0][3] * subFactor02,
  2042. + this->value[0][0] * subFactor00 - this->value[0][2] * subFactor03 + this->value[0][3] * subFactor04,
  2043. - this->value[0][0] * subFactor01 + this->value[0][1] * subFactor03 - this->value[0][3] * subFactor05,
  2044. + this->value[0][0] * subFactor02 - this->value[0][1] * subFactor04 + this->value[0][2] * subFactor05,
  2045. + this->value[0][1] * subFactor06 - this->value[0][2] * subFactor07 + this->value[0][3] * subFactor08,
  2046. - this->value[0][0] * subFactor06 + this->value[0][2] * subFactor09 - this->value[0][3] * subFactor10,
  2047. + this->value[0][0] * subFactor11 - this->value[0][1] * subFactor09 + this->value[0][3] * SubFactor12,
  2048. - this->value[0][0] * subFactor08 + this->value[0][1] * subFactor10 - this->value[0][2] * SubFactor12,
  2049. - this->value[0][1] * subFactor13 + this->value[0][2] * subFactor14 - this->value[0][3] * subFactor15,
  2050. + this->value[0][0] * subFactor13 - this->value[0][2] * subFactor16 + this->value[0][3] * subFactor17,
  2051. - this->value[0][0] * subFactor14 + this->value[0][1] * subFactor16 - this->value[0][3] * subFactor18,
  2052. + this->value[0][0] * subFactor15 - this->value[0][1] * subFactor17 + this->value[0][2] * subFactor18);
  2053. value_type determinant =
  2054. + this->value[0][0] * res[0][0]
  2055. + this->value[0][1] * res[1][0]
  2056. + this->value[0][2] * res[2][0]
  2057. + this->value[0][3] * res[3][0];
  2058. res /= determinant;
  2059. return res;
  2060. }
  2061. private:
  2062. col_type value[4];
  2063. public:
  2064. size_type length() const
  2065. {
  2066. return 4;
  2067. }
  2068. size_type col_size()
  2069. {
  2070. return 4;
  2071. }
  2072. size_type row_size()
  2073. {
  2074. return 4;
  2075. }
  2076. void identify()
  2077. {
  2078. this->value[0] = col_type(1, 0, 0, 0);
  2079. this->value[1] = col_type(0, 1, 0, 0);
  2080. this->value[2] = col_type(0, 0, 1, 0);
  2081. this->value[3] = col_type(0, 0, 0, 1);
  2082. }
  2083. col_type & operator[](size_type i)
  2084. {
  2085. assert(i < this->length());
  2086. return this->value[i];
  2087. }
  2088. col_type const & operator[](size_type i) const
  2089. {
  2090. assert(i < this->length());
  2091. return this->value[i];
  2092. }
  2093. tmat4x4(tmat4x4<T> const & m)
  2094. {
  2095. this->value[0] = m.value[0];
  2096. this->value[1] = m.value[1];
  2097. this->value[2] = m.value[2];
  2098. this->value[3] = m.value[3];
  2099. }
  2100. tmat4x4(tmat3x3<T> const & m)
  2101. {
  2102. this->value[0] = col_type(m[0], value_type(0));
  2103. this->value[1] = col_type(m[1], value_type(0));
  2104. this->value[2] = col_type(m[2], value_type(0));
  2105. this->value[3] = col_type(value_type(0), value_type(0), value_type(0), value_type(1));
  2106. }
  2107. tmat4x4()
  2108. {
  2109. }
  2110. tmat4x4(value_type s)
  2111. {
  2112. value_type const Zero(0);
  2113. this->value[0] = col_type(s, Zero, Zero, Zero);
  2114. this->value[1] = col_type(Zero, s, Zero, Zero);
  2115. this->value[2] = col_type(Zero, Zero, s, Zero);
  2116. this->value[3] = col_type(Zero, Zero, Zero, s);
  2117. }
  2118. tmat4x4
  2119. (
  2120. value_type const & x0, value_type const & y0, value_type const & z0, value_type const & w0,
  2121. value_type const & x1, value_type const & y1, value_type const & z1, value_type const & w1,
  2122. value_type const & x2, value_type const & y2, value_type const & z2, value_type const & w2,
  2123. value_type const & x3, value_type const & y3, value_type const & z3, value_type const & w3
  2124. )
  2125. {
  2126. this->value[0] = col_type(x0, y0, z0, w0);
  2127. this->value[1] = col_type(x1, y1, z1, w1);
  2128. this->value[2] = col_type(x2, y2, z2, w2);
  2129. this->value[3] = col_type(x3, y3, z3, w3);
  2130. }
  2131. tmat4x4
  2132. (
  2133. col_type const & v0,
  2134. col_type const & v1,
  2135. col_type const & v2,
  2136. col_type const & v3
  2137. )
  2138. {
  2139. this->value[0] = v0;
  2140. this->value[1] = v1;
  2141. this->value[2] = v2;
  2142. this->value[3] = v3;
  2143. }
  2144. template <typename U>
  2145. tmat4x4(tmat4x4<U> const & m)
  2146. {
  2147. this->value[0] = col_type(m[0]);
  2148. this->value[1] = col_type(m[1]);
  2149. this->value[2] = col_type(m[2]);
  2150. this->value[3] = col_type(m[3]);
  2151. }
  2152. template <typename U>
  2153. tmat4x4(U const & s)
  2154. {
  2155. value_type const Zero(0);
  2156. this->value[0] = tvec4<T>(value_type(s), Zero, Zero, Zero);
  2157. this->value[1] = tvec4<T>(Zero, value_type(s), Zero, Zero);
  2158. this->value[2] = tvec4<T>(Zero, Zero, value_type(s), Zero);
  2159. this->value[3] = tvec4<T>(Zero, Zero, Zero, value_type(s));
  2160. }
  2161. template <
  2162. typename X1, typename Y1, typename Z1, typename W1,
  2163. typename X2, typename Y2, typename Z2, typename W2,
  2164. typename X3, typename Y3, typename Z3, typename W3,
  2165. typename X4, typename Y4, typename Z4, typename W4>
  2166. tmat4x4
  2167. (
  2168. X1 const & x1, Y1 const & y1, Z1 const & z1, W1 const & w1,
  2169. X2 const & x2, Y2 const & y2, Z2 const & z2, W2 const & w2,
  2170. X3 const & x3, Y3 const & y3, Z3 const & z3, W3 const & w3,
  2171. X4 const & x4, Y4 const & y4, Z4 const & z4, W4 const & w4
  2172. )
  2173. {
  2174. this->value[0] = col_type(value_type(x1), value_type(y1), value_type(z1), value_type(w1));
  2175. this->value[1] = col_type(value_type(x2), value_type(y2), value_type(z2), value_type(w2));
  2176. this->value[2] = col_type(value_type(x3), value_type(y3), value_type(z3), value_type(w3));
  2177. this->value[3] = col_type(value_type(x4), value_type(y4), value_type(z4), value_type(w4));
  2178. }
  2179. template <typename V1, typename V2, typename V3, typename V4>
  2180. tmat4x4
  2181. (
  2182. tvec4<V1> const & v1,
  2183. tvec4<V2> const & v2,
  2184. tvec4<V3> const & v3,
  2185. tvec4<V4> const & v4
  2186. )
  2187. {
  2188. this->value[0] = col_type(v1);
  2189. this->value[1] = col_type(v2);
  2190. this->value[2] = col_type(v3);
  2191. this->value[3] = col_type(v4);
  2192. }
  2193. T const * data() const
  2194. {
  2195. return &this->value[0][0];
  2196. }
  2197. tmat4x4<T>& operator= (tmat4x4<T> const & m)
  2198. {
  2199. this->value[0] = m[0];
  2200. this->value[1] = m[1];
  2201. this->value[2] = m[2];
  2202. this->value[3] = m[3];
  2203. return *this;
  2204. }
  2205. template <typename U>
  2206. tmat4x4<T>& operator= (tmat4x4<U> const & m)
  2207. {
  2208. this->value[0] = m[0];
  2209. this->value[1] = m[1];
  2210. this->value[2] = m[2];
  2211. this->value[3] = m[3];
  2212. return *this;
  2213. }
  2214. template <typename U>
  2215. tmat4x4<T>& operator+= (U const & s)
  2216. {
  2217. this->value[0] += s;
  2218. this->value[1] += s;
  2219. this->value[2] += s;
  2220. this->value[3] += s;
  2221. return *this;
  2222. }
  2223. template <typename U>
  2224. tmat4x4<T>& operator+= (tmat4x4<U> const & m)
  2225. {
  2226. this->value[0] += m[0];
  2227. this->value[1] += m[1];
  2228. this->value[2] += m[2];
  2229. this->value[3] += m[3];
  2230. return *this;
  2231. }
  2232. template <typename U>
  2233. tmat4x4<T> & operator-= (U const & s)
  2234. {
  2235. this->value[0] -= s;
  2236. this->value[1] -= s;
  2237. this->value[2] -= s;
  2238. this->value[3] -= s;
  2239. return *this;
  2240. }
  2241. template <typename U>
  2242. tmat4x4<T> & operator-= (tmat4x4<U> const & m)
  2243. {
  2244. this->value[0] -= m[0];
  2245. this->value[1] -= m[1];
  2246. this->value[2] -= m[2];
  2247. this->value[3] -= m[3];
  2248. return *this;
  2249. }
  2250. template <typename U>
  2251. tmat4x4<T> & operator*= (U const & s)
  2252. {
  2253. this->value[0] *= s;
  2254. this->value[1] *= s;
  2255. this->value[2] *= s;
  2256. this->value[3] *= s;
  2257. return *this;
  2258. }
  2259. template <typename U>
  2260. tmat4x4<T> & operator*= (tmat4x4<U> const & m)
  2261. {
  2262. return (*this = *this * m);
  2263. }
  2264. template <typename U>
  2265. tmat4x4<T> & operator/= (U const & s)
  2266. {
  2267. this->value[0] /= s;
  2268. this->value[1] /= s;
  2269. this->value[2] /= s;
  2270. this->value[3] /= s;
  2271. return *this;
  2272. }
  2273. template <typename U>
  2274. tmat4x4<T> & operator/= (tmat4x4<U> const & m)
  2275. {
  2276. return (*this = *this / m);
  2277. }
  2278. tmat4x4<T> & operator++ ()
  2279. {
  2280. ++this->value[0];
  2281. ++this->value[1];
  2282. ++this->value[2];
  2283. ++this->value[3];
  2284. return *this;
  2285. }
  2286. tmat4x4<T> & operator-- ()
  2287. {
  2288. --this->value[0];
  2289. --this->value[1];
  2290. --this->value[2];
  2291. --this->value[3];
  2292. return *this;
  2293. }
  2294. tmat4x4<T>& translate( value_type x,value_type y,value_type z)
  2295. {
  2296. this->value[0] = col_type(1, 0, 0, 0);
  2297. this->value[1] = col_type(0, 1, 0, 0);
  2298. this->value[2] = col_type(0, 0, 1, 0);
  2299. this->value[3] = col_type(x, y, z, 1);
  2300. return *this;
  2301. }
  2302. template<typename U>
  2303. tmat4x4<T>& translate( U x,U y,U z)
  2304. {
  2305. this->value[0] = col_type(1, 0, 0, 0);
  2306. this->value[1] = col_type(0, 1, 0, 0);
  2307. this->value[2] = col_type(0, 0, 1, 0);
  2308. this->value[3] = col_type(T(x), T(y), T(z), 1);
  2309. return *this;
  2310. }
  2311. tmat4x4<T>& translate(tvec3<T> const& pos)
  2312. {
  2313. this->value[0] = col_type(1, 0, 0, 0);
  2314. this->value[1] = col_type(0, 1, 0, 0);
  2315. this->value[2] = col_type(0, 0, 1, 0);
  2316. this->value[3] = col_type(pos.x,pos.y, pos.z, 1);
  2317. return *this;
  2318. }
  2319. template<typename U>
  2320. tmat4x4<T>& translate(tvec3<U> const& pos)
  2321. {
  2322. this->value[0] = col_type(1, 0, 0, 0);
  2323. this->value[1] = col_type(0, 1, 0, 0);
  2324. this->value[2] = col_type(0, 0, 1, 0);
  2325. this->value[3] = col_type(T(pos.x),T(pos.y), T(pos.z), 1);
  2326. return *this;
  2327. }
  2328. tmat4x4<T>& rotate(value_type angle,tvec3<T> const & v )
  2329. {
  2330. T a = DEG2RAD(angle);
  2331. T c = cos(a);
  2332. T s = sin(a);
  2333. tvec3<T> axis = normalize(v);
  2334. tvec3<T> temp = (T(1) - c) * axis;
  2335. tmat4x4<T> res;
  2336. this->value[0][0] = c + temp[0] * axis[0];
  2337. this->value[0][1] = 0 + temp[0] * axis[1] + s * axis[2];
  2338. this->value[0][2] = 0 + temp[0] * axis[2] - s * axis[1];
  2339. this->value[0][3] = 0;
  2340. this->value[1][0] = 0 + temp[1] * axis[0] - s * axis[2];
  2341. this->value[1][1] = c + temp[1] * axis[1];
  2342. this->value[1][2] = 0 + temp[1] * axis[2] + s * axis[0];
  2343. this->value[1][3] = 0;
  2344. this->value[2][0] = 0 + temp[2] * axis[0] + s * axis[1];
  2345. this->value[2][1] = 0 + temp[2] * axis[1] - s * axis[0];
  2346. this->value[2][2] = c + temp[2] * axis[2];
  2347. this->value[2][3] = 0;
  2348. this->value[3][0] = 0;
  2349. this->value[3][1] = 0;
  2350. this->value[3][2] = 0;
  2351. this->value[3][3] = 1;
  2352. return *this;
  2353. }
  2354. tmat4x4<T>& rotateX(value_type angle)
  2355. {
  2356. T a = DEG2RAD(angle);
  2357. T c = cos(a);
  2358. T s = sin(a);
  2359. this->value[0] = col_type(1, 0, 0, 0);
  2360. this->value[1] = col_type(0, c, s, 0);
  2361. this->value[2] = col_type(0,-s, c, 0);
  2362. this->value[3] = col_type(0, 0, 0, 1);
  2363. return *this;
  2364. }
  2365. template<typename U>
  2366. tmat4x4<T>& rotateX(U angle)
  2367. {
  2368. T a = DEG2RAD(angle);
  2369. T c = cos(a);
  2370. T s = sin(a);
  2371. this->value[0] = col_type(1, 0, 0, 0);
  2372. this->value[1] = col_type(0, c, s, 0);
  2373. this->value[2] = col_type(0,-s, c, 0);
  2374. this->value[3] = col_type(0, 0, 0, 1);
  2375. return *this;
  2376. }
  2377. tmat4x4<T>& rotateY(value_type angle)
  2378. {
  2379. T a = DEG2RAD(angle);
  2380. T c = cos(a);
  2381. T s = sin(a);
  2382. this->value[0] = col_type(c, 0,-s, 0);
  2383. this->value[1] = col_type(0, 1, 0, 0);
  2384. this->value[2] = col_type(s, 0, c, 0);
  2385. this->value[3] = col_type(0, 0, 0, 1);
  2386. return *this;
  2387. }
  2388. template<typename U>
  2389. tmat4x4<T>& rotateY(U angle)
  2390. {
  2391. T a = DEG2RAD(angle);
  2392. T c = cos(a);
  2393. T s = sin(a);
  2394. this->value[0] = col_type(c, 0,-s, 0);
  2395. this->value[1] = col_type(0, 1, 0, 0);
  2396. this->value[2] = col_type(s, 0, c, 0);
  2397. this->value[3] = col_type(0, 0, 0, 1);
  2398. return *this;
  2399. }
  2400. tmat4x4<T>& rotateZ(value_type angle)
  2401. {
  2402. T a = T(DEG2RAD(angle));
  2403. T c = cos(a);
  2404. T s = sin(a);
  2405. this->value[0] = col_type( c, s, 0, 0);
  2406. this->value[1] = col_type(-s, c, 0, 0);
  2407. this->value[2] = col_type( 0, 0, 1, 0);
  2408. this->value[3] = col_type( 0, 0, 0, 1);
  2409. return *this;
  2410. }
  2411. template<typename U>
  2412. tmat4x4<T>& rotateZ(U angle)
  2413. {
  2414. T a = DEG2RAD(angle);
  2415. T c = cos(a);
  2416. T s = sin(a);
  2417. this->value[0] = col_type( c, s, 0, 0);
  2418. this->value[1] = col_type(-s, c, 0, 0);
  2419. this->value[2] = col_type( 0, 0, 1, 0);
  2420. this->value[3] = col_type( 0, 0, 0, 1);
  2421. return *this;
  2422. }
  2423. tmat4x4<T> rotateXY(T angleX, T angleY)
  2424. {
  2425. T cosX = cos(DEG2RAD(angleX));
  2426. T sinX = sin(DEG2RAD(angleX));
  2427. T cosY = cos(DEG2RAD(angleY));
  2428. T sinY = sin(DEG2RAD(angleY));
  2429. this->value[0] = col_type( cosY, -sinX * sinY, cosX * sinY,0);
  2430. this->value[1] = col_type( 0, cosX, sinX, 0);
  2431. this->value[2] = col_type( -sinY , -sinX * cosY, cosX * cosY,0);
  2432. this->value[3] = col_type( 0, 0, 0, 1);
  2433. return *this;
  2434. }
  2435. tmat4x4<T> rotateYX(T angleX, T angleY)
  2436. {
  2437. T cosX = cos(DEG2RAD(angleX));
  2438. T sinX = sin(DEG2RAD(angleX));
  2439. T cosY = cos(DEG2RAD(angleY));
  2440. T sinY = sin(DEG2RAD(angleY));
  2441. this->value[0] = col_type( cosY, 0, sinY, 0);
  2442. this->value[1] = col_type( -sinX * sinY,cosX, sinX * cosY,0);
  2443. this->value[2] = col_type( -cosX * sinY,-sinX, cosX * cosY,0);
  2444. this->value[3] = col_type( 0, 0, 0, 1);
  2445. return *this;
  2446. }
  2447. tmat4x4<T> rotateYXZ( T yaw, T pitch, T roll)
  2448. {
  2449. T tmp_ch = cos(DEG2RAD(yaw));
  2450. T tmp_sh = sin(DEG2RAD(yaw));
  2451. T tmp_cp = cos(DEG2RAD(pitch));
  2452. T tmp_sp = sin(DEG2RAD(pitch));
  2453. T tmp_cb = cos(DEG2RAD(roll));
  2454. T tmp_sb = sin(DEG2RAD(roll));
  2455. tmat4x4<T> Result;
  2456. this->value[0][0] = tmp_ch * tmp_cb + tmp_sh * tmp_sp * tmp_sb;
  2457. this->value[0][1] = tmp_sb * tmp_cp;
  2458. this->value[0][2] = -tmp_sh * tmp_cb + tmp_ch * tmp_sp * tmp_sb;
  2459. this->value[0][3] = T(0);
  2460. this->value[1][0] = -tmp_ch * tmp_sb + tmp_sh * tmp_sp * tmp_cb;
  2461. this->value[1][1] = tmp_cb * tmp_cp;
  2462. this->value[1][2] = tmp_sb * tmp_sh + tmp_ch * tmp_sp * tmp_cb;
  2463. this->value[1][3] = T(0);
  2464. this->value[2][0] = tmp_sh * tmp_cp;
  2465. this->value[2][1] = -tmp_sp;
  2466. this->value[2][2] = tmp_ch * tmp_cp;
  2467. this->value[2][3] = T(0);
  2468. this->value[3][0] = T(0);
  2469. this->value[3][1] = T(0);
  2470. this->value[3][2] = T(0);
  2471. this->value[3][3] = T(1);
  2472. return *this;
  2473. }
  2474. tmat4x4<T> yawPitchRoll( T yaw, T pitch, T roll)
  2475. {
  2476. T tmp_ch = cos(DEG2RAD(yaw));
  2477. T tmp_sh = sin(DEG2RAD(yaw));
  2478. T tmp_cp = cos(DEG2RAD(pitch));
  2479. T tmp_sp = sin(DEG2RAD(pitch));
  2480. T tmp_cb = cos(DEG2RAD(roll));
  2481. T tmp_sb = sin(DEG2RAD(roll));
  2482. this->value[0][0] = tmp_ch * tmp_cb + tmp_sh * tmp_sp * tmp_sb;
  2483. this->value[0][1] = tmp_sb * tmp_cp;
  2484. this->value[0][2] = -tmp_sh * tmp_cb + tmp_ch * tmp_sp * tmp_sb;
  2485. this->value[0][3] = T(0);
  2486. this->value[1][0] = -tmp_ch * tmp_sb + tmp_sh * tmp_sp * tmp_cb;
  2487. this->value[1][1] = tmp_cb * tmp_cp;
  2488. this->value[1][2] = tmp_sb * tmp_sh + tmp_ch * tmp_sp * tmp_cb;
  2489. this->value[1][3] = T(0);
  2490. this->value[2][0] = tmp_sh * tmp_cp;
  2491. this->value[2][1] = -tmp_sp;
  2492. this->value[2][2] = tmp_ch * tmp_cp;
  2493. this->value[2][3] = T(0);
  2494. this->value[3][0] = T(0);
  2495. this->value[3][1] = T(0);
  2496. this->value[3][2] = T(0);
  2497. this->value[3][3] = T(1);
  2498. return *this;
  2499. }
  2500. tmat4x4<T>& scale(tvec3<T> const& s)
  2501. {
  2502. this->value[0] = col_type(s[0], 0, 0, 0);
  2503. this->value[1] = col_type(0, s[1], 0, 0);
  2504. this->value[2] = col_type(0, 0, s[2], 0);
  2505. this->value[3] = col_type(0, 0, 0, 1);
  2506. return *this;
  2507. }
  2508. tmat4x4<T>& scale(value_type x,value_type y,value_type z)
  2509. {
  2510. this->value[0] = col_type(x, 0, 0, 0);
  2511. this->value[1] = col_type(0, y, 0, 0);
  2512. this->value[2] = col_type(0, 0, z, 0);
  2513. this->value[3] = col_type(0, 0, 0, 1);
  2514. return *this;
  2515. }
  2516. template<typename U>
  2517. tmat4x4<T>& scale(U x,U y,U z)
  2518. {
  2519. this->value[0] = col_type(value_type(x), 0, 0, 0);
  2520. this->value[1] = col_type(0, value_type(y), 0, 0);
  2521. this->value[2] = col_type(0, 0, value_type(z), 0);
  2522. this->value[3] = col_type(0, 0, 0, 1);
  2523. return *this;
  2524. }
  2525. template<typename U,typename V,typename W>
  2526. tmat4x4<T>& scale(U x,V y,W z)
  2527. {
  2528. this->value[0] = col_type(value_type(x), 0, 0, 0);
  2529. this->value[1] = col_type(0, value_type(y), 0, 0);
  2530. this->value[2] = col_type(0, 0, value_type(z), 0);
  2531. this->value[3] = col_type(0, 0, 0, 1);
  2532. return *this;
  2533. }
  2534. tmat4x4<T> transpose( ) const
  2535. {
  2536. return tmat4x4<T>(
  2537. this->value[0][0], this->value[1][0], this->value[2][0], this->value[3][0],
  2538. this->value[0][1], this->value[1][1], this->value[2][1], this->value[3][1],
  2539. this->value[0][2], this->value[1][2], this->value[2][2], this->value[3][2],
  2540. this->value[0][3], this->value[1][3], this->value[2][3], this->value[3][3]
  2541. );
  2542. }
  2543. tmat4x4<T> extractMatrixRotation() const
  2544. {
  2545. return tmat4x4<T>(
  2546. this->value[0][0], this->value[0][1], this->value[0][2], 0.0,
  2547. this->value[1][0], this->value[1][1], this->value[1][2], 0.0,
  2548. this->value[2][0], this->value[2][1], this->value[2][2], 0.0,
  2549. 0.0, 0.0, 0.0, 1.0
  2550. );
  2551. }
  2552. };
  2553. template <typename T>
  2554. tmat4x4<T> rotateX(T angleX)
  2555. {
  2556. T cosX = cos(DEG2RAD(angleX));
  2557. T sinX = sin(DEG2RAD(angleX));
  2558. return tmat4x4<T>(
  2559. T(1), T(0), T(0), T(0),
  2560. T(0), cosX, sinX, T(0),
  2561. T(0),-sinX, cosX, T(0),
  2562. T(0), T(0), T(0), T(1));
  2563. }
  2564. template <typename T>
  2565. tmat4x4<T> rotateY(T angleY)
  2566. {
  2567. T cosY = cos(DEG2RAD(angleY));
  2568. T sinY = sin(DEG2RAD(angleY));
  2569. return tmat4x4<T>(
  2570. cosY, T(0), sinY, T(0),
  2571. T(0), T(1), T(0), T(0),
  2572. -sinY, T(0), cosY, T(0),
  2573. T(0), T(0), T(0), T(1));
  2574. }
  2575. template <typename T>
  2576. tmat4x4<T> rotateZ(T angleZ)
  2577. {
  2578. T cosZ = (T)cos(DEG2RAD(angleZ));
  2579. T sinZ = (T)sin(DEG2RAD(angleZ));
  2580. return tmat4x4<T>(
  2581. cosZ, sinZ, T(0), T(0),
  2582. -sinZ, cosZ, T(0), T(0),
  2583. T(0), T(0), T(1), T(0),
  2584. T(0), T(0), T(0), T(1));
  2585. }
  2586. template <typename T>
  2587. tmat4x4<T> rotateXY(T angleX, T angleY)
  2588. {
  2589. T cosX = cos(DEG2RAD(angleX));
  2590. T sinX = sin(DEG2RAD(angleX));
  2591. T cosY = cos(DEG2RAD(angleY));
  2592. T sinY = sin(DEG2RAD(angleY));
  2593. return tmat4x4<T>(
  2594. cosY, -sinX * sinY, cosX * sinY, T(0),
  2595. T(0), cosX, sinX, T(0),
  2596. -sinY, -sinX * cosY, cosX * cosY, T(0),
  2597. T(0), T(0), T(0), T(1));
  2598. }
  2599. template <typename T>
  2600. tmat4x4<T> rotateYX(T angleY, T angleX)
  2601. {
  2602. T cosX = cos(DEG2RAD(angleX));
  2603. T sinX = sin(DEG2RAD(angleX));
  2604. T cosY = cos(DEG2RAD(angleY));
  2605. T sinY = sin(DEG2RAD(angleY));
  2606. return tmat4x4<T>(
  2607. cosY, T(0), sinY, T(0),
  2608. -sinX * sinY, cosX, sinX * cosY, T(0),
  2609. -cosX * sinY, -sinX, cosX * cosY, T(0),
  2610. T(0), T(0), T(0), T(1));
  2611. }
  2612. template <typename T>
  2613. tmat4x4<T> rotateXZ(T angleX, T angleZ)
  2614. {
  2615. return rotateX(angleX) * rotateZ(angleZ);
  2616. }
  2617. template <typename T>
  2618. tmat4x4<T> rotateZX(T angleX, T angleZ)
  2619. {
  2620. return rotateZ(angleZ) * rotateX(angleX);
  2621. }
  2622. template <typename T>
  2623. tmat4x4<T> rotateYXZ(T yaw, T pitch, T roll)
  2624. {
  2625. T tmp_ch = cos(DEG2RAD(yaw));
  2626. T tmp_sh = sin(DEG2RAD(yaw));
  2627. T tmp_cp = cos(DEG2RAD(pitch));
  2628. T tmp_sp = sin(DEG2RAD(pitch));
  2629. T tmp_cb = cos(DEG2RAD(roll));
  2630. T tmp_sb = sin(DEG2RAD(roll));
  2631. tmat4x4<T> res;
  2632. res[0][0] = tmp_ch * tmp_cb + tmp_sh * tmp_sp * tmp_sb;
  2633. res[0][1] = tmp_sb * tmp_cp;
  2634. res[0][2] = -tmp_sh * tmp_cb + tmp_ch * tmp_sp * tmp_sb;
  2635. res[0][3] = T(0);
  2636. res[1][0] = -tmp_ch * tmp_sb + tmp_sh * tmp_sp * tmp_cb;
  2637. res[1][1] = tmp_cb * tmp_cp;
  2638. res[1][2] = tmp_sb * tmp_sh + tmp_ch * tmp_sp * tmp_cb;
  2639. res[1][3] = T(0);
  2640. res[2][0] = tmp_sh * tmp_cp;
  2641. res[2][1] = -tmp_sp;
  2642. res[2][2] = tmp_ch * tmp_cp;
  2643. res[2][3] = T(0);
  2644. res[3][0] = T(0);
  2645. res[3][1] = T(0);
  2646. res[3][2] = T(0);
  2647. res[3][3] = T(1);
  2648. return res;
  2649. }
  2650. template <typename T>
  2651. tmat4x4<T> yawPitchRoll(T yaw, T pitch, T roll)
  2652. {
  2653. T tmp_ch = cos(DEG2RAD(yaw));
  2654. T tmp_sh = sin(DEG2RAD(yaw));
  2655. T tmp_cp = cos(DEG2RAD(pitch));
  2656. T tmp_sp = sin(DEG2RAD(pitch));
  2657. T tmp_cb = cos(DEG2RAD(roll));
  2658. T tmp_sb = sin(DEG2RAD(roll));
  2659. tmat4x4<T> res;
  2660. res[0][0] = tmp_ch * tmp_cb + tmp_sh * tmp_sp * tmp_sb;
  2661. res[0][1] = tmp_sb * tmp_cp;
  2662. res[0][2] = -tmp_sh * tmp_cb + tmp_ch * tmp_sp * tmp_sb;
  2663. res[0][3] = T(0);
  2664. res[1][0] = -tmp_ch * tmp_sb + tmp_sh * tmp_sp * tmp_cb;
  2665. res[1][1] = tmp_cb * tmp_cp;
  2666. res[1][2] = tmp_sb * tmp_sh + tmp_ch * tmp_sp * tmp_cb;
  2667. res[1][3] = T(0);
  2668. res[2][0] = tmp_sh * tmp_cp;
  2669. res[2][1] = -tmp_sp;
  2670. res[2][2] = tmp_ch * tmp_cp;
  2671. res[2][3] = T(0);
  2672. res[3][0] = T(0);
  2673. res[3][1] = T(0);
  2674. res[3][2] = T(0);
  2675. res[3][3] = T(1);
  2676. return res;
  2677. }
  2678. template <typename T>
  2679. void axisAngle
  2680. (
  2681. tmat4x4<T> const & mat,
  2682. tvec3<T> & axis,
  2683. T & angle
  2684. )
  2685. {
  2686. T epsilon = (T)0.01;
  2687. T epsilon2 = (T)0.1;
  2688. if ((fabs(mat[1][0] - mat[0][1]) < epsilon) &&
  2689. (fabs(mat[2][0] - mat[0][2]) < epsilon) &&
  2690. (fabs(mat[2][1] - mat[1][2]) < epsilon))
  2691. {
  2692. if ((fabs(mat[1][0] + mat[0][1]) < epsilon2) &&
  2693. (fabs(mat[2][0] + mat[0][2]) < epsilon2) &&
  2694. (fabs(mat[2][1] + mat[1][2]) < epsilon2) &&
  2695. (fabs(mat[0][0] + mat[1][1] + mat[2][2] - (T)3.0) < epsilon2))
  2696. {
  2697. angle = (T)0.0;
  2698. axis.x = (T)1.0;
  2699. axis.y = (T)0.0;
  2700. axis.z = (T)0.0;
  2701. return;
  2702. }
  2703. angle = T(3.1415926535897932384626433832795);
  2704. T xx = (mat[0][0] + (T)1.0) / (T)2.0;
  2705. T yy = (mat[1][1] + (T)1.0) / (T)2.0;
  2706. T zz = (mat[2][2] + (T)1.0) / (T)2.0;
  2707. T xy = (mat[1][0] + mat[0][1]) / (T)4.0;
  2708. T xz = (mat[2][0] + mat[0][2]) / (T)4.0;
  2709. T yz = (mat[2][1] + mat[1][2]) / (T)4.0;
  2710. if ((xx > yy) && (xx > zz))
  2711. {
  2712. if (xx < epsilon)
  2713. {
  2714. axis.x = (T)0.0;
  2715. axis.y = (T)0.7071;
  2716. axis.z = (T)0.7071;
  2717. }
  2718. else
  2719. {
  2720. axis.x = sqrt(xx);
  2721. axis.y = xy / axis.x;
  2722. axis.z = xz / axis.x;
  2723. }
  2724. }
  2725. else if (yy > zz)
  2726. {
  2727. if (yy < epsilon)
  2728. {
  2729. axis.x = (T)0.7071;
  2730. axis.y = (T)0.0;
  2731. axis.z = (T)0.7071;
  2732. }
  2733. else
  2734. {
  2735. axis.y = sqrt(yy);
  2736. axis.x = xy / axis.y;
  2737. axis.z = yz / axis.y;
  2738. }
  2739. }
  2740. else
  2741. {
  2742. if (zz < epsilon)
  2743. {
  2744. axis.x = (T)0.7071;
  2745. axis.y = (T)0.7071;
  2746. axis.z = (T)0.0;
  2747. }
  2748. else
  2749. {
  2750. axis.z = sqrt(zz);
  2751. axis.x = xz / axis.z;
  2752. axis.y = yz / axis.z;
  2753. }
  2754. }
  2755. return;
  2756. }
  2757. T s = sqrt((mat[2][1] - mat[1][2]) * (mat[2][1] - mat[1][2]) + (mat[2][0] - mat[0][2]) * (mat[2][0] - mat[0][2]) + (mat[1][0] - mat[0][1]) * (mat[1][0] - mat[0][1]));
  2758. if (abs(s) < T(0.001))
  2759. s = (T)1.0;
  2760. angle = acos((mat[0][0] + mat[1][1] + mat[2][2] - (T)1.0) / (T)2.0);
  2761. axis.x = (mat[1][2] - mat[2][1]) / s;
  2762. axis.y = (mat[2][0] - mat[0][2]) / s;
  2763. axis.z = (mat[0][1] - mat[1][0]) / s;
  2764. }
  2765. template <typename T>
  2766. tmat4x4<T> axisAngleMatrix(tvec3<T> const & axis,T const angle)
  2767. {
  2768. T c = cos(angle);
  2769. T s = sin(angle);
  2770. T t = T(1) - c;
  2771. tvec3<T> n = normalize(axis);
  2772. return tmat4x4<T>(
  2773. t * n.x * n.x + c, t * n.x * n.y + n.z * s, t * n.x * n.z - n.y * s, T(0),
  2774. t * n.x * n.y - n.z * s, t * n.y * n.y + c, t * n.y * n.z + n.x * s, T(0),
  2775. t * n.x * n.z + n.y * s, t * n.y * n.z - n.x * s, t * n.z * n.z + c, T(0),
  2776. T(0), T(0), T(0), T(1)
  2777. );
  2778. }
  2779. template <typename T>
  2780. tmat4x4<T> interpolate
  2781. (
  2782. tmat4x4<T> const & m1,
  2783. tmat4x4<T> const & m2,
  2784. T const delta
  2785. )
  2786. {
  2787. tmat4x4<T> m1rot = m1.extractMatrixRotation();
  2788. tmat4x4<T> dltRotation = m2 * m1rot.transpose();
  2789. tvec3<T> dltAxis;
  2790. T dltAngle;
  2791. axisAngle(dltRotation, dltAxis, dltAngle);
  2792. tmat4x4<T> out = axisAngleMatrix(dltAxis, dltAngle * delta) * m1rot;
  2793. out[3][0] = m1[3][0] + delta * (m2[3][0] - m1[3][0]);
  2794. out[3][1] = m1[3][1] + delta * (m2[3][1] - m1[3][1]);
  2795. out[3][2] = m1[3][2] + delta * (m2[3][2] - m1[3][2]);
  2796. return out;
  2797. }
  2798. template <typename T>
  2799. tvec3<T> operator * (tvec3<T> const& v, tmat4x4<T> const& mat)
  2800. {
  2801. return tvec3<T>
  2802. (
  2803. v.x*mat[0][0] + v.y*mat[1][0] + v.z*mat[2][0] + 1*mat[3][0],
  2804. v.x*mat[0][1] + v.y*mat[1][1] + v.z*mat[2][1] + 1*mat[3][1],
  2805. v.x*mat[0][2] + v.y*mat[1][2] + v.z*mat[2][2] + 1*mat[3][2]
  2806. );
  2807. }
  2808. template <typename T>
  2809. tmat4x4<T> operator+ (tmat4x4<T> const & m, typename tmat4x4<T>::value_type s)
  2810. {
  2811. return tmat4x4<T>(
  2812. m[0] + s,
  2813. m[1] + s,
  2814. m[2] + s,
  2815. m[3] + s);
  2816. }
  2817. template <typename T>
  2818. tmat4x4<T> operator+ (typename tmat4x4<T>::value_type s, tmat4x4<T> const & m)
  2819. {
  2820. return tmat4x4<T>(
  2821. m[0] + s,
  2822. m[1] + s,
  2823. m[2] + s,
  2824. m[3] + s);
  2825. }
  2826. template <typename T>
  2827. tmat4x4<T> operator+ (tmat4x4<T> const & m1, tmat4x4<T> const & m2)
  2828. {
  2829. return tmat4x4<T>(
  2830. m1[0] + m2[0],
  2831. m1[1] + m2[1],
  2832. m1[2] + m2[2],
  2833. m1[3] + m2[3]);
  2834. }
  2835. template <typename T>
  2836. tmat4x4<T> operator- (tmat4x4<T> const & m, typename tmat4x4<T>::value_type s)
  2837. {
  2838. return tmat4x4<T>(
  2839. m[0] - s,
  2840. m[1] - s,
  2841. m[2] - s,
  2842. m[3] - s);
  2843. }
  2844. template <typename T>
  2845. tmat4x4<T> operator- (typename tmat4x4<T>::value_type s, tmat4x4<T> const & m)
  2846. {
  2847. return tmat4x4<T>(
  2848. s - m[0],
  2849. s - m[1],
  2850. s - m[2],
  2851. s - m[3]);
  2852. }
  2853. template <typename T>
  2854. tmat4x4<T> operator- (tmat4x4<T> const & m1, tmat4x4<T> const & m2)
  2855. {
  2856. return tmat4x4<T>(
  2857. m1[0] - m2[0],
  2858. m1[1] - m2[1],
  2859. m1[2] - m2[2],
  2860. m1[3] - m2[3]);
  2861. }
  2862. template <typename T>
  2863. tmat4x4<T> operator* (tmat4x4<T> const & m, typename tmat4x4<T>::value_type s)
  2864. {
  2865. return tmat4x4<T>(
  2866. m[0] * s,
  2867. m[1] * s,
  2868. m[2] * s,
  2869. m[3] * s);
  2870. }
  2871. template <typename T>
  2872. tmat4x4<T> operator* (typename tmat4x4<T>::value_type s, tmat4x4<T> const & m)
  2873. {
  2874. return tmat4x4<T>(
  2875. m[0] * s,
  2876. m[1] * s,
  2877. m[2] * s,
  2878. m[3] * s);
  2879. }
  2880. template <typename T>
  2881. typename tmat4x4<T>::col_type operator* (tmat4x4<T> const & m, typename tmat4x4<T>::row_type const & v)
  2882. {
  2883. return typename tmat4x4<T>::col_type(
  2884. m[0][0] * v.x + m[1][0] * v.y + m[2][0] * v.z + m[3][0] * v.w,
  2885. m[0][1] * v.x + m[1][1] * v.y + m[2][1] * v.z + m[3][1] * v.w,
  2886. m[0][2] * v.x + m[1][2] * v.y + m[2][2] * v.z + m[3][2] * v.w,
  2887. m[0][3] * v.x + m[1][3] * v.y + m[2][3] * v.z + m[3][3] * v.w);
  2888. }
  2889. template <typename T>
  2890. typename tmat4x4<T>::row_type operator* (typename tmat4x4<T>::col_type const & v, tmat4x4<T> const & m)
  2891. {
  2892. return typename tmat4x4<T>::row_type(
  2893. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w,
  2894. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w,
  2895. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w,
  2896. m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w);
  2897. }
  2898. template <typename T>
  2899. tmat4x4<T> operator* (tmat4x4<T> const & m1, tmat4x4<T> const & m2)
  2900. {
  2901. typename tmat4x4<T>::col_type const srcA0 = m1[0];
  2902. typename tmat4x4<T>::col_type const srcA1 = m1[1];
  2903. typename tmat4x4<T>::col_type const srcA2 = m1[2];
  2904. typename tmat4x4<T>::col_type const srcA3 = m1[3];
  2905. typename tmat4x4<T>::col_type const srcB0 = m2[0];
  2906. typename tmat4x4<T>::col_type const srcB1 = m2[1];
  2907. typename tmat4x4<T>::col_type const srcB2 = m2[2];
  2908. typename tmat4x4<T>::col_type const srcB3 = m2[3];
  2909. tmat4x4<T> res;
  2910. res[0] = srcA0 * srcB0[0] + srcA1 * srcB0[1] + srcA2 * srcB0[2] + srcA3 * srcB0[3];
  2911. res[1] = srcA0 * srcB1[0] + srcA1 * srcB1[1] + srcA2 * srcB1[2] + srcA3 * srcB1[3];
  2912. res[2] = srcA0 * srcB2[0] + srcA1 * srcB2[1] + srcA2 * srcB2[2] + srcA3 * srcB2[3];
  2913. res[3] = srcA0 * srcB3[0] + srcA1 * srcB3[1] + srcA2 * srcB3[2] + srcA3 * srcB3[3];
  2914. return res;
  2915. }
  2916. template <typename T>
  2917. tmat4x4<T> operator/ (tmat4x4<T> const & m, typename tmat4x4<T>::value_type s)
  2918. {
  2919. return tmat4x4<T>(
  2920. m[0] / s,
  2921. m[1] / s,
  2922. m[2] / s,
  2923. m[3] / s);
  2924. }
  2925. template <typename T>
  2926. tmat4x4<T> operator/ (typename tmat4x4<T>::value_type s, tmat4x4<T> const & m)
  2927. {
  2928. return tmat4x4<T>(
  2929. s / m[0],
  2930. s / m[1],
  2931. s / m[2],
  2932. s / m[3]);
  2933. }
  2934. template <typename T>
  2935. typename tmat4x4<T>::col_type operator/ (tmat4x4<T> const & m, typename tmat4x4<T>::row_type const & v)
  2936. {
  2937. return m.inverse() * v;
  2938. }
  2939. template <typename T>
  2940. typename tmat4x4<T>::row_type operator/ (typename tmat4x4<T>::col_type const & v, tmat4x4<T> const & m)
  2941. {
  2942. return v * m.inverse();
  2943. }
  2944. template <typename T>
  2945. tmat4x4<T> operator/ (tmat4x4<T> const & m1, tmat4x4<T> const & m2)
  2946. {
  2947. return m1 * m2.inverse();
  2948. }
  2949. template <typename T>
  2950. tmat4x4<T> const operator- (tmat4x4<T> const & m)
  2951. {
  2952. return tmat4x4<T>(
  2953. -m[0],
  2954. -m[1],
  2955. -m[2],
  2956. -m[3]);
  2957. }
  2958. template <typename T>
  2959. tmat4x4<T> const operator++ (tmat4x4<T> const & m, int)
  2960. {
  2961. return tmat4x4<T>(
  2962. m[0] + T(1),
  2963. m[1] + T(1),
  2964. m[2] + T(1),
  2965. m[3] + T(1));
  2966. }
  2967. template <typename T>
  2968. tmat4x4<T> const operator-- (tmat4x4<T> const & m, int)
  2969. {
  2970. return tmat4x4<T>(
  2971. m[0] - T(1),
  2972. m[1] - T(1),
  2973. m[2] - T(1),
  2974. m[3] - T(1));
  2975. }
  2976. template <typename T>
  2977. bool operator==(tmat4x4<T> const & m1, tmat4x4<T> const & m2)
  2978. {
  2979. return (m1[0] == m2[0]) && (m1[1] == m2[1]) && (m1[2] == m2[2]) && (m1[3] == m2[3]);
  2980. }
  2981. template <typename T>
  2982. bool operator!=(tmat4x4<T> const & m1, tmat4x4<T> const & m2)
  2983. {
  2984. return (m1[0] != m2[0]) || (m1[1] != m2[1]) || (m1[2] != m2[2]) || (m1[3] != m2[3]);
  2985. }
  2986. //////////////////////////////////////////////////////////////////////////
  2987. //////////////////////////////////////////////////////////////////////////
  2988. //! ������
  2989. //////////////////////////////////////////////////////////////////////////
  2990. //////////////////////////////////////////////////////////////////////////
  2991. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  2992. template <typename T>
  2993. typename tvec2<T>::value_type length(tvec2<T> const & v)
  2994. {
  2995. typename tvec2<T>::value_type sqr = v.x * v.x + v.y * v.y;
  2996. return sqrt(sqr);
  2997. }
  2998. template <typename T>
  2999. typename tvec3<T>::value_type length(tvec3<T> const & v)
  3000. {
  3001. typename tvec3<T>::value_type sqr = v.x * v.x + v.y * v.y + v.z * v.z;
  3002. return sqrt(sqr);
  3003. }
  3004. template <typename T>
  3005. typename tvec4<T>::value_type length(tvec4<T> const & v)
  3006. {
  3007. typename tvec4<T>::value_type sqr = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
  3008. return sqrt(sqr);
  3009. }
  3010. template <typename T>
  3011. typename tvec2<T>::value_type distance(tvec2<T> const & p0,tvec2<T> const & p1)
  3012. {
  3013. return length(p1 - p0);
  3014. }
  3015. template <typename T>
  3016. typename tvec3<T>::value_type distance(tvec3<T> const & p0,tvec3<T> const & p1)
  3017. {
  3018. return length(p1 - p0);
  3019. }
  3020. template <typename T>
  3021. typename tvec4<T>::value_type distance(tvec4<T> const & p0,tvec4<T> const & p1)
  3022. {
  3023. return length(p1 - p0);
  3024. }
  3025. template <typename T>
  3026. typename tvec2<T>::value_type dot(tvec2<T> const & x, tvec2<T> const & y)
  3027. {
  3028. return x.x * y.x + x.y * y.y;
  3029. }
  3030. template <typename T>
  3031. typename tvec3<T>::value_type dot(tvec3<T> const & x, tvec3<T> const & y)
  3032. {
  3033. return x.x * y.x + x.y * y.y + x.z * y.z;
  3034. }
  3035. template <typename T>
  3036. typename tvec4<T>::value_type dot(tvec4<T> const & x, tvec4<T> const & y)
  3037. {
  3038. return x.x * y.x + x.y * y.y + x.z * y.z + x.w * y.w;
  3039. }
  3040. template <typename T>
  3041. tvec3<T> cross(tvec3<T> const & x, tvec3<T> const & y)
  3042. {
  3043. return tvec3<T>
  3044. (
  3045. x.y * y.z - y.y * x.z,
  3046. x.z * y.x - y.z * x.x,
  3047. x.x * y.y - y.x * x.y
  3048. );
  3049. }
  3050. template <typename T>
  3051. T inversesqrt(T x)
  3052. {
  3053. return T(1) / sqrt(x);
  3054. }
  3055. template <typename T>
  3056. tvec2<T> normalize(tvec2<T> const & x)
  3057. {
  3058. typename tvec2<T>::value_type sqr = x.x * x.x + x.y * x.y;
  3059. return x * inversesqrt(sqr);
  3060. }
  3061. template <typename T>
  3062. tvec3<T> normalize(tvec3<T> const & x)
  3063. {
  3064. typename tvec3<T>::value_type sqr = x.x * x.x + x.y * x.y + x.z * x.z;
  3065. return x * inversesqrt(sqr);
  3066. }
  3067. template <typename T>
  3068. tvec4<T> normalize(tvec4<T> const & x)
  3069. {
  3070. typename tvec4<T>::value_type sqr = x.x * x.x + x.y * x.y + x.z * x.z + x.w * x.w;
  3071. return x * inversesqrt(sqr);
  3072. }
  3073. //////////////////////////////////////////////////////////////////////////
  3074. //////////////////////////////////////////////////////////////////////////
  3075. //////////////////////////////////////////////////////////////////////////
  3076. template <typename T>
  3077. struct tquat
  3078. {
  3079. typedef T value_type;
  3080. typedef std::size_t size_type;
  3081. public:
  3082. value_type x;
  3083. value_type y;
  3084. value_type z;
  3085. value_type w;
  3086. size_type length() const
  3087. {
  3088. return 4;
  3089. }
  3090. tquat():
  3091. x(0),
  3092. y(0),
  3093. z(0),
  3094. w(1)
  3095. {}
  3096. explicit tquat(value_type s, tvec3<T> const & v):
  3097. x(v.x),
  3098. y(v.y),
  3099. z(v.z),
  3100. w(s)
  3101. {
  3102. }
  3103. explicit tquat(tvec3<T> const & v,value_type s):
  3104. x(v.x),
  3105. y(v.y),
  3106. z(v.z),
  3107. w(s)
  3108. {
  3109. }
  3110. explicit tquat(value_type w, value_type x, value_type y, value_type z):
  3111. x(x),
  3112. y(y),
  3113. z(z),
  3114. w(w)
  3115. {}
  3116. explicit tquat(tvec3<T> const & eulerAngle)
  3117. {
  3118. tvec3<T> c = cos(eulerAngle * value_type(0.5));
  3119. tvec3<T> s = sin(eulerAngle * value_type(0.5));
  3120. this->w = c.x * c.y * c.z + s.x * s.y * s.z;
  3121. this->x = s.x * c.y * c.z - c.x * s.y * s.z;
  3122. this->y = c.x * s.y * c.z + s.x * c.y * s.z;
  3123. this->z = c.x * c.y * s.z - s.x * s.y * c.z;
  3124. }
  3125. explicit tquat(tmat3x3<T> const & m)
  3126. {
  3127. *this = quat_cast(m);
  3128. }
  3129. explicit tquat(tmat4x4<T> const & m)
  3130. {
  3131. *this = quat_cast(m);
  3132. }
  3133. value_type & operator[](int i)
  3134. {
  3135. return (&x)[i];
  3136. }
  3137. value_type const & operator[](int i) const
  3138. {
  3139. return (&x)[i];
  3140. }
  3141. tquat<T> & operator*=(value_type s)
  3142. {
  3143. this->w *= s;
  3144. this->x *= s;
  3145. this->y *= s;
  3146. this->z *= s;
  3147. return *this;
  3148. }
  3149. tquat<T> & operator = (const tquat<T>& right)
  3150. {
  3151. this->w = right.w;
  3152. this->x = right.x;
  3153. this->y = right.y;
  3154. this->z = right.z;
  3155. return *this;
  3156. }
  3157. tquat<T> & operator/=(value_type s)
  3158. {
  3159. this->w /= s;
  3160. this->x /= s;
  3161. this->y /= s;
  3162. this->z /= s;
  3163. return *this;
  3164. }
  3165. };
  3166. template< typename T>
  3167. tmat4x4<T> makeTransform( tvec3<T> const & position, tvec3<T> const& scale, const tquat<T>& orientation)
  3168. {
  3169. tmat3x3<T> rot3x3 = mat3_cast(orientation);
  3170. return tmat4x4<T>
  3171. (
  3172. scale.x * rot3x3[0][0], scale.x * rot3x3[0][1], scale.x * rot3x3[0][2], 0,
  3173. scale.y * rot3x3[1][0], scale.y * rot3x3[1][1], scale.y * rot3x3[1][2], 0,
  3174. scale.z * rot3x3[2][0], scale.z * rot3x3[2][1], scale.z * rot3x3[2][2], 0,
  3175. position.x, position.y, position.z, 1
  3176. );
  3177. }
  3178. template <typename T>
  3179. T dot(tquat<T> const & q1, tquat<T> const & q2)
  3180. {
  3181. return q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;
  3182. }
  3183. template <typename T>
  3184. tquat<T> cross(tquat<T> const & q1, tquat<T> const & q2)
  3185. {
  3186. return tquat<T>(
  3187. q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z,
  3188. q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y,
  3189. q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z,
  3190. q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x);
  3191. }
  3192. template <typename T>
  3193. T length(tquat<T> const & q)
  3194. {
  3195. return sqrt(dot(q, q));
  3196. }
  3197. template <typename genType>
  3198. genType mix(genType x, genType y, genType a)
  3199. {
  3200. return x + a * (y - x);
  3201. }
  3202. template <typename T>
  3203. T epsilon()
  3204. {
  3205. return std::numeric_limits<T>::epsilon();
  3206. }
  3207. template <typename T>
  3208. tquat<T> conjugate(tquat<T> const & q)
  3209. {
  3210. return tquat<T>(q.w, -q.x, -q.y, -q.z);
  3211. }
  3212. template <typename T>
  3213. tquat<T> inverse(tquat<T> const & q)
  3214. {
  3215. return conjugate(q) / dot(q, q);
  3216. }
  3217. template <typename T>
  3218. bool operator==(tquat<T> const & q1, tquat<T> const & q2)
  3219. {
  3220. return (q1.x == q2.x) && (q1.y == q2.y) && (q1.z == q2.z) && (q1.w == q2.w);
  3221. }
  3222. template <typename T>
  3223. bool operator!=(tquat<T> const & q1, tquat<T> const & q2)
  3224. {
  3225. return (q1.x != q2.x) || (q1.y != q2.y) || (q1.z != q2.z) || (q1.w != q2.w);
  3226. }
  3227. template <typename T>
  3228. tquat<T> operator- (tquat<T> const & q)
  3229. {
  3230. return tquat<T>(-q.w, -q.x, -q.y, -q.z);
  3231. }
  3232. template <typename T>
  3233. tquat<T> operator+ ( tquat<T> const & q, tquat<T> const & p)
  3234. {
  3235. return tquat<T>(
  3236. q.w + p.w,
  3237. q.x + p.x,
  3238. q.y + p.y,
  3239. q.z + p.z
  3240. );
  3241. }
  3242. template <typename T>
  3243. tquat<T> operator* ( tquat<T> const & q, tquat<T> const & p)
  3244. {
  3245. return tquat<T>(
  3246. q.w * p.w - q.x * p.x - q.y * p.y - q.z * p.z,
  3247. q.w * p.x + q.x * p.w + q.y * p.z - q.z * p.y,
  3248. q.w * p.y + q.y * p.w + q.z * p.x - q.x * p.z,
  3249. q.w * p.z + q.z * p.w + q.x * p.y - q.y * p.x
  3250. );
  3251. }
  3252. template <typename T>
  3253. tvec3<T> operator* (tquat<T> const & q, tvec3<T> const & v)
  3254. {
  3255. typename tquat<T>::value_type two(2);
  3256. tvec3<T> uv;
  3257. tvec3<T> uuv;
  3258. tvec3<T> quatVector(q.x, q.y, q.z);
  3259. uv = cross(quatVector, v);
  3260. uuv = cross(quatVector, uv);
  3261. uv *= two * q.w;
  3262. uuv *= two;
  3263. return v + uv + uuv;
  3264. }
  3265. template <typename T>
  3266. tvec3<T> operator* (tvec3<T> const & v,tquat<T> const & q)
  3267. {
  3268. return inverse(q) * v;
  3269. }
  3270. template <typename T>
  3271. tquat<T> operator* (tquat<T> const & q, typename tquat<T>::value_type s)
  3272. {
  3273. return tquat<T>(q.w * s, q.x * s, q.y * s, q.z * s);
  3274. }
  3275. template <typename T>
  3276. tquat<T> operator* (typename tquat<T>::value_type s,tquat<T> const & q)
  3277. {
  3278. return q * s;
  3279. }
  3280. template <typename T>
  3281. tquat<T> operator/ (tquat<T> const & q, typename tquat<T>::value_type s)
  3282. {
  3283. return tquat<T>(q.w / s, q.x / s, q.y / s, q.z / s);
  3284. }
  3285. template <typename T>
  3286. tquat<T> mix(tquat<T> const & x, tquat<T> const & y, T const & a)
  3287. {
  3288. T cosTheta = dot(x, y);
  3289. if(cosTheta > T(1) - epsilon<T>())
  3290. {
  3291. return tquat<T>(
  3292. mix(x.w, y.w, a),
  3293. mix(x.x, y.x, a),
  3294. mix(x.y, y.y, a),
  3295. mix(x.z, y.z, a)
  3296. );
  3297. }
  3298. else
  3299. {
  3300. // Essential Mathematics, page 467
  3301. T angle = acos(cosTheta);
  3302. return (sin((T(1) - a) * angle) * x + sin(a * angle) * y) / sin(angle);
  3303. }
  3304. }
  3305. template <typename T>
  3306. tquat<T> lerp(tquat<T> const & x, tquat<T> const & y, T a)
  3307. {
  3308. assert(a >= T(0));
  3309. assert(a <= T(1));
  3310. return x * (T(1) - a) + (y * a);
  3311. }
  3312. template <typename T>
  3313. tquat<T> slerp(tquat<T> const & x, tquat<T> const & y, T a)
  3314. {
  3315. tquat<T> z = y;
  3316. T cosTheta = dot(x, y);
  3317. if (cosTheta < T(0))
  3318. {
  3319. z = -y;
  3320. cosTheta = -cosTheta;
  3321. }
  3322. if(cosTheta > T(1) - epsilon<T>())
  3323. {
  3324. return tquat<T>
  3325. (
  3326. mix(x.w, z.w, a),
  3327. mix(x.x, z.x, a),
  3328. mix(x.y, z.y, a),
  3329. mix(x.z, z.z, a)
  3330. );
  3331. }
  3332. else
  3333. {
  3334. // Essential Mathematics, page 467
  3335. T angle = acos(cosTheta);
  3336. return (sin((T(1) - a) * angle) * x + sin(a * angle) * z) / sin(angle);
  3337. }
  3338. }
  3339. template <typename T>
  3340. tquat<T> rotate
  3341. (
  3342. typename tquat<T>::value_type angle,
  3343. tvec3<T> const & axis
  3344. )
  3345. {
  3346. tvec3<T> Tmp = axis;
  3347. typename tquat<T>::value_type len = length(Tmp);
  3348. if(abs(len - T(1)) > T(0.001f))
  3349. {
  3350. T oneOverLen = T(1) / len;
  3351. Tmp.x *= oneOverLen;
  3352. Tmp.y *= oneOverLen;
  3353. Tmp.z *= oneOverLen;
  3354. }
  3355. typename tquat<T>::value_type const AngleRad = (T)DEG2RAD(angle);
  3356. typename tquat<T>::value_type const Sin = (T)sin(AngleRad * T(0.5));
  3357. return tquat<T>((T)cos(AngleRad * T(0.5)), Tmp.x * Sin, Tmp.y * Sin, Tmp.z * Sin);
  3358. }
  3359. template <typename valType>
  3360. valType roll(tquat<valType> const & q)
  3361. {
  3362. return atan2(valType(2) * (q.x * q.y + q.w * q.z), q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z) * valType(RAD2DEG);
  3363. }
  3364. template <typename valType>
  3365. valType pitch(tquat<valType> const & q)
  3366. {
  3367. return ::atan2(valType(2) * (q.y * q.z + q.w * q.x), q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z)* valType(RAD2DEG);
  3368. }
  3369. template <typename valType>
  3370. valType yaw(tquat<valType> const & q)
  3371. {
  3372. return ::asin(valType(-2) * (q.x * q.z - q.w * q.y)) * valType(RAD2DEG);
  3373. }
  3374. template <typename T>
  3375. tvec3<T> eulerAngles(tquat<T> const & x)
  3376. {
  3377. return tvec3<T>(pitch(x), yaw(x), roll(x));
  3378. }
  3379. template <typename T>
  3380. tmat3x3<T> mat3_cast(const tquat<T>& q)
  3381. {
  3382. return tmat3x3<T>
  3383. (
  3384. 1 - 2 * q.y * q.y - 2 * q.z * q.z, 2 * q.x * q.y + 2 * q.w * q.z, 2 * q.x * q.z - 2 * q.w * q.y,
  3385. 2 * q.x * q.y - 2 * q.w * q.z, 1 - 2 * q.x * q.x - 2 * q.z * q.z, 2 * q.y * q.z + 2 * q.w * q.x,
  3386. 2 * q.x * q.z + 2 * q.w * q.y, 2 * q.y * q.z - 2 * q.w * q.x, 1 - 2 * q.x * q.x - 2 * q.y * q.y
  3387. );
  3388. }
  3389. template <typename T>
  3390. tmat4x4<T> mat4_cast(tquat<T> const & q)
  3391. {
  3392. return tmat4x4<T>(mat3_cast(q));
  3393. }
  3394. template <typename T>
  3395. tquat<T> quat_cast(tmat3x3<T> const & m)
  3396. {
  3397. typename tquat<T>::value_type fourXSquaredMinus1 = m[0][0] - m[1][1] - m[2][2];
  3398. typename tquat<T>::value_type fourYSquaredMinus1 = m[1][1] - m[0][0] - m[2][2];
  3399. typename tquat<T>::value_type fourZSquaredMinus1 = m[2][2] - m[0][0] - m[1][1];
  3400. typename tquat<T>::value_type fourWSquaredMinus1 = m[0][0] + m[1][1] + m[2][2];
  3401. int biggestIndex = 0;
  3402. typename tquat<T>::value_type fourBiggestSquaredMinus1 = fourWSquaredMinus1;
  3403. if(fourXSquaredMinus1 > fourBiggestSquaredMinus1)
  3404. {
  3405. fourBiggestSquaredMinus1 = fourXSquaredMinus1;
  3406. biggestIndex = 1;
  3407. }
  3408. if(fourYSquaredMinus1 > fourBiggestSquaredMinus1)
  3409. {
  3410. fourBiggestSquaredMinus1 = fourYSquaredMinus1;
  3411. biggestIndex = 2;
  3412. }
  3413. if(fourZSquaredMinus1 > fourBiggestSquaredMinus1)
  3414. {
  3415. fourBiggestSquaredMinus1 = fourZSquaredMinus1;
  3416. biggestIndex = 3;
  3417. }
  3418. typename tquat<T>::value_type biggestVal = sqrt(fourBiggestSquaredMinus1 + T(1)) * T(0.5);
  3419. typename tquat<T>::value_type mult = T(0.25) / biggestVal;
  3420. tquat<T> res;
  3421. switch(biggestIndex)
  3422. {
  3423. case 0:
  3424. res.w = biggestVal;
  3425. res.x = (m[1][2] - m[2][1]) * mult;
  3426. res.y = (m[2][0] - m[0][2]) * mult;
  3427. res.z = (m[0][1] - m[1][0]) * mult;
  3428. break;
  3429. case 1:
  3430. res.w = (m[1][2] - m[2][1]) * mult;
  3431. res.x = biggestVal;
  3432. res.y = (m[0][1] + m[1][0]) * mult;
  3433. res.z = (m[2][0] + m[0][2]) * mult;
  3434. break;
  3435. case 2:
  3436. res.w = (m[2][0] - m[0][2]) * mult;
  3437. res.x = (m[0][1] + m[1][0]) * mult;
  3438. res.y = biggestVal;
  3439. res.z = (m[1][2] + m[2][1]) * mult;
  3440. break;
  3441. case 3:
  3442. res.w = (m[0][1] - m[1][0]) * mult;
  3443. res.x = (m[2][0] + m[0][2]) * mult;
  3444. res.y = (m[1][2] + m[2][1]) * mult;
  3445. res.z = biggestVal;
  3446. break;
  3447. default:
  3448. assert(false);
  3449. break;
  3450. }
  3451. return res;
  3452. }
  3453. template <typename T>
  3454. tquat<T> quat_cast(tmat4x4<T> const & m4)
  3455. {
  3456. return quat_cast(tmat3x3<T>(m4[0][0],m4[0][1],m4[0][2],
  3457. m4[1][0],m4[1][1],m4[1][2],
  3458. m4[2][0],m4[2][1],m4[2][2]));
  3459. }
  3460. template <typename T>
  3461. T angle(tquat<T> const & x)
  3462. {
  3463. return acos(x.w) * T(2) * T(RAD2DEG);
  3464. }
  3465. template <typename T>
  3466. tvec3<T> axis(tquat<T> const & x)
  3467. {
  3468. T tmp1 = T(1) - x.w * x.w;
  3469. if(tmp1 <= T(0))
  3470. {
  3471. return tvec3<T>(0, 0, 1);
  3472. }
  3473. T tmp2 = T(1) / sqrt(tmp1);
  3474. return tvec3<T>(x.x * tmp2, x.y * tmp2, x.z * tmp2);
  3475. }
  3476. template <typename valType>
  3477. tquat<valType> angleAxis(valType angle, tvec3<valType> const & axis)
  3478. {
  3479. tquat<valType> result;
  3480. valType a = (valType)(valType(DEG2RAD(angle)));
  3481. valType s = sin(a * valType(0.5));
  3482. result.w = cos(a * valType(0.5));
  3483. result.x = axis.x * s;
  3484. result.y = axis.y * s;
  3485. result.z = axis.z * s;
  3486. return result;
  3487. }
  3488. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  3489. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  3490. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  3491. template <typename T>
  3492. tmat4x4<T> translate(tmat4x4<T> const & m,tvec3<T> const & v)
  3493. {
  3494. tmat4x4<T> res(m);
  3495. res[3] = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3];
  3496. return res;
  3497. }
  3498. template <typename T>
  3499. tmat4x4<T> rotate
  3500. (
  3501. tmat4x4<T> const & m,
  3502. T const & angle,
  3503. tvec3<T> const & v
  3504. )
  3505. {
  3506. T a = DEG2RAD(angle);
  3507. T c = cos(a);
  3508. T s = sin(a);
  3509. tvec3<T> axis = normalize(v);
  3510. tvec3<T> temp = (T(1) - c) * axis;
  3511. tmat4x4<T> res;
  3512. res[0][0] = c + temp[0] * axis[0];
  3513. res[0][1] = 0 + temp[0] * axis[1] + s * axis[2];
  3514. res[0][2] = 0 + temp[0] * axis[2] - s * axis[1];
  3515. res[1][0] = 0 + temp[1] * axis[0] - s * axis[2];
  3516. res[1][1] = c + temp[1] * axis[1];
  3517. res[1][2] = 0 + temp[1] * axis[2] + s * axis[0];
  3518. res[2][0] = 0 + temp[2] * axis[0] + s * axis[1];
  3519. res[2][1] = 0 + temp[2] * axis[1] - s * axis[0];
  3520. res[2][2] = c + temp[2] * axis[2];
  3521. tmat4x4<T> rot;
  3522. rot[0] = m[0] * res[0][0] + m[1] * res[0][1] + m[2] * res[0][2];
  3523. rot[1] = m[0] * res[1][0] + m[1] * res[1][1] + m[2] * res[1][2];
  3524. rot[2] = m[0] * res[2][0] + m[1] * res[2][1] + m[2] * res[2][2];
  3525. rot[3] = m[3];
  3526. return rot;
  3527. }
  3528. template <typename T>
  3529. tmat4x4<T> scale(tmat4x4<T> const & m,tvec3<T> const & v)
  3530. {
  3531. tmat4x4<T> res;
  3532. res[0] = m[0] * v[0];
  3533. res[1] = m[1] * v[1];
  3534. res[2] = m[2] * v[2];
  3535. res[3] = m[3];
  3536. return res;
  3537. }
  3538. template <typename T>
  3539. tmat4x4<T> rotate_slow
  3540. (
  3541. tmat4x4<T> const & m,
  3542. T const & angle,
  3543. tvec3<T> const & v
  3544. )
  3545. {
  3546. T const a = DEG2RAD(angle);
  3547. T c = cos(a);
  3548. T s = sin(a);
  3549. tmat4x4<T> res;
  3550. tvec3<T> axis = normalize(v);
  3551. res[0][0] = c + (1 - c) * axis.x * axis.x;
  3552. res[0][1] = (1 - c) * axis.x * axis.y + s * axis.z;
  3553. res[0][2] = (1 - c) * axis.x * axis.z - s * axis.y;
  3554. res[0][3] = 0;
  3555. res[1][0] = (1 - c) * axis.y * axis.x - s * axis.z;
  3556. res[1][1] = c + (1 - c) * axis.y * axis.y;
  3557. res[1][2] = (1 - c) * axis.y * axis.z + s * axis.x;
  3558. res[1][3] = 0;
  3559. res[2][0] = (1 - c) * axis.z * axis.x + s * axis.y;
  3560. res[2][1] = (1 - c) * axis.z * axis.y - s * axis.x;
  3561. res[2][2] = c + (1 - c) * axis.z * axis.z;
  3562. res[2][3] = 0;
  3563. res[3] = tvec4<T>(0, 0, 0, 1);
  3564. return m * res;
  3565. }
  3566. template <typename T>
  3567. tmat4x4<T> scale_slow(tmat4x4<T> const & m,tvec3<T> const & v)
  3568. {
  3569. tmat4x4<T> res(T(1));
  3570. res[0][0] = v.x;
  3571. res[1][1] = v.y;
  3572. res[2][2] = v.z;
  3573. return m * res;
  3574. }
  3575. template <typename valType>
  3576. tmat4x4<valType> ortho
  3577. (
  3578. valType left,
  3579. valType right,
  3580. valType bottom,
  3581. valType top,
  3582. valType zNear,
  3583. valType zFar
  3584. )
  3585. {
  3586. tmat4x4<valType> res(1);
  3587. res[0][0] = valType(2) / (right - left);
  3588. res[1][1] = valType(2) / (top - bottom);
  3589. res[2][2] = - valType(2) / (zFar - zNear);
  3590. res[3][0] = - (right + left) / (right - left);
  3591. res[3][1] = - (top + bottom) / (top - bottom);
  3592. res[3][2] = - (zFar + zNear) / (zFar - zNear);
  3593. return res;
  3594. }
  3595. template <typename valType>
  3596. tmat4x4<valType> frustum
  3597. (
  3598. valType left,
  3599. valType right,
  3600. valType bottom,
  3601. valType top,
  3602. valType nearVal,
  3603. valType farVal
  3604. )
  3605. {
  3606. tmat4x4<valType> res(0);
  3607. res[0][0] = (valType(2) * nearVal) / (right - left);
  3608. res[1][1] = (valType(2) * nearVal) / (top - bottom);
  3609. res[2][0] = (right + left) / (right - left);
  3610. res[2][1] = (top + bottom) / (top - bottom);
  3611. res[2][2] = -(farVal + nearVal) / (farVal - nearVal);
  3612. res[2][3] = valType(-1);
  3613. res[3][2] = -(valType(2) * farVal * nearVal) / (farVal - nearVal);
  3614. return res;
  3615. }
  3616. template <typename valType>
  3617. tmat4x4<valType> perspective(valType fovy, valType aspect, valType zNear, valType zFar)
  3618. {
  3619. valType range = tan(fovy * valType(DEG2RAD(0.5))) * zNear;
  3620. valType left = -range * aspect;
  3621. valType right = range * aspect;
  3622. valType bottom = -range;
  3623. valType top = range;
  3624. tmat4x4<valType> res(valType(0));
  3625. res[0][0] = (valType(2) * zNear) / (right - left);
  3626. res[1][1] = (valType(2) * zNear) / (top - bottom);
  3627. res[2][2] = - (zFar + zNear) / (zFar - zNear);
  3628. res[2][3] = - valType(1);
  3629. res[3][2] = - (valType(2) * zFar * zNear) / (zFar - zNear);
  3630. return res;
  3631. }
  3632. template <typename T>
  3633. tvec3<T> project
  3634. (
  3635. tvec3<T> const & obj,
  3636. tmat4x4<T> const & model,
  3637. tmat4x4<T> const & proj,
  3638. tvec4<T> const & viewport
  3639. )
  3640. {
  3641. tvec4<T> tmp = tvec4<T>(obj.x, obj.y,obj.z,T(1));
  3642. tmp = model * tmp;
  3643. tmp = proj * tmp;
  3644. tmp /= tmp.w;
  3645. tmp = tmp * T(0.5) + T(0.5);
  3646. tmp[0] = tmp[0] * T(viewport[2]) + T(viewport[0]);
  3647. tmp[1] = tmp[1] * T(viewport[3]) + T(viewport[1]);
  3648. return tvec3<T>(tmp.x,tmp.y,tmp.z);
  3649. }
  3650. template <typename T>
  3651. tvec3<T> unProject
  3652. (
  3653. tvec3<T> const & win,
  3654. tmat4x4<T> const & model,
  3655. tmat4x4<T> const & proj,
  3656. tvec4<T> const & viewport
  3657. )
  3658. {
  3659. tmat4x4<T> inverses = (proj * model).inverse();
  3660. tvec4<T> tmp = tvec4<T>(win.x,win.y,win.z, T(1));
  3661. tmp.x = (tmp.x - T(viewport[0])) / T(viewport[2]);
  3662. tmp.y = (tmp.y - T(viewport[1])) / T(viewport[3]);
  3663. tmp = tmp * T(2) - T(1);
  3664. tvec4<T> obj = inverses * tmp;
  3665. obj /= obj.w;
  3666. return tvec3<T>(obj.x,obj.y,obj.z);
  3667. }
  3668. template <typename T>
  3669. tmat4x4<T> pickMatrix
  3670. (
  3671. tvec2<T> const & center,
  3672. tvec2<T> const & delta,
  3673. tvec4<T> const & viewport
  3674. )
  3675. {
  3676. assert(delta.x > T(0) && delta.y > T(0));
  3677. tmat4x4<T> res(1.0f);
  3678. if(!(delta.x > T(0) && delta.y > T(0)))
  3679. {
  3680. return res;
  3681. }
  3682. tvec3<T> Temp
  3683. (
  3684. (T(viewport[2]) - T(2) * (center.x - T(viewport[0]))) / delta.x,
  3685. (T(viewport[3]) - T(2) * (center.y - T(viewport[1]))) / delta.y,
  3686. T(0)
  3687. );
  3688. res = translate(res, Temp);
  3689. return scale(res, tvec3<T>(T(viewport[2]) / delta.x, T(viewport[3]) / delta.y, T(1)));
  3690. }
  3691. template <typename T>
  3692. tmat4x4<T> lookAt
  3693. (
  3694. tvec3<T> const & eye,
  3695. tvec3<T> const & center,
  3696. tvec3<T> const & up
  3697. )
  3698. {
  3699. tvec3<T> f = normalize(center - eye);
  3700. tvec3<T> u = normalize(up);
  3701. tvec3<T> s = normalize(cross(f, u));
  3702. u = cross(s, f);
  3703. tmat4x4<T> res(1);
  3704. res[0][0] = s.x;
  3705. res[1][0] = s.y;
  3706. res[2][0] = s.z;
  3707. res[0][1] = u.x;
  3708. res[1][1] = u.y;
  3709. res[2][1] = u.z;
  3710. res[0][2] = -f.x;
  3711. res[1][2] = -f.y;
  3712. res[2][2] = -f.z;
  3713. res[3][0] = -dot(s, eye);
  3714. res[3][1] = -dot(u, eye);
  3715. res[3][2] = dot(f, eye);
  3716. return res;
  3717. }
  3718. template<typename T>
  3719. class AxisAlignedBox2D
  3720. {
  3721. public:
  3722. enum Extent
  3723. {
  3724. EXTENT_NULL,
  3725. EXTENT_FINITE,
  3726. EXTENT_INFINITE
  3727. };
  3728. public:
  3729. tvec2<T> _minimum;
  3730. tvec2<T> _maximum;
  3731. Extent _extent;
  3732. public:
  3733. /*
  3734. 1-----2
  3735. /| /|
  3736. / | / |
  3737. 5-----4 |
  3738. | 0--|--3
  3739. | / | /
  3740. |/ |/
  3741. 6-----7
  3742. */
  3743. typedef enum
  3744. {
  3745. FAR_LEFT_BOTTOM = 0,
  3746. FAR_LEFT_TOP = 1,
  3747. FAR_RIGHT_TOP = 2,
  3748. FAR_RIGHT_BOTTOM = 3,
  3749. NEAR_RIGHT_BOTTOM = 7,
  3750. NEAR_LEFT_BOTTOM = 6,
  3751. NEAR_LEFT_TOP = 5,
  3752. NEAR_RIGHT_TOP = 4
  3753. } CornerEnum;
  3754. AxisAlignedBox2D()
  3755. {
  3756. _minimum = tvec2<T>( T(-0.5), T(-0.5));
  3757. _maximum = tvec2<T>( T(0.5), T(0.5));
  3758. _extent = EXTENT_NULL;
  3759. }
  3760. AxisAlignedBox2D(const AxisAlignedBox2D & rkBox)
  3761. {
  3762. setExtents( rkBox._minimum, rkBox._maximum );
  3763. _extent = rkBox._extent;
  3764. }
  3765. AxisAlignedBox2D( const tvec2<T>& min, const tvec2<T>& max )
  3766. {
  3767. setExtents( min, max );
  3768. }
  3769. AxisAlignedBox2D(
  3770. T mx, T my,
  3771. T Mx, T My
  3772. )
  3773. {
  3774. setExtents( tvec2<T>(mx, my), tvec2<T>(Mx, My));
  3775. }
  3776. AxisAlignedBox2D<T>& operator=(const AxisAlignedBox2D<T>& right)
  3777. {
  3778. setExtents(right._minimum, right._maximum);
  3779. return *this;
  3780. }
  3781. ~AxisAlignedBox2D()
  3782. {
  3783. }
  3784. /**
  3785. * Gets the minimum corner of the box.
  3786. */
  3787. const tvec2<T>& getMinimum(void) const
  3788. {
  3789. return _minimum;
  3790. }
  3791. /**
  3792. * Gets a modifiable version of the minimum
  3793. * corner of the box.
  3794. */
  3795. tvec2<T>& getMinimum(void)
  3796. {
  3797. return _minimum;
  3798. }
  3799. void setMinimum( const tvec2<T>& vec )
  3800. {
  3801. _minimum = vec;
  3802. }
  3803. void setMinimum( T x,T y )
  3804. {
  3805. _minimum = tvec2<T>(x,y);
  3806. }
  3807. /**
  3808. * Gets the maximum corner of the box.
  3809. */
  3810. const tvec2<T>& getMaximum(void) const
  3811. {
  3812. return _maximum;
  3813. }
  3814. /**
  3815. * Gets a modifiable version of the maximum
  3816. * corner of the box.
  3817. */
  3818. tvec2<T>& getMaximum(void)
  3819. {
  3820. return _maximum;
  3821. }
  3822. /**
  3823. * Sets the maximum corner of the box.
  3824. */
  3825. void setMaximum( const tvec2<T>& vec )
  3826. {
  3827. _maximum = vec;
  3828. }
  3829. void setMaximum( T x, T y )
  3830. {
  3831. _maximum.x = x;
  3832. _maximum.y = y;
  3833. }
  3834. /**
  3835. * Sets both minimum and maximum extents at once.
  3836. */
  3837. void setExtents( const tvec2<T>& min, const tvec2<T>& max )
  3838. {
  3839. _minimum = min;
  3840. _maximum = max;
  3841. _extent = EXTENT_FINITE;
  3842. }
  3843. void setExtents(
  3844. T mx, T my,
  3845. T Mx, T My
  3846. )
  3847. {
  3848. _minimum.x = mx;
  3849. _minimum.y = my;
  3850. _maximum.x = Mx;
  3851. _maximum.y = My;
  3852. _extent = EXTENT_FINITE;
  3853. }
  3854. inline bool intersects(const AxisAlignedBox2D& b2) const
  3855. {
  3856. if (_maximum.x < b2._minimum.x)
  3857. return false;
  3858. if (_maximum.y < b2._minimum.y)
  3859. return false;
  3860. if (_minimum.x > b2._maximum.x)
  3861. return false;
  3862. if (_minimum.y > b2._maximum.y)
  3863. return false;
  3864. return true;
  3865. }
  3866. inline AxisAlignedBox2D<T> intersection(const AxisAlignedBox2D<T>& b2) const
  3867. {
  3868. tvec2<T> intMin = _minimum;
  3869. tvec2<T> intMax = _maximum;
  3870. intMin.makeCeil(b2.getMinimum());
  3871. intMax.makeFloor(b2.getMaximum());
  3872. if (intMin.x < intMax.x &&
  3873. intMin.y < intMax.y)
  3874. {
  3875. return AxisAlignedBox2D<T>(intMin, intMax);
  3876. }
  3877. return AxisAlignedBox2D<T>();
  3878. }
  3879. inline void setNull()
  3880. {
  3881. _extent = EXTENT_NULL;
  3882. }
  3883. inline bool isNull(void) const
  3884. {
  3885. return (_extent == EXTENT_NULL);
  3886. }
  3887. bool isFinite(void) const
  3888. {
  3889. return (_extent == EXTENT_FINITE);
  3890. }
  3891. inline void setInfinite()
  3892. {
  3893. _extent = EXTENT_INFINITE;
  3894. }
  3895. inline bool isInfinite(void) const
  3896. {
  3897. return (_extent == EXTENT_INFINITE);
  3898. }
  3899. inline bool intersects(const tvec2<T>& v) const
  3900. {
  3901. return( v.x >= _minimum.x && v.x <= _maximum.x &&
  3902. v.y >= _minimum.y && v.y <= _maximum.y );
  3903. }
  3904. inline tvec2<T> getCenter(void) const
  3905. {
  3906. return tvec2<T>(
  3907. (_maximum.x + _minimum.x) * T(0.5f),
  3908. (_maximum.y + _minimum.y) * T(0.5f));
  3909. }
  3910. /**
  3911. * Gets the size of the box
  3912. */
  3913. inline tvec2<T> getSize(void) const
  3914. {
  3915. return _maximum - _minimum;
  3916. }
  3917. inline tvec2<T> getHalfSize(void) const
  3918. {
  3919. return (_maximum - _minimum) * T(0.5);
  3920. }
  3921. inline bool contains(const tvec2<T>& v) const
  3922. {
  3923. return _minimum.x <= v.x && v.x <= _maximum.x &&
  3924. _minimum.y <= v.y && v.y <= _maximum.y;
  3925. }
  3926. inline bool contains(const AxisAlignedBox2D& other) const
  3927. {
  3928. return this->_minimum.x <= other._minimum.x &&
  3929. this->_minimum.y <= other._minimum.y &&
  3930. other._maximum.x <= this->_maximum.x &&
  3931. other._maximum.y <= this->_maximum.y;
  3932. }
  3933. inline bool operator== (const AxisAlignedBox2D& right) const
  3934. {
  3935. return this->_minimum == right._minimum &&
  3936. this->_maximum == right._maximum;
  3937. }
  3938. inline bool operator!= (const AxisAlignedBox2D& right) const
  3939. {
  3940. return !(*this == right);
  3941. }
  3942. inline void merge(tvec2<T> point)
  3943. {
  3944. if (_minimum.x > point.x)
  3945. {
  3946. _minimum.x = point.x;
  3947. }
  3948. if (_minimum.y > point.y)
  3949. {
  3950. _minimum.y = point.y;
  3951. }
  3952. if (_maximum.x < point.x)
  3953. {
  3954. _maximum.x = point.x;
  3955. }
  3956. if (_maximum.y < point.y)
  3957. {
  3958. _maximum.y = point.y;
  3959. }
  3960. }
  3961. inline void merge(AxisAlignedBox2D<T> other)
  3962. {
  3963. _maximum.makeCeil(other._maximum);
  3964. _minimum.makeFloor(other._minimum);
  3965. }
  3966. };
  3967. template<typename T>
  3968. class AxisAlignedBox
  3969. {
  3970. public:
  3971. enum Extent
  3972. {
  3973. EXTENT_NULL,
  3974. EXTENT_FINITE,
  3975. EXTENT_INFINITE
  3976. };
  3977. public:
  3978. tvec3<T> _minimum;
  3979. tvec3<T> _maximum;
  3980. Extent _extent;
  3981. public:
  3982. /*
  3983. 1-----2
  3984. /| /|
  3985. / | / |
  3986. 5-----4 |
  3987. | 0--|--3
  3988. | / | /
  3989. |/ |/
  3990. 6-----7
  3991. */
  3992. typedef enum
  3993. {
  3994. FAR_LEFT_BOTTOM = 0,
  3995. FAR_LEFT_TOP = 1,
  3996. FAR_RIGHT_TOP = 2,
  3997. FAR_RIGHT_BOTTOM = 3,
  3998. NEAR_RIGHT_BOTTOM = 7,
  3999. NEAR_LEFT_BOTTOM = 6,
  4000. NEAR_LEFT_TOP = 5,
  4001. NEAR_RIGHT_TOP = 4
  4002. } CornerEnum;
  4003. AxisAlignedBox()
  4004. {
  4005. _minimum = tvec3<T>( T(-0.5), T(-0.5), T(-0.5) );
  4006. _maximum = tvec3<T>( T(0.5), T(0.5), T(0.5) );
  4007. _extent = EXTENT_NULL;
  4008. }
  4009. AxisAlignedBox(const AxisAlignedBox & rkBox)
  4010. {
  4011. setExtents( rkBox._minimum, rkBox._maximum );
  4012. _extent = rkBox._extent;
  4013. }
  4014. AxisAlignedBox( const tvec3<T>& min, const tvec3<T>& max )
  4015. {
  4016. setExtents( min, max );
  4017. }
  4018. AxisAlignedBox(
  4019. T mx, T my, T mz,
  4020. T Mx, T My, T Mz
  4021. )
  4022. {
  4023. setExtents( mx, my, mz, Mx, My, Mz );
  4024. }
  4025. AxisAlignedBox<T>& operator=(const AxisAlignedBox<T>& right)
  4026. {
  4027. setExtents(right._minimum, right._maximum);
  4028. return *this;
  4029. }
  4030. ~AxisAlignedBox()
  4031. {
  4032. }
  4033. /**
  4034. * Gets the minimum corner of the box.
  4035. */
  4036. const tvec3<T>& getMinimum(void) const
  4037. {
  4038. return _minimum;
  4039. }
  4040. /**
  4041. * Gets a modifiable version of the minimum
  4042. * corner of the box.
  4043. */
  4044. tvec3<T>& getMinimum(void)
  4045. {
  4046. return _minimum;
  4047. }
  4048. void setMinimum(const tvec3<T>& mins)
  4049. {
  4050. _minimum = mins;
  4051. }
  4052. void setMinimum(T x,T y, T z)
  4053. {
  4054. _minimum = tvec3<T>(x,y,z);
  4055. }
  4056. /**
  4057. * Gets the maximum corner of the box.
  4058. */
  4059. const tvec3<T>& getMaximum(void) const
  4060. {
  4061. return _maximum;
  4062. }
  4063. /**
  4064. * Gets a modifiable version of the maximum
  4065. * corner of the box.
  4066. */
  4067. tvec3<T>& getMaximum(void)
  4068. {
  4069. return _maximum;
  4070. }
  4071. /**
  4072. * Sets the maximum corner of the box.
  4073. */
  4074. void setMaximum( const tvec3<T>& vec )
  4075. {
  4076. _maximum = vec;
  4077. }
  4078. void setMaximum( T x, T y, T z )
  4079. {
  4080. _maximum.x = x;
  4081. _maximum.y = y;
  4082. _maximum.z = z;
  4083. }
  4084. /**
  4085. * Changes one of the components of the maximum corner of the box
  4086. * used to resize only one dimension of the box
  4087. */
  4088. void setMaximumX( T x )
  4089. {
  4090. _maximum.x = x;
  4091. }
  4092. void setMaximumY( T y )
  4093. {
  4094. _maximum.y = y;
  4095. }
  4096. void setMaximumZ( T z )
  4097. {
  4098. _maximum.z = z;
  4099. }
  4100. /**
  4101. * Sets both minimum and maximum extents at once.
  4102. */
  4103. void setExtents( const tvec3<T>& min, const tvec3<T>& max )
  4104. {
  4105. _minimum = min;
  4106. _maximum = max;
  4107. _extent = EXTENT_FINITE;
  4108. }
  4109. void setExtents(
  4110. T mx, T my, T mz,
  4111. T Mx, T My, T Mz )
  4112. {
  4113. _minimum.x = mx;
  4114. _minimum.y = my;
  4115. _minimum.z = mz;
  4116. _maximum.x = Mx;
  4117. _maximum.y = My;
  4118. _maximum.z = Mz;
  4119. _extent = EXTENT_FINITE;
  4120. }
  4121. /** Returns a pointer to an array of 8 corner points, useful for
  4122. collision vs. non-aligned objects.
  4123. @remarks
  4124. If the order of these corners is important, they are as
  4125. follows: The 4 points of the minimum Z face (note that
  4126. because Ogre uses right-handed coordinates, the minimum Z is
  4127. at the 'back' of the box) starting with the minimum point of
  4128. all, then anticlockwise around this face (if you are looking
  4129. onto the face from outside the box). Then the 4 points of the
  4130. maximum Z face, starting with maximum point of all, then
  4131. anticlockwise around this face (looking onto the face from
  4132. outside the box). Like this:
  4133. <pre>
  4134. 1-----2
  4135. /| /|
  4136. / | / |
  4137. 5-----4 |
  4138. | 0--|--3
  4139. | / | /
  4140. |/ |/
  4141. 6-----7
  4142. </pre>
  4143. @remarks as this implementation uses a static member, make sure to use your own copy !
  4144. */
  4145. void getAllCorners(tvec3<T> mpCorners[8] ) const
  4146. {
  4147. mpCorners[0] = _minimum;
  4148. mpCorners[1].x = _minimum.x; mpCorners[1].y = _maximum.y; mpCorners[1].z = _minimum.z;
  4149. mpCorners[2].x = _maximum.x; mpCorners[2].y = _maximum.y; mpCorners[2].z = _minimum.z;
  4150. mpCorners[3].x = _maximum.x; mpCorners[3].y = _minimum.y; mpCorners[3].z = _minimum.z;
  4151. mpCorners[4] = _maximum;
  4152. mpCorners[5].x = _minimum.x; mpCorners[5].y = _maximum.y; mpCorners[5].z = _maximum.z;
  4153. mpCorners[6].x = _minimum.x; mpCorners[6].y = _minimum.y; mpCorners[6].z = _maximum.z;
  4154. mpCorners[7].x = _maximum.x; mpCorners[7].y = _minimum.y; mpCorners[7].z = _maximum.z;
  4155. }
  4156. /**
  4157. * gets the position of one of the corners
  4158. */
  4159. tvec3<T> getCorner(CornerEnum cornerToGet) const
  4160. {
  4161. switch(cornerToGet)
  4162. {
  4163. case FAR_LEFT_BOTTOM:
  4164. return _minimum;
  4165. case FAR_LEFT_TOP:
  4166. return tvec3<T>(_minimum.x, _maximum.y, _minimum.z);
  4167. case FAR_RIGHT_TOP:
  4168. return tvec3<T>(_maximum.x, _maximum.y, _minimum.z);
  4169. case FAR_RIGHT_BOTTOM:
  4170. return tvec3<T>(_maximum.x, _minimum.y, _minimum.z);
  4171. case NEAR_RIGHT_BOTTOM:
  4172. return tvec3<T>(_maximum.x, _minimum.y, _maximum.z);
  4173. case NEAR_LEFT_BOTTOM:
  4174. return tvec3<T>(_minimum.x, _minimum.y, _maximum.z);
  4175. case NEAR_LEFT_TOP:
  4176. return tvec3<T>(_minimum.x, _maximum.y, _maximum.z);
  4177. case NEAR_RIGHT_TOP:
  4178. return _maximum;
  4179. default:
  4180. return tvec3<T>();
  4181. }
  4182. }
  4183. /**
  4184. * Merges the passed in box into the current box. The result is the
  4185. * box which encompasses both.
  4186. */
  4187. void merge( const AxisAlignedBox<T>& right )
  4188. {
  4189. if ((right._extent == EXTENT_NULL) || (_extent == EXTENT_INFINITE))
  4190. {
  4191. return;
  4192. }
  4193. else if (right._extent == EXTENT_INFINITE)
  4194. {
  4195. _extent = EXTENT_INFINITE;
  4196. }
  4197. else if (_extent == EXTENT_NULL)
  4198. {
  4199. setExtents(right._minimum, right._maximum);
  4200. }
  4201. else
  4202. {
  4203. //! merge
  4204. tvec3<T> min = _minimum;
  4205. tvec3<T> max = _maximum;
  4206. max.makeCeil(right._maximum);
  4207. min.makeFloor(right._minimum);
  4208. setExtents(min, max);
  4209. }
  4210. }
  4211. /**
  4212. * Extends the box to encompass the specified point (if needed).
  4213. */
  4214. void merge( const tvec3<T>& point )
  4215. {
  4216. switch (_extent)
  4217. {
  4218. case EXTENT_NULL: // if null, use this point
  4219. setExtents(point, point);
  4220. return;
  4221. case EXTENT_FINITE:
  4222. _maximum.makeCeil(point);
  4223. _minimum.makeFloor(point);
  4224. return;
  4225. case EXTENT_INFINITE:
  4226. return;
  4227. }
  4228. }
  4229. void transform( const tmat4x4<T>& matrix )
  4230. {
  4231. tvec3<T> oldMin;
  4232. tvec3<T> oldMax;
  4233. tvec3<T> currentCorner;
  4234. oldMin = _minimum;
  4235. oldMax = _maximum;
  4236. // We sequentially compute the corners in the following order :
  4237. // 0, 6, 5, 1, 2, 4 ,7 , 3
  4238. // This sequence allows us to only change one member at a time to get at all corners.
  4239. // For each one, we transform it using the matrix
  4240. // Which gives the resulting point and merge the resulting point.
  4241. currentCorner = oldMin;
  4242. tvec3<T> vVert = currentCorner * matrix;
  4243. setExtents(vVert,vVert);
  4244. // First corner
  4245. // min min min
  4246. currentCorner = oldMin;
  4247. merge( currentCorner * matrix );
  4248. // min,min,max
  4249. currentCorner.z = oldMax.z;
  4250. merge( currentCorner * matrix );
  4251. // min max max
  4252. currentCorner.y = oldMax.y;
  4253. merge( currentCorner * matrix );
  4254. // min max min
  4255. currentCorner.z = oldMin.z;
  4256. merge( currentCorner * matrix );
  4257. // max max min
  4258. currentCorner.x = oldMax.x;
  4259. merge( currentCorner * matrix );
  4260. // max max max
  4261. currentCorner.z = oldMax.z;
  4262. merge( currentCorner * matrix );
  4263. // max min max
  4264. currentCorner.y = oldMin.y;
  4265. merge( currentCorner * matrix);
  4266. // max min min
  4267. currentCorner.z = oldMin.z;
  4268. merge( currentCorner * matrix);
  4269. }
  4270. /**
  4271. * Returns whether or not this box intersects another.
  4272. */
  4273. bool intersects(const AxisAlignedBox& b2) const
  4274. {
  4275. if (_maximum.x < b2._minimum.x)
  4276. return false;
  4277. if (_maximum.y < b2._minimum.y)
  4278. return false;
  4279. if (_maximum.z < b2._minimum.z)
  4280. return false;
  4281. if (_minimum.x > b2._maximum.x)
  4282. return false;
  4283. if (_minimum.y > b2._maximum.y)
  4284. return false;
  4285. if (_minimum.z > b2._maximum.z)
  4286. return false;
  4287. return true;
  4288. }
  4289. /**
  4290. * Returns whether or not this box intersects another.
  4291. */
  4292. bool intersectsNoZ(const AxisAlignedBox& b2) const
  4293. {
  4294. if (_maximum.x < b2._minimum.x)
  4295. return false;
  4296. if (_maximum.y < b2._minimum.y)
  4297. return false;
  4298. if (_minimum.x > b2._maximum.x)
  4299. return false;
  4300. if (_minimum.y > b2._maximum.y)
  4301. return false;
  4302. return true;
  4303. }
  4304. AxisAlignedBox<T> intersection(const AxisAlignedBox<T>& b2) const
  4305. {
  4306. tvec3<T> intMin = _minimum;
  4307. tvec3<T> intMax = _maximum;
  4308. intMin.makeCeil(b2.getMinimum());
  4309. intMax.makeFloor(b2.getMaximum());
  4310. if (intMin.x < intMax.x &&
  4311. intMin.y < intMax.y &&
  4312. intMin.z < intMax.z)
  4313. {
  4314. return AxisAlignedBox<T>(intMin, intMax);
  4315. }
  4316. return AxisAlignedBox<T>();
  4317. }
  4318. void setNull()
  4319. {
  4320. _extent = EXTENT_NULL;
  4321. }
  4322. bool isNull(void) const
  4323. {
  4324. return (_extent == EXTENT_NULL);
  4325. }
  4326. bool isFinite(void) const
  4327. {
  4328. return (_extent == EXTENT_FINITE);
  4329. }
  4330. void setInfinite()
  4331. {
  4332. _extent = EXTENT_INFINITE;
  4333. }
  4334. bool isInfinite(void) const
  4335. {
  4336. return (_extent == EXTENT_INFINITE);
  4337. }
  4338. void scale(const tvec3<T>& s)
  4339. {
  4340. tvec3<T> min = _minimum * s;
  4341. tvec3<T> max = _maximum * s;
  4342. setExtents(min, max);
  4343. }
  4344. bool intersects(const tvec3<T>& v) const
  4345. {
  4346. return( v.x >= _minimum.x && v.x <= _maximum.x &&
  4347. v.y >= _minimum.y && v.y <= _maximum.y &&
  4348. v.z >= _minimum.z && v.z <= _maximum.z);
  4349. }
  4350. bool intersects(const tvec2<T>& v) const
  4351. {
  4352. return( v.x >= _minimum.x && v.x <= _maximum.x &&
  4353. v.y >= _minimum.y && v.y <= _maximum.y );
  4354. }
  4355. tvec3<T> getCenter(void) const
  4356. {
  4357. return tvec3<T>(
  4358. (_maximum.x + _minimum.x) * T(0.5f),
  4359. (_maximum.y + _minimum.y) * T(0.5f),
  4360. (_maximum.z + _minimum.z) * T(0.5f)
  4361. );
  4362. }
  4363. /**
  4364. * Gets the size of the box
  4365. */
  4366. tvec3<T> getSize(void) const
  4367. {
  4368. return _maximum - _minimum;
  4369. }
  4370. tvec3<T> getHalfSize(void) const
  4371. {
  4372. return (_maximum - _minimum) * T(0.5);
  4373. }
  4374. bool contains(const tvec3<T>& v) const
  4375. {
  4376. return _minimum.x <= v.x && v.x <= _maximum.x &&
  4377. _minimum.y <= v.y && v.y <= _maximum.y &&
  4378. _minimum.z <= v.z && v.z <= _maximum.z;
  4379. }
  4380. bool contains(const AxisAlignedBox& other) const
  4381. {
  4382. return this->_minimum.x <= other._minimum.x &&
  4383. this->_minimum.y <= other._minimum.y &&
  4384. this->_minimum.z <= other._minimum.z &&
  4385. other._maximum.x <= this->_maximum.x &&
  4386. other._maximum.y <= this->_maximum.y &&
  4387. other._maximum.z <= this->_maximum.z;
  4388. }
  4389. bool operator== (const AxisAlignedBox& right) const
  4390. {
  4391. return this->_minimum == right._minimum &&
  4392. this->_maximum == right._maximum;
  4393. }
  4394. bool operator!= (const AxisAlignedBox& right) const
  4395. {
  4396. return !(*this == right);
  4397. }
  4398. };
  4399. template<typename T>
  4400. class tspline
  4401. {
  4402. public:
  4403. tspline()
  4404. {
  4405. mCoeffs[0][0] = 2;
  4406. mCoeffs[0][1] = -2;
  4407. mCoeffs[0][2] = 1;
  4408. mCoeffs[0][3] = 1;
  4409. mCoeffs[1][0] = -3;
  4410. mCoeffs[1][1] = 3;
  4411. mCoeffs[1][2] = -2;
  4412. mCoeffs[1][3] = -1;
  4413. mCoeffs[2][0] = 0;
  4414. mCoeffs[2][1] = 0;
  4415. mCoeffs[2][2] = 1;
  4416. mCoeffs[2][3] = 0;
  4417. mCoeffs[3][0] = 1;
  4418. mCoeffs[3][1] = 0;
  4419. mCoeffs[3][2] = 0;
  4420. mCoeffs[3][3] = 0;
  4421. mCoeffs = mCoeffs.transpose();
  4422. mAutoCalc = true;
  4423. }
  4424. ~tspline(){};
  4425. void addPoint(const tvec3<T>& p)
  4426. {
  4427. mPoints.push_back(p);
  4428. if (mAutoCalc)
  4429. {
  4430. recalcTangents();
  4431. }
  4432. }
  4433. const tvec3<T>& getPoint(size_t index) const
  4434. {
  4435. assert (index < mPoints.size() && "Point index is out of bounds!!");
  4436. return mPoints[index];
  4437. }
  4438. tvec3<T>& getPoint(size_t index)
  4439. {
  4440. assert (index < mPoints.size() && "Point index is out of bounds!!");
  4441. return mPoints[index];
  4442. }
  4443. /**
  4444. * ȡ
  4445. */
  4446. size_t getNumPoints(void) const
  4447. {
  4448. return mPoints.size();
  4449. }
  4450. /**
  4451. * еĵ
  4452. */
  4453. void clear(void)
  4454. {
  4455. mPoints.clear();
  4456. mTangents.clear();
  4457. }
  4458. /**
  4459. * µ
  4460. */
  4461. void updatePoint(size_t index, const tvec3<T>& value)
  4462. {
  4463. assert (index < mPoints.size() && "Point index is out of bounds!!");
  4464. mPoints[index] = value;
  4465. if (mAutoCalc)
  4466. {
  4467. recalcTangents();
  4468. }
  4469. }
  4470. /**
  4471. * ֵȡ
  4472. */
  4473. tvec3<T> interpolate(T time) const
  4474. {
  4475. T fSeg = time * (mPoints.size() - 1);
  4476. unsigned segIdx = (unsigned)fSeg;
  4477. // Apportion t
  4478. time = fSeg - segIdx;
  4479. return interpolate(segIdx, time);
  4480. }
  4481. /**
  4482. * ֵ
  4483. */
  4484. tvec3<T> interpolate(size_t fromIndex, T t) const
  4485. {
  4486. // Bounds check
  4487. assert (fromIndex < mPoints.size() && "fromIndex out of bounds");
  4488. if ((fromIndex + 1) == mPoints.size())
  4489. {
  4490. // Duff request, cannot blend to nothing
  4491. // Just return source
  4492. return mPoints[fromIndex];
  4493. }
  4494. // Fast special cases
  4495. if (t == 0.0f)
  4496. {
  4497. return mPoints[fromIndex];
  4498. }
  4499. else if(t == 1.0f)
  4500. {
  4501. return mPoints[fromIndex + 1];
  4502. }
  4503. // float interpolation
  4504. // Form a vector of powers of t
  4505. T t2, t3;
  4506. t2 = t * t;
  4507. t3 = t2 * t;
  4508. tvec4<T> powers(t3, t2, t, 1);
  4509. const tvec3<T>& point1 = mPoints[fromIndex];
  4510. const tvec3<T>& point2 = mPoints[fromIndex+1];
  4511. const tvec3<T>& tan1 = mTangents[fromIndex];
  4512. const tvec3<T>& tan2 = mTangents[fromIndex+1];
  4513. tmat4x4<T> pt;
  4514. pt[0][0] = point1.x;
  4515. pt[0][1] = point1.y;
  4516. pt[0][2] = point1.z;
  4517. pt[0][3] = 1.0f;
  4518. pt[1][0] = point2.x;
  4519. pt[1][1] = point2.y;
  4520. pt[1][2] = point2.z;
  4521. pt[1][3] = 1.0f;
  4522. pt[2][0] = tan1.x;
  4523. pt[2][1] = tan1.y;
  4524. pt[2][2] = tan1.z;
  4525. pt[2][3] = 1.0f;
  4526. pt[3][0] = tan2.x;
  4527. pt[3][1] = tan2.y;
  4528. pt[3][2] = tan2.z;
  4529. pt[3][3] = 1.0f;
  4530. pt = pt.transpose();
  4531. tvec4<T> ret = powers * mCoeffs * pt;
  4532. return tvec3<T>(ret.x, ret.y, ret.z);
  4533. }
  4534. /**
  4535. * Զ
  4536. */
  4537. void setAutoCalculate(bool autoCalc)
  4538. {
  4539. mAutoCalc = autoCalc;
  4540. }
  4541. /**
  4542. *
  4543. */
  4544. void recalcTangents(void)
  4545. {
  4546. size_t i, numPoints;
  4547. bool isClosed;
  4548. numPoints = mPoints.size();
  4549. if (numPoints < 2)
  4550. {
  4551. return;
  4552. }
  4553. if (mPoints[0] == mPoints[numPoints-1])
  4554. {
  4555. isClosed = true;
  4556. }
  4557. else
  4558. {
  4559. isClosed = false;
  4560. }
  4561. mTangents.resize(numPoints);
  4562. for(i = 0; i < numPoints; ++i)
  4563. {
  4564. if (i ==0)
  4565. {
  4566. // Special case start
  4567. if (isClosed)
  4568. {
  4569. // Use numPoints-2 since numPoints-1 is the last point and == [0]
  4570. mTangents[i] = 0.5f * (mPoints[1] - mPoints[numPoints-2]);
  4571. }
  4572. else
  4573. {
  4574. mTangents[i] = 0.5f * (mPoints[1] - mPoints[0]);
  4575. }
  4576. }
  4577. else if (i == numPoints-1)
  4578. {
  4579. if (isClosed)
  4580. {
  4581. mTangents[i] = mTangents[0];
  4582. }
  4583. else
  4584. {
  4585. mTangents[i] = 0.5f * (mPoints[i] - mPoints[i-1]);
  4586. }
  4587. }
  4588. else
  4589. {
  4590. mTangents[i] = 0.5f * (mPoints[i+1] - mPoints[i-1]);
  4591. }
  4592. }
  4593. }
  4594. public:
  4595. bool mAutoCalc;
  4596. std::vector< tvec3<T> > mPoints;
  4597. std::vector< tvec3<T> > mTangents;
  4598. tmat4x4<T> mCoeffs;
  4599. };
  4600. template < typename T >
  4601. class tellipsoidModel
  4602. {
  4603. public:
  4604. tellipsoidModel(T radiusEquator = T(WGS_84_RADIUS_EQUATOR),T radiusPolar = T(WGS_84_RADIUS_POLAR))
  4605. {
  4606. _radiusEquator = radiusEquator;
  4607. _radiusPolar = radiusPolar;
  4608. T flattening = (_radiusEquator-_radiusPolar)/_radiusEquator;
  4609. _eccentricitySquared= T(2)*flattening - flattening*flattening;
  4610. }
  4611. ~tellipsoidModel(void)
  4612. {
  4613. }
  4614. void convertLatLongHeightToXYZ(
  4615. T latitude,
  4616. T longitude,
  4617. T height,
  4618. T& X,
  4619. T& Y,
  4620. T& Z
  4621. ) const
  4622. {
  4623. // for details on maths see http://www.colorado.edu/geography/gcraft/notes/datum/gif/llhxyz.gif
  4624. T sin_latitude = sin(latitude);
  4625. T cos_latitude = cos(latitude);
  4626. T N = _radiusEquator / sqrt( 1.0 - _eccentricitySquared*sin_latitude*sin_latitude);
  4627. X = (N+height) * cos_latitude*cos(longitude);
  4628. Y = (N+height) * cos_latitude*sin(longitude);
  4629. Z = (N*(1-_eccentricitySquared)+height)*sin_latitude;
  4630. }
  4631. void convertXYZToLatLongHeight(
  4632. T X,
  4633. T Y,
  4634. T Z,
  4635. T& latitude,
  4636. T& longitude,
  4637. T& height
  4638. ) const
  4639. {
  4640. // http://www.colorado.edu/geography/gcraft/notes/datum/gif/xyzllh.gif
  4641. T p = (T)sqrt(X*X + Y*Y);
  4642. T theta = (T)atan2(Z*_radiusEquator , (p*_radiusPolar));
  4643. T eDashSquared = (_radiusEquator*_radiusEquator - _radiusPolar*_radiusPolar) / (_radiusPolar*_radiusPolar);
  4644. T sin_theta = (T)sin(theta);
  4645. T cos_theta = (T)cos(theta);
  4646. latitude = (T)atan( (Z + eDashSquared*_radiusPolar*sin_theta*sin_theta*sin_theta) /
  4647. (p - _eccentricitySquared*_radiusEquator*cos_theta*cos_theta*cos_theta) );
  4648. longitude = (T)atan2(Y,X);
  4649. T sin_latitude = (T)sin(latitude);
  4650. T N = _radiusEquator / (T)sqrt( 1.0 - _eccentricitySquared*sin_latitude*sin_latitude);
  4651. height = p/(T)cos(latitude) - N;
  4652. }
  4653. protected:
  4654. T _radiusEquator;
  4655. T _radiusPolar;
  4656. T _eccentricitySquared;
  4657. };
  4658. class Rgba4Byte
  4659. {
  4660. public:
  4661. Rgba4Byte(
  4662. unsigned char r = 255,
  4663. unsigned char g = 255,
  4664. unsigned char b = 255,
  4665. unsigned char a = 255
  4666. )
  4667. {
  4668. _r = r;
  4669. _g = g;
  4670. _b = b;
  4671. _a = a;
  4672. }
  4673. Rgba4Byte(uint rgba)
  4674. {
  4675. _color = rgba;
  4676. }
  4677. friend bool operator == (const Rgba4Byte& left,const Rgba4Byte& right)
  4678. {
  4679. return left._r==right._r &&
  4680. left._g==right._g &&
  4681. left._b==right._b &&
  4682. left._a==right._a;
  4683. }
  4684. friend bool operator != (const Rgba4Byte& left,const Rgba4Byte& right)
  4685. {
  4686. return left._r !=right._r ||
  4687. left._g!=right._g ||
  4688. left._b!=right._b ||
  4689. left._a!=right._a;
  4690. }
  4691. friend Rgba4Byte operator + (const Rgba4Byte& left,const Rgba4Byte& right)
  4692. {
  4693. return Rgba4Byte(left._r * right._r
  4694. ,left._g * right._g
  4695. ,left._b * right._b
  4696. ,left._a * right._a);
  4697. }
  4698. operator unsigned()
  4699. {
  4700. return _color;
  4701. }
  4702. uint toUint()
  4703. {
  4704. return _color;
  4705. }
  4706. public:
  4707. union
  4708. {
  4709. struct
  4710. {
  4711. unsigned char _b;
  4712. unsigned char _g;
  4713. unsigned char _r;
  4714. unsigned char _a;
  4715. };
  4716. uint _color;
  4717. };
  4718. };
  4719. typedef Rgba4Byte Rgba;
  4720. inline Rgba4Byte colorLerp(const Rgba4Byte& c1, const Rgba4Byte& c2, float s)
  4721. {
  4722. Rgba4Byte color;
  4723. color._r = (unsigned char)(c1._r + s * (c2._r - c1._r));
  4724. color._g = (unsigned char)(c1._g + s * (c2._g - c1._g));
  4725. color._b = (unsigned char)(c1._b + s * (c2._b - c1._b));
  4726. color._a = (unsigned char)(c1._a + s * (c2._a - c1._a));
  4727. return color;
  4728. }
  4729. inline tvec2<float> uvLerp(const tvec2<float>& c1, const tvec2<float>& c2, float s)
  4730. {
  4731. tvec2<float> color;
  4732. color.x = (c1.x + s * (c2.x - c1.x));
  4733. color.y = (c1.y + s * (c2.y - c1.y));
  4734. return color;
  4735. }
  4736. template <typename T>
  4737. class tAxisAlignedBox2
  4738. {
  4739. public:
  4740. enum Extent
  4741. {
  4742. EXTENT_NULL,
  4743. EXTENT_FINITE,
  4744. EXTENT_INFINITE
  4745. };
  4746. public:
  4747. tvec2<T> _vMin;
  4748. tvec2<T> _vMax;
  4749. Extent mExtent;
  4750. public:
  4751. tvec2<T> center() const
  4752. {
  4753. return (_vMin + _vMax) * T(0.5);
  4754. }
  4755. tvec2<T> size() const
  4756. {
  4757. return _vMax - _vMin;
  4758. }
  4759. tvec2<T> halfSize() const
  4760. {
  4761. return (_vMax - _vMin) * T(0.5);
  4762. }
  4763. bool intersects(tvec2<T> v) const
  4764. {
  4765. return( v.x >= _vMin.x && v.x <= _vMax.x &&
  4766. v.y >= _vMin.y && v.y <= _vMax.y );
  4767. }
  4768. void merge(tvec2<T> point)
  4769. {
  4770. if (_vMin.x > point.x)
  4771. {
  4772. _vMin.x = point.x;
  4773. }
  4774. if (_vMin.y > point.y)
  4775. {
  4776. _vMin.y = point.y;
  4777. }
  4778. if (_vMax.x < point.x)
  4779. {
  4780. _vMax.x = point.x;
  4781. }
  4782. if (_vMax.y < point.y)
  4783. {
  4784. _vMax.y = point.y;
  4785. }
  4786. }
  4787. void merge(tAxisAlignedBox2<T> other)
  4788. {
  4789. _vMax.makeCeil(other._vMax);
  4790. _vMin.makeFloor(other._vMin);
  4791. }
  4792. bool contains(tvec2<T> point) const
  4793. {
  4794. return _vMin.x <= point.x && point.x <= _vMax.x &&
  4795. _vMin.y <= point.y && point.y <= _vMax.y ;
  4796. }
  4797. bool contains(tAxisAlignedBox2<T> other) const
  4798. {
  4799. return this->_vMin.x <= other._vMin.x &&
  4800. this->_vMin.y <= other._vMin.y &&
  4801. other._vMax.x <= this->_vMax.x &&
  4802. other._vMax.y <= this->_vMax.y ;
  4803. }
  4804. };
  4805. template<typename T>
  4806. class tray
  4807. {
  4808. typedef T value_type;
  4809. typedef tray<T> type;
  4810. protected:
  4811. tvec3<T> _origin;
  4812. tvec3<T> _direction;
  4813. public:
  4814. tray():
  4815. _origin(value_type(0),value_type(0),value_type(0)),
  4816. _direction(value_type(0),value_type(0),value_type(1))
  4817. {}
  4818. tray(const tvec3<T>& origin, const tvec3<T>& direction):
  4819. _origin(origin),
  4820. _direction(direction)
  4821. {}
  4822. /**
  4823. * ߵ
  4824. */
  4825. void setOrigin(const tvec3<T>& origin)
  4826. {
  4827. _origin = origin;
  4828. }
  4829. /**
  4830. * ߵ
  4831. */
  4832. const tvec3<T>& getOrigin(void) const
  4833. {
  4834. return _origin;
  4835. }
  4836. /**
  4837. * ߵķ
  4838. */
  4839. void setDirection(const tvec3<T>& dir)
  4840. {
  4841. _direction = dir;
  4842. }
  4843. /**
  4844. * ߵķ
  4845. */
  4846. const tvec3<T>& getDirection(void) const
  4847. {
  4848. return _direction;
  4849. }
  4850. /**
  4851. * Gets the position of a point t units along the ray.
  4852. */
  4853. tvec3<T> getPoint(T time) const
  4854. {
  4855. return tvec3<T>(_origin + (_direction * time));
  4856. }
  4857. /**
  4858. * box
  4859. * ,ֵеfirst == true.false
  4860. * secondΪߵľ
  4861. * getPoint򷵻ؽ
  4862. */
  4863. std::pair<bool, T> intersects(const AxisAlignedBox<T>& box) const
  4864. {
  4865. T lowt = 0.0f;
  4866. T t;
  4867. bool hit = false;
  4868. tvec3<T> hitpoint;
  4869. tvec3<T> min = box.getMinimum();
  4870. tvec3<T> max = box.getMaximum();
  4871. /**
  4872. * ڰΧ
  4873. */
  4874. if ( _origin > min && _origin < max )
  4875. {
  4876. return std::pair<bool, T>(true, 0.0f);
  4877. }
  4878. // Check each face in turn, only check closest 3
  4879. // Min x
  4880. if (_origin.x <= min.x && _direction.x > 0)
  4881. {
  4882. t = (min.x - _origin.x) / _direction.x;
  4883. if (t >= 0)
  4884. {
  4885. // Substitute t back into ray and check bounds and dist
  4886. hitpoint = _origin + _direction * t;
  4887. if (hitpoint.y >= min.y &&
  4888. hitpoint.y <= max.y &&
  4889. hitpoint.z >= min.z &&
  4890. hitpoint.z <= max.z &&
  4891. (!hit || t < lowt))
  4892. {
  4893. hit = true;
  4894. lowt = t;
  4895. }
  4896. }
  4897. }
  4898. // Max x
  4899. if (_origin.x >= max.x && _direction.x < 0)
  4900. {
  4901. t = (max.x - _origin.x) / _direction.x;
  4902. if (t >= 0)
  4903. {
  4904. // Substitute t back into ray and check bounds and dist
  4905. hitpoint = _origin + _direction * t;
  4906. if (hitpoint.y >= min.y &&
  4907. hitpoint.y <= max.y &&
  4908. hitpoint.z >= min.z &&
  4909. hitpoint.z <= max.z &&
  4910. (!hit || t < lowt))
  4911. {
  4912. hit = true;
  4913. lowt = t;
  4914. }
  4915. }
  4916. }
  4917. // Min y
  4918. if (_origin.y <= min.y && _direction.y > 0)
  4919. {
  4920. t = (min.y - _origin.y) / _direction.y;
  4921. if (t >= 0)
  4922. {
  4923. // Substitute t back into ray and check bounds and dist
  4924. hitpoint = _origin + _direction * t;
  4925. if (hitpoint.x >= min.x &&
  4926. hitpoint.x <= max.x &&
  4927. hitpoint.z >= min.z &&
  4928. hitpoint.z <= max.z &&
  4929. (!hit || t < lowt))
  4930. {
  4931. hit = true;
  4932. lowt = t;
  4933. }
  4934. }
  4935. }
  4936. // Max y
  4937. if (_origin.y >= max.y && _direction.y < 0)
  4938. {
  4939. t = (max.y - _origin.y) / _direction.y;
  4940. if (t >= 0)
  4941. {
  4942. // Substitute t back into ray and check bounds and dist
  4943. hitpoint = _origin + _direction * t;
  4944. if (hitpoint.x >= min.x &&
  4945. hitpoint.x <= max.x &&
  4946. hitpoint.z >= min.z &&
  4947. hitpoint.z <= max.z &&
  4948. (!hit || t < lowt))
  4949. {
  4950. hit = true;
  4951. lowt = t;
  4952. }
  4953. }
  4954. }
  4955. // Min z
  4956. if (_origin.z <= min.z && _direction.z > 0)
  4957. {
  4958. t = (min.z - _origin.z) / _direction.z;
  4959. if (t >= 0)
  4960. {
  4961. // Substitute t back into ray and check bounds and dist
  4962. hitpoint = _origin + _direction * t;
  4963. if (hitpoint.x >= min.x &&
  4964. hitpoint.x <= max.x &&
  4965. hitpoint.y >= min.y &&
  4966. hitpoint.y <= max.y &&
  4967. (!hit || t < lowt))
  4968. {
  4969. hit = true;
  4970. lowt = t;
  4971. }
  4972. }
  4973. }
  4974. // Max z
  4975. if (_origin.z >= max.z && _direction.z < 0)
  4976. {
  4977. t = (max.z - _origin.z) / _direction.z;
  4978. if (t >= 0)
  4979. {
  4980. // Substitute t back into ray and check bounds and dist
  4981. hitpoint = _origin + _direction * t;
  4982. if (hitpoint.x >= min.x &&
  4983. hitpoint.x <= max.x &&
  4984. hitpoint.y >= min.y &&
  4985. hitpoint.y <= max.y &&
  4986. (!hit || t < lowt))
  4987. {
  4988. hit = true;
  4989. lowt = t;
  4990. }
  4991. }
  4992. }
  4993. return std::pair<bool, T>(hit, lowt);
  4994. }
  4995. };
  4996. template<class T>
  4997. class Plane
  4998. {
  4999. public:
  5000. tvec3<T> _normal;
  5001. T _distance;
  5002. public:
  5003. Plane ()
  5004. {
  5005. _normal = tvec3<T>(0,0,0);
  5006. _distance = 0.0f;
  5007. }
  5008. Plane (const Plane& right)
  5009. {
  5010. _normal = right._normal;
  5011. _distance = right._distance;
  5012. }
  5013. /** Construct a plane through a normal, and a distance to move the plane along the normal.*/
  5014. Plane (const tvec3<T>& rkNormal, T fConstant)
  5015. {
  5016. _normal = rkNormal;
  5017. _distance = -fConstant;
  5018. }
  5019. /** Construct a plane using the 4 constants directly **/
  5020. Plane (T x, T y, T z, T o)
  5021. {
  5022. _normal = tvec3<T>(x, y, z);
  5023. T invLen = 1.0f / (_normal).length();
  5024. _normal *= invLen;
  5025. _distance = o * invLen;
  5026. }
  5027. Plane (const tvec3<T>& rkNormal, const tvec3<T>& rkPoint)
  5028. {
  5029. redefine(rkNormal, rkPoint);
  5030. }
  5031. Plane (const tvec3<T>& rkPoint0, const tvec3<T>& rkPoint1,const tvec3<T>& rkPoint2)
  5032. {
  5033. redefine(rkPoint0, rkPoint1, rkPoint2);
  5034. }
  5035. /**
  5036. * ľ
  5037. */
  5038. float distance(const tvec3<T> &pos) const
  5039. {
  5040. return dot(_normal,pos) + _distance;
  5041. }
  5042. /** The "positive side" of the plane is the half space to which the
  5043. plane normal points. The "negative side" is the other half
  5044. space. The flag "no side" indicates the plane itself.
  5045. */
  5046. enum Side
  5047. {
  5048. NO_SIDE,
  5049. POSITIVE_SIDE,
  5050. NEGATIVE_SIDE,
  5051. BOTH_SIDE
  5052. };
  5053. Side getSide (const tvec3<T>& rkPoint) const
  5054. {
  5055. float fDistance = getDistance(rkPoint);
  5056. if ( fDistance < 0.0 )
  5057. return Plane::NEGATIVE_SIDE;
  5058. if ( fDistance > 0.0 )
  5059. return Plane::POSITIVE_SIDE;
  5060. return Plane::NO_SIDE;
  5061. }
  5062. Side getSide (const tvec3<T>& centre, const tvec3<T>& halfSize) const
  5063. {
  5064. // Calculate the distance between box centre and the plane
  5065. float dist = getDistance(centre);
  5066. // Calculate the maximise allows absolute distance for
  5067. // the distance between box centre and plane
  5068. float maxAbsDist = _normal.absDot(halfSize);
  5069. if (dist < -maxAbsDist)
  5070. return Plane::NEGATIVE_SIDE;
  5071. if (dist > +maxAbsDist)
  5072. return Plane::POSITIVE_SIDE;
  5073. return Plane::BOTH_SIDE;
  5074. }
  5075. float getDistance (const tvec3<T>& rkPoint) const
  5076. {
  5077. return _normal.dot(rkPoint) + _distance;
  5078. }
  5079. void redefine(const tvec3<T>& rkPoint0, const tvec3<T>& rkPoint1,
  5080. const tvec3<T>& rkPoint2)
  5081. {
  5082. tvec3<T> kEdge1 = rkPoint1 - rkPoint0;
  5083. tvec3<T> kEdge2 = rkPoint2 - rkPoint0;
  5084. _normal = cross(kEdge1,kEdge2);
  5085. _normal.normalise();
  5086. _distance = -dot(_normal,rkPoint0);
  5087. }
  5088. /** Redefine this plane based on a normal and a point. */
  5089. void redefine(const tvec3<T>& rkNormal, const tvec3<T>& rkPoint)
  5090. {
  5091. _normal = rkNormal;
  5092. _distance = -dot(rkNormal,rkPoint);
  5093. }
  5094. // tvec3<T> projectVector(const tvec3<T>& p) const
  5095. // {
  5096. // matrix3 xform;
  5097. // xform[0][0] = 1.0f - _normal.x * _normal.x;
  5098. // xform[0][1] = -_normal.x * _normal.y;
  5099. // xform[0][2] = -_normal.x * _normal.z;
  5100. // xform[1][0] = -_normal.y * _normal.x;
  5101. // xform[1][1] = 1.0f - _normal.y * _normal.y;
  5102. // xform[1][2] = -_normal.y * _normal.z;
  5103. // xform[2][0] = -_normal.z * _normal.x;
  5104. // xform[2][1] = -_normal.z * _normal.y;
  5105. // xform[2][2] = 1.0f - _normal.z * _normal.z;
  5106. // return xform * p;
  5107. // }
  5108. /** Normalises the plane.
  5109. @remarks
  5110. This method normalises the plane's normal and the length scale of d
  5111. is as well.
  5112. @note
  5113. This function will not crash for zero-sized vectors, but there
  5114. will be no changes made to their components.
  5115. @returns The previous length of the plane's normal.
  5116. */
  5117. float normalise(void)
  5118. {
  5119. float fLength = _normal.length();
  5120. // Will also work for zero-sized vectors, but will change nothing
  5121. if (fLength > 1e-08f)
  5122. {
  5123. float fInvLength = 1.0f / fLength;
  5124. _normal *= fInvLength;
  5125. _distance *= fInvLength;
  5126. }
  5127. return fLength;
  5128. }
  5129. /// Comparison operator
  5130. bool operator==(const Plane& right) const
  5131. {
  5132. return (right._distance == _distance && right._normal == _normal);
  5133. }
  5134. bool operator!=(const Plane& right) const
  5135. {
  5136. return (right._distance != _distance && right._normal != _normal);
  5137. }
  5138. };
  5139. template<class T>
  5140. class tfrustum
  5141. {
  5142. public:
  5143. enum
  5144. {
  5145. FRUSTUM_LEFT = 0,
  5146. FRUSTUM_RIGHT = 1,
  5147. FRUSTUM_TOP = 2,
  5148. FRUSTUM_BOTTOM = 3,
  5149. FRUSTUM_FAR = 4,
  5150. FRUSTUM_NEAR = 5,
  5151. };
  5152. public:
  5153. /**
  5154. * project * modleview
  5155. */
  5156. void loadFrustum(const tmat4x4<T> &mvp)
  5157. {
  5158. const T* dataPtr = mvp.data();
  5159. _planes[FRUSTUM_LEFT ] = Plane<T>(dataPtr[12] - dataPtr[0], dataPtr[13] - dataPtr[1], dataPtr[14] - dataPtr[2], dataPtr[15] - dataPtr[3]);
  5160. _planes[FRUSTUM_RIGHT ] = Plane<T>(dataPtr[12] + dataPtr[0], dataPtr[13] + dataPtr[1], dataPtr[14] + dataPtr[2], dataPtr[15] + dataPtr[3]);
  5161. _planes[FRUSTUM_TOP ] = Plane<T>(dataPtr[12] - dataPtr[4], dataPtr[13] - dataPtr[5], dataPtr[14] - dataPtr[6], dataPtr[15] - dataPtr[7]);
  5162. _planes[FRUSTUM_BOTTOM] = Plane<T>(dataPtr[12] + dataPtr[4], dataPtr[13] + dataPtr[5], dataPtr[14] + dataPtr[6], dataPtr[15] + dataPtr[7]);
  5163. _planes[FRUSTUM_FAR ] = Plane<T>(dataPtr[12] - dataPtr[8], dataPtr[13] - dataPtr[9], dataPtr[14] - dataPtr[10], dataPtr[15] - dataPtr[11]);
  5164. _planes[FRUSTUM_NEAR ] = Plane<T>(dataPtr[12] + dataPtr[8], dataPtr[13] + dataPtr[9], dataPtr[14] + dataPtr[10], dataPtr[15] + dataPtr[11]);
  5165. }
  5166. bool pointInFrustum(const tvec3<T> &pos) const
  5167. {
  5168. for (int i = 0; i < 6; i++)
  5169. {
  5170. if (_planes[i].distance(pos) <= 0)
  5171. return false;
  5172. }
  5173. return true;
  5174. }
  5175. bool sphereInFrustum(const tvec3<T> &pos, const float radius) const
  5176. {
  5177. for (int i = 0; i < 6; i++)
  5178. {
  5179. if (_planes[i].distance(pos) <= -radius)
  5180. return false;
  5181. }
  5182. return true;
  5183. }
  5184. bool cubeInFrustum(T minX,T maxX,T minY,T maxY,T minZ,T maxZ) const
  5185. {
  5186. for (int i = 0; i < 6; i++)
  5187. {
  5188. if (_planes[i].distance(tvec3<T>(minX, minY, minZ)) > 0) continue;
  5189. if (_planes[i].distance(tvec3<T>(minX, minY, maxZ)) > 0) continue;
  5190. if (_planes[i].distance(tvec3<T>(minX, maxY, minZ)) > 0) continue;
  5191. if (_planes[i].distance(tvec3<T>(minX, maxY, maxZ)) > 0) continue;
  5192. if (_planes[i].distance(tvec3<T>(maxX, minY, minZ)) > 0) continue;
  5193. if (_planes[i].distance(tvec3<T>(maxX, minY, maxZ)) > 0) continue;
  5194. if (_planes[i].distance(tvec3<T>(maxX, maxY, minZ)) > 0) continue;
  5195. if (_planes[i].distance(tvec3<T>(maxX, maxY, maxZ)) > 0) continue;
  5196. return false;
  5197. }
  5198. return true;
  5199. }
  5200. const Plane<T> &getPlane(const int plane) const
  5201. {
  5202. return _planes[plane];
  5203. }
  5204. protected:
  5205. Plane<T> _planes[6];
  5206. };
  5207. typedef float real;
  5208. typedef tvec2<int> int2;
  5209. typedef tvec2<float> float2;
  5210. typedef tvec2<double> double2;
  5211. typedef tvec2<real> real2;
  5212. typedef tvec3<int> int3;
  5213. typedef tvec3<unsigned> uint3;
  5214. typedef tvec3<float> float3;
  5215. typedef tvec3<double> double3;
  5216. typedef tvec3<real> real3;
  5217. typedef tvec4<int> int4;
  5218. typedef tvec4<float> float4;
  5219. typedef tvec4<double> double4;
  5220. typedef tvec4<real> real4;
  5221. typedef trect<real> rect4;
  5222. typedef trect<int> rect4i;
  5223. typedef AxisAlignedBox<float> aabb3d;
  5224. typedef AxisAlignedBox<real> aabbr;
  5225. typedef AxisAlignedBox2D<float> AABB2D;
  5226. typedef AxisAlignedBox2D<real> aabb2dr;
  5227. typedef AxisAlignedBox2D<int> aabb2di;
  5228. typedef tmat2x2<float> matrix2;
  5229. typedef tmat3x3<float> matrix3;
  5230. typedef tmat4x4<float> matrix4;
  5231. typedef tmat4x4<real> matrix4r;
  5232. typedef tquat<float> quaternion;
  5233. typedef tquat<real> quatr;
  5234. typedef tray<float> Ray;
  5235. typedef tfrustum<float> Frustum;
  5236. typedef tellipsoidModel<float> ellipsoid;
  5237. }