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>
51 DGtal::DigitalSetFromMap<TMapImage>
52 ::DigitalSetFromMap( Image& aImage,
53 const typename Image::Value& aDefaultValue )
54 : myImgPtr( &aImage ), myFun( Functor() ), myDefault( aDefaultValue )
57 // ------------------------------------------------------------------------
58 template <typename TMapImage>
60 DGtal::DigitalSetFromMap<TMapImage>
61 ::DigitalSetFromMap( const DigitalSetFromMap<TMapImage> & other )
62 : myImgPtr( other.myImgPtr ), myFun( other.myFun ), myDefault (other.myDefault)
66 // ------------------------------------------------------------------------
67 template <typename TMapImage>
69 DGtal::DigitalSetFromMap<TMapImage> &
70 DGtal::DigitalSetFromMap<TMapImage>
71 ::operator= ( const DigitalSetFromMap<TMapImage> & other )
75 myImgPtr = other.myImgPtr;
77 myDefault = other.myDefault;
82 // ------------------------------------------------------------------------
83 template <typename TMapImage>
85 const typename DGtal::DigitalSetFromMap<TMapImage>::Domain&
86 DGtal::DigitalSetFromMap<TMapImage>::domain() const
88 return myImgPtr->domain();
91 // ------------------------------------------------------------------------
92 template <typename TMapImage>
94 DGtal::CowPtr<typename DGtal::DigitalSetFromMap<TMapImage>::Domain>
95 DGtal::DigitalSetFromMap<TMapImage>::domainPointer() const
97 return CowPtr<Domain>( new Domain( myImgPtr->domain() ) );
102 ///////////////////////////////////////////////////////////////////////////////
103 // Interface - public :
106 // ------------------------------------------------------------------------
107 template <typename TMapImage>
109 typename DGtal::DigitalSetFromMap<TMapImage>::Size
110 DGtal::DigitalSetFromMap<TMapImage>::size() const
112 return myImgPtr->size();
115 // ------------------------------------------------------------------------
116 template <typename TMapImage>
119 DGtal::DigitalSetFromMap<TMapImage>::empty() const
121 return myImgPtr->empty();
125 // ------------------------------------------------------------------------
126 template <typename TMapImage>
129 DGtal::DigitalSetFromMap<TMapImage>::insert( const Point & p )
131 ASSERT( this->domain().isInside( p ) );
132 myImgPtr->insert( Pair( p, myDefault ) );
136 // ------------------------------------------------------------------------
137 template <typename TMapImage>
138 template <typename PointInputIterator>
140 DGtal::DigitalSetFromMap<TMapImage>::insert( PointInputIterator first, PointInputIterator last )
142 for (PointInputIterator it = first; it != last; ++it)
147 // ------------------------------------------------------------------------
148 template <typename TMapImage>
151 DGtal::DigitalSetFromMap<TMapImage>::insertNew( const Point & p )
153 ASSERT( this->domain().isInside( p ) );
154 myImgPtr->insert( Pair( p, myDefault ) );
157 // ------------------------------------------------------------------------
158 template <typename TMapImage>
159 template <typename PointInputIterator>
162 DGtal::DigitalSetFromMap<TMapImage>::insertNew
163 ( PointInputIterator first, PointInputIterator last )
165 for (PointInputIterator it = first; it != last; ++it)
169 // ------------------------------------------------------------------------
170 template <typename TMapImage>
171 typename DGtal::DigitalSetFromMap<TMapImage>::Size
172 DGtal::DigitalSetFromMap<TMapImage>::erase( const Point & p )
174 return myImgPtr->erase( p );
177 // ------------------------------------------------------------------------
178 template <typename TMapImage>
181 DGtal::DigitalSetFromMap<TMapImage>::erase( Iterator it )
183 myImgPtr->erase( *it );
186 // ------------------------------------------------------------------------
187 template <typename TMapImage>
190 DGtal::DigitalSetFromMap<TMapImage>::clear()
195 // ------------------------------------------------------------------------
196 template <typename TMapImage>
198 typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
199 DGtal::DigitalSetFromMap<TMapImage>::find( const Point & p ) const
201 return ConstIterator( myImgPtr->find( p ), myFun );
204 // ------------------------------------------------------------------------
205 template <typename TMapImage>
207 typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
208 DGtal::DigitalSetFromMap<TMapImage>::find( const Point & p )
210 return Iterator( myImgPtr->find( p ), myFun );
213 // ------------------------------------------------------------------------
214 template <typename TMapImage>
216 typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
217 DGtal::DigitalSetFromMap<TMapImage>::begin() const
219 return ConstIterator( myImgPtr->begin(), myFun );
222 // ------------------------------------------------------------------------
223 template <typename TMapImage>
225 typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
226 DGtal::DigitalSetFromMap<TMapImage>::end() const
228 return ConstIterator( myImgPtr->end(), myFun );
231 // ------------------------------------------------------------------------
232 template <typename TMapImage>
234 typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
235 DGtal::DigitalSetFromMap<TMapImage>::begin()
237 return Iterator( myImgPtr->begin(), myFun );
240 // ------------------------------------------------------------------------
241 template <typename TMapImage>
243 typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
244 DGtal::DigitalSetFromMap<TMapImage>::end()
246 return Iterator( myImgPtr->end(), myFun );
249 // ------------------------------------------------------------------------
250 template <typename TMapImage>
251 template <typename TDigitalSet>
253 DGtal::DigitalSetFromMap<TMapImage> &
254 DGtal::DigitalSetFromMap<TMapImage>
255 ::operator+=( const TDigitalSet & aSet )
259 typename TMapImage::iterator itDst = myImgPtr->end();
260 for ( typename TDigitalSet::ConstIterator itSrc = aSet.begin();
264 itDst = myImgPtr->insert( itDst, Pair(*itSrc, myDefault) );
269 // ------------------------------------------------------------------------
270 template <typename TMapImage>
273 DGtal::DigitalSetFromMap<TMapImage>
274 ::operator()( const Point & p ) const
276 return myImgPtr->find( p ) != myImgPtr->end();
279 ///////////////////////////////////////////////////////////////////////////////
280 // ----------------------- Other Set services -----------------------------
282 // ------------------------------------------------------------------------
283 template <typename TMapImage>
284 template <typename TOutputIterator>
287 DGtal::DigitalSetFromMap<TMapImage>::computeComplement
288 (TOutputIterator& ito) const
290 Domain d = this->domain();
291 typename Domain::ConstIterator itPoint = d.begin();
292 typename Domain::ConstIterator itEnd = d.end();
293 while ( itPoint != itEnd ) {
294 if ( this->find( *itPoint ) == end() ) {
301 // ------------------------------------------------------------------------
302 template <typename TMapImage>
303 template <typename TDigitalSet>
306 DGtal::DigitalSetFromMap<TMapImage>::assignFromComplement
307 ( const TDigitalSet& otherSet )
310 Domain d = this->domain();
311 typename Domain::ConstIterator itPoint = d.begin();
312 typename Domain::ConstIterator itEnd = d.end();
313 while ( itPoint != itEnd ) {
314 if ( otherSet.find( *itPoint ) == otherSet.end() ) {
315 this->insert( *itPoint );
321 // ------------------------------------------------------------------------
322 template <typename TMapImage>
325 DGtal::DigitalSetFromMap<TMapImage>::computeBoundingBox
326 ( Point & lower, Point & upper ) const
328 Domain d = this->domain();
329 lower = d.upperBound();
330 upper = d.lowerBound();
331 ConstIterator it = this->begin();
332 ConstIterator itEnd = this->end();
333 while ( it != itEnd ) {
334 lower = lower.inf( *it );
335 upper = upper.sup( *it );
340 ///////////////////////////////////////////////////////////////////////////////
341 // Interface - public :
343 template <typename TMapImage>
346 DGtal::DigitalSetFromMap<TMapImage>::selfDisplay ( std::ostream & out ) const
348 out << "[DigitalSetFromMap]" << " size=" << size();
351 template <typename TMapImage>
354 DGtal::DigitalSetFromMap<TMapImage>::isValid() const
356 return ( (myImgPtr) && (myImgPtr->isValid()) );
360 // --------------- CDrawableWithBoard2D realization -------------------------
362 template<typename TMapImage>
365 DGtal::DigitalSetFromMap<TMapImage>::className() const
367 return "DigitalSetFromMap";
370 ///////////////////////////////////////////////////////////////////////////////
371 // Implementation of inline function //
373 template <typename TMapImage>
376 DGtal::operator<< ( std::ostream & out, const DGtal::DigitalSetFromMap<TMapImage> & object )
378 object.selfDisplay( out );
383 ///////////////////////////////////////////////////////////////////////////////