Example #1
0
void m3dTest::gramSchmidtTest()
{
	using namespace m3d;
	Vec3f dir(frand(), frand(), frand());
	Vec3f pos(frand(), frand(), frand());
	Mat4f matrix = Mat4f::gramSchmidt(dir, pos);

	// check if all axes are perpendicular
	CPPUNIT_ASSERT(fabs(matrix.getX() * matrix.getY()) < EPSILON);
	CPPUNIT_ASSERT(fabs(matrix.getY() * matrix.getZ()) < EPSILON);
	CPPUNIT_ASSERT(fabs(matrix.getX() * matrix.getZ()) < EPSILON);

	CPPUNIT_ASSERT(fabs(matrix.getX().len() - 1.0f) < EPSILON);
	CPPUNIT_ASSERT(fabs(matrix.getY().len() - 1.0f) < EPSILON);
	CPPUNIT_ASSERT(fabs(matrix.getZ().len() - 1.0f) < EPSILON);

	for (int x = 0; x < 4; ++x) {
		for (int y = 0; y < 4; ++y) {

			// check for NaN
			CPPUNIT_ASSERT(matrix[x][y] == matrix[x][y]);
		}
	}

	// check if position is correct
	CPPUNIT_ASSERT(matrix.getW() == pos);
}
Example #2
0
void m3dTest::quaternionTest()
{
	using namespace m3d;

	// generate first matrix and quaternion
	Vec3f axis(frand(), frand(), frand());
	float angle = frand(0, 2.0f*PI);
	Mat4f input1 = Mat4f::rotAxis(axis, angle);
	Quatf quat1(axis, angle);

	// generate second matrix and quaternion
	Vec3f axis2(frand(), frand(), frand());
	float angle2 = frand(0, 2.0f*PI);
	Mat4f input2 = Mat4f::rotAxis(axis2, angle2);
	Quatf quat2(axis2, angle2);

	// multiply them
	Mat4f input = input1 * input2;
	Quatf quat = quat1 * quat2;

	Mat4f output = quat.mat4();

	// normalize all axes
	input.setX(input.getX().normalized());
	input.setY(input.getY().normalized());
	input.setZ(input.getZ().normalized());

	output.setX(input.getX().normalized());
	output.setY(input.getY().normalized());
	output.setZ(input.getZ().normalized());

	// compare
	for (int x = 0; x < 4; ++x) {
		for (int y = 0; y < 4; ++y) {
			// check if equal, allow a reasonable deviation
			CPPUNIT_ASSERT(fabs(input[x][y] - output[x][y]) < EPSILON);

			// check for NaN
			CPPUNIT_ASSERT(output[x][y] == output[x][y]);
		}
	}
}