DGtal  1.3.beta
MostCenteredMaximalSegmentEstimator.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 MostCenteredMaximalSegmentEstimator.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/01/26
23  *
24  * Implementation of inline methods defined in MostCenteredMaximalSegmentEstimator.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 // ----------------------- Standard services ------------------------------
40 
41 // ------------------------------------------------------------------------
42 template <typename SegmentComputer, typename SCEstimator>
43 inline
44 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
45 ::MostCenteredMaximalSegmentEstimator(const SegmentComputer& aSegmentComputer,
46  const SCEstimator& aSCEstimator)
47  : mySC(aSegmentComputer), mySCEstimator(aSCEstimator)
48 {}
49 
50 
51 // ------------------------------------------------------------------------
52 template <typename SegmentComputer, typename SCEstimator>
53 inline
54 void
55 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
56 ::init(const ConstIterator& itb, const ConstIterator& ite)
57 {
58  myBegin = itb;
59  myEnd = ite;
60 }
61 
62 
63 
64 // ------------------------------------------------------------------------
65 template <typename SegmentComputer, typename SCEstimator>
66 inline
67 bool
68 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>::isValid() const
69 {
70  return isNotEmpty(myBegin, myEnd);
71 }
72 
73 // ------------------------------------------------------------------------
74 template <typename SegmentComputer, typename SCEstimator>
75 template <typename OutputIterator>
76 inline
77 OutputIterator
78 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
79  ::endEval(const ConstIterator& itb, const ConstIterator& ite, ConstIterator& itCurrent,
80  SegmentIterator& first, SegmentIterator& last,
81  OutputIterator result)
82 {
83  typedef typename IteratorCirculatorTraits<ConstIterator>::Type Type;
84  return endEval (itb, ite, itCurrent, first, last, result, Type() );
85 }
86 
87 // ------------------------------------------------------------------------
88 template <typename SegmentComputer, typename SCEstimator>
89 template <typename OutputIterator>
90 inline
91 OutputIterator
92 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
93 ::endEval(const ConstIterator& /*itb*/, const ConstIterator& ite, ConstIterator& itCurrent,
94  SegmentIterator& /*first*/, SegmentIterator& last,
95  OutputIterator result, IteratorType )
96 {
97  mySCEstimator.attach( *last );
98  result = mySCEstimator.eval( itCurrent, ite, result );
99  return result;
100 }
101 
102 // ------------------------------------------------------------------------
103 template <typename SegmentComputer, typename SCEstimator>
104 template <typename OutputIterator>
105 inline
106 OutputIterator
107 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
108 ::endEval(const ConstIterator& itb, const ConstIterator& ite, ConstIterator& itCurrent,
109  SegmentIterator& first, SegmentIterator& last,
110  OutputIterator result, CirculatorType )
111 {
112  if ( (itb == ite) && (first.intersectPrevious() && last.intersectNext() ) )
113  {//if first and last segment intersect (whole range)
114  //last segment
115  ConstIterator itEnd = getMiddleIterator( first->begin(), last->end() );//(floor)
116  ++itEnd; //(ceil)
117  mySCEstimator.attach( *last );
118  result = mySCEstimator.eval( itCurrent, itEnd, result );
119  itCurrent = itEnd;
120  if (itCurrent != ite)
121  {
122  //first segment
123  mySCEstimator.attach( *first );
124  result = mySCEstimator.eval( itCurrent, ite, result );
125  }
126  }
127  else
128  { //(sub range)
129  mySCEstimator.attach( *last );
130  result = mySCEstimator.eval( itCurrent, ite, result );
131  }
132  return result;
133 }
134 
135 // ------------------------------------------------------------------------
136 template <typename SegmentComputer, typename SCEstimator>
137 template <typename OutputIterator>
138 inline
139 OutputIterator
140 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
141 ::eval(const ConstIterator& itb, const ConstIterator& ite,
142  OutputIterator result, const double h)
143 {
144  ASSERT(isValid());
145 
146  mySCEstimator.init( h, myBegin, myEnd );
147 
148  Segmentation seg(myBegin, myEnd, mySC);
149  seg.setSubRange(itb, ite);
150  if ((myBegin != itb) || (myEnd != ite))
151  { //if subrange
152  seg.setMode("MostCentered++");
153  }
154  else
155  {//whole range
156  seg.setMode("MostCentered");
157  }
158 
159  SegmentIterator segItBegin = seg.begin();
160  SegmentIterator segItEnd = seg.end();
161  SegmentIterator segIt = segItBegin;
162  SegmentIterator nextSegIt = segIt;
163 
164  if (nextSegIt != segItEnd )
165  { //at least one maximal segment
166  ++nextSegIt;
167 
168  if (nextSegIt == segItEnd )
169  {
170  //only one maximal segment
171  mySCEstimator.attach( *segIt );
172  result = mySCEstimator.eval( itb, ite, result );
173  }
174  else
175  {
176  //strictly more than one maximal segment
177  ConstIterator itCurrent = itb;
178 
179  //main loop
180  while (nextSegIt != segItEnd)
181  {
182  ConstIterator itEnd = getMiddleIterator( nextSegIt->begin(), segIt->end() );//(floor)
183  ++itEnd;//(ceil)
184 
185  mySCEstimator.attach( *segIt );
186  result = mySCEstimator.eval( itCurrent, itEnd, result );
187 
188  itCurrent = itEnd;
189  segIt = nextSegIt;
190  ++nextSegIt;
191  }
192 
193  //end
194  result = endEval(itb, ite, itCurrent, segItBegin, segIt, result);
195 
196  }//end one or more maximal segments test
197  }//end zero or one maximal segment test
198  return result;
199 }
200 
201 
202 
203 
204 // ------------------------------------------------------------------------
205 template <typename SegmentComputer, typename SCEstimator>
206 inline
207 typename DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>::Quantity
208 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
209 ::eval(const ConstIterator& it, const double h)
210 {
211  ASSERT(isValid() && isNotEmpty(it,myEnd));
212 
213  mostCenteredMaximalSegment( mySC, it, myBegin, myEnd );
214  mySCEstimator.init( h, myBegin, myEnd );
215  mySCEstimator.attach( mySC );
216  return mySCEstimator.eval( it );
217 }
218 
219