MeshLib C++ Docs
Loading...
Searching...
No Matches
MRRayBoxIntersection.h
Go to the documentation of this file.
1#pragma once
2#include "MRBox.h"
4#include "MRMesh/MRMacros.h"
5#include "MRPch/MRBindingMacros.h"
6#include <cassert>
7
8namespace MR
9{
10
14
15template<typename T>
17{
19 MR_BIND_IGNORE Vector3<T> p;
20 RayOrigin( const Vector3<T> & ro ) : p( ro ) { }
21};
22
23#ifdef MR_SIMD4_HW
24template<>
25struct RayOrigin<float>
26{
27 MR_BIND_IGNORE Simd4::F p;
28 RayOrigin( const Vector3f & ro ) : p( Simd4::make( ro.x, ro.y, ro.z, 0 ) ) { }
29};
30#endif
31
36template <typename T = float>
37bool rayBoxIntersect( const Box3<T>& box, const RayOrigin<T> & rayOrigin, T & t0, T & t1, const IntersectionPrecomputes<T>& prec )
38{
39 assert( box.valid() );
40
41#ifdef MR_SIMD4_HW
42 if constexpr ( std::is_same_v<T, float> )
43 {
44 using namespace Simd4;
46 static_assert( sizeof( Box3f ) == 6 * sizeof( float ) );
47 const float * const c = &box.min.x;
48 const F l = mul( sub( loadu( c ), rayOrigin.p ), prec.invDir );
49 const F r = mul( sub( shiftDown( loadu( c + 2 ) ), rayOrigin.p ), prec.invDir );
51 t0 = max3( minFast( l, r ), t0 );
52 t1 = min3( maxFast( l, r ), t1 );
53 return t0 <= t1;
54 }
55 else
56#endif
57 {
58 const Vector3i& sign = prec.sign;
59
61 t1 = std::min( (box[sign.x].x - rayOrigin.p.x) * prec.invDir.x, t1 );
62 t0 = std::max( (box[1 - sign.x].x - rayOrigin.p.x) * prec.invDir.x, t0 );
63
65 t1 = std::min( (box[sign.y].y - rayOrigin.p.y) * prec.invDir.y, t1 );
66 t0 = std::max( (box[1 - sign.y].y - rayOrigin.p.y) * prec.invDir.y, t0 );
67
69 t1 = std::min( (box[sign.z].z - rayOrigin.p.z) * prec.invDir.z, t1 );
70 t0 = std::max( (box[1 - sign.z].z - rayOrigin.p.z) * prec.invDir.z, t0 );
71 return t0 <= t1;
72 }
73}
74
76template <typename T = float>
77bool rayBoxIntersect( const Box3<T>& box, const Line3<T>& line, T t0, T t1 )
78{
79 IntersectionPrecomputes<T> prec( line.d );
80 return rayBoxIntersect( box, RayOrigin<T>( line.p ), t0, t1, prec );
81}
82
84
85}
Box< Vector3< T > > Box3
Definition MRMeshFwd.h:422
Line< Vector3< T > > Line3
Definition MRMeshFwd.h:340
bool rayBoxIntersect(const Box3< T > &box, const RayOrigin< T > &rayOrigin, T &t0, T &t1, const IntersectionPrecomputes< T > &prec)
Definition MRRayBoxIntersection.h:37
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8
V min
Definition MRBox.h:44
bool valid() const
true if the box contains at least one point
Definition MRBox.h:75
Definition MRIntersectionPrecomputes.h:114
Vector3i sign
stores signs of direction vector;
Definition MRIntersectionPrecomputes.h:124
Vector3< T > invDir
{1 / dir}
Definition MRIntersectionPrecomputes.h:116
Definition MRRayBoxIntersection.h:17
RayOrigin(const Vector3< T > &ro)
Definition MRRayBoxIntersection.h:20
Vector3< T > p
This is hidden to match the specialization below.
Definition MRRayBoxIntersection.h:19
Definition MRVector3.h:33