Browse Source

Added Matrix and Vector3d into utils

Thomas Buck 11 years ago
parent
commit
cf3e00c3d6
4 changed files with 902 additions and 0 deletions
  1. 201
    0
      include/utils/Matrix.h
  2. 160
    0
      include/utils/Vector3d.h
  3. 406
    0
      src/utils/Matrix.cpp
  4. 135
    0
      src/utils/Vector3d.cpp

+ 201
- 0
include/utils/Matrix.h View File

@@ -0,0 +1,201 @@
1
+/*!
2
+ * \file include/utils/Matrix.h
3
+ * \brief 3D Matrix
4
+ *
5
+ * \author Mongoose
6
+ */
7
+
8
+#ifndef _UTILS_MATRIX_H_
9
+#define _UTILS_MATRIX_H_
10
+
11
+#include "utils/math.h"
12
+#include "utils/Quaternion.h"
13
+#include "utils/Vector3d.h"
14
+
15
+
16
+/*!
17
+ * \brief 3D Matrix
18
+ *
19
+ * Multidim map for row order encoding
20
+ *
21
+ *     ///////////////////////////////////////////////
22
+ *     // 0,0 - 0;   0,1 - 1;   0,2 - 2;   0,3 - 3  //
23
+ *     // 1,0 - 4;   1,1 - 5;   1,2 - 6;   1,3 - 7  //
24
+ *     // 2,0 - 8;   2,1 - 9;   2,2 - 10;  2,3 - 11 //
25
+ *     // 3,0 - 12;  3,1 - 13;  3,2 - 14;  3,3 - 15 //
26
+ *     ///////////////////////////////////////////////
27
+ *
28
+ * Multidim map for column order encoding
29
+ *
30
+ *     ///////////////////////////////////////////////
31
+ *     // 0,0 - 0;   0,1 - 4;   0,2 - 8;   0,3 - 12 //
32
+ *     // 1,0 - 1;   1,1 - 5;   1,2 - 9;   1,3 - 13 //
33
+ *     // 2,0 - 2;   2,1 - 6;   2,2 - 10;  2,3 - 14 //
34
+ *     // 3,0 - 3;   3,1 - 7;   3,2 - 11;  3,3 - 15 //
35
+ *     ///////////////////////////////////////////////
36
+ */
37
+class Matrix {
38
+public:
39
+
40
+    /*!
41
+     * \brief Constructs an object of Matrix
42
+     */
43
+    Matrix();
44
+
45
+    /*!
46
+     * \brief Constructs an object of Matrix
47
+     * \param mat Matrix as data source
48
+     */
49
+    Matrix(matrix_t mat);
50
+
51
+    /*!
52
+     * \brief Constructs an object of Matrix
53
+     * \param q Converts and assigns the Quaternion to the Matrix
54
+     */
55
+    Matrix(Quaternion &q);
56
+
57
+    /*!
58
+     * \brief Returns this matrix copy
59
+     * \param mat target
60
+     */
61
+    void getMatrix(matrix_t mat);
62
+
63
+    /*!
64
+     * \brief Returns this matrix transposed
65
+     * \param mat target
66
+     */
67
+    void getTransposeMatrix(matrix_t mat);
68
+
69
+    /*!
70
+     * \brief Returns this matrix inverted
71
+     * \param mat target
72
+     */
73
+    bool getInvert(matrix_t mat);
74
+
75
+    /*!
76
+     * \brief Multiplies two matrices
77
+     * \param a first matrix
78
+     * \param b second matrix
79
+     * \returns resultant matrix
80
+     */
81
+    static Matrix multiply(const Matrix &a, const Matrix &b);
82
+
83
+    /*!
84
+     * \brief Multiplies v vector and this matrix
85
+     * \param v vector
86
+     * \param result where the result will be stored, may be same as v
87
+     */
88
+    void multiply4v(vec4_t v, vec4_t result);
89
+
90
+    /*!
91
+     * \brief Multiplies v vector and this matrix
92
+     * \param v vector
93
+     * \param result where the result will be stored, may be same as v
94
+     */
95
+    void multiply3v(vec3_t v, vec3_t result);
96
+
97
+    /*!
98
+     * \brief Prints matrix values to stdout
99
+     */
100
+    void print();
101
+
102
+    /*!
103
+     * \brief Is this matrix the identity matrix?
104
+     * \returns true if it is identity, false otherwise
105
+     */
106
+    bool isIdentity();
107
+
108
+    /*!
109
+     * \brief Multiplies a and this matrix
110
+     * \param a matrix to multiply with
111
+     * \returns resultant matrix
112
+     */
113
+    Matrix operator *(const Matrix &a);
114
+
115
+    /*!
116
+     * \brief Multiply vector by this matrix
117
+     * \param v Vector to multiply with
118
+     * \returns resultant vector (mult)
119
+     */
120
+    Vector3d operator *(Vector3d v);
121
+
122
+    /*!
123
+     * \brief Sets to identity matrix
124
+     */
125
+    void setIdentity();
126
+
127
+    /*!
128
+     * \brief S et the matrix
129
+     * \fixme dangerous, scary, boo!
130
+     * \param mat new matrix
131
+     */
132
+    void setMatrix(matrix_t mat);
133
+
134
+    /*!
135
+     * \brief Rotate object in 3D space
136
+     * \param x x rotation in radians
137
+     * \param y y rotation in radians
138
+     * \param z z rotation in radians
139
+     */
140
+    void rotate(vec_t x, vec_t y, vec_t z);
141
+
142
+    /*!
143
+     * \brief Rotate object in 3D space
144
+     * \param xyz rotation in radians
145
+     */
146
+    void rotate(const vec_t *xyz);
147
+
148
+    /*!
149
+     * \brief Scale object in 3D space
150
+     * \param x x scaling
151
+     * \param y y scaling
152
+     * \param z z scaling
153
+     */
154
+    void scale(vec_t x, vec_t y, vec_t z);
155
+
156
+    /*!
157
+     * \brief Scale object in 3D space
158
+     * \param xyz scaling factors
159
+     */
160
+    void scale(const vec_t *xyz);
161
+
162
+    /*!
163
+     * \brief Translate (move) object in 3D space
164
+     * \param x x translation
165
+     * \param y y translation
166
+     * \param z z translation
167
+     */
168
+    void translate(vec_t x, vec_t y, vec_t z);
169
+
170
+    /*!
171
+     * \brief Translate (move) object in 3D space
172
+     * \param xyz translations
173
+     */
174
+    void translate(const vec_t *xyz);
175
+
176
+    /*!
177
+     * \brief Transpose this matrix
178
+     */
179
+    void transpose();
180
+
181
+    matrix_t mMatrix; //!< Data model, moved public for faster external renderer feedback use
182
+
183
+private:
184
+
185
+    /*!
186
+     * \brief Copys value from source to dest
187
+     * \param source source
188
+     * \param dest destination
189
+     */
190
+    static void copy(matrix_t source, matrix_t dest);
191
+
192
+    /*!
193
+     * \brief Multiplies matrices a and b. Neither a or b is also the result.
194
+     * \param a first matrix
195
+     * \param b second matrix
196
+     * \param result wil be set to resultant matrix value
197
+     */
198
+    static void multiply(const matrix_t a, const matrix_t b, matrix_t result);
199
+};
200
+
201
+#endif

+ 160
- 0
include/utils/Vector3d.h View File

@@ -0,0 +1,160 @@
1
+/*!
2
+ * \file include/utils/Vector3d.h
3
+ * \brief 3D Math vector
4
+ *
5
+ * \author Mongoose
6
+ */
7
+
8
+#ifndef _UTILS_VECTOR3D_H_
9
+#define _UTILS_VECTOR3D_H_
10
+
11
+#include "utils/math.h"
12
+
13
+/*!
14
+ * \brief 3D Math Vector
15
+ */
16
+class Vector3d {
17
+public:
18
+
19
+    /*!
20
+     * \brief Constructs an object of Vector3d
21
+     */
22
+    Vector3d();
23
+
24
+    /*!
25
+     * \brief Constructs an object of Vector3d
26
+     * \param v data to load into new Vector3d
27
+     */
28
+    Vector3d(vec3_t v);
29
+
30
+    /*!
31
+     * \brief Constructs an object of Vector3d
32
+     * \param x X part of new Vector3d
33
+     * \param y Y part of new Vector3d
34
+     * \param z Z part of new Vector3d
35
+     */
36
+    Vector3d(vec_t x, vec_t y, vec_t z);
37
+
38
+    /*!
39
+     * \brief Constructs an object of Vector3d
40
+     * \param v contents of new Vector3d
41
+     */
42
+    Vector3d(const Vector3d &v);
43
+
44
+    /*!
45
+     * \brief Calculate dot product
46
+     * \param u first argument
47
+     * \param v second argument
48
+     * \returns dot product of u and v vectors
49
+     */
50
+    static vec_t dot(const Vector3d &u, const Vector3d &v);
51
+
52
+    /*!
53
+     * \brief Calculate cross product
54
+     * \param u first argument
55
+     * \param v second argument
56
+     * \returns cross product of u and v vectors
57
+     */
58
+    static Vector3d cross(const Vector3d &u, const Vector3d &v);
59
+
60
+    /*!
61
+     * \brief Get Magnitude
62
+     * \returns magnitude of this vector
63
+     */
64
+    vec_t magnitude();
65
+
66
+    /*!
67
+     * \brief Normalize
68
+     * \returns normalized copy of this vector
69
+     */
70
+    Vector3d unit();
71
+
72
+    /*!
73
+     * \brief Get the Zero vector
74
+     * \returns (0, 0, 0) vector
75
+     */
76
+    static Vector3d zeroVector();
77
+
78
+    /*!
79
+     * \brief Add to this vector
80
+     * \param v addend
81
+     * \returns a vector = this vector + v
82
+     */
83
+    Vector3d operator +(const Vector3d &v);
84
+
85
+    /*!
86
+     * \brief Subtract from this vector
87
+     * \param v subtrahend
88
+     * \returns a vector = this vector - v
89
+     */
90
+    Vector3d operator -(const Vector3d &v);
91
+
92
+    /*!
93
+     * \brief Negate this vector
94
+     * \returns a copy of this vector, negated
95
+     */
96
+    Vector3d operator -();
97
+
98
+    /*!
99
+     * \brief Scale this vector
100
+     * \param s scaling factor
101
+     * \returns this vector multiplied with s
102
+     */
103
+    Vector3d operator *(vec_t s);
104
+
105
+    /*!
106
+     * \brief Scale this vactor
107
+     * \param s inverse scaling factor
108
+     * \returns this vector divided by s
109
+     */
110
+    Vector3d operator /(vec_t s);
111
+
112
+    /*!
113
+     * \brief Dot product this vector
114
+     * \param v second vector for dot product
115
+     * \returns dot product of V by this vector
116
+     */
117
+    vec_t operator *(const Vector3d &v);
118
+
119
+    /*!
120
+     * \brief Normalizes this vector
121
+     */
122
+    void normalize();
123
+
124
+    /*!
125
+     * \brief Set this vector to Zero (0, 0, 0)
126
+     */
127
+    void zero();
128
+
129
+    /*!
130
+     * \brief Set this vector
131
+     * \param v what this vector will be set to
132
+     * \returns this vector, now equal to v
133
+     */
134
+    Vector3d &operator =(const Vector3d &v);
135
+
136
+    /*!
137
+     * \brief Add to this vector, in place
138
+     * \param v what will be added to this vector
139
+     * \returns this vector, with v added
140
+     */
141
+    Vector3d &operator +=(const Vector3d &v);
142
+
143
+    /*!
144
+     * \brief Subtract from this vector, in place
145
+     * \param v what will be subtracted from this vector
146
+     * \returns this vector, with v subtracted
147
+     */
148
+    Vector3d &operator -=(const Vector3d &v);
149
+
150
+    /*!
151
+     * \brief Scale this vector, in place
152
+     * \param s scaling factor
153
+     * \returns this vactor multiplied by s
154
+     */
155
+    Vector3d &operator *=(vec_t s);
156
+
157
+    vec3_t mVec; //!< Vector data
158
+};
159
+
160
+#endif

+ 406
- 0
src/utils/Matrix.cpp View File

@@ -0,0 +1,406 @@
1
+/*!
2
+ * \file src/utils/Matrix.cpp
3
+ * \brief 3D Matrix
4
+ *
5
+ * \author Mongoose
6
+ */
7
+
8
+#include <stdio.h>
9
+#include <math.h>
10
+
11
+#include "utils/Matrix.h"
12
+
13
+Matrix::Matrix() {
14
+    setIdentity();
15
+}
16
+
17
+Matrix::Matrix(matrix_t m) {
18
+    setMatrix(m);
19
+}
20
+
21
+Matrix::Matrix(Quaternion &q) {
22
+    matrix_t m;
23
+    q.getMatrix(m);
24
+    setMatrix(m);
25
+}
26
+
27
+bool Matrix::getInvert(matrix_t out) {
28
+    matrix_t m;
29
+
30
+#ifdef COLUMN_ORDER
31
+    getMatrix(m);
32
+#else
33
+    getTransposeMatrix(m);
34
+#endif
35
+
36
+    /* Mongoose: This code was from a Jeff Lander tutorial which was based
37
+       on MESA GL's InvertMatrix */
38
+
39
+    /* NB. OpenGL Matrices are COLUMN major. */
40
+#define SWAP_ROWS(a, b) { float *_tmp = a; (a)=(b); (b)=_tmp; }
41
+#define MAT(m,r,c) (m)[(c)*4+(r)]
42
+
43
+    float wtmp[4][8];
44
+    float m0, m1, m2, m3, s;
45
+    float *r0, *r1, *r2, *r3;
46
+
47
+    r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
48
+
49
+    r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
50
+    r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
51
+    r0[4] = 1.0f, r0[5] = r0[6] = r0[7] = 0.0f,
52
+
53
+    r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
54
+    r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
55
+    r1[5] = 1.0f, r1[4] = r1[6] = r1[7] = 0.0f,
56
+
57
+    r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
58
+    r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
59
+    r2[6] = 1.0f, r2[4] = r2[5] = r2[7] = 0.0f,
60
+
61
+    r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
62
+    r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
63
+    r3[7] = 1.0f, r3[4] = r3[5] = r3[6] = 0.0f;
64
+
65
+    /* choose pivot - or die */
66
+    if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
67
+    if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
68
+    if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
69
+    if (0.0f == r0[0])  return false;
70
+
71
+    /* eliminate first variable     */
72
+    m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
73
+    s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
74
+    s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
75
+    s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
76
+    s = r0[4];
77
+    if (s != 0.0f) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
78
+    s = r0[5];
79
+    if (s != 0.0f) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
80
+    s = r0[6];
81
+    if (s != 0.0f) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
82
+    s = r0[7];
83
+    if (s != 0.0f) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
84
+
85
+    /* choose pivot - or die */
86
+    if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
87
+    if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
88
+    if (0.0f == r1[1])  return false;
89
+
90
+    /* eliminate second variable */
91
+    m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
92
+    r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
93
+    r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
94
+    s = r1[4]; if (0.0f != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
95
+    s = r1[5]; if (0.0f != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
96
+    s = r1[6]; if (0.0f != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
97
+    s = r1[7]; if (0.0f != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
98
+
99
+    /* choose pivot - or die */
100
+    if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
101
+    if (0.0f == r2[2])  return false;
102
+
103
+    /* eliminate third variable */
104
+    m3 = r3[2]/r2[2];
105
+    r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
106
+    r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
107
+    r3[7] -= m3 * r2[7];
108
+
109
+    /* last check */
110
+    if (0.0f == r3[3]) return false;
111
+
112
+    s = 1.0f/r3[3];              /* now back substitute row 3 */
113
+    r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
114
+
115
+    m2 = r2[3];                 /* now back substitute row 2 */
116
+    s  = 1.0f/r2[2];
117
+    r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
118
+    r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
119
+    m1 = r1[3];
120
+    r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
121
+    r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
122
+    m0 = r0[3];
123
+    r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
124
+    r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
125
+
126
+    m1 = r1[2];                 /* now back substitute row 1 */
127
+    s  = 1.0f/r1[1];
128
+    r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
129
+    r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
130
+    m0 = r0[2];
131
+    r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
132
+    r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
133
+
134
+    m0 = r0[1];                 /* now back substitute row 0 */
135
+    s  = 1.0f/r0[0];
136
+    r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
137
+    r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
138
+
139
+    MAT(out,0,0) = r0[4];
140
+    MAT(out,0,1) = r0[5], MAT(out,0,2) = r0[6];
141
+    MAT(out,0,3) = r0[7], MAT(out,1,0) = r1[4];
142
+    MAT(out,1,1) = r1[5], MAT(out,1,2) = r1[6];
143
+    MAT(out,1,3) = r1[7], MAT(out,2,0) = r2[4];
144
+    MAT(out,2,1) = r2[5], MAT(out,2,2) = r2[6];
145
+    MAT(out,2,3) = r2[7], MAT(out,3,0) = r3[4];
146
+    MAT(out,3,1) = r3[5], MAT(out,3,2) = r3[6];
147
+    MAT(out,3,3) = r3[7];
148
+
149
+    return true;
150
+#undef MAT
151
+#undef SWAP_ROWS
152
+}
153
+
154
+void Matrix::getMatrix(matrix_t mat) {
155
+    copy(mMatrix, mat);
156
+}
157
+
158
+void Matrix::getTransposeMatrix(matrix_t m) {
159
+    m[ 0]= mMatrix[0]; m[ 1]= mMatrix[4]; m[ 2]= mMatrix[ 8]; m[ 3]=mMatrix[12];
160
+    m[ 4]= mMatrix[1]; m[ 5]= mMatrix[5]; m[ 6]= mMatrix[ 9]; m[ 7]=mMatrix[13];
161
+    m[ 8]= mMatrix[2]; m[ 9]= mMatrix[6]; m[10]= mMatrix[10]; m[11]=mMatrix[14];
162
+    m[12]= mMatrix[3]; m[13]= mMatrix[7]; m[14]= mMatrix[11]; m[15]=mMatrix[15];
163
+}
164
+
165
+Matrix Matrix::multiply(const Matrix &a, const Matrix &b) {
166
+    Matrix c;
167
+    multiply(a.mMatrix, b.mMatrix, c.mMatrix);
168
+    return c;
169
+}
170
+
171
+Matrix Matrix::operator *(const Matrix &a) {
172
+    return multiply(a, *this);
173
+}
174
+
175
+Vector3d Matrix::operator *(Vector3d v) {
176
+    vec_t x = v.mVec[0], y = v.mVec[1], z = v.mVec[2];
177
+
178
+#ifdef COLUMN_ORDER
179
+    return Vector3d(mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z + mMatrix[12],
180
+            mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z + mMatrix[13],
181
+            mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z + mMatrix[14]);
182
+#else
183
+    return Vector3d(mMatrix[0]*x + mMatrix[1]*y + mMatrix[ 2]*z + mMatrix[ 3],
184
+            mMatrix[4]*x + mMatrix[5]*y + mMatrix[ 6]*z + mMatrix[ 7],
185
+            mMatrix[8]*x + mMatrix[9]*y + mMatrix[10]*z + mMatrix[11]);
186
+#endif
187
+}
188
+
189
+void Matrix::multiply3v(vec3_t v, vec3_t result) {
190
+    vec_t x = v[0], y = v[1], z = v[2];
191
+
192
+    result[0] = mMatrix[0]*x + mMatrix[1]*y + mMatrix[ 2]*z + mMatrix[ 3];
193
+    result[1] = mMatrix[4]*x + mMatrix[5]*y + mMatrix[ 6]*z + mMatrix[ 7];
194
+    result[2] = mMatrix[8]*x + mMatrix[9]*y + mMatrix[10]*z + mMatrix[11];
195
+}
196
+
197
+void Matrix::multiply4v(vec4_t v, vec4_t result) {
198
+    vec_t x = v[0], y = v[1], z = v[2], w = v[3];
199
+
200
+    result[0] = mMatrix[ 0]*x + mMatrix[ 1]*y + mMatrix[ 2]*z + mMatrix[ 3]*w;
201
+    result[1] = mMatrix[ 4]*x + mMatrix[ 5]*y + mMatrix[ 6]*z + mMatrix[ 7]*w;
202
+    result[2] = mMatrix[ 8]*x + mMatrix[ 9]*y + mMatrix[10]*z + mMatrix[11]*w;
203
+    result[3] = mMatrix[12]*x + mMatrix[13]*y + mMatrix[14]*z + mMatrix[15]*w;
204
+}
205
+
206
+void Matrix::print() {
207
+    printf("{\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n}\n",
208
+#ifdef COLUMN_ORDER
209
+            mMatrix[0], mMatrix[4], mMatrix[ 8], mMatrix[12],
210
+            mMatrix[1], mMatrix[5], mMatrix[ 9], mMatrix[13],
211
+            mMatrix[2], mMatrix[6], mMatrix[10], mMatrix[14],
212
+            mMatrix[3], mMatrix[7], mMatrix[11], mMatrix[15]);
213
+#else
214
+            mMatrix[ 0], mMatrix[ 1], mMatrix[ 2], mMatrix[ 3],
215
+            mMatrix[ 4], mMatrix[ 5], mMatrix[ 6], mMatrix[ 7],
216
+            mMatrix[ 8], mMatrix[ 9], mMatrix[10], mMatrix[11],
217
+            mMatrix[12], mMatrix[13], mMatrix[14], mMatrix[15]);
218
+#endif
219
+}
220
+
221
+bool Matrix::isIdentity() {
222
+    // Hhhmm... floating point using direct comparisons
223
+    /*
224
+    if (mMatrix[ 0] == 1 && mMatrix[ 1] == 0 && mMatrix[ 2] == 0 &&
225
+            mMatrix[ 3] == 0 &&    mMatrix[ 4] == 0 && mMatrix[ 5] == 1 &&
226
+            mMatrix[ 6] == 0 && mMatrix[ 7] == 0 && mMatrix[ 8] == 0 &&
227
+            mMatrix[ 9] == 0 && mMatrix[10] == 1 && mMatrix[11] == 0 &&
228
+            mMatrix[12] == 0 && mMatrix[13] == 0 && mMatrix[14] == 0 &&
229
+            mMatrix[15] == 1)
230
+        return true;
231
+    */
232
+    if (equalEpsilon(mMatrix[ 0], 1.0) && equalEpsilon(mMatrix[ 1], 0.0) && equalEpsilon(mMatrix[ 2], 0.0) &&
233
+        equalEpsilon(mMatrix[ 3], 0.0) && equalEpsilon(mMatrix[ 4], 0.0) && equalEpsilon(mMatrix[ 5], 1.0) &&
234
+        equalEpsilon(mMatrix[ 6], 0.0) && equalEpsilon(mMatrix[ 7], 0.0) && equalEpsilon(mMatrix[ 8], 0.0) &&
235
+        equalEpsilon(mMatrix[ 9], 0.0) && equalEpsilon(mMatrix[10], 1.0) && equalEpsilon(mMatrix[11], 0.0) &&
236
+        equalEpsilon(mMatrix[12], 0.0) && equalEpsilon(mMatrix[13], 0.0) && equalEpsilon(mMatrix[14], 0.0) &&
237
+        equalEpsilon(mMatrix[15], 1.0))
238
+        return true;
239
+
240
+    return false;
241
+}
242
+
243
+void Matrix::setMatrix(matrix_t mat) {
244
+    copy(mat, mMatrix);
245
+}
246
+
247
+void Matrix::setIdentity() {
248
+    mMatrix[ 0] = 1; mMatrix[ 1] = 0; mMatrix[ 2] = 0; mMatrix[ 3] = 0;
249
+    mMatrix[ 4] = 0; mMatrix[ 5] = 1; mMatrix[ 6] = 0; mMatrix[ 7] = 0;
250
+    mMatrix[ 8] = 0; mMatrix[ 9] = 0; mMatrix[10] = 1; mMatrix[11] = 0;
251
+    mMatrix[12] = 0; mMatrix[13] = 0; mMatrix[14] = 0; mMatrix[15] = 1;
252
+}
253
+
254
+void Matrix::scale(const vec_t *xyz) {
255
+    scale(xyz[0], xyz[1], xyz[2]);
256
+}
257
+
258
+void Matrix::scale(vec_t sx, vec_t sy, vec_t sz) {
259
+    matrix_t smatrix;
260
+    matrix_t tmp;
261
+
262
+    smatrix[ 0] = sx; smatrix[ 1] = 0;  smatrix[ 2] = 0;  smatrix[ 3] = 0;
263
+    smatrix[ 4] = 0;  smatrix[ 5] = sy; smatrix[ 6] = 0;  smatrix[ 7] = 0;
264
+    smatrix[ 8] = 0;  smatrix[ 9] = 0;  smatrix[10] = sz; smatrix[11] = 0;
265
+    smatrix[12] = 0;  smatrix[13] = 0;  smatrix[14] = 0;  smatrix[15] = 1;
266
+
267
+    copy(mMatrix, tmp);
268
+    multiply(tmp, smatrix, mMatrix);
269
+}
270
+
271
+void Matrix::rotate(const vec_t *xyz) {
272
+    rotate(xyz[0], xyz[1], xyz[2]);
273
+}
274
+
275
+void Matrix::rotate(vec_t ax, vec_t ay, vec_t az) {
276
+    matrix_t xmat, ymat, zmat, tmp, tmp2;
277
+
278
+    xmat[ 0]=1;         xmat[ 1]=0;         xmat[ 2]=0;         xmat[ 3]=0;
279
+    xmat[ 4]=0;         xmat[ 5]=cosf(ax);  xmat[ 6]=sinf(ax);  xmat[ 7]=0;
280
+    xmat[ 8]=0;         xmat[ 9]=-sinf(ax); xmat[10]=cosf(ax);  xmat[11]=0;
281
+    xmat[12]=0;         xmat[13]=0;         xmat[14]=0;         xmat[15]=1;
282
+
283
+    ymat[ 0]=cosf(ay);  ymat[ 1]=0;         ymat[ 2]=-sinf(ay); ymat[ 3]=0;
284
+    ymat[ 4]=0;         ymat[ 5]=1;         ymat[ 6]=0;         ymat[ 7]=0;
285
+    ymat[ 8]=sinf(ay);  ymat[ 9]=0;         ymat[10]=cosf(ay);  ymat[11]=0;
286
+    ymat[12]=0;         ymat[13]=0;         ymat[14]=0;         ymat[15]=1;
287
+
288
+    zmat[ 0]=cosf(az);  zmat[ 1]=sinf(az);  zmat[ 2]=0;         zmat[ 3]=0;
289
+    zmat[ 4]=-sinf(az); zmat[ 5]=cosf(az);  zmat[ 6]=0;         zmat[ 7]=0;
290
+    zmat[ 8]=0;         zmat[ 9]=0;         zmat[10]=1;         zmat[11]=0;
291
+    zmat[12]=0;         zmat[13]=0;         zmat[14]=0;         zmat[15]=1;
292
+
293
+    multiply(mMatrix, ymat, tmp);
294
+    multiply(tmp, xmat, tmp2);
295
+    multiply(tmp2, zmat, mMatrix);
296
+}
297
+
298
+void Matrix::translate(const vec_t *xyz) {
299
+    translate(xyz[0], xyz[1], xyz[2]);
300
+}
301
+
302
+void Matrix::translate(vec_t tx, vec_t ty, vec_t tz) {
303
+    matrix_t tmat, tmp;
304
+
305
+    tmat[ 0]=1;  tmat[ 1]=0;  tmat[ 2]=0;  tmat[ 3]=0;
306
+    tmat[ 4]=0;  tmat[ 5]=1;  tmat[ 6]=0;  tmat[ 7]=0;
307
+    tmat[ 8]=0;  tmat[ 9]=0;  tmat[10]=1;  tmat[11]=0;
308
+    tmat[12]=tx; tmat[13]=ty; tmat[14]=tz; tmat[15]=1;
309
+
310
+    copy(mMatrix, tmp);
311
+    multiply(tmp, tmat, mMatrix);
312
+}
313
+
314
+void Matrix::copy(matrix_t source, matrix_t dest) {
315
+    for (int i = 0; i < 16; i++)
316
+        dest[i] = source[i];
317
+}
318
+
319
+void Matrix::multiply(const matrix_t a, const matrix_t b, matrix_t result) {
320
+    /* Generated code for matrix mult
321
+     * Code used:
322
+
323
+    // char order is argument
324
+    int i, j, k;
325
+    if (order == 'r') {
326
+        printf("// Row order\n");
327
+    } else {
328
+        printf("// Column order\n");
329
+    }
330
+    for (i = 0; i < 4; ++i) {
331
+        for (j = 0; j < 4; ++j) {
332
+            if (order == 'r') {
333
+                printf("result[%2i] = ", j+i*4);
334
+            } else {
335
+                printf("result[%2i] = ", j+i*4);
336
+            }
337
+            for (k = 0; k < 4; ++k) {
338
+                if (order == 'r') {
339
+                    printf("a[%2i] * b[%2i]%s",
340
+                    k+i*4, j+k*4, (k == 3) ? ";\n" : " + ");
341
+                } else {
342
+                    printf("a[%2i] * b[%2i]%s",
343
+                    i+k*4, k+j*4, (k == 3) ? ";\n" : " + ");
344
+                }
345
+                //sum+=(elements[i+k*4]*m.elements[k+j*4]);
346
+            }
347
+            //result.elements[i+j*4]=sum;
348
+        }
349
+        printf("\n");
350
+    }
351
+    printf("\n");
352
+    printf("// Transpose\n");
353
+    for(i = 0; i < 4; ++i) {
354
+        for (j = 0; j < 4; ++j) {
355
+            printf("a[%2i] = b[%2i]%s",
356
+            j+i*4, i+j*4, (j == 3) ? ";\n" : "; ");
357
+        }
358
+    }
359
+
360
+     * was in test/Matrix.cpp
361
+     */
362
+#ifdef COLUMN_ORDER
363
+    /* Column order */
364
+    result[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12] * b[ 3];
365
+    result[ 1] = a[ 0] * b[ 4] + a[ 4] * b[ 5] + a[ 8] * b[ 6] + a[12] * b[ 7];
366
+    result[ 2] = a[ 0] * b[ 8] + a[ 4] * b[ 9] + a[ 8] * b[10] + a[12] * b[11];
367
+    result[ 3] = a[ 0] * b[12] + a[ 4] * b[13] + a[ 8] * b[14] + a[12] * b[15];
368
+
369
+    result[ 4] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13] * b[ 3];
370
+    result[ 5] = a[ 1] * b[ 4] + a[ 5] * b[ 5] + a[ 9] * b[ 6] + a[13] * b[ 7];
371
+    result[ 6] = a[ 1] * b[ 8] + a[ 5] * b[ 9] + a[ 9] * b[10] + a[13] * b[11];
372
+    result[ 7] = a[ 1] * b[12] + a[ 5] * b[13] + a[ 9] * b[14] + a[13] * b[15];
373
+
374
+    result[ 8] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14] * b[ 3];
375
+    result[ 9] = a[ 2] * b[ 4] + a[ 6] * b[ 5] + a[10] * b[ 6] + a[14] * b[ 7];
376
+    result[10] = a[ 2] * b[ 8] + a[ 6] * b[ 9] + a[10] * b[10] + a[14] * b[11];
377
+    result[11] = a[ 2] * b[12] + a[ 6] * b[13] + a[10] * b[14] + a[14] * b[15];
378
+
379
+    result[12] = a[ 3] * b[ 0] + a[ 7] * b[ 1] + a[11] * b[ 2] + a[15] * b[ 3];
380
+    result[13] = a[ 3] * b[ 4] + a[ 7] * b[ 5] + a[11] * b[ 6] + a[15] * b[ 7];
381
+    result[14] = a[ 3] * b[ 8] + a[ 7] * b[ 9] + a[11] * b[10] + a[15] * b[11];
382
+    result[15] = a[ 3] * b[12] + a[ 7] * b[13] + a[11] * b[14] + a[15] * b[15];
383
+#else
384
+    /* Row order */
385
+    result[ 0] = a[ 0] * b[ 0] + a[ 1] * b[ 4] + a[ 2] * b[ 8] + a[ 3] * b[12];
386
+    result[ 1] = a[ 0] * b[ 1] + a[ 1] * b[ 5] + a[ 2] * b[ 9] + a[ 3] * b[13];
387
+    result[ 2] = a[ 0] * b[ 2] + a[ 1] * b[ 6] + a[ 2] * b[10] + a[ 3] * b[14];
388
+    result[ 3] = a[ 0] * b[ 3] + a[ 1] * b[ 7] + a[ 2] * b[11] + a[ 3] * b[15];
389
+
390
+    result[ 4] = a[ 4] * b[ 0] + a[ 5] * b[ 4] + a[ 6] * b[ 8] + a[ 7] * b[12];
391
+    result[ 5] = a[ 4] * b[ 1] + a[ 5] * b[ 5] + a[ 6] * b[ 9] + a[ 7] * b[13];
392
+    result[ 6] = a[ 4] * b[ 2] + a[ 5] * b[ 6] + a[ 6] * b[10] + a[ 7] * b[14];
393
+    result[ 7] = a[ 4] * b[ 3] + a[ 5] * b[ 7] + a[ 6] * b[11] + a[ 7] * b[15];
394
+
395
+    result[ 8] = a[ 8] * b[ 0] + a[ 9] * b[ 4] + a[10] * b[ 8] + a[11] * b[12];
396
+    result[ 9] = a[ 8] * b[ 1] + a[ 9] * b[ 5] + a[10] * b[ 9] + a[11] * b[13];
397
+    result[10] = a[ 8] * b[ 2] + a[ 9] * b[ 6] + a[10] * b[10] + a[11] * b[14];
398
+    result[11] = a[ 8] * b[ 3] + a[ 9] * b[ 7] + a[10] * b[11] + a[11] * b[15];
399
+
400
+    result[12] = a[12] * b[ 0] + a[13] * b[ 4] + a[14] * b[ 8] + a[15] * b[12];
401
+    result[13] = a[12] * b[ 1] + a[13] * b[ 5] + a[14] * b[ 9] + a[15] * b[13];
402
+    result[14] = a[12] * b[ 2] + a[13] * b[ 6] + a[14] * b[10] + a[15] * b[14];
403
+    result[15] = a[12] * b[ 3] + a[13] * b[ 7] + a[14] * b[11] + a[15] * b[15];
404
+#endif
405
+}
406
+

+ 135
- 0
src/utils/Vector3d.cpp View File

@@ -0,0 +1,135 @@
1
+/*!
2
+ * \file src/utils/Vector3d.cpp
3
+ * \brief 3D Math vector
4
+ *
5
+ * \author Mongoose
6
+ */
7
+
8
+#include <math.h>
9
+
10
+#include "utils/Vector3d.h"
11
+
12
+Vector3d::Vector3d() {
13
+    mVec[0] = mVec[1] = mVec[2] = 0.0f;
14
+}
15
+
16
+Vector3d::Vector3d(vec3_t v) {
17
+    mVec[0] = v[0];
18
+    mVec[1] = v[1];
19
+    mVec[2] = v[2];
20
+}
21
+
22
+Vector3d::Vector3d(vec_t x, vec_t y, vec_t z) {
23
+    mVec[0] = x;
24
+    mVec[1] = y;
25
+    mVec[2] = z;
26
+}
27
+
28
+Vector3d::Vector3d(const Vector3d &v) {
29
+    mVec[0] = v.mVec[0];
30
+    mVec[1] = v.mVec[1];
31
+    mVec[2] = v.mVec[2];
32
+}
33
+
34
+vec_t Vector3d::dot(const Vector3d &u, const Vector3d &v) {
35
+    return (u.mVec[0]*v.mVec[0] + u.mVec[1]*v.mVec[1] + u.mVec[2]*v.mVec[2]);
36
+}
37
+
38
+Vector3d Vector3d::cross(const Vector3d &u, const Vector3d &v) {
39
+    return Vector3d(u.mVec[1] * v.mVec[2] - u.mVec[2] * v.mVec[1],
40
+            u.mVec[2] * v.mVec[0] - u.mVec[0] * v.mVec[2],
41
+            u.mVec[0] * v.mVec[1] - u.mVec[1] * v.mVec[0]);
42
+}
43
+
44
+vec_t Vector3d::magnitude() {
45
+    return sqrtf(mVec[0]*mVec[0] + mVec[1]*mVec[1] + mVec[2]*mVec[2]);
46
+}
47
+
48
+Vector3d Vector3d::unit() {
49
+    vec_t norm = magnitude();
50
+
51
+    return Vector3d(mVec[0] / norm,
52
+            mVec[1] / norm,
53
+            mVec[2] / norm);
54
+}
55
+
56
+Vector3d Vector3d::zeroVector() {
57
+    return Vector3d(0, 0, 0);
58
+}
59
+
60
+Vector3d Vector3d::operator +(const Vector3d &v) {
61
+    return Vector3d(mVec[0] + v.mVec[0],
62
+            mVec[1] + v.mVec[1],
63
+            mVec[2] + v.mVec[2]);
64
+}
65
+
66
+Vector3d Vector3d::operator -(const Vector3d &v) {
67
+    return Vector3d(mVec[0] - v.mVec[0],
68
+            mVec[1] - v.mVec[1],
69
+            mVec[2] - v.mVec[2]);
70
+}
71
+
72
+Vector3d Vector3d::operator -() {
73
+    return Vector3d(-mVec[0],
74
+            -mVec[1],
75
+            -mVec[2]);
76
+}
77
+
78
+Vector3d Vector3d::operator *(vec_t s) {
79
+    return Vector3d(s * mVec[0],
80
+            s * mVec[1],
81
+            s * mVec[2]);
82
+}
83
+
84
+Vector3d Vector3d::operator /(vec_t s) {
85
+    return Vector3d(mVec[0] / s,
86
+            mVec[1] / s,
87
+            mVec[2] / s);
88
+}
89
+
90
+vec_t Vector3d::operator *(const Vector3d &v) {
91
+    return dot(*this, v);
92
+}
93
+
94
+void Vector3d::normalize() {
95
+    vec_t norm = magnitude();
96
+
97
+    mVec[0] /= norm;
98
+    mVec[1] /= norm;
99
+    mVec[2] /= norm;
100
+}
101
+
102
+void Vector3d::zero() {
103
+    mVec[0] = 0;
104
+    mVec[1] = 0;
105
+    mVec[2] = 0;
106
+}
107
+
108
+Vector3d &Vector3d::operator =(const Vector3d &v) {
109
+    mVec[0] = v.mVec[0];
110
+    mVec[1] = v.mVec[1];
111
+    mVec[2] = v.mVec[2];
112
+    return *this;
113
+}
114
+
115
+Vector3d &Vector3d::operator +=(const Vector3d &v) {
116
+    mVec[0] += v.mVec[0];
117
+    mVec[1] += v.mVec[1];
118
+    mVec[2] += v.mVec[2];
119
+    return *this;
120
+}
121
+
122
+Vector3d &Vector3d::operator -=(const Vector3d &v) {
123
+    mVec[0] -= v.mVec[0];
124
+    mVec[1] -= v.mVec[1];
125
+    mVec[2] -= v.mVec[2];
126
+    return *this;
127
+}
128
+
129
+Vector3d &Vector3d::operator *=(vec_t s) {
130
+    mVec[0] *= s;
131
+    mVec[1] *= s;
132
+    mVec[2] *= s;
133
+    return *this;
134
+}
135
+

Loading…
Cancel
Save