Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
RayAABox8.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <Jolt/Math/Vec8.h>
9
11
14JPH_INLINE Vec8 RayAABox8(Vec3Arg inOrigin, const RayInvDirection &inInvDirection, Vec8Arg inBoundsMinX, Vec8Arg inBoundsMinY, Vec8Arg inBoundsMinZ, Vec8Arg inBoundsMaxX, Vec8Arg inBoundsMaxY, Vec8Arg inBoundsMaxZ)
15{
16 // Constants
17 Vec8 flt_min = Vec8::sReplicate(-FLT_MAX);
18 Vec8 flt_max = Vec8::sReplicate(FLT_MAX);
19
20 // Origin
21 Vec8 originx = Vec8::sSplatX(Vec4(inOrigin));
22 Vec8 originy = Vec8::sSplatY(Vec4(inOrigin));
23 Vec8 originz = Vec8::sSplatZ(Vec4(inOrigin));
24
25 // Parallel
26 UVec8 parallelx = UVec8::sSplatX(inInvDirection.mIsParallel);
27 UVec8 parallely = UVec8::sSplatY(inInvDirection.mIsParallel);
28 UVec8 parallelz = UVec8::sSplatZ(inInvDirection.mIsParallel);
29
30 // Inverse direction
31 Vec8 invdirx = Vec8::sSplatX(Vec4(inInvDirection.mInvDirection));
32 Vec8 invdiry = Vec8::sSplatY(Vec4(inInvDirection.mInvDirection));
33 Vec8 invdirz = Vec8::sSplatZ(Vec4(inInvDirection.mInvDirection));
34
35 // Test against all three axii simultaneously.
36 Vec8 t1x = (inBoundsMinX - originx) * invdirx;
37 Vec8 t1y = (inBoundsMinY - originy) * invdiry;
38 Vec8 t1z = (inBoundsMinZ - originz) * invdirz;
39 Vec8 t2x = (inBoundsMaxX - originx) * invdirx;
40 Vec8 t2y = (inBoundsMaxY - originy) * invdiry;
41 Vec8 t2z = (inBoundsMaxZ - originz) * invdirz;
42
43 // Compute the max of min(t1,t2) and the min of max(t1,t2) ensuring we don't
44 // use the results from any directions parallel to the slab.
45 Vec8 t_minx = Vec8::sSelect(Vec8::sMin(t1x, t2x), flt_min, parallelx);
46 Vec8 t_miny = Vec8::sSelect(Vec8::sMin(t1y, t2y), flt_min, parallely);
47 Vec8 t_minz = Vec8::sSelect(Vec8::sMin(t1z, t2z), flt_min, parallelz);
48 Vec8 t_maxx = Vec8::sSelect(Vec8::sMax(t1x, t2x), flt_max, parallelx);
49 Vec8 t_maxy = Vec8::sSelect(Vec8::sMax(t1y, t2y), flt_max, parallely);
50 Vec8 t_maxz = Vec8::sSelect(Vec8::sMax(t1z, t2z), flt_max, parallelz);
51
52 // t_min.xyz = maximum(t_min.x, t_min.y, t_min.z);
53 Vec8 t_min = Vec8::sMax(Vec8::sMax(t_minx, t_miny), t_minz);
54
55 // t_max.xyz = minimum(t_max.x, t_max.y, t_max.z);
56 Vec8 t_max = Vec8::sMin(Vec8::sMin(t_maxx, t_maxy), t_maxz);
57
58 // if (t_min > t_max) return FLT_MAX;
59 UVec8 no_intersection = Vec8::sGreater(t_min, t_max);
60
61 // if (t_max < 0.0f) return FLT_MAX;
62 no_intersection = UVec8::sOr(no_intersection, Vec8::sLess(t_max, Vec8::sZero()));
63
64 // if bounds are invalid return FLOAT_MAX;
65 UVec8 bounds_invalid = UVec8::sOr(UVec8::sOr(Vec8::sGreater(inBoundsMinX, inBoundsMaxX), Vec8::sGreater(inBoundsMinY, inBoundsMaxY)), Vec8::sGreater(inBoundsMinZ, inBoundsMaxZ));
66 no_intersection = UVec8::sOr(no_intersection, bounds_invalid);
67
68 // if (inInvDirection.mIsParallel && !(Min <= inOrigin && inOrigin <= Max)) return FLT_MAX; else return t_min;
69 UVec8 no_parallel_overlapx = UVec8::sAnd(parallelx, UVec8::sOr(Vec8::sLess(originx, inBoundsMinX), Vec8::sGreater(originx, inBoundsMaxX)));
70 UVec8 no_parallel_overlapy = UVec8::sAnd(parallely, UVec8::sOr(Vec8::sLess(originy, inBoundsMinY), Vec8::sGreater(originy, inBoundsMaxY)));
71 UVec8 no_parallel_overlapz = UVec8::sAnd(parallelz, UVec8::sOr(Vec8::sLess(originz, inBoundsMinZ), Vec8::sGreater(originz, inBoundsMaxZ)));
72 no_intersection = UVec8::sOr(no_intersection, UVec8::sOr(UVec8::sOr(no_parallel_overlapx, no_parallel_overlapy), no_parallel_overlapz));
73 return Vec8::sSelect(t_min, flt_max, no_intersection);
74}
75
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
JPH_NAMESPACE_BEGIN JPH_INLINE Vec8 RayAABox8(Vec3Arg inOrigin, const RayInvDirection &inInvDirection, Vec8Arg inBoundsMinX, Vec8Arg inBoundsMinY, Vec8Arg inBoundsMinZ, Vec8Arg inBoundsMaxX, Vec8Arg inBoundsMaxY, Vec8Arg inBoundsMaxZ)
Definition: RayAABox8.h:14
Helper structure holding the reciprocal of a ray for Ray vs AABox testing.
Definition: RayAABox.h:11
UVec4 mIsParallel
for each component if it is parallel to the coordinate axis
Definition: RayAABox.h:28
Vec3 mInvDirection
1 / ray direction
Definition: RayAABox.h:27
Definition: UVec8.h:12
static JPH_INLINE UVec8 sSplatZ(UVec4Arg inV)
Replicate the Z component of inV to all components.
Definition: UVec8.inl:32
static JPH_INLINE UVec8 sAnd(UVec8Arg inV1, UVec8Arg inV2)
Logical and.
Definition: UVec8.inl:61
static JPH_INLINE UVec8 sSplatY(UVec4Arg inV)
Replicate the Y component of inV to all components.
Definition: UVec8.inl:27
static JPH_INLINE UVec8 sSplatX(UVec4Arg inV)
Replicate the X component of inV to all components.
Definition: UVec8.inl:22
static JPH_INLINE UVec8 sOr(UVec8Arg inV1, UVec8Arg inV2)
Logical or.
Definition: UVec8.inl:51
Definition: Vec3.h:16
Definition: Vec4.h:14
Definition: Vec8.h:12
static JPH_INLINE UVec8 sLess(Vec8Arg inV1, Vec8Arg inV2)
Less than.
Definition: Vec8.inl:63
static JPH_INLINE Vec8 sSplatX(Vec4Arg inV)
Replicate the X component of inV to all components.
Definition: Vec8.inl:24
static JPH_INLINE Vec8 sSplatY(Vec4Arg inV)
Replicate the Y component of inV to all components.
Definition: Vec8.inl:29
static JPH_INLINE Vec8 sMax(Vec8Arg inV1, Vec8Arg inV2)
Component wise max.
Definition: Vec8.inl:58
static JPH_INLINE UVec8 sGreater(Vec8Arg inV1, Vec8Arg inV2)
Greater than.
Definition: Vec8.inl:68
static JPH_INLINE Vec8 sZero()
Vector with all zeros.
Definition: Vec8.inl:14
static JPH_INLINE Vec8 sSelect(Vec8Arg inV1, Vec8Arg inV2, UVec8Arg inControl)
Component wise select, returns inV1 when highest bit of inControl = 0 and inV2 when highest bit of in...
Definition: Vec8.inl:48
static JPH_INLINE Vec8 sMin(Vec8Arg inV1, Vec8Arg inV2)
Component wise min.
Definition: Vec8.inl:53
static JPH_INLINE Vec8 sSplatZ(Vec4Arg inV)
Replicate the Z component of inV to all components.
Definition: Vec8.inl:34
static JPH_INLINE Vec8 sReplicate(float inV)
Replicate across all components.
Definition: Vec8.inl:19