Surface.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_SURFACE_HPP
12 #define CUBBYFLOW_SURFACE_HPP
13 
14 #include <Core/Geometry/Ray.hpp>
16 #include <Core/Matrix/Matrix.hpp>
17 
18 #include <limits>
19 #include <memory>
20 
21 namespace CubbyFlow
22 {
24 template <size_t N>
26 {
27  bool isIntersecting = false;
28  double distance = std::numeric_limits<double>::max();
31 };
32 
35 
37 template <size_t N>
38 class Surface
39 {
40  public:
42  Surface(const Transform<N>& transform = Transform<N>(),
43  bool isNormalFlipped = false);
44 
46  virtual ~Surface() = default;
47 
49  Surface(const Surface& other);
50 
52  Surface(Surface&& other) noexcept;
53 
55  Surface& operator=(const Surface& other);
56 
58  Surface& operator=(Surface&& other) noexcept;
59 
62  [[nodiscard]] Vector<double, N> ClosestPoint(
63  const Vector<double, N>& otherPoint) const;
64 
66  [[nodiscard]] BoundingBox<double, N> GetBoundingBox() const;
67 
69  [[nodiscard]] bool Intersects(const Ray<double, N>& ray) const;
70 
73  [[nodiscard]] double ClosestDistance(
74  const Vector<double, N>& otherPoint) const;
75 
77  [[nodiscard]] SurfaceRayIntersection<N> ClosestIntersection(
78  const Ray<double, N>& ray) const;
79 
82  [[nodiscard]] Vector<double, N> ClosestNormal(
83  const Vector<double, N>& otherPoint) const;
84 
86  virtual void UpdateQueryEngine();
87 
89  [[nodiscard]] virtual bool IsBounded() const;
90 
92  [[nodiscard]] virtual bool IsValidGeometry() const;
93 
96  [[nodiscard]] bool IsInside(const Vector<double, N>& otherPoint) const;
97 
100 
102  bool isNormalFlipped = false;
103 
104  protected:
107  [[nodiscard]] virtual Vector<double, N> ClosestPointLocal(
108  const Vector<double, N>& otherPoint) const = 0;
109 
111  [[nodiscard]] virtual BoundingBox<double, N> BoundingBoxLocal() const = 0;
112 
114  [[nodiscard]] virtual SurfaceRayIntersection<N> ClosestIntersectionLocal(
115  const Ray<double, N>& ray) const = 0;
116 
119  [[nodiscard]] virtual Vector<double, N> ClosestNormalLocal(
120  const Vector<double, N>& otherPoint) const = 0;
121 
124  [[nodiscard]] virtual bool IntersectsLocal(const Ray<double, N>& ray) const;
125 
128  [[nodiscard]] virtual double ClosestDistanceLocal(
129  const Vector<double, N>& otherPoint) const;
130 
133  [[nodiscard]] virtual bool IsInsideLocal(
134  const Vector<double, N>& otherPoint) const;
135 };
136 
139 
142 
144 using Surface2Ptr = std::shared_ptr<Surface2>;
145 
147 using Surface3Ptr = std::shared_ptr<Surface3>;
148 
152 template <size_t N, typename DerivedBuilder>
154 {
155  public:
157  DerivedBuilder& WithIsNormalFlipped(bool isNormalFlipped);
158 
160  DerivedBuilder& WithTranslation(const Vector<double, N>& translation);
161 
163  DerivedBuilder& WithOrientation(const Orientation<N>& orientation);
164 
166  DerivedBuilder& WithTransform(const Transform<N>& transform);
167 
168  protected:
169  bool m_isNormalFlipped = false;
171 };
172 
173 template <size_t N, typename T>
175 {
176  m_isNormalFlipped = isNormalFlipped;
177  return static_cast<T&>(*this);
178 }
179 
180 template <size_t N, typename T>
182  const Vector<double, N>& translation)
183 {
184  m_transform.SetTranslation(translation);
185  return static_cast<T&>(*this);
186 }
187 
188 template <size_t N, typename T>
190 {
191  m_transform.SetOrientation(orientation);
192  return static_cast<T&>(*this);
193 }
194 
195 template <size_t N, typename T>
197 {
198  m_transform = transform;
199  return static_cast<T&>(*this);
200 }
201 
202 template <typename T>
204 
205 template <typename T>
207 } // namespace CubbyFlow
208 
209 #endif
Class for N-D ray.
Definition: Ray.hpp:25
DerivedBuilder & WithTranslation(const Vector< double, N > &translation)
Returns builder with translation.
Definition: Surface.hpp:181
Vector< double, N > point
Definition: Surface.hpp:29
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
std::shared_ptr< Surface3 > Surface3Ptr
Shared pointer for the Surface3 type.
Definition: Surface.hpp:147
Transform< N > m_transform
Definition: Surface.hpp:170
Struct that represents ray-surface intersection point.
Definition: Surface.hpp:25
Definition: Transform.hpp:22
DerivedBuilder & WithTransform(const Transform< N > &transform)
Returns builder with transform.
Definition: Surface.hpp:196
Transform< N > transform
Local-to-world transform.
Definition: Surface.hpp:99
double distance
Definition: Surface.hpp:28
std::shared_ptr< Surface2 > Surface2Ptr
Shared pointer for the Surface2 type.
Definition: Surface.hpp:144
Vector< double, N > normal
Definition: Surface.hpp:30
Base class for N-D surface builder.
Definition: Surface.hpp:153
DerivedBuilder & WithOrientation(const Orientation< N > &orientation)
Returns builder with orientation.
Definition: Surface.hpp:189
bool isIntersecting
Definition: Surface.hpp:27
DerivedBuilder & WithIsNormalFlipped(bool isNormalFlipped)
Returns builder with flipped normal flag.
Definition: Surface.hpp:174