Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Interval.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 "Templates/IsArithmetic.h"
7#include "Templates/UnrealTypeTraits.h"
8#include "Math/NumericLimits.h"
9#include "Math/UnrealMathUtility.h"
10
11/**
12 * Type traits for Arithmetic interval.
13 */
14template <typename ElementType> struct TIntervalTraits
15{
16 static_assert(TIsArithmetic<ElementType>::Value, "Incompatible TInterval element type.");
17
18 static ElementType Max()
19 {
21 }
22
23 static ElementType Lowest()
24 {
26 }
27};
28
29/**
30 * Template for numeric interval
31 */
32template<typename ElementType> struct TInterval
33{
34 /** Holds the lower bound of the interval. */
35 ElementType Min;
36
37 /** Holds the upper bound of the interval. */
38 ElementType Max;
39
40public:
41
42 /**
43 * Default constructor.
44 *
45 * The interval is invalid
46 */
50 { }
51
52 /**
53 * Creates and initializes a new interval with the specified lower and upper bounds.
54 *
55 * @param InMin The lower bound of the constructed interval.
56 * @param InMax The upper bound of the constructed interval.
57 */
58 TInterval( ElementType InMin, ElementType InMax )
59 : Min(InMin)
60 , Max(InMax)
61 { }
62
63public:
64
65 /**
66 * Offset the interval by adding X.
67 *
68 * @param X The offset.
69 */
70 void operator+= ( ElementType X )
71 {
72 if (IsValid())
73 {
74 Min += X;
75 Max += X;
76 }
77 }
78
79 /**
80 * Offset the interval by subtracting X.
81 *
82 * @param X The offset.
83 */
84 void operator-= ( ElementType X )
85 {
86 if (IsValid())
87 {
88 Min -= X;
89 Max -= X;
90 }
91 }
92
93public:
94
95 /**
96 * Computes the size of this interval.
97 *
98 * @return Interval size.
99 */
100 ElementType Size() const
101 {
102 return (Max - Min);
103 }
104
105 /**
106 * Whether interval is valid (Min <= Max).
107 *
108 * @return false when interval is invalid, true otherwise
109 */
110 bool IsValid() const
111 {
112 return (Min <= Max);
113 }
114
115 /**
116 * Checks whether this interval contains the specified element.
117 *
118 * @param Element The element to check.
119 * @return true if the range interval the element, false otherwise.
120 */
121 bool Contains( const ElementType& Element ) const
122 {
123 return IsValid() && (Element >= Min && Element <= Max);
124 }
125
126 /**
127 * Expands this interval to both sides by the specified amount.
128 *
129 * @param ExpandAmount The amount to expand by.
130 */
131 void Expand( ElementType ExpandAmount )
132 {
133 if (IsValid())
134 {
135 Min -= ExpandAmount;
136 Max += ExpandAmount;
137 }
138 }
139
140 /**
141 * Expands this interval if necessary to include the specified element.
142 *
143 * @param X The element to include.
144 */
145 void Include( ElementType X )
146 {
147 if (!IsValid())
148 {
149 Min = X;
150 Max = X;
151 }
152 else
153 {
154 if (X < Min)
155 {
156 Min = X;
157 }
158
159 if (X > Max)
160 {
161 Max = X;
162 }
163 }
164 }
165
166 /**
167 * Interval interpolation
168 *
169 * @param Alpha interpolation amount
170 * @return interpolation result
171 */
172 ElementType Interpolate( float Alpha ) const
173 {
174 if (IsValid())
175 {
176 return Min + ElementType(Alpha*Size());
177 }
178
179 return ElementType();
180 }
181
182public:
183
184 /**
185 * Calculates the intersection of two intervals.
186 *
187 * @param A The first interval.
188 * @param B The second interval.
189 * @return The intersection.
190 */
191 friend TInterval Intersect( const TInterval& A, const TInterval& B )
192 {
193 if (A.IsValid() && B.IsValid())
194 {
195 return TInterval(FMath::Max(A.Min, B.Min), FMath::Min(A.Max, B.Max));
196 }
197
198 return TInterval();
199 }
200
201 /**
202 * Serializes the interval.
203 *
204 * @param Ar The archive to serialize into.
205 * @param Interval The interval to serialize.
206 * @return Reference to the Archive after serialization.
207 */
208 friend class FArchive& operator<<( class FArchive& Ar, TInterval& Interval )
209 {
210 return Ar << Interval.Min << Interval.Max;
211 }
212
213 /**
214 * Gets the hash for the specified interval.
215 *
216 * @param Interval The Interval to get the hash for.
217 * @return Hash value.
218 */
219 friend uint32 GetTypeHash(const TInterval& Interval)
220 {
222 }
223};
224
225/* Default intervals for built-in types
226 *****************************************************************************/
227
228#define DEFINE_INTERVAL_WRAPPER_STRUCT(Name, ElementType)
229 struct Name : TInterval<ElementType>
230 {
231 private:
232 typedef TInterval<ElementType> Super;
233
234 public:
235 Name()
236 : Super()
237 {
238 }
239
240 Name( const Super& Other )
241 : Super( Other )
242 {
243 }
244
245 Name( ElementType InMin, ElementType InMax )
246 : Super( InMin, InMax )
247 {
248 }
249
250 friend Name Intersect( const Name& A, const Name& B )
251 {
252 return Intersect( static_cast<const Super&>( A ), static_cast<const Super&>( B ) );
253 }
254 };
255
256 template <>
257 struct TIsBitwiseConstructible<Name, TInterval<ElementType>>
258 {
259 enum { Value = true };
260 };
261
262 template <>
263 struct TIsBitwiseConstructible<TInterval<ElementType>, Name>
264 {
265 enum { Value = true };
266 };
267
268DEFINE_INTERVAL_WRAPPER_STRUCT(FFloatInterval, float)
269DEFINE_INTERVAL_WRAPPER_STRUCT(FDoubleInterval, double)
270DEFINE_INTERVAL_WRAPPER_STRUCT(FInt32Interval, int32)
#define DEFINE_INTERVAL_WRAPPER_STRUCT(Name, ElementType)
Definition Interval.h:228
friend TInterval Intersect(const TInterval &A, const TInterval &B)
Definition Interval.h:191
void operator-=(ElementType X)
Definition Interval.h:84
bool IsValid() const
Definition Interval.h:110
void Expand(ElementType ExpandAmount)
Definition Interval.h:131
TInterval(ElementType InMin, ElementType InMax)
Definition Interval.h:58
bool Contains(const ElementType &Element) const
Definition Interval.h:121
friend uint32 GetTypeHash(const TInterval &Interval)
Definition Interval.h:219
TInterval()
Definition Interval.h:47
void operator+=(ElementType X)
Definition Interval.h:70
ElementType Max
Definition Interval.h:38
ElementType Min
Definition Interval.h:35
ElementType Interpolate(float Alpha) const
Definition Interval.h:172
ElementType Size() const
Definition Interval.h:100
void Include(ElementType X)
Definition Interval.h:145
static ElementType Max()
Definition Interval.h:18
static ElementType Lowest()
Definition Interval.h:23