DGtal  1.5.beta
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 #define FLOWER_PI_SQUARE (M_PI * M_PI)
37 #define FLOWER_2_PI (2. * M_PI)
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // IMPLEMENTATION of inline methods.
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 // ----------------------- Standard services ------------------------------
45 
46 template <typename T>
47 inline
48 DGtal::AccFlower2D<T>::AccFlower2D(const double x0, const double y0,
49  const double radius, const double smallRadius,
50  const unsigned int k, const double phi)
51  : myCenter(x0,y0), myRadius(radius), myVarRadius(smallRadius),
52  myK(k), myPhi(phi)
53 {
54  myKp = 2 * myK / FLOWER_PI_SQUARE;
55 }
56 
57 
58 template <typename T>
59 inline
60 DGtal::AccFlower2D<T>::AccFlower2D(const RealPoint &aPoint, const double radius,
61  const double smallRadius,
62  const unsigned int k, const double phi)
63  : myCenter(aPoint), myRadius(radius), myVarRadius(smallRadius),
64  myK(k), myPhi(phi)
65 {
66  myKp = 2 * myK/ FLOWER_PI_SQUARE;
67 }
68 
69 template <typename T>
70 inline
71 DGtal::AccFlower2D<T>::AccFlower2D(const AccFlower2D& other)
72  : myCenter(other.myCenter), myRadius(other.myRadius), myVarRadius(other.myVarRadius),
73  myK(other.myK), myPhi(other.myPhi)
74 {}
75 
76 /////////////////////////////////////////////////////////////////////////////
77 // ------------- Implementation of 'StarShaped' services ------------------
78 
79 /**
80  * @param pp any point in the plane.
81  *
82  * @return the angle parameter between 0 and 2*Pi corresponding to
83  * this point for the shape.
84  */
85 template <typename T>
86 inline
87 double
88 DGtal::AccFlower2D<T>::parameter( const RealPoint& pp ) const
89 {
90  RealPoint p = pp - myCenter;
91 
92  const double angle = atan2( p[1], p[0] );
93 
94  return ( angle < 0. ) ? angle + FLOWER_2_PI : angle;
95 }
96 
97 /**
98  * @param tt any angle between 0 and 2*Pi.
99  *
100  * @return the vector (x(t),y(t)) which is the position on the
101  * shape boundary.
102  */
103 template <typename T>
104 inline
105 typename DGtal::AccFlower2D<T>::RealPoint
106 DGtal::AccFlower2D<T>::x( const double tt ) const
107 {
108  double t = tt;
109  while ( t >= M_PI ) t -= FLOWER_2_PI;
110  while ( t < -M_PI ) t += FLOWER_2_PI;
111 
112  const double r = myRadius + myVarRadius * cos( myKp * t * t * t );
113  return RealPoint( r * cos( t ), r * sin( t ) ) + myCenter;
114 }
115 
116 
117 /**
118  * @param tt any angle between 0 and 2*Pi.
119  *
120  * @return the vector (x'(t),y'(t)) which is the tangent to the
121  * shape boundary.
122  */
123 template <typename T>
124 inline
125 typename DGtal::AccFlower2D<T>::RealVector
126 DGtal::AccFlower2D<T>::xp( const double tt ) const
127 {
128  double t = tt;
129  while ( t >= M_PI ) t -= FLOWER_2_PI;
130  while ( t < -M_PI ) t += FLOWER_2_PI;
131 
132  const double ktn = myKp * t * t * t;
133  const double ktnp = 3 * myKp * t * t ;
134 
135  const double r = myRadius + myVarRadius * cos( ktn );
136  const double rp = - myVarRadius * sin( ktn ) * ktnp;
137  return RealPoint(
138  rp * cos( t ) - r * sin( t ),
139  rp * sin( t ) + r * cos( t )
140  );
141 }
142 
143 /**
144  * @param tt any angle between 0 and 2*Pi.
145  *
146  * @return the vector (x''(t),y''(t)).
147  */
148 template <typename T>
149 inline
150 typename DGtal::AccFlower2D<T>::RealVector
151 DGtal::AccFlower2D<T>::xpp( const double tt ) const
152 {
153  double t = tt;
154  while ( t >= M_PI ) t -= FLOWER_2_PI;
155  while ( t < -M_PI ) t += FLOWER_2_PI;
156 
157  const double ktn = myKp * t * t * t;
158  const double ktnp = 3 * myKp * t * t;
159  const double ktnpp = 6 * myKp * t;
160 
161  const double r = myRadius + myVarRadius * cos( ktn );
162  const double rp = - myVarRadius * sin( ktn ) * ktnp;
163  const double rpp = - myVarRadius * cos( ktn ) * ktnp * ktnp -
164  myVarRadius * sin( ktn ) * ktnpp;
165 
166  return RealPoint(
167  rpp * cos( t ) - 2 * rp * sin( t ) - r * cos( t ),
168  rpp * sin( t ) + 2 * rp * cos( t ) - r * sin( t )
169  );
170 }
171 
172 
173 ///////////////////////////////////////////////////////////////////////////////
174 // Interface - public :
175 
176 /**
177  * Writes/Displays the object on an output stream.
178  * @param out the output stream where the object is written.
179  */
180 template <typename T>
181 inline
182 void
183 DGtal::AccFlower2D<T>::selfDisplay ( std::ostream & out ) const
184 {
185  out << "[AccFlower2D] center= " << myCenter
186  << " radius=" << myRadius
187  << " smallradius=" << myVarRadius
188  <<" myK=" << myK
189  << " phase-shift=" << myPhi;
190 }
191 
192 /**
193  * Checks the validity/consistency of the object.
194  * @return 'true' if the object is valid, 'false' otherwise.
195  */
196 template <typename T>
197 inline
198 bool
199 DGtal::AccFlower2D<T>::isValid() const
200 {
201  return true;
202 }
203 
204 
205 
206 ///////////////////////////////////////////////////////////////////////////////
207 // Implementation of inline functions //
208 
209 template <typename T>
210 inline
211 std::ostream&
212 DGtal::operator<< ( std::ostream & out,
213  const AccFlower2D<T> & object )
214 {
215  object.selfDisplay( out );
216  return out;
217 }
218 
219 // //
220 ///////////////////////////////////////////////////////////////////////////////
221 
222