DGtal  1.0.0
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 angle = atan2( p[1], p[0] );
97  angle = ( angle < 0.0 ) ? angle + 2 * M_PI : angle;
98 
99  return angle;
100 }
101 
102 /**
103  * @param t any angle between 0 and 2*Pi.
104  *
105  * @return the vector (x(t),y(t)) which is the position on the
106  * shape boundary.
107  */
108 template <typename T>
109 inline
110 typename DGtal::NGon2D<T>::RealPoint2D
111 DGtal::NGon2D<T>::x( double t ) const
112 {
113  double angle = t - myPhi;
114  angle = (angle < 0.0) ? angle + 2 * M_PI : angle;
115 
116  // seek the vertices between the point, then compute the vector from one vertex to the next one.
117 
118  unsigned int intervale_lower = static_cast<unsigned int>( floor( ( angle ) * myK / (2.0 * M_PI ) ) );
119  unsigned int intervale_upper = intervale_lower == ( myK - 1 ) ? 0 : intervale_lower + 1;
120  double dist = myRadius * cos( M_PI / myK );
121  RealPoint2D s1 ( myRadius * cos( myPhi + intervale_lower * 2.0 * M_PI / myK ),
122  myRadius * sin( myPhi + intervale_lower * 2.0 * M_PI / myK ));
123  RealPoint2D s2 ( myRadius * cos( myPhi + intervale_upper * 2.0 * M_PI / myK ),
124  myRadius * sin( myPhi + intervale_upper * 2.0 * M_PI / myK ));
125  RealPoint2D s3 ( s2[0] - s1[0],
126  s2[1] - s1[1]);
127 
128  //double line_angle = atan2f( (float)s3[ 1 ], (float)s3[ 0 ]);
129  double line_angle = atan2( s3[1], s3[0] );
130  //line_angle = (line_angle < 0.0) ? angle + 2 * M_PI : line_angle;
131 
132  double rho = dist / ( cos(t - line_angle - 0.5 * M_PI) );
133 
134  RealPoint2D c( - rho*cos(t), - rho*sin(t) );
135 
136  c += myCenter;
137 
138  return c;
139 }
140 
141 
142 /**
143  * @param t any angle between 0 and 2*Pi.
144  *
145  * @return the vector (x'(t),y'(t)) which is the tangent to the
146  * shape boundary.
147  */
148 template <typename T>
149 inline
150 typename DGtal::NGon2D<T>::RealVector2D
151 DGtal::NGon2D<T>::xp( const double t ) const
152 {
153  // seek the vertices between the point, then compute the vector from one vertex to the next one.
154  // TODO check if angle equals that of a vertex ?
155  double angle = t - myPhi;
156  while ( angle < 0.0 )
157  angle += 2.0*M_PI;
158 
159  unsigned int intervalle_lower = static_cast<unsigned int>( floor( angle * myK / (2.0 * M_PI ) ) );
160  unsigned int intervalle_upper = intervalle_lower == ( myK -1 ) ? 0 : intervalle_lower+1;
161  //float dist = myRadius*sin ( M_PI / myK );
162  RealPoint2D s1 ( myRadius*cos(myPhi + intervalle_lower*2.0*M_PI/myK),
163  myRadius*sin(myPhi + intervalle_lower*2.0*M_PI/myK) );
164  RealPoint2D s2 ( myRadius*cos(myPhi + intervalle_upper*2.0*M_PI/myK),
165  myRadius*sin(myPhi + intervalle_upper*2.0*M_PI/myK) );
166  s2 -= s1;
167 
168  //normalize
169  double norm = s2.norm();
170  s2[0] /= norm;
171  s2[1] /= norm;
172 
173  return s2;
174 }
175 
176 /**
177  * @param t any angle between 0 and 2*Pi.
178  *
179  * @return the vector (x''(t),y''(t)).
180  */
181 template <typename T>
182 inline
183 typename DGtal::NGon2D<T>::RealVector2D
184 DGtal::NGon2D<T>::xpp( const double /*t*/ ) const
185 {
186  RealVector2D c(0,0);
187  return c;
188 }
189 
190 
191 ///////////////////////////////////////////////////////////////////////////////
192 // Interface - public :
193 
194 /**
195  * Writes/Displays the object on an output stream.
196  * @param out the output stream where the object is written.
197  */
198 template <typename T>
199 inline
200 void
201 DGtal::NGon2D<T>::selfDisplay ( std::ostream & out ) const
202 {
203  out << "[NGon2D] center= "<<myCenter<<" radius="<<myRadius<<" number of sides="<<myK
204  << " phase-shift="<<myPhi;
205 }
206 
207 /**
208  * Checks the validity/consistency of the object.
209  * @return 'true' if the object is valid, 'false' otherwise.
210  */
211 template <typename T>
212 inline
213 bool
214 DGtal::NGon2D<T>::isValid() const
215 {
216  return true;
217 }
218 
219 
220 
221 ///////////////////////////////////////////////////////////////////////////////
222 // Implementation of inline functions //
223 
224 template <typename T>
225 inline
226 std::ostream&
227 DGtal::operator<< ( std::ostream & out,
228  const NGon2D<T> & object )
229 {
230  object.selfDisplay( out );
231  return out;
232 }
233 
234 // //
235 ///////////////////////////////////////////////////////////////////////////////
236 
237