Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
AABB.cpp
1/*
2Copyright (C) 2017-2019,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#include <stdexcept>
17#include "3DMath.h"
18#include "aeongames/Plane.hpp"
19#include "aeongames/AABB.hpp"
21
22namespace AeonGames
23{
25 = default;
26 AABB::AABB ( const Vector3 & aCenter, const Vector3 & aRadii ) : mCenter ( aCenter ), mRadii ( aRadii )
27 {
28 }
29 const Vector3 & AABB::GetCenter() const
30 {
31 return mCenter;
32 }
33 const Vector3 & AABB::GetRadii() const
34 {
35 return mRadii;
36 }
37 void AABB::SetCenter ( const Vector3 & aCenter )
38 {
39 mCenter = aCenter;
40 }
41 void AABB::SetRadii ( const Vector3 & aRadii )
42 {
43 mRadii = aRadii;
44 }
45 std::array<Vector3, 8> AABB::GetPoints ( const Vector3 & aOffset ) const
46 {
47 return std::array<Vector3, 8>
48 {
49 {
50 aOffset + mCenter + mRadii,
51 aOffset + mCenter - mRadii,
52 aOffset + mCenter + Vector3 ( -mRadii[0], mRadii[1], mRadii[2] ),
53 aOffset + mCenter - Vector3 ( -mRadii[0], mRadii[1], mRadii[2] ),
54 aOffset + mCenter + Vector3 ( mRadii[0], -mRadii[1], mRadii[2] ),
55 aOffset + mCenter - Vector3 ( mRadii[0], -mRadii[1], mRadii[2] ),
56 aOffset + mCenter + Vector3 ( mRadii[0], mRadii[1], -mRadii[2] ),
57 aOffset + mCenter - Vector3 ( mRadii[0], mRadii[1], -mRadii[2] )
58 }
59 };
60 }
61
63 {
64 return {mRadii.IsZero() ? Vector3{1, 1, 1} : mRadii, Quaternion{}, mCenter};
65 }
66
67 float AABB::GetDistanceToPlane ( const Plane & aPlane ) const
68 {
69 return Dot ( aPlane.GetNormal(),
70 mCenter +
72 {
73 ( aPlane.GetNormal() [0] < 0 ) ? mRadii[0] : -mRadii[0],
74 ( aPlane.GetNormal() [1] < 0 ) ? mRadii[1] : -mRadii[1],
75 ( aPlane.GetNormal() [2] < 0 ) ? mRadii[2] : -mRadii[2]
76 } ) - aPlane.GetDistance();
77 }
79 {
80 Vector3 min =
81 {
82 std::min ( mCenter[0] - mRadii[0], lhs.mCenter[0] - lhs.mRadii[0] ),
83 std::min ( mCenter[1] - mRadii[1], lhs.mCenter[1] - lhs.mRadii[1] ),
84 std::min ( mCenter[2] - mRadii[2], lhs.mCenter[2] - lhs.mRadii[2] )
85 };
86 Vector3 max =
87 {
88 std::max ( mCenter[0] + mRadii[0], lhs.mCenter[0] + lhs.mRadii[0] ),
89 std::max ( mCenter[1] + mRadii[1], lhs.mCenter[1] + lhs.mRadii[1] ),
90 std::max ( mCenter[2] + mRadii[2], lhs.mCenter[2] + lhs.mRadii[2] )
91 };
92 mCenter = ( min + max ) / 2.0f;
93 mRadii = max - mCenter;
94 return *this;
95 }
96}
Inline functions related to 3D Math.
Header for the axis aligned bounding box class.
Header for the plane class.
Header for Transform component class.
DLL AABB & operator+=(const AABB &lhs)
Expand this AABB to enclose another AABB.
Definition AABB.cpp:78
DLL void SetRadii(const Vector3 &aRadii)
Set the radii (half-extents) of the AABB.
Definition AABB.cpp:41
DLL AABB()
Default constructor.
DLL Transform GetTransform() const
Get the AABB as a transform, the AABB center becomes translation and the radii becomes scale.
Definition AABB.cpp:62
DLL float GetDistanceToPlane(const Plane &aPlane) const
Returns the shortest distance from any point in the plane's surface to the support point of the AABB ...
Definition AABB.cpp:67
DLL std::array< Vector3, 8 > GetPoints(const Vector3 &aOffset={ 0.0f, 0.0f, 0.0f, }) const
Get the eight corner points of the AABB.
Definition AABB.cpp:45
DLL const Vector3 & GetRadii() const
Get the radii (half-extents) of the AABB.
Definition AABB.cpp:33
DLL const Vector3 & GetCenter() const
Get the center position of the AABB.
Definition AABB.cpp:29
DLL void SetCenter(const Vector3 &aCenter)
Set the center position of the AABB.
Definition AABB.cpp:37
Plane class.
Definition Plane.hpp:33
DLL const float & GetDistance() const
Get the distance from the origin to the plane.
Definition Plane.cpp:45
DLL const Vector3 & GetNormal() const
Get the plane normal vector.
Definition Plane.cpp:40
Quaternion class.
Component class for any object that requires space transformations.
Definition Transform.hpp:34
3D vector class.
Definition Vector3.hpp:32
<- This is here just for the literals
Definition AABB.hpp:31
DLL const float Dot(const Vector3 &aLhs, const Vector3 &aRhs)
Compute the dot product of two vectors.
Definition Vector3.cpp:249