2012-05-09 03:06:17 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-05-10 05:42:40 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2012-05-09 03:06:17 +00:00
|
|
|
#include "QF/mathlib.h"
|
|
|
|
|
|
|
|
//PITCH YAW ROLL
|
|
|
|
static vec3_t test_angles[] = {
|
|
|
|
{ 0, 0, 0},
|
|
|
|
{45, 0, 0},
|
|
|
|
{ 0, 45, 0},
|
|
|
|
{ 0, 0, 45},
|
|
|
|
{45, 45, 0},
|
|
|
|
{ 0, 45, 45},
|
|
|
|
{45, 0, 45},
|
|
|
|
{45, 45, 45},
|
|
|
|
{0, 180, 180},
|
|
|
|
{180, 0, 180},
|
|
|
|
{180, 180, 0},
|
|
|
|
};
|
|
|
|
#define num_angle_tests \
|
|
|
|
(sizeof (test_angles) / sizeof (test_angles[0]))
|
|
|
|
|
2012-05-09 10:07:27 +00:00
|
|
|
static vec3_t test_scales[] = {
|
|
|
|
{ 1, 1, 1},
|
|
|
|
{ 2, 1, 1},
|
|
|
|
{ 1, 2, 1},
|
|
|
|
{ 1, 1, 2},
|
|
|
|
{ 2, 2, 1},
|
|
|
|
{ 1, 2, 2},
|
|
|
|
{ 2, 1, 2},
|
|
|
|
{ 2, 2, 2},
|
|
|
|
{ 1, 2, 3},
|
|
|
|
{ 1, 3, 2},
|
|
|
|
{ 2, 1, 3},
|
|
|
|
{ 2, 3, 1},
|
|
|
|
{ 3, 1, 2},
|
|
|
|
{ 3, 2, 1},
|
|
|
|
};
|
2012-08-18 07:29:57 +00:00
|
|
|
#define num_scale_tests \
|
|
|
|
(sizeof (test_scales) / sizeof (test_scales[0]))
|
2012-05-09 10:07:27 +00:00
|
|
|
|
2012-05-09 03:06:17 +00:00
|
|
|
// return true if a and b are close enough (yay, floats)
|
|
|
|
static int
|
|
|
|
compare (vec_t a, vec_t b)
|
|
|
|
{
|
|
|
|
vec_t diff = a - b;
|
|
|
|
return diff * diff < 0.001;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
test_angle (const vec3_t angles)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
quat_t rotation, r;
|
2012-08-18 07:29:57 +00:00
|
|
|
vec3_t scale, shear;
|
|
|
|
mat3_t mat;
|
2012-05-09 03:06:17 +00:00
|
|
|
|
|
|
|
AngleQuat (angles, rotation);
|
2012-08-18 07:29:57 +00:00
|
|
|
QuatToMatrix (rotation, mat, 0, 1);
|
|
|
|
Mat3Decompose (mat, r, shear, scale);
|
2012-05-09 03:06:17 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
if (!compare (rotation[i], r[i]))
|
|
|
|
goto negate;
|
|
|
|
return 1;
|
|
|
|
negate:
|
2012-08-18 07:29:57 +00:00
|
|
|
// Mat3Decompose always sets the rotation quaternion's scalar to +ve
|
2012-05-09 03:06:17 +00:00
|
|
|
// but AngleQuat might produce a -ve scalar.
|
|
|
|
QuatNegate (r, r);
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
if (!compare (rotation[i], r[i]))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
fail:
|
|
|
|
printf ("\n\n(%g %g %g)\n", VectorExpand (angles));
|
|
|
|
printf (" [%g %g %g %g]\n", QuatExpand (rotation));
|
2012-08-18 07:29:57 +00:00
|
|
|
printf (" [%g %g %g %g] [%g %g %g] [%g %g %g]\n",
|
|
|
|
QuatExpand (r), VectorExpand (scale), VectorExpand (shear));
|
2012-05-09 03:06:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-09 10:07:27 +00:00
|
|
|
static int
|
2012-08-18 07:29:57 +00:00
|
|
|
test_transform (const vec3_t angles, const vec3_t scale)
|
2012-05-09 10:07:27 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const vec3_t v = {4,5,6};
|
|
|
|
vec3_t x, y;
|
|
|
|
quat_t rotation;
|
2012-08-18 07:29:57 +00:00
|
|
|
mat3_t mat;
|
2012-05-09 10:07:27 +00:00
|
|
|
|
|
|
|
VectorCopy (v, x);
|
|
|
|
AngleQuat (angles, rotation);
|
|
|
|
VectorCompMult (scale, x, x);
|
|
|
|
QuatMultVec (rotation, x, x);
|
|
|
|
|
2012-08-18 07:29:57 +00:00
|
|
|
Mat3Init (rotation, scale, mat);
|
|
|
|
Mat3MultVec (mat, v, y);
|
2012-05-09 10:07:27 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
if (!compare (x[i], y[i]))
|
|
|
|
goto fail;
|
2012-05-09 13:28:22 +00:00
|
|
|
|
2012-05-09 10:07:27 +00:00
|
|
|
return 1;
|
|
|
|
fail:
|
2012-08-18 07:29:57 +00:00
|
|
|
printf ("\n\n(%g %g %g) (%g %g %g)\n", VectorExpand (angles),
|
|
|
|
VectorExpand (scale));
|
2012-05-09 10:07:27 +00:00
|
|
|
printf (" (%g %g %g)\n", VectorExpand (x));
|
|
|
|
printf (" (%g %g %g)\n", VectorExpand (y));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-09 13:28:22 +00:00
|
|
|
static int
|
2012-08-18 07:29:57 +00:00
|
|
|
test_transform2 (const vec3_t angles, const vec3_t scale)
|
2012-05-09 13:28:22 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const vec3_t v = {4,5,6};
|
|
|
|
vec3_t x, y;
|
|
|
|
quat_t rotation;
|
2012-08-18 07:29:57 +00:00
|
|
|
mat3_t mat;
|
|
|
|
vec3_t rot, sc, sh;
|
2012-05-09 13:28:22 +00:00
|
|
|
|
|
|
|
VectorCopy (v, x);
|
|
|
|
AngleQuat (angles, rotation);
|
|
|
|
VectorCompMult (scale, x, x);
|
|
|
|
QuatMultVec (rotation, x, x);
|
|
|
|
|
2012-08-18 07:29:57 +00:00
|
|
|
Mat3Init (rotation, scale, mat);
|
|
|
|
Mat3Decompose (mat, rot, sh, sc);
|
2012-05-09 13:28:22 +00:00
|
|
|
|
|
|
|
VectorCopy (v, y);
|
|
|
|
QuatMultVec (rot, y, y);
|
|
|
|
VectorShear (sh, y, y);
|
|
|
|
VectorCompMult (sc, y, y);//scale
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
if (!compare (x[i], y[i]))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
fail:
|
2012-08-18 07:29:57 +00:00
|
|
|
printf ("\n\n(%g %g %g) (%g %g %g) (%g %g %g)\n",
|
|
|
|
VectorExpand (angles), VectorExpand (scale), VectorExpand (v));
|
2012-05-09 13:28:22 +00:00
|
|
|
printf (" (%g %g %g)\n", VectorExpand (x));
|
|
|
|
printf (" (%g %g %g)\n", VectorExpand (y));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-10 05:42:40 +00:00
|
|
|
static int
|
2012-08-18 07:29:57 +00:00
|
|
|
test_inverse (const vec3_t angles, const vec3_t scale)
|
2012-05-10 05:42:40 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
quat_t rotation;
|
2012-08-18 07:29:57 +00:00
|
|
|
mat3_t mat, inv, I, res;
|
2012-05-10 05:42:40 +00:00
|
|
|
|
|
|
|
AngleQuat (angles, rotation);
|
2012-08-18 07:29:57 +00:00
|
|
|
Mat3Init (rotation, scale, mat);
|
2012-05-10 05:42:40 +00:00
|
|
|
|
2012-08-18 07:29:57 +00:00
|
|
|
Mat3Identity (I);
|
|
|
|
Mat3Inverse (mat, inv);
|
|
|
|
Mat3Mult (mat, inv, res);
|
2012-05-10 05:42:40 +00:00
|
|
|
|
2012-08-18 07:29:57 +00:00
|
|
|
for (i = 0; i < 3 * 3; i++)
|
2012-05-10 05:42:40 +00:00
|
|
|
if (!compare (I[i], res[i]))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
fail:
|
2012-08-18 07:29:57 +00:00
|
|
|
printf ("\n\n(%g %g %g) (%g %g %g)\n",
|
|
|
|
VectorExpand (angles), VectorExpand (scale));
|
|
|
|
printf (" [%g %g %g]\n [%g %g %g]\n [%g %g %g]\n\n", Mat3Expand (mat));
|
|
|
|
printf (" [%g %g %g]\n [%g %g %g]\n [%g %g %g]\n\n", Mat3Expand (inv));
|
|
|
|
printf (" [%g %g %g]\n [%g %g %g]\n [%g %g %g]\n\n", Mat3Expand (res));
|
2012-05-10 05:42:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-09 03:06:17 +00:00
|
|
|
int
|
|
|
|
main (int argc, const char **argv)
|
|
|
|
{
|
|
|
|
int res = 0;
|
2012-08-18 07:29:57 +00:00
|
|
|
size_t i, j;
|
2012-05-09 03:06:17 +00:00
|
|
|
|
|
|
|
for (i = 0; i < num_angle_tests; i ++) {
|
|
|
|
if (!test_angle (test_angles[i]))
|
|
|
|
res = 1;
|
|
|
|
}
|
2012-05-09 10:07:27 +00:00
|
|
|
for (i = 0; i < num_angle_tests; i ++) {
|
2012-08-18 07:29:57 +00:00
|
|
|
for (j = 0; j < num_scale_tests; j ++) {
|
|
|
|
if (!test_transform (test_angles[i], test_scales[j]))
|
|
|
|
res = 1;
|
2012-05-09 10:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-09 13:28:22 +00:00
|
|
|
for (i = 0; i < num_angle_tests; i ++) {
|
2012-08-18 07:29:57 +00:00
|
|
|
for (j = 0; j < num_scale_tests; j ++) {
|
|
|
|
if (!test_transform2 (test_angles[i], test_scales[j]))
|
|
|
|
res = 1;
|
2012-05-09 13:28:22 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-10 05:42:40 +00:00
|
|
|
for (i = 0; i < num_angle_tests; i ++) {
|
2012-08-18 07:29:57 +00:00
|
|
|
for (j = 0; j < num_scale_tests; j ++) {
|
|
|
|
if (!test_inverse (test_angles[i], test_scales[j]))
|
|
|
|
res = 1;
|
2012-05-10 05:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-09 03:06:17 +00:00
|
|
|
return res;
|
|
|
|
}
|