IMP logo
IMP Reference Guide  2.12.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  *
4  * Copyright 2007-2019 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 #ifdef IMP_SWIG_WRAPPER
55  static void _get_struct_size(size_t &sz, size_t &center_offset,
56  size_t &radius_offset) {
57  sz = sizeof(SphereD<D>);
58  center_offset = offsetof(SphereD<D>, center_);
59  radius_offset = offsetof(SphereD<D>, radius_);
60  }
61 #endif
62 #ifndef IMP_DOXYGEN
63 #ifndef SWIG
64  VectorD<D> &_access_center() { return center_; }
65  void _set_radius(double d) { radius_ = d; }
66  void _set_center(const VectorD<D> &center){ center_ = center; }
67  double &operator[](unsigned int i) {
68  IMP_USAGE_CHECK(i < D + 1, "Out of range");
69  if (i < D) {
70  return center_[i];
71  } else {
72  return radius_;
73  }
74  }
75  double operator[](unsigned int i) const {
76  IMP_USAGE_CHECK(i < D + 1, "Out of range");
77  if (i < D) {
78  return center_[i];
79  } else {
80  return radius_;
81  }
82  }
83 #endif
84 #endif
85  unsigned int get_dimension() const { return center_.get_dimension(); }
86 
87  private:
88  VectorD<D> center_;
89  double radius_;
90 };
91 
92 IMP_VOLUME_GEOMETRY_METHODS_D(Sphere, sphere,
93 { return PI * 4.0 * get_squared(g.get_radius()); },
94 { return PI * (4.0 / 3.0) * std::pow(g.get_radius(), 3.0); },
95  return BoundingBoxD<D>(g.get_center()) +
96  g.get_radius(););
97 
98 template <unsigned int D>
99 inline SphereD<D> get_unit_sphere_d() {
100  return SphereD<D>(get_zero_vector_d<D>(), 1.0);
101 }
102 
103 inline SphereD<-1> get_unit_sphere_kd(unsigned int d) {
104  return SphereD<-1>(get_zero_vector_kd(d), 1.0);
105 }
106 
107 //! Return the distance between the two spheres if they are disjoint
108 /** If they intersect, the distances are not meaningful.
109  \see SphereD
110 */
111 template <int D>
112 inline double get_distance(const SphereD<D> &a, const SphereD<D> &b) {
113  double d = (a.get_center() - b.get_center()).get_magnitude();
114  return d - a.get_radius() - b.get_radius();
115 }
116 
117 //! Return the power distance between the two spheres
118 /** The power distance is the square of the distance between the centers
119  minus the sum of the square of the radii.
120  \see SphereD
121 */
122 template <int D>
123 inline double get_power_distance(const SphereD<D> &a, const SphereD<D> &b) {
124  double d = (a.get_center() - b.get_center()).get_squared_magnitude();
125  return d - square(a.get_radius()) - square(b.get_radius());
126 }
127 
128 //! Return true if the two balls bounded by the two spheres intersect
129 /** \see SphereD
130  */
131 template <int D>
132 inline bool get_interiors_intersect(const SphereD<D> &a, const SphereD<D> &b) {
133  double sr = a.get_radius() + b.get_radius();
134  for (unsigned int i = 0; i < a.get_dimension(); ++i) {
135  double delta = std::abs(a.get_center()[i] - b.get_center()[i]);
136  if (delta >= sr) return false;
137  }
138  return get_squared_distance(a.get_center(), b.get_center()) < get_squared(sr);
139 }
140 
141 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
142 
143 namespace internal {
144 template <int D>
145 struct SphereSpacesIO {
146  const SphereD<D> &v_;
147  SphereSpacesIO(const SphereD<D> &v) : v_(v) {}
148 };
149 template <int D>
150 inline std::ostream &operator<<(std::ostream &out, const SphereSpacesIO<D> &s) {
151  for (unsigned int i = 0; i < s.v_.get_center().get_dimension(); ++i) {
152  out << s.v_.get_center()[i] << " ";
153  }
154  out << s.v_.get_radius();
155  return out;
156 }
157 }
158 
159 //! Use this before outputting to a stream with spaces delimiting
160 /** std::cout << spaces_io(s);
161  produces "1.0 2.0 3.0 4.0" where the radius is 4.0
162  \see SphereD
163  */
164 template <int D>
165 inline internal::SphereSpacesIO<D> spaces_io(const SphereD<D> &v) {
166  return internal::SphereSpacesIO<D>(v);
167 }
168 #endif
169 
170 #ifdef IMP_DOXYGEN
171 //! Compute the bounding box of any geometric object
172 template <class Geometry>
173 BoundingBoxD<3> get_bounding_box(const Geometry &);
174 //! Compute the surface area of any volumetric object
175 template <class Geometry>
176 double get_surface_area(const Geometry &);
177 //! Compute the volume of any volumetric object
178 template <class Geometry>
179 double get_volume(const Geometry &);
180 //! Compute the area of any surface object
181 template <class Geometry>
182 double get_area(const Geometry &);
183 
184 #endif
185 template <int D>
186 VectorD<D> get_vector_geometry(const SphereD<D> &s) {
187  return s.get_center();
188 }
189 
190 IMPALGEBRA_END_NAMESPACE
191 
192 #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:276
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:132
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:112
#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:123