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