Skip to content

Commit

Permalink
WIP: Add script for query creation and Compute_classical_Frechet_dist…
Browse files Browse the repository at this point in the history
…ance_100.cpp
  • Loading branch information
André Nusser committed Dec 12, 2024
1 parent 00c7054 commit 21e3a7c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
0.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
0.txt
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <CGAL/Frechet_distance/internal/Frechet_classical.h>
#include <CGAL/Frechet_distance_traits_d.h>
#include <CGAL/Cartesian_d.h>
#include <CGAL/FPU.h>

#include <iostream>
#include <sstream>
#include <fstream>

using Kernel = CGAL::Cartesian_d<double>;
using Traits = CGAL::Frechet_distance_traits_d<Kernel>;
using Point = Traits::Point_d;
using Points = std::vector<Point>;
using Curve = CGAL::Frechet_distance_::internal::Curve<Traits>;

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<std::streamsize>::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<true> 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<Curve> 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;
}
4 changes: 2 additions & 2 deletions Frechet_distance/test/Frechet_distance/data/scripts/TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 21e3a7c

Please sign in to comment.