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