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