vgl_sphere_3d.h
Go to the documentation of this file.
1 // This is core/vgl/vgl_sphere_3d.h
2 #ifndef vgl_sphere_3d_h
3 #define vgl_sphere_3d_h
4 //:
5 // \file
6 // \brief a sphere in 3D nonhomogeneous space
7 // \author Ian Scott
8 
9 #include <iosfwd>
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13 #include <vgl/vgl_fwd.h> // forward declare vgl_line_3d_2_points
14 #include <vgl/vgl_point_3d.h>
15 
16 //: Represents a cartesian 3D point
17 template <class Type>
18 class vgl_sphere_3d
19 {
20  vgl_point_3d<Type> c_; //!< centre
21  Type r_; //!< radius
22  public:
23 
24  // Constructors/Initializers/Destructor------------------------------------
25 
26  //: Default constructor
27  inline vgl_sphere_3d (): c_(Type(0), Type(0), Type(0)), r_(Type(-1)) {}
28 
29  //: Construct from four scalars: centre and radius.
30  inline vgl_sphere_3d(Type px, Type py, Type pz, Type rad) : c_(px, py, pz), r_(rad) {}
31 
32  //: Construct from a 4-array, representing centre and radius.
33  inline vgl_sphere_3d (const Type v[4]): c_(v[0], v[1], v[2]), r_(v[3]) {}
34 
35  //: Construct from centre point and radius.
36  vgl_sphere_3d (vgl_point_3d<Type> const& cntr, Type rad): c_(cntr), r_(rad) {}
37 
38  //: Test for equality
39  inline bool operator==(const vgl_sphere_3d<Type> &s) const { return this==&s || (c_==s.c_ && r_==s.r_); }
40  //: Test for inequality
41  inline bool operator!=(vgl_sphere_3d<Type>const& s) const { return !operator==(s); }
42 
43  // Data Access-------------------------------------------------------------
44 
45  inline const vgl_point_3d<Type> & centre() const {return c_;}
46  inline Type radius() const {return r_;}
47 
48  //: Return true if this sphere is empty
49  inline bool is_empty() const {
50  return r_ < 0.0;
51  }
52 
53  //: Return true iff the point p is inside (or on) this sphere
54  bool contains(vgl_point_3d<Type> const& p) const;
55 
56  //: Make the sphere empty.
57  void set_empty() {c_.set(0,0,0); r_=-1;}
58 
59  //: Set radius \a r of this sphere (while centre unchanged)
60  inline void set_radius(Type r) { r_=r; }
61  //: Set centre of this sphere to \a c (while radius unchanged)
62  inline void set_centre(const vgl_point_3d<Type> & c) { c_=c; }
63 
64  //: Calculate the end points of a line clipped by this sphere.
65  bool clip(const vgl_line_3d_2_points<Type> & line,
66  vgl_point_3d<Type> &p1, vgl_point_3d<Type> &p2) const;
67 
68 
69  //: convert point on sphere to Cartesian coordinates, angles in radians
70  void spherical_to_cartesian(Type elevation_rad, Type azimuth_rad,
71  Type& x, Type& y, Type& z) const;
72 
73  void spherical_to_cartesian(Type elevation_rad, Type azimuth_rad,
74  vgl_point_3d<Type>& pt) const;
75 
76  //:find elevation and azimuth of closest point on the sphere to x,y,z
77  void cartesian_to_spherical(Type x, Type y, Type z, Type& elevation_rad, Type& azimuth_rad) const;
78  void cartesian_to_spherical(vgl_point_3d<Type> const& pt, Type& elevation_rad, Type& azimuth_rad) const;
79 
80  //: Writes "<vgl_sphere_3d centre=vgl_point_3d<x,y,z> radius=r)>" to stream
81  std::ostream& print(std::ostream& os) const;
82 
83 
84  //: Read from stream, possibly with formatting.
85  // Either just reads 4 blank-separated numbers,
86  // or reads 4 comma-separated numbers,
87  // or reads 4 numbers in parenthesized form "(123, 321, 567, 890)"
88  std::istream& read(std::istream& is);
89 };
90 
91 
92 //: Writes "<vgl_sphere_3d centre=vgl_point_3d<x,y,z> radius=r)>" to stream
93 template <class Type>
94 std::ostream& operator<<(std::ostream& os, const vgl_sphere_3d<Type>& sph);
95 
96 //: Read from stream, possibly with formatting.
97 // Either just reads 4 blank-separated numbers,
98 // or reads 4 comma-separated numbers,
99 // or reads 4 numbers in parenthesized form "(123, 321, 567, 890)"
100 template <class Type>
101 std::istream& operator>>(std::istream& is, vgl_sphere_3d<Type>& sph);
102 
103 
104 #define VGL_SPHERE_3D_INSTANTIATE(T) extern "please include vgl/vgl_sphere_3d.hxx first"
105 
106 #endif // vgl_sphere_3d_h
vgl_sphere_3d(Type px, Type py, Type pz, Type rad)
Construct from four scalars: centre and radius.
Definition: vgl_sphere_3d.h:30
bool operator==(const vgl_sphere_3d< Type > &s) const
Test for equality.
Definition: vgl_sphere_3d.h:39
bool is_empty() const
Return true if this sphere is empty.
Definition: vgl_sphere_3d.h:49
Represents a cartesian 3D point.
Definition: vgl_fwd.h:11
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
Type radius() const
Definition: vgl_sphere_3d.h:46
void set_empty()
Make the sphere empty.
Definition: vgl_sphere_3d.h:57
A class to hold a non-homogeneous representation of a 3D line.
Definition: vgl_fwd.h:17
#define v
Definition: vgl_vector_2d.h:74
vgl_point_3d< Type > c_
centre
Definition: vgl_sphere_3d.h:20
std::ostream & print(std::ostream &os) const
Writes "<vgl_sphere_3d centre=vgl_point_3d<x,y,z> radius=r)>" to stream.
a point in 3D nonhomogeneous space
Represents a cartesian 3D point.
Definition: vgl_fwd.h:29
vgl_sphere_3d(vgl_point_3d< Type > const &cntr, Type rad)
Construct from centre point and radius.
Definition: vgl_sphere_3d.h:36
void cartesian_to_spherical(Type x, Type y, Type z, Type &elevation_rad, Type &azimuth_rad) const
find elevation and azimuth of closest point on the sphere to x,y,z.
std::istream & read(std::istream &is)
Read from stream, possibly with formatting.
void set_radius(Type r)
Set radius r of this sphere (while centre unchanged).
Definition: vgl_sphere_3d.h:60
Type r_
radius
Definition: vgl_sphere_3d.h:21
const vgl_point_3d< Type > & centre() const
Definition: vgl_sphere_3d.h:45
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
bool clip(const vgl_line_3d_2_points< Type > &line, vgl_point_3d< Type > &p1, vgl_point_3d< Type > &p2) const
Calculate the end points of a line clipped by this sphere.
vgl_sphere_3d()
Default constructor.
Definition: vgl_sphere_3d.h:27
bool operator!=(vgl_sphere_3d< Type >const &s) const
Test for inequality.
Definition: vgl_sphere_3d.h:41
void spherical_to_cartesian(Type elevation_rad, Type azimuth_rad, Type &x, Type &y, Type &z) const
convert point on sphere to Cartesian coordinates, angles in radians.
void set_centre(const vgl_point_3d< Type > &c)
Set centre of this sphere to c (while radius unchanged).
Definition: vgl_sphere_3d.h:62
bool contains(vgl_point_3d< Type > const &p) const
Return true iff the point p is inside (or on) this sphere.
vgl_sphere_3d(const Type v[4])
Construct from a 4-array, representing centre and radius.
Definition: vgl_sphere_3d.h:33