DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GreedySegmentation.ih
1 
30 
31 #include <cstdlib>
33 
35 // IMPLEMENTATION of inline methods.
37 
39 // class GreedySegmentation::SegmentComputerIterator
41 
42 // ------------------------- Main processings -----------------------
43 
44 
45  template <typename TSegmentComputer>
46 inline
47 bool
49 {
51  return this->doesIntersectNext( it, itb, ite, Type() );
52 }
53 
54  template <typename TSegmentComputer>
55 inline
56 bool
58 {
59  ConstIterator previousIt(it);
60  if ( (it != itb)&&(it != ite) )
61  {
62  --previousIt;
63  SegmentComputer tmpSegmentComputer=mySegmentComputer.getSelf();
64  tmpSegmentComputer.init(previousIt);
65  return tmpSegmentComputer.extendForward();
66  }
67  else
68  {
69  return false;
70  }
71 }
72 
73 
74  template <typename TSegmentComputer>
75 inline
76 bool
78 {
79  return this->doesIntersectNext(it);
80 }
81 
82 
83  template <typename TSegmentComputer>
84 inline
85 bool
87 {
88  ConstIterator previousIt(it); --previousIt;
89 
90  SegmentComputer tmpSegmentComputer=mySegmentComputer.getSelf();
91  tmpSegmentComputer.init(previousIt);
92  return tmpSegmentComputer.extendForward();
93 }
94 
95 
96 template <typename TSegmentComputer>
97 inline
98 void
100 {
101 
102  mySegmentComputer.init(it);
103 
104  //while my segmentComputer can be extended
105  while ( (mySegmentComputer.end() != myS->myStop)
106  &&(mySegmentComputer.extendForward()) ) {}
107 
108  //if the end is reached
109  if (mySegmentComputer.end() == myS->myStop) {
110 
111  myFlagIntersectNext = doesIntersectNext( mySegmentComputer.end(), myS->myBegin, myS->myEnd );
112  myFlagIsLast = true;
113 
114  //last extension
115  if ( isNotEmpty<ConstIterator>(myS->myStop, myS->myEnd) ) {
116  if (myS->myMode == "Truncate+1") {
117  mySegmentComputer.extendForward();
118  }
119  if (myS->myMode == "DoNotTruncate") {
120  while ( (mySegmentComputer.extendForward())
121  && (mySegmentComputer.end() != myS->myEnd) ) {}
122  }
123  }
124 
125  } else {
126  //otherwise
127  myFlagIntersectNext = doesIntersectNext( mySegmentComputer.end() );
128  }
129 }
130 
131 
132  template <typename TSegmentComputer>
133 inline
134 void
136 {
137 
138  if (myFlagIsLast) { //if the segmentComputer has reached the end
139 
140  myFlagIsValid = false;
141 
142  } else { //otherwise
143 
144  myFlagIntersectPrevious = myFlagIntersectNext;
145 
146  ConstIterator it( mySegmentComputer.end() );
147  if (myFlagIntersectPrevious) --it;
148 
149  this->longestSegment(it);
150 
151  }
152 
153 }
154 
155 // ------------------------- Standard services -----------------------
156 
157 
158  template <typename TSegmentComputer>
159 inline
162  const TSegmentComputer& aSegmentComputer,
163  const bool& aIsValid )
164  : myS( s ),
165  mySegmentComputer( aSegmentComputer ),
166  myFlagIsValid( aIsValid ),
167  myFlagIntersectNext( false ),
168  myFlagIntersectPrevious( false ),
169  myFlagIsLast( false )
170 {
171 
172  if (myFlagIsValid)
173  {
174  if ( isNotEmpty<ConstIterator>(myS->myStart, myS->myStop) )
175  { //if at least one element
176 
177  myFlagIntersectPrevious = doesIntersectNext( myS->myStart, myS->myBegin, myS->myEnd );
178 
179  //computation of the longest segment from myS->myStart
180  this->longestSegment(myS->myStart);
181 
182  }
183  else
184  {
185  myFlagIsValid = false;
186  }
187  }
188 }
189 
190 
191  template <typename TSegmentComputer>
192 inline
194 ( const SegmentComputerIterator & other )
195  : myS( other.myS ),
197  myFlagIsValid( other.myFlagIsValid ),
198  myFlagIntersectNext( other.myFlagIntersectNext ),
199  myFlagIntersectPrevious( other.myFlagIntersectPrevious ) ,
200  myFlagIsLast( other.myFlagIsLast )
201 {
202 }
203 
204 
205  template <typename TSegmentComputer>
206 inline
209 ( const SegmentComputerIterator & other )
210 {
211  if ( this != &other )
212  {
213  myS = other.myS;
215  myFlagIsValid = other.myFlagIsValid;
216  myFlagIntersectNext = other.myFlagIntersectNext;
217  myFlagIntersectPrevious = other.myFlagIntersectPrevious;
218  myFlagIsLast = other.myFlagIsLast;
219  }
220  return *this;
221 }
222 
223 
224  template <typename TSegmentComputer>
225 inline
227 {
228 }
229 
231 // ------------------------- iteration services -------------------------
232 
233 
234  template <typename TSegmentComputer>
235 inline
236 const TSegmentComputer&
238 {
239  return mySegmentComputer;
240 }
241 
242  template <typename TSegmentComputer>
243 inline
244 const TSegmentComputer*
246 {
247  return &mySegmentComputer;
248 }
249 
250 
251  template <typename TSegmentComputer>
252 inline
253 TSegmentComputer
255 {
256  return mySegmentComputer;
257 }
258 
259  template <typename TSegmentComputer>
260 inline
263 {
264  this->next();
265  return *this;
266 }
267 
268 
269 
270  template <typename TSegmentComputer>
271 inline
272 bool
273 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator==
274 ( const SegmentComputerIterator & other ) const
275 {
276 
277  if ( isValid() )
278  return ( (other.isValid() ) &&
279 ( mySegmentComputer.begin() == other.mySegmentComputer.begin() ) &&
280 ( mySegmentComputer.end() == other.mySegmentComputer.end() ) );
281  else
282  return ( ! other.isValid() );
283 }
284 
285 
286  template <typename TSegmentComputer>
287 inline
288 bool
289 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator!=
290 ( const SegmentComputerIterator & other ) const
291 {
292  return !(*this == other);
293 }
294 
295 
296 // ------------------------- accessors -------------------------
297 
298  template <typename TSegmentComputer>
299 inline
300 bool
302 {
303  return myFlagIntersectNext;
304 }
305 
306  template <typename TSegmentComputer>
307 inline
308 bool
310 {
311  return myFlagIntersectPrevious;
312 }
313 
314 
315 
316  template <typename TSegmentComputer>
317 inline
320 {
321  return mySegmentComputer.begin();
322 }
323 
324  template <typename TSegmentComputer>
325 inline
328 {
329  return mySegmentComputer.end();
330 }
331 
332 
333 
335 // class GreedySegmentation
337 
339 // Interface - public :
340 
341 
342  template <typename TSegmentComputer>
344 (const ConstIterator& itb, const ConstIterator& ite, const SegmentComputer& aSegmentComputer)
345  : myBegin(itb),
346  myEnd(ite),
347  myStart(itb),
348  myStop(ite),
349  myMode("Truncate"),
350  mySegmentComputer(aSegmentComputer)
351 {
352 }
353 
354 
355  template <typename TSegmentComputer>
356 inline
357 void
359 (const ConstIterator& itb, const ConstIterator& ite)
360 {
361  myStart = itb;
362  myStop = ite;
363  myMode = "Truncate";
364 }
365 
366  template <typename TSegmentComputer>
367 inline
368 void
370 (const std::string& aMode)
371 {
372  myMode = aMode;
373 }
374 
375 
376 
377  template <typename TSegmentComputer>
378 inline
380 {
381 }
382 
383 
384  template <typename TSegmentComputer>
385 inline
388 {
389  return SegmentComputerIterator(this, mySegmentComputer, true);
390 }
391 
392 
393  template <typename TSegmentComputer>
394 inline
397 {
398  return SegmentComputerIterator(this, mySegmentComputer, false);
399 }
400 
401 
402 
403  template <typename TSegmentComputer>
404 inline
405 void
407 {
408  out << "[GreedySegmentation]";
409 }
410 
411 
412  template <typename TSegmentComputer>
413 inline
414 bool
416 {
417  return true;
418 }
419 
420 
421 
423 // Implementation of inline functions //
424 
425  template <typename TSegmentComputer>
426 inline
427 std::ostream&
428 DGtal::operator<< ( std::ostream & out,
429  const GreedySegmentation<TSegmentComputer> & object )
430 {
431  object.selfDisplay( out );
432  return out;
433 }
434 
435 // //
437 
438