Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Bounds.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Math/UnrealMathUtility.h"
7#include "Math/Vector.h"
8#include "Math/Vector4.h"
9
10template< typename T >
11struct TBounds
12{
13 template< typename U > using TVector = UE::Math::TVector<U>;
14 template< typename U > using TVector4 = UE::Math::TVector4<U>;
15 using FReal = T;
16
17 TVector4<T> Min = TVector4<T>( TNumericLimits<T>::Max(), TNumericLimits<T>::Max(), TNumericLimits<T>::Max() );
18 TVector4<T> Max = TVector4<T>( -TNumericLimits<T>::Max(), -TNumericLimits<T>::Max(), -TNumericLimits<T>::Max() );
19
20 FORCEINLINE TBounds<T>& operator=( const TVector<T>& Other )
21 {
22 Min = Other;
23 Max = Other;
24 return *this;
25 }
26
27 FORCEINLINE TBounds<T>& operator+=( const TVector<T>& Other )
28 {
29 VectorStoreAligned( VectorMin( VectorLoadAligned( &Min ), VectorLoadFloat3( &Other ) ), &Min );
30 VectorStoreAligned( VectorMax( VectorLoadAligned( &Max ), VectorLoadFloat3( &Other ) ), &Max );
31 return *this;
32 }
33
34 FORCEINLINE TBounds<T>& operator+=( const TBounds<T>& Other )
35 {
36 VectorStoreAligned( VectorMin( VectorLoadAligned( &Min ), VectorLoadAligned( &Other.Min ) ), &Min );
37 VectorStoreAligned( VectorMax( VectorLoadAligned( &Max ), VectorLoadAligned( &Other.Max ) ), &Max );
38 return *this;
39 }
40
41 FORCEINLINE TBounds<T> operator+( const TBounds<T>& Other ) const
42 {
43 return TBounds<T>(*this) += Other;
44 }
45
46 FORCEINLINE bool Intersect( const TBounds<T>& Other ) const
47 {
48 int Separated;
49 Separated = VectorAnyGreaterThan( VectorLoadAligned( &Min ), VectorLoadAligned( &Other.Max ) );
50 Separated |= VectorAnyGreaterThan( VectorLoadAligned( &Other.Min ), VectorLoadAligned( &Max ) );
51 Separated &= 0b111;
52 return Separated == 0;
53 }
54
55 FORCEINLINE bool Contains( const TBounds<T>& Other ) const
56 {
57 int MaskMin = VectorMaskBits( VectorCompareLE( VectorLoadAligned( &Min ), VectorLoadAligned( &Other.Min ) ) );
58 int MaskMax = VectorMaskBits( VectorCompareGE( VectorLoadAligned( &Max ), VectorLoadAligned( &Other.Max ) ) );
59 return ( MaskMin & MaskMax & 7 ) == 7;
60 }
61
62 FORCEINLINE T DistSqr( const TVector<T>& Point ) const
63 {
64 auto rMin = VectorLoadAligned( &Min );
65 auto rMax = VectorLoadAligned( &Max );
66 auto rPoint = VectorLoadFloat3( &Point );
67 auto rClosest = VectorSubtract( VectorMin( VectorMax( rPoint, rMin ), rMax ), rPoint );
68 return VectorDot3Scalar( rClosest, rClosest );
69 }
70
71 FORCEINLINE TVector<T> GetCenter() const
72 {
73 return (Max + Min) * 0.5;
74 }
75
76 FORCEINLINE TVector<T> GetExtent() const
77 {
78 return (Max - Min) * 0.5;
79 }
80
81 FORCEINLINE T GetSurfaceArea() const
82 {
83 TVector<T> Size = Max - Min;
84 return 0.5 * (Size.X * Size.Y + Size.X * Size.Z + Size.Y * Size.Z);
85 }
86
87 template< typename U >
88 FORCEINLINE auto ToAbsolute( const TVector<U>& Offset ) const
89 {
90 using CommonType = typename std::common_type<T, U>::type;
91
92 TBounds< CommonType > Bounds;
93 Bounds.Min = TVector4< CommonType >( Min ) + TVector4< CommonType >( Offset, 0.0 );
94 Bounds.Max = TVector4< CommonType >( Max ) + TVector4< CommonType >( Offset, 0.0 );
95 return Bounds;
96 }
97
98 template< typename U >
99 FORCEINLINE TBounds< float > ToRelative( const TVector<U>& Offset ) const
100 {
101 using CommonType = typename std::common_type<T, U>::type;
102
103 TBounds< float > Bounds;
104 Bounds.Min = TVector4< float >( TVector4< CommonType >( Min ) - TVector4< CommonType >( Offset, 0.0 ) );
105 Bounds.Max = TVector4< float >( TVector4< CommonType >( Max ) - TVector4< CommonType >( Offset, 0.0 ) );
106 return Bounds;
107 }
108
109 FORCEINLINE friend FArchive& operator<<( FArchive& Ar, TBounds<T>& Bounds )
110 {
111 Ar << Bounds.Min;
112 Ar << Bounds.Max;
113 return Ar;
114 }
115};
116
117using FBounds3f = TBounds< float >;
118using FBounds3d = TBounds< double >;
#define FORCEINLINE
Definition Platform.h:644