void ShapeInfoTests::testBoxShape() { ShapeInfo info; glm::vec3 halfExtents(1.23f, 4.56f, 7.89f); info.setBox(halfExtents); DoubleHashKey key = info.getHash(); btCollisionShape* shape = ShapeInfoUtil::createShapeFromInfo(info); if (!shape) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: NULL Box shape" << std::endl; } ShapeInfo otherInfo = info; DoubleHashKey otherKey = otherInfo.getHash(); if (key.getHash() != otherKey.getHash()) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected Box shape hash = " << key.getHash() << " but found hash = " << otherKey.getHash() << std::endl; } if (key.getHash2() != otherKey.getHash2()) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected Box shape hash2 = " << key.getHash2() << " but found hash2 = " << otherKey.getHash2() << std::endl; } delete shape; }
void ShapeManagerTests::addManyShapes() { ShapeManager shapeManager; int numSizes = 100; float startSize = 1.0f; float endSize = 99.0f; float deltaSize = (endSize - startSize) / (float)numSizes; ShapeInfo info; for (int i = 0; i < numSizes; ++i) { float s = startSize + (float)i * deltaSize; glm::vec3 scale(s, 1.23f + s, s - 0.573f); info.setBox(0.5f * scale); btCollisionShape* shape = shapeManager.getShape(info); if (!shape) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: i = " << i << " null box shape for scale = " << scale << std::endl; } float radius = 0.5f * s; info.setSphere(radius); shape = shapeManager.getShape(info); if (!shape) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: i = " << i << " null sphere shape for radius = " << radius << std::endl; } } int numShapes = shapeManager.getNumShapes(); if (numShapes != 2 * numSizes) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected numShapes = " << numSizes << " but found numShapes = " << numShapes << std::endl; } }
void ShapeManagerTests::addBoxShape() { ShapeInfo info; glm::vec3 halfExtents(1.23f, 4.56f, 7.89f); info.setBox(halfExtents); ShapeManager shapeManager; btCollisionShape* shape = shapeManager.getShape(info); ShapeInfo otherInfo; ShapeInfoUtil::collectInfoFromShape(shape, otherInfo); btCollisionShape* otherShape = shapeManager.getShape(otherInfo); if (shape != otherShape) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: Box ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl; } }
void ShapeInfoTests::testHashFunctions() { int maxTests = 10000000; ShapeInfo info; btHashMap<btHashInt, uint32_t> hashes; uint32_t bits[32]; uint32_t 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; 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 << key.getHash() << " h2 = 0x" << std::hex << key.getHash2() << std::endl; ++numCollisions; assert(false); } else { hashes.insert(key.getHash(), key.getHash2()); } for (int k = 0; k < 32; ++k) { 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) { switch(types[i]) { case CYLINDER_SHAPE_PROXYTYPE: { info.setCylinder(radiusX, radiusY); break; } case CAPSULE_SHAPE_PROXYTYPE: { info.setCapsuleY(radiusX, radiusY); break; } } ++testCount; 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 << key.getHash() << " h2 = 0x" << std::hex << key.getHash2() << std::endl; ++numCollisions; assert(false); } else { hashes.insert(key.getHash(), key.getHash2()); } for (int k = 0; k < 32; ++k) { 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 = 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 << key.getHash() << " h2 = 0x" << std::hex << key.getHash2() << std::endl; ++numCollisions; assert(false); } else { hashes.insert(key.getHash(), key.getHash2()); } for (int k = 0; k < 32; ++k) { if (masks[k] & key.getHash2()) { ++bits[k]; } } } } } uint64_t 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; } }
void BoxEntityItem::computeShapeInfo(ShapeInfo& info) const { glm::vec3 halfExtents = 0.5f * getDimensionsInMeters(); info.setBox(halfExtents); }
void ShapeManagerTests::testShapeAccounting() { ShapeManager shapeManager; ShapeInfo info; info.setBox(glm::vec3(1.0f, 1.0f, 1.0f)); // NOTE: ShapeManager returns -1 as refcount when the shape is unknown, // which is distinct from "known but with zero references" int numReferences = shapeManager.getNumReferences(info); if (numReferences != -1) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected ignorant ShapeManager after initialization" << std::endl; } // create one shape and verify we get a valid pointer btCollisionShape* shape = shapeManager.getShape(info); if (!shape) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected shape creation for default parameters" << std::endl; } // verify number of shapes if (shapeManager.getNumShapes() != 1) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected one shape" << std::endl; } // reference the shape again and verify that we get the same pointer btCollisionShape* otherShape = shapeManager.getShape(info); if (otherShape != shape) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected shape* " << (void*)(shape) << " but found shape* " << (void*)(otherShape) << std::endl; } // verify number of references numReferences = shapeManager.getNumReferences(info); int expectedNumReferences = 2; if (numReferences != expectedNumReferences) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected " << expectedNumReferences << " references but found " << numReferences << std::endl; } // release all references bool released = shapeManager.releaseShape(info); numReferences--; while (numReferences > 0) { released = shapeManager.releaseShape(info) && released; numReferences--; } if (!released) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected shape released" << std::endl; } // verify shape still exists (not yet garbage collected) if (shapeManager.getNumShapes() != 1) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected one shape after release but before garbage collection" << std::endl; } // verify shape's refcount is zero numReferences = shapeManager.getNumReferences(info); if (numReferences != 0) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected refcount = 0 for shape but found refcount = " << numReferences << std::endl; } // reference the shape again and verify refcount is updated otherShape = shapeManager.getShape(info); numReferences = shapeManager.getNumReferences(info); if (numReferences != 1) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected refcount = 1 for shape but found refcount = " << numReferences << std::endl; } // verify that shape is not collected as garbage shapeManager.collectGarbage(); if (shapeManager.getNumShapes() != 1) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected one shape after release" << std::endl; } numReferences = shapeManager.getNumReferences(info); if (numReferences != 1) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected refcount = 1 for shape but found refcount = " << numReferences << std::endl; } // release reference and verify that it is collected as garbage released = shapeManager.releaseShape(info); shapeManager.collectGarbage(); if (shapeManager.getNumShapes() != 0) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected zero shapes after release" << std::endl; } numReferences = shapeManager.getNumReferences(info); if (numReferences != -1) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected ignorant ShapeManager after garbage collection" << std::endl; } // add the shape again and verify that it gets added again otherShape = shapeManager.getShape(info); numReferences = shapeManager.getNumReferences(info); if (numReferences != 1) { std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected refcount = 1 for shape but found refcount = " << numReferences << std::endl; } }