KiCad PCB EDA Suite
Loading...
Searching...
No Matches
convex_hull.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22/*
23 * Implementation of Andrew's monotone chain 2D convex hull algorithm.
24 * Asymptotic complexity: O(n log n).
25 * See http://www.algorithmist.com/index.php/Monotone_Chain_Convex_Hull
26 * (Licence GNU Free Documentation License 1.2)
27 *
28 * Pseudo-code:
29 *
30 * Input: a list P of points in the plane.
31 *
32 * Sort the points of P by x-coordinate (in case of a tie, sort by y-coordinate).
33 *
34 * Initialize U and L as empty lists.
35 * The lists will hold the vertices of upper and lower hulls respectively.
36 *
37 * for i = 1, 2, ..., n:
38 * while L contains at least two points and the sequence of last two points
39 * of L and the point P[i] does not make a counter-clockwise turn:
40 * remove the last point from L
41 * append P[i] to L
42 *
43 * for i = n, n-1, ..., 1:
44 * while U contains at least two points and the sequence of last two points
45 * of U and the point P[i] does not make a counter-clockwise turn:
46 * remove the last point from U
47 * append P[i] to U
48 *
49 * Remove the last point of each list (it's the same as the first point of the other list).
50 * Concatenate L and U to obtain the convex hull of P.
51 * Points in the result will be listed in counter-clockwise order.
52 */
53
55#include <geometry/shape_line_chain.h> // for SHAPE_LINE_CHAIN
57#include <math/vector2d.h> // for VECTOR2I
58#include <trigo.h>
59#include <algorithm>
60
61
62typedef long long coord2_t; // must be big enough to hold 2*max(|coordinate|)^2
63
64// this function is used to sort points.
65// Andrew's monotone chain 2D convex hull algorithm needs a sorted set of points
66static bool compare_point( const VECTOR2I& ref, const VECTOR2I& p )
67{
68 return ref.x < p.x || (ref.x == p.x && ref.y < p.y);
69}
70
71
72// 2D cross product of OA and OB vectors, i.e. z-component of their 3D cross product.
73// Returns a positive value, if OAB makes a counter-clockwise turn,
74// negative for clockwise turn, and zero if the points are collinear.
75static coord2_t cross_product( const VECTOR2I& O, const VECTOR2I& A, const VECTOR2I& B )
76{
77 return (coord2_t) (A.x - O.x) * (coord2_t) (B.y - O.y)
78 - (coord2_t) (A.y - O.y) * (coord2_t) (B.x - O.x);
79}
80
81
82// Fills aResult with a list of points on the convex hull in counter-clockwise order.
83void BuildConvexHull( std::vector<VECTOR2I>& aResult, const std::vector<VECTOR2I>& aPoly )
84{
85 std::vector<VECTOR2I> poly = aPoly;
86 int point_count = poly.size();
87
88 if( point_count < 2 ) // Should not happen, but who know
89 return;
90
91 // Sort points lexicographically
92 // Andrew's monotone chain 2D convex hull algorithm needs that
93 std::sort( poly.begin(), poly.end(), compare_point );
94
95 int k = 0;
96
97 // Store room (2 * n points) for result:
98 // The actual convex hull use less points. the room will be adjusted later
99 aResult.resize( 2 * point_count );
100
101 // Build lower hull
102 for( int ii = 0; ii < point_count; ++ii )
103 {
104 while( k >= 2 && cross_product( aResult[k - 2], aResult[k - 1], poly[ii] ) <= 0 )
105 k--;
106
107 aResult[k++] = poly[ii];
108 }
109
110 // Build upper hull
111 for( int ii = point_count - 2, t = k + 1; ii >= 0; ii-- )
112 {
113 while( k >= t && cross_product( aResult[k - 2], aResult[k - 1], poly[ii] ) <= 0 )
114 k--;
115
116 aResult[k++] = poly[ii];
117 }
118
119 // The last point in the list is the same as the first one.
120 // This is not needed, and sometimes create issues ( 0 length polygon segment:
121 // remove it:
122
123 if( k > 1 && aResult[0] == aResult[k - 1] )
124 k -= 1;
125
126 aResult.resize( k );
127}
128
129
130void BuildConvexHull( std::vector<VECTOR2I>& aResult, const SHAPE_POLY_SET& aPolygons )
131{
132 BuildConvexHull( aResult, aPolygons, VECTOR2I( 0, 0 ), ANGLE_0 );
133}
134
135
136void BuildConvexHull( std::vector<VECTOR2I>& aResult, const SHAPE_POLY_SET& aPolygons,
137 const VECTOR2I& aPosition, const EDA_ANGLE& aRotation )
138{
139 // Build the convex hull of the SHAPE_POLY_SET
140 std::vector<VECTOR2I> buf;
141
142 for( int cnt = 0; cnt < aPolygons.OutlineCount(); cnt++ )
143 {
144 const SHAPE_LINE_CHAIN& poly = aPolygons.COutline( cnt );
145
146 for( int ii = 0; ii < poly.PointCount(); ++ii )
147 buf.emplace_back( poly.CPoint( ii ).x, poly.CPoint( ii ).y );
148 }
149
150 BuildConvexHull( aResult, buf );
151
152 // Move and rotate the points according to aPosition and aRotation
153
154 for( unsigned ii = 0; ii < aResult.size(); ii++ )
155 {
156 RotatePoint( aResult[ii], aRotation );
157 aResult[ii] += aPosition;
158 }
159}
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a set of closed polygons.
int OutlineCount() const
Return the number of outlines in the set.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
static coord2_t cross_product(const VECTOR2I &O, const VECTOR2I &A, const VECTOR2I &B)
static bool compare_point(const VECTOR2I &ref, const VECTOR2I &p)
long long coord2_t
void BuildConvexHull(std::vector< VECTOR2I > &aResult, const std::vector< VECTOR2I > &aPoly)
Calculate the convex hull of a list of points in counter-clockwise order.
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:225
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683