Skip to content

Commit

Permalink
get rid of dynamic_cast
Browse files Browse the repository at this point in the history
This library does not need RTTI as all bodies & shapes are marked with `type`.

The EXPECT_TRUE in the test degenerates to true for all minimally-optimizing compilers.
Replaced it with something meaningful.
  • Loading branch information
v4hn committed May 22, 2020
1 parent a40825a commit 4dedda8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/body_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ void bodies::computeBoundingSphere(const std::vector<const bodies::Body*>& bodie
unsigned int vertex_count = 0;
for (auto body : bodies)
{
const bodies::ConvexMesh* conv = dynamic_cast<const bodies::ConvexMesh*>(body);
if (!conv)
if (!body || body->getType() != MESH)
continue;
// MESH type implies bodies::ConvexMesh
const bodies::ConvexMesh* conv = static_cast<const bodies::ConvexMesh*>(body);

for (unsigned int j = 0; j < conv->getScaledVertices().size(); j++, vertex_count++)
{
sum += conv->getPose() * conv->getScaledVertices()[j];
Expand All @@ -174,9 +176,11 @@ void bodies::computeBoundingSphere(const std::vector<const bodies::Body*>& bodie
double max_dist_squared = 0.0;
for (auto body : bodies)
{
const bodies::ConvexMesh* conv = dynamic_cast<const bodies::ConvexMesh*>(body);
if (!conv)
if (!body || body->getType() != MESH)
continue;
// MESH type implies bodies::ConvexMesh
const bodies::ConvexMesh* conv = static_cast<const bodies::ConvexMesh*>(body);

for (unsigned int j = 0; j < conv->getScaledVertices().size(); j++)
{
double dist = (conv->getPose() * conv->getScaledVertices()[j] - sphere.center).squaredNorm();
Expand Down
4 changes: 2 additions & 2 deletions test/test_bounding_sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ TEST(ConeBoundingSphere, Cone4)
TEST(MeshBoundingSphere, Mesh1)
{
shapes::Shape* shape = new shapes::Mesh(8, 12);
shapes::Mesh* m = dynamic_cast<shapes::Mesh*>(shape);
EXPECT_TRUE(m);
EXPECT_EQ(shape->type, shapes::MESH);
shapes::Mesh* m = static_cast<shapes::Mesh*>(shape);

// box mesh

Expand Down

0 comments on commit 4dedda8

Please sign in to comment.