1 #ifndef vsph_spherical_coord_h_
2 #define vsph_spherical_coord_h_
3 //:
4 // \file
5 // \brief 3D spherical coordinate system
6 // \author Gamze Tunali
7 //
8 // \verbatim
9 //  Modifications
10 // \endverbatim
11 
12 #include <iostream>
13 #include "vsph_spherical_coord_sptr.h"
14 #include "vsph_sph_point_3d.h"
15 #include <vgl/vgl_point_3d.h>
16 #include <vbl/vbl_ref_count.h>
17 #include <vsl/vsl_binary_io.h>
18 #ifdef _MSC_VER
19 #  include <vcl_msvc_warnings.h>
20 #endif
21 
22 
23 //: 3D coordinate system specified by distance rho, angles theta (elevation) and phi (azimuth).
24 // theta is zero at the North Pole and 180 at the South Pole
25 // phi is zero pointing East, positive values rotating towards North
26 
27 class vsph_spherical_coord : public vbl_ref_count
28 {
29  public:
30   //***************************************************************************
31   // Constructors/Destructor
32   //***************************************************************************
33 
34   // Default constructor
vsph_spherical_coord()35   vsph_spherical_coord() : radius_(1.0), origin_(vgl_point_3d<double>(0,0,0)) {}
36 
37   vsph_spherical_coord(vgl_point_3d<double> origin, double radius = 1.0)
radius_(radius)38   : radius_(radius), origin_(origin) {}
39 
40   //: Copy constructor
vsph_spherical_coord(vsph_spherical_coord const & rhs)41   vsph_spherical_coord(vsph_spherical_coord const& rhs)
42     : vbl_ref_count(), radius_(rhs.radius_), origin_(rhs.origin_) {}
43 
44   // Destructor
45   ~vsph_spherical_coord() override = default;
46 
47   //***************************************************************************
48   // Methods
49   //***************************************************************************
50 
radius()51   double radius() const { return radius_; }
52 
origin()53   vgl_point_3d<double> origin() const { return origin_; }
54 
create_point(double theta,double phi)55   vsph_sph_point_3d create_point(double theta, double phi) const { return vsph_sph_point_3d(radius_,theta,phi); }
56 
57   void spherical_coord(vgl_point_3d<double> cp, vsph_sph_point_3d& sp);
58 
59   //: converts to cartesian coordinates
60   vgl_point_3d<double> cart_coord(vsph_sph_point_3d const& p) const;
61 
62   //: converts to cartesian coordinates
cart_coord(double theta,double phi)63   vgl_point_3d<double> cart_coord(double theta, double phi) const { return cart_coord(vsph_sph_point_3d(radius_,theta,phi)); }
64 
65   //: moves the point onto the surface of the sphere on the ray from origin to the point
66   // Returns true if the point changed, false if it was already on the sphere
67   bool move_point(vsph_sph_point_3d& p);
68 
69   //: Cartesian unit vector along the sphere radius at a point
70   vgl_vector_3d<double> radial_vector(vsph_sph_point_3d const& p);
71 
72   void print(std::ostream& os) const;
73 
74   void b_read(vsl_b_istream& is);
75 
76   void b_write(vsl_b_ostream& os);
77 
version()78   short version() const { return 1; }
79 
80  private:
81   double radius_;                // distance from the origin
82   vgl_point_3d<double> origin_;  // the origin in cartesian coordinates
83 };
84 
85 std::ostream& operator<<(std::ostream& os, vsph_spherical_coord const& p);
86 std::ostream& operator<<(std::ostream& os, vsph_spherical_coord_sptr const& p);
87 
88 #endif // vsph_spherical_coord_h_
89