DGtal 1.3.0
Loading...
Searching...
No Matches
DigitalSetFromMap.ih
1/**
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.
6 *
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.
11 *
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/>.
14 *
15 **/
16
17/**
18 * @file
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
21 *
22 * @date 2012/02/16
23 *
24 * Implementation of inline methods defined in DigitalSetFromMap.h
25 *
26 * This file is part of the DGtal library.
27 */
28
29
30//////////////////////////////////////////////////////////////////////////////
31#include <cstdlib>
32//////////////////////////////////////////////////////////////////////////////
33#include "DGtal/kernel/sets/DigitalSetFromMap.h"
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
37
38///////////////////////////////////////////////////////////////////////////////
39// ----------------------- Standard services ------------------------------
40
41// ------------------------------------------------------------------------
42template <typename TMapImage>
43inline
44DGtal::DigitalSetFromMap<TMapImage>::~DigitalSetFromMap()
45{
46}
47
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 )
54{
55}
56// ------------------------------------------------------------------------
57template <typename TMapImage>
58inline
59DGtal::DigitalSetFromMap<TMapImage>
60::DigitalSetFromMap( const DigitalSetFromMap<TMapImage> & other )
61 : myImgPtr( other.myImgPtr ), myFun( other.myFun ), myDefault (other.myDefault)
62{
63}
64
65// ------------------------------------------------------------------------
66template <typename TMapImage>
67inline
68DGtal::DigitalSetFromMap<TMapImage> &
69DGtal::DigitalSetFromMap<TMapImage>
70::operator= ( const DigitalSetFromMap<TMapImage> & other )
71{
72 if (this != &other)
73 {
74 myImgPtr = other.myImgPtr;
75 myFun = other.myFun;
76 myDefault = other.myDefault;
77 }
78 return *this;
79}
80
81// ------------------------------------------------------------------------
82template <typename TMapImage>
83inline
84const typename DGtal::DigitalSetFromMap<TMapImage>::Domain&
85DGtal::DigitalSetFromMap<TMapImage>::domain() const
86{
87 return myImgPtr->domain();
88}
89
90// ------------------------------------------------------------------------
91template <typename TMapImage>
92inline
93DGtal::CowPtr<typename DGtal::DigitalSetFromMap<TMapImage>::Domain>
94DGtal::DigitalSetFromMap<TMapImage>::domainPointer() const
95{
96 return CowPtr<Domain>( new Domain( myImgPtr->domain() ) );
97}
98
99
100
101///////////////////////////////////////////////////////////////////////////////
102// Interface - public :
103
104
105// ------------------------------------------------------------------------
106template <typename TMapImage>
107inline
108typename DGtal::DigitalSetFromMap<TMapImage>::Size
109DGtal::DigitalSetFromMap<TMapImage>::size() const
110{
111 return static_cast<Size>(myImgPtr->size());
112}
113
114// ------------------------------------------------------------------------
115template <typename TMapImage>
116inline
117bool
118DGtal::DigitalSetFromMap<TMapImage>::empty() const
119{
120 return myImgPtr->empty();
121}
122
123
124// ------------------------------------------------------------------------
125template <typename TMapImage>
126inline
127void
128DGtal::DigitalSetFromMap<TMapImage>::insert( const Point & p )
129{
130 ASSERT( this->domain().isInside( p ) );
131 myImgPtr->insert( Pair( p, myDefault ) );
132}
133
134
135// ------------------------------------------------------------------------
136template <typename TMapImage>
137template <typename PointInputIterator>
138void
139DGtal::DigitalSetFromMap<TMapImage>::insert( PointInputIterator first, PointInputIterator last )
140{
141 for (PointInputIterator it = first; it != last; ++it)
142 this->insert( *it );
143}
144
145
146// ------------------------------------------------------------------------
147template <typename TMapImage>
148inline
149void
150DGtal::DigitalSetFromMap<TMapImage>::insertNew( const Point & p )
151{
152 ASSERT( this->domain().isInside( p ) );
153 myImgPtr->insert( Pair( p, myDefault ) );
154}
155
156// ------------------------------------------------------------------------
157template <typename TMapImage>
158template <typename PointInputIterator>
159inline
160void
161DGtal::DigitalSetFromMap<TMapImage>::insertNew
162( PointInputIterator first, PointInputIterator last )
163{
164 for (PointInputIterator it = first; it != last; ++it)
165 this->insert( *it );
166}
167
168// ------------------------------------------------------------------------
169template <typename TMapImage>
170typename DGtal::DigitalSetFromMap<TMapImage>::Size
171DGtal::DigitalSetFromMap<TMapImage>::erase( const Point & p )
172{
173 return static_cast<Size>(myImgPtr->erase( p ));
174}
175
176// ------------------------------------------------------------------------
177template <typename TMapImage>
178inline
179void
180DGtal::DigitalSetFromMap<TMapImage>::erase( Iterator it )
181{
182 myImgPtr->erase( *it );
183}
184
185// ------------------------------------------------------------------------
186template <typename TMapImage>
187inline
188void
189DGtal::DigitalSetFromMap<TMapImage>::clear()
190{
191 myImgPtr->clear();
192}
193
194// ------------------------------------------------------------------------
195template <typename TMapImage>
196inline
197typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
198DGtal::DigitalSetFromMap<TMapImage>::find( const Point & p ) const
199{
200 return ConstIterator( myImgPtr->find( p ), myFun );
201}
202
203// ------------------------------------------------------------------------
204template <typename TMapImage>
205inline
206typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
207DGtal::DigitalSetFromMap<TMapImage>::find( const Point & p )
208{
209 return Iterator( myImgPtr->find( p ), myFun );
210}
211
212// ------------------------------------------------------------------------
213template <typename TMapImage>
214inline
215typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
216DGtal::DigitalSetFromMap<TMapImage>::begin() const
217{
218 return ConstIterator( myImgPtr->begin(), myFun );
219}
220
221// ------------------------------------------------------------------------
222template <typename TMapImage>
223inline
224typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
225DGtal::DigitalSetFromMap<TMapImage>::end() const
226{
227 return ConstIterator( myImgPtr->end(), myFun );
228}
229
230// ------------------------------------------------------------------------
231template <typename TMapImage>
232inline
233typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
234DGtal::DigitalSetFromMap<TMapImage>::begin()
235{
236 return Iterator( myImgPtr->begin(), myFun );
237}
238
239// ------------------------------------------------------------------------
240template <typename TMapImage>
241inline
242typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
243DGtal::DigitalSetFromMap<TMapImage>::end()
244{
245 return Iterator( myImgPtr->end(), myFun );
246}
247
248// ------------------------------------------------------------------------
249template <typename TMapImage>
250template <typename TDigitalSet>
251inline
252DGtal::DigitalSetFromMap<TMapImage> &
253DGtal::DigitalSetFromMap<TMapImage>
254::operator+=( const TDigitalSet & aSet )
255{
256 if ( this != &aSet )
257 {
258 typename TMapImage::iterator itDst = myImgPtr->end();
259 for ( typename TDigitalSet::ConstIterator itSrc = aSet.begin();
260 itSrc != aSet.end();
261 ++itSrc )
262 {
263 itDst = myImgPtr->insert( itDst, Pair(*itSrc, myDefault) );
264 }
265 }
266 return *this;
267}
268// ------------------------------------------------------------------------
269template <typename TMapImage>
270inline
271bool
272DGtal::DigitalSetFromMap<TMapImage>
273::operator()( const Point & p ) const
274{
275 return myImgPtr->find( p ) != myImgPtr->end();
276}
277
278///////////////////////////////////////////////////////////////////////////////
279// ----------------------- Other Set services -----------------------------
280
281// ------------------------------------------------------------------------
282template <typename TMapImage>
283template <typename TOutputIterator>
284inline
285void
286DGtal::DigitalSetFromMap<TMapImage>::computeComplement
287(TOutputIterator& ito) const
288{
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() ) {
294 *ito++ = *itPoint;
295 }
296 ++itPoint;
297 }
298}
299
300// ------------------------------------------------------------------------
301template <typename TMapImage>
302template <typename TDigitalSet>
303inline
304void
305DGtal::DigitalSetFromMap<TMapImage>::assignFromComplement
306( const TDigitalSet& otherSet )
307{
308 this->clear();
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 );
315 }
316 ++itPoint;
317 }
318}
319
320// ------------------------------------------------------------------------
321template <typename TMapImage>
322inline
323void
324DGtal::DigitalSetFromMap<TMapImage>::computeBoundingBox
325( Point & lower, Point & upper ) const
326{
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 );
335 ++it;
336 }
337}
338
339///////////////////////////////////////////////////////////////////////////////
340// Interface - public :
341
342template <typename TMapImage>
343inline
344void
345DGtal::DigitalSetFromMap<TMapImage>::selfDisplay ( std::ostream & out ) const
346{
347 out << "[DigitalSetFromMap]" << " size=" << size();
348}
349
350template <typename TMapImage>
351inline
352bool
353DGtal::DigitalSetFromMap<TMapImage>::isValid() const
354{
355 return ( (myImgPtr) && (myImgPtr->isValid()) );
356}
357
358
359// --------------- CDrawableWithBoard2D realization -------------------------
360
361template<typename TMapImage>
362inline
363std::string
364DGtal::DigitalSetFromMap<TMapImage>::className() const
365{
366 return "DigitalSetFromMap";
367}
368
369///////////////////////////////////////////////////////////////////////////////
370// Implementation of inline function //
371
372template <typename TMapImage>
373inline
374std::ostream &
375DGtal::operator<< ( std::ostream & out, const DGtal::DigitalSetFromMap<TMapImage> & object )
376{
377 object.selfDisplay( out );
378 return out;
379}
380
381// //
382///////////////////////////////////////////////////////////////////////////////
383
384