[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

polygon.hxx
1 /************************************************************************/
2 /* */
3 /* Copyright 1998-2010 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35 
36 #ifndef VIGRA_POLYGON_HXX
37 #define VIGRA_POLYGON_HXX
38 
39 #include <cmath>
40 #include <cstdlib>
41 #include <iterator>
42 #include <algorithm>
43 #include "config.hxx"
44 #include "error.hxx"
45 #include "array_vector.hxx"
46 
47 namespace vigra {
48 
49 /** \addtogroup MathFunctions
50 */
51 //@{
52 
53 namespace detail {
54 
55 template<class Point>
56 struct CCWCompare
57 {
58  Point p0_;
59  CCWCompare(const Point &p0)
60  : p0_(p0)
61  {}
62 
63  bool operator()(const Point &a, const Point &b) const
64  {
65  return (a[1]-p0_[1])*(b[0]-p0_[0]) - (a[0]-p0_[0])*(b[1]-p0_[1]) < 0;
66  }
67 };
68 
69 } // namespace detail
70 
71 
72 /** \brief Compute convex hull of a 2D polygon.
73 
74  The input array \a points contains a (not necessarily ordered) set of 2D points
75  whose convex hull is to be computed. The array's <tt>value_type</tt> (i.e. the point type)
76  must be compatible with std::vector (in particular, it must support indexing,
77  copying, and have <tt>size() == 2</tt>). The points of the convex hull will be appended
78  to the output array \a convex_hull (which must support <tt>std::back_inserter(convex_hull)</tt>).
79  Since the convex hull is a closed polygon, the first and last point of the output will
80  be the same (i.e. the first point will simply be inserted at the end again). The points
81  of the convex hull will be ordered counter-clockwise, starting with the leftmost point
82  of the imput.
83 */
84 template<class PointArray1, class PointArray2>
86  const PointArray1 &points, PointArray2 &convex_hull)
87 {
88  vigra_precondition(points.size() >= 2,
89  "convexHull(): at least two input points are needed.");
90  vigra_precondition(points[0].size() == 2,
91  "convexHull(): 2-dimensional points required.");
92 
93  typedef typename PointArray1::value_type Point;
94  typedef typename Point::value_type Coordinate;
95 
96  // find extremal point (min. x, then min. y):
97  unsigned int i0 = 0;
98  Point p0 = points[0];
99  for(unsigned int i = 1; i < points.size(); ++i)
100  {
101  Coordinate xDiff = points[i][0] - p0[0];
102  if(xDiff < 0 || (xDiff == 0 && points[i][1] < p0[1]))
103  {
104  p0 = points[i];
105  i0 = i;
106  }
107  }
108 
109  // sort other points by angle from p0:
110  ArrayVector<Point> other(points.begin(), points.begin() + i0);
111  other.insert(other.end(), points.begin()+i0+1, points.end());
112 
113  // the current definition of CCWCompare ensures that points identical to p0
114  // end up at the end of the list => those duplicates will be removed during
115  // Graham scan
116  std::sort(other.begin(), other.end(), detail::CCWCompare<Point>(p0));
117 
118  ArrayVector<Point> result(points.size()+1);
119  result[0] = p0;
120  result[1] = other[0];
121 
122  typename ArrayVector<Point>::iterator currentEnd = result.begin() + 1;
123 
124  // Graham's scan:
125  Point endSegment = *currentEnd - currentEnd[-1];
126  Coordinate sa2;
127  for(unsigned int i = 1; i < other.size(); ++i)
128  {
129  if(other[i] == other[i-1] || other[i] == p0) // skip duplicate points
130  continue;
131  do
132  {
133  Point diff = other[i] - currentEnd[-1];
134  sa2 = diff[0]*endSegment[1] - endSegment[0]*diff[1];
135  if(sa2 < 0)
136  {
137  // point is to the left, add to convex hull:
138  *(++currentEnd) = other[i];
139  endSegment = other[i] - currentEnd[-1];
140  }
141  else if(sa2 == 0)
142  {
143  // points are collinear, keep far one:
144  if(diff.squaredMagnitude() > endSegment.squaredMagnitude())
145  {
146  *currentEnd = other[i];
147  endSegment = diff;
148  }
149  }
150  else
151  {
152  // point is to the right, backtracking needed:
153  --currentEnd;
154  endSegment = *currentEnd - currentEnd[-1];
155  }
156  }
157  while(sa2 > 0);
158  }
159 
160  // return closed Polygon:
161  *(++currentEnd) = p0;
162  ++currentEnd;
163  std::copy(result.begin(), currentEnd, std::back_inserter(convex_hull));
164 }
165 
166 //@}
167 
168 } // namespace vigra
169 
170 #endif /* VIGRA_POLYGON_HXX */

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.7.1 (Wed Mar 12 2014)