-
Notifications
You must be signed in to change notification settings - Fork 16
/
demo.cpp
158 lines (139 loc) · 3.56 KB
/
demo.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// Demonstration program for hierarchical clustering
// with fastcluster by Daniel Muellner
//
// Line segments are clustered in two directions
//
// Author: Christoph Dalitz, 2018
//
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include "fastcluster.h"
// 2D point or vector
class Point {
public:
double x;
double y;
Point(double xx=0.0, double yy=0.0) { x=xx; y=yy; }
Point(const Point& p) { x=p.x; y=p.y; }
double norm() { return(sqrt(x*x + y*y)); }
};
// line segment
class Segment {
public:
Point p1;
Point p2;
Point dir;
Segment(const Point p, const Point q) { p1=p, p2=q; dir=direction(); }
Segment(const Segment& s) { p1=s.p1, p2=s.p2; dir=direction(); }
private:
Point direction() {
Point d(p2.x-p1.x, p2.y-p1.y);
double n = d.norm();
if (n > 0.0) {
d.x /= n; d.y /= n;
}
return d;
}
};
// line segment distance (cosine dissimilarity)
double distance(const Segment& s1, const Segment& s2) {
double sprod = s1.dir.x*s2.dir.x + s1.dir.y*s2.dir.y;
double d = 1 - sprod*sprod;
if (d < 0.0)
return 0;
else
return d;
}
// main program
int main(int argc, char** argv)
{
int i,j,k,npoints;
// parse command line
std::string opt_infile;
int opt_method = HCLUST_METHOD_SINGLE;
const char* usagemsg = "Usage: hclust-demo <infile> [-m (single|complete|average|median)]\n";
for (i=1; i<argc; i++) {
if (0 == strcmp(argv[i], "-m")) {
i++;
if (i<argc) {
if (0 == strcmp(argv[i], "single"))
opt_method = HCLUST_METHOD_SINGLE;
else if (0 == strcmp(argv[i], "complete"))
opt_method = HCLUST_METHOD_COMPLETE;
else if (0 == strcmp(argv[i], "average"))
opt_method = HCLUST_METHOD_AVERAGE;
else if (0 == strcmp(argv[i], "median"))
opt_method = HCLUST_METHOD_MEDIAN;
else {
fputs(usagemsg, stderr);
return 1;
}
} else {
fputs(usagemsg, stderr);
return 1;
}
}
else if (argv[i][0] == '-') {
fputs(usagemsg, stderr);
return 1;
}
else {
opt_infile = argv[i];
}
}
if (opt_infile == "") {
fputs(usagemsg, stderr);
return 1;
}
// read line segments from input file
std::vector<Segment> segs;
double x1,x2,y1,y2;
FILE* f = fopen(opt_infile.c_str(), "r");
if (!f) {
fprintf(stderr, "Cannot open '%s'\n", opt_infile.c_str());
return 2;
}
npoints = 0;
while (!feof(f)) {
npoints++;
k = fscanf(f, "%lf,%lf,%lf,%lf\n", &x1, &x2, &y1, &y2);
if (k != 4) {
fprintf(stderr, "Error in line %i of '%s': wrong format\n", npoints, argv[1]);
return 3;
}
segs.push_back(Segment(Point(x1,x2), Point(y1,y2)));
}
fclose(f);
// computation of condensed distance matrix
double* distmat = new double[(npoints*(npoints-1))/2];
k = 0;
for (i=0; i<npoints; i++) {
for (j=i+1; j<npoints; j++) {
distmat[k] = distance(segs[i], segs[j]);
k++;
}
}
// clustering call
int* merge = new int[2*(npoints-1)];
double* height = new double[npoints-1];
hclust_fast(npoints, distmat, opt_method, merge, height);
int* labels = new int[npoints];
cutree_k(npoints, merge, 2, labels);
//cutree_cdist(npoints, merge, height, 0.5, labels);
// print result
for (i=0; i<npoints; i++) {
printf("%3.2f,%3.2f,%3.2f,%3.2f,%i\n",
segs[i].p1.x, segs[i].p1.y, segs[i].p2.x, segs[i].p2.y, labels[i]);
}
// clean up
delete[] distmat;
delete[] merge;
delete[] height;
delete[] labels;
return 0;
}