Surface2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Surface2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Abstract base class for 2-D surface.
6 > Created Time: 2017/03/20
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_SURFACE2_H
10 #define CUBBYFLOW_SURFACE2_H
11 
13 #include <Core/Ray/Ray2.h>
15 #include <Core/Vector/Vector2.h>
16 
17 #include <limits>
18 #include <memory>
19 
20 namespace CubbyFlow
21 {
24  {
25  bool isIntersecting = false;
26  double distance = std::numeric_limits<double>::max();
29  };
30 
32  class Surface2
33  {
34  public:
37 
39  bool isNormalFlipped = false;
40 
42  Surface2(
43  const Transform2& transform = Transform2(),
44  bool isNormalFlipped = false);
45 
47  Surface2(const Surface2& other);
48 
50  virtual ~Surface2();
51 
53  Vector2D ClosestPoint(const Vector2D& otherPoint) const;
54 
56  BoundingBox2D BoundingBox() const;
57 
59  bool Intersects(const Ray2D& ray) const;
60 
63  double ClosestDistance(const Vector2D& otherPoint) const;
64 
67 
70  Vector2D ClosestNormal(const Vector2D& otherPoint) const;
71 
73  virtual void UpdateQueryEngine();
74 
75  protected:
78  virtual Vector2D ClosestPointLocal(const Vector2D& otherPoint) const = 0;
79 
81  virtual BoundingBox2D BoundingBoxLocal() const = 0;
82 
84  virtual SurfaceRayIntersection2 ClosestIntersectionLocal(const Ray2D& ray) const = 0;
85 
88  virtual Vector2D ClosestNormalLocal(const Vector2D& otherPoint) const = 0;
89 
92  virtual bool IntersectsLocal(const Ray2D& ray) const;
93 
96  virtual double ClosestDistanceLocal(const Vector2D& otherPoint) const;
97  };
98 
100  using Surface2Ptr = std::shared_ptr<Surface2>;
101 
105  template <typename DerivedBuilder>
107  {
108  public:
110  DerivedBuilder& WithIsNormalFlipped(bool isNormalFlipped);
111 
113  DerivedBuilder& WithTranslation(const Vector2D& translation);
114 
116  DerivedBuilder& WithOrientation(double orientation);
117 
119  DerivedBuilder& WithTransform(const Transform2& transform);
120 
121  protected:
122  bool m_isNormalFlipped = false;
124  };
125 
126  template <typename T>
128  {
129  m_isNormalFlipped = isNormalFlipped;
130  return static_cast<T&>(*this);
131  }
132 
133  template <typename T>
135  {
136  m_transform.SetTranslation(translation);
137  return static_cast<T&>(*this);
138  }
139 
140  template <typename T>
142  {
143  m_transform.SetOrientation(orientation);
144  return static_cast<T&>(*this);
145  }
146 
147  template <typename T>
149  {
150  m_transform = transform;
151  return static_cast<T&>(*this);
152  }
153 }
154 
155 #endif
virtual SurfaceRayIntersection2 ClosestIntersectionLocal(const Ray2D &ray) const =0
Returns the closest intersection point for given ray in local frame.
DerivedBuilder & WithTranslation(const Vector2D &translation)
Returns builder with translation.
Definition: Surface2.h:134
bool m_isNormalFlipped
Definition: Surface2.h:122
virtual BoundingBox2D BoundingBoxLocal() const =0
Returns the bounding box of this surface object in local frame.
DerivedBuilder & WithOrientation(double orientation)
Returns builder with orientation.
Definition: Surface2.h:141
DerivedBuilder & WithTransform(const Transform2 &transform)
Returns builder with transform.
Definition: Surface2.h:148
Base class for 2-D surface builder.
Definition: Surface2.h:106
Class for 2-D ray.
Definition: Ray2.h:23
virtual void UpdateQueryEngine()
Updates internal spatial query engine.
Represents 2-D rigid body transform.
Definition: Transform2.h:21
double ClosestDistance(const Vector2D &otherPoint) const
Transform2 transform
Local-to-world transform.
Definition: Surface2.h:36
virtual Vector2D ClosestPointLocal(const Vector2D &otherPoint) const =0
Structure that represents ray-surface intersection point.
Definition: Surface2.h:23
Abstract base class for 2-D surface.
Definition: Surface2.h:32
bool Intersects(const Ray2D &ray) const
Returns true if the given ray intersects with this surface object.
bool isIntersecting
Definition: Surface2.h:25
2-D axis-aligned bounding box class.
Definition: BoundingBox2.h:44
Vector2D ClosestNormal(const Vector2D &otherPoint) const
bool isNormalFlipped
Flips normal.
Definition: Surface2.h:39
Vector2D ClosestPoint(const Vector2D &otherPoint) const
Returns the closest point from the given point otherPoint to the surface.
Definition: pybind11Utils.h:24
virtual ~Surface2()
Default destructor.
virtual bool IntersectsLocal(const Ray2D &ray) const
Vector2D point
Definition: Surface2.h:27
virtual double ClosestDistanceLocal(const Vector2D &otherPoint) const
Surface2(const Transform2 &transform=Transform2(), bool isNormalFlipped=false)
Constructs a surface with normal direction.
DerivedBuilder & WithIsNormalFlipped(bool isNormalFlipped)
Returns builder with flipped normal flag.
Definition: Surface2.h:127
2-D vector class.
Definition: Vector2.h:26
Vector2D normal
Definition: Surface2.h:28
std::shared_ptr< Surface2 > Surface2Ptr
Shared pointer for the Surface2 type.
Definition: Surface2.h:100
SurfaceRayIntersection2 ClosestIntersection(const Ray2D &ray) const
Returns the closest intersection point for given ray.
Transform2 m_transform
Definition: Surface2.h:123
BoundingBox2D BoundingBox() const
Returns the bounding box of this surface object.
virtual Vector2D ClosestNormalLocal(const Vector2D &otherPoint) const =0
double distance
Definition: Surface2.h:26