2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @file DigitalConvexity.ih
19 * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20 * Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
24 * Implementation of inline methods defined in DigitalConvexity.h
26 * This file is part of the DGtal library.
30//////////////////////////////////////////////////////////////////////////////
32#include "DGtal/geometry/volumes/ConvexityHelper.h"
33//////////////////////////////////////////////////////////////////////////////
35//-----------------------------------------------------------------------------
36template <typename TKSpace>
37DGtal::DigitalConvexity<TKSpace>::
38DigitalConvexity( Clone<KSpace> K_, bool safe )
39 : myK( K_ ), mySafe( safe )
42//-----------------------------------------------------------------------------
43template <typename TKSpace>
44DGtal::DigitalConvexity<TKSpace>::
45DigitalConvexity( Point lo, Point hi, bool safe )
48 myK.init( lo, hi, true );
51//-----------------------------------------------------------------------------
52template <typename TKSpace>
54DGtal::DigitalConvexity<TKSpace>::
60//-----------------------------------------------------------------------------
61template <typename TKSpace>
62template <typename PointIterator>
63typename DGtal::DigitalConvexity<TKSpace>::LatticePolytope
64DGtal::DigitalConvexity<TKSpace>::
65makeSimplex( PointIterator itB, PointIterator itE )
67 return LatticePolytope( itB, itE );
70//-----------------------------------------------------------------------------
71template <typename TKSpace>
72typename DGtal::DigitalConvexity<TKSpace>::LatticePolytope
73DGtal::DigitalConvexity<TKSpace>::
74makeSimplex( std::initializer_list<Point> l )
76 return LatticePolytope( l );
79//-----------------------------------------------------------------------------
80template <typename TKSpace>
81template <typename PointIterator>
82typename DGtal::DigitalConvexity<TKSpace>::RationalPolytope
83DGtal::DigitalConvexity<TKSpace>::
84makeRationalSimplex( Integer d, PointIterator itB, PointIterator itE )
86 return RationalPolytope( d, itB, itE );
89//-----------------------------------------------------------------------------
90template <typename TKSpace>
91typename DGtal::DigitalConvexity<TKSpace>::RationalPolytope
92DGtal::DigitalConvexity<TKSpace>::
93makeRationalSimplex( std::initializer_list<Point> l )
95 return RationalPolytope( l );
98//-----------------------------------------------------------------------------
99template <typename TKSpace>
100template <typename PointIterator>
102DGtal::DigitalConvexity<TKSpace>::
103isSimplexFullDimensional( PointIterator itB, PointIterator itE )
105 typedef SimpleMatrix<Integer,dimension,dimension> Matrix;
106 const Dimension d = KSpace::dimension;
107 std::vector<Point> pts( d+1 );
109 for ( ; itB != itE && k <= d; ++itB, ++k ) pts[ k ] = *itB;
110 // A simplex has exactly d+1 vertices.
111 if ( k != d+1 ) return false;
113 for ( Dimension i = 0; i < d; ++i )
114 for ( Dimension j = 0; j < d; ++j )
115 M.setComponent( i, j, pts[ i ][ j ] - pts[ d ][ j ] );
116 // A simplex has its vectors linearly independent.
117 return M.determinant() != 0;
120//-----------------------------------------------------------------------------
121template <typename TKSpace>
123DGtal::DigitalConvexity<TKSpace>::
124isSimplexFullDimensional( std::initializer_list<Point> l )
126 return isSimplexFullDimensional( l.begin(), l.end() );
129//-----------------------------------------------------------------------------
130template <typename TKSpace>
131template <typename PointIterator>
132typename DGtal::DigitalConvexity<TKSpace>::SimplexType
133DGtal::DigitalConvexity<TKSpace>::
134simplexType( PointIterator itB, PointIterator itE )
136 typedef SimpleMatrix<Integer,dimension,dimension> Matrix;
137 const Dimension d = KSpace::dimension;
138 std::vector<Point> pts( d+1 );
140 for ( ; itB != itE && k <= d; ++itB, ++k ) pts[ k ] = *itB;
141 // A simplex has exactly d+1 vertices.
142 if ( k != d+1 ) return SimplexType::INVALID;
144 for ( Dimension i = 0; i < d; ++i )
145 for ( Dimension j = 0; j < d; ++j )
146 M.setComponent( i, j, pts[ i ][ j ] - pts[ d ][ j ] );
147 // A simplex has its vectors linearly independent.
148 auto V = M.determinant();
149 return (V == 0) ? SimplexType::DEGENERATED
150 : ( ((V == 1) || (V==-1)) ? SimplexType::UNITARY : SimplexType::COMMON );
153//-----------------------------------------------------------------------------
154template <typename TKSpace>
156DGtal::DigitalConvexity<TKSpace>::
157displaySimplex( std::ostream& out, std::initializer_list<Point> l )
159 displaySimplex( out, l.begin(), l.end() );
162//-----------------------------------------------------------------------------
163template <typename TKSpace>
164template <typename PointIterator>
166DGtal::DigitalConvexity<TKSpace>::
167displaySimplex( std::ostream& out, PointIterator itB, PointIterator itE )
169 typedef SimpleMatrix<Integer,dimension,dimension> Matrix;
170 const Dimension d = KSpace::dimension;
171 std::vector<Point> pts( d+1 );
173 for ( ; itB != itE && k <= d; ++itB, ++k ) pts[ k ] = *itB;
174 // A simplex has exactly d+1 vertices.
175 if ( k != d+1 ) { out << "[SPLX INVALID]"; return; }
177 for ( Dimension i = 0; i < d; ++i )
178 for ( Dimension kk = 0; kk < d; ++kk )
179 M.setComponent( i, kk, pts[ i ][ kk ] - pts[ d ][ kk ] );
180 // A simplex has its vectors linearly independent.
181 auto V = M.determinant();
182 out << "[SPLX V=" << V;
183 for ( Dimension i = 0; i < d; ++i ) {
185 for ( Dimension j = 0; j < d; ++j )
186 out << " " << M( i, j );
192//-----------------------------------------------------------------------------
193template <typename TKSpace>
194typename DGtal::DigitalConvexity<TKSpace>::SimplexType
195DGtal::DigitalConvexity<TKSpace>::
196simplexType( std::initializer_list<Point> l )
198 return simplexType( l.begin(), l.end() );
202//-----------------------------------------------------------------------------
203template <typename TKSpace>
204typename DGtal::DigitalConvexity<TKSpace>::PointRange
205DGtal::DigitalConvexity<TKSpace>::
206insidePoints( const LatticePolytope& polytope )
209 polytope.getPoints( pts );
212//-----------------------------------------------------------------------------
213template <typename TKSpace>
214typename DGtal::DigitalConvexity<TKSpace>::PointRange
215DGtal::DigitalConvexity<TKSpace>::
216interiorPoints( const LatticePolytope& polytope )
219 polytope.getInteriorPoints( pts );
223//-----------------------------------------------------------------------------
224template <typename TKSpace>
225typename DGtal::DigitalConvexity<TKSpace>::PointRange
226DGtal::DigitalConvexity<TKSpace>::
227insidePoints( const RationalPolytope& polytope )
230 polytope.getPoints( pts );
233//-----------------------------------------------------------------------------
234template <typename TKSpace>
235typename DGtal::DigitalConvexity<TKSpace>::PointRange
236DGtal::DigitalConvexity<TKSpace>::
237interiorPoints( const RationalPolytope& polytope )
240 polytope.getInteriorPoints( pts );
244//-----------------------------------------------------------------------------
245template <typename TKSpace>
246template <typename PointIterator>
247typename DGtal::DigitalConvexity<TKSpace>::CellGeometry
248DGtal::DigitalConvexity<TKSpace>::
249makeCellCover( PointIterator itB, PointIterator itE,
250 Dimension i, Dimension k ) const
253 ASSERT( k <= KSpace::dimension );
254 CellGeometry cgeom( myK, i, k, false );
255 cgeom.addCellsTouchingPoints( itB, itE );
259//-----------------------------------------------------------------------------
260template <typename TKSpace>
261typename DGtal::DigitalConvexity<TKSpace>::CellGeometry
262DGtal::DigitalConvexity<TKSpace>::
263makeCellCover( const LatticePolytope& P,
264 Dimension i, Dimension k ) const
267 ASSERT( k <= KSpace::dimension );
268 CellGeometry cgeom( myK, i, k, false );
269 cgeom.addCellsTouchingPolytope( P );
273//-----------------------------------------------------------------------------
274template <typename TKSpace>
275typename DGtal::DigitalConvexity<TKSpace>::CellGeometry
276DGtal::DigitalConvexity<TKSpace>::
277makeCellCover( const RationalPolytope& P,
278 Dimension i, Dimension k ) const
281 ASSERT( k <= KSpace::dimension );
282 CellGeometry cgeom( myK, i, k, false );
283 cgeom.addCellsTouchingPolytope( P );
287//-----------------------------------------------------------------------------
288template <typename TKSpace>
289typename DGtal::DigitalConvexity<TKSpace>::LatticePolytope
290DGtal::DigitalConvexity<TKSpace>::
291makePolytope( const PointRange& X, bool make_minkowski_summable ) const
295 typedef typename detail::ConvexityHelperInternalInteger< Integer, true >::Type
297 return ConvexityHelper< dimension, Integer, InternalInteger >::
298 computeLatticePolytope( X, false, make_minkowski_summable );
302 typedef typename detail::ConvexityHelperInternalInteger< Integer, false >::Type
304 return ConvexityHelper< dimension, Integer, InternalInteger >::
305 computeLatticePolytope( X, false, make_minkowski_summable );
309//-----------------------------------------------------------------------------
310template <typename TKSpace>
311typename DGtal::DigitalConvexity<TKSpace>::PointRange
312DGtal::DigitalConvexity<TKSpace>::
313U( Dimension i, const PointRange& X ) const
317 Z.reserve( X.size() );
318 auto add_one = [&i] ( Point & p ) { p[ i ] += 1; };
319 std::for_each( Y.begin(), Y.end(), add_one );
320 std::set_union( X.cbegin(), X.cend(), Y.cbegin(), Y.cend(),
321 std::back_inserter( Z ) );
325//-----------------------------------------------------------------------------
326template <typename TKSpace>
328DGtal::DigitalConvexity<TKSpace>::
329is0Convex( const PointRange& X ) const
331 if ( X.empty() ) return true;
332 const auto P = makePolytope( X );
333 return P.count() == (DGtal::BoundedLatticePolytope<DGtal::SpaceND<3>>::Integer)X.size();
336//-----------------------------------------------------------------------------
337template <typename TKSpace>
339DGtal::DigitalConvexity<TKSpace>::
340isFullyConvex( const PointRange& Z, bool convex0 ) const
342 if ( Z.size() <= 1 ) return true;
343 ASSERT( dimension <= 64 );
344 typedef DGtal::int64_t Direction;
345 typedef std::vector< Direction > Directions;
346 std::array< Directions, dimension > C;
347 const bool cvx0 = convex0 ? true : is0Convex( Z );
348 if ( ! cvx0 ) return false;
349 C[ 0 ].push_back( (Direction) 0 );
350 std::map< Direction, PointRange > X;
352 std::sort( X[ 0 ].begin(), X[ 0 ].end() );
353 for ( Dimension k = 1; k < dimension; k++ )
355 for ( const auto beta : C[ k-1 ] )
357 for ( Dimension j = 0; j < dimension; j++ )
359 const Direction dir_j = Direction(1) << j;
362 const Direction alpha = beta | dir_j;
363 C[ k ].push_back( alpha );
364 X[ alpha ] = U( j, X[ beta ] );
365 if ( ! is0Convex( X[ alpha ] ) ) return false;
373//-----------------------------------------------------------------------------
374template <typename TKSpace>
376DGtal::DigitalConvexity<TKSpace>::
377isFullyConvexFast( const PointRange& Z ) const
379 if ( Z.size() <= 1 ) return true;
380 LatticeSet C_Z( Z.cbegin(), Z.cend(), 0 );
381 const auto nb_cells = C_Z.starOfPoints().size();
382 const auto s = sizeStarCvxH( Z );
383 return s == (Integer)nb_cells;
386//-----------------------------------------------------------------------------
387template <typename TKSpace>
388typename DGtal::DigitalConvexity<TKSpace>::PointRange
389DGtal::DigitalConvexity<TKSpace>::
390ExtrCvxH( const PointRange& X ) const
394 typedef typename detail::ConvexityHelperInternalInteger< Integer, true >::Type
396 return ConvexityHelper< dimension, Integer, InternalInteger >::
397 computeLatticePolytopeVertices( X, false, false );
401 typedef typename detail::ConvexityHelperInternalInteger< Integer, false >::Type
403 return ConvexityHelper< dimension, Integer, InternalInteger >::
404 computeLatticePolytopeVertices( X, false, false );
408//-----------------------------------------------------------------------------
409template <typename TKSpace>
410typename DGtal::DigitalConvexity<TKSpace>::LatticeSet
411DGtal::DigitalConvexity<TKSpace>::
412StarCvxH( const PointRange& X, Dimension axis ) const
415 // Computes Minkowski sum of Z with hypercube
416 PointRange Z = U( 0, X );
417 for ( Dimension k = 1; k < dimension; k++ )
420 const auto P = makePolytope( Z );
421 // Extracts lattice points within polytope
422 // they correspond 1-1 to the d-cells intersected by Cvxh( Z )
424 const Dimension a = axis >= dimension ? C.longestAxis() : axis;
425 auto cellP = C.getLatticeCells( a );
426 return LatticeSet( cellP, a );
429//-----------------------------------------------------------------------------
430template <typename TKSpace>
431typename DGtal::DigitalConvexity<TKSpace>::LatticeSet
432DGtal::DigitalConvexity<TKSpace>::
433StarCvxH( const Point& a, const Point& b, const Point& c,
434 Dimension axis ) const
439 using InternalInteger
440 = typename detail::ConvexityHelperInternalInteger< Integer, true >::Type;
441 using Helper = ConvexityHelper< dimension, Integer, InternalInteger >;
442 using UnitSegment = typename Helper::LatticePolytope::UnitSegment;
443 auto P = Helper::compute3DTriangle( a, b, c, true );
444 if ( ! P.isValid() ) return LS;
445 P += UnitSegment( 0 );
446 P += UnitSegment( 1 );
447 P += UnitSegment( 2 );
448 // Extracts lattice points within polytope
449 // they correspond 1-1 to the d-cells intersected by Cvxh( Z )
451 if ( axis >= dimension ) axis = C.longestAxis();
452 const auto cellP = C.getLatticeCells( axis );
453 return LatticeSet( cellP, axis );
457 using InternalInteger
458 = typename detail::ConvexityHelperInternalInteger< Integer, false >::Type;
459 using Helper = ConvexityHelper< dimension, Integer, InternalInteger >;
460 using UnitSegment = typename Helper::LatticePolytope::UnitSegment;
461 auto P = Helper::compute3DTriangle( a, b, c, true );
462 if ( ! P.isValid() ) return LS;
463 P += UnitSegment( 0 );
464 P += UnitSegment( 1 );
465 P += UnitSegment( 2 );
466 // Extracts lattice points within polytope
467 // they correspond 1-1 to the d-cells intersected by Cvxh( Z )
469 if ( axis >= dimension ) axis = C.longestAxis();
470 const auto cellP = C.getLatticeCells( axis );
471 return LatticeSet( cellP, axis );
475//-----------------------------------------------------------------------------
476template <typename TKSpace>
477typename DGtal::DigitalConvexity<TKSpace>::LatticeSet
478DGtal::DigitalConvexity<TKSpace>::
479StarOpenTriangle( const Point& a, const Point& b, const Point& c,
480 Dimension axis ) const
485 using InternalInteger
486 = typename detail::ConvexityHelperInternalInteger< Integer, true >::Type;
487 using Helper = ConvexityHelper< dimension, Integer, InternalInteger >;
488 using UnitSegment = typename Helper::LatticePolytope::UnitSegment;
489 auto P = Helper::compute3DOpenTriangle( a, b, c, true );
490 if ( ! P.isValid() ) return LS;
491 P += UnitSegment( 0 );
492 P += UnitSegment( 1 );
493 P += UnitSegment( 2 );
494 // Extracts lattice points within polytope
495 // they correspond 1-1 to the d-cells intersected by Cvxh( Z )
497 if ( axis >= dimension ) axis = C.longestAxis();
498 const auto cellP = C.getLatticeCells( axis );
499 return LatticeSet( cellP, axis );
503 using InternalInteger
504 = typename detail::ConvexityHelperInternalInteger< Integer, false >::Type;
505 using Helper = ConvexityHelper< dimension, Integer, InternalInteger >;
506 using UnitSegment = typename Helper::LatticePolytope::UnitSegment;
507 auto P = Helper::compute3DOpenTriangle( a, b, c, true );
508 if ( ! P.isValid() ) return LS;
509 P += UnitSegment( 0 );
510 P += UnitSegment( 1 );
511 P += UnitSegment( 2 );
512 // Extracts lattice points within polytope
513 // they correspond 1-1 to the d-cells intersected by Cvxh( Z )
515 if ( axis >= dimension ) axis = C.longestAxis();
516 const auto cellP = C.getLatticeCells( axis );
517 return LatticeSet( cellP, axis );
521//-----------------------------------------------------------------------------
522template <typename TKSpace>
523typename DGtal::DigitalConvexity<TKSpace>::LatticeSet
524DGtal::DigitalConvexity<TKSpace>::
525Star( const PointRange& X, const Dimension axis ) const
527 const Dimension a = axis >= dimension ? 0 : axis;
528 LatticeSet L( X.cbegin(), X.cend(), a );
529 return L.starOfPoints();
531//-----------------------------------------------------------------------------
532template <typename TKSpace>
533typename DGtal::DigitalConvexity<TKSpace>::LatticeSet
534DGtal::DigitalConvexity<TKSpace>::
535StarCells( const PointRange& C, const Dimension axis ) const
537 const Dimension a = axis >= dimension ? 0 : axis;
538 LatticeSet L( C.cbegin(), C.cend(), a );
539 return L.starOfCells();
542//-----------------------------------------------------------------------------
543template <typename TKSpace>
544template <typename TPolytope>
545typename DGtal::DigitalConvexity<TKSpace>::LatticeSet
546DGtal::DigitalConvexity<TKSpace>::
547CoverPolytope( const TPolytope& P, Dimension axis ) const
549 using StrictUnitSegment = typename Polytope::StrictUnitSegment;
551 if ( ! P.isValid() ) return LS;
552 if ( ! P.canBeSummed() ) {
553 trace.error() << "[DigitalConvexity::CoverPolytope] Polytope should be Minkowski summabel." << std::endl;
557 if ( axis >= dimension ) axis = C.longestAxis();
558 auto V = LatticeSet( C.getLatticeSet( axis ), axis ).toPointRange(); // << 0-cells
559 auto P0 = P + StrictUnitSegment( 0 );
560 auto P1 = P + StrictUnitSegment( 1 );
561 auto P2 = P + StrictUnitSegment( 2 );
565 auto V0 = LatticeSet( C0.getLatticeSet( axis ), axis ).toPointRange(); // << 1-cells
566 auto V1 = LatticeSet( C1.getLatticeSet( axis ), axis ).toPointRange(); // << 1-cells
567 auto V2 = LatticeSet( C2.getLatticeSet( axis ), axis ).toPointRange(); // << 1-cells
568 auto P01 = P0 + StrictUnitSegment( 1 );
569 auto P02 = P0 + StrictUnitSegment( 2 );
570 auto P12 = P1 + StrictUnitSegment( 2 );
574 auto V01 = LatticeSet( C01.getLatticeSet( axis ), axis ).toPointRange(); // << 2-cells
575 auto V02 = LatticeSet( C02.getLatticeSet( axis ), axis ).toPointRange(); // << 2-cells
576 auto V12 = LatticeSet( C12.getLatticeSet( axis ), axis ).toPointRange(); // << 2-cells
577 auto P012 = P01 + StrictUnitSegment( 2 );
578 Counter C012( P012 );
579 auto V012 = LatticeSet( C012.getLatticeSet( axis ), axis ).toPointRange(); // << 3-cells
580 // Convert points to Khalimsky coordinates and insert them in LatticeSet.
581 LS = LatticeSet( axis );
582 for ( const auto& p : V ) LS.insert( Point( 2*p[0] , 2*p[1] , 2*p[2] ) );
583 for ( const auto& p : V0 ) LS.insert( Point( 2*p[0]-1, 2*p[1] , 2*p[2] ) );
584 for ( const auto& p : V1 ) LS.insert( Point( 2*p[0] , 2*p[1]-1, 2*p[2] ) );
585 for ( const auto& p : V2 ) LS.insert( Point( 2*p[0] , 2*p[1] , 2*p[2]-1 ) );
586 for ( const auto& p : V01 ) LS.insert( Point( 2*p[0]-1, 2*p[1]-1, 2*p[2] ) );
587 for ( const auto& p : V02 ) LS.insert( Point( 2*p[0]-1, 2*p[1] , 2*p[2]-1 ) );
588 for ( const auto& p : V12 ) LS.insert( Point( 2*p[0] , 2*p[1]-1, 2*p[2]-1 ) );
589 for ( const auto& p : V012 ) LS.insert( Point( 2*p[0]-1, 2*p[1]-1, 2*p[2]-1 ) );
593//-----------------------------------------------------------------------------
594template <typename TKSpace>
595typename DGtal::DigitalConvexity<TKSpace>::LatticeSet
596DGtal::DigitalConvexity<TKSpace>::
597CoverCvxH( const Point& a, const Point& b, const Point& c,
598 Dimension axis ) const
603 using InternalInteger
604 = typename detail::ConvexityHelperInternalInteger< Integer, true >::Type;
605 using Helper = ConvexityHelper< dimension, Integer, InternalInteger >;
606 // using StrictUnitSegment = typename Helper::LatticePolytope::StrictUnitSegment;
607 auto P = Helper::compute3DTriangle( a, b, c, true );
608 return CoverPolytope( P, axis );
612 using InternalInteger
613 = typename detail::ConvexityHelperInternalInteger< Integer, false >::Type;
614 using Helper = ConvexityHelper< dimension, Integer, InternalInteger >;
615 // using StrictUnitSegment = typename Helper::LatticePolytope::StrictUnitSegment;
616 auto P = Helper::compute3DTriangle( a, b, c, true );
617 return CoverPolytope( P, axis );
621//-----------------------------------------------------------------------------
622template <typename TKSpace>
623typename DGtal::DigitalConvexity<TKSpace>::LatticeSet
624DGtal::DigitalConvexity<TKSpace>::
625CoverCvxH( const Point& a, const Point& b,
626 Dimension axis ) const
631 using InternalInteger
632 = typename detail::ConvexityHelperInternalInteger< Integer, true >::Type;
633 using Helper = ConvexityHelper< dimension, Integer, InternalInteger >;
634 auto P = Helper::computeSegment( a, b );
635 return CoverPolytope( P, axis );
639 using InternalInteger
640 = typename detail::ConvexityHelperInternalInteger< Integer, false >::Type;
641 using Helper = ConvexityHelper< dimension, Integer, InternalInteger >;
642 auto P = Helper::computeSegment( a, b );
643 return CoverPolytope( P, axis );
648//-----------------------------------------------------------------------------
649template <typename TKSpace>
650typename DGtal::DigitalConvexity<TKSpace>::LatticeSet
651DGtal::DigitalConvexity<TKSpace>::
652toLatticeSet( const PointRange& X, const Dimension axis ) const
654 const Dimension a = axis >= dimension ? 0 : axis;
655 return LatticeSet( X.cbegin(), X.cend(), a );
658//-----------------------------------------------------------------------------
659template <typename TKSpace>
660typename DGtal::DigitalConvexity<TKSpace>::PointRange
661DGtal::DigitalConvexity<TKSpace>::
662toPointRange( const LatticeSet& L ) const
664 return L.toPointRange();
667//-----------------------------------------------------------------------------
668template <typename TKSpace>
669typename DGtal::DigitalConvexity<TKSpace>::Integer
670DGtal::DigitalConvexity<TKSpace>::
671sizeStarCvxH( const PointRange& X ) const
674 // Computes Minkowski sum of Z with hypercube
675 PointRange Z = U( 0, X );
676 for ( Dimension k = 1; k < dimension; k++ )
679 const auto P = makePolytope( Z );
680 // Extracts lattice points within polytope
681 // they correspond 1-1 to the d-cells intersected by Cvxh( Z )
683 const Dimension a = C.longestAxis();
684 auto cellP = C.getLatticeCells( a );
686 // Counts the number of cells
688 for ( const auto& value : cellP )
690 Point p = value.first;
691 Interval I = value.second;
692 nb += I.second - I.first + 1;
697//-----------------------------------------------------------------------------
698template <typename TKSpace>
699typename DGtal::DigitalConvexity<TKSpace>::PointRange
700DGtal::DigitalConvexity<TKSpace>::
701Extr( const PointRange& C ) const
703 // JOL: using std::set< Point > or std::unordered_set< Point > is slightly slower.
704 // We prefer to use vector for easier vectorization.
705 std::vector<Point> E;
706 E.reserve( 2*C.size() );
707 for ( auto&& kp : C )
709 auto c = myK.uCell( kp );
710 if ( myK.uDim( c ) == 0 )
711 E.push_back( myK.uCoords( c ) );
714 auto faces = myK.uFaces( c );
715 for ( auto&& f : faces )
716 if ( myK.uDim( f ) == 0 )
717 E.push_back( myK.uCoords( f ) );
720 std::sort( E.begin(), E.end() );
721 auto last = std::unique( E.begin(), E.end() );
722 E.erase( last, E.end() );
725//-----------------------------------------------------------------------------
726template <typename TKSpace>
727typename DGtal::DigitalConvexity<TKSpace>::PointRange
728DGtal::DigitalConvexity<TKSpace>::
729Extr( const LatticeSet& C ) const
731 return C.extremaOfCells();
733//-----------------------------------------------------------------------------
734template <typename TKSpace>
735typename DGtal::DigitalConvexity<TKSpace>::LatticeSet
736DGtal::DigitalConvexity<TKSpace>::
737Skel( const LatticeSet& C ) const
739 return C.skeletonOfCells();
741//-----------------------------------------------------------------------------
742template <typename TKSpace>
743typename DGtal::DigitalConvexity<TKSpace>::PointRange
744DGtal::DigitalConvexity<TKSpace>::
745ExtrSkel( const LatticeSet& C ) const
747 return C.skeletonOfCells().extremaOfCells();
750//-----------------------------------------------------------------------------
751template <typename TKSpace>
752typename DGtal::DigitalConvexity<TKSpace>::PointRange
753DGtal::DigitalConvexity<TKSpace>::
754FC_direct( const PointRange& Z ) const
756 typedef typename LatticePolytope::Domain Domain;
758 // Computes Minkowski sum of Z with hypercube
760 for ( Dimension k = 0; k < dimension; k++ )
763 const auto P = makePolytope( X );
764 // Extracts lattice points within polytope
765 // they correspond 1-1 to the d-cells intersected by Cvxh( Z )
767 const Dimension a = C.longestAxis();
768 Point lo = C.lowerBound();
769 Point hi = C.upperBound();
771 const Domain projD( lo, hi ); //< the projected domain of the polytope.
772 const Point One = Point::diagonal( 1 );
773 //const Size size = projD.size(); //not USED
774 std::unordered_map< Point, Interval > cellP;
776 for ( auto&& p : projD )
779 const auto I = C.intersectionIntervalAlongAxis( p, a );
780 const auto n = I.second - I.first;
783 // Now the second bound is included
784 cellP[ q ] = Interval( 2 * I.first - 1, 2 * I.second - 3 );
787 // It remains to compute all the k-cells, 0 <= k < d, intersected by Cvxh( Z )
788 for ( Dimension k = 0; k < dimension; k++ )
790 if ( k == a ) continue;
791 std::vector< Point > q_computed;
792 std::vector< Interval > I_computed;
793 for ( const auto& value : cellP )
795 Point p = value.first;
796 Interval I = value.second;
797 Point r = p; r[ k ] += 2;
798 const auto it = cellP.find( r );
799 if ( it == cellP.end() ) continue; // neighbor is empty
800 // Otherwise compute common part.
801 Interval J = it->second;
802 auto f = std::max( I.first, J.first );
803 auto s = std::min( I.second, J.second );
806 Point qq = p; qq[ k ] += 1;
807 q_computed.push_back( qq );
808 I_computed.push_back( Interval( f, s ) );
811 // Add new columns to map Point -> column
812 for ( size_t i = 0; i < q_computed.size(); ++i )
814 cellP[ q_computed[ i ] ] = I_computed[ i ];
817 // The built complex is open.
818 // Check it and fill skelP
819 std::unordered_map< Point, std::vector< Interval > > skelP;
820 for ( const auto& value : cellP )
822 Point p = value.first;
823 Interval I = value.second;
824 auto n = I.second - I.first + 1;
826 trace.error() << "Weird thickness step 1="
828 std::vector< Interval > V( 1, I );
829 auto result = skelP.insert( std::make_pair( p, V ) );
830 (void)result;//unused var;
833 // std::cout << "Extract skel" << std::endl;
834 // Now extracting implicitly its Skel
835 for ( const auto& value : cellP )
837 const Point& p = value.first;
838 const auto& I = value.second;
839 for ( Dimension k = 0; k < dimension; k++ )
841 if ( k == a ) continue;
842 if ( ( p[ k ] & 0x1 ) != 0 ) continue; // if open along axis continue
843 // if closed, check upper incident cells along direction k
844 Point qq = p; qq[ k ] -= 1;
845 Point r = p; r[ k ] += 1;
846 auto itq = skelP.find( qq );
847 if ( itq != skelP.end() )
849 auto& W = itq->second;
850 eraseInterval( I, W );
851 // if ( W.empty() ) skelP.erase( itq );
853 auto itr = skelP.find( r );
854 if ( itr != skelP.end() )
856 auto& W = itr->second;
857 eraseInterval( I, W );
858 // if ( W.empty() ) skelP.erase( itr );
862 // Extract skel along main axis
863 for ( const auto& value : cellP )
865 const Point& p = value.first;
866 auto I = value.second;
867 const auto itp = skelP.find( p );
868 if ( itp == skelP.end() ) continue;
869 if ( ( I.first & 0x1 ) != 0 ) I.first += 1;
870 if ( ( I.second & 0x1 ) != 0 ) I.second -= 1;
871 auto& W = itp->second;
872 for ( auto x = I.first; x <= I.second; x += 2 )
874 eraseInterval( Interval{ x-1,x-1} , W );
875 eraseInterval( Interval{ x+1,x+1} , W );
878 // Erase empty stacks
879 for ( auto it = skelP.begin(), itE = skelP.end(); it != itE; )
881 const auto& V = it->second;
886 skelP.erase( it_erase );
890 // Skel is constructed, now compute its Extr.
892 for ( const auto& value : skelP )
894 Point p = value.first;
895 auto W = value.second;
904//-----------------------------------------------------------------------------
905template <typename TKSpace>
906typename DGtal::DigitalConvexity<TKSpace>::PointRange
907DGtal::DigitalConvexity<TKSpace>::
908FC_LatticeSet( const PointRange& Z ) const
910 const auto StarCvxZ = StarCvxH( Z );
911 const auto SkelStarCvxZ = StarCvxZ.skeletonOfCells().toPointRange();
912 return Extr( SkelStarCvxZ );
915//-----------------------------------------------------------------------------
916template <typename TKSpace>
917typename DGtal::DigitalConvexity<TKSpace>::PointRange
918DGtal::DigitalConvexity<TKSpace>::
919FC( const PointRange& Z, EnvelopeAlgorithm algo ) const
921 if ( algo == EnvelopeAlgorithm::DIRECT )
922 return FC_direct( Z );
923 else if ( algo == EnvelopeAlgorithm::LATTICE_SET )
924 return FC_LatticeSet( Z );
929//-----------------------------------------------------------------------------
930template <typename TKSpace>
931typename DGtal::DigitalConvexity<TKSpace>::PointRange
932DGtal::DigitalConvexity<TKSpace>::
933envelope( const PointRange& Z, EnvelopeAlgorithm algo ) const
938 auto card_In = In.size();
940 if ( In.size() == card_In ) return In;
943 trace.error() << "[DigitalConvexity::envelope] Should never pass here."
945 return Z; // to avoid warnings.
948//-----------------------------------------------------------------------------
949template <typename TKSpace>
950typename DGtal::DigitalConvexity<TKSpace>::PointRange
951DGtal::DigitalConvexity<TKSpace>::
952relativeEnvelope( const PointRange& Z, const PointRange& Y,
953 EnvelopeAlgorithm algo ) const
959 std::set_intersection( In.cbegin(), In.cend(),
960 Y.cbegin(), Y.cend(),
961 std::back_inserter( Out ) );
962 In = FC( Out, algo );
963 if ( In.size() == Out.size() ) return Out;
969//-----------------------------------------------------------------------------
970template <typename TKSpace>
971template <typename Predicate>
972typename DGtal::DigitalConvexity<TKSpace>::PointRange
973DGtal::DigitalConvexity<TKSpace>::
974relativeEnvelope( const PointRange& Z, const Predicate& PredY,
975 EnvelopeAlgorithm algo ) const
980 auto Out = filter( In, PredY );
981 In = FC( Out, algo );
982 if ( In.size() == Out.size() ) return In;
988//-----------------------------------------------------------------------------
989template <typename TKSpace>
990typename DGtal::DigitalConvexity<TKSpace>::Size
991DGtal::DigitalConvexity<TKSpace>::
992depthLastEnvelope() const
994 return myDepthLastFCE;
997//-----------------------------------------------------------------------------
998template <typename TKSpace>
1000DGtal::DigitalConvexity<TKSpace>::
1001isKConvex( const LatticePolytope& P, const Dimension k ) const
1003 if ( k == 0 ) return true;
1004 auto S = insidePoints( P );
1005 auto touched_cells = makeCellCover( S.begin(), S.end(), k, k );
1006 auto intersected_cells = makeCellCover( P, k, k );
1007 return intersected_cells.nbCells() == touched_cells.nbCells();
1008 // JOL: number should be enough
1009 // && intersected_cells.subset( touched_cells );
1012//-----------------------------------------------------------------------------
1013template <typename TKSpace>
1015DGtal::DigitalConvexity<TKSpace>::
1016isFullyConvex( const LatticePolytope& P ) const
1018 auto S = insidePoints( P );
1019 if ( S.size() <= 1 ) return true;
1020 for ( Dimension k = 1; k < KSpace::dimension; ++ k )
1022 auto touched_cells = makeCellCover( S.begin(), S.end(), k, k );
1023 auto intersected_cells = makeCellCover( P, k, k );
1024 if ( ( intersected_cells.nbCells() != touched_cells.nbCells() ) )
1025 // JOL: number should be enough
1026 // || ( ! intersected_cells.subset( touched_cells ) ) )
1032//-----------------------------------------------------------------------------
1033template <typename TKSpace>
1035DGtal::DigitalConvexity<TKSpace>::
1036isKSubconvex( const LatticePolytope& P, const CellGeometry& C, const Dimension k ) const
1038 auto intersected_cells = makeCellCover( P, k, k );
1039 return intersected_cells.subset( C );
1041//-----------------------------------------------------------------------------
1042template <typename TKSpace>
1044DGtal::DigitalConvexity<TKSpace>::
1045isFullySubconvex( const LatticePolytope& P, const CellGeometry& C ) const
1047 auto intersected_cells = makeCellCover( P, C.minCellDim(), C.maxCellDim() );
1048 return intersected_cells.subset( C );
1051//-----------------------------------------------------------------------------
1052template <typename TKSpace>
1054DGtal::DigitalConvexity<TKSpace>::
1055isFullySubconvex( const LatticePolytope& P, const LatticeSet& StarX ) const
1057 LatticePolytope Q = P + typename LatticePolytope::UnitSegment( 0 );
1058 for ( Dimension k = 1; k < dimension; k++ )
1059 Q = Q + typename LatticePolytope::UnitSegment( k );
1061 const auto cells = C.getLatticeCells( StarX.axis() );
1062 LatticeSet StarP( cells, StarX.axis() );
1063 return StarX.includes( StarP );
1066//-----------------------------------------------------------------------------
1067template <typename TKSpace>
1069DGtal::DigitalConvexity<TKSpace>::
1070isFullySubconvex( const PointRange& Y, const LatticeSet& StarX ) const
1072 const auto SCY = StarCvxH( Y, StarX.axis() );
1073 return StarX.includes( SCY );
1076//-----------------------------------------------------------------------------
1077template <typename TKSpace>
1079DGtal::DigitalConvexity<TKSpace>::
1080isFullySubconvex( const Point& a, const Point& b, const Point& c,
1081 const LatticeSet& StarX ) const
1083 ASSERT( dimension == 3 );
1084 const auto SCabc = StarCvxH( a, b, c, StarX.axis() );
1085 return StarX.includes( SCabc );
1087//-----------------------------------------------------------------------------
1088template <typename TKSpace>
1090DGtal::DigitalConvexity<TKSpace>::
1091isFullyCovered( const Point& a, const Point& b, const Point& c,
1092 const LatticeSet& cells ) const
1094 ASSERT( dimension == 3 );
1095 const auto SCabc = CoverCvxH( a, b, c, cells.axis() );
1096 return cells.includes( SCabc );
1098//-----------------------------------------------------------------------------
1099template <typename TKSpace>
1101DGtal::DigitalConvexity<TKSpace>::
1102isFullyCovered( const Point& a, const Point& b,
1103 const LatticeSet& cells ) const
1105 ASSERT( dimension == 3 );
1106 LatticeSet L_ab( cells.axis() );
1107 const auto V = b - a;
1109 for ( Dimension k = 0; k < dimension; k++ )
1111 const Integer n = ( V[ k ] >= 0 ) ? V[ k ] : -V[ k ];
1112 const Integer d = ( V[ k ] >= 0 ) ? 1 : -1;
1113 if ( n == 0 ) continue;
1115 for ( Integer i = 1; i < n; i++ )
1117 for ( Dimension j = 0; j < dimension; j++ )
1119 if ( j == k ) kc[ k ] = 2 * ( a[ k ] + d * i );
1122 const auto v = V[ j ];
1123 const auto q = ( v * i ) / n;
1124 const auto r = ( v * i ) % n; // might be negative
1125 kc[ j ] = 2 * ( a[ j ] + q );
1126 if ( r < 0 ) kc[ j ] -= 1;
1127 else if ( r > 0 ) kc[ j ] += 1;
1133 if ( a != b ) L_ab.insert( 2*b );
1134 // LatticeSet Star_ab = L_ab.starOfCells();
1135 return cells.includes( L_ab );
1137 // ASSERT( dimension == 3 );
1138 // const auto SCab = CoverCvxH( a, b, cells.axis() );
1139 // return cells.includes( SCab );
1142//-----------------------------------------------------------------------------
1143template <typename TKSpace>
1145DGtal::DigitalConvexity<TKSpace>::
1146isOpenTriangleFullySubconvex( const Point& a, const Point& b, const Point& c,
1147 const LatticeSet& StarX ) const
1149 ASSERT( dimension == 3 );
1150 const auto SCabc = StarOpenTriangle( a, b, c, StarX.axis() );
1151 return StarX.includes( SCabc );
1154//-----------------------------------------------------------------------------
1155template <typename TKSpace>
1157DGtal::DigitalConvexity<TKSpace>::
1158isKSubconvex( const Point& a, const Point& b,
1159 const CellGeometry& C, const Dimension k ) const
1161 CellGeometry cgeom( myK, k, k, false );
1162 cgeom.addCellsTouchingSegment( a, b );
1163 return cgeom.subset( C );
1166//-----------------------------------------------------------------------------
1167template <typename TKSpace>
1169DGtal::DigitalConvexity<TKSpace>::
1170isFullySubconvex( const Point& a, const Point& b,
1171 const CellGeometry& C ) const
1173 CellGeometry cgeom( myK, C.minCellDim(), C.maxCellDim(), false );
1174 cgeom.addCellsTouchingSegment( a, b );
1175 return cgeom.subset( C );
1178//-----------------------------------------------------------------------------
1179template <typename TKSpace>
1181DGtal::DigitalConvexity<TKSpace>::
1182isFullySubconvex( const Point& a, const Point& b,
1183 const LatticeSet& StarX ) const
1185 LatticeSet L_ab( StarX.axis() );
1186 const auto V = b - a;
1188 for ( Dimension k = 0; k < dimension; k++ )
1190 const Integer n = ( V[ k ] >= 0 ) ? V[ k ] : -V[ k ];
1191 const Integer d = ( V[ k ] >= 0 ) ? 1 : -1;
1192 if ( n == 0 ) continue;
1194 for ( Integer i = 1; i < n; i++ )
1196 for ( Dimension j = 0; j < dimension; j++ )
1198 if ( j == k ) kc[ k ] = 2 * ( a[ k ] + d * i );
1201 const auto v = V[ j ];
1202 const auto q = ( v * i ) / n;
1203 const auto r = ( v * i ) % n; // might be negative
1204 kc[ j ] = 2 * ( a[ j ] + q );
1205 if ( r < 0 ) kc[ j ] -= 1;
1206 else if ( r > 0 ) kc[ j ] += 1;
1212 if ( a != b ) L_ab.insert( 2*b );
1213 LatticeSet Star_ab = L_ab.starOfCells();
1214 return StarX.includes( Star_ab );
1217//-----------------------------------------------------------------------------
1218template <typename TKSpace>
1220DGtal::DigitalConvexity<TKSpace>::
1221isKConvex( const RationalPolytope& P, const Dimension k ) const
1223 if ( k == 0 ) return true;
1224 auto Q = P.denominator() * P;
1225 auto S = insidePoints( Q );
1226 auto touched_cells = makeCellCover( S.begin(), S.end(), k, k );
1227 auto intersected_cells = makeCellCover( Q, k, k );
1228 return intersected_cells.nbCells() == touched_cells.nbCells()
1229 && intersected_cells.subset( touched_cells );
1232//-----------------------------------------------------------------------------
1233template <typename TKSpace>
1235DGtal::DigitalConvexity<TKSpace>::
1236isFullyConvex( const RationalPolytope& P ) const
1238 auto Q = P.denominator() * P;
1239 auto S = insidePoints( Q );
1240 if ( S.size() <= 1 ) return true;
1241 for ( Dimension k = 1; k < KSpace::dimension; ++ k )
1243 auto touched_cells = makeCellCover( S.begin(), S.end(), k, k );
1244 auto intersected_cells = makeCellCover( Q, k, k );
1245 if ( ( intersected_cells.nbCells() != touched_cells.nbCells() )
1246 || ( ! intersected_cells.subset( touched_cells ) ) )
1252//-----------------------------------------------------------------------------
1253template <typename TKSpace>
1255DGtal::DigitalConvexity<TKSpace>::
1256isKSubconvex( const RationalPolytope& P, const CellGeometry& C,
1257 const Dimension k ) const
1259 auto intersected_cells = makeCellCover( P, k, k );
1260 return intersected_cells.subset( C );
1262//-----------------------------------------------------------------------------
1263template <typename TKSpace>
1265DGtal::DigitalConvexity<TKSpace>::
1266isFullySubconvex( const RationalPolytope& P, const CellGeometry& C ) const
1268 auto intersected_cells = makeCellCover( P, C.minCellDim(), C.maxCellDim() );
1269 return intersected_cells.subset( C );
1272//-----------------------------------------------------------------------------
1273template <typename TKSpace>
1275DGtal::DigitalConvexity<TKSpace>::
1276eraseInterval( Interval I, std::vector< Interval > & V )
1278 for ( std::size_t i = 0; i < V.size(); )
1280 Interval& J = V[ i ];
1281 // I=[a,b], J=[a',b'], a <= b, a' <= b'
1282 if ( I.second < J.first ) { break; } // b < a' : no further intersection
1283 if ( J.second < I.first ) { ++i; continue; } // b' < a : no further intersection
1284 // a' <= b and a <= b'
1286 // a' ............... a'
1287 // b' ................. b'
1289 // a' ..................... b' => a'..a-1 b+1 b'
1290 Interval K1( J.first, I.first - 1 );
1291 Interval K2( I.second + 1, J.second );
1292 bool K1_exist = K1.second >= K1.first;
1293 bool K2_exist = K2.second >= K2.first;
1294 if ( K1_exist && K2_exist )
1297 V.insert( V.begin() + i, K1 );
1298 break; // no further intersection possible
1300 else if ( K1_exist )
1304 else if ( K2_exist )
1310 V.erase( V.begin() + i );
1315//-----------------------------------------------------------------------------
1316template <typename TKSpace>
1317template <typename Predicate>
1318typename DGtal::DigitalConvexity<TKSpace>::PointRange
1319DGtal::DigitalConvexity<TKSpace>::
1320filter( const PointRange& E, const Predicate& Pred )
1323 Out.reserve( E.size() );
1324 for ( auto&& p : E )
1325 if ( Pred( p ) ) Out.push_back( p );
1329///////////////////////////////////////////////////////////////////////////////
1330// Interface - public :
1333 * Writes/Displays the object on an output stream.
1334 * @param out the output stream where the object is written.
1336template <typename TKSpace>
1339DGtal::DigitalConvexity<TKSpace>::selfDisplay ( std::ostream & out ) const
1341 out << "[DigitalConvexity]";
1345 * Checks the validity/consistency of the object.
1346 * @return 'true' if the object is valid, 'false' otherwise.
1348template <typename TKSpace>
1351DGtal::DigitalConvexity<TKSpace>::isValid() const
1357///////////////////////////////////////////////////////////////////////////////
1358// Implementation of inline functions //
1360//-----------------------------------------------------------------------------
1361template <typename TKSpace>
1364DGtal::operator<< ( std::ostream & out,
1365 const DigitalConvexity<TKSpace> & object )
1367 object.selfDisplay( out );
1372///////////////////////////////////////////////////////////////////////////////