I am trying to design a geometric intersection API. Below is the code to represent geometric elements.
#include <iostream>
#include <memory>
// Since I did not write the Shape class I cannot edit or change the geometric elements class.
class Shape {
//
public:
virtual ~Shape() {}
};
class Segment : public Shape {
public:
~Segment() {}
};
class Triangle : public Shape {
public:
~Triangle() {}
};
class Quad : public Shape {
public:
~Quad() {}
};
// Below is the function that handles the intersection of the geometric elements.
bool intersect(const std::shared_ptr<Segment> &s1,
const std::shared_ptr<Segment> &s2) {
std::cout << "Seg Seg " << std::endl;
// Algorithm goes here
return true;
}
bool intersect(const std::shared_ptr<Segment> &s1,
const std::shared_ptr<Triangle> &t1) {
std::cout << "Seg Tri " << std::endl;
// Algorithm goes here
return true;
}
bool intersect(const std::shared_ptr<Triangle> &t1,
const std::shared_ptr<Segment> &s1
) {
return intersect(s1, t1);
}
bool intersect(const std::shared_ptr<Shape> &s1,
const std::shared_ptr<Shape> &s2) {
// Segment Segment
{
const std::shared_ptr<Segment> segment1 =
std::dynamic_pointer_cast<Segment>(s1);
const std::shared_ptr<Segment> segment2 =
std::dynamic_pointer_cast<Segment>(s2);
if (segment1 && segment2) {
return intersect(segment1, segment2);
}
}
// Segment Triangle
{
const std::shared_ptr<Segment> segment =
std::dynamic_pointer_cast<Segment>(s1);
const std::shared_ptr<Triangle> triangle =
std::dynamic_pointer_cast<Triangle>(s2);
if (segment && triangle) {
return intersect(segment, triangle);
}
}
// Triangle Segment
{
const std::shared_ptr<Triangle> triangle =
std::dynamic_pointer_cast<Triangle>(s1);
const std::shared_ptr<Segment> segment =
std::dynamic_pointer_cast<Segment>(s2);
if (segment && triangle) {
return intersect(triangle, segment);
}
}
// Handle other types appropriately ..
return false;
}
int main() {
std::shared_ptr<Shape> s1(std::make_shared<Segment>());
std::shared_ptr<Shape> s2(std::make_shared<Segment>());
std::shared_ptr<Shape> t1(std::make_shared<Triangle>());
bool ret_val_seg_seg = intersect(s1, s2);
bool ret_val_seg_tri = intersect(s1, t1);
bool ret_val_tri_seg = intersect(t1, s1);
}
My main concern is with code that is inside function bool intersect(const std::shared_ptr<Shape> &s1, const std::shared_ptr<Shape> &s2)
and the use of dynamic_pointer_cast
. Is there any better way to handle the geometric elements. I am open any suggestion.
My worry in that function is :
To do Triangle Segment intersection
it has to go through six dynamic cast
and three
if statement. The last combination of intersection get hit the most.
shared_ptr
as function arguments? Within theintersect
functions you do not have any ownership changes, so you can directly use a const reference to the shape types instead of a const reference to ashared_ptr
.