DGtal  0.9.2
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 //-----------------------------------------------------------------------------
42 template <typename TConstIteratorOnPoints, typename TValue>
43 inline
44 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>::~BinomialConvolver()
45 {
46 }
47 //-----------------------------------------------------------------------------
48 template <typename TConstIteratorOnPoints, typename TValue>
49 inline
50 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
51 ::BinomialConvolver( unsigned int n )
52 {
53  setSize( n );
54 }
55 //-----------------------------------------------------------------------------
56 template <typename TConstIteratorOnPoints, typename TValue>
57 inline
58 void
59 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
60 ::setSize( unsigned int n )
61 {
62  myN = n;
63 }
64 //-----------------------------------------------------------------------------
65 template <typename TConstIteratorOnPoints, typename TValue>
66 inline
67 unsigned int
68 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>
69 ::size() const
70 {
71  return myN;
72 }
73 //-----------------------------------------------------------------------------
74 template <typename TConstIteratorOnPoints, typename TValue>
75 inline
76 int
77 DGtal::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 //-----------------------------------------------------------------------------
89 template <typename TConstIteratorOnPoints, typename TValue>
90 inline
91 void
92 DGtal::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 //-----------------------------------------------------------------------------
130 template <typename TConstIteratorOnPoints, typename TValue>
131 inline
132 std::pair<TValue,TValue>
133 DGtal::BinomialConvolver<TConstIteratorOnPoints,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>
142 DGtal::BinomialConvolver<TConstIteratorOnPoints,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>
151 DGtal::BinomialConvolver<TConstIteratorOnPoints,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>
160 DGtal::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 //-----------------------------------------------------------------------------
168 template <typename TConstIteratorOnPoints, typename TValue>
169 inline
170 TValue
171 DGtal::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 //-----------------------------------------------------------------------------
186 template <typename TConstIteratorOnPoints, typename TValue>
187 inline
188 unsigned int
189 DGtal::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  */
227 template <typename TConstIteratorOnPoints, typename TValue>
228 inline
229 void
230 DGtal::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  */
239 template <typename TConstIteratorOnPoints, typename TValue>
240 inline
241 bool
242 DGtal::BinomialConvolver<TConstIteratorOnPoints,TValue>::isValid() const
243 {
244  return true;
245 }
246 
247 ///////////////////////////////////////////////////////////////////////////////
248 // TangentFromBinomialConvolverFunctor<,TBinomialConvolver,TRealPoint>
249 //-----------------------------------------------------------------------------
250 template <typename TBinomialConvolver, typename TRealPoint>
251 inline
252 typename DGtal::TangentFromBinomialConvolverFunctor<TBinomialConvolver,TRealPoint>::Value
253 DGtal::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 //-----------------------------------------------------------------------------
265 template <typename TBinomialConvolver, typename TReal>
266 inline
267 typename DGtal::CurvatureFromBinomialConvolverFunctor<TBinomialConvolver,TReal>::Value
268 DGtal::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 //-----------------------------------------------------------------------------
280 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
281 inline
282 DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>
283 ::BinomialConvolverEstimator( unsigned int n,
284  const BinomialConvolverFunctor & f )
285  : myBC( n ), myFunctor( f )
286 {
287 }
288 //-----------------------------------------------------------------------------
289 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
290 inline
291 void
292 DGtal::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 //-----------------------------------------------------------------------------
304 template <typename TBinomialConvolver, typename TBinomialConvolverFunctor>
305 inline
306 typename DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>::Quantity
307 DGtal::BinomialConvolverEstimator<TBinomialConvolver,TBinomialConvolverFunctor>
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
317 DGtal::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 
331 template <typename TConstIteratorOnPoints, typename TValue>
332 inline
333 std::ostream&
334 DGtal::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