Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
RayTriangle8.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>
8
10
12JPH_INLINE Vec8 RayTriangle8(Vec3Arg inOrigin, Vec3Arg inDirection, Vec8Arg inV0X, Vec8Arg inV0Y, Vec8Arg inV0Z, Vec8Arg inV1X, Vec8Arg inV1Y, Vec8Arg inV1Z, Vec8Arg inV2X, Vec8Arg inV2Y, Vec8Arg inV2Z)
13{
14 // Epsilon
15 Vec8 epsilon = Vec8::sReplicate(1.0e-12f);
16
17 // Zero & one
18 Vec8 zero = Vec8::sZero();
19 Vec8 one = Vec8::sReplicate(1.0f);
20
21 // Find vectors for two edges sharing inV0
22 Vec8 e1x = inV1X - inV0X;
23 Vec8 e1y = inV1Y - inV0Y;
24 Vec8 e1z = inV1Z - inV0Z;
25 Vec8 e2x = inV2X - inV0X;
26 Vec8 e2y = inV2Y - inV0Y;
27 Vec8 e2z = inV2Z - inV0Z;
28
29 // Get direction vector components
30 Vec8 dx = Vec8::sSplatX(Vec4(inDirection));
31 Vec8 dy = Vec8::sSplatY(Vec4(inDirection));
32 Vec8 dz = Vec8::sSplatZ(Vec4(inDirection));
33
34 // Begin calculating determinant - also used to calculate u parameter
35 Vec8 px = dy * e2z - dz * e2y;
36 Vec8 py = dz * e2x - dx * e2z;
37 Vec8 pz = dx * e2y - dy * e2x;
38
39 // if determinant is near zero, ray lies in plane of triangle
40 Vec8 det = e1x * px + e1y * py + e1z * pz;
41
42 // Check which determinants are near zero
43 UVec8 det_near_zero = Vec8::sLess(det.Abs(), epsilon);
44
45 // Set components of the determinant to 1 that are near zero to avoid dividing by zero
46 det = Vec8::sSelect(det, Vec8::sReplicate(1.0f), det_near_zero);
47
48 // Calculate distance from inV0 to ray origin
49 Vec8 sx = Vec8::sSplatX(Vec4(inOrigin)) - inV0X;
50 Vec8 sy = Vec8::sSplatY(Vec4(inOrigin)) - inV0Y;
51 Vec8 sz = Vec8::sSplatZ(Vec4(inOrigin)) - inV0Z;
52
53 // Calculate u parameter and flip sign if determinant was negative
54 Vec8 u = (sx * px + sy * py + sz * pz) / det;
55
56 // Prepare to test v parameter
57 Vec8 qx = sy * e1z - sz * e1y;
58 Vec8 qy = sz * e1x - sx * e1z;
59 Vec8 qz = sx * e1y - sy * e1x;
60
61 // Calculate v parameter and flip sign if determinant was negative
62 Vec8 v = (dx * qx + dy * qy + dz * qz) / det;
63
64 // Get intersection point and flip sign if determinant was negative
65 Vec8 t = (e2x * qx + e2y * qy + e2z * qz) / det;
66
67 // Check if there is an intersection
68 UVec8 no_intersection =
70 (
72 (
74 (
75 det_near_zero,
76 Vec8::sLess(u, zero)
77 ),
79 (
80 Vec8::sLess(v, zero),
81 Vec8::sGreater(u + v, one)
82 )
83 ),
84 Vec8::sLess(t, zero)
85 );
86
87 // Select intersection point or FLT_MAX based on if there is an intersection or not
88 return Vec8::sSelect(t, Vec8::sReplicate(FLT_MAX), no_intersection);
89}
90
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
JPH_NAMESPACE_BEGIN JPH_INLINE Vec8 RayTriangle8(Vec3Arg inOrigin, Vec3Arg inDirection, Vec8Arg inV0X, Vec8Arg inV0Y, Vec8Arg inV0Z, Vec8Arg inV1X, Vec8Arg inV1Y, Vec8Arg inV1Z, Vec8Arg inV2X, Vec8Arg inV2Y, Vec8Arg inV2Z)
Intersect ray with 8 triangles in SOA format, returns 8 vector of closest points or FLT_MAX if no hit...
Definition: RayTriangle8.h:12
Definition: UVec8.h:12
static JPH_INLINE UVec8 sOr(UVec8Arg inV1, UVec8Arg inV2)
Logical or.
Definition: UVec8.inl:51
Definition: Vec3.h:17
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
JPH_INLINE Vec8 Abs() const
Get absolute value of all components.
Definition: Vec8.inl:124
static JPH_INLINE Vec8 sSplatY(Vec4Arg inV)
Replicate the Y component of inV to all components.
Definition: Vec8.inl:29
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 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