-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaneSweep.cpp
More file actions
394 lines (349 loc) · 13.9 KB
/
PlaneSweep.cpp
File metadata and controls
394 lines (349 loc) · 13.9 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "PlaneSweep.h"
#include <algorithm>
#include <iostream>
//constructor
PlaneSweep::PlaneSweep() {
//
}
//functions
//cody
bool CheckLessThan(Segment2D segToAdd, Segment2D prevSeg)
{
SimplePoint2D dp = segToAdd.leftEndPoint;
SimplePoint2D left = prevSeg.leftEndPoint;
SimplePoint2D right = prevSeg.rightEndPoint;
// handle infinite slope
if (right.x == left.x) {
return dp < left;
}
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
bool pointGreaterThan(SimplePoint2D point, Segment2D segment)
{
SimplePoint2D left = segment.leftEndPoint;
SimplePoint2D right = segment.rightEndPoint;
// handle infinite slope
if (right.x == left.x) {
return point.y > left.y;
}
Number slope = (right.y - left.y) / (right.x - left.x);
Number b = left.y - (slope * left.x);
Number SegY = (point.x * slope) + b;
return point.y > SegY;
}
//cody
void PlaneSweep::add_left(Segment2D segment, int obj)
{
if(sweepStatus.empty())
sweepStatus.push_back(segment); //add half segment to sweep status at beginning
else
{
if(segment.leftEndPoint == sweepStatus.back().leftEndPoint) //add half segment to sweep status at end
{
sweepStatus.push_back(segment);
}
else if(segment.leftEndPoint.x == sweepStatus.back().leftEndPoint.x && segment.leftEndPoint.y > sweepStatus.back().leftEndPoint.y)
{
sweepStatus.push_back(segment);
}
else if(CheckLessThan(segment, sweepStatus[0]))
sweepStatus.emplace(sweepStatus.begin(), segment);
else //add halfsegment to sweep status somewhere in the middle
{
int index = 0;
while((index < sweepStatus.size()) && !CheckLessThan(segment, sweepStatus[index])) //find index
index++;
sweepStatus.emplace(sweepStatus.begin() + index, segment); //emplace at index
}
}
objects[segment] = obj;
}
//cody
void PlaneSweep::del_right(Segment2D segment)
{
sweepStatus.erase(find(sweepStatus.begin(), sweepStatus.end(), segment));
if(attributes.find(segment) != attributes.end())
attributes.erase(segment);
}
//cody
bool PlaneSweep::pred_exists(Segment2D segment)
{
if(sweepStatus.empty() || sweepStatus[0] == segment)
return false;
else
return true;
}
//cody
bool PlaneSweep::pred_of_p_exists(SimplePoint2D point)
{
if(sweepStatus.empty()) {
return false;
}
else {
SimplePoint2D left = sweepStatus[0].leftEndPoint;
SimplePoint2D right = sweepStatus[0].rightEndPoint;
// handle infinite slope
if (right.x == left.x)
return point.y > left.y;
Number slope = (right.y - left.y) / (right.x - left.x);
Number b = left.y - (slope * left.x);
Number SegY = (point.x * slope) + b;
return point.y > SegY;
}
}
//cody
Segment2D PlaneSweep::pred_of_p(SimplePoint2D point)
{
Segment2D returnSeg;
for(int i = 0; i < sweepStatus.size(); i++)
{
if(pointGreaterThan(point, sweepStatus[i]))
returnSeg = sweepStatus[i];
else
break;
}
return returnSeg;
}
// Sam and Cody (I think we both independently did this)
bool PlaneSweep::poi_on_seg(SimplePoint2D point)
{
for(int i = 0; i < sweepStatus.size(); i++)
if(sweepStatus[i].poiOnSeg(point))
return true;
return false;
}
bool PlaneSweep::get_attr(Segment2D segment)
{
bool attr = attributes[segment];
return attr;
}
void PlaneSweep::set_attr(Segment2D segment, bool attr)
{
attributes[segment] = attr;
}
bool PlaneSweep::get_pred_attr(SimplePoint2D point)
{
Segment2D seg = pred_of_p(point);
return attributes[seg];
}
// Sam
bool PlaneSweep::look_ahead(HalfSegment2D half, std::vector<HalfSegment2D> halfSegs)
{
for (int i = 0; i < halfSegs.size() - 1; i++)
{
if (halfSegs[i] == half)
{
if (halfSegs[i].getDP() == halfSegs[i+1].getDP())
{
return true;
}
}
}
return false;
}
// Sam
bool PlaneSweep::look_ahead_3(AttributedHalfSegment2D ahs, std::vector<AttributedHalfSegment2D> segments)
{
for (int i = 0; i < segments.size() - 1; i++)
{
if (segments[i] == ahs)
{
if (segments[i].hs.getDP() == segments[i+1].hs.getDP())
{
return true;
}
}
}
return false;
}
/*
for (int i = 0; i < halfSegs.size(); i++)
{
if (halfSegs[i] == half)
{
if (halfSegs[i].getDP() == halfSegs[i+1].getDP())
{
return true;
}
}
}
return false;
*/
// Region Region Functions
// Douglas
std::pair<int, int> PlaneSweep::get_attr_2(Segment2D segment)
{
return attributes2[segment];
}
// Douglas
void PlaneSweep::set_attr_2(Segment2D segment, std::pair<int, int> p)
{
attributes2[segment] = p;
}
// Douglas
std::pair<int, int> PlaneSweep::get_pred_attr_2(Segment2D segment)
{
for(int i = 0; i < sweepStatus.size() - 1; ++i)
if(sweepStatus[i + 1] == segment)
return get_attr_2(sweepStatus[i]);
return std::make_pair(0, 0);
}
// Douglas
AttributedHalfSegment2D PlaneSweep::look_ahead_2(AttributedHalfSegment2D ahs, std::vector<AttributedHalfSegment2D> segments)
{
for (int i = 0; i < segments.size() - 1; ++i)
if (segments[i] == ahs)
{
AttributedHalfSegment2D ans = segments[i + 1];
/*if(ans.hs.isDominatingPointLeft)
return ans.hs.s.leftEndPoint;
else
return ans.hs.s.rightEndPoint;*/
return ans;
}
// if it was not found in the for loop, it must be the last segment
// if last segment just return its own point as it is checked before the look_ahead call anyway so will not impact the result.
/*if(ahs.hs.isDominatingPointLeft)
return ahs.hs.s.leftEndPoint;
else
return ahs.hs.s.rightEndPoint;*/
return ahs;
}
// Douglas
bool PlaneSweep::coincident_line(Segment2D segment, std::queue<HalfSegment2D> &eventPointsDynamicObj1, std::queue<HalfSegment2D> &eventPointsDynamicObj2)
{
for(int i = 0; i < sweepStatus.size(); ++i)
{
if(sweepStatus[i] == segment) // technically cannot intersect with a segment from its own object
{
if(i > 0) //check intersect with sweepStatus[i - 1]
if((segment.findIntersection_excludeEndpoints(sweepStatus[i - 1])).first)
{
SimplePoint2D iPoint = segment.findIntersection_excludeEndpoints(sweepStatus[i - 1]).second;
Segment2D seg1 = Segment2D(iPoint, segment.rightEndPoint);
Segment2D seg2 = Segment2D(iPoint, sweepStatus[i - 1].rightEndPoint);
HalfSegment2D hs1 = HalfSegment2D(seg1, false);
HalfSegment2D hs2 = HalfSegment2D(seg1, true);
HalfSegment2D hs3 = HalfSegment2D(seg2, false);
HalfSegment2D hs4 = HalfSegment2D(seg2, true);
if(objects[segment] == 1)
{
eventPointsDynamicObj1.push(hs1);
eventPointsDynamicObj1.push(hs2);
eventPointsDynamicObj2.push(hs3);
eventPointsDynamicObj2.push(hs4);
}
else
{
eventPointsDynamicObj2.push(hs1);
eventPointsDynamicObj2.push(hs2);
eventPointsDynamicObj1.push(hs3);
eventPointsDynamicObj1.push(hs4);
}
return true;
}
else if(i < sweepStatus.size() - 1) //check intersect with sweepStatus[i + 1]
if((segment.findIntersection_excludeEndpoints(sweepStatus[i + 1])).first)
{
SimplePoint2D iPoint = segment.findIntersection_excludeEndpoints(sweepStatus[i + 1]).second;
Segment2D seg1 = Segment2D(iPoint, segment.rightEndPoint);
Segment2D seg2 = Segment2D(iPoint, sweepStatus[i + 1].rightEndPoint);
HalfSegment2D hs1 = HalfSegment2D(seg1, false);
HalfSegment2D hs2 = HalfSegment2D(seg1, true);
HalfSegment2D hs3 = HalfSegment2D(seg2, false);
HalfSegment2D hs4 = HalfSegment2D(seg2, true);
if(objects[segment] == 1)
{
eventPointsDynamicObj1.push(hs1);
eventPointsDynamicObj1.push(hs2);
eventPointsDynamicObj2.push(hs3);
eventPointsDynamicObj2.push(hs4);
}
else
{
eventPointsDynamicObj2.push(hs1);
eventPointsDynamicObj2.push(hs2);
eventPointsDynamicObj1.push(hs3);
eventPointsDynamicObj1.push(hs4);
}
return true;
}
}
}
return false;
}
// Douglas
bool PlaneSweep::coincident(Segment2D segment, std::queue<AttributedHalfSegment2D> &eventPointsDynamicObj1, std::queue<AttributedHalfSegment2D> &eventPointsDynamicObj2)
{
for(int i = 0; i < sweepStatus.size(); ++i)
{
if(sweepStatus[i] == segment) // technically cannot intersect with a segment from its own object
{
if(i > 0) //check intersect with sweepStatus[i - 1]
if((segment.findIntersection_excludeEndpoints(sweepStatus[i - 1])).first)
{
SimplePoint2D iPoint = segment.findIntersection_excludeEndpoints(sweepStatus[i - 1]).second;
Segment2D seg1 = Segment2D(iPoint, segment.rightEndPoint);
Segment2D seg2 = Segment2D(iPoint, sweepStatus[i - 1].rightEndPoint);
HalfSegment2D hs1 = HalfSegment2D(seg1, false);
HalfSegment2D hs2 = HalfSegment2D(seg1, true);
HalfSegment2D hs3 = HalfSegment2D(seg2, false);
HalfSegment2D hs4 = HalfSegment2D(seg2, true);
AttributedHalfSegment2D ahs1 = AttributedHalfSegment2D(hs1, attributes[segment]);
AttributedHalfSegment2D ahs2 = AttributedHalfSegment2D(hs2, attributes[segment]);
AttributedHalfSegment2D ahs3 = AttributedHalfSegment2D(hs3, attributes[sweepStatus[i - 1]]);
AttributedHalfSegment2D ahs4 = AttributedHalfSegment2D(hs4, attributes[sweepStatus[i - 1]]);
if(objects[segment] == 1)
{
eventPointsDynamicObj1.push(ahs1);
eventPointsDynamicObj1.push(ahs2);
eventPointsDynamicObj2.push(ahs3);
eventPointsDynamicObj2.push(ahs4);
}
else
{
eventPointsDynamicObj2.push(ahs1);
eventPointsDynamicObj2.push(ahs2);
eventPointsDynamicObj1.push(ahs3);
eventPointsDynamicObj1.push(ahs4);
}
return true;
}
else if(i < sweepStatus.size() - 1) //check intersect with sweepStatus[i + 1]
if((segment.findIntersection_excludeEndpoints(sweepStatus[i + 1])).first)
{
SimplePoint2D iPoint = segment.findIntersection_excludeEndpoints(sweepStatus[i + 1]).second;
Segment2D seg1 = Segment2D(iPoint, segment.rightEndPoint);
Segment2D seg2 = Segment2D(iPoint, sweepStatus[i + 1].rightEndPoint);
HalfSegment2D hs1 = HalfSegment2D(seg1, false);
HalfSegment2D hs2 = HalfSegment2D(seg1, true);
HalfSegment2D hs3 = HalfSegment2D(seg2, false);
HalfSegment2D hs4 = HalfSegment2D(seg2, true);
AttributedHalfSegment2D ahs1 = AttributedHalfSegment2D(hs1, attributes[segment]);
AttributedHalfSegment2D ahs2 = AttributedHalfSegment2D(hs2, attributes[segment]);
AttributedHalfSegment2D ahs3 = AttributedHalfSegment2D(hs3, attributes[sweepStatus[i + 1]]);
AttributedHalfSegment2D ahs4 = AttributedHalfSegment2D(hs4, attributes[sweepStatus[i + 1]]);
if(objects[segment] == 1)
{
eventPointsDynamicObj1.push(ahs1);
eventPointsDynamicObj1.push(ahs2);
eventPointsDynamicObj2.push(ahs3);
eventPointsDynamicObj2.push(ahs4);
}
else
{
eventPointsDynamicObj2.push(ahs1);
eventPointsDynamicObj2.push(ahs2);
eventPointsDynamicObj1.push(ahs3);
eventPointsDynamicObj1.push(ahs4);
}
return true;
}
}
}
return false;
}