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 BinomialConvolver.ih
19 * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
20 * Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
24 * Implementation of inline methods defined in BinomialConvolver.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
32 //////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
41 //-----------------------------------------------------------------------------
42 template <typename TConstIteratorOnPoints, typename TValue>
44 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>::~BinomialConvolver()
47 //-----------------------------------------------------------------------------
48 template <typename TConstIteratorOnPoints, typename TValue>
50 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
51 ::BinomialConvolver( unsigned int n )
55 //-----------------------------------------------------------------------------
56 template <typename TConstIteratorOnPoints, typename TValue>
59 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
60 ::setSize( unsigned int n )
64 //-----------------------------------------------------------------------------
65 template <typename TConstIteratorOnPoints, typename TValue>
68 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
73 //-----------------------------------------------------------------------------
74 template <typename TConstIteratorOnPoints, typename TValue>
77 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
78 ::index( const ConstIteratorOnPoints& it ) const
80 typename std::map<ConstIteratorOnPoints,int>::const_iterator
81 map_it = myMapIt2Idx.find( it );
82 if ( map_it != myMapIt2Idx.end() )
83 return map_it->second;
88 //-----------------------------------------------------------------------------
89 template <typename TConstIteratorOnPoints, typename TValue>
92 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
93 ::init( const double h,
94 const ConstIteratorOnPoints& itb,
95 const ConstIteratorOnPoints& ite,
102 unsigned int aSize = 0;
103 for ( ConstIteratorOnPoints it = itb; it != ite; ++it )
105 myMapIt2Idx[ it ] = aSize;
108 myX.init( aSize, 0, isClosed, 0.0 );
109 myY.init( aSize, 0, isClosed, 0.0 );
111 for ( ConstIteratorOnPoints it = itb; it != ite; ++it, ++aSize )
113 /* myX[ size ] = it->operator[]( 0 );
114 myY[ size ] = it->operator[]( 1 );*/
115 // TRIS ConstIterator may have no -> operator
120 Signal<double> G = Signal<double>::G2n( myN );
123 myDX = myX * Signal<double>::Delta();
124 myDY = myY * Signal<double>::Delta();
125 myDDX = myDX * Signal<double>::Delta();
126 myDDY = myDY * Signal<double>::Delta();
129 //-----------------------------------------------------------------------------
130 template <typename TConstIteratorOnPoints, typename TValue>
132 std::pair<TValue,TValue>
133 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
136 return std::make_pair( myX[ i ], myY[ i ] );
138 //-----------------------------------------------------------------------------
139 template <typename TConstIteratorOnPoints, typename TValue>
141 std::pair<TValue,TValue>
142 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
145 return std::make_pair( myDX[ i ], myDY[ i ] );
147 //-----------------------------------------------------------------------------
148 template <typename TConstIteratorOnPoints, typename TValue>
150 std::pair<TValue,TValue>
151 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
154 return std::make_pair( myDDX[ i ], myDDY[ i ] );
156 //-----------------------------------------------------------------------------
157 template <typename TConstIteratorOnPoints, typename TValue>
159 std::pair<TValue,TValue>
160 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
161 ::tangent( int i ) const
163 Value n = sqrt( myDX[ i ] * myDX[ i ] +
164 myDY[ i ] * myDY[ i ] );
165 return std::make_pair( -myDX[ i ] / n, -myDY[ i ] / n );
167 //-----------------------------------------------------------------------------
168 template <typename TConstIteratorOnPoints, typename TValue>
171 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
172 ::curvature( int i ) const
174 Value denom = pow( myDX[ i ] * myDX[ i ] + myDY[ i ] * myDY[ i ], 1.5 );
175 return ( denom != TValue( 0.0 ) )
176 ? ( myDDX[ i ] * myDY[ i ] - myDDY[ i ] * myDX[ i ] ) / denom / myH
181 @return the suggested size for the binomial convolver as
182 ceil( d / pow( h, 1.0/3.0 ) ), with d the diameter of the
185 //-----------------------------------------------------------------------------
186 template <typename TConstIteratorOnPoints, typename TValue>
189 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
190 ::suggestedSize( const double h,
191 const ConstIteratorOnPoints& itb,
192 const ConstIteratorOnPoints& ite )
195 TValue xmin = p[ 0 ];
196 TValue ymin = p[ 1 ];
197 TValue xmax = p[ 0 ];
198 TValue ymax = p[ 1 ];
199 for ( ConstIteratorOnPoints it = itb; it != ite; ++it )
201 /* TValue x = it->operator[]( 0 );
202 TValue y = it->operator[]( 1 );*/
203 // TRIS ConstIterator may have no -> operator
207 if ( x < xmin ) xmin = x;
208 if ( x > xmax ) xmax = x;
209 if ( y < ymin ) ymin = y;
210 if ( y > ymax ) ymax = y;
212 TValue diameter = ( xmax - xmin ) > ( ymax - ymin )
215 // return (unsigned int) ceil( 0.5 / pow( h / diameter, 4.0/3.0 ) );
216 //TRIS (diameter*h is the diameter of the shape)
217 return (unsigned int) ceil( diameter / pow( h, 1.0/3.0 ) );
220 ///////////////////////////////////////////////////////////////////////////////
221 // Interface - public :
224 * Writes/Displays the object on an output stream.
225 * @param out the output stream where the object is written.
227 template <typename TConstIteratorOnPoints, typename TValue>
230 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>::selfDisplay ( std::ostream & out ) const
232 out << "[BinomialConvolver]";
236 * Checks the validity/consistency of the object.
237 * @return 'true' if the object is valid, 'false' otherwise.
239 template <typename TConstIteratorOnPoints, typename TValue>
242 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>::isValid() const
247 ///////////////////////////////////////////////////////////////////////////////
248 // TangentFromBinomialConvolverFunctor<,TBinomialConvolver,TRealPoint>
249 //-----------------------------------------------------------------------------
250 template <typename TBinomialConvolver, typename TRealPoint>
252 typename DGtal::TangentFromBinomialConvolverFunctor<TBinomialConvolver,TRealPoint>::Value
253 DGtal::TangentFromBinomialConvolverFunctor<TBinomialConvolver,TRealPoint>
254 ::operator()( const BinomialConvolver & bc,
255 const ConstIteratorOnPoints & it ) const
257 int index = bc.index( it );
258 std::pair<SignalValue,SignalValue> v = bc.tangent( index );
259 return RealPoint( v.first, v.second );
262 ///////////////////////////////////////////////////////////////////////////////
263 // CurvatureFromBinomialConvolverFunctor<,TBinomialConvolver,TRealPoint>
264 //-----------------------------------------------------------------------------
265 template <typename TBinomialConvolver, typename TReal>
267 typename DGtal::CurvatureFromBinomialConvolverFunctor<TBinomialConvolver,TReal>::Value
268 DGtal::CurvatureFromBinomialConvolverFunctor<TBinomialConvolver,TReal>
269 ::operator()( const BinomialConvolver & bc,
270 const ConstIteratorOnPoints & it ) const
272 int index = bc.index( it );
273 Value v = bc.curvature( index );
277 ///////////////////////////////////////////////////////////////////////////////
278 // class BinomialConvolverEstimator <TBinomialConvolver,TBinomialConvolverFunctor>
279 //-----------------------------------------------------------------------------
280 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
282 DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>
283 ::BinomialConvolverEstimator( unsigned int n,
284 const BinomialConvolverFunctor & f )
285 : myBC( n ), myFunctor( f )
288 //-----------------------------------------------------------------------------
289 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
292 DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>
293 ::init( const double h,
294 const ConstIterator & itb,
295 const ConstIterator & ite,
296 const bool isClosed )
298 if ( myBC.size() == 0 )
299 myBC.setSize( myBC.suggestedSize( h, itb, ite ) );
300 myBC.init( h, itb, ite, isClosed );
303 //-----------------------------------------------------------------------------
304 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
306 typename DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>::Quantity
307 DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>
308 ::eval( const ConstIterator& it )
310 return myFunctor( myBC, it );
312 //-----------------------------------------------------------------------------
313 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
314 template <typename OutputIterator>
317 DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>
318 ::eval( const ConstIterator& itb,
319 const ConstIterator& ite,
320 OutputIterator result )
322 for ( ConstIterator it = itb; it != ite; ++it )
323 *result++ = eval( it );
328 ///////////////////////////////////////////////////////////////////////////////
329 // Implementation of inline functions //
331 template <typename TConstIteratorOnPoints, typename TValue>
335 ( std::ostream & out,
336 const BinomialConvolver<TConstIteratorOnPoints,TValue> & object )
338 object.selfDisplay( out );
343 ///////////////////////////////////////////////////////////////////////////////