UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
OrientedBoxTypes.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// ported from geometry3Sharp Box3
4
5#pragma once
6
7#include "Math/OrientedBox.h"
8#include "VectorTypes.h"
9#include "BoxTypes.h"
10#include "FrameTypes.h"
11
12namespace UE
13{
14namespace Geometry
15{
16
17using namespace UE::Math;
18
23template<typename RealType>
25{
26 // available for porting: ContainPoint
27
28
33
34 TOrientedBox3() : Extents(1,1,1) {}
35
40 : Frame(Origin), Extents(ExtentsIn)
41 {
42 }
43
51
59
60 // Convert to Core Math's FOrientedBox
61 explicit operator FOrientedBox() const
62 {
64 ToRet.Center = (FVector)Frame.Origin;
65 ToRet.AxisX = (FVector)Frame.X();
66 ToRet.AxisY = (FVector)Frame.Y();
67 ToRet.AxisZ = (FVector)Frame.Z();
68 ToRet.ExtentX = (double)Extents.X;
69 ToRet.ExtentY = (double)Extents.Y;
70 ToRet.ExtentZ = (double)Extents.Z;
71 return ToRet;
72 }
73
74 // Convert from Core Math's FOrientedBox
76 Frame(TVector<RealType>(Box.Center),
77 TVector<RealType>(Box.AxisX),
78 TVector<RealType>(Box.AxisY),
79 TVector<RealType>(Box.AxisZ)),
80 Extents((RealType)Box.ExtentX, (RealType)Box.ExtentY, (RealType)Box.ExtentZ)
81 {
82 }
83
86
89
90
92 TVector<RealType> Center() const { return Frame.Origin; }
93
95 TVector<RealType> AxisX() const { return Frame.X(); }
96
98 TVector<RealType> AxisY() const { return Frame.Y(); }
99
101 TVector<RealType> AxisZ() const { return Frame.Z(); }
102
104 TVector<RealType> GetAxis(int AxisIndex) const { return Frame.GetAxis(AxisIndex); }
105
108 {
109 return Frame.PointAt(Extents.X, Extents.Y, Extents.Z) - Frame.PointAt(-Extents.X, -Extents.Y, -Extents.Z);
110 }
111
113 inline RealType Volume() const
114 {
115 return (RealType)8 * FMath::Max(0, Extents.X) * FMath::Max(0, Extents.Y) * FMath::Max(0, Extents.Z);
116 }
117
119 inline RealType SurfaceArea() const
120 {
121 TVector<RealType> ClampExtents(FMath::Max(0, Extents.X), FMath::Max(0, Extents.Y), FMath::Max(0, Extents.Z));
122 return (RealType)8 * (ClampExtents.X * ClampExtents.Y + ClampExtents.X * ClampExtents.Z + ClampExtents.Y * ClampExtents.Z);
123 }
124
133
135 bool IsValid() const
136 {
137 return Extents.X >= 0 && Extents.Y >= 0 && Extents.Z >= 0;
138 }
139
140 // corners [ (-x,-y), (x,-y), (x,y), (-x,y) ], -z, then +z
141 //
142 // 7---6 +z or 3---2 -z
143 // |\ |\ |\ |\
144 // 4-\-5 \ 0-\-1 \
145 // \ 3---2 \ 7---6
146 // \| | \| |
147 // 0---1 -z 4---5 +z
148 //
149 // @todo does this ordering make sense for UE? we are in LHS instead of RHS here
150 // if this is modified, likely need to update IndexUtil::BoxFaces and BoxFaceNormals
151
157 {
158 check(Index >= 0 && Index <= 7);
159 RealType dx = (((Index & 1) != 0) ^ ((Index & 2) != 0)) ? (Extents.X) : (-Extents.X);
160 RealType dy = ((Index / 2) % 2 == 0) ? (-Extents.Y) : (Extents.Y);
161 RealType dz = (Index < 4) ? (-Extents.Z) : (Extents.Z);
162 return Frame.PointAt(dx, dy, dz);
163 }
164
169 template<typename PointFuncType>
183
184
189 template<typename PointPredicateType>
203
204
205
212 {
213 check(Index >= 0 && Index <= 7);
214 return FIndex3i(
215 (((Index & 1) != 0) ^ ((Index & 2) != 0)) ? 1 : 0,
216 ((Index / 2) % 2 == 0) ? 0 : 1,
217 (Index < 4) ? 0 : 1
218 );
219 }
220
221
222
229 {
230 TVector<RealType> Local = Frame.ToFramePoint(Point);
231 TVector<RealType> BeyondExtents = Local.GetAbs() - Extents;
232 TVector<RealType> ClampedBeyond( // clamp negative (inside) to zero
233 FMath::Max((RealType)0, BeyondExtents.X),
234 FMath::Max((RealType)0, BeyondExtents.Y),
235 FMath::Max((RealType)0, BeyondExtents.Z));
236 return ClampedBeyond.SquaredLength();
237 }
238
245 {
246 TVector<RealType> Local = Frame.ToFramePoint(Point);
247 TVector<RealType> VsExtents = Local.GetAbs() - Extents;
248 RealType MaxComponent = VsExtents.GetMax();
249 if (MaxComponent < 0) // Inside the box on every dimension
250 {
251 // The least-inside dimension gives the distance to the box surface
252 return MaxComponent;
253 }
254 else // Outside the box along at least one dimension
255 {
256 // clamp negative (inside) to zero
258 FMath::Max((RealType)0, VsExtents.X),
259 FMath::Max((RealType)0, VsExtents.Y),
260 FMath::Max((RealType)0, VsExtents.Z)
261 );
262 return ClampedBeyond.Length();
263 }
264 }
265
272 {
273 TMatrix3<RealType> RotMatrix = Frame.Rotation.ToRotationMatrix();
275 // Transform to local space
276 TVector<RealType> Local(RotMatrix.TransformByTranspose(FromOrigin));
277 // Clamp to box
278 Local.X = FMath::Clamp(Local.X, -Extents.X, Extents.X);
279 Local.Y = FMath::Clamp(Local.Y, -Extents.Y, Extents.Y);
280 Local.Z = FMath::Clamp(Local.Z, -Extents.Z, Extents.Z);
281 // Transform back
282 return Frame.Origin + RotMatrix * Local;
283 }
284
285
286 // Create a merged TOrientedBox, encompassing this box and one other.
287 // Uses heuristics to test a few possible orientations and pick the smallest-volume result. Not an optimal fit.
288 // @param Other The box to merge with
289 // @param bOnlyConsiderExistingAxes If true, the merged box will always use the Rotation of one of the input boxes
291
297
304
311};
312
313template<typename RealType>
315{
316 // Center of the box
318 // X Axis of the box -- must be normalized
322
323 TOrientedBox2() : Origin(0, 0), UnitAxisX(1, 0), Extents(1, 1) {}
324
332
344
352
360
362 {
363 UnitAxisX = TVector2<RealType>(FMath::Cos(AngleRad), FMath::Sin(AngleRad));
364 }
365
368
371
372
374 inline TVector2<RealType> Center() const { return Origin; }
375
377 inline TVector2<RealType> AxisX() const { return UnitAxisX; }
378
381
383 inline TVector2<RealType> GetAxis(int AxisIndex) const { return AxisIndex == 0 ? AxisX() : AxisY(); }
384
387 {
388 return (AxisX() * Extents.X + AxisY() * Extents.Y) * (RealType)2;
389 }
390
392 inline RealType Area() const
393 {
394 return (RealType)4 * FMath::Max(0, Extents.X) * FMath::Max(0, Extents.Y);
395 }
396
398 inline RealType Perimeter() const
399 {
400 return (RealType)4 * (FMath::Max(0, Extents.X) + FMath::Max(0, Extents.Y));
401 }
402
409
412 {
413 return Origin + Point.X * UnitAxisX + Point.Y * AxisY();
414 }
415
423
424
426
433 {
434 check(Index >= 0 && Index <= 3);
435 RealType DX = RealType(int((Index == 1) | (Index == 2)) * 2 - 1) * Extents.X; // X positive at indices 1, 2
436 RealType DY = RealType((Index & 2) - 1) * Extents.Y; // Y positive at indices 2, 3
437 return Origin + DX * UnitAxisX + DY * AxisY();
438 }
439
443 template<typename PointFuncType>
453
458 template<typename PointPredicateType>
460 {
462
463 return
464 CornerPointFunc(Origin - X - Y) &&
465 CornerPointFunc(Origin + X - Y) &&
466 CornerPointFunc(Origin + X + Y) &&
468 }
469
476 {
477 check(Index >= 0 && Index <= 3);
478 return FIndex2i(
479 int((Index == 1) | (Index == 2)),
480 (Index & 2) >> 1
481 );
482 }
483
484
485
492 {
494 TVector2<RealType> ClampedBeyond( // clamp negative (inside) to zero
495 FMath::Max((RealType)0, BeyondExtents.X),
496 FMath::Max((RealType)0, BeyondExtents.Y));
497 return ClampedBeyond.SquaredLength();
498 }
499
506 {
508 Local.X = FMath::Clamp(Local.X, -Extents.X, Extents.X);
509 Local.Y = FMath::Clamp(Local.Y, -Extents.Y, Extents.Y);
510 return FromLocalSpace(Local);
511 }
512
513};
514
519
520} // end namespace UE::Geometry
521} // end namespace UE
#define check(expr)
Definition AssertionMacros.h:314
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 FVector
Definition IOSSystemIncludes.h:8
Definition MathUtil.h:150
static RealType Abs(const RealType Value)
Definition MathUtil.h:215
TOrientedBox2< double > FOrientedBox2d
Definition OrientedBoxTypes.h:516
TOrientedBox2< float > FOrientedBox2f
Definition OrientedBoxTypes.h:515
TOrientedBox3< double > FOrientedBox3d
Definition OrientedBoxTypes.h:518
TOrientedBox3< float > FOrientedBox3f
Definition OrientedBoxTypes.h:517
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
U16 Index
Definition radfft.cpp:71
Definition UnrealMathUtility.h:270
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592
Definition OrientedBox.h:13
FVector Center
Definition OrientedBox.h:15
Definition IndexTypes.h:27
Definition IndexTypes.h:158
Definition BoxTypes.h:637
Definition BoxTypes.h:247
Definition FrameTypes.h:27
Definition MatrixTypes.h:17
Definition OrientedBoxTypes.h:315
TVector2< RealType > Center() const
Definition OrientedBoxTypes.h:374
TVector2< RealType > Diagonal() const
Definition OrientedBoxTypes.h:386
bool Contains(const TVector2< RealType > &Point) const
Definition OrientedBoxTypes.h:417
RealType Perimeter() const
Definition OrientedBoxTypes.h:398
RealType Area() const
Definition OrientedBoxTypes.h:392
void EnumerateCorners(PointFuncType CornerPointFunc) const
Definition OrientedBoxTypes.h:444
static TOrientedBox2< RealType > UnitPositive()
Definition OrientedBoxTypes.h:370
TVector2< RealType > Extents
Definition OrientedBoxTypes.h:321
TOrientedBox2(const TVector2< RealType > &OriginIn, RealType AngleRad, const TVector2< RealType > &ExtentsIn)
Definition OrientedBoxTypes.h:348
void SetAngleRadians(RealType AngleRad)
Definition OrientedBoxTypes.h:361
TVector2< RealType > ToLocalSpace(const TVector2< RealType > &Point) const
Definition OrientedBoxTypes.h:404
TVector2< RealType > AxisY() const
Definition OrientedBoxTypes.h:380
TVector2< RealType > GetCorner(int Index) const
Definition OrientedBoxTypes.h:432
static FIndex2i GetCornerSide(int Index)
Definition OrientedBoxTypes.h:475
TOrientedBox2(const TAxisAlignedBox2< RealType > &AxisBox)
Definition OrientedBoxTypes.h:356
TVector2< RealType > FromLocalSpace(const TVector2< RealType > &Point) const
Definition OrientedBoxTypes.h:411
TOrientedBox2()
Definition OrientedBoxTypes.h:323
static TOrientedBox2< RealType > UnitZeroCentered()
Definition OrientedBoxTypes.h:367
TVector2< RealType > UnitAxisX
Definition OrientedBoxTypes.h:319
TVector2< RealType > GetAxis(int AxisIndex) const
Definition OrientedBoxTypes.h:383
TVector2< RealType > Origin
Definition OrientedBoxTypes.h:317
RealType DistanceSquared(const TVector2< RealType > &Point) const
Definition OrientedBoxTypes.h:491
TVector2< RealType > ClosestPoint(const TVector2< RealType > &Point) const
Definition OrientedBoxTypes.h:505
bool TestCorners(PointPredicateType CornerPointPredicate) const
Definition OrientedBoxTypes.h:459
TOrientedBox2(const TVector2< RealType > &OriginIn, const TVector2< RealType > &ExtentsIn)
Definition OrientedBoxTypes.h:328
TOrientedBox2(const TVector2< RealType > &OriginIn, const TVector2< RealType > &XAxisIn, const TVector2< RealType > &ExtentsIn)
Definition OrientedBoxTypes.h:336
TVector2< RealType > AxisX() const
Definition OrientedBoxTypes.h:377
Definition OrientedBoxTypes.h:25
TVector< RealType > GetCorner(int Index) const
Definition OrientedBoxTypes.h:156
RealType Volume() const
Definition OrientedBoxTypes.h:113
TVector< RealType > Diagonal() const
Definition OrientedBoxTypes.h:107
bool IsValid() const
Definition OrientedBoxTypes.h:135
RealType SurfaceArea() const
Definition OrientedBoxTypes.h:119
bool TestCorners(PointPredicateType CornerPointPredicate) const
Definition OrientedBoxTypes.h:190
TVector< RealType > GetAxis(int AxisIndex) const
Definition OrientedBoxTypes.h:104
TVector< RealType > Extents
Definition OrientedBoxTypes.h:32
TOrientedBox3(const FOrientedBox &Box)
Definition OrientedBoxTypes.h:75
RealType DistanceSquared(TVector< RealType > Point) const
Definition OrientedBoxTypes.h:228
void EnumerateCorners(PointFuncType CornerPointFunc) const
Definition OrientedBoxTypes.h:170
TVector< RealType > Center() const
Definition OrientedBoxTypes.h:92
static FIndex3i GetCornerSide(int Index)
Definition OrientedBoxTypes.h:211
TOrientedBox3()
Definition OrientedBoxTypes.h:34
TOrientedBox3(const TFrame3< RealType > &FrameIn, const TVector< RealType > &ExtentsIn)
Definition OrientedBoxTypes.h:47
GEOMETRYCORE_API TOrientedBox3< RealType > ReparameterizedCloserToWorldFrame() const
Definition OrientedBoxTypes.cpp:334
bool Contains(const TVector< RealType > &Point) const
Definition OrientedBoxTypes.h:126
TOrientedBox3(const TVector< RealType > &Origin, const TVector< RealType > &ExtentsIn)
Definition OrientedBoxTypes.h:39
RealType SignedDistance(TVector< RealType > Point) const
Definition OrientedBoxTypes.h:244
static TOrientedBox3< RealType > UnitZeroCentered()
Definition OrientedBoxTypes.h:85
TVector< RealType > AxisY() const
Definition OrientedBoxTypes.h:98
TOrientedBox3(const TAxisAlignedBox3< RealType > &AxisBox)
Definition OrientedBoxTypes.h:55
TVector< RealType > ClosestPoint(const TVector< RealType > &Point) const
Definition OrientedBoxTypes.h:271
TFrame3< RealType > Frame
Definition OrientedBoxTypes.h:30
TVector< RealType > AxisZ() const
Definition OrientedBoxTypes.h:101
TVector< RealType > AxisX() const
Definition OrientedBoxTypes.h:95
static TOrientedBox3< RealType > UnitPositive()
Definition OrientedBoxTypes.h:88
Definition Vector2D.h:38
bool Normalize(T Tolerance=UE_SMALL_NUMBER)
Definition Vector2D.h:1154
T Y
Definition Vector2D.h:52
T Dot(const TVector2< T > &V2) const
Definition Vector2D.h:1123
UE_FORCEINLINE_HINT TVector2< T > GetAbs() const
Definition Vector2D.h:1296
T X
Definition Vector2D.h:49
Definition Vector.h:51
T Z
Definition Vector.h:68
T Y
Definition Vector.h:65
T GetMax() const
Definition Vector.h:1674
T X
Definition Vector.h:62