-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.pwn
373 lines (310 loc) · 15.5 KB
/
test.pwn
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#define RUN_TESTS
#include <console>
#include <streamer>
#include <YSI_Core\y_testing>
#include "rotation"
#include "rotation_misc"
#include "rotation_extra"
forward bool: CheckOrPrintAxisAngle(const comment[], Float: a1, Float: x1, Float: y1, Float: z1, Float: a2, Float: x2, Float: y2, Float: z2);
forward bool: CheckOrPrintEuler(const comment[], Float: x1, Float: y1, Float: z1, Float: x2, Float: y2, Float: z2);
forward bool: CheckOrPrintQuat(const comment[], Float: w1, Float: x1, Float: y1, Float: z1, Float: w2, Float: x2, Float: y2, Float: z2);
forward bool: CheckOrPrintMatrix(const comment[], const Float: matrix1[3][3], const Float: matrix2[3][3]);
Test:Compile() { // all functions which should call all subfunctions
new Float: matrix[4][4];
new rotation[E_ROTATION];
new Float: w, Float: x, Float: y, Float: z;
// rotation.inc
SetRotation(rotation, rtype_axis_angle, w, x, y, z); // SetRotationFromAxisAngle
SetRotation(rotation, rtype_euler_samp, x, y, z); // SetRotationFromEuler
SetRotation(rotation, rtype_quaternion, w, x, y, z); // SetRotationFromQuat
SetRotation(rotation, rtype_rotation_matrix, matrix); // SetRotationFromMatrix
ConvertRotation(rotation, rtype_euler_samp, rotation); // ConvertMatrixToEuler
ConvertRotation(rotation, rtype_quaternion, rotation); // ConvertEulerToQuat
ConvertRotation(rotation, rtype_axis_angle, rotation); // ConvertQuatToAxisAngle
GetRotation(rotation, rtype_axis_angle, w, x, y, z); // GetAxisAngleFromRotation
RotatePoint(rotation, x, y, z, x, y, z, x, y, z); // RotateAxisAngle
ReverseRotation(rotation, rotation); // ReverseAxisAngle
ConvertRotation(rotation, rtype_rotation_matrix, rotation); // ConvertAxisAngleToMatrix
ConvertRotation(rotation, rtype_quaternion, rotation); // ConvertMatrixToQuat
ConvertRotation(rotation, rtype_euler_samp, rotation); // ConvertQuatToEuler
GetRotation(rotation, rtype_euler_samp, x, y, z); // GetEulerFromRotation
ReverseRotation(rotation, rotation); // ReverseEuler
ConvertRotation(rotation, rtype_axis_angle, rotation); // ConvertEulerToAxisAngle
ConvertRotation(rotation, rtype_quaternion, rotation); // ConvertAxisAngleToQuat
GetRotation(rotation, rtype_quaternion, w, x, y, z); // GetQuatFromRotation
RotatePoint(rotation, x, y, z, x, y, z, x, y, z); // RotateQuat
CombineRotation(rotation, rotation, rotation); // CombineQuat
ReverseRotation(rotation, rotation); // ReverseQuat
ConvertRotation(rotation, rtype_rotation_matrix, rotation); // ConvertQuatToMatrix
ConvertRotation(rotation, rtype_axis_angle, rotation); // ConvertMatrixToAxisAngle
ConvertRotation(rotation, rtype_euler_samp, rotation); // ConvertAxisAngleToEuler
ConvertRotation(rotation, rtype_euler_xyx, rotation); // ConvertEulerToEuler
ConvertRotation(rotation, rtype_rotation_matrix, rotation); // ConvertEulerToMatrix
GetRotation(rotation, rtype_rotation_matrix, matrix); // GetMatrixFromRotation
RotatePoint(rotation, x, y, z, x, y, z, x, y, z); // RotateMatrix
CombineRotation(rotation, rotation, rotation); // CombineMatrix
ReverseRotation(rotation, rotation); // ReverseMatrix
// rotation_misc.inc
QuatNormalise(w, x, y, z);
QuatScale(w, x, y, z, 5.0);
QuatMul(w, x, y, z, w, x, y, z, w, x, y, z);
QuatAdd(w, x, y, z, w, x, y, z, w, x, y, z);
MatrixMul(matrix, matrix, matrix);
MatrixAdd(matrix, matrix, matrix);
MatrixSub(matrix, matrix, matrix);
RotationMatrixX(matrix, w);
RotationMatrixY(matrix, w);
RotationMatrixZ(matrix, w);
ScalerMatrix(matrix, w, x, y, z);
TranslationMatrix(matrix, x, y, z);
// rotation_extra.inc
GetAttachedPos(x, y, z, rotation, x, y, z, rotation, x, y, z, rotation);
GetAttachedOffset(x, y, z, rotation, x, y, z, rotation, x, y, z, rotation);
GetObjectAttachedPos(0, x, y, z, x, y, z, x, y, z, x, y, z);
GetObjectAttachedOffset(0, x, y, z, x, y, z, x, y, z, x, y, z);
GetPlayerAttachedPos(0, x, y, z, x, y, z, x, y, z, x, y, z);
GetPlayerAttachedOffset(0, x, y, z, x, y, z, x, y, z, x, y, z);
GetVehicleAttachedPos(0, x, y, z, x, y, z, x, y, z, x, y, z);
GetVehicleAttachedOffset(0, x, y, z, x, y, z, x, y, z, x, y, z);
AttachObjectToObjectEx(0, 0);
AttachObjectToPlayerEx(0, 0);
AttachObjectToVehicleEx(0, 0);
AttachPlayerObjectToVehicleEx(0, 0, 0);
GetVehicleRelativePos(0, x, y, z, x, y, z);
GetVehicleForwardVector(0, x, y, z);
GetVehicleRightVector(0, x, y, z);
GetVehicleUpVector(0, x, y, z);
GetDynamicObjectAttachedPos(0, x, y, z, x, y, z, x, y, z, x, y, z);
GetDynamicObjectAttachedOffset(0, x, y, z, x, y, z, x, y, z, x, y, z);
GetDynamicObjectPosRot(0, x, y, z, x, y, z);
DetachDynamicObject(0, _: x);
AttachDynamicObjectToObjectEx(0, 0);
AttachDynamicObjectToPlayerEx(0, 0);
AttachDynamicObjectToVehicleEx(0, 0);
}
Test:ConvertRotation() { // check all conversion functions, although occasionally some test fail due to floating point inaccuracy
new comment[32];
new src[E_ROTATION];
new dest[E_ROTATION];
new rotationtype: type;
new Float: src_angle = random(18000) / 100.0;
new Float: src_w = (random(200) - 100) / 100.0;
new Float: src_x = (random(200) - 100) / 100.0;
new Float: src_y = (random(200) - 100) / 100.0;
new Float: src_z = (random(200) - 100) / 100.0;
new Float: src_a = random(36000) / 100.0;
new Float: src_b = random(36000) / 100.0;
new Float: src_g = random(36000) / 100.0;
new Float: src_matrix[3][3];
new Float: dest_angle;
new Float: dest_w;
new Float: dest_x;
new Float: dest_y;
new Float: dest_z;
new Float: dest_a;
new Float: dest_b;
new Float: dest_g;
new Float: dest_matrix[3][3];
for(new i, j; i < sizeof src_matrix; ++i) {
for(j = 0; j < sizeof src_matrix[]; ++j) {
src_matrix[i][j] = (random(200) - 100) / 100.0;
}
}
SetRotation(src, rtype_axis_angle, src_angle, src_x, src_y, src_z); // set / get to go through normalization
GetRotation(src, rtype_axis_angle, src_angle, src_x, src_y, src_z);
for(type = rotationtype: 0, comment = "rtype_axis_angle_"; type < rotationtype; ++type) {
if(type != rtype_axis_angle) {
dest = src;
valstr(comment[17], _: type);
ConvertRotation(dest, type, dest);
GetRotation(dest, rtype_axis_angle, dest_angle, dest_x, dest_y, dest_z);
ASSERT(CheckOrPrintAxisAngle(comment, src_angle, src_x, src_y, src_z, dest_angle, dest_x, dest_y, dest_z));
}
}
for(new rotationtype: euler = rtype_euler_xzx; euler <= rtype_euler_samp; ++euler) {
SetRotation(src, euler, src_a, src_b, src_g);
ConvertRotation(src, rtype_quaternion, src);
GetRotation(src, euler, src_a, src_b, src_g);
format(comment, sizeof comment, "rtype_euler_%02d_", _: euler);
for(type = rotationtype: 0; type < rotationtype; ++type) {
if(type != euler) {
dest = src;
valstr(comment[15], _: type);
ConvertRotation(dest, type, dest);
GetRotation(dest, euler, dest_a, dest_b, dest_g);
ASSERT(CheckOrPrintEuler(comment, src_a, src_b, src_g, dest_a, dest_b, dest_g));
}
}
}
SetRotation(src, rtype_quaternion, src_w, src_x, src_y, src_z);
GetRotation(src, rtype_quaternion, src_w, src_x, src_y, src_z);
for(type = rotationtype: 0, comment = "rtype_quaternion_"; type < rotationtype; ++type) {
if(type != rtype_quaternion) {
dest = src;
valstr(comment[17], _: type);
ConvertRotation(dest, type, dest);
GetRotation(dest, rtype_quaternion, dest_w, dest_x, dest_y, dest_z);
ASSERT(CheckOrPrintQuat(comment, src_w, src_x, src_y, src_z, dest_w, dest_x, dest_y, dest_z));
}
}
SetRotation(src, rtype_rotation_matrix, src_matrix);
GetRotation(src, rtype_rotation_matrix, src_matrix);
for(type = rotationtype: 0, comment = "rtype_rotation_matrix_"; type < rotationtype; ++type) {
if(type != rtype_rotation_matrix) {
dest = src;
valstr(comment[22], _: type);
ConvertRotation(dest, type, dest);
GetRotation(dest, rtype_rotation_matrix, dest_matrix);
ASSERT(CheckOrPrintMatrix(comment, src_matrix, dest_matrix));
}
}
}
Test:RotatePoint() {
new rotation[E_ROTATION];
new Float: matrix[4][4];
new comment[16] = "RotatePoint_";
new Float: pX = (random(5000) - 2500) / 100.0;
new Float: pY = (random(5000) - 2500) / 100.0;
new Float: pZ = (random(5000) - 2500) / 100.0;
new Float: src_oX;
new Float: src_oY;
new Float: src_oZ;
new Float: dest_oX;
new Float: dest_oY;
new Float: dest_oZ;
// check if all different rotation function result in the same output
SetRotation(rotation, rtype_euler_samp, random(36000) / 100.0, random(36000) / 100.0, random(36000) / 100.0);
RotatePoint(rotation, 0.0, 0.0, 0.0, pX, pY, pZ, src_oX, src_oY, src_oZ);
for(new rotationtype: type; type < rotationtype; ++type) {
valstr(comment[12], _: type);
ConvertRotation(rotation, type, rotation);
RotatePoint(rotation, 0.0, 0.0, 0.0, pX, pY, pZ, dest_oX, dest_oY, dest_oZ);
ASSERT(CheckOrPrintEuler(comment, src_oX, src_oY, src_oZ, dest_oX, dest_oY, dest_oZ));
}
// check if the rotation is actually correct
SetRotation(rotation, rtype_axis_angle, 180.0, 0.0, 0.0, 1.0);
RotatePoint(rotation, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, dest_oX, dest_oY, dest_oZ);
ASSERT(CheckOrPrintEuler("rtype_axis_angle", dest_oX, dest_oY, dest_oZ, -1.0, 0.0, 0.0));
SetRotation(rotation, rtype_euler_samp, 0.0, 0.0, 180.0);
RotatePoint(rotation, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, dest_oX, dest_oY, dest_oZ);
ASSERT(CheckOrPrintEuler("rtype_euler_samp", dest_oX, dest_oY, dest_oZ, -1.0, 2.0, 0.0));
SetRotation(rotation, rtype_euler_zxy, 180.0, 0.0, 0.0);
RotatePoint(rotation, 0.0, -1.0, 0.0, 1.0, 0.0, 0.0, dest_oX, dest_oY, dest_oZ);
ASSERT(CheckOrPrintEuler("rtype_euler_zxy", dest_oX, dest_oY, dest_oZ, -1.0, -2.0, 0.0));
SetRotation(rotation, rtype_quaternion, 0.0, 0.0, 0.0, 1.0);
RotatePoint(rotation, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0, dest_oX, dest_oY, dest_oZ);
ASSERT(CheckOrPrintEuler("rtype_quaternion", dest_oX, dest_oY, dest_oZ, -3.0, 0.0, 0.0));
RotationMatrixZ(matrix, 180.0);
SetRotation(rotation, rtype_rotation_matrix, matrix);
RotatePoint(rotation, -1.0, -1.0, 0.0, 1.0, 0.0, 0.0, dest_oX, dest_oY, dest_oZ);
ASSERT(CheckOrPrintEuler("rtype_rotation_matrix", dest_oX, dest_oY, dest_oZ, -3.0, -2.0, 0.0));
}
Test:CombineRotation() {
new Float: dest_oX;
new Float: dest_oY;
new Float: dest_oZ;
new rot1[E_ROTATION];
new rot2[E_ROTATION];
new rotation[E_ROTATION];
SetRotation(rot1, rtype_euler_zxz, 0.0, 0.0, 90.0);
SetRotation(rot2, rtype_euler_zxz, 0.0, 90.0, 0.0);
CombineRotation(rot2, rot1, rotation); // CombineQuat - global frame of reference = [second Rotation] * [first Rotation]
RotatePoint(rotation, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, dest_oX, dest_oY, dest_oZ);
ASSERT(CheckOrPrintEuler("CombineRotation global", dest_oX, dest_oY, dest_oZ, 0.0, 0.0, 1.0));
ConvertRotation(rot2, rtype_rotation_matrix, rot2);
CombineRotation(rot1, rot2, rotation); // CombineMatrix - local frame-of-reference = [first Rotation] * [second Rotation]
RotatePoint(rotation, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, dest_oX, dest_oY, dest_oZ);
ASSERT(CheckOrPrintEuler("CombineRotation local", dest_oX, dest_oY, dest_oZ, 0.0, 1.0, 0.0));
}
Test:ReverseRotation() {
new rotation[E_ROTATION];
new comment[32] = "ReverseRotation_";
new Float: dest_oX = (random(5000) - 2500) / 100.0;
new Float: dest_oY = (random(5000) - 2500) / 100.0;
new Float: dest_oZ = (random(5000) - 2500) / 100.0;
SetRotation(rotation, rtype_euler_yxz, dest_oX, dest_oY, dest_oZ);
for(new rotationtype: type; type < rotationtype; ++type) {
valstr(comment[16], _: type);
ConvertRotation(rotation, type, rotation);
RotatePoint(rotation, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, dest_oX, dest_oY, dest_oZ);
ReverseRotation(rotation, rotation);
RotatePoint(rotation, 0.0, 0.0, 0.0, dest_oX, dest_oY, dest_oZ, dest_oX, dest_oY, dest_oZ);
ASSERT(CheckOrPrintEuler(comment, dest_oX, dest_oY, dest_oZ, 1.0, 0.0, 0.0));
}
}
#define EPSILON 0.001
bool: CheckOrPrintEuler(const comment[], Float: x1, Float: y1, Float: z1, Float: x2, Float: y2, Float: z2) {
new
Float: x = x1 - x2,
Float: y = y1 - y2,
Float: z = z1 - z2
;
while(x >= 360.0) x -= 360.0;
while(y >= 360.0) y -= 360.0;
while(z >= 360.0) z -= 360.0;
while(x <= -EPSILON) x += 360.0;
while(y <= -EPSILON) y += 360.0;
while(z <= -EPSILON) z += 360.0;
return (
-EPSILON < x < EPSILON && -EPSILON < y < EPSILON && -EPSILON < z < EPSILON
) || printf("%s\nvec1 - %4.4f %4.4f %4.4f\nvec2 - %4.4f %4.4f %4.4f\ndiff - %4.4f %4.4f %4.4f", comment, x1, y1, z1, x2, y2, z2, x, y, z);
}
bool: CheckOrPrintMatrix(const comment[], const Float: matrix1[3][3], const Float: matrix2[3][3]) {
new
Float: matrix[3][3]
;
MatrixSub(matrix1, matrix2, matrix);
return (
-EPSILON < matrix[0][0] < EPSILON && -EPSILON < matrix[0][1] < EPSILON && -EPSILON < matrix[0][2] < EPSILON &&
-EPSILON < matrix[1][0] < EPSILON && -EPSILON < matrix[1][1] < EPSILON && -EPSILON < matrix[1][2] < EPSILON &&
-EPSILON < matrix[2][0] < EPSILON && -EPSILON < matrix[2][1] < EPSILON && -EPSILON < matrix[2][2] < EPSILON
) || printf("%s\n%4.4f %4.4f %4.4f\n%4.4f %4.4f %4.4f\n%4.4f %4.4f %4.4f", comment,
matrix[0][0], matrix[0][1], matrix[0][2],
matrix[1][0], matrix[1][1], matrix[1][2],
matrix[2][0], matrix[2][1], matrix[2][2]
);
}
bool: CheckOrPrintQuat(const comment[], Float: w1, Float: x1, Float: y1, Float: z1, Float: w2, Float: x2, Float: y2, Float: z2) {
new
Float: w,
Float: x,
Float: y,
Float: z
;
if((w1 * w2) < 0.0) {
w = w1 + w2;
x = x1 + x2;
y = y1 + y2;
z = z1 + z2;
} else {
w = w1 - w2;
x = x1 - x2;
y = y1 - y2;
z = z1 - z2;
}
return (
-EPSILON < w < EPSILON && -EPSILON < x < EPSILON && -EPSILON < y < EPSILON && -EPSILON < z < EPSILON
) || printf("%s\nquat1 - %4.4f %4.4f %4.4f %4.4f\nquat2 - %4.4f %4.4f %4.4f %4.4f\n diff - %4.4f %4.4f %4.4f %4.4f", comment, w1, x1, y1, z1, w2, x2, y2, z2, w, x, y, z);
}
bool: CheckOrPrintAxisAngle(const comment[], Float: a1, Float: x1, Float: y1, Float: z1, Float: a2, Float: x2, Float: y2, Float: z2) {
new
Float: a,
Float: x,
Float: y,
Float: z
;
if((x1 * x2) < 0.0) {
a = 360.0 - a1 - a2;
x = x1 + x2;
y = y1 + y2;
z = z1 + z2;
} else {
a = a1 - a2;
x = x1 - x2;
y = y1 - y2;
z = z1 - z2;
}
return (
-EPSILON < a < EPSILON && -EPSILON < x < EPSILON && -EPSILON < y < EPSILON && -EPSILON < z < EPSILON
) || printf("%s\naangle1 - %4.4f %4.4f %4.4f %4.4f\naangle2 - %4.4f %4.4f %4.4f %4.4f\n diff - %4.4f %4.4f %4.4f %4.4f", comment, a1, x1, y1, z1, a2, x2, y2, z2, a, x, y, z);
}