summaryrefslogtreecommitdiff
path: root/geom_bottleneck/example/bottleneck_dist.cpp
blob: 81d8605d04a9640794dead6476f70d57906d3267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iomanip>
#include "bottleneck.h"

// any container of pairs of doubles can be used,
// we use vector in this example.

typedef std::vector<std::pair<double, double>>  PairVector;

// estimate initial guess on sampled diagram?
constexpr bool useSamplingHeur = false;
// if diagrams contain fewer points, don't use heuristic
constexpr int heurThreshold = 30000;

int main(int argc, char* argv[])
{
    if (argc < 3 ) {
        std::cerr << "Usage: " << argv[0] << " file1 file2 [relative_error]. Without relative_error calculate the exact distance." << std::endl;
        return 1;
    }

    PairVector diagramA, diagramB;
    int decPrecision { 0 };
    if (!geom_bt::readDiagramPointSet(argv[1], diagramA, decPrecision)) {
        std::exit(1);
    }

    if (!geom_bt::readDiagramPointSet(argv[2], diagramB, decPrecision)) {
        std::exit(1);
    }

    double res;
    if (argc >= 4) {
        // the third parameter is epsilon,
        // return approximate distance (faster)
        double approxEpsilon =  atof(argv[3]);
        if (approxEpsilon > 0.0) {
            if (useSamplingHeur && diagramA.size() > heurThreshold && diagramB.size() > heurThreshold) {
#ifdef VERBOSE_BOTTLENECK
                std::cout << "using sampling heuristic" << std::endl;
#endif
                res = geom_bt::bottleneckDistApproxHeur(diagramA, diagramB, approxEpsilon);
            } else {
#ifdef VERBOSE_BOTTLENECK
                std::cout << "NOT using sampling heuristic" << std::endl;
#endif
                res = geom_bt::bottleneckDistApprox(diagramA, diagramB, approxEpsilon);
            }
        } else if (approxEpsilon == 0.0) {
#ifdef VERBOSE_BOTTLENECK
                std::cout << "NOT using sampling heuristic, computing EXACT answer" << std::endl;
#endif
            res = geom_bt::bottleneckDistExact(diagramA, diagramB, decPrecision);
        } else {
            std::cerr << "The third parameter (relative error) must be positive!" << std::endl;
            std::exit(1);
        }
    } else {
        // only filenames have been supplied, return exact distance
#ifdef VERBOSE_BOTTLENECK
        std::cout << "NOT using sampling heuristic, computing EXACT answer" << std::endl;
#endif
        res = geom_bt::bottleneckDistExact(diagramA, diagramB, decPrecision);
    }
    std::cout << std::setprecision(15) << res << std::endl;

    // Alternative could be to construct DiagramPointSet
    // using the constructor with iterators.
    // May be useful if the same diagram is used multiple times
    // to avoid copying data from user's container each time.
    
    //geom_bt::DiagramPointSet dA(diagramA.begin(), diagramA.end());
    //geom_bt::DiagramPointSet dB(diagramB.begin(), diagramB.end());
    //double result1 = geom_bt::bottleneckDistExact(dA, dB);
    //std::cout << std::setprecision(15) << result1 << std::endl;

    return 0;
}