DGtal 1.3.0
Loading...
Searching...
No Matches
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
46template <typename T>
47inline
48DGtal::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
53template <typename T>
54inline
55DGtal::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
60template <typename T>
61inline
62DGtal::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 */
76template <typename T>
77inline
78double
79DGtal::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 */
101template <typename T>
102inline
103typename DGtal::Ellipse2D<T>::RealPoint
104DGtal::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 */
123template <typename T>
124inline
125typename DGtal::Ellipse2D<T>::RealVector
126DGtal::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 */
149template <typename T>
150inline
151typename DGtal::Ellipse2D<T>::RealVector
152DGtal::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 */
183template <typename T>
184inline
185void
186DGtal::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 */
198template <typename T>
199inline
200bool
201DGtal::Ellipse2D<T>::isValid() const
202{
203 return true;
204}
205
206
207
208///////////////////////////////////////////////////////////////////////////////
209// Implementation of inline functions //
210
211template <typename T>
212inline
213std::ostream&
214DGtal::operator<< ( std::ostream & out,
215 const Ellipse2D<T> & object )
216{
217 object.selfDisplay( out );
218 return out;
219}
220
221// //
222///////////////////////////////////////////////////////////////////////////////
223
224