Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
IntRect.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 "Containers/UnrealString.h"
8#include "Math/IntPoint.h"
9#include "Math/Vector2D.h"
10
11namespace UE::Math
12{
13
14/**
15 * Structure for integer rectangles in 2-d space.
16 *
17 * @todo Docs: The operators need better documentation, i.e. what does it mean to divide a rectangle?
18 */
19template <typename InIntType>
20struct TIntRect
21{
22 using IntType = InIntType;
23 using IntPointType = TIntPoint<IntType>;
24 static_assert(std::is_integral_v<IntType>, "Only an integer types are supported.");
25
26 union
27 {
28 struct
29 {
30 /** Holds the first pixel line/row (like in Win32 RECT). */
31 IntPointType Min;
32
33 /** Holds the last pixel line/row (like in Win32 RECT). */
34 IntPointType Max;
35 };
36
37 UE_DEPRECATED(all, "For internal use only")
38 IntPointType MinMax[2];
39 };
40
41 /** Constructor */
42 TIntRect()
43 : Min(ForceInit)
44 , Max(ForceInit)
45 {}
46
47 /**
48 * Constructor
49 *
50 * @param X0 Minimum X coordinate.
51 * @param Y0 Minimum Y coordinate.
52 * @param X1 Maximum X coordinate.
53 * @param Y1 Maximum Y coordinate.
54 */
55 TIntRect(IntType X0, IntType Y0, IntType X1, IntType Y1)
56 : Min(X0, Y0)
57 , Max(X1, Y1)
58 {
59 }
60
61 /**
62 * Constructor
63 *
64 * @param InMin Minimum Point
65 * @param InMax Maximum Point
66 */
67 TIntRect(IntPointType InMin, IntPointType InMax)
68 : Min(InMin)
69 , Max(InMax)
70 {
71 }
72
73 TIntRect(const TIntRect& Other)
74 {
75 *this = Other;
76 }
77
78 TIntRect& operator=(const TIntRect& Other)
79 {
80 Min = Other.Min;
81 Max = Other.Max;
82 return *this;
83 }
84
85 /**
86 * Converts to another int type. Checks that the cast will succeed.
87 */
88 template <typename OtherIntType>
89 explicit TIntRect(TIntRect<OtherIntType> Other)
90 : Min(IntPointType(Other.Min))
91 , Max(IntPointType(Other.Max))
92 {
93 }
94
95 /**
96 * Gets a specific point in this rectangle.
97 *
98 * @param PointIndex Index of Point in rectangle.
99 * @return Const reference to point in rectangle.
100 */
101 const TIntRect& operator()(int32 PointIndex) const
102 {
104 return MinMax[PointIndex];
106 }
107
108 /**
109 * Gets a specific point in this rectangle.
110 *
111 * @param PointIndex Index of Point in rectangle.
112 * @return Reference to point in rectangle.
113 */
114 TIntRect& operator()(int32 PointIndex)
115 {
117 return MinMax[PointIndex];
119 }
120
121 /**
122 * Compares Rectangles for equality.
123 *
124 * @param Other The Other Rectangle for comparison.
125 * @return true if the rectangles are equal, false otherwise..
126 */
127 bool operator==(const TIntRect& Other) const
128 {
129 return Min == Other.Min && Max == Other.Max;
130 }
131
132 /**
133 * Compares Rectangles for inequality.
134 *
135 * @param Other The Other Rectangle for comparison.
136 * @return true if the rectangles are not equal, false otherwise..
137 */
138 bool operator!=(const TIntRect& Other) const
139 {
140 return Min != Other.Min || Max != Other.Max;
141 }
142
143 /**
144 * Applies scaling to this rectangle.
145 *
146 * @param Scale What to multiply the rectangle by.
147 * @return Reference to this rectangle after scaling.
148 */
149 TIntRect& operator*=(IntType Scale)
150 {
151 Min *= Scale;
152 Max *= Scale;
153
154 return *this;
155 }
156
157 /**
158 * Adds a point to this rectangle.
159 *
160 * @param Point The point to add onto both points in the rectangle.
161 * @return Reference to this rectangle after addition.
162 */
163 TIntRect& operator+=(const IntPointType& Point)
164 {
165 Min += Point;
166 Max += Point;
167
168 return *this;
169 }
170
171 /**
172 * Subtracts a point from this rectangle.
173 *
174 * @param Point The point to subtract from both points in the rectangle.
175 * @return Reference to this rectangle after subtraction.
176 */
177 TIntRect& operator-=(const IntPointType& Point)
178 {
179 Min -= Point;
180 Max -= Point;
181
182 return *this;
183 }
184
185 /**
186 * Gets the result of scaling on this rectangle.
187 *
188 * @param Scale What to multiply this rectangle by.
189 * @return New scaled rectangle.
190 */
191 TIntRect operator*(IntType Scale) const
192 {
193 return TIntRect(Min * Scale, Max * Scale);
194 }
195
196 /**
197 * Gets the result of division on this rectangle.
198 *
199 * @param Div What to divide this rectangle by.
200 * @return New divided rectangle.
201 */
202 TIntRect operator/(IntType Div) const
203 {
204 return TIntRect(Min / Div, Max / Div);
205 }
206
207 /**
208 * Gets the result of adding a point to this rectangle.
209 *
210 * @param Point The point to add to both points in the rectangle.
211 * @return New rectangle with point added to it.
212 */
213 TIntRect operator+(const IntPointType& Point) const
214 {
215 return TIntRect(Min + Point, Max + Point);
216 }
217
218 /**
219 * Gets the result of dividing a point with this rectangle.
220 *
221 * @param Point The point to divide with.
222 * @return New rectangle with point divided.
223 */
224 TIntRect operator/(const IntPointType& Point) const
225 {
226 return TIntRect(Min / Point, Max / Point);
227 }
228
229 /**
230 * Gets the result of subtracting a point from this rectangle.
231 *
232 * @param Point The point to subtract from both points in the rectangle.
233 * @return New rectangle with point subtracted from it.
234 */
235 TIntRect operator-(const IntPointType& Point) const
236 {
237 return TIntRect(Min - Point, Max - Point);
238 }
239
240 /**
241 * Gets the result of adding two rectangles together.
242 *
243 * @param Other The other rectangle to add to this.
244 * @return New rectangle after both are added together.
245 */
246 TIntRect operator+(const TIntRect& Other) const
247 {
248 return TIntRect(Min + Other.Min, Max + Other.Max);
249 }
250
251 /**
252 * Gets the result of subtracting a rectangle from this one.
253 *
254 * @param Other The other rectangle to subtract from this.
255 * @return New rectangle after one is subtracted from this.
256 */
257 TIntRect operator-(const TIntRect& Other) const
258 {
259 return TIntRect(Min - Other.Min, Max - Other.Max);
260 }
261
262 /**
263 * Calculates the area of this rectangle.
264 *
265 * @return The area of this rectangle.
266 */
267 IntType Area() const
268 {
269 return (Max.X - Min.X) * (Max.Y - Min.Y);
270 }
271
272 /**
273 * Creates a rectangle from the bottom part of this rectangle.
274 *
275 * @param InHeight Height of the new rectangle (<= rectangles original height).
276 * @return The new rectangle.
277 */
278
279 TIntRect Bottom(IntType InHeight) const
280 {
281 return TIntRect(Min.X, FMath::Max(Min.Y, Max.Y - InHeight), Max.X, Max.Y);
282 }
283
284 /**
285 * Clip a rectangle using the bounds of another rectangle.
286 *
287 * @param Other The other rectangle to clip against.
288 */
289 void Clip(const TIntRect& R)
290 {
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);
295
296 // return zero area if not overlapping
297 Max.X = FMath::Max<IntType>(Min.X, Max.X);
298 Max.Y = FMath::Max<IntType>(Min.Y, Max.Y);
299 }
300
301 /** Combines the two rectanges. */
302 void Union(const TIntRect& R)
303 {
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);
308 }
309
310 /** Returns true if the two rects have any overlap. */
311 bool Intersect(const TIntRect& Other) const
312 {
313 return Other.Min.X < Max.X&& Other.Max.X > Min.X && Other.Min.Y < Max.Y&& Other.Max.Y > Min.Y;
314 }
315
316 /**
317 * Test whether this rectangle contains a point.
318 *
319 * @param Point The point to test against.
320 * @return true if the rectangle contains the specified point,, false otherwise..
321 */
322 bool Contains(IntPointType P) const
323 {
324 return P.X >= Min.X && P.X < Max.X&& P.Y >= Min.Y && P.Y < Max.Y;
325 }
326
327 /**
328 * Gets the Center and Extents of this rectangle.
329 *
330 * @param OutCenter Will contain the center point.
331 * @param OutExtent Will contain the extent.
332 */
333 void GetCenterAndExtents(IntPointType& OutCenter, IntPointType& OutExtent) const
334 {
335 OutExtent.X = (Max.X - Min.X) / 2;
336 OutExtent.Y = (Max.Y - Min.Y) / 2;
337
338 OutCenter.X = Min.X + OutExtent.X;
339 OutCenter.Y = Min.Y + OutExtent.Y;
340 }
341
342 /**
343 * Gets the Height of the rectangle.
344 *
345 * @return The Height of the rectangle.
346 */
347 IntType Height() const
348 {
349 return (Max.Y - Min.Y);
350 }
351
352
353 /**
354 * Inflates or deflates the rectangle.
355 *
356 * @param Amount The amount to inflate or deflate the rectangle on each side.
357 */
358 void InflateRect(IntType Amount)
359 {
360 Min.X -= Amount;
361 Min.Y -= Amount;
362 Max.X += Amount;
363 Max.Y += Amount;
364 }
365 /**
366 * Adds to this rectangle to include a given point.
367 *
368 * @param Point The point to increase the rectangle to.
369 */
370 void Include(IntPointType Point)
371 {
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);
376 }
377
378 /**
379 * Gets a new rectangle from the inner of this one.
380 *
381 * @param Shrink How much to remove from each point of this rectangle.
382 * @return New inner Rectangle.
383 */
384 TIntRect Inner(IntPointType Shrink) const
385 {
386 return TIntRect(Min + Shrink, Max - Shrink);
387 }
388 /**
389 * Creates a rectangle from the right hand side of this rectangle.
390 *
391 * @param InWidth Width of the new rectangle (<= rectangles original width).
392 * @return The new rectangle.
393 */
394 TIntRect Right(IntType InWidth) const
395 {
396 return TIntRect(FMath::Max(Min.X, Max.X - InWidth), Min.Y, Max.X, Max.Y);
397 }
398
399 /**
400 * Scales a rectangle using a floating point number.
401 *
402 * @param Fraction What to scale the rectangle by
403 * @return New scaled rectangle.
404 */
405 TIntRect Scale(double Fraction) const
406 {
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;
410
411 return TIntRect(
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))
416 );
417 }
418
419 /**
420 * Gets the distance from one corner of the rectangle to the other.
421 *
422 * @return The distance from one corner of the rectangle to the other.
423 */
424 IntPointType Size() const
425 {
426 return IntPointType(Max.X - Min.X, Max.Y - Min.Y);
427 }
428
429 /**
430 * Get a textual representation of this rectangle.
431 *
432 * @return A string describing the rectangle.
433 */
434 FString ToString() const
435 {
436 return FString::Printf(TEXT("Min=(%s) Max=(%s)"), *Min.ToString(), *Max.ToString());
437 }
438
439 /**
440 * Gets the width of the rectangle.
441 *
442 * @return The width of the rectangle.
443 */
444 IntType Width() const
445 {
446 return Max.X - Min.X;
447 }
448
449 /**
450 * Returns true if the rectangle is 0 x 0.
451 *
452 * @return true if the rectangle is 0 x 0.
453 */
454 bool IsEmpty() const
455 {
456 return Width() == 0 && Height() == 0;
457 }
458
459 /**
460 * Divides a rectangle and rounds up to the nearest integer.
461 *
462 * @param lhs The Rectangle to divide.
463 * @param Div What to divide by.
464 * @return New divided rectangle.
465 */
466 static TIntRect DivideAndRoundUp(TIntRect lhs, IntType Div)
467 {
468 return DivideAndRoundUp(lhs, IntPointType(Div, Div));
469 }
470
471 static TIntRect DivideAndRoundUp(TIntRect lhs, IntPointType Div)
472 {
473 return TIntRect(lhs.Min / Div, IntPointType::DivideAndRoundUp(lhs.Max, Div));
474 }
475
476 /**
477 * Gets number of points in the Rectangle.
478 *
479 * @return Number of points in the Rectangle.
480 */
481 static int32 Num()
482 {
483 return 2;
484 }
485
486public:
487
488 /**
489 * Serializes the Rectangle.
490 *
491 * @param Ar The archive to serialize into.
492 * @param Rect The rectangle to serialize.
493 * @return Reference to the Archive after serialization.
494 */
495 friend FArchive& operator<<( FArchive& Ar, TIntRect& Rect )
496 {
497 return Ar << Rect.Min.X << Rect.Min.Y << Rect.Max.X << Rect.Max.Y;
498 }
499};
500
501template <typename IntType>
502uint32 GetTypeHash(const TIntRect<IntType>& InRect)
503{
504 return HashCombine(GetTypeHash(InRect.Min), GetTypeHash(InRect.Max));
505}
506
507} //! namespace UE::Math
508
509template <> struct TIsPODType<FInt32Rect> { enum { Value = true }; };
510template <> struct TIsPODType<FUint32Rect> { enum { Value = true }; };
511
512template <> struct TIsUECoreVariant<FInt32Rect> { enum { Value = true }; };
513template <> struct TIsUECoreVariant<FUint32Rect> { enum { Value = true }; };
514
515template <> struct TIsPODType<FInt64Rect> { enum { Value = true }; };
516template <> struct TIsPODType<FUint64Rect> { enum { Value = true }; };
517
518template <> struct TIsUECoreVariant<FInt64Rect> { enum { Value = true }; };
519template <> struct TIsUECoreVariant<FUint64Rect> { enum { Value = true }; };
#define UE_DEPRECATED(Version, Message)
#define PRAGMA_ENABLE_DEPRECATION_WARNINGS
#define PRAGMA_DISABLE_DEPRECATION_WARNINGS
#define TEXT(x)
Definition Platform.h:1108