view include/Math.h @ 15:ea32c94fc495

General minor improvements. - Spelling corrections. - Made the top level constants like PI static. - Changed instances of literal 1 and 0 to T(1) and T(0)
author Eris Caffee <discordia@eldalin.com>
date Sun, 19 May 2013 20:17:16 -0500
parents c029247daf0e
children f49d8ba063ca
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 // Should these be #defines? I'm starting to think that they should be.
29 static long double const PI = 3.14159265358979323846264338327950288;
30 static long double const TWO_PI = 6.28318530717958647692528676655900577;
31 static long double const PI_DIV_180 = 0.01745329251994329576923690768488613;
32 static long double const INV_PI_DIV_180 = 57.29577951308232087679815481410517033;
34 //////////////////////////////////////////////////////////////////////////
35 // Mixed Vector / Matrix operations.
36 // * *=
37 // Defined for Vector * Matrix and Matrix * Vector, but these
38 // operators are not part of the class. They are defined independently.
39 //
41 // Vector2 * Matrix22
42 template <typename T>
43 inline Vector2<T>& operator*=(Vector2<T> & v,
44 Matrix22<T> const & m)
45 {
46 Vector2<T> vres(0);
47 vres[0] = v[0]*m[0] + v[1]*m[1];
48 vres[1] = v[0]*m[2] + v[1]*m[3];
49 v = vres;
50 return v;
51 }
53 template <typename T>
54 inline Vector2<T> operator*(Vector2<T> const & v,
55 Matrix22<T> const & m)
56 { return Vector2<T>(v) *= m; }
58 // Note: can't define Matrix *= Vector since the result is a Vector.
59 template <typename T>
60 inline Vector2<T> operator*(Matrix22<T> const & m,
61 Vector2<T> const & v)
62 {
63 Vector2<T> vres(0);
64 vres[0] = m[0]*v[0] + m[2]*v[1];
65 vres[1] = m[1]*v[0] + m[3]*v[1];
66 return vres;
67 }
69 ////////////////////////////////////////
70 // Vector3 * Matrix33
71 template <typename T>
72 inline Vector3<T>& operator*=(Vector3<T> & v,
73 Matrix33<T> const & m)
74 {
75 Vector3<T> vres(0);
76 vres[0] = v[0]*m[0] + v[1]*m[1] + v[2]*m[2];
77 vres[1] = v[0]*m[3] + v[1]*m[4] + v[2]*m[5];
78 vres[2] = v[0]*m[6] + v[1]*m[7] + v[2]*m[8];
79 v = vres;
80 return v;
81 }
83 template <typename T>
84 inline Vector3<T> operator*(Vector3<T> const & v,
85 Matrix33<T> const & m)
86 { return Vector3<T>(v) *= m; }
88 // Matrix * Vector
89 // Note: can't define Matrix *= Vector since the result is a Vector.
90 template <typename T>
91 inline Vector3<T> operator*(Matrix33<T> const & m,
92 Vector3<T> const & v)
93 {
94 Vector3<T> vres(0);
95 vres[0] = m[0]*v[0] + m[3]*v[1] + m[6]*v[2];
96 vres[1] = m[1]*v[0] + m[4]*v[1] + m[7]*v[2];
97 vres[2] = m[2]*v[0] + m[5]*v[1] + m[8]*v[2];
98 return vres;
99 }
101 ////////////////////////////////////////
102 // Vector4 * Matrix44
103 template <typename T>
104 inline Vector4<T>& operator*=(Vector4<T> & v,
105 Matrix44<T> const & m)
106 {
107 Vector4<T> vres(0);
108 vres[0] = v[0]*m[0] + v[1]*m[1] + v[2]*m[2] + v[3]*m[3];
109 vres[1] = v[0]*m[4] + v[1]*m[5] + v[2]*m[6] + v[3]*m[7];
110 vres[2] = v[0]*m[8] + v[1]*m[9] + v[2]*m[10] + v[3]*m[11];
111 vres[3] = v[0]*m[12] + v[1]*m[13] + v[2]*m[14] + v[3]*m[15];
112 v = vres;
113 return v;
114 }
116 template <typename T>
117 inline Vector4<T> operator*(Vector4<T> const & v,
118 Matrix44<T> const & m)
119 { return Vector4<T>(v) *= m; }
121 // Matrix * Vector
122 // Note: can't define Matrix *= Vector since the result is a Vector.
123 template <typename T>
124 inline Vector4<T> operator*(Matrix44<T> const & m,
125 Vector4<T> const & v)
126 {
127 Vector4<T> vres(0);
128 vres[0] = m[0]*v[0] + m[4]*v[1] + m[8]*v[2] + m[12]*v[3];
129 vres[1] = m[1]*v[0] + m[5]*v[1] + m[9]*v[2] + m[13]*v[3];
130 vres[2] = m[2]*v[0] + m[6]*v[1] + m[10]*v[2] + m[14]*v[3];
131 vres[3] = m[3]*v[0] + m[7]*v[1] + m[11]*v[2] + m[15]*v[3];
132 return vres;
133 }
135 //////////////////////////////////////////////////////////////////////////
137 inline double deg_to_rad(double deg) { return deg * PI_DIV_180; };
138 inline double rad_to_deg(double rad) { return rad * INV_PI_DIV_180; };
140 } // namespace Math
142 } // namespace arda
145 #endif