diff --git a/include/geode/mesh/helpers/mesh_statistics.hpp b/include/geode/mesh/helpers/mesh_statistics.hpp new file mode 100644 index 000000000..9d68edbf9 --- /dev/null +++ b/include/geode/mesh/helpers/mesh_statistics.hpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 - 2026 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 + +namespace geode +{ + FORWARD_DECLARATION_DIMENSION_CLASS( SurfaceMesh ); + ALIAS_2D_AND_3D( SurfaceMesh ); +} // namespace geode + +namespace geode +{ + struct MeshStatistics + { + double min_edge_size{ std::numeric_limits< double >::max() }; + double mean_edge_size{ 0. }; + double max_edge_size{ std::numeric_limits< double >::min() }; + }; + + template < index_t dimension > + [[nodiscard]] MeshStatistics compute_surface_metrics( + const SurfaceMesh< dimension >& mesh ); +} // namespace geode diff --git a/src/geode/mesh/CMakeLists.txt b/src/geode/mesh/CMakeLists.txt index 6567f01e6..8b7b1d9db 100644 --- a/src/geode/mesh/CMakeLists.txt +++ b/src/geode/mesh/CMakeLists.txt @@ -110,6 +110,7 @@ add_geode_library( "helpers/euclidean_distance_transform.cpp" "helpers/gradient_computation.cpp" "helpers/hausdorff_distance.cpp" + "helpers/mesh_statistics.cpp" "helpers/rasterize.cpp" "helpers/ray_tracing.cpp" "helpers/grid_point_function.cpp" @@ -252,6 +253,7 @@ add_geode_library( "helpers/generic_edged_curve_accessor.hpp" "helpers/hausdorff_distance.hpp" "helpers/nnsearch_mesh.hpp" + "helpers/mesh_statistics.hpp" "helpers/rasterize.hpp" "helpers/ray_tracing.hpp" "helpers/grid_point_function.hpp" diff --git a/src/geode/mesh/helpers/mesh_statistics.cpp b/src/geode/mesh/helpers/mesh_statistics.cpp new file mode 100644 index 000000000..e466595c6 --- /dev/null +++ b/src/geode/mesh/helpers/mesh_statistics.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2019 - 2026 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 + +#include + +#include + +namespace +{ +} // namespace + +namespace geode +{ + template < index_t dimension > + [[nodiscard]] MeshStatistics compute_surface_metrics( + const SurfaceMesh< dimension >& mesh ) + { + MeshStatistics result; + index_t nb_edges{ 0 }; + for( const auto polygon_id : Range{ mesh.nb_polygons() } ) + { + for( const auto edge_id : + LRange{ mesh.nb_polygon_edges( polygon_id ) } ) + { + const PolygonEdge polygon_edge{ polygon_id, edge_id }; + if( const auto adjacent = + mesh.polygon_adjacent( polygon_edge ) ) + { + if( adjacent.value() < polygon_id ) + { + continue; + } + } + const auto length = mesh.edge_length( polygon_edge ); + result.max_edge_size = std::max( result.max_edge_size, length ); + result.min_edge_size = std::min( result.min_edge_size, length ); + result.mean_edge_size += length; + nb_edges++; + } + } + if( nb_edges != 0 ) + { + result.mean_edge_size /= nb_edges; + } + return result; + } + + template MeshStatistics compute_surface_metrics< 2 >( + const SurfaceMesh< 2 >& ); + template MeshStatistics compute_surface_metrics< 3 >( + const SurfaceMesh< 3 >& ); +} // namespace geode