From d82ca437c452d93b200b2347732a7d46db83fec0 Mon Sep 17 00:00:00 2001 From: Dmitry Anisimov Date: Fri, 20 Aug 2021 11:19:22 +0200 Subject: [PATCH] correctly handling wrong values in new classes --- .../Least_squares_circle_fit_region.h | 17 ++++++++++++++--- .../Least_squares_cylinder_fit_region.h | 14 +++++++++++--- .../Least_squares_sphere_fit_region.h | 17 ++++++++++++++--- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_circle_fit_region.h b/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_circle_fit_region.h index 246b168b87a..26332dae622 100644 --- a/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_circle_fit_region.h +++ b/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_circle_fit_region.h @@ -16,6 +16,7 @@ #include +#include #include #include @@ -218,6 +219,10 @@ public: if (indices.size() < 6) return true; + // TODO: Why do we get so many nan in this class? + if (std::isnan(m_radius)) + return false; + // If radius is out of bound, nothing fits, early ending if (m_radius < m_min_radius || m_radius > m_max_radius) return false; @@ -226,16 +231,22 @@ public: const Point_2& query_point = get(m_point_map, key); Vector_2 normal = get(m_normal_map, key); - FT distance_to_center = m_sqrt(m_squared_distance_2 (query_point, m_center)); + const FT sq_dist = m_squared_distance_2(query_point, m_center); + if (std::isnan(sq_dist)) return false; + FT distance_to_center = m_sqrt (sq_dist); FT distance_to_circle = CGAL::abs (distance_to_center - m_radius); if (distance_to_circle > m_distance_threshold) return false; - normal = normal / m_sqrt (normal * normal); + const FT sq_norm = normal * normal; + if (std::isnan(sq_norm)) return false; + normal = normal / m_sqrt (sq_norm); Vector_2 ray (m_center, query_point); - ray = ray / m_sqrt (ray * ray); + const FT sq_ray = ray * ray; + if (std::isnan(sq_ray)) return false; + ray = ray / m_sqrt (sq_ray); if (CGAL::abs (normal * ray) < m_normal_threshold) return false; diff --git a/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_cylinder_fit_region.h b/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_cylinder_fit_region.h index 9bd2102b181..26cc8416c14 100644 --- a/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_cylinder_fit_region.h +++ b/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_cylinder_fit_region.h @@ -16,6 +16,7 @@ #include +#include #include #include @@ -218,6 +219,7 @@ public: if (indices.size() < 6) return true; + // TODO: Why do we get so many nan in this class? if (std::isnan(m_radius)) return false; @@ -232,17 +234,23 @@ public: // TODO: Why do we have m_axis = 0 here sometimes? // Should it ever happen? if (m_axis.to_vector() == Vector_3(0, 0, 0)) return false; - FT distance_to_center = m_sqrt(m_squared_distance_3 (query_point, m_axis)); + const FT sq_dist = m_squared_distance_3(query_point, m_axis); + if (std::isnan(sq_dist)) return false; + FT distance_to_center = m_sqrt (sq_dist); FT distance_to_cylinder = CGAL::abs (distance_to_center - m_radius); if (distance_to_cylinder > m_distance_threshold) return false; - normal = normal / m_sqrt (normal * normal); + const FT sq_norm = normal * normal; + if (std::isnan(sq_norm)) return false; + normal = normal / m_sqrt (sq_norm); Point_3 proj = m_axis.projection(query_point); Vector_3 ray (proj, query_point); - ray = ray / m_sqrt (ray * ray); + const FT sq_ray = ray * ray; + if (std::isnan(sq_ray)) return false; + ray = ray / m_sqrt (sq_ray); if (CGAL::abs (normal * ray) < m_normal_threshold) return false; diff --git a/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_sphere_fit_region.h b/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_sphere_fit_region.h index 568f20ad5b6..90cb7ce5da9 100644 --- a/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_sphere_fit_region.h +++ b/Shape_detection/include/CGAL/Shape_detection/Region_growing/Region_growing_on_point_set/Least_squares_sphere_fit_region.h @@ -16,6 +16,7 @@ #include +#include #include #include @@ -224,6 +225,10 @@ public: if (indices.size() < 6) return true; + // TODO: Why do we get so many nan in this class? + if (std::isnan(m_radius)) + return false; + // If radius is out of bound, nothing fits, early ending if (m_radius < m_min_radius || m_radius > m_max_radius) return false; @@ -232,16 +237,22 @@ public: const Point_3& query_point = get(m_point_map, key); Vector_3 normal = get(m_normal_map, key); - FT distance_to_center = m_sqrt(m_squared_distance_3 (query_point, m_center)); + const FT sq_dist = m_squared_distance_3(query_point, m_center); + if (std::isnan(sq_dist)) return false; + FT distance_to_center = m_sqrt (sq_dist); FT distance_to_sphere = CGAL::abs (distance_to_center - m_radius); if (distance_to_sphere > m_distance_threshold) return false; - normal = normal / m_sqrt (normal * normal); + const FT sq_norm = normal * normal; + if (std::isnan(sq_norm)) return false; + normal = normal / m_sqrt (sq_norm); Vector_3 ray (m_center, query_point); - ray = ray / m_sqrt (ray * ray); + const FT sq_ray = ray * ray; + if (std::isnan(sq_ray)) return false; + ray = ray / m_sqrt (sq_ray); if (CGAL::abs (normal * ray) < m_normal_threshold) return false;