-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path2DConvexHull.h
196 lines (167 loc) · 5.04 KB
/
2DConvexHull.h
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
//-------------------------------------------------------------------
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files ( the "Software" ), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall
// be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
//-------------------------------------------------------------------
//-------------------------------------------------------------------
#ifndef TWODCONVEXHULL
#define TWODCONVEXHULL 1
#include <iostream>
#include <stack>
#include <vector>
#include <stdlib.h>
#include "vec3.h"
struct Point
{
double x, y;
int index;
};
// A utility function to find next to top in a stack
Point nextToTop(std::stack<Point> &S)
{
Point p = S.top();
S.pop();
Point res = S.top();
S.push(p);
return res;
}
void swap(Point &p1, Point &p2)
{
Point temp = p1;
p1 = p2;
p2 = temp;
}
double distSq(const Point& p1, const Point& p2)
{
return (p1.x - p2.x) * (p1.x - p2.x) +
(p1.y - p2.y) * (p1.y - p2.y);
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 -> p, q and r are colinear
// 1 -> Clockwise
// 2 -> Counterclockwise
int orientation(const Point& p, const Point& q, const Point& r)
{
int val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // colinear
// TODO: make enum or consts...
return (val > 0) ? 1 : 2; // clock or counterclock wise
}
// A function used by library function qsort() to sort an array of
// points with respect to the first point
int compare(const void *vp1, const void *vp2)
{
// TODO: C-style cast...
Point *p1 = (Point *)vp1;
Point *p2 = (Point *)vp2;
Point p0; p0.x=0; p0.y=0;
// Find orientation
int o = orientation(p0, *p1, *p2);
if (o == 0)
return (distSq(p0, *p2) >= distSq(p0, *p1))? -1 : 1;
return (o == 2)? -1: 1;
}
// Prints convex hull of a set of n points.
// TODO: points gets overwritten...
std::stack<Point> convexHull(std::vector<Point>& points)
{
const int n = points.size();
// Find the bottommost point
int ymin = points[0].y, min = 0;
for (int i = 1; i < n; i++)
{
int y = points[i].y;
// Pick the bottom-most or chose the left
// most point in case of tie
if ( (y < ymin) || (ymin == y && points[i].x < points[min].x) )
ymin = points[i].y, min = i;
}
// Place the bottom-most point at first position
std::swap(points[0], points[min]);
// Sort n-1 points with respect to the first point.
Point p0;
for(int i = 1; i < n; i++)
{
points[i].x -= points[0].x;
points[i].y -= points[0].y;
}
p0.x = points[0].x = 0;
p0.y = points[0].y = 0;
qsort(&points[1], n-1, sizeof(Point), compare);
// Process first 3 points
// If two or more points make same angle with p0,
// Remove all but the one that is farthest from p0
int m = 1;
for (int i = 1; i < n; i++)
{
while ( (i < n-1) && orientation(p0, points[i], points[i+1]) == 0 )
i++;
points[m] = points[i];
m++; // Update size of modified array
}
std::stack<Point> S;
if (m < 3) return S;
S.push(points[0]);
S.push(points[1]);
S.push(points[2]);
// Process remaining n-3 points
for (int i = 3; i < m; i++)
{
// Keep removing top while the angle formed by
// points next-to-top, top, and points[i] makes
// a non-left turn
while (orientation(nextToTop(S), S.top(), points[i]) != 2)
S.pop();
S.push(points[i]);
}
return S;
}
// dim refers to the dimension that needs to be ignored
std::vector<int> getConvexHull(const std::vector<vec3> &inPoints, const int dim)
{
std::vector<Point> myPoints(inPoints.size());
for(int i = 0; i < inPoints.size(); i++)
{
int index = 0;
for(int j = 0; j < 3; j++)
{
if (j == dim) continue;
if (index == 0) myPoints[i].x = inPoints[i][j];
if (index == 1) myPoints[i].y = inPoints[i][j];
index++;
}
myPoints[i].index = i;
}
// TODO: myPoints gets overwritten...
auto myStack = convexHull(myPoints);
std::vector<int> ret;
while (! myStack.empty() )
{
Point p = myStack.top();
ret.push_back(p.index);
myStack.pop();
}
return ret;
}
#endif