mt
 All Classes Files Functions Enumerations Groups Pages
sphere3.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Adolfo Rodriguez *
3  * adolfo.rodriguez@upc.edu *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
22 
23 // HEADER GUARD
24 #ifndef MT_SPHERE3_H
25 #define MT_SPHERE3_H
26 
27 // C++ STANDARD HEADERS
28 #include <iostream>
29 #include <limits>
30 
31 // MT LIBRARY HEADERS
32 #include <mt/exception.h>
33 #include <mt/point3.h>
34 #include <mt/scalar.h>
35 #include <mt/vector3.h>
36 
37 
39 
40 namespace mt
41 {
42 
43 
45 
48 
57 
58 class Sphere3
59 {
60 public:
61 
62 // LIFECYCLE
63 
65  Sphere3();
66 
70  Sphere3(const Point3& center, const Scalar& radius);
71 
72  // Compiler generated copy constructor is being used
73 
74  // Compiler generated destructor is being used
75 
76 
77 // OPERATORS
78 
79  // Compiler generated assignment operator is being used
80 
81  bool operator==(const Sphere3& s) const;
82  bool operator!=(const Sphere3& s) const;
83 
84 // OPERATIONS
85 
87  Point3 project(const Point3& p) const;
88 
94  Scalar distance(const Point3& p) const;
95 
96 // ACCESS
97 
98  Point3 getCenter() const;
99  Point3& getCenterRef();
100  const Point3& getCenterRef() const;
101 
102  Scalar getRadius() const;
103  Scalar& getRadiusRef();
104  const Scalar& getRadiusRef() const;
105 
106  void setCenter(const Point3& center);
107 
111  void setRadius(const Scalar& radius);
112 
116  void setValue(const Point3& center, const Scalar& radius);
117 
118 
119 // INQUIRY
120 
122  bool isSingular() const;
123 
124 
125 private:
126 
127 // MEMBERS
128 
130 Point3 m_center;
131 
133 Scalar m_radius;
134 
135 };
136 
137 
139 
140 // OPERATORS
141 
142 std::ostream& operator<<(std::ostream& os,
143  const Sphere3& s);
144 
145 
146 // FUNCTIONS
147 
149 Point3 project(const Point3& p,
150  const Sphere3& s);
151 
153 Scalar distance(const Point3& p,
154  const Sphere3& s);
155 
157 Scalar distance(const Sphere3& s,
158  const Point3& p);
159 
160 
162 
163 // LIFECYCLE
164 
166 
167  m_center(0.0, 0.0, 0.0),
168  m_radius(1.0) {}
169 
170 inline Sphere3::Sphere3(const Point3& center, const Scalar& radius) :
171 
172  m_center(center),
173  m_radius(abs(radius)) {}
174 
175 
176 // OPERATORS
177 
178 inline bool Sphere3::operator==(const Sphere3& s) const
179 {
180  return (m_center == s.m_center) && (m_radius == s.m_radius);
181 }
182 
183 
184 inline bool Sphere3::operator!=(const Sphere3& s) const
185 {
186  return !(*this == s);
187 }
188 
189 
190 // OPERATIONS
191 
192 inline Point3 Sphere3::project(const Point3& p) const
193 {
194  // If the point to be projected coincides with sphere center, its value is
195  // perturbed to obtain a particular projection point.
196 
197  Point3 p_temp = p;
198  const value_t ep = std::numeric_limits<value_t>::epsilon();
199  if (getValue(m_center.distance(p)) < ep)
200  {
201  // Perturbation vector (its direction is perpendicular to circle normal)
202  const Vector3 pert = Scalar(1000.0) * ep * Unit3(1.0, 0.0, 0.0);
203  p_temp += pert;
204  }
205 
206  if (m_radius == Scalar(0.0))
207  {
208  // Singular sphere
209  return m_center;
210  }
211  else
212  {
213  // Non-singular sphere
214  const Unit3 dist_center(p_temp - m_center);
215  return m_center + m_radius * dist_center;
216  }
217 }
218 
219 
220 inline Scalar Sphere3::distance(const Point3& p) const
221 {
222  const Scalar dist_center(length(p - m_center));
223  return dist_center - m_radius;
224 }
225 
226 
227 // ACCESS
228 
229 inline Point3 Sphere3::getCenter() const
230 {
231  return m_center;
232 }
233 
234 
235 inline Point3& Sphere3::getCenterRef()
236 {
237  return m_center;
238 }
239 
240 
241 inline const Point3& Sphere3::getCenterRef() const
242 {
243  return m_center;
244 }
245 
246 
247 inline Scalar Sphere3::getRadius() const
248 {
249  return m_radius;
250 }
251 
252 
253 inline Scalar& Sphere3::getRadiusRef()
254 {
255  return m_radius;
256 }
257 
258 
259 inline const Scalar& Sphere3::getRadiusRef() const
260 {
261  return m_radius;
262 }
263 
264 
265 inline void Sphere3::setCenter(const Point3& center)
266 {
267  m_center = center;
268 }
269 
270 
271 inline void Sphere3::setRadius(const Scalar& radius)
272 {
273  m_radius = abs(radius);
274 }
275 
276 
277 inline void Sphere3::setValue(const Point3& center, const Scalar& radius)
278 {
279  m_center = center;
280  m_radius = abs(radius);
281 }
282 
283 
284 // INQUIRY
285 
286 inline bool Sphere3::isSingular() const
287 {
288  return m_radius == Scalar(0.0);
289 }
290 
291 
293 
294 // OPERATORS
295 
296 inline std::ostream& operator<<(std::ostream& os,
297  const Sphere3& s)
298 {
299  return os << "center: " << s.getCenter() << ' '
300  << "radius: " << s.getRadius();
301 }
302 
303 
304 // FUNCTIONS
305 
306 inline Point3 project(const Point3& p,
307  const Sphere3& s)
308 {
309  return s.project(p);
310 }
311 
312 
313 inline Scalar distance(const Point3& p,
314  const Sphere3& s)
315 {
316  return s.distance(p);
317 }
318 
319 
320 inline Scalar distance(const Sphere3& s,
321  const Point3& p)
322 {
323  return s.distance(p);
324 }
325 
326 } // mt
327 
328 #endif // MT_SPHERE3_H