DGtal  0.9.2
AccFlower2D.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 AccFlower2D.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  * @author Jacques-Olivier Lachaud (\c jacques-olivier.lachaud@univ-savoie.fr )
22  * Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
23  *
24  * @date 2011/04/12
25  *
26  * Implementation of inline methods defined in AccFlower2D.h
27  *
28  * This file is part of the DGtal library.
29  */
30 
31 
32 //////////////////////////////////////////////////////////////////////////////
33 #include <cstdlib>
34 //////////////////////////////////////////////////////////////////////////////
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // IMPLEMENTATION of inline methods.
38 ///////////////////////////////////////////////////////////////////////////////
39 
40 ///////////////////////////////////////////////////////////////////////////////
41 // ----------------------- Standard services ------------------------------
42 
43 /**
44  * Destructor.
45  */
46 template <typename T>
47 inline
48 DGtal::AccFlower2D<T>::~AccFlower2D()
49 {
50 }
51 
52 template <typename T>
53 inline
54 DGtal::AccFlower2D<T>::AccFlower2D(const double x0, const double y0,
55  const double radius, const double smallRadius,
56  const unsigned int k, const double phi)
57  : myCenter(x0,y0), myRadius(radius), myVarRadius(smallRadius),
58  myK(k), myPhi(phi)
59 {
60  myKp = 2 * myK/ ( M_PI * M_PI );
61 }
62 
63 
64 template <typename T>
65 inline
66 DGtal::AccFlower2D<T>::AccFlower2D(const RealPoint2D &aPoint, const double radius,
67  const double smallRadius,
68  const unsigned int k, const double phi)
69  : myCenter(aPoint), myRadius(radius), myVarRadius(smallRadius),
70  myK(k), myPhi(phi)
71 {
72  myKp = 2 * myK/ ( M_PI * M_PI );
73 }
74 
75 template <typename T>
76 inline
77 DGtal::AccFlower2D<T>::AccFlower2D(const Point &aPoint, const double radius,
78  const double smallRadius,
79  const unsigned int k, const double phi)
80  : myRadius(radius), myVarRadius(smallRadius), myK(k), myPhi(phi)
81 {
82  myCenter = aPoint;
83  myKp = 2 * myK/ ( M_PI * M_PI );
84 }
85 
86 /////////////////////////////////////////////////////////////////////////////
87 // ------------- Implementation of 'StarShaped' services ------------------
88 
89 /**
90  * @param p any point in the plane.
91  *
92  * @return the angle parameter between 0 and 2*Pi corresponding to
93  * this point for the shape.
94  */
95 template <typename T>
96 inline
97 double
98 DGtal::AccFlower2D<T>::parameter( const RealPoint2D & pp ) const
99 {
100  RealPoint2D p( pp );
101  p -= myCenter;
102 
103  double angle = 0.0;
104  if ( ( p[0] == 0.0 ) && ( p[1] == 0.0 ) )
105  return angle;
106 
107  if ( p[0] >= p[1] )
108  {
109  if ( p[0] >= -p[1] )
110  angle = atan( p[1] / p[0] );
111  else
112  angle = 1.5* M_PI + atan( - p[0] / p[1] );
113  }
114  else // ( p[0] >= p[1] )
115  {
116  if ( p[0] >= -p[1] )
117  angle = 0.5*M_PI - atan( p[0] / p[1] );
118  else
119  angle = M_PI + atan( p[1] / p[0] );
120  }
121  angle = ( angle < 0.0 ) ? angle + 2*M_PI : angle;
122  return angle;
123 }
124 
125 /**
126  * @param t any angle between 0 and 2*Pi.
127  *
128  * @return the vector (x(t),y(t)) which is the position on the
129  * shape boundary.
130  */
131 template <typename T>
132 inline
133 typename DGtal::AccFlower2D<T>::RealPoint2D
134 DGtal::AccFlower2D<T>::x(const double t ) const
135 {
136  double tt =t;
137  while ( tt >= M_PI ) tt -= 2 * M_PI;
138  while ( tt < -M_PI ) tt += 2 * M_PI;
139 
140  double ktn = myKp * tt * tt * tt;
141  double r = myRadius + myVarRadius * cos( ktn );
142  RealPoint2D c( r * cos( tt ), r * sin( tt ) );
143  c += myCenter;
144  return c;
145 }
146 
147 
148 /**
149  * @param t any angle between 0 and 2*Pi.
150  *
151  * @return the vector (x'(t),y'(t)) which is the tangent to the
152  * shape boundary.
153  */
154 template <typename T>
155 inline
156 typename DGtal::AccFlower2D<T>::RealVector2D
157 DGtal::AccFlower2D<T>::xp( const double tt ) const
158 {
159  double t= tt;
160  while ( t >= M_PI ) t -= 2 * M_PI;
161  while ( t < -M_PI ) t += 2 * M_PI;
162 
163  double ktn = myKp * t * t * t;
164  double ktnp = 3 * myKp * t * t ;
165 
166  double r = myRadius + myVarRadius * cos( ktn );
167  double rp = - myVarRadius * sin( ktn ) * ktnp;
168  RealPoint2D c( rp * cos( t ) - r * sin( t ),
169  rp * sin( t ) + r * cos( t ) );
170  return c;
171 }
172 
173 /**
174  * @param t any angle between 0 and 2*Pi.
175  *
176  * @return the vector (x''(t),y''(t)).
177  */
178 template <typename T>
179 inline
180 typename DGtal::AccFlower2D<T>::RealVector2D
181 DGtal::AccFlower2D<T>::xpp( const double tt ) const
182 {
183  double t=tt;
184  while ( t >= M_PI ) t -= 2 * M_PI;
185  while ( t < -M_PI ) t += 2 * M_PI;
186 
187  double ktn = myKp * t * t * t;
188  double ktnp = 3 * myKp * t * t;
189  double ktnpp = 6 * myKp * t;
190 
191  double r = myRadius + myVarRadius * cos( ktn );
192  double rp = - myVarRadius * sin( ktn ) * ktnp;
193  double rpp = - myVarRadius * cos( ktn ) * ktnp * ktnp -
194  myVarRadius * sin( ktn ) * ktnpp;
195 
196  RealPoint2D c( rpp * cos( t ) - 2 * rp * sin( t ) - r * cos( t ),
197  rpp * sin( t ) + 2 * rp * cos( t ) - r * sin( t ) );
198  return c;
199 }
200 
201 
202 ///////////////////////////////////////////////////////////////////////////////
203 // Interface - public :
204 
205 /**
206  * Writes/Displays the object on an output stream.
207  * @param out the output stream where the object is written.
208  */
209 template <typename T>
210 inline
211 void
212 DGtal::AccFlower2D<T>::selfDisplay ( std::ostream & out ) const
213 {
214  out << "[AccFlower2D] center= "<<myCenter<<" radius="<<myRadius<<" smallradius="<<myVarRadius
215  <<" myK="<<myK<<" phase-shift="<<myPhi;
216 }
217 
218 /**
219  * Checks the validity/consistency of the object.
220  * @return 'true' if the object is valid, 'false' otherwise.
221  */
222 template <typename T>
223 inline
224 bool
225 DGtal::AccFlower2D<T>::isValid() const
226 {
227  return true;
228 }
229 
230 
231 
232 ///////////////////////////////////////////////////////////////////////////////
233 // Implementation of inline functions //
234 
235 template <typename T>
236 inline
237 std::ostream&
238 DGtal::operator<< ( std::ostream & out,
239  const AccFlower2D<T> & object )
240 {
241  object.selfDisplay( out );
242  return out;
243 }
244 
245 // //
246 ///////////////////////////////////////////////////////////////////////////////
247 
248