DGtal  1.5.beta
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 #define ASTROID_3_PI_2 (3. * M_PI_2)
34 #define ASTROID_2_PI (2. * M_PI)
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // IMPLEMENTATION of inline methods.
38 ///////////////////////////////////////////////////////////////////////////////
39 
40 ///////////////////////////////////////////////////////////////////////////////
41 // ----------------------- Standard services ----------------------------------
42 
43 template <typename T>
44 inline
45 DGtal::Astroid2D<T>::Astroid2D( const double x0, const double y0,
46  const double a, const double b ) : myCenter(x0,y0), myA(fabs(a)), myB(fabs(b))
47 {}
48 
49 template <typename T>
50 inline
51 DGtal::Astroid2D<T>::Astroid2D( const RealPoint &aPoint,
52  const double a, const double b ) : myCenter(aPoint), myA(fabs(a)), myB(fabs(b))
53 {}
54 
55 template <typename T>
56 inline
57 DGtal::Astroid2D<T>::Astroid2D( const Astroid2D& other ) :
58  myCenter(other.myCenter), myA(other.myA), myB(other.myB)
59 {}
60 
61 ///////////////////////////////////////////////////////////////////////////////
62 // ------------- Implementation of 'StarShaped' services ----------------------
63 
64 /**
65  * @param pp any point in the plane.
66  *
67  * @return the angle parameter between 0 and 2*Pi corresponding to
68  * this point for the shape.
69  */
70 template <typename T>
71 inline
72 double
73 DGtal::Astroid2D<T>::parameter( const RealPoint & pp ) const
74 {
75  const RealPoint p( pp - myCenter );
76  double angle = 0.;
77 
78  if ( isAlmostEqual(p[0],0.) )
79  angle = p[1] > 0. ? M_PI_2 : ASTROID_3_PI_2;
80  else if ( isAlmostEqual(p[1],0.) )
81  angle = p[0] > 0. ? 0. : M_PI;
82  else if ( ! isAlmostEqual(myB,0.) )
83  {
84  if( p[1]/p[0] < 0. )
85  angle = atan(-pow(-(myA*p[1])/(myB*p[0]) , 1/3.))
86  + (p[0] > 0. ? ASTROID_2_PI : M_PI);
87  else
88  angle = atan(pow(( myA*p[1])/(myB*p[0]) , 1/3.));
89  }
90 
91  return angle;
92 }
93 
94 /**
95  * @param t any angle between 0 and 2*Pi.
96  *
97  * @return the vector (x(t),y(t)) which is the position on the
98  * shape boundary.
99  */
100 template <typename T>
101 inline
102 typename DGtal::Astroid2D<T>::RealPoint
103 DGtal::Astroid2D<T>::x( const double t ) const
104 {
105  return RealPoint( myA*pow(cos(t),3), myB*pow(sin(t),3) ) + myCenter;
106 }
107 
108 
109 /**
110  * @param t any angle between 0 and 2*Pi.
111  *
112  * @return the vector (x'(t),y'(t)) which is the tangent to the
113  * shape boundary.
114  */
115 template <typename T>
116 inline
117 typename DGtal::Astroid2D<T>::RealVector
118 DGtal::Astroid2D<T>::xp( const double t ) const
119 {
120 
121  return RealVector( myA * 3*(-sin(t))*pow(cos(t),2),
122  myB * 3*cos(t)*pow(sin(t),2) );
123 }
124 
125 template <typename T>
126 inline
127 typename DGtal::Astroid2D<T>::RealVector
128 DGtal::Astroid2D<T>::xpp( const double t ) const
129 {
130  return RealVector( -myA * 3*pow(cos(t),3) + 6*pow(sin(t),2)*cos(t),
131  myB * (6*pow(cos(t),2)*sin(t) - 3*pow(sin(t),3)) );
132 }
133 
134 
135 ///////////////////////////////////////////////////////////////////////////////
136 // Interface - public :
137 
138 /**
139  * Writes/Displays the object on an output stream.
140  * @param out the output stream where the object is written.
141  */
142 template <typename T>
143 inline
144 void
145 DGtal::Astroid2D<T>::selfDisplay ( std::ostream & out ) const
146 {
147  out << "[Astroid2D] center= " << myCenter
148  << " a=" << myA
149  << " b=" << myB;
150 }
151 
152 /**
153  * Checks the validity/consistency of the object.
154  * @return 'true' if the object is valid, 'false' otherwise.
155  */
156 template <typename T>
157 inline
158 bool
159 DGtal::Astroid2D<T>::isValid() const
160 {
161  return true;
162 }
163 
164 
165 ///////////////////////////////////////////////////////////////////////////////
166 // Implementation of inline functions //
167 
168 template <typename T>
169 inline
170 std::ostream&
171 DGtal::operator<< ( std::ostream & out,
172  const Astroid2D<T> & object )
173 {
174  object.selfDisplay( out );
175  return out;
176 }
177 
178 // //
179 ///////////////////////////////////////////////////////////////////////////////