DGtal  1.5.beta
exampleBoundedLatticePolytopeCount2D.cpp
Go to the documentation of this file.
1 
58 #include "DGtal/base/Common.h"
59 #include "DGtal/geometry/volumes/ConvexityHelper.h"
60 #include "DGtal/geometry/volumes/BoundedLatticePolytope.h"
61 
62 int main( int argc, char* argv[] )
63 {
64  int N = argc > 1 ? atoi( argv[ 1 ] ) : 1000; // nb of polytopes
65  int nb = argc > 2 ? atoi( argv[ 2 ] ) : 10; // nb points per polytope
66  int R = argc > 3 ? atoi( argv[ 3 ] ) : 200; // max diameter of shape
67 
68  typedef int64_t Integer;
70  typedef Helper::LatticePolytope Polytope;
71  typedef Polytope::Point Point;
72  // Compute all polytopes
73  DGtal::trace.beginBlock( "Compute 2D polytopes" );
74  std::vector< Polytope > polytopes;
75  int sum_nb_facets = 0;
76  for ( int i = 0; i < N; i++ )
77  {
78  std::vector< Point > V;
79  for ( int i = 0; i < nb; i++ ) {
80  Point p( rand() % (2*R+1) - R, rand() % (2*R+1) - R );
81  V.push_back( p );
82  }
83  Polytope P = Helper::computeLatticePolytope( V );
84  sum_nb_facets += P.nbHalfSpaces();
85  polytopes.push_back( P );
86  }
87  double t1 = DGtal::trace.endBlock();
88  DGtal::trace.info() << "Computed " << N
89  << " 2D polytopes with " << ( sum_nb_facets / (double) N )
90  << " facets on average, in " << t1 << " ms." << std::endl;
91  // Count interior points (slow method)
92  DGtal::trace.beginBlock( "Compute number of lattice points within polytope (slow)" );
93  std::size_t slow_nb = 0;
94  std::vector< Integer > slow_counts;
95  for ( const auto& P : polytopes )
96  {
97  const auto nb = P.countByScanning();
98  slow_nb += nb;
99  slow_counts.push_back( nb );
100  }
101  double t2 = DGtal::trace.endBlock();
102  // Count interior points (fast method)
103  DGtal::trace.beginBlock( "Compute number of lattice points within polytope (fast)" );
104  std::size_t fast_nb = 0;
105  std::vector< Integer > fast_counts;
106  for ( const auto& P : polytopes )
107  {
108  const auto nb = P.count();
109  fast_nb += nb;
110  fast_counts.push_back( nb );
111  }
112  double t3 = DGtal::trace.endBlock();
113  bool ok = std::equal( slow_counts.cbegin(), slow_counts.cend(), fast_counts.cbegin() );
114  DGtal::trace.info() << "Computed inside points is " << ( ok ? "OK" : "ERROR" ) << std::endl;
115  DGtal::trace.info() << "Reference method computed " << slow_nb
116  << " points in " << t2 << " ms." << std::endl;
117  DGtal::trace.info() << "Fast method computed " << fast_nb
118  << " points in " << t3 << " ms." << std::endl;
119  return 0;
120 }
121 
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
int main(int argc, char *argv[])
Trace trace
Definition: Common.h:153
Aim: Provides a set of functions to facilitate the computation of convex hulls and polytopes,...
MyPointD Point
Definition: testClone2.cpp:383