DGtal  1.5.beta
Ball2D.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 Ball2D.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 Ball2D.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 #define BALL2D_2_PI (2. * M_PI)
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // IMPLEMENTATION of inline methods.
38 ///////////////////////////////////////////////////////////////////////////////
39 
40 ///////////////////////////////////////////////////////////////////////////////
41 // ----------------------- Standard services ------------------------------
42 
43 template <typename T>
44 inline
45 DGtal::Ball2D<T>::Ball2D(const double x0, const double y0, const double radius):
46  myRadius(radius), myCenter(x0,y0)
47 {}
48 
49 
50 template <typename T>
51 inline
52 DGtal::Ball2D<T>::Ball2D(const RealPoint &aPoint, const double radius):
53  myRadius(radius), myCenter(aPoint)
54 {}
55 
56 template <typename T>
57 inline
58 DGtal::Ball2D<T>::Ball2D(const Ball2D &other):
59  myCenter(other.myCenter), myRadius(other.myRadius)
60 {}
61 
62 /////////////////////////////////////////////////////////////////////////////
63 // ------------- Implementation of 'StarShaped' services ------------------
64 
65 /**
66  * @param p any point in the plane.
67  *
68  * @return the angle parameter between 0 and 2*Pi corresponding to
69  * this point for the shape.
70  */
71 template <typename T>
72 inline
73 double
74 DGtal::Ball2D<T>::parameter( const RealPoint & p ) const
75 {
76  const double angle = atan2(p[1]-myCenter[1], p[0]-myCenter[0]);
77  return angle < 0. ? angle + BALL2D_2_PI : angle;
78 }
79 
80 /**
81  * @param t any angle between 0 and 2*Pi.
82  *
83  * @return the vector (x(t),y(t)) which is the position on the
84  * shape boundary.
85  */
86 template <typename T>
87 inline
88 typename DGtal::Ball2D<T>::RealPoint
89 DGtal::Ball2D<T>::x( const double t ) const
90 {
91  return RealPoint( myRadius*cos(t)+myCenter[0], myRadius*sin(t)+myCenter[1] );
92 }
93 
94 
95 /**
96  * @param t any angle between 0 and 2*Pi.
97  *
98  * @return the vector (x'(t),y'(t)) which is the tangent to the
99  * shape boundary.
100  */
101 template <typename T>
102 inline
103 typename DGtal::Ball2D<T>::RealVector
104 DGtal::Ball2D<T>::xp( const double t ) const
105 {
106  return RealVector( -myRadius*sin(t), myRadius*cos(t) );
107 }
108 
109 /**
110  * @param t any angle between 0 and 2*Pi.
111  *
112  * @return the vector (x''(t),y''(t)).
113  */
114 template <typename T>
115 inline
116 typename DGtal::Ball2D<T>::RealVector
117 DGtal::Ball2D<T>::xpp( const double t ) const
118 {
119  return RealVector( -myRadius*cos(t), -myRadius*sin(t) );
120 }
121 
122 
123 ///////////////////////////////////////////////////////////////////////////////
124 // Interface - public :
125 
126 /**
127  * Writes/Displays the object on an output stream.
128  * @param out the output stream where the object is written.
129  */
130 template <typename T>
131 inline
132 void
133 DGtal::Ball2D<T>::selfDisplay ( std::ostream & out ) const
134 {
135  out << "[Ball2D] center= " << myCenter
136  << " radius=" << myRadius;
137 }
138 
139 /**
140  * Checks the validity/consistency of the object.
141  * @return 'true' if the object is valid, 'false' otherwise.
142  */
143 template <typename T>
144 inline
145 bool
146 DGtal::Ball2D<T>::isValid() const
147 {
148  return true;
149 }
150 
151 
152 
153 ///////////////////////////////////////////////////////////////////////////////
154 // Implementation of inline functions //
155 
156 template <typename T>
157 inline
158 std::ostream&
159 DGtal::operator<< ( std::ostream & out,
160  const Ball2D<T> & object )
161 {
162  object.selfDisplay( out );
163  return out;
164 }
165 
166 // //
167 ///////////////////////////////////////////////////////////////////////////////
168 
169