IMP  2.3.1
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  *
4  * Copyright 2007-2014 IMP Inventors. All rights reserved.
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
29  radius_ = std::numeric_limits<double>::quiet_NaN();
30 #endif
31  }
32  SphereD(const VectorD<D> &center, double radius)
33  : center_(center), radius_(radius) {
34  IMP_USAGE_CHECK(radius >= 0, "Radius can't be negative");
35  }
36  double get_radius() const {
38  "Attempt to use uninitialized sphere.");
39  return radius_;
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();
45  return (d + o.get_radius() < get_radius());
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_; }
57  void _set_radius(double d) { radius_ = d; }
58  double &operator[](unsigned int i) {
59  IMP_USAGE_CHECK(i < D + 1, "Out of range");
60  if (i < D) {
61  return center_[i];
62  } else {
63  return radius_;
64  }
65  }
66  double operator[](unsigned int i) const {
67  IMP_USAGE_CHECK(i < D + 1, "Out of range");
68  if (i < D) {
69  return center_[i];
70  } else {
71  return radius_;
72  }
73  }
74 #endif
75 #endif
76  unsigned int get_dimension() const { return center_.get_dimension(); }
77 
78  private:
79  VectorD<D> center_;
80  double radius_;
81 };
82 
83 IMP_VOLUME_GEOMETRY_METHODS_D(Sphere, sphere,
84 { return PI * 4.0 * get_squared(g.get_radius()); },
85 { return PI * (4.0 / 3.0) * std::pow(g.get_radius(), 3.0); },
86  return BoundingBoxD<D>(g.get_center()) +
87  g.get_radius(););
88 
89 template <unsigned int D>
90 inline SphereD<D> get_unit_sphere_d() {
91  return SphereD<D>(get_zero_vector_d<D>(), 1.0);
92 }
93 
94 inline SphereD<-1> get_unit_sphere_kd(unsigned int d) {
95  return SphereD<-1>(get_zero_vector_kd(d), 1.0);
96 }
97 
98 //! Return the distance between the two spheres if they are disjoint
99 /** If they intersect, the distances are not meaningful.
100  \see SphereD
101 */
102 template <int D>
103 inline double get_distance(const SphereD<D> &a, const SphereD<D> &b) {
104  double d = (a.get_center() - b.get_center()).get_magnitude();
105  return d - a.get_radius() - b.get_radius();
106 }
107 
108 //! Return the power distance between the two spheres
109 /** The power distance is the square of the distance between the centers
110  minus the sum of the square of the radii.
111  \see SphereD
112 */
113 template <int D>
114 inline double get_power_distance(const SphereD<D> &a, const SphereD<D> &b) {
115  double d = (a.get_center() - b.get_center()).get_squared_magnitude();
116  return d - square(a.get_radius()) - square(b.get_radius());
117 }
118 
119 //! Return true if the two balls bounded by the two spheres intersect
120 /** \see SphereD
121  */
122 template <int D>
123 inline bool get_interiors_intersect(const SphereD<D> &a, const SphereD<D> &b) {
124  double sr = a.get_radius() + b.get_radius();
125  for (unsigned int i = 0; i < 3; ++i) {
126  double delta = std::abs(a.get_center()[i] - b.get_center()[i]);
127  if (delta >= sr) return false;
128  }
129  return get_squared_distance(a.get_center(), b.get_center()) < get_squared(sr);
130 }
131 
132 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
133 
134 namespace internal {
135 template <int D>
136 struct SphereSpacesIO {
137  const SphereD<D> &v_;
138  SphereSpacesIO(const SphereD<D> &v) : v_(v) {}
139 };
140 template <int D>
141 inline std::ostream &operator<<(std::ostream &out, const SphereSpacesIO<D> &s) {
142  for (unsigned int i = 0; i < s.v_.get_center().get_dimension(); ++i) {
143  out << s.v_.get_center()[i] << " ";
144  }
145  out << s.v_.get_radius();
146  return out;
147 }
148 }
149 
150 //! Use this before outputting to a stream with spaces delimiting
151 /** std::cout << spaces_io(s);
152  produces "1.0 2.0 3.0 4.0" where the radius is 4.0
153  \see SphereD
154  */
155 template <int D>
156 inline internal::SphereSpacesIO<D> spaces_io(const SphereD<D> &v) {
157  return internal::SphereSpacesIO<D>(v);
158 }
159 #endif
160 
161 #ifdef IMP_DOXYGEN
162 //! Compute the bounding box of any geometric object
163 template <class Geometry>
164 BoundingBoxD<3> get_bounding_box(const Geometry &);
165 //! Compute the surface area of any volumetric object
166 template <class Geometry>
167 double get_surface_area(const Geometry &);
168 //! Compute the volume of any volumetric object
169 template <class Geometry>
170 double get_volume(const Geometry &);
171 //! Compute the area of any surface object
172 template <class Geometry>
173 double get_area(const Geometry &);
174 
175 #endif
176 template <int D>
177 VectorD<D> get_vector_geometry(const SphereD<D> &s) {
178  return s.get_center();
179 }
180 
181 IMPALGEBRA_END_NAMESPACE
182 
183 #endif /* IMPALGEBRA_SPHERE_D_H */
Basic types used by IMP.
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:141
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:123
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:86
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:103
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:170
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 important macros for implementing geometry.
double get_power_distance(const SphereD< D > &a, const SphereD< D > &b)
Return the power distance between the two spheres.
Definition: SphereD.h:114