MxEngine
AABB.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "Utilities/Math/Math.h"
32 
33 namespace MxEngine
34 {
35  class AABB
36  {
37  public:
38  Vector3 Min = MakeVector3(0.0f);
39  Vector3 Max = MakeVector3(0.0f);
40 
41  constexpr Vector3 Length() const
42  {
43  return Max - Min;
44  }
45 
46  constexpr Vector3 GetCenter() const
47  {
48  return (Max + Min) * 0.5f;
49  }
50  };
51 
52  inline constexpr AABB operator*(const AABB& box, const Vector3& scale)
53  {
54  return AABB{ box.Min * scale, box.Max * scale };
55  }
56 
57  inline constexpr AABB operator/(const AABB& box, const Vector3& scale)
58  {
59  return AABB{ box.Min / scale, box.Max / scale };
60  }
61 
62  inline constexpr AABB operator+(const AABB& box, const Vector3& translate)
63  {
64  return AABB{ box.Min + translate, box.Max + translate };
65  }
66 
67  inline constexpr AABB operator-(const AABB& box, const Vector3& translate)
68  {
69  return AABB{ box.Min - translate, box.Max - translate };
70  }
71 
72  inline AABB operator*(const AABB& box, const Matrix4x4& matrix)
73  {
74  std::array<Vector3, 8> vecs;
75  vecs =
76  {
77  matrix * MakeVector4(box.Min.x, box.Min.y, box.Min.z, 1.0f),
78  matrix * MakeVector4(box.Min.x, box.Min.y, box.Max.z, 1.0f),
79  matrix * MakeVector4(box.Min.x, box.Max.y, box.Min.z, 1.0f),
80  matrix * MakeVector4(box.Min.x, box.Max.y, box.Max.z, 1.0f),
81  matrix * MakeVector4(box.Max.x, box.Min.y, box.Min.z, 1.0f),
82  matrix * MakeVector4(box.Max.x, box.Min.y, box.Max.z, 1.0f),
83  matrix * MakeVector4(box.Max.x, box.Max.y, box.Min.z, 1.0f),
84  matrix * MakeVector4(box.Max.x, box.Max.y, box.Max.z, 1.0f),
85  };
86  auto minmax = MinMaxComponents(vecs.data(), vecs.size());
87  return AABB{ minmax.first, minmax.second };
88  }
89 }
std::pair< Vector3, Vector3 > MinMaxComponents(Vector3 *verteces, size_t size)
Definition: Math.h:575
Definition: Application.cpp:49
Definition: AABB.h:35