IMP logo
IMP Reference Guide  develop.eb1b99edaa,2026/06/22
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-2026 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPALGEBRA_SPHERE_D_H
9 #define IMPALGEBRA_SPHERE_D_H
10 
11 #include "constants.h"
12 #include "BoundingBoxD.h"
13 #include <IMP/algebra/VectorD.h>
14 #include "utility.h"
16 #include <cereal/access.hpp>
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  friend class cereal::access;
27 
28  template<class Archive>
29  void serialize(Archive &ar) {
30  ar(center_, radius_);
31  }
32 
33  public:
34  SphereD() {
35 #if IMP_HAS_CHECKS >= IMP_USAGE
36  radius_ = std::numeric_limits<double>::quiet_NaN();
37 #endif
38  }
39  SphereD(const VectorD<D> &center, double radius)
40  : center_(center), radius_(radius) {
41  IMP_USAGE_CHECK(radius >= 0, "Radius can't be negative");
42  }
43  double get_radius() const {
45  "Attempt to use uninitialized sphere.");
46  return radius_;
47  }
48  const VectorD<D> &get_center() const { return center_; }
49  //! Return true if this sphere contains the other one
50  bool get_contains(const SphereD<D> &o) const {
51  double d = (get_center() - o.get_center()).get_magnitude();
52  return (d + o.get_radius() < get_radius());
53  }
54 
55  //! Return true if the point is in or on the surface of the sphere
56  bool get_contains(const VectorD<D> &p) const {
57  return ((p - center_).get_squared_magnitude() <= get_squared(radius_));
58  }
60  { out << "(" << spaces_io(center_) << ": " << get_radius() << ")"; });
61 #ifdef IMP_SWIG_WRAPPER
62  static void _get_struct_size(size_t &sz, size_t &center_offset,
63  size_t &radius_offset) {
64  sz = sizeof(SphereD<D>);
65  center_offset = offsetof(SphereD<D>, center_);
66  radius_offset = offsetof(SphereD<D>, radius_);
67  }
68 #endif
69 #ifndef IMP_DOXYGEN
70 #ifndef SWIG
71  VectorD<D> &_access_center() { return center_; }
72  void _set_radius(double d) { radius_ = d; }
73  void _set_center(const VectorD<D> &center){ center_ = center; }
74  double &operator[](unsigned int i) {
75  IMP_USAGE_CHECK(i < D + 1, "Out of range");
76  if (i < D) {
77  return center_[i];
78  } else {
79  return radius_;
80  }
81  }
82  double operator[](unsigned int i) const {
83  IMP_USAGE_CHECK(i < D + 1, "Out of range");
84  if (i < D) {
85  return center_[i];
86  } else {
87  return radius_;
88  }
89  }
90 #endif
91 #endif
92  unsigned int get_dimension() const { return center_.get_dimension(); }
93 
94  private:
95  VectorD<D> center_;
96  double radius_;
97 };
98 
99 IMP_VOLUME_GEOMETRY_METHODS_D(Sphere, sphere,
100 { return PI * 4.0 * get_squared(g.get_radius()); },
101 { return PI * (4.0 / 3.0) * std::pow(g.get_radius(), 3.0); },
102  return BoundingBoxD<D>(g.get_center()) +
103  g.get_radius(););
104 
105 template <unsigned int D>
106 inline SphereD<D> get_unit_sphere_d() {
107  return SphereD<D>(get_zero_vector_d<D>(), 1.0);
108 }
109 
110 inline SphereD<-1> get_unit_sphere_kd(unsigned int d) {
111  return SphereD<-1>(get_zero_vector_kd(d), 1.0);
112 }
113 
114 //! Return the distance between the two spheres if they are disjoint
115 /** If they intersect, the distances are not meaningful.
116  \see SphereD
117 */
118 template <int D>
119 inline double get_distance(const SphereD<D> &a, const SphereD<D> &b) {
120  double d = (a.get_center() - b.get_center()).get_magnitude();
121  return d - a.get_radius() - b.get_radius();
122 }
123 
124 //! Return the power distance between the two spheres
125 /** The power distance is the square of the distance between the centers
126  minus the sum of the square of the radii.
127  \see SphereD
128 */
129 template <int D>
130 inline double get_power_distance(const SphereD<D> &a, const SphereD<D> &b) {
131  double d = (a.get_center() - b.get_center()).get_squared_magnitude();
132  return d - square(a.get_radius()) - square(b.get_radius());
133 }
134 
135 //! Return true if the two balls bounded by the two spheres intersect
136 /** \see SphereD
137  */
138 template <int D>
139 inline bool get_interiors_intersect(const SphereD<D> &a, const SphereD<D> &b) {
140  double sr = a.get_radius() + b.get_radius();
141  for (unsigned int i = 0; i < a.get_dimension(); ++i) {
142  double delta = std::abs(a.get_center()[i] - b.get_center()[i]);
143  if (delta >= sr) return false;
144  }
145  return get_squared_distance(a.get_center(), b.get_center()) < get_squared(sr);
146 }
147 
148 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
149 
150 namespace internal {
151 template <int D>
152 struct SphereSpacesIO {
153  const SphereD<D> &v_;
154  SphereSpacesIO(const SphereD<D> &v) : v_(v) {}
155 };
156 template <int D>
157 inline std::ostream &operator<<(std::ostream &out, const SphereSpacesIO<D> &s) {
158  for (unsigned int i = 0; i < s.v_.get_center().get_dimension(); ++i) {
159  out << s.v_.get_center()[i] << " ";
160  }
161  out << s.v_.get_radius();
162  return out;
163 }
164 }
165 
166 //! Use this before outputting to a stream with spaces delimiting
167 /** std::cout << spaces_io(s);
168  produces "1.0 2.0 3.0 4.0" where the radius is 4.0
169  \see SphereD
170  */
171 template <int D>
172 inline internal::SphereSpacesIO<D> spaces_io(const SphereD<D> &v) {
173  return internal::SphereSpacesIO<D>(v);
174 }
175 #endif
176 
177 #ifdef IMP_DOXYGEN
178 //! Compute the bounding box of any geometric object
179 template <class Geometry>
180 BoundingBoxD<3> get_bounding_box(const Geometry &);
181 //! Compute the surface area of any volumetric object
182 template <class Geometry>
183 double get_surface_area(const Geometry &);
184 //! Compute the volume of any volumetric object
185 template <class Geometry>
186 double get_volume(const Geometry &);
187 //! Compute the area of any surface object
188 template <class Geometry>
189 double get_area(const Geometry &);
190 
191 #endif
192 template <int D>
193 VectorD<D> get_vector_geometry(const SphereD<D> &s) {
194  return s.get_center();
195 }
196 
197 IMPALGEBRA_END_NAMESPACE
198 
199 #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:50
A Cartesian vector in D-dimensions.
Definition: VectorD.h:38
#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
double get_volume(const Cone3D &g)
Definition: Cone3D.h:71
#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
Functions to deal with very common math operations.
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:139
BoundingBoxD< 3 > get_bounding_box(const Cone3D &g)
Definition: Cone3D.h:71
#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.
double get_squared_distance(const VectorD< D > &v1, const VectorD< D > &v2)
Compute the squared distance between two vectors.
Definition: VectorD.h:187
Base class for geometric types.
double get_surface_area(const Cone3D &g)
Definition: Cone3D.h:71
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:119
#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:56
Simple D vector class.
Represent a sphere in D-dimensions.
Definition: SphereD.h:25
double get_power_distance(const SphereD< D > &a, const SphereD< D > &b)
Return the power distance between the two spheres.
Definition: SphereD.h:130