UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
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"
10
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
32template<typename ElementType> struct TInterval
33{
35 ElementType Min;
36
38 ElementType Max;
39
40public:
41
48 : Min(TIntervalTraits<ElementType>::Max())
49 , Max(TIntervalTraits<ElementType>::Lowest())
50 { }
51
58 [[nodiscard]] TInterval( ElementType InMin, ElementType InMax )
59 : Min(InMin)
60 , Max(InMax)
61 { }
62
63public:
64
70 void operator+= ( ElementType X )
71 {
72 if (IsValid())
73 {
74 Min += X;
75 Max += X;
76 }
77 }
78
84 void operator-= ( ElementType X )
85 {
86 if (IsValid())
87 {
88 Min -= X;
89 Max -= X;
90 }
91 }
92
99 [[nodiscard]] bool operator==(const TInterval& Other) const
100 {
101 return Min == Other.Min && Max == Other.Max;
102 }
103
110 [[nodiscard]] bool operator!=(const TInterval& Other) const
111 {
112 return (Min != Other.Min) || (Max != Other.Max);
113 }
114
115public:
116
122 [[nodiscard]] ElementType Size() const
123 {
124 return (Max - Min);
125 }
126
132 [[nodiscard]] bool IsValid() const
133 {
134 return (Min <= Max);
135 }
136
143 [[nodiscard]] bool Contains( const ElementType& Element ) const
144 {
145 return IsValid() && (Element >= Min && Element <= Max);
146 }
147
153 void Expand( ElementType ExpandAmount )
154 {
155 if (IsValid())
156 {
157 Min -= ExpandAmount;
158 Max += ExpandAmount;
159 }
160 }
161
167 void Include( ElementType X )
168 {
169 if (!IsValid())
170 {
171 Min = X;
172 Max = X;
173 }
174 else
175 {
176 if (X < Min)
177 {
178 Min = X;
179 }
180
181 if (X > Max)
182 {
183 Max = X;
184 }
185 }
186 }
187
194 [[nodiscard]] ElementType Interpolate( float Alpha ) const
195 {
196 if (IsValid())
197 {
198 return Min + ElementType(Alpha*Size());
199 }
200
201 return ElementType();
202 }
203
210 [[nodiscard]] ElementType Clamp( ElementType X ) const
211 {
212 if (!IsValid())
213 {
214 return ElementType();
215 }
216
217 return FMath::Clamp(X, Min, Max);
218 }
219
225 [[nodiscard]] ElementType GetRangePct(ElementType X) const
226 {
227 if (!IsValid())
228 {
229 return ElementType();
230 }
231
232 return FMath::GetRangePct(Min, Max, X);
233 }
234public:
235
243 [[nodiscard]] friend TInterval Intersect( const TInterval& A, const TInterval& B )
244 {
245 if (A.IsValid() && B.IsValid())
246 {
247 return TInterval(FMath::Max(A.Min, B.Min), FMath::Min(A.Max, B.Max));
248 }
249
250 return TInterval();
251 }
252
260 friend class FArchive& operator<<( class FArchive& Ar, TInterval& Interval )
261 {
262 return Ar << Interval.Min << Interval.Max;
263 }
264
271 friend uint32 GetTypeHash(const TInterval& Interval)
272 {
273 return HashCombine(GetTypeHash(Interval.Min), GetTypeHash(Interval.Max));
274 }
275};
276
277/* Default intervals for built-in types
278 *****************************************************************************/
279
280#define DEFINE_INTERVAL_WRAPPER_STRUCT(Name, ElementType) \
281 struct Name : TInterval<ElementType> \
282 { \
283 private: \
284 typedef TInterval<ElementType> Super; \
285 \
286 public: \
287 Name() \
288 : Super() \
289 { \
290 } \
291 \
292 Name( const Super& Other ) \
293 : Super( Other ) \
294 { \
295 } \
296 \
297 Name( ElementType InMin, ElementType InMax ) \
298 : Super( InMin, InMax ) \
299 { \
300 } \
301 \
302 friend Name Intersect( const Name& A, const Name& B ) \
303 { \
304 return Intersect( static_cast<const Super&>( A ), static_cast<const Super&>( B ) ); \
305 } \
306 }; \
307 \
308 template <> \
309 struct TIsBitwiseConstructible<Name, TInterval<ElementType>> \
310 { \
311 enum { Value = true }; \
312 }; \
313 \
314 template <> \
315 struct TIsBitwiseConstructible<TInterval<ElementType>, Name> \
316 { \
317 enum { Value = true }; \
318 };
319
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define X(Name, Desc)
Definition FormatStringSan.h:47
#define DEFINE_INTERVAL_WRAPPER_STRUCT(Name, ElementType)
Definition Interval.h:280
constexpr uint32 HashCombine(uint32 A, uint32 C)
Definition TypeHash.h:36
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
static constexpr auto GetRangePct(T MinValue, T MaxValue, T2 Value)
Definition UnrealMathUtility.h:1047
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592
Definition Interval.h:15
static ElementType Max()
Definition Interval.h:18
static ElementType Lowest()
Definition Interval.h:23
Definition Interval.h:33
friend TInterval Intersect(const TInterval &A, const TInterval &B)
Definition Interval.h:243
ElementType GetRangePct(ElementType X) const
Definition Interval.h:225
void operator-=(ElementType X)
Definition Interval.h:84
ElementType Clamp(ElementType X) const
Definition Interval.h:210
bool IsValid() const
Definition Interval.h:132
void Expand(ElementType ExpandAmount)
Definition Interval.h:153
TInterval(ElementType InMin, ElementType InMax)
Definition Interval.h:58
bool Contains(const ElementType &Element) const
Definition Interval.h:143
friend uint32 GetTypeHash(const TInterval &Interval)
Definition Interval.h:271
TInterval()
Definition Interval.h:47
void operator+=(ElementType X)
Definition Interval.h:70
friend class FArchive & operator<<(class FArchive &Ar, TInterval &Interval)
Definition Interval.h:260
ElementType Max
Definition Interval.h:38
ElementType Min
Definition Interval.h:35
ElementType Interpolate(float Alpha) const
Definition Interval.h:194
ElementType Size() const
Definition Interval.h:122
bool operator!=(const TInterval &Other) const
Definition Interval.h:110
bool operator==(const TInterval &Other) const
Definition Interval.h:99
void Include(ElementType X)
Definition Interval.h:167
Definition IsArithmetic.h:12
Definition NumericLimits.h:41