MeshLib Documentation
Loading...
Searching...
No Matches
MRIntersectionPrecomputes.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVector3.h"
4#include "MRPch/MRBindingMacros.h"
5
6#if defined(__x86_64__) || defined(_M_X64)
7#include <xmmintrin.h> //SSE instructions
8#endif
9
10namespace MR
11{
12
15
24template <typename T>
25void findMaxVectorDim( int& dimX, int& dimY, int& dimZ, const Vector3<T>& dir )
26{
27 if( dir.x > dir.y )
28 {
29 if( dir.x > dir.z )
30 {
31 if( dir.y > dir.z )
32 {
33 // x>y>z
34 if( -dir.z > dir.x )
35 {
36 dimZ = 2; dimX = 1; dimY = 0;
37 }
38 else
39 {
40 dimZ = 0; dimX = 1; dimY = 2;
41 }
42 }
43 else
44 {
45 // x>z>y
46 if( -dir.y > dir.x )
47 {
48 dimZ = 1; dimX = 0; dimY = 2;
49 }
50 else
51 {
52 dimZ = 0; dimX = 1; dimY = 2;
53 }
54 }
55 }
56 else
57 {
58 // z>x>y
59 if( -dir.y > dir.z )
60 {
61 dimZ = 1; dimX = 0; dimY = 2;
62 }
63 else
64 {
65 dimZ = 2; dimX = 0; dimY = 1;
66 }
67 }
68 }
69 else
70 {
71 if( dir.y > dir.z )
72 {
73 if( dir.x < dir.z )
74 {
75 // y>z>x
76 if( -dir.x > dir.y )
77 {
78 dimZ = 0; dimX = 2; dimY = 1;
79 }
80 else
81 {
82 dimZ = 1; dimX = 2; dimY = 0;
83 }
84 }
85 else
86 {
87 // y>x>z
88 if( -dir.z > dir.y )
89 {
90 dimZ = 2; dimX = 1; dimY = 0;
91 }
92 else
93 {
94 dimZ = 1; dimX = 2; dimY = 0;
95 }
96 }
97 }
98 else
99 {
100 // z>y>x
101 if( -dir.x > dir.z )
102 {
103 dimZ = 0; dimX = 2; dimY = 1;
104 }
105 else
106 {
107 dimZ = 2; dimX = 0; dimY = 1;
108 }
109 }
110 }
111}
112
115template<typename T>
116struct IntersectionPrecomputes
117{
118 // {1 / dir}
120 // [0]max, [1]next, [2]next-next
121 // f.e. {1,2,-3} => {2,1,0}
122 int maxDimIdxZ = 2;
123 int idxX = 0;
124 int idxY = 1;
125
127 Vector3i sign;
128
130 T Sx, Sy, Sz;
133 {
135
136 sign.x = dir.x >= T( 0 ) ? 1 : 0;
137 sign.y = dir.y >= T( 0 ) ? 1 : 0;
138 sign.z = dir.z >= T( 0 ) ? 1 : 0;
139
140 Sx = dir[idxX] / dir[maxDimIdxZ];
141 Sy = dir[idxY] / dir[maxDimIdxZ];
142 Sz = T( 1 ) / dir[maxDimIdxZ];
143
144 invDir.x = ( dir.x == 0 ) ? std::numeric_limits<T>::max() : T( 1 ) / dir.x;
145 invDir.y = ( dir.y == 0 ) ? std::numeric_limits<T>::max() : T( 1 ) / dir.y;
146 invDir.z = ( dir.z == 0 ) ? std::numeric_limits<T>::max() : T( 1 ) / dir.z;
147 }
148
149};
150
151/* CPU(X86_64) - AMD64 / Intel64 / x86_64 64-bit */
152#if defined(__x86_64__) || defined(_M_X64)
153template<>
154struct IntersectionPrecomputes<float>
155{
156 // {1.f / dir}
157 MR_BIND_IGNORE __m128 invDir;
158 // [0]max, [1]next, [2]next-next
159 // f.e. {1,2,-3} => {2,1,0}
160 int maxDimIdxZ = 2;
161 int idxX = 0;
162 int idxY = 1;
163
165 float Sx, Sy, Sz;
166 IntersectionPrecomputes() = default;
168 {
170
171 Sx = dir[idxX] / dir[maxDimIdxZ];
172 Sy = dir[idxY] / dir[maxDimIdxZ];
173 Sz = float( 1 ) / dir[maxDimIdxZ];
174
175 invDir = _mm_set_ps(
176 ( dir.x == 0 ) ? std::numeric_limits<float>::max() : 1 / dir.x,
177 ( dir.y == 0 ) ? std::numeric_limits<float>::max() : 1 / dir.y,
178 ( dir.z == 0 ) ? std::numeric_limits<float>::max() : 1 / dir.z,
179 1 );
180 }
181
182};
183
185
186#else
187 #pragma message("IntersectionPrecomputes<float>: no hardware optimized instructions")
188#endif
189}
int idxY
Definition MRIntersectionPrecomputes.h:124
T Sx
precomputed factors
Definition MRIntersectionPrecomputes.h:130
Vector3i sign
stores signs of direction vector;
Definition MRIntersectionPrecomputes.h:127
int idxX
Definition MRIntersectionPrecomputes.h:123
Vector3< T > invDir
Definition MRIntersectionPrecomputes.h:119
T Sz
Definition MRIntersectionPrecomputes.h:130
void findMaxVectorDim(int &dimX, int &dimY, int &dimZ, const Vector3< T > &dir)
finds index of maximum axis and stores it into dimZ
Definition MRIntersectionPrecomputes.h:25
IntersectionPrecomputes(const Vector3< T > &dir)
Definition MRIntersectionPrecomputes.h:132
T Sy
Definition MRIntersectionPrecomputes.h:130
int maxDimIdxZ
Definition MRIntersectionPrecomputes.h:122
Definition MRCameraOrientationPlugin.h:8
MRMESH_CLASS Vector3
Definition MRMesh/MRMeshFwd.h:137
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