forked from james-yoo/DBSCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbscan.cpp
92 lines (81 loc) · 2.81 KB
/
dbscan.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
#include "dbscan.h"
int DBSCAN::run()
{
int clusterID = 1;
vector<Point>::iterator iter;
for(iter = m_points.begin(); iter != m_points.end(); ++iter)
{
if ( iter->clusterID == UNCLASSIFIED )
{
if ( expandCluster(*iter, clusterID) != FAILURE )
{
clusterID += 1;
}
}
}
return 0;
}
int DBSCAN::expandCluster(Point point, int clusterID)
{
vector<int> clusterSeeds = calculateCluster(point);
if ( clusterSeeds.size() < m_minPoints )
{
point.clusterID = NOISE;
return FAILURE;
}
else
{
int index = 0, indexCorePoint = 0;
vector<int>::iterator iterSeeds;
for( iterSeeds = clusterSeeds.begin(); iterSeeds != clusterSeeds.end(); ++iterSeeds)
{
m_points.at(*iterSeeds).clusterID = clusterID;
if (m_points.at(*iterSeeds).x == point.x && m_points.at(*iterSeeds).y == point.y && m_points.at(*iterSeeds).z == point.z )
{
indexCorePoint = index;
}
++index;
}
clusterSeeds.erase(clusterSeeds.begin()+indexCorePoint);
for( vector<int>::size_type i = 0, n = clusterSeeds.size(); i < n; ++i )
{
vector<int> clusterNeighors = calculateCluster(m_points.at(clusterSeeds[i]));
if ( clusterNeighors.size() >= m_minPoints )
{
vector<int>::iterator iterNeighors;
for ( iterNeighors = clusterNeighors.begin(); iterNeighors != clusterNeighors.end(); ++iterNeighors )
{
if ( m_points.at(*iterNeighors).clusterID == UNCLASSIFIED || m_points.at(*iterNeighors).clusterID == NOISE )
{
if ( m_points.at(*iterNeighors).clusterID == UNCLASSIFIED )
{
clusterSeeds.push_back(*iterNeighors);
n = clusterSeeds.size();
}
m_points.at(*iterNeighors).clusterID = clusterID;
}
}
}
}
return SUCCESS;
}
}
vector<int> DBSCAN::calculateCluster(Point point)
{
int index = 0;
vector<Point>::iterator iter;
vector<int> clusterIndex;
for( iter = m_points.begin(); iter != m_points.end(); ++iter)
{
if ( calculateDistance(point, *iter) <= m_epsilon )
{
clusterIndex.push_back(index);
}
index++;
}
return clusterIndex;
}
inline double DBSCAN::calculateDistance(const Point& pointCore, const Point& pointTarget )
{
return pow(pointCore.x - pointTarget.x,2)+pow(pointCore.y - pointTarget.y,2)+pow(pointCore.z - pointTarget.z,2);
}