-
Notifications
You must be signed in to change notification settings - Fork 1
/
DBSCANCPU.hpp
146 lines (117 loc) · 3.75 KB
/
DBSCANCPU.hpp
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
#pragma once
#include <map>
#include <sstream>
#include <vector>
#include <cmath>
class DBSCANCPU
{
public:
std::vector<Point2>& data;
double eps;
int mnpts;
int pointCount;
std::vector<std::vector<int>> clusterMatrix;
std::vector<int> valid;
std::vector<int> reroute;
std::map<int, int> labels;
std::map<int, std::vector<int>> clusters;
DBSCANCPU(std::vector<Point2>& _data,double _eps,int _mnpts):data(_data), eps(_eps), mnpts(_mnpts)
{
pointCount = _data.size();
clusterMatrix.resize(pointCount);
valid.resize(pointCount);
reroute.resize(pointCount);
for (int i = 0; i < pointCount; i++) {
clusterMatrix[i].resize(pointCount);
valid[i] = 0;
reroute[i] = -1;
labels[i]=-99;
}
}
int run()
{
for(int tid = 0 ; tid < pointCount; tid++) {
for (int i = 0; i < pointCount; i++) {
// Same elem is always 1
if (i == tid) {
set(i, tid);
continue;
}
// Compute the distance
double dist = computeDistance(i, tid);
if (dist <= eps)
set(i, tid);
}
}
// Now merge the clusters
for(int i = 0; i < pointCount; i++)
while (merge(i)); // Means merge everybody into this one
// Now set the labels!
int runningClusterIndex = 0;
for(int col = 0 ; col < pointCount ; col++) {
if (!valid[col])
continue;
std::vector<int> assignedIndices;
for (int row = 0 ; row < pointCount ; row++)
if (clusterMatrix[row][col])
assignedIndices.push_back(row);
if (assignedIndices.size() > mnpts) {
clusters[runningClusterIndex++] = assignedIndices;
}
}
for(auto &c : clusters) {
for(auto &index : c.second) {
labels[index] = c.first;
}
}
return clusters.size();
}
bool merge(int clusterIndex)
{
int correctedIndex = clusterIndex;
if (!valid[clusterIndex] && reroute[clusterIndex] >= 0)
correctedIndex = reroute[clusterIndex];
else if (!valid[clusterIndex])
throw std::runtime_error("Invalid index!");
return mergeInto(correctedIndex);
}
bool mergeInto(int clusterIndex)
{
bool thisClusterWasMerged = false;
for(int tid = 0 ; tid < pointCount ; tid++) {
if (tid == clusterIndex)
continue;
if (valid[tid])
for (int i = 0 ; i < pointCount ; i++) {
// Are both one?
if (clusterMatrix[i][tid] && clusterMatrix[i][clusterIndex]) {
copyClusterAndInvalidate(tid, clusterIndex);
thisClusterWasMerged = true;
break; // no need to go any further!
}
}
}
return thisClusterWasMerged;
}
void copyClusterAndInvalidate(int from, int to) {
for(int i = 0 ; i < pointCount ; i++) {
clusterMatrix[i][to] |= clusterMatrix[i][from];
clusterMatrix[i][from] = 0; // so that we know this was de-assigned
}
valid[from] = false;
reroute[from] = to;
}
void set(int i, int tid)
{
clusterMatrix[i][tid] = 1;
valid[tid] = 1;
}
double computeDistance(int ai,int bi)
{
Point2 a = data[ai];
Point2 b = data[bi];
float deltaX = a.x - b.x;
float deltaY = a.y - b.y;
return std::sqrt(deltaX * deltaX + deltaY * deltaY);
}
};