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/>.
19 * @author Tristan Roussillon (\c tristan.roussillon@liris.cnrs.fr )
20 * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
24 * Implementation of inline methods defined in DigitalSetFromMap.h
26 * This file is part of the DGtal library.
30//////////////////////////////////////////////////////////////////////////////
32//////////////////////////////////////////////////////////////////////////////
33#include "DGtal/kernel/sets/DigitalSetFromMap.h"
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
38///////////////////////////////////////////////////////////////////////////////
39// ----------------------- Standard services ------------------------------
41// ------------------------------------------------------------------------
42template <typename TMapImage>
44DGtal::DigitalSetFromMap<TMapImage>::~DigitalSetFromMap()
48// ------------------------------------------------------------------------
49template <typename TMapImage>
50inline DGtal::DigitalSetFromMap<TMapImage>::DigitalSetFromMap(
51typename DGtal::DigitalSetFromMap<TMapImage>::Image & aImage,
52const typename DGtal::DigitalSetFromMap<TMapImage>::Image::Value & aDefaultValue )
53 : myImgPtr( &aImage ), myFun( Functor() ), myDefault( aDefaultValue )
56// ------------------------------------------------------------------------
57template <typename TMapImage>
59DGtal::DigitalSetFromMap<TMapImage>
60::DigitalSetFromMap( const DigitalSetFromMap<TMapImage> & other )
61 : myImgPtr( other.myImgPtr ), myFun( other.myFun ), myDefault (other.myDefault)
65// ------------------------------------------------------------------------
66template <typename TMapImage>
68DGtal::DigitalSetFromMap<TMapImage> &
69DGtal::DigitalSetFromMap<TMapImage>
70::operator= ( const DigitalSetFromMap<TMapImage> & other )
74 myImgPtr = other.myImgPtr;
76 myDefault = other.myDefault;
81// ------------------------------------------------------------------------
82template <typename TMapImage>
84const typename DGtal::DigitalSetFromMap<TMapImage>::Domain&
85DGtal::DigitalSetFromMap<TMapImage>::domain() const
87 return myImgPtr->domain();
90// ------------------------------------------------------------------------
91template <typename TMapImage>
93DGtal::CowPtr<typename DGtal::DigitalSetFromMap<TMapImage>::Domain>
94DGtal::DigitalSetFromMap<TMapImage>::domainPointer() const
96 return CowPtr<Domain>( new Domain( myImgPtr->domain() ) );
101///////////////////////////////////////////////////////////////////////////////
102// Interface - public :
105// ------------------------------------------------------------------------
106template <typename TMapImage>
108typename DGtal::DigitalSetFromMap<TMapImage>::Size
109DGtal::DigitalSetFromMap<TMapImage>::size() const
111 return static_cast<Size>(myImgPtr->size());
114// ------------------------------------------------------------------------
115template <typename TMapImage>
118DGtal::DigitalSetFromMap<TMapImage>::empty() const
120 return myImgPtr->empty();
124// ------------------------------------------------------------------------
125template <typename TMapImage>
128DGtal::DigitalSetFromMap<TMapImage>::insert( const Point & p )
130 ASSERT( this->domain().isInside( p ) );
131 myImgPtr->insert( Pair( p, myDefault ) );
135// ------------------------------------------------------------------------
136template <typename TMapImage>
137template <typename PointInputIterator>
139DGtal::DigitalSetFromMap<TMapImage>::insert( PointInputIterator first, PointInputIterator last )
141 for (PointInputIterator it = first; it != last; ++it)
146// ------------------------------------------------------------------------
147template <typename TMapImage>
150DGtal::DigitalSetFromMap<TMapImage>::insertNew( const Point & p )
152 ASSERT( this->domain().isInside( p ) );
153 myImgPtr->insert( Pair( p, myDefault ) );
156// ------------------------------------------------------------------------
157template <typename TMapImage>
158template <typename PointInputIterator>
161DGtal::DigitalSetFromMap<TMapImage>::insertNew
162( PointInputIterator first, PointInputIterator last )
164 for (PointInputIterator it = first; it != last; ++it)
168// ------------------------------------------------------------------------
169template <typename TMapImage>
170typename DGtal::DigitalSetFromMap<TMapImage>::Size
171DGtal::DigitalSetFromMap<TMapImage>::erase( const Point & p )
173 return static_cast<Size>(myImgPtr->erase( p ));
176// ------------------------------------------------------------------------
177template <typename TMapImage>
180DGtal::DigitalSetFromMap<TMapImage>::erase( Iterator it )
182 myImgPtr->erase( *it );
185// ------------------------------------------------------------------------
186template <typename TMapImage>
189DGtal::DigitalSetFromMap<TMapImage>::clear()
194// ------------------------------------------------------------------------
195template <typename TMapImage>
197typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
198DGtal::DigitalSetFromMap<TMapImage>::find( const Point & p ) const
200 return ConstIterator( myImgPtr->find( p ), myFun );
203// ------------------------------------------------------------------------
204template <typename TMapImage>
206typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
207DGtal::DigitalSetFromMap<TMapImage>::find( const Point & p )
209 return Iterator( myImgPtr->find( p ), myFun );
212// ------------------------------------------------------------------------
213template <typename TMapImage>
215typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
216DGtal::DigitalSetFromMap<TMapImage>::begin() const
218 return ConstIterator( myImgPtr->begin(), myFun );
221// ------------------------------------------------------------------------
222template <typename TMapImage>
224typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
225DGtal::DigitalSetFromMap<TMapImage>::end() const
227 return ConstIterator( myImgPtr->end(), myFun );
230// ------------------------------------------------------------------------
231template <typename TMapImage>
233typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
234DGtal::DigitalSetFromMap<TMapImage>::begin()
236 return Iterator( myImgPtr->begin(), myFun );
239// ------------------------------------------------------------------------
240template <typename TMapImage>
242typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
243DGtal::DigitalSetFromMap<TMapImage>::end()
245 return Iterator( myImgPtr->end(), myFun );
248// ------------------------------------------------------------------------
249template <typename TMapImage>
250template <typename TDigitalSet>
252DGtal::DigitalSetFromMap<TMapImage> &
253DGtal::DigitalSetFromMap<TMapImage>
254::operator+=( const TDigitalSet & aSet )
258 typename TMapImage::iterator itDst = myImgPtr->end();
259 for ( typename TDigitalSet::ConstIterator itSrc = aSet.begin();
263 itDst = myImgPtr->insert( itDst, Pair(*itSrc, myDefault) );
268// ------------------------------------------------------------------------
269template <typename TMapImage>
272DGtal::DigitalSetFromMap<TMapImage>
273::operator()( const Point & p ) const
275 return myImgPtr->find( p ) != myImgPtr->end();
278///////////////////////////////////////////////////////////////////////////////
279// ----------------------- Other Set services -----------------------------
281// ------------------------------------------------------------------------
282template <typename TMapImage>
283template <typename TOutputIterator>
286DGtal::DigitalSetFromMap<TMapImage>::computeComplement
287(TOutputIterator& ito) const
289 Domain d = this->domain();
290 typename Domain::ConstIterator itPoint = d.begin();
291 typename Domain::ConstIterator itEnd = d.end();
292 while ( itPoint != itEnd ) {
293 if ( this->find( *itPoint ) == end() ) {
300// ------------------------------------------------------------------------
301template <typename TMapImage>
302template <typename TDigitalSet>
305DGtal::DigitalSetFromMap<TMapImage>::assignFromComplement
306( const TDigitalSet& otherSet )
309 Domain d = this->domain();
310 typename Domain::ConstIterator itPoint = d.begin();
311 typename Domain::ConstIterator itEnd = d.end();
312 while ( itPoint != itEnd ) {
313 if ( otherSet.find( *itPoint ) == otherSet.end() ) {
314 this->insert( *itPoint );
320// ------------------------------------------------------------------------
321template <typename TMapImage>
324DGtal::DigitalSetFromMap<TMapImage>::computeBoundingBox
325( Point & lower, Point & upper ) const
327 Domain d = this->domain();
328 lower = d.upperBound();
329 upper = d.lowerBound();
330 ConstIterator it = this->begin();
331 ConstIterator itEnd = this->end();
332 while ( it != itEnd ) {
333 lower = lower.inf( *it );
334 upper = upper.sup( *it );
339///////////////////////////////////////////////////////////////////////////////
340// Interface - public :
342template <typename TMapImage>
345DGtal::DigitalSetFromMap<TMapImage>::selfDisplay ( std::ostream & out ) const
347 out << "[DigitalSetFromMap]" << " size=" << size();
350template <typename TMapImage>
353DGtal::DigitalSetFromMap<TMapImage>::isValid() const
355 return ( (myImgPtr) && (myImgPtr->isValid()) );
359// --------------- CDrawableWithBoard2D realization -------------------------
361template<typename TMapImage>
364DGtal::DigitalSetFromMap<TMapImage>::className() const
366 return "DigitalSetFromMap";
369///////////////////////////////////////////////////////////////////////////////
370// Implementation of inline function //
372template <typename TMapImage>
375DGtal::operator<< ( std::ostream & out, const DGtal::DigitalSetFromMap<TMapImage> & object )
377 object.selfDisplay( out );
382///////////////////////////////////////////////////////////////////////////////