DGtal  1.5.beta
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 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 )
54 {
55 }
56 // ------------------------------------------------------------------------
57 template <typename TMapImage>
58 inline
59 DGtal::DigitalSetFromMap<TMapImage>
60 ::DigitalSetFromMap( const DigitalSetFromMap<TMapImage> & other )
61  : myImgPtr( other.myImgPtr ), myFun( other.myFun ), myDefault (other.myDefault)
62 {
63 }
64 
65 // ------------------------------------------------------------------------
66 template <typename TMapImage>
67 inline
68 DGtal::DigitalSetFromMap<TMapImage> &
69 DGtal::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 // ------------------------------------------------------------------------
82 template <typename TMapImage>
83 inline
84 const typename DGtal::DigitalSetFromMap<TMapImage>::Domain&
85 DGtal::DigitalSetFromMap<TMapImage>::domain() const
86 {
87  return myImgPtr->domain();
88 }
89 
90 // ------------------------------------------------------------------------
91 template <typename TMapImage>
92 inline
93 DGtal::CowPtr<typename DGtal::DigitalSetFromMap<TMapImage>::Domain>
94 DGtal::DigitalSetFromMap<TMapImage>::domainPointer() const
95 {
96  return CowPtr<Domain>( new Domain( myImgPtr->domain() ) );
97 }
98 
99 
100 
101 ///////////////////////////////////////////////////////////////////////////////
102 // Interface - public :
103 
104 
105 // ------------------------------------------------------------------------
106 template <typename TMapImage>
107 inline
108 typename DGtal::DigitalSetFromMap<TMapImage>::Size
109 DGtal::DigitalSetFromMap<TMapImage>::size() const
110 {
111  return static_cast<Size>(myImgPtr->size());
112 }
113 
114 // ------------------------------------------------------------------------
115 template <typename TMapImage>
116 inline
117 bool
118 DGtal::DigitalSetFromMap<TMapImage>::empty() const
119 {
120  return myImgPtr->empty();
121 }
122 
123 
124 // ------------------------------------------------------------------------
125 template <typename TMapImage>
126 inline
127 void
128 DGtal::DigitalSetFromMap<TMapImage>::insert( const Point & p )
129 {
130  ASSERT( this->domain().isInside( p ) );
131  myImgPtr->insert( Pair( p, myDefault ) );
132 }
133 
134 
135 // ------------------------------------------------------------------------
136 template <typename TMapImage>
137 template <typename PointInputIterator>
138 void
139 DGtal::DigitalSetFromMap<TMapImage>::insert( PointInputIterator first, PointInputIterator last )
140 {
141  for (PointInputIterator it = first; it != last; ++it)
142  this->insert( *it );
143 }
144 
145 
146 // ------------------------------------------------------------------------
147 template <typename TMapImage>
148 inline
149 void
150 DGtal::DigitalSetFromMap<TMapImage>::insertNew( const Point & p )
151 {
152  ASSERT( this->domain().isInside( p ) );
153  myImgPtr->insert( Pair( p, myDefault ) );
154 }
155 
156 // ------------------------------------------------------------------------
157 template <typename TMapImage>
158 template <typename PointInputIterator>
159 inline
160 void
161 DGtal::DigitalSetFromMap<TMapImage>::insertNew
162 ( PointInputIterator first, PointInputIterator last )
163 {
164  for (PointInputIterator it = first; it != last; ++it)
165  this->insert( *it );
166 }
167 
168 // ------------------------------------------------------------------------
169 template <typename TMapImage>
170 typename DGtal::DigitalSetFromMap<TMapImage>::Size
171 DGtal::DigitalSetFromMap<TMapImage>::erase( const Point & p )
172 {
173  return static_cast<Size>(myImgPtr->erase( p ));
174 }
175 
176 // ------------------------------------------------------------------------
177 template <typename TMapImage>
178 inline
179 void
180 DGtal::DigitalSetFromMap<TMapImage>::erase( Iterator it )
181 {
182  myImgPtr->erase( *it );
183 }
184 
185 // ------------------------------------------------------------------------
186 template <typename TMapImage>
187 inline
188 void
189 DGtal::DigitalSetFromMap<TMapImage>::clear()
190 {
191  myImgPtr->clear();
192 }
193 
194 // ------------------------------------------------------------------------
195 template <typename TMapImage>
196 inline
197 typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
198 DGtal::DigitalSetFromMap<TMapImage>::find( const Point & p ) const
199 {
200  return ConstIterator( myImgPtr->find( p ), myFun );
201 }
202 
203 // ------------------------------------------------------------------------
204 template <typename TMapImage>
205 inline
206 typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
207 DGtal::DigitalSetFromMap<TMapImage>::find( const Point & p )
208 {
209  return Iterator( myImgPtr->find( p ), myFun );
210 }
211 
212 // ------------------------------------------------------------------------
213 template <typename TMapImage>
214 inline
215 typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
216 DGtal::DigitalSetFromMap<TMapImage>::begin() const
217 {
218  return ConstIterator( myImgPtr->begin(), myFun );
219 }
220 
221 // ------------------------------------------------------------------------
222 template <typename TMapImage>
223 inline
224 typename DGtal::DigitalSetFromMap<TMapImage>::ConstIterator
225 DGtal::DigitalSetFromMap<TMapImage>::end() const
226 {
227  return ConstIterator( myImgPtr->end(), myFun );
228 }
229 
230 // ------------------------------------------------------------------------
231 template <typename TMapImage>
232 inline
233 typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
234 DGtal::DigitalSetFromMap<TMapImage>::begin()
235 {
236  return Iterator( myImgPtr->begin(), myFun );
237 }
238 
239 // ------------------------------------------------------------------------
240 template <typename TMapImage>
241 inline
242 typename DGtal::DigitalSetFromMap<TMapImage>::Iterator
243 DGtal::DigitalSetFromMap<TMapImage>::end()
244 {
245  return Iterator( myImgPtr->end(), myFun );
246 }
247 
248 // ------------------------------------------------------------------------
249 template <typename TMapImage>
250 template <typename TDigitalSet>
251 inline
252 DGtal::DigitalSetFromMap<TMapImage> &
253 DGtal::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 // ------------------------------------------------------------------------
269 template <typename TMapImage>
270 inline
271 bool
272 DGtal::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 // ------------------------------------------------------------------------
282 template <typename TMapImage>
283 template <typename TOutputIterator>
284 inline
285 void
286 DGtal::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 // ------------------------------------------------------------------------
301 template <typename TMapImage>
302 template <typename TDigitalSet>
303 inline
304 void
305 DGtal::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 // ------------------------------------------------------------------------
321 template <typename TMapImage>
322 inline
323 void
324 DGtal::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 
342 template <typename TMapImage>
343 inline
344 void
345 DGtal::DigitalSetFromMap<TMapImage>::selfDisplay ( std::ostream & out ) const
346 {
347  out << "[DigitalSetFromMap]" << " size=" << size();
348 }
349 
350 template <typename TMapImage>
351 inline
352 bool
353 DGtal::DigitalSetFromMap<TMapImage>::isValid() const
354 {
355  return ( (myImgPtr) && (myImgPtr->isValid()) );
356 }
357 
358 
359 // --------------- CDrawableWithBoard2D realization -------------------------
360 
361 template<typename TMapImage>
362 inline
363 std::string
364 DGtal::DigitalSetFromMap<TMapImage>::className() const
365 {
366  return "DigitalSetFromMap";
367 }
368 
369 ///////////////////////////////////////////////////////////////////////////////
370 // Implementation of inline function //
371 
372 template <typename TMapImage>
373 inline
374 std::ostream &
375 DGtal::operator<< ( std::ostream & out, const DGtal::DigitalSetFromMap<TMapImage> & object )
376 {
377  object.selfDisplay( out );
378  return out;
379 }
380 
381 // //
382 ///////////////////////////////////////////////////////////////////////////////
383 
384