DGtal  0.9.2
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 ///////////////////////////////////////////////////////////////////////////////
37 // IMPLEMENTATION of inline methods.
38 ///////////////////////////////////////////////////////////////////////////////
39 
40 ///////////////////////////////////////////////////////////////////////////////
41 // ----------------------- Standard services ------------------------------
42 
43 /**
44  * Destructor.
45  */
46 template <typename T>
47 inline
48 DGtal::NGon2D<T>::~NGon2D()
49 {
50 }
51 
52 template <typename T>
53 inline
54 DGtal::NGon2D<T>::NGon2D(const double x0, const double y0,
55  const double radius, const unsigned int k,
56  const double phi):
57  myCenter(x0,y0), myRadius(radius), myK(k), myPhi(phi)
58 {}
59 
60 
61 template <typename T>
62 inline
63 DGtal::NGon2D<T>::NGon2D(const RealPoint2D &aPoint,
64  const double radius, const unsigned int k,
65  const double phi):
66  myCenter(aPoint), myRadius(radius) , myK(k), myPhi(phi)
67 {}
68 
69 template <typename T>
70 inline
71 DGtal::NGon2D<T>::NGon2D(const Point &aPoint,
72  const double radius, const unsigned int k,
73  const double phi):
74  myRadius(radius), myK(k), myPhi(phi)
75 {
76  myCenter = aPoint;
77 }
78 
79 /////////////////////////////////////////////////////////////////////////////
80 // ------------- Implementation of 'StarShaped' services ------------------
81 
82 /**
83  * @param p any point in the plane.
84  *
85  * @return the angle parameter between 0 and 2*Pi corresponding to
86  * this point for the shape.
87  */
88 template <typename T>
89 inline
90 double
91 DGtal::NGon2D<T>::parameter( const RealPoint2D & pp ) const
92 {
93  RealPoint2D p( pp );
94  p -= myCenter;
95 
96  double t = atan2( p[ 1 ], p[ 0 ] );
97  return ( t < 0.0 ) ? ( t + 2.0 * M_PI ) : t;
98  // double angle = 0.0;
99  // if ( p[0] == 0.0 )
100  // {
101  // if ( p[1] >0 )
102  // angle = M_PI/2.0;
103  // else
104  // angle = 1.5*M_PI;
105  // }
106  // else if ( ( p[0] > 0.0 ) && ( p[1] >= 0.0 ) )
107  // angle = atan(p[1]/p[0]);
108  // else if ( ( p[0] > 0.0 ) && ( p[1] <= 0.0 ) )
109  // angle = 2*M_PI + atan(p[1]/p[0]);
110  // else if ( ( p[0] < 0.0 ) && ( p[1] >= 0.0 ) )
111  // angle = atan(p[1]/p[0]) + M_PI;
112  // else // ( ( p[0] < 0.0 ) && ( p[1] <= 0.0 ) )
113  // angle = atan(p[1]/p[0]) + M_PI;
114 
115  // return angle;
116 }
117 
118 /**
119  * @param t any angle between 0 and 2*Pi.
120  *
121  * @return the vector (x(t),y(t)) which is the position on the
122  * shape boundary.
123  */
124 template <typename T>
125 inline
126 typename DGtal::NGon2D<T>::RealPoint2D
127 DGtal::NGon2D<T>::x( double t ) const
128 {
129  double angle = t - myPhi;
130  while ( angle < 0.0 )
131  angle += 2.0*M_PI;
132 
133 
134  // seek the vertices between the point, then compute the vector from one vertex to the next one.
135 
136  unsigned int intervale_lower = static_cast<unsigned int>( floor( ( angle )* myK / (2.0 * M_PI ) ) );
137  unsigned int intervale_upper = intervale_lower == ( myK -1 ) ? 0 : intervale_lower+1;
138  double dist = myRadius*cos ( M_PI / myK );
139  RealPoint2D s1 ( myRadius*cos(myPhi + intervale_lower*2.0*M_PI/myK),
140  myRadius*sin(myPhi + intervale_lower*2.0*M_PI/myK) );
141  RealPoint2D s2 ( myRadius*cos(myPhi + intervale_upper*2.0*M_PI/myK),
142  myRadius*sin(myPhi + intervale_upper*2.0*M_PI/myK) );
143  RealPoint2D s3( s2[0] - s1[0], s2[1] - s1[1]);
144 
145  double line_angle = atan2f( (float)s3[ 1 ], (float)s3[ 0 ]);
146 
147  double rho = dist/(cos (t - line_angle - 0.5*M_PI));
148 
149  RealPoint2D c( rho*cos(t), rho*sin(t) );
150 
151  c += myCenter;
152 
153  return c;
154 }
155 
156 
157 /**
158  * @param t any angle between 0 and 2*Pi.
159  *
160  * @return the vector (x'(t),y'(t)) which is the tangent to the
161  * shape boundary.
162  */
163 template <typename T>
164 inline
165 typename DGtal::NGon2D<T>::RealVector2D
166 DGtal::NGon2D<T>::xp( const double t ) const
167 {
168  // seek the vertices between the point, then compute the vector from one vertex to the next one.
169  // TODO check if angle equals that of a vertex ?
170  double angle = t - myPhi;
171  while ( angle < 0.0 )
172  angle += 2.0*M_PI;
173 
174  unsigned int intervalle_lower = static_cast<unsigned int>( floor( angle * myK / (2.0 * M_PI ) ) );
175  unsigned int intervalle_upper = intervalle_lower == ( myK -1 ) ? 0 : intervalle_lower+1;
176  //float dist = myRadius*sin ( M_PI / myK );
177  RealPoint2D s1 ( myRadius*cos(myPhi + intervalle_lower*2.0*M_PI/myK),
178  myRadius*sin(myPhi + intervalle_lower*2.0*M_PI/myK) );
179  RealPoint2D s2 ( myRadius*cos(myPhi + intervalle_upper*2.0*M_PI/myK),
180  myRadius*sin(myPhi + intervalle_upper*2.0*M_PI/myK) );
181  s2 -= s1;
182 
183  //normalize
184  double norm = s2.norm();
185  s2[0] /= norm;
186  s2[1] /= norm;
187 
188  return s2;
189 }
190 
191 /**
192  * @param t any angle between 0 and 2*Pi.
193  *
194  * @return the vector (x''(t),y''(t)).
195  */
196 template <typename T>
197 inline
198 typename DGtal::NGon2D<T>::RealVector2D
199 DGtal::NGon2D<T>::xpp( const double /*t*/ ) const
200 {
201  RealVector2D c(0,0);
202  return c;
203 }
204 
205 
206 ///////////////////////////////////////////////////////////////////////////////
207 // Interface - public :
208 
209 /**
210  * Writes/Displays the object on an output stream.
211  * @param out the output stream where the object is written.
212  */
213 template <typename T>
214 inline
215 void
216 DGtal::NGon2D<T>::selfDisplay ( std::ostream & out ) const
217 {
218  out << "[NGon2D] center= "<<myCenter<<" radius="<<myRadius<<" number of sides="<<myK
219  << " phase-shift="<<myPhi;
220 }
221 
222 /**
223  * Checks the validity/consistency of the object.
224  * @return 'true' if the object is valid, 'false' otherwise.
225  */
226 template <typename T>
227 inline
228 bool
229 DGtal::NGon2D<T>::isValid() const
230 {
231  return true;
232 }
233 
234 
235 
236 ///////////////////////////////////////////////////////////////////////////////
237 // Implementation of inline functions //
238 
239 template <typename T>
240 inline
241 std::ostream&
242 DGtal::operator<< ( std::ostream & out,
243  const NGon2D<T> & object )
244 {
245  object.selfDisplay( out );
246  return out;
247 }
248 
249 // //
250 ///////////////////////////////////////////////////////////////////////////////
251 
252