view include/Math.h @ 14:c029247daf0e

Started new header Transform.h to collect functions that produce transformation matrices.
author Eris Caffee <discordia@eldalin.com>
date Sun, 12 May 2013 03:05:37 -0500
parents e00321d11fe1
children ea32c94fc495
line source
1 #ifndef MATH_H_
2 #define MATH_H_
4 #include "Vector.h"
5 #include "Matrix.h"
6 #include "Transform.h"
8 ///////////////////////////////////////////////////////////////////////////////////
9 // Notes for improvement
10 //
11 // The angle related constants (PI, 2PI, etc) and functions (deg_to_rad, rad_to_deg)
12 // could be made faster by using #define's. In fact, this would turn them into
13 // compile time operations. But it would also put them, effectively, in the
14 // global namespace. Do I want that? For now I'm leaving things as const
15 // variables and inline functions. I can profile things later to see if changes
16 // are really needed.
18 namespace arda
19 {
21 ////////////////////////////////////////////////////////////////////////////////
22 // Angles
24 namespace Math
25 {
26 // This many decimals is enough to cover even IEEE quad precision (128 bit)
27 // reals. Run on PA-RISC lately? :)
28 long double const PI = 3.14159265358979323846264338327950288;
29 long double const TWO_PI = 6.28318530717958647692528676655900577;
30 long double const PI_DIV_180 = 0.01745329251994329576923690768488613;
31 long double const INV_PI_DIV_180 = 57.29577951308232087679815481410517033;
33 //////////////////////////////////////////////////////////////////////////
34 // Mixed Vector / Matrix operations.
35 // * *=
36 // Defined for Vector * Matrix and Matrix * Vector, but these
37 // operators are not part of the class. They are defined independently.
38 //
40 // Vector2 * Matrix22
41 template <typename T>
42 inline Vector2<T>& operator*=(Vector2<T> & v,
43 Matrix22<T> const & m)
44 {
45 Vector2<T> vres(0);
46 vres[0] = v[0]*m[0] + v[1]*m[1];
47 vres[1] = v[0]*m[2] + v[1]*m[3];
48 v = vres;
49 return v;
50 }
52 template <typename T>
53 inline Vector2<T> operator*(Vector2<T> const & v,
54 Matrix22<T> const & m)
55 { return Vector2<T>(v) *= m; }
57 // Note: can't define Matrix *= Vector since the result is a Vector.
58 template <typename T>
59 inline Vector2<T> operator*(Matrix22<T> const & m,
60 Vector2<T> const & v)
61 {
62 Vector2<T> vres(0);
63 vres[0] = m[0]*v[0] + m[2]*v[1];
64 vres[1] = m[1]*v[0] + m[3]*v[1];
65 return vres;
66 }
68 ////////////////////////////////////////
69 // Vector3 * Matrix33
70 template <typename T>
71 inline Vector3<T>& operator*=(Vector3<T> & v,
72 Matrix33<T> const & m)
73 {
74 Vector3<T> vres(0);
75 vres[0] = v[0]*m[0] + v[1]*m[1] + v[2]*m[2];
76 vres[1] = v[0]*m[3] + v[1]*m[4] + v[2]*m[5];
77 vres[2] = v[0]*m[6] + v[1]*m[7] + v[2]*m[8];
78 v = vres;
79 return v;
80 }
82 template <typename T>
83 inline Vector3<T> operator*(Vector3<T> const & v,
84 Matrix33<T> const & m)
85 { return Vector3<T>(v) *= m; }
87 // Matrix * Vector
88 // Note: can't define Matrix *= Vector since the result is a Vector.
89 template <typename T>
90 inline Vector3<T> operator*(Matrix33<T> const & m,
91 Vector3<T> const & v)
92 {
93 Vector3<T> vres(0);
94 vres[0] = m[0]*v[0] + m[3]*v[1] + m[6]*v[2];
95 vres[1] = m[1]*v[0] + m[4]*v[1] + m[7]*v[2];
96 vres[2] = m[2]*v[0] + m[5]*v[1] + m[8]*v[2];
97 return vres;
98 }
100 ////////////////////////////////////////
101 // Vector4 * Matrix44
102 template <typename T>
103 inline Vector4<T>& operator*=(Vector4<T> & v,
104 Matrix44<T> const & m)
105 {
106 Vector4<T> vres(0);
107 vres[0] = v[0]*m[0] + v[1]*m[1] + v[2]*m[2] + v[3]*m[3];
108 vres[1] = v[0]*m[4] + v[1]*m[5] + v[2]*m[6] + v[3]*m[7];
109 vres[2] = v[0]*m[8] + v[1]*m[9] + v[2]*m[10] + v[3]*m[11];
110 vres[3] = v[0]*m[12] + v[1]*m[13] + v[2]*m[14] + v[3]*m[15];
111 v = vres;
112 return v;
113 }
115 template <typename T>
116 inline Vector4<T> operator*(Vector4<T> const & v,
117 Matrix44<T> const & m)
118 { return Vector4<T>(v) *= m; }
120 // Matrix * Vector
121 // Note: can't define Matrix *= Vector since the result is a Vector.
122 template <typename T>
123 inline Vector4<T> operator*(Matrix44<T> const & m,
124 Vector4<T> const & v)
125 {
126 Vector4<T> vres(0);
127 vres[0] = m[0]*v[0] + m[4]*v[1] + m[8]*v[2] + m[12]*v[3];
128 vres[1] = m[1]*v[0] + m[5]*v[1] + m[9]*v[2] + m[13]*v[3];
129 vres[2] = m[2]*v[0] + m[6]*v[1] + m[10]*v[2] + m[14]*v[3];
130 vres[3] = m[3]*v[0] + m[7]*v[1] + m[11]*v[2] + m[15]*v[3];
131 return vres;
132 }
134 //////////////////////////////////////////////////////////////////////////
136 inline double deg_to_rad(double deg) { return deg * PI_DIV_180; };
137 inline double rad_to_deg(double rad) { return rad * INV_PI_DIV_180; };
139 } // namespace Math
141 } // namespace arda
144 #endif