Merge pull request #5300 from hyperlogic/ajt/transform-unit-test

Added unit test for shared/transform.
This commit is contained in:
samcake 2015-07-10 11:35:13 -07:00
commit 09ea9b751f
2 changed files with 136 additions and 0 deletions

View file

@ -0,0 +1,84 @@
//
// TransformTests.cpp
// tests/shared/src
//
// Copyright 2013-2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <Transform.h>
#include "TransformTests.h"
#include "SharedLogging.h"
using namespace glm;
const vec3 xAxis(1.0f, 0.0f, 0.0f);
const vec3 yAxis(0.0f, 1.0f, 0.0f);
const vec3 zAxis(0.0f, 0.0f, 1.0f);
const quat rot90 = angleAxis((float)M_PI / 2.0f, yAxis);
QTEST_MAIN(TransformTests)
const float EPSILON = 0.001f;
void TransformTests::getMatrix() {
const vec3 t(0.0f, 0.0f, 10.0f);
// create a matrix that is composed of a PI/2 rotation followed by a small z translation
const mat4 m(vec4(rot90 * xAxis, 0.0f),
vec4(rot90 * yAxis, 0.0f),
vec4(rot90 * zAxis, 0.0f),
vec4(vec4(t, 1.0f)));
// postScale by a mirror about the x axis.
const mat4 mirrorX(vec4(-1.0f, 0.0f, 0.0f, 0.0f),
vec4( 0.0f, 1.0f, 0.0f, 0.0f),
vec4( 0.0f, 0.0f, 1.0f, 0.0f),
vec4( 0.0f, 0.0f, 0.0f, 1.0f));
const mat4 result_a = m * mirrorX;
Transform xform;
xform.setRotation(rot90);
xform.setTranslation(t);
xform.postScale(vec3(-1.0f, 1.0f, 1.0f));
mat4 result_b;
xform.getMatrix(result_b);
QCOMPARE_WITH_ABS_ERROR(result_a, result_b, EPSILON);
}
void TransformTests::getInverseMatrix() {
const vec3 t(0.0f, 0.0f, 10.0f);
// create a matrix that is composed of a PI/2 rotation followed by a small z translation
const mat4 m(vec4(rot90 * xAxis, 0.0f),
vec4(rot90 * yAxis, 0.0f),
vec4(rot90 * zAxis, 0.0f),
vec4(vec4(t, 1.0f)));
// mirror about the x axis.
const mat4 mirrorX(vec4(-1.0f, 0.0f, 0.0f, 0.0f),
vec4( 0.0f, 1.0f, 0.0f, 0.0f),
vec4( 0.0f, 0.0f, 1.0f, 0.0f),
vec4( 0.0f, 0.0f, 0.0f, 1.0f));
const mat4 result_a = inverse(m * mirrorX);
Transform xform;
xform.setTranslation(t);
xform.setRotation(rot90);
//
// change postScale to preScale and the test will pass...
//
xform.postScale(vec3(-1.0f, 1.0f, 1.0f));
mat4 result_b;
xform.getInverseMatrix(result_b);
QCOMPARE_WITH_ABS_ERROR(result_a, result_b, EPSILON);
}

View file

@ -0,0 +1,52 @@
//
// TransformTests.h
// tests/shared/src
//
// Copyright 2013-2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_TransformTests_h
#define hifi_TransformTests_h
#include <QtTest/QtTest>
#include <glm/glm.hpp>
#include <algorithm>
inline float getErrorDifference(const glm::mat4& a, const glm::mat4& b) {
float maxDiff = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
float diff = fabs(a[i][j] - b[i][j]);
maxDiff = std::max(diff, maxDiff);
}
}
return maxDiff;
}
inline QTextStream& operator<< (QTextStream& stream, const glm::mat4& matrix) {
stream << "[\n\t\t";
stream.setFieldWidth(15);
for (int r = 0; r < 4; ++r) {
for (int c = 0; c < 4; ++c) {
stream << matrix[c][r];
}
stream << "\n\t\t";
}
stream.setFieldWidth(0);
stream << "]\n\t"; // hacky as hell, but this should work...
return stream;
}
#include <../QTestExtensions.h>
class TransformTests : public QObject {
Q_OBJECT
private slots:
void getMatrix();
void getInverseMatrix();
};
#endif // hifi_TransformTests_h