/* * ===================================================================================== * * Filename: Box.hpp * * Description: * * Created: 02/07/2018 03:24:23 * * Author: Quentin Bazin, * * ===================================================================================== */ #ifndef BOX_HPP_ #define BOX_HPP_ #include #include "Vector3.hpp" template class Box { public: Box() = default; Box(T _x, T _y, T _z, T _width, T _height, T _depth) { reset(_x, _y, _z, _width, _height, _depth); } Box(const Vector3 &_position, const Vector3 &_size) { reset(_position.x, _position.y, _position.z, _size.x, _size.y, _size.z); } template Box(const Box &box) : Box(box.x, box.y, box.width, box.height) {} void reset(T _x, T _y, T _z, T _width, T _height, T _depth) { x = _x; y = _y; z = _z; width = _width; height = _height; depth = _depth; } void reset(Box box) { reset(box.x, box.y, box.z, box.width, box.height, box.depth); } void move(T _x, T _y, T _z) { x += _x; y += _y; z += _z; } void move(Vector3 d) { move(d.x, d.y, d.z); } bool intersects(const Box &box) const { T r1MinX = std::min(x, static_cast(x + width)); T r1MaxX = std::max(x, static_cast(x + width)); T r1MinY = std::min(y, static_cast(y + height)); T r1MaxY = std::max(y, static_cast(y + height)); T r1MinZ = std::min(z, static_cast(z + depth)); T r1MaxZ = std::max(z, static_cast(z + depth)); T r2MinX = std::min(box.x, static_cast(box.x + box.width)); T r2MaxX = std::max(box.x, static_cast(box.x + box.width)); T r2MinY = std::min(box.y, static_cast(box.y + box.height)); T r2MaxY = std::max(box.y, static_cast(box.y + box.height)); T r2MinZ = std::min(box.z, static_cast(box.z + box.depth)); T r2MaxZ = std::max(box.z, static_cast(box.z + box.depth)); T interLeft = std::max(r1MinX, r2MinX); T interRight = std::min(r1MaxX, r2MaxX); T interBottom = std::max(r1MinY, r2MinY); T interTop = std::min(r1MaxY, r2MaxY); T interFront = std::max(r1MinZ, r2MinZ); T interBack = std::min(r1MaxZ, r2MaxZ); return interLeft < interRight && interBottom < interTop && interFront < interBack; } Vector3 position() const { return {x, y, z}; } void setPosition(Vector3 vector3) { x = vector3.x; y = vector3.y; z = vector3.z; } Box operator+(const Vector3 &vector3) const { return Box{x + vector3.x, y + vector3.y, z + vector3.z, width, height, depth}; } Box operator-(const Vector3 &vector3) const { return Box{x - vector3.x, y - vector3.y, z - vector3.z, width, height, depth}; } Box &operator+=(const Vector3 &vector3) { *this = operator+(vector3); return *this; } Box &operator-=(const Vector3 &vector3) { *this = operator-(vector3); return *this; } T x = 0; T y = 0; T z = 0; T width = 0; T height = 0; T depth = 0; }; using IntBox = Box; using FloatBox = Box; #endif // BOX_HPP_