-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegion2D.cpp
More file actions
220 lines (194 loc) · 7.12 KB
/
Copy pathRegion2D.cpp
File metadata and controls
220 lines (194 loc) · 7.12 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
#include "vector"
#include "utility"
#include "algorithm"
#include "queue"
#include "iostream"
#include "Region2D.h"
#include "HalfSegment2D.h"
#include "Segment2D.h"
#include <memory>
#include <iostream>
//Implementation
struct Region2D::Impl {
Impl();
Impl(std::vector<Segment2D> _regionSegments);
Impl(const Region2D ®ion);
~Impl();
std::vector<AttributedHalfSegment2D> regionSegments;
std::vector<HalfSegment2D> halfSegments;
std::vector<Segment2D> segments;
void setFlags();
SimplePoint2D GetDominatePoint(HalfSegment2D inputHalfSegment);
HalfSegment2D GetBrotherSeg(HalfSegment2D currentHalfSeg);
bool GetAboveFlag(HalfSegment2D currentHalfSeg);
bool CheckLessThan(SimplePoint2D dp, HalfSegment2D halfSeg);
};
//cody
Region2D::Impl::Impl()
{
std::vector<AttributedHalfSegment2D> empty;
this->regionSegments = empty;
}
Region2D::Impl::~Impl() {}
//cody
Region2D::Impl::Impl(std::vector<Segment2D> _regionSegments)
{
this->segments = _regionSegments;
std::vector<HalfSegment2D> HalfSegVec; //temporary vector of half segments for later use
for(int i = 0; i < _regionSegments.size(); i++)
{
HalfSegment2D domHalfSeg = HalfSegment2D(_regionSegments[i], true); //set dominant point half segment from segment
HalfSegVec.push_back(domHalfSeg);
HalfSegment2D endHalfSeg = HalfSegment2D(_regionSegments[i], false); //set end point half segment from segment
HalfSegVec.push_back(endHalfSeg);
}
std::sort(HalfSegVec.begin(), HalfSegVec.end());
this->halfSegments = HalfSegVec;
setFlags();
}
//cody
SimplePoint2D Region2D::Impl::GetDominatePoint(HalfSegment2D inputHalfSegment)
{
Segment2D tempSegment = inputHalfSegment.s;
if(inputHalfSegment.isDominatingPointLeft)
{
return tempSegment.leftEndPoint;
}
else {
return tempSegment.rightEndPoint;
}
}
//cody
HalfSegment2D Region2D::Impl::GetBrotherSeg(HalfSegment2D currentHalfSeg)
{
HalfSegment2D brotherSeg = currentHalfSeg;
brotherSeg.isDominatingPointLeft = !currentHalfSeg.isDominatingPointLeft;
return brotherSeg;
}
//cody
bool Region2D::Impl::GetAboveFlag(HalfSegment2D currentHalfSeg)
{
for(int i = 0; i < regionSegments.size(); i++) {
if(regionSegments[i].hs == currentHalfSeg)
{
return regionSegments[i].above;
}
}
}
//cody
bool Region2D::Impl::CheckLessThan(SimplePoint2D dp, HalfSegment2D halfSeg)
{
Segment2D seg = halfSeg.s;
SimplePoint2D left = seg.leftEndPoint;
SimplePoint2D right = seg.rightEndPoint;
// handle infinite slope
if (right.x == left.x) {
return dp.y < left.y;
}
Number slope = (right.y - left.y) / (right.x - left.x);
Number b = left.y - (slope * left.x); // The minus '-' was originally a '/'. I think this is supposed to be based on y=mx+b, which rewrites into b=y-mx.
Number halfSegY = (dp.x * slope) + b;
return dp.y < halfSegY;
}
//cody
void Region2D::Impl::setFlags()
{
//initializing sweep structures
std::queue<HalfSegment2D> sweepQueue;
std::vector<HalfSegment2D> sweepStatus;
for(int i = 0; i < halfSegments.size(); i++) //loading queue with half segments for sweep
{
sweepQueue.push(halfSegments[i]);
}
HalfSegment2D currentHalfSeg;
while(!sweepQueue.empty())
{
currentHalfSeg = sweepQueue.front();
sweepQueue.pop();
if(currentHalfSeg.isDominatingPointLeft) {
if(sweepStatus.empty())
{
regionSegments.push_back(AttributedHalfSegment2D(currentHalfSeg,true));
regionSegments.push_back(AttributedHalfSegment2D(GetBrotherSeg(currentHalfSeg), true));
sweepStatus.push_back(currentHalfSeg); //add half segment to sweep status at beginning
}
else if(GetDominatePoint(currentHalfSeg) == GetDominatePoint(sweepStatus.back())) //add half segment to sweep status at end
{
regionSegments.push_back(AttributedHalfSegment2D(currentHalfSeg, !(GetAboveFlag(sweepStatus.back()))));
regionSegments.push_back(AttributedHalfSegment2D(GetBrotherSeg(currentHalfSeg), !(GetAboveFlag(sweepStatus.back()))));
sweepStatus.push_back(currentHalfSeg);
}
else if(CheckLessThan(GetDominatePoint(currentHalfSeg), sweepStatus[0]))
{
regionSegments.push_back(AttributedHalfSegment2D(currentHalfSeg,true));
regionSegments.push_back(AttributedHalfSegment2D(GetBrotherSeg(currentHalfSeg), true));
sweepStatus.emplace(sweepStatus.begin(), currentHalfSeg);
}
else //add halfsegment to sweep status somewhere in the middle
{
int index = 0;
while((index < sweepStatus.size()) && !CheckLessThan(GetDominatePoint(currentHalfSeg), sweepStatus[index])) //find index
{
index++;
}
sweepStatus.emplace(sweepStatus.begin() + index, currentHalfSeg); //emplace at index
regionSegments.push_back(AttributedHalfSegment2D(currentHalfSeg, !(GetAboveFlag(sweepStatus[index - 1]))));
regionSegments.push_back(AttributedHalfSegment2D(GetBrotherSeg(currentHalfSeg), !(GetAboveFlag(sweepStatus[index - 1]))));
}
}
else {
//remove halfsegment from status
for(int i = 0; i < sweepStatus.size(); i++)
{
if(sweepStatus[i].s == currentHalfSeg.s)
{
sweepStatus.erase(sweepStatus.begin() + i);
break;
}
}
}
}
std::sort(regionSegments.begin(), regionSegments.end());
}
///////////////////////////////////////////////////////// Region Constructors ///////////////////////////////////////////////////////////////////////////////////
Region2D::Impl::Impl(const Region2D ®ion)
{
this->regionSegments = region.pimpl->regionSegments;
}
//empty constructor
Region2D::Region2D() : pimpl(new Impl) {}
//copy constructor
Region2D::Region2D(Region2D const ®ion) : pimpl(new Impl(region)) {}
//base constructor
Region2D::Region2D(std::vector<Segment2D> region) : pimpl(new Impl(region)) {}
//move constructor
Region2D::Region2D(Region2D &®ion) : pimpl(std::move(region.pimpl)) {}
//destructor
Region2D::~Region2D() {}
//iterator methods
Region2D::iterator Region2D::begin()
{
return this->pimpl->regionSegments.begin();
}
Region2D::iterator Region2D::end()
{
return this->pimpl->regionSegments.end();
}
std::vector<Segment2D> Region2D::getSegments()
{
std::vector<Segment2D> segments;
for (HalfSegment2D h : this->pimpl->halfSegments)
if (h.isDominatingPointLeft)
segments.push_back(h.s);
return segments;
}
bool Region2D::operator==(const Region2D& other) const
{
for (int i = 0; i < std::min(this->pimpl->halfSegments.size(), other.pimpl->halfSegments.size()); i++) {
HalfSegment2D h1 = this->pimpl->halfSegments[i];
HalfSegment2D h2 = other.pimpl->halfSegments[i];
if (h1 != h2)
return false;
}
return true;
}