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 // ------------------------------------------------------------------------
42 template <typename TMapImage>
44 DGtal::DigitalSetFromMap<TMapImage>::~DigitalSetFromMap()
48 // ------------------------------------------------------------------------
49 template <typename TMapImage>
50 inline DGtal::DigitalSetFromMap<TMapImage>::DigitalSetFromMap(
51 typename DGtal::DigitalSetFromMap<TMapImage>::Image & aImage,
52 const typename DGtal::DigitalSetFromMap<TMapImage>::Image::Value & aDefaultValue )
53 : myImgPtr( &aImage ), myFun( Functor() ), myDefault( aDefaultValue )
56 // ------------------------------------------------------------------------
57 template <typename TMapImage>
59 DGtal::DigitalSetFromMap<TMapImage>
60 ::DigitalSetFromMap( const DigitalSetFromMap<TMapImage> & other )
61 : myImgPtr( other.myImgPtr ), myFun( other.myFun ), myDefault (other.myDefault)
65 // ------------------------------------------------------------------------
66 template <typename TMapImage>
68 DGtal::DigitalSetFromMap<TMapImage> &
69 DGtal::DigitalSetFromMap<TMapImage>
70 ::operator= ( const DigitalSetFromMap<TMapImage> & other )
74 myImgPtr = other.myImgPtr;
76 myDefault = other.myDefault;
81 // ------------------------------------------------------------------------
82 template <typename TMapImage>
84 const typename DGtal::DigitalSetFromMap<TMapImage>::Domain&
85 DGtal::DigitalSetFromMap<TMapImage>::domain() const
87 return myImgPtr->domain();
90 // ------------------------------------------------------------------------
91 template <typename TMapImage>
93 DGtal::CowPtr<typename DGtal::DigitalSetFromMap<TMapImage>::Domain>
94 DGtal::DigitalSetFromMap<TMapImage>::domainPointer() const
96 return CowPtr<Domain>( new Domain( myImgPtr->domain() ) );
101 ///////////////////////////////////////////////////////////////////////////////
102 // Interface - public :
105 // ------------------------------------------------------------------------
106 template <typename TMapImage>
108 typename DGtal::DigitalSetFromMap<TMapImage>::Size
109 DGtal::DigitalSetFromMap<TMapImage>::size() const
111 return static_cast<Size>(myImgPtr->size());
114 // ------------------------------------------------------------------------
115 template <typename TMapImage>
118 DGtal::DigitalSetFromMap<TMapImage>::empty() const
120 return myImgPtr->empty();
124 // ------------------------------------------------------------------------
125 template <typename TMapImage>
128 DGtal::DigitalSetFromMap<TMapImage>::insert( const Point & p )
130 ASSERT( this->domain().isInside( p ) );
131 myImgPtr->insert( Pair( p, myDefault ) );
135 // ------------------------------------------------------------------------
136 template <typename TMapImage>
137 template <typename PointInputIterator>
139 DGtal::DigitalSetFromMap<TMapImage>::insert( PointInputIterator first, PointInputIterator last )
141 for (PointInputIterator it = first; it != last; ++it)
146 // ------------------------------------------------------------------------
147 template <typename TMapImage>
150 DGtal::DigitalSetFromMap<TMapImage>::insertNew( const Point & p )
152 ASSERT( this->domain().isInside( p ) );
153 myImgPtr->insert( Pair( p, myDefault ) );
156 // ------------------------------------------------------------------------
157 template <typename TMapImage>
158 template <typename PointInputIterator>
161 DGtal::DigitalSetFromMap<TMapImage>::insertNew
162 ( PointInputIterator first, PointInputIterator last )
164 for (PointInputIterator it = first; it != last; ++it)
168 // ------------------------------------------------------------------------
169 template <typename TMapImage>
170 typename DGtal::DigitalSetFromMap<TMapImage>::Size
171 DGtal::DigitalSetFromMap<TMapImage>::erase( const Point & p )
173 return static_cast<Size>(myImgPtr->erase( p ));
176 // ------------------------------------------------------------------------
177 template <typename TMapImage>
180 DGtal::DigitalSetFromMap<TMapImage>::erase( Iterator it )
182 myImgPtr->erase( *it );
185 // ------------------------------------------------------------------------
186 template <typename TMapImage>
189 DGtal::DigitalSetFromMap<TMapImage>::clear()
194 // ------------------------------------------------------------------------
195 template <typename TMapImage>
197 typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
198 DGtal::DigitalSetFromMap<TMapImage>::find( const Point & p ) const
200 return ConstIterator( myImgPtr->find( p ), myFun );
203 // ------------------------------------------------------------------------
204 template <typename TMapImage>
206 typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
207 DGtal::DigitalSetFromMap<TMapImage>::find( const Point & p )
209 return Iterator( myImgPtr->find( p ), myFun );
212 // ------------------------------------------------------------------------
213 template <typename TMapImage>
215 typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
216 DGtal::DigitalSetFromMap<TMapImage>::begin() const
218 return ConstIterator( myImgPtr->begin(), myFun );
221 // ------------------------------------------------------------------------
222 template <typename TMapImage>
224 typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
225 DGtal::DigitalSetFromMap<TMapImage>::end() const
227 return ConstIterator( myImgPtr->end(), myFun );
230 // ------------------------------------------------------------------------
231 template <typename TMapImage>
233 typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
234 DGtal::DigitalSetFromMap<TMapImage>::begin()
236 return Iterator( myImgPtr->begin(), myFun );
239 // ------------------------------------------------------------------------
240 template <typename TMapImage>
242 typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
243 DGtal::DigitalSetFromMap<TMapImage>::end()
245 return Iterator( myImgPtr->end(), myFun );
248 // ------------------------------------------------------------------------
249 template <typename TMapImage>
250 template <typename TDigitalSet>
252 DGtal::DigitalSetFromMap<TMapImage> &
253 DGtal::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 // ------------------------------------------------------------------------
269 template <typename TMapImage>
272 DGtal::DigitalSetFromMap<TMapImage>
273 ::operator()( const Point & p ) const
275 return myImgPtr->find( p ) != myImgPtr->end();
278 ///////////////////////////////////////////////////////////////////////////////
279 // ----------------------- Other Set services -----------------------------
281 // ------------------------------------------------------------------------
282 template <typename TMapImage>
283 template <typename TOutputIterator>
286 DGtal::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 // ------------------------------------------------------------------------
301 template <typename TMapImage>
302 template <typename TDigitalSet>
305 DGtal::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 // ------------------------------------------------------------------------
321 template <typename TMapImage>
324 DGtal::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 :
342 template <typename TMapImage>
345 DGtal::DigitalSetFromMap<TMapImage>::selfDisplay ( std::ostream & out ) const
347 out << "[DigitalSetFromMap]" << " size=" << size();
350 template <typename TMapImage>
353 DGtal::DigitalSetFromMap<TMapImage>::isValid() const
355 return ( (myImgPtr) && (myImgPtr->isValid()) );
359 // --------------- CDrawableWithBoard2D realization -------------------------
361 template<typename TMapImage>
364 DGtal::DigitalSetFromMap<TMapImage>::className() const
366 return "DigitalSetFromMap";
369 ///////////////////////////////////////////////////////////////////////////////
370 // Implementation of inline function //
372 template <typename TMapImage>
375 DGtal::operator<< ( std::ostream & out, const DGtal::DigitalSetFromMap<TMapImage> & object )
377 object.selfDisplay( out );
382 ///////////////////////////////////////////////////////////////////////////////