IMP Reference Guide  2.7.0 The Integrative Modeling Platform
SphereD.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/SphereD.h \brief Simple 3D sphere class.
3  *
5  *
6  */
7
8 #ifndef IMPALGEBRA_SPHERE_D_H
9 #define IMPALGEBRA_SPHERE_D_H
10
11 #include "algebra_macros.h"
12 #include "constants.h"
13 #include "BoundingBoxD.h"
14 #include "VectorD.h"
15 #include "utility.h"
16 #include "GeometricPrimitiveD.h"
17 #include <cmath>
18
19 IMPALGEBRA_BEGIN_NAMESPACE
20
21 //! Represent a sphere in D-dimensions.
22 /** \geometry
23  */
24 template <int D>
25 class SphereD : public GeometricPrimitiveD<D> {
26  public:
27  SphereD() {
28 #if IMP_HAS_CHECKS >= IMP_USAGE
30 #endif
31  }
32  SphereD(const VectorD<D> &center, double radius)
35  }
38  "Attempt to use uninitialized sphere.");
40  }
41  const VectorD<D> &get_center() const { return center_; }
42  //! Return true if this sphere contains the other one
43  bool get_contains(const SphereD<D> &o) const {
44  double d = (get_center() - o.get_center()).get_magnitude();
46  }
47
48  //! Return true if the point is in or on the surface of the sphere
49  bool get_contains(const VectorD<D> &p) const {
50  return ((p - center_).get_squared_magnitude() <= get_squared(radius_));
51  }
53  { out << "(" << spaces_io(center_) << ": " << get_radius() << ")"; });
54 #ifndef IMP_DOXYGEN
55 #ifndef SWIG
56  VectorD<D> &_access_center() { return center_; }
58  void _set_center(const VectorD<D> &center){ center_ = center; }
59  double &operator[](unsigned int i) {
60  IMP_USAGE_CHECK(i < D + 1, "Out of range");
61  if (i < D) {
62  return center_[i];
63  } else {
65  }
66  }
67  double operator[](unsigned int i) const {
68  IMP_USAGE_CHECK(i < D + 1, "Out of range");
69  if (i < D) {
70  return center_[i];
71  } else {
73  }
74  }
75 #endif
76 #endif
77  unsigned int get_dimension() const { return center_.get_dimension(); }
78
79  private:
80  VectorD<D> center_;
82 };
83
84 IMP_VOLUME_GEOMETRY_METHODS_D(Sphere, sphere,
85 { return PI * 4.0 * get_squared(g.get_radius()); },
86 { return PI * (4.0 / 3.0) * std::pow(g.get_radius(), 3.0); },
87  return BoundingBoxD<D>(g.get_center()) +
89
90 template <unsigned int D>
91 inline SphereD<D> get_unit_sphere_d() {
92  return SphereD<D>(get_zero_vector_d<D>(), 1.0);
93 }
94
95 inline SphereD<-1> get_unit_sphere_kd(unsigned int d) {
96  return SphereD<-1>(get_zero_vector_kd(d), 1.0);
97 }
98
99 //! Return the distance between the two spheres if they are disjoint
100 /** If they intersect, the distances are not meaningful.
101  \see SphereD
102 */
103 template <int D>
104 inline double get_distance(const SphereD<D> &a, const SphereD<D> &b) {
105  double d = (a.get_center() - b.get_center()).get_magnitude();
107 }
108
109 //! Return the power distance between the two spheres
110 /** The power distance is the square of the distance between the centers
111  minus the sum of the square of the radii.
112  \see SphereD
113 */
114 template <int D>
115 inline double get_power_distance(const SphereD<D> &a, const SphereD<D> &b) {
116  double d = (a.get_center() - b.get_center()).get_squared_magnitude();
118 }
119
120 //! Return true if the two balls bounded by the two spheres intersect
121 /** \see SphereD
122  */
123 template <int D>
124 inline bool get_interiors_intersect(const SphereD<D> &a, const SphereD<D> &b) {
126  for (unsigned int i = 0; i < a.get_dimension(); ++i) {
127  double delta = std::abs(a.get_center()[i] - b.get_center()[i]);
128  if (delta >= sr) return false;
129  }
130  return get_squared_distance(a.get_center(), b.get_center()) < get_squared(sr);
131 }
132
133 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
134
135 namespace internal {
136 template <int D>
137 struct SphereSpacesIO {
138  const SphereD<D> &v_;
139  SphereSpacesIO(const SphereD<D> &v) : v_(v) {}
140 };
141 template <int D>
142 inline std::ostream &operator<<(std::ostream &out, const SphereSpacesIO<D> &s) {
143  for (unsigned int i = 0; i < s.v_.get_center().get_dimension(); ++i) {
144  out << s.v_.get_center()[i] << " ";
145  }
147  return out;
148 }
149 }
150
151 //! Use this before outputting to a stream with spaces delimiting
152 /** std::cout << spaces_io(s);
153  produces "1.0 2.0 3.0 4.0" where the radius is 4.0
154  \see SphereD
155  */
156 template <int D>
157 inline internal::SphereSpacesIO<D> spaces_io(const SphereD<D> &v) {
158  return internal::SphereSpacesIO<D>(v);
159 }
160 #endif
161
162 #ifdef IMP_DOXYGEN
163 //! Compute the bounding box of any geometric object
164 template <class Geometry>
165 BoundingBoxD<3> get_bounding_box(const Geometry &);
166 //! Compute the surface area of any volumetric object
167 template <class Geometry>
168 double get_surface_area(const Geometry &);
169 //! Compute the volume of any volumetric object
170 template <class Geometry>
171 double get_volume(const Geometry &);
172 //! Compute the area of any surface object
173 template <class Geometry>
174 double get_area(const Geometry &);
175
176 #endif
177 template <int D>
178 VectorD<D> get_vector_geometry(const SphereD<D> &s) {
179  return s.get_center();
180 }
181
182 IMPALGEBRA_END_NAMESPACE
183
184 #endif /* IMPALGEBRA_SPHERE_D_H */
Base class for geometric types.
bool get_contains(const SphereD< D > &o) const
Return true if this sphere contains the other one.
Definition: SphereD.h:43
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
VectorD< D > get_zero_vector_kd(int Di)
Return a dynamically sized vector of zeros.
Definition: VectorD.h:262
static const double PI
the constant pi
double get_volume(const Cone3D &g)
Definition: Cone3D.h:64
double get_squared_distance(const VectorD< D > &v1, const VectorD< D > &v2)
Compute the squared distance between two vectors.
Definition: VectorD.h:201
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Definition: check_macros.h:139
Base class for geometric types.
Functions to deal with very common math operations.
A Cartesian vector in D-dimensions.
Definition: VectorD.h:52
bool get_interiors_intersect(const SphereD< D > &a, const SphereD< D > &b)
Return true if the two balls bounded by the two spheres intersect.
Definition: SphereD.h:124
BoundingBoxD< 3 > get_bounding_box(const Cone3D &g)
Definition: Cone3D.h:64
#define IMP_VOLUME_GEOMETRY_METHODS_D(Name, name, area, volume, bounding_box)
Implement the needed namespace methods for a geometry type.
A bounding box in D dimensions.
Simple D vector class.
double get_surface_area(const Cone3D &g)
Definition: Cone3D.h:64
double get_area(const Plane3D &g)
Definition: Plane3D.h:78
Various useful constants.
bool isnan(const T &a)
Return true if a number is NaN.
Definition: math.h:24
double get_distance(const SphereD< D > &a, const SphereD< D > &b)
Return the distance between the two spheres if they are disjoint.
Definition: SphereD.h:104
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
bool get_contains(const VectorD< D > &p) const
Return true if the point is in or on the surface of the sphere.
Definition: SphereD.h:49
Represent a sphere in D-dimensions.
Definition: SphereD.h:25
Various helper macros.
double get_power_distance(const SphereD< D > &a, const SphereD< D > &b)
Return the power distance between the two spheres.
Definition: SphereD.h:115