1 #ifndef vgl_convex_hull_2d_hxx_ 2 #define vgl_convex_hull_2d_hxx_ 10 # include <vcl_msvc_warnings.h> 13 #include <vnl/vnl_math.h> 43 static void print_hull(
double **P,
int m)
45 for (
int i=0; i<m; i++)
46 std::cout << (P[i]-points[0])/2) <<
' ';
47 std::cout << std::endl;
51 static int ccw(
double **P,
int i,
int j,
int k)
53 double a = P[i][0] - P[j][0],
54 b = P[i][1] - P[j][1],
55 c = P[k][0] - P[j][0],
56 d = P[k][1] - P[j][1];
57 return a*d - b*c <= 0;
62 v = (*(double*const*)(A))[c] - (*(double*const*)(B))[c];\ 66 static int cmpl(
const void *a,
const void *b)
75 static int cmph(
const void *a,
const void *b) {
return cmpl(b,a);}
78 static int make_chain(
double** V,
int n,
int (*cmp)(
const void*,
const void*))
80 std::qsort(V, n,
sizeof(
double*), cmp);
82 for (
int i=2; i<n; i++) {
83 while (s>=1 && ccw(V, i, s, s-1)) --s;
85 double* t = V[s]; V[s] = V[i]; V[i] = t;
90 static inline int ch2d(
double **P,
int n)
92 int u = make_chain(P, n, cmpl);
95 return u+make_chain(P+u, n-u+1, cmph);
110 int N = points_.size();
114 double * array =
new double[2*N];
115 double** points =
new double*[N];
116 double** P =
new double*[N+1];
117 for (
int i = 0; i<N; i++)
118 points[i]=&array[2*i];
120 for (
int n = 0; n<N; n++)
122 points[n][0]=(double)points_[n].x();
123 points[n][1]=(double)points_[n].y();
124 P[n] = &points[n][0];
127 int n_hull = ch2d(P, N);
130 std::vector<vgl_point_2d<T> > temp;
131 for (
int i = 0; i<n_hull; i++)
137 if (P[0][0] != P[n_hull][0] || P[0][1] != P[n_hull][1])
153 this->compute_hull();
159 this->compute_hull();
162 std::vector<vgl_point_2d<T> > verts = hull_[0];
163 size_t n = verts.size();
166 T min_area = std::numeric_limits<T>::max();
168 for(
size_t i = 1; i<=n; ++i){
171 T theta = atan2(dir.
y(), dir.
x());
172 T c = cos(-theta), s=sin(-theta);
176 for(
size_t j = 0; j<n; ++j){
183 min_offset.
set(vm1.x(), vm1.y());
192 T width = w, height = h;
197 pmaj0.
set(c.
x(), c.
y()-width/T(2)); pmaj1.set(c.
x(), c.
y()+width/T(2));
201 T cs = cos(min_angle), s = sin(min_angle);
202 T pmaj0x = pmaj0.x(), pmaj0y = pmaj0.y();
203 T pmaj1x = pmaj1.x(), pmaj1y = pmaj1.y();
208 pmaj0r += min_offset; pmaj1r += min_offset;
212 #undef VGL_CONVEX_HULL_2D_INSTANTIATE 213 #define VGL_CONVEX_HULL_2D_INSTANTIATE(T) \ 216 template class vgl_convex_hull_2d<T > 218 #endif // vgl_convex_hull_2d_hxx_ Direction vector in Euclidean 2D space, templated by type of element.
Compute the convex hull of a 2-d point set.
vgl_convex_hull_2d()=delete
void set(T vx, T vy)
Assignment.
Type width() const
Get width of this box (= x dimension).
vgl_oriented_box_2d< T > min_area_enclosing_rectangle()
the oriented box enclosing the hull with minimum area.
void set(Type px, Type py)
Set x and y.
Contains class to represent a cartesian 2D bounding box.
vgl_point_2d< Type > centroid() const
Get the centroid point.
Type height() const
Get height of this box (= y dimension).
void add(vgl_point_2d< Type > const &p)
Add a point to this box.
T vgl_area(vgl_polygon< T > const &poly)
The area of a polygon.