Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Box.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Mathematics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_BOX_HPP
8 #define NAZARA_BOX_HPP
9 
10 #include <Nazara/Core/String.hpp>
11 #include <Nazara/Math/Enums.hpp>
12 #include <Nazara/Math/Matrix4.hpp>
13 #include <Nazara/Math/Rect.hpp>
14 #include <Nazara/Math/Sphere.hpp>
15 #include <Nazara/Math/Vector3.hpp>
16 
17 namespace Nz
18 {
19  struct SerializationContext;
20 
21  template<typename T>
22  class Box
23  {
24  public:
25  Box() = default;
26  Box(T Width, T Height, T Depth);
27  Box(T X, T Y, T Z, T Width, T Height, T Depth);
28  Box(const T box[6]);
29  Box(const Rect<T>& rect);
30  Box(const Vector3<T>& lengths);
31  Box(const Vector3<T>& vec1, const Vector3<T>& vec2);
32  template<typename U> explicit Box(const Box<U>& box);
33  Box(const Box& box) = default;
34  ~Box() = default;
35 
36  bool Contains(T X, T Y, T Z) const;
37  bool Contains(const Box& box) const;
38  bool Contains(const Vector3<T>& point) const;
39 
40  Box& ExtendTo(T X, T Y, T Z);
41  Box& ExtendTo(const Box& box);
42  Box& ExtendTo(const Vector3<T>& point);
43 
45  Vector3<T> GetCenter() const;
46  Vector3<T> GetCorner(BoxCorner corner) const;
47  Vector3<T> GetLengths() const;
48  Vector3<T> GetMaximum() const;
49  Vector3<T> GetMinimum() const;
50  Vector3<T> GetNegativeVertex(const Vector3<T>& normal) const;
51  Vector3<T> GetPosition() const;
52  Vector3<T> GetPositiveVertex(const Vector3<T>& normal) const;
53  T GetRadius() const;
55  T GetSquaredRadius() const;
56 
57  bool Intersect(const Box& box, Box* intersection = nullptr) const;
58 
59  bool IsValid() const;
60 
61  Box& MakeZero();
62 
63  Box& Set(T Width, T Height, T Depth);
64  Box& Set(T X, T Y, T Z, T Width, T Height, T Depth);
65  Box& Set(const T box[6]);
66  Box& Set(const Box& box);
67  Box& Set(const Rect<T>& rect);
68  Box& Set(const Vector3<T>& lengths);
69  Box& Set(const Vector3<T>& vec1, const Vector3<T>& vec2);
70  template<typename U> Box& Set(const Box<U>& box);
71 
72  String ToString() const;
73 
74  Box& Transform(const Matrix4<T>& matrix, bool applyTranslation = true);
75  Box& Translate(const Vector3<T>& translation);
76 
77  T& operator[](std::size_t i);
78  T operator[](std::size_t i) const;
79 
80  Box operator*(T scalar) const;
81  Box operator*(const Vector3<T>& vec) const;
82  Box& operator=(const Box& other) = default;
83 
84  Box& operator*=(T scalar);
85  Box& operator*=(const Vector3<T>& vec);
86 
87  bool operator==(const Box& box) const;
88  bool operator!=(const Box& box) const;
89 
90  static Box Lerp(const Box& from, const Box& to, T interpolation);
91  static Box Zero();
92 
93  T x, y, z, width, height, depth;
94  };
95 
96  typedef Box<double> Boxd;
97  typedef Box<float> Boxf;
98  typedef Box<int> Boxi;
99  typedef Box<unsigned int> Boxui;
100  typedef Box<Int32> Boxi32;
101  typedef Box<UInt32> Boxui32;
102 
103  template<typename T> bool Serialize(SerializationContext& context, const Box<T>& box);
104  template<typename T> bool Unserialize(SerializationContext& context, Box<T>* box);
105 }
106 
107 template<typename T>
108 std::ostream& operator<<(std::ostream& out, const Nz::Box<T>& box);
109 
110 
111 #include <Nazara/Math/Box.inl>
112 
113 #endif // NAZARA_BOX_HPP
Sphere< T > GetSquaredBoundingSphere() const
Gets the squared bounding sphere for the box.
Definition: Box.inl:431
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Box & ExtendTo(T X, T Y, T Z)
Extends the box to contain the point in the boundary.
Definition: Box.inl:184
T & operator[](std::size_t i)
Returns the ith element of the box.
Definition: Box.inl:744
bool Intersect(const Box &box, Box *intersection=nullptr) const
Checks whether or not this box intersects another one.
Definition: Box.inl:459
static Box Lerp(const Box &from, const Box &to, T interpolation)
Interpolates the box to other one with a factor of interpolation.
Definition: Box.inl:870
bool Unserialize(SerializationContext &context, bool *value)
Unserializes a boolean.
Definition: Algorithm.inl:279
bool Contains(T X, T Y, T Z) const
Tests whether the box contains the provided point inclusive of the edge of the box.
Definition: Box.inl:134
Sphere< T > GetBoundingSphere() const
Gets the bounding sphere for the box.
Definition: Box.inl:251
Box & MakeZero()
Makes the box position (0, 0, 0) and lengths (0, 0, 0)
Definition: Box.inl:508
Math class that represents a sphere "S2" in a three dimensional euclidean space.
Definition: Sphere.hpp:20
Core class that represents a string.
Definition: String.hpp:22
Box & operator*=(T scalar)
Multiplies the lengths of this box with the scalar.
Definition: Box.inl:802
Vector3< T > GetCenter() const
Gets a Vector3 for the center.
Definition: Box.inl:262
T GetSquaredRadius() const
Gets the squared radius of the box.
Definition: Box.inl:442
Math class that represents an axis-aligned rectangle in two dimensions.
Definition: Rect.hpp:19
bool IsValid() const
Checks whether this box is valid.
Definition: Box.inl:495
Box & Translate(const Vector3< T > &translation)
Translates the box.
Definition: Box.inl:725
T GetRadius() const
Gets the radius of the box.
Definition: Box.inl:418
Vector3< T > GetNegativeVertex(const Vector3< T > &normal) const
Computes the negative vertex of one direction.
Definition: Box.inl:357
String ToString() const
Gives a string representation.
Definition: Box.inl:689
Vector3< T > GetPositiveVertex(const Vector3< T > &normal) const
Computes the positive vertex of one direction.
Definition: Box.inl:396
Structure containing a serialization/unserialization context states.
Definition: SerializationContext.hpp:18
bool Serialize(SerializationContext &context, bool value)
Serializes a boolean.
Definition: Algorithm.inl:214
bool operator==(const Box &box) const
Compares the box to other one.
Definition: Box.inl:836
Vector3< T > GetMaximum() const
Gets a Vector3 for the maximum point.
Definition: Box.inl:329
Vector3< T > GetMinimum() const
Gets a Vector3 for the minimum point.
Definition: Box.inl:342
Box operator*(T scalar) const
Multiplies the lengths with the scalar.
Definition: Box.inl:776
Box & Set(T Width, T Height, T Depth)
Sets the components of the box with width, height and depth.
Definition: Box.inl:532
Math class that represents a transformation of the four dimensional vector space with the notion of p...
Definition: Matrix4.hpp:26
static Box Zero()
Shorthand for the box (0, 0, 0, 0, 0, 0)
Definition: Box.inl:899
Vector3< T > GetLengths() const
Gets a Vector3 for the lengths.
Definition: Box.inl:316
Math class that represents an element of the three dimensional vector space.
Definition: Matrix4.hpp:22
Vector3< T > GetPosition() const
Gets a Vector3 for the position.
Definition: Box.inl:381
Vector3< T > GetCorner(BoxCorner corner) const
Gets the Vector3 for the corner.
Definition: Box.inl:277
Box & Transform(const Matrix4< T > &matrix, bool applyTranslation=true)
Transforms the box according to the matrix.
Definition: Box.inl:705
bool operator!=(const Box &box) const
Compares the box to other one.
Definition: Box.inl:850
Math class that represents a three dimensional box.
Definition: Box.hpp:22