DGtal  0.9.2
StarShaped2D.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 StarShaped2D.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  *
22  * @date 2011/04/12
23  *
24  * Implementation of inline methods defined in StarShaped2D.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 
42 /////////////////////////////////////////////////////////////////////////////
43 // ------------------------- star-shaped services -------------------------
44 
45 /**
46  * @param p any point in the plane.
47  *
48  * @return 'true' if the point is inside the shape, 'false' if it
49  * is strictly outside.
50  */
51 template<typename TSpace>
52 inline
53 DGtal::Orientation
54 DGtal::StarShaped2D<TSpace>::orientation( const RealPoint & p ) const
55 {
56  RealPoint x_rel = x( parameter( p ));
57  x_rel -= center();
58  RealPoint p_rel( p );
59  p_rel -= center();
60 
61  double d_x = x_rel.norm();
62  double d_p = p_rel.norm();
63  double diff = d_p - d_x;
64 
65  if ( diff > 0.0 )
66  return OUTSIDE;
67  else if ( diff < 0.0 )
68  return INSIDE;
69  else
70  return ON;
71 }
72 
73 
74 /**
75  * @param t any angle between 0 and 2*Pi.
76  *
77  * @return the vector (x'(t),y'(t)) made unitary which is the unit
78  * tangent to the shape boundary.
79  */
80 template<typename TSpace>
81 inline
82 typename DGtal::StarShaped2D<TSpace>::RealPoint
83 DGtal::StarShaped2D<TSpace>::tangent( double t ) const
84 {
85  RealPoint tgt( xp( t ) );
86  double norm = tgt.norm();
87  tgt /= norm;
88  return tgt;
89 }
90 
91 
92 /**
93  * @param t any angle between 0 and 2*Pi.
94  *
95  * @return the vector (x''(t),y''(t)) made unitary which is the unit
96  * normal to the shape boundary looking inside the shape.
97 b */
98 template<typename TSpace>
99 inline
100 typename DGtal::StarShaped2D<TSpace>::RealPoint
101 DGtal::StarShaped2D<TSpace>::normal( double t ) const
102 {
103  RealPoint tgt( tangent( t ) );
104  return RealPoint( -tgt[1], tgt[0]);
105 }
106 
107 
108 /**
109  * @param t any angle between 0 and 2*Pi.
110  *
111  * @return the algebraic curvature at point (x(t),y(t)), positive
112  * is convex, negative is concave when shape is to the left and
113  * the shape boundary is followed counterclockwise.
114  */
115 template<typename TSpace>
116 inline
117 double
118 DGtal::StarShaped2D<TSpace>::curvature( double t ) const
119 {
120  RealPoint tgt( xp( t ) );
121  RealPoint dt( xpp( t ) );
122  double norm = tgt.norm();
123  double curv = ( dt[0] * tgt[1] - dt[1] * tgt[0] ) / ( norm * norm * norm );
124  return - curv;
125 }
126 
127 
128 /**
129  * @param t1 any angle between 0 and 2*Pi.
130  * @param t2 any angle between 0 and 2*Pi, further from [t1].
131  * @param nb the number of points used to estimate the arclength between x(t1) and x(t2).
132  * @return the estimated arclength.
133  */
134 template<typename TSpace>
135 inline
136 double
137 DGtal::StarShaped2D<TSpace>::arclength( double t1, double t2, unsigned int nb ) const
138 {
139  while ( t2 < t1 ) t2 += 2.0*M_PI;
140 
141  RealPoint x0( x( t1 ) );
142  double l = 0.0;
143  // JOL 2008/08/28
144  for ( unsigned int i = 1; i <= nb; ++i )
145  {
146  double t = ( ( t2 - t1 ) * i ) / nb;
147  RealPoint x1( x( t1 + t ) );
148  l += sqrt( ( x1[0] - x0[0] )*( x1[0] - x0[0] )
149  + ( x1[1] - x0[1] )*( x1[1] - x0[1] ) );
150  x0 = x1;
151  }
152  return l;
153 }
154 
155 
156 
157 /**
158  * Destructor.
159  */
160 template <typename T>
161 inline
162 DGtal::StarShaped2D<T>::~StarShaped2D()
163 {
164 }
165 
166 ///////////////////////////////////////////////////////////////////////////////
167 // Interface - public :
168 
169 /**
170  * Writes/Displays the object on an output stream.
171  * @param out the output stream where the object is written.
172  */
173 template <typename T>
174 inline
175 void
176 DGtal::StarShaped2D<T>::selfDisplay ( std::ostream & out ) const
177 {
178  out << "[StarShaped2D]";
179 }
180 
181 /**
182  * Checks the validity/consistency of the object.
183  * @return 'true' if the object is valid, 'false' otherwise.
184  */
185 template <typename T>
186 inline
187 bool
188 DGtal::StarShaped2D<T>::isValid() const
189 {
190  return true;
191 }
192 
193 
194 
195 ///////////////////////////////////////////////////////////////////////////////
196 // Implementation of inline functions //
197 
198 template <typename T>
199 inline
200 std::ostream&
201 DGtal::operator<< ( std::ostream & out,
202  const StarShaped2D<T> & object )
203 {
204  object.selfDisplay( out );
205  return out;
206 }
207 
208 // //
209 ///////////////////////////////////////////////////////////////////////////////
210 
211