GameKit  0.0.1a
C++ gamedev tools
Box.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Box.hpp
5  *
6  * Description:
7  *
8  * Created: 02/07/2018 03:24:23
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_BOX_HPP_
15 #define GK_BOX_HPP_
16 
17 #include <algorithm>
18 
19 #include "Vector3.hpp"
20 
21 namespace gk {
22 
23 template<typename T>
24 class Box {
25  public:
26  Box() = default;
27 
28  Box(T _x, T _y, T _z, T _width, T _height, T _depth) {
29  reset(_x, _y, _z, _width, _height, _depth);
30  }
31 
32  Box(const Vector3<T> &_position, const Vector3<T> &_size) {
33  reset(_position.x, _position.y, _position.z, _size.x, _size.y, _size.z);
34  }
35 
36  template<typename U>
37  Box(const Box<U> &box)
38  : Box(box.x, box.y, box.width, box.height) {}
39 
40  void reset(T _x, T _y, T _z, T _width, T _height, T _depth) {
41  x = _x;
42  y = _y;
43  z = _z;
44  width = _width;
45  height = _height;
46  depth = _depth;
47  }
48 
49  void reset(Box<T> box) { reset(box.x, box.y, box.z, box.width, box.height, box.depth); }
50 
51  void move(T _x, T _y, T _z) { x += _x; y += _y; z += _z; }
52  void move(Vector3<T> d) { move(d.x, d.y, d.z); }
53 
54  bool intersects(const Box<T> &box) const {
55  T r1MinX = std::min(x, static_cast<T>(x + width));
56  T r1MaxX = std::max(x, static_cast<T>(x + width));
57  T r1MinY = std::min(y, static_cast<T>(y + height));
58  T r1MaxY = std::max(y, static_cast<T>(y + height));
59  T r1MinZ = std::min(z, static_cast<T>(z + depth));
60  T r1MaxZ = std::max(z, static_cast<T>(z + depth));
61 
62  T r2MinX = std::min(box.x, static_cast<T>(box.x + box.width));
63  T r2MaxX = std::max(box.x, static_cast<T>(box.x + box.width));
64  T r2MinY = std::min(box.y, static_cast<T>(box.y + box.height));
65  T r2MaxY = std::max(box.y, static_cast<T>(box.y + box.height));
66  T r2MinZ = std::min(box.z, static_cast<T>(box.z + box.depth));
67  T r2MaxZ = std::max(box.z, static_cast<T>(box.z + box.depth));
68 
69  T interLeft = std::max(r1MinX, r2MinX);
70  T interRight = std::min(r1MaxX, r2MaxX);
71  T interBottom = std::max(r1MinY, r2MinY);
72  T interTop = std::min(r1MaxY, r2MaxY);
73  T interFront = std::max(r1MinZ, r2MinZ);
74  T interBack = std::min(r1MaxZ, r2MaxZ);
75 
76  return interLeft < interRight && interBottom < interTop && interFront < interBack;
77  }
78 
79  Vector3<T> position() const { return {x, y, z}; }
80 
81  void setPosition(Vector3<T> vector3) { x = vector3.x; y = vector3.y; z = vector3.z; }
82 
83  Box operator+(const Vector3<T> &vector3) const { return Box{x + vector3.x, y + vector3.y, z + vector3.z, width, height, depth}; }
84  Box operator-(const Vector3<T> &vector3) const { return Box{x - vector3.x, y - vector3.y, z - vector3.z, width, height, depth}; }
85 
86  Box &operator+=(const Vector3<T> &vector3) { *this = operator+(vector3); return *this; }
87  Box &operator-=(const Vector3<T> &vector3) { *this = operator-(vector3); return *this; }
88 
89  T x = 0;
90  T y = 0;
91  T z = 0;
92 
93  T width = 0;
94  T height = 0;
95  T depth = 0;
96 };
97 
98 using IntBox = Box<int>;
100 
101 }
102 
103 #endif // GK_BOX_HPP_
T height
Definition: Box.hpp:94
T y
Definition: Box.hpp:90
void setPosition(Vector3< T > vector3)
Definition: Box.hpp:81
Box operator-(const Vector3< T > &vector3) const
Definition: Box.hpp:84
void reset(T _x, T _y, T _z, T _width, T _height, T _depth)
Definition: Box.hpp:40
Box operator+(const Vector3< T > &vector3) const
Definition: Box.hpp:83
T depth
Definition: Box.hpp:95
T width
Definition: Box.hpp:93
Box(T _x, T _y, T _z, T _width, T _height, T _depth)
Definition: Box.hpp:28
bool intersects(const Box< T > &box) const
Definition: Box.hpp:54
T z
Definition: Box.hpp:91
Box & operator+=(const Vector3< T > &vector3)
Definition: Box.hpp:86
Box & operator-=(const Vector3< T > &vector3)
Definition: Box.hpp:87
void move(Vector3< T > d)
Definition: Box.hpp:52
void move(T _x, T _y, T _z)
Definition: Box.hpp:51
Vector3< T > position() const
Definition: Box.hpp:79
T x
Definition: Box.hpp:89
Definition: Box.hpp:24
Box(const Vector3< T > &_position, const Vector3< T > &_size)
Definition: Box.hpp:32
Box()=default
void reset(Box< T > box)
Definition: Box.hpp:49
Box(const Box< U > &box)
Definition: Box.hpp:37