|
| 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 |
0 commit comments