DGtal  1.0.0
Lemniscate2D.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 Lemniscate2D.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 Lemniscate2D.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::Lemniscate2D<T>::~Lemniscate2D()
46 {
47 }
48 
49 template <typename T>
50 inline
51 DGtal::Lemniscate2D<T>::Lemniscate2D( 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::Lemniscate2D<T>::Lemniscate2D( 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::Lemniscate2D<T>::Lemniscate2D( 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::Lemniscate2D<T>::parameter( const RealPoint2D & pp ) const
82 {
83  RealPoint2D p( pp );
84  p -= myCenter;
85  double angle;
86 
87  if ( abs(p[1]) > abs(p[0]) )
88  return 0.;
89  if ( p[0] == 0. )
90  angle=0.0;
91  else if ( p[0] == 0. && p[1] < 0. )
92  angle=M_PI;
93  else if ( p[1] == 0. && p[0] > 0. )
94  angle=M_PI/2.0;
95  else if ( p[1] == 0. && p[0] < 0. )
96  angle=3.0*M_PI/2.0;
97  else
98  angle = acos(p[1]/p[0]);
99 
100  return angle;
101 }
102 
103 /**
104  * @param t any angle between 0 and 2*Pi.
105  *
106  * @return the vector (x(t),y(t)) which is the position on the
107  * shape boundary.
108  */
109 template <typename T>
110 inline
111 typename DGtal::Lemniscate2D<T>::RealPoint2D
112 DGtal::Lemniscate2D<T>::x( double t ) const
113 {
114  RealPoint2D c( myA * sin(t)/(1+pow(cos(t),2)),
115  myB * (sin(t)*cos(t)) / (1+pow(cos(t),2)) );
116  c += myCenter;
117  return c;
118 }
119 
120 
121 /**
122  * @param t any angle between 0 and 2*Pi.
123  *
124  * @return the vector (x'(t),y'(t)) which is the tangent to the
125  * shape boundary.
126  */
127 template <typename T>
128 inline
129 typename DGtal::Lemniscate2D<T>::RealVector2D
130 DGtal::Lemniscate2D<T>::xp( const double t ) const
131 {
132 
133  RealVector2D c(
134  myA * (cos(t)+2*pow(sin(t),2)*cos(t)+pow(cos(t),3))/pow(1+pow(cos(t),2),2),
135  myB * (pow(cos(t),4)+pow(cos(t),2)-pow(sin(t),2)+
136  pow(sin(t),2)*pow(cos(t),2)) / pow(1+pow(cos(t),2),2));
137  return c;
138 }
139 
140 template <typename T>
141 inline
142 typename DGtal::Lemniscate2D<T>::RealVector2D
143 DGtal::Lemniscate2D<T>::xpp( const double t ) const
144 {
145 
146  RealVector2D c( myA * ( 4*pow(sin(t),3)*pow(cos(t),2) +
147  6*pow(sin(t),3)*pow(cos(t),4) +
148  3*sin(t)*pow(cos(t),2) - sin(t) +
149  9*sin(t)*pow(cos(t),4) +
150  5*pow(cos(t),6) * sin(t) -
151  2*pow(sin(t),3) ) / pow(1+pow(cos(t),2),4),
152  myB * ( -4*pow(sin(t),3)*pow(cos(t),3) -
153  6*pow(sin(t),3)*cos(t) +
154  2*pow(sin(t),3)*pow(cos(t),5) -
155  4*sin(t)*cos(t) - 6*sin(t)*pow(cos(t),3) +
156  2*sin(t)*pow(cos(t),7) ) / pow(1+pow(cos(t),2),4) );
157  return c;
158 }
159 
160 
161 ///////////////////////////////////////////////////////////////////////////////
162 // Interface - public :
163 
164 /**
165  * Writes/Displays the object on an output stream.
166  * @param out the output stream where the object is written.
167  */
168 template <typename T>
169 inline
170 void
171 DGtal::Lemniscate2D<T>::selfDisplay ( std::ostream & out ) const
172 {
173  out << "[Lemniscate2D] center= "<<myCenter<<" a="<<myA<<" b="<<myB;
174 }
175 
176 /**
177  * Checks the validity/consistency of the object.
178  * @return 'true' if the object is valid, 'false' otherwise.
179  */
180 template <typename T>
181 inline
182 bool
183 DGtal::Lemniscate2D<T>::isValid() const
184 {
185  return true;
186 }
187 
188 
189 ///////////////////////////////////////////////////////////////////////////////
190 // Implementation of inline functions //
191 
192 template <typename T>
193 inline
194 std::ostream&
195 DGtal::operator<< ( std::ostream & out,
196  const Lemniscate2D<T> & object )
197 {
198  object.selfDisplay( out );
199  return out;
200 }
201 
202 // //
203 ///////////////////////////////////////////////////////////////////////////////