DGtal 1.3.0
Loading...
Searching...
No Matches
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>
46inline
47bool
48DGtal::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>
55inline
56bool
57DGtal::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>
75inline
76bool
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>
84inline
85bool
86DGtal::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
96template <typename TSegmentComputer>
97inline
98void
99DGtal::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>
133inline
134void
135DGtal::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>
159inline
160DGtal::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>
192inline
193DGtal::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>
206inline
207typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator&
208DGtal::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>
225inline
226DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::~SegmentComputerIterator()
227{
228}
229
230///////////////////////////////////////////////////////////////////////////////
231// ------------------------- iteration services -------------------------
232
233
234 template <typename TSegmentComputer>
235inline
236const TSegmentComputer&
237DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator*() const
238{
239 return mySegmentComputer;
240}
241
242 template <typename TSegmentComputer>
243inline
244const TSegmentComputer*
245DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator->() const
246{
247 return &mySegmentComputer;
248}
249
250
251 template <typename TSegmentComputer>
252inline
253TSegmentComputer
254DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::get() const
255{
256 return mySegmentComputer;
257}
258
259 template <typename TSegmentComputer>
260inline
261typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator &
262DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator++()
263{
264 this->next();
265 return *this;
266}
267
268
269
270 template <typename TSegmentComputer>
271inline
272bool
273DGtal::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>
287inline
288bool
289DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator!=
290( const SegmentComputerIterator & other ) const
291{
292 return !(*this == other);
293}
294
295
296// ------------------------- accessors -------------------------
297
298 template <typename TSegmentComputer>
299inline
300bool
301DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::intersectNext() const
302{
303 return myFlagIntersectNext;
304}
305
306 template <typename TSegmentComputer>
307inline
308bool
309DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::intersectPrevious() const
310{
311 return myFlagIntersectPrevious;
312}
313
314
315
316 template <typename TSegmentComputer>
317inline
318const typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::ConstIterator
319DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::begin() const
320{
321 return mySegmentComputer.begin();
322}
323
324 template <typename TSegmentComputer>
325inline
326const typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::ConstIterator
327DGtal::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>
343DGtal::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>
356inline
357void
358DGtal::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>
367inline
368void
369DGtal::GreedySegmentation<TSegmentComputer>::setMode
370(const std::string& aMode)
371{
372 myMode = aMode;
373}
374
375
376
377 template <typename TSegmentComputer>
378inline
379DGtal::GreedySegmentation<TSegmentComputer>::~GreedySegmentation()
380{
381}
382
383
384 template <typename TSegmentComputer>
385inline
386typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator
387DGtal::GreedySegmentation<TSegmentComputer>::begin() const
388{
389 return SegmentComputerIterator(this, mySegmentComputer, true);
390}
391
392
393 template <typename TSegmentComputer>
394inline
395typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator
396DGtal::GreedySegmentation<TSegmentComputer>::end() const
397{
398 return SegmentComputerIterator(this, mySegmentComputer, false);
399}
400
401
402
403 template <typename TSegmentComputer>
404inline
405void
406DGtal::GreedySegmentation<TSegmentComputer>::selfDisplay ( std::ostream & out ) const
407{
408 out << "[GreedySegmentation]";
409}
410
411
412 template <typename TSegmentComputer>
413inline
414bool
415DGtal::GreedySegmentation<TSegmentComputer>::isValid() const
416{
417 return true;
418}
419
420
421
422///////////////////////////////////////////////////////////////////////////////
423// Implementation of inline functions //
424
425 template <typename TSegmentComputer>
426inline
427std::ostream&
428DGtal::operator<< ( std::ostream & out,
429 const GreedySegmentation<TSegmentComputer> & object )
430{
431 object.selfDisplay( out );
432 return out;
433}
434
435// //
436///////////////////////////////////////////////////////////////////////////////
437
438