-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquickshiftpp.cpp
439 lines (379 loc) · 9.46 KB
/
quickshiftpp.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#define D_SCL_SECURE_NO_WARNINGS
//#include <pcl/io/pcd_io.h>
//#include <pcl/io/ply_io.h>
//%%%%%%%%%%% Add by Miao, 2022-9-1
//I added some destructors to avoid memory leakage when calling mex files
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include"mex.h"
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <algorithm>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <math.h>
using namespace std;
struct Node {
/*
Node struct for our k-NN or neighborhood Graph
*/
int index;
int rank;
Node * parent;
set <Node * > children;
Node(int idx) {
index = idx;
rank = 0;
parent = NULL;
children.clear();
}
};
struct Graph {
/*
Graph struct.
Allows us to build the graph one node at a time
*/
vector <Node *> nodes;
map <int, Node * > M;
set <Node * > intersecting_sets;
Graph() {
M.clear();
intersecting_sets.clear();
nodes.clear();
}
~Graph() {
for (int i=0;i<nodes.size();i++)
delete nodes[i];
M.clear();
intersecting_sets.clear();
nodes.clear();
}
Node * get_root(Node * node) {
if (node->parent != NULL) {
node->parent->children.erase(node);
node->parent = get_root(node->parent);
node->parent->children.insert(node);
return node->parent;
}
else {
return node;
}
}
void add_node(int idx) {
nodes.push_back(new Node(idx));
M[idx] = nodes[nodes.size() - 1];
}
void add_edge(int n1, int n2) {
Node * r1 = get_root(M[n1]);
Node * r2 = get_root(M[n2]);
if (r1 != r2) {
if (r1->rank > r2->rank) {
r2->parent = r1;
r1->children.insert(r2);
if (intersecting_sets.count(r2)) {
intersecting_sets.erase(r2);
intersecting_sets.insert(r1);
}
}
else {
r1->parent = r2;
r2->children.insert(r1);
if (intersecting_sets.count(r1)) {
intersecting_sets.erase(r1);
intersecting_sets.insert(r2);
}
if (r1->rank == r2->rank) {
r2->rank++;
}
}
}
}
vector <int> get_connected_component(int n) {
Node * r = get_root(M[n]);
vector <int> L;
stack <Node * > s;
s.push(r);
while (!s.empty()) {
Node * top = s.top(); s.pop();
L.push_back(top->index);
for (set<Node * >::iterator it = top->children.begin();
it != top->children.end();
++it) {
s.push(*it);
}
}
return L;
}
bool component_seen(int n) {
Node * r = get_root(M[n]);
if (intersecting_sets.count(r)) {
return true;
}
intersecting_sets.insert(r);
return false;
}
int GET_ROOT(int idx) {
Node * r = get_root(M[idx]);
return r->index;
}
vector <int> GET_CHILDREN(int idx) {
Node * r = M[idx];
vector <int> to_ret;
for (set<Node *>::iterator it = r->children.begin();
it != r->children.end();
++it) {
to_ret.push_back((*it)->index);
}
return to_ret;
}
};
struct NodeBasic {
int index;
int rank;
NodeBasic * parent;
NodeBasic(int idx) {
index = idx;
rank = 0;
parent = NULL;
}
};
struct GraphBasic {
/*
Basic disjoint set data structure. */
vector<NodeBasic *> M;
GraphBasic(const int n) {
M.clear();
for (int i = 0; i < n; ++i) {
M.push_back(new NodeBasic(i));
}
}
NodeBasic * get_root(NodeBasic * node) {
if (!node) return NULL;
if (!node->parent) return node;
node->parent = get_root(node->parent);
return node->parent;
}
void add_edge(const int n1, const int n2) {
NodeBasic * r1 = get_root(M[n1]);
NodeBasic * r2 = get_root(M[n2]);
if (!r1 || !r2) return;
if (r1 != r2) {
if (r1->rank > r2->rank) {
r2->parent = r1;
}
else {
r1->parent = r2;
if (r1->rank == r2->rank) {
r2->rank++;
}
}
}
}
};
void compute_mutual_knn(int n, int k,
int d,
double * radii,
double * neighbors,
double beta,
double epsilon,
int * result) {
/* Given the kNN density and neighbors
We build the k-NN graph / cluster tree and return the estimated modes.
Note that here, we don't require the dimension of the dataset
Returns array of estimated mode membership, where each index cosrresponds
the respective index in the density array. Points without
membership are assigned -1 */
vector<pair <double, int> > knn_radii(n);
vector <set <int> > knn_neighbors(n);
for (int i = 0; i < n; ++i) {
knn_radii[i].first = radii[i];
knn_radii[i].second = i;
for (int j = 0; j < k; ++j) {
knn_neighbors[i].insert(neighbors[i * k + j]);
}
}
int* m_hat = new int[n];
int *cluster_membership = new int[n];
int n_chosen_points = 0;
int n_chosen_clusters = 0;
sort(knn_radii.begin(), knn_radii.end());
Graph G = Graph();
int last_considered = 0;
int last_pruned = 0;
for (int i = 0; i < n; ++i) {
while (last_pruned < n && pow(1. + epsilon, 1. / d) * knn_radii[i].first > knn_radii[last_pruned].first) {
G.add_node(knn_radii[last_pruned].second);
for (set <int>::iterator it = knn_neighbors[knn_radii[last_pruned].second].begin();
it != knn_neighbors[knn_radii[last_pruned].second].end();
++it) {
if (G.M.count(*it)) {
if (knn_neighbors[*it].count(knn_radii[last_pruned].second)) {
G.add_edge(knn_radii[last_pruned].second, *it);
}
}
}
last_pruned++;
}
while (knn_radii[i].first * pow(1. - beta, 1. / d) > knn_radii[last_considered].first) {
if (!G.component_seen(knn_radii[last_considered].second)) {
vector <int> res = G.get_connected_component(knn_radii[last_considered].second);
for (size_t j = 0; j < res.size(); j++) {
if (radii[res[j]] <= knn_radii[i].first) {
cluster_membership[n_chosen_points] = n_chosen_clusters;
m_hat[n_chosen_points++] = res[j];
}
}
n_chosen_clusters++;
}
last_considered++;
}
}
for (int i = 0; i < n; ++i) {
result[i] = -1;
}
for (int i = 0; i < n_chosen_points; ++i) {
result[m_hat[i]] = cluster_membership[i];
}
}
double dist(int i, int j, int d, double ** dataset) {
double sum = 0.;
for (int m = 0; m < d; ++m) {
sum += (dataset[i][m] - dataset[j][m]) * (dataset[i][m] - dataset[j][m]);
}
return sum;
}
void cluster_remaining(
int n, int k, int d,
double * dataset,
double * radii,
double * neighbors,
int * initial_memberships,
int * result) {
int ** knn_neighbors = new int*[n];
double ** data;
data = new double *[n];
for (int i = 0; i < n; ++i) {
data[i] = new double[d];
knn_neighbors[i] = new int[k];
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
knn_neighbors[i][j] = neighbors[i * k + j];
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < d; ++j) {
data[i][j] = dataset[i * d + j];
}
}
// Final clusters.
GraphBasic H = GraphBasic(n);
int n_chosen_clusters = 0;
for (int i = 0; i < n; ++i) {
if (n_chosen_clusters < initial_memberships[i]) {
n_chosen_clusters = initial_memberships[i];
}
}
n_chosen_clusters += 1;
vector<vector<int> > modal_sets(n_chosen_clusters);
for (int c = 0; c < n_chosen_clusters; ++c) {
modal_sets.push_back(vector<int>());
}
for (int i = 0; i < n; ++i) {
if (initial_memberships[i] >= 0) {
modal_sets[initial_memberships[i]].push_back(i);
}
}
for (int c = 0; c < n_chosen_clusters; ++c) {
for (size_t i = 0; i < modal_sets[c].size() - 1; ++i) {
H.add_edge(modal_sets[c][i], modal_sets[c][i + 1]);
}
}
int next = -1;
double dt, best_distance = 0.;
for (int i = 0; i < n; ++i) {
if (initial_memberships[i] >= 0) {
continue;
}
next = -1;
for (int j = 0; j < k; ++j) {
if (radii[knn_neighbors[i][j]] < radii[i]) {
next = knn_neighbors[i][j];
break;
}
}
if (next < 0) {
best_distance = 1000000000.;
for (int j = 0; j < n; ++j) {
if (radii[j] >= radii[i]) {
continue;
}
dt = 0.0;
for (int m = 0; m < d; ++m) {
dt += (data[i][m] - data[j][m]) * (data[i][m] - data[j][m]);
}
if (best_distance > dt) {
best_distance = dt;
next = j;
}
}
}
H.add_edge(i, next);
}
for (int i = 0; i < n; ++i) {
result[i] = -1;
}
int n_clusters = 0;
map<int, int> label_mapping;
for (int i = 0; i < n; ++i) {
if (result[i] < 0) {
int label = (H.get_root(H.M[i]))->index;
if (label_mapping.count(label)) {
result[i] = label_mapping[label];
}
else {
label_mapping[label] = n_clusters;
result[i] = n_clusters;
n_clusters++;
}
}
}
for(int i=0;i<n;i++)
{
delete []knn_neighbors[i];
delete []data[i];
}
delete [] knn_neighbors;
delete [] data;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *dataset = mxGetPr(prhs[0]);
int d = mxGetM(prhs[0]);
int n = mxGetN(prhs[0]);
double *neighbors = mxGetPr(prhs[1]);
int k = mxGetM(prhs[1]);
double * radii = mxGetPr(prhs[2]);
double *beta = mxGetPr(prhs[3]);
double *epsilon = mxGetPr(prhs[4]);
double *membership_= mxGetPr(prhs[5]);
int * membership = new int[n];
for (int i = 0; i < n; i++)
{
membership[i] = int(membership_[i]);
}
int * result = new int[n];
cluster_remaining(n, k, d, dataset, radii, neighbors, membership, result);
plhs[0] = mxCreateDoubleMatrix(n, 1, mxREAL);
double *ptr = mxGetPr(plhs[0]);
for (int j = 0; j <n; j++)
{
ptr[j] = result[j];
}
delete [] result;
delete []membership;
}