Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions include/geode/mesh/helpers/remove_element_duplication.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2019 - 2025 Geode-solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#pragma once

#include <geode/mesh/common.hpp>

namespace geode
{
FORWARD_DECLARATION_DIMENSION_CLASS( PointSet );
FORWARD_DECLARATION_DIMENSION_CLASS( EdgedCurve );
FORWARD_DECLARATION_DIMENSION_CLASS( SurfaceMesh );
FORWARD_DECLARATION_DIMENSION_CLASS( SolidMesh );
FORWARD_DECLARATION_DIMENSION_CLASS( PointSetBuilder );
FORWARD_DECLARATION_DIMENSION_CLASS( EdgedCurveBuilder );
FORWARD_DECLARATION_DIMENSION_CLASS( SurfaceMeshBuilder );
FORWARD_DECLARATION_DIMENSION_CLASS( SolidMeshBuilder );
ALIAS_3D( SolidMesh );
ALIAS_3D( SolidMeshBuilder );
} // namespace geode

namespace geode
{
template < index_t dimension >
void remove_edge_duplication( const EdgedCurve< dimension >& mesh,
EdgedCurveBuilder< dimension >& builder );

template < index_t dimension >
void remove_polygon_duplication( const SurfaceMesh< dimension >& mesh,
SurfaceMeshBuilder< dimension >& builder );

void opengeode_mesh_api remove_polyhedron_duplication(
const SolidMesh3D& mesh, SolidMeshBuilder3D& builder );
} // namespace geode
2 changes: 2 additions & 0 deletions src/geode/mesh/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ add_geode_library(
"helpers/grid_scalar_function.cpp"
"helpers/repair_polygon_orientations.cpp"
"helpers/remove_vertex_duplication.cpp"
"helpers/remove_element_duplication.cpp"
"helpers/tetrahedral_solid_point_function.cpp"
"helpers/tetrahedral_solid_scalar_function.cpp"
"helpers/triangulated_surface_point_function.cpp"
Expand Down Expand Up @@ -259,6 +260,7 @@ add_geode_library(
"helpers/grid_scalar_function.hpp"
"helpers/repair_polygon_orientations.hpp"
"helpers/remove_vertex_duplication.hpp"
"helpers/remove_element_duplication.hpp"
"helpers/tetrahedral_solid_point_function.hpp"
"helpers/tetrahedral_solid_scalar_function.hpp"
"helpers/triangulated_surface_point_function.hpp"
Expand Down
155 changes: 155 additions & 0 deletions src/geode/mesh/helpers/remove_element_duplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2019 - 2025 Geode-solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#include <geode/mesh/helpers/remove_element_duplication.hpp>

#include <geode/mesh/builder/edged_curve_builder.hpp>
#include <geode/mesh/builder/solid_edges_builder.hpp>
#include <geode/mesh/builder/solid_facets_builder.hpp>
#include <geode/mesh/builder/solid_mesh_builder.hpp>
#include <geode/mesh/builder/surface_edges_builder.hpp>
#include <geode/mesh/builder/surface_mesh_builder.hpp>
#include <geode/mesh/core/edged_curve.hpp>
#include <geode/mesh/core/solid_mesh.hpp>
#include <geode/mesh/core/surface_mesh.hpp>
#include <geode/mesh/helpers/generic_edged_curve_accessor.hpp>
#include <geode/mesh/helpers/generic_solid_accessor.hpp>
#include <geode/mesh/helpers/generic_surface_accessor.hpp>

namespace
{

template < typename Mesh >
absl::flat_hash_map< geode::index_t, std::vector< geode::index_t > >
compute_vertex_to_element_mapping( const Mesh& mesh )
{
using Accessor = geode::GenericMeshAccessor< Mesh >;
const Accessor accessor{ mesh };
absl::flat_hash_map< geode::index_t, std::vector< geode::index_t > >
vertex_to_element;
for( const auto element_id : geode::Range{ accessor.nb_elements() } )
{
for( const auto vertex_id :
accessor.element_vertices( element_id ) )
{
vertex_to_element[vertex_id].push_back( element_id );
}
}
return vertex_to_element;
}

template < geode::index_t dimension >
void delete_elements( const std::vector< bool >& to_delete,
geode::EdgedCurveBuilder< dimension >& builder )
{
builder.delete_edges( to_delete );
}
template < geode::index_t dimension >
void delete_elements( const std::vector< bool >& to_delete,
geode::SurfaceMeshBuilder< dimension >& builder )
{
builder.delete_polygons( to_delete );
}
template < geode::index_t dimension >
void delete_elements( const std::vector< bool >& to_delete,
geode::SolidMeshBuilder< dimension >& builder )
{
builder.delete_polyhedra( to_delete );
}

template < typename Mesh, typename Builder >
void remove_element_duplication( const Mesh& mesh, Builder& builder )
Comment on lines +80 to +81
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
template < typename Mesh, typename Builder >
void remove_element_duplication( const Mesh& mesh, Builder& builder )
template < typename Mesh >
void remove_element_duplication( const Mesh& mesh, typename Mesh::Builder& builder )

{
const auto vertex_to_element =
compute_vertex_to_element_mapping( mesh );
using Accessor = geode::GenericMeshAccessor< Mesh >;
const Accessor accessor{ mesh };
std::vector< bool > to_delete( accessor.nb_elements(), false );
for( const auto element_id : geode::Range{ accessor.nb_elements() } )
{
if( to_delete[element_id] )
{
continue;
}
const auto element_vertices =
accessor.element_vertices( element_id );
std::vector< geode::index_t > common_elements =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::vector< geode::index_t > common_elements =
auto common_elements =

vertex_to_element.at( element_vertices[0] );
for( const auto v :
geode::LRange{ 1, accessor.nb_element_vertices( element_id ) } )
{
std::vector< geode::index_t > temp;
absl::c_set_intersection( common_elements,
vertex_to_element.at( element_vertices[v] ),
std::back_inserter( temp ) );
common_elements = std::move( temp );
}
if( common_elements.size() > 1 )
{
for( const auto common_element_id : common_elements )
{
if( element_id == common_element_id )
{
continue;
}
to_delete[common_element_id] = true;
}
}
}
delete_elements( to_delete, builder );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return the mapping between input mesh and output mesh ?

}
} // namespace

namespace geode
{
template < index_t dimension >
void remove_edge_duplication( const EdgedCurve< dimension >& mesh,
EdgedCurveBuilder< dimension >& builder )
{
::remove_element_duplication( mesh, builder );
}

template < index_t dimension >
void remove_polygon_duplication( const SurfaceMesh< dimension >& mesh,
SurfaceMeshBuilder< dimension >& builder )
{
::remove_element_duplication( mesh, builder );
}

void remove_polyhedron_duplication(
const SolidMesh3D& mesh, SolidMeshBuilder3D& builder )
{
::remove_element_duplication( mesh, builder );
}

template void opengeode_mesh_api remove_edge_duplication(
const EdgedCurve2D&, EdgedCurveBuilder2D& );
template void opengeode_mesh_api remove_edge_duplication(
const EdgedCurve3D&, EdgedCurveBuilder3D& );

template void opengeode_mesh_api remove_polygon_duplication(
const SurfaceMesh2D&, SurfaceMeshBuilder2D& );
template void opengeode_mesh_api remove_polygon_duplication(
const SurfaceMesh3D&, SurfaceMeshBuilder3D& );

} // namespace geode
10 changes: 10 additions & 0 deletions src/geode/mesh/helpers/repair_polygon_orientations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ namespace
== reorient_polygon_[adj->polygon_id]
: cur_polygon_reorient
!= reorient_polygon_[adj->polygon_id];
if( !is_valid )
{
DEBUG( "Mobius strip detected" );
DEBUG( cur_polygon );
DEBUG( e );
SDEBUG( adj.value() );
SDEBUG( mesh_.polygon_barycenter( cur_polygon ) );
SDEBUG(
mesh_.polygon_barycenter( adj->polygon_id ) );
Comment on lines +102 to +108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger::debug ?

}
OPENGEODE_DATA_EXCEPTION( is_valid,
"[RepairPolygonOrientations] Mobius "
"strip detected, polygons orientations "
Expand Down