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 CubicalComplex.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 CubicalComplex.h
26 * This file is part of the DGtal library.
30//////////////////////////////////////////////////////////////////////////////
33#include "DGtal/base/SetFunctions.h"
34#include "DGtal/topology/CubicalComplexFunctions.h"
35//////////////////////////////////////////////////////////////////////////////
37///////////////////////////////////////////////////////////////////////////////
38// IMPLEMENTATION of inline methods.
39///////////////////////////////////////////////////////////////////////////////
41///////////////////////////////////////////////////////////////////////////////
42// ----------------------- Standard services ------------------------------
44//-----------------------------------------------------------------------------
45template <typename TKSpace, typename TCellContainer>
47DGtal::CubicalComplex<TKSpace, TCellContainer>::
52//-----------------------------------------------------------------------------
53template <typename TKSpace, typename TCellContainer>
55DGtal::CubicalComplex<TKSpace, TCellContainer>::
57 : myKSpace( 0 ), myCells( dimension+1 )
61//-----------------------------------------------------------------------------
62template <typename TKSpace, typename TCellContainer>
64DGtal::CubicalComplex<TKSpace, TCellContainer>::
65CubicalComplex( ConstAlias<KSpace> aK )
66 : myKSpace( &aK ), myCells( dimension+1 )
70//-----------------------------------------------------------------------------
71template <typename TKSpace, typename TCellContainer>
73DGtal::CubicalComplex<TKSpace, TCellContainer>::
74CubicalComplex( const CubicalComplex& other )
75 : myKSpace( other.myKSpace ), myCells( other.myCells )
79//-----------------------------------------------------------------------------
80template <typename TKSpace, typename TCellContainer>
81template <typename TDigitalSet>
83void DGtal::CubicalComplex<TKSpace, TCellContainer>::
84construct( const TDigitalSet & set )
86 assert ( TDigitalSet::Domain::dimension == dimension );
87 for ( typename TDigitalSet::ConstIterator it = set.begin(); it != set.end(); ++it )
89 typedef typename TKSpace::Cells CellsCollection;
90 typename TKSpace::Cell cell = myKSpace->uSpel ( *it );
92 CellsCollection n = myKSpace->uFaces ( cell );
93 for ( typename CellsCollection::ConstIterator itt = n.begin() ; itt < n.end(); ++itt )
98//-----------------------------------------------------------------------------
99template <typename TKSpace, typename TCellContainer>
101const typename DGtal::CubicalComplex<TKSpace, TCellContainer>::KSpace &
102DGtal::CubicalComplex<TKSpace, TCellContainer>::
108//-----------------------------------------------------------------------------
109template <typename TKSpace, typename TCellContainer>
111typename DGtal::CubicalComplex<TKSpace, TCellContainer>::ConstIterator
112DGtal::CubicalComplex<TKSpace, TCellContainer>::
115 return ConstIterator( *this, 0 );
118//-----------------------------------------------------------------------------
119template <typename TKSpace, typename TCellContainer>
121typename DGtal::CubicalComplex<TKSpace, TCellContainer>::ConstIterator
122DGtal::CubicalComplex<TKSpace, TCellContainer>::
125 return ConstIterator( *this, dimension+1 );
128//-----------------------------------------------------------------------------
129template <typename TKSpace, typename TCellContainer>
131typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Iterator
132DGtal::CubicalComplex<TKSpace, TCellContainer>::
135 return Iterator( *this, 0 );
138//-----------------------------------------------------------------------------
139template <typename TKSpace, typename TCellContainer>
141typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Iterator
142DGtal::CubicalComplex<TKSpace, TCellContainer>::
145 return Iterator( *this, dimension+1 );
148//-----------------------------------------------------------------------------
149template <typename TKSpace, typename TCellContainer>
151DGtal::CubicalComplex<TKSpace, TCellContainer>&
152DGtal::CubicalComplex<TKSpace, TCellContainer>::
153operator=( const CubicalComplex& other )
155 if ( this != &other )
157 myKSpace = other.myKSpace;
158 myCells = other.myCells;
163//-----------------------------------------------------------------------------
164template <typename TKSpace, typename TCellContainer>
167DGtal::CubicalComplex<TKSpace, TCellContainer>::
170 for ( Dimension d = 0; d <= dimension; ++d )
174//-----------------------------------------------------------------------------
175template <typename TKSpace, typename TCellContainer>
177typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Size
178DGtal::CubicalComplex<TKSpace, TCellContainer>::
179count( const Cell& aCell ) const
181 return belongs( aCell ) ? 1 : 0;
184//-----------------------------------------------------------------------------
185template <typename TKSpace, typename TCellContainer>
188DGtal::CubicalComplex<TKSpace, TCellContainer>::
191 myCells[ d ].clear();
193//-----------------------------------------------------------------------------
194template <typename TKSpace, typename TCellContainer>
197DGtal::CubicalComplex<TKSpace, TCellContainer>::
200 for ( Dimension d = 0; d <= dimension; ++d )
203//-----------------------------------------------------------------------------
204template <typename TKSpace, typename TCellContainer>
207DGtal::CubicalComplex<TKSpace, TCellContainer>::
208fillData( Dimension d, Data data )
210 ASSERT( d <= dimension );
211 for ( CellMapIterator it = begin( d ), itE = end( d ); it != itE; ++it )
217//-----------------------------------------------------------------------------
218template <typename TKSpace, typename TCellContainer>
221DGtal::CubicalComplex<TKSpace, TCellContainer>::
224 Dimension d = static_cast<Dimension>((int)myCells.size()-1);
225 for ( typename std::vector<CellMap>::const_reverse_iterator it = myCells.rbegin(), itE = myCells.rend();
226 it != itE; ++it, --d )
227 if ( ! it->empty() ) return d;
230//-----------------------------------------------------------------------------
231template <typename TKSpace, typename TCellContainer>
234DGtal::CubicalComplex<TKSpace, TCellContainer>::
235dim( const Cell& c ) const
237 return myKSpace->uDim( c );
240//-----------------------------------------------------------------------------
241template <typename TKSpace, typename TCellContainer>
243typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Size
244DGtal::CubicalComplex<TKSpace, TCellContainer>::
245nbCells( Dimension d ) const
247 return static_cast<Size>(myCells[ d ].size());
250//-----------------------------------------------------------------------------
251template <typename TKSpace, typename TCellContainer>
253typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Size
254DGtal::CubicalComplex<TKSpace, TCellContainer>::
258 for ( Dimension d = 0; d <= dimension; ++d )
259 n += myCells[ d ].size();
262//-----------------------------------------------------------------------------
263template <typename TKSpace, typename TCellContainer>
265typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Size
266DGtal::CubicalComplex<TKSpace, TCellContainer>::
270 Point p = space().uKCoords( space().upperCell() )
271 - space().uKCoords( space().lowerCell() );
272 Size n = (Size) p.norm( Point::L_1 );
276//-----------------------------------------------------------------------------
277template <typename TKSpace, typename TCellContainer>
280DGtal::CubicalComplex<TKSpace, TCellContainer>::
283 for ( Dimension d = 0; d <= dimension; ++d )
284 if ( ! myCells[ d ].empty() ) return false;
288//-----------------------------------------------------------------------------
289template <typename TKSpace, typename TCellContainer>
291typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Integer
292DGtal::CubicalComplex<TKSpace, TCellContainer>::
297 for ( typename std::vector<CellMap>::const_iterator it = myCells.begin(), itE = myCells.end();
300 n += pos ? (Integer) it->size() : -(Integer) it->size();
306//-----------------------------------------------------------------------------
307template <typename TKSpace, typename TCellContainer>
309std::pair< typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Iterator,
310 typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Iterator >
311DGtal::CubicalComplex<TKSpace, TCellContainer>::
312equal_range( const Cell& aCell )
314 Dimension d = myKSpace->uDim( aCell );
315 std::pair< CellMapIterator, CellMapIterator > pIt
316 = myCells[ d ].equal_range( aCell );
317 return std::make_pair( Iterator( *this, d, pIt->first ),
318 Iterator( *this, d, pIt->second ) );
321//-----------------------------------------------------------------------------
322template <typename TKSpace, typename TCellContainer>
324std::pair< typename DGtal::CubicalComplex<TKSpace, TCellContainer>::ConstIterator,
325 typename DGtal::CubicalComplex<TKSpace, TCellContainer>::ConstIterator >
326DGtal::CubicalComplex<TKSpace, TCellContainer>::
327equal_range( const Cell& aCell ) const
329 Dimension d = myKSpace->uDim( aCell );
330 std::pair< CellMapConstIterator, CellMapConstIterator > pIt
331 = myCells[ d ].equal_range( aCell );
332 return std::make_pair( ConstIterator( *this, d, pIt->first ),
333 ConstIterator( *this, d, pIt->second ) );
336//-----------------------------------------------------------------------------
337template <typename TKSpace, typename TCellContainer>
340DGtal::CubicalComplex<TKSpace, TCellContainer>::
343 eraseCell( it.myIt );
346//-----------------------------------------------------------------------------
347template <typename TKSpace, typename TCellContainer>
349typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Size
350DGtal::CubicalComplex<TKSpace, TCellContainer>::
351erase( const Cell& aCell )
353 return eraseCell( aCell );
356//-----------------------------------------------------------------------------
357template <typename TKSpace, typename TCellContainer>
360DGtal::CubicalComplex<TKSpace, TCellContainer>::
361erase( Iterator first, Iterator last )
363 while ( first != last )
365 Iterator tmp = first++;
370//-----------------------------------------------------------------------------
371template <typename TKSpace, typename TCellContainer>
373typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Iterator
374DGtal::CubicalComplex<TKSpace, TCellContainer>::
375find( const Cell& aCell )
377 Dimension d = myKSpace->uDim( aCell );
378 CellMapIterator cmIt = findCell( d, aCell );
379 if ( cmIt == end( d ) ) return Iterator( *this, d+1 );
380 else return Iterator( *this, d, cmIt );
383//-----------------------------------------------------------------------------
384template <typename TKSpace, typename TCellContainer>
386typename DGtal::CubicalComplex<TKSpace, TCellContainer>::ConstIterator
387DGtal::CubicalComplex<TKSpace, TCellContainer>::
388find( const Cell& aCell ) const
390 Dimension d = myKSpace->uDim( aCell );
391 CellMapConstIterator cmIt = findCell( d, aCell );
392 if ( cmIt == end( d ) ) return ConstIterator( *this, d+1 );
393 else return ConstIterator( *this, d, cmIt );
396//-----------------------------------------------------------------------------
397template <typename TKSpace, typename TCellContainer>
399std::pair< typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Iterator,
401DGtal::CubicalComplex<TKSpace, TCellContainer>::
402insert( const Cell& aCell )
404 Dimension d = myKSpace->uDim( aCell );
405 std::pair< CellMapIterator,bool > pIt
406 = myCells[ d ].insert( std::make_pair( aCell, Data() ) );
407 return ( pIt.first == myCells[ d ].end() )
408 ? std::make_pair( Iterator( *this, dimension+1 ), false )
409 : std::make_pair( Iterator( *this, d, pIt.first ), pIt.second );
412//-----------------------------------------------------------------------------
413template <typename TKSpace, typename TCellContainer>
415typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Iterator
416DGtal::CubicalComplex<TKSpace, TCellContainer>::
417insert( Iterator position, const Cell& aCell )
419 Dimension d = myKSpace->uDim( aCell );
420 if ( position.dimension() == d )
421 return Iterator( *this, d,
422 myCells[ d ].insert( position.myIt,
423 std::make_pair( aCell, Data() ) ) );
425 return Iterator( *this, d,
426 myCells[ d ].insert( std::make_pair( aCell, Data() ) ).first );
428//-----------------------------------------------------------------------------
429template <typename TKSpace, typename TCellContainer>
430template <class InputIterator>
433DGtal::CubicalComplex<TKSpace, TCellContainer>::
434insert( InputIterator first, InputIterator last )
436 for ( ; first != last; ++first )
440//-----------------------------------------------------------------------------
441template <typename TKSpace, typename TCellContainer>
444DGtal::CubicalComplex<TKSpace, TCellContainer>::
445swap( CubicalComplex& other )
447 if ( other != *this )
448 { // Swap space with special care of invalid complexes.
449 if ( myKSpace == 0 ) myKSpace = other.myKSpace;
450 else if ( other.myKSpace == 0 ) other.myKSpace = myKSpace;
451 else std::swap( myKSpace, other.myKSpace );
452 myCells.swap( other.myCells );
457//-----------------------------------------------------------------------------
458template <typename TKSpace, typename TCellContainer>
460typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Data&
461DGtal::CubicalComplex<TKSpace, TCellContainer>::
462operator[]( const Cell& aCell )
464 Dimension d = space().uDim( aCell );
465 return myCells[ d ][ aCell ];
468//-----------------------------------------------------------------------------
469template <typename TKSpace, typename TCellContainer>
472DGtal::CubicalComplex<TKSpace, TCellContainer>::
473operator()( const Cell& aCell ) const
475 Dimension d = space().uDim( aCell );
476 return findCell( d, aCell ) != end( d );
479//-----------------------------------------------------------------------------
480template <typename TKSpace, typename TCellContainer>
483DGtal::CubicalComplex<TKSpace, TCellContainer>::
484insertCell( const Cell& aCell, const Data& data )
486 insertCell( myKSpace->uDim( aCell ), aCell, data );
488//-----------------------------------------------------------------------------
489template <typename TKSpace, typename TCellContainer>
492DGtal::CubicalComplex<TKSpace, TCellContainer>::
493insertCell( Dimension d, const Cell& aCell, const Data& data )
495 myCells[ d ][ aCell ] = data;
498//-----------------------------------------------------------------------------
499template <typename TKSpace, typename TCellContainer>
500template <typename CellConstIterator>
503DGtal::CubicalComplex<TKSpace, TCellContainer>::
504insertCells( CellConstIterator it, CellConstIterator itE, const Data& data )
506 for ( ; it != itE; ++it )
507 insertCell( *it, data );
510//-----------------------------------------------------------------------------
511template <typename TKSpace, typename TCellContainer>
512template <typename CellConstIterator>
515DGtal::CubicalComplex<TKSpace, TCellContainer>::
516insertCells( Dimension d, CellConstIterator it, CellConstIterator itE, const Data& data )
518 for ( ; it != itE; ++it )
519 insertCell( d, *it, data );
522//-----------------------------------------------------------------------------
523template <typename TKSpace, typename TCellContainer>
525typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Size
526DGtal::CubicalComplex<TKSpace, TCellContainer>::
527eraseCell( const Cell& aCell )
529 return eraseCell( myKSpace->uDim( aCell ), aCell );
532//-----------------------------------------------------------------------------
533template <typename TKSpace, typename TCellContainer>
535typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Size
536DGtal::CubicalComplex<TKSpace, TCellContainer>::
537eraseCell( Dimension d, const Cell& aCell )
539 return (Size) myCells[ d ].erase( aCell );
542//-----------------------------------------------------------------------------
543template <typename TKSpace, typename TCellContainer>
546DGtal::CubicalComplex<TKSpace, TCellContainer>::
547eraseCell( CellMapIterator it )
549 Dimension d = myKSpace->uDim( it->first );
550 myCells[ d ].erase( it );
553//-----------------------------------------------------------------------------
554template <typename TKSpace, typename TCellContainer>
557DGtal::CubicalComplex<TKSpace, TCellContainer>::
558eraseCells( CellMapIterator it, CellMapIterator itE )
560 for ( ; it != itE; ++it )
564//-----------------------------------------------------------------------------
565template <typename TKSpace, typename TCellContainer>
566template <typename CellConstIterator>
568typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Size
569DGtal::CubicalComplex<TKSpace, TCellContainer>::
570eraseCells( CellConstIterator it, CellConstIterator itE )
573 for ( ; it != itE; ++it )
574 nb += eraseCell( *it );
577//-----------------------------------------------------------------------------
578template <typename TKSpace, typename TCellContainer>
579template <typename CellConstIterator>
581typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Size
582DGtal::CubicalComplex<TKSpace, TCellContainer>::
583eraseCells( Dimension d, CellConstIterator it, CellConstIterator itE )
586 for ( ; it != itE; ++it )
587 nb += eraseCell( d, *it );
590//-----------------------------------------------------------------------------
591template <typename TKSpace, typename TCellContainer>
594DGtal::CubicalComplex<TKSpace, TCellContainer>::
595belongs( const Cell& aCell ) const
597 return belongs( myKSpace->uDim( aCell ), aCell );
599//-----------------------------------------------------------------------------
600template <typename TKSpace, typename TCellContainer>
603DGtal::CubicalComplex<TKSpace, TCellContainer>::
604belongs( Dimension d, const Cell& aCell ) const
606 ASSERT( d <= dimension );
607 CellMapConstIterator it = myCells[ d ].find( aCell );
608 return it != myCells[ d ].end();
610//-----------------------------------------------------------------------------
611template <typename TKSpace, typename TCellContainer>
614DGtal::CubicalComplex<TKSpace, TCellContainer>::
615belongs( const PreCell& aCell ) const
617 return belongs( TKSpace::PreCellularGridSpace::uDim( aCell ), aCell );
619//-----------------------------------------------------------------------------
620template <typename TKSpace, typename TCellContainer>
623DGtal::CubicalComplex<TKSpace, TCellContainer>::
624belongs( Dimension d, const PreCell& aCell ) const
626 if (!myKSpace->uIsValid(aCell)) return false;
627 return belongs(d, myKSpace->uCell(aCell) );
629//-----------------------------------------------------------------------------
630template <typename TKSpace, typename TCellContainer>
631template <typename CellOutputIterator>
634DGtal::CubicalComplex<TKSpace, TCellContainer>::
635faces( CellOutputIterator& outIt, const Cell& aCell, bool hintClosed ) const
637 Cells all_proper_faces = myKSpace->uFaces( aCell );
639 std::copy( all_proper_faces.begin(), all_proper_faces.end(), outIt );
641 for ( typename Cells::ConstIterator it = all_proper_faces.begin(),
642 itE = all_proper_faces.end(); it != itE; ++it )
643 if ( belongs( *it ) )
646//-----------------------------------------------------------------------------
647template <typename TKSpace, typename TCellContainer>
648template <typename CellOutputIterator>
651DGtal::CubicalComplex<TKSpace, TCellContainer>::
652coFaces( CellOutputIterator& outIt, const Cell& aCell, bool hintOpen ) const
654 Cells all_proper_co_faces = myKSpace->uCoFaces( aCell );
656 std::copy( all_proper_co_faces.begin(), all_proper_co_faces.end(), outIt );
658 for ( typename Cells::ConstIterator it = all_proper_co_faces.begin(),
659 itE = all_proper_co_faces.end(); it != itE; ++it )
660 if ( belongs( *it ) )
663//-----------------------------------------------------------------------------
664template <typename TKSpace, typename TCellContainer>
665template <typename CellOutputIterator>
668DGtal::CubicalComplex<TKSpace, TCellContainer>::
669directFaces( CellOutputIterator& outIt, const Cell& aCell, bool hintClosed ) const
671 Cells all_direct_faces = myKSpace->uLowerIncident( aCell );
673 std::copy( all_direct_faces.begin(), all_direct_faces.end(), outIt );
675 for ( typename Cells::ConstIterator it = all_direct_faces.begin(),
676 itE = all_direct_faces.end(); it != itE; ++it )
677 if ( belongs( *it ) )
680//-----------------------------------------------------------------------------
681template <typename TKSpace, typename TCellContainer>
682template <typename CellMapIteratorOutputIterator>
685DGtal::CubicalComplex<TKSpace, TCellContainer>::
686directFacesIterators( CellMapIteratorOutputIterator& outIt, const Cell& aCell )
688 Cells all_direct_faces = myKSpace->uLowerIncident( aCell );
689 Dimension k = dim( aCell );
690 for ( typename Cells::ConstIterator it = all_direct_faces.begin(),
691 itE = all_direct_faces.end(); it != itE; ++it )
693 CellMapIterator map_it = findCell( *it );
694 if ( map_it != end( k-1 ) )
698//-----------------------------------------------------------------------------
699template <typename TKSpace, typename TCellContainer>
700template <typename CellOutputIterator>
703DGtal::CubicalComplex<TKSpace, TCellContainer>::
704directCoFaces( CellOutputIterator& outIt, const Cell& aCell, bool hintOpen ) const
706 Cells all_direct_co_faces = myKSpace->uUpperIncident( aCell );
708 std::copy( all_direct_co_faces.begin(), all_direct_co_faces.end(), outIt );
710 for ( typename Cells::ConstIterator it = all_direct_co_faces.begin(),
711 itE = all_direct_co_faces.end(); it != itE; ++it )
712 if ( belongs( *it ) )
715//-----------------------------------------------------------------------------
716template <typename TKSpace, typename TCellContainer>
717template <typename CellMapIteratorOutputIterator>
720DGtal::CubicalComplex<TKSpace, TCellContainer>::
721directCoFacesIterators( CellMapIteratorOutputIterator& outIt, const Cell& aCell )
723 Cells all_direct_faces = myKSpace->uUpperIncident( aCell );
724 Dimension k = dim( aCell );
725 for ( typename Cells::ConstIterator it = all_direct_faces.begin(),
726 itE = all_direct_faces.end(); it != itE; ++it )
728 CellMapIterator map_it = findCell( *it );
729 if ( map_it != end( k+1 ) )
734//-----------------------------------------------------------------------------
735template <typename TKSpace, typename TCellContainer>
736typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellMap &
737DGtal::CubicalComplex<TKSpace, TCellContainer>::getCells(const Dimension d)
742template <typename TKSpace, typename TCellContainer>
743const typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellMap &
744DGtal::CubicalComplex<TKSpace, TCellContainer>::getCells(const Dimension d) const
748//-----------------------------------------------------------------------------
749template <typename TKSpace, typename TCellContainer>
751typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellMapConstIterator
752DGtal::CubicalComplex<TKSpace, TCellContainer>::
753begin( Dimension d ) const
755 return myCells[ d ].begin();
758//-----------------------------------------------------------------------------
759template <typename TKSpace, typename TCellContainer>
761typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellMapConstIterator
762DGtal::CubicalComplex<TKSpace, TCellContainer>::
763end( Dimension d ) const
765 return myCells[ d ].end();
768//-----------------------------------------------------------------------------
769template <typename TKSpace, typename TCellContainer>
771typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellMapIterator
772DGtal::CubicalComplex<TKSpace, TCellContainer>::
775 return myCells[ d ].begin();
778//-----------------------------------------------------------------------------
779template <typename TKSpace, typename TCellContainer>
781typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellMapIterator
782DGtal::CubicalComplex<TKSpace, TCellContainer>::
785 return myCells[ d ].end();
788//-----------------------------------------------------------------------------
789template <typename TKSpace, typename TCellContainer>
792DGtal::CubicalComplex<TKSpace, TCellContainer>::
798//-----------------------------------------------------------------------------
799template <typename TKSpace, typename TCellContainer>
802DGtal::CubicalComplex<TKSpace, TCellContainer>::
805 if ( k <= 0 ) return;
807 for ( CellMapConstIterator it = begin( k ), itE = end( k );
810 Cells direct_faces = myKSpace->uLowerIncident( it->first );
811 for ( typename Cells::const_iterator cells_it = direct_faces.begin(),
812 cells_it_end = direct_faces.end(); cells_it != cells_it_end; ++cells_it )
813 insertCell( l, *cells_it );
818//-----------------------------------------------------------------------------
819template <typename TKSpace, typename TCellContainer>
822DGtal::CubicalComplex<TKSpace, TCellContainer>::
828//-----------------------------------------------------------------------------
829template <typename TKSpace, typename TCellContainer>
832DGtal::CubicalComplex<TKSpace, TCellContainer>::
838 for ( CellMapIterator it = begin( k ), itE = end( k ); it != itE; )
840 Cells direct_cofaces = myKSpace->uUpperIncident( it->first );
842 for ( typename Cells::const_iterator cells_it = direct_cofaces.begin(),
843 cells_it_end = direct_cofaces.end(); cells_it != cells_it_end; ++cells_it )
844 if ( ! belongs( l, *cells_it ) )
849 CellMapIterator itMem = it;
851 if ( ! is_open ) myCells[ k ].erase( itMem );
854 if ( k > 0 ) open( k - 1 );
857//-----------------------------------------------------------------------------
858template <typename TKSpace, typename TCellContainer>
860typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellMapConstIterator
861DGtal::CubicalComplex<TKSpace, TCellContainer>::
862findCell( const Cell& aCell ) const
864 return findCell( dim( aCell ), aCell );
867//-----------------------------------------------------------------------------
868template <typename TKSpace, typename TCellContainer>
870typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellMapConstIterator
871DGtal::CubicalComplex<TKSpace, TCellContainer>::
872findCell( Dimension d, const Cell& aCell ) const
874 return myCells[ d ].find( aCell );
877//-----------------------------------------------------------------------------
878template <typename TKSpace, typename TCellContainer>
880typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellMapIterator
881DGtal::CubicalComplex<TKSpace, TCellContainer>::
882findCell( const Cell& aCell )
884 return findCell( dim( aCell ), aCell );
887//-----------------------------------------------------------------------------
888template <typename TKSpace, typename TCellContainer>
890typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellMapIterator
891DGtal::CubicalComplex<TKSpace, TCellContainer>::
892findCell( Dimension d, const Cell& aCell )
894 return myCells[ d ].find( aCell );
899//-----------------------------------------------------------------------------
900template <typename TKSpace, typename TCellContainer>
902typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Cells
903DGtal::CubicalComplex<TKSpace, TCellContainer>::
904cellBoundary( const Cell& aCell, bool hintClosed ) const
906 if ( hintClosed ) return myKSpace->uFaces( aCell );
907 Cells proper_faces = myKSpace->uFaces( aCell );
908 // NB: (JOL) do not use std::remove_if since predicate is passed by value.
909 typename Cells::iterator first = proper_faces.begin();
910 typename Cells::iterator last = proper_faces.end();
911 typename Cells::iterator result = first;
912 for ( ; first != last; ++first )
914 if ( belongs( *first ) )
916 *result = std::move( *first );
920 proper_faces.resize( result - proper_faces.begin() );
923//-----------------------------------------------------------------------------
924template <typename TKSpace, typename TCellContainer>
926typename DGtal::CubicalComplex<TKSpace, TCellContainer>::Cells
927DGtal::CubicalComplex<TKSpace, TCellContainer>::
928cellCoBoundary( const Cell& aCell, bool hintOpen ) const
930 if ( hintOpen ) return myKSpace->uCoFaces( aCell );
931 Cells proper_cofaces = myKSpace->uCoFaces( aCell );
932 // NB: (JOL) do not use std::remove_if since predicate is passed by value.
933 typename Cells::iterator first = proper_cofaces.begin();
934 typename Cells::iterator last = proper_cofaces.end();
935 typename Cells::iterator result = first;
936 for ( ; first != last; ++first )
938 if ( belongs( *first ) )
940 *result = std::move( *first );
944 proper_cofaces.resize( result - proper_cofaces.begin() );
945 return proper_cofaces;
948//-----------------------------------------------------------------------------
949template <typename TKSpace, typename TCellContainer>
952DGtal::CubicalComplex<TKSpace, TCellContainer>::
953isCellInterior( const Cell& aCell ) const
955 Cells proper_cofaces = myKSpace->uCoFaces( aCell );
956 typename Cells::iterator first = proper_cofaces.begin();
957 typename Cells::iterator last = proper_cofaces.end();
958 for ( ; first != last; ++first )
960 if ( ! belongs( *first ) ) return false;
965//-----------------------------------------------------------------------------
966template <typename TKSpace, typename TCellContainer>
969DGtal::CubicalComplex<TKSpace, TCellContainer>::
970isCellBoundary( const Cell& aCell ) const
972 return ! isCellInterior( aCell );
975//-----------------------------------------------------------------------------
976template <typename TKSpace, typename TCellContainer>
978DGtal::CubicalComplex<TKSpace, TCellContainer>
979DGtal::CubicalComplex<TKSpace, TCellContainer>::
982 CubicalComplex I( space() );
983 for ( Dimension d = 0; d <= dimension; ++d )
985 for ( CellMapConstIterator it = begin( d ), itE = end( d ); it != itE; ++it )
987 if ( isCellInterior( it->first ) )
988 I.insertCell( d, it->first, it->second );
994//-----------------------------------------------------------------------------
995template <typename TKSpace, typename TCellContainer>
997DGtal::CubicalComplex<TKSpace, TCellContainer>
998DGtal::CubicalComplex<TKSpace, TCellContainer>::
999boundary( bool hintClosed ) const
1001 CubicalComplex B( *this );
1002 if ( ! hintClosed ) B.close();
1003 for ( Dimension d = 0; d <= dimension; ++d )
1005 CellMapIterator itNext;
1006 for ( CellMapIterator it = B.begin( d ), itE = B.end( d ); it != itE; )
1008 itNext = it; ++itNext;
1009 if ( B.isCellInterior( it->first ) )
1017//-----------------------------------------------------------------------------
1018template <typename TKSpace, typename TCellContainer>
1021DGtal::CubicalComplex<TKSpace, TCellContainer>::
1022getInteriorAndBoundary( CubicalComplex& intcc, CubicalComplex& bdcc,
1023 bool hintClosed ) const
1026 bdcc = CubicalComplex( this->space() );
1027 if ( ! hintClosed ) intcc.close();
1028 for ( Dimension d = 0; d <= dimension; ++d )
1030 CellMapIterator itNext;
1031 for ( CellMapIterator it = intcc.begin( d ), itE = intcc.end( d ); it != itE; )
1033 itNext = it; ++itNext;
1034 if ( ! intcc.isCellInterior( it->first ) )
1036 intcc.eraseCell( it );
1037 bdcc.insertCell( d, it->first, it->second );
1044//-----------------------------------------------------------------------------
1045template <typename TKSpace, typename TCellContainer>
1047typename DGtal::CubicalComplex<TKSpace, TCellContainer>
1048DGtal::CubicalComplex<TKSpace, TCellContainer>::
1049closure( const CubicalComplex& S, bool hintClosed ) const
1051 CubicalComplex cl_S = S;
1052 for ( ConstIterator it = S.begin(), itE = S.end(); it != itE; ++it )
1054 Cells cell_faces = cellBoundary( *it, hintClosed );
1055 cl_S.insert( cell_faces.begin(), cell_faces.end() );
1059//-----------------------------------------------------------------------------
1060template <typename TKSpace, typename TCellContainer>
1062typename DGtal::CubicalComplex<TKSpace, TCellContainer>
1063DGtal::CubicalComplex<TKSpace, TCellContainer>::
1064star( const CubicalComplex& S, bool hintOpen ) const
1066 CubicalComplex star_S = S;
1067 for ( ConstIterator it = S.begin(), itE = S.end(); it != itE; ++it )
1069 Cells cell_cofaces = cellCoBoundary( *it, hintOpen );
1070 star_S.insert( cell_cofaces.begin(), cell_cofaces.end() );
1074//-----------------------------------------------------------------------------
1075template <typename TKSpace, typename TCellContainer>
1077typename DGtal::CubicalComplex<TKSpace, TCellContainer>
1078DGtal::CubicalComplex<TKSpace, TCellContainer>::
1079link( const CubicalComplex& S, bool hintClosed, bool hintOpen ) const
1081 CubicalComplex cl_star_S = closure( star ( S, hintOpen ), hintClosed );
1082 CubicalComplex star_cl_S = star ( closure( S, hintClosed ), hintOpen );
1084 // Calls a specializable difference of sets operation.
1085 return cl_star_S - star_cl_S;
1088//-----------------------------------------------------------------------------
1089template <typename TKSpace, typename TCellContainer>
1091typename DGtal::CubicalComplex<TKSpace, TCellContainer>::CellType
1092DGtal::CubicalComplex<TKSpace, TCellContainer>::
1093computeCellType( const Cell& c, CellMapIterator& it_cell_up, Dimension n )
1095 // Check if it is a maximal cell
1096 Dimension k = dim( c );
1097 if ( k == n ) return Maximal;
1099 std::vector< CellMapIterator > Q_up;
1100 std::back_insert_iterator< std::vector< CellMapIterator > > back_it( Q_up );
1101 this->directCoFacesIterators( back_it, c );
1103 // Filtering unwanted up-incident cells.
1105 for ( typename std::vector< CellMapIterator >::const_iterator it = Q_up.begin(), itE = Q_up.end();
1108 uint32_t& data = (*it)->second.data;
1109 if ( ! ( data & COLLAPSIBLE ) ) return Any;
1110 if ( ! ( data & REMOVED ) )
1113 if ( nb > 1 ) return Any;
1117 return ( nb != 0 ) ? Free : Maximal ;
1122///////////////////////////////////////////////////////////////////////////////
1123// Interface - public :
1126 * Writes/Displays the object on an output stream.
1127 * @param out the output stream where the object is written.
1129template <typename TKSpace, typename TCellContainer>
1132DGtal::CubicalComplex<TKSpace, TCellContainer>::selfDisplay ( std::ostream & out ) const
1134 out << "[CubicalComplex dim=" << dim() << " chi=" << euler();
1135 for ( Dimension i = 0; i < myCells.size(); ++i )
1136 out << " #" << i << "=" << myCells[ i ].size();
1141 * Checks the validity/consistency of the object.
1142 * @return 'true' if the object is valid, 'false' otherwise.
1144template <typename TKSpace, typename TCellContainer>
1147DGtal::CubicalComplex<TKSpace, TCellContainer>::isValid() const
1152//-----------------------------------------------------------------------------
1153template <typename TKSpace, typename TCellContainer>
1156DGtal::CubicalComplex<TKSpace, TCellContainer>::className() const
1158 return "CubicalComplex";
1163///////////////////////////////////////////////////////////////////////////////
1164// Implementation of inline functions //
1166template <typename TKSpace, typename TCellContainer>
1169DGtal::operator<< ( std::ostream & out,
1170 const CubicalComplex<TKSpace, TCellContainer> & object )
1172 object.selfDisplay( out );
1177///////////////////////////////////////////////////////////////////////////////