DGtal  1.0.0
Astroid2D.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 Astroid2D.ih
19  * @author Chouaib Fellah, Adrien Krähenbühl (\c krahenbuhl@unistra.fr )
20  * Laboratoire des sciences de l'ingénieur, de l'informatique et de l'imagerie - ICube (UMR 7357), France
21  *
22  * @date 2018/06/12
23  *
24  * Implementation of inline methods defined in Astroid2D.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 #include <cstdlib>
31 ///////////////////////////////////////////////////////////////////////////////
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 // IMPLEMENTATION of inline methods.
35 ///////////////////////////////////////////////////////////////////////////////
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 // ----------------------- Standard services ----------------------------------
39 
40 /**
41  * Destructor.
42  */
43 template <typename T>
44 inline
45 DGtal::Astroid2D<T>::~Astroid2D()
46 {
47 }
48 
49 template <typename T>
50 inline
51 DGtal::Astroid2D<T>::Astroid2D( const double x0, const double y0,
52  const double a, const double b ) : myCenter(x0,y0), myA(a), myB(b)
53 {}
54 
55 template <typename T>
56 inline
57 DGtal::Astroid2D<T>::Astroid2D( const RealPoint2D &aPoint,
58  const double a, const double b ) : myCenter(aPoint), myA(a), myB(b)
59 {}
60 
61 template <typename T>
62 inline
63 DGtal::Astroid2D<T>::Astroid2D( const Point2D &aPoint,
64  const double a, const double b ) : myA(a), myB(b)
65 {
66  myCenter = aPoint;
67 }
68 
69 ///////////////////////////////////////////////////////////////////////////////
70 // ------------- Implementation of 'StarShaped' services ----------------------
71 
72 /**
73  * @param p any point in the plane.
74  *
75  * @return the angle parameter between 0 and 2*Pi corresponding to
76  * this point for the shape.
77  */
78 template <typename T>
79 inline
80 double
81 DGtal::Astroid2D<T>::parameter( const RealPoint2D & pp ) const
82 {
83  RealPoint2D p( pp );
84  p -= myCenter;
85  double angle;
86 
87  if ( p[0] == 0. && p[1] > 0. )
88  angle = M_PI/2.0;
89  else if ( p[0] == 0. && p[1] < 0. )
90  angle = 3.0*M_PI/2.0;
91  else if ( p[1] == 0. && p[0] >= 0. )
92  angle = 0.0;
93  else if ( p[1] == 0. && p[0] < 0. )
94  angle = M_PI;
95  else
96  {
97  if( p[1]/p[0] < 0. )
98  angle = atan(-pow(-(myA*p[1])/(myB*p[0]) , 1/3.));
99  else
100  angle = atan(pow(( myA*p[1])/(myB*p[0]) , 1/3.));
101 
102  if ( p[1] < 0. && p[0] > 0. )
103  angle += M_PI;
104  else if ( p[0] < 0. && p[1] > 0. )
105  angle += 2*M_PI;
106  else if ( p[0] < 0. && p[1] < 0. )
107  angle += M_PI;
108  }
109 
110  return angle;
111 }
112 
113 /**
114  * @param t any angle between 0 and 2*Pi.
115  *
116  * @return the vector (x(t),y(t)) which is the position on the
117  * shape boundary.
118  */
119 template <typename T>
120 inline
121 typename DGtal::Astroid2D<T>::RealPoint2D
122 DGtal::Astroid2D<T>::x( double t ) const
123 {
124  RealPoint2D c( myA*pow(cos(t),3), myB*pow(sin(t),3) );
125  c += myCenter;
126  return c;
127 }
128 
129 
130 /**
131  * @param t any angle between 0 and 2*Pi.
132  *
133  * @return the vector (x'(t),y'(t)) which is the tangent to the
134  * shape boundary.
135  */
136 template <typename T>
137 inline
138 typename DGtal::Astroid2D<T>::RealVector2D
139 DGtal::Astroid2D<T>::xp( const double t ) const
140 {
141 
142  RealVector2D c( myA * 3*(-sin(t))*pow(cos(t),2),
143  myB * 3*cos(t)*pow(sin(t),2) );
144  return c;
145 }
146 
147 template <typename T>
148 inline
149 typename DGtal::Astroid2D<T>::RealVector2D
150 DGtal::Astroid2D<T>::xpp( const double t ) const
151 {
152  RealVector2D c( -myA * 3*pow(cos(t),3)+6*pow(sin(t),2)*cos(t),
153  myB * (6*pow(cos(t),2)*sin(t) - 3*pow(sin(t),3)) );
154  return c;
155 }
156 
157 
158 ///////////////////////////////////////////////////////////////////////////////
159 // Interface - public :
160 
161 /**
162  * Writes/Displays the object on an output stream.
163  * @param out the output stream where the object is written.
164  */
165 template <typename T>
166 inline
167 void
168 DGtal::Astroid2D<T>::selfDisplay ( std::ostream & out ) const
169 {
170  out << "[Astroid2D] center= "<<myCenter<<" a="<<myA<<" b="<<myB;
171 }
172 
173 /**
174  * Checks the validity/consistency of the object.
175  * @return 'true' if the object is valid, 'false' otherwise.
176  */
177 template <typename T>
178 inline
179 bool
180 DGtal::Astroid2D<T>::isValid() const
181 {
182  return true;
183 }
184 
185 
186 ///////////////////////////////////////////////////////////////////////////////
187 // Implementation of inline functions //
188 
189 template <typename T>
190 inline
191 std::ostream&
192 DGtal::operator<< ( std::ostream & out,
193  const Astroid2D<T> & object )
194 {
195  object.selfDisplay( out );
196  return out;
197 }
198 
199 // //
200 ///////////////////////////////////////////////////////////////////////////////