mirror of
https://thingvellir.net/git/overte
synced 2025-03-27 23:52:03 +01:00
unit tests support new ShapeInfo API
This commit is contained in:
parent
12a7021bb5
commit
0ef5d42a5b
3 changed files with 60 additions and 88 deletions
|
@ -1,38 +0,0 @@
|
|||
//
|
||||
// DoubleHashKey.h
|
||||
// libraries/physcis/src
|
||||
//
|
||||
// Created by Andrew Meadows 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_DoubleHashKey_h
|
||||
#define hifi_DoubleHashKey_h
|
||||
|
||||
// DoubleHashKey for use with btHashMap
|
||||
class DoubleHashKey {
|
||||
public:
|
||||
static unsigned int hashFunction(unsigned int value, int primeIndex);
|
||||
static unsigned int hashFunction2(unsigned int value);
|
||||
|
||||
DoubleHashKey() : _hash(0), _hash2(0) { }
|
||||
|
||||
DoubleHashKey(unsigned int value, int primeIndex = 0) :
|
||||
_hash(hashFunction(value, primeIndex)),
|
||||
_hash2(hashFunction2(value)) {
|
||||
}
|
||||
|
||||
bool equals(const DoubleHashKey& other) const {
|
||||
return _hash == other._hash && _hash2 == other._hash2;
|
||||
}
|
||||
|
||||
unsigned int getHash() const { return (unsigned int)_hash; }
|
||||
|
||||
int _hash;
|
||||
int _hash2;
|
||||
};
|
||||
|
||||
#endif // hifi_DoubleHashKey_h
|
|
@ -24,10 +24,10 @@
|
|||
void ShapeInfoTests::testHashFunctions() {
|
||||
int maxTests = 10000000;
|
||||
ShapeInfo info;
|
||||
btHashMap<btHashInt, int> hashes;
|
||||
btHashMap<btHashInt, uint32_t> hashes;
|
||||
|
||||
int bits[32];
|
||||
unsigned int masks[32];
|
||||
uint32_t bits[32];
|
||||
uint32_t masks[32];
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
bits[i] = 0;
|
||||
masks[i] = 1U << i;
|
||||
|
@ -45,26 +45,27 @@ void ShapeInfoTests::testHashFunctions() {
|
|||
// test sphere
|
||||
info.setSphere(radiusX);
|
||||
++testCount;
|
||||
DoubleHashKey key = ShapeInfoUtil::computeHash(info);
|
||||
int* hashPtr = hashes.find(key._hash);
|
||||
if (hashPtr && *hashPtr == key._hash2) {
|
||||
DoubleHashKey key = info.getHash();
|
||||
uint32_t* hashPtr = hashes.find(key.getHash());
|
||||
if (hashPtr && *hashPtr == key.getHash2()) {
|
||||
std::cout << testCount << " hash collision radiusX = " << radiusX
|
||||
<< " h1 = 0x" << std::hex << (unsigned int)(key._hash)
|
||||
<< " h2 = 0x" << std::hex << (unsigned int)(key._hash2)
|
||||
<< " h1 = 0x" << std::hex << key.getHash()
|
||||
<< " h2 = 0x" << std::hex << key.getHash2()
|
||||
<< std::endl;
|
||||
++numCollisions;
|
||||
assert(false);
|
||||
} else {
|
||||
hashes.insert(key._hash, key._hash2);
|
||||
hashes.insert(key.getHash(), key.getHash2());
|
||||
}
|
||||
for (int k = 0; k < 32; ++k) {
|
||||
if (masks[k] & key._hash2) {
|
||||
if (masks[k] & key.getHash2()) {
|
||||
++bits[k];
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = 1; y < numSteps && testCount < maxTests; ++y) {
|
||||
float radiusY = (float)y * deltaLength;
|
||||
/* TODO: reimplement Cylinder and Capsule shapes
|
||||
// test cylinder and capsule
|
||||
int types[] = { CYLINDER_SHAPE_PROXYTYPE, CAPSULE_SHAPE_PROXYTYPE };
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
|
@ -74,58 +75,59 @@ void ShapeInfoTests::testHashFunctions() {
|
|||
break;
|
||||
}
|
||||
case CAPSULE_SHAPE_PROXYTYPE: {
|
||||
info.setCapsule(radiusX, radiusY);
|
||||
info.setCapsuleY(radiusX, radiusY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
++testCount;
|
||||
key = ShapeInfoUtil::computeHash(info);
|
||||
hashPtr = hashes.find(key._hash);
|
||||
if (hashPtr && *hashPtr == key._hash2) {
|
||||
key = info.getHash();
|
||||
hashPtr = hashes.find(key.getHash());
|
||||
if (hashPtr && *hashPtr == key.getHash2()) {
|
||||
std::cout << testCount << " hash collision radiusX = " << radiusX << " radiusY = " << radiusY
|
||||
<< " h1 = 0x" << std::hex << (unsigned int)(key._hash)
|
||||
<< " h2 = 0x" << std::hex << (unsigned int)(key._hash2)
|
||||
<< " h1 = 0x" << std::hex << key.getHash()
|
||||
<< " h2 = 0x" << std::hex << key.getHash2()
|
||||
<< std::endl;
|
||||
++numCollisions;
|
||||
assert(false);
|
||||
} else {
|
||||
hashes.insert(key._hash, key._hash2);
|
||||
hashes.insert(key.getHash(), key.getHash2());
|
||||
}
|
||||
for (int k = 0; k < 32; ++k) {
|
||||
if (masks[k] & key._hash2) {
|
||||
if (masks[k] & key.getHash2()) {
|
||||
++bits[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
for (int z = 1; z < numSteps && testCount < maxTests; ++z) {
|
||||
float radiusZ = (float)z * deltaLength;
|
||||
// test box
|
||||
info.setBox(glm::vec3(radiusX, radiusY, radiusZ));
|
||||
++testCount;
|
||||
DoubleHashKey key = ShapeInfoUtil::computeHash(info);
|
||||
hashPtr = hashes.find(key._hash);
|
||||
if (hashPtr && *hashPtr == key._hash2) {
|
||||
DoubleHashKey key = info.getHash();
|
||||
hashPtr = hashes.find(key.getHash());
|
||||
if (hashPtr && *hashPtr == key.getHash2()) {
|
||||
std::cout << testCount << " hash collision radiusX = " << radiusX
|
||||
<< " radiusY = " << radiusY << " radiusZ = " << radiusZ
|
||||
<< " h1 = 0x" << std::hex << (unsigned int)(key._hash)
|
||||
<< " h2 = 0x" << std::hex << (unsigned int)(key._hash2)
|
||||
<< " h1 = 0x" << std::hex << key.getHash()
|
||||
<< " h2 = 0x" << std::hex << key.getHash2()
|
||||
<< std::endl;
|
||||
++numCollisions;
|
||||
assert(false);
|
||||
} else {
|
||||
hashes.insert(key._hash, key._hash2);
|
||||
hashes.insert(key.getHash(), key.getHash2());
|
||||
}
|
||||
for (int k = 0; k < 32; ++k) {
|
||||
if (masks[k] & key._hash2) {
|
||||
if (masks[k] & key.getHash2()) {
|
||||
++bits[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
unsigned long int msec = timer.getTimeMilliseconds();
|
||||
uint64_t msec = timer.getTimeMilliseconds();
|
||||
std::cout << msec << " msec with " << numCollisions << " collisions out of " << testCount << " hashes" << std::endl;
|
||||
|
||||
// print out distribution of bits
|
||||
|
@ -138,7 +140,7 @@ void ShapeInfoTests::testBoxShape() {
|
|||
ShapeInfo info;
|
||||
glm::vec3 halfExtents(1.23f, 4.56f, 7.89f);
|
||||
info.setBox(halfExtents);
|
||||
DoubleHashKey key = ShapeInfoUtil::computeHash(info);
|
||||
DoubleHashKey key = info.getHash();
|
||||
|
||||
btCollisionShape* shape = ShapeInfoUtil::createShapeFromInfo(info);
|
||||
if (!shape) {
|
||||
|
@ -148,15 +150,15 @@ void ShapeInfoTests::testBoxShape() {
|
|||
ShapeInfo otherInfo;
|
||||
ShapeInfoUtil::collectInfoFromShape(shape, otherInfo);
|
||||
|
||||
DoubleHashKey otherKey = ShapeInfoUtil::computeHash(otherInfo);
|
||||
if (key._hash != otherKey._hash) {
|
||||
DoubleHashKey otherKey = otherInfo.getHash();
|
||||
if (key.getHash() != otherKey.getHash()) {
|
||||
std::cout << __FILE__ << ":" << __LINE__
|
||||
<< " ERROR: expected Box shape hash = " << key._hash << " but found hash = " << otherKey._hash << std::endl;
|
||||
<< " ERROR: expected Box shape hash = " << key.getHash() << " but found hash = " << otherKey.getHash() << std::endl;
|
||||
}
|
||||
|
||||
if (key._hash2 != otherKey._hash2) {
|
||||
if (key.getHash2() != otherKey.getHash2()) {
|
||||
std::cout << __FILE__ << ":" << __LINE__
|
||||
<< " ERROR: expected Box shape hash2 = " << key._hash2 << " but found hash2 = " << otherKey._hash2 << std::endl;
|
||||
<< " ERROR: expected Box shape hash2 = " << key.getHash2() << " but found hash2 = " << otherKey.getHash2() << std::endl;
|
||||
}
|
||||
|
||||
delete shape;
|
||||
|
@ -166,74 +168,78 @@ void ShapeInfoTests::testSphereShape() {
|
|||
ShapeInfo info;
|
||||
float radius = 1.23f;
|
||||
info.setSphere(radius);
|
||||
DoubleHashKey key = ShapeInfoUtil::computeHash(info);
|
||||
DoubleHashKey key = info.getHash();
|
||||
|
||||
btCollisionShape* shape = ShapeInfoUtil::createShapeFromInfo(info);
|
||||
|
||||
ShapeInfo otherInfo;
|
||||
ShapeInfoUtil::collectInfoFromShape(shape, otherInfo);
|
||||
|
||||
DoubleHashKey otherKey = ShapeInfoUtil::computeHash(otherInfo);
|
||||
if (key._hash != otherKey._hash) {
|
||||
DoubleHashKey otherKey = otherInfo.getHash();
|
||||
if (key.getHash() != otherKey.getHash()) {
|
||||
std::cout << __FILE__ << ":" << __LINE__
|
||||
<< " ERROR: expected Sphere shape hash = " << key._hash << " but found hash = " << otherKey._hash << std::endl;
|
||||
<< " ERROR: expected Sphere shape hash = " << key.getHash() << " but found hash = " << otherKey.getHash() << std::endl;
|
||||
}
|
||||
if (key._hash2 != otherKey._hash2) {
|
||||
if (key.getHash2() != otherKey.getHash2()) {
|
||||
std::cout << __FILE__ << ":" << __LINE__
|
||||
<< " ERROR: expected Sphere shape hash2 = " << key._hash2 << " but found hash2 = " << otherKey._hash2 << std::endl;
|
||||
<< " ERROR: expected Sphere shape hash2 = " << key.getHash2() << " but found hash2 = " << otherKey.getHash2() << std::endl;
|
||||
}
|
||||
|
||||
delete shape;
|
||||
}
|
||||
|
||||
void ShapeInfoTests::testCylinderShape() {
|
||||
/* TODO: reimplement Cylinder shape
|
||||
ShapeInfo info;
|
||||
float radius = 1.23f;
|
||||
float height = 4.56f;
|
||||
info.setCylinder(radius, height);
|
||||
DoubleHashKey key = ShapeInfoUtil::computeHash(info);
|
||||
DoubleHashKey key = info.getHash();
|
||||
|
||||
btCollisionShape* shape = ShapeInfoUtil::createShapeFromInfo(info);
|
||||
|
||||
ShapeInfo otherInfo;
|
||||
ShapeInfoUtil::collectInfoFromShape(shape, otherInfo);
|
||||
|
||||
DoubleHashKey otherKey = ShapeInfoUtil::computeHash(otherInfo);
|
||||
if (key._hash != otherKey._hash) {
|
||||
DoubleHashKey otherKey = otherInfo.getHash();
|
||||
if (key.getHash() != otherKey.getHash()) {
|
||||
std::cout << __FILE__ << ":" << __LINE__
|
||||
<< " ERROR: expected Cylinder shape hash = " << key._hash << " but found hash = " << otherKey._hash << std::endl;
|
||||
<< " ERROR: expected Cylinder shape hash = " << key.getHash() << " but found hash = " << otherKey.getHash() << std::endl;
|
||||
}
|
||||
if (key._hash2 != otherKey._hash2) {
|
||||
if (key.getHash2() != otherKey.getHash2()) {
|
||||
std::cout << __FILE__ << ":" << __LINE__
|
||||
<< " ERROR: expected Cylinder shape hash2 = " << key._hash2 << " but found hash2 = " << otherKey._hash2 << std::endl;
|
||||
<< " ERROR: expected Cylinder shape hash2 = " << key.getHash2() << " but found hash2 = " << otherKey.getHash2() << std::endl;
|
||||
}
|
||||
|
||||
delete shape;
|
||||
*/
|
||||
}
|
||||
|
||||
void ShapeInfoTests::testCapsuleShape() {
|
||||
/* TODO: reimplement Capsule shape
|
||||
ShapeInfo info;
|
||||
float radius = 1.23f;
|
||||
float height = 4.56f;
|
||||
info.setCapsule(radius, height);
|
||||
DoubleHashKey key = ShapeInfoUtil::computeHash(info);
|
||||
DoubleHashKey key = info.getHash();
|
||||
|
||||
btCollisionShape* shape = ShapeInfoUtil::createShapeFromInfo(info);
|
||||
|
||||
ShapeInfo otherInfo;
|
||||
ShapeInfoUtil::collectInfoFromShape(shape, otherInfo);
|
||||
|
||||
DoubleHashKey otherKey = ShapeInfoUtil::computeHash(otherInfo);
|
||||
if (key._hash != otherKey._hash) {
|
||||
DoubleHashKey otherKey = otherInfo.getHash();
|
||||
if (key.getHash() != otherKey.getHash()) {
|
||||
std::cout << __FILE__ << ":" << __LINE__
|
||||
<< " ERROR: expected Capsule shape hash = " << key._hash << " but found hash = " << otherKey._hash << std::endl;
|
||||
<< " ERROR: expected Capsule shape hash = " << key.getHash() << " but found hash = " << otherKey.getHash() << std::endl;
|
||||
}
|
||||
if (key._hash2 != otherKey._hash2) {
|
||||
if (key.getHash2() != otherKey.getHash2()) {
|
||||
std::cout << __FILE__ << ":" << __LINE__
|
||||
<< " ERROR: expected Capsule shape hash2 = " << key._hash2 << " but found hash2 = " << otherKey._hash2 << std::endl;
|
||||
<< " ERROR: expected Capsule shape hash2 = " << key.getHash2() << " but found hash2 = " << otherKey.getHash2() << std::endl;
|
||||
}
|
||||
|
||||
delete shape;
|
||||
*/
|
||||
}
|
||||
|
||||
void ShapeInfoTests::runAllTests() {
|
||||
|
|
|
@ -188,6 +188,7 @@ void ShapeManagerTests::addSphereShape() {
|
|||
}
|
||||
|
||||
void ShapeManagerTests::addCylinderShape() {
|
||||
/* TODO: reimplement Cylinder shape
|
||||
ShapeInfo info;
|
||||
float radius = 1.23f;
|
||||
float height = 4.56f;
|
||||
|
@ -204,9 +205,11 @@ void ShapeManagerTests::addCylinderShape() {
|
|||
std::cout << __FILE__ << ":" << __LINE__
|
||||
<< " ERROR: Cylinder ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void ShapeManagerTests::addCapsuleShape() {
|
||||
/* TODO: reimplement Capsule shape
|
||||
ShapeInfo info;
|
||||
float radius = 1.23f;
|
||||
float height = 4.56f;
|
||||
|
@ -223,6 +226,7 @@ void ShapeManagerTests::addCapsuleShape() {
|
|||
std::cout << __FILE__ << ":" << __LINE__
|
||||
<< " ERROR: Capsule ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void ShapeManagerTests::runAllTests() {
|
||||
|
|
Loading…
Reference in a new issue