Skip to content

Commit bb0f823

Browse files
committed
fix(Mesh): add helpers to remove elements having same vertices
1 parent 1f598c6 commit bb0f823

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2019 - 2025 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#pragma once
25+
26+
#include <geode/mesh/common.hpp>
27+
28+
namespace geode
29+
{
30+
FORWARD_DECLARATION_DIMENSION_CLASS( PointSet );
31+
FORWARD_DECLARATION_DIMENSION_CLASS( EdgedCurve );
32+
FORWARD_DECLARATION_DIMENSION_CLASS( SurfaceMesh );
33+
FORWARD_DECLARATION_DIMENSION_CLASS( SolidMesh );
34+
FORWARD_DECLARATION_DIMENSION_CLASS( PointSetBuilder );
35+
FORWARD_DECLARATION_DIMENSION_CLASS( EdgedCurveBuilder );
36+
FORWARD_DECLARATION_DIMENSION_CLASS( SurfaceMeshBuilder );
37+
FORWARD_DECLARATION_DIMENSION_CLASS( SolidMeshBuilder );
38+
ALIAS_3D( SolidMesh );
39+
ALIAS_3D( SolidMeshBuilder );
40+
} // namespace geode
41+
42+
namespace geode
43+
{
44+
template < index_t dimension >
45+
void remove_edge_duplication( const EdgedCurve< dimension >& mesh,
46+
EdgedCurveBuilder< dimension >& builder );
47+
48+
template < index_t dimension >
49+
void remove_polygon_duplication( const SurfaceMesh< dimension >& mesh,
50+
SurfaceMeshBuilder< dimension >& builder );
51+
52+
void opengeode_mesh_api remove_polyhedron_duplication(
53+
const SolidMesh3D& mesh, SolidMeshBuilder3D& builder );
54+
} // namespace geode

src/geode/mesh/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ add_geode_library(
116116
"helpers/grid_scalar_function.cpp"
117117
"helpers/repair_polygon_orientations.cpp"
118118
"helpers/remove_vertex_duplication.cpp"
119+
"helpers/remove_element_duplication.cpp"
119120
"helpers/tetrahedral_solid_point_function.cpp"
120121
"helpers/tetrahedral_solid_scalar_function.cpp"
121122
"helpers/triangulated_surface_point_function.cpp"
@@ -259,6 +260,7 @@ add_geode_library(
259260
"helpers/grid_scalar_function.hpp"
260261
"helpers/repair_polygon_orientations.hpp"
261262
"helpers/remove_vertex_duplication.hpp"
263+
"helpers/remove_element_duplication.hpp"
262264
"helpers/tetrahedral_solid_point_function.hpp"
263265
"helpers/tetrahedral_solid_scalar_function.hpp"
264266
"helpers/triangulated_surface_point_function.hpp"
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2019 - 2025 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#include <geode/mesh/helpers/remove_element_duplication.hpp>
25+
26+
#include <geode/mesh/builder/edged_curve_builder.hpp>
27+
#include <geode/mesh/builder/point_set_builder.hpp>
28+
#include <geode/mesh/builder/solid_edges_builder.hpp>
29+
#include <geode/mesh/builder/solid_facets_builder.hpp>
30+
#include <geode/mesh/builder/solid_mesh_builder.hpp>
31+
#include <geode/mesh/builder/surface_edges_builder.hpp>
32+
#include <geode/mesh/builder/surface_mesh_builder.hpp>
33+
#include <geode/mesh/core/edged_curve.hpp>
34+
#include <geode/mesh/core/point_set.hpp>
35+
#include <geode/mesh/core/solid_mesh.hpp>
36+
#include <geode/mesh/core/surface_mesh.hpp>
37+
#include <geode/mesh/helpers/generic_edged_curve_accessor.hpp>
38+
#include <geode/mesh/helpers/generic_solid_accessor.hpp>
39+
#include <geode/mesh/helpers/generic_surface_accessor.hpp>
40+
41+
namespace
42+
{
43+
44+
template < typename Mesh >
45+
absl::flat_hash_map< geode::index_t, std::vector< geode::index_t > >
46+
compute_vertex_to_element_mapping( const Mesh& mesh )
47+
{
48+
using Accessor = geode::GenericMeshAccessor< Mesh >;
49+
const Accessor accessor{ mesh };
50+
absl::flat_hash_map< geode::index_t, std::vector< geode::index_t > >
51+
vertex_to_element;
52+
for( const auto element_id : geode::Range{ accessor.nb_elements() } )
53+
{
54+
for( const auto vertex_id :
55+
accessor.element_vertices( element_id ) )
56+
{
57+
vertex_to_element[vertex_id].push_back( element_id );
58+
}
59+
}
60+
return vertex_to_element;
61+
}
62+
63+
template < geode::index_t dimension >
64+
void delete_elements( const std::vector< bool >& to_delete,
65+
geode::EdgedCurveBuilder< dimension >& builder )
66+
{
67+
builder.delete_edges( to_delete );
68+
}
69+
template < geode::index_t dimension >
70+
void delete_elements( const std::vector< bool >& to_delete,
71+
geode::SurfaceMeshBuilder< dimension >& builder )
72+
{
73+
builder.delete_polygons( to_delete );
74+
}
75+
template < geode::index_t dimension >
76+
void delete_elements( const std::vector< bool >& to_delete,
77+
geode::SolidMeshBuilder< dimension >& builder )
78+
{
79+
builder.delete_polyhedra( to_delete );
80+
}
81+
82+
template < typename Mesh, typename Builder >
83+
void remove_element_duplication( const Mesh& mesh, Builder& builder )
84+
{
85+
const auto vertex_to_element =
86+
compute_vertex_to_element_mapping( mesh );
87+
using Accessor = geode::GenericMeshAccessor< Mesh >;
88+
const Accessor accessor{ mesh };
89+
std::vector< bool > to_delete( accessor.nb_elements(), false );
90+
for( const auto element_id : geode::Range{ accessor.nb_elements() } )
91+
{
92+
if( to_delete[element_id] )
93+
{
94+
continue;
95+
}
96+
const auto element_vertices =
97+
accessor.element_vertices( element_id );
98+
std::vector< geode::index_t > common_elements =
99+
vertex_to_element.at( element_vertices[0] );
100+
for( const auto v :
101+
geode::LRange{ 1, accessor.nb_element_vertices( element_id ) } )
102+
{
103+
std::vector< geode::index_t > temp;
104+
absl::c_set_intersection( common_elements,
105+
vertex_to_element.at( element_vertices[v] ),
106+
std::back_inserter( temp ) );
107+
common_elements = std::move( temp );
108+
}
109+
if( common_elements.size() > 1 )
110+
{
111+
for( const auto common_element_id : common_elements )
112+
{
113+
if( element_id == common_element_id )
114+
{
115+
continue;
116+
}
117+
to_delete[common_element_id] = true;
118+
}
119+
}
120+
}
121+
delete_elements( to_delete, builder );
122+
}
123+
} // namespace
124+
125+
namespace geode
126+
{
127+
template < index_t dimension >
128+
void remove_edge_duplication( const EdgedCurve< dimension >& mesh,
129+
EdgedCurveBuilder< dimension >& builder )
130+
{
131+
::remove_element_duplication( mesh, builder );
132+
}
133+
134+
template < index_t dimension >
135+
void remove_polygon_duplication( const SurfaceMesh< dimension >& mesh,
136+
SurfaceMeshBuilder< dimension >& builder )
137+
{
138+
::remove_element_duplication( mesh, builder );
139+
}
140+
141+
void remove_polyhedron_duplication(
142+
const SolidMesh3D& mesh, SolidMeshBuilder3D& builder )
143+
{
144+
::remove_element_duplication( mesh, builder );
145+
}
146+
147+
template void opengeode_mesh_api remove_edge_duplication(
148+
const EdgedCurve2D&, EdgedCurveBuilder2D& );
149+
template void opengeode_mesh_api remove_edge_duplication(
150+
const EdgedCurve3D&, EdgedCurveBuilder3D& );
151+
152+
template void opengeode_mesh_api remove_polygon_duplication(
153+
const SurfaceMesh2D&, SurfaceMeshBuilder2D& );
154+
template void opengeode_mesh_api remove_polygon_duplication(
155+
const SurfaceMesh3D&, SurfaceMeshBuilder3D& );
156+
157+
} // namespace geode

src/geode/mesh/helpers/repair_polygon_orientations.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ namespace
9797
== reorient_polygon_[adj->polygon_id]
9898
: cur_polygon_reorient
9999
!= reorient_polygon_[adj->polygon_id];
100+
if( !is_valid )
101+
{
102+
DEBUG( "Mobius strip detected" );
103+
DEBUG( cur_polygon );
104+
DEBUG( e );
105+
SDEBUG( adj.value() );
106+
SDEBUG( mesh_.polygon_barycenter( cur_polygon ) );
107+
SDEBUG(
108+
mesh_.polygon_barycenter( adj->polygon_id ) );
109+
}
100110
OPENGEODE_DATA_EXCEPTION( is_valid,
101111
"[RepairPolygonOrientations] Mobius "
102112
"strip detected, polygons orientations "

0 commit comments

Comments
 (0)