DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BinomialConvolver.ih
1 
30 
31 #include <cstdlib>
33 
35 // IMPLEMENTATION of inline methods.
37 
39 // ----------------------- Standard services ------------------------------
40 
41 //-----------------------------------------------------------------------------
42 template <typename TConstIteratorOnPoints, typename TValue>
43 inline
45 {
46 }
47 //-----------------------------------------------------------------------------
48 template <typename TConstIteratorOnPoints, typename TValue>
49 inline
51 ::BinomialConvolver( unsigned int n )
52 {
53  setSize( n );
54 }
55 //-----------------------------------------------------------------------------
56 template <typename TConstIteratorOnPoints, typename TValue>
57 inline
58 void
60 ::setSize( unsigned int n )
61 {
62  myN = n;
63 }
64 //-----------------------------------------------------------------------------
65 template <typename TConstIteratorOnPoints, typename TValue>
66 inline
67 unsigned int
69 ::size() const
70 {
71  return myN;
72 }
73 //-----------------------------------------------------------------------------
74 template <typename TConstIteratorOnPoints, typename TValue>
75 inline
76 int
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 //-----------------------------------------------------------------------------
89 template <typename TConstIteratorOnPoints, typename TValue>
90 inline
91 void
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  }
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 //-----------------------------------------------------------------------------
130 template <typename TConstIteratorOnPoints, typename TValue>
131 inline
132 std::pair<TValue,TValue>
134 ::x( int i ) const
135 {
136  return std::make_pair( myX[ i ], myY[ i ] );
137 }
138 //-----------------------------------------------------------------------------
139 template <typename TConstIteratorOnPoints, typename TValue>
140 inline
141 std::pair<TValue,TValue>
143 ::dx( int i ) const
144 {
145  return std::make_pair( myDX[ i ], myDY[ i ] );
146 }
147 //-----------------------------------------------------------------------------
148 template <typename TConstIteratorOnPoints, typename TValue>
149 inline
150 std::pair<TValue,TValue>
152 ::d2x( int i ) const
153 {
154  return std::make_pair( myDDX[ i ], myDDY[ i ] );
155 }
156 //-----------------------------------------------------------------------------
157 template <typename TConstIteratorOnPoints, typename TValue>
158 inline
159 std::pair<TValue,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 //-----------------------------------------------------------------------------
168 template <typename TConstIteratorOnPoints, typename TValue>
169 inline
170 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 
185 //-----------------------------------------------------------------------------
186 template <typename TConstIteratorOnPoints, typename TValue>
187 inline
188 unsigned int
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 
221 // Interface - public :
222 
227 template <typename TConstIteratorOnPoints, typename TValue>
228 inline
229 void
231 {
232  out << "[BinomialConvolver]";
233 }
234 
239 template <typename TConstIteratorOnPoints, typename TValue>
240 inline
241 bool
243 {
244  return true;
245 }
246 
248 // TangentFromBinomialConvolverFunctor<,TBinomialConvolver,TRealPoint>
249 //-----------------------------------------------------------------------------
250 template <typename TBinomialConvolver, typename TRealPoint>
251 inline
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 
263 // CurvatureFromBinomialConvolverFunctor<,TBinomialConvolver,TRealPoint>
264 //-----------------------------------------------------------------------------
265 template <typename TBinomialConvolver, typename TReal>
266 inline
270  const ConstIteratorOnPoints & it ) const
271 {
272  int index = bc.index( it );
273  Value v = bc.curvature( index );
274  return v;
275 }
276 
278 // class BinomialConvolverEstimator <TBinomialConvolver,TBinomialConvolverFunctor>
279 //-----------------------------------------------------------------------------
280 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
281 inline
284  const BinomialConvolverFunctor & f )
285  : myBC( n ), myFunctor( f )
286 {
287 }
288 //-----------------------------------------------------------------------------
289 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
290 inline
291 void
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 //-----------------------------------------------------------------------------
304 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
305 inline
308 ::eval( const ConstIterator& it )
309 {
310  return myFunctor( myBC, it );
311 }
312 //-----------------------------------------------------------------------------
313 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
314 template <typename OutputIterator>
315 inline
316 OutputIterator
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 
329 // Implementation of inline functions //
330 
331 template <typename TConstIteratorOnPoints, typename TValue>
332 inline
333 std::ostream&
334 DGtal::operator<<
335 ( std::ostream & out,
337 {
338  object.selfDisplay( out );
339  return out;
340 }
341 
342 // //
344 
345