From 0bd09ea2db95f3d5f13eae81844e8a448269a79f Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Wed, 5 Feb 2025 14:59:55 +0000 Subject: [PATCH] WIP: Add Compare_squared_distance to NewKernel_d --- .../examples/Frechet_distance/CMakeLists.txt | 3 ++ .../Frechet_distance/Frechet_DS_d.cpp | 50 +++++++++++++++++++ .../include/CGAL/Frechet_distance_traits_d.h | 5 ++ .../CGAL/NewKernel_d/Kernel_d_interface.h | 2 + .../NewKernel_d/function_objects_cartesian.h | 34 +++++++++++++ .../include/CGAL/NewKernel_d/functor_tags.h | 1 + 6 files changed, 95 insertions(+) create mode 100644 Frechet_distance/examples/Frechet_distance/Frechet_DS_d.cpp diff --git a/Frechet_distance/examples/Frechet_distance/CMakeLists.txt b/Frechet_distance/examples/Frechet_distance/CMakeLists.txt index 5b93527092c..bb812d7a19e 100644 --- a/Frechet_distance/examples/Frechet_distance/CMakeLists.txt +++ b/Frechet_distance/examples/Frechet_distance/CMakeLists.txt @@ -16,6 +16,9 @@ create_single_source_cgal_program( "Frechet_DS_2.cpp" ) create_single_source_cgal_program( "Frechet_DS_3.cpp" ) if(TARGET CGAL::Eigen3_support) +create_single_source_cgal_program( "Frechet_DS_d.cpp" ) +target_link_libraries(Frechet_DS_d PUBLIC CGAL::Eigen3_support) + create_single_source_cgal_program( "Frechet_distance_d.cpp" ) target_link_libraries(Frechet_distance_d PUBLIC CGAL::Eigen3_support) endif() diff --git a/Frechet_distance/examples/Frechet_distance/Frechet_DS_d.cpp b/Frechet_distance/examples/Frechet_distance/Frechet_DS_d.cpp new file mode 100644 index 00000000000..5b89e7f4b3a --- /dev/null +++ b/Frechet_distance/examples/Frechet_distance/Frechet_DS_d.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include + +#include +#include +#include + +using Kernel = CGAL::Epick_d>; +using Traits = CGAL::Frechet_distance_traits_d; +using Point = Traits::Point_d; +using Curve = std::vector; +using Curves = std::vector; + +int main() +{ + Curves curves; + + const std::filesystem::path data{"./data_2d"}; + std::vector filenames; + for (auto const& dir_entry : std::filesystem::directory_iterator{data}) { + filenames.push_back(dir_entry.path().string()); + } + std::sort(filenames.begin(), filenames.end()); + + for (auto const& filename : filenames) { + std::cout << filename << std::endl; + std::ifstream in(filename); + curves.push_back(Curve()); + // CGAL::IO::read_linestring_WKT(in, curves.back()); + } + + // last curve is the query curve + Curve query = curves.back(); + curves.pop_back(); + + CGAL::Frechet_distance::Neighbor_search ds; + ds.insert(curves); + + for(const Curve& c : curves){ + std::pair res = CGAL::bounded_error_Frechet_distance(c, query, 0.000001); + std::cout << "The Frechet distance between the polylines is between " << res.first << " and " << res.second << std::endl; + } + double distance = 16; + std::vector result = ds.get_close_curves(query, distance); + + std::cout << result.size() << " curves at Frechet distance closer than " << distance << std::endl; +} diff --git a/Frechet_distance/include/CGAL/Frechet_distance_traits_d.h b/Frechet_distance/include/CGAL/Frechet_distance_traits_d.h index f21d3393af3..b86cdd64571 100644 --- a/Frechet_distance/include/CGAL/Frechet_distance_traits_d.h +++ b/Frechet_distance/include/CGAL/Frechet_distance_traits_d.h @@ -40,6 +40,7 @@ public: using Construct_bbox_d = typename Kernel::Construct_bbox_d; using Cartesian_const_iterator_d = typename Kernel::Cartesian_const_iterator_d; using Construct_cartesian_const_iterator_d = typename Kernel::Construct_cartesian_const_iterator_d; + using Compare_squared_distance_d = typename Kernel::Compare_squared_distance_d; Construct_bbox_d construct_bbox_d_object() const { return Construct_bbox_d(); @@ -48,6 +49,10 @@ public: Construct_cartesian_const_iterator_d construct_cartesian_const_iterator_d_object() const { return Construct_cartesian_const_iterator_d(); } + + Compare_squared_distance_d construct_compare_squared_distance_d_object() const { + return Compare_squared_distance_d(); + } }; } // end of namespace CGAL diff --git a/NewKernel_d/include/CGAL/NewKernel_d/Kernel_d_interface.h b/NewKernel_d/include/CGAL/NewKernel_d/Kernel_d_interface.h index fd8582a5b33..4a6ed60f7c3 100644 --- a/NewKernel_d/include/CGAL/NewKernel_d/Kernel_d_interface.h +++ b/NewKernel_d/include/CGAL/NewKernel_d/Kernel_d_interface.h @@ -48,6 +48,7 @@ template struct Kernel_d_interface : public Base_ { typedef typename Get_type::type Weighted_point_d; typedef typename Get_functor::type Compute_coordinate_d; typedef typename Get_functor::type Compare_lexicographically_d; + typedef typename Get_functor::type Compare_squared_distance_d; typedef typename Get_functor::type Equal_d; typedef typename Get_functor::type Less_lexicographically_d; typedef typename Get_functor::type Less_or_equal_lexicographically_d; @@ -230,6 +231,7 @@ template struct Kernel_d_interface : public Base_ { Compute_coordinate_d compute_coordinate_d_object()const{ return Compute_coordinate_d(*this); } Has_on_positive_side_d has_on_positive_side_d_object()const{ return Has_on_positive_side_d(*this); } Compare_lexicographically_d compare_lexicographically_d_object()const{ return Compare_lexicographically_d(*this); } + Compare_squared_distance_d compare_squared_distance_d_object()const{ return Compare_squared_distance_d(*this); } Equal_d equal_d_object()const{ return Equal_d(*this); } Less_lexicographically_d less_lexicographically_d_object()const{ return Less_lexicographically_d(*this); } Less_or_equal_lexicographically_d less_or_equal_lexicographically_d_object()const{ return Less_or_equal_lexicographically_d(*this); } diff --git a/NewKernel_d/include/CGAL/NewKernel_d/function_objects_cartesian.h b/NewKernel_d/include/CGAL/NewKernel_d/function_objects_cartesian.h index 7429d3f879a..523710b8b44 100644 --- a/NewKernel_d/include/CGAL/NewKernel_d/function_objects_cartesian.h +++ b/NewKernel_d/include/CGAL/NewKernel_d/function_objects_cartesian.h @@ -1194,6 +1194,40 @@ template struct Compare_lexicographically : private Store_kernel { CGAL_KD_DEFAULT_FUNCTOR(Compare_lexicographically_tag,(CartesianDKernelFunctors::Compare_lexicographically),(),(Construct_ttag)); +namespace CartesianDKernelFunctors { +template struct Compare_squared_distance : private Store_kernel { + CGAL_FUNCTOR_INIT_STORE(Compare_squared_distance) + typedef R_ R; + typedef typename Get_type::type result_type; + typedef typename Get_functor >::type CI; + // TODO: This is_exact thing should be reengineered. + // the goal is to have a way to tell: don't filter this + typedef typename CGAL::Uses_no_arithmetic Uses_no_arithmetic; + + template + result_type operator()(V const&a, V const&b, W const&c)const{ // Point, Point. FT +#if 0 + CI c(this->kernel()); + + + auto a_begin=c(a,Begin_tag()); + auto b_begin=c(b,Begin_tag()); + auto a_end=c(a,End_tag()); + result_type res; + // can't we do slightly better for Uncertain<*> ? + // after res=...; if(is_uncertain(res))return indeterminate(); + do res=CGAL_NTS compare(*a_begin++,*b_begin++); + while(a_begin!=a_end && res==EQUAL); + return res; +#endif + return EQUAL; + } +}; +} + +CGAL_KD_DEFAULT_FUNCTOR(Compare_squared_distance_tag,(CartesianDKernelFunctors::Compare_squared_distance),(),(Construct_ttag)); + + namespace CartesianDKernelFunctors { template struct Less_lexicographically : private Store_kernel { CGAL_FUNCTOR_INIT_STORE(Less_lexicographically) diff --git a/NewKernel_d/include/CGAL/NewKernel_d/functor_tags.h b/NewKernel_d/include/CGAL/NewKernel_d/functor_tags.h index 210be275766..927b51fcb35 100644 --- a/NewKernel_d/include/CGAL/NewKernel_d/functor_tags.h +++ b/NewKernel_d/include/CGAL/NewKernel_d/functor_tags.h @@ -278,6 +278,7 @@ namespace CGAL { CGAL_DECL_PREDICATE(Compare_point_cartesian_coordinate); CGAL_DECL_PREDICATE(Compare_distance); CGAL_DECL_PREDICATE(Compare_lexicographically); + CGAL_DECL_PREDICATE(Compare_squared_distance); CGAL_DECL_PREDICATE(Less_lexicographically); CGAL_DECL_PREDICATE(Less_or_equal_lexicographically); CGAL_DECL_PREDICATE(Equal_points);