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