-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquadtree.cpp
165 lines (139 loc) · 4.57 KB
/
quadtree.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
#include "quadtree.h"
#include <cfloat> // FLT_EPSILON
void PolygonMask::precompute()
{
// see http://alienryderflex.com/polygon/
int i, j = size - 1;
constant.resize(size);
multiple.resize(size);
for (i = 0; i < size; ++i)
{
if (polyY[j] == polyY[i])
{
constant[i] = polyX[i];
multiple[i] = 0;
}
else
{
constant[i] = polyX[i] - (polyY[i] * polyX[j]) / (polyY[j] - polyY[i])
+ (polyY[i] * polyX[i]) / (polyY[j] - polyY[i]);
multiple[i] = (polyX[j] - polyX[i])/(polyY[j] - polyY[i]);
}
j = i;
}
}
PolygonMask::PolygonMask(std::vector<float> x, std::vector<float> y,
int size) : size(size), polyX(x), polyY(y)
{ precompute(); }
bool PolygonMask::pointInPolygon(float x, float y) const
{
// see http://alienryderflex.com/polygon/
int i, j = size - 1;
bool oddNodes = false;
for (i = 0; i < size; i++)
{
if ((polyY[i] < y && polyY[j] >= y) || (polyY[j] < y && polyY[i] >= y))
oddNodes ^= (y*multiple[i] + constant[i] < x);
j=i;
}
return oddNodes;
}
/*
std::ostream& operator<<(std::ostream& out, std::vector<float> x)
{
out << "[";
std::vector<float>::iterator it = x.begin(), ie = x.end();
for( ; it != ie ; ++it, out<<",")
out << *it;
out << "]";
return out;
}
*/
PolygonMask PolygonMask::clip(const Boundary& box) const
{
// http://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm
std::vector<float> xIn, yIn, xOut = polyX, yOut = polyY;
std::vector<Boundary::OUTSIDE_TEST> outsideTest;
outsideTest.push_back((Boundary::OUTSIDE_TEST) &Boundary::leftOf);
outsideTest.push_back((Boundary::OUTSIDE_TEST) &Boundary::rightOf);
outsideTest.push_back((Boundary::OUTSIDE_TEST) &Boundary::bottomOf);
outsideTest.push_back((Boundary::OUTSIDE_TEST) &Boundary::upOf);
std::vector<Boundary::INTERSECT> intersect;
intersect.push_back((Boundary::INTERSECT) &Boundary::interLeft);
intersect.push_back((Boundary::INTERSECT) &Boundary::interRight);
intersect.push_back((Boundary::INTERSECT) &Boundary::interBottom);
intersect.push_back((Boundary::INTERSECT) &Boundary::interUp);
// for each edge of the boundary box
for (size_t i = 0; i < 4; ++i)
{
xIn = xOut; yIn = yOut; xOut.clear(); yOut.clear();
if (xIn.size() == 0) break;
float xfrom = xIn.back(), yfrom = yIn.back();
std::vector<float>::iterator xpoly = xIn.begin(), ypoly = yIn.begin();
std::vector<float>::iterator xend = xIn.end();
// for each edge of the polygon
for ( ; xpoly != xend ; ++xpoly, ++ypoly)
{
if (!(box.*outsideTest[i])(*xpoly, *ypoly))
{
if ((box.*outsideTest[i])(xfrom, yfrom))
{
float x, y;
(box.*intersect[i])(xfrom, yfrom, *xpoly, *ypoly, x, y);
if ((x != *xpoly)||(y != *ypoly))
{xOut.push_back(x); yOut.push_back(y);}
}
xOut.push_back(*xpoly); yOut.push_back(*ypoly);
}
else if (!(box.*outsideTest[i])(xfrom, yfrom))
{
float x, y;
(box.*intersect[i])(xfrom, yfrom, *xpoly, *ypoly, x, y);
if ((x != xfrom)||(y != yfrom))
{ xOut.push_back(x); yOut.push_back(y); }
}
xfrom = *xpoly; yfrom = *ypoly;
}
}
return PolygonMask(xOut, yOut, xOut.size());
}
bool Boundary::contains(float x, float y)
{
return ((x < center_x + dim_x * 1.00001) &&
(x > center_x - dim_x * 1.00001) &&
(y < center_y + dim_y * 1.00001) &&
(y > center_y - dim_y * 1.00001));
}
int Boundary::coveredByPolygon(const PolygonMask& m) const
{
int nb = 0;
if (m.pointInPolygon(center_x + dim_x, center_y + dim_y)) ++nb;
if (m.pointInPolygon(center_x + dim_x, center_y - dim_y)) ++nb;
if (m.pointInPolygon(center_x - dim_x, center_y + dim_y)) ++nb;
if (m.pointInPolygon(center_x - dim_x, center_y - dim_y)) ++nb;
return nb;
}
void Boundary::interLeft(float x1, float y1, float x2, float y2,
float& xout, float& yout)
{
xout = center_x - dim_x;
yout = y1 + (xout - x1) / (x2 - x1) * (y2 - y1);
}
void Boundary::interRight(float x1, float y1, float x2, float y2,
float& xout, float& yout)
{
xout = center_x + dim_x;
yout = y1 + (xout - x1) / (x2 - x1) * (y2 - y1);
}
void Boundary::interBottom(float x1, float y1, float x2, float y2,
float& xout, float& yout)
{
yout = center_y - dim_y;
xout = x1 + (yout - y1) / (y2 - y1) * (x2 - x1);
}
void Boundary::interUp(float x1, float y1, float x2, float y2,
float& xout, float& yout)
{
yout = center_y + dim_y;
xout = x1 + (yout - y1) / (y2 - y1) * (x2 - x1);
}