MeshLib Documentation
Loading...
Searching...
No Matches
MRRayBoxIntersection.h
Go to the documentation of this file.
1#pragma once
2#include "MRBox.h"
4#include "MRPch/MRBindingMacros.h"
5
6namespace MR
7{
8
12
13template<typename T>
15{
17 RayOrigin( const Vector3<T> & ro ) : p( ro ) { }
18};
19
20/* CPU(X86_64) - AMD64 / Intel64 / x86_64 64-bit */
21#if defined(__x86_64__) || defined(_M_X64)
22template<>
23struct RayOrigin<float>
24{
25 MR_BIND_IGNORE __m128 p;
26 RayOrigin( const Vector3f & ro ) { p = _mm_set_ps( ro.x, ro.y, ro.z, 0 ); }
27};
28
32inline bool rayBoxIntersect( const Box3f& box, const RayOrigin<float> & rayOrigin, float & t0, float & t1, const IntersectionPrecomputes<float>& prec )
33{
34 __m128 l = _mm_set_ps( box.min.x, box.min.y, box.min.z, t0 );
35 __m128 r = _mm_set_ps( box.max.x, box.max.y, box.max.z, t1 );
36 l = _mm_sub_ps( l, rayOrigin.p );
37 r = _mm_sub_ps( r, rayOrigin.p );
38 l = _mm_mul_ps( l, prec.invDir );
39 r = _mm_mul_ps( r, prec.invDir );
40
41 __m128 a = _mm_min_ps( l, r );
42 __m128 b = _mm_max_ps( l, r );
43
44 __m128 aa = _mm_movehl_ps( a, a );
45 aa = _mm_max_ps( aa, a );
46 __m128 aaa = _mm_shuffle_ps( aa, aa, 1 );
47 aaa = _mm_max_ss( aaa, aa );
48 t0 = _mm_cvtss_f32( aaa );
49
50 __m128 bb = _mm_movehl_ps( b, b );
51 bb = _mm_min_ps( bb, b );
52 __m128 bbb = _mm_shuffle_ps( bb, bb, 1 );
53 bbb = _mm_min_ss( bbb, bb );
54 t1 = _mm_cvtss_f32( bbb );
55
56 return t0 <= t1;
57}
58#else
59 #pragma message("rayBoxIntersect: no hardware optimized instructions")
60#endif
61
62template<typename T>
63bool rayBoxIntersect( const Box3<T>& box, const RayOrigin<T> & rayOrigin, T & t0, T & t1, const IntersectionPrecomputes<T>& prec )
64{
65 const Vector3i& sign = prec.sign;
66
67 // compare and update x-dimension with t0-t1
68 t1 = std::min( (box[sign.x].x - rayOrigin.p.x) * prec.invDir.x, t1 );
69 t0 = std::max( (box[1 - sign.x].x - rayOrigin.p.x) * prec.invDir.x, t0 );
70
71 // compare and update y-dimension with t0-t1
72 t1 = std::min( (box[sign.y].y - rayOrigin.p.y) * prec.invDir.y, t1 );
73 t0 = std::max( (box[1 - sign.y].y - rayOrigin.p.y) * prec.invDir.y, t0 );
74
75 // compare and update z-dimension with t0-t1
76 t1 = std::min( (box[sign.z].z - rayOrigin.p.z) * prec.invDir.z, t1 );
77 t0 = std::max( (box[1 - sign.z].z - rayOrigin.p.z) * prec.invDir.z, t0 );
78 return t0 <= t1;
79}
80
81template <typename T = float>
82bool rayBoxIntersect( const Box3<T>& box, const Line3<T>& line, T t0, T t1 )
83{
84 IntersectionPrecomputes<T> prec( line.d );
85 return rayBoxIntersect( box, line, t0, t1, prec );
86}
87
89
90}
Vector3i sign
stores signs of direction vector;
Definition MRIntersectionPrecomputes.h:127
Vector3< T > invDir
Definition MRIntersectionPrecomputes.h:119
bool rayBoxIntersect(const Box3< T > &box, const RayOrigin< T > &rayOrigin, T &t0, T &t1, const IntersectionPrecomputes< T > &prec)
Definition MRRayBoxIntersection.h:63
Definition MRCameraOrientationPlugin.h:8
Box given by its min- and max- corners.
Definition MRMesh/MRBox.h:25
Definition MRMesh/MRMeshFwd.h:374
Definition MRLine.h:12
Definition MRRayBoxIntersection.h:15
RayOrigin(const Vector3< T > &ro)
Definition MRRayBoxIntersection.h:17
Vector3< T > p
Definition MRRayBoxIntersection.h:16
Definition MRMesh/MRVector3.h:19