forked from matalek/eth-dphpc-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_hull.hh
148 lines (122 loc) · 4.92 KB
/
merge_hull.hh
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
#ifndef MERGE_HULL
#define MERGE_HULL
#include <vector>
#include "geometric_helpers.hh"
#include <stdio.h>
#include <algorithm>
#include "data_structures/convex_hull_representation.hh"
#include "data_structures/representation_iterator.hh"
using namespace std;
int turn_right(POINT *a, POINT *b, POINT *c) {
return DetPointer(a, b, c) < 0;
}
int turn_left(POINT *a, POINT *b, POINT *c) {
return DetPointer(a, b, c) > 0;
}
int left_lower_tangent(shared_ptr<RepresentationIterator> iterator, POINT *b) {
POINT *a = iterator->get_point();
POINT *u = iterator->get_point_counter_clockwise();
POINT *w = iterator->get_point_clockwise();
return turn_right(b, a, w) && !turn_left(b, a, u);
}
int left_upper_tangent(shared_ptr<RepresentationIterator> iterator, POINT *b) {
POINT *a = iterator->get_point();
POINT *u = iterator->get_point_counter_clockwise();
POINT *w = iterator->get_point_clockwise();
return turn_left(b, a, u) && !turn_right(b, a, w);
}
int lowerTangentB(shared_ptr<RepresentationIterator> iterator, POINT *a) {
POINT *u = iterator->get_point_counter_clockwise();
POINT *w = iterator->get_point_clockwise();
POINT *b = iterator->get_point();
return !turn_right(a, b, w) && turn_left(a, b, u);
}
int right_upper_tangent(shared_ptr<RepresentationIterator> iterator, POINT *a) {
POINT *b = iterator->get_point();
POINT *u = iterator->get_point_counter_clockwise();
POINT *w = iterator->get_point_clockwise();
return !turn_left(a, b, u) && turn_right(a, b, w);
}
shared_ptr<RepresentationIterator>
binary_right_upper_tangent(ConvexHullRepresentation &hull, shared_ptr<RepresentationIterator> right_iter, int to, POINT *P) {
int a, b, c;
int dnC;
if (right_upper_tangent(right_iter, P))
return right_iter;
a = right_iter->get_index();
b = to;
while (true) {
c = (a + b) / 2;
shared_ptr<RepresentationIterator> iterator = hull.get_iterator(c);
dnC = turn_right(P, iterator->get_point(), iterator->get_point_clockwise());
if (dnC && !turn_left(P, hull.get_point(c), iterator->get_point_counter_clockwise())) {
return iterator;
} else {
if (dnC) {
b = c;
} else {
a = c;
}
}
}
}
shared_ptr<RepresentationIterator>
binary_left_upper_tangent(ConvexHullRepresentation &hull, shared_ptr<RepresentationIterator> left_iter, int to, POINT *P) {
int a, b, c;
int dnC;
if (left_upper_tangent(left_iter, P))
return left_iter;
a = left_iter->get_index();
b = to;
while (true) {
c = (a + b) / 2;
shared_ptr<RepresentationIterator> iterator = hull.get_iterator(c);
dnC = turn_left(P, hull.get_point(c), iterator->get_point_counter_clockwise());
if (dnC && !turn_right(P, iterator->get_point(), iterator->get_point_clockwise())) {
return iterator;
} else {
if (dnC) {
b = c;
} else {
a = c;
}
}
}
}
void find_left_upper_tangent(shared_ptr<RepresentationIterator> iterator, POINT *p) {
while (!left_upper_tangent(iterator, p)) {
iterator->go_counter_clockwise();
}
}
void find_right_upper_tangent(shared_ptr<RepresentationIterator> iterator, POINT *p) {
while (!right_upper_tangent(iterator, p)) {
iterator->go_clockwise();
}
}
pair<int, int> findLowerT(ConvexHullRepresentation &left_hull, ConvexHullRepresentation &right_hull) {
pair<int, int> return_values;
shared_ptr<RepresentationIterator> left_iter = left_hull.get_iterator(left_hull.find_rightmost_point());
shared_ptr<RepresentationIterator> right_iter = right_hull.get_iterator(right_hull.find_leftmost_point());
while (!(right_upper_tangent(left_iter, right_iter->get_point()) &&
lowerTangentB(right_iter, left_iter->get_point()))) {
find_right_upper_tangent(left_iter, right_iter->get_point());
find_left_upper_tangent(right_iter, left_iter->get_point());
}
return_values.first = left_iter->get_index();
return_values.second = right_iter->get_index();
return return_values;
}
pair<int, int> findUpperT(ConvexHullRepresentation &left_hull, ConvexHullRepresentation &right_hull) {
pair<int, int> return_values;
shared_ptr<RepresentationIterator> left_iter = left_hull.get_iterator(left_hull.find_rightmost_point());
shared_ptr<RepresentationIterator> right_iter = right_hull.get_iterator(right_hull.find_leftmost_point());
while (!(left_upper_tangent(left_iter, right_iter->get_point()) &&
right_upper_tangent(right_iter, left_iter->get_point()))) {
find_left_upper_tangent(left_iter, right_iter->get_point());
find_right_upper_tangent(right_iter, left_iter->get_point());
}
return_values.first = left_iter->get_index();
return_values.second = right_iter->get_index();
return return_values;
}
#endif // MERGE_HULL