1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
#include "test.h"
#include "../math/math.h"
#include "../util/time.h"
Mat4 m1 = {
1, 5, 9,13,
2, 6,10,14,
3, 7,11,15,
4, 8,12,16
};
Mat4 m2 = {
16,12, 8, 4,
15,11, 7, 3,
14,10, 6, 2,
13, 9, 5, 1
};
static void test_multiply() {
print("test_multiply\n");
Mat4 res;
internal_mat4_multiply(&m1, &m2, &res);
internal_mat4_print(&res);
}
static void test_invert() {
print("test_invert\n");
Mat4 m = {
0, 3, 0, 0,
2, 7, 0, 1,
8, 1, 1, 0,
6, 0, 2, 1,
};
Mat4 res, res2;
internal_mat4_invertfull(&m, &res);
internal_mat4_print(&res);
internal_mat4_multiply(&m1, &m, &res2);
internal_mat4_multiply(&res2, &res, &res2);
internal_mat4_print(&res2);
/*
-0.250 0.333 1.667 -1.833
0.083 0.000 -0.667 0.833
0.167 -0.000 -0.333 -0.333
-0.083 0.000 0.667 0.167
*/
}
static void test_transpose() {
print("test_transpose\n");
Mat4 res;
internal_mat4_transpose(&m1, &res);
internal_mat4_print(&res);
}
static void test_scale() {
print("test_scale\n");
Mat4 scale;
internal_mat4_setscale(1, 2, 3, &scale);
Mat4 res;
internal_mat4_multiply(&scale, &m1, &res);
internal_mat4_print(&scale);
}
static void test_position() {
print("test_position\n");
Mat4 pos;
internal_mat4_setposition(1, 2, 3, &pos);
internal_mat4_print(&pos);
}
static void test_rotation() {
print("test_rotation\n");
Mat4 i;
internal_mat4_setidentity(&i);
Vec3 axis = { 0, 0, 1 };
internal_mat4_rotate(&i, 90, &axis, &i);
internal_mat4_print(&i);
}
static void test_invertgeneral3d() {
print("test_invertgeneral3d\n");
Mat4 trans;
internal_mat4_setidentity(&trans);
print("original matrix"); internal_mat4_print(&m1);
Vec3 v;
v.x = 1; v.y = 3.3; v.z = 0; internal_mat4_translate(&trans, &v, &trans);
v.x = 1; v.y = 0; v.z = 0; internal_mat4_rotate(&trans, 23, &v, &trans);
v.x = 1; v.y = 1.2; v.z = 1; internal_mat4_scale(&trans, &v, &trans);
v.x = 1; v.y = 3; v.z = 0; internal_mat4_rotate(&trans, 23, &v, &trans);
v.x = 2; v.y = 3.3; v.z = 0; internal_mat4_translate(&trans, &v, &trans);
print("transform matrix"); internal_mat4_print(&trans);
Mat4 res;
internal_mat4_multiply(&trans, &m1, &res);
internal_mat4_invertgeneral3d(&trans, &trans);
print("inverse transform matrix"); internal_mat4_print(&trans);
internal_mat4_multiply(&trans, &res, &res);
print("inverted result"); internal_mat4_print(&res);
}
void test_invertrot() {
print("test_invertrot\n");
print("original matrix"); internal_mat4_print(&m1);
Mat4 rot; internal_mat4_setidentity(&rot);
Vec3 axis = { 1,2,3 }; internal_mat4_rotate(&rot, 90, &axis, &rot);
print("rotation matrix"); internal_mat4_print(&rot);
Mat4 res;
internal_mat4_multiply(&rot, &m1, &res);
print("result matrix"); internal_mat4_print(&res);
internal_mat4_invertgeneral3d(&rot, &rot);
print("inverse rotation matrix"); internal_mat4_print(&rot);
internal_mat4_multiply(&rot, &res, &res);
print("result matrix"); internal_mat4_print(&res);
}
TEST(test_orthogonalize)
ROWMAT(A0,
1, 2, 4, 0,
0, 0, 5, 0,
0, 3, 6, 0,
0, 0, 0, 0
);
Mat4 A;
TIME_STAMP("internal_mat4_orthogonalize time used")
for (int i = 0; i < 1000000; ++i) {
internal_mat4_orthogonalize(&A0, &A);
}
TIME_STAMP_END
TIME_STAMP("internal_mat4_isorthogonal time used")
for (int i = 0; i < 1000000; ++i) {
internal_mat4_isorthogonal(&A0);
}
TIME_STAMP_END
internal_mat4_print(&A);
float res = internal_vec3_dot(&A.basis.y, &A.basis.x);
printf("%f\n", res);
printf("%d\n", internal_mat4_isorthogonal(&A));
Mat4 rev;
internal_mat4_transpose(&A, &rev);
internal_mat4_multiply(&A, &rev, &rev);
internal_mat4_print(&rev);
END
TEST(test_toeuler)
Euler euler = { 90, 0, 33 };
Quat rot;
internal_quat_fromeuler(&euler, &rot);
Mat4 m;
internal_quat_tomat4(&rot, &m);
Euler res;
internal_quat_toeuler(&rot, &res);
internal_euler_print(&res);
END
TEST(test_rotatematrix)
Euler euler = { 10, 20, 30 };
Quat rot;
internal_quat_fromeuler(&euler, &rot);
Mat4 m;
internal_quat_tomat4(&rot, &m);
internal_mat4_print(&m);
Mat4 mz, mx, my;
internal_mat4_setrotatez(30, &mz);
internal_mat4_setrotatex(10, &mx);
internal_mat4_setrotatey(20, &my);
internal_mat4_multiply(&mx, &mz, &mx);
internal_mat4_multiply(&my, &mx, &m);
internal_mat4_print(&m);
internal_mat4_setrotate(10, 20, 30, &m);
internal_mat4_print(&m);
END
TEST(test_isidentity)
ROWMAT(mat,
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
)
bool res;
TIME_STAMP_1000000("test is isidentity")
res = internal_mat4_isidentity(&mat);
TIME_STAMP_END
printf("%d\n", res);
END
void test_mat4() {
print("================================================\n");
print("test_mat4\n");
print("================================================\n");
//test_transpose();
//test_multiply();
//test_invert();
//test_scale();
//test_position();
//test_rotation();
//test_invertgeneral3d();
//test_invertrot();
//test_orthogonalize();
//test_toeuler();
test_rotatematrix();
//test_isidentity();
}
|