DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
NGon2D.ih
1 
32 
33 #include <cstdlib>
35 
37 // IMPLEMENTATION of inline methods.
39 
41 // ----------------------- Standard services ------------------------------
42 
46 template <typename T>
47 inline
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
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
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 
80 // ------------- Implementation of 'StarShaped' services ------------------
81 
88 template <typename T>
89 inline
90 double
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 
124 template <typename T>
125 inline
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 
163 template <typename T>
164 inline
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 
196 template <typename T>
197 inline
199 DGtal::NGon2D<T>::xpp( const double /*t*/ ) const
200 {
201  RealVector2D c(0,0);
202  return c;
203 }
204 
205 
207 // Interface - public :
208 
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 
226 template <typename T>
227 inline
228 bool
230 {
231  return true;
232 }
233 
234 
235 
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 // //
251 
252