DGtal  1.5.beta
Ellipse2D.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 Ellipse2D.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 Ellipse2D.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 #define ELLIPSE2D_3_PI_2 (1.5*M_PI)
41 #define ELLIPSE2D_2_PI (2.*M_PI)
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 // ----------------------- Standard services ------------------------------
45 
46 template <typename T>
47 inline
48 DGtal::Ellipse2D<T>::Ellipse2D(const double x0, const double y0,
49  const double a0, const double a1, const double theta)
50  : myCenter(x0,y0), myAxis1(a0), myAxis2(a1), myTheta(theta)
51 {}
52 
53 template <typename T>
54 inline
55 DGtal::Ellipse2D<T>::Ellipse2D(const RealPoint &aPoint,
56  const double a0, const double a1, const double theta)
57  : myCenter(aPoint), myAxis1(a0), myAxis2(a1), myTheta(theta)
58 {}
59 
60 template <typename T>
61 inline
62 DGtal::Ellipse2D<T>::Ellipse2D(const Ellipse2D& other)
63  : myCenter(other.myCenter), myAxis1(other.myAxis1),
64  myAxis2(other.myAxis2), myTheta(other.myTheta)
65 {}
66 
67 /////////////////////////////////////////////////////////////////////////////
68 // ------------- Implementation of 'StarShaped' services ------------------
69 
70 /**
71  * @param pp any point in the plane.
72  *
73  * @return the angle parameter between 0 and 2*Pi corresponding to
74  * this point for the shape.
75  */
76 template <typename T>
77 inline
78 double
79 DGtal::Ellipse2D<T>::parameter( const RealPoint & pp ) const
80 {
81  RealPoint v2d( pp - myCenter );
82 
83  double angle;
84 
85  if ( isAlmostEqual(v2d[0],0.) )
86  angle = v2d[1] > 0. ? M_PI_2 : ELLIPSE2D_3_PI_2;
87  else if ( v2d[0] > 0. )
88  angle = atan(v2d[1]/v2d[0]) + (v2d[1] < 0. ? ELLIPSE2D_2_PI : 0.);
89  else
90  angle = atan(v2d[1]/v2d[0]) + M_PI;
91 
92  return angle;
93 }
94 
95 /**
96  * @param t any angle between 0 and 2*Pi.
97  *
98  * @return the vector (x(t),y(t)) which is the position on the
99  * shape boundary.
100  */
101 template <typename T>
102 inline
103 typename DGtal::Ellipse2D<T>::RealPoint
104 DGtal::Ellipse2D<T>::x( const double t ) const
105 {
106  const double a2 = myAxis1*myAxis1;
107  const double b2 = myAxis2*myAxis2;
108  const double costth = cos(t - myTheta);
109  const double rho = myAxis2 / sqrt(1. - (a2-b2) / a2 * costth * costth);
110 
111  return RealPoint(
112  rho * cos(t) + myCenter[0],
113  rho * sin(t) + myCenter[1] );
114 }
115 
116 
117 /**
118  * @param t any angle between 0 and 2*Pi.
119  *
120  * @return the vector (x'(t),y'(t)) which is the tangent to the
121  * shape boundary.
122  */
123 template <typename T>
124 inline
125 typename DGtal::Ellipse2D<T>::RealVector
126 DGtal::Ellipse2D<T>::xp( const double t ) const
127 {
128  const double a2 = myAxis1 * myAxis1;
129  const double b2 = myAxis2 * myAxis2;
130  const double costth = cos(t - myTheta);
131  const double sintth = sin(t - myTheta);
132  const double cost = cos(t);
133  const double sint = sin(t);
134  const double rho = myAxis2 / sqrt(1. - (a2-b2) / a2 * costth * costth);
135  const double rhod = myAxis1 * myAxis2 * (b2-a2) * sintth * costth
136  / std::pow(a2 * sintth * sintth + b2 * costth * costth, 1.5);
137 
138  return RealPoint(
139  rhod*cost - rho*sint,
140  rhod*sint + rho*cost
141  );
142 }
143 
144 /**
145  * @param t any angle between 0 and 2*Pi.
146  *
147  * @return the vector (x''(t),y''(t)).
148  */
149 template <typename T>
150 inline
151 typename DGtal::Ellipse2D<T>::RealVector
152 DGtal::Ellipse2D<T>::xpp( const double t ) const
153 {
154  const double a2 = myAxis1 * myAxis1;
155  const double b2 = myAxis2 * myAxis2;
156  const double costth = cos(t - myTheta);
157  const double sintth = sin(t - myTheta);
158  const double cost = cos(t);
159  const double sint = sin(t);
160  const double rho = myAxis2 / sqrt( 1. - (a2-b2)/a2*costth*costth);
161 
162  const double rhod = myAxis1 * myAxis2 * (b2-a2) * sintth * costth
163  / std::pow( a2*sintth*sintth + b2*costth*costth, 1.5 );
164  const double rhodd = myAxis1 * myAxis2 * (b2-a2)
165  / std::pow( a2*sintth*sintth + b2*costth*costth, 2.5 )
166  * ( (costth*costth - sintth*sintth) * (a2*sintth*sintth + b2*costth*costth)
167  + 3.*(b2-a2)*sintth*sintth*costth*costth );
168 
169  return RealPoint(
170  rhodd*cost - 2.*rhod*sint - rho*cost,
171  rhodd*sint + 2.*rhod*cost - rho*sint
172  );
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::Ellipse2D<T>::selfDisplay ( std::ostream & out ) const
187 {
188  out << "[Ellipse2D] center= " << myCenter
189  << " big axis=" << myAxis1
190  << " small axis=" << myAxis2
191  << " phase=" << myTheta;
192 }
193 
194 /**
195  * Checks the validity/consistency of the object.
196  * @return 'true' if the object is valid, 'false' otherwise.
197  */
198 template <typename T>
199 inline
200 bool
201 DGtal::Ellipse2D<T>::isValid() const
202 {
203  return true;
204 }
205 
206 
207 
208 ///////////////////////////////////////////////////////////////////////////////
209 // Implementation of inline functions //
210 
211 template <typename T>
212 inline
213 std::ostream&
214 DGtal::operator<< ( std::ostream & out,
215  const Ellipse2D<T> & object )
216 {
217  object.selfDisplay( out );
218  return out;
219 }
220 
221 // //
222 ///////////////////////////////////////////////////////////////////////////////
223 
224