DGtal  0.9.2
DigitalSetBySTLVector.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 DigitalSetBySTLVector.ih
19  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20  * Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
21  *
22  * @date 2010/07/01
23  *
24  * Implementation of inline methods defined in DigitalSetBySTLVector.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 #include <algorithm>
33 //////////////////////////////////////////////////////////////////////////////
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 // IMPLEMENTATION of inline methods.
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // ----------------------- Standard services ------------------------------
41 
42 /**
43  * Destructor.
44  */
45 template <typename Domain>
46 inline
47 DGtal::DigitalSetBySTLVector<Domain>::~DigitalSetBySTLVector()
48 {
49 }
50 
51 /**
52  * Constructor.
53  * Creates the empty set in the domain [d].
54  *
55  * @param d any domain.
56  */
57 template <typename Domain>
58 inline
59 DGtal::DigitalSetBySTLVector<Domain>::DigitalSetBySTLVector
60 ( Clone<Domain> d )
61  : myDomain( d ), myVector()
62 {
63 }
64 
65 /**
66  * Copy constructor.
67  * @param other the object to clone.
68  */
69 template <typename Domain>
70 inline
71 DGtal::DigitalSetBySTLVector<Domain>::DigitalSetBySTLVector
72 ( const DigitalSetBySTLVector & other )
73  : myDomain( other.myDomain ), myVector( other.myVector )
74 {
75 }
76 
77 /**
78  * Assignment.
79  * @param other the object to copy.
80  * @return a reference on 'this'.
81  */
82 template <typename Domain>
83 inline
84 DGtal::DigitalSetBySTLVector<Domain> &
85 DGtal::DigitalSetBySTLVector<Domain>::operator=
86 ( const DigitalSetBySTLVector & other )
87 {
88  ASSERT( ( domain().lowerBound() <= other.domain().lowerBound() )
89  && ( domain().upperBound() >= other.domain().upperBound() )
90  && "This domain should include the domain of the other set in case of assignment." );
91  myVector = other.myVector;
92  return *this;
93 }
94 
95 
96 /**
97  * @return the embedding domain.
98  */
99 template <typename Domain>
100 inline
101 const Domain &
102 DGtal::DigitalSetBySTLVector<Domain>::domain() const
103 {
104  return *myDomain;
105 }
106 
107 template <typename Domain>
108 inline
109 DGtal::CowPtr<Domain>
110 DGtal::DigitalSetBySTLVector<Domain>::domainPointer() const
111 {
112  return myDomain;
113 }
114 
115 
116 
117 // ----------------------- Standard Set services --------------------------
118 
119 /**
120  * @return the number of elements in the set.
121  */
122 template <typename Domain>
123 inline
124 typename DGtal::DigitalSetBySTLVector<Domain>::Size
125 DGtal::DigitalSetBySTLVector<Domain>::size() const
126 {
127  return (unsigned int)myVector.size();
128 }
129 
130 /**
131  * @return 'true' iff the set is empty (no element).
132  */
133 template <typename Domain>
134 inline
135 bool
136 DGtal::DigitalSetBySTLVector<Domain>::empty() const
137 {
138  return myVector.empty();
139 }
140 
141 /**
142  * Adds point [p] to this set.
143  *
144  * @param p any digital point.
145  * @pre p should belong to the associated domain.
146  */
147 template <typename Domain>
148 inline
149 void
150 DGtal::DigitalSetBySTLVector<Domain>::insert( const Point & p )
151 {
152  // ASSERT( domain().isInside( p ) );
153  Iterator it = find( p );
154  if ( it == end() )
155  myVector.push_back( p );
156 }
157 
158 /**
159  * Adds the collection of points specified by the two iterators to
160  * this set.
161  *
162  * @param first the start point in the collection of Point.
163  * @param last the last point in the collection of Point.
164  * @pre all points should belong to the associated domain.
165  */
166 template <typename Domain>
167 template <typename PointInputIterator>
168 inline
169 void
170 DGtal::DigitalSetBySTLVector<Domain>::insert
171 ( PointInputIterator first, PointInputIterator last )
172 {
173  for ( ; first != last; ++first )
174  insert( *first );
175 }
176 
177 /**
178  * Adds point [p] to this set if the point is not already in the
179  * set. There is no defined behavior if the point is already in
180  * the set (for instance, may be present twice).
181  *
182  * @param p any digital point.
183  *
184  * @pre p should belong to the associated domain.
185  * @pre p should not belong to this.
186  */
187 template <typename Domain>
188 inline
189 void
190 DGtal::DigitalSetBySTLVector<Domain>::insertNew( const Point & p )
191 {
192  // ASSERT( domain().isInside( p ) );
193  ASSERT( find( p ) == end() );
194  myVector.push_back( p );
195 }
196 
197 /**
198  * Adds the collection of points specified by the two iterators to
199  * this set. The collection should contain distinct points. Each
200  * of these points should also not belong already to the set.
201  * set. There is no defined behavior if the preceding requisites
202  * are not satisfied (for instance, points may be present several
203  * times in the set).
204  *
205  * @param first the start point in the collection of Point.
206  * @param last the last point in the collection of Point.
207  *
208  * @pre all points should belong to the associated domain.
209  * @pre each point should not belong to this.
210  */
211 template <typename Domain>
212 template <typename PointInputIterator>
213 inline
214 void
215 DGtal::DigitalSetBySTLVector<Domain>::insertNew
216 ( PointInputIterator first, PointInputIterator last )
217 {
218  while ( first != last )
219  myVector.push_back( *first++ );
220  // std::copy( first, last, myVector.end() );
221 }
222 
223 
224 /**
225  * Removes point [p] from the set.
226  *
227  * @param p the point to remove.
228  * @return the number of removed elements (0 or 1).
229  */
230 template <typename Domain>
231 inline
232 typename DGtal::DigitalSetBySTLVector<Domain>::Size
233 DGtal::DigitalSetBySTLVector<Domain>::erase( const Point & p )
234 {
235  Iterator it = find( p );
236  if ( it != end() )
237  {
238  erase( it );
239  return 1;
240  }
241  return 0;
242 }
243 
244 /**
245  * Removes the point pointed by [it] from the set.
246  *
247  * @param it an iterator on this set.
248  * @pre it should point on a valid element ( it != end() ).
249  * Note: generally faster than giving just the point.
250  */
251 template <typename Domain>
252 inline
253 void
254 DGtal::DigitalSetBySTLVector<Domain>::erase( Iterator it )
255 {
256  //take a mutable iterator
257  typename std::iterator_traits<Iterator>::difference_type d = it - this->begin();
258  typename std::vector<Point>::iterator it2 = myVector.begin() + d;
259  //swap
260  *it2 = myVector.back();
261  myVector.pop_back();
262 }
263 
264 /**
265  * Removes the collection of points specified by the two iterators from
266  * this set.
267  *
268  * @param first the start point in this set.
269  * @param last the last point in this set.
270  */
271 template <typename Domain>
272 inline
273 void
274 DGtal::DigitalSetBySTLVector<Domain>::erase( Iterator first, Iterator last )
275 {
276  while ( ( last != end() )
277  && ( first != last ) )
278  {
279  //take a mutable iterator
280  typename std::iterator_traits<Iterator>::difference_type d = first - this->begin();
281  typename std::vector<Point>::iterator first2 = myVector.begin() + d;
282  //swap
283  *first2 = myVector.back();
284  myVector.pop_back();
285  //advance
286  first++;
287  }
288  if ( first != last )
289  while ( first != end() )
290  myVector.pop_back();
291 }
292 
293 /**
294  * Clears the set.
295  * @post this set is empty.
296  */
297 template <typename Domain>
298 inline
299 void
300 DGtal::DigitalSetBySTLVector<Domain>::clear()
301 {
302  myVector.clear();
303 }
304 
305 /**
306  * @param p any digital point.
307  * @return a const iterator pointing on [p] if found, otherwise end().
308  */
309 template <typename Domain>
310 inline
311 typename DGtal::DigitalSetBySTLVector<Domain>::ConstIterator
312 DGtal::DigitalSetBySTLVector<Domain>::find( const Point & p ) const
313 {
314  const ConstIterator it_end = end();
315  for ( ConstIterator it = begin(); it != it_end; ++it )
316  if ( p == *it ) return it;
317  return it_end;
318 }
319 
320 /**
321  * @param p any digital point.
322  * @return an iterator pointing on [p] if found, otherwise end().
323  */
324 template <typename Domain>
325 inline
326 typename DGtal::DigitalSetBySTLVector<Domain>::Iterator
327 DGtal::DigitalSetBySTLVector<Domain>::find( const Point & p )
328 {
329  const Iterator it_end = end();
330  for ( Iterator it = begin(); it != it_end; ++it )
331  if ( p == *it ) return it;
332  return it_end;
333 }
334 
335 
336 /**
337  * @return a const iterator on the first element in this set.
338  */
339 template <typename Domain>
340 inline
341 typename DGtal::DigitalSetBySTLVector<Domain>::ConstIterator
342 DGtal::DigitalSetBySTLVector<Domain>::begin() const
343 {
344  return myVector.begin();
345 }
346 
347 /**
348  * @return a const iterator on the element after the last in this set.
349  */
350 template <typename Domain>
351 inline
352 typename DGtal::DigitalSetBySTLVector<Domain>::ConstIterator
353 DGtal::DigitalSetBySTLVector<Domain>::end() const
354 {
355  return myVector.end();
356 }
357 
358 
359 /**
360  * @return an iterator on the first element in this set.
361  */
362 template <typename Domain>
363 inline
364 typename DGtal::DigitalSetBySTLVector<Domain>::Iterator
365 DGtal::DigitalSetBySTLVector<Domain>::begin()
366 {
367  return myVector.begin();
368 }
369 
370 
371 /**
372  * @return a iterator on the element after the last in this set.
373  */
374 template <typename Domain>
375 inline
376 typename DGtal::DigitalSetBySTLVector<Domain>::Iterator
377 DGtal::DigitalSetBySTLVector<Domain>::end()
378 {
379  return myVector.end();
380 }
381 
382 /**
383  * set union to left.
384  * @param aSet any other set.
385  */
386 template <typename Domain>
387 inline
388 DGtal::DigitalSetBySTLVector<Domain> &
389 DGtal::DigitalSetBySTLVector<Domain>
390 ::operator+=( const DigitalSetBySTLVector<Domain> & aSet )
391 {
392  if ( this != &aSet )
393  {
394  std::vector<Point> other( aSet.myVector );
395  std::stable_sort( other.begin(), other.end() );
396  std::stable_sort( myVector.begin(), myVector.end() );
397  std::vector<Point> new_vector;
398  new_vector.reserve( size() + other.size() );
399  std::set_union( begin(), end(), other.begin(), other.end(),
400  std::back_insert_iterator< std::vector<Point> >
401  ( new_vector ) );
402  myVector.swap( new_vector );
403  }
404  return *this;
405 }
406 
407 //-----------------------------------------------------------------------------
408 template <typename Domain>
409 inline
410 bool
411 DGtal::DigitalSetBySTLVector<Domain>
412 ::operator()( const Point & p ) const
413 {
414  return find( p ) != end();
415 }
416 
417 
418 ///////////////////////////////////////////////////////////////////////////////
419 // ----------------------- Other Set services -----------------------------
420 
421 
422 template <typename Domain>
423 template <typename TOutputIterator>
424 inline
425 void
426 DGtal::DigitalSetBySTLVector<Domain>::computeComplement(TOutputIterator& ito) const
427 {
428  typename Domain::ConstIterator itPoint = domain().begin();
429  typename Domain::ConstIterator itEnd = domain().end();
430  while ( itPoint != itEnd ) {
431  if ( std::find( begin(), end(), *itPoint ) == end() ) {
432  *ito++ = *itPoint;
433  }
434  ++itPoint;
435  }
436 }
437 
438 /**
439  * Builds the complement in the domain of the set [other_set] in
440  * this.
441  *
442  * @param other_set defines the set whose complement is assigned to 'this'.
443  */
444 template <typename Domain>
445 inline
446 void
447 DGtal::DigitalSetBySTLVector<Domain>::assignFromComplement
448 ( const DigitalSetBySTLVector<Domain> & other_set )
449 {
450  clear();
451  typename Domain::ConstIterator itPoint = domain().begin();
452  typename Domain::ConstIterator itEnd = domain().end();
453  while ( itPoint != itEnd ) {
454  if ( std::find( other_set.begin(),
455  other_set.end(),
456  *itPoint ) == other_set.end() ) {
457  insert( *itPoint );
458  }
459  ++itPoint;
460  }
461 }
462 
463 /**
464  * Computes the bounding box of this set.
465  *
466  * @param lower the first point of the bounding box (lowest in all
467  * directions).
468  * @param upper the last point of the bounding box (highest in all
469  * directions).
470  */
471 template <typename Domain>
472 inline
473 void
474 DGtal::DigitalSetBySTLVector<Domain>::computeBoundingBox
475 ( Point & lower, Point & upper ) const
476 {
477  if ( begin() != end() )
478  {
479  ConstIterator it = begin();
480  ConstIterator it_end = end();
481  upper = lower = *it;
482  for ( ; it != it_end; ++it )
483  if ( it->isLower( lower ) ) lower = *it;
484  else if ( it->isUpper( upper ) ) upper = *it;
485  }
486  else
487  {
488  lower = domain().upperBound();
489  upper = domain().lowerBound();
490  }
491 }
492 
493 
494 ///////////////////////////////////////////////////////////////////////////////
495 // Interface - public :
496 
497 /**
498  * Writes/Displays the object on an output stream.
499  * @param out the output stream where the object is written.
500  */
501 template <typename Domain>
502 inline
503 void
504 DGtal::DigitalSetBySTLVector<Domain>::selfDisplay ( std::ostream & out ) const
505 {
506  out << "[DigitalSetBySTLVector]" << " size=" << size();
507 }
508 
509 /**
510  * Checks the validity/consistency of the object.
511  * @return 'true' if the object is valid, 'false' otherwise.
512  */
513 template <typename Domain>
514 inline
515 bool
516 DGtal::DigitalSetBySTLVector<Domain>::isValid() const
517 {
518  return true;
519 }
520 
521 
522 
523 // --------------- CDrawableWithBoard2D realization -------------------------
524 
525 
526 /**
527  * @return the style name used for drawing this object.
528  */
529 template<typename Domain>
530 inline
531 std::string
532 DGtal::DigitalSetBySTLVector<Domain>::className() const
533 {
534  return "DigitalSetBySTLVector";
535 }
536 
537 ///////////////////////////////////////////////////////////////////////////////
538 // Implementation of inline function //
539 
540 template <typename Domain>
541 inline
542 std::ostream&
543 DGtal::operator<< ( std::ostream & out,
544  const DigitalSetBySTLVector<Domain> & object )
545 {
546  object.selfDisplay( out );
547  return out;
548 }
549 
550 // //
551 ///////////////////////////////////////////////////////////////////////////////
552 
553