DGtal 1.4.0
Loading...
Searching...
No Matches
BinomialConvolver.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 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
21 *
22 * @date 2011/07/06
23 *
24 * Implementation of inline methods defined in BinomialConvolver.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 TConstIteratorOnPoints, typename TValue>
43inline
44DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>::~BinomialConvolver()
45{
46}
47//-----------------------------------------------------------------------------
48template <typename TConstIteratorOnPoints, typename TValue>
49inline
50DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
51::BinomialConvolver( unsigned int n )
52{
53 setSize( n );
54}
55//-----------------------------------------------------------------------------
56template <typename TConstIteratorOnPoints, typename TValue>
57inline
58void
59DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
60::setSize( unsigned int n )
61{
62 myN = n;
63}
64//-----------------------------------------------------------------------------
65template <typename TConstIteratorOnPoints, typename TValue>
66inline
67unsigned int
68DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
69::size() const
70{
71 return myN;
72}
73//-----------------------------------------------------------------------------
74template <typename TConstIteratorOnPoints, typename TValue>
75inline
76int
77DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
78::index( const ConstIteratorOnPoints& it ) const
79{
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;
84 ASSERT( false );
85 return 0;
86}
87
88//-----------------------------------------------------------------------------
89template <typename TConstIteratorOnPoints, typename TValue>
90inline
91void
92DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
93::init( const double h,
94 const ConstIteratorOnPoints& itb,
95 const ConstIteratorOnPoints& ite,
96 const bool isClosed )
97{
98 myMapIt2Idx.clear();
99 myH = h;
100 myBegin = itb;
101 myEnd = ite;
102 unsigned int aSize = 0;
103 for ( ConstIteratorOnPoints it = itb; it != ite; ++it )
104 {
105 myMapIt2Idx[ it ] = aSize;
106 ++aSize;
107 }
108 myX.init( aSize, 0, isClosed, 0.0 );
109 myY.init( aSize, 0, isClosed, 0.0 );
110 aSize = 0;
111 for ( ConstIteratorOnPoints it = itb; it != ite; ++it, ++aSize )
112 {
113/* myX[ size ] = it->operator[]( 0 );
114 myY[ size ] = it->operator[]( 1 );*/
115// TRIS ConstIterator may have no -> operator
116 Point p(*it);
117 myX[ aSize ] = p[0];
118 myY[ aSize ] = p[1];
119 }
120 Signal<double> G = Signal<double>::G2n( myN );
121 myX = myX * G;
122 myY = myY * G;
123 myDX = myX * Signal<double>::Delta();
124 myDY = myY * Signal<double>::Delta();
125 myDDX = myDX * Signal<double>::Delta();
126 myDDY = myDY * Signal<double>::Delta();
127}
128
129//-----------------------------------------------------------------------------
130template <typename TConstIteratorOnPoints, typename TValue>
131inline
132std::pair<TValue,TValue>
133DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
134::x( int i ) const
135{
136 return std::make_pair( myX[ i ], myY[ i ] );
137}
138//-----------------------------------------------------------------------------
139template <typename TConstIteratorOnPoints, typename TValue>
140inline
141std::pair<TValue,TValue>
142DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
143::dx( int i ) const
144{
145 return std::make_pair( myDX[ i ], myDY[ i ] );
146}
147//-----------------------------------------------------------------------------
148template <typename TConstIteratorOnPoints, typename TValue>
149inline
150std::pair<TValue,TValue>
151DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
152::d2x( int i ) const
153{
154 return std::make_pair( myDDX[ i ], myDDY[ i ] );
155}
156//-----------------------------------------------------------------------------
157template <typename TConstIteratorOnPoints, typename TValue>
158inline
159std::pair<TValue,TValue>
160DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
161::tangent( int i ) const
162{
163 Value n = sqrt( myDX[ i ] * myDX[ i ] +
164 myDY[ i ] * myDY[ i ] );
165 return std::make_pair( -myDX[ i ] / n, -myDY[ i ] / n );
166}
167//-----------------------------------------------------------------------------
168template <typename TConstIteratorOnPoints, typename TValue>
169inline
170TValue
171DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
172::curvature( int i ) const
173{
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
177 : TValue( 0.0 );
178}
179
180 /**
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
183 contour.
184 */
185//-----------------------------------------------------------------------------
186template <typename TConstIteratorOnPoints, typename TValue>
187inline
188unsigned int
189DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
190::suggestedSize( const double h,
191 const ConstIteratorOnPoints& itb,
192 const ConstIteratorOnPoints& ite )
193{
194 Point p(*itb);
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 )
200 {
201/* TValue x = it->operator[]( 0 );
202 TValue y = it->operator[]( 1 );*/
203// TRIS ConstIterator may have no -> operator
204 Point pp(*it);
205 TValue x = pp[0];
206 TValue y = pp[1];
207 if ( x < xmin ) xmin = x;
208 if ( x > xmax ) xmax = x;
209 if ( y < ymin ) ymin = y;
210 if ( y > ymax ) ymax = y;
211 }
212 TValue diameter = ( xmax - xmin ) > ( ymax - ymin )
213 ? ( xmax - xmin )
214 : ( 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 ) );
218}
219
220///////////////////////////////////////////////////////////////////////////////
221// Interface - public :
222
223/**
224 * Writes/Displays the object on an output stream.
225 * @param out the output stream where the object is written.
226 */
227template <typename TConstIteratorOnPoints, typename TValue>
228inline
229void
230DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>::selfDisplay ( std::ostream & out ) const
231{
232 out << "[BinomialConvolver]";
233}
234
235/**
236 * Checks the validity/consistency of the object.
237 * @return 'true' if the object is valid, 'false' otherwise.
238 */
239template <typename TConstIteratorOnPoints, typename TValue>
240inline
241bool
242DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>::isValid() const
243{
244 return true;
245}
246
247///////////////////////////////////////////////////////////////////////////////
248// TangentFromBinomialConvolverFunctor<,TBinomialConvolver,TRealPoint>
249//-----------------------------------------------------------------------------
250template <typename TBinomialConvolver, typename TRealPoint>
251inline
252typename DGtal::TangentFromBinomialConvolverFunctor<TBinomialConvolver,TRealPoint>::Value
253DGtal::TangentFromBinomialConvolverFunctor<TBinomialConvolver,TRealPoint>
254::operator()( const BinomialConvolver & bc,
255 const ConstIteratorOnPoints & it ) const
256{
257 int index = bc.index( it );
258 std::pair<SignalValue,SignalValue> v = bc.tangent( index );
259 return RealPoint( v.first, v.second );
260}
261
262///////////////////////////////////////////////////////////////////////////////
263// CurvatureFromBinomialConvolverFunctor<,TBinomialConvolver,TRealPoint>
264//-----------------------------------------------------------------------------
265template <typename TBinomialConvolver, typename TReal>
266inline
267typename DGtal::CurvatureFromBinomialConvolverFunctor<TBinomialConvolver,TReal>::Value
268DGtal::CurvatureFromBinomialConvolverFunctor<TBinomialConvolver,TReal>
269::operator()( const BinomialConvolver & bc,
270 const ConstIteratorOnPoints & it ) const
271{
272 int index = bc.index( it );
273 Value v = bc.curvature( index );
274 return v;
275}
276
277///////////////////////////////////////////////////////////////////////////////
278// class BinomialConvolverEstimator <TBinomialConvolver,TBinomialConvolverFunctor>
279//-----------------------------------------------------------------------------
280template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
281inline
282DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>
283::BinomialConvolverEstimator( unsigned int n,
284 const BinomialConvolverFunctor & f )
285 : myBC( n ), myFunctor( f )
286{
287}
288//-----------------------------------------------------------------------------
289template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
290inline
291void
292DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>
293::init( const double h,
294 const ConstIterator & itb,
295 const ConstIterator & ite,
296 const bool isClosed )
297{
298 if ( myBC.size() == 0 )
299 myBC.setSize( myBC.suggestedSize( h, itb, ite ) );
300 myBC.init( h, itb, ite, isClosed );
301}
302
303//-----------------------------------------------------------------------------
304template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
305inline
306typename DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>::Quantity
307DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>
308::eval( const ConstIterator& it )
309{
310 return myFunctor( myBC, it );
311}
312//-----------------------------------------------------------------------------
313template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
314template <typename OutputIterator>
315inline
316OutputIterator
317DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>
318::eval( const ConstIterator& itb,
319 const ConstIterator& ite,
320 OutputIterator result )
321{
322 for ( ConstIterator it = itb; it != ite; ++it )
323 *result++ = eval( it );
324 return result;
325}
326
327
328///////////////////////////////////////////////////////////////////////////////
329// Implementation of inline functions //
330
331template <typename TConstIteratorOnPoints, typename TValue>
332inline
333std::ostream&
334DGtal::operator<<
335( std::ostream & out,
336 const BinomialConvolver<TConstIteratorOnPoints,TValue> & object )
337{
338 object.selfDisplay( out );
339 return out;
340}
341
342// //
343///////////////////////////////////////////////////////////////////////////////
344
345