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