vmath vmath-0.13

vmath.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
119 // Modified 2011-06-12, Davide Bacchet (davide.bacchet at gmail dot com)
120 // added () operators with standard matrix notation (opposite wrt at() ).
121 #ifndef __vmath_Header_File__
122 #define __vmath_Header_File__
123
124 #include <cmath>
125 #include <cstring>
126 #include <iostream>
127 #include <sstream>
128 #include <string>
129 #include <cassert>
130
131 #ifdef VMATH_NAMESPACE
132 namespace VMATH_NAMESPACE
133 {
134 #endif
135
136 #ifndef M_PI
137 #define M_PI 3.14159265358979323846 /* pi */
138 #endif
139
140 #define DEG2RAD(x) ((x * M_PI) / 180.0)
141 //#define EPSILON (4.37114e-07)
142
143 const double epsilon = 4.37114e-05;
144 #define EPSILON epsilon
145
156 template<class T>
157 class Vector2
158 {
159 public:
160 union
161 {
165 T x;
166
171 T s;
172 };
173
174 union
175 {
179 T y;
180
185 T t;
186 };
187
188 //----------------[ constructors ]--------------------------
192 Vector2()
193 : x(0), y(0)
194 {
195 }
196
202 Vector2(T nx, T ny)
203 : x(nx), y(ny)
204 {
205 }
206
211 Vector2(const Vector2<T>& src)
212 : x(src.x), y(src.y)
213 {
214 }
215
220 template<class FromT>
221 Vector2(const Vector2<FromT>& src)
222 : x(static_cast<T>(src.x)), y(static_cast<T>(src.y))
223 {
224 }
225
226 //----------------[ access operators ]-------------------
231 template<class FromT>
232 Vector2<T>& operator=(const Vector2<FromT>& rhs)
233 {
234 x = static_cast<T>(rhs.x);
235 y = static_cast<T>(rhs.y);
236 return *this;
237 }
238
243 Vector2<T>& operator=(const Vector2<T>& rhs)
244 {
245 x = rhs.x;
246 y = rhs.y;
247 return *this;
248 }
249
256 T& operator[](int n)
257 {
258 assert(n >= 0 && n <= 1);
259 if (0 == n)
260 return x;
261 else
262 return y;
263 }
264
271 const T& operator[](int n) const
272 {
273 assert(n >= 0 && n <= 1);
274 if (0 == n)
275 return x;
276 else
277 return y;
278 }
279
280 //---------------[ vector aritmetic operator ]--------------
285 Vector2<T> operator+(const Vector2<T>& rhs) const
286 {
287 return Vector2<T>(x + rhs.x, y + rhs.y);
288 }
289
294 Vector2<T> operator-(const Vector2<T>& rhs) const
295 {
296 return Vector2<T>(x - rhs.x, y - rhs.y);
297 }
298
303 Vector2<T> operator*(const Vector2<T>& rhs) const
304 {
305 return Vector2<T>(x * rhs.x, y * rhs.y);
306 }
307
312 Vector2<T> operator/(const Vector2<T>& rhs) const
313 {
314 return Vector2<T>(x / rhs.x, y / rhs.y);
315 }
316
321 Vector2<T>& operator+=(const Vector2<T>& rhs)
322 {
323 x += rhs.x;
324 y += rhs.y;
325 return *this;
326 }
327
332 Vector2<T>& operator-=(const Vector2<T>& rhs)
333 {
334 x -= rhs.x;
335 y -= rhs.y;
336 return *this;
337 }
338
343 Vector2<T>& operator*=(const Vector2<T>& rhs)
344 {
345 x *= rhs.x;
346 y *= rhs.y;
347 return *this;
348 }
349
354 Vector2<T>& operator/=(const Vector2<T>& rhs)
355 {
356 x /= rhs.x;
357 y /= rhs.y;
358 return *this;
359 }
360
361 //--------------[ scalar vector operator ]--------------------
366 Vector2<T> operator+(T rhs) const
367 {
368 return Vector2<T>(x + rhs, y + rhs);
369 }
370
375 Vector2<T> operator-(T rhs) const
376 {
377 return Vector2<T>(x - rhs, y - rhs);
378 }
379
384 Vector2<T> operator*(T rhs) const
385 {
386 return Vector2<T>(x * rhs, y * rhs);
387 }
388
393 Vector2<T> operator/(T rhs) const
394 {
395 return Vector2<T>(x / rhs, y / rhs);
396 }
397
402 Vector2<T>& operator+=(T rhs)
403 {
404 x += rhs;
405 y += rhs;
406 return *this;
407 }
408
413 Vector2<T>& operator-=(T rhs)
414 {
415 x -= rhs;
416 y -= rhs;
417 return *this;
418 }
419
424 Vector2<T>& operator*=(T rhs)
425 {
426 x *= rhs;
427 y *= rhs;
428 return *this;
429 }
430
435 Vector2<T>& operator/=(T rhs)
436 {
437 x /= rhs;
438 y /= rhs;
439 return *this;
440 }
441
442 //--------------[ equality operator ]------------------------
450 bool operator==(const Vector2<T>& rhs) const
451 {
452 return (std::abs(x - rhs.x) < EPSILON) && (std::abs(y - rhs.y) < EPSILON);
453 }
454
460 bool operator!=(const Vector2<T>& rhs) const
461 {
462 return !(*this == rhs);
463 }
464
465 //-------------[ unary operations ]--------------------------
470 Vector2<T> operator-() const
471 {
472 return Vector2<T>(-x, -y);
473 }
474
475 //-------------[ size operations ]---------------------------
480 T length() const
481 {
482 return (T) std::sqrt(x * x + y * y);
483 }
484
488 void normalize()
489 {
490 T s = length();
491 x /= s;
492 y /= s;
493 }
494
502 T lengthSq() const
503 {
504 return x * x + y * y;
505 }
506
507 //--------------[ misc. operations ]-----------------------
517 Vector2<T> lerp(T fact, const Vector2<T>& r) const
518 {
519 return (*this) + (r - (*this)) * fact;
520 }
521
522 //-------------[ conversion ]-----------------------------
528 operator T*()
529 {
530 return (T*) this;
531 }
537 operator const T*() const
538 {
539 return (const T*) this;
540 }
541
542 //-------------[ output operator ]------------------------
549 friend std::ostream& operator<<(std::ostream& lhs, const Vector2<T>& rhs)
550 {
551 lhs << "[" << rhs.x << "," << rhs.y << "]";
552 return lhs;
553 }
554
558 std::string toString() const
559 {
560 std::ostringstream oss;
561 oss << *this;
562 return oss.str();
563 }
564 };
565
566 //--------------------------------------
567 // Typedef shortcuts for 2D vector
568 //-------------------------------------
570 typedef class Vector2<float> Vector2f;
572 typedef class Vector2<double> Vector2d;
574 typedef class Vector2<int> Vector2i;
575
587 template<class T>
588 class Vector3
589 {
590 public:
591 //T x, y, z;
592 union
593 {
597 T x;
598
603 T s;
604
609 T r;
610 };
611
612 union
613 {
617 T y;
622 T t;
627 T g;
628 };
629
630 union
631 {
635 T z;
636
641 T u;
646 T b;
647 };
648
649 //----------------[ constructors ]--------------------------
653 Vector3()
654 : x(0), y(0), z(0)
655 {
656 }
657
664 Vector3(T nx, T ny, T nz)
665 : x(nx), y(ny), z(nz)
666 {
667 }
668
673 Vector3(const Vector3<T>& src)
674 : x(src.x), y(src.y), z(src.z)
675 {
676 }
677
682 template<class FromT>
683 Vector3(const Vector3<FromT>& src)
684 : x(static_cast<T>(src.x)), y(static_cast<T>(src.y)), z(static_cast<T>(src.z))
685 {
686 }
687
688 //----------------[ access operators ]-------------------
693 Vector3<T> operator=(const Vector3<T>& rhs)
694 {
695 x = rhs.x;
696 y = rhs.y;
697 z = rhs.z;
698 return *this;
699 }
700
705 template<class FromT>
706 Vector3<T> operator=(const Vector3<FromT>& rhs)
707 {
708 x = static_cast<T>(rhs.x);
709 y = static_cast<T>(rhs.y);
710 z = static_cast<T>(rhs.z);
711 return *this;
712 }
713
721 T & operator[](int n)
722 {
723 assert(n >= 0 && n <= 2);
724 if (0 == n)
725 return x;
726 else if (1 == n)
727 return y;
728 else
729 return z;
730 }
731
739 const T & operator[](int n) const
740 {
741 assert(n >= 0 && n <= 2);
742 if (0 == n)
743 return x;
744 else if (1 == n)
745 return y;
746 else
747 return z;
748 }
749
750 //---------------[ vector arithmetic operator ]--------------
755 Vector3<T> operator+(const Vector3<T>& rhs) const
756 {
757 return Vector3<T>(x + rhs.x, y + rhs.y, z + rhs.z);
758 }
759
764 Vector3<T> operator-(const Vector3<T>& rhs) const
765 {
766 return Vector3<T>(x - rhs.x, y - rhs.y, z - rhs.z);
767 }
768
773 Vector3<T> operator*(const Vector3<T>& rhs) const
774 {
775 return Vector3<T>(x * rhs.x, y * rhs.y, z * rhs.z);
776 }
777
782 Vector3<T> operator/(const Vector3<T>& rhs) const
783 {
784 return Vector3<T>(x / rhs.x, y / rhs.y, z / rhs.z);
785 }
786
791 Vector3<T>& operator+=(const Vector3<T>& rhs)
792 {
793 x += rhs.x;
794 y += rhs.y;
795 z += rhs.z;
796 return *this;
797 }
798
803 Vector3<T>& operator-=(const Vector3<T>& rhs)
804 {
805 x -= rhs.x;
806 y -= rhs.y;
807 z -= rhs.z;
808 return *this;
809 }
810
815 Vector3<T>& operator*=(const Vector3<T>& rhs)
816 {
817 x *= rhs.x;
818 y *= rhs.y;
819 z *= rhs.z;
820 return *this;
821 }
822
827 Vector3<T>& operator/=(const Vector3<T>& rhs)
828 {
829 x /= rhs.x;
830 y /= rhs.y;
831 z /= rhs.z;
832 return *this;
833 }
834
839 T dotProduct(const Vector3<T>& rhs) const
840 {
841 return x * rhs.x + y * rhs.y + z * rhs.z;
842 }
843
848 Vector3<T> crossProduct(const Vector3<T>& rhs) const
849 {
850 return Vector3<T>(y * rhs.z - rhs.y * z, z * rhs.x - rhs.z * x, x * rhs.y - rhs.x * y);
851 }
852
853 //--------------[ scalar vector operator ]--------------------
858 Vector3<T> operator+(T rhs) const
859 {
860 return Vector3<T>(x + rhs, y + rhs, z + rhs);
861 }
862
867 Vector3<T> operator-(T rhs) const
868 {
869 return Vector3<T>(x - rhs, y - rhs, z - rhs);
870 }
871
876 Vector3<T> operator*(T rhs) const
877 {
878 return Vector3<T>(x * rhs, y * rhs, z * rhs);
879 }
880
885 Vector3<T> operator/(T rhs) const
886 {
887 return Vector3<T>(x / rhs, y / rhs, z / rhs);
888 }
889
894 Vector3<T>& operator+=(T rhs)
895 {
896 x += rhs;
897 y += rhs;
898 z += rhs;
899 return *this;
900 }
901
906 Vector3<T>& operator-=(T rhs)
907 {
908 x -= rhs;
909 y -= rhs;
910 z -= rhs;
911 return *this;
912 }
913
918 Vector3<T>& operator*=(T rhs)
919 {
920 x *= rhs;
921 y *= rhs;
922 z *= rhs;
923 return *this;
924 }
925
930 Vector3<T>& operator/=(T rhs)
931 {
932 x /= rhs;
933 y /= rhs;
934 z /= rhs;
935 return *this;
936 }
937
938 //--------------[ Equality operator ]------------------------
946 bool operator==(const Vector3<T>& rhs) const
947 {
948 return std::fabs(x - rhs.x) < EPSILON && std::fabs(y - rhs.y) < EPSILON && std::fabs(z - rhs.z) < EPSILON;
949 }
950
956 bool operator!=(const Vector3<T>& rhs) const
957 {
958 return !(*this == rhs);
959 }
960
961 //-------------[ unary operations ]--------------------------
966 Vector3<T> operator-() const
967 {
968 return Vector3<T>(-x, -y, -z);
969 }
970
971 //-------------[ size operations ]---------------------------
976 T length() const
977 {
978 return (T) std::sqrt(x * x + y * y + z * z);
979 }
980
988 T lengthSq() const
989 {
990 return x * x + y * y + z * z;
991 }
992
996 void normalize()
997 {
998 T s = length();
999 x /= s;
1000 y /= s;
1001 z /= s;
1002 }
1003
1004 //------------[ other operations ]---------------------------
1011 void rotate(T ax, T ay, T az)
1012 {
1013 T a = cos(DEG2RAD(ax));
1014 T b = sin(DEG2RAD(ax));
1015 T c = cos(DEG2RAD(ay));
1016 T d = sin(DEG2RAD(ay));
1017 T e = cos(DEG2RAD(az));
1018 T f = sin(DEG2RAD(az));
1019 T nx = c * e * x - c * f * y + d * z;
1020 T ny = (a * f + b * d * e) * x + (a * e - b * d * f) * y - b * c * z;
1021 T nz = (b * f - a * d * e) * x + (a * d * f + b * e) * y + a * c * z;
1022 x = nx;
1023 y = ny;
1024 z = nz;
1025
1026 }
1027
1037 Vector3<T> lerp(T fact, const Vector3<T>& r) const
1038 {
1039 return (*this) + (r - (*this)) * fact;
1040 }
1041
1042 //-------------[ conversion ]-----------------------------
1043
1049 operator T*()
1050 {
1051 return (T*) this;
1052 }
1053
1059 operator const T*() const
1060 {
1061 return (const T*) this;
1062 }
1063
1064 //-------------[ output operator ]------------------------
1071 friend std::ostream& operator<<(std::ostream& lhs, const Vector3<T> rhs)
1072 {
1073 lhs << "[" << rhs.x << "," << rhs.y << "," << rhs.z << "]";
1074 return lhs;
1075 }
1076
1080 std::string toString() const
1081 {
1082 std::ostringstream oss;
1083 oss << *this;
1084 return oss.str();
1085 }
1086 };
1087
1089 typedef Vector3<float> Vector3f;
1091 typedef Vector3<double> Vector3d;
1093 typedef Vector3<int> Vector3i;
1094
1106 template<class T>
1107 class Vector4
1108 {
1109 public:
1110
1111 union
1112 {
1117 T r;
1121 T x;
1122 };
1123
1124 union
1125 {
1130 T g;
1134 T y;
1135 };
1136
1137 union
1138 {
1143 T b;
1147 T z;
1148 };
1149
1150 union
1151 {
1156 T a;
1162 T w;
1163 };
1164
1165 //----------------[ constructors ]--------------------------
1169 Vector4()
1170 : x(0), y(0), z(0), w(0)
1171 {
1172 }
1173
1181 Vector4(T nx, T ny, T nz, T nw)
1182 : x(nx), y(ny), z(nz), w(nw)
1183 {
1184 }
1185
1190 Vector4(const Vector4<T>& src)
1191 : x(src.x), y(src.y), z(src.z), w(src.w)
1192 {
1193 }
1194
1199 template<class FromT>
1200 Vector4(const Vector4<FromT>& src)
1201 : x(static_cast<T>(src.x)), y(static_cast<T>(src.y)), z(static_cast<T>(src.z)), w(static_cast<T>(src.w))
1202 {
1203 }
1204
1205 Vector4(const Vector3<T>& src, T w)
1206 : x(src.x), y(src.y), z(src.z), w(w)
1207 {}
1208
1209 template <typename FromT>
1210 Vector4(const Vector3<FromT>& src, FromT w)
1211 : x(static_cast<T>(src.x)), y(static_cast<T>(src.y)), z(static_cast<T>(src.z)), w(static_cast<T>(w))
1212 {}
1213
1214 //----------------[ access operators ]-------------------
1219 Vector4<T> operator=(const Vector4<T>& rhs)
1220 {
1221 x = rhs.x;
1222 y = rhs.y;
1223 z = rhs.z;
1224 w = rhs.w;
1225 return *this;
1226 }
1227
1232 template<class FromT>
1233 Vector4<T> operator=(const Vector4<FromT>& rhs)
1234 {
1235 x = static_cast<T>(rhs.x);
1236 y = static_cast<T>(rhs.y);
1237 z = static_cast<T>(rhs.z);
1238 w = static_cast<T>(rhs.w);
1239 return *this;
1240 }
1241
1249 T & operator[](int n)
1250 {
1251 assert(n >= 0 && n <= 3);
1252 if (0 == n)
1253 return x;
1254 else if (1 == n)
1255 return y;
1256 else if (2 == n)
1257 return z;
1258 else
1259 return w;
1260 }
1261
1269 const T & operator[](int n) const
1270 {
1271 assert(n >= 0 && n <= 3);
1272 if (0 == n)
1273 return x;
1274 else if (1 == n)
1275 return y;
1276 else if (2 == n)
1277 return z;
1278 else
1279 return w;
1280 }
1281
1282 //---------------[ vector aritmetic operator ]--------------
1287 Vector4<T> operator+(const Vector4<T>& rhs) const
1288 {
1289 return Vector4<T>(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
1290 }
1291
1296 Vector4<T> operator-(const Vector4<T>& rhs) const
1297 {
1298 return Vector4<T>(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
1299 }
1300
1305 Vector4<T> operator*(const Vector4<T> rhs) const
1306 {
1307 return Vector4<T>(x * rhs.x, y * rhs.y, z * rhs.z, w * rhs.w);
1308 }
1309
1314 Vector4<T> operator/(const Vector4<T>& rhs) const
1315 {
1316 return Vector4<T>(x / rhs.x, y / rhs.y, z / rhs.z, w / rhs.w);
1317 }
1318
1323 Vector4<T>& operator+=(const Vector4<T>& rhs)
1324 {
1325 x += rhs.x;
1326 y += rhs.y;
1327 z += rhs.z;
1328 w += rhs.w;
1329 return *this;
1330 }
1331
1336 Vector4<T>& operator-=(const Vector4<T>& rhs)
1337 {
1338 x -= rhs.x;
1339 y -= rhs.y;
1340 z -= rhs.z;
1341 w -= rhs.w;
1342 return *this;
1343 }
1344
1349 Vector4<T>& operator*=(const Vector4<T>& rhs)
1350 {
1351 x *= rhs.x;
1352 y *= rhs.y;
1353 z *= rhs.z;
1354 w *= rhs.w;
1355 return *this;
1356 }
1357
1362 Vector4<T>& operator/=(const Vector4<T>& rhs)
1363 {
1364 x /= rhs.x;
1365 y /= rhs.y;
1366 z /= rhs.z;
1367 w /= rhs.w;
1368 return *this;
1369 }
1370
1371 //--------------[ equiality operator ]------------------------
1379 bool operator==(const Vector4<T>& rhs) const
1380 {
1381 return std::fabs(x - rhs.x) < EPSILON && std::fabs(y - rhs.y) < EPSILON && std::fabs(z - rhs.z) < EPSILON
1382 && std::fabs(w - rhs.w) < EPSILON;
1383 }
1384
1390 bool operator!=(const Vector4<T>& rhs) const
1391 {
1392 return !(*this == rhs);
1393 }
1394
1395 //-------------[ unary operations ]--------------------------
1400 Vector4<T> operator-() const
1401 {
1402 return Vector4<T>(-x, -y, -z, -w);
1403 }
1404
1405 //--------------[ scalar vector operator ]--------------------
1406
1411 Vector4<T> operator+(T rhs) const
1412 {
1413 return Vector4<T>(x + rhs, y + rhs, z + rhs, w + rhs);
1414 }
1415
1420 Vector4<T> operator-(T rhs) const
1421 {
1422 return Vector4<T>(x - rhs, y - rhs, z - rhs, w - rhs);
1423 }
1424
1429 Vector4<T> operator*(T rhs) const
1430 {
1431 return Vector4<T>(x * rhs, y * rhs, z * rhs, w * rhs);
1432 }
1433
1438 Vector4<T> operator/(T rhs) const
1439 {
1440 return Vector4<T>(x / rhs, y / rhs, z / rhs, w / rhs);
1441 }
1442
1447 Vector4<T>& operator+=(T rhs)
1448 {
1449 x += rhs;
1450 y += rhs;
1451 z += rhs;
1452 w += rhs;
1453 return *this;
1454 }
1455
1460 Vector4<T>& operator-=(T rhs)
1461 {
1462 x -= rhs;
1463 y -= rhs;
1464 z -= rhs;
1465 w -= rhs;
1466 return *this;
1467 }
1468
1473 Vector4<T>& operator*=(T rhs)
1474 {
1475 x *= rhs;
1476 y *= rhs;
1477 z *= rhs;
1478 w *= rhs;
1479 return *this;
1480 }
1481
1486 Vector4<T>& operator/=(T rhs)
1487 {
1488 x /= rhs;
1489 y /= rhs;
1490 z /= rhs;
1491 w /= rhs;
1492 return *this;
1493 }
1494
1495 //-------------[ size operations ]---------------------------
1500 T length() const
1501 {
1502 return (T) std::sqrt(x * x + y * y + z * z + w * w);
1503 }
1504
1508 void normalize()
1509 {
1510 T s = length();
1511 x /= s;
1512 y /= s;
1513 z /= s;
1514 w /= s;
1515 }
1516
1524 T lengthSq() const
1525 {
1526 return x * x + y * y + z * z + w * w;
1527 }
1528
1529 //--------------[ misc. operations ]-----------------------
1539 Vector4<T> lerp(T fact, const Vector4<T>& r) const
1540 {
1541 return (*this) + (r - (*this)) * fact;
1542 }
1543
1544 //-------------[ conversion ]-----------------------------
1545
1551 operator T*()
1552 {
1553 return (T*) this;
1554 }
1555
1561 operator const T*() const
1562 {
1563 return (const T*) this;
1564 }
1565
1571 Vector3<T> xyz() const
1572 {
1573 if (w == 0 || w == 1)
1574 return Vector3<T>(x,y,z);
1575
1576 const T invW = 1.0 / w;
1577 return Vector3<T>(x * invW, y * invW, z * invW);
1578 }
1579
1580 //-------------[ output operator ]------------------------
1587 friend std::ostream& operator<<(std::ostream& lhs, const Vector4<T>& rhs)
1588 {
1589 lhs << "[" << rhs.x << "," << rhs.y << "," << rhs.z << "," << rhs.w << "]";
1590 return lhs;
1591 }
1592
1596 std::string toString() const
1597 {
1598 std::ostringstream oss;
1599 oss << *this;
1600 return oss.str();
1601 }
1602
1603 };
1604
1606 typedef Vector4<float> Vector4f;
1608 typedef Vector4<double> Vector4d;
1610 typedef Vector4<int> Vector4i;
1611
1618 template<class T>
1619 class Matrix3
1620 {
1621 public:
1623 T data[9];
1624
1625 //--------------------------[ constructors ]-------------------------------
1629 Matrix3()
1630 {
1631 for (int i = 0; i < 9; i++)
1632 data[i] = (i % 4) ? 0 : 1;
1633 }
1634
1639 Matrix3(const T * dt)
1640 {
1641 std::memcpy(data, dt, sizeof(T) * 9);
1642 }
1643
1648 Matrix3(const Matrix3<T>& src)
1649 {
1650 std::memcpy(data, src.data, sizeof(T) * 9);
1651 }
1652
1657 template<class FromT>
1658 Matrix3(const Matrix3<FromT>& src)
1659 {
1660 for (int i = 0; i < 9; i++)
1661 {
1662 data[i] = static_cast<T>(src.data[i]);
1663 }
1664 }
1665
1669 void identity()
1670 {
1671 for (int i = 0; i < 9; i++)
1672 data[i] = (i % 4) ? 0 : 1;
1673 }
1674
1681 static Matrix3<T> createRotationAroundAxis(T xDeg, T yDeg, T zDeg)
1682 {
1683 T xRads(DEG2RAD(xDeg));
1684 T yRads(DEG2RAD(yDeg));
1685 T zRads(DEG2RAD(zDeg));
1686
1687 Matrix3<T> ma, mb, mc;
1688 float ac = cos(xRads);
1689 float as = sin(xRads);
1690 float bc = cos(yRads);
1691 float bs = sin(yRads);
1692 float cc = cos(zRads);
1693 float cs = sin(zRads);
1694
1695 ma.at(1, 1) = ac;
1696 ma.at(2, 1) = as;
1697 ma.at(1, 2) = -as;
1698 ma.at(2, 2) = ac;
1699
1700 mb.at(0, 0) = bc;
1701 mb.at(2, 0) = -bs;
1702 mb.at(0, 2) = bs;
1703 mb.at(2, 2) = bc;
1704
1705 mc.at(0, 0) = cc;
1706 mc.at(1, 0) = cs;
1707 mc.at(0, 1) = -cs;
1708 mc.at(1, 1) = cc;
1709
1710 Matrix3<T> ret = ma * mb * mc;
1711 return ret;
1712 }
1713
1717 template<class It>
1718 static Matrix3<T> fromOde(const It* mat)
1719 {
1720 Matrix3<T> ret;
1721 for (int i = 0; i < 3; i++)
1722 {
1723 for (int j = 0; j < 3; j++)
1724 {
1725 ret.at(i, j) = static_cast<T>(mat[j * 4 + i]);
1726 }
1727 }
1728 return ret;
1729 }
1730
1737 template<class FromT>
1738 static Matrix3<T> fromRowMajorArray(const FromT* arr)
1739 {
1740 const T retData[] =
1741 { static_cast<T>(arr[0]), static_cast<T>(arr[3]), static_cast<T>(arr[6]), static_cast<T>(arr[1]),
1742 static_cast<T>(arr[4]), static_cast<T>(arr[7]), static_cast<T>(arr[2]), static_cast<T>(arr[5]),
1743 static_cast<T>(arr[8]) };
1744
1745 return retData;
1746 }
1747
1754 template<class FromT>
1755 static Matrix3<T> fromColumnMajorArray(const FromT* arr)
1756 {
1757 const T retData[] =
1758 { static_cast<T>(arr[0]), static_cast<T>(arr[1]), static_cast<T>(arr[2]), static_cast<T>(arr[3]),
1759 static_cast<T>(arr[4]), static_cast<T>(arr[5]), static_cast<T>(arr[6]), static_cast<T>(arr[7]),
1760 static_cast<T>(arr[8]) };
1761
1762 return retData;
1763 }
1764
1765 //---------------------[ equiality operators ]------------------------------
1774 bool operator==(const Matrix3<T>& rhs) const
1775 {
1776 for (int i = 0; i < 9; i++)
1777 {
1778 if (std::fabs(data[i] - rhs.data[i]) >= EPSILON)
1779 return false;
1780 }
1781 return true;
1782 }
1783
1789 bool operator!=(const Matrix3<T>& rhs) const
1790 {
1791 return !(*this == rhs);
1792 }
1793
1794 //---------------------[ access operators ]---------------------------------
1800 T& at(int x, int y)
1801 {
1802 assert(x >= 0 && x < 3);
1803 assert(y >= 0 && y < 3);
1804 return data[x * 3 + y];
1805 }
1806
1812 const T& at(int x, int y) const
1813 {
1814 assert(x >= 0 && x < 3);
1815 assert(y >= 0 && y < 3);
1816 return data[x * 3 + y];
1817 }
1818
1824 T& operator()(int i, int j)
1825 {
1826 assert(i >= 1 && i <= 3);
1827 assert(j >= 1 && j <= 3);
1828 return data[(j - 1) * 3 + i - 1];
1829 }
1830
1836 const T& operator()(int i, int j) const
1837 {
1838 assert(i >= 1 && i <= 3);
1839 assert(j >= 1 && j <= 3);
1840 return data[(j - 1) * 3 + i - 1];
1841 }
1842
1847 Matrix3<T>& operator=(const Matrix3<T>& rhs)
1848 {
1849 std::memcpy(data, rhs.data, sizeof(T) * 9);
1850 return *this;
1851 }
1852
1857 template<class FromT>
1858 Matrix3<T>& operator=(const Matrix3<FromT>& rhs)
1859 {
1860 for (int i = 0; i < 9; i++)
1861 {
1862 data[i] = static_cast<T>(rhs.data[i]);
1863 }
1864 return *this;
1865 }
1866
1871 Matrix3<T>& operator=(const T* rhs)
1872 {
1873 std::memcpy(data, rhs, sizeof(T) * 9);
1874 return *this;
1875 }
1876
1877 /*Matrix3<T> & operator=(const double* m)
1878 {
1879 for (int i = 0; i < 9; i++) data[i] = (T)m[i];
1880 return * this;
1881 }*/
1882
1883 //--------------------[ matrix with matrix operations ]---------------------
1888 Matrix3<T> operator+(const Matrix3<T>& rhs) const
1889 {
1890 Matrix3<T> ret;
1891 for (int i = 0; i < 9; i++)
1892 ret.data[i] = data[i] + rhs.data[i];
1893 return ret;
1894 }
1895
1900 Matrix3<T> operator-(const Matrix3<T>& rhs) const
1901 {
1902 Matrix3<T> ret;
1903 for (int i = 0; i < 9; i++)
1904 ret.data[i] = data[i] - rhs.data[i];
1905 return ret;
1906 }
1907
1908 //--------------------[ matrix with scalar operations ]---------------------
1913 Matrix3<T> operator+(T rhs) const
1914 {
1915 Matrix3<T> ret;
1916 for (int i = 0; i < 9; i++)
1917 ret.data[i] = data[i] + rhs;
1918 return ret;
1919 }
1920
1925 Matrix3<T> operator-(T rhs) const
1926 {
1927 Matrix3<T> ret;
1928 for (int i = 0; i < 9; i++)
1929 ret.data[i] = data[i] - rhs;
1930 return ret;
1931 }
1932
1937 Matrix3<T> operator*(T rhs) const
1938 {
1939 Matrix3<T> ret;
1940 for (int i = 0; i < 9; i++)
1941 ret.data[i] = data[i] * rhs;
1942 return ret;
1943 }
1944
1949 Matrix3<T> operator/(T rhs) const
1950 {
1951 Matrix3<T> ret;
1952 for (int i = 0; i < 9; i++)
1953 ret.data[i] = data[i] / rhs;
1954 return ret;
1955 }
1956
1957 //--------------------[ multiply operators ]--------------------------------
1962 Vector3<T> operator*(const Vector3<T>& rhs) const
1963 {
1964 return Vector3<T>(data[0] * rhs.x + data[3] * rhs.y + data[6] * rhs.z,
1965 data[1] * rhs.x + data[4] * rhs.y + data[7] * rhs.z,
1966 data[2] * rhs.x + data[5] * rhs.y + data[8] * rhs.z);
1967 }
1968
1973 Matrix3<T> operator*(Matrix3<T> rhs) const
1974 {
1975 Matrix3<T> w;
1976 for (int i = 0; i < 3; i++)
1977 {
1978 for (int j = 0; j < 3; j++)
1979 {
1980 T n = 0;
1981 for (int k = 0; k < 3; k++)
1982 n += rhs.at(i, k) * at(k, j);
1983 w.at(i, j) = n;
1984 }
1985 }
1986 return w;
1987
1988 }
1989
1990 //---------------------------[ misc operations ]----------------------------
1994 Matrix3<T> transpose()
1995 {
1996 Matrix3<T> ret;
1997 for (int i = 0; i < 3; i++)
1998 {
1999 for (int j = 0; j < 3; j++)
2000 {
2001 ret.at(i, j) = at(j, i);
2002 }
2003 }
2004 return ret;
2005 }
2006
2016 Matrix3<T> lerp(T fact, const Matrix3<T>& rhs) const
2017 {
2018 Matrix3<T> ret = (*this) + (rhs - (*this)) * fact;
2019 return ret;
2020 }
2021
2022 T det()
2023 {
2024 return +at(0, 0) * at(1, 1) * at(2, 2) + at(0, 1) * at(1, 2) * at(2, 0) + at(0, 2) * at(1, 0) * at(2, 1)
2025 - at(0, 0) * at(1, 2) * at(2, 1) - at(0, 1) * at(1, 0) * at(2, 2) - at(0, 2) * at(1, 1) * at(2, 0);
2026 }
2027
2032 Matrix3<T> inverse()
2033 {
2034 Matrix3<T> ret;
2035 ret.at(0, 0) = at(1, 1) * at(2, 2) - at(2, 1) * at(1, 2);
2036 ret.at(0, 1) = at(2, 1) * at(0, 2) - at(0, 1) * at(2, 2);
2037 ret.at(0, 2) = at(0, 1) * at(1, 2) - at(1, 1) * at(0, 2);
2038 ret.at(1, 0) = at(2, 0) * at(1, 2) - at(1, 0) * at(2, 2);
2039 ret.at(1, 1) = at(0, 0) * at(2, 2) - at(2, 0) * at(0, 2);
2040 ret.at(1, 2) = at(1, 0) * at(0, 2) - at(0, 0) * at(1, 2);
2041 ret.at(2, 0) = at(1, 0) * at(2, 1) - at(2, 0) * at(1, 1);
2042 ret.at(2, 1) = at(2, 0) * at(0, 1) - at(0, 0) * at(2, 1);
2043 ret.at(2, 2) = at(0, 0) * at(1, 1) - at(1, 0) * at(0, 1);
2044 return ret * (1.0f / det());
2045 }
2046
2047 //-------------[ conversion ]-----------------------------
2048
2054 operator T*()
2055 {
2056 return (T*) data;
2057 }
2058
2064 operator const T*() const
2065 {
2066 return (const T*) data;
2067 }
2068
2069 //----------[ output operator ]----------------------------
2076 friend std::ostream& operator <<(std::ostream& lhs, const Matrix3<T>& rhs)
2077 {
2078 for (int i = 0; i < 3; i++)
2079 {
2080 lhs << "|\t";
2081 for (int j = 0; j < 3; j++)
2082 {
2083 lhs << rhs.at(j, i) << "\t";
2084 }
2085 lhs << "|" << std::endl;
2086 }
2087 return lhs;
2088 }
2089
2093 std::string toString() const
2094 {
2095 std::ostringstream oss;
2096 oss << *this;
2097 return oss.str();
2098 }
2099 };
2100
2102 typedef Matrix3<float> Matrix3f;
2104 typedef Matrix3<double> Matrix3d;
2106 typedef Matrix3<int> Matrix3i;
2107
2114 template<class T>
2115 class Matrix4
2116 {
2117 public:
2119 T data[16];
2120
2121 //--------------------------[ constructors ]-------------------------------
2125 Matrix4()
2126 {
2127 for (int i = 0; i < 16; i++)
2128 data[i] = (i % 5) ? 0 : 1;
2129 }
2130
2135 Matrix4(const T * dt)
2136 {
2137 std::memcpy(data, dt, sizeof(T) * 16);
2138 }
2139
2144 Matrix4(const Matrix4<T>& src)
2145 {
2146 std::memcpy(data, src.data, sizeof(T) * 16);
2147 }
2148
2153 template<class FromT>
2154 Matrix4(const Matrix4<FromT>& src)
2155 {
2156 for (int i = 0; i < 16; i++)
2157 {
2158 data[i] = static_cast<T>(src.data[i]);
2159 }
2160 }
2161
2165 void identity()
2166 {
2167 for (int i = 0; i < 16; i++)
2168 data[i] = (i % 5) ? 0 : 1;
2169 }
2170
2177 static Matrix4<T> createRotationAroundAxis(T xDeg, T yDeg, T zDeg)
2178 {
2179 T xRads(DEG2RAD(xDeg));
2180 T yRads(DEG2RAD(yDeg));
2181 T zRads(DEG2RAD(zDeg));
2182
2183 Matrix4<T> ma, mb, mc;
2184 float ac = cos(xRads);
2185 float as = sin(xRads);
2186 float bc = cos(yRads);
2187 float bs = sin(yRads);
2188 float cc = cos(zRads);
2189 float cs = sin(zRads);
2190
2191 ma.at(1, 1) = ac;
2192 ma.at(2, 1) = as;
2193 ma.at(1, 2) = -as;
2194 ma.at(2, 2) = ac;
2195
2196 mb.at(0, 0) = bc;
2197 mb.at(2, 0) = -bs;
2198 mb.at(0, 2) = bs;
2199 mb.at(2, 2) = bc;
2200
2201 mc.at(0, 0) = cc;
2202 mc.at(1, 0) = cs;
2203 mc.at(0, 1) = -cs;
2204 mc.at(1, 1) = cc;
2205
2206 /*std::cout << "RotVec = " << a << "," << b << "," << c << std::endl;
2207 std::cout << "Rx = " << std::endl << ma;
2208 std::cout << "Ry = " << std::endl << mb;
2209 std::cout << "Rz = " << std::endl << mc;*/
2210
2211 Matrix4<T> ret = ma * mb * mc;
2212 //std::cout << "Result = " << std::endl << ma * (mb * mc);
2213
2214 return ret;
2215 }
2216
2218
2225 static Matrix4<T> createTranslation(T x, T y, T z, T w = 1)
2226 {
2227 Matrix4 ret;
2228 ret.at(3, 0) = x;
2229 ret.at(3, 1) = y;
2230 ret.at(3, 2) = z;
2231 ret.at(3, 3) = w;
2232
2233 return ret;
2234 }
2235
2244 static Matrix4<T> createScale(T sx, T sy, T sz)
2245 {
2246 Matrix4<T> ret;
2247 ret.at(0,0) = sx;
2248 ret.at(1,1) = sy;
2249 ret.at(2,2) = sz;
2250
2251 return ret;
2252 }
2253
2261 static Matrix4<T> createLookAt(const Vector3<T>& eyePos, const Vector3<T>& centerPos, const Vector3<T>& upDir)
2262 {
2263 Vector3<T> forward, side, up;
2264 Matrix4<T> m;
2265
2266 forward = centerPos - eyePos;
2267 up = upDir;
2268
2269 forward.normalize();
2270
2271 // Side = forward x up
2272 side = forward.crossProduct(up);
2273 side.normalize();
2274
2275 // Recompute up as: up = side x forward
2276 up = side.crossProduct(forward);
2277
2278 m.at(0, 0) = side.x;
2279 m.at(1, 0) = side.y;
2280 m.at(2, 0) = side.z;
2281
2282 m.at(0, 1) = up.x;
2283 m.at(1, 1) = up.y;
2284 m.at(2, 1) = up.z;
2285
2286 m.at(0, 2) = -forward.x;
2287 m.at(1, 2) = -forward.y;
2288 m.at(2, 2) = -forward.z;
2289
2290 m = m * Matrix4<T>::createTranslation(-eyePos.x, -eyePos.y, -eyePos.z);
2291 return m;
2292 }
2293
2294
2307 static Matrix4<T> createFrustum(T left, T right, T bottom, T top, T zNear, T zFar)
2308 {
2309 /*
2310 *
2311 2 zNear
2312 ------------ 0 A 0
2313 right - left
2314
2315 2 zNear
2316 0 ------------ B 0
2317 top - bottom
2318
2319 0 0 C D
2320
2321 0 0 -1 0
2322
2323 A = (right + left) / (right - left)
2324
2325 B = (top + bottom) / (top - bottom)
2326
2327 C = - (zFar + zNear) / (zFar - zNear)
2328
2329 D = - (2 zFar zNear) / (zFar - zNear)
2330 *
2331 */
2332 Matrix4<T> ret;
2333
2334 const T invWidth = 1.0 / (right - left);
2335 const T invHeight = 1.0 / (top - bottom);
2336 const T invDepth = 1.0 / (zFar - zNear);
2337
2338 const T twoZNear = 2 * zNear;
2339
2340 ret.at(0,0) = twoZNear * invWidth;
2341 ret.at(1,1) = twoZNear * invHeight;
2342
2343 ret.at(2,0) = (right + left) * invWidth;
2344 ret.at(2,1) = (top + bottom) * invHeight;
2345 ret.at(2,2) = - (zFar + zNear) * invDepth;
2346 ret.at(2,3) = -1;
2347
2348 ret.at(3,2) = - twoZNear * zFar * invDepth;
2349 ret.at(3,3) = 0;
2350
2351 return ret;
2352 }
2353
2366 static Matrix4<T> createOrtho(T left, T right, T bottom, T top, T zNear, T zFar)
2367 {
2368 /*
2369 2
2370 ------------ 0 0 tx
2371 right - left
2372
2373 2
2374 0 ------------ 0 ty
2375 top - bottom
2376
2377 -2
2378 0 0 ------------ tz
2379 zFar-zNear
2380
2381 0 0 0 1
2382
2383 where
2384
2385 tx = - (right + left) / (right - left)
2386
2387 ty = - (top + bottom) / (top - bottom)
2388
2389 tz = - (zFar + zNear) / (zFar - zNear)
2390
2391 */
2392
2393 const T invWidth = 1.0 / (right - left);
2394 const T invHeight = 1.0 / (top - bottom);
2395 const T invDepth = 1.0 / (zFar - zNear);
2396
2397 Matrix4<T> ret;
2398
2399 ret.at(0,0) = 2 * invWidth;
2400 ret.at(1,1) = 2 * invHeight;
2401 ret.at(2,2) = -2 * invDepth;
2402
2403 ret.at(3,0) = -(right + left) * invWidth;
2404 ret.at(3,1) = -(top + bottom) * invHeight;
2405 ret.at(3,2) = -(zFar + zNear) * invDepth;
2406
2407 return ret;
2408 }
2409
2416 template<class FromT>
2417 static Matrix4<T> fromRowMajorArray(const FromT* arr)
2418 {
2419 const T retData[] =
2420 { static_cast<T>(arr[0]), static_cast<T>(arr[4]), static_cast<T>(arr[8]), static_cast<T>(arr[12]),
2421 static_cast<T>(arr[1]), static_cast<T>(arr[5]), static_cast<T>(arr[9]), static_cast<T>(arr[13]),
2422 static_cast<T>(arr[2]), static_cast<T>(arr[6]), static_cast<T>(arr[10]), static_cast<T>(arr[14]),
2423 static_cast<T>(arr[3]), static_cast<T>(arr[7]), static_cast<T>(arr[11]), static_cast<T>(arr[15]) };
2424
2425 return retData;
2426 }
2427
2434 template<class FromT>
2435 static Matrix4<T> fromColumnMajorArray(const FromT* arr)
2436 {
2437 const T retData[] =
2438 { static_cast<T>(arr[0]), static_cast<T>(arr[1]), static_cast<T>(arr[2]), static_cast<T>(arr[3]),
2439 static_cast<T>(arr[4]), static_cast<T>(arr[5]), static_cast<T>(arr[6]), static_cast<T>(arr[7]),
2440 static_cast<T>(arr[8]), static_cast<T>(arr[9]), static_cast<T>(arr[10]), static_cast<T>(arr[11]),
2441 static_cast<T>(arr[12]), static_cast<T>(arr[13]), static_cast<T>(arr[14]), static_cast<T>(arr[15]) };
2442
2443 return retData;
2444 }
2445
2446 //---------------------[ Equality operators ]------------------------------
2455 bool operator==(const Matrix4<T>& rhs) const
2456 {
2457 for (int i = 0; i < 16; i++)
2458 {
2459 if (std::fabs(data[i] - rhs.data[i]) >= EPSILON
2460 )
2461 return false;
2462 }
2463 return true;
2464 }
2465
2471 bool operator!=(const Matrix4<T>& rhs) const
2472 {
2473 return !(*this == rhs);
2474 }
2475
2476 //---------------------[ access operators ]---------------------------------
2482 T& at(int x, int y)
2483 {
2484 assert(x >= 0 && x < 4);
2485 assert(y >= 0 && y < 4);
2486 return data[x * 4 + y];
2487 }
2488
2494 const T& at(int x, int y) const
2495 {
2496 assert(x >= 0 && x < 4);
2497 assert(y >= 0 && y < 4);
2498 return data[x * 4 + y];
2499 }
2500
2506 T& operator()(int i, int j)
2507 {
2508 assert(i >= 1 && i <= 4);
2509 assert(j >= 1 && j <= 4);
2510 return data[(j - 1) * 4 + i - 1];
2511 }
2512
2518 const T& operator()(int i, int j) const
2519 {
2520 assert(i >= 1 && i <= 4);
2521 assert(j >= 1 && j <= 4);
2522 return data[(j - 1) * 4 + i - 1];
2523 }
2524
2530 void setTranslation(const Vector3<T>& v)
2531 {
2532 at(3, 0) = v.x;
2533 at(3, 1) = v.y;
2534 at(3, 2) = v.z;
2535 at(3, 3) = 1;
2536 }
2537
2538 Vector3<T> getTranslation() const
2539 {
2540 return Vector3<T>(at(3, 0), at(3, 1), at(3, 2));
2541 }
2542
2548 void setRotation(const Matrix3<T>& m)
2549 {
2550 for (int i = 0; i < 3; i++)
2551 {
2552 for (int j = 0; j < 3; j++)
2553 {
2554 at(i, j) = m.at(i, j);
2555 }
2556 }
2557 }
2558
2563 Vector3<T> getScale() const
2564 { return Vector3<T>(at(0,0), at(1,1), at(2,2)); }
2565
2570 void setScale(T s)
2571 {
2572 at(0,0) = at(1,1) = at(2,2) = s;
2573 }
2574
2581 void setScale(T sx, T sy, T sz)
2582 {
2583 at(0,0) = sx;
2584 at(1,1) = sy;
2585 at(2,2) = sz;
2586 }
2587
2592 void setScale(const Vector3<T>& s)
2593 {
2594 at(0,0) = s.x;
2595 at(1,1) = s.y;
2596 at(2,2) = s.z;
2597 }
2598
2603 Matrix4<T>& operator=(const Matrix4<T>& rhs)
2604 {
2605 std::memcpy(data, rhs.data, sizeof(T) * 16);
2606 return *this;
2607 }
2608
2613 template<class FromT>
2614 Matrix4<T>& operator=(const Matrix4<FromT>& rhs)
2615 {
2616 for (int i = 0; i < 16; i++)
2617 {
2618 data[i] = static_cast<T>(rhs.data[i]);
2619 }
2620 return *this;
2621 }
2622
2627 Matrix4<T>& operator=(const T* rhs)
2628 {
2629 std::memcpy(data, rhs, sizeof(T) * 16);
2630 return *this;
2631 }
2632
2633 /*Matrix4<T> & operator=(const double* m)
2634 {
2635 for (int i = 0; i < 16; i++) data[i] = (T)m[i];
2636 return * this;
2637 }*/
2638
2639 //--------------------[ matrix with matrix operations ]---------------------
2644 Matrix4<T> operator+(const Matrix4<T>& rhs) const
2645 {
2646 Matrix4<T> ret;
2647 for (int i = 0; i < 16; i++)
2648 ret.data[i] = data[i] + rhs.data[i];
2649 return ret;
2650 }
2651
2656 Matrix4<T> operator-(const Matrix4<T>& rhs) const
2657 {
2658 Matrix4<T> ret;
2659 for (int i = 0; i < 16; i++)
2660 ret.data[i] = data[i] - rhs.data[i];
2661 return ret;
2662 }
2663
2664 //--------------------[ matrix with scalar operations ]---------------------
2669 Matrix4<T> operator+(T rhs) const
2670 {
2671 Matrix4<T> ret;
2672 for (int i = 0; i < 16; i++)
2673 ret.data[i] = data[i] + rhs;
2674 return ret;
2675 }
2676
2681 Matrix4<T> operator-(T rhs) const
2682 {
2683 Matrix4<T> ret;
2684 for (int i = 0; i < 16; i++)
2685 ret.data[i] = data[i] - rhs;
2686 return ret;
2687 }
2688
2693 Matrix4<T> operator*(T rhs) const
2694 {
2695 Matrix4<T> ret;
2696 for (int i = 0; i < 16; i++)
2697 ret.data[i] = data[i] * rhs;
2698 return ret;
2699 }
2700
2705 Matrix4<T> operator/(T rhs) const
2706 {
2707 Matrix4<T> ret;
2708 for (int i = 0; i < 16; i++)
2709 ret.data[i] = data[i] / rhs;
2710 return ret;
2711 }
2712
2713 //--------------------[ multiply operators ]--------------------------------
2718 Vector4<T> operator*(const Vector4<T>& rhs) const
2719 {
2720 return Vector4<T>(data[0] * rhs.x + data[4] * rhs.y + data[8] * rhs.z + data[12] * rhs.w,
2721 data[1] * rhs.x + data[5] * rhs.y + data[9] * rhs.z + data[13] * rhs.w,
2722 data[2] * rhs.x + data[6] * rhs.y + data[10] * rhs.z + data[14] * rhs.w,
2723 data[3] * rhs.x + data[7] * rhs.y + data[11] * rhs.z + data[15] * rhs.w);
2724
2725 }
2726
2731 Vector3<T> operator*(const Vector3<T>& rhs) const
2732 {
2733 return Vector3<T>(data[0] * rhs.x + data[4] * rhs.y + data[8] * rhs.z,
2734 data[1] * rhs.x + data[5] * rhs.y + data[9] * rhs.z,
2735 data[2] * rhs.x + data[6] * rhs.y + data[10] * rhs.z);
2736 }
2737
2742 Matrix4<T> operator*(Matrix4<T> rhs) const
2743 {
2744 Matrix4<T> w;
2745 for (int i = 0; i < 4; i++)
2746 {
2747 for (int j = 0; j < 4; j++)
2748 {
2749 T n = 0;
2750 for (int k = 0; k < 4; k++)
2751 n += rhs.at(i, k) * at(k, j);
2752 w.at(i, j) = n;
2753 }
2754 }
2755 return w;
2756
2757 }
2758
2759 //---------------------------[ misc operations ]----------------------------
2760
2766 T det()
2767 {
2768
2769 return +at(3, 0) * at(2, 1) * at(1, 2) * at(0, 3) - at(2, 0) * at(3, 1) * at(1, 2) * at(0, 3)
2770 - at(3, 0) * at(1, 1) * at(2, 2) * at(0, 3) + at(1, 0) * at(3, 1) * at(2, 2) * at(0, 3)
2771
2772 + at(2, 0) * at(1, 1) * at(3, 2) * at(0, 3) - at(1, 0) * at(2, 1) * at(3, 2) * at(0, 3)
2773 - at(3, 0) * at(2, 1) * at(0, 2) * at(1, 3) + at(2, 0) * at(3, 1) * at(0, 2) * at(1, 3)
2774
2775 + at(3, 0) * at(0, 1) * at(2, 2) * at(1, 3) - at(0, 0) * at(3, 1) * at(2, 2) * at(1, 3)
2776 - at(2, 0) * at(0, 1) * at(3, 2) * at(1, 3) + at(0, 0) * at(2, 1) * at(3, 2) * at(1, 3)
2777
2778 + at(3, 0) * at(1, 1) * at(0, 2) * at(2, 3) - at(1, 0) * at(3, 1) * at(0, 2) * at(2, 3)
2779 - at(3, 0) * at(0, 1) * at(1, 2) * at(2, 3) + at(0, 0) * at(3, 1) * at(1, 2) * at(2, 3)
2780
2781 + at(1, 0) * at(0, 1) * at(3, 2) * at(2, 3) - at(0, 0) * at(1, 1) * at(3, 2) * at(2, 3)
2782 - at(2, 0) * at(1, 1) * at(0, 2) * at(3, 3) + at(1, 0) * at(2, 1) * at(0, 2) * at(3, 3)
2783
2784 + at(2, 0) * at(0, 1) * at(1, 2) * at(3, 3) - at(0, 0) * at(2, 1) * at(1, 2) * at(3, 3)
2785 - at(1, 0) * at(0, 1) * at(2, 2) * at(3, 3) + at(0, 0) * at(1, 1) * at(2, 2) * at(3, 3);
2786
2787 }
2788
2795 Matrix4<T> inverse()
2796 {
2797 Matrix4<T> ret;
2798
2799 ret.at(0, 0) = +at(2, 1) * at(3, 2) * at(1, 3) - at(3, 1) * at(2, 2) * at(1, 3) + at(3, 1) * at(1, 2) * at(2, 3)
2800 - at(1, 1) * at(3, 2) * at(2, 3) - at(2, 1) * at(1, 2) * at(3, 3) + at(1, 1) * at(2, 2) * at(3, 3);
2801
2802 ret.at(1, 0) = +at(3, 0) * at(2, 2) * at(1, 3) - at(2, 0) * at(3, 2) * at(1, 3) - at(3, 0) * at(1, 2) * at(2, 3)
2803 + at(1, 0) * at(3, 2) * at(2, 3) + at(2, 0) * at(1, 2) * at(3, 3) - at(1, 0) * at(2, 2) * at(3, 3);
2804
2805 ret.at(2, 0) = +at(2, 0) * at(3, 1) * at(1, 3) - at(3, 0) * at(2, 1) * at(1, 3) + at(3, 0) * at(1, 1) * at(2, 3)
2806 - at(1, 0) * at(3, 1) * at(2, 3) - at(2, 0) * at(1, 1) * at(3, 3) + at(1, 0) * at(2, 1) * at(3, 3);
2807
2808 ret.at(3, 0) = +at(3, 0) * at(2, 1) * at(1, 2) - at(2, 0) * at(3, 1) * at(1, 2) - at(3, 0) * at(1, 1) * at(2, 2)
2809 + at(1, 0) * at(3, 1) * at(2, 2) + at(2, 0) * at(1, 1) * at(3, 2) - at(1, 0) * at(2, 1) * at(3, 2);
2810
2811 ret.at(0, 1) = +at(3, 1) * at(2, 2) * at(0, 3) - at(2, 1) * at(3, 2) * at(0, 3) - at(3, 1) * at(0, 2) * at(2, 3)
2812 + at(0, 1) * at(3, 2) * at(2, 3) + at(2, 1) * at(0, 2) * at(3, 3) - at(0, 1) * at(2, 2) * at(3, 3);
2813
2814 ret.at(1, 1) = +at(2, 0) * at(3, 2) * at(0, 3) - at(3, 0) * at(2, 2) * at(0, 3) + at(3, 0) * at(0, 2) * at(2, 3)
2815 - at(0, 0) * at(3, 2) * at(2, 3) - at(2, 0) * at(0, 2) * at(3, 3) + at(0, 0) * at(2, 2) * at(3, 3);
2816
2817 ret.at(2, 1) = +at(3, 0) * at(2, 1) * at(0, 3) - at(2, 0) * at(3, 1) * at(0, 3) - at(3, 0) * at(0, 1) * at(2, 3)
2818 + at(0, 0) * at(3, 1) * at(2, 3) + at(2, 0) * at(0, 1) * at(3, 3) - at(0, 0) * at(2, 1) * at(3, 3);
2819
2820 ret.at(3, 1) = +at(2, 0) * at(3, 1) * at(0, 2) - at(3, 0) * at(2, 1) * at(0, 2) + at(3, 0) * at(0, 1) * at(2, 2)
2821 - at(0, 0) * at(3, 1) * at(2, 2) - at(2, 0) * at(0, 1) * at(3, 2) + at(0, 0) * at(2, 1) * at(3, 2);
2822
2823 ret.at(0, 2) = +at(1, 1) * at(3, 2) * at(0, 3) - at(3, 1) * at(1, 2) * at(0, 3) + at(3, 1) * at(0, 2) * at(1, 3)
2824 - at(0, 1) * at(3, 2) * at(1, 3) - at(1, 1) * at(0, 2) * at(3, 3) + at(0, 1) * at(1, 2) * at(3, 3);
2825
2826 ret.at(1, 2) = +at(3, 0) * at(1, 2) * at(0, 3) - at(1, 0) * at(3, 2) * at(0, 3) - at(3, 0) * at(0, 2) * at(1, 3)
2827 + at(0, 0) * at(3, 2) * at(1, 3) + at(1, 0) * at(0, 2) * at(3, 3) - at(0, 0) * at(1, 2) * at(3, 3);
2828
2829 ret.at(2, 2) = +at(1, 0) * at(3, 1) * at(0, 3) - at(3, 0) * at(1, 1) * at(0, 3) + at(3, 0) * at(0, 1) * at(1, 3)
2830 - at(0, 0) * at(3, 1) * at(1, 3) - at(1, 0) * at(0, 1) * at(3, 3) + at(0, 0) * at(1, 1) * at(3, 3);
2831
2832 ret.at(3, 2) = +at(3, 0) * at(1, 1) * at(0, 2) - at(1, 0) * at(3, 1) * at(0, 2) - at(3, 0) * at(0, 1) * at(1, 2)
2833 + at(0, 0) * at(3, 1) * at(1, 2) + at(1, 0) * at(0, 1) * at(3, 2) - at(0, 0) * at(1, 1) * at(3, 2);
2834
2835 ret.at(0, 3) = +at(2, 1) * at(1, 2) * at(0, 3) - at(1, 1) * at(2, 2) * at(0, 3) - at(2, 1) * at(0, 2) * at(1, 3)
2836 + at(0, 1) * at(2, 2) * at(1, 3) + at(1, 1) * at(0, 2) * at(2, 3) - at(0, 1) * at(1, 2) * at(2, 3);
2837
2838 ret.at(1, 3) = +at(1, 0) * at(2, 2) * at(0, 3) - at(2, 0) * at(1, 2) * at(0, 3) + at(2, 0) * at(0, 2) * at(1, 3)
2839 - at(0, 0) * at(2, 2) * at(1, 3) - at(1, 0) * at(0, 2) * at(2, 3) + at(0, 0) * at(1, 2) * at(2, 3);
2840
2841 ret.at(2, 3) = +at(2, 0) * at(1, 1) * at(0, 3) - at(1, 0) * at(2, 1) * at(0, 3) - at(2, 0) * at(0, 1) * at(1, 3)
2842 + at(0, 0) * at(2, 1) * at(1, 3) + at(1, 0) * at(0, 1) * at(2, 3) - at(0, 0) * at(1, 1) * at(2, 3);
2843
2844 ret.at(3, 3) = +at(1, 0) * at(2, 1) * at(0, 2) - at(2, 0) * at(1, 1) * at(0, 2) + at(2, 0) * at(0, 1) * at(1, 2)
2845 - at(0, 0) * at(2, 1) * at(1, 2) - at(1, 0) * at(0, 1) * at(2, 2) + at(0, 0) * at(1, 1) * at(2, 2);
2846
2847 return ret / det();
2848 }
2849
2853 Matrix4<T> transpose()
2854 {
2855 Matrix4<T> ret;
2856 for (int i = 0; i < 4; i++)
2857 {
2858 for (int j = 0; j < 4; j++)
2859 {
2860 ret.at(i, j) = at(j, i);
2861 }
2862 }
2863 return ret;
2864 }
2865
2875 Matrix4<T> lerp(T fact, const Matrix4<T>& rhs) const
2876 {
2877 Matrix4<T> ret = (*this) + (rhs - (*this)) * fact;
2878 return ret;
2879 }
2880
2881 //-------------[ conversion ]-----------------------------
2887 operator T*()
2888 {
2889 return (T*) data;
2890 }
2891
2897 operator const T*() const
2898 {
2899 return (const T*) data;
2900 }
2901
2902 //----------[ output operator ]----------------------------
2909 friend std::ostream& operator <<(std::ostream& lhs, const Matrix4<T>& rhs)
2910 {
2911 for (int i = 0; i < 4; i++)
2912 {
2913 lhs << "|\t";
2914 for (int j = 0; j < 4; j++)
2915 {
2916 lhs << rhs.at(j, i) << "\t";
2917 }
2918 lhs << "|" << std::endl;
2919 }
2920 return lhs;
2921 }
2922
2926 std::string toString() const
2927 {
2928 std::ostringstream oss;
2929 oss << *this;
2930 return oss.str();
2931 }
2932
2933 };
2934
2936 typedef Matrix4<float> Matrix4f;
2938 typedef Matrix4<double> Matrix4d;
2940 typedef Matrix4<int> Matrix4i;
2941
2949 template<class T>
2950 class Quaternion
2951 {
2952 public:
2956 T w;
2960 Vector3<T> v;
2961
2965 Quaternion()
2966 : w(0), v(0, 0, 0)
2967 {
2968 }
2969
2973 Quaternion(const Quaternion<T>& q)
2974 : w(q.w), v(q.v)
2975 {
2976 }
2977
2981 template<class FromT>
2982 Quaternion(const Quaternion<FromT>& q)
2983 : w(static_cast<T>(q.w)), v(q.v)
2984 {
2985 }
2986
2992 Quaternion(T w_, const Vector3<T>& v_)
2993 : w(w_), v(v_)
2994 {
2995 }
2996
3004 Quaternion(T w_, T x, T y, T z)
3005 : w(w_), v(x, y, z)
3006 {
3007 }
3008
3013 Quaternion<T>& operator=(const Quaternion<T>& rhs)
3014 {
3015 v = rhs.v;
3016 w = rhs.w;
3017 return *this;
3018 }
3019
3024 template<class FromT>
3025 Quaternion<T>& operator=(const Quaternion<FromT>& rhs)
3026 {
3027 v = rhs.v;
3028 w = static_cast<T>(rhs.w);
3029 return *this;
3030 }
3031
3036 Quaternion<T> operator+(const Quaternion<T>& rhs) const
3037 {
3038 const Quaternion<T>& lhs = *this;
3039 return Quaternion<T>(lhs.w + rhs.w, lhs.v + rhs.v);
3040 }
3041
3046 Quaternion<T> operator*(const Quaternion<T>& rhs) const
3047 {
3048 const Quaternion<T>& lhs = *this;
3049 return Quaternion<T>(lhs.w * rhs.w - lhs.v.x * rhs.v.x - lhs.v.y * rhs.v.y - lhs.v.z * rhs.v.z,
3050 lhs.w * rhs.v.x + lhs.v.x * rhs.w + lhs.v.y * rhs.v.z - lhs.v.z * rhs.v.y,
3051 lhs.w * rhs.v.y - lhs.v.x * rhs.v.z + lhs.v.y * rhs.w + lhs.v.z * rhs.v.x,
3052 lhs.w * rhs.v.z + lhs.v.x * rhs.v.y - lhs.v.y * rhs.v.x + lhs.v.z * rhs.w);
3053 }
3054
3059 Quaternion<T> operator*(T rhs) const
3060 {
3061 return Quaternion<T>(w * rhs, v * rhs);
3062 }
3063
3068 Quaternion<T> operator-(const Quaternion<T>& rhs) const
3069 {
3070 const Quaternion<T>& lhs = *this;
3071 return Quaternion<T>(lhs.w - rhs.w, lhs.v - rhs.v);
3072 }
3073
3078 Quaternion<T>& operator+=(const Quaternion<T>& rhs)
3079 {
3080 w += rhs.w;
3081 v += rhs.v;
3082 return *this;
3083 }
3084
3089 Quaternion<T>& operator-=(const Quaternion<T>& rhs)
3090 {
3091 w -= rhs.w;
3092 v -= rhs.v;
3093 return *this;
3094 }
3095
3100 Quaternion<T>& operator*=(const Quaternion<T>& rhs)
3101 {
3102 Quaternion q = (*this) * rhs;
3103 v = q.v;
3104 w = q.w;
3105 return *this;
3106 }
3107
3112 Quaternion<T>& operator*=(T rhs)
3113 {
3114 w *= rhs;
3115 v *= rhs;
3116 return *this;
3117 }
3118
3126 bool operator==(const Quaternion<T>& rhs) const
3127 {
3128 const Quaternion<T>& lhs = *this;
3129 return (std::fabs(lhs.w - rhs.w) < EPSILON) && lhs.v == rhs.v;
3130 }
3131
3137 bool operator!=(const Quaternion<T>& rhs) const
3138 {
3139 return !(*this == rhs);
3140 }
3141
3142 //-------------[ unary operations ]--------------------------
3147 Quaternion<T> operator-() const
3148 {
3149 return Quaternion<T>(-w, -v);
3150 }
3151
3156 Quaternion<T> operator~() const
3157 {
3158 return Quaternion<T>(w, -v);
3159 }
3160
3165 T length() const
3166 {
3167 return (T) std::sqrt(w * w + v.lengthSq());
3168 }
3169
3177 T lengthSq() const
3178 {
3179 return w * w + v.lengthSq();
3180 }
3181
3185 void normalize()
3186 {
3187 T len = length();
3188 w /= len;
3189 v /= len;
3190 }
3191
3199 static Quaternion<T> fromEulerAngles(T x, T y, T z)
3200 {
3201 Quaternion<T> ret = fromAxisRot(Vector3<T>(1, 0, 0), x) * fromAxisRot(Vector3<T>(0, 1, 0), y)
3202 * fromAxisRot(Vector3<T>(0, 0, 1), z);
3203 return ret;
3204 }
3205
3211 static Quaternion<T> fromAxisRot(Vector3<T> axis, float angleDeg)
3212 {
3213 double angleRad = DEG2RAD(angleDeg);
3214 double sa2 = std::sin(angleRad / 2);
3215 double ca2 = std::cos(angleRad / 2);
3216 return Quaternion<T>(ca2, axis * sa2);
3217 }
3218
3223 Matrix3<T> rotMatrix()
3224 {
3225 Matrix3<T> ret;
3226
3227 /*ret.at(0,0) = 1 - 2*v.y*v.y - 2*v.z*v.z;
3228 ret.at(1,0) = 2*v.x*v.y - 2*w*v.z;
3229 ret.at(2,0) = 2*v.x*v.z - 2*w*v.y;
3230
3231 ret.at(0,1) = 2*v.x*v.y + 2*w*v.z;
3232 ret.at(1,1) = 1 - 2*v.x*v.x - 2*v.z*v.z;
3233 ret.at(2,1) = 2*v.y*v.z - 2*w*v.x;
3234
3235 ret.at(0,2) = 2*v.x*v.z - 2*w*v.y;
3236 ret.at(1,2) = 2*v.y*v.z + 2*w*v.x;
3237 ret.at(2,2) = 1 - 2*v.x*v.x - 2*v.y*v.y;*/
3238
3239 T xx = v.x * v.x;
3240 T xy = v.x * v.y;
3241 T xz = v.x * v.z;
3242 T xw = v.x * w;
3243
3244 T yy = v.y * v.y;
3245 T yz = v.y * v.z;
3246 T yw = v.y * w;
3247
3248 T zz = v.z * v.z;
3249 T zw = v.z * w;
3250
3251 ret.at(0, 0) = 1 - 2 * (yy + zz);
3252 ret.at(1, 0) = 2 * (xy - zw);
3253 ret.at(2, 0) = 2 * (xz + yw);
3254
3255 ret.at(0, 1) = 2 * (xy + zw);
3256 ret.at(1, 1) = 1 - 2 * (xx + zz);
3257 ret.at(2, 1) = 2 * (yz - xw);
3258
3259 ret.at(0, 2) = 2 * (xz - yw);
3260 ret.at(1, 2) = 2 * (yz + xw);
3261 ret.at(2, 2) = 1 - 2 * (xx + yy);
3262
3263 return ret;
3264 }
3265
3272 Matrix4<T> transform() const
3273 {
3274 Matrix4<T> ret;
3275
3276 T xx = v.x * v.x;
3277 T xy = v.x * v.y;
3278 T xz = v.x * v.z;
3279 T xw = v.x * w;
3280
3281 T yy = v.y * v.y;
3282 T yz = v.y * v.z;
3283 T yw = v.y * w;
3284
3285 T zz = v.z * v.z;
3286 T zw = v.z * w;
3287
3288 ret.at(0, 0) = 1 - 2 * (yy + zz);
3289 ret.at(1, 0) = 2 * (xy - zw);
3290 ret.at(2, 0) = 2 * (xz + yw);
3291 ret.at(3, 0) = 0;
3292
3293 ret.at(0, 1) = 2 * (xy + zw);
3294 ret.at(1, 1) = 1 - 2 * (xx + zz);
3295 ret.at(2, 1) = 2 * (yz - xw);
3296 ret.at(3, 1) = 0;
3297
3298 ret.at(0, 2) = 2 * (xz - yw);
3299 ret.at(1, 2) = 2 * (yz + xw);
3300 ret.at(2, 2) = 1 - 2 * (xx + yy);
3301 ret.at(3, 2) = 0;
3302
3303 ret.at(0, 3) = 0;
3304 ret.at(1, 3) = 0;
3305 ret.at(2, 3) = 0;
3306 ret.at(3, 3) = 1;
3307
3308 return ret;
3309
3310 }
3311
3321 Quaternion<T> lerp(T fact, const Quaternion<T>& rhs) const
3322 {
3323 return Quaternion<T>((1 - fact) * w + fact * rhs.w, v.lerp(fact, rhs.v));
3324 }
3325
3329 friend std::ostream& operator <<(std::ostream& oss, const Quaternion<T>& q)
3330 {
3331 oss << "Re: " << q.w << " Im: " << q.v;
3332 return oss;
3333 }
3334
3338 std::string toString() const
3339 {
3340 std::ostringstream oss;
3341 oss << *this;
3342 return oss.str();
3343 }
3344
3351 // 2011-07-02: Davide Bacchet: changed formula to fix degenerate cases
3352 static Quaternion<T> fromMatrix(const Matrix4<T>& m)
3353 {
3354 Quaternion<T> q;
3355
3356 T tr, s;
3357 tr = m(1, 1) + m(2, 2) + m(3, 3);
3358 if (tr >= epsilon)
3359 {
3360 s = 0.5 / (T) sqrt(tr + 1.0);
3361 q.w = 0.25 / s;
3362 q.v.x = (m(3, 2) - m(2, 3)) * s;
3363 q.v.y = (m(1, 3) - m(3, 1)) * s;
3364 q.v.z = (m(2, 1) - m(1, 2)) * s;
3365 }
3366 else
3367 {
3368 T d0 = m(1, 1);
3369 T d1 = m(2, 2);
3370 T d2 = m(3, 3);
3371
3372 char bigIdx = (d0 > d1) ? ((d0 > d2) ? 0 : 2):((d1 > d2) ? 1 : 2);
3373
3374 if (bigIdx == 0)
3375 {
3376 s = 2.0 * (T) sqrt(1.0 + m(1, 1) - m(2, 2) - m(3, 3));
3377 q.w = (m(3, 2) - m(2, 3)) / s;
3378 q.v.x = 0.25 * s;
3379 q.v.y = (m(1, 2) + m(2, 1)) / s;
3380 q.v.z = (m(1, 3) + m(3, 1)) / s;
3381 }
3382 else if (bigIdx == 1)
3383 {
3384 s = 2.0 * (T) sqrt(1.0 + m(2, 2) - m(1, 1) - m(3, 3));
3385 q.w = (m(1, 3) - m(3, 1)) / s;
3386 q.v.x = (m(1, 2) + m(2, 1)) / s;
3387 q.v.y = 0.25 * s;
3388 q.v.z = (m(2, 3) + m(3, 2)) / s;
3389 }
3390 else
3391 {
3392 s = 2.0 * (T) sqrt(1.0 + m(3, 3) - m(1, 1) - m(2, 2));
3393 q.w = (m(2, 1) - m(1, 2)) / s;
3394 q.v.x = (m(1, 3) + m(3, 1)) / s;
3395 q.v.y = (m(2, 3) + m(3, 2)) / s;
3396 q.v.z = 0.25 * s;
3397 }
3398 }
3399
3400 return q;
3401 }
3402
3410 // 2011-07-02: Davide Bacchet: changed formula to fix degenerate cases
3411 static Quaternion<T> fromMatrix(const Matrix3<T>& m)
3412 {
3413 Quaternion<T> q;
3414
3415 T tr, s;
3416 tr = m(1, 1) + m(2, 2) + m(3, 3);
3417 if (tr >= epsilon)
3418 {
3419 s = 0.5 / (T) sqrt(tr + 1.0);
3420 q.w = 0.25 / s;
3421 q.v.x = (m(3, 2) - m(2, 3)) * s;
3422 q.v.y = (m(1, 3) - m(3, 1)) * s;
3423 q.v.z = (m(2, 1) - m(1, 2)) * s;
3424 }
3425 else
3426 {
3427 T d0 = m(1, 1);
3428 T d1 = m(2, 2);
3429 T d2 = m(3, 3);
3430
3431 char bigIdx = (d0 > d1) ? ((d0 > d2) ? 0 : 2):((d1 > d2) ? 1 : 2);
3432
3433 if (bigIdx == 0)
3434 {
3435 s = 2.0 * (T) sqrt(1.0 + m(1, 1) - m(2, 2) - m(3, 3));
3436 q.w = (m(3, 2) - m(2, 3)) / s;
3437 q.v.x = 0.25 * s;
3438 q.v.y = (m(1, 2) + m(2, 1)) / s;
3439 q.v.z = (m(1, 3) + m(3, 1)) / s;
3440 }
3441 else if (bigIdx == 1)
3442 {
3443 s = 2.0 * (T) sqrt(1.0 + m(2, 2) - m(1, 1) - m(3, 3));
3444 q.w = (m(1, 3) - m(3, 1)) / s;
3445 q.v.x = (m(1, 2) + m(2, 1)) / s;
3446 q.v.y = 0.25 * s;
3447 q.v.z = (m(2, 3) + m(3, 2)) / s;
3448 }
3449 else
3450 {
3451 s = 2.0 * (T) sqrt(1.0 + m(3, 3) - m(1, 1) - m(2, 2));
3452 q.w = (m(2, 1) - m(1, 2)) / s;
3453 q.v.x = (m(1, 3) + m(3, 1)) / s;
3454 q.v.y = (m(2, 3) + m(3, 2)) / s;
3455 q.v.z = 0.25 * s;
3456 }
3457 }
3458
3459 return q;
3460 }
3461
3470 Quaternion<T> slerp(T r, const Quaternion<T>& q2) const
3471 {
3472 Quaternion<T> ret;
3473 T cosTheta = w * q2.w + v.x * q2.v.x + v.y * q2.v.y + v.z * q2.v.z;
3474 T theta = (T) acos(cosTheta);
3475 if (fabs(theta) < epsilon)
3476 {
3477 ret = *this;
3478 }
3479 else
3480 {
3481 T sinTheta = (T) sqrt(1.0 - cosTheta * cosTheta);
3482 if (fabs(sinTheta) < epsilon)
3483 {
3484 ret.w = 0.5 * w + 0.5 * q2.w;
3485 ret.v = v.lerp(0.5, q2.v);
3486 }
3487 else
3488 {
3489 T rA = (T) sin((1.0 - r) * theta) / sinTheta;
3490 T rB = (T) sin(r * theta) / sinTheta;
3491
3492 ret.w = w * rA + q2.w * rB;
3493 ret.v.x = v.x * rA + q2.v.x * rB;
3494 ret.v.y = v.y * rA + q2.v.y * rB;
3495 ret.v.z = v.z * rA + q2.v.z * rB;
3496 }
3497 }
3498 return ret;
3499 }
3500
3501 };
3502
3503 typedef Quaternion<float> Quatf;
3504 typedef Quaternion<double> Quatd;
3505
3506 #ifdef VMATH_NAMESPACE
3507 }
3508 #endif
3509
3510
3511
3513 //
3514 // Standard C++ library extensions
3515 //
3517
3518
3519 // Shortcut defines
3520 #ifdef VMATH_NAMESPACE
3521 #define VEC2 VMATH_NAMESPACE::Vector2
3522 #define VEC3 VMATH_NAMESPACE::Vector3
3523 #define VEC4 VMATH_NAMESPACE::Vector4
3524 #else
3525 #define VEC2 Vector2
3526 #define VEC3 Vector3
3527 #define VEC4 Vector4
3528 #endif
3529
3530 namespace std
3531 {
3532
3537 template <typename T>
3538 VEC2<T> min(const VEC2<T>& a, const VEC2<T>& b)
3539 {
3540 return VEC2<T>(::std::min(a.x, b.x), ::std::min(a.y, b.y));
3541 }
3542
3547 template <typename T>
3548 VEC3<T> min(const VEC3<T>& a, const VEC3<T>& b)
3549 {
3550 return VEC3<T>(::std::min(a.x, b.x), ::std::min(a.y, b.y), ::std::min(a.z, b.z));
3551 }
3552
3557 template <typename T>
3558 VEC4<T> min(const VEC4<T>& a, const VEC4<T>& b)
3559 {
3560 return VEC4<T>(::std::min(a.x, b.x), ::std::min(a.y, b.y), ::std::min(a.z, b.z), ::std::min(a.w, b.w));
3561 }
3562
3567 template <typename T>
3568 VEC2<T> max(const VEC2<T>& a, const VEC2<T>& b)
3569 {
3570 return VEC2<T>(::std::max(a.x, b.x), ::std::max(a.y, b.y));
3571 }
3572
3577 template <typename T>
3578 VEC3<T> max(const VEC3<T>& a, const VEC3<T>& b)
3579 {
3580 return VEC3<T>(::std::max(a.x, b.x), ::std::max(a.y, b.y), ::std::max(a.z, b.z));
3581 }
3582
3587 template <typename T>
3588 VEC4<T> max(const VEC4<T>& a, const VEC4<T>& b)
3589 {
3590 return VEC4<T>(::std::max(a.x, b.x), ::std::max(a.y, b.y), ::std::max(a.z, b.z), ::std::max(a.w, b.w));
3591 }
3592 }
3593
3594 // cleanup shortcut defines
3595 #undef VEC2
3596 #undef VEC3
3597 #undef VEC4
3598
3599 #ifdef VMATH_NAMESPACE
3600 namespace VMATH_NAMESPACE
3601 {
3602 #endif //VMATH_NAMESPACE
3603
3620 template <typename T>
3621 class Aabb3
3622 {
3623 public:
3627 Vector3<T> min;
3628
3632 Vector3<T> max;
3633
3638 Aabb3()
3639 : min(1,1,1), max(-1,-1,-1)
3640 {}
3641
3646 template <typename SrcT>
3647 Aabb3(const Vector3<SrcT>& point)
3648 : min(point), max(point)
3649 {}
3650
3661 template <typename SrcT>
3662 Aabb3(SrcT x0, SrcT y0, SrcT z0, SrcT x1, SrcT y1, SrcT z1)
3663 : min(std::min(x0,x1), std::min(y0,y1), std::min(z0,z1)),
3664 max(std::max(x0,x1), std::max(y0,y1), std::max(z0,z1))
3665 {}
3666
3673 template <typename SrcT>
3674 Aabb3(SrcT x, SrcT y, SrcT z)
3675 : min(x,y,z), max(x,y,z)
3676 {}
3677
3682 template <typename SrcT>
3683 Aabb3(const Aabb3<SrcT>& src)
3684 : min(src.min), max(src.max)
3685 {}
3686
3692 template <typename SrcT>
3693 Aabb3<T>& operator=(const Aabb3<SrcT>& rhs)
3694 {
3695 min = rhs.min;
3696 max = rhs.max;
3697 return *this;
3698 }
3699
3700
3707 bool valid() const
3708 { return min.x <= max.x && min.y <= max.y && min.z <= max.z; }
3709
3714 void invalidate()
3715 { min = Vector3<T>(1,1,1); max = Vector3<T>(-1,-1,-1); }
3716
3721 template <typename SrcT>
3722 void extend(const Vector3<SrcT>& point)
3723 {
3724 if (!valid())
3725 {
3726 min = max = point;
3727 }
3728 else
3729 {
3730 min = std::min(min, point);
3731 max = std::max(max, point);
3732 }
3733 }
3734
3739 template <typename SrcT>
3740 void extend(const Aabb3<SrcT>& box)
3741 {
3742 if (!valid())
3743 {
3744 min = box.min;
3745 max = box.max;
3746 }
3747 else
3748 {
3749 min = std::min(min, box.min);
3750 max = std::max(max, box.max);
3751 }
3752 }
3753
3759 template <typename SrcT>
3760 Aabb3<T> extended(const Vector3<SrcT>& point) const
3761 {
3762 Aabb3<T> ret(*this);
3763 ret.extend(point);
3764 return ret;
3765 }
3766
3772 template <typename SrcT>
3773 Aabb3<T> extended(const Aabb3<SrcT>& box) const
3774 {
3775 Aabb3<T> ret(*this);
3776 ret.extend(box);
3777 return ret;
3778 }
3779
3785 template <typename SrcT>
3786 bool intersects(const Vector3<SrcT>& point) const
3787 {
3788 /*
3789 for (size_t i = 0; i < 3; i++)
3790 {
3791 if (min[i] > point[i] || point[i] > max[i])
3792 return false;
3793 }
3794 */
3795 // unrolled loop above
3796 if (min.x > point.x || point.x > max.x)
3797 return false;
3798 if (min.y > point.y || point.y > max.y)
3799 return false;
3800 if (min.z > point.z || point.z > max.z)
3801 return false;
3802
3803 return true;
3804 }
3805
3811 template <typename SrcT>
3812 bool intersects(const Aabb3<SrcT>& box) const
3813 {
3814 /*
3815 for (size_t i = 0; i < 3; i++)
3816 {
3817 if (max[i] < box.min[i] || min[i] > box.max[i])
3818 return false;
3819 }
3820 */
3821 // unrolled loop above
3822 if (max.x < box.min.x || min.x > box.max.x)
3823 return false;
3824 if (max.y < box.min.y || min.y > box.max.y)
3825 return false;
3826 if (max.z < box.min.z || min.z > box.max.z)
3827 return false;
3828
3829 return true;
3830 }
3831
3839 template <typename SrcT>
3840 Aabb3<T> intersection(const Aabb3<SrcT>& other) const
3841 {
3842 Aabb3<T> ret;
3843 if (max.x < other.min.x || min.x > other.max.x)
3844 return ret;
3845 if (max.y < other.min.y || min.y > other.max.y)
3846 return ret;
3847 if (max.z < other.min.z || min.z > other.max.z)
3848 return ret;
3849
3850 ret.min = std::max(min, other.min);
3851 ret.max = std::min(max, other.max);
3852
3853 return ret;
3854 }
3855
3860 Vector3<T> center() const
3861 { return (min + max) * 0.5f; }
3862
3867 Vector3<T> extent() const
3868 { return (max - min) * 0.5f; }
3869
3874 Vector3<T> size() const
3875 { return max - min; }
3876
3892 Vector3<T> point(size_t i) const
3893 {
3894 assert(i < 8);
3895 return Vector3<T>(i & 1 ? min.x : max.x, i & 2 ? min.y : max.y, i & 4 ? min.z : max.z);
3896 }
3897
3903 Aabb3<T> transformed(const Matrix4<T>& t) const
3904 {
3905 Aabb3<T> ret;
3906 for (size_t i = 0; i < 8; i++)
3907 {
3908 const Vector4<T> p(point(i), 1);
3909 ret.extend((t * p).xyz());
3910 }
3911
3912 return ret;
3913 }
3914
3915 //-------------------------------------------------------------------------------------------------------------
3916 // operators
3917 //-------------------------------------------------------------------------------------------------------------
3923 template <typename RhsT>
3924 bool operator==(const Aabb3<RhsT>& rhs) const
3925 {
3926 return min == rhs.min && max == rhs.max;
3927 }
3928
3934 template <typename RhsT>
3935 bool operator!=(const Aabb3<RhsT>& rhs) const
3936 {
3937 return min != rhs.min || max != rhs.max;
3938 }
3939
3945 Aabb3<T> operator*(const Matrix4<T>& rhs) const
3946 {
3947 return transformed(rhs);
3948 }
3949
3955 Aabb3<T>& operator*=(const Matrix4<T>& rhs)
3956 {
3957 *this = transformed(rhs);
3958 return *this;
3959 }
3960
3961
3967 template <typename SrcT>
3968 Aabb3<T>& operator<<(const Vector3<SrcT>& rhs)
3969 {
3970 extend(rhs);
3971 return *this;
3972 }
3973
3979 template <typename SrcT>
3980 Aabb3<T>& operator<<(const Aabb3<SrcT>& rhs)
3981 {
3982 extend(rhs);
3983 return *this;
3984 }
3985
3991 template <typename RhsT>
3992 Aabb3<T> operator|(const Aabb3<RhsT>& rhs) const
3993 {
3994 return extended(rhs);
3995 }
3996
4002 template <typename RhsT>
4003 Aabb3<T> operator&(const Aabb3<RhsT>& rhs) const
4004 {
4005 return intersection(rhs);
4006 }
4007
4014 friend std::ostream& operator<<(std::ostream& lhs, const Aabb3<T>& rhs)
4015 {
4016 lhs << rhs.min << " x " << rhs.max;
4017 return lhs;
4018 }
4019
4020 };
4021
4022 typedef Aabb3<float> Aabb3f;
4023 typedef Aabb3<double> Aabb3d;
4024
4025 #ifdef VMATH_NAMESPACE
4026 }
4027 #endif //VMATH_NAMESPACE
4028
4029
4030 #endif // __vmath_Header_File__
4031
Quaternion::operator!=
bool operator!=(const Quaternion< T > &rhs) const
Inequality test operator.
Definition: vmath.h:3137
Vector2::toString
std::string toString() const
Gets string representation.
Definition: vmath.h:558
Quaternion::fromMatrix
static Quaternion< T > fromMatrix(const Matrix4< T > &m)
Creates quaternion from transform matrix.
Definition: vmath.h:3352
Vector4::operator*=
Vector4< T > & operator*=(const Vector4< T > &rhs)
Multiplication operator.
Definition: vmath.h:1349
Vector2::operator/=
Vector2< T > & operator/=(const Vector2< T > &rhs)
Division operator.
Definition: vmath.h:354
Aabb3::Aabb3
Aabb3(SrcT x, SrcT y, SrcT z)
Constructs axes-aligned bounding-box containing point (x, y, z)
Definition: vmath.h:3674
Vector2::y
T y
Second element of vector, alias for Y-coordinate.
Definition: vmath.h:179
Aabb3::operator=
Aabb3< T > & operator=(const Aabb3< SrcT > &rhs)
Assign operator.
Definition: vmath.h:3693
Matrix4::fromRowMajorArray
static Matrix4< T > fromRowMajorArray(const FromT *arr)
Creates new matrix 4x4 from array that represents such matrix 4x4 as array of tightly packed elements...
Definition: vmath.h:2417
Vector4i
Vector4< int > Vector4i
Three dimensional Vector of ints.
Definition: vmath.h:1610
Matrix4::operator*
Matrix4< T > operator*(T rhs) const
Multiplication operator.
Definition: vmath.h:2693
Matrix3::operator-
Matrix3< T > operator-(const Matrix3< T > &rhs) const
Subtraction operator.
Definition: vmath.h:1900
Quaternion::slerp
Quaternion< T > slerp(T r, const Quaternion< T > &q2) const
Computes spherical interpolation between quaternions (this, q2) using coefficient of interpolation r ...
Definition: vmath.h:3470
Matrix3d
Matrix3< double > Matrix3d
Matrix 3x3 of doubles.
Definition: vmath.h:2104
Aabb3::transformed
Aabb3< T > transformed(const Matrix4< T > &t) const
Gets transformed bounding-box by transform t.
Definition: vmath.h:3903
Aabb3::min
Vector3< T > min
Position of Min corner of bounding box.
Definition: vmath.h:3627
Aabb3::intersects
bool intersects(const Aabb3< SrcT > &box) const
Tests if other bounding-box box intersects (even partially) with this bouding-box.
Definition: vmath.h:3812
Matrix4::Matrix4
Matrix4(const Matrix4< T > &src)
Copy constructor.
Definition: vmath.h:2144
Matrix4d
Matrix4< double > Matrix4d
Matrix 4x4 of doubles.
Definition: vmath.h:2938
Quaternion::v
Vector3< T > v
Imaginary part of quaternion.
Definition: vmath.h:2960
Vector4::operator*
Vector4< T > operator*(T rhs) const
Multiplication operator.
Definition: vmath.h:1429
Vector3::operator+=
Vector3< T > & operator+=(T rhs)
Addition operator.
Definition: vmath.h:894
Vector3::operator/=
Vector3< T > & operator/=(const Vector3< T > &rhs)
Division operator.
Definition: vmath.h:827
Matrix3::createRotationAroundAxis
static Matrix3< T > createRotationAroundAxis(T xDeg, T yDeg, T zDeg)
Creates rotation matrix by rotation around axis.
Definition: vmath.h:1681
Quaternion::length
T length() const
Get lenght of quaternion.
Definition: vmath.h:3165
Matrix4::operator-
Matrix4< T > operator-(const Matrix4< T > &rhs) const
Subtraction operator.
Definition: vmath.h:2656
Vector4::lengthSq
T lengthSq() const
Return square of length.
Definition: vmath.h:1524
Vector3::x
T x
First element of vector, alias for X-coordinate.
Definition: vmath.h:597
Matrix3::fromColumnMajorArray
static Matrix3< T > fromColumnMajorArray(const FromT *arr)
Creates new matrix 3x3 from array that represents such matrix 3x3 as array of tightly packed elements...
Definition: vmath.h:1755
Matrix3::operator*
Vector3< T > operator*(const Vector3< T > &rhs) const
Multiplication operator.
Definition: vmath.h:1962
Quaternion::Quaternion
Quaternion(const Quaternion< FromT > &q)
Copy casting constructor.
Definition: vmath.h:2982
Matrix3::operator+
Matrix3< T > operator+(T rhs) const
Addition operator.
Definition: vmath.h:1913
Vector4d
Vector4< double > Vector4d
Three dimensional Vector of doubles.
Definition: vmath.h:1608
Matrix3::operator()
const T & operator()(int i, int j) const
Get constant reference to element at position (i,j), with math matrix notation.
Definition: vmath.h:1836
Aabb3::valid
bool valid() const
Checks if bounding-box is valid.
Definition: vmath.h:3707
Matrix3::inverse
Matrix3< T > inverse()
Computes inverse matrix.
Definition: vmath.h:2032
Matrix4::operator==
bool operator==(const Matrix4< T > &rhs) const
Equality test operator.
Definition: vmath.h:2455
Aabb3::center
Vector3< T > center() const
Gets center point of bounding-box.
Definition: vmath.h:3860
Vector4::operator==
bool operator==(const Vector4< T > &rhs) const
Equality test operator.
Definition: vmath.h:1379
Matrix3::transpose
Matrix3< T > transpose()
Transpose matrix.
Definition: vmath.h:1994
Matrix4::Matrix4
Matrix4(const T *dt)
Copy matrix values from array (these data must be in column major order!)
Definition: vmath.h:2135
Vector3::u
T u
Third element of vector, alias for U-coordinate.
Definition: vmath.h:641
Matrix4::data
T data[16]
Data stored in column major order.
Definition: vmath.h:2119
Vector4::xyz
Vector3< T > xyz() const
Gets 3D vector.
Definition: vmath.h:1571
Quatd
Quaternion< double > Quatd
Definition: vmath.h:3504
Vector3::toString
std::string toString() const
Gets string representation.
Definition: vmath.h:1080
Vector2::lengthSq
T lengthSq() const
Return square of length.
Definition: vmath.h:502
Vector4::operator=
Vector4< T > operator=(const Vector4< FromT > &rhs)
Copy casting operator.
Definition: vmath.h:1233
Aabb3::operator==
bool operator==(const Aabb3< RhsT > &rhs) const
Tests if rhs is equal to this bounding-box.
Definition: vmath.h:3924
Aabb3::operator|
Aabb3< T > operator|(const Aabb3< RhsT > &rhs) const
Union of this and rhs bounding-boxes.
Definition: vmath.h:3992
Vector3::length
T length() const
Get length of vector.
Definition: vmath.h:976
Matrix3::data
T data[9]
Data stored in column major order.
Definition: vmath.h:1623
Quaternion::w
T w
Real part of quaternion.
Definition: vmath.h:2956
Vector4::operator*=
Vector4< T > & operator*=(T rhs)
Multiplication operator.
Definition: vmath.h:1473
Vector3::lengthSq
T lengthSq() const
Return square of length.
Definition: vmath.h:988
Vector3::normalize
void normalize()
Normalize vector.
Definition: vmath.h:996
Aabb3::extended
Aabb3< T > extended(const Aabb3< SrcT > &box) const
Gets a copy of this bounding-box extnended by box box.
Definition: vmath.h:3773
Vector3::operator*
Vector3< T > operator*(T rhs) const
Multiplication operator.
Definition: vmath.h:876
Matrix4::createFrustum
static Matrix4< T > createFrustum(T left, T right, T bottom, T top, T zNear, T zFar)
Creates OpenGL compatible perspective projection according specified frustum parameters.
Definition: vmath.h:2307
Aabb3::extend
void extend(const Aabb3< SrcT > &box)
Extends this bounding-box by a box box.
Definition: vmath.h:3740
Matrix3
Class for matrix 3x3.
Definition: vmath.h:1619
Vector2::operator!=
bool operator!=(const Vector2< T > &rhs) const
Inequality test operator.
Definition: vmath.h:460
Vector4::x
T x
Definition: vmath.h:1121
Vector3d
Vector3< double > Vector3d
Three dimensional Vector of doubles.
Definition: vmath.h:1091
Quaternion::Quaternion
Quaternion(T w_, T x, T y, T z)
Creates quaternion object from value (w_ + xi + yj + zk).
Definition: vmath.h:3004
Matrix4::operator+
Matrix4< T > operator+(T rhs) const
Addition operator.
Definition: vmath.h:2669
Quaternion
Quaternion class implementing some quaternion algebra operations.
Definition: vmath.h:2950
Matrix4::at
const T & at(int x, int y) const
Get constant reference to element at position (x,y).
Definition: vmath.h:2494
Matrix3::Matrix3
Matrix3(const Matrix3< T > &src)
Copy constructor.
Definition: vmath.h:1648
Vector3::s
T s
First element of vector, alias for S-coordinate.
Definition: vmath.h:603
Vector2::operator=
Vector2< T > & operator=(const Vector2< T > &rhs)
Copy operator.
Definition: vmath.h:243
Matrix3::operator=
Matrix3< T > & operator=(const T *rhs)
Copy operator.
Definition: vmath.h:1871
Vector2::Vector2
Vector2(const Vector2< FromT > &src)
Copy casting constructor.
Definition: vmath.h:221
Matrix4::operator-
Matrix4< T > operator-(T rhs) const
Subtraction operator.
Definition: vmath.h:2681
Aabb3::operator!=
bool operator!=(const Aabb3< RhsT > &rhs) const
Tests if rhs is not equal to this bounding-box.
Definition: vmath.h:3935
Matrix3::operator+
Matrix3< T > operator+(const Matrix3< T > &rhs) const
Addition operator.
Definition: vmath.h:1888
std
STL namespace.
Matrix4::inverse
Matrix4< T > inverse()
Computes inverse matrix.
Definition: vmath.h:2795
Quaternion::operator==
bool operator==(const Quaternion< T > &rhs) const
Equality test operator.
Definition: vmath.h:3126
Matrix3::operator()
T & operator()(int i, int j)
Get reference to element at position (i,j), with math matrix notation.
Definition: vmath.h:1824
Vector4::length
T length() const
Get length of vector.
Definition: vmath.h:1500
Aabb3::extended
Aabb3< T > extended(const Vector3< SrcT > &point) const
Gets a copy of this bounding-box extend by a point point.
Definition: vmath.h:3760
Matrix4::lerp
Matrix4< T > lerp(T fact, const Matrix4< T > &rhs) const
Linear interpolation of two matrices.
Definition: vmath.h:2875
Vector3::operator+
Vector3< T > operator+(const Vector3< T > &rhs) const
Addition operator.
Definition: vmath.h:755
Vector3::Vector3
Vector3(const Vector3< T > &src)
Copy constructor.
Definition: vmath.h:673
Vector2::operator/=
Vector2< T > & operator/=(T rhs)
Division operator.
Definition: vmath.h:435
Aabb3::extend
void extend(const Vector3< SrcT > &point)
Extends this bounding-box by a point point.
Definition: vmath.h:3722
Matrix4::Matrix4
Matrix4()
Creates identity matrix.
Definition: vmath.h:2125
Vector2::operator+=
Vector2< T > & operator+=(T rhs)
Addition operator.
Definition: vmath.h:402
Quaternion::Quaternion
Quaternion()
Quaternion constructor, sets quaternion to (0 + 0i + 0j + 0k).
Definition: vmath.h:2965
Vector4::y
T y
Second element of vector, alias for Y-coordinate.
Definition: vmath.h:1134
Vector2::operator+
Vector2< T > operator+(const Vector2< T > &rhs) const
Addition operator.
Definition: vmath.h:285
Matrix3::fromRowMajorArray
static Matrix3< T > fromRowMajorArray(const FromT *arr)
Creates new matrix 3x3 from array that represents such matrix 3x3 as array of tightly packed elements...
Definition: vmath.h:1738
Quaternion::operator+=
Quaternion< T > & operator+=(const Quaternion< T > &rhs)
Addition operator.
Definition: vmath.h:3078
Matrix3::det
T det()
Definition: vmath.h:2022
Aabb3::Aabb3
Aabb3(const Vector3< SrcT > &point)
Constructs axes-aligned bound-box containing one point point.
Definition: vmath.h:3647
Vector2::Vector2
Vector2()
Creates and sets to (0,0)
Definition: vmath.h:192
Matrix3::operator!=
bool operator!=(const Matrix3< T > &rhs) const
Inequality test operator.
Definition: vmath.h:1789
Vector3::operator==
bool operator==(const Vector3< T > &rhs) const
Equality test operator.
Definition: vmath.h:946
Vector4::Vector4
Vector4(const Vector4< T > &src)
Copy constructor.
Definition: vmath.h:1190
Quaternion::operator-
Quaternion< T > operator-() const
Unary negate operator.
Definition: vmath.h:3147
Aabb3
Axes-aligned bounding-box (aka AABB) class.
Definition: vmath.h:3621
Aabb3::Aabb3
Aabb3(const Aabb3< SrcT > &src)
Creates copy of axis-aligned bounding-box.
Definition: vmath.h:3683
Matrix3::operator*
Matrix3< T > operator*(T rhs) const
Multiplication operator.
Definition: vmath.h:1937
Vector4::z
T z
Third element of vector, alias for Z-coordinate.
Definition: vmath.h:1147
Vector3::Vector3
Vector3()
Creates and sets to (0,0,0)
Definition: vmath.h:653
Matrix3::lerp
Matrix3< T > lerp(T fact, const Matrix3< T > &rhs) const
Linear interpolation of two matrices.
Definition: vmath.h:2016
Aabb3d
Aabb3< double > Aabb3d
Definition: vmath.h:4023
Matrix3::identity
void identity()
Resets matrix to be identity matrix.
Definition: vmath.h:1669
Vector4f
Vector4< float > Vector4f
Three dimensional Vector of floats.
Definition: vmath.h:1606
Matrix3::operator-
Matrix3< T > operator-(T rhs) const
Subtraction operator.
Definition: vmath.h:1925
Vector4::operator-=
Vector4< T > & operator-=(T rhs)
Subtraction operator.
Definition: vmath.h:1460
Vector2::operator-
Vector2< T > operator-(const Vector2< T > &rhs) const
Subtraction operator.
Definition: vmath.h:294
Matrix4::createScale
static Matrix4< T > createScale(T sx, T sy, T sz)
Create scale matrix with sx, sy, and sz being values of matrix main diagonal.
Definition: vmath.h:2244
Vector2::operator*=
Vector2< T > & operator*=(T rhs)
Multiplication operator.
Definition: vmath.h:424
Matrix3::Matrix3
Matrix3(const T *dt)
Copy matrix values from array (these data must be in column major order!)
Definition: vmath.h:1639
Vector3::b
T b
Third element of vector, alias for B-coordinate.
Definition: vmath.h:646
Matrix4::transpose
Matrix4< T > transpose()
Transpose matrix.
Definition: vmath.h:2853
Aabb3::intersects
bool intersects(const Vector3< SrcT > &point) const
Tests if the point point is within this bounding-box.
Definition: vmath.h:3786
Vector4::operator-=
Vector4< T > & operator-=(const Vector4< T > &rhs)
Subtraction operator.
Definition: vmath.h:1336
Vector4::toString
std::string toString() const
Gets string representation.
Definition: vmath.h:1596
Vector3::operator/
Vector3< T > operator/(T rhs) const
Division operator.
Definition: vmath.h:885
Vector3::operator*
Vector3< T > operator*(const Vector3< T > &rhs) const
Multiplication operator.
Definition: vmath.h:773
Vector2::t
T t
Second element of vector, alias for T-coordinate.
Definition: vmath.h:185
Matrix4::det
T det()
Computes determinant of matrix.
Definition: vmath.h:2766
Matrix3::Matrix3
Matrix3()
Creates identity matrix.
Definition: vmath.h:1629
Vector3::operator[]
T & operator[](int n)
Array access operator.
Definition: vmath.h:721
Vector4::a
T a
Fourth element of vector, alias for A-coordinate.
Definition: vmath.h:1156
Matrix4::Matrix4
Matrix4(const Matrix4< FromT > &src)
Copy casting constructor.
Definition: vmath.h:2154
Vector4::w
T w
First element of vector, alias for W-coordinate.
Definition: vmath.h:1162
Vector3::operator*=
Vector3< T > & operator*=(const Vector3< T > &rhs)
Multiplication operator.
Definition: vmath.h:815
Vector3::operator+
Vector3< T > operator+(T rhs) const
Addition operator.
Definition: vmath.h:858
Vector2::lerp
Vector2< T > lerp(T fact, const Vector2< T > &r) const
Linear interpolation of two vectors.
Definition: vmath.h:517
Matrix3::operator/
Matrix3< T > operator/(T rhs) const
Division operator.
Definition: vmath.h:1949
Vector4::operator+=
Vector4< T > & operator+=(const Vector4< T > &rhs)
Addition operator.
Definition: vmath.h:1323
Vector2::operator-
Vector2< T > operator-(T rhs) const
Subtraction operator.
Definition: vmath.h:375
Matrix4
Class for matrix 4x4.
Definition: vmath.h:2115
Matrix4::at
T & at(int x, int y)
Get reference to element at postion (x,y).
Definition: vmath.h:2482
Vector2::operator/
Vector2< T > operator/(const Vector2< T > &rhs) const
Division operator.
Definition: vmath.h:312
Vector2::operator*
Vector2< T > operator*(T rhs) const
Multiplication operator.
Definition: vmath.h:384
Matrix4::operator*
Matrix4< T > operator*(Matrix4< T > rhs) const
Multiplication operator.
Definition: vmath.h:2742
Vector2::operator/
Vector2< T > operator/(T rhs) const
Division operator.
Definition: vmath.h:393
Vector2::operator+=
Vector2< T > & operator+=(const Vector2< T > &rhs)
Addition operator.
Definition: vmath.h:321
Matrix4::setScale
void setScale(const Vector3< T > &s)
Sets matrix scale for all axes.
Definition: vmath.h:2592
Quaternion::lerp
Quaternion< T > lerp(T fact, const Quaternion< T > &rhs) const
Linear interpolation of two quaternions.
Definition: vmath.h:3321
Matrix3::toString
std::string toString() const
Gets string representation.
Definition: vmath.h:2093
Vector2::operator-=
Vector2< T > & operator-=(T rhs)
Subtraction operator.
Definition: vmath.h:413
Matrix3f
Matrix3< float > Matrix3f
Matrix 3x3 of floats.
Definition: vmath.h:2102
Matrix4::operator()
const T & operator()(int i, int j) const
Get constant reference to element at position (i,j), with math matrix notation.
Definition: vmath.h:2518
Vector3::operator/
Vector3< T > operator/(const Vector3< T > &rhs) const
Division operator.
Definition: vmath.h:782
Vector4::b
T b
Third element of vector, alias for B-coordinate.
Definition: vmath.h:1143
Vector2::normalize
void normalize()
Normalize vector.
Definition: vmath.h:488
Vector4::operator[]
const T & operator[](int n) const
Array access operator.
Definition: vmath.h:1269
Vector3::lerp
Vector3< T > lerp(T fact, const Vector3< T > &r) const
Linear interpolation of two vectors.
Definition: vmath.h:1037
Quaternion::operator*=
Quaternion< T > & operator*=(T rhs)
Multiplication operator.
Definition: vmath.h:3112
Quaternion::operator*
Quaternion< T > operator*(T rhs) const
Multiplication operator.
Definition: vmath.h:3059
Vector4::Vector4
Vector4()
Creates and sets to (0,0,0,0)
Definition: vmath.h:1169
Vector3::t
T t
Second element of vector, alias for T-coordinate.
Definition: vmath.h:622
Matrix4::setRotation
void setRotation(const Matrix3< T > &m)
Sets rotation part (matrix 3x3) of matrix.
Definition: vmath.h:2548
Vector3::operator[]
const T & operator[](int n) const
Constant array access operator.
Definition: vmath.h:739
Vector2::Vector2
Vector2(T nx, T ny)
Creates and sets to (x,y)
Definition: vmath.h:202
Vector2::operator+
Vector2< T > operator+(T rhs) const
Addition operator.
Definition: vmath.h:366
Aabb3::operator*
Aabb3< T > operator*(const Matrix4< T > &rhs) const
Gets transformed bounding-box by transform rhs.
Definition: vmath.h:3945
Matrix4::setTranslation
void setTranslation(const Vector3< T > &v)
Sets translation part of matrix.
Definition: vmath.h:2530
Matrix4i
Matrix4< int > Matrix4i
Matrix 4x4 of int.
Definition: vmath.h:2940
Vector3::operator+=
Vector3< T > & operator+=(const Vector3< T > &rhs)
Addition operator.
Definition: vmath.h:791
Matrix4::setScale
void setScale(T sx, T sy, T sz)
Sets matrix scale for all axes.
Definition: vmath.h:2581
Vector4
Class for four dimensional vector.
Definition: vmath.h:1107
Quaternion::operator+
Quaternion< T > operator+(const Quaternion< T > &rhs) const
Addition operator.
Definition: vmath.h:3036
Quaternion::operator-=
Quaternion< T > & operator-=(const Quaternion< T > &rhs)
Subtraction operator.
Definition: vmath.h:3089
Aabb3::Aabb3
Aabb3()
Constructs invalid axes-aligned bounding-box.
Definition: vmath.h:3638
Vector4::operator-
Vector4< T > operator-(const Vector4< T > &rhs) const
Subtraction operator.
Definition: vmath.h:1296
EPSILON
#define EPSILON
Definition: vmath.h:144
Matrix4::operator=
Matrix4< T > & operator=(const T *rhs)
Copy operator.
Definition: vmath.h:2627
Vector2::operator[]
const T & operator[](int n) const
Constant array access operator.
Definition: vmath.h:271
Vector3::r
T r
First element of vector, alias for R-coordinate.
Definition: vmath.h:609
Vector3f
Vector3< float > Vector3f
Three dimensional Vector of floats.
Definition: vmath.h:1089
Vector2::Vector2
Vector2(const Vector2< T > &src)
Copy constructor.
Definition: vmath.h:211
Vector2
Class for two dimensional vector.
Definition: vmath.h:157
Quaternion::lengthSq
T lengthSq() const
Return square of length.
Definition: vmath.h:3177
Aabb3::operator*=
Aabb3< T > & operator*=(const Matrix4< T > &rhs)
Apply transform rhs to this bounding-box.
Definition: vmath.h:3955
Matrix3::Matrix3
Matrix3(const Matrix3< FromT > &src)
Copy casting constructor.
Definition: vmath.h:1658
Vector4::Vector4
Vector4(T nx, T ny, T nz, T nw)
Creates and sets to (x,y,z,z)
Definition: vmath.h:1181
Matrix4f
Matrix4< float > Matrix4f
Matrix 4x4 of floats.
Definition: vmath.h:2936
Quaternion::operator*=
Quaternion< T > & operator*=(const Quaternion< T > &rhs)
Multiplication operator.
Definition: vmath.h:3100
Quaternion::operator=
Quaternion< T > & operator=(const Quaternion< T > &rhs)
Copy operator.
Definition: vmath.h:3013
Matrix4::identity
void identity()
Resets matrix to be identity matrix.
Definition: vmath.h:2165
Vector3::y
T y
Second element of vector, alias for Y-coordinate.
Definition: vmath.h:617
Matrix4::getScale
Vector3< T > getScale() const
Gets matrix scale.
Definition: vmath.h:2563
Matrix3::fromOde
static Matrix3< T > fromOde(const It *mat)
Creates rotation matrix from ODE Matrix.
Definition: vmath.h:1718
Vector4::Vector4
Vector4(const Vector3< T > &src, T w)
Definition: vmath.h:1205
Vector2d
class Vector2< double > Vector2d
Two dimensional Vector of doubles.
Definition: vmath.h:572
Quaternion::fromEulerAngles
static Quaternion< T > fromEulerAngles(T x, T y, T z)
Creates quaternion for eulers angles.
Definition: vmath.h:3199
Vector3::operator=
Vector3< T > operator=(const Vector3< FromT > &rhs)
Copy casting operator.
Definition: vmath.h:706
Matrix4::operator+
Matrix4< T > operator+(const Matrix4< T > &rhs) const
Addition operator.
Definition: vmath.h:2644
Vector3::operator-
Vector3< T > operator-() const
Unary negate operator.
Definition: vmath.h:966
Vector2::x
T x
First element of vector, alias for X-coordinate.
Definition: vmath.h:165
Vector3::crossProduct
Vector3< T > crossProduct(const Vector3< T > &rhs) const
Cross product operator.
Definition: vmath.h:848
Matrix3::operator=
Matrix3< T > & operator=(const Matrix3< FromT > &rhs)
Copy casting operator.
Definition: vmath.h:1858
Vector2i
class Vector2< int > Vector2i
Two dimensional Vector of ints.
Definition: vmath.h:574
Quaternion::rotMatrix
Matrix3< T > rotMatrix()
Converts quaternion into rotation matrix.
Definition: vmath.h:3223
Vector3::operator-
Vector3< T > operator-(T rhs) const
Subtraction operator.
Definition: vmath.h:867
Matrix4::operator=
Matrix4< T > & operator=(const Matrix4< T > &rhs)
Copy operator.
Definition: vmath.h:2603
Matrix3i
Matrix3< int > Matrix3i
Matrix 3x3 of int.
Definition: vmath.h:2106
Matrix3::at
const T & at(int x, int y) const
Get constant reference to element at position (x,y).
Definition: vmath.h:1812
Matrix4::operator=
Matrix4< T > & operator=(const Matrix4< FromT > &rhs)
Copy casting operator.
Definition: vmath.h:2614
Matrix3::operator=
Matrix3< T > & operator=(const Matrix3< T > &rhs)
Copy operator.
Definition: vmath.h:1847
Vector4::operator/=
Vector4< T > & operator/=(const Vector4< T > &rhs)
Division operator.
Definition: vmath.h:1362
Vector2::operator*
Vector2< T > operator*(const Vector2< T > &rhs) const
Multiplication operator.
Definition: vmath.h:303
Vector2f
class Vector2< float > Vector2f
Two dimensional Vector of floats.
Definition: vmath.h:570
Quaternion::Quaternion
Quaternion(const Quaternion< T > &q)
Copy constructor.
Definition: vmath.h:2973
epsilon
const double epsilon
Definition: vmath.h:143
Matrix4::fromColumnMajorArray
static Matrix4< T > fromColumnMajorArray(const FromT *arr)
Creates new matrix 4x4 from array that represents such matrix 4x4 as array of tightly packed elements...
Definition: vmath.h:2435
Vector3::z
T z
Third element of vector, alias for Z-coordinate.
Definition: vmath.h:635
Vector3::operator-
Vector3< T > operator-(const Vector3< T > &rhs) const
Subtraction operator.
Definition: vmath.h:764
Vector3::rotate
void rotate(T ax, T ay, T az)
Rotate vector around three axis.
Definition: vmath.h:1011
Matrix4::operator*
Vector4< T > operator*(const Vector4< T > &rhs) const
Multiplication operator.
Definition: vmath.h:2718
Vector3::operator*=
Vector3< T > & operator*=(T rhs)
Multiplication operator.
Definition: vmath.h:918
Vector4::lerp
Vector4< T > lerp(T fact, const Vector4< T > &r) const
Linear interpolation of two vectors.
Definition: vmath.h:1539
Quaternion::operator*
Quaternion< T > operator*(const Quaternion< T > &rhs) const
Multiplication operator.
Definition: vmath.h:3046
Matrix4::operator!=
bool operator!=(const Matrix4< T > &rhs) const
Inequality test operator.
Definition: vmath.h:2471
Vector4::operator/
Vector4< T > operator/(T rhs) const
Division operator.
Definition: vmath.h:1438
Matrix4::setScale
void setScale(T s)
Sets matrix uniform scale values.
Definition: vmath.h:2570
Vector2::s
T s
First element of vector, alias for S-coordinate.
Definition: vmath.h:171
Quaternion::toString
std::string toString() const
Gets string representation.
Definition: vmath.h:3338
Vector3
Class for three dimensional vector.
Definition: vmath.h:588
std::max
VEC2< T > max(const VEC2< T > &a, const VEC2< T > &b)
Gets vector containing maximal values of a and b coordinates.
Definition: vmath.h:3568
Matrix4::createTranslation
static Matrix4< T > createTranslation(T x, T y, T z, T w=1)
Creates translation matrix.
Definition: vmath.h:2225
Vector4::operator=
Vector4< T > operator=(const Vector4< T > &rhs)
Copy operator.
Definition: vmath.h:1219
Vector4::operator/
Vector4< T > operator/(const Vector4< T > &rhs) const
Division operator.
Definition: vmath.h:1314
Matrix3::at
T & at(int x, int y)
Get reference to element at position (x,y).
Definition: vmath.h:1800
Vector3::Vector3
Vector3(const Vector3< FromT > &src)
Copy casting constructor.
Definition: vmath.h:683
Vector2::operator-=
Vector2< T > & operator-=(const Vector2< T > &rhs)
Substraction operator.
Definition: vmath.h:332
Aabb3::point
Vector3< T > point(size_t i) const
Gets all 8 corner-points of bounding box.
Definition: vmath.h:3892
Matrix4::createRotationAroundAxis
static Matrix4< T > createRotationAroundAxis(T xDeg, T yDeg, T zDeg)
Creates rotation matrix by rotation around axis.
Definition: vmath.h:2177
Vector4::g
T g
Second element of vector, alias for G-coordinate.
Definition: vmath.h:1130
Vector4::Vector4
Vector4(const Vector3< FromT > &src, FromT w)
Definition: vmath.h:1210
Vector4::operator/=
Vector4< T > & operator/=(T rhs)
Division operator.
Definition: vmath.h:1486
Matrix4::operator*
Vector3< T > operator*(const Vector3< T > &rhs) const
Multiplication operator.
Definition: vmath.h:2731
Vector4::operator!=
bool operator!=(const Vector4< T > &rhs) const
Inequality test operator.
Definition: vmath.h:1390
Quaternion::operator=
Quaternion< T > & operator=(const Quaternion< FromT > &rhs)
Copy convert operator.
Definition: vmath.h:3025
Quaternion::transform
Matrix4< T > transform() const
Converts quaternion into transformation matrix.
Definition: vmath.h:3272
Vector3::operator/=
Vector3< T > & operator/=(T rhs)
Division operator.
Definition: vmath.h:930
Vector4::operator+
Vector4< T > operator+(T rhs) const
Addition operator.
Definition: vmath.h:1411
Vector3::dotProduct
T dotProduct(const Vector3< T > &rhs) const
Dot product of two vectors.
Definition: vmath.h:839
Vector3::operator=
Vector3< T > operator=(const Vector3< T > &rhs)
Copy operator.
Definition: vmath.h:693
Quaternion::normalize
void normalize()
Normalize quaternion.
Definition: vmath.h:3185
Vector4::normalize
void normalize()
Normalize vector.
Definition: vmath.h:1508
Vector4::operator-
Vector4< T > operator-() const
Unary negate operator.
Definition: vmath.h:1400
Matrix4::createOrtho
static Matrix4< T > createOrtho(T left, T right, T bottom, T top, T zNear, T zFar)
Creates OpenGL compatible orthographic projection matrix.
Definition: vmath.h:2366
Vector2::operator[]
T & operator[](int n)
Array access operator.
Definition: vmath.h:256
Matrix3::operator*
Matrix3< T > operator*(Matrix3< T > rhs) const
Multiplication operator.
Definition: vmath.h:1973
Vector4::Vector4
Vector4(const Vector4< FromT > &src)
Copy casting constructor.
Definition: vmath.h:1200
Quaternion::operator-
Quaternion< T > operator-(const Quaternion< T > &rhs) const
Subtraction operator.
Definition: vmath.h:3068
Vector2::operator==
bool operator==(const Vector2< T > &rhs) const
Equality test operator.
Definition: vmath.h:450
Quaternion::fromAxisRot
static Quaternion< T > fromAxisRot(Vector3< T > axis, float angleDeg)
Creates quaternion as rotation around axis.
Definition: vmath.h:3211
Matrix4::getTranslation
Vector3< T > getTranslation() const
Definition: vmath.h:2538
Vector4::operator+=
Vector4< T > & operator+=(T rhs)
Addition operator.
Definition: vmath.h:1447
Aabb3::Aabb3
Aabb3(SrcT x0, SrcT y0, SrcT z0, SrcT x1, SrcT y1, SrcT z1)
Constructs axes-aligned bounding-box form two corner points (x0, y0, z0) and (x1, y1...
Definition: vmath.h:3662
Matrix3::operator==
bool operator==(const Matrix3< T > &rhs) const
Equality test operator.
Definition: vmath.h:1774
Vector3::operator!=
bool operator!=(const Vector3< T > &rhs) const
Inequality test operator.
Definition: vmath.h:956
Vector2::operator*=
Vector2< T > & operator*=(const Vector2< T > &rhs)
Multiplication operator.
Definition: vmath.h:343
Vector4::operator+
Vector4< T > operator+(const Vector4< T > &rhs) const
Addition operator.
Definition: vmath.h:1287
Vector4::operator[]
T & operator[](int n)
Array access operator.
Definition: vmath.h:1249
Quaternion::Quaternion
Quaternion(T w_, const Vector3< T > &v_)
Creates quaternion object from real part w_ and complex part v_.
Definition: vmath.h:2992
Vector2::operator=
Vector2< T > & operator=(const Vector2< FromT > &rhs)
Copy casting operator.
Definition: vmath.h:232
Aabb3::invalidate
void invalidate()
Makes this bounding-box invalid.
Definition: vmath.h:3714
Matrix4::operator()
T & operator()(int i, int j)
Get reference to element at position (i,j), with math matrix notation.
Definition: vmath.h:2506
Vector4::operator-
Vector4< T > operator-(T rhs) const
Subtraction operator.
Definition: vmath.h:1420
Vector2::length
T length() const
Get length of vector.
Definition: vmath.h:480
Matrix4::toString
std::string toString() const
Gets string representation.
Definition: vmath.h:2926
Vector3::operator-=
Vector3< T > & operator-=(const Vector3< T > &rhs)
Subtraction operator.
Definition: vmath.h:803
Vector2::operator-
Vector2< T > operator-() const
Unary negate operator.
Definition: vmath.h:470
Matrix4::createLookAt
static Matrix4< T > createLookAt(const Vector3< T > &eyePos, const Vector3< T > &centerPos, const Vector3< T > &upDir)
Creates new view matrix to look from specified position eyePos to specified position centerPos...
Definition: vmath.h:2261
Vector4::operator*
Vector4< T > operator*(const Vector4< T > rhs) const
Multiplication operator.
Definition: vmath.h:1305
DEG2RAD
#define DEG2RAD(x)
Definition: vmath.h:140
Matrix4::operator/
Matrix4< T > operator/(T rhs) const
Division operator.
Definition: vmath.h:2705
Vector3i
Vector3< int > Vector3i
Three dimensional Vector of ints.
Definition: vmath.h:1093
Vector3::Vector3
Vector3(T nx, T ny, T nz)
Creates and sets to (x,y,z)
Definition: vmath.h:664
std::min
VEC2< T > min(const VEC2< T > &a, const VEC2< T > &b)
Gets vector containing minimal values of a and b coordinates.
Definition: vmath.h:3538
Vector3::g
T g
Second element of vector, alias for G-coordinate.
Definition: vmath.h:627
Quatf
Quaternion< float > Quatf
Definition: vmath.h:3503
Quaternion::operator~
Quaternion< T > operator~() const
Unary conjugate operator.
Definition: vmath.h:3156
Aabb3::size
Vector3< T > size() const
Gets diagonal size of bounding-box.
Definition: vmath.h:3874
Aabb3::intersection
Aabb3< T > intersection(const Aabb3< SrcT > &other) const
Gets result of intersection of this bounding-box with other bounding-box.
Definition: vmath.h:3840
Vector3::operator-=
Vector3< T > & operator-=(T rhs)
Subtraction operator.
Definition: vmath.h:906
Quaternion::fromMatrix
static Quaternion< T > fromMatrix(const Matrix3< T > &m)
Creates quaternion from rotation matrix.
Definition: vmath.h:3411
Aabb3::extent
Vector3< T > extent() const
Gets extent of bounding-box.
Definition: vmath.h:3867
Aabb3f
Aabb3< float > Aabb3f
Definition: vmath.h:4022
Aabb3::max
Vector3< T > max
Position of Max corner of bounding box.
Definition: vmath.h:3632