-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFASTCOVER-PP.h
170 lines (134 loc) · 6.16 KB
/
FASTCOVER-PP.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
164
165
166
167
168
169
170
#ifndef FASTCOVERPP_H
#define FASTCOVERPP_H
#include <chrono>
#include <list>
#include <vector>
#include <unordered_set>
#include <CGAL/Cartesian.h>
typedef CGAL::Cartesian<double>::Point_2 Point;
class FASTCOVER_PP {
std::vector<Point> &P;
std::list<Point> &diskCenters;
const double sqrt2 = std::sqrt(2);
const double additiveFactor = sqrt2/2;
const double sqrt2TimesOnePointFiveMinusOne = (sqrt2 * 1.5)-1;
const double sqrt2TimesZeroPointFivePlusOne = (sqrt2 * 0.5)-1;
struct BoundingBox {
double minX, maxX, minY, maxY;
BoundingBox() {
minX = minY = DBL_MAX;
maxX = maxY = DBL_MIN;
}
explicit BoundingBox(const Point &p) : minX(p.x()), maxX(p.x()), minY(p.y()), maxY(p.y()) { }
void update(const Point &p) {
minX = std::min( minX, p.x());
minY = std::min( minY, p.y());
maxX = std::max( maxX, p.x());
maxY = std::max( maxY, p.y());
}
};
typedef std::pair<int,int> intPair;
typedef std::pair<BoundingBox,bool> diskInfo;
typedef std::unordered_map< intPair, diskInfo, boost::hash<intPair> > HashMap;
inline bool static trytoMergeDisk(HashMap &H, HashMap::iterator &iterToSourceDisk, int vPrime, int hPrime, std::list<Point> &diskCenters) {
auto iterToTargetDisk = H.find(std::make_pair(vPrime,hPrime));
if( iterToTargetDisk == H.end())
return false;
if( iterToTargetDisk->second.second ) {
double minX = std::min((*iterToSourceDisk).second.first.minX, iterToTargetDisk->second.first.minX);
double minY = std::min((*iterToSourceDisk).second.first.minY, iterToTargetDisk->second.first.minY);
double maxX = std::max((*iterToSourceDisk).second.first.maxX, iterToTargetDisk->second.first.maxX);
double maxY = std::max((*iterToSourceDisk).second.first.maxY, iterToTargetDisk->second.first.maxY);
Point lowerLeft(minX,minY), upperRight(maxX,maxY);
if( CGAL::squared_distance(lowerLeft,upperRight) <= 4) {
(*iterToSourceDisk).second.second = false;
iterToTargetDisk->second.second = false;
diskCenters.push_back(CGAL::midpoint(lowerLeft,upperRight));
return true;
}
}
return false;
}
public:
FASTCOVER_PP(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 H;
for( const Point &p : P) {
int v = floor(p.x()/sqrt2), h = floor(p.y()/sqrt2);
double verticalTimesSqrtTwo = v*sqrt2, horizontalTimesSqrt2 = h*sqrt2;
auto it = H.find(std::make_pair(v,h));
if( it != H.end() ) {
it->second.first.update(p);
continue;
}
if( (p.x() >= verticalTimesSqrtTwo + sqrt2TimesOnePointFiveMinusOne) ) {
it = H.find(std::make_pair(v+1,h));
if( it != H.end() && (CGAL::squared_distance(p,Point(sqrt2*(v+1)+additiveFactor,horizontalTimesSqrt2+additiveFactor))<=1)) {
it->second.first.update(p);
continue;
}
}
if( (p.x() <= verticalTimesSqrtTwo - sqrt2TimesZeroPointFivePlusOne) ) {
it = H.find(std::make_pair(v-1,h));
if( it != H.end() && (CGAL::squared_distance(p,Point(sqrt2*(v-1)+additiveFactor,horizontalTimesSqrt2+additiveFactor))<=1)) {
it->second.first.update(p);
continue;
}
}
if( (p.y() <= horizontalTimesSqrt2 + sqrt2TimesOnePointFiveMinusOne) ) {
it = H.find(std::make_pair(v,h-1));
if( it != H.end() && (CGAL::squared_distance(p,Point(verticalTimesSqrtTwo+additiveFactor,sqrt2*(h-1)+additiveFactor))<=1)) {
it->second.first.update(p);
continue;
}
}
if( (p.y() >= horizontalTimesSqrt2 - sqrt2TimesZeroPointFivePlusOne) ) {
it = H.find(std::make_pair(v,h+1));
if(it != H.end() && (CGAL::squared_distance(p,Point(verticalTimesSqrtTwo+additiveFactor,sqrt2*(h+1)+additiveFactor))<=1)) {
it->second.first.update(p);
continue;
}
}
H[std::make_pair(v,h)] = std::make_pair(BoundingBox(p),true);
}
for(auto iter = H.begin(); iter != H.end(); ++iter) {
int v =(*iter).first.first, h = (*iter).first.second;
if(!(*iter).second.second) {
continue;
}
// Attempt to merge with the S disk
if( trytoMergeDisk(H,iter,v,h-1,diskCenters))
continue;
// Attempt to merge with the N disk
if( trytoMergeDisk(H,iter,v,h+1,diskCenters))
continue;
// Attempt to merge with the E disk
if( trytoMergeDisk(H,iter,v+1,h,diskCenters))
continue;
// Attempt to merge with the W disk
if( trytoMergeDisk(H,iter,v-1,h,diskCenters))
continue;
// Attempt to merge with the SW disk
if( trytoMergeDisk(H,iter,v-1,h-1,diskCenters))
continue;
// Attempt to merge with the SE disk
if( trytoMergeDisk(H,iter,v+1,h-1,diskCenters))
continue;
// Attempt to merge with the NE disk
if( trytoMergeDisk(H,iter,v+1,h+1,diskCenters))
continue;
// Attempt to merge with the NW disk
if( trytoMergeDisk(H,iter,v-1,h+1,diskCenters))
continue;
}
for(auto aPair: H)
if(aPair.second.second)
diskCenters.emplace_back(Point(aPair.first.first*sqrt2+additiveFactor,aPair.first.second*sqrt2+additiveFactor));
auto stop = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = stop - start;
return duration.count();
}
};
#endif // FASTCOVERPP_H