DGtal  0.9.2
Flower2D.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 Flower2D.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 Flower2D.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::Flower2D<T>::~Flower2D()
49 {
50 }
51 
52 template <typename T>
53 inline
54 DGtal::Flower2D<T>::Flower2D(const double x0, const double y0,
55  const double radius, const double varRadius,
56  const unsigned int k, const double phi)
57  : myCenter(x0,y0), myRadius(radius), myVarRadius(varRadius), myK(k),myPhi(phi)
58 {}
59 
60 
61 template <typename T>
62 inline
63 DGtal::Flower2D<T>::Flower2D(const RealPoint2D &aPoint, const double radius,
64  const double varRadius,
65  const unsigned int k, const double phi)
66  : myCenter(aPoint), myRadius(radius), myVarRadius(varRadius),
67  myK(k), myPhi(phi)
68 {}
69 
70 template <typename T>
71 inline
72 DGtal::Flower2D<T>::Flower2D(const Point &aPoint, const double radius,
73  const double varRadius,
74  const unsigned int k, const double phi)
75  : myRadius(radius),myVarRadius(varRadius),myK(k),myPhi(phi)
76 {
77  myCenter = aPoint;
78 }
79 
80 /////////////////////////////////////////////////////////////////////////////
81 // ------------- Implementation of 'StarShaped' services ------------------
82 
83 /**
84  * @param p any point in the plane.
85  *
86  * @return the angle parameter between 0 and 2*Pi corresponding to
87  * this point for the shape.
88  */
89 template <typename T>
90 inline
91 double
92 DGtal::Flower2D<T>::parameter( const RealPoint2D & pp ) const
93 {
94  RealPoint2D p( pp );
95  p -= myCenter;
96 
97  double angle = 0.0;
98  if ( ( p[0] == 0.0 ) && ( p[1] == 0.0 ) )
99  return angle;
100 
101 /*
102  if ( p[0] >= p[1] )
103  {
104  if ( p[0] >= -p[1] )
105  angle = atan( p[1] / p[0] );
106  else
107  angle = 1.5* M_PI + atan( - p[0] / p[1] );
108  }
109  else // ( p[0] >= p[1] )
110  {
111  if ( p[0] >= -p[1] )
112  angle = 0.5*M_PI - atan( p[0] / p[1] );
113  else
114  angle = M_PI + atan( p[1] / p[0] );
115  }
116  angle = ( angle < 0.0 ) ? angle + 2*M_PI : angle;
117 */
118  angle = atan2( p[1], p[0] ) + M_PI;
119  return angle;
120 }
121 
122 /**
123  * @param t any angle between 0 and 2*Pi.
124  *
125  * @return the vector (x(t),y(t)) which is the position on the
126  * shape boundary.
127  */
128 template <typename T>
129 inline
130 typename DGtal::Flower2D<T>::RealPoint2D
131 DGtal::Flower2D<T>::x( double t ) const
132 {
133  double r= myRadius+ myVarRadius*cos(myK*t + myPhi);
134  RealPoint2D c( r*cos(t), r*sin(t) );
135  c += myCenter;
136  return c;
137 }
138 
139 
140 /**
141  * @param t any angle between 0 and 2*Pi.
142  *
143  * @return the vector (x'(t),y'(t)) which is the tangent to the
144  * shape boundary.
145  */
146 template <typename T>
147 inline
148 typename DGtal::Flower2D<T>::RealVector2D
149 DGtal::Flower2D<T>::xp( const double t ) const
150 {
151  double r= myRadius+ myVarRadius*cos(myK*t + myPhi);
152  double rp = - myVarRadius * sin( myK * t + myPhi ) * myK;
153  RealVector2D c( rp*cos(t) - r*sin(t),rp*sin(t) + r*cos(t) );
154  return c;
155 }
156 
157 /**
158  * @param t any angle between 0 and 2*Pi.
159  *
160  * @return the vector (x''(t),y''(t)).
161  */
162 template <typename T>
163 inline
164 typename DGtal::Flower2D<T>::RealVector2D
165 DGtal::Flower2D<T>::xpp( const double t ) const
166 {
167  double r= myRadius+ myVarRadius*cos(myK*t + myPhi);
168  double rp = - myVarRadius * sin( myK * t + myPhi ) * myK;
169  double rpp = - myVarRadius * cos( myK * t + myPhi ) * myK * myK;
170  RealVector2D c(rpp * cos( t ) - 2 * rp * sin( t ) - r * cos( t ),
171  rpp * sin( t ) + 2 * rp * cos( t ) - r * sin( t ) );
172  return c;
173 }
174 
175 
176 ///////////////////////////////////////////////////////////////////////////////
177 // Interface - public :
178 
179 /**
180  * Writes/Displays the object on an output stream.
181  * @param out the output stream where the object is written.
182  */
183 template <typename T>
184 inline
185 void
186 DGtal::Flower2D<T>::selfDisplay ( std::ostream & out ) const
187 {
188  out << "[Flower2D] center= "<<myCenter<<" radius="<<myRadius<<" varRadius="<<myVarRadius
189  <<" myK="<<myK<<" 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::Flower2D<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 Flower2D<T> & object )
214 {
215  object.selfDisplay( out );
216  return out;
217 }
218 
219 // //
220 ///////////////////////////////////////////////////////////////////////////////
221 
222