IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/21
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-2022 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/exception.h"
14 #include "constants.h"
15 #include <IMP/showable_macros.h>
16 #include "GeometricPrimitiveD.h"
17 #include <cereal/access.hpp>
18 #include <cmath>
19 
20 IMPALGEBRA_BEGIN_NAMESPACE
21 
22 //! Class to represent a 3D point in spherical coordinates
23 /**
24  The order assumed for the representation is radial distance, zenith,
25  and azimuth (r, theta, psi).
26  zenith - angle with axis z
27  azimuth - angle with axis x
28  \geometry
29 */
30 class IMPALGEBRAEXPORT SphericalVector3D : public GeometricPrimitiveD<3> {
31  bool check(double r, double theta, double psi) const;
32 
33  public:
34  SphericalVector3D() {};
35 
36  //! Construct from a Cartesian coordinate vector
37  SphericalVector3D(const Vector3D& v) { set_cartesian_coordinates(v); }
38 
39  //! Construct from provided spherical coordinates.
40  /** A check for the validity of the coords is done by default.
41  Set apply_check to false if you do not want the check.
42  */
43  SphericalVector3D(double r, double theta, double psi,
44  bool apply_check = true) {
45  if (apply_check) {
46  if (!check(r, theta, psi)) {
47  String msg = "SphericalCoords:: wrong SphericalCoords coordinates.";
48  throw ValueException(msg.c_str());
49  }
50  }
51  v_[0] = r;
52  v_[1] = theta;
53  v_[2] = psi;
54  }
55 
56  double operator[](unsigned int i) const {
57  IMP_USAGE_CHECK(i < 3, "Invalid component of SphericalCoords requested: "
58  << i << " of " << 3);
59  return v_[i];
60  }
61 
62  double& operator[](unsigned int i) {
63  IMP_USAGE_CHECK(i < 3, "Invalid component of SphericalCoords requested: "
64  << i << " of " << 3);
65  return v_[i];
66  }
67 
68  //! Returns a vector with the Cartesian coordinates
69  Vector3D get_cartesian_coordinates() const;
70 
71  IMP_SHOWABLE_INLINE(SphericalVector3D, out << v_[0] << " , " << v_[1] << " , "
72  << v_[2]);
73 
74 private:
75  void set_cartesian_coordinates(const Vector3D& v);
76  double v_[3];
77 
78  friend class cereal::access;
79 
80  template<class Archive> void serialize(Archive &ar) {
81  ar(v_[0], v_[1], v_[2]);
82  }
83 };
84 
86 
87 IMPALGEBRA_END_NAMESPACE
88 
89 #endif /* IMPALGEBRA_SPHERICAL_VECTOR_3D_H */
Base class for geometric types.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Exception definitions and assertions.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
SphericalVector3D(const Vector3D &v)
Construct from a Cartesian coordinate vector.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
Base class for geometric types.
SphericalVector3D(double r, double theta, double psi, bool apply_check=true)
Construct from provided spherical coordinates.
Various useful constants.
Class to represent a 3D point in spherical coordinates.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
Simple 3D vector class.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
An exception for an invalid value being passed to IMP.
Definition: exception.h:136
std::string String
Basic string value.
Definition: types.h:43
Macros to help with objects that can be printed to a stream.