DGtal  0.9.2
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() {}
46 
47 
48 // ------------------------------------------------------------------------
49 template <typename SegmentComputer, typename SCEstimator>
50 inline
51 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
52 ::MostCenteredMaximalSegmentEstimator(const SegmentComputer& aSegmentComputer,
53  const SCEstimator& aSCEstimator)
54  : myH(0), mySC(aSegmentComputer), mySCEstimator(aSCEstimator)
55 {}
56 
57 
58 // ------------------------------------------------------------------------
59 template <typename SegmentComputer, typename SCEstimator>
60 inline
61 void
62 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
63 ::init(const double h, const ConstIterator& itb, const ConstIterator& ite)
64 {
65 
66  myH = h;
67  myBegin = itb;
68  myEnd = ite;
69  if (this->isValid())
70  mySCEstimator.init( myH, myBegin, myEnd );
71 }
72 
73 
74 
75 // ------------------------------------------------------------------------
76 template <typename SegmentComputer, typename SCEstimator>
77 inline
78 bool
79 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>::isValid() const
80 {
81  return ( (myH > 0)&&(isNotEmpty(myBegin, myEnd)) );
82 }
83 
84 // ------------------------------------------------------------------------
85 template <typename SegmentComputer, typename SCEstimator>
86 template <typename OutputIterator>
87 inline
88 OutputIterator
89 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
90  ::endEval(const ConstIterator& itb, const ConstIterator& ite, ConstIterator& itCurrent,
91  SegmentIterator& first, SegmentIterator& last,
92  OutputIterator result)
93 {
94  typedef typename IteratorCirculatorTraits<ConstIterator>::Type Type;
95  return endEval (itb, ite, itCurrent, first, last, result, Type() );
96 }
97 
98 // ------------------------------------------------------------------------
99 template <typename SegmentComputer, typename SCEstimator>
100 template <typename OutputIterator>
101 inline
102 OutputIterator
103 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
104 ::endEval(const ConstIterator& /*itb*/, const ConstIterator& ite, ConstIterator& itCurrent,
105  SegmentIterator& /*first*/, SegmentIterator& last,
106  OutputIterator result, IteratorType )
107 {
108  mySCEstimator.attach( *last );
109  result = mySCEstimator.eval( itCurrent, ite, result );
110  return result;
111 }
112 
113 // ------------------------------------------------------------------------
114 template <typename SegmentComputer, typename SCEstimator>
115 template <typename OutputIterator>
116 inline
117 OutputIterator
118 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
119 ::endEval(const ConstIterator& itb, const ConstIterator& ite, ConstIterator& itCurrent,
120  SegmentIterator& first, SegmentIterator& last,
121  OutputIterator result, CirculatorType )
122 {
123  if ( (itb == ite) && (first.intersectPrevious() && last.intersectNext() ) )
124  {//if first and last segment intersect (whole range)
125  //last segment
126  ConstIterator itEnd = getMiddleIterator( first->begin(), last->end() );//(floor)
127  ++itEnd; //(ceil)
128  mySCEstimator.attach( *last );
129  result = mySCEstimator.eval( itCurrent, itEnd, result );
130  itCurrent = itEnd;
131  if (itCurrent != ite)
132  {
133  //first segment
134  mySCEstimator.attach( *first );
135  result = mySCEstimator.eval( itCurrent, ite, result );
136  }
137  }
138  else
139  { //(sub range)
140  mySCEstimator.attach( *last );
141  result = mySCEstimator.eval( itCurrent, ite, result );
142  }
143  return result;
144 }
145 
146 // ------------------------------------------------------------------------
147 template <typename SegmentComputer, typename SCEstimator>
148 template <typename OutputIterator>
149 inline
150 OutputIterator
151 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
152  ::eval(const ConstIterator& itb, const ConstIterator& ite,
153  OutputIterator result) {
154 
155 
156  Segmentation seg(myBegin, myEnd, mySC);
157  seg.setSubRange(itb, ite);
158  if ((myBegin != itb) || (myEnd != ite))
159  { //if subrange
160  seg.setMode("MostCentered++");
161  }
162  else
163  {//whole range
164  seg.setMode("MostCentered");
165  }
166 
167  if (this->isValid()) {
168 
169  SegmentIterator segItBegin = seg.begin();
170  SegmentIterator segItEnd = seg.end();
171  SegmentIterator segIt = segItBegin;
172  SegmentIterator nextSegIt = segIt;
173 
174  if (nextSegIt != segItEnd )
175  { //at least one maximal segment
176  ++nextSegIt;
177 
178  if (nextSegIt == segItEnd )
179  { //only one maximal segment
180  mySCEstimator.attach( *segIt );
181  result = mySCEstimator.eval( itb, ite, result );
182  }
183  else
184  { //strictly more than one maximal segment
185 
186  ConstIterator itCurrent = itb;
187 
188  //main loop
189  while (nextSegIt != segItEnd)
190  {
191  ConstIterator itEnd = getMiddleIterator( nextSegIt->begin(), segIt->end() );//(floor)
192  ++itEnd;//(ceil)
193 
194  mySCEstimator.attach( *segIt );
195  result = mySCEstimator.eval( itCurrent, itEnd, result );
196 
197  itCurrent = itEnd;
198 
199  segIt = nextSegIt;
200  ++nextSegIt;
201  }
202 
203  //end
204  result = endEval(itb, ite, itCurrent, segItBegin, segIt, result);
205 
206  }//end one or more maximal segments test
207  }//end zero or one maximal segment test
208  return result;
209 
210  }
211  else
212  {//nothing is done without correct initialization
213  std::cerr << "[DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>::eval(const ConstIterator& itb, const ConstIterator& ite,OutputIterator result)]"
214  << " ERROR. Object is not initialized." << std::endl;
215  throw InputException();
216  return result;
217  }
218 }
219 
220 
221 
222 
223 // ------------------------------------------------------------------------
224 template <typename SegmentComputer, typename SCEstimator>
225 inline
226 typename DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>::Quantity
227 DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
228 ::eval(const ConstIterator& it) {
229 
230  if ( this->isValid() )
231  {
232 
233  if (isNotEmpty(it,myEnd))
234  {
235  mostCenteredMaximalSegment( mySC, it, myBegin, myEnd );
236  mySCEstimator.attach( mySC );
237  return mySCEstimator.eval( it );
238  }
239  else
240  {
241  std::cerr << "[DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>::eval(const ConstIterator& it)]"
242  << " ERROR. Iterator is invalid (==myEnd)." << std::endl;
243  throw InputException();
244  return Quantity();
245  }
246 
247  }
248  else
249  {
250  std::cerr << "[DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>::eval(const ConstIterator& it)]"
251  << " ERROR. Object is not initialized." << std::endl;
252  throw InputException();
253  return Quantity();
254  }
255 }
256 
257