-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add script for query creation and Compute_classical_Frechet_dist…
…ance_100.cpp
- Loading branch information
André Nusser
committed
Dec 12, 2024
1 parent
00c7054
commit 21e3a7c
Showing
6 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
Frechet_distance/test/Frechet_distance/data/curves/generated_100d/dataset.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
10 changes: 10 additions & 0 deletions
10
Frechet_distance/test/Frechet_distance/data/curves/generated_3d/dataset.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...et_distance/test/Frechet_distance/data/scripts/Compute_classical_Frechet_distance_100.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 51 additions & 1 deletion
52
Frechet_distance/test/Frechet_distance/data/scripts/create_queries.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |