-
Notifications
You must be signed in to change notification settings - Fork 9
/
Polygon.cpp
177 lines (147 loc) · 3.35 KB
/
Polygon.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
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Polygon.h"
PolyClip::Polygon::Polygon(std::vector<Point2d> vertices)
{
vertexNum_ = 0;
for (int i = 0; i<vertices.size(); ++i)
{
if (i == 0)
{
Vertex* new_v = new Vertex(vertices[0]);
new_v->next_ = new_v;
new_v->prev_ = new_v;
start_ = new_v;
end_ = new_v;
vertexNum_++;
}
else
{
Insert(vertices[i], end_);
end_ = end_->next_;
}
}
}
PolyClip::Polygon::Polygon(Polygon& poly)
{
int i = 0;
for (auto iter = poly.begin(); iter != poly.end(); ++iter, ++i)
{
if ((*iter).intersect_) // kick out intersection points which were added by polygon clipping operation.
continue;
if (i == 0)
{
Vertex* new_v = new Vertex(Point2d((*iter).x_, (*iter).y_));
/* Note: these member variables cannot be validly copy since they are modified by polygon clipping operation.
*new_v->alpha_ = (*iter).alpha_;
new_v->entryExit_ = (*iter).entryExit_;
new_v->intersect_ = (*iter).intersect_;
new_v->neighbour_ = (*iter).neighbour_;*/
new_v->next_ = new_v;
new_v->prev_ = new_v;
start_ = new_v;
end_ = new_v;
vertexNum_++;
}
else
{
Vertex* new_v = new Vertex(Point2d((*iter).x_, (*iter).y_));
/*new_v->alpha_ = (*iter).alpha_;
new_v->entryExit_ = (*iter).entryExit_;
new_v->intersect_ = (*iter).intersect_;
new_v->neighbour_ = (*iter).neighbour_;*/
Insert(new_v, end_);
end_ = end_->next_;
}
}
}
PolyClip::Polygon& PolyClip::Polygon::operator=(Polygon& poly)
{
if (this == &poly) {
return *this;
}
// first, deallocate memory that 'this' used to hold
while (start_)
Remove(start_);
// second, copy from poly by using copy constructor
Polygon* copy_poly = new Polygon(poly);
// update
vertexNum_ = copy_poly->vertexNum_;
start_ = copy_poly->start_;
end_ = copy_poly->end_;
return *this;
}
PolyClip::Polygon::~Polygon()
{
while (start_)
Remove(start_);
}
void PolyClip::Polygon::Insert(Vertex* new_v, Vertex* pos)
{
auto next_v = pos->next_;
// modify new vertex
new_v->next_ = next_v;
new_v->prev_ = pos;
// modify current vertex
pos->next_ = new_v;
// modify next vertex
next_v->prev_ = new_v;
vertexNum_++;
}
void PolyClip::Polygon::Insert(const Point2d& vertex, Vertex* pos)
{
Vertex * new_v = new Vertex(vertex);
auto next_v = pos->next_;
// modify new vertex
new_v->next_ = next_v;
new_v->prev_ = pos;
// modify current vertex
pos->next_ = new_v;
// modify next vertex
next_v->prev_ = new_v;
vertexNum_++;
}
void PolyClip::Polygon::Insert(float x, float y, Vertex* pos)
{
Vertex * new_v = new Vertex(x, y);
auto next_v = pos->next_;
// modify new vertex
new_v->next_ = next_v;
new_v->prev_ = pos;
// modify current vertex
pos->next_ = new_v;
// modify next vertex
next_v->prev_ = new_v;
vertexNum_++;
}
void PolyClip::Polygon::Remove(Vertex* pos)
{
if (vertexNum_ == 1)
{
start_ = nullptr;
end_ = nullptr;
auto neighbor_v = pos->neighbour_;
if (neighbor_v)
neighbor_v->neighbour_ = nullptr;
delete pos;
}
else
{
auto pre_v = pos->prev_;
auto next_v = pos->next_;
auto neighbor_v = pos->neighbour_;
// modify previous vertex
if (pre_v)
pre_v->next_ = next_v;
// modify next vertex
if (next_v)
next_v->prev_ = pre_v;
// modify neighbor vertex
if (neighbor_v)
neighbor_v->neighbour_ = nullptr;
if (pos == start_)
start_ = next_v;
if (pos == end_)
end_ = pre_v;
delete pos;
}
vertexNum_--;
}