Skip to content

Commit

Permalink
Merge pull request #1047 from Geode-solutions/fix/basic-object-exception
Browse files Browse the repository at this point in the history
fix(Exception): add basic object exceptions
  • Loading branch information
MelchiorSchuh authored Nov 26, 2024
2 parents ec958e3 + f734306 commit 724c3f7
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 4 deletions.
5 changes: 5 additions & 0 deletions include/geode/basic/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ namespace geode
* Always return 1.
*/
int opengeode_basic_api geode_lippincott();

/*!
* Catch all exceptions and rethrow an OpenGeodeException
*/
void opengeode_basic_api throw_lippincott();
} // namespace geode

#ifdef OPENGEODE_DEBUG
Expand Down
38 changes: 38 additions & 0 deletions include/geode/geometry/basic_objects/segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace geode
[[nodiscard]] const std::array< PointType, 2 >& vertices() const;
[[nodiscard]] BoundingBox< dimension > bounding_box() const;
[[nodiscard]] bool is_degenerated() const;
[[nodiscard]] std::string string() const;

private:
std::array< PointType, 2 > vertices_;
Expand Down Expand Up @@ -102,4 +103,41 @@ namespace geode
Segment< dimension >&& other ) noexcept;
};
ALIAS_1D_AND_2D_AND_3D( Segment );

template < index_t dimension >
class OpenGeodeSegmentException : public OpenGeodeException
{
public:
template < typename... Args >
explicit OpenGeodeSegmentException(
Segment< dimension > segment_in, const Args&... message )
: OpenGeodeException{ absl::StrCat(
message..., " at ", segment_in.string() ) },
segment{ std::move( segment_in ) }
{
}

OwnerSegment< dimension > segment;
};
ALIAS_1D_AND_2D_AND_3D( OpenGeodeSegmentException );
} // namespace geode

// NOLINTNEXTLINE
#define OPENGEODE_SEGMENT_EXCEPTION( dimension, condition, segment, ... ) \
if( ABSL_PREDICT_FALSE( !( condition ) ) ) \
throw geode::OpenGeodeSegmentException< dimension > \
{ \
segment, __VA_ARGS__ \
}

// NOLINTNEXTLINE
#define OPENGEODE_SEGMENT1D_EXCEPTION( condition, segment, ... ) \
OPENGEODE_SEGMENT_EXCEPTION( 1, condition, segment, __VA_ARGS__ )

// NOLINTNEXTLINE
#define OPENGEODE_SEGMENT2D_EXCEPTION( condition, segment, ... ) \
OPENGEODE_SEGMENT_EXCEPTION( 2, condition, segment, __VA_ARGS__ )

// NOLINTNEXTLINE
#define OPENGEODE_SEGMENT3D_EXCEPTION( condition, segment, ... ) \
OPENGEODE_SEGMENT_EXCEPTION( 3, condition, segment, __VA_ARGS__ )
25 changes: 25 additions & 0 deletions include/geode/geometry/basic_objects/tetrahedron.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace geode
[[nodiscard]] const std::array< PointType, 4 >& vertices() const;
[[nodiscard]] BoundingBox3D bounding_box() const;
[[nodiscard]] bool is_degenerated() const;
[[nodiscard]] std::string string() const;

private:
std::array< PointType, 4 > vertices_;
Expand Down Expand Up @@ -104,4 +105,28 @@ namespace geode
Tetrahedron( Tetrahedron&& other ) noexcept;
Tetrahedron& operator=( Tetrahedron&& other ) noexcept;
};

class OpenGeodeTetrahedronException : public OpenGeodeException
{
public:
template < typename... Args >
explicit OpenGeodeTetrahedronException(
Tetrahedron tetrahedron_in, const Args&... message )
: OpenGeodeException{ absl::StrCat(
message..., " at ", tetrahedron_in.string() ) },
tetrahedron{ std::move( tetrahedron_in ) }
{
}

OwnerTetrahedron tetrahedron;
};
} // namespace geode

// NOLINTNEXTLINE
#define OPENGEODE_TETRAHEDRON_EXCEPTION( \
dimension, condition, tetrahedron, ... ) \
if( ABSL_PREDICT_FALSE( !( condition ) ) ) \
throw geode::OpenGeodeTetrahedronException< dimension > \
{ \
tetrahedron, __VA_ARGS__ \
}
34 changes: 34 additions & 0 deletions include/geode/geometry/basic_objects/triangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace geode
[[nodiscard]] local_index_t longest_edge_index() const;
[[nodiscard]] double minimum_height() const;
[[nodiscard]] bool is_degenerated() const;
[[nodiscard]] std::string string() const;

private:
std::array< PointType, 3 > vertices_;
Expand Down Expand Up @@ -129,4 +130,37 @@ namespace geode
Triangle< dimension >&& other ) noexcept;
};
ALIAS_2D_AND_3D( Triangle );

template < index_t dimension >
class OpenGeodeTriangleException : public OpenGeodeException
{
public:
template < typename... Args >
explicit OpenGeodeTriangleException(
Triangle< dimension > triangle_in, const Args&... message )
: OpenGeodeException{ absl::StrCat(
message..., " at ", triangle_in.string() ) },
triangle{ std::move( triangle_in ) }
{
}

OwnerTriangle< dimension > triangle;
};
ALIAS_2D_AND_3D( OpenGeodeTriangleException );
} // namespace geode

// NOLINTNEXTLINE
#define OPENGEODE_TRIANGLE_EXCEPTION( dimension, condition, triangle, ... ) \
if( ABSL_PREDICT_FALSE( !( condition ) ) ) \
throw geode::OpenGeodeTriangleException< dimension > \
{ \
triangle, __VA_ARGS__ \
}

// NOLINTNEXTLINE
#define OPENGEODE_TRIANGLE2D_EXCEPTION( condition, triangle, ... ) \
OPENGEODE_TRIANGLE_EXCEPTION( 2, condition, triangle, __VA_ARGS__ )

// NOLINTNEXTLINE
#define OPENGEODE_TRIANGLE3D_EXCEPTION( condition, triangle, ... ) \
OPENGEODE_TRIANGLE_EXCEPTION( 3, condition, triangle, __VA_ARGS__ )
6 changes: 3 additions & 3 deletions include/geode/geometry/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@ namespace geode
}

// NOLINTNEXTLINE
#define OPENGEODE_POINT_EXCEPTION1D( condition, point, ... ) \
#define OPENGEODE_POINT1D_EXCEPTION( condition, point, ... ) \
OPENGEODE_POINT_EXCEPTION( 1, condition, point, __VA_ARGS__ )

// NOLINTNEXTLINE
#define OPENGEODE_POINT_EXCEPTION2D( condition, point, ... ) \
#define OPENGEODE_POINT2D_EXCEPTION( condition, point, ... ) \
OPENGEODE_POINT_EXCEPTION( 2, condition, point, __VA_ARGS__ )

// NOLINTNEXTLINE
#define OPENGEODE_POINT_EXCEPTION3D( condition, point, ... ) \
#define OPENGEODE_POINT3D_EXCEPTION( condition, point, ... ) \
OPENGEODE_POINT_EXCEPTION( 3, condition, point, __VA_ARGS__ )

namespace std
Expand Down
20 changes: 20 additions & 0 deletions src/geode/basic/assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,24 @@ namespace geode
}
return 1;
}

void throw_lippincott()
{
try
{
throw;
}
catch( const OpenGeodeException& /*unused*/ )
{
throw;
}
catch( const std::exception& exception )
{
throw OpenGeodeException{ "std::exception, ", exception.what() };
}
catch( ... )
{
throw OpenGeodeException{ "Unknown exception" };
}
}
} // namespace geode
8 changes: 8 additions & 0 deletions src/geode/geometry/basic_objects/segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ namespace geode
return length() <= GLOBAL_EPSILON;
}

template < typename PointType, index_t dimension >
std::string GenericSegment< PointType, dimension >::string() const
{
const Point< dimension >& point0 = vertices_[0];
const Point< dimension >& point1 = vertices_[1];
return absl::StrCat( "[", point0.string(), ", ", point1.string(), "]" );
}

template < index_t dimension >
OwnerSegment< dimension >::OwnerSegment(
Point< dimension > point0, Point< dimension > point1 ) noexcept
Expand Down
11 changes: 11 additions & 0 deletions src/geode/geometry/basic_objects/tetrahedron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ namespace geode
<= GLOBAL_EPSILON;
}

template < typename PointType >
std::string GenericTetrahedron< PointType >::string() const
{
const Point3D& point0 = vertices_[0];
const Point3D& point1 = vertices_[1];
const Point3D& point2 = vertices_[2];
const Point3D& point3 = vertices_[3];
return absl::StrCat( "[", point0.string(), ", ", point1.string(), ", ",
point2.string(), ", ", point3.string(), "]" );
}

OwnerTetrahedron::OwnerTetrahedron( Point3D point0,
Point3D point1,
Point3D point2,
Expand Down
10 changes: 10 additions & 0 deletions src/geode/geometry/basic_objects/triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ namespace geode
<= GLOBAL_EPSILON;
}

template < typename PointType, index_t dimension >
std::string GenericTriangle< PointType, dimension >::string() const
{
const Point< dimension >& point0 = vertices_[0];
const Point< dimension >& point1 = vertices_[1];
const Point< dimension >& point2 = vertices_[2];
return absl::StrCat( "[", point0.string(), ", ", point1.string(), ", ",
point2.string(), "]" );
}

template < index_t dimension >
OwnerTriangle< dimension >::OwnerTriangle( Point< dimension > point0,
Point< dimension > point1,
Expand Down
2 changes: 1 addition & 1 deletion src/geode/geometry/bounding_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ namespace geode
for( const auto c : geode::LRange{ dimension } )
{
const auto local_distance = std::min(
std::abs( Pmin.value( c ) ), std::abs( Pmax.value( c ) ) );
std::fabs( Pmin.value( c ) ), std::fabs( Pmax.value( c ) ) );
inner_distance = std::min( inner_distance, local_distance );
}
return -inner_distance;
Expand Down

0 comments on commit 724c3f7

Please sign in to comment.