BoundingBox2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: BoundingBox2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 2-D axis-aligned bounding box class.
6 > Created Time: 2017/03/29
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_BOUNDING_BOX2_H
10 #define CUBBYFLOW_BOUNDING_BOX2_H
11 
13 #include <Core/Ray/Ray2.h>
14 #include <Core/Vector/Vector2.h>
15 
16 #include <limits>
17 
18 namespace CubbyFlow
19 {
25  template <typename T>
27  {
29  bool isIntersecting = false;
30 
32  T near = std::numeric_limits<T>::max();
33 
35  T far = std::numeric_limits<T>::max();
36  };
37 
43  template <typename T>
44  class BoundingBox<T, 2>
45  {
46  public:
49 
52 
54  BoundingBox();
55 
57  BoundingBox(const Vector2<T>& point1, const Vector2<T>& point2);
58 
60  BoundingBox(const BoundingBox& other);
61 
63  T GetWidth() const;
64 
66  T GetHeight() const;
67 
69  T Length(size_t axis);
70 
72  bool Overlaps(const BoundingBox& other) const;
73 
75  bool Contains(const Vector2<T>& point) const;
76 
78  bool Intersects(const Ray2<T>& ray) const;
79 
84  BoundingBoxRayIntersection2<T> ClosestIntersection(const Ray2<T>& ray) const;
85 
87  Vector2<T> MidPoint() const;
88 
90  T DiagonalLength() const;
91 
93  T DiagonalLengthSquared() const;
94 
96  void Reset();
97 
99  void Merge(const Vector2<T>& point);
100 
102  void Merge(const BoundingBox& other);
103 
107  void Expand(T delta);
108 
110  Vector2<T> Corner(size_t idx) const;
111 
113  Vector2<T> Clamp(const Vector2<T>& pt) const;
114 
116  bool IsEmpty() const;
117  };
118 
120  template <typename T> using BoundingBox2 = BoundingBox<T, 2>;
121 
124 
127 
130 
133 }
134 
136 
137 #endif
bool Contains(const VectorType &point) const
Returns true if the input point is inside of this box.
Definition: BoundingBox-Impl.h:54
T far
Distance to the second (and the last) intersection point.
Definition: BoundingBox2.h:35
VectorType MidPoint() const
Returns the mid-point of this box.
Definition: BoundingBox-Impl.h:68
BoundingBox()
Default constructor.
Definition: BoundingBox-Impl.h:17
Generic N-D axis-aligned bounding box class.
Definition: BoundingBox.h:23
Class for 2-D ray.
Definition: Ray2.h:23
void Reset()
Resets this box to initial state (min=infinite, max=-infinite).
Definition: BoundingBox-Impl.h:107
Vector2< T > upperCorner
Upper corner of the bounding box.
Definition: BoundingBox2.h:51
T near
Distance to the first intersection point.
Definition: BoundingBox2.h:32
2-D axis-aligned bounding box class.
Definition: BoundingBox2.h:44
Definition: pybind11Utils.h:24
bool isIntersecting
True if the box and ray intersects.
Definition: BoundingBox2.h:29
T Clamp(T val, T low, T high)
Returns the clamped value.
Definition: MathUtils-Impl.h:123
Vector2< T > lowerCorner
Lower corner of the bounding box.
Definition: BoundingBox2.h:48
2-D box-ray intersection result.
Definition: BoundingBox2.h:26
T DiagonalLength() const
Returns diagonal length of this box.
Definition: BoundingBox-Impl.h:81
void Merge(const VectorType &point)
Merges this and other point.
Definition: BoundingBox-Impl.h:117
2-D vector class.
Definition: Vector2.h:26
bool Overlaps(const BoundingBox &other) const
Returns true of this box and other box overlaps.
Definition: BoundingBox-Impl.h:40
void Expand(T delta)
Definition: BoundingBox-Impl.h:137
T DiagonalLengthSquared() const
Returns squared diagonal length of this box.
Definition: BoundingBox-Impl.h:94