-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCFM1997.h
164 lines (129 loc) · 5.5 KB
/
CCFM1997.h
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
//
// Created by Ghosh, Anirban on 11/9/21.
//
#ifndef CCFM1997_H
#define CCFM1997_H
#include <chrono>
#include <list>
#include <vector>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/spatial_sort.h>
#include <CGAL/Cartesian.h>
typedef CGAL::Cartesian<double>::Point_2 Point;
class CCFM1997 {
std::vector<Point> &P;
std::list<Point> &diskCenters;
long double sqrt3 = std::sqrt(3), sqrt3Over2 = sqrt3/2;
typedef std::pair<int,int> intPair;
typedef std::unordered_map< intPair, std::list<Point>, boost::hash<intPair> > HashMap;
const double sqrt2 = std::sqrt(2);
inline void insertIntoMap(HashMap &H, const Point &p) const {
int v = floor(p.x()/sqrt2), h = floor(p.y()/sqrt2);
auto it = H.find(std::make_pair(v,h));
if( it != H.end() )
it->second.emplace_back(p);
else {
std::list<Point> L;
L.push_back(p);
H[std::make_pair(v, h)] = L;
}
}
inline void removeFromMap(HashMap &H, const Point &p) const {
int v = floor(p.x()/sqrt2), h = floor(p.y()/sqrt2);
auto it = H.find(std::make_pair(v,h));
if( it != H.end() )
return;
else {
auto L = it->second;
for(auto itForL = L.begin(); itForL != L.end(); ++itForL)
if( *itForL == p ) {
L.erase(itForL);
return;
}
}
}
inline void static nnInsideACell(const std::list<Point> &L, const Point &p, double &minDistance, Point &nn) {
for(const Point &q : L) {
double dist = CGAL::squared_distance(p,q);
if( dist < minDistance ) {
minDistance = dist;
nn = q;
}
}
}
inline Point findNN(HashMap &H, const Point &p) const {
double minDistance = INFINITY;
Point nn(INFINITY,INFINITY);
int v = floor(p.x()/sqrt2), h = floor(p.y()/sqrt2);
auto it = H.find(std::make_pair(v,h));
if( it != H.end() && !it->second.empty() )
nnInsideACell(it->second,p,minDistance,nn);
it = H.find(std::make_pair(v-1,h+1));
if( it != H.end() && !it->second.empty() )
nnInsideACell(it->second,p,minDistance,nn);
it = H.find(std::make_pair(v,h+1));
if( it != H.end() && !it->second.empty() )
nnInsideACell(it->second,p,minDistance,nn);
it = H.find(std::make_pair(v+1,h+1));
if( it != H.end() && !it->second.empty() )
nnInsideACell(it->second,p,minDistance,nn);
it = H.find(std::make_pair(v-1,h));
if( it != H.end() && !it->second.empty() )
nnInsideACell(it->second,p,minDistance,nn);
it = H.find(std::make_pair(v+1,h));
if( it != H.end() && !it->second.empty() )
nnInsideACell(it->second,p,minDistance,nn);
it = H.find(std::make_pair(v-1,h-1));
if( it != H.end() && !it->second.empty() )
nnInsideACell(it->second,p,minDistance,nn);
it = H.find(std::make_pair(v,h-1));
if( it != H.end() && !it->second.empty() )
nnInsideACell(it->second,p,minDistance,nn);
it = H.find(std::make_pair(v+1,h-1));
if( it != H.end() && !it->second.empty() )
nnInsideACell(it->second,p,minDistance,nn);
return nn;
}
inline void newPoint(const Point &p, HashMap &activeDiskCenters, HashMap &inactiveDiskCenters) {
insertIntoMap(activeDiskCenters,p);
insertIntoMap(inactiveDiskCenters,Point(p.x()+sqrt3, p.y()));
insertIntoMap(inactiveDiskCenters,Point(p.x()+sqrt3Over2, p.y()+1.5));
insertIntoMap(inactiveDiskCenters,Point(p.x()+sqrt3Over2, p.y()-1.5));
insertIntoMap(inactiveDiskCenters,Point(p.x()-sqrt3Over2, p.y()+1.5));
insertIntoMap(inactiveDiskCenters,Point(p.x()-sqrt3, p.y()));
insertIntoMap(inactiveDiskCenters,Point(p.x()-sqrt3Over2, p.y()-1.5));
}
public:
CCFM1997(std::vector<Point> &P, std::list<Point> &diskCenters) : P(P), diskCenters(diskCenters) { }
double execute() {
assert(!P.empty());
auto start = std::chrono::high_resolution_clock::now();
HashMap activeDiskCenters, inactiveDiskCenters;
newPoint(P[0],activeDiskCenters,inactiveDiskCenters);
diskCenters.emplace_back(P[0]);
for(unsigned i = 1; i < P.size(); i++) {
Point nearestDiskCenter = findNN(activeDiskCenters,P[i]);
if(nearestDiskCenter != Point(INFINITY,INFINITY) && squared_distance(P[i],nearestDiskCenter) <=1)
continue;
if( inactiveDiskCenters.empty() ) {
newPoint(P[i],activeDiskCenters,inactiveDiskCenters);
diskCenters.emplace_back(P[i]);
continue;
}
nearestDiskCenter = findNN(inactiveDiskCenters,P[i]);
if( nearestDiskCenter == Point(INFINITY,INFINITY) || squared_distance(P[i],nearestDiskCenter) > 1) {
newPoint(P[i], activeDiskCenters, inactiveDiskCenters);
diskCenters.emplace_back(P[i]);
}
else {
removeFromMap(inactiveDiskCenters,nearestDiskCenter);
insertIntoMap(activeDiskCenters,nearestDiskCenter);
diskCenters.emplace_back(nearestDiskCenter);
}
}
auto stop = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = stop - start;
return duration.count();
}
};
#endif // CCFM1997_H