DGtal  0.9.2
GreedySegmentation.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 GreedySegmentation.ih
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 2011/07/21
23  *
24  * Implementation of inline methods defined in GreedySegmentation.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // class GreedySegmentation::SegmentComputerIterator
40 ///////////////////////////////////////////////////////////////////////////////
41 
42 // ------------------------- Main processings -----------------------
43 
44 
45  template <typename TSegmentComputer>
46 inline
47 bool
48 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::doesIntersectNext(const ConstIterator& it, const ConstIterator& itb, const ConstIterator& ite)
49 {
50  typedef typename IteratorCirculatorTraits<typename SegmentComputer::ConstIterator>::Type Type;
51  return this->doesIntersectNext( it, itb, ite, Type() );
52 }
53 
54  template <typename TSegmentComputer>
55 inline
56 bool
57 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::doesIntersectNext(const ConstIterator& it, const ConstIterator& itb, const ConstIterator& ite, IteratorType )
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.extendFront();
66  }
67  else
68  {
69  return false;
70  }
71 }
72 
73 
74  template <typename TSegmentComputer>
75 inline
76 bool
77  DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::doesIntersectNext(const ConstIterator& it, const ConstIterator& /*itb*/, const ConstIterator& /*ite*/, CirculatorType )
78 {
79  return this->doesIntersectNext(it);
80 }
81 
82 
83  template <typename TSegmentComputer>
84 inline
85 bool
86 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::doesIntersectNext(const ConstIterator& it)
87 {
88  ConstIterator previousIt(it); --previousIt;
89 
90  SegmentComputer tmpSegmentComputer=mySegmentComputer.getSelf();
91  tmpSegmentComputer.init(previousIt);
92  return tmpSegmentComputer.extendFront();
93 }
94 
95 
96 template <typename TSegmentComputer>
97 inline
98 void
99 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::longestSegment(const ConstIterator& it)
100 {
101 
102  mySegmentComputer.init(it);
103 
104  //while my segmentComputer can be extended
105  while ( (mySegmentComputer.end() != myS->myStop)
106  &&(mySegmentComputer.extendFront()) ) {}
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.extendFront();
118  }
119  if (myS->myMode == "DoNotTruncate") {
120  while ( (mySegmentComputer.extendFront())
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
135 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::next()
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
160 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::SegmentComputerIterator (
161  const GreedySegmentation<TSegmentComputer> *s,
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
193 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::SegmentComputerIterator
194 ( const SegmentComputerIterator & other )
195  : myS( other.myS ),
196  mySegmentComputer( other.mySegmentComputer ),
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
207 typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator&
208 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator=
209 ( const SegmentComputerIterator & other )
210 {
211  if ( this != &other )
212  {
213  myS = other.myS;
214  mySegmentComputer = other.mySegmentComputer;
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
226 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::~SegmentComputerIterator()
227 {
228 }
229 
230 ///////////////////////////////////////////////////////////////////////////////
231 // ------------------------- iteration services -------------------------
232 
233 
234  template <typename TSegmentComputer>
235 inline
236 const TSegmentComputer&
237 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator*() const
238 {
239  return mySegmentComputer;
240 }
241 
242  template <typename TSegmentComputer>
243 inline
244 const TSegmentComputer*
245 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator->() const
246 {
247  return &mySegmentComputer;
248 }
249 
250 
251  template <typename TSegmentComputer>
252 inline
253 TSegmentComputer
254 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::get() const
255 {
256  return mySegmentComputer;
257 }
258 
259  template <typename TSegmentComputer>
260 inline
261 typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator &
262 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator++()
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
301 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::intersectNext() const
302 {
303  return myFlagIntersectNext;
304 }
305 
306  template <typename TSegmentComputer>
307 inline
308 bool
309 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::intersectPrevious() const
310 {
311  return myFlagIntersectPrevious;
312 }
313 
314 
315 
316  template <typename TSegmentComputer>
317 inline
318 const typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::ConstIterator
319 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::begin() const
320 {
321  return mySegmentComputer.begin();
322 }
323 
324  template <typename TSegmentComputer>
325 inline
326 const typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::ConstIterator
327 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::end() const
328 {
329  return mySegmentComputer.end();
330 }
331 
332 
333 
334 ///////////////////////////////////////////////////////////////////////////////
335 // class GreedySegmentation
336 ///////////////////////////////////////////////////////////////////////////////
337 
338 ///////////////////////////////////////////////////////////////////////////////
339 // Interface - public :
340 
341 
342  template <typename TSegmentComputer>
343 DGtal::GreedySegmentation<TSegmentComputer>::GreedySegmentation
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
358 DGtal::GreedySegmentation<TSegmentComputer>::setSubRange
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
369 DGtal::GreedySegmentation<TSegmentComputer>::setMode
370 (const std::string& aMode)
371 {
372  myMode = aMode;
373 }
374 
375 
376 
377  template <typename TSegmentComputer>
378 inline
379 DGtal::GreedySegmentation<TSegmentComputer>::~GreedySegmentation()
380 {
381 }
382 
383 
384  template <typename TSegmentComputer>
385 inline
386 typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator
387 DGtal::GreedySegmentation<TSegmentComputer>::begin() const
388 {
389  return SegmentComputerIterator(this, mySegmentComputer, true);
390 }
391 
392 
393  template <typename TSegmentComputer>
394 inline
395 typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator
396 DGtal::GreedySegmentation<TSegmentComputer>::end() const
397 {
398  return SegmentComputerIterator(this, mySegmentComputer, false);
399 }
400 
401 
402 
403  template <typename TSegmentComputer>
404 inline
405 void
406 DGtal::GreedySegmentation<TSegmentComputer>::selfDisplay ( std::ostream & out ) const
407 {
408  out << "[GreedySegmentation]";
409 }
410 
411 
412  template <typename TSegmentComputer>
413 inline
414 bool
415 DGtal::GreedySegmentation<TSegmentComputer>::isValid() const
416 {
417  return true;
418 }
419 
420 
421 
422 ///////////////////////////////////////////////////////////////////////////////
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 // //
436 ///////////////////////////////////////////////////////////////////////////////
437 
438