DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MostCenteredMaximalSegmentEstimator.ih
1 
30 
31 #include <cstdlib>
33 
35 // IMPLEMENTATION of inline methods.
37 
39 // ----------------------- Standard services ------------------------------
40 
41 // ------------------------------------------------------------------------
42 template <typename SegmentComputer, typename SCEstimator>
43 inline
46 
47 
48 // ------------------------------------------------------------------------
49 template <typename SegmentComputer, typename SCEstimator>
50 inline
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
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
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
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
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
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
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
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