vigra - vigra/polygon.hxx Source File

[ 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 bool sortPoints(Point const & p1, Point const & p2)
57 {
58 return (p1[0]<p2[0]) || (p1[0] == p2[0] && p1[1] < p2[1]);
59 }
60
61 template < class Point >
62 bool orderedClockwise(const Point &O, const Point &A, const Point &B)
63 {
64 return (A[0] - O[0]) * (B[1] - O[1]) - (A[1] - O[1]) * (B[0] - O[0]) <= 0;
65 }
66
67 } // namespace detail
68
69
70 /** \brief Compute convex hull of a 2D polygon.
71
72 The input array \a points contains a (not necessarily ordered) set of 2D points
73 whose convex hull is to be computed. The array's <tt>value_type</tt> (i.e. the point type)
74 must be compatible with std::vector (in particular, it must support indexing,
75 copying, and have <tt>size() == 2</tt>). The points of the convex hull will be appended
76 to the output array \a convex_hull (which must support <tt>std::back_inserter(convex_hull)</tt>).
77 Since the convex hull is a closed polygon, the first and last point of the output will
78 be the same (i.e. the first point will simply be inserted at the end again). The points
79 of the convex hull will be ordered counter-clockwise, starting with the leftmost point
80 of the input. The function implements Andrew's Monotone Chain algorithm.
81 */
82 template<class PointArray1, class PointArray2>
83 void convexHull(const PointArray1 &points, PointArray2 & convex_hull)
84 {
85 vigra_precondition(points.size() >= 2,
86 "convexHull(): at least two input points are needed.");
87 vigra_precondition(points[0].size() == 2,
88 "convexHull(): 2-dimensional points required.");
89
90 typedef typename PointArray1::value_type Point;
91
92 ArrayVector<Point> ordered(points.begin(), points.end());
93 std::sort(ordered.begin(), ordered.end(), detail::sortPoints<Point>);
94
95 ArrayVector<Point> H;
96
97 int n = points.size(), k=0;
98
99 // Build lower hull
100 for (int i = 0; i < n; i++)
101 {
102 while (k >= 2 && detail::orderedClockwise(H[k-2], H[k-1], ordered[i]))
103 {
104 H.pop_back();
105 --k;
106 }
107 H.push_back(ordered[i]);
108 ++k;
109 }
110
111 // Build upper hull
112 for (int i = n-2, t = k+1; i >= 0; i--)
113 {
114 while (k >= t && detail::orderedClockwise(H[k-2], H[k-1], ordered[i]))
115 {
116 H.pop_back();
117 --k;
118 }
119 H.push_back(ordered[i]);
120 ++k;
121 }
122
123 std::copy(H.begin(), H.begin()+k, std::back_inserter(convex_hull));
124 }
125
126 //@}
127
128 } // namespace vigra
129
130 #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.10.0 (Mon Nov 18 2013)