IMP  2.0.0
The Integrative Modeling Platform
SphericalVector3D.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/SphericalVector3D.h
3  * \brief Stores and converts spherical coordinates
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPALGEBRA_SPHERICAL_VECTOR_3D_H
9 #define IMPALGEBRA_SPHERICAL_VECTOR_3D_H
10 
11 #include <IMP/algebra/algebra_config.h>
12 #include "Vector3D.h"
13 #include "IMP/base/exception.h"
14 #include "constants.h"
16 #include "GeometricPrimitiveD.h"
17 #include <cmath>
18 
19 IMPALGEBRA_BEGIN_NAMESPACE
20 
21 //! Class to represent a 3D point in spherical coordinates
22 /**
23  The order assumed for the representation is radial distance, zenith,
24  and azimuth (r, theta, psi).
25  zenith - angle with axis z
26  azimuth - angle with axis x
27  \geometry
28 */
29 class IMPALGEBRAEXPORT SphericalVector3D: public GeometricPrimitiveD<3>
30 {
31  bool check(double r, double theta, double psi);
32  public:
33  SphericalVector3D() {};
34 
35  //! Constructor that directly converts to spherical coordinates from a vector
36  //! v in Cartesian coordinates
38  set_cartesian_coordinates(v);
39  }
40 
41  //! Direct Constructor. A check for the validity of the coords is done
42  //! by default
43  /**
44  Set apply_check to false if you do not want the check
45  **/
46  SphericalVector3D(double r, double theta,
47  double psi,bool apply_check=true) {
48  if(apply_check) {
49  if(!check(r,theta,psi)) {
50  String msg = "SphericalCoords:: wrong SphericalCoords coordinates." ;
51  throw base::ValueException(msg.c_str());
52  }
53  }
54  v_[0] = r;
55  v_[1] = theta;
56  v_[2] = psi;
57  }
58 
59  double operator[](unsigned int i) const {
60  IMP_USAGE_CHECK(i < 3, "Invalid component of SphericalCoords requested: "
61  << i << " of " << 3);
62  return v_[i];
63  }
64 
65  double& operator[](unsigned int i) {
66  IMP_USAGE_CHECK(i < 3, "Invalid component of SphericalCoords requested: "
67  << i << " of " << 3);
68  return v_[i];
69  }
70 
71  //! Returns a vector with the Cartesian coordinates
72  Vector3D get_cartesian_coordinates();
73 
74  IMP_SHOWABLE_INLINE(SphericalVector3D,
75  out << v_[0] << " , " << v_[1] << " , "
76  << v_[2]);
77 
78  private:
79  void set_cartesian_coordinates(const Vector3D& v);
80  double v_[3];
81 };
82 
84 
85 IMPALGEBRA_END_NAMESPACE
86 
87 #endif /* IMPALGEBRA_SPHERICAL_VECTOR_3D_H */