Sphere.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_SPHERE_HPP
12 #define CUBBYFLOW_SPHERE_HPP
13 
15 
16 namespace CubbyFlow
17 {
24 template <size_t N>
25 class Sphere final : public Surface<N>
26 {
27  public:
28  class Builder;
29 
31  Sphere(const Transform<N>& _transform = Transform<N>{},
32  bool _isNormalFlipped = false);
33 
35  Sphere(const Vector<double, N>& center, double radius,
36  const Transform<N>& _transform = Transform<N>{},
37  bool _isNormalFlipped = false);
38 
40  ~Sphere() override = default;
41 
43  Sphere(const Sphere& other);
44 
46  Sphere(Sphere&& other) noexcept;
47 
49  Sphere& operator=(const Sphere& other);
50 
52  Sphere& operator=(Sphere&& other) noexcept;
53 
55  static Builder GetBuilder();
56 
59 
61  double radius = 1.0;
62 
63  private:
64  [[nodiscard]] Vector<double, N> ClosestPointLocal(
65  const Vector<double, N>& otherPoint) const override;
66 
67  [[nodiscard]] double ClosestDistanceLocal(
68  const Vector<double, N>& otherPoint) const override;
69 
70  [[nodiscard]] bool IntersectsLocal(
71  const Ray<double, N>& ray) const override;
72 
73  [[nodiscard]] BoundingBox<double, N> BoundingBoxLocal() const override;
74 
75  [[nodiscard]] Vector<double, N> ClosestNormalLocal(
76  const Vector<double, N>& otherPoint) const override;
77 
78  [[nodiscard]] SurfaceRayIntersection<N> ClosestIntersectionLocal(
79  const Ray<double, N>& ray) const override;
80 };
81 
84 
87 
89 using Sphere2Ptr = std::shared_ptr<Sphere2>;
90 
92 using Sphere3Ptr = std::shared_ptr<Sphere3>;
93 
97 template <size_t N>
98 class Sphere<N>::Builder final
99  : public SurfaceBuilderBase<N, typename Sphere<N>::Builder>
100 {
101  public:
103  Builder& WithCenter(const Vector<double, N>& _center);
104 
106  Builder& WithRadius(double _radius);
107 
109  Sphere<N> Build() const;
110 
112  std::shared_ptr<Sphere<N>> MakeShared() const;
113 
114  private:
116  using Base::m_isNormalFlipped;
117  using Base::m_transform;
118 
119  Vector<double, N> m_center;
120  double m_radius = 0.0;
121 };
122 } // namespace CubbyFlow
123 
124 #endif
std::shared_ptr< Sphere3 > Sphere3Ptr
Shared pointer for the Sphere3 type.
Definition: Sphere.hpp:92
Class for N-D ray.
Definition: Ray.hpp:25
std::shared_ptr< Sphere2 > Sphere2Ptr
Shared pointer for the Sphere2 type.
Definition: Sphere.hpp:89
Sphere(const Transform< N > &_transform=Transform< N >{}, bool _isNormalFlipped=false)
Constructs a sphere with center at the origin and radius of 1.
static Builder GetBuilder()
Returns builder fox Sphere.
double radius
Radius of the sphere.
Definition: Sphere.hpp:61
~Sphere() override=default
Default virtual destructor.
Front-end to create Sphere objects step by step.
Definition: Sphere.hpp:98
Represents N-D rigid body transform.
Definition: Transform.hpp:82
Definition: Matrix.hpp:27
Definition: pybind11Utils.hpp:20
Abstract base class for N-D surface.
Definition: Surface.hpp:38
Struct that represents ray-surface intersection point.
Definition: Surface.hpp:25
N-D sphere geometry.
Definition: Sphere.hpp:25
Vector< double, N > center
Center of the sphere.
Definition: Sphere.hpp:58
Base class for N-D surface builder.
Definition: Surface.hpp:153
Sphere & operator=(const Sphere &other)
Copy assignment operator.