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.
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.
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/>.
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
26 * Implementation of inline methods defined in NGon2D.h
28 * This file is part of the DGtal library.
32 //////////////////////////////////////////////////////////////////////////////
34 //////////////////////////////////////////////////////////////////////////////
36 #define NGON2D_2_PI (2. * M_PI)
38 ///////////////////////////////////////////////////////////////////////////////
39 // IMPLEMENTATION of inline methods.
40 ///////////////////////////////////////////////////////////////////////////////
42 ///////////////////////////////////////////////////////////////////////////////
43 // ----------------------- Standard services ------------------------------
47 DGtal::NGon2D<T>::NGon2D(const double x0, const double y0,
48 const double radius, const unsigned int k,
50 myCenter(x0,y0), myRadius(radius), myK(k), myPhi(phi)
56 DGtal::NGon2D<T>::NGon2D(const RealPoint &aPoint,
57 const double radius, const unsigned int k,
59 myCenter(aPoint), myRadius(radius) , myK(k), myPhi(phi)
64 DGtal::NGon2D<T>::NGon2D(const NGon2D& other):
65 myCenter(other.myCenter), myRadius(other.myRadius),
66 myK(other.myK), myPhi(other.myPhi)
69 /////////////////////////////////////////////////////////////////////////////
70 // ------------- Implementation of 'StarShaped' services ------------------
73 * @param p any point in the plane.
75 * @return the angle parameter between 0 and 2*Pi corresponding to
76 * this point for the shape.
81 DGtal::NGon2D<T>::parameter( const RealPoint & p ) const
83 const double angle = atan2( p[1]-myCenter[1], p[0]-myCenter[0] );
84 return angle < 0. ? angle + NGON2D_2_PI : angle;
88 * @param t any angle between 0 and 2*Pi.
90 * @return the vector (x(t),y(t)) which is the position on the
95 typename DGtal::NGon2D<T>::RealPoint
96 DGtal::NGon2D<T>::x( const double t ) const
98 double angle = t - myPhi;
99 angle = (angle < 0.0) ? angle + NGON2D_2_PI : angle;
101 // seek the vertices between the point, then compute the vector from one vertex to the next one.
103 const unsigned int intervale_lower = static_cast<unsigned int>( floor( angle * myK / NGON2D_2_PI ) );
104 const unsigned int intervale_upper = intervale_lower == ( myK - 1 ) ? 0 : intervale_lower + 1;
105 const RealPoint s1 ( myRadius * cos( myPhi + intervale_lower * NGON2D_2_PI / myK ),
106 myRadius * sin( myPhi + intervale_lower * NGON2D_2_PI / myK ));
107 const RealPoint s2 ( myRadius * cos( myPhi + intervale_upper * NGON2D_2_PI / myK ),
108 myRadius * sin( myPhi + intervale_upper * NGON2D_2_PI / myK ));
110 const double line_angle = atan2( s2[1]-s1[1], s2[0]-s1[0] );
111 //line_angle = (line_angle < 0.0) ? angle + 2 * M_PI : line_angle;
112 const double rho = myRadius * cos(M_PI / myK) / cos(t - line_angle - M_PI_2);
115 -rho * cos(t) + myCenter[0],
116 -rho * sin(t) + myCenter[1]
122 * @param t any angle between 0 and 2*Pi.
124 * @return the vector (x'(t),y'(t)) which is the tangent to the
127 template <typename T>
129 typename DGtal::NGon2D<T>::RealVector
130 DGtal::NGon2D<T>::xp( const double t ) const
132 // seek the vertices between the point, then compute the vector from one vertex to the next one.
133 // TODO check if angle equals that of a vertex ?
134 double angle = t - myPhi;
135 while ( angle < 0.0 )
136 angle += NGON2D_2_PI;
138 unsigned int intervalle_lower = static_cast<unsigned int>( floor( angle * myK / (NGON2D_2_PI ) ) );
139 unsigned int intervalle_upper = intervalle_lower == ( myK -1 ) ? 0 : intervalle_lower+1;
142 myRadius*cos(myPhi + intervalle_upper*NGON2D_2_PI/myK)
143 - myRadius*cos(myPhi + intervalle_lower*NGON2D_2_PI/myK),
144 myRadius*sin(myPhi + intervalle_upper*NGON2D_2_PI/myK)
145 - myRadius*sin(myPhi + intervalle_lower*NGON2D_2_PI/myK)
154 * @return the vector (x''(t),y''(t)).
156 template <typename T>
158 typename DGtal::NGon2D<T>::RealVector
159 DGtal::NGon2D<T>::xpp( const double /*t*/ ) const
161 return RealVector(0.,0.);
165 ///////////////////////////////////////////////////////////////////////////////
166 // Interface - public :
169 * Writes/Displays the object on an output stream.
170 * @param out the output stream where the object is written.
172 template <typename T>
175 DGtal::NGon2D<T>::selfDisplay ( std::ostream & out ) const
177 out << "[NGon2D] center= " << myCenter
178 << " radius=" << myRadius
179 << " number of sides=" << myK
180 << " phase-shift=" << myPhi;
184 * Checks the validity/consistency of the object.
185 * @return 'true' if the object is valid, 'false' otherwise.
187 template <typename T>
190 DGtal::NGon2D<T>::isValid() const
197 ///////////////////////////////////////////////////////////////////////////////
198 // Implementation of inline functions //
200 template <typename T>
203 DGtal::operator<< ( std::ostream & out,
204 const NGon2D<T> & object )
206 object.selfDisplay( out );
211 ///////////////////////////////////////////////////////////////////////////////