forked from pchalamet/Martinez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.cpp
111 lines (105 loc) · 2.66 KB
/
connector.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
/***************************************************************************
* Developer: Francisco Martínez del Río (2011) *
* Version: 1.4.1 *
* *
* This is a public domain program *
***************************************************************************/
#include "connector.h"
#include <algorithm>
void PointChain::init (const Segment& s)
{
l.push_back (s.begin ());
l.push_back (s.end ());
}
bool PointChain::LinkSegment (const Segment& s)
{
if (s.begin () == l.front ()) {
if (s.end () == l.back ())
_closed = true;
else
l.push_front (s.end ());
return true;
}
if (s.end () == l.back ()) {
if (s.begin () == l.front ())
_closed = true;
else
l.push_back (s.begin ());
return true;
}
if (s.end () == l.front ()) {
if (s.begin () == l.back ())
_closed = true;
else
l.push_front (s.begin ());
return true;
}
if (s.begin () == l.back ()) {
if (s.end () == l.front ())
_closed = true;
else
l.push_back (s.end ());
return true;
}
return false;
}
bool PointChain::LinkPointChain (PointChain& chain)
{
if (chain.l.front () == l.back ()) {
chain.l.pop_front ();
l.splice (l.end (), chain.l);
return true;
}
if (chain.l.back () == l.front ()) {
l.pop_front ();
l.splice (l.begin (), chain.l);
return true;
}
if (chain.l.front () == l.front ()) {
l.pop_front ();
reverse (chain.l.begin (), chain.l.end ());
l.splice (l.begin (), chain.l);
return true;
}
if (chain.l.back () == l.back ()) {
l.pop_back ();
reverse (chain.l.begin (), chain.l.end ());
l.splice (l.end (), chain.l);
return true;
}
return false;
}
void Connector::add(const Segment& s)
{
iterator j = openPolygons.begin ();
while (j != openPolygons.end ()) {
if (j->LinkSegment (s)) {
if (j->closed ())
closedPolygons.splice (closedPolygons.end (), openPolygons, j);
else {
list<PointChain>::iterator k = j;
for (++k; k != openPolygons.end (); k++) {
if (j->LinkPointChain (*k)) {
openPolygons.erase (k);
break;
}
}
}
return;
}
j++;
}
// The segment cannot be connected with any open polygon
openPolygons.push_back (PointChain ());
openPolygons.back ().init (s);
}
void Connector::toPolygon (Polygon& p)
{
for (iterator it = begin (); it != end (); it++) {
p.push_back (Contour ());
Contour& contour = p.back ();
for (PointChain::iterator it2 = it->begin (); it2 != it->end (); it2++)
contour.add (*it2);
}
}