39 #ifndef PCL_GPU_KINFU_CUDA_UTILS_HPP_
40 #define PCL_GPU_KINFU_CUDA_UTILS_HPP_
42 #include <pcl/common/utils.h>
55 __device__ __host__ __forceinline__
void swap ( T& a, T& b )
60 __device__ __forceinline__
float
61 dot(
const float3& v1,
const float3& v2)
63 return v1.x * v2.x + v1.y*v2.y + v1.z*v2.z;
66 __device__ __forceinline__ float3&
69 vec.x += v; vec.y += v; vec.z += v;
return vec;
72 __device__ __forceinline__ float3
75 return make_float3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
78 __device__ __forceinline__ float3&
81 vec.x *= v; vec.y *= v; vec.z *= v;
return vec;
84 __device__ __forceinline__ float3
87 return make_float3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
90 __device__ __forceinline__ float3
93 return make_float3(v1.x * v, v1.y * v, v1.z * v);
96 __device__ __forceinline__
float
99 return sqrt(
dot(v, v));
102 __device__ __forceinline__ float3
105 return v * rsqrt(
dot(v, v));
108 __device__ __host__ __forceinline__ float3
109 cross(
const float3& v1,
const float3& v2)
111 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);
114 __device__ __forceinline__
void computeRoots2(
const float& b,
const float& c, float3& roots)
117 float d = b * b - 4.f * c;
123 roots.z = 0.5f * (b + sd);
124 roots.y = 0.5f * (b - sd);
127 __device__ __forceinline__
void
130 if ( std::abs(c0) < std::numeric_limits<float>::epsilon())
136 const float s_inv3 = 1.f/3.f;
137 const float s_sqrt3 = sqrtf(3.f);
140 float c2_over_3 = c2 * s_inv3;
141 float a_over_3 = (c1 - c2*c2_over_3)*s_inv3;
145 float half_b = 0.5f * (c0 + c2_over_3 * (2.f * c2_over_3 * c2_over_3 - c1));
147 float q = half_b * half_b + a_over_3 * a_over_3 * a_over_3;
152 float rho = sqrtf(-a_over_3);
153 float theta = std::atan2 (sqrtf (-q), half_b)*s_inv3;
154 float cos_theta = __cosf (theta);
155 float sin_theta = __sinf (theta);
156 roots.x = c2_over_3 + 2.f * rho * cos_theta;
157 roots.y = c2_over_3 - rho * (cos_theta + s_sqrt3 * sin_theta);
158 roots.z = c2_over_3 - rho * (cos_theta - s_sqrt3 * sin_theta);
161 if (roots.x >= roots.y)
162 swap(roots.x, roots.y);
164 if (roots.y >= roots.z)
166 swap(roots.y, roots.z);
168 if (roots.x >= roots.y)
169 swap (roots.x, roots.y);
183 __device__ __host__ __forceinline__ float3&
operator[](
int i) {
return data[i]; }
184 __device__ __host__ __forceinline__
const float3&
operator[](
int i)
const {
return data[i]; }
190 static __forceinline__ __device__ float3
201 if(!isMuchSmallerThan(src.x, src.z) || !isMuchSmallerThan(src.y, src.z))
203 float invnm = rsqrtf(src.x*src.x + src.y*src.y);
204 perp.x = -src.y * invnm;
205 perp.y = src.x * invnm;
214 float invnm = rsqrtf(src.z * src.z + src.y * src.y);
216 perp.y = -src.z * invnm;
217 perp.z = src.y * invnm;
223 __device__ __forceinline__
224 Eigen33(
volatile float* mat_pkg_arg) : mat_pkg(mat_pkg_arg) {}
225 __device__ __forceinline__
void
231 float max01 = fmaxf( std::abs(mat_pkg[0]), std::abs(mat_pkg[1]) );
232 float max23 = fmaxf( std::abs(mat_pkg[2]), std::abs(mat_pkg[3]) );
233 float max45 = fmaxf( std::abs(mat_pkg[4]), std::abs(mat_pkg[5]) );
234 float m0123 = fmaxf( max01, max23);
235 float scale = fmaxf( max45, m0123);
237 if (scale <= std::numeric_limits<float>::min())
250 float c0 = m00() * m11() * m22()
251 + 2.f * m01() * m02() * m12()
252 - m00() * m12() * m12()
253 - m11() * m02() * m02()
254 - m22() * m01() * m01();
255 float c1 = m00() * m11() -
261 float c2 = m00() + m11() + m22();
265 if(evals.z - evals.x <= std::numeric_limits<float>::epsilon())
267 evecs[0] = make_float3(1.f, 0.f, 0.f);
268 evecs[1] = make_float3(0.f, 1.f, 0.f);
269 evecs[2] = make_float3(0.f, 0.f, 1.f);
271 else if (evals.y - evals.x <= std::numeric_limits<float>::epsilon() )
274 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
275 tmp[0].x -= evals.z; tmp[1].y -= evals.z; tmp[2].z -= evals.z;
277 vec_tmp[0] =
cross(tmp[0], tmp[1]);
278 vec_tmp[1] =
cross(tmp[0], tmp[2]);
279 vec_tmp[2] =
cross(tmp[1], tmp[2]);
281 float len1 =
dot (vec_tmp[0], vec_tmp[0]);
282 float len2 =
dot (vec_tmp[1], vec_tmp[1]);
283 float len3 =
dot (vec_tmp[2], vec_tmp[2]);
285 if (len1 >= len2 && len1 >= len3)
287 evecs[2] = vec_tmp[0] * rsqrtf (len1);
289 else if (len2 >= len1 && len2 >= len3)
291 evecs[2] = vec_tmp[1] * rsqrtf (len2);
295 evecs[2] = vec_tmp[2] * rsqrtf (len3);
299 evecs[0] =
cross(evecs[1], evecs[2]);
301 else if (evals.z - evals.y <= std::numeric_limits<float>::epsilon() )
304 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
305 tmp[0].x -= evals.x; tmp[1].y -= evals.x; tmp[2].z -= evals.x;
307 vec_tmp[0] =
cross(tmp[0], tmp[1]);
308 vec_tmp[1] =
cross(tmp[0], tmp[2]);
309 vec_tmp[2] =
cross(tmp[1], tmp[2]);
311 float len1 =
dot(vec_tmp[0], vec_tmp[0]);
312 float len2 =
dot(vec_tmp[1], vec_tmp[1]);
313 float len3 =
dot(vec_tmp[2], vec_tmp[2]);
315 if (len1 >= len2 && len1 >= len3)
317 evecs[0] = vec_tmp[0] * rsqrtf(len1);
319 else if (len2 >= len1 && len2 >= len3)
321 evecs[0] = vec_tmp[1] * rsqrtf(len2);
325 evecs[0] = vec_tmp[2] * rsqrtf(len3);
329 evecs[2] =
cross(evecs[0], evecs[1]);
334 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
335 tmp[0].x -= evals.z; tmp[1].y -= evals.z; tmp[2].z -= evals.z;
337 vec_tmp[0] =
cross(tmp[0], tmp[1]);
338 vec_tmp[1] =
cross(tmp[0], tmp[2]);
339 vec_tmp[2] =
cross(tmp[1], tmp[2]);
341 float len1 =
dot(vec_tmp[0], vec_tmp[0]);
342 float len2 =
dot(vec_tmp[1], vec_tmp[1]);
343 float len3 =
dot(vec_tmp[2], vec_tmp[2]);
347 unsigned int min_el = 2;
348 unsigned int max_el = 2;
349 if (len1 >= len2 && len1 >= len3)
352 evecs[2] = vec_tmp[0] * rsqrtf (len1);
354 else if (len2 >= len1 && len2 >= len3)
357 evecs[2] = vec_tmp[1] * rsqrtf (len2);
362 evecs[2] = vec_tmp[2] * rsqrtf (len3);
365 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
366 tmp[0].x -= evals.y; tmp[1].y -= evals.y; tmp[2].z -= evals.y;
368 vec_tmp[0] =
cross(tmp[0], tmp[1]);
369 vec_tmp[1] =
cross(tmp[0], tmp[2]);
370 vec_tmp[2] =
cross(tmp[1], tmp[2]);
372 len1 =
dot(vec_tmp[0], vec_tmp[0]);
373 len2 =
dot(vec_tmp[1], vec_tmp[1]);
374 len3 =
dot(vec_tmp[2], vec_tmp[2]);
376 if (len1 >= len2 && len1 >= len3)
379 evecs[1] = vec_tmp[0] * rsqrtf (len1);
380 min_el = len1 <= mmax[min_el] ? 1 : min_el;
381 max_el = len1 > mmax[max_el] ? 1 : max_el;
383 else if (len2 >= len1 && len2 >= len3)
386 evecs[1] = vec_tmp[1] * rsqrtf (len2);
387 min_el = len2 <= mmax[min_el] ? 1 : min_el;
388 max_el = len2 > mmax[max_el] ? 1 : max_el;
393 evecs[1] = vec_tmp[2] * rsqrtf (len3);
394 min_el = len3 <= mmax[min_el] ? 1 : min_el;
395 max_el = len3 > mmax[max_el] ? 1 : max_el;
398 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
399 tmp[0].x -= evals.x; tmp[1].y -= evals.x; tmp[2].z -= evals.x;
401 vec_tmp[0] =
cross(tmp[0], tmp[1]);
402 vec_tmp[1] =
cross(tmp[0], tmp[2]);
403 vec_tmp[2] =
cross(tmp[1], tmp[2]);
405 len1 =
dot (vec_tmp[0], vec_tmp[0]);
406 len2 =
dot (vec_tmp[1], vec_tmp[1]);
407 len3 =
dot (vec_tmp[2], vec_tmp[2]);
410 if (len1 >= len2 && len1 >= len3)
413 evecs[0] = vec_tmp[0] * rsqrtf (len1);
414 min_el = len3 <= mmax[min_el] ? 0 : min_el;
415 max_el = len3 > mmax[max_el] ? 0 : max_el;
417 else if (len2 >= len1 && len2 >= len3)
420 evecs[0] = vec_tmp[1] * rsqrtf (len2);
421 min_el = len3 <= mmax[min_el] ? 0 : min_el;
422 max_el = len3 > mmax[max_el] ? 0 : max_el;
427 evecs[0] = vec_tmp[2] * rsqrtf (len3);
428 min_el = len3 <= mmax[min_el] ? 0 : min_el;
429 max_el = len3 > mmax[max_el] ? 0 : max_el;
432 unsigned mid_el = 3 - min_el - max_el;
433 evecs[min_el] =
normalized(
cross( evecs[(min_el+1) % 3], evecs[(min_el+2) % 3] ) );
434 evecs[mid_el] =
normalized(
cross( evecs[(mid_el+1) % 3], evecs[(mid_el+2) % 3] ) );
440 volatile float* mat_pkg;
442 __device__ __forceinline__
float m00()
const {
return mat_pkg[0]; }
443 __device__ __forceinline__
float m01()
const {
return mat_pkg[1]; }
444 __device__ __forceinline__
float m02()
const {
return mat_pkg[2]; }
445 __device__ __forceinline__
float m10()
const {
return mat_pkg[1]; }
446 __device__ __forceinline__
float m11()
const {
return mat_pkg[3]; }
447 __device__ __forceinline__
float m12()
const {
return mat_pkg[4]; }
448 __device__ __forceinline__
float m20()
const {
return mat_pkg[2]; }
449 __device__ __forceinline__
float m21()
const {
return mat_pkg[4]; }
450 __device__ __forceinline__
float m22()
const {
return mat_pkg[5]; }
452 __device__ __forceinline__ float3 row0()
const {
return make_float3( m00(), m01(), m02() ); }
453 __device__ __forceinline__ float3 row1()
const {
return make_float3( m10(), m11(), m12() ); }
454 __device__ __forceinline__ float3 row2()
const {
return make_float3( m20(), m21(), m22() ); }
456 __device__ __forceinline__
static bool isMuchSmallerThan (
float x,
float y)
459 constexpr
float prec_sqr = std::numeric_limits<float>::epsilon() * std::numeric_limits<float>::epsilon();
460 return x * x <= prec_sqr * y * y;
466 static __device__ __forceinline__
unsigned int stride()
468 return blockDim.x * blockDim.y * blockDim.z;
471 static __device__ __forceinline__
int
474 return threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x;
477 template<
int CTA_SIZE,
typename T,
class BinOp>
478 static __device__ __forceinline__
void reduce(
volatile T* buffer, BinOp op)
483 if (CTA_SIZE >= 1024) {
if (tid < 512) buffer[tid] = val = op(val, buffer[tid + 512]); __syncthreads(); }
484 if (CTA_SIZE >= 512) {
if (tid < 256) buffer[tid] = val = op(val, buffer[tid + 256]); __syncthreads(); }
485 if (CTA_SIZE >= 256) {
if (tid < 128) buffer[tid] = val = op(val, buffer[tid + 128]); __syncthreads(); }
486 if (CTA_SIZE >= 128) {
if (tid < 64) buffer[tid] = val = op(val, buffer[tid + 64]); __syncthreads(); }
490 if (CTA_SIZE >= 64) { buffer[tid] = val = op(val, buffer[tid + 32]); }
491 if (CTA_SIZE >= 32) { buffer[tid] = val = op(val, buffer[tid + 16]); }
492 if (CTA_SIZE >= 16) { buffer[tid] = val = op(val, buffer[tid + 8]); }
493 if (CTA_SIZE >= 8) { buffer[tid] = val = op(val, buffer[tid + 4]); }
494 if (CTA_SIZE >= 4) { buffer[tid] = val = op(val, buffer[tid + 2]); }
495 if (CTA_SIZE >= 2) { buffer[tid] = val = op(val, buffer[tid + 1]); }
499 template<
int CTA_SIZE,
typename T,
class BinOp>
500 static __device__ __forceinline__ T
reduce(
volatile T* buffer, T init, BinOp op)
503 T val = buffer[tid] = init;
506 if (CTA_SIZE >= 1024) {
if (tid < 512) buffer[tid] = val = op(val, buffer[tid + 512]); __syncthreads(); }
507 if (CTA_SIZE >= 512) {
if (tid < 256) buffer[tid] = val = op(val, buffer[tid + 256]); __syncthreads(); }
508 if (CTA_SIZE >= 256) {
if (tid < 128) buffer[tid] = val = op(val, buffer[tid + 128]); __syncthreads(); }
509 if (CTA_SIZE >= 128) {
if (tid < 64) buffer[tid] = val = op(val, buffer[tid + 64]); __syncthreads(); }
513 if (CTA_SIZE >= 64) { buffer[tid] = val = op(val, buffer[tid + 32]); }
514 if (CTA_SIZE >= 32) { buffer[tid] = val = op(val, buffer[tid + 16]); }
515 if (CTA_SIZE >= 16) { buffer[tid] = val = op(val, buffer[tid + 8]); }
516 if (CTA_SIZE >= 8) { buffer[tid] = val = op(val, buffer[tid + 4]); }
517 if (CTA_SIZE >= 4) { buffer[tid] = val = op(val, buffer[tid + 2]); }
518 if (CTA_SIZE >= 2) { buffer[tid] = val = op(val, buffer[tid + 1]); }
535 static __device__ __forceinline__
unsigned int
539 asm(
"mov.u32 %0, %laneid;" :
"=r"(ret) );
543 static __device__ __forceinline__
unsigned int id()
545 int tid = threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x;
549 static __device__ __forceinline__
553 asm(
"mov.u32 %0, %lanemask_lt;" :
"=r"(ret) );
566 static __device__ __forceinline__
int
569 const unsigned int lane = tid & 31;
573 int partial = ptr[tid];
575 ptr[tid] = partial = partial + ptr[tid + 16];
576 ptr[tid] = partial = partial + ptr[tid + 8];
577 ptr[tid] = partial = partial + ptr[tid + 4];
578 ptr[tid] = partial = partial + ptr[tid + 2];
579 ptr[tid] = partial = partial + ptr[tid + 1];
581 return ptr[tid - lane];
584 static __forceinline__ __device__
int
585 Ballot(
int predicate,
volatile int* cta_buffer)
588 #if CUDA_VERSION >= 9000
589 return __ballot_sync (__activemask (), predicate);
591 return __ballot (predicate);
595 static __forceinline__ __device__
bool
596 All(
int predicate,
volatile int* cta_buffer)
599 #if CUDA_VERSION >= 9000
600 return __all_sync (__activemask (), predicate);
602 return __all (predicate);
__device__ __forceinline__ float norm(const float3 &v)
__device__ __forceinline__ float3 operator*(const Mat33 &m, const float3 &vec)
__device__ __forceinline__ float3 operator+(const float3 &v1, const float3 &v2)
__device__ __forceinline__ float dot(const float3 &v1, const float3 &v2)
__device__ __forceinline__ void computeRoots2(const float &b, const float &c, float3 &roots)
__device__ __forceinline__ float3 & operator+=(float3 &vec, const float &v)
__device__ __forceinline__ float3 & operator*=(float3 &vec, const float &v)
__device__ __forceinline__ float3 operator-(const float3 &v1, const float3 &v2)
__device__ __host__ __forceinline__ void swap(T &a, T &b)
__device__ __forceinline__ float3 normalized(const float3 &v)
__device__ __host__ __forceinline__ float3 cross(const float3 &v1, const float3 &v2)
__device__ __forceinline__ void computeRoots3(float c0, float c1, float c2, float3 &roots)
void ignore(const T &...)
Utility function to eliminate unused variable warnings.
static __device__ __forceinline__ int flattenedThreadId()
static __device__ __forceinline__ void reduce(volatile T *buffer, BinOp op)
static __device__ __forceinline__ T reduce(volatile T *buffer, T init, BinOp op)
static __device__ __forceinline__ unsigned int stride()
__device__ __host__ __forceinline__ float3 & operator[](int i)
__device__ __host__ __forceinline__ const float3 & operator[](int i) const
static __forceinline__ __device__ float3 unitOrthogonal(const float3 &src)
__device__ __forceinline__ void compute(Mat33 &tmp, Mat33 &vec_tmp, Mat33 &evecs, float3 &evals)
__device__ __forceinline__ Eigen33(volatile float *mat_pkg_arg)
static __forceinline__ __device__ bool All(int predicate, volatile int *cta_buffer)
static __forceinline__ __device__ int Ballot(int predicate, volatile int *cta_buffer)
static __device__ __forceinline__ int warp_reduce(volatile int *ptr, const unsigned int tid)
static __device__ __forceinline__ unsigned int laneId()
Returns the warp lane ID of the calling thread.
static __device__ __forceinline__ int laneMaskLt()
static __device__ __forceinline__ unsigned int id()
static __device__ __forceinline__ int binaryExclScan(int ballot_mask)