DGtal 1.3.0
Loading...
Searching...
No Matches
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// ------------------------------------------------------------------------
42template <typename SegmentComputer, typename SCEstimator>
43inline
44DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
45::MostCenteredMaximalSegmentEstimator(const SegmentComputer& aSegmentComputer,
46 const SCEstimator& aSCEstimator)
47 : mySC(aSegmentComputer), mySCEstimator(aSCEstimator)
48{}
49
50
51// ------------------------------------------------------------------------
52template <typename SegmentComputer, typename SCEstimator>
53inline
54void
55DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>
56::init(const ConstIterator& itb, const ConstIterator& ite)
57{
58 myBegin = itb;
59 myEnd = ite;
60}
61
62
63
64// ------------------------------------------------------------------------
65template <typename SegmentComputer, typename SCEstimator>
66inline
67bool
68DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>::isValid() const
69{
70 return isNotEmpty(myBegin, myEnd);
71}
72
73// ------------------------------------------------------------------------
74template <typename SegmentComputer, typename SCEstimator>
75template <typename OutputIterator>
76inline
77OutputIterator
78DGtal::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// ------------------------------------------------------------------------
88template <typename SegmentComputer, typename SCEstimator>
89template <typename OutputIterator>
90inline
91OutputIterator
92DGtal::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// ------------------------------------------------------------------------
103template <typename SegmentComputer, typename SCEstimator>
104template <typename OutputIterator>
105inline
106OutputIterator
107DGtal::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// ------------------------------------------------------------------------
136template <typename SegmentComputer, typename SCEstimator>
137template <typename OutputIterator>
138inline
139OutputIterator
140DGtal::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// ------------------------------------------------------------------------
205template <typename SegmentComputer, typename SCEstimator>
206inline
207typename DGtal::MostCenteredMaximalSegmentEstimator<SegmentComputer,SCEstimator>::Quantity
208DGtal::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