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