forked from DGtal-team/DGtal
/
exampleConvexHull2D.cpp
369 lines (311 loc) · 12.5 KB
/
exampleConvexHull2D.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
**/
/**
* @file geometry/tools/exampleConvexHull2D.cpp
* @ingroup Examples
* @author Tristan Roussillon (\c tristan.roussillon@liris.cnrs.fr )
* Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
*
* @date 2013/12/17
*
* An example file named exampleConvexHull2D.
*
* This file is part of the DGtal library.
*/
/**
Computation of the convex hull of a planar point set by different algorithms:
- functions::Hull2D::andrewConvexHullAlgorithm \cite Andrew1979
- functions::Hull2D::grahamConvexHullAlgorithm \cite Graham1972
- functions::Hull2D::melkmanConvexHullAlgorithm \cite Melkman1987
@see \ref moduleHull2D
\example geometry/tools/exampleConvexHull2D.cpp
*/
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/base/IteratorCirculatorTraits.h"
#include "DGtal/helpers/StdDefs.h"
//! [Hull2D-Include]
#include "DGtal/geometry/tools/Hull2DHelpers.h"
//! [Hull2D-Include]
#include "DGtal/geometry/tools/PolarPointComparatorBy2x2DetComputer.h"
#include "DGtal/geometry/tools/determinant/AvnaimEtAl2x2DetSignComputer.h"
#include "DGtal/geometry/tools/determinant/InHalfPlaneBySimple3x3Matrix.h"
#include "DGtal/shapes/ShapeFactory.h"
#include "DGtal/shapes/Shapes.h"
#include "DGtal/topology/DigitalSetBoundary.h"
#include "DGtal/topology/DigitalSurface.h"
#include "DGtal/graph/DepthFirstVisitor.h"
#include "DGtal/io/boards/Board2D.h"
///////////////////////////////////////////////////////////////////////////////
using namespace std;
using namespace DGtal;
///////////////////////////////////////////////////////////////////////////////
/*
* @brief Procedure that draws on a board a polygon given by a range of vertices.
* @param itb begin iterator
* @param ite end iterator
* @param aBoard any board
* @param isClosed bool equal to 'true' for closed polygon, 'false' otherwise.
* @tparam ForwardIterator at least a readable and forward iterator
* @tparam Board equivalent to Board2D
*/
template <typename ForwardIterator, typename Board>
void drawPolygon(const ForwardIterator& itb, const ForwardIterator& ite,
Board& aBoard, bool isClosed = true)
{
BOOST_CONCEPT_ASSERT(( boost_concepts::ForwardTraversalConcept<ForwardIterator> ));
BOOST_CONCEPT_ASSERT(( boost_concepts::ReadableIteratorConcept<ForwardIterator> ));
typedef typename IteratorCirculatorTraits<ForwardIterator>::Value Point;
ForwardIterator it = itb;
if (it != ite)
{//for the first point
Point p1 = *it;
Point p = p1;
//display
aBoard << SetMode( p.className(), "Grid" )
<< CustomStyle( p.className()+"/Grid", new CustomPenColor( Color::Red ) )
<< p
<< CustomStyle( p.className()+"/Grid", new CustomPenColor( Color::Black ) );
Point prev = p;
for (++it; it != ite; ++it, prev = p)
{//for all other points
//conversion
p = *it;
//display
aBoard << p;
if (prev != p)
aBoard.drawArrow(prev[0], prev[1], p[0], p[1]);
}
//display the last edge too
if (isClosed)
{
if (prev != p1)
aBoard.drawArrow(prev[0], prev[1], p1[0], p1[1]);
}
}
}
/**
* Algorithms that computes the convex hull
* of a point set
*/
void convexHull()
{
//Digitization of a disk of radius 6
Ball2D<Z2i::Space> ball(Z2i::Point(0,0), 6);
Z2i::Domain domain(ball.getLowerBound(), ball.getUpperBound());
Z2i::DigitalSet pointSet(domain);
Shapes<Z2i::Domain>::euclideanShaper(pointSet, ball);
//! [Hull2D-Namespace]
using namespace functions::Hull2D;
//! [Hull2D-Namespace]
//! [Hull2D-Functor]
typedef InHalfPlaneBySimple3x3Matrix<Z2i::Point, DGtal::int64_t> Functor;
Functor functor;
//! [Hull2D-Functor]
{ //convex hull in counter-clockwise order
vector<Z2i::Point> res;
//! [Hull2D-StrictPredicateCCW]
typedef PredicateFromOrientationFunctor2<Functor, false, false> StrictPredicate;
StrictPredicate predicate( functor );
//! [Hull2D-StrictPredicateCCW]
//according to the last two template arguments, neither strictly negative values, nor zeros are accepted:
//the predicate returns 'true' only for strictly positive values returned by the underlying functor.
//! [Hull2D-AndrewAlgo]
andrewConvexHullAlgorithm( pointSet.begin(), pointSet.end(), back_inserter( res ), predicate );
//! [Hull2D-AndrewAlgo]
//![Hull2D-Caliper-computeBasic]
double th = DGtal::functions::Hull2D::computeHullThickness(res.begin(), res.end(), DGtal::functions::Hull2D::HorizontalVerticalThickness);
//![Hull2D-Caliper-computeBasic]
//![Hull2D-Caliper-computeAnti]
Z2i::Point antipodalP, antipodalQ, antipodalS;
th = DGtal::functions::Hull2D::computeHullThickness(res.begin(), res.end(), DGtal::functions::Hull2D::HorizontalVerticalThickness, antipodalP, antipodalQ, antipodalS);
//![Hull2D-Caliper-computeAnti]
trace.info() <<" ConvexHull HV thickness: " << th << std::endl;
//display
Board2D board;
drawPolygon( res.begin(), res.end(), board );
//![Hull2D-Caliper-display]
board.setPenColor(DGtal::Color::Red);
board.drawCircle( antipodalS[0], antipodalS[1], 0.2) ;
board.setPenColor(DGtal::Color::Blue);
board.drawCircle(antipodalP[0], antipodalP[1], 0.2);
board.drawCircle(antipodalQ[0], antipodalQ[1], 0.2);
board.drawLine(antipodalP[0], antipodalP[1], antipodalQ[0], antipodalQ[1]);
//![Hull2D-Caliper-display]
board.saveSVG( "ConvexHullCCW.svg" );
#ifdef WITH_CAIRO
board.saveCairo("ConvexHullCCW.png", Board2D::CairoPNG);
#endif
}
{ //convex hull in counter-clockwise order with all the points lying on the edges
vector<Z2i::Point> res;
//! [Hull2D-LargePredicateCCW]
typedef PredicateFromOrientationFunctor2<Functor, false, true> LargePredicate;
LargePredicate predicate( functor );
//! [Hull2D-LargePredicateCCW]
//according to the last template argument, zeros are accepted so that
//the predicate returns 'true' for all the positive values returned by the underlying functor.
//andrew algorithm
andrewConvexHullAlgorithm( pointSet.begin(), pointSet.end(), back_inserter( res ), predicate );
//display
Board2D board;
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "ConvexHullCCWWithPointsOnEdges.svg" );
#ifdef WITH_CAIRO
board.saveCairo("ConvexHullCCWWithPointsOnEdges.png", Board2D::CairoPNG);
#endif
}
{ //convex hull in clockwise order
vector<Z2i::Point> res;
//! [Hull2D-StrictPredicateCW]
typedef PredicateFromOrientationFunctor2<Functor, true, false> StrictPredicate;
StrictPredicate predicate( functor );
//! [Hull2D-StrictPredicateCW]
//according to the last two argument template,
//the predicate returns 'true' only for strictly negative values returned by the underlying functor.
//andrew algorithm
andrewConvexHullAlgorithm( pointSet.begin(), pointSet.end(), back_inserter( res ), predicate );
//display
Board2D board;
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "ConvexHullCW.svg" );
#ifdef WITH_CAIRO
board.saveCairo("ConvexHullCW.png", Board2D::CairoPNG);
#endif
}
{ //convex hull in counter-clockwise order
vector<Z2i::Point> res;
//geometric predicate
typedef PredicateFromOrientationFunctor2<Functor, false, false> StrictPredicate;
StrictPredicate predicate( functor );
//! [Hull2D-GrahamAlgo]
grahamConvexHullAlgorithm( pointSet.begin(), pointSet.end(), back_inserter( res ), predicate );
//! [Hull2D-GrahamAlgo]
//display
Board2D board;
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "ConvexHullCCWbis.svg" );
#ifdef WITH_CAIRO
board.saveCairo("ConvexHullCCWbis.png", Board2D::CairoPNG);
#endif
}
{ //convex hull of a simple polygonal line that is not weakly externally visible
vector<Z2i::Point> polygonalLine;
polygonalLine.push_back(Z2i::Point(0,0));
polygonalLine.push_back(Z2i::Point(0,4));
polygonalLine.push_back(Z2i::Point(1,4));
polygonalLine.push_back(Z2i::Point(1,1));
polygonalLine.push_back(Z2i::Point(3,1));
polygonalLine.push_back(Z2i::Point(2,2));
polygonalLine.push_back(Z2i::Point(3,4));
polygonalLine.push_back(Z2i::Point(4,4));
polygonalLine.push_back(Z2i::Point(4,0));
vector<Z2i::Point> resGraham, res;
typedef PredicateFromOrientationFunctor2<Functor, false, false> StrictPredicate;
StrictPredicate predicate( functor );
closedGrahamScanFromAnyPoint( polygonalLine.begin(), polygonalLine.end(), back_inserter( resGraham ), predicate );
//! [Hull2D-OnLineMelkmanAlgo]
DGtal::MelkmanConvexHull<Z2i::Point, Functor> ch( functor );
for (std::vector<Z2i::Point>::const_iterator
it = polygonalLine.begin(),
itEnd = polygonalLine.end();
it != itEnd; ++it)
ch.add( *it );
//! [Hull2D-OnLineMelkmanAlgo]
//! [Hull2D-OffLineMelkmanAlgo]
melkmanConvexHullAlgorithm( polygonalLine.begin(), polygonalLine.end(), back_inserter( res ), functor );
//! [Hull2D-OffLineMelkmanAlgo]
//display
Board2D board;
drawPolygon( polygonalLine.begin(), polygonalLine.end(), board, true );
board.saveSVG( "SimplePolygonalLine.svg" );
#ifdef WITH_CAIRO
board.saveCairo("SimplePolygonalLine.png", Board2D::CairoPNG);
#endif
board.clear();
drawPolygon( resGraham.begin(), resGraham.end(), board );
board.saveSVG( "SimplePolygonalLineGraham.svg" );
#ifdef WITH_CAIRO
board.saveCairo("SimplePolygonalLineGraham.png", Board2D::CairoPNG);
#endif
board.clear();
drawPolygon( res.begin(), res.end(), board );
board.saveSVG( "SimplePolygonalLineMelkman.svg" );
#ifdef WITH_CAIRO
board.saveCairo("SimplePolygonalLineMelkman.png", Board2D::CairoPNG);
#endif
board.clear();
drawPolygon( ch.begin(), ch.end(), board );
board.saveSVG( "SimplePolygonalLineMelkman2.svg" );
#ifdef WITH_CAIRO
board.saveCairo("SimplePolygonalLineMelkman2.png", Board2D::CairoPNG);
#endif
}
{ //order of the points for andrew algorithm
vector<Z2i::Point> res;
std::copy( pointSet.begin(), pointSet.end(), back_inserter( res ) );
std::sort( res.begin(), res.end() );
//display
Board2D board;
drawPolygon( res.begin(), res.end(), board, false );
board.saveSVG( "AndrewWEVP.svg" );
#ifdef WITH_CAIRO
board.saveCairo("AndrewWEVP.png", Board2D::CairoPNG);
#endif
}
{ //order of the points for graham algorithm
vector<Z2i::Point> res;
std::copy( pointSet.begin(), pointSet.end(), back_inserter( res ) );
//find an extremal point
//NB: we choose the point of greatest x-coordinate
//so that the sort step (by a polar comparator)
//returns a weakly externally visible polygon
std::vector<Z2i::Point>::iterator itMax
= std::max_element( res.begin(), res.end() );
//sort around this point with a polar comparator
functors::PolarPointComparatorBy2x2DetComputer<Z2i::Point> comparator;
comparator.setPole( *itMax );
std::sort( res.begin(), res.end(), comparator );
//display
Board2D board;
drawPolygon( res.begin(), res.end(), board, false );
board.saveSVG( "GrahamWEVP.svg" );
#ifdef WITH_CAIRO
board.saveCairo("GrahamWEVP.png", Board2D::CairoPNG);
#endif
}
}
/**
* Main procedure.
* Draw the convex hull
* of a point set coming from the digitization of a disk.
* @param argc number of arguments
* @param argv array of arguments
*/
int main( int argc, char** argv )
{
trace.beginBlock ( "Example exampleConvexHull2D" );
trace.info() << "Args:";
for ( int i = 0; i < argc; ++i )
trace.info() << " " << argv[ i ];
trace.info() << endl;
convexHull();
trace.endBlock();
return 0;
}
// //
///////////////////////////////////////////////////////////////////////////////