Skip to content

Commit cb0f479

Browse files
committed
add test for all the matrix operations
1 parent 280202f commit cb0f479

File tree

2 files changed

+227
-0
lines changed

2 files changed

+227
-0
lines changed

examples/matrixTest/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
3+
# create target
4+
add_executable(matrixTest main.cu)
5+
6+
# link libraries
7+
target_link_libraries(matrixTest mpUtils::mpUtils)
8+

examples/matrixTest/main.cu

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/*
2+
* mpUtils
3+
* main.cpp
4+
*
5+
* @author: Hendrik Schwanekamp
6+
7+
*
8+
* mpUtils = my personal Utillities
9+
* A utility library for my personal c++ projects
10+
*
11+
* Copyright 2016 Hendrik Schwanekamp
12+
*
13+
*/
14+
15+
/*
16+
* This is testing features of the matrix class... to be replaced by actual unit tests in the future...
17+
*/
18+
19+
#include <mpUtils/mpUtils.h>
20+
//#include <mpUtils/mpGraphics.h>
21+
#include <glm/glm.hpp>
22+
#include <mpUtils/mpCuda.h>
23+
24+
using namespace mpu;
25+
using namespace std;
26+
using namespace std::chrono;
27+
28+
int main()
29+
{
30+
Log myLog( LogLvl::ALL, ConsoleSink());
31+
myLog.printHeader("matrixTest", "0.9.1");
32+
33+
yield();
34+
35+
logINFO("MtrixTesting") << "testing the matrix class..";
36+
logINFO("MtrixTesting") << "testing parameters..";
37+
38+
Mat<float,4,6> m1; // 4 by 6 matrix with undefined values
39+
if(m1.size != 4*6)
40+
logERROR("MatrixTesting") << "size is wrong";
41+
if(m1.rows != 4)
42+
logERROR("MatrixTesting") << "rows wrong";
43+
if(m1.cols != 6)
44+
logERROR("MatrixTesting") << "cols wrong";
45+
46+
logINFO("MatrixTesting") << "testing construction..";
47+
48+
logINFO("MatrixTesting") << "constructing unit matrix";
49+
Mat<float,4,4> identitiy(1.0);
50+
myLog.print(LogLvl::INFO) << toString(identitiy);
51+
52+
logINFO("MatrixTesting") << "constructing matrix with 5es in the diagonal";
53+
Mat<float,4,4> m2(5.0);
54+
myLog.print(LogLvl::INFO) << toString(m2);
55+
56+
logINFO("MatrixTesting") << "constructing zero matrix";
57+
Mat<float,4,4> m0(0.0);
58+
myLog.print(LogLvl::INFO) << toString(m0);
59+
60+
logINFO("MatrixTesting") << "constructing matrix as series of numbers from 1 to 18";
61+
Mat<float,6,3> m3( 1.0, 2.0, 3.0,
62+
4.0, 5.0, 6.0,
63+
7.0, 8.0, 9.0,
64+
10.0,11.0,12.0,
65+
13.0,14.0,15.0,
66+
16.0,17.0,18.0);
67+
myLog.print(LogLvl::INFO) << toString(m3);
68+
69+
logINFO("MatrixTesting") << "constructing matrix from glm mat3";
70+
glm::mat3 glmmat(1,2,3,
71+
4,5,6,
72+
7,8,9);
73+
Mat<float,3,3> m4(glmmat);
74+
myLog.print(LogLvl::INFO) << toString(m4);
75+
76+
logINFO("MatrixTesting") << "converting to glm mat3 and back";
77+
glm::mat3 glmmat3(m4);
78+
Mat<float,3,3> convert(glmmat3);
79+
myLog.print(LogLvl::INFO) << mpu::toString(convert);
80+
81+
logINFO("MatrixTesting") << "testing component access..";
82+
logINFO("MatrixTesting") << "manually accessing data of last created matrix using double brackets:";
83+
myLog.print(LogLvl::INFO) << "[0][0]: "<<m4[0][0]<< " [0][1]: "<<m4[0][1]<< " [0][2]: "<<m4[0][2] <<"\n"
84+
<< "[1][0]: "<<m4[1][0]<< " [1][1]: "<<m4[1][1]<< " [1][2]: "<<m4[1][2] <<"\n"
85+
<< "[2][0]: "<<m4[2][0]<< " [2][1]: "<<m4[2][1]<< " [2][2]: "<<m4[2][2] <<"\n";
86+
87+
logINFO("MatrixTesting") << "manually accessing data of last created matrix using round brackets:";
88+
myLog.print(LogLvl::INFO) << " 0: " << m4(0) << ", 1: " << m4(1) << ", 2: " << m4(2) << "\n"
89+
<< " 3: " << m4(3) << ", 4: " << m4(4) << ", 5: " << m4(5) << "\n"
90+
<< " 6: " << m4(6) << ", 7: " << m4(7) << ", 8: " << m4(8) << "\n";
91+
92+
logINFO("MatrixTesting") << "modifing values:";
93+
m4[0][0] = 21;
94+
m4(1)= 42;
95+
myLog.print(LogLvl::INFO) << m4(0) << " " << m4[0][1];
96+
97+
98+
logINFO("MatrixTesting") << "testing logical compare operators..";
99+
logINFO("MatrixTesting") << "expected true, false, false, true";
100+
Mat<float,4,4 > m5(1.0);
101+
myLog.print(LogLvl::INFO) << boolalpha << (m5==identitiy);
102+
myLog.print(LogLvl::INFO) << boolalpha << (m5==m0);
103+
myLog.print(LogLvl::INFO) << boolalpha << (m5!=identitiy);
104+
myLog.print(LogLvl::INFO) << boolalpha << (m5!=m0);
105+
106+
107+
logINFO("MatrixTesting") << "testing arithmetic operators..";
108+
logINFO("MatrixTesting") << "testing addition and subtraction operators..";
109+
logINFO("MatrixTesting") << "using the series of numbers matrix and the identity..";
110+
111+
Mat<float,4,4> m6( 1.0, 2.0, 3.0, 4.0,
112+
5.0, 6.0, 7.0, 8.0,
113+
9.0,10.0,11.0,12.0,
114+
13.0,14.0,15.0,16.0);
115+
116+
logINFO("MatrixTesting") << "plus";
117+
myLog.print(LogLvl::INFO) << toString(identitiy + m6);
118+
119+
logINFO("MatrixTesting") << "minus";
120+
myLog.print(LogLvl::INFO) << toString(identitiy - m6);
121+
122+
logINFO("MatrixTesting") << "plus equal";
123+
auto r1=identitiy;
124+
r1 += m6;
125+
myLog.print(LogLvl::INFO) << toString(r1);
126+
127+
logINFO("MatrixTesting") << "minus equal";
128+
auto r2=identitiy;
129+
r2 -= m6;
130+
myLog.print(LogLvl::INFO) << toString(r2);
131+
132+
logINFO("MatrixTesting") << "testing scalar multiplication..";
133+
logINFO("MatrixTesting") << "using the series of numbers matrix..";
134+
135+
logINFO("MatrixTesting") << "multiply by 2";
136+
myLog.print(LogLvl::INFO) << toString(m6*2);
137+
138+
logINFO("MatrixTesting") << "multiply other way around";
139+
myLog.print(LogLvl::INFO) << toString(2.0f*m6);
140+
141+
logINFO("MatrixTesting") << "divide by 2";
142+
myLog.print(LogLvl::INFO) << toString(m6/2);
143+
144+
logINFO("MatrixTesting") << "multiply equal";
145+
auto r3=m6;
146+
r3*=2;
147+
myLog.print(LogLvl::INFO) << toString(r3);
148+
149+
logINFO("MatrixTesting") << "divide equal";
150+
auto r4=m6;
151+
r4/=2;
152+
myLog.print(LogLvl::INFO) << toString(r4);
153+
154+
logINFO("MatrixTesting") << "testing matrix multiplication..";
155+
logINFO("MatrixTesting") << "using the series of numbers matrix..";
156+
auto r5=m6;
157+
r5*=m6;
158+
r5*=identitiy;
159+
myLog.print(LogLvl::INFO) << toString(r5);
160+
161+
Mat<float,4,2> m7(1,2,3,4,5,6,7,8);
162+
myLog.print(LogLvl::INFO) << toString(m6*m7);
163+
164+
logINFO("MatrixTesting") << "testing matrix transpose..";
165+
myLog.print(LogLvl::INFO) << toString(transpose(m6));
166+
167+
logINFO("MatrixTesting") << "testing component wise multiplication..";
168+
myLog.print(LogLvl::INFO) << toString(compWiseMult(m6, identitiy));
169+
170+
logINFO("MatrixTesting") << "testing invert of 2x2..";
171+
Mat<float,2,2> m8( 0,5,
172+
5,0);
173+
myLog.print(LogLvl::INFO) << toString(invert(m8));
174+
myLog.print(LogLvl::INFO) << toString(m8*invert(m8));
175+
myLog.print(LogLvl::INFO) << toString(invert(m8)*m8);
176+
177+
Mat<float,4,4> m9( 0,0,0,5,
178+
0,0,5,0,
179+
0,5,0,0,
180+
5,0,0,0);
181+
logINFO("MatrixTesting") << "testing invert of 4x4..";
182+
myLog.print(LogLvl::INFO) << toString(invert(m9));
183+
myLog.print(LogLvl::INFO) << toString(m9*invert(m9));
184+
myLog.print(LogLvl::INFO) << toString(invert(m9)*m9);
185+
186+
logINFO("MatrixTesting") << "testing matrix vector multiplication..";
187+
Mat<float,2,2> m10(2,4,3,5);
188+
Mat<float,3,3> m11(2,4,6,3,5,7,4,6,8);
189+
Mat<float,4,4> m12(2,4,6,7,
190+
3,5,7,8,
191+
4,6,8,9,
192+
5,7,9,10);
193+
194+
float2 f{3,5};
195+
f = m10*f;
196+
myLog.print(LogLvl::INFO) << f.x << ", " << f.y;
197+
198+
float3 f2{3,5,7};
199+
f2 = m11*f2;
200+
myLog.print(LogLvl::INFO) << f2.x << ", " << f2.y << ", " << f2.z;
201+
202+
float4 f3{3,5,7,9};
203+
f3 = m12*f3;
204+
myLog.print(LogLvl::INFO) << f3.x << ", " << f3.y << ", " << f3.z << ", " << f3.w;
205+
206+
glm::vec2 g2(3,5);
207+
g2 = m10 * g2;
208+
myLog.print(LogLvl::INFO) << g2.x << ", " << g2.y;
209+
210+
glm::vec3 g3(3,5,7);
211+
g3 = m11 * g3;
212+
myLog.print(LogLvl::INFO) << g3.x << ", " << g3.y << ", " << g3.z;
213+
214+
glm::vec4 g4(3,5,7,9);
215+
g4 = m12 * g4;
216+
myLog.print(LogLvl::INFO) << g4.x << ", " << g4.y << ", " << g4.z << ", " << g4.w;
217+
218+
return 0;
219+
}

0 commit comments

Comments
 (0)