-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArea.cpp
More file actions
309 lines (270 loc) · 7.39 KB
/
Area.cpp
File metadata and controls
309 lines (270 loc) · 7.39 KB
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
// Area.cpp
// Copyright 2011, Dan Heeks
// This program is released under the BSD license. See the file COPYING for details.
#include "Area.h"
#include "AreaOrderer.h"
#include <map>
double CArea::m_accuracy = 0.01;
double CArea::m_units = 1.0;
bool CArea::m_fit_arcs = true;
//static const double PI = 3.1415926535897932;
void CArea::append(const CCurve& curve)
{
m_curves.push_back(curve);
}
void CArea::FitArcs(){
for(std::list<CCurve>::iterator It = m_curves.begin(); It != m_curves.end(); It++)
{
CCurve& curve = *It;
curve.FitArcs();
}
}
Point CArea::NearestPoint(const Point& p)const
{
double best_dist = 0.0;
Point best_point = Point(0, 0);
for(std::list<CCurve>::const_iterator It = m_curves.begin(); It != m_curves.end(); It++)
{
const CCurve& curve = *It;
Point near_point = curve.NearestPoint(p);
double dist = near_point.dist(p);
if(It == m_curves.begin() || dist < best_dist)
{
best_dist = dist;
best_point = near_point;
}
}
return best_point;
}
void CArea::GetBox(CBox2D &box)const
{
for(std::list<CCurve>::const_iterator It = m_curves.begin(); It != m_curves.end(); It++)
{
const CCurve& curve = *It;
curve.GetBox(box);
}
}
void CArea::Reorder()
{
// curves may have been added with wrong directions
// test all kurves to see which one are outsides and which are insides and
// make sure outsides are anti-clockwise and insides are clockwise
// returns 0, if the curves are OK
// returns 1, if the curves are overlapping
CAreaOrderer ao;
for(std::list<CCurve>::iterator It = m_curves.begin(); It != m_curves.end(); It++)
{
CCurve& curve = *It;
ao.Insert(&curve);
}
*this = ao.ResultArea();
}
void CArea::Split(std::list<CArea> &m_areas)const
{
if(HolesLinked())
{
for(std::list<CCurve>::const_iterator It = m_curves.begin(); It != m_curves.end(); It++)
{
const CCurve& curve = *It;
m_areas.push_back(CArea());
m_areas.back().m_curves.push_back(curve);
}
}
else
{
CArea a = *this;
a.Reorder();
for(std::list<CCurve>::const_iterator It = a.m_curves.begin(); It != a.m_curves.end(); It++)
{
const CCurve& curve = *It;
if(curve.IsClockwise())
{
if(m_areas.size() > 0)
m_areas.back().m_curves.push_back(curve);
}
else
{
m_areas.push_back(CArea());
m_areas.back().m_curves.push_back(curve);
}
}
}
}
double CArea::GetArea(bool always_add)const
{
// returns the area of the area
double area = 0.0;
for(std::list<CCurve>::const_iterator It = m_curves.begin(); It != m_curves.end(); It++)
{
const CCurve& curve = *It;
double a = curve.GetArea();
if(always_add)area += fabs(a);
else area += a;
}
return area;
}
eOverlapType GetOverlapType(const CCurve& c1, const CCurve& c2)
{
CArea a1;
a1.m_curves.push_back(c1);
CArea a2;
a2.m_curves.push_back(c2);
return GetOverlapType(a1, a2);
}
eOverlapType GetOverlapType(const CArea& a1, const CArea& a2)
{
CArea A1(a1);
A1.Subtract(a2);
if(A1.m_curves.size() == 0)
{
return eInside;
}
CArea A2(a2);
A2.Subtract(a1);
if(A2.m_curves.size() == 0)
{
return eOutside;
}
A1 = a1;
A1.Intersect(a2);
if(A1.m_curves.size() == 0)
{
return eSiblings;
}
return eCrossing;
}
bool IsInside(const Point& p, const CCurve& c)
{
CArea a;
a.m_curves.push_back(c);
return IsInside(p, a);
}
bool IsInside(const Point& p, const CArea& a)
{
CArea a2;
CCurve c;
c.m_vertices.push_back(CVertex(Point(p.x - 0.01, p.y - 0.01)));
c.m_vertices.push_back(CVertex(Point(p.x + 0.01, p.y - 0.01)));
c.m_vertices.push_back(CVertex(Point(p.x + 0.01, p.y + 0.01)));
c.m_vertices.push_back(CVertex(Point(p.x - 0.01, p.y + 0.01)));
c.m_vertices.push_back(CVertex(Point(p.x - 0.01, p.y - 0.01)));
a2.m_curves.push_back(c);
a2.Intersect(a);
if(fabs(a2.GetArea()) < 0.0004)return false;
return true;
}
void CArea::SpanIntersections(const Span& span, std::list<Point> &pts)const
{
// this returns all the intersections of this area with the given span, ordered along the span
// get all points where this area's curves intersect the span
std::list<Point> pts2;
for(std::list<CCurve>::const_iterator It = m_curves.begin(); It != m_curves.end(); It++)
{
const CCurve &c = *It;
c.SpanIntersections(span, pts2);
}
// order them along the span
std::multimap<double, Point> ordered_points;
for(std::list<Point>::iterator It = pts2.begin(); It != pts2.end(); It++)
{
Point &p = *It;
double t;
if(span.On(p, &t))
{
ordered_points.insert(std::make_pair(t, p));
}
}
// add them to the given list of points
for(std::multimap<double, Point>::iterator It = ordered_points.begin(); It != ordered_points.end(); It++)
{
Point p = It->second;
pts.push_back(p);
}
}
void CArea::CurveIntersections(const CCurve& curve, std::list<Point> &pts)const
{
// this returns all the intersections of this area with the given curve, ordered along the curve
std::list<Span> spans;
curve.GetSpans(spans);
for(std::list<Span>::iterator It = spans.begin(); It != spans.end(); It++)
{
Span& span = *It;
std::list<Point> pts2;
SpanIntersections(span, pts2);
for(std::list<Point>::iterator It = pts2.begin(); It != pts2.end(); It++)
{
Point &pt = *It;
if(pts.size() == 0)
{
pts.push_back(pt);
}
else
{
if(pt != pts.back())pts.push_back(pt);
}
}
}
}
class ThickLine
{
public:
CArea m_area;
CCurve m_curve;
ThickLine(const CCurve& curve)
{
m_curve = curve;
m_area.append(curve);
m_area.Thicken(0.001);
}
};
void CArea::InsideCurves(const CCurve& curve, std::list<CCurve> &curves_inside)const
{
//1. find the intersectionpoints between these two curves.
std::list<Point> pts;
CurveIntersections(curve, pts);
//2.seperate curve2 in multiple curves between these intersections.
std::list<CCurve> separate_curves;
curve.ExtractSeparateCurves(pts, separate_curves);
//3. if the midpoint of a seperate curve lies in a1, then we return it.
for(std::list<CCurve>::iterator It = separate_curves.begin(); It != separate_curves.end(); It++)
{
CCurve &curve = *It;
double length = curve.Perim();
Point mid_point = curve.PerimToPoint(length * 0.5);
if(IsInside(mid_point, *this))curves_inside.push_back(curve);
}
}
CArea CArea::Swept(const Point& vector)const
{
// make a sweep area by uniting lots of lozenges
std::list<CCurve> curves;
for (std::list<CCurve>::const_iterator It = m_curves.begin(); It != m_curves.end(); It++)
{
const CCurve &curve = *It;
std::list<Span> spans;
curve.GetSpans(spans);
for (std::list<Span>::iterator It = spans.begin(); It != spans.end(); It++)
{
Span& span = *It;
CCurve lozenge;
lozenge.m_vertices.push_back(span.m_p);
lozenge.m_vertices.push_back(span.m_v);
lozenge.m_vertices.push_back(span.m_v.m_p + vector);
lozenge.m_vertices.push_back(CVertex(-span.m_v.m_type, span.m_p + vector, span.m_v.m_c + vector));
lozenge.m_vertices.push_back(span.m_p);
if (lozenge.IsClockwise())lozenge.Reverse();
curves.push_back(lozenge);
}
}
CArea area = CArea::UniteCurves(curves);
area.Union(*this);
return area;
}
void CArea::Transform(const Matrix& matrix)
{
for (std::list<CCurve>::iterator It = m_curves.begin(); It != m_curves.end(); It++)
{
CCurve &curve = *It;
curve.Transform(matrix);
}
}