From 21e3a7cc94e4c4d88df4169f2fb80ec9b6841b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Thu, 12 Dec 2024 12:34:08 +0100 Subject: [PATCH] WIP: Add script for query creation and Compute_classical_Frechet_distance_100.cpp --- .../data/curves/generated_100d/dataset.txt | 10 ++++ .../data/curves/generated_3d/dataset.txt | 10 ++++ .../data/scripts/CMakeLists.txt | 2 +- ...Compute_classical_Frechet_distance_100.cpp | 59 +++++++++++++++++++ .../test/Frechet_distance/data/scripts/TODO | 4 +- .../data/scripts/create_queries.py | 52 +++++++++++++++- 6 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 Frechet_distance/test/Frechet_distance/data/curves/generated_100d/dataset.txt create mode 100644 Frechet_distance/test/Frechet_distance/data/curves/generated_3d/dataset.txt create mode 100644 Frechet_distance/test/Frechet_distance/data/scripts/Compute_classical_Frechet_distance_100.cpp diff --git a/Frechet_distance/test/Frechet_distance/data/curves/generated_100d/dataset.txt b/Frechet_distance/test/Frechet_distance/data/curves/generated_100d/dataset.txt new file mode 100644 index 000000000000..59d0e29e0b27 --- /dev/null +++ b/Frechet_distance/test/Frechet_distance/data/curves/generated_100d/dataset.txt @@ -0,0 +1,10 @@ +0.txt +1.txt +2.txt +3.txt +4.txt +5.txt +6.txt +7.txt +8.txt +9.txt diff --git a/Frechet_distance/test/Frechet_distance/data/curves/generated_3d/dataset.txt b/Frechet_distance/test/Frechet_distance/data/curves/generated_3d/dataset.txt new file mode 100644 index 000000000000..59d0e29e0b27 --- /dev/null +++ b/Frechet_distance/test/Frechet_distance/data/curves/generated_3d/dataset.txt @@ -0,0 +1,10 @@ +0.txt +1.txt +2.txt +3.txt +4.txt +5.txt +6.txt +7.txt +8.txt +9.txt diff --git a/Frechet_distance/test/Frechet_distance/data/scripts/CMakeLists.txt b/Frechet_distance/test/Frechet_distance/data/scripts/CMakeLists.txt index 647a50aa9d15..001bdfa50824 100644 --- a/Frechet_distance/test/Frechet_distance/data/scripts/CMakeLists.txt +++ b/Frechet_distance/test/Frechet_distance/data/scripts/CMakeLists.txt @@ -7,4 +7,4 @@ project( classical_Frechet_distance ) find_package(CGAL REQUIRED) create_single_source_cgal_program("Compute_classical_Frechet_distance_3.cpp") - +create_single_source_cgal_program("Compute_classical_Frechet_distance_100.cpp") diff --git a/Frechet_distance/test/Frechet_distance/data/scripts/Compute_classical_Frechet_distance_100.cpp b/Frechet_distance/test/Frechet_distance/data/scripts/Compute_classical_Frechet_distance_100.cpp new file mode 100644 index 000000000000..4197bac8fdc5 --- /dev/null +++ b/Frechet_distance/test/Frechet_distance/data/scripts/Compute_classical_Frechet_distance_100.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include +#include +#include + +using Kernel = CGAL::Cartesian_d; +using Traits = CGAL::Frechet_distance_traits_d; +using Point = Traits::Point_d; +using Points = std::vector; +using Curve = CGAL::Frechet_distance_::internal::Curve; + +void readCurve(std::ifstream& curve_file, Points& points) +{ + // Read everything into a stringstream. + std::stringstream ss; + ss << curve_file.rdbuf(); + CGAL::set_ascii_mode(ss); + + Point p; + auto ignore_count = std::numeric_limits::max(); + while (ss >> p) { + ss.ignore(ignore_count, '\n'); + + if ((!points.empty()) && (p == points.back())) { + continue; + } + points.push_back(p); + } +} + +int main(int argc, char* argv[]) +{ + // TODO AF: why do we need this here? + CGAL::Protect_FPU_rounding p; + + double epsilon = 10e-10; + + assert(argc == 3); + std::ifstream file1(argv[1]); + std::ifstream file2(argv[2]); + if (!file1 || !file2) { + std::cout << "One of the input files does not exist." << std::endl; + return 1; + } + + Points points1, points2; + readCurve(file1, points1); + readCurve(file2, points2); + + CGAL::Frechet_distance_::internal::FrechetClassical frechet_classical; + auto dist = frechet_classical.calcDistance(Curve(points1), Curve(points2), epsilon); + + std::cout.precision(17); + std::cout << (dist.second + dist.first)/2. << std::endl; +} diff --git a/Frechet_distance/test/Frechet_distance/data/scripts/TODO b/Frechet_distance/test/Frechet_distance/data/scripts/TODO index 3a5f83d92519..cf39381fbec3 100644 --- a/Frechet_distance/test/Frechet_distance/data/scripts/TODO +++ b/Frechet_distance/test/Frechet_distance/data/scripts/TODO @@ -2,11 +2,11 @@ x clean up Compute_naive_Frechet_distance_3.cpp x move Frechet_naive into internal directory and adapt everything for it to compile x create small set of 3d test curves x create small set of 100d test curves -- create 3d queries +x create 3d queries - add them to the test suite - run tests -- copy Compute_naive_Frechet_distance_3.cpp to Compute_naive_Frechet_distance_100.cpp +x copy Compute_naive_Frechet_distance_3.cpp to Compute_naive_Frechet_distance_100.cpp - then do the same as for 3d tests - also use compute distance in the tests if it wasn't done yet diff --git a/Frechet_distance/test/Frechet_distance/data/scripts/create_queries.py b/Frechet_distance/test/Frechet_distance/data/scripts/create_queries.py index 808695404125..3db80277cd0d 100644 --- a/Frechet_distance/test/Frechet_distance/data/scripts/create_queries.py +++ b/Frechet_distance/test/Frechet_distance/data/scripts/create_queries.py @@ -1 +1,51 @@ -# TODO: create files like in queries directory using naive Fréchet implementation +# +# Small helper script to create query files using the classcial Fréchet algorithm +# + +import subprocess + +dimensions = [3, 100] +eps = 10e-10 # note that this is just set the same as in Compute_classical_Frechet_distance_3.cpp + +def read_curve_filenames(d): + # read file names from dataset + file = open(f'../curves/generated_{d}d/dataset.txt') + + # read all curves + filenames = [] + for line in file: + filenames.append(f'../curves/generated_{d}d/{line.strip()}') + + return filenames + +def write_queries(queries, d): + # open file + file = open(f'../queries/generated_{d}d.txt', 'w') + + # write queries + for q in queries: + # larger query + file.write(f'{q[0]} {q[1]} {q[2]+2*eps} 1\n') + # smaller query + file.write(f'{q[1]} {q[0]} {q[2]-2*eps} 0\n') + +def frechet_distance(curve1, curve2): + args = ('build/Compute_classical_Frechet_distance_{d}', curve1, curve2) + popen = subprocess.Popen(args, stdout=subprocess.PIPE) + popen.wait() + output = popen.stdout.read() + return float(output) + +for d in dimensions: + # read curves + curve_filenames = read_curve_filenames(d) + + # compute pairwise Fréchet distance + queries = [] + for i in range(len(curve_filenames)): + for j in range(i+1,len(curve_filenames)): + dist = frechet_distance(curve_filenames[i], curve_filenames[j]) + queries.append((i,j,dist)) + + # write queries to file + write_queries(queries, d)