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
|
#pragma once
#include "geometry/point2d.hpp"
#include "geometry/rect2d.hpp"
#include "geometry/rect_intersect.hpp"
#include "geometry/angles.hpp"
#include "base/math.hpp"
namespace m2
{
/// axis aligned rect
template <typename T>
class AnyRect
{
ang::Angle<T> m_angle;
Point<T> m_zero;
Rect<T> m_rect;
static Point<T> const Convert(Point<T> const & p,
Point<T> const & fromI, Point<T> const & fromJ,
Point<T> const & toI, Point<T> const & toJ)
{
Point<T> res;
res.x = p.x * DotProduct(fromI, toI) + p.y * DotProduct(fromJ, toI);
res.y = p.x * DotProduct(fromI, toJ) + p.y * DotProduct(fromJ, toJ);
return res;
}
public:
AnyRect() : m_zero(0, 0), m_rect() {}
/// creating from regular rect
explicit AnyRect(Rect<T> const & r)
{
if (r.IsValid())
{
m_zero = Point<T>(r.minX(), r.minY());
m_rect = Rect<T>(0, 0, r.SizeX(), r.SizeY());
}
else
{
m_zero = Point<T>(0, 0);
m_rect = r;
}
}
AnyRect(Point<T> const & zero, ang::Angle<T> const & angle, Rect<T> const & r)
: m_angle(angle), m_rect(r)
{
m_zero = Convert(zero, Point<T>(1, 0), Point<T>(0, 1), i(), j());
}
Point<T> const & LocalZero() const
{
return m_zero;
}
Point<T> const GlobalZero() const
{
return Convert(m_zero, i(), j(), m2::Point<T>(1, 0), m2::Point<T>(0, 1));
}
Point<T> const i() const
{
return Point<T>(m_angle.cos(), m_angle.sin());
}
Point<T> const j() const
{
return Point<T>(-m_angle.sin(), m_angle.cos());
}
void SetAngle(ang::Angle<T> const & a)
{
m2::Point<T> glbZero = GlobalZero();
m_angle = a;
m_zero = Convert(glbZero, Point<T>(1, 0), Point<T>(0, 1), i(), j());
}
ang::Angle<T> const & Angle() const
{
return m_angle;
}
Point<T> const GlobalCenter() const
{
return ConvertFrom(m_rect.Center());
}
Point<T> const LocalCenter() const
{
return m_rect.Center();
}
T GetMaxSize() const { return max(m_rect.SizeX(), m_rect.SizeY()); }
bool IsPointInside(Point<T> const & pt) const
{
return m_rect.IsPointInside(ConvertTo(pt));
}
bool IsRectInside(AnyRect<T> const & r) const
{
m2::Point<T> pts[4];
r.GetGlobalPoints(pts);
ConvertTo(pts, 4);
return m_rect.IsPointInside(pts[0])
&& m_rect.IsPointInside(pts[1])
&& m_rect.IsPointInside(pts[2])
&& m_rect.IsPointInside(pts[3]);
}
bool IsIntersect(AnyRect<T> const & r) const
{
m2::Point<T> pts[4];
if (r.GetLocalRect() == Rect<T>())
return false;
r.GetGlobalPoints(pts);
ConvertTo(pts, 4);
m2::Rect<T> r1(pts[0], pts[0]);
r1.Add(pts[1]);
r1.Add(pts[2]);
r1.Add(pts[3]);
if (!GetLocalRect().IsIntersect(r1))
return false;
if (r.IsRectInside(*this))
return true;
if (IsRectInside(r))
return true;
return Intersect(GetLocalRect(), pts[0], pts[1])
|| Intersect(GetLocalRect(), pts[1], pts[2])
|| Intersect(GetLocalRect(), pts[2], pts[3])
|| Intersect(GetLocalRect(), pts[3], pts[0]);
}
/// Convert into coordinate system of this AnyRect
Point<T> const ConvertTo(Point<T> const & p) const
{
m2::Point<T> i1(1, 0);
m2::Point<T> j1(0, 1);
return Convert(p - Convert(m_zero, i(), j(), i1, j1), i1, j1, i(), j());
}
void ConvertTo(Point<T> * pts, size_t count) const
{
for (size_t i = 0; i < count; ++i)
pts[i] = ConvertTo(pts[i]);
}
/// Convert into global coordinates from the local coordinates of this AnyRect
Point<T> const ConvertFrom(Point<T> const & p) const
{
return Convert(p + m_zero, i(), j(), m2::Point<T>(1, 0), m2::Point<T>(0, 1));
}
void ConvertFrom(Point<T> * pts, size_t count) const
{
for (size_t i = 0; i < count; ++i)
pts[i] = ConvertFrom(pts[i]);
}
Rect<T> const & GetLocalRect() const
{
return m_rect;
}
Rect<T> const GetGlobalRect() const
{
Point<T> pts[4];
GetGlobalPoints(pts);
Rect<T> res(pts[0], pts[1]);
res.Add(pts[2]);
res.Add(pts[3]);
return res;
}
void GetGlobalPoints(Point<T> * pts) const
{
pts[0] = Point<T>(ConvertFrom(Point<T>(m_rect.minX(), m_rect.minY())));
pts[1] = Point<T>(ConvertFrom(Point<T>(m_rect.minX(), m_rect.maxY())));
pts[2] = Point<T>(ConvertFrom(Point<T>(m_rect.maxX(), m_rect.maxY())));
pts[3] = Point<T>(ConvertFrom(Point<T>(m_rect.maxX(), m_rect.minY())));
}
template <typename U>
void Inflate(U const & dx, U const & dy)
{
m_rect.Inflate(dx, dy);
}
void Add(AnyRect<T> const & r)
{
Point<T> pts[4];
r.GetGlobalPoints(pts);
ConvertTo(pts, 4);
m_rect.Add(pts[0]);
m_rect.Add(pts[1]);
m_rect.Add(pts[2]);
m_rect.Add(pts[3]);
}
void Offset(Point<T> const & p)
{
m_zero = ConvertTo(ConvertFrom(m_zero) + p);
}
Point<T> const Center() const
{
return ConvertFrom(m_rect.Center());
}
void SetSizesToIncludePoint(Point<T> const & p)
{
m_rect.SetSizesToIncludePoint(ConvertTo(p));
}
friend string DebugPrint(m2::AnyRect<T> const & r)
{
return "{ Zero = " + DebugPrint(r.m_zero) +
", Rect = " + DebugPrint(r.m_rect) +
", Ang = " + DebugPrint(r.m_angle) + " }";
}
};
template <typename T>
AnyRect<T> const Offset(AnyRect<T> const & r, Point<T> const & pt)
{
AnyRect<T> res(r);
res.Offset(pt);
return res;
}
template <typename T, typename U>
AnyRect<T> const Inflate(AnyRect<T> const & r, U const & dx, U const & dy)
{
AnyRect<T> res = r;
res.Inflate(dx, dy);
return res;
}
template <typename T, typename U>
AnyRect<T> const Inflate(AnyRect<T> const & r, Point<U> const & pt)
{
return Inflate(r, pt.x, pt.y);
}
typedef AnyRect<double> AnyRectD;
typedef AnyRect<float> AnyRectF;
}
|