6#include "Math/UnrealMathUtility.h"
7#include "Containers/UnrealString.h"
8#include "Math/IntPoint.h"
9#include "Math/Vector2D.h"
15
16
17
18
19template <
typename InIntType>
22 using IntType = InIntType;
23 using IntPointType = TIntPoint<IntType>;
24 static_assert(std::is_integral_v<IntType>,
"Only an integer types are supported.");
38 IntPointType MinMax[2];
48
49
50
51
52
53
54
55 TIntRect(IntType X0, IntType Y0, IntType X1, IntType Y1)
62
63
64
65
66
67 TIntRect(IntPointType InMin, IntPointType InMax)
73 TIntRect(
const TIntRect& Other)
78 TIntRect& operator=(
const TIntRect& Other)
86
87
88 template <
typename OtherIntType>
89 explicit TIntRect(TIntRect<OtherIntType> Other)
90 : Min(IntPointType(Other.Min))
91 , Max(IntPointType(Other.Max))
96
97
98
99
100
101 const TIntRect& operator()(int32 PointIndex)
const
104 return MinMax[PointIndex];
109
110
111
112
113
114 TIntRect& operator()(int32 PointIndex)
117 return MinMax[PointIndex];
122
123
124
125
126
127 bool operator==(
const TIntRect& Other)
const
129 return Min == Other.Min && Max == Other.Max;
133
134
135
136
137
138 bool operator!=(
const TIntRect& Other)
const
140 return Min != Other.Min || Max != Other.Max;
144
145
146
147
148
149 TIntRect& operator*=(IntType Scale)
158
159
160
161
162
163 TIntRect& operator+=(
const IntPointType& Point)
172
173
174
175
176
177 TIntRect& operator-=(
const IntPointType& Point)
186
187
188
189
190
191 TIntRect operator*(IntType Scale)
const
193 return TIntRect(Min * Scale, Max * Scale);
197
198
199
200
201
202 TIntRect operator/(IntType Div)
const
204 return TIntRect(Min / Div, Max / Div);
208
209
210
211
212
213 TIntRect operator+(
const IntPointType& Point)
const
215 return TIntRect(Min + Point, Max + Point);
219
220
221
222
223
224 TIntRect operator/(
const IntPointType& Point)
const
226 return TIntRect(Min / Point, Max / Point);
230
231
232
233
234
235 TIntRect operator-(
const IntPointType& Point)
const
237 return TIntRect(Min - Point, Max - Point);
241
242
243
244
245
246 TIntRect operator+(
const TIntRect& Other)
const
248 return TIntRect(Min + Other.Min, Max + Other.Max);
252
253
254
255
256
257 TIntRect operator-(
const TIntRect& Other)
const
259 return TIntRect(Min - Other.Min, Max - Other.Max);
263
264
265
266
269 return (Max.X - Min.X) * (Max.Y - Min.Y);
273
274
275
276
277
279 TIntRect Bottom(IntType InHeight)
const
281 return TIntRect(Min.X, FMath::Max(Min.Y, Max.Y - InHeight), Max.X, Max.Y);
285
286
287
288
289 void Clip(
const TIntRect& R)
291 Min.X = FMath::Max<IntType>(Min.X, R.Min.X);
292 Min.Y = FMath::Max<IntType>(Min.Y, R.Min.Y);
293 Max.X = FMath::Min<IntType>(Max.X, R.Max.X);
294 Max.Y = FMath::Min<IntType>(Max.Y, R.Max.Y);
297 Max.X = FMath::Max<IntType>(Min.X, Max.X);
298 Max.Y = FMath::Max<IntType>(Min.Y, Max.Y);
302 void Union(
const TIntRect& R)
304 Min.X = FMath::Min<IntType>(Min.X, R.Min.X);
305 Min.Y = FMath::Min<IntType>(Min.Y, R.Min.Y);
306 Max.X = FMath::Max<IntType>(Max.X, R.Max.X);
307 Max.Y = FMath::Max<IntType>(Max.Y, R.Max.Y);
311 bool Intersect(
const TIntRect& Other)
const
313 return Other.Min.X < Max.X&& Other.Max.X > Min.X && Other.Min.Y < Max.Y&& Other.Max.Y > Min.Y;
317
318
319
320
321
322 bool Contains(IntPointType P)
const
324 return P.X >= Min.X && P.X < Max.X&& P.Y >= Min.Y && P.Y < Max.Y;
328
329
330
331
332
333 void GetCenterAndExtents(IntPointType& OutCenter, IntPointType& OutExtent)
const
335 OutExtent.X = (Max.X - Min.X) / 2;
336 OutExtent.Y = (Max.Y - Min.Y) / 2;
338 OutCenter.X = Min.X + OutExtent.X;
339 OutCenter.Y = Min.Y + OutExtent.Y;
343
344
345
346
347 IntType Height()
const
349 return (Max.Y - Min.Y);
354
355
356
357
358 void InflateRect(IntType Amount)
366
367
368
369
370 void Include(IntPointType Point)
372 Min.X = FMath::Min(Min.X, Point.X);
373 Min.Y = FMath::Min(Min.Y, Point.Y);
374 Max.X = FMath::Max(Max.X, Point.X);
375 Max.Y = FMath::Max(Max.Y, Point.Y);
379
380
381
382
383
384 TIntRect Inner(IntPointType Shrink)
const
386 return TIntRect(Min + Shrink, Max - Shrink);
389
390
391
392
393
394 TIntRect Right(IntType InWidth)
const
396 return TIntRect(FMath::Max(Min.X, Max.X - InWidth), Min.Y, Max.X, Max.Y);
400
401
402
403
404
405 TIntRect Scale(
double Fraction)
const
407 using Vec2D = UE::Math::TVector2<
double>;
408 const Vec2D Min2D = Vec2D((
double)Min.X, (
double)Min.Y) * Fraction;
409 const Vec2D Max2D = Vec2D((
double)Max.X, (
double)Max.Y) * Fraction;
412 IntCastChecked<IntType>(FMath::FloorToInt64(Min2D.X)),
413 IntCastChecked<IntType>(FMath::FloorToInt64(Min2D.Y)),
414 IntCastChecked<IntType>(FMath::CeilToInt64(Max2D.X)),
415 IntCastChecked<IntType>(FMath::CeilToInt64(Max2D.Y))
420
421
422
423
424 IntPointType Size()
const
426 return IntPointType(Max.X - Min.X, Max.Y - Min.Y);
430
431
432
433
434 FString ToString()
const
436 return FString::Printf(
TEXT(
"Min=(%s) Max=(%s)"), *Min.ToString(), *Max.ToString());
440
441
442
443
444 IntType Width()
const
446 return Max.X - Min.X;
450
451
452
453
456 return Width() == 0 && Height() == 0;
460
461
462
463
464
465
466 static TIntRect DivideAndRoundUp(TIntRect lhs, IntType Div)
468 return DivideAndRoundUp(lhs, IntPointType(Div, Div));
471 static TIntRect DivideAndRoundUp(TIntRect lhs, IntPointType Div)
473 return TIntRect(lhs.Min / Div, IntPointType::DivideAndRoundUp(lhs.Max, Div));
477
478
479
480
489
490
491
492
493
494
495 friend FArchive& operator<<( FArchive& Ar, TIntRect& Rect )
497 return Ar << Rect.Min.X << Rect.Min.Y << Rect.Max.X << Rect.Max.Y;
501template <
typename IntType>
502uint32 GetTypeHash(
const TIntRect<IntType>& InRect)
504 return HashCombine(GetTypeHash(InRect.Min), GetTypeHash(InRect.Max));
509template <>
struct TIsPODType<FInt32Rect> {
enum { Value =
true }; };
510template <>
struct TIsPODType<FUint32Rect> {
enum {
Value =
true }; };
512template <>
struct TIsUECoreVariant<FInt32Rect> {
enum { Value =
true }; };
513template <>
struct TIsUECoreVariant<FUint32Rect> {
enum { Value =
true }; };
515template <>
struct TIsPODType<FInt64Rect> {
enum {
Value =
true }; };
516template <>
struct TIsPODType<FUint64Rect> {
enum {
Value =
true }; };
518template <>
struct TIsUECoreVariant<FInt64Rect> {
enum { Value =
true }; };
519template <>
struct TIsUECoreVariant<FUint64Rect> {
enum { Value =
true }; };
#define UE_DEPRECATED(Version, Message)