DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GreedyDecomposition.ih
1 
30 
31 #include <cstdlib>
33 
35 // IMPLEMENTATION of inline methods.
37 
39 // class GreedyDecomposition::SegmentIterator
41 
42 // ------------------------- Standard services -----------------------
43 
44 
45 
53  template <typename TSegment>
54 inline
57  const typename TSegment::ConstIterator& aBack,
58  const TSegment& aSegment)
59  : myDec( aDec ),
60  myFront( aBack ),
61  myBack( aBack ),
62  mySegment( aSegment ),
63  myFlagIntersectNext(false),
64  myFlagIntersectPrevious(false)
65 {
66  if (myBack != myDec->myEnd) { //if at least one element
67 
68  ++myFront;
69  if (myFront == myDec->myEnd) {//if only one element
70 
71  mySegment.init(myBack);
72 
73  } else { //if strictly more than one element
74 
75  if (myDec->isClosed) { //if processed as closed
76  //check is the first and last element form a valid segment
77  Iterator tmpIt = myDec->myEnd; --tmpIt;
78  Segment tmpSegment; tmpSegment.init(tmpIt);
79  myFlagIntersectPrevious = tmpSegment.extendForward(myDec->myBegin);
80  } else { //if processed as open
82  }
83  //computation of the longest segment from myBack
84  this->longestSegment();
85 
86  }
87 
88  }
89 }
90 
91 
92 
97  template <typename TSegment>
98 inline
100 ( const SegmentIterator & aOther )
101  : myDec( aOther.myDec ),
102  myFront( aOther.myFront ),
103  myBack( aOther.myBack ),
104  mySegment( aOther.mySegment ),
105  myFlagIntersectNext( aOther.myFlagIntersectNext ),
106  myFlagIntersectPrevious( aOther.myFlagIntersectPrevious )
107 {
108 }
109 
115  template <typename TSegment>
116 inline
119 ( const SegmentIterator & other )
120 {
121  if ( this != &other )
122  {
123  myDec = other.myDec;
124  myFront = other.myFront;
125  myBack = other.myBack;
126  mySegment = other.mySegment;
127  myFlagIntersectNext = other.myFlagIntersectNext;
128  myFlagIntersectPrevious = other.myFlagIntersectPrevious;
129  }
130  return *this;
131 }
132 
137  template <typename TSegment>
138 inline
140 {
141 }
142 
144 // ------------------------- iteration services -------------------------
145 
146 
150  template <typename TSegment>
151 inline
152 TSegment
154 {
155  return mySegment;
156 }
157 
161  template <typename TSegment>
162 inline
163 TSegment
165 {
166  return mySegment;
167 }
168 
174  template <typename TSegment>
175 inline
178 {
179  this->next();
180  return *this;
181 }
182 
187  template <typename TSegment>
188 inline
189 bool
191 {
192  return myFlagIntersectNext;
193 }
194 
199  template <typename TSegment>
200 inline
201 bool
203 {
204  return myFlagIntersectPrevious;
205 }
206 
210  template <typename TSegment>
211 inline
212 bool
214 {
215  Segment tmpSegment;
216  Iterator previousIt = it; --previousIt;
217  tmpSegment.init(previousIt);
218  return tmpSegment.extendForward(it);
219 }
220 
225  template <typename TSegment>
226 inline
227 void
229 {
230 
231  mySegment.init(myBack);
232 
233  //while my segment can be extended at the front
234  while ( (myFront != myDec->myEnd) &&
235  (mySegment.extendForward(myFront)) ) {
236  ++myFront;
237  }
238 
239  //if the end of the sequence is reached
240  if (myFront == myDec->myEnd) {
241  //if the sequence is processed as a closed one
242  //the first element of the sequence is added if possible
243  if (myDec->isClosed) {
244  myFlagIntersectNext = mySegment.extendForward(myDec->myBegin);
245  } else {
246  myFlagIntersectNext = false;
247  }
248  } else {
249  //otherwise
250  myFlagIntersectNext = doesIntersectNext(myFront);
251  }
252 
253 }
254 
255 
259  template <typename TSegment>
260 inline
261 void
263 {
264 
265  if (myFront != myDec->myEnd) { //if not the end
266 
267  myBack = myFront;
268 
269  myFlagIntersectPrevious = myFlagIntersectNext;
270 
271  if (myFlagIntersectPrevious) {
272  --myBack;
273  } else {
274  ++myFront;
275  }
276 
277  this->longestSegment();
278 
279  } else { //if the front of the segment is at the end of the sequence
280  myBack = myDec->myEnd;
281  }
282 }
283 
284 
289  template <typename TSegment>
290 inline
291 const typename TSegment::ConstIterator
293 {
294  return myFront;
295 }
296 
301  template <typename TSegment>
302 inline
303 const typename TSegment::ConstIterator
305 {
306  return myBack;
307 }
308 
309 
317  template <typename TSegment>
318 inline
319 bool
320 DGtal::deprecated::GreedyDecomposition<TSegment>::SegmentIterator::operator==
321 ( const SegmentIterator & aOther ) const
322 {
323  return ( (myFront == aOther.myFront)&&(myBack == aOther.myBack) );
324 }
325 
333  template <typename TSegment>
334 inline
335 bool
336 DGtal::deprecated::GreedyDecomposition<TSegment>::SegmentIterator::operator!=
337 ( const SegmentIterator & aOther ) const
338 {
339  return ( (myFront != aOther.myFront)||(myBack != aOther.myBack) );
340 }
341 
342 
344 // class GreedyDecomposition
346 
348 // Interface - public :
349 
350 
356  template <typename TSegment>
358 (const Iterator& aBegin, const Iterator& aEnd, const Segment& aSegment, const bool& aFlag)
359  : myBegin(aBegin),
360  myEnd(aEnd),
361  mySegment(aSegment),
362  isClosed(aFlag)
363 {
364 }
365 
371  template <typename TSegment>
372 inline
373 void
375 (const Iterator& aBegin, const Iterator& aEnd, const Segment& aSegment, const bool& aFlag)
376 {
377  myBegin = aBegin;
378  myEnd = aEnd;
379  mySegment = aSegment;
380  isClosed = aFlag;
381 }
382 
386  template <typename TSegment>
387 inline
389 {
390 }
391 
397  template <typename TSegment>
398 inline
401 {
402  return SegmentIterator(this, myBegin, mySegment);
403 }
404 
410  template <typename TSegment>
411 inline
414 {
415  return SegmentIterator(this, myEnd, mySegment);
416 }
417 
418 
423  template <typename TSegment>
424 inline
425 void
427 {
428  out << "[GreedyDecomposition]";
429 }
430 
435  template <typename TSegment>
436 inline
437 bool
439 {
440  return true;
441 }
442 
443 
444 
446 // Implementation of inline functions //
447 
448  template <typename TSegment>
449 inline
450 std::ostream&
451 DGtal::deprecated::operator<< ( std::ostream & out,
452  const GreedyDecomposition<TSegment> & object )
453 {
454  object.selfDisplay( out );
455  return out;
456 }
457 
458 // //
460 
461