physics test cleanup

This commit is contained in:
Seiji Emery 2015-06-22 15:55:34 -07:00
parent c2b7f70d2b
commit 0d9a661839
8 changed files with 147 additions and 109 deletions

View file

@ -0,0 +1,87 @@
//
// BulletTestUtils.h
// hifi
//
// Created by Seiji Emery on 6/22/15.
//
//
#ifndef hifi_BulletTestUtils_h
#define hifi_BulletTestUtils_h
#include <BulletUtil.h>
// Implements functionality in QTestExtensions.hpp for glm types
// There are 3 functions in here (which need to be defined for all types that use them):
//
// - fuzzyCompare (const T &, const T &) -> V (used by QFUZZY_COMPARE)
// - operator << (QTextStream &, const T &) -> QTextStream & (used by all (additional) test macros)
// - errorTest (const T &, const T &, V) -> std::function<bool()>
// (used by QCOMPARE_WITH_RELATIVE_ERROR via QCOMPARE_WITH_LAMBDA)
// (this is only used by btMatrix3x3 in MeshMassPropertiesTests.cpp, so it's only defined for the Mat3 type)
//
// fuzzy compare (this is a distance function, basically)
//
inline btScalar fuzzyCompare(const btScalar & a, const btScalar & b) {
return fabs(a - b);
}
inline btScalar fuzzyCompare(const btVector3 & a, const btVector3 & b)
{
return (a - b).length();
}
// Matrices are compared element-wise -- if the error value for any element > epsilon, then fail
inline btScalar fuzzyCompare (const btMatrix3x3 & a, const btMatrix3x3 & b) {
btScalar maxDiff = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
btScalar diff = fabs(a[i][j] - b[i][j]);
maxDiff = qMax(diff, maxDiff);
}
}
return maxDiff;
}
//
// Printing (operator <<)
//
// btMatrix3x3 stream printing (not advised to use this outside of the test macros, due to formatting)
inline QTextStream & operator << (QTextStream & stream, const btMatrix3x3 & matrix) {
stream << "[\n\t\t";
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
stream << " " << matrix[i][j];
}
stream << "\n\t\t";
}
stream << "]\n\t"; // hacky as hell, but this should work...
return stream;
}
inline QTextStream & operator << (QTextStream & stream, const btVector3 & v) {
return stream << "btVector3 { " << v.x() << ", " << v.y() << ", " << v.z() << " }";
}
// btScalar operator<< is already implemented (as it's just a typedef-ed float/double)
//
// errorTest (actual, expected, acceptableRelativeError) -> callback closure
//
// Produces a relative error test for btMatrix3x3 usable with QCOMPARE_WITH_LAMBDA
inline auto errorTest (const btMatrix3x3 & actual, const btMatrix3x3 & expected, const btScalar acceptableRelativeError)
-> std::function<bool ()>
{
return [&actual, &expected, acceptableRelativeError] () {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
auto err = (actual[i][j] - expected[i][j]) / expected[i][j];
if (fabsf(err) > acceptableRelativeError)
return false;
}
}
return true;
};
}
#endif

View file

@ -13,8 +13,10 @@
#define hifi_BulletUtilTests_h
#include <QtTest/QtTest>
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
// Add additional qtest functionality (the include order is important!)
#include "GlmTestUtils.h"
#include "../QTestExtensions.hpp"
class BulletUtilTests : public QObject {
Q_OBJECT
@ -25,16 +27,4 @@ private slots:
// void fooTest ();
};
// Define comparison + printing functions for the data types we need
inline float fuzzyCompare (const glm::vec3 & a, const glm::vec3 & b) {
return glm::distance(a, b);
}
inline QTextStream & operator << (QTextStream & stream, const glm::vec3 & v) {
return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }";
}
// These hook into this (and must be defined first...)
#include "../QTestExtensions.hpp"
#endif // hifi_BulletUtilTests_h

View file

@ -12,10 +12,12 @@
#ifndef hifi_CollisionInfoTests_h
#define hifi_CollisionInfoTests_h
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
#include <QtTest/QtTest>
// Add additional qtest functionality (the include order is important!)
#include "GlmTestUtils.h"
#include "../QTestExtensions.hpp"
class CollisionInfoTests : public QObject {
Q_OBJECT
@ -24,16 +26,4 @@ private slots:
// void translateThenRotate();
};
// Define comparison + printing functions for the data types we need
inline float fuzzyCompare (const glm::vec3 & a, const glm::vec3 & b) {
return glm::distance(a, b);
}
inline QTextStream & operator << (QTextStream & stream, const glm::vec3 & v) {
return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }";
}
// These hook into this (and must be defined first...)
#include "../QTestExtensions.hpp"
#endif // hifi_CollisionInfoTests_h

View file

@ -0,0 +1,24 @@
//
// GlmTestUtils.h
// hifi
//
// Created by Seiji Emery on 6/22/15.
//
//
#ifndef hifi_GlmTestUtils_h
#define hifi_GlmTestUtils_h
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
// Implements functionality in QTestExtensions.hpp for glm types
inline float fuzzyCompare(const glm::vec3 & a, const glm::vec3 & b) {
return glm::distance(a, b);
}
inline QTextStream & operator << (QTextStream & stream, const glm::vec3 & v) {
return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }";
}
#endif

View file

@ -12,13 +12,20 @@
#ifndef hifi_MeshMassPropertiesTests_h
#define hifi_MeshMassPropertiesTests_h
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
#include <BulletUtil.h>
#include <QtTest/QtTest>
#include <QtGlobal>
// Add additional qtest functionality (the include order is important!)
#include "BulletTestUtils.h"
#include "GlmTestUtils.h"
#include "../QTestExtensions.hpp"
// Relative error macro (see errorTest in BulletTestUtils.h)
#define QCOMPARE_WITH_RELATIVE_ERROR(actual, expected, relativeError) \
QCOMPARE_WITH_LAMBDA(actual, expected, errorTest(actual, expected, relativeError))
// Testcase class
class MeshMassPropertiesTests : public QObject {
Q_OBJECT
@ -30,75 +37,4 @@ private slots:
void testBoxAsMesh();
};
// Define comparison + printing functions for the data types we need
inline float fuzzyCompare(const glm::vec3 & a, const glm::vec3 & b) {
return glm::distance(a, b);
}
inline QTextStream & operator << (QTextStream & stream, const glm::vec3 & v) {
return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }";
}
inline btScalar fuzzyCompare(const btScalar & a, const btScalar & b) {
return fabs(a - b);
}
// Matrices are compared element-wise -- if the error value for any element > epsilon, then fail
inline btScalar fuzzyCompare (const btMatrix3x3 & a, const btMatrix3x3 & b) {
btScalar totalDiff = 0;
btScalar maxDiff = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
btScalar diff = fabs(a[i][j] - b[i][j]);
totalDiff += diff;
maxDiff = qMax(diff, maxDiff);
}
}
// return totalDiff;
return maxDiff;
}
inline QTextStream & operator << (QTextStream & stream, const btMatrix3x3 & matrix) {
stream << "[\n\t\t";
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
stream << " " << matrix[i][j];
}
stream << "\n\t\t";
}
stream << "]\n\t"; // hacky as hell, but this should work...
return stream;
}
inline btScalar fuzzyCompare(const btVector3 & a, const btVector3 & b)
{
return (a - b).length();
}
inline QTextStream & operator << (QTextStream & stream, const btVector3 & v) {
return stream << "btVector3 { " << v.x() << ", " << v.y() << ", " << v.z() << " }";
}
// Produces a relative error test for btMatrix3x3 usable with QCOMPARE_WITH_LAMBDA
inline auto errorTest (const btMatrix3x3 & actual, const btMatrix3x3 & expected, const btScalar acceptableRelativeError)
-> std::function<bool ()>
{
return [&actual, &expected, acceptableRelativeError] () {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
auto err = (actual[i][j] - expected[i][j]) / expected[i][j];
if (fabsf(err) > acceptableRelativeError)
return false;
}
}
return true;
};
}
#define QCOMPARE_WITH_RELATIVE_ERROR(actual, expected, relativeError) \
QCOMPARE_WITH_LAMBDA(actual, expected, errorTest(actual, expected, relativeError))
// These hook into this (and must be defined first...)
#include "../QTestExtensions.hpp"
#endif // hifi_MeshMassPropertiesTests_h

View file

@ -10,7 +10,6 @@
//
//#include <stdio.h>
#include "PhysicsTestUtil.h"
#include <iostream>
#include <math.h>
@ -29,10 +28,10 @@
#include "ShapeColliderTests.h"
//const glm::vec3 origin(0.0f);
//static const glm::vec3 xAxis(1.0f, 0.0f, 0.0f);
//static const glm::vec3 yAxis(0.0f, 1.0f, 0.0f);
//static const glm::vec3 zAxis(0.0f, 0.0f, 1.0f);
const glm::vec3 origin(0.0f);
static const glm::vec3 xAxis(1.0f, 0.0f, 0.0f);
static const glm::vec3 yAxis(0.0f, 1.0f, 0.0f);
static const glm::vec3 zAxis(0.0f, 0.0f, 1.0f);
QTEST_MAIN(ShapeColliderTests)

View file

@ -13,6 +13,13 @@
#define hifi_ShapeColliderTests_h
#include <QtTest/QtTest>
#include <QtGlobal>
// Add additional qtest functionality (the include order is important!)
#include "BulletTestUtils.h"
#include "GlmTestUtils.h"
#include "../QTestExtensions.hpp"
class ShapeColliderTests : public QObject {
Q_OBJECT

View file

@ -14,6 +14,10 @@
#include <QtTest/QtTest>
// Add additional qtest functionality (the include order is important!)
#include "BulletTestUtils.h"
#include "../QTestExtensions.hpp"
class ShapeInfoTests : public QObject {
Q_OBJECT
private slots:
@ -22,7 +26,8 @@ private slots:
void testSphereShape();
void testCylinderShape();
void testCapsuleShape();
// void runAllTests();
};
#include "../QTestExtensions.hpp"
#endif // hifi_ShapeInfoTests_h