DGtal  1.5.beta
PConvexity.h
1 
17 #pragma once
18 
31 #if defined(PConvexity_RECURSES)
32 #error Recursive header files inclusion detected in PConvexity.h
33 #else // defined(PConvexity_RECURSES)
35 #define PConvexity_RECURSES
36 
37 #if !defined PConvexity_h
39 #define PConvexity_h
40 
42 // Inclusions
43 #include <iostream>
44 #include <vector>
45 #include "DGtal/base/Common.h"
46 #include "DGtal/kernel/CInteger.h"
47 #include "DGtal/kernel/CSpace.h"
48 #include "DGtal/geometry/volumes/ConvexityHelper.h"
50 
51 namespace DGtal
52 {
53  namespace detail
54  {
64  template < Dimension dim,
65  typename TInteger = DGtal::int32_t >
69  using Integer = TInteger;
73 
78  {
79  init( bd );
80  }
81 
83  void init( Dimension bd = dim )
84  {
85  for ( Dimension j = 0; j < bd; j++ )
86  projp.push_back( ProjPConvexity( j ) );
87  }
88 
99  static
100  bool is0Convex( const std::vector< Point >& X, bool safe )
101  {
102  if ( X.empty() ) return true;
103  // Build polytope according to internal integer type.
104  if ( safe )
105  {
109  computeLatticePolytope( X, false, false );
110  const std::size_t number_lattice_points_in_P = P.count();
111  return number_lattice_points_in_P == X.size();
112  }
113  else
114  {
118  computeLatticePolytope( X, false, false );
119  const std::size_t number_lattice_points_in_P = P.count();
120  return number_lattice_points_in_P == X.size();
121  }
122  }
123 
133  bool isPConvex( const std::vector< Point >& X, bool safe ) const
134  {
135  if ( ! is0Convex( X, safe ) ) return false;
136  for ( std::size_t j = 0; j < projp.size(); j++ )
137  {
138  const auto pi_j_X = project( X, j );
139  if ( ! projp[ j ].isPConvex( pi_j_X, safe ) ) return false;
140  }
141  return true;
142  }
143 
154  static
155  double convexityMeasure( const std::vector< Point >& X, bool safe )
156  {
157  if ( X.empty() ) return 1.0;
158  // Build polytope according to internal integer type.
159  if ( safe )
160  {
164  computeLatticePolytope( X, false, false );
165  const std::size_t number_lattice_points_in_P = P.count();
166  return double( X.size() ) / double( number_lattice_points_in_P );
167  }
168  else
169  {
173  computeLatticePolytope( X, false, false );
174  const std::size_t number_lattice_points_in_P = P.count();
175  return double( X.size() ) / double( number_lattice_points_in_P );
176  }
177  }
178 
189  double fullConvexityMeasure( const std::vector< Point >& X, bool safe ) const
190  {
191  double m = convexityMeasure( X, safe );
192  for ( std::size_t j = 0; j < projp.size(); j++ )
193  {
194  auto pX = project( X, j );
195  m *= projp[ j ].fullConvexityMeasure( pX, safe );
196  }
197  return m;
198  }
199 
205  static
207  {
208  ProjPoint pp;
209  Dimension j = 0;
210  for ( Dimension i = 0; i < Point::dimension; i++ )
211  if ( i != a ) pp[ j++ ] = p[ i ];
212  return pp;
213  }
214 
224  static
225  std::vector< ProjPoint > project( const std::vector< Point >& p, Dimension a )
226  {
227  std::vector< ProjPoint > pp( p.size() );
228  for ( std::size_t i = 0; i < p.size(); i++ )
229  pp[ i ] = project( p[ i ], a );
230  std::sort( pp.begin(), pp.end() );
231  auto last = std::unique( pp.begin(), pp.end() );
232  pp.erase( last, pp.end() );
233  return pp;
234  }
235 
237  std::vector< ProjPConvexity > projp;
238  };
239 
250  template < typename TInteger >
251  struct RecursivePConvexity< 1, TInteger> {
254  using Integer = TInteger;
256 
258  RecursivePConvexity( Dimension /* unused parameter in 1D specialization */ )
259  {}
260 
271  static
272  bool is0Convex( std::vector< Point > X, bool safe )
273  {
274  (void) safe;
275  std::sort( X.begin(), X.end() );
276  return X.empty()
277  || ( ( Integer(X.back()[ 0 ]) - Integer(X.front()[ 0 ]) + Integer(1) )
278  == Integer( X.size() ) );
279  }
280 
290  static
291  bool isPConvex( const std::vector< Point >& X, bool safe )
292  {
293  return is0Convex( X, safe );
294  }
295 
306  static
307  double convexityMeasure( std::vector< Point > X, bool safe )
308  {
309  (void) safe; //< not used in dimension 1.
310  if ( X.empty() ) return 1.0;
311  std::sort( X.begin(), X.end() );
312  Integer nb = Integer(X.back()[ 0 ]) - Integer(X.front()[ 0 ]) + Integer(1);
313  return double( X.size() ) / double( nb );
314  }
315 
326  static
327  double fullConvexityMeasure( const std::vector< Point >& X, bool safe )
328  {
329  return convexityMeasure( X, safe );
330  }
331 
332  };
333 
334  } // namespace detail
335 
336 
337 
339  // template class PConvexity
353  template < typename TSpace >
355  {
357 
358  public:
360  typedef TSpace Space;
361  typedef typename Space::Integer Integer;
362  typedef typename Space::Point Point;
363 
364  static const Dimension dimension = Space::dimension;
366 
367  // ----------------------- Standard services --------------------------------------
368  public:
371 
373  ~PConvexity() = default;
374 
380  PConvexity( bool safe = false )
381  : myRPC(), mySafe( safe )
382  {}
383 
385 
386  // ----------------------- Convexity services --------------------------------------
387  public:
390 
397  bool is0Convex( const std::vector< Point >& X ) const
398  {
399  return myRPC.is0Convex( X, mySafe );
400  }
401 
407  bool isPConvex( const std::vector< Point >& X ) const
408  {
409  return myRPC.isPConvex( X, mySafe );
410  }
411 
413 
414  // ----------------------- Measure services --------------------------------------
415  public:
418 
425  double convexityMeasure( const std::vector< Point >& X ) const
426  {
427  return myRPC.convexityMeasure( X, mySafe );
428  }
429 
436  double fullConvexityMeasure( const std::vector< Point >& X ) const
437  {
438  return myRPC.fullConvexityMeasure( X, mySafe );
439  }
440 
442 
443  // ----------------------- Interface --------------------------------------
444  public:
447 
452  void selfDisplay ( std::ostream & out ) const
453  {
454  out << "[PConvexity dim=" << dimension
455  << " safe=" << ( mySafe ? "True" : "False" )
456  << " #bits<int>=" << ( sizeof( Integer ) * 8 ) << "]";
457  }
458 
464  bool isValid() const
465  {
466  return dimension >= 1;
467  }
468 
470 
471  // ------------------------- Protected Datas ------------------------------
472  protected:
473 
476 
480  bool mySafe;
481 
482  // ------------------------- Private Datas --------------------------------
483  private:
484 
485  // ------------------------- Internals ------------------------------------
486  private:
487 
488  }; // end of class PConvexity
489 
492 
499  template <typename TKSpace>
500  std::ostream&
501  operator<< ( std::ostream & out,
502  const PConvexity<TKSpace> & object )
503  {
504  object.selfDisplay( out );
505  return out;
506  }
507 
509 
510 } // namespace DGtal
511 
512 
514 // Includes inline functions.
515 
516 // //
518 
519 #endif // !defined PConvexity_h
520 
521 #undef PConvexity_RECURSES
522 #endif // else defined(PConvexity_RECURSES)
Aim: A class to check if digital sets are P-convex. The P-convexity is defined as follows: A digital ...
Definition: PConvexity.h:355
static const Dimension dimension
Definition: PConvexity.h:364
bool isPConvex(const std::vector< Point > &X) const
Definition: PConvexity.h:407
void selfDisplay(std::ostream &out) const
Definition: PConvexity.h:452
BOOST_CONCEPT_ASSERT((concepts::CSpace< TSpace >))
double fullConvexityMeasure(const std::vector< Point > &X) const
Definition: PConvexity.h:436
double convexityMeasure(const std::vector< Point > &X) const
Definition: PConvexity.h:425
PConvexity< TSpace > Self
Definition: PConvexity.h:359
Space::Point Point
Definition: PConvexity.h:362
bool isValid() const
Definition: PConvexity.h:464
bool is0Convex(const std::vector< Point > &X) const
Definition: PConvexity.h:397
~PConvexity()=default
Destructor.
PConvexity(bool safe=false)
Definition: PConvexity.h:380
Space::Integer Integer
Definition: PConvexity.h:361
RPConvexity myRPC
The recursive PConvexity object used to determine P-convexity.
Definition: PConvexity.h:475
static const Dimension dimension
Copy of the static dimension of the Point/Vector.
Definition: PointVector.h:626
TInteger Integer
Arithmetic ring induced by (+,-,*) and Integer numbers.
Definition: SpaceND.h:102
DGtal::int64_t InternalInteger
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ATu0v1< TKSpace, TLinearAlgebra > &object)
DGtal::uint32_t Dimension
Definition: Common.h:136
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
static LatticePolytope computeLatticePolytope(const PointRange &input_points, bool remove_duplicates=true, bool make_minkowski_summable=false)
Aim: Concept checking for Integer Numbers. More precisely, this concept is a refinement of both CEucl...
Definition: CInteger.h:88
Aim: Defines the concept describing a digital space, ie a cartesian product of integer lines.
Definition: CSpace.h:106
static double convexityMeasure(std::vector< Point > X, bool safe)
Definition: PConvexity.h:307
static bool is0Convex(std::vector< Point > X, bool safe)
Definition: PConvexity.h:272
BOOST_CONCEPT_ASSERT((concepts::CInteger< TInteger >))
Integer must be a model of the concept CInteger.
static double fullConvexityMeasure(const std::vector< Point > &X, bool safe)
Definition: PConvexity.h:327
RecursivePConvexity(Dimension)
Default constructor. Nothing to do.
Definition: PConvexity.h:258
static bool isPConvex(const std::vector< Point > &X, bool safe)
Definition: PConvexity.h:291
static std::vector< ProjPoint > project(const std::vector< Point > &p, Dimension a)
Definition: PConvexity.h:225
bool isPConvex(const std::vector< Point > &X, bool safe) const
Definition: PConvexity.h:133
static double convexityMeasure(const std::vector< Point > &X, bool safe)
Definition: PConvexity.h:155
std::vector< ProjPConvexity > projp
The array of lower dimensional P-convexities.
Definition: PConvexity.h:237
double fullConvexityMeasure(const std::vector< Point > &X, bool safe) const
Definition: PConvexity.h:189
RecursivePConvexity(Dimension bd=dim)
Definition: PConvexity.h:77
BOOST_CONCEPT_ASSERT((concepts::CInteger< TInteger >))
Integer must be a model of the concept CInteger.
void init(Dimension bd=dim)
Definition: PConvexity.h:83
static bool is0Convex(const std::vector< Point > &X, bool safe)
Definition: PConvexity.h:100
static ProjPoint project(const Point &p, Dimension a)
Definition: PConvexity.h:206
unsigned int dim(const Vector &z)