DGtal  1.5.beta
NGon2D.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 NGon2D.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 NGon2D.h
27  *
28  * This file is part of the DGtal library.
29  */
30 
31 
32 //////////////////////////////////////////////////////////////////////////////
33 #include <cstdlib>
34 //////////////////////////////////////////////////////////////////////////////
35 
36 #define NGON2D_2_PI (2. * M_PI)
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // IMPLEMENTATION of inline methods.
40 ///////////////////////////////////////////////////////////////////////////////
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 // ----------------------- Standard services ------------------------------
44 
45 template <typename T>
46 inline
47 DGtal::NGon2D<T>::NGon2D(const double x0, const double y0,
48  const double radius, const unsigned int k,
49  const double phi):
50  myCenter(x0,y0), myRadius(radius), myK(k), myPhi(phi)
51 {}
52 
53 
54 template <typename T>
55 inline
56 DGtal::NGon2D<T>::NGon2D(const RealPoint &aPoint,
57  const double radius, const unsigned int k,
58  const double phi):
59  myCenter(aPoint), myRadius(radius) , myK(k), myPhi(phi)
60 {}
61 
62 template <typename T>
63 inline
64 DGtal::NGon2D<T>::NGon2D(const NGon2D& other):
65  myCenter(other.myCenter), myRadius(other.myRadius),
66  myK(other.myK), myPhi(other.myPhi)
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::NGon2D<T>::parameter( const RealPoint & p ) const
82 {
83  const double angle = atan2( p[1]-myCenter[1], p[0]-myCenter[0] );
84  return angle < 0. ? angle + NGON2D_2_PI : angle;
85 }
86 
87 /**
88  * @param t any angle between 0 and 2*Pi.
89  *
90  * @return the vector (x(t),y(t)) which is the position on the
91  * shape boundary.
92  */
93 template <typename T>
94 inline
95 typename DGtal::NGon2D<T>::RealPoint
96 DGtal::NGon2D<T>::x( const double t ) const
97 {
98  double angle = t - myPhi;
99  angle = (angle < 0.0) ? angle + NGON2D_2_PI : angle;
100 
101  // seek the vertices between the point, then compute the vector from one vertex to the next one.
102 
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 ));
109 
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);
113 
114  return RealPoint(
115  -rho * cos(t) + myCenter[0],
116  -rho * sin(t) + myCenter[1]
117  );
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::NGon2D<T>::RealVector
130 DGtal::NGon2D<T>::xp( const double t ) const
131 {
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;
137 
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;
140 
141  const RealPoint s(
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)
146  );
147 
148  //normalize
149  return s / s.norm();
150 }
151 
152 /**
153  *
154  * @return the vector (x''(t),y''(t)).
155  */
156 template <typename T>
157 inline
158 typename DGtal::NGon2D<T>::RealVector
159 DGtal::NGon2D<T>::xpp( const double /*t*/ ) const
160 {
161  return RealVector(0.,0.);
162 }
163 
164 
165 ///////////////////////////////////////////////////////////////////////////////
166 // Interface - public :
167 
168 /**
169  * Writes/Displays the object on an output stream.
170  * @param out the output stream where the object is written.
171  */
172 template <typename T>
173 inline
174 void
175 DGtal::NGon2D<T>::selfDisplay ( std::ostream & out ) const
176 {
177  out << "[NGon2D] center= " << myCenter
178  << " radius=" << myRadius
179  << " number of sides=" << myK
180  << " phase-shift=" << myPhi;
181 }
182 
183 /**
184  * Checks the validity/consistency of the object.
185  * @return 'true' if the object is valid, 'false' otherwise.
186  */
187 template <typename T>
188 inline
189 bool
190 DGtal::NGon2D<T>::isValid() const
191 {
192  return true;
193 }
194 
195 
196 
197 ///////////////////////////////////////////////////////////////////////////////
198 // Implementation of inline functions //
199 
200 template <typename T>
201 inline
202 std::ostream&
203 DGtal::operator<< ( std::ostream & out,
204  const NGon2D<T> & object )
205 {
206  object.selfDisplay( out );
207  return out;
208 }
209 
210 // //
211 ///////////////////////////////////////////////////////////////////////////////
212 
213