view include/Math.h @ 9:81d2aa42a860

Matrix22 working, but a few methods not yet written. I started thinking about refactoring again t omake things even cleaner in design, with less repetition.
author Eris Caffee <discordia@eldalin.com>
date Fri, 07 Oct 2011 11:08:38 -0500
parents 378862555189
children 398894c52b69
line source
1 #ifndef MATH_H_
2 #define MATH_H_
4 #include "Vector.h"
5 #include "Matrix.h"
7 namespace arda
8 {
10 ////////////////////////////////////////////////////////////////////////////////
11 // General notes:
12 //
13 // As much as possible is defined inline for performance.
14 // Using typedef'ed arrays for vectors and matrices necessitates a lot of
15 // redundant function names: scale2, scale3, scale4, for example.
16 // This is, however, perfectly in line with the implementation of OpenGL and
17 // allows the library to maintain optimal memory usage and performance. (I hope!)
18 // In any event, I think I could do far worse than to emulate the OpenGL
19 // working group - a group of people who have studied this for years and are
20 // experts in the field.
22 ////////////////////////////////////////////////////////////////////////////////
23 // Angles
25 namespace Angles
26 {
27 // This many decimals is enough to cover even IEEE quad precision (128 bit)
28 // reals. Run on PA-RISC lately? :)
29 const double PI = 3.14159265358979323846264338327950288;
30 }
32 ////////////////////////////////////////////////////////////////////////////////
33 // Mixed Vector / Matrix operations.
34 // * *=
35 // Defined for Vector * Matrix and Matrix * Vector, but these
36 // operators are not part of the class. They are defined independently.
38 // Vector * Matrix
39 template <typename T> inline Vector2<T>& operator*=(Vector2<T>& v, const Matrix22<T>& m)
40 {
41 Vector2<T> vres(0);
42 vres[0] = v[0]*m[0] + v[1]*m[1];
43 vres[1] = v[0]*m[2] + v[1]*m[3];
44 v = vres;
45 return v;
46 }
47 template <typename T> inline Vector2<T> operator*(const Vector2<T>& v, const Matrix22<T>& m)
48 { return Vector2<T>(v) *= m; }
50 // Matrix * Vector
51 // Note: can't define Matrix *= Vector since the result is a Vector.
52 template <typename T> inline Vector2<T> operator*(const Matrix22<T>& m, const Vector2<T>& v)
53 {
54 Vector2<T> vres(0);
55 vres[0] = m[0]*v[0] + m[2]*v[1];
56 vres[1] = m[1]*v[0] + m[3]*v[1];
57 return vres;
58 }
61 } // namespace arda
63 #endif