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.
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.
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/>.
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
24 * Implementation of inline methods defined in GreedySegmentation.h
26 * This file is part of the DGtal library.
30//////////////////////////////////////////////////////////////////////////////
32//////////////////////////////////////////////////////////////////////////////
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
38///////////////////////////////////////////////////////////////////////////////
39// class GreedySegmentation::SegmentComputerIterator
40///////////////////////////////////////////////////////////////////////////////
42// ------------------------- Main processings -----------------------
45 template <typename TSegmentComputer>
48DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::doesIntersectNext(const ConstIterator& it, const ConstIterator& itb, const ConstIterator& ite)
50 typedef typename IteratorCirculatorTraits<typename SegmentComputer::ConstIterator>::Type Type;
51 return this->doesIntersectNext( it, itb, ite, Type() );
54 template <typename TSegmentComputer>
57DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::doesIntersectNext(const ConstIterator& it, const ConstIterator& itb, const ConstIterator& ite, IteratorType )
59 ConstIterator previousIt(it);
60 if ( (it != itb)&&(it != ite) )
63 SegmentComputer tmpSegmentComputer=mySegmentComputer.getSelf();
64 tmpSegmentComputer.init(previousIt);
65 return tmpSegmentComputer.extendFront();
74 template <typename TSegmentComputer>
77 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::doesIntersectNext(const ConstIterator& it, const ConstIterator& /*itb*/, const ConstIterator& /*ite*/, CirculatorType )
79 return this->doesIntersectNext(it);
83 template <typename TSegmentComputer>
86DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::doesIntersectNext(const ConstIterator& it)
88 ConstIterator previousIt(it); --previousIt;
90 SegmentComputer tmpSegmentComputer=mySegmentComputer.getSelf();
91 tmpSegmentComputer.init(previousIt);
92 return tmpSegmentComputer.extendFront();
96template <typename TSegmentComputer>
99DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::longestSegment(const ConstIterator& it)
102 mySegmentComputer.init(it);
104 //while my segmentComputer can be extended
105 while ( (mySegmentComputer.end() != myS->myStop)
106 &&(mySegmentComputer.extendFront()) ) {}
108 //if the end is reached
109 if (mySegmentComputer.end() == myS->myStop) {
111 myFlagIntersectNext = doesIntersectNext( mySegmentComputer.end(), myS->myBegin, myS->myEnd );
115 if ( isNotEmpty<ConstIterator>(myS->myStop, myS->myEnd) ) {
116 if (myS->myMode == "Truncate+1") {
117 mySegmentComputer.extendFront();
119 if (myS->myMode == "DoNotTruncate") {
120 while ( (mySegmentComputer.extendFront())
121 && (mySegmentComputer.end() != myS->myEnd) ) {}
127 myFlagIntersectNext = doesIntersectNext( mySegmentComputer.end() );
132 template <typename TSegmentComputer>
135DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::next()
138 if (myFlagIsLast) { //if the segmentComputer has reached the end
140 myFlagIsValid = false;
144 myFlagIntersectPrevious = myFlagIntersectNext;
146 ConstIterator it( mySegmentComputer.end() );
147 if (myFlagIntersectPrevious) --it;
149 this->longestSegment(it);
155// ------------------------- Standard services -----------------------
158 template <typename TSegmentComputer>
160DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::SegmentComputerIterator (
161 const GreedySegmentation<TSegmentComputer> *s,
162 const TSegmentComputer& aSegmentComputer,
163 const bool& aIsValid )
165 mySegmentComputer( aSegmentComputer ),
166 myFlagIsValid( aIsValid ),
167 myFlagIntersectNext( false ),
168 myFlagIntersectPrevious( false ),
169 myFlagIsLast( false )
174 if ( isNotEmpty<ConstIterator>(myS->myStart, myS->myStop) )
175 { //if at least one element
177 myFlagIntersectPrevious = doesIntersectNext( myS->myStart, myS->myBegin, myS->myEnd );
179 //computation of the longest segment from myS->myStart
180 this->longestSegment(myS->myStart);
185 myFlagIsValid = false;
191 template <typename TSegmentComputer>
193DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::SegmentComputerIterator
194( const SegmentComputerIterator & other )
196 mySegmentComputer( other.mySegmentComputer ),
197 myFlagIsValid( other.myFlagIsValid ),
198 myFlagIntersectNext( other.myFlagIntersectNext ),
199 myFlagIntersectPrevious( other.myFlagIntersectPrevious ) ,
200 myFlagIsLast( other.myFlagIsLast )
205 template <typename TSegmentComputer>
207typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator&
208DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator=
209( const SegmentComputerIterator & other )
211 if ( this != &other )
214 mySegmentComputer = other.mySegmentComputer;
215 myFlagIsValid = other.myFlagIsValid;
216 myFlagIntersectNext = other.myFlagIntersectNext;
217 myFlagIntersectPrevious = other.myFlagIntersectPrevious;
218 myFlagIsLast = other.myFlagIsLast;
224 template <typename TSegmentComputer>
226DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::~SegmentComputerIterator()
230///////////////////////////////////////////////////////////////////////////////
231// ------------------------- iteration services -------------------------
234 template <typename TSegmentComputer>
236const TSegmentComputer&
237DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator*() const
239 return mySegmentComputer;
242 template <typename TSegmentComputer>
244const TSegmentComputer*
245DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator->() const
247 return &mySegmentComputer;
251 template <typename TSegmentComputer>
254DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::get() const
256 return mySegmentComputer;
259 template <typename TSegmentComputer>
261typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator &
262DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator++()
270 template <typename TSegmentComputer>
273DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator==
274( const SegmentComputerIterator & other ) const
278 return ( (other.isValid() ) &&
279( mySegmentComputer.begin() == other.mySegmentComputer.begin() ) &&
280( mySegmentComputer.end() == other.mySegmentComputer.end() ) );
282 return ( ! other.isValid() );
286 template <typename TSegmentComputer>
289DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator!=
290( const SegmentComputerIterator & other ) const
292 return !(*this == other);
296// ------------------------- accessors -------------------------
298 template <typename TSegmentComputer>
301DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::intersectNext() const
303 return myFlagIntersectNext;
306 template <typename TSegmentComputer>
309DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::intersectPrevious() const
311 return myFlagIntersectPrevious;
316 template <typename TSegmentComputer>
318const typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::ConstIterator
319DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::begin() const
321 return mySegmentComputer.begin();
324 template <typename TSegmentComputer>
326const typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::ConstIterator
327DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::end() const
329 return mySegmentComputer.end();
334///////////////////////////////////////////////////////////////////////////////
335// class GreedySegmentation
336///////////////////////////////////////////////////////////////////////////////
338///////////////////////////////////////////////////////////////////////////////
339// Interface - public :
342 template <typename TSegmentComputer>
343DGtal::GreedySegmentation<TSegmentComputer>::GreedySegmentation
344(const ConstIterator& itb, const ConstIterator& ite, const SegmentComputer& aSegmentComputer)
350 mySegmentComputer(aSegmentComputer)
355 template <typename TSegmentComputer>
358DGtal::GreedySegmentation<TSegmentComputer>::setSubRange
359(const ConstIterator& itb, const ConstIterator& ite)
366 template <typename TSegmentComputer>
369DGtal::GreedySegmentation<TSegmentComputer>::setMode
370(const std::string& aMode)
377 template <typename TSegmentComputer>
379DGtal::GreedySegmentation<TSegmentComputer>::~GreedySegmentation()
384 template <typename TSegmentComputer>
386typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator
387DGtal::GreedySegmentation<TSegmentComputer>::begin() const
389 return SegmentComputerIterator(this, mySegmentComputer, true);
393 template <typename TSegmentComputer>
395typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator
396DGtal::GreedySegmentation<TSegmentComputer>::end() const
398 return SegmentComputerIterator(this, mySegmentComputer, false);
403 template <typename TSegmentComputer>
406DGtal::GreedySegmentation<TSegmentComputer>::selfDisplay ( std::ostream & out ) const
408 out << "[GreedySegmentation]";
412 template <typename TSegmentComputer>
415DGtal::GreedySegmentation<TSegmentComputer>::isValid() const
422///////////////////////////////////////////////////////////////////////////////
423// Implementation of inline functions //
425 template <typename TSegmentComputer>
428DGtal::operator<< ( std::ostream & out,
429 const GreedySegmentation<TSegmentComputer> & object )
431 object.selfDisplay( out );
436///////////////////////////////////////////////////////////////////////////////