-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRegion2D.cpp
More file actions
178 lines (151 loc) · 5.8 KB
/
Copy pathRegion2D.cpp
File metadata and controls
178 lines (151 loc) · 5.8 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
#include "vector"
#include "utility"
#include "algorithm"
#include "queue"
#include "iostream"
#include "Region2D.h"
#include "HalfSegment2D.h"
#include "Segment2D.h"
#include <memory>
//Implementation
struct Region2D::Impl {
Impl();
Impl(std::vector<Segment2D> _regionSegments);
Impl(const Region2D ®ion);
~Impl();
std::vector<AttributedHalfSegment2D> regionSegments;
std::vector<HalfSegment2D> halfSegments;
void setFlags();
SimplePoint2D GetDominatePoint(HalfSegment2D inputHalfSegment);
HalfSegment2D GetBrotherSeg(HalfSegment2D currentHalfSeg);
bool GetAboveFlag(HalfSegment2D currentHalfSeg);
bool CheckLessThan(SimplePoint2D dp, HalfSegment2D halfSeg);
};
Region2D::Impl::Impl()
{
std::vector<AttributedHalfSegment2D> empty;
this->regionSegments = empty;
}
Region2D::Impl::~Impl() {}
Region2D::Impl::Impl(std::vector<Segment2D> _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();
}
SimplePoint2D Region2D::Impl::GetDominatePoint(HalfSegment2D inputHalfSegment)
{
Segment2D tempSegment = inputHalfSegment.s;
if(inputHalfSegment.isDominatingPointLeft)
{
return tempSegment.leftEndPoint;
}
else {
return tempSegment.rightEndPoint;
}
}
HalfSegment2D Region2D::Impl::GetBrotherSeg(HalfSegment2D currentHalfSeg)
{
HalfSegment2D brotherSeg = currentHalfSeg;
brotherSeg.isDominatingPointLeft = !currentHalfSeg.isDominatingPointLeft;
return brotherSeg;
}
bool Region2D::Impl::GetAboveFlag(HalfSegment2D currentHalfSeg)
{
for(int i = 0; i < regionSegments.size(); i++) {
if(regionSegments[i].hs == currentHalfSeg)
{
return regionSegments[i].above;
}
}
}
bool Region2D::Impl::CheckLessThan(SimplePoint2D dp, HalfSegment2D halfSeg)
{
Segment2D seg = halfSeg.s;
SimplePoint2D leftPoint = seg.leftEndPoint;
SimplePoint2D rightPoint = seg.rightEndPoint;
Number slope = (rightPoint.y - leftPoint.y) / (rightPoint.x - leftPoint.x);
Number b = leftPoint.y / (slope * leftPoint.x);
Number halfSegY = (dp.x * slope) + b;
if(dp.y < halfSegY)
{
return true;
}
else
{
return false;
}
}
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())
{
sweepStatus.push_back(currentHalfSeg); //add half segment to sweep status at beginning
regionSegments.push_back(AttributedHalfSegment2D(currentHalfSeg,true));
regionSegments.push_back(AttributedHalfSegment2D(GetBrotherSeg(currentHalfSeg), true));
}
else if(GetDominatePoint(currentHalfSeg) == GetDominatePoint(sweepStatus.back())) //add half segment to sweep status at end
{
sweepStatus.push_back(currentHalfSeg);
regionSegments.push_back(AttributedHalfSegment2D(currentHalfSeg, !(GetAboveFlag(sweepStatus.back()))));
regionSegments.push_back(AttributedHalfSegment2D(GetBrotherSeg(currentHalfSeg), !(GetAboveFlag(sweepStatus.back()))));
}
else //add halfsegment to sweep status somewhere in the middle
{
int index = sweepStatus.size() - 1;
while(!CheckLessThan(GetDominatePoint(currentHalfSeg), sweepStatus[index])) //find index
{
index -= 1;
}
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;
}
}
}
}
}
///////////////////////////////////////////////////////// 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)) {}