OpenBarnyard
 
Loading...
Searching...
No Matches
TSphere.h
Go to the documentation of this file.
1#pragma once
2#include "TVector4.h"
3#include "TPlane.h"
4
6
7class TSphere
8{
9public:
10 constexpr TSphere() = default;
11
12 constexpr TSphere( const TVector4& a_rSphereData )
13 : m_Origin( a_rSphereData.AsVector3() ), m_fRadius( a_rSphereData.w )
14 {}
15
16 constexpr TSphere( const TVector3& a_rOrigin, TFLOAT a_fRadius )
17 : m_Origin( a_rOrigin ), m_fRadius( a_fRadius )
18 {}
19
20 constexpr TSphere( TFLOAT a_fOriginX, TFLOAT a_fOriginY, TFLOAT a_fOriginZ, TFLOAT a_fRadius )
21 : m_Origin( a_fOriginX, a_fOriginY, a_fOriginZ ), m_fRadius( a_fRadius )
22 {}
23
24 constexpr TSphere( const TSphere& a_rSphere )
25 : m_Origin( a_rSphere.m_Origin ), m_fRadius( a_rSphere.m_fRadius )
26 {}
27
28 constexpr void Set( const TVector4& a_rSphereData )
29 {
30 m_Origin = a_rSphereData.AsVector3();
31 m_fRadius = a_rSphereData.w;
32 }
33
34 constexpr void Set( const TVector3& a_rOrigin, TFLOAT a_fRadius )
35 {
36 m_Origin = a_rOrigin;
37 m_fRadius = a_fRadius;
38 }
39
40 constexpr void Set( TFLOAT a_fOriginX, TFLOAT a_fOriginY, TFLOAT a_fOriginZ, TFLOAT a_fRadius )
41 {
42 m_Origin.Set( a_fOriginX, a_fOriginY, a_fOriginZ );
43 m_fRadius = a_fRadius;
44 }
45
46 constexpr void Set( const TSphere& a_rSphere )
47 {
48 m_Origin = a_rSphere.m_Origin;
49 m_fRadius = a_rSphere.m_fRadius;
50 }
51
52 void Union( const TSphere& a_rSphere1, const TSphere& a_rSphere2 )
53 {
54 TVector3 diff = a_rSphere2.m_Origin - a_rSphere1.m_Origin;
55
56 TFLOAT fMag = diff.MagnitudeSq();
57 TFLOAT fRadDiff = a_rSphere2.m_fRadius - a_rSphere1.m_fRadius;
58
59 if ( fRadDiff <= 0.0f )
60 {
61 if ( fMag < fRadDiff * fRadDiff )
62 Set( a_rSphere1 );
63 }
64 else if ( fMag < fRadDiff * fRadDiff )
65 {
66 Set( a_rSphere2 );
67 }
68 else
69 {
70 m_fRadius = ( a_rSphere1.m_fRadius + a_rSphere2.m_fRadius + TMath::Sqrt( fMag ) ) * 0.5f;
71 diff.Multiply( ( fRadDiff - a_rSphere1.m_fRadius ) / TMath::Sqrt( fMag ) );
72 m_Origin.Add( a_rSphere1.m_Origin, diff );
73 }
74 }
75
76 constexpr TPlane::PlaneComparison ComparePlane( const TPlane& a_rPlane )
77 {
78 float fDist = TVector3::DotProduct( a_rPlane.GetNormal(), m_Origin ) - a_rPlane.GetD();
79
80 if ( m_fRadius < fDist )
82
83 if ( fDist < -m_fRadius )
85
87 }
88
89 constexpr TSphere& operator=( const TSphere& a_rSphere )
90 {
91 m_Origin = a_rSphere.m_Origin;
92 m_fRadius = a_rSphere.m_fRadius;
93 return *this;
94 }
95
97 {
98 return m_Origin;
99 }
100
101 constexpr TFORCEINLINE const TVector3& GetOrigin() const
102 {
103 return m_Origin;
104 }
105
106 constexpr TFORCEINLINE TFLOAT GetRadius() const
107 {
108 return m_fRadius;
109 }
110
112 TFORCEINLINE const TVector4& AsVector4() const { return *TREINTERPRETCAST( const TVector4*, this ); }
113
114private:
115 TVector3 m_Origin;
116 TFLOAT m_fRadius;
117};
118
#define TREINTERPRETCAST(TYPE, VALUE)
Definition Defines.h:68
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TFORCEINLINE
Definition Defines.h:74
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
float TFLOAT
Definition Typedefs.h:4
TFORCEINLINE TFLOAT Sqrt(TFLOAT a_fX)
Definition TMathInline.h:84
Definition TPlane.h:7
PlaneComparison
Definition TPlane.h:10
@ PlaneComparison_InBack
Definition TPlane.h:13
@ PlaneComparison_Intersects
Definition TPlane.h:12
@ PlaneComparison_InFront
Definition TPlane.h:11
TFORCEINLINE constexpr TFLOAT GetD() const
Definition TPlane.h:69
TFORCEINLINE constexpr const TVector3 & GetNormal() const
Definition TPlane.h:79
constexpr void Set(const TSphere &a_rSphere)
Definition TSphere.h:46
constexpr TSphere(const TVector4 &a_rSphereData)
Definition TSphere.h:12
constexpr void Set(const TVector4 &a_rSphereData)
Definition TSphere.h:28
constexpr TSphere(const TVector3 &a_rOrigin, TFLOAT a_fRadius)
Definition TSphere.h:16
constexpr TSphere(const TSphere &a_rSphere)
Definition TSphere.h:24
constexpr void Set(TFLOAT a_fOriginX, TFLOAT a_fOriginY, TFLOAT a_fOriginZ, TFLOAT a_fRadius)
Definition TSphere.h:40
constexpr TSphere()=default
TFORCEINLINE TVector4 & AsVector4()
Definition TSphere.h:111
constexpr TFORCEINLINE TFLOAT GetRadius() const
Definition TSphere.h:106
constexpr TSphere(TFLOAT a_fOriginX, TFLOAT a_fOriginY, TFLOAT a_fOriginZ, TFLOAT a_fRadius)
Definition TSphere.h:20
TFORCEINLINE const TVector4 & AsVector4() const
Definition TSphere.h:112
constexpr TPlane::PlaneComparison ComparePlane(const TPlane &a_rPlane)
Definition TSphere.h:76
constexpr void Set(const TVector3 &a_rOrigin, TFLOAT a_fRadius)
Definition TSphere.h:34
constexpr TSphere & operator=(const TSphere &a_rSphere)
Definition TSphere.h:89
constexpr TFORCEINLINE TVector3 & GetOrigin()
Definition TSphere.h:96
void Union(const TSphere &a_rSphere1, const TSphere &a_rSphere2)
Definition TSphere.h:52
constexpr TFORCEINLINE const TVector3 & GetOrigin() const
Definition TSphere.h:101
constexpr void Multiply(const TVector3 &vec)
Definition TVector3.h:103
constexpr TFLOAT MagnitudeSq() const
Definition TVector3.h:133
static constexpr TFLOAT DotProduct(const TVector3 &vec1, const TVector3 &vec2)
Definition TVector3.h:147
TVector3 & AsVector3()
Definition TVector4.h:321
TFLOAT w
Definition TVector4.h:367