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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 | /**
* @file CanopyClustering.h
*
* @author <a href="mailto:holzhaue@informatik.hu-berlin.de">Florian Holzhauer</a>
* @author <a href="mailto:mellmann@informatik.hu-berlin.de">Heinrich Mellmann</a>
* Declaration of class CanopyClustering
*/
#ifndef _CanopyClustering_h_
#define _CanopyClustering_h_
#include "SampleSet.h"
// debug
#include "Tools/Debug/DebugRequest.h"
#include "Tools/Debug/DebugDrawings.h"
#include <vector>
template<class C>
class CanopyClustering
{
public:
CanopyClustering(double clusterThreshold = 0, int maxNumberOfClusters = 100)
:
numOfClusters(0),
largestCluster(-1),
clusters(maxNumberOfClusters),
clusterThreshold(clusterThreshold)
{
}
~CanopyClustering() {}
class CanopyCluster
{
protected:
unsigned int _size;
Vector2d _clusterSum;
Vector2d _center;
void add(const Vector2d& point)
{
_size++;
_clusterSum += point;
_center = _clusterSum / static_cast<double>(_size);
}
void set(const Vector2d& point)
{
_size = 1;
_clusterSum = point;
_center = point;
}
public:
CanopyCluster() : _size(0){}
virtual ~CanopyCluster(){}
unsigned int size() const { return _size; }
const Vector2d& clusterSum() const { return _clusterSum; }
const Vector2d& center() const { return _center; }
};//end class CanopyCluster
unsigned int size() const { return numOfClusters; }
const CanopyCluster& operator[](int index) const { ASSERT(index >= 0 && (unsigned int)index < this->numOfClusters); return clusters[index];}
const CanopyCluster& getLargestCluster() const { ASSERT(largestCluster >= 0 && (unsigned int)largestCluster < this->numOfClusters); return (*this).clusters[largestCluster]; }
int getLargestClusterID() const {return largestCluster;}
void setClusterThreshold(const double clusterThreshold) {this->clusterThreshold = clusterThreshold;}
void cluster(C& sampleSet)
{
numOfClusters = 0;
largestCluster = -1;
for (typename C::size_type j = 0; j < sampleSet.size(); j++)
{
Sample2D& sample = sampleSet[j];
sample.cluster = -1; // no cluster
// look for a cluster with the smallest distance
double minDistance = 1e+100;
int minIdx = -1;
for (unsigned int k = 0; k < numOfClusters; k++) {
double dist = clusters[k].distance(sample.getPos());
if(dist < minDistance)
{
minIdx = (int)k;
minDistance = dist;
}
}//end for
// try to add to the nearest cluster
if(minIdx != -1 && isInCluster(clusters[minIdx], sample))
{
sample.cluster = minIdx;
clusters[minIdx].add(sample.getPos());
if(clusters[minIdx].size() > clusters[largestCluster].size()) {
largestCluster = minIdx;
}
}
// othervise create new cluster
else if(numOfClusters < clusters.size()) // ACHTUNG: don't resize clusters
{
// initialize a new cluster
clusters[numOfClusters].set(sample.getPos());
sample.cluster = (int)numOfClusters;
if(largestCluster == -1) {
largestCluster = numOfClusters;
}
numOfClusters++;
}//end if
}//end for j
// merge close clusters
// note: don't consider clusters smaller than minClusterSize
const int minClusterSize = 4;
for(unsigned int k = 0; k < numOfClusters; k++)
{
if((int)clusters[k].size() < minClusterSize) {
continue;
}
for(unsigned int j = k+1; j < numOfClusters; j++)
{
if ( (int)clusters[j].size() < minClusterSize) {
continue;
}
// merge the clusters k and j
if((clusters[k].center() - clusters[j].center()).abs() < 500)
{
clusters[k].merge(clusters[j]);
clusters[j].clear();
if(clusters[k].size() > clusters[largestCluster].size())
largestCluster = (int)k;
// TODO: make it more effivient
for (typename C::size_type i = 0; i < sampleSet.size(); i++)
{
if(sampleSet[i].cluster == (int)j) {
sampleSet[i].cluster = (int)k;
}
} //end for i
} //end if abs < 500
} // end for j
} //end for k
}//end cluster
unsigned int cluster(C& sampleSet, const Vector2d& start)
{
numOfClusters = 1;
largestCluster = 0;
CanopyClusterBuilder& cluster = clusters[0];
cluster.set(start);
for (typename C::size_type j = 0; j < sampleSet.size(); j++)
{
sampleSet[j].cluster = -1;
if(isInCluster(cluster, sampleSet[j]))
{
sampleSet[j].cluster = 0;
cluster.add(sampleSet[j].getPos());
}
}
return cluster.size();
}//end cluster
private:
class CanopyClusterBuilder: public CanopyCluster
{
public:
virtual ~CanopyClusterBuilder(){}
CanopyClusterBuilder(){}
CanopyClusterBuilder(const Vector2d& point) {<--- Class 'CanopyClusterBuilder' has a constructor with 1 argument that is not explicit. [+]Class 'CanopyClusterBuilder' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
set(point);
}
void add(const Vector2d& point) {
CanopyCluster::add(point);
}
void set(const Vector2d& point) {
CanopyCluster::set(point);
}
void merge(const CanopyCluster& other) {
this->_size += other.size();
this->_clusterSum = (this->_clusterSum + other.clusterSum()) * 0.5;
this->_center = (this->_center + other.center()) * 0.5;
}
void clear() {
this->_size = 0;
}
// TODO: make it switchable
double distance(const Vector2d& point) const {
return (CanopyCluster::center() - point).abs2();
}
};//end class CanopyClusterBuilder
bool isInCluster(const CanopyClusterBuilder& cluster, const Sample2D& sample) const {
return cluster.distance(sample.getPos()) < clusterThreshold*clusterThreshold;
}
// results of the clustering
unsigned int numOfClusters;
int largestCluster;
std::vector<CanopyClusterBuilder> clusters; //FIXME
// parameter of clustering
double clusterThreshold;
};
#endif //_CanopyClustering_h_
|