Skip to content

Commit

Permalink
Merge pull request #911 from Geode-solutions/feat/polyhedron_facet_area
Browse files Browse the repository at this point in the history
feat(SolidMesh): add polyhedron_facet_area
  • Loading branch information
panquez authored Mar 26, 2024
2 parents 28c61a3 + b5a5832 commit f4ceb1a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/geode/mesh/core/solid_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ namespace geode
Vector3D OPENGEODE_MESH_DEPRECATED polyhedron_facet_normal(
const PolyhedronFacet& polyhedron_facet ) const;

/*!
* Return the area of a given PolyhedronFacet.
* @param[in] polyhedron_facet Local index of facet in polyhedron.
*/
double polyhedron_facet_area(
const PolyhedronFacet& polyhedron_facet ) const;

/*!
* Return the normal of a given PolyhedronFacet.
* @param[in] polyhedron_facet Local index of facet in polyhedron.
Expand Down
22 changes: 22 additions & 0 deletions src/geode/mesh/core/solid_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,28 @@ namespace geode
return volume;
}

template < index_t dimension >
double SolidMesh< dimension >::polyhedron_facet_area(
const PolyhedronFacet& polyhedron_facet ) const
{
if( nb_polyhedron_facet_vertices( polyhedron_facet ) < 3 )
{
return 0;
}
double area{ 0 };
const auto direction = new_polyhedron_facet_normal( polyhedron_facet )
.value_or( Vector3D{ { 0, 0, 1 } } );
const auto vertices = polyhedron_facet_vertices( polyhedron_facet );
const auto& p1 = this->point( vertices[0] );
for( const auto i : LRange{ 1, vertices.size() - 1 } )
{
const auto& p2 = this->point( vertices[i] );
const auto& p3 = this->point( vertices[i + 1] );
area += triangle_signed_area( { p1, p2, p3 }, direction );
}
return area;
}

template < index_t dimension >
Vector3D SolidMesh< dimension >::polyhedron_facet_normal(
const PolyhedronFacet& polyhedron_facet ) const
Expand Down
17 changes: 17 additions & 0 deletions tests/mesh/test-tetrahedral-solid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <geode/basic/logger.h>

#include <geode/geometry/basic_objects/tetrahedron.h>
#include <geode/geometry/basic_objects/triangle.h>
#include <geode/geometry/mensuration.h>
#include <geode/geometry/point.h>

Expand Down Expand Up @@ -82,6 +83,21 @@ void test_polyhedron_volumes( const geode::TetrahedralSolid3D& solid )
}
}

void test_polyhedron_facet_area( const geode::TetrahedralSolid3D& solid )
{
for( const auto p : geode::Range{ solid.nb_polyhedra() } )
{
for( const auto f : geode::LRange{ solid.nb_polyhedron_facets( p ) } )
{
auto actual = std::fabs( solid.polyhedron_facet_area( { p, f } ) );
auto expected = geode::triangle_area( solid.triangle( { p, f } ) );
OPENGEODE_EXCEPTION(
std::fabs( actual - expected ) < geode::global_epsilon,
"[Test] Not correct tetrahedron facet area computation" );
}
}
}

void test_polyhedron_adjacencies( const geode::TetrahedralSolid3D& solid,
geode::TetrahedralSolidBuilder3D& builder )
{
Expand Down Expand Up @@ -369,6 +385,7 @@ void test()
test_create_vertices( *solid, *builder );
test_create_tetrahedra( *solid, *builder );
test_polyhedron_volumes( *solid );
test_polyhedron_facet_area( *solid );
test_polyhedron_adjacencies( *solid, *builder );
test_is_on_border( *solid );
test_io( *solid, absl::StrCat( "test.", solid->native_extension() ) );
Expand Down

0 comments on commit f4ceb1a

Please sign in to comment.