diff --git a/libraries/physics/src/CollisionRenderMeshCache.cpp b/libraries/physics/src/CollisionRenderMeshCache.cpp index 517e25e1c4..3a1c4d0ea4 100644 --- a/libraries/physics/src/CollisionRenderMeshCache.cpp +++ b/libraries/physics/src/CollisionRenderMeshCache.cpp @@ -90,7 +90,11 @@ bool copyShapeToMesh(const btTransform& transform, const btConvexShape* shape, avgVertex = transform * (avgVertex * (1.0f / (float)numHullVertices)); for (int i = 0; i < numHullVertices; ++i) { - btVector3 norm = (transform * hullVertices[i] - avgVertex).normalize(); + btVector3 norm = transform * hullVertices[i] - avgVertex; + btScalar normLength = norm.length(); + if (normLength > FLT_EPSILON) { + norm /= normLength; + } memcpy(tempVertices + 3 * i, norm.m_floats, SIZE_OF_VEC3); } gpu::BufferView::Size numBytes = sizeof(float) * (3 * numHullVertices); diff --git a/tests/physics/src/CollisionRenderMeshCacheTests.cpp b/tests/physics/src/CollisionRenderMeshCacheTests.cpp index 085c9a2fe3..ad5d5db0ab 100644 --- a/tests/physics/src/CollisionRenderMeshCacheTests.cpp +++ b/tests/physics/src/CollisionRenderMeshCacheTests.cpp @@ -20,7 +20,7 @@ #include #include // for MAX_HULL_POINTS -#include "MeshUtil.cpp" +#include "MeshUtil.h" QTEST_MAIN(CollisionRenderMeshCacheTests) diff --git a/tests/physics/src/MeshUtil.cpp b/tests/physics/src/MeshUtil.cpp deleted file mode 100644 index d3eb815948..0000000000 --- a/tests/physics/src/MeshUtil.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// -// MeshUtil.cpp -// tests/physics/src -// -// Created by Andrew Meadows 2016.07.14 -// Copyright 2016 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 "MeshUtil.h" - -#include - -// returns false if any edge has only one adjacent triangle -bool MeshUtil::isClosedManifold(const uint32_t* meshIndices, uint32_t numIndices) { - using EdgeList = std::unordered_map; - EdgeList edges; - - // count the triangles for each edge - const uint32_t TRIANGLE_STRIDE = 3; - for (uint32_t i = 0; i < numIndices; i += TRIANGLE_STRIDE) { - MeshUtil::TriangleEdge edge; - // the triangles indices are stored in sequential order - for (uint32_t j = 0; j < 3; ++j) { - edge.setIndices(meshIndices[i + j], meshIndices[i + ((j + 1) % 3)]); - - EdgeList::iterator edgeEntry = edges.find(edge); - if (edgeEntry == edges.end()) { - edges.insert(std::pair(edge, 1)); - } else { - edgeEntry->second += 1; - } - } - } - // scan for outside edge - for (auto& edgeEntry : edges) { - if (edgeEntry.second == 1) { - return false; - } - } - return true; -} - diff --git a/tests/physics/src/MeshUtil.h b/tests/physics/src/MeshUtil.h index 82d33d631b..6603ee4fae 100644 --- a/tests/physics/src/MeshUtil.h +++ b/tests/physics/src/MeshUtil.h @@ -42,8 +42,6 @@ private: uint32_t _indexB { (uint32_t)(-1) }; }; -bool isClosedManifold(const uint32_t* meshIndices, uint32_t numIndices); - } // MeshUtil namespace namespace std { @@ -55,7 +53,39 @@ namespace std { return hash()((ab * (ab + 1)) / 2 + edge.getIndexB()); } }; +} // std namesspace + +namespace MeshUtil { +bool isClosedManifold(const uint32_t* meshIndices, uint32_t numIndices) { + using EdgeList = std::unordered_map; + EdgeList edges; + + // count the triangles for each edge + const uint32_t TRIANGLE_STRIDE = 3; + for (uint32_t i = 0; i < numIndices; i += TRIANGLE_STRIDE) { + TriangleEdge edge; + // the triangles indices are stored in sequential order + for (uint32_t j = 0; j < 3; ++j) { + edge.setIndices(meshIndices[i + j], meshIndices[i + ((j + 1) % 3)]); + + EdgeList::iterator edgeEntry = edges.find(edge); + if (edgeEntry == edges.end()) { + edges.insert(std::pair(edge, 1)); + } else { + edgeEntry->second += 1; + } + } + } + // scan for outside edge + for (auto& edgeEntry : edges) { + if (edgeEntry.second == 1) { + return false; + } + } + return true; } +} // MeshUtil namespace + #endif // hifi_MeshUtil_h diff --git a/tests/physics/src/ShapeInfoTests.cpp b/tests/physics/src/ShapeInfoTests.cpp index 01f5c4e511..c6a19084a2 100644 --- a/tests/physics/src/ShapeInfoTests.cpp +++ b/tests/physics/src/ShapeInfoTests.cpp @@ -42,7 +42,7 @@ void ShapeInfoTests::testHashFunctions() { int testCount = 0; int numCollisions = 0; - + btClock timer; for (int x = 1; x < numSteps && testCount < maxTests; ++x) { float radiusX = (float)x * deltaLength; @@ -52,7 +52,7 @@ void ShapeInfoTests::testHashFunctions() { DoubleHashKey key = info.getHash(); uint32_t* hashPtr = hashes.find(key.getHash()); if (hashPtr && *hashPtr == key.getHash2()) { - std::cout << testCount << " hash collision radiusX = " << radiusX + std::cout << testCount << " hash collision radiusX = " << radiusX << " h1 = 0x" << std::hex << key.getHash() << " h2 = 0x" << std::hex << key.getHash2() << std::endl; @@ -88,7 +88,7 @@ void ShapeInfoTests::testHashFunctions() { key = info.getHash(); hashPtr = hashes.find(key.getHash()); if (hashPtr && *hashPtr == key.getHash2()) { - std::cout << testCount << " hash collision radiusX = " << radiusX << " radiusY = " << radiusY + std::cout << testCount << " hash collision radiusX = " << radiusX << " radiusY = " << radiusY << " h1 = 0x" << std::hex << key.getHash() << " h2 = 0x" << std::hex << key.getHash2() << std::endl; @@ -113,8 +113,8 @@ void ShapeInfoTests::testHashFunctions() { 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 + 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; @@ -148,9 +148,9 @@ void ShapeInfoTests::testBoxShape() { info.setBox(halfExtents); DoubleHashKey key = info.getHash(); - btCollisionShape* shape = ShapeFactory::createShapeFromInfo(info); + const btCollisionShape* shape = ShapeFactory::createShapeFromInfo(info); QCOMPARE(shape != nullptr, true); - + ShapeInfo otherInfo = info; DoubleHashKey otherKey = otherInfo.getHash(); QCOMPARE(key.getHash(), otherKey.getHash()); @@ -165,7 +165,7 @@ void ShapeInfoTests::testSphereShape() { info.setSphere(radius); DoubleHashKey key = info.getHash(); - btCollisionShape* shape = ShapeFactory::createShapeFromInfo(info); + const btCollisionShape* shape = ShapeFactory::createShapeFromInfo(info); QCOMPARE(shape != nullptr, true); ShapeInfo otherInfo = info; diff --git a/tests/physics/src/ShapeManagerTests.cpp b/tests/physics/src/ShapeManagerTests.cpp index 24d4a5ab35..f214601a42 100644 --- a/tests/physics/src/ShapeManagerTests.cpp +++ b/tests/physics/src/ShapeManagerTests.cpp @@ -27,14 +27,14 @@ void ShapeManagerTests::testShapeAccounting() { QCOMPARE(numReferences, 0); // create one shape and verify we get a valid pointer - btCollisionShape* shape = shapeManager.getShape(info); + const btCollisionShape* shape = shapeManager.getShape(info); QCOMPARE(shape != nullptr, true); // verify number of shapes QCOMPARE(shapeManager.getNumShapes(), 1); // reference the shape again and verify that we get the same pointer - btCollisionShape* otherShape = shapeManager.getShape(info); + const btCollisionShape* otherShape = shapeManager.getShape(info); QCOMPARE(otherShape, shape); // verify number of references @@ -84,7 +84,7 @@ void ShapeManagerTests::testShapeAccounting() { void ShapeManagerTests::addManyShapes() { ShapeManager shapeManager; - QVector shapes; + QVector shapes; int numSizes = 100; float startSize = 1.0f; @@ -96,7 +96,7 @@ void ShapeManagerTests::addManyShapes() { 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); + const btCollisionShape* shape = shapeManager.getShape(info); shapes.push_back(shape); QCOMPARE(shape != nullptr, true); @@ -114,14 +114,14 @@ void ShapeManagerTests::addManyShapes() { // release each shape by pointer for (int i = 0; i < numShapes; ++i) { - btCollisionShape* shape = shapes[i]; + const btCollisionShape* shape = shapes[i]; bool success = shapeManager.releaseShape(shape); QCOMPARE(success, true); } // verify zero references for (int i = 0; i < numShapes; ++i) { - btCollisionShape* shape = shapes[i]; + const btCollisionShape* shape = shapes[i]; int numReferences = shapeManager.getNumReferences(shape); QCOMPARE(numReferences, 0); } @@ -133,10 +133,10 @@ void ShapeManagerTests::addBoxShape() { info.setBox(halfExtents); ShapeManager shapeManager; - btCollisionShape* shape = shapeManager.getShape(info); + const btCollisionShape* shape = shapeManager.getShape(info); ShapeInfo otherInfo = info; - btCollisionShape* otherShape = shapeManager.getShape(otherInfo); + const btCollisionShape* otherShape = shapeManager.getShape(otherInfo); QCOMPARE(shape, otherShape); } @@ -146,10 +146,10 @@ void ShapeManagerTests::addSphereShape() { info.setSphere(radius); ShapeManager shapeManager; - btCollisionShape* shape = shapeManager.getShape(info); + const btCollisionShape* shape = shapeManager.getShape(info); ShapeInfo otherInfo = info; - btCollisionShape* otherShape = shapeManager.getShape(otherInfo); + const btCollisionShape* otherShape = shapeManager.getShape(otherInfo); QCOMPARE(shape, otherShape); } @@ -161,10 +161,10 @@ void ShapeManagerTests::addCylinderShape() { info.setCylinder(radius, height); ShapeManager shapeManager; - btCollisionShape* shape = shapeManager.getShape(info); + const btCollisionShape* shape = shapeManager.getShape(info); ShapeInfo otherInfo = info; - btCollisionShape* otherShape = shapeManager.getShape(otherInfo); + const btCollisionShape* otherShape = shapeManager.getShape(otherInfo); QCOMPARE(shape, otherShape); */ } @@ -177,10 +177,10 @@ void ShapeManagerTests::addCapsuleShape() { info.setCapsule(radius, height); ShapeManager shapeManager; - btCollisionShape* shape = shapeManager.getShape(info); + const btCollisionShape* shape = shapeManager.getShape(info); ShapeInfo otherInfo = info; - btCollisionShape* otherShape = shapeManager.getShape(otherInfo); + const btCollisionShape* otherShape = shapeManager.getShape(otherInfo); QCOMPARE(shape, otherShape); */ } @@ -219,14 +219,14 @@ void ShapeManagerTests::addCompoundShape() { // create the shape ShapeManager shapeManager; - btCollisionShape* shape = shapeManager.getShape(info); + const btCollisionShape* shape = shapeManager.getShape(info); QVERIFY(shape != nullptr); // verify the shape is correct type QCOMPARE(shape->getShapeType(), (int)COMPOUND_SHAPE_PROXYTYPE); // verify the shape has correct number of children - btCompoundShape* compoundShape = static_cast(shape); + const btCompoundShape* compoundShape = static_cast(shape); QCOMPARE(compoundShape->getNumChildShapes(), numHulls); // verify manager has only one shape