Berserk
TAabb.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_TAABB_HPP
29 #define BERSERK_TAABB_HPP
30 
31 #include <core/Config.hpp>
32 #include <core/math/MathUtils.hpp>
33 #include <core/math/TVecN.hpp>
34 
36 
48 template<typename T>
49 class TAabb {
50 public:
51  TAabb() = default;
52 
53  TAabb(const TVecN<T, 3> &minv, const TVecN<T, 3> &maxv) {
54  min = minv;
55  max = maxv;
56  }
57 
58  TAabb(const TVecN<T, 3> &center, T radius) {
59  TVecN<T, 3> offset = {radius, radius, radius};
60  min = center - offset;
61  max = center + radius;
62  }
63 
64  void Fit(const TVecN<T, 3> &p) {
65  min = TVecN<T, 3>::Min(min, p);
66  max = TVecN<T, 3>::Max(max, p);
67  }
68 
69  void Fit(const TAabb &aabb) {
70  min = TVecN<T, 3>::Min(min, aabb.min);
71  max = TVecN<T, 3>::Max(max, aabb.max);
72  }
73 
74  bool Contains(const TVecN<T, 3> &p) const {
75  return min <= p && p <= max;
76  }
77 
78  bool Contains(const TAabb<T> &aabb) const {
79  return min <= aabb.min && aabb.max <= max;
80  }
81 
82  void GetPoints(T *points) const {
83  points[0] = TVecN<T, 3>(min[0], min[1], min[2]);
84  points[1] = TVecN<T, 3>(min[0], min[1], max[2]);
85  points[2] = TVecN<T, 3>(min[0], max[1], min[2]);
86  points[3] = TVecN<T, 3>(min[0], max[1], max[2]);
87  points[4] = TVecN<T, 3>(max[0], min[1], min[2]);
88  points[5] = TVecN<T, 3>(max[0], min[1], max[2]);
89  points[6] = TVecN<T, 3>(max[0], max[1], min[2]);
90  points[7] = TVecN<T, 3>(max[0], max[1], max[2]);
91  }
92 
94  return (max + min) * static_cast<T>(0.5);
95  }
96 
98  return (max - min) * static_cast<T>(0.5);
99  }
100 
101  const TVecN<T, 3> &GetMin() const {
102  return min;
103  }
104 
105  const TVecN<T, 3> &GetMax() const {
106  return max;
107  }
108 
109 private:
110  TVecN<T, 3> min{0, 0, 0};
111  TVecN<T, 3> max{0, 0, 0};
112 };
113 
115 
121 
122 #endif//BERSERK_TAABB_HPP
#define BRK_NS_END
Definition: Config.hpp:48
Axis-aligned 3d bounding box.
Definition: TAabb.hpp:49
bool Contains(const TVecN< T, 3 > &p) const
Definition: TAabb.hpp:74
TAabb(const TVecN< T, 3 > &minv, const TVecN< T, 3 > &maxv)
Definition: TAabb.hpp:53
TVecN< T, 3 > GetCenter() const
Definition: TAabb.hpp:93
void GetPoints(T *points) const
Definition: TAabb.hpp:82
TAabb()=default
const TVecN< T, 3 > & GetMax() const
Definition: TAabb.hpp:105
const TVecN< T, 3 > & GetMin() const
Definition: TAabb.hpp:101
void Fit(const TVecN< T, 3 > &p)
Definition: TAabb.hpp:64
TAabb(const TVecN< T, 3 > &center, T radius)
Definition: TAabb.hpp:58
TVecN< T, 3 > GetExtent() const
Definition: TAabb.hpp:97
bool Contains(const TAabb< T > &aabb) const
Definition: TAabb.hpp:78
void Fit(const TAabb &aabb)
Definition: TAabb.hpp:69
static TVecN Min(const TVecN &a, const TVecN &b)
Definition: TVecN.hpp:455
static TVecN Max(const TVecN &a, const TVecN &b)
Definition: TVecN.hpp:465
Definition: GLDevice.cpp:46