Go to the documentation of this file.
32 #include "../../Platforms/interface/PlatformDefinitions.h"
33 #include "../../Primitives/interface/FlagEnum.h"
48 return *
reinterpret_cast<float4*
>(
this);
52 return *
reinterpret_cast<const float4*
>(
this);
80 return Planes[
static_cast<size_t>(Idx)];
87 return Planes[
static_cast<size_t>(Idx)];
168 float nearClipZ = bIsOpenGL ? -1.f : 0.f;
170 static const float3 ProjSpaceCorners[] =
173 float3(-1, -1, nearClipZ),
174 float3( 1, -1, nearClipZ),
185 for (
int i = 0; i < 8; ++i)
269 (Direction.
x > 0) ?
Box.Min.x :
Box.Max.x,
270 (Direction.
y > 0) ?
Box.Min.y :
Box.Max.y,
271 (Direction.
z > 0) ?
Box.Min.z :
Box.Max.z
280 (Direction.
x > 0) ?
Box.Max.x :
Box.Min.x,
281 (Direction.
y > 0) ?
Box.Max.y :
Box.Min.y,
282 (Direction.
z > 0) ?
Box.Max.z :
Box.Min.z
292 (Normal.
x > 0) ?
Box.Max.x :
Box.Min.x,
293 (Normal.
y > 0) ?
Box.Max.y :
Box.Min.y,
294 (Normal.
z > 0) ?
Box.Max.z :
Box.Min.z
296 float DMax =
dot(MaxPoint, Normal) + Plane.
Distance;
302 (Normal.
x > 0) ?
Box.Min.x :
Box.Max.x,
303 (Normal.
y > 0) ?
Box.Min.y :
Box.Max.y,
304 (Normal.
z > 0) ?
Box.Min.z :
Box.Max.z
306 float DMin =
dot(MinPoint, Normal) + Plane.
Distance;
345 int NumPlanesInside = 0;
349 if ((PlaneFlags & (1 << plane_idx)) == 0)
395 for (
int iBoundBoxPlane = 0; iBoundBoxPlane < 6; ++iBoundBoxPlane)
402 float CurrPlaneCoord =
reinterpret_cast<const float*
>(&
Box)[iBoundBoxPlane];
404 int iCoordOrder = iBoundBoxPlane % 3;
407 float fSign = (iBoundBoxPlane >= 3) ? +1.f : -1.f;
408 bool bAllCornersOutside =
true;
409 for (
int iCorner = 0; iCorner < 8; iCorner++)
414 if (fSign * (CurrPlaneCoord - CurrCornerCoord) > 0)
416 bAllCornersOutside =
false;
420 if (bAllCornersOutside)
433 float fdX = (Pos.
x > BndBox.
Max.
x) ? (Pos.
x - BndBox.
Max.
x) : ((Pos.
x < BndBox.
Min.
x) ? (BndBox.
Min.
x - Pos.
x) : 0.f);
434 float fdY = (Pos.
y > BndBox.
Max.
y) ? (Pos.
y - BndBox.
Max.
y) : ((Pos.
y < BndBox.
Min.
y) ? (BndBox.
Min.
y - Pos.
y) : 0.f);
435 float fdZ = (Pos.
z > BndBox.
Max.
z) ? (Pos.
z - BndBox.
Max.
z) : ((Pos.
z < BndBox.
Min.
z) ? (BndBox.
Min.
z - Pos.
z) : 0.f);
438 float3 RangeVec(fdX, fdY, fdZ);
472 template <
typename T,
typename Y>
482 return (2 * x3 - 3 * x2 + 1) * f0 + (x3 - 2 * x2 + x) * t0 + (-2 * x3 + 3 * x2) * f1 + (x3 - x2) * t1;
496 auto k2 = 1.f / (Proj_00 * Proj_00) + 1.f / (Proj_11 * Proj_11);
497 if (k2 > (FarPlane - NearPlane) / (FarPlane + NearPlane))
499 Center =
float3(0, 0, FarPlane);
500 Radius = FarPlane * std::sqrt(k2);
504 Center =
float3(0, 0, 0.5f * (FarPlane + NearPlane) * (1 + k2));
505 Radius = 0.5f * std::sqrt((FarPlane - NearPlane) * (FarPlane - NearPlane) + 2 * (FarPlane * FarPlane + NearPlane * NearPlane) * k2 + (FarPlane + NearPlane) * (FarPlane + NearPlane) * k2 * k2);
511 const float3& RayDirection,
522 static constexpr
float Epsilon = 1e-20f;
527 AbsRayDir.
x > Epsilon ? BoxMin.
x / RayDirection.
x : +FLT_MAX,
528 AbsRayDir.
y > Epsilon ? BoxMin.
y / RayDirection.
y : +FLT_MAX,
529 AbsRayDir.
z > Epsilon ? BoxMin.
z / RayDirection.
z : +FLT_MAX
533 AbsRayDir.
x > Epsilon ? BoxMax.
x / RayDirection.
x : -FLT_MAX,
534 AbsRayDir.
y > Epsilon ? BoxMax.
y / RayDirection.
y : -FLT_MAX,
535 AbsRayDir.
z > Epsilon ? BoxMax.
z / RayDirection.
z : -FLT_MAX
543 return ExitDist >= 0 && EnterDist <= ExitDist;
548 const float3& RayDirection,
558 const float2& RayDirection,
569 static constexpr
float Epsilon = 1e-20f;
574 AbsRayDir.
x > Epsilon ? BoxMin.
x / RayDirection.
x : +FLT_MAX,
575 AbsRayDir.
y > Epsilon ? BoxMin.
y / RayDirection.
y : +FLT_MAX
579 AbsRayDir.
x > Epsilon ? BoxMax.
x / RayDirection.
x : -FLT_MAX,
580 AbsRayDir.
y > Epsilon ? BoxMax.
y / RayDirection.
y : -FLT_MAX
588 return ExitDist >= 0 && EnterDist <= ExitDist;
600 const float3& RayDirection,
601 bool CullBackFace =
false)
608 float Det =
dot(V0_V1, PVec);
612 static constexpr
float Epsilon = 1e-10f;
614 if (Det > Epsilon || (!CullBackFace && Det < -Epsilon))
616 float3 V0_RO = RayOrigin - V0;
619 float u =
dot(V0_RO, PVec) / Det;
620 if (u >= 0 && u <= 1)
625 float v =
dot(RayDirection, QVec) / Det;
626 if (v >= 0 && u + v <= 1)
629 t =
dot(V0_V2, QVec) / Det;
668 template <
typename TCallback>
675 const auto f2GridSize = i2GridSize.
Recast<
float>();
677 if (f2Start == f2End)
679 if (f2Start.
x >= 0 && f2Start.
x < f2GridSize.x &&
680 f2Start.
y >= 0 && f2Start.
y < f2GridSize.y)
682 Callback(f2Start.
Recast<
int>());
687 float2 f2Direction = f2End - f2Start;
689 float EnterDist, ExitDist;
692 f2End = f2Start + f2Direction *
std::min(ExitDist, 1.f);
693 f2Start = f2Start + f2Direction *
std::max(EnterDist, 0.f);
695 f2Start =
clamp(f2Start,
float2{0, 0}, f2GridSize);
698 const int dh = f2Direction.
x > 0 ? 1 : -1;
699 const int dv = f2Direction.
y > 0 ? 1 : -1;
700 const float p = f2Direction.
y * f2Start.x - f2Direction.
x * f2Start.y;
701 const float tx = p - f2Direction.
y *
static_cast<float>(dh);
702 const float ty = p + f2Direction.
x *
static_cast<float>(dv);
705 VERIFY_EXPR(i2End.
x >= 0 && i2End.
y >= 0 && i2End.
x <= i2GridSize.
x && i2End.
y <= i2GridSize.
y);
708 VERIFY_EXPR(i2Pos.
x >= 0 && i2Pos.
y >= 0 && i2Pos.
x <= i2GridSize.
x && i2Pos.
y <= i2GridSize.
y);
713 while ((i2End.
x - i2Pos.
x) * dh >= 0 &&
714 (i2End.
y - i2Pos.
y) * dv >= 0)
716 if (i2Pos.
x < i2GridSize.
x && i2Pos.
y < i2GridSize.
y)
718 if (!Callback(i2Pos))
730 float t = f2Direction.
x * (
static_cast<float>(i2Pos.
y) + 0.5f) - f2Direction.
y * (
static_cast<float>(i2Pos.
x) + 0.5f);
753 template <
typename T,
typename IntermediateType>
762 const DirType Rib[3] =
766 IntermediateType{V1.
x} - IntermediateType{V0.
x},
767 IntermediateType{V1.
y} - IntermediateType{V0.
y},
771 IntermediateType{V2.
x} - IntermediateType{V1.
x},
772 IntermediateType{V2.
y} - IntermediateType{V1.
y},
776 IntermediateType{V0.
x} - IntermediateType{V2.
x},
777 IntermediateType{V0.
y} - IntermediateType{V2.
y},
781 const DirType VertToPoint[3] =
785 IntermediateType{Point.
x} - IntermediateType{V0.
x},
786 IntermediateType{Point.
y} - IntermediateType{V0.
y}
790 IntermediateType{Point.
x} - IntermediateType{V1.
x},
791 IntermediateType{Point.
y} - IntermediateType{V1.
y}
795 IntermediateType{Point.
x} - IntermediateType{V2.
x},
796 IntermediateType{Point.
y} - IntermediateType{V2.
y}
800 IntermediateType NormalZ[3];
801 for (
int i = 0; i < 3; i++)
803 NormalZ[i] = Rib[i].x * VertToPoint[i].y - Rib[i].y * VertToPoint[i].x;
809 return (NormalZ[0] >= 0 && NormalZ[1] >= 0 && NormalZ[2] >= 0) ||
810 (NormalZ[0] <= 0 && NormalZ[1] <= 0 && NormalZ[2] <= 0);
814 return (NormalZ[0] > 0 && NormalZ[1] > 0 && NormalZ[2] > 0) ||
815 (NormalZ[0] < 0 && NormalZ[1] < 0 && NormalZ[2] < 0);
820 template <
typename T>
827 return IsPointInsideTriangle<T, T>(V0, V1, V2, Point, AllowEdges);
856 template <
typename T,
872 const int iStartRow =
static_cast<int>(
FastCeil(V0.
y));
873 const int iEndRow =
static_cast<int>(
FastFloor(V2.
y));
875 if (iStartRow == iEndRow)
879 for (
int iCol = iStartCol; iCol <= iEndCol; ++iCol)
881 Callback(
int2{iCol, iStartRow});
886 auto LerpCol = [](T StartCol, T EndCol, T StartRow, T EndRow,
int CurrRow)
889 ((EndCol - StartCol) * (
static_cast<T
>(CurrRow) - StartRow)) / (EndRow - StartRow);
892 for (
int iRow = iStartRow; iRow <= iEndRow; ++iRow)
894 auto dStartCol = LerpCol(V0.
x, V2.
x, V0.
y, V2.
y, iRow);
897 if (
static_cast<T
>(iRow) < V1.
y)
906 dEndCol = LerpCol(V0.
x, V1.
x, V0.
y, V1.
y, iRow);
919 dEndCol = LerpCol(V1.
x, V2.
x, V1.
y, V2.
y, iRow);
932 if (dStartCol > dEndCol)
933 std::swap(dStartCol, dEndCol);
935 int iStartCol =
static_cast<int>(
FastCeil(dStartCol));
936 int iEndCol =
static_cast<int>(
FastFloor(dEndCol));
938 for (
int iCol = iStartCol; iCol <= iEndCol; ++iCol)
940 Callback(
int2{iCol, iRow});
958 template <
bool AllowTouch,
typename T>
965 Box1Max.
x >= Box1Min.
x && Box1Max.
y >= Box1Min.
y);
968 return !(Box0Min.
x > Box1Max.
x || Box1Min.
x > Box0Max.
x || Box0Min.
y > Box1Max.
y || Box1Min.
y > Box0Max.
y);
972 return !(Box0Min.
x >= Box1Max.
x || Box1Min.
x >= Box0Max.
x || Box0Min.
y >= Box1Max.
y || Box1Min.
y >= Box0Max.
y);
@ NUM_PLANES
Definition: AdvancedMath.hpp:66
@ FRUSTUM_PLANE_FLAG_TOP_PLANE
Definition: AdvancedMath.hpp:321
size_t operator()(const Diligent::ViewFrustum &Frustum) const
Definition: AdvancedMath.hpp:994
T _14
Definition: BasicMath.hpp:1073
float3 Normal
Definition: AdvancedMath.hpp:43
const Plane3D & GetPlane(PLANE_IDX Idx) const
Definition: AdvancedMath.hpp:76
Vector3< T > cross(const Vector3< T > &a, const Vector3< T > &b)
Definition: BasicMath.hpp:1731
T x
Definition: BasicMath.hpp:80
T _34
Definition: BasicMath.hpp:1081
T z
Definition: BasicMath.hpp:267
@ FRUSTUM_PLANE_FLAG_RIGHT_PLANE
Definition: AdvancedMath.hpp:319
static Vector3 MakeVector(const Y &vals)
Definition: BasicMath.hpp:440
Vector3< float > float3
Definition: BasicMath.hpp:1836
PLANE_IDX
Definition: AdvancedMath.hpp:58
@ FRUSTUM_PLANE_FLAG_OPEN_NEAR
Definition: AdvancedMath.hpp:332
float3 GetBoxFarthestCorner(const float3 &Direction, const BoundBox &Box)
Returns the farthest bounding box corner along the given direction.
Definition: AdvancedMath.hpp:276
T _11
Definition: BasicMath.hpp:1070
T _21
Definition: BasicMath.hpp:1074
T _44
Definition: BasicMath.hpp:1085
BoxVisibility GetBoxVisibility(const ViewFrustum &ViewFrustum, const BoundBox &Box, FRUSTUM_PLANE_FLAGS PlaneFlags=FRUSTUM_PLANE_FLAG_FULL_FRUSTUM)
Definition: AdvancedMath.hpp:341
Vector2< T > abs(const Vector2< T > &a)
Definition: BasicMath.hpp:1672
Matrix4x4 Inverse() const
Definition: BasicMath.hpp:1493
bool IntersectRayBox3D(const float3 &RayOrigin, const float3 &RayDirection, float3 BoxMin, float3 BoxMax, float &EnterDist, float &ExitDist)
Intersects a ray with 3D box and computes distances to intersections.
Definition: AdvancedMath.hpp:510
bool operator==(const Plane3D &p1, const Plane3D &p2)
Definition: AdvancedMath.hpp:442
auto length(const VectorType &a) -> decltype(dot(a, a))
Definition: BasicMath.hpp:1641
Definition: BasicMath.hpp:71
size_t operator()(const Diligent::Plane3D &Plane) const
Definition: AdvancedMath.hpp:985
Plane3D NearPlane
Definition: AdvancedMath.hpp:73
Box.
Definition: GraphicsTypes.h:2407
float3 FrustumCorners[8]
Definition: AdvancedMath.hpp:93
float Distance
Definition: AdvancedMath.hpp:44
T _23
Definition: BasicMath.hpp:1076
BoundBox Transform(const float4x4 &m) const
Definition: AdvancedMath.hpp:195
@ LEFT_PLANE_IDX
Definition: AdvancedMath.hpp:60
BoxVisibility GetBoxVisibilityAgainstPlane(const Plane3D &Plane, const BoundBox &Box)
Definition: AdvancedMath.hpp:286
BoxVisibility
Definition: AdvancedMath.hpp:227
T clamp(T val, T _min, T _max)
Definition: BasicMath.hpp:1700
Diligent::Vector2< T > min(const Diligent::Vector2< T > &Left, const Diligent::Vector2< T > &Right)
Definition: BasicMath.hpp:2289
Plane3D FarPlane
Definition: AdvancedMath.hpp:74
void RasterizeTriangle(Vector2< T > V0, Vector2< T > V1, Vector2< T > V2, TCallback Callback)
Rasterizes a triangle and calls the callback function for every sample covered.
Definition: AdvancedMath.hpp:858
T _33
Definition: BasicMath.hpp:1080
DEFINE_FLAG_ENUM_OPERATORS(FRUSTUM_PLANE_FLAGS)
T max3(const T &x, const T &y, const T &z)
Definition: BasicMath.hpp:2036
float3 Max
Definition: AdvancedMath.hpp:192
Plane3D & GetPlane(PLANE_IDX Idx)
Definition: AdvancedMath.hpp:83
T _32
Definition: BasicMath.hpp:1079
@ FRUSTUM_PLANE_FLAG_NEAR_PLANE
Definition: AdvancedMath.hpp:322
Plane3D RightPlane
Definition: AdvancedMath.hpp:70
struct Box Box
Definition: GraphicsTypes.h:2440
void ExtractViewFrustumPlanesFromMatrix(const float4x4 &Matrix, ViewFrustum &Frustum, bool bIsOpenGL)
Definition: AdvancedMath.hpp:106
T _31
Definition: BasicMath.hpp:1078
Definition: AdvancedMath.hpp:189
T y
Definition: BasicMath.hpp:266
void TraceLineThroughGrid(float2 f2Start, float2 f2End, int2 i2GridSize, TCallback Callback)
Traces a 2D line through the square cell grid and enumerates all cells the line touches.
Definition: AdvancedMath.hpp:669
size_t operator()(const Diligent::ViewFrustumExt &Frustum) const
Definition: AdvancedMath.hpp:1003
bool CheckBox2DBox2DOverlap(const Vector2< T > &Box0Min, const Vector2< T > &Box0Max, const Vector2< T > &Box1Min, const Vector2< T > &Box1Max)
Checks if two 2D-boxes overlap.
Definition: AdvancedMath.hpp:959
Plane3D BottomPlane
Definition: AdvancedMath.hpp:71
@ RIGHT_PLANE_IDX
Definition: AdvancedMath.hpp:61
FRUSTUM_PLANE_FLAGS
Definition: AdvancedMath.hpp:315
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Vector2< float > float2
Definition: BasicMath.hpp:1835
std::size_t ComputeHash(const ArgsType &... Args)
Definition: HashUtils.hpp:57
Definition: BasicMath.hpp:74
T _12
Definition: BasicMath.hpp:1071
Plane3D LeftPlane
Definition: AdvancedMath.hpp:69
@ FRUSTUM_PLANE_FLAG_FAR_PLANE
Definition: AdvancedMath.hpp:323
T min3(const T &x, const T &y, const T &z)
Definition: BasicMath.hpp:2042
@ FRUSTUM_PLANE_FLAG_LEFT_PLANE
Definition: AdvancedMath.hpp:318
Vector2< Y > Recast() const
Definition: BasicMath.hpp:245
@ TOP_PLANE_IDX
Definition: AdvancedMath.hpp:63
float3 Min
Definition: AdvancedMath.hpp:191
Plane3D TopPlane
Definition: AdvancedMath.hpp:72
T _42
Definition: BasicMath.hpp:1083
float IntersectRayTriangle(const float3 &V0, const float3 &V1, const float3 &V2, const float3 &RayOrigin, const float3 &RayDirection, bool CullBackFace=false)
Intersects a ray with the trianlge using Moller-Trumbore algorithm and returns the distance along the...
Definition: AdvancedMath.hpp:596
T FastFloor(T x)
Definition: BasicMath.hpp:2093
bool IntersectRayBox2D(const float2 &RayOrigin, const float2 &RayDirection, float2 BoxMin, float2 BoxMax, float &EnterDist, float &ExitDist)
Intersects a 2D ray with the 2D axis-aligned bounding box and computes distances to intersections.
Definition: AdvancedMath.hpp:557
Definition: AdvancedMath.hpp:979
Definition: AdvancedMath.hpp:91
Diligent::Vector2< T > max(const Diligent::Vector2< T > &Left, const Diligent::Vector2< T > &Right)
Definition: BasicMath.hpp:2261
T _24
Definition: BasicMath.hpp:1077
bool IntersectRayAABB(const float3 &RayOrigin, const float3 &RayDirection, const BoundBox &AABB, float &EnterDist, float &ExitDist)
Intersects a ray with the axis-aligned bounding box and computes distances to intersections.
Definition: AdvancedMath.hpp:547
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
T _22
Definition: BasicMath.hpp:1075
T _43
Definition: BasicMath.hpp:1084
@ FRUSTUM_PLANE_FLAG_BOTTOM_PLANE
Definition: AdvancedMath.hpp:320
float3 GetBoxNearestCorner(const float3 &Direction, const BoundBox &Box)
Returns the nearest bounding box corner along the given direction.
Definition: AdvancedMath.hpp:265
Definition: AdvancedMath.hpp:56
bool IsPointInsideTriangle(const Vector2< T > &V0, const Vector2< T > &V1, const Vector2< T > &V2, const Vector2< T > &Point, bool AllowEdges)
Tests if a point is inside triangle.
Definition: AdvancedMath.hpp:754
@ BOTTOM_PLANE_IDX
Definition: AdvancedMath.hpp:62
@ FRUSTUM_PLANE_FLAG_FULL_FRUSTUM
Definition: AdvancedMath.hpp:325
T _41
Definition: BasicMath.hpp:1082
T FastCeil(T x)
Definition: BasicMath.hpp:2108
void GetFrustumMinimumBoundingSphere(float Proj_00, float Proj_11, float NearPlane, float FarPlane, float3 &Center, float &Radius)
Definition: AdvancedMath.hpp:486
T HermiteSpline(T f0, T f1, T t0, T t1, Y x)
Definition: AdvancedMath.hpp:473
T x
Definition: BasicMath.hpp:265
@ FRUSTUM_PLANE_FLAG_NONE
Definition: AdvancedMath.hpp:317
T dot(const Vector2< T > &a, const Vector2< T > &b)
Definition: BasicMath.hpp:1623
void HashCombine(std::size_t &Seed, const T &Val)
Definition: HashUtils.hpp:44
@ FAR_PLANE_IDX
Definition: AdvancedMath.hpp:65
float GetPointToBoxDistance(const BoundBox &BndBox, const float3 &Pos)
Definition: AdvancedMath.hpp:428
T _13
Definition: BasicMath.hpp:1072
Definition: AdvancedMath.hpp:41
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
@ NEAR_PLANE_IDX
Definition: AdvancedMath.hpp:64
T y
Definition: BasicMath.hpp:81