From 4a1133fbc2484663e72e5da72a659ce67bafd977 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 3 Nov 2014 15:25:04 -0800 Subject: [PATCH] moved ShapeInfo tests to their own file --- libraries/physics/src/ShapeInfo.h | 4 +- tests/physics/src/ShapeInfoTests.cpp | 266 ++++++++++++++++++++++++ tests/physics/src/ShapeInfoTests.h | 24 +++ tests/physics/src/ShapeManagerTests.cpp | 174 ++-------------- tests/physics/src/ShapeManagerTests.h | 11 +- tests/physics/src/main.cpp | 6 +- 6 files changed, 312 insertions(+), 173 deletions(-) create mode 100644 tests/physics/src/ShapeInfoTests.cpp create mode 100644 tests/physics/src/ShapeInfoTests.h diff --git a/libraries/physics/src/ShapeInfo.h b/libraries/physics/src/ShapeInfo.h index 50f7be7275..d5d70d7b56 100644 --- a/libraries/physics/src/ShapeInfo.h +++ b/libraries/physics/src/ShapeInfo.h @@ -38,11 +38,9 @@ public: static unsigned int hashFunction(unsigned int value, int primeIndex); static unsigned int hashFunction2(unsigned int value); -private: - friend class ShapeManager; - btCollisionShape* createShape() const; +private: int _type; btAlignedObjectArray _data; }; diff --git a/tests/physics/src/ShapeInfoTests.cpp b/tests/physics/src/ShapeInfoTests.cpp new file mode 100644 index 0000000000..5ce74888fc --- /dev/null +++ b/tests/physics/src/ShapeInfoTests.cpp @@ -0,0 +1,266 @@ +// +// ShapeInfoTests.cpp +// tests/physics/src +// +// Created by Andrew Meadows on 2014.11.02 +// Copyright 2014 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 +#include +#include + +#include "ShapeInfoTests.h" + +void ShapeInfoTests::testHashFunctions() { +#ifdef USE_BULLET_PHYSICS + int maxTests = 10000000; + ShapeInfo info; + btHashMap hashes; + + int bits[32]; + unsigned int masks[32]; + for (int i = 0; i < 32; ++i) { + bits[i] = 0; + masks[i] = 1U << i; + } + + float deltaLength = 0.002f; + float endLength = 100.0f; + int numSteps = (int)(endLength / deltaLength); + + int testCount = 0; + int numCollisions = 0; + btClock timer; + for (int x = 1; x < numSteps && testCount < maxTests; ++x) { + float radiusX = (float)x * deltaLength; + // test sphere + info.setSphere(radiusX); + ++testCount; + int hash = info.computeHash(); + int hash2 = info.computeHash2(); + int* hashPtr = hashes.find(hash); + if (hashPtr && *hashPtr == hash2) { + std::cout << testCount << " hash collision radiusX = " << radiusX + << " h1 = 0x" << std::hex << (unsigned int)(hash) + << " h2 = 0x" << std::hex << (unsigned int)(hash2) + << std::endl; + ++numCollisions; + assert(false); + } else { + hashes.insert(hash, hash2); + } + for (int k = 0; k < 32; ++k) { + if (masks[k] & hash2) { + ++bits[k]; + } + } + + for (int y = 1; y < numSteps && testCount < maxTests; ++y) { + float radiusY = (float)y * deltaLength; + // test cylinder and capsule + int types[] = { CYLINDER_SHAPE_PROXYTYPE, CAPSULE_SHAPE_PROXYTYPE }; + for (int i = 0; i < 2; ++i) { + switch(types[i]) { + case CYLINDER_SHAPE_PROXYTYPE: { + info.setCylinder(radiusX, radiusY); + break; + } + case CAPSULE_SHAPE_PROXYTYPE: { + info.setCapsule(radiusX, radiusY); + break; + } + } + + ++testCount; + hash = info.computeHash(); + hash2 = info.computeHash2(); + hashPtr = hashes.find(hash); + if (hashPtr && *hashPtr == hash2) { + std::cout << testCount << " hash collision radiusX = " << radiusX << " radiusY = " << radiusY + << " h1 = 0x" << std::hex << (unsigned int)(hash) + << " h2 = 0x" << std::hex << (unsigned int)(hash2) + << std::endl; + ++numCollisions; + assert(false); + } else { + hashes.insert(hash, hash2); + } + for (int k = 0; k < 32; ++k) { + if (masks[k] & hash2) { + ++bits[k]; + } + } + } + + for (int z = 1; z < numSteps && testCount < maxTests; ++z) { + float radiusZ = (float)z * deltaLength; + // test box + info.setBox(btVector3(radiusX, radiusY, radiusZ)); + ++testCount; + hash = info.computeHash(); + hash2 = info.computeHash2(); + hashPtr = hashes.find(hash); + if (hashPtr && *hashPtr == hash2) { + std::cout << testCount << " hash collision radiusX = " << radiusX + << " radiusY = " << radiusY << " radiusZ = " << radiusZ + << " h1 = 0x" << std::hex << (unsigned int)(hash) + << " h2 = 0x" << std::hex << (unsigned int)(hash2) + << std::endl; + ++numCollisions; + assert(false); + } else { + hashes.insert(hash, hash2); + } + for (int k = 0; k < 32; ++k) { + if (masks[k] & hash2) { + ++bits[k]; + } + } + } + } + } + unsigned long int msec = timer.getTimeMilliseconds(); + std::cout << msec << " msec with " << numCollisions << " collisions out of " << testCount << " hashes" << std::endl; + + // print out distribution of bits + for (int i = 0; i < 32; ++i) { + std::cout << "bit 0x" << std::hex << masks[i] << std::dec << " = " << bits[i] << std::endl; + } +#endif // USE_BULLET_PHYSICS +} + +void ShapeInfoTests::testBoxShape() { +#ifdef USE_BULLET_PHYSICS + ShapeInfo info; + btVector3 halfExtents(1.23f, 4.56f, 7.89f); + info.setBox(halfExtents); + int hash = info.computeHash(); + int hash2 = info.computeHash2(); + + btCollisionShape* shape = info.createShape(); + if (!shape) { + std::cout << __FILE__ << ":" << __LINE__ << " ERROR: NULL Box shape" << std::endl; + } + + ShapeInfo otherInfo; + otherInfo.collectInfo(shape); + + int otherHash = otherInfo.computeHash(); + if (hash != otherHash) { + std::cout << __FILE__ << ":" << __LINE__ + << " ERROR: expected Box shape hash = " << hash << " but found hash = " << otherHash << std::endl; + } + + int otherHash2= otherInfo.computeHash2(); + if (hash2 != otherHash2) { + std::cout << __FILE__ << ":" << __LINE__ + << " ERROR: expected Box shape hash2 = " << hash2 << " but found hash2 = " << otherHash2 << std::endl; + } + + delete shape; +#endif // USE_BULLET_PHYSICS +} + +void ShapeInfoTests::testSphereShape() { +#ifdef USE_BULLET_PHYSICS + ShapeInfo info; + float radius = 1.23f; + info.setSphere(radius); + int hash = info.computeHash(); + int hash2 = info.computeHash2(); + + btCollisionShape* shape = info.createShape(); + + ShapeInfo otherInfo; + otherInfo.collectInfo(shape); + + int otherHash = otherInfo.computeHash(); + if (hash != otherHash) { + std::cout << __FILE__ << ":" << __LINE__ + << " ERROR: expected Sphere shape hash = " << hash << " but found hash = " << otherHash << std::endl; + } + + int otherHash2 = otherInfo.computeHash2(); + if (hash2 != otherHash2) { + std::cout << __FILE__ << ":" << __LINE__ + << " ERROR: expected Sphere shape hash2 = " << hash2 << " but found hash2 = " << otherHash2 << std::endl; + } + + delete shape; +#endif // USE_BULLET_PHYSICS +} + +void ShapeInfoTests::testCylinderShape() { +#ifdef USE_BULLET_PHYSICS + ShapeInfo info; + float radius = 1.23f; + float height = 4.56f; + info.setCylinder(radius, height); + int hash = info.computeHash(); + int hash2 = info.computeHash2(); + + btCollisionShape* shape = info.createShape(); + + ShapeInfo otherInfo; + otherInfo.collectInfo(shape); + + int otherHash = otherInfo.computeHash(); + if (hash != otherHash) { + std::cout << __FILE__ << ":" << __LINE__ + << " ERROR: expected Cylinder shape hash = " << hash << " but found hash = " << otherHash << std::endl; + } + + int otherHash2 = otherInfo.computeHash2(); + if (hash2 != otherHash2) { + std::cout << __FILE__ << ":" << __LINE__ + << " ERROR: expected Cylinder shape hash2 = " << hash2 << " but found hash2 = " << otherHash2 << std::endl; + } + + delete shape; +#endif // USE_BULLET_PHYSICS +} + +void ShapeInfoTests::testCapsuleShape() { +#ifdef USE_BULLET_PHYSICS + ShapeInfo info; + float radius = 1.23f; + float height = 4.56f; + info.setCapsule(radius, height); + int hash = info.computeHash(); + int hash2 = info.computeHash2(); + + btCollisionShape* shape = info.createShape(); + + ShapeInfo otherInfo; + otherInfo.collectInfo(shape); + + int otherHash = otherInfo.computeHash(); + if (hash != otherHash) { + std::cout << __FILE__ << ":" << __LINE__ + << " ERROR: expected Capsule shape hash = " << hash << " but found hash = " << otherHash << std::endl; + } + + int otherHash2 = otherInfo.computeHash2(); + if (hash2 != otherHash2) { + std::cout << __FILE__ << ":" << __LINE__ + << " ERROR: expected Capsule shape hash2 = " << hash2 << " but found hash2 = " << otherHash2 << std::endl; + } + + delete shape; +#endif // USE_BULLET_PHYSICS +} + +void ShapeInfoTests::runAllTests() { +//#define MANUAL_TEST +#ifdef MANUAL_TEST + testHashFunctions(); +#endif // MANUAL_TEST + testBoxShape(); + testSphereShape(); + testCylinderShape(); + testCapsuleShape(); +} diff --git a/tests/physics/src/ShapeInfoTests.h b/tests/physics/src/ShapeInfoTests.h new file mode 100644 index 0000000000..b786ca92d5 --- /dev/null +++ b/tests/physics/src/ShapeInfoTests.h @@ -0,0 +1,24 @@ +// +// ShapeInfoTests.h +// tests/physics/src +// +// Created by Andrew Meadows on 2014.11.02 +// Copyright 2014 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_ShapeInfoTests_h +#define hifi_ShapeInfoTests_h + +namespace ShapeInfoTests { + void testHashFunctions(); + void testBoxShape(); + void testSphereShape(); + void testCylinderShape(); + void testCapsuleShape(); + void runAllTests(); +} + +#endif // hifi_ShapeInfoTests_h diff --git a/tests/physics/src/ShapeManagerTests.cpp b/tests/physics/src/ShapeManagerTests.cpp index 9dab84dd08..78ef45f107 100644 --- a/tests/physics/src/ShapeManagerTests.cpp +++ b/tests/physics/src/ShapeManagerTests.cpp @@ -15,124 +15,6 @@ #include "ShapeManagerTests.h" -void ShapeManagerTests::testHashFunctions() { -#ifdef USE_BULLET_PHYSICS - int maxTests = 10000000; - ShapeInfo info; - btHashMap hashes; - - int bits[32]; - unsigned int masks[32]; - for (int i = 0; i < 32; ++i) { - bits[i] = 0; - masks[i] = 1U << i; - } - - float deltaLength = 0.002f; - float endLength = 100.0f; - int numSteps = (int)(endLength / deltaLength); - - int testCount = 0; - int numCollisions = 0; - btClock timer; - for (int x = 1; x < numSteps && testCount < maxTests; ++x) { - float radiusX = (float)x * deltaLength; - // test sphere - info.setSphere(radiusX); - ++testCount; - int hash = info.computeHash(); - int hash2 = info.computeHash2(); - int* hashPtr = hashes.find(hash); - if (hashPtr && *hashPtr == hash2) { - std::cout << testCount << " hash collision radiusX = " << radiusX - << " h1 = 0x" << std::hex << (unsigned int)(hash) - << " h2 = 0x" << std::hex << (unsigned int)(hash2) - << std::endl; - ++numCollisions; - assert(false); - } else { - hashes.insert(hash, hash2); - } - for (int k = 0; k < 32; ++k) { - if (masks[k] & hash2) { - ++bits[k]; - } - } - - for (int y = 1; y < numSteps && testCount < maxTests; ++y) { - float radiusY = (float)y * deltaLength; - // test cylinder and capsule - int types[] = { CYLINDER_SHAPE_PROXYTYPE, CAPSULE_SHAPE_PROXYTYPE }; - for (int i = 0; i < 2; ++i) { - switch(types[i]) { - case CYLINDER_SHAPE_PROXYTYPE: { - info.setCylinder(radiusX, radiusY); - break; - } - case CAPSULE_SHAPE_PROXYTYPE: { - info.setCapsule(radiusX, radiusY); - break; - } - } - - ++testCount; - hash = info.computeHash(); - hash2 = info.computeHash2(); - hashPtr = hashes.find(hash); - if (hashPtr && *hashPtr == hash2) { - std::cout << testCount << " hash collision radiusX = " << radiusX << " radiusY = " << radiusY - << " h1 = 0x" << std::hex << (unsigned int)(hash) - << " h2 = 0x" << std::hex << (unsigned int)(hash2) - << std::endl; - ++numCollisions; - assert(false); - } else { - hashes.insert(hash, hash2); - } - for (int k = 0; k < 32; ++k) { - if (masks[k] & hash2) { - ++bits[k]; - } - } - } - - for (int z = 1; z < numSteps && testCount < maxTests; ++z) { - float radiusZ = (float)z * deltaLength; - // test box - info.setBox(btVector3(radiusX, radiusY, radiusZ)); - ++testCount; - hash = info.computeHash(); - hash2 = info.computeHash2(); - hashPtr = hashes.find(hash); - if (hashPtr && *hashPtr == hash2) { - std::cout << testCount << " hash collision radiusX = " << radiusX - << " radiusY = " << radiusY << " radiusZ = " << radiusZ - << " h1 = 0x" << std::hex << (unsigned int)(hash) - << " h2 = 0x" << std::hex << (unsigned int)(hash2) - << std::endl; - ++numCollisions; - assert(false); - } else { - hashes.insert(hash, hash2); - } - for (int k = 0; k < 32; ++k) { - if (masks[k] & hash2) { - ++bits[k]; - } - } - } - } - } - unsigned long int msec = timer.getTimeMilliseconds(); - std::cout << msec << " msec with " << numCollisions << " collisions out of " << testCount << " hashes" << std::endl; - - // print out distribution of bits - for (int i = 0; i < 32; ++i) { - std::cout << "bit 0x" << std::hex << masks[i] << std::dec << " = " << bits[i] << std::endl; - } -#endif // USE_BULLET_PHYSICS -} - void ShapeManagerTests::testShapeAccounting() { #ifdef USE_BULLET_PHYSICS ShapeManager shapeManager; @@ -271,26 +153,19 @@ void ShapeManagerTests::addManyShapes() { #endif // USE_BULLET_PHYSICS } -void ShapeManagerTests::testBoxShape() { +void ShapeManagerTests::addBoxShape() { #ifdef USE_BULLET_PHYSICS ShapeInfo info; btVector3 halfExtents(1.23f, 4.56f, 7.89f); info.setBox(halfExtents); - int hash = info.computeHash(); ShapeManager shapeManager; btCollisionShape* shape = shapeManager.getShape(info); ShapeInfo otherInfo; otherInfo.collectInfo(shape); - int otherHash = otherInfo.computeHash(); - if (hash != otherHash) { - std::cout << __FILE__ << ":" << __LINE__ - << " ERROR: expected Box shape hash = " << hash << " but found hash = " << otherHash << std::endl; - } - - btCollisionShape* otherShape = shapeManager.getShape(info); + btCollisionShape* otherShape = shapeManager.getShape(otherInfo); if (shape != otherShape) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: Box ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl; @@ -298,26 +173,19 @@ void ShapeManagerTests::testBoxShape() { #endif // USE_BULLET_PHYSICS } -void ShapeManagerTests::testSphereShape() { +void ShapeManagerTests::addSphereShape() { #ifdef USE_BULLET_PHYSICS ShapeInfo info; float radius = 1.23f; info.setSphere(radius); - int hash = info.computeHash(); ShapeManager shapeManager; btCollisionShape* shape = shapeManager.getShape(info); ShapeInfo otherInfo; otherInfo.collectInfo(shape); - int otherHash = otherInfo.computeHash(); - if (hash != otherHash) { - std::cout << __FILE__ << ":" << __LINE__ - << " ERROR: expected Sphere shape hash = " << hash << " but found hash = " << otherHash << std::endl; - } - - btCollisionShape* otherShape = shapeManager.getShape(info); + btCollisionShape* otherShape = shapeManager.getShape(otherInfo); if (shape != otherShape) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: Sphere ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl; @@ -325,27 +193,20 @@ void ShapeManagerTests::testSphereShape() { #endif // USE_BULLET_PHYSICS } -void ShapeManagerTests::testCylinderShape() { +void ShapeManagerTests::addCylinderShape() { #ifdef USE_BULLET_PHYSICS ShapeInfo info; float radius = 1.23f; float height = 4.56f; info.setCylinder(radius, height); - int hash = info.computeHash(); ShapeManager shapeManager; btCollisionShape* shape = shapeManager.getShape(info); ShapeInfo otherInfo; otherInfo.collectInfo(shape); - int otherHash = otherInfo.computeHash(); - if (hash != otherHash) { - std::cout << __FILE__ << ":" << __LINE__ - << " ERROR: expected Cylinder shape hash = " << hash << " but found hash = " << otherHash << std::endl; - } - - btCollisionShape* otherShape = shapeManager.getShape(info); + btCollisionShape* otherShape = shapeManager.getShape(otherInfo); if (shape != otherShape) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: Cylinder ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl; @@ -353,27 +214,20 @@ void ShapeManagerTests::testCylinderShape() { #endif // USE_BULLET_PHYSICS } -void ShapeManagerTests::testCapsuleShape() { +void ShapeManagerTests::addCapsuleShape() { #ifdef USE_BULLET_PHYSICS ShapeInfo info; float radius = 1.23f; float height = 4.56f; info.setCapsule(radius, height); - int hash = info.computeHash(); ShapeManager shapeManager; btCollisionShape* shape = shapeManager.getShape(info); ShapeInfo otherInfo; otherInfo.collectInfo(shape); - int otherHash = otherInfo.computeHash(); - if (hash != otherHash) { - std::cout << __FILE__ << ":" << __LINE__ - << " ERROR: expected Capsule shape hash = " << hash << " but found hash = " << otherHash << std::endl; - } - - btCollisionShape* otherShape = shapeManager.getShape(info); + btCollisionShape* otherShape = shapeManager.getShape(otherInfo); if (shape != otherShape) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: Capsule ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl; @@ -382,14 +236,10 @@ void ShapeManagerTests::testCapsuleShape() { } void ShapeManagerTests::runAllTests() { -//#define MANUAL_TEST -#ifdef MANUAL_TEST - testHashFunctions(); -#endif // MANUAL_TEST testShapeAccounting(); addManyShapes(); - testBoxShape(); - testSphereShape(); - testCylinderShape(); - testCapsuleShape(); + addBoxShape(); + addSphereShape(); + addCylinderShape(); + addCapsuleShape(); } diff --git a/tests/physics/src/ShapeManagerTests.h b/tests/physics/src/ShapeManagerTests.h index 3cbae8cffb..98703fda1e 100644 --- a/tests/physics/src/ShapeManagerTests.h +++ b/tests/physics/src/ShapeManagerTests.h @@ -13,14 +13,13 @@ #define hifi_ShapeManagerTests_h namespace ShapeManagerTests { - void testHashFunctions(); void testShapeAccounting(); void addManyShapes(); - void testBoxShape(); - void testSphereShape(); - void testCylinderShape(); - void testCapsuleShape(); + void addBoxShape(); + void addSphereShape(); + void addCylinderShape(); + void addCapsuleShape(); void runAllTests(); } -#endif // hifi_VerletShapeTests_h +#endif // hifi_ShapeManagerTests_h diff --git a/tests/physics/src/main.cpp b/tests/physics/src/main.cpp index 0e25f0f48c..dfc3698eb5 100644 --- a/tests/physics/src/main.cpp +++ b/tests/physics/src/main.cpp @@ -8,13 +8,15 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include "ShapeColliderTests.h" -#include "VerletShapeTests.h" +//#include "ShapeColliderTests.h" +//#include "VerletShapeTests.h" +#include "ShapeInfoTests.h" #include "ShapeManagerTests.h" int main(int argc, char** argv) { //ShapeColliderTests::runAllTests(); //VerletShapeTests::runAllTests(); + ShapeInfoTests::runAllTests(); ShapeManagerTests::runAllTests(); return 0; }