Browse Source

matrix and vector; tests

develop
Robin Thoni 7 years ago
parent
commit
27941fb890

+ 1
- 0
TheGame/TheGame.pro View File

@@ -11,6 +11,7 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
11 11
 TARGET = TheGame
12 12
 TEMPLATE = app
13 13
 
14
+CONFIG += c++11
14 15
 
15 16
 SOURCES += main.cpp\
16 17
         mainwindow.cpp \

+ 2
- 0
UGameEngine/UGameEngine.pro View File

@@ -9,6 +9,8 @@ QT       += gui core
9 9
 TARGET = UGameEngine
10 10
 TEMPLATE = lib
11 11
 
12
+CONFIG += c++11
13
+
12 14
 DEFINES += UGAMEENGINE_LIBRARY
13 15
 
14 16
 SOURCES += engine/ugameengine.cpp \

+ 35
- 2
UGameEngine/utils/matrixmxn.h View File

@@ -20,6 +20,9 @@ public:
20 20
 
21 21
     bool equal(const T& other) const;
22 22
 
23
+    T& fill(double k);
24
+    T& fill(double scalars[M][N]);
25
+
23 26
     T& add(double k);
24 27
     T& add(double scalars[M][N]);
25 28
     T& add(const T& other);
@@ -29,13 +32,43 @@ public:
29 32
     T& sub(const T& other);
30 33
 
31 34
     T& mult(double k);
32
-    uge_gm_tmpl_p MatrixMxN<M, P> mult(double scalars[N][P]);
33
-    uge_gm_tmpl_p MatrixMxN<M, P> mult(const MatrixMxN<N, P>& other);
35
+    T& mult(double scalars[M][N]);
36
+    T& mult(const T& other);
37
+    uge_gm_tmpl_p MatrixMxN<M, P> multMatrix(double scalars[N][P]);
38
+    uge_gm_tmpl_p MatrixMxN<M, P> multMatrix(const MatrixMxN<N, P>& other);
34 39
 
35 40
     T& div(double k);
36 41
     T& div(double scalars[M][N]);
37 42
     T& div(const T& other);
38 43
 
44
+    T operator+() const;
45
+    T operator+(const double& k) const;
46
+    T& operator+=(const double& k);
47
+    T operator+(const T& other) const;
48
+    T& operator+=(const T& other);
49
+
50
+    T operator-() const;
51
+    T operator-(const double& k) const;
52
+    T& operator-=(const double& k);
53
+    T operator-(const T& other) const;
54
+    T& operator-=(const T& other);
55
+
56
+    T operator*(const double& k) const;
57
+    T& operator*=(const double& k);
58
+    T operator*(const T& other) const;
59
+    T& operator*=(const T& other);
60
+
61
+    T operator/(const double& k) const;
62
+    T& operator/=(const double& k);
63
+    T operator/(const T& other) const;
64
+    T& operator/=(const T& other);
65
+
66
+    bool operator==(const T& other) const;
67
+    bool operator!=(const T& other) const;
68
+
69
+    bool operator!() const;
70
+    operator bool() const;
71
+
39 72
 protected:
40 73
     double _scalars[M][N];
41 74
 

+ 168
- 24
UGameEngine/utils/matrixmxn.hxx View File

@@ -3,29 +3,17 @@
3 3
 
4 4
 uge_gm_tmpl_mn MatrixMxN<M, N>::MatrixMxN()
5 5
 {
6
-    for (int i = 0; i < M; ++i) {
7
-        for (int j = 0; j < N; ++j) {
8
-            _scalars[i][j] = 0;
9
-        }
10
-    }
6
+    this->fill(0.0);
11 7
 }
12 8
 
13 9
 uge_gm_tmpl_mn MatrixMxN<M, N>::MatrixMxN(double k)
14 10
 {
15
-    for (int i = 0; i < M; ++i) {
16
-        for (int j = 0; j < N; ++j) {
17
-            _scalars[i][j] = k;
18
-        }
19
-    }
11
+    this->fill(k);
20 12
 }
21 13
 
22 14
 uge_gm_tmpl_mn MatrixMxN<M, N>::MatrixMxN(double scalars[M][N])
23 15
 {
24
-    for (int i = 0; i < M; ++i) {
25
-        for (int j = 0; j < N; ++j) {
26
-            _scalars[i][j] = scalars[i][j];
27
-        }
28
-    }
16
+    this->fill(scalars);
29 17
 }
30 18
 
31 19
 uge_gm_tmpl_mn MatrixMxN<M, N>::~MatrixMxN()
@@ -47,11 +35,6 @@ uge_gm_tmpl_mnt double GenericMatrix<M, N, T>::getScalar(unsigned i, unsigned j)
47 35
     return _scalars[i][j];
48 36
 }
49 37
 
50
-uge_gm_tmpl_mnt T *GenericMatrix<M, N, T>::getThis() const
51
-{
52
-    return (T*)this;
53
-}
54
-
55 38
 uge_gm_tmpl_mnt bool GenericMatrix<M, N, T>::isNull() const
56 39
 {
57 40
     return equal(T());
@@ -69,6 +52,26 @@ uge_gm_tmpl_mnt bool GenericMatrix<M, N, T>::equal(const T &other) const
69 52
     return true;
70 53
 }
71 54
 
55
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::fill(double k)
56
+{
57
+    for (int i = 0; i < M; ++i) {
58
+        for (int j = 0; j < N; ++j) {
59
+            _scalars[i][j] = k;
60
+        }
61
+    }
62
+    return *getThis();
63
+}
64
+
65
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::fill(double scalars[M][N])
66
+{
67
+    for (int i = 0; i < M; ++i) {
68
+        for (int j = 0; j < N; ++j) {
69
+            _scalars[i][j] = scalars[i][j];
70
+        }
71
+    }
72
+    return *getThis();
73
+}
74
+
72 75
 uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::add(double k)
73 76
 {
74 77
     return add(T(k));
@@ -119,14 +122,40 @@ uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::mult(double k)
119 122
     return *getThis();
120 123
 }
121 124
 
122
-uge_gm_tmpl_mnt uge_gm_tmpl_p MatrixMxN<M, P> GenericMatrix<M, N, T>::mult(double scalars[N][P])
125
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::mult(double scalars[M][N])
126
+{
127
+    return mult(T(scalars));
128
+}
129
+
130
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::mult(const T &other)
123 131
 {
124
-    return mult(MatrixMxN<N, P>(scalars));
132
+    for (int i = 0; i < M; ++i) {
133
+        for (int j = 0; j < N; ++j) {
134
+            _scalars[i][j] *= other._scalars[i][j];
135
+        }
136
+    }
137
+    return *getThis();
125 138
 }
126 139
 
127
-uge_gm_tmpl_mnt uge_gm_tmpl_p MatrixMxN<M, P> GenericMatrix<M, N, T>::mult(const MatrixMxN<N, P>& other)
140
+uge_gm_tmpl_mnt uge_gm_tmpl_p
141
+MatrixMxN<M, P> GenericMatrix<M, N, T>::multMatrix(double scalars[N][P])
128 142
 {
129
-    MatrixMxN<N, P> m;
143
+    return multMatrix(MatrixMxN<N, P>(scalars));
144
+}
145
+
146
+uge_gm_tmpl_mnt uge_gm_tmpl_p
147
+MatrixMxN<M, P> GenericMatrix<M, N, T>::multMatrix(const MatrixMxN<N, P>& other)
148
+{
149
+    MatrixMxN<M, P> m;
150
+    for (int i = 0; i < M; ++i) {
151
+        for (int j = 0; j < P; ++j) {
152
+            double t = 0;
153
+            for (int k = 0; k < N; ++k) {
154
+                t += _scalars[i][k] * other._scalars[k][j];
155
+            }
156
+            m._scalars[i][j] = t;
157
+        }
158
+    }
130 159
     return m;
131 160
 }
132 161
 
@@ -149,3 +178,118 @@ uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::div(const T &other)
149 178
     }
150 179
     return *getThis();
151 180
 }
181
+
182
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::operator+() const
183
+{
184
+    return *getThis();
185
+}
186
+
187
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::operator+(const double &k) const
188
+{
189
+    return T(*getThis()).add(k);
190
+}
191
+
192
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::operator+=(const double &k)
193
+{
194
+    return add(k);
195
+}
196
+
197
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::operator+(const T &other) const
198
+{
199
+    return T(*getThis()).add(other);
200
+}
201
+
202
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::operator+=(const T &other)
203
+{
204
+    return add(other);
205
+}
206
+
207
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::operator-() const
208
+{
209
+    return T() - (*getThis());
210
+}
211
+
212
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::operator-(const double &k) const
213
+{
214
+    return T(*getThis()).sub(k);
215
+}
216
+
217
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::operator-=(const double &k)
218
+{
219
+    return sub(k);
220
+}
221
+
222
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::operator-(const T &other) const
223
+{
224
+    return T(*getThis()).sub(other);
225
+}
226
+
227
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::operator-=(const T &other)
228
+{
229
+    return sub(other);
230
+}
231
+
232
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::operator*(const double &k) const
233
+{
234
+    return T(*getThis()).mult(k);
235
+}
236
+
237
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::operator*=(const double &k)
238
+{
239
+    return mult(k);
240
+}
241
+
242
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::operator*(const T &other) const
243
+{
244
+    return T(*getThis()).mult(other);
245
+}
246
+
247
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::operator*=(const T &other)
248
+{
249
+    return mult(other);
250
+}
251
+
252
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::operator/(const double &k) const
253
+{
254
+    return T(*getThis()).div(k);
255
+}
256
+
257
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::operator/=(const double &k)
258
+{
259
+    return div(k);
260
+}
261
+
262
+uge_gm_tmpl_mnt T GenericMatrix<M, N, T>::operator/(const T &other) const
263
+{
264
+    return T(*getThis()).div(other);
265
+}
266
+
267
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::operator/=(const T &other)
268
+{
269
+    return div(other);
270
+}
271
+
272
+uge_gm_tmpl_mnt bool GenericMatrix<M, N, T>::operator==(const T &other) const
273
+{
274
+    return equal(other);
275
+}
276
+
277
+uge_gm_tmpl_mnt bool GenericMatrix<M, N, T>::operator!=(const T &other) const
278
+{
279
+    return !equal(other);
280
+}
281
+
282
+uge_gm_tmpl_mnt bool GenericMatrix<M, N, T>::operator!() const
283
+{
284
+    return isNull();
285
+}
286
+
287
+uge_gm_tmpl_mnt GenericMatrix<M, N, T>::operator bool() const
288
+{
289
+    return !isNull();
290
+}
291
+
292
+uge_gm_tmpl_mnt T *GenericMatrix<M, N, T>::getThis() const
293
+{
294
+    return (T*)this;
295
+}

+ 17
- 17
UGameEngine/utils/vector2d.cpp View File

@@ -1,51 +1,51 @@
1 1
 #include "vector2d.h"
2 2
 
3
-VectorXD<2>::VectorXD()
3
+Vector2D::MatrixMxN()
4 4
 {
5
-    _scalars[0] = 0.0;
6
-    _scalars[1] = 0.0;
5
+    _scalars[0][0] = 0.0;
6
+    _scalars[1][0] = 0.0;
7 7
 }
8 8
 
9
-VectorXD<2>::VectorXD(const double scalars[2])
9
+Vector2D::MatrixMxN(const double scalars[2])
10 10
 {
11
-    _scalars[0] = scalars[0];
12
-    _scalars[1] = scalars[1];
11
+    _scalars[0][0] = scalars[0];
12
+    _scalars[1][0] = scalars[1];
13 13
 }
14 14
 
15
-Vector2D::VectorXD(double x, double y)
15
+Vector2D::MatrixMxN(double x, double y)
16 16
 {
17
-    _scalars[0] = x;
18
-    _scalars[1] = y;
17
+    _scalars[0][0] = x;
18
+    _scalars[1][0] = y;
19 19
 }
20 20
 
21
-Vector2D::VectorXD(const VectorXD<2> &other)
21
+Vector2D::MatrixMxN(const Vector2D &other)
22 22
 {
23
-    _scalars[0] = other._scalars[0];
24
-    _scalars[1] = other._scalars[1];
23
+    _scalars[0][0] = other._scalars[0][0];
24
+    _scalars[1][0] = other._scalars[1][0];
25 25
 }
26 26
 
27
-Vector2D::~VectorXD()
27
+Vector2D::~MatrixMxN()
28 28
 {
29 29
 }
30 30
 
31 31
 double Vector2D::getX() const
32 32
 {
33
-    return _scalars[0];
33
+    return _scalars[0][0];
34 34
 }
35 35
 
36 36
 Vector2D& Vector2D::setX(double x)
37 37
 {
38
-    _scalars[0] = x;
38
+    _scalars[0][0] = x;
39 39
     return *this;
40 40
 }
41 41
 double Vector2D::getY() const
42 42
 {
43
-    return _scalars[1];
43
+    return _scalars[1][0];
44 44
 }
45 45
 
46 46
 Vector2D& Vector2D::setY(double y)
47 47
 {
48
-    _scalars[1] = y;
48
+    _scalars[1][0] = y;
49 49
     return *this;
50 50
 }
51 51
 

+ 10
- 10
UGameEngine/utils/vector2d.h View File

@@ -3,16 +3,16 @@
3 3
 
4 4
 #include "vectorxd.h"
5 5
 
6
-typedef VectorXD<2> Vector2D;
6
+typedef MatrixMxN<2, 1> Vector2D;
7 7
 
8
-template<> class VectorXD<2> : public GenericVector<2, VectorXD<2> >
8
+template<> class MatrixMxN<2, 1> : public GenericVector<2, Vector2D>
9 9
 {
10 10
 public:
11
-    VectorXD<2>();
12
-    VectorXD<2>(const double scalars[2]);
13
-    VectorXD<2>(double x, double y);
14
-    VectorXD<2>(const VectorXD<2>& other);
15
-    virtual ~VectorXD<2>();
11
+    MatrixMxN<2, 1>();
12
+    MatrixMxN<2, 1>(const double scalars[2]);
13
+    MatrixMxN<2, 1>(double x, double y);
14
+    MatrixMxN<2, 1>(const Vector2D& other);
15
+    virtual ~MatrixMxN<2, 1>();
16 16
 
17 17
     double getX() const;
18 18
     Vector2D& setX(double x);
@@ -20,13 +20,13 @@ public:
20 20
     double getY() const;
21 21
     Vector2D& setY(double y);
22 22
 
23
-    using GenericVector<2, VectorXD<2> >::add;
23
+    using GenericVector<2, Vector2D>::add;
24 24
     Vector2D& add(double x, double y);
25 25
 
26
-    using GenericVector<2, VectorXD<2> >::sub;
26
+    using GenericVector<2, Vector2D>::sub;
27 27
     Vector2D& sub(double x, double y);
28 28
 
29
-    using GenericVector<2, VectorXD<2> >::dotProduct;
29
+    using GenericVector<2, Vector2D>::dotProduct;
30 30
     double dotProduct(double x, double y) const;
31 31
 };
32 32
 

+ 32
- 29
UGameEngine/utils/vector3d.cpp View File

@@ -1,82 +1,85 @@
1 1
 #include "vector3d.h"
2 2
 #include <math.h>
3 3
 
4
-Vector3D::VectorXD()
4
+Vector3D::MatrixMxN()
5 5
 {
6
-    _scalars[0] = 0.0;
7
-    _scalars[1] = 0.0;
8
-    _scalars[2] = 0.0;
6
+    this->fill(0.0);
9 7
 }
10 8
 
11
-Vector3D::VectorXD(const double scalars[3])
9
+Vector3D::MatrixMxN(double k)
12 10
 {
13
-    _scalars[0] = scalars[0];
14
-    _scalars[1] = scalars[1];
15
-    _scalars[2] = scalars[2];
11
+    this->fill(k);
16 12
 }
17 13
 
18
-Vector3D::VectorXD(double x, double y, double z)
14
+Vector3D::MatrixMxN(const double scalars[3])
19 15
 {
20
-    _scalars[0] = x;
21
-    _scalars[1] = y;
22
-    _scalars[2] = z;
16
+    _scalars[0][0] = scalars[0];
17
+    _scalars[1][0] = scalars[1];
18
+    _scalars[2][0] = scalars[2];
23 19
 }
24 20
 
25
-Vector3D::VectorXD(const Vector3D &other)
21
+Vector3D::MatrixMxN(double x, double y, double z)
26 22
 {
27
-    _scalars[0] = other._scalars[0];
28
-    _scalars[1] = other._scalars[1];
29
-    _scalars[2] = other._scalars[2];
23
+    _scalars[0][0] = x;
24
+    _scalars[1][0] = y;
25
+    _scalars[2][0] = z;
30 26
 }
31 27
 
32
-Vector3D::~VectorXD()
28
+Vector3D::MatrixMxN(const Vector3D &other)
29
+{
30
+    _scalars[0][0] = other._scalars[0][0];
31
+    _scalars[1][0] = other._scalars[1][0];
32
+    _scalars[2][0] = other._scalars[2][0];
33
+}
34
+
35
+Vector3D::~MatrixMxN()
33 36
 {
34 37
 }
35 38
 
36 39
 double Vector3D::getX() const
37 40
 {
38
-    return _scalars[0];
41
+    return _scalars[0][0];
39 42
 }
40 43
 
41 44
 Vector3D& Vector3D::setX(double x)
42 45
 {
43
-    _scalars[0] = x;
46
+    _scalars[0][0] = x;
44 47
     return *this;
45 48
 }
46 49
 double Vector3D::getY() const
47 50
 {
48
-    return _scalars[1];
51
+    return _scalars[1][0];
49 52
 }
50 53
 
51 54
 Vector3D& Vector3D::setY(double y)
52 55
 {
53
-    _scalars[1] = y;
56
+    _scalars[1][0] = y;
54 57
     return *this;
55 58
 }
56 59
 double Vector3D::getZ() const
57 60
 {
58
-    return _scalars[2];
61
+    return _scalars[2][0];
59 62
 }
60 63
 
61 64
 Vector3D& Vector3D::setZ(double z)
62 65
 {
63
-    _scalars[2] = z;
66
+    _scalars[2][0] = z;
64 67
     return *this;
65 68
 }
66 69
 
67 70
 Vector3D &Vector3D::add(double x, double y, double z)
68 71
 {
69
-    _scalars[0] += x;
70
-    _scalars[1] += y;
71
-    _scalars[2] += z;
72
+    _scalars[0][0] += x;
73
+    _scalars[1][0] += y;
74
+    _scalars[2][0] += z;
72 75
     return *this;
73 76
 }
74 77
 
75 78
 Vector3D &Vector3D::sub(double x, double y, double z)
76 79
 {
77
-    _scalars[0] -= x;
78
-    _scalars[1] -= y;
79
-    _scalars[2] -= z;
80
+    _scalars[0][0] -= x;
81
+    _scalars[1][0] -= y;
82
+    _scalars[2][0] -= z;
80 83
     return *this;
81 84
 }
82 85
 

+ 11
- 10
UGameEngine/utils/vector3d.h View File

@@ -3,16 +3,17 @@
3 3
 
4 4
 #include "vectorxd.h"
5 5
 
6
-typedef VectorXD<3> Vector3D;
6
+typedef MatrixMxN<3, 1> Vector3D;
7 7
 
8
-template<> class VectorXD<3>: public GenericVector<3, VectorXD<3> >
8
+template<> class MatrixMxN<3, 1>: public GenericVector<3, Vector3D>
9 9
 {
10 10
 public:
11
-    VectorXD<3>();
12
-    VectorXD<3>(const double scalars[3]);
13
-    VectorXD<3>(double x, double y, double z);
14
-    VectorXD<3>(const Vector3D& other);
15
-    virtual ~VectorXD<3>();
11
+    MatrixMxN<3, 1>();
12
+    MatrixMxN<3, 1>(double k);
13
+    MatrixMxN<3, 1>(const double scalars[3]);
14
+    MatrixMxN<3, 1>(double x, double y, double z);
15
+    MatrixMxN<3, 1>(const Vector3D& other);
16
+    virtual ~MatrixMxN<3, 1>();
16 17
 
17 18
     double getX() const;
18 19
     Vector3D& setX(double x);
@@ -23,13 +24,13 @@ public:
23 24
     double getZ() const;
24 25
     Vector3D& setZ(double z);
25 26
 
26
-    using GenericVector<3, VectorXD<3> >::add;
27
+    using GenericVector<3, Vector3D>::add;
27 28
     Vector3D& add(double x, double y, double z);
28 29
 
29
-    using GenericVector<3, VectorXD<3> >::sub;
30
+    using GenericVector<3, Vector3D>::sub;
30 31
     Vector3D& sub(double x, double y, double z);
31 32
 
32
-    using GenericVector<3, VectorXD<3> >::dotProduct;
33
+    using GenericVector<3, Vector3D>::dotProduct;
33 34
     double dotProduct(double x, double y, double z) const;
34 35
 };
35 36
 

+ 20
- 54
UGameEngine/utils/vectorxd.h View File

@@ -2,37 +2,20 @@
2 2
 #define VECTORXD_H
3 3
 
4 4
 #include <QDebug>
5
+#include "matrixmxn.h"
5 6
 
6 7
 #define tmplx template<unsigned X>
7 8
 #define tmpl template<unsigned X, class T>
8 9
 
9
-tmpl class GenericVector
10
+tmpl class GenericVector: public GenericMatrix<X, 1, T >
10 11
 {
11 12
 public:
12 13
 
14
+    using GenericMatrix<X, 1, T >::setScalar;
13 15
     T& setScalar(unsigned i, double value);
16
+    using GenericMatrix<X, 1, T >::getScalar;
14 17
     double getScalar(unsigned i) const;
15 18
 
16
-    bool isNull() const;
17
-
18
-    bool equal(const T& other) const;
19
-
20
-    T& add(double k);
21
-    T& add(double scalars[X]);
22
-    T& add(const T& other);
23
-
24
-    T& sub(double k);
25
-    T& sub(double scalars[X]);
26
-    T& sub(const T& other);
27
-
28
-    T& mult(double k);
29
-    T& mult(double scalars[X]);
30
-    T& mult(const T& other);
31
-
32
-    T& div(double k);
33
-    T& div(double scalars[X]);
34
-    T& div(const T& other);
35
-
36 19
     double dotProduct(const T& other) const;
37 20
 
38 21
     T& crossProduct(const T& other);
@@ -40,54 +23,37 @@ public:
40 23
 
41 24
     double norm() const;
42 25
 
43
-    T operator+() const;
44
-    T operator+(const double& k) const;
45
-    T& operator+=(const double& k);
46
-    T operator+(const T& other) const;
47
-    T& operator+=(const T& other);
48
-
49
-    T operator-() const;
50
-    T operator-(const double& k) const;
51
-    T& operator-=(const double& k);
52
-    T operator-(const T& other) const;
53
-    T& operator-=(const T& other);
54
-
55
-    T operator*(const double& k) const;
56
-    T& operator*=(const double& k);
57
-    T operator*(const T& other) const;
58
-    T& operator*=(const T& other);
59
-
60
-    T operator/(const double& k) const;
61
-    T& operator/=(const double& k);
62
-
63
-    bool operator==(const T& other) const;
64
-    bool operator!=(const T& other) const;
65
-
66 26
     double operator[](unsigned i) const;
67 27
 
68
-    bool operator!() const;
69
-    operator bool() const;
70
-
71 28
 protected:
72
-    double _scalars[X];
29
+    using GenericMatrix<X, 1, T >::_scalars;
73 30
 
74 31
 private:
75 32
     T* getThis() const;
76 33
 };
77 34
 
78
-tmplx class VectorXD: public GenericVector<X, VectorXD<X> >
35
+tmplx using VectorXD = MatrixMxN<X, 1>;
36
+
37
+tmplx class MatrixMxN<X, 1>: public GenericVector<X, VectorXD<X>>
79 38
 {
80 39
 public:
81
-    VectorXD();
82
-    VectorXD(const double scalars[X]);
83
-    VectorXD(const VectorXD<X>& other);
40
+    MatrixMxN();
41
+    MatrixMxN(double k);
42
+    MatrixMxN(const double scalars[X]);
43
+    MatrixMxN(const VectorXD<X>& other);
84 44
 
85 45
 protected:
86
-    using GenericVector<X, VectorXD<X> >::_scalars;
46
+    using GenericMatrix<X, 1, VectorXD<X>>::_scalars;
87 47
 };
88 48
 
89
-tmplx QDebug operator<<(QDebug dbg, const VectorXD<X>& v);
49
+//tmplx QDebug operator<<(QDebug dbg, const VectorXD<X>& v);
90 50
 
91 51
 #include "vectorxd.hxx"
92 52
 
93 53
 #endif // VECTORXD_H
54
+
55
+
56
+
57
+
58
+
59
+

+ 21
- 229
UGameEngine/utils/vectorxd.hxx View File

@@ -3,145 +3,48 @@
3 3
 #define tmplx template<unsigned X>
4 4
 #define tmpl template<unsigned X, class T>
5 5
 
6
-tmplx VectorXD<X>::VectorXD()
6
+tmplx VectorXD<X>::MatrixMxN()
7 7
 {
8
-    for (unsigned i = 0; i < X; ++i) {
9
-        _scalars[i] = 0;
10
-    }
11
-}
12
-
13
-tmplx VectorXD<X>::VectorXD(const double scalars[X])
14
-{
15
-    for (unsigned i = 0; i < X; ++i) {
16
-        _scalars[i] = scalars[i];
17
-    }
18
-}
19
-
20
-tmplx VectorXD<X>::VectorXD(const VectorXD<X>& other)
21
-{
22
-    for (unsigned i = 0; i < X; ++i) {
23
-        _scalars[i] = other._scalars[i];
24
-    }
25
-}
26
-
27
-tmpl T& GenericVector<X, T>::setScalar(unsigned i, double value)
28
-{
29
-    _scalars[i] = value;
30
-    return *getThis();
31
-}
32
-
33
-tmpl double GenericVector<X, T>::getScalar(unsigned i) const
34
-{
35
-    return _scalars[i];
36
-}
37
-
38
-tmpl bool GenericVector<X, T>::isNull() const
39
-{
40
-    return equal(T());
41
-}
42
-
43
-tmpl bool GenericVector<X, T>::equal(const T &other) const
44
-{
45
-    for (unsigned i = 0; i < X; ++i) {
46
-        if (_scalars[i] != other._scalars[i]) {
47
-            return false;
48
-        }
49
-    }
50
-    return true;
51
-}
52
-
53
-tmpl T &GenericVector<X, T>::add(double k)
54
-{
55
-    double scalars[X];
56
-    for (unsigned i = 0; i < X; ++i) {
57
-        scalars[i] = k;
58
-    }
59
-    return add(T(scalars));
60
-}
61
-
62
-tmpl T &GenericVector<X, T>::add(double scalars[X])
63
-{
64
-    return add(T(scalars));
65
-}
66
-
67
-tmpl T& GenericVector<X, T>::add(const  T &other)
68
-{
69
-    for (unsigned i = 0; i < X; ++i) {
70
-        _scalars[i] += other._scalars[i];
71
-    }
72
-    return *getThis();
73
-}
74
-
75
-tmpl T &GenericVector<X, T>::sub(double k)
76
-{
77
-    double scalars[X];
78
-    for (unsigned i = 0; i < X; ++i) {
79
-        scalars[i] = k;
80
-    }
81
-    return sub(T(scalars));
82
-}
83
-
84
-tmpl T &GenericVector<X, T>::sub(double scalars[X])
85
-{
86
-    return sub(T(scalars));
87
-}
88
-
89
-tmpl T& GenericVector<X, T>::sub(const T &other)
90
-{
91
-    for (unsigned i = 0; i < X; ++i) {
92
-        _scalars[i] -= other._scalars[i];
93
-    }
94
-    return *getThis();
8
+    this->fill(0);
95 9
 }
96 10
 
97
-tmpl T &GenericVector<X, T>::mult(double k)
11
+tmplx VectorXD<X>::MatrixMxN(double k)
98 12
 {
99
-    for (unsigned i = 0; i < X; ++i) {
100
-        _scalars[i] *= k;
101
-    }
102
-    return *getThis();
13
+    this->fill(k);
103 14
 }
104 15
 
105
-tmpl T &GenericVector<X, T>::mult(double scalars[X])
16
+tmplx VectorXD<X>::MatrixMxN(const double scalars[X])
106 17
 {
107
-    return mult(T(scalars));
18
+    this->fill(scalars);
108 19
 }
109 20
 
110
-tmpl T &GenericVector<X, T>::mult(const T &other)
21
+tmplx VectorXD<X>::MatrixMxN(const VectorXD<X>& other)
111 22
 {
112 23
     for (unsigned i = 0; i < X; ++i) {
113
-        _scalars[i] *= other._scalars[i];
24
+        _scalars[i] = other._scalars[i];
114 25
     }
115
-    return *getThis();
116 26
 }
117 27
 
118
-tmpl T &GenericVector<X, T>::div(double k)
28
+tmpl T& GenericVector<X, T>::setScalar(unsigned i, double value)
119 29
 {
120
-    double scalars[X];
121
-    for (unsigned i = 0; i < X; ++i) {
122
-        scalars[i] = k;
123
-    }
124
-    return div(T(scalars));
30
+    return setScalar(i, 0,value);
125 31
 }
126 32
 
127
-tmpl T &GenericVector<X, T>::div(double scalars[X])
33
+tmpl double GenericVector<X, T>::getScalar(unsigned i) const
128 34
 {
129
-    return div(T(scalars));
35
+    return getScalar(i, 0);
130 36
 }
131 37
 
132
-tmpl T &GenericVector<X, T>::div(const T &other)
38
+tmpl double GenericVector<X, T>::operator[](unsigned i) const
133 39
 {
134
-    for (unsigned i = 0; i < X; ++i) {
135
-        _scalars[i] /= other._scalars[i];
136
-    }
137
-    return *getThis();
40
+    return getScalar(i);
138 41
 }
139 42
 
140 43
 tmpl double GenericVector<X, T>::dotProduct(const T &other) const
141 44
 {
142 45
     double total = 0;
143 46
     for (unsigned i = 0; i < X; ++i) {
144
-        total += _scalars[i] * other._scalars[i];
47
+        total += _scalars[i][0] * other._scalars[i][0];
145 48
     }
146 49
     return total;
147 50
 }
@@ -152,7 +55,7 @@ tmpl T& GenericVector<X, T>::crossProduct(const T& other)
152 55
     for (unsigned i = 0; i < X; ++i) {
153 56
         unsigned j = (i + 1) % X;
154 57
         unsigned k = (i + 2) % X;
155
-        _scalars[i] = (t._scalars[j] * other._scalars[k]) - (t._scalars[k] * other._scalars[j]);
58
+        _scalars[i][0] = (t._scalars[j][0] * other._scalars[k][0]) - (t._scalars[k][0] * other._scalars[j][0]);
156 59
     }
157 60
     return *getThis();
158 61
 }
@@ -166,128 +69,17 @@ tmpl double GenericVector<X, T>::norm() const
166 69
 {
167 70
     double total = 0;
168 71
     for (unsigned i = 0; i < X; ++i) {
169
-        total += _scalars[i] * _scalars[i];
72
+        total += _scalars[i][0] * _scalars[i][0];
170 73
     }
171 74
     return sqrt(total);
172 75
 }
173 76
 
174
-tmpl T GenericVector<X, T>::operator+() const
175
-{
176
-    return *getThis();
177
-}
178
-
179
-tmpl T GenericVector<X, T>::operator+(const double &k) const
180
-{
181
-    return T(*getThis()).add(k);
182
-}
183
-
184
-tmpl T &GenericVector<X, T>::operator+=(const double &k)
185
-{
186
-    return add(k);
187
-}
188
-
189
-tmpl T GenericVector<X, T>::operator+(const T &other) const
190
-{
191
-    T v = *getThis();
192
-    return v.add(other);
193
-}
194
-
195
-tmpl T &GenericVector<X, T>::operator+=(const T &other)
196
-{
197
-    return add(other);
198
-}
199
-
200
-tmpl T GenericVector<X, T>::operator-() const
201
-{
202
-    double scalars[X];
203
-    for (unsigned i = 0; i < X; ++i) {
204
-        scalars[i] = -_scalars[i];
205
-    }
206
-    return T(scalars);
207
-}
208
-
209
-tmpl T GenericVector<X, T>::operator-(const double &k) const
210
-{
211
-    return T(*getThis()).sub(k);
212
-}
213
-
214
-tmpl T &GenericVector<X, T>::operator-=(const double &k)
215
-{
216
-    return sub(k);
217
-}
218
-
219
-tmpl T GenericVector<X, T>::operator-(const T &other) const
220
-{
221
-    return T(*getThis()).sub(other);
222
-}
223
-
224
-tmpl T &GenericVector<X, T>::operator-=(const T &other)
225
-{
226
-    return sub(other);
227
-}
228
-
229
-tmpl T GenericVector<X, T>::operator*(const double &k) const
230
-{
231
-    return T(*getThis()).mult(k);
232
-}
233
-
234
-tmpl T &GenericVector<X, T>::operator*=(const double &k)
235
-{
236
-    return mult(k);
237
-}
238
-
239
-tmpl T GenericVector<X, T>::operator*(const T &other) const
240
-{
241
-    return T(*getThis()).mult(other);
242
-}
243
-
244
-tmpl T &GenericVector<X, T>::operator*=(const T &other)
245
-{
246
-    dotProduct(other);
247
-    return *getThis();
248
-}
249
-
250
-tmpl T GenericVector<X, T>::operator/(const double &k) const
251
-{
252
-    return T(*getThis()).div(k);
253
-}
254
-
255
-tmpl T &GenericVector<X, T>::operator/=(const double &k)
256
-{
257
-    return div(k);
258
-}
259
-
260
-tmpl bool GenericVector<X, T>::operator==(const T &other) const
261
-{
262
-    return equal(other);
263
-}
264
-
265
-tmpl bool GenericVector<X, T>::operator!=(const T &other) const
266
-{
267
-    return !equal(other);
268
-}
269
-
270
-tmpl double GenericVector<X, T>::operator[](unsigned i) const
271
-{
272
-    return getScalar(i);
273
-}
274
-
275
-tmpl bool GenericVector<X, T>::operator!() const
276
-{
277
-    return isNull();
278
-}
279
-
280
-tmpl GenericVector<X, T>::operator bool() const
281
-{
282
-    return !isNull();
283
-}
284
-
285 77
 tmpl T* GenericVector<X, T>::getThis() const
286 78
 {
287 79
     return (T*)this;
288 80
 }
289 81
 
290
-tmpl QDebug operator<<(QDebug dbg, const T &v)
291
-{
292
-    return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
293
-}
82
+//tmpl QDebug operator<<(QDebug dbg, const T &v)
83
+//{
84
+//    return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
85
+//}

+ 25
- 7
tests/testmatrix3x3.cpp View File

@@ -42,11 +42,11 @@ void TestMatrix3x3::isNull1()
42 42
 
43 43
 void TestMatrix3x3::isNull1Op()
44 44
 {
45
-//    double scalars[3][3] = {{0.0, 1.0, 2.0},
46
-//                            {3.0, 4.0, 5.0},
47
-//                            {6.0, 7.0, 8.0}};
48
-//    Matrix3x3 m(scalars);
49
-//    QCOMPARE((bool)m, false);
45
+    double scalars[3][3] = {{0.0, 1.0, 2.0},
46
+                            {3.0, 4.0, 5.0},
47
+                            {6.0, 7.0, 8.0}};
48
+    Matrix3x3 m(scalars);
49
+    QCOMPARE((bool)m, true);
50 50
 }
51 51
 
52 52
 void TestMatrix3x3::isNull2()
@@ -57,6 +57,24 @@ void TestMatrix3x3::isNull2()
57 57
 
58 58
 void TestMatrix3x3::isNull2Op()
59 59
 {
60
-//    Matrix3x3 m;
61
-//    QCOMPARE((bool)m, true);
60
+    Matrix3x3 m;
61
+    QCOMPARE((bool)m, false);
62
+}
63
+
64
+void TestMatrix3x3::mult1()
65
+{
66
+    double scalars1[3][3] = {{42.0, 5.0, 6.0},
67
+                            {2.0, 10.0, 9.0},
68
+                            {69.0, 35.0, 51.0}};
69
+    double scalars2[3][3] = {{26.0, 17.0, 35.0},
70
+                            {6.0, 48.0, 52.0},
71
+                            {12.0, 25.0, 62.0}};
72
+    double scalars3[3][3] = {{1194.0, 1104.0, 2102.0},
73
+                            {220.0, 739.0, 1148.0},
74
+                            {2616.0, 4128.0, 7397.0}};
75
+    Matrix3x3 m1(scalars1);
76
+    Matrix3x3 m2(scalars2);
77
+    Matrix3x3 m3(scalars3);
78
+    Matrix3x3 m = m1.multMatrix(m2);
79
+    QCOMPARE(m.equal(m3), true);
62 80
 }

+ 2
- 0
tests/testmatrix3x3.h View File

@@ -16,6 +16,8 @@ private slots:
16 16
     void isNull1Op();
17 17
     void isNull2();
18 18
     void isNull2Op();
19
+
20
+    void mult1();
19 21
 };
20 22
 
21 23
 #endif // TESTMATRIX3X3_H

+ 1
- 0
tests/tests.pro View File

@@ -11,6 +11,7 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
11 11
 TARGET = tests
12 12
 TEMPLATE = app
13 13
 
14
+CONFIG += c++11
14 15
 
15 16
 SOURCES += \
16 17
     testvector3d.cpp \

Loading…
Cancel
Save