Skip to content

Commit 9717261

Browse files
Merge pull request #1150 from Geode-solutions/feat/geometry_adding_degeneration_functions
Feat/geometry_adding_degeneration_functions
2 parents 652e612 + bcc8e00 commit 9717261

File tree

7 files changed

+97
-58
lines changed

7 files changed

+97
-58
lines changed

include/geode/geometry/basic_objects/polygon.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ namespace geode
7777
[[nodiscard]] BoundingBox< dimension > bounding_box() const;
7878
[[nodiscard]] std::vector< std::array< index_t, 3 > >
7979
triangulate() const;
80+
[[nodiscard]] double minimum_height() const;
8081
[[nodiscard]] bool is_degenerated() const;
8182
[[nodiscard]] std::string string() const;
8283

include/geode/mesh/core/solid_mesh.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ namespace geode
592592
[[nodiscard]] std::optional< PolyhedronVertex >
593593
polyhedron_around_vertex( index_t vertex_id ) const;
594594

595+
[[nodiscard]] double polyhedron_minimum_height(
596+
index_t polyhedron_id ) const;
597+
595598
public:
596599
void associate_polyhedron_vertex_to_vertex(
597600
const PolyhedronVertex& polyhedron_vertex,

include/geode/mesh/core/surface_mesh.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ namespace geode
442442
[[nodiscard]] std::optional< PolygonVertex > polygon_around_vertex(
443443
index_t vertex_id ) const;
444444

445+
[[nodiscard]] double polygon_minimum_height( index_t polygon_id ) const;
446+
445447
public:
446448
void associate_polygon_vertex_to_vertex(
447449
const PolygonVertex& polygon_vertex,

src/geode/geometry/basic_objects/polygon.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -235,43 +235,47 @@ namespace geode
235235
}
236236

237237
template < typename PointType, index_t dimension >
238-
bool GenericPolygon< PointType, dimension >::is_degenerated() const
238+
double GenericPolygon< PointType, dimension >::minimum_height() const
239239
{
240+
const auto nb_vertices = vertices_.size();
240241
double max_length{ 0. };
241-
index_t max_length_edge{ 0 };
242-
for( const auto e : Range{ nb_vertices() } )
242+
geode::index_t max_length_edge{ 0 };
243+
for( const auto e : geode::Range{ nb_vertices } )
243244
{
244-
const Point< dimension >& point0 = vertices_[e];
245-
const Point< dimension >& point1 =
246-
vertices_[e == nb_vertices() - 1 ? 0 : e + 1];
247-
const auto cur_length = point_point_distance( point0, point1 );
245+
const geode::Point< dimension >& point0 = vertices_[e];
246+
const geode::Point< dimension >& point1 =
247+
vertices_[e == nb_vertices - 1 ? 0 : e + 1];
248+
const auto cur_length =
249+
geode::point_point_distance( point0, point1 );
248250
if( cur_length > max_length )
249251
{
250252
max_length = cur_length;
251253
max_length_edge = e;
252254
}
253255
}
254-
if( max_length < GLOBAL_EPSILON )
255-
{
256-
return true;
257-
}
258256
const auto next =
259-
max_length_edge + 1 == nb_vertices() ? 0 : max_length_edge + 1;
260-
const InfiniteLine< dimension > line{ Segment< dimension >{
261-
vertices_[max_length_edge], vertices_[next] } };
262-
for( const auto v : Range{ nb_vertices() } )
257+
max_length_edge + 1 == nb_vertices ? 0 : max_length_edge + 1;
258+
const geode::InfiniteLine< dimension > line{
259+
geode::Segment< dimension >{
260+
vertices_[max_length_edge], vertices_[next] }
261+
};
262+
auto opposite_vertex = 0;
263+
for( const auto vertex : geode::Range{ nb_vertices } )
263264
{
264-
if( v == max_length_edge || v == next )
265+
if( vertex == max_length_edge || vertex == next )
265266
{
266267
continue;
267268
}
268-
const Point< dimension >& point = vertices_[v];
269-
if( point_line_distance( point, line ) > GLOBAL_EPSILON )
270-
{
271-
return false;
272-
}
269+
opposite_vertex = vertex;
273270
}
274-
return true;
271+
const geode::Point< dimension >& point = vertices_[opposite_vertex];
272+
return geode::point_line_distance( point, line );
273+
}
274+
275+
template < typename PointType, index_t dimension >
276+
bool GenericPolygon< PointType, dimension >::is_degenerated() const
277+
{
278+
return minimum_height() < GLOBAL_EPSILON;
275279
}
276280

277281
template < typename PointType, index_t dimension >

src/geode/mesh/core/solid_mesh.cpp

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ namespace geode
458458
}
459459

460460
std::optional< PolyhedronVertex > polyhedron_around_vertex(
461-
const index_t vertex_id ) const
461+
index_t vertex_id ) const
462462
{
463463
const auto& value = polyhedron_around_vertex_->value( vertex_id );
464464
if( value.polyhedron_id != NO_ID )
@@ -468,6 +468,46 @@ namespace geode
468468
return std::nullopt;
469469
}
470470

471+
double polyhedron_minimum_height(
472+
const geode::SolidMesh< dimension >& mesh,
473+
const index_t polyhedron_id ) const
474+
{
475+
double max_area{ 0. };
476+
local_index_t max_area_facet{ 0 };
477+
for( const auto f :
478+
LRange{ mesh.nb_polyhedron_facets( polyhedron_id ) } )
479+
{
480+
const auto cur_area =
481+
mesh.polyhedron_facet_area( { polyhedron_id, f } );
482+
if( cur_area > max_area )
483+
{
484+
max_area = cur_area;
485+
max_area_facet = f;
486+
}
487+
}
488+
const auto vertices = mesh.polyhedron_vertices( polyhedron_id );
489+
const auto normal = mesh.polyhedron_facet_normal(
490+
{ polyhedron_id, max_area_facet } );
491+
if( !normal )
492+
{
493+
return true;
494+
}
495+
const auto facet_vertices = mesh.polyhedron_facet_vertices(
496+
{ polyhedron_id, max_area_facet } );
497+
Plane plane{ normal.value(), mesh.point( facet_vertices[0] ) };
498+
auto opposite_vertex{ 0 };
499+
for( const auto vertex_id : vertices )
500+
{
501+
if( absl::c_contains( facet_vertices, vertex_id ) )
502+
{
503+
continue;
504+
}
505+
opposite_vertex = vertex_id;
506+
}
507+
return std::get< 0 >(
508+
point_plane_distance( mesh.point( opposite_vertex ), plane ) );
509+
}
510+
471511
void reset_polyhedra_around_vertex( index_t vertex_id )
472512
{
473513
polyhedra_around_vertex_->modify_value(
@@ -960,41 +1000,7 @@ namespace geode
9601000
bool SolidMesh< dimension >::is_polyhedron_degenerated(
9611001
index_t polyhedron_id ) const
9621002
{
963-
double max_area{ 0. };
964-
local_index_t max_area_facet{ 0 };
965-
for( const auto f : LRange{ nb_polyhedron_facets( polyhedron_id ) } )
966-
{
967-
const auto cur_area = polyhedron_facet_area( { polyhedron_id, f } );
968-
if( cur_area > max_area )
969-
{
970-
max_area = cur_area;
971-
max_area_facet = f;
972-
}
973-
}
974-
const auto vertices = polyhedron_vertices( polyhedron_id );
975-
const auto normal =
976-
polyhedron_facet_normal( { polyhedron_id, max_area_facet } );
977-
if( !normal )
978-
{
979-
return true;
980-
}
981-
const auto facet_vertices =
982-
polyhedron_facet_vertices( { polyhedron_id, max_area_facet } );
983-
Plane plane{ normal.value(), this->point( facet_vertices[0] ) };
984-
for( const auto vertex_id : vertices )
985-
{
986-
if( absl::c_contains( facet_vertices, vertex_id ) )
987-
{
988-
continue;
989-
}
990-
if( std::get< 0 >(
991-
point_plane_distance( this->point( vertex_id ), plane ) )
992-
> GLOBAL_EPSILON )
993-
{
994-
return false;
995-
}
996-
}
997-
return true;
1003+
return polyhedron_minimum_height( polyhedron_id ) < GLOBAL_EPSILON;
9981004
}
9991005

10001006
template < index_t dimension >
@@ -1358,6 +1364,13 @@ namespace geode
13581364
return get_polyhedron_around_vertex( vertex_id );
13591365
}
13601366

1367+
template < index_t dimension >
1368+
double SolidMesh< dimension >::polyhedron_minimum_height(
1369+
const index_t polyhedron_id ) const
1370+
{
1371+
return impl_->polyhedron_minimum_height( *this, polyhedron_id );
1372+
}
1373+
13611374
template < index_t dimension >
13621375
std::optional< PolyhedronVertex >
13631376
SolidMesh< dimension >::get_polyhedron_around_vertex(

src/geode/mesh/core/surface_mesh.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ namespace geode
285285
return result;
286286
}
287287

288+
double polygon_minimum_height(
289+
const SurfaceMesh< dimension >& mesh, index_t polygon_id ) const
290+
{
291+
OPENGEODE_EXCEPTION( polygon_id < mesh.nb_polygons(),
292+
"[Impl::polygon_minimum_height] Wrong polygon id" );
293+
return mesh.polygon( polygon_id ).minimum_height();
294+
}
295+
288296
std::optional< PolygonVertex > polygon_around_vertex(
289297
const index_t vertex_id ) const
290298
{
@@ -594,6 +602,13 @@ namespace geode
594602
return get_polygon_around_vertex( vertex_id );
595603
}
596604

605+
template < index_t dimension >
606+
double SurfaceMesh< dimension >::polygon_minimum_height(
607+
index_t polygon_id ) const
608+
{
609+
return impl_->polygon_minimum_height( *this, polygon_id );
610+
}
611+
597612
template < index_t dimension >
598613
std::optional< PolygonVertex >
599614
SurfaceMesh< dimension >::get_polygon_around_vertex(

src/geode/mesh/helpers/triangulated_surface_scalar_function.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <geode/basic/attribute_manager.hpp>
2727
#include <geode/basic/pimpl_impl.hpp>
28+
#include <geode/basic/variable_attribute.hpp>
2829

2930
#include <geode/geometry/barycentric_coordinates.hpp>
3031
#include <geode/geometry/basic_objects/triangle.hpp>

0 commit comments

Comments
 (0)