39 #ifndef PCL_GPU_KINFU_CUDA_UTILS_HPP_ 40 #define PCL_GPU_KINFU_CUDA_UTILS_HPP_ 51 __device__ __host__ __forceinline__
void swap ( T& a, T& b )
60 __device__ __forceinline__
static float 62 __device__ __forceinline__
static float 65 __device__ __forceinline__
static float 66 min() {
return 1.175494351e-38f; };
67 __device__ __forceinline__
static float 68 max() {
return 3.402823466e+38f; };
73 __device__ __forceinline__
static short 74 max() {
return SHRT_MAX; };
77 __device__ __forceinline__
float 78 dot(
const float3& v1,
const float3& v2)
80 return v1.x * v2.x + v1.y*v2.y + v1.z*v2.z;
83 __device__ __forceinline__ float3&
86 vec.x += v; vec.y += v; vec.z += v;
return vec;
89 __device__ __forceinline__ float3
92 return make_float3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
95 __device__ __forceinline__ float3&
98 vec.x *= v; vec.y *= v; vec.z *= v;
return vec;
101 __device__ __forceinline__ float3
104 return make_float3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
107 __device__ __forceinline__ float3
110 return make_float3(v1.x * v, v1.y * v, v1.z * v);
113 __device__ __forceinline__
float 116 return sqrt(
dot(v, v));
119 __device__ __forceinline__ float3
122 return v * rsqrt(
dot(v, v));
125 __device__ __host__ __forceinline__ float3
126 cross(
const float3& v1,
const float3& v2)
128 return make_float3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x);
131 __device__ __forceinline__
void computeRoots2(
const float& b,
const float& c, float3& roots)
134 float d = b * b - 4.f * c;
140 roots.z = 0.5f * (b + sd);
141 roots.y = 0.5f * (b - sd);
144 __device__ __forceinline__
void 153 const float s_inv3 = 1.f/3.f;
154 const float s_sqrt3 = sqrtf(3.f);
157 float c2_over_3 = c2 * s_inv3;
158 float a_over_3 = (c1 - c2*c2_over_3)*s_inv3;
162 float half_b = 0.5f * (c0 + c2_over_3 * (2.f * c2_over_3 * c2_over_3 - c1));
164 float q = half_b * half_b + a_over_3 * a_over_3 * a_over_3;
169 float rho = sqrtf(-a_over_3);
170 float theta = atan2f (sqrtf (-q), half_b)*s_inv3;
171 float cos_theta = __cosf (theta);
172 float sin_theta = __sinf (theta);
173 roots.x = c2_over_3 + 2.f * rho * cos_theta;
174 roots.y = c2_over_3 - rho * (cos_theta + s_sqrt3 * sin_theta);
175 roots.z = c2_over_3 - rho * (cos_theta - s_sqrt3 * sin_theta);
178 if (roots.x >= roots.y)
179 swap(roots.x, roots.y);
181 if (roots.y >= roots.z)
183 swap(roots.y, roots.z);
185 if (roots.x >= roots.y)
186 swap (roots.x, roots.y);
200 __device__ __host__ __forceinline__ float3&
operator[](
int i) {
return data[i]; }
201 __device__ __host__ __forceinline__
const float3&
operator[](
int i)
const {
return data[i]; }
207 static __forceinline__ __device__ float3
218 if(!isMuchSmallerThan(src.x, src.z) || !isMuchSmallerThan(src.y, src.z))
220 float invnm = rsqrtf(src.x*src.x + src.y*src.y);
221 perp.x = -src.y * invnm;
222 perp.y = src.x * invnm;
231 float invnm = rsqrtf(src.z * src.z + src.y * src.y);
233 perp.y = -src.z * invnm;
234 perp.z = src.y * invnm;
240 __device__ __forceinline__
241 Eigen33(
volatile float* mat_pkg_arg) : mat_pkg(mat_pkg_arg) {}
242 __device__ __forceinline__
void 248 float max01 = fmaxf( fabsf(mat_pkg[0]), fabsf(mat_pkg[1]) );
249 float max23 = fmaxf( fabsf(mat_pkg[2]), fabsf(mat_pkg[3]) );
250 float max45 = fmaxf( fabsf(mat_pkg[4]), fabsf(mat_pkg[5]) );
251 float m0123 = fmaxf( max01, max23);
252 float scale = fmaxf( max45, m0123);
267 float c0 = m00() * m11() * m22()
268 + 2.f * m01() * m02() * m12()
269 - m00() * m12() * m12()
270 - m11() * m02() * m02()
271 - m22() * m01() * m01();
272 float c1 = m00() * m11() -
278 float c2 = m00() + m11() + m22();
284 evecs[0] = make_float3(1.f, 0.f, 0.f);
285 evecs[1] = make_float3(0.f, 1.f, 0.f);
286 evecs[2] = make_float3(0.f, 0.f, 1.f);
291 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
292 tmp[0].x -= evals.z; tmp[1].y -= evals.z; tmp[2].z -= evals.z;
294 vec_tmp[0] =
cross(tmp[0], tmp[1]);
295 vec_tmp[1] =
cross(tmp[0], tmp[2]);
296 vec_tmp[2] =
cross(tmp[1], tmp[2]);
298 float len1 =
dot (vec_tmp[0], vec_tmp[0]);
299 float len2 =
dot (vec_tmp[1], vec_tmp[1]);
300 float len3 =
dot (vec_tmp[2], vec_tmp[2]);
302 if (len1 >= len2 && len1 >= len3)
304 evecs[2] = vec_tmp[0] * rsqrtf (len1);
306 else if (len2 >= len1 && len2 >= len3)
308 evecs[2] = vec_tmp[1] * rsqrtf (len2);
312 evecs[2] = vec_tmp[2] * rsqrtf (len3);
316 evecs[0] =
cross(evecs[1], evecs[2]);
321 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
322 tmp[0].x -= evals.x; tmp[1].y -= evals.x; tmp[2].z -= evals.x;
324 vec_tmp[0] =
cross(tmp[0], tmp[1]);
325 vec_tmp[1] =
cross(tmp[0], tmp[2]);
326 vec_tmp[2] =
cross(tmp[1], tmp[2]);
328 float len1 =
dot(vec_tmp[0], vec_tmp[0]);
329 float len2 =
dot(vec_tmp[1], vec_tmp[1]);
330 float len3 =
dot(vec_tmp[2], vec_tmp[2]);
332 if (len1 >= len2 && len1 >= len3)
334 evecs[0] = vec_tmp[0] * rsqrtf(len1);
336 else if (len2 >= len1 && len2 >= len3)
338 evecs[0] = vec_tmp[1] * rsqrtf(len2);
342 evecs[0] = vec_tmp[2] * rsqrtf(len3);
346 evecs[2] =
cross(evecs[0], evecs[1]);
351 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
352 tmp[0].x -= evals.z; tmp[1].y -= evals.z; tmp[2].z -= evals.z;
354 vec_tmp[0] =
cross(tmp[0], tmp[1]);
355 vec_tmp[1] =
cross(tmp[0], tmp[2]);
356 vec_tmp[2] =
cross(tmp[1], tmp[2]);
358 float len1 =
dot(vec_tmp[0], vec_tmp[0]);
359 float len2 =
dot(vec_tmp[1], vec_tmp[1]);
360 float len3 =
dot(vec_tmp[2], vec_tmp[2]);
364 unsigned int min_el = 2;
365 unsigned int max_el = 2;
366 if (len1 >= len2 && len1 >= len3)
369 evecs[2] = vec_tmp[0] * rsqrtf (len1);
371 else if (len2 >= len1 && len2 >= len3)
374 evecs[2] = vec_tmp[1] * rsqrtf (len2);
379 evecs[2] = vec_tmp[2] * rsqrtf (len3);
382 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
383 tmp[0].x -= evals.y; tmp[1].y -= evals.y; tmp[2].z -= evals.y;
385 vec_tmp[0] =
cross(tmp[0], tmp[1]);
386 vec_tmp[1] =
cross(tmp[0], tmp[2]);
387 vec_tmp[2] =
cross(tmp[1], tmp[2]);
389 len1 =
dot(vec_tmp[0], vec_tmp[0]);
390 len2 =
dot(vec_tmp[1], vec_tmp[1]);
391 len3 =
dot(vec_tmp[2], vec_tmp[2]);
393 if (len1 >= len2 && len1 >= len3)
396 evecs[1] = vec_tmp[0] * rsqrtf (len1);
397 min_el = len1 <= mmax[min_el] ? 1 : min_el;
398 max_el = len1 > mmax[max_el] ? 1 : max_el;
400 else if (len2 >= len1 && len2 >= len3)
403 evecs[1] = vec_tmp[1] * rsqrtf (len2);
404 min_el = len2 <= mmax[min_el] ? 1 : min_el;
405 max_el = len2 > mmax[max_el] ? 1 : max_el;
410 evecs[1] = vec_tmp[2] * rsqrtf (len3);
411 min_el = len3 <= mmax[min_el] ? 1 : min_el;
412 max_el = len3 > mmax[max_el] ? 1 : max_el;
415 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
416 tmp[0].x -= evals.x; tmp[1].y -= evals.x; tmp[2].z -= evals.x;
418 vec_tmp[0] =
cross(tmp[0], tmp[1]);
419 vec_tmp[1] =
cross(tmp[0], tmp[2]);
420 vec_tmp[2] =
cross(tmp[1], tmp[2]);
422 len1 =
dot (vec_tmp[0], vec_tmp[0]);
423 len2 =
dot (vec_tmp[1], vec_tmp[1]);
424 len3 =
dot (vec_tmp[2], vec_tmp[2]);
427 if (len1 >= len2 && len1 >= len3)
430 evecs[0] = vec_tmp[0] * rsqrtf (len1);
431 min_el = len3 <= mmax[min_el] ? 0 : min_el;
432 max_el = len3 > mmax[max_el] ? 0 : max_el;
434 else if (len2 >= len1 && len2 >= len3)
437 evecs[0] = vec_tmp[1] * rsqrtf (len2);
438 min_el = len3 <= mmax[min_el] ? 0 : min_el;
439 max_el = len3 > mmax[max_el] ? 0 : max_el;
444 evecs[0] = vec_tmp[2] * rsqrtf (len3);
445 min_el = len3 <= mmax[min_el] ? 0 : min_el;
446 max_el = len3 > mmax[max_el] ? 0 : max_el;
449 unsigned mid_el = 3 - min_el - max_el;
450 evecs[min_el] =
normalized(
cross( evecs[(min_el+1) % 3], evecs[(min_el+2) % 3] ) );
451 evecs[mid_el] =
normalized(
cross( evecs[(mid_el+1) % 3], evecs[(mid_el+2) % 3] ) );
457 volatile float* mat_pkg;
459 __device__ __forceinline__
float m00()
const {
return mat_pkg[0]; }
460 __device__ __forceinline__
float m01()
const {
return mat_pkg[1]; }
461 __device__ __forceinline__
float m02()
const {
return mat_pkg[2]; }
462 __device__ __forceinline__
float m10()
const {
return mat_pkg[1]; }
463 __device__ __forceinline__
float m11()
const {
return mat_pkg[3]; }
464 __device__ __forceinline__
float m12()
const {
return mat_pkg[4]; }
465 __device__ __forceinline__
float m20()
const {
return mat_pkg[2]; }
466 __device__ __forceinline__
float m21()
const {
return mat_pkg[4]; }
467 __device__ __forceinline__
float m22()
const {
return mat_pkg[5]; }
469 __device__ __forceinline__ float3 row0()
const {
return make_float3( m00(), m01(), m02() ); }
470 __device__ __forceinline__ float3 row1()
const {
return make_float3( m10(), m11(), m12() ); }
471 __device__ __forceinline__ float3 row2()
const {
return make_float3( m20(), m21(), m22() ); }
473 __device__ __forceinline__
static bool isMuchSmallerThan (
float x,
float y)
477 return x * x <= prec_sqr * y * y;
483 static __device__ __forceinline__
unsigned int stride()
485 return blockDim.x * blockDim.y * blockDim.z;
488 static __device__ __forceinline__
int 491 return threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x;
494 template<
int CTA_SIZE,
typename T,
class BinOp>
495 static __device__ __forceinline__
void reduce(
volatile T* buffer, BinOp op)
500 if (CTA_SIZE >= 1024) {
if (tid < 512) buffer[tid] = val = op(val, buffer[tid + 512]); __syncthreads(); }
501 if (CTA_SIZE >= 512) {
if (tid < 256) buffer[tid] = val = op(val, buffer[tid + 256]); __syncthreads(); }
502 if (CTA_SIZE >= 256) {
if (tid < 128) buffer[tid] = val = op(val, buffer[tid + 128]); __syncthreads(); }
503 if (CTA_SIZE >= 128) {
if (tid < 64) buffer[tid] = val = op(val, buffer[tid + 64]); __syncthreads(); }
507 if (CTA_SIZE >= 64) { buffer[tid] = val = op(val, buffer[tid + 32]); }
508 if (CTA_SIZE >= 32) { buffer[tid] = val = op(val, buffer[tid + 16]); }
509 if (CTA_SIZE >= 16) { buffer[tid] = val = op(val, buffer[tid + 8]); }
510 if (CTA_SIZE >= 8) { buffer[tid] = val = op(val, buffer[tid + 4]); }
511 if (CTA_SIZE >= 4) { buffer[tid] = val = op(val, buffer[tid + 2]); }
512 if (CTA_SIZE >= 2) { buffer[tid] = val = op(val, buffer[tid + 1]); }
516 template<
int CTA_SIZE,
typename T,
class BinOp>
517 static __device__ __forceinline__ T
reduce(
volatile T* buffer, T init, BinOp op)
520 T val = buffer[tid] = init;
523 if (CTA_SIZE >= 1024) {
if (tid < 512) buffer[tid] = val = op(val, buffer[tid + 512]); __syncthreads(); }
524 if (CTA_SIZE >= 512) {
if (tid < 256) buffer[tid] = val = op(val, buffer[tid + 256]); __syncthreads(); }
525 if (CTA_SIZE >= 256) {
if (tid < 128) buffer[tid] = val = op(val, buffer[tid + 128]); __syncthreads(); }
526 if (CTA_SIZE >= 128) {
if (tid < 64) buffer[tid] = val = op(val, buffer[tid + 64]); __syncthreads(); }
530 if (CTA_SIZE >= 64) { buffer[tid] = val = op(val, buffer[tid + 32]); }
531 if (CTA_SIZE >= 32) { buffer[tid] = val = op(val, buffer[tid + 16]); }
532 if (CTA_SIZE >= 16) { buffer[tid] = val = op(val, buffer[tid + 8]); }
533 if (CTA_SIZE >= 8) { buffer[tid] = val = op(val, buffer[tid + 4]); }
534 if (CTA_SIZE >= 4) { buffer[tid] = val = op(val, buffer[tid + 2]); }
535 if (CTA_SIZE >= 2) { buffer[tid] = val = op(val, buffer[tid + 1]); }
552 static __device__ __forceinline__
unsigned int 556 asm(
"mov.u32 %0, %laneid;" :
"=r"(ret) );
560 static __device__ __forceinline__
unsigned int id()
562 int tid = threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x;
566 static __device__ __forceinline__
569 #if (__CUDA_ARCH__ >= 200) 571 asm(
"mov.u32 %0, %lanemask_lt;" :
"=r"(ret) );
574 return 0xFFFFFFFF >> (32 -
laneId());
587 static __device__ __forceinline__
int 590 const unsigned int lane = tid & 31;
594 int partial = ptr[tid];
596 ptr[tid] = partial = partial + ptr[tid + 16];
597 ptr[tid] = partial = partial + ptr[tid + 8];
598 ptr[tid] = partial = partial + ptr[tid + 4];
599 ptr[tid] = partial = partial + ptr[tid + 2];
600 ptr[tid] = partial = partial + ptr[tid + 1];
602 return ptr[tid - lane];
605 static __forceinline__ __device__
int 606 Ballot(
int predicate,
volatile int* cta_buffer)
608 #if __CUDA_ARCH__ >= 200 610 return __ballot(predicate);
613 cta_buffer[tid] = predicate ? (1 << (tid & 31)) : 0;
618 static __forceinline__ __device__
bool 619 All(
int predicate,
volatile int* cta_buffer)
621 #if __CUDA_ARCH__ >= 200 623 return __all(predicate);
626 cta_buffer[tid] = predicate ? 1 : 0;
__device__ __forceinline__ float3 normalized(const float3 &v)
__device__ __forceinline__ float3 operator+(const float3 &v1, const float3 &v2)
__device__ static __forceinline__ float epsilon()
__device__ static __forceinline__ float quiet_NaN()
static __device__ __forceinline__ int laneMaskLt()
static __device__ __forceinline__ int binaryExclScan(int ballot_mask)
__device__ __forceinline__ float norm(const float3 &v)
__device__ __host__ __forceinline__ const float3 & operator[](int i) const
__device__ __forceinline__ float3 & operator*=(float3 &vec, const float &v)
__device__ __forceinline__ float3 operator*(const Mat33 &m, const float3 &vec)
static __forceinline__ __device__ bool All(int predicate, volatile int *cta_buffer)
__device__ __forceinline__ float dot(const float3 &v1, const float3 &v2)
static __device__ __forceinline__ T reduce(volatile T *buffer, T init, BinOp op)
__device__ __host__ __forceinline__ void swap(T &a, T &b)
__device__ __host__ __forceinline__ float3 cross(const float3 &v1, const float3 &v2)
static __device__ __forceinline__ unsigned int id()
__device__ __forceinline__ float3 operator-(const float3 &v1, const float3 &v2)
__device__ __forceinline__ void computeRoots3(float c0, float c1, float c2, float3 &roots)
__device__ static __forceinline__ type epsilon()
static __device__ __forceinline__ int warp_reduce(volatile int *ptr, const unsigned int tid)
static __forceinline__ __device__ int Ballot(int predicate, volatile int *cta_buffer)
__device__ __forceinline__ void computeRoots2(const float &b, const float &c, float3 &roots)
static __forceinline__ __device__ float3 unitOrthogonal(const float3 &src)
static __device__ __forceinline__ void reduce(volatile T *buffer, BinOp op)
__device__ __host__ __forceinline__ float3 & operator[](int i)
static __device__ __forceinline__ unsigned int laneId()
Returns the warp lane ID of the calling thread.
static __device__ __forceinline__ int flattenedThreadId()
__device__ static __forceinline__ float min()
static __device__ __forceinline__ unsigned int stride()
__device__ static __forceinline__ float max()
__device__ __forceinline__ void compute(Mat33 &tmp, Mat33 &vec_tmp, Mat33 &evecs, float3 &evals)
__device__ static __forceinline__ short max()
__device__ __forceinline__ float3 & operator+=(float3 &vec, const float &v)
__device__ __forceinline__ Eigen33(volatile float *mat_pkg_arg)