DGtal  1.0.0
Ball3D.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 #pragma once
18 
19 /**
20  * @file Ball3D.ih
21  * @author Anis Benyoub (\c anis.benyoub@liris.cnrs.fr )
22  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
23  *
24  * @date 2012/06/05
25  *
26  * Header file for module Ball3D.cpp
27  *
28  * This file is part of the DGtal library.
29  */
30 
31 
32 //////////////////////////////////////////////////////////////////////////////
33 #include <cstdlib>
34 #include <algorithm>
35 
36 
37 //////////////////////////////////////////////////////////////////////////////
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // IMPLEMENTATION of inline methods.
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 // ----------------------- Standard services ------------------------------
45 typedef std::pair<double,double> AngularCoordinates;
46 /**
47  * Destructor.
48  */
49 template <typename T>
50 inline
51 DGtal::Ball3D<T>::~Ball3D()
52 {
53 }
54 
55 template <typename T>
56 inline
57 DGtal::Ball3D<T>::Ball3D(const double x0, const double y0, const double z0, const double radius):
58  myRadius(radius), myCenter(x0,y0,z0)
59 {}
60 
61 
62 template <typename T>
63 inline
64 DGtal::Ball3D<T>::Ball3D(const RealPoint &aPoint, const double radius):
65  myRadius(radius), myCenter(aPoint)
66 {}
67 
68 
69 /////////////////////////////////////////////////////////////////////////////
70 // ------------- Implementation of 'StarShaped' services ------------------
71 
72 /**
73  * @param p any point in the plane.
74  *
75  * @return the couple of angle parameters (Teta,Phi) respectivly between [0,2PI] and [0,Pi] corresponding to
76  * this point for the shape.
77  */
78 template <typename T>
79 inline
80 AngularCoordinates
81 DGtal::Ball3D<T>::parameter( const RealPoint & pp ) const
82 {
83  RealPoint p( pp );
84  p -= myCenter;
85  AngularCoordinates angle;
86 
87  angle.first = atan2( p[1], p[0] );
88  double tmp = std::min ( std::abs( p[2] ), 1.0 );
89  double pn = p.norm();
90  angle.second = pn > 0.0 ? acos( tmp / pn ) : 0.0;
91 
92  angle.first = ( angle.first < 0.0 ) ? angle.first + 2*M_PI : angle.first;
93  angle.second = ( angle.second < 0.0 ) ? angle.second + M_PI : angle.second;
94 
95  return angle;
96 }
97 
98 /**
99  * @param t any angle between 0 and 2*Pi.
100  *
101  * @return the vector (x(t),y(t),z(t)) which is the position on the
102  * shape boundary.
103  */
104 template <typename T>
105 inline
106 typename DGtal::Ball3D<T>::RealPoint
107 DGtal::Ball3D<T>::x( AngularCoordinates t ) const
108 {
109  RealPoint c( myRadius*cos(t.first)*sin(t.second), myRadius*sin(t.first)*sin(t.second) , myRadius*cos(t.second));
110  c += myCenter;
111  return c;
112 }
113 
114 
115 
116 /**
117  * @param t is a couple of Teta && Phi wich are angles respectivly betweend [0,2PI] and [0,Pi].
118  *
119  * @return the vector (gradf(M)).
120  */
121 template <typename T>
122 inline
123 typename DGtal::Ball3D<T>::RealPoint
124 DGtal::Ball3D<T>::gradient( const AngularCoordinates t) const
125 {
126  RealPoint p= x(t);
127  double xx=2*(p[0]-myCenter[0]);
128  double yy =2*(p[1]-myCenter[1]);
129  double zz=2*(p[2]-myCenter[2]);
130  return RealPoint(xx,yy,zz);
131 }
132 
133 /**
134  * @param t is a couple of Teta && Phi wich are angles respectivly betweend [0,2PI] and [0,Pi].
135  *
136  * @return the vector (rt(M)) wich is the first partial derivative with respect to Teta.
137  */
138 template <typename T>
139 inline
140 typename DGtal::Ball3D<T>::RealPoint
141 DGtal::Ball3D<T>::rt( const AngularCoordinates t) const
142 {
143  return RealPoint(-myRadius*sin(t.first)*sin(t.second),myRadius*cos(t.first)*sin(t.second),0);
144 }
145 
146 
147 
148 /**
149  * @param t is a couple of Teta && Phi wich are angles respectivly betweend [-Pi/2,Pi/2) and [-Pi,Pi].
150  *
151  * @return the vector (rp(M)) wich is the first partial derivative with respect to Phi.
152  */
153 template <typename T>
154 inline
155 typename DGtal::Ball3D<T>::RealPoint
156 DGtal::Ball3D<T>::rp( const AngularCoordinates t) const
157 {
158  return RealPoint(myRadius*cos(t.first)*cos(t.second),myRadius*sin(t.first)*cos(t.second),-myRadius*sin(t.second));
159 }
160 
161 
162 /**
163  * @param t is a couple of Teta && Phi wich are angles respectivly betweend [0,2PI] and [0,Pi].
164  *
165  * @return the vector (rtt(M)) wich is second the second partial derivative with respect to Teta(twice).
166  */
167 template <typename T>
168 inline
169 typename DGtal::Ball3D<T>::RealPoint
170 DGtal::Ball3D<T>::rtt( const AngularCoordinates t) const
171 {
172  return RealPoint(-myRadius*cos(t.first)*sin(t.second),-myRadius*sin(t.first)*sin(t.second),0);
173 }
174 
175 
176 
177 /**
178  * @param t is a couple of Teta && Phi wich are angles respectivly betweend [0,2PI] and [0,Pi].
179  *
180  * @return the vector (rpp(M)) wich is second the partial derivatif with respect to Phi(twice).
181  */
182 template <typename T>
183 inline
184 typename DGtal::Ball3D<T>::RealPoint
185 DGtal::Ball3D<T>::rpp( const AngularCoordinates t) const
186 {
187  return RealPoint(-myRadius*cos(t.first)*sin(t.second),-myRadius*sin(t.first)*sin(t.second),-myRadius*cos(t.second));
188 }
189 
190 /**
191  * @param t is a couple of Teta && Phi wich are angles respectivly betweend [0,2PI] and [0,Pi]
192  *
193  * @return the vector (rtp(M)) wich is second the partial derivatif with respect to Teta then Phi.
194  */
195 template <typename T>
196 inline
197 typename DGtal::Ball3D<T>::RealPoint
198 DGtal::Ball3D<T>::rtp( const AngularCoordinates t) const
199 {
200  return RealPoint(-myRadius*sin(t.first)*cos(t.second),myRadius*cos(t.first)*cos(t.second),0);
201 }
202 
203 
204 
205 
206 ///////////////////////////////////////////////////////////////////////////////
207 // Interface - public :
208 
209 /**
210  * Writes/Displays the object on an output stream.
211  * @param out the output stream where the object is written.
212  */
213 template <typename T>
214 inline
215 void
216 DGtal::Ball3D<T>::selfDisplay ( std::ostream & out ) const
217 {
218  out << "[Ball3D] center= "<<myCenter<<" radius="<<myRadius;
219 }
220 
221 /**
222  * Checks the validity/consistency of the object.
223  * @return 'true' if the object is valid, 'false' otherwise.
224  */
225 template <typename T>
226 inline
227 bool
228 DGtal::Ball3D<T>::isValid() const
229 {
230  return true;
231 }
232 
233 
234 
235 
236 
237 ///////////////////////////////////////////////////////////////////////////////
238 // Implementation of inline functions //
239 
240 template <typename T>
241 inline
242 std::ostream&
243 DGtal::operator<< ( std::ostream & out,
244  const Ball3D<T> & object )
245 {
246  object.selfDisplay( out );
247  return out;
248 }
249 
250 // //
251 ///////////////////////////////////////////////////////////////////////////////