DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MaximalSegments.ih
1 
30 
31 #include <cstdlib>
33 
35 // IMPLEMENTATION of inline methods.
37 
38 
40 // class MaximalSegments<TSegment>::SegmentIterator
42 
46  template <typename TSegment>
47 inline
48 bool
50 {
51  Segment tmpSegment;
52  Iterator previousIt = it; --previousIt;
53  tmpSegment.init(previousIt);
54  return tmpSegment.extendForward(it);
55 }
56 
60 template <typename TSegment>
61 inline
62 void
64 {
65 
66  mySegment.init(myBack);
67  extension<Segment,Iterator>(mySegment,myFront,myCov->myEnd);
68 
69  if (myFront != myCov->myEnd) {
70 
71  if (myCov->isClosed) { //if processed as closed
72 
73  myFlagIntersectNext = doesIntersectNext(myFront);
75  myCov->myFirstMaximalSegmentBack = myBack;
76 
77  } else { //if processed as open
78 
79  myFlagIntersectNext = doesIntersectNext(myFront);
80 
81  }
82 
83  }
84 
85 }
86 
90 template <typename TSegment>
91 inline
92 void
94 {
95 
96  myFlagIntersectPrevious = myFlagIntersectNext;
97 
98  if (myFlag == false) {
99  //if the current segment lies between
100  //the first and last elements
101  //of the sequence
102 
103  if (myFront != myCov->myEnd) { //if not the end
104 
105  //backward extension
106  myBack = myFront;
107  ++myBack;
108  ReverseIterator ri(myBack);
109  ReverseSegment rseg; rseg.init(ri);
110  extension<ReverseSegment,ReverseIterator>(rseg,ri);
111  myBack = ri.base();
112 
113 
114  //forward extension
115  myFront = myBack;
116  mySegment.init(myFront);
117  extension<Segment,Iterator>(mySegment,myFront,myCov->myEnd);
118 
119 
120 
121  if (myFront == myCov->myEnd) {
122 
123  if (myCov->isClosed) { //if processed as closed
124  //while my segment can be extended at the beginning
125  myFront = myCov->myBegin;
126  while (mySegment.extendForward(myFront)) {
127  ++myFront;
128  }
129  //overlap
130  if (myFront == myCov->myBegin) {
131  Iterator tmpIt = myCov->myEnd; --tmpIt;
132  Segment tmpSegment; tmpSegment.init(tmpIt);
133  myFlagIntersectNext = tmpSegment.extendForward(myFront);
134  } else {
135  myFlagIntersectNext = doesIntersectNext(myFront);
136  }
137  myFlag = true;
138  } else { //if processed as open
139  //the current segment is the last one and
140  //does not have any next segment
141  myFlagIntersectNext = false;
142  }
143 
144  } else {
145  myFlagIntersectNext = doesIntersectNext(myFront);
146  }
147 
148  } else { //if the front of the segment is at the end
149  // of the sequence
150  myBack = myCov->myEnd;
151  }
152 
153  } else {
154  //if the current segment contains
155  //the first and last element
156  //of the sequence
157 
158 
159  //backward extension
160  myBack = myFront;
161  ++myBack;
162  ReverseIterator ri(myBack);
163  ReverseSegment rseg; rseg.init(ri);
164  extensionInLoop<ReverseSegment,ReverseIterator>(
165  rseg,ri,
166  ReverseIterator(myCov->myEnd),ReverseIterator(myCov->myBegin) );
167  myBack = ri.base();
168  if (myBack == myCov->myEnd) myBack = myCov->myBegin;
169 
170  if (myBack != myCov->myFirstMaximalSegmentBack) { //if not the end
171 
172  //forward extension
173  myFront = myBack;
174  mySegment.init(myFront);
175  extensionInLoop<Segment,Iterator>(mySegment,myFront,myCov->myBegin,myCov->myEnd);
176 
177  //overlap
178  if (myFront == myCov->myBegin) {
179  Iterator tmpIt = myCov->myEnd; --tmpIt;
180  Segment tmpSegment; tmpSegment.init(tmpIt);
181  myFlagIntersectNext = tmpSegment.extendForward(myFront);
182  } else {
183  myFlagIntersectNext = doesIntersectNext(myFront);
184  }
185 
186  } else { //if the back of the segment is at the
187  //back of the first maximal segment
188  myFront = myCov->myEnd;
189  myBack = myCov->myEnd;
190  }
191 
192  }
193 
194 }
195 
200  template <typename TSegment>
201 template <typename TypeSegment, typename TypeIterator>
202 inline
203 void
205  extension(TypeSegment& aSeg, TypeIterator& it, const TypeIterator& end) {
206  do {
207  ++it;
208  } while ( (it != end) &&
209  (aSeg.extendForward(it)) );
210 }
211 
212 
213 
218 template <typename TSegment>
219 template <typename TypeSegment, typename TypeIterator>
220 inline
221 void
223 {
224  do {
225  ++it;
226  } while (aSeg.extendForward(it));
227 }
228 
229 
234  template <typename TSegment>
235 template <typename TypeSegment, typename TypeIterator>
236 inline
237  void
239  extensionInLoop(TypeSegment& aSeg, TypeIterator& it,
240  const TypeIterator& begin, const TypeIterator& end) {
241  do {
242  ++it;
243  } while ( (it != end) &&
244  (aSeg.extendForward(it)) );
245  //if it is one-element-past-the end,
246  //go on at the beginning
247  if (it == end) {
248  it = begin;
249  while ( (it != end) &&
250  (aSeg.extendForward(it)) ) {
251  ++it;
252  }
253  }
254 }
255 // ------------------------- Standard services -----------------------
256 
264 template <typename TSegment>
265 inline
267  const Iterator& aBack,
268  const Segment& aSegment )
269  : myCov( aCov ),
270  myFront( aBack ),
271  myBack( aBack ),
272  mySegment( aSegment ),
273  myFlagIntersectNext(false),
274  myFlagIntersectPrevious(false),
275  myFlag(false)
276 {
277  if (myBack != myCov->myEnd) {
278  this->firstMaximalSegment();
279  }
280 }
281 
282 
287 template <typename TSegment>
288 inline
291  : myCov( aOther.myCov ),
292  myFront( aOther.myFront ), myBack( aOther.myBack ),
293  mySegment( aOther.mySegment ),
294  myFlagIntersectNext( aOther.myFlagIntersectNext ),
295  myFlagIntersectPrevious( aOther.myFlagIntersectPrevious ),
296  myFlag( aOther.myFlag )
297 {
298 }
299 
305 template <typename TSegment>
306 inline
309 ( const SegmentIterator & other )
310 {
311  if ( this != &other )
312  {
313  myCov = other.myCov;
314  myFront = other.myFront;
315  myBack = other.myBack;
316  mySegment = other.mySegment;
317  myFlagIntersectNext = other.myFlagIntersectNext;
318  myFlagIntersectPrevious = other.myFlagIntersectPrevious;
319  myFlag = other.myFlag;
320  }
321  return *this;
322 }
323 
328  template <typename TSegment>
329 inline
331 {
332 }
333 
335 // ------------------------- iteration services -------------------------
336 
337 
341  template <typename TSegment>
342 inline
343 TSegment
345 {
346  return mySegment;
347 }
348 
352  template <typename TSegment>
353 inline
354 TSegment
356 {
357  return mySegment;
358 }
359 
365  template <typename TSegment>
366 inline
369 {
370  this->nextMaximalSegment();
371  return *this;
372 }
373 
374 
375 
380  template <typename TSegment>
381 inline
382 const typename TSegment::ConstIterator
384 {
385  return myFront;
386 }
387 
392  template <typename TSegment>
393 inline
394 const typename TSegment::ConstIterator
396 {
397  return myBack;
398 }
399 
400 
405  template <typename TSegment>
406 inline
407 bool
409 {
410  return myFlagIntersectNext;
411 }
412 
417  template <typename TSegment>
418 inline
419 bool
421 {
422  return myFlagIntersectPrevious;
423 }
424 
432  template <typename TSegment>
433 inline
434 bool
435 DGtal::deprecated::MaximalSegments<TSegment>::SegmentIterator::operator==
436 ( const SegmentIterator & aOther ) const
437 {
438  return ( (myFront == aOther.myFront)&&(myBack == aOther.myBack) );
439 }
440 
448  template <typename TSegment>
449 inline
450 bool
451 DGtal::deprecated::MaximalSegments<TSegment>::SegmentIterator::operator!=
452 ( const SegmentIterator & aOther ) const
453 {
454  return ( (myFront != aOther.myFront)||(myBack != aOther.myBack) );
455 }
456 
457 
459 // class MaximalSegments<TSegment>
461 
463 // Interface - public :
464 
468  template <typename TSegment>
470  const Iterator& aBegin,
471  const Iterator& aEnd,
472  const Segment& aSegment,
473  const bool& aFlag)
474  : myBegin(aBegin),
475  myEnd(aEnd),
477  mySegment(aSegment),
478  isClosed(aFlag)
479 {
480 }
481 
485  template <typename TSegment>
486 inline
487 void
489  const Iterator& aBegin,
490  const Iterator& aEnd,
491  const Segment& aSegment,
492  const bool& aFlag) {
493 
494  myBegin = aBegin;
495  myEnd = aEnd;
496  myFirstMaximalSegmentBack = aBegin;
497  mySegment = aSegment;
498  isClosed = aFlag;
499 
500 }
501 
505  template <typename TSegment>
506 inline
508 {
509 }
510 
516  template <typename TSegment>
517 inline
520 {
521  return SegmentIterator(this, myBegin, mySegment);
522 }
523 
529  template <typename TSegment>
530 inline
533 {
534  return SegmentIterator(this, myEnd, mySegment);
535 }
536 
537 
542  template <typename TSegment>
543 inline
544 void
546 {
547  out << "[MaximalSegments]";
548 }
549 
554  template <typename TSegment>
555 inline
556 bool
558 {
559  return true;
560 }
561 
562 
563 
565 // Implementation of inline functions //
566 
567  template <typename TSegment>
568 inline
569 std::ostream&
570 DGtal::deprecated::operator<< ( std::ostream & out,
571  const MaximalSegments<TSegment> & object )
572 {
573  object.selfDisplay( out );
574  return out;
575 }
576 
577 // //
579 
580