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>
48 DGtal::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>
57 DGtal::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>
86 DGtal::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();
96 template <typename TSegmentComputer>
99 DGtal::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>
135 DGtal::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>
160 DGtal::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>
193 DGtal::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>
207 typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator&
208 DGtal::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>
226 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::~SegmentComputerIterator()
230 ///////////////////////////////////////////////////////////////////////////////
231 // ------------------------- iteration services -------------------------
234 template <typename TSegmentComputer>
236 const TSegmentComputer&
237 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator*() const
239 return mySegmentComputer;
242 template <typename TSegmentComputer>
244 const TSegmentComputer*
245 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator->() const
247 return &mySegmentComputer;
251 template <typename TSegmentComputer>
254 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::get() const
256 return mySegmentComputer;
259 template <typename TSegmentComputer>
261 typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator &
262 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator++()
270 template <typename TSegmentComputer>
273 DGtal::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>
289 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::operator!=
290 ( const SegmentComputerIterator & other ) const
292 return !(*this == other);
296 // ------------------------- accessors -------------------------
298 template <typename TSegmentComputer>
301 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::intersectNext() const
303 return myFlagIntersectNext;
306 template <typename TSegmentComputer>
309 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::intersectPrevious() const
311 return myFlagIntersectPrevious;
316 template <typename TSegmentComputer>
318 const typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::ConstIterator
319 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::begin() const
321 return mySegmentComputer.begin();
324 template <typename TSegmentComputer>
326 const typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::ConstIterator
327 DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator::end() const
329 return mySegmentComputer.end();
334 ///////////////////////////////////////////////////////////////////////////////
335 // class GreedySegmentation
336 ///////////////////////////////////////////////////////////////////////////////
338 ///////////////////////////////////////////////////////////////////////////////
339 // Interface - public :
342 template <typename TSegmentComputer>
343 DGtal::GreedySegmentation<TSegmentComputer>::GreedySegmentation
344 (const ConstIterator& itb, const ConstIterator& ite, const SegmentComputer& aSegmentComputer)
350 mySegmentComputer(aSegmentComputer)
355 template <typename TSegmentComputer>
358 DGtal::GreedySegmentation<TSegmentComputer>::setSubRange
359 (const ConstIterator& itb, const ConstIterator& ite)
366 template <typename TSegmentComputer>
369 DGtal::GreedySegmentation<TSegmentComputer>::setMode
370 (const std::string& aMode)
377 template <typename TSegmentComputer>
379 DGtal::GreedySegmentation<TSegmentComputer>::~GreedySegmentation()
384 template <typename TSegmentComputer>
386 typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator
387 DGtal::GreedySegmentation<TSegmentComputer>::begin() const
389 return SegmentComputerIterator(this, mySegmentComputer, true);
393 template <typename TSegmentComputer>
395 typename DGtal::GreedySegmentation<TSegmentComputer>::SegmentComputerIterator
396 DGtal::GreedySegmentation<TSegmentComputer>::end() const
398 return SegmentComputerIterator(this, mySegmentComputer, false);
403 template <typename TSegmentComputer>
406 DGtal::GreedySegmentation<TSegmentComputer>::selfDisplay ( std::ostream & out ) const
408 out << "[GreedySegmentation]";
412 template <typename TSegmentComputer>
415 DGtal::GreedySegmentation<TSegmentComputer>::isValid() const
422 ///////////////////////////////////////////////////////////////////////////////
423 // Implementation of inline functions //
425 template <typename TSegmentComputer>
428 DGtal::operator<< ( std::ostream & out,
429 const GreedySegmentation<TSegmentComputer> & object )
431 object.selfDisplay( out );
436 ///////////////////////////////////////////////////////////////////////////////