소스 검색

begin matrix; tests

develop
Robin Thoni 8 년 전
부모
커밋
f27f164eee

+ 7
- 3
UGameEngine/UGameEngine.pro 파일 보기

@@ -14,14 +14,15 @@ DEFINES += UGAMEENGINE_LIBRARY
14 14
 SOURCES += engine/ugameengine.cpp \
15 15
     engine/abstractrenderdevice.cpp \
16 16
     entities/ugeentity.cpp \
17
-    utils/vector3d.cpp \
18 17
     utils/colorvector3d.cpp \
19 18
     entities/ugeentitycube.cpp \
20 19
     entities/ugeentityaxes.cpp \
21 20
     utils/wavefrontobj.cpp \
22 21
     entities/ugeentitywavefrontobj.cpp \
23 22
     utils/texturevector3d.cpp \
24
-    utils/vector2d.cpp
23
+    utils/vector2d.cpp \
24
+    utils/vector3d.cpp \
25
+    utils/matrix3x3.cpp
25 26
 
26 27
 HEADERS += engine/ugameengine.h\
27 28
     engine/abstractrenderdevice.h \
@@ -35,7 +36,10 @@ HEADERS += engine/ugameengine.h\
35 36
     utils/texturevector3d.h \
36 37
     utils/vectorxd.h \
37 38
     utils/vectorxd.hxx \
38
-    utils/vector2d.h
39
+    utils/vector2d.h \
40
+    utils/matrixmxn.h \
41
+    utils/matrixmxn.hxx \
42
+    utils/matrix3x3.h
39 43
 
40 44
 
41 45
 # FLEX && BISON

+ 5
- 0
UGameEngine/utils/matrix3x3.cpp 파일 보기

@@ -0,0 +1,5 @@
1
+#include "matrix3x3.h"
2
+
3
+//Matrix3x3::Matrix3x3()
4
+//{
5
+//}

+ 14
- 0
UGameEngine/utils/matrix3x3.h 파일 보기

@@ -0,0 +1,14 @@
1
+#ifndef MATRIX3X3_H
2
+#define MATRIX3X3_H
3
+
4
+#include "utils/matrixmxn.h"
5
+
6
+typedef MatrixMxN<3, 3> Matrix3x3;
7
+
8
+//class Matrix3x3
9
+//{
10
+//public:
11
+//    Matrix3x3();
12
+//};
13
+
14
+#endif // MATRIX3X3_H

+ 60
- 0
UGameEngine/utils/matrixmxn.h 파일 보기

@@ -0,0 +1,60 @@
1
+#ifndef MATRIXMXN_H
2
+#define MATRIXMXN_H
3
+
4
+#define uge_gm_tmpl_mnt template<unsigned M, unsigned N, class T>
5
+#define uge_gm_tmpl_mn template<unsigned M, unsigned N>
6
+#define uge_gm_tmpl_p template<unsigned P>
7
+#define uge_gm_tmpl_mntp template<unsigned M, unsigned N, class T, unsigned P>
8
+
9
+uge_gm_tmpl_mn class MatrixMxN;
10
+
11
+uge_gm_tmpl_mnt class GenericMatrix
12
+{
13
+public:
14
+    virtual ~GenericMatrix();
15
+
16
+    T& setScalar(unsigned i, unsigned j, double value);
17
+    double getScalar(unsigned i, unsigned j) const;
18
+
19
+    bool isNull() const;
20
+
21
+    bool equal(const T& other) const;
22
+
23
+    T& add(double k);
24
+    T& add(double scalars[M][N]);
25
+    T& add(const T& other);
26
+
27
+    T& sub(double k);
28
+    T& sub(double scalars[M][N]);
29
+    T& sub(const T& other);
30
+
31
+    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);
34
+
35
+    T& div(double k);
36
+    T& div(double scalars[M][N]);
37
+    T& div(const T& other);
38
+
39
+protected:
40
+    double _scalars[M][N];
41
+
42
+private:
43
+    T* getThis() const;
44
+};
45
+
46
+uge_gm_tmpl_mn class MatrixMxN: public GenericMatrix<M, N, MatrixMxN<M, N> >
47
+{
48
+public:
49
+    MatrixMxN();
50
+    MatrixMxN(double k);
51
+    MatrixMxN(double scalars[M][N]);
52
+    virtual ~MatrixMxN();
53
+
54
+protected:
55
+    using GenericMatrix<M, N, MatrixMxN<M, N> >::_scalars;
56
+};
57
+
58
+#include "matrixmxn.hxx"
59
+
60
+#endif // MATRIXMXN_H

+ 151
- 0
UGameEngine/utils/matrixmxn.hxx 파일 보기

@@ -0,0 +1,151 @@
1
+#define uge_gm_tmpl_mnt template<unsigned M, unsigned N, class T>
2
+#define uge_gm_tmpl_mn template<unsigned M, unsigned N>
3
+
4
+uge_gm_tmpl_mn MatrixMxN<M, N>::MatrixMxN()
5
+{
6
+    for (int i = 0; i < M; ++i) {
7
+        for (int j = 0; j < N; ++j) {
8
+            _scalars[i][j] = 0;
9
+        }
10
+    }
11
+}
12
+
13
+uge_gm_tmpl_mn MatrixMxN<M, N>::MatrixMxN(double k)
14
+{
15
+    for (int i = 0; i < M; ++i) {
16
+        for (int j = 0; j < N; ++j) {
17
+            _scalars[i][j] = k;
18
+        }
19
+    }
20
+}
21
+
22
+uge_gm_tmpl_mn MatrixMxN<M, N>::MatrixMxN(double scalars[M][N])
23
+{
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
+    }
29
+}
30
+
31
+uge_gm_tmpl_mn MatrixMxN<M, N>::~MatrixMxN()
32
+{
33
+}
34
+
35
+uge_gm_tmpl_mnt GenericMatrix<M, N, T>::~GenericMatrix()
36
+{
37
+}
38
+
39
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::setScalar(unsigned i, unsigned j, double value)
40
+{
41
+    _scalars[i][j] = value;
42
+    return *getThis();
43
+}
44
+
45
+uge_gm_tmpl_mnt double GenericMatrix<M, N, T>::getScalar(unsigned i, unsigned j) const
46
+{
47
+    return _scalars[i][j];
48
+}
49
+
50
+uge_gm_tmpl_mnt T *GenericMatrix<M, N, T>::getThis() const
51
+{
52
+    return (T*)this;
53
+}
54
+
55
+uge_gm_tmpl_mnt bool GenericMatrix<M, N, T>::isNull() const
56
+{
57
+    return equal(T());
58
+}
59
+
60
+uge_gm_tmpl_mnt bool GenericMatrix<M, N, T>::equal(const T &other) const
61
+{
62
+    for (int i = 0; i < M; ++i) {
63
+        for (int j = 0; j < N; ++j) {
64
+            if (_scalars[i][j] != other._scalars[i][j]) {
65
+                return false;
66
+            }
67
+        }
68
+    }
69
+    return true;
70
+}
71
+
72
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::add(double k)
73
+{
74
+    return add(T(k));
75
+}
76
+
77
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::add(double scalars[M][N])
78
+{
79
+    return add(T(scalars));
80
+}
81
+
82
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::add(const T &other)
83
+{
84
+    for (int i = 0; i < M; ++i) {
85
+        for (int j = 0; j < N; ++j) {
86
+            _scalars[i][j] += other._scalars[i][j];
87
+        }
88
+    }
89
+    return *getThis();
90
+}
91
+
92
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::sub(double k)
93
+{
94
+    return sub(T(k));
95
+}
96
+
97
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::sub(double scalars[M][N])
98
+{
99
+    return sub(T(scalars));
100
+}
101
+
102
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::sub(const T &other)
103
+{
104
+    for (int i = 0; i < M; ++i) {
105
+        for (int j = 0; j < N; ++j) {
106
+            _scalars[i][j] -= other._scalars[i][j];
107
+        }
108
+    }
109
+    return *getThis();
110
+}
111
+
112
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::mult(double k)
113
+{
114
+    for (int i = 0; i < M; ++i) {
115
+        for (int j = 0; j < N; ++j) {
116
+            _scalars[i][j] *= k;
117
+        }
118
+    }
119
+    return *getThis();
120
+}
121
+
122
+uge_gm_tmpl_mnt uge_gm_tmpl_p MatrixMxN<M, P> GenericMatrix<M, N, T>::mult(double scalars[N][P])
123
+{
124
+    return mult(MatrixMxN<N, P>(scalars));
125
+}
126
+
127
+uge_gm_tmpl_mnt uge_gm_tmpl_p MatrixMxN<M, P> GenericMatrix<M, N, T>::mult(const MatrixMxN<N, P>& other)
128
+{
129
+    MatrixMxN<N, P> m;
130
+    return m;
131
+}
132
+
133
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::div(double k)
134
+{
135
+    return div(T(k));
136
+}
137
+
138
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::div(double scalars[M][N])
139
+{
140
+    return div(T(scalars));
141
+}
142
+
143
+uge_gm_tmpl_mnt T &GenericMatrix<M, N, T>::div(const T &other)
144
+{
145
+    for (int i = 0; i < M; ++i) {
146
+        for (int j = 0; j < N; ++j) {
147
+            _scalars[i][j] /= other._scalars[i][j];
148
+        }
149
+    }
150
+    return *getThis();
151
+}

+ 2
- 2
UGameEngine/utils/vectorxd.h 파일 보기

@@ -27,11 +27,11 @@ public:
27 27
 
28 28
     T& mult(double k);
29 29
     T& mult(double scalars[X]);
30
-    T& mult(const T& k);
30
+    T& mult(const T& other);
31 31
 
32 32
     T& div(double k);
33 33
     T& div(double scalars[X]);
34
-    T& div(const T& k);
34
+    T& div(const T& other);
35 35
 
36 36
     double dotProduct(const T& other) const;
37 37
 

+ 17
- 0
tests/main.cpp 파일 보기

@@ -0,0 +1,17 @@
1
+#include <QApplication>
2
+#include <QtTest/QtTest>
3
+
4
+#include "testvector3d.h"
5
+#include "testmatrix3x3.h"
6
+
7
+int main(int argc, char *argv[])
8
+{
9
+    QApplication app(argc, argv);
10
+    app.setAttribute(Qt::AA_Use96Dpi, true);
11
+    QTEST_DISABLE_KEYPAD_NAVIGATION;
12
+    TestVector3D tv3d;
13
+    int rtv3d = QTest::qExec(&tv3d, argc, argv);
14
+    TestMatrix3x3 tm33;
15
+    int rtm33 = QTest::qExec(&tm33, argc, argv);
16
+    return !rtv3d && !rtm33;
17
+}

+ 62
- 0
tests/testmatrix3x3.cpp 파일 보기

@@ -0,0 +1,62 @@
1
+#include <QtTest/QtTest>
2
+#include "utils/matrix3x3.h"
3
+#include "testmatrix3x3.h"
4
+
5
+void TestMatrix3x3::gettersSetters1()
6
+{
7
+    Matrix3x3 m;
8
+    QCOMPARE(m.getScalar(0, 0), 0.0);
9
+    QCOMPARE(m.getScalar(0, 1), 0.0);
10
+    QCOMPARE(m.getScalar(1, 0), 0.0);
11
+}
12
+
13
+void TestMatrix3x3::gettersSetters2()
14
+{
15
+    double scalars[3][3] = {{0.0, 1.0, 2.0},
16
+                            {3.0, 4.0, 5.0},
17
+                            {6.0, 7.0, 8.0}};
18
+    Matrix3x3 m(scalars);
19
+    QCOMPARE(m.getScalar(0, 0), 0.0);
20
+    QCOMPARE(m.getScalar(0, 1), 1.0);
21
+    QCOMPARE(m.getScalar(1, 0), 3.0);
22
+    QCOMPARE(m.getScalar(1, 2), 5.0);
23
+}
24
+
25
+void TestMatrix3x3::gettersSetters3()
26
+{
27
+    Matrix3x3 m;
28
+    m.setScalar(0, 0, 42.0).setScalar(0, 1, 24.0).setScalar(1, 0, 10.0);
29
+    QCOMPARE(m.getScalar(0, 0), 42.0);
30
+    QCOMPARE(m.getScalar(0, 1), 24.0);
31
+    QCOMPARE(m.getScalar(1, 0), 10.0);
32
+}
33
+
34
+void TestMatrix3x3::isNull1()
35
+{
36
+    double scalars[3][3] = {{0.0, 1.0, 2.0},
37
+                            {3.0, 4.0, 5.0},
38
+                            {6.0, 7.0, 8.0}};
39
+    Matrix3x3 m(scalars);
40
+    QCOMPARE(m.isNull(), false);
41
+}
42
+
43
+void TestMatrix3x3::isNull1Op()
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);
50
+}
51
+
52
+void TestMatrix3x3::isNull2()
53
+{
54
+    Matrix3x3 m;
55
+    QCOMPARE(m.isNull(), true);
56
+}
57
+
58
+void TestMatrix3x3::isNull2Op()
59
+{
60
+//    Matrix3x3 m;
61
+//    QCOMPARE((bool)m, true);
62
+}

+ 21
- 0
tests/testmatrix3x3.h 파일 보기

@@ -0,0 +1,21 @@
1
+#ifndef TESTMATRIX3X3_H
2
+#define TESTMATRIX3X3_H
3
+
4
+#include <QObject>
5
+
6
+class TestMatrix3x3 : public QObject
7
+{
8
+    Q_OBJECT
9
+
10
+private slots:
11
+    void gettersSetters1();
12
+    void gettersSetters2();
13
+    void gettersSetters3();
14
+
15
+    void isNull1();
16
+    void isNull1Op();
17
+    void isNull2();
18
+    void isNull2Op();
19
+};
20
+
21
+#endif // TESTMATRIX3X3_H

+ 6
- 2
tests/tests.pro 파일 보기

@@ -13,9 +13,13 @@ TEMPLATE = app
13 13
 
14 14
 
15 15
 SOURCES += \
16
-    testvector3d.cpp
16
+    testvector3d.cpp \
17
+    testmatrix3x3.cpp \
18
+    main.cpp
17 19
 
18
-HEADERS  +=
20
+HEADERS  += \
21
+    testvector3d.h \
22
+    testmatrix3x3.h
19 23
 
20 24
 FORMS    +=
21 25
 

+ 1
- 55
tests/testvector3d.cpp 파일 보기

@@ -1,59 +1,7 @@
1 1
 #include <QtTest/QtTest>
2 2
 #include "utils/vector3d.h"
3 3
 
4
-class TestVector3D : public QObject
5
-{
6
-    Q_OBJECT
7
-
8
-private slots:
9
-    void gettersSetters1();
10
-    void gettersSetters2();
11
-    void gettersSetters3();
12
-
13
-    void isNull1();
14
-    void isNull1Op();
15
-    void isNull2();
16
-    void isNull2Op();
17
-
18
-    void equal1();
19
-    void equal2();
20
-    void equal3();
21
-    void equal4();
22
-    void equal5();
23
-    void equal6();
24
-
25
-    void add1();
26
-    void add1Op();
27
-    void add2();
28
-    void add2Op();
29
-    void add3Op();
30
-
31
-    void sub1();
32
-    void sub1Op();
33
-    void sub2();
34
-    void sub2Op();
35
-    void sub3Op();
36
-
37
-    void mult();
38
-    void mult1Op();
39
-    void mult2Op();
40
-    void mult3Op();
41
-
42
-    void div();
43
-    void divOp();
44
-
45
-    void dotProduct1();
46
-    void dotProduct2();
47
-
48
-    void crossProduct1();
49
-    void crossProduct2();
50
-    void crossProduct3();
51
-
52
-    void norm1();
53
-    void norm2();
54
-
55
-    void getScalarOp();
56
-};
4
+#include "testvector3d.h"
57 5
 
58 6
 void TestVector3D::gettersSetters1()
59 7
 {
@@ -415,5 +363,3 @@ void TestVector3D::getScalarOp()
415 363
     QCOMPARE(v[2], z);
416 364
 }
417 365
 
418
-QTEST_MAIN(TestVector3D)
419
-#include "testvector3d.moc"

+ 60
- 0
tests/testvector3d.h 파일 보기

@@ -0,0 +1,60 @@
1
+#ifndef TESTVECTOR3D_H
2
+#define TESTVECTOR3D_H
3
+
4
+#include <QObject>
5
+
6
+class TestVector3D : public QObject
7
+{
8
+    Q_OBJECT
9
+
10
+private slots:
11
+    void gettersSetters1();
12
+    void gettersSetters2();
13
+    void gettersSetters3();
14
+
15
+    void isNull1();
16
+    void isNull1Op();
17
+    void isNull2();
18
+    void isNull2Op();
19
+
20
+    void equal1();
21
+    void equal2();
22
+    void equal3();
23
+    void equal4();
24
+    void equal5();
25
+    void equal6();
26
+
27
+    void add1();
28
+    void add1Op();
29
+    void add2();
30
+    void add2Op();
31
+    void add3Op();
32
+
33
+    void sub1();
34
+    void sub1Op();
35
+    void sub2();
36
+    void sub2Op();
37
+    void sub3Op();
38
+
39
+    void mult();
40
+    void mult1Op();
41
+    void mult2Op();
42
+    void mult3Op();
43
+
44
+    void div();
45
+    void divOp();
46
+
47
+    void dotProduct1();
48
+    void dotProduct2();
49
+
50
+    void crossProduct1();
51
+    void crossProduct2();
52
+    void crossProduct3();
53
+
54
+    void norm1();
55
+    void norm2();
56
+
57
+    void getScalarOp();
58
+};
59
+
60
+#endif // TESTVECTOR3D_H

Loading…
취소
저장