1 // This is bbas/vsph/vsph_sph_point_2d.cxx
2 #include "vsph_sph_point_2d.h"
3 #include "vnl/vnl_math.h"
4 #include <cassert>
5 #ifdef _MSC_VER
6 #  include "vcl_msvc_warnings.h"
7 #endif
8 
vsph_sph_point_2d(double theta,double phi,bool in_radians)9 vsph_sph_point_2d::vsph_sph_point_2d(double theta, double phi, bool in_radians)
10   : in_radians_(in_radians), theta_(theta),phi_(phi){
11   double pye = in_radians?vnl_math::pi:180.0;
12   double two_pye = 2.0*pye;
13   assert(theta>=0.0);
14   assert(theta<=pye);
15   if (phi<-pye) phi_ = phi + two_pye;
16   if (phi> pye) phi_ = phi - two_pye;
17 }
18 
print(std::ostream & os) const19 void vsph_sph_point_2d::print(std::ostream& os) const
20 {
21   os << " vsph_sph_point_2d(";
22   if (in_radians_) os << "rad"; else os << "deg";
23   os << "):[theta=" << theta_ << ",phi=" << phi_ << "] ";
24 }
25 
operator ==(const vsph_sph_point_2d & other) const26 bool vsph_sph_point_2d::operator==(const vsph_sph_point_2d &other) const
27 {
28   double tol = 0.0000001; //close enough for angles in radians
29   double th = theta_, ph = phi_;
30   if (!in_radians_) {
31     th /= vnl_math::deg_per_rad;
32     ph /= vnl_math::deg_per_rad;
33   }
34   double oth = other.theta_, oph = other.phi_;
35   if (!other.in_radians_) {
36     oth /= vnl_math::deg_per_rad;
37     oph /= vnl_math::deg_per_rad;
38   }
39   double er = std::fabs(oth-th) + std::fabs(oph - ph);
40   return er <= tol;
41 }
42 
b_read(vsl_b_istream & is)43 void vsph_sph_point_2d::b_read(vsl_b_istream& is)
44 {
45   short version;
46   vsl_b_read(is, version);
47   switch (version) {
48     case 1:
49       vsl_b_read(is,in_radians_);
50       vsl_b_read(is,theta_);
51       vsl_b_read(is,phi_);
52   }
53 }
54 
b_write(vsl_b_ostream & os) const55 void vsph_sph_point_2d::b_write(vsl_b_ostream& os) const
56 {
57   vsl_b_write(os, version());
58   vsl_b_write(os, in_radians_);
59   vsl_b_write(os, theta_);
60   vsl_b_write(os,phi_);
61 }
62 
vsl_b_read(vsl_b_istream & is,vsph_sph_point_2d & sp)63 void vsl_b_read(vsl_b_istream& is, vsph_sph_point_2d& sp)
64 {
65   sp.b_read(is);
66 }
67 
vsl_b_write(vsl_b_ostream & os,vsph_sph_point_2d const & sp)68 void vsl_b_write(vsl_b_ostream& os, vsph_sph_point_2d const& sp)
69 {
70   sp.b_write(os);
71 }
72 
vsl_print_summary(std::ostream & os,vsph_sph_point_2d const & sp)73 void vsl_print_summary(std::ostream& os, vsph_sph_point_2d const& sp)
74 {
75   sp.print(os);
76 }
77 
operator <<(std::ostream & os,vsph_sph_point_2d const & sp)78 std::ostream& operator<<(std::ostream& os, vsph_sph_point_2d const& sp)
79 {
80   sp.print(os);
81   return os;
82 }
83