SPH
Sphere.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "objects/geometry/Box.h"
9 
11 
12 enum class IntersectResult {
16 };
17 
18 class Sphere {
19 private:
21  Vector p;
22 
24  Float r;
25 
26 public:
28  Sphere() = default;
29 
31  Sphere(const Vector& center, const Float radius)
32  : p(center)
33  , r(radius) {
34  SPH_ASSERT(radius >= 0._f);
35  }
36 
37  INLINE Vector center() const {
38  return p;
39  }
40 
42  return p;
43  }
44 
45  INLINE Float radius() const {
46  return r;
47  }
48 
50  return r;
51  }
52 
53  INLINE Float volume() const {
54  return sphereVolume(r);
55  }
56 
57  INLINE bool contains(const Vector& v) const {
58  return getSqrLength(p - v) < sqr(r);
59  }
60 
61  INLINE Box getBBox() const {
62  return Box(p - Vector(r), p + Vector(r));
63  }
64 
68  INLINE bool intersects(const Sphere& other) const {
69  return getSqrLength(p - other.p) < sqr(r + other.r);
70  }
71 
73  INLINE bool overlaps(const Box& box) const {
74  const Vector leftOf = max(box.lower() - p, Vector(0._f));
75  const Vector rightOf = max(p - box.upper(), Vector(0._f));
76  const Float rSqr = sqr(r);
77  const Float distSqr = rSqr - getSqrLength(leftOf) - getSqrLength(rightOf);
78  return distSqr > 0._f;
79  }
80 
84  SPH_ASSERT(box != Box::EMPTY());
85  if (!this->overlaps(box)) {
87  }
88  const Float rSqr = sqr(r);
89  // either the whole box is inside the sphere, or it intersects the sphere
90  auto vertexInsideSphere = [&](const Vector& v) { return getSqrLength(v - p) < rSqr; };
91  if (!vertexInsideSphere(box.lower())) {
93  }
94  if (!vertexInsideSphere(box.lower() + Vector(box.size()[X], 0._f, 0._f))) {
96  }
97  if (!vertexInsideSphere(box.lower() + Vector(0._f, box.size()[Y], 0._f))) {
99  }
100  if (!vertexInsideSphere(box.lower() + Vector(0._f, 0._f, box.size()[Z]))) {
102  }
103  if (!vertexInsideSphere(box.upper())) {
105  }
106  if (!vertexInsideSphere(box.upper() - Vector(box.size()[X], 0._f, 0._f))) {
108  }
109  if (!vertexInsideSphere(box.upper() - Vector(0._f, box.size()[Y], 0._f))) {
111  }
112  if (!vertexInsideSphere(box.upper() - Vector(0._f, 0._f, box.size()[Z]))) {
114  }
116  }
117 };
118 
119 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Object representing a three-dimensional axis-aligned box.
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
constexpr INLINE Float sphereVolume(const Float radius)
Computes a volume of a sphere given its radius.
Definition: MathUtils.h:375
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
IntersectResult
Definition: Sphere.h:12
@ INTERESECTION
Sphere intersects the box.
@ BOX_INSIDE_SPHERE
Sphere contains the whole box.
@ BOX_OUTSIDE_SPHERE
Sphere has no intersection with the box.
INLINE Float getSqrLength(const Vector &v)
Definition: Vector.h:574
BasicVector< Float > Vector
Definition: Vector.h:539
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
@ Z
Definition: Vector.h:24
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
INLINE const Vector & lower() const
Returns lower bounds of the box.
Definition: Box.h:82
INLINE const Vector & upper() const
Returns upper bounds of the box.
Definition: Box.h:94
INLINE Vector size() const
Returns box dimensions.
Definition: Box.h:106
static Box EMPTY()
Syntactic sugar, returns a default-constructed (empty) box.
Definition: Box.h:41
Definition: Sphere.h:18
INLINE Box getBBox() const
Definition: Sphere.h:61
INLINE Vector & center()
Definition: Sphere.h:41
INLINE bool overlaps(const Box &box) const
Checks whether the sphere partially or fully overlaps given box.
Definition: Sphere.h:73
INLINE IntersectResult intersectsBox(const Box &box) const
Checks the intersection of the sphere with a box.
Definition: Sphere.h:83
INLINE Float & radius()
Definition: Sphere.h:49
INLINE Vector center() const
Definition: Sphere.h:37
INLINE bool contains(const Vector &v) const
Definition: Sphere.h:57
Sphere()=default
Creates an uninitialized sphere.
INLINE Float radius() const
Definition: Sphere.h:45
INLINE Float volume() const
Definition: Sphere.h:53
Sphere(const Vector &center, const Float radius)
Creates a sphere given its center and radius.
Definition: Sphere.h:31
INLINE bool intersects(const Sphere &other) const
Checks if the sphere intersects another sphere.
Definition: Sphere.h:68