39 #ifndef PCL_GPU_KINFU_CUDA_UTILS_HPP_
40 #define PCL_GPU_KINFU_CUDA_UTILS_HPP_
42 #include <pcl/common/utils.h>
53 __device__ __host__ __forceinline__
void swap ( T& a, T& b )
58 __device__ __forceinline__
float
59 dot(
const float3& v1,
const float3& v2)
61 return v1.x * v2.x + v1.y*v2.y + v1.z*v2.z;
64 __device__ __forceinline__ float3&
67 vec.x += v; vec.y += v; vec.z += v;
return vec;
70 __device__ __forceinline__ float3
73 return make_float3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
76 __device__ __forceinline__ float3&
79 vec.x *= v; vec.y *= v; vec.z *= v;
return vec;
82 __device__ __forceinline__ float3
85 return make_float3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
88 __device__ __forceinline__ float3
91 return make_float3(v1.x * v, v1.y * v, v1.z * v);
94 __device__ __forceinline__
float
97 return sqrt(
dot(v, v));
100 __device__ __forceinline__ float3
103 return v * rsqrt(
dot(v, v));
106 __device__ __host__ __forceinline__ float3
107 cross(
const float3& v1,
const float3& v2)
109 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);
112 __device__ __forceinline__
void computeRoots2(
const float& b,
const float& c, float3& roots)
115 float d = b * b - 4.f * c;
121 roots.z = 0.5f * (b + sd);
122 roots.y = 0.5f * (b - sd);
125 __device__ __forceinline__
void
128 if ( std::abs(c0) < std::numeric_limits<float>::epsilon())
134 const float s_inv3 = 1.f/3.f;
135 const float s_sqrt3 = sqrtf(3.f);
138 float c2_over_3 = c2 * s_inv3;
139 float a_over_3 = (c1 - c2*c2_over_3)*s_inv3;
143 float half_b = 0.5f * (c0 + c2_over_3 * (2.f * c2_over_3 * c2_over_3 - c1));
145 float q = half_b * half_b + a_over_3 * a_over_3 * a_over_3;
150 float rho = sqrtf(-a_over_3);
151 float theta = std::atan2 (sqrtf (-q), half_b)*s_inv3;
152 float cos_theta = __cosf (theta);
153 float sin_theta = __sinf (theta);
154 roots.x = c2_over_3 + 2.f * rho * cos_theta;
155 roots.y = c2_over_3 - rho * (cos_theta + s_sqrt3 * sin_theta);
156 roots.z = c2_over_3 - rho * (cos_theta - s_sqrt3 * sin_theta);
159 if (roots.x >= roots.y)
160 swap(roots.x, roots.y);
162 if (roots.y >= roots.z)
164 swap(roots.y, roots.z);
166 if (roots.x >= roots.y)
167 swap (roots.x, roots.y);
181 __device__ __host__ __forceinline__ float3&
operator[](
int i) {
return data[i]; }
182 __device__ __host__ __forceinline__
const float3&
operator[](
int i)
const {
return data[i]; }
188 static __forceinline__ __device__ float3
199 if(!isMuchSmallerThan(src.x, src.z) || !isMuchSmallerThan(src.y, src.z))
201 float invnm = rsqrtf(src.x*src.x + src.y*src.y);
202 perp.x = -src.y * invnm;
203 perp.y = src.x * invnm;
212 float invnm = rsqrtf(src.z * src.z + src.y * src.y);
214 perp.y = -src.z * invnm;
215 perp.z = src.y * invnm;
221 __device__ __forceinline__
222 Eigen33(
volatile float* mat_pkg_arg) : mat_pkg(mat_pkg_arg) {}
223 __device__ __forceinline__
void
229 float max01 = fmaxf( std::abs(mat_pkg[0]), std::abs(mat_pkg[1]) );
230 float max23 = fmaxf( std::abs(mat_pkg[2]), std::abs(mat_pkg[3]) );
231 float max45 = fmaxf( std::abs(mat_pkg[4]), std::abs(mat_pkg[5]) );
232 float m0123 = fmaxf( max01, max23);
233 float scale = fmaxf( max45, m0123);
235 if (scale <= std::numeric_limits<float>::min())
248 float c0 = m00() * m11() * m22()
249 + 2.f * m01() * m02() * m12()
250 - m00() * m12() * m12()
251 - m11() * m02() * m02()
252 - m22() * m01() * m01();
253 float c1 = m00() * m11() -
259 float c2 = m00() + m11() + m22();
263 if(evals.z - evals.x <= std::numeric_limits<float>::epsilon())
265 evecs[0] = make_float3(1.f, 0.f, 0.f);
266 evecs[1] = make_float3(0.f, 1.f, 0.f);
267 evecs[2] = make_float3(0.f, 0.f, 1.f);
269 else if (evals.y - evals.x <= std::numeric_limits<float>::epsilon() )
272 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
273 tmp[0].x -= evals.z; tmp[1].y -= evals.z; tmp[2].z -= evals.z;
275 vec_tmp[0] =
cross(tmp[0], tmp[1]);
276 vec_tmp[1] =
cross(tmp[0], tmp[2]);
277 vec_tmp[2] =
cross(tmp[1], tmp[2]);
279 float len1 =
dot (vec_tmp[0], vec_tmp[0]);
280 float len2 =
dot (vec_tmp[1], vec_tmp[1]);
281 float len3 =
dot (vec_tmp[2], vec_tmp[2]);
283 if (len1 >= len2 && len1 >= len3)
285 evecs[2] = vec_tmp[0] * rsqrtf (len1);
287 else if (len2 >= len1 && len2 >= len3)
289 evecs[2] = vec_tmp[1] * rsqrtf (len2);
293 evecs[2] = vec_tmp[2] * rsqrtf (len3);
297 evecs[0] =
cross(evecs[1], evecs[2]);
299 else if (evals.z - evals.y <= std::numeric_limits<float>::epsilon() )
302 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
303 tmp[0].x -= evals.x; tmp[1].y -= evals.x; tmp[2].z -= evals.x;
305 vec_tmp[0] =
cross(tmp[0], tmp[1]);
306 vec_tmp[1] =
cross(tmp[0], tmp[2]);
307 vec_tmp[2] =
cross(tmp[1], tmp[2]);
309 float len1 =
dot(vec_tmp[0], vec_tmp[0]);
310 float len2 =
dot(vec_tmp[1], vec_tmp[1]);
311 float len3 =
dot(vec_tmp[2], vec_tmp[2]);
313 if (len1 >= len2 && len1 >= len3)
315 evecs[0] = vec_tmp[0] * rsqrtf(len1);
317 else if (len2 >= len1 && len2 >= len3)
319 evecs[0] = vec_tmp[1] * rsqrtf(len2);
323 evecs[0] = vec_tmp[2] * rsqrtf(len3);
327 evecs[2] =
cross(evecs[0], evecs[1]);
332 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
333 tmp[0].x -= evals.z; tmp[1].y -= evals.z; tmp[2].z -= evals.z;
335 vec_tmp[0] =
cross(tmp[0], tmp[1]);
336 vec_tmp[1] =
cross(tmp[0], tmp[2]);
337 vec_tmp[2] =
cross(tmp[1], tmp[2]);
339 float len1 =
dot(vec_tmp[0], vec_tmp[0]);
340 float len2 =
dot(vec_tmp[1], vec_tmp[1]);
341 float len3 =
dot(vec_tmp[2], vec_tmp[2]);
345 unsigned int min_el = 2;
346 unsigned int max_el = 2;
347 if (len1 >= len2 && len1 >= len3)
350 evecs[2] = vec_tmp[0] * rsqrtf (len1);
352 else if (len2 >= len1 && len2 >= len3)
355 evecs[2] = vec_tmp[1] * rsqrtf (len2);
360 evecs[2] = vec_tmp[2] * rsqrtf (len3);
363 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
364 tmp[0].x -= evals.y; tmp[1].y -= evals.y; tmp[2].z -= evals.y;
366 vec_tmp[0] =
cross(tmp[0], tmp[1]);
367 vec_tmp[1] =
cross(tmp[0], tmp[2]);
368 vec_tmp[2] =
cross(tmp[1], tmp[2]);
370 len1 =
dot(vec_tmp[0], vec_tmp[0]);
371 len2 =
dot(vec_tmp[1], vec_tmp[1]);
372 len3 =
dot(vec_tmp[2], vec_tmp[2]);
374 if (len1 >= len2 && len1 >= len3)
377 evecs[1] = vec_tmp[0] * rsqrtf (len1);
378 min_el = len1 <= mmax[min_el] ? 1 : min_el;
379 max_el = len1 > mmax[max_el] ? 1 : max_el;
381 else if (len2 >= len1 && len2 >= len3)
384 evecs[1] = vec_tmp[1] * rsqrtf (len2);
385 min_el = len2 <= mmax[min_el] ? 1 : min_el;
386 max_el = len2 > mmax[max_el] ? 1 : max_el;
391 evecs[1] = vec_tmp[2] * rsqrtf (len3);
392 min_el = len3 <= mmax[min_el] ? 1 : min_el;
393 max_el = len3 > mmax[max_el] ? 1 : max_el;
396 tmp[0] = row0(); tmp[1] = row1(); tmp[2] = row2();
397 tmp[0].x -= evals.x; tmp[1].y -= evals.x; tmp[2].z -= evals.x;
399 vec_tmp[0] =
cross(tmp[0], tmp[1]);
400 vec_tmp[1] =
cross(tmp[0], tmp[2]);
401 vec_tmp[2] =
cross(tmp[1], tmp[2]);
403 len1 =
dot (vec_tmp[0], vec_tmp[0]);
404 len2 =
dot (vec_tmp[1], vec_tmp[1]);
405 len3 =
dot (vec_tmp[2], vec_tmp[2]);
408 if (len1 >= len2 && len1 >= len3)
411 evecs[0] = vec_tmp[0] * rsqrtf (len1);
412 min_el = len3 <= mmax[min_el] ? 0 : min_el;
413 max_el = len3 > mmax[max_el] ? 0 : max_el;
415 else if (len2 >= len1 && len2 >= len3)
418 evecs[0] = vec_tmp[1] * rsqrtf (len2);
419 min_el = len3 <= mmax[min_el] ? 0 : min_el;
420 max_el = len3 > mmax[max_el] ? 0 : max_el;
425 evecs[0] = vec_tmp[2] * rsqrtf (len3);
426 min_el = len3 <= mmax[min_el] ? 0 : min_el;
427 max_el = len3 > mmax[max_el] ? 0 : max_el;
430 unsigned mid_el = 3 - min_el - max_el;
431 evecs[min_el] =
normalized(
cross( evecs[(min_el+1) % 3], evecs[(min_el+2) % 3] ) );
432 evecs[mid_el] =
normalized(
cross( evecs[(mid_el+1) % 3], evecs[(mid_el+2) % 3] ) );
438 volatile float* mat_pkg;
440 __device__ __forceinline__
float m00()
const {
return mat_pkg[0]; }
441 __device__ __forceinline__
float m01()
const {
return mat_pkg[1]; }
442 __device__ __forceinline__
float m02()
const {
return mat_pkg[2]; }
443 __device__ __forceinline__
float m10()
const {
return mat_pkg[1]; }
444 __device__ __forceinline__
float m11()
const {
return mat_pkg[3]; }
445 __device__ __forceinline__
float m12()
const {
return mat_pkg[4]; }
446 __device__ __forceinline__
float m20()
const {
return mat_pkg[2]; }
447 __device__ __forceinline__
float m21()
const {
return mat_pkg[4]; }
448 __device__ __forceinline__
float m22()
const {
return mat_pkg[5]; }
450 __device__ __forceinline__ float3 row0()
const {
return make_float3( m00(), m01(), m02() ); }
451 __device__ __forceinline__ float3 row1()
const {
return make_float3( m10(), m11(), m12() ); }
452 __device__ __forceinline__ float3 row2()
const {
return make_float3( m20(), m21(), m22() ); }
454 __device__ __forceinline__
static bool isMuchSmallerThan (
float x,
float y)
457 const float prec_sqr = std::numeric_limits<float>::epsilon() * std::numeric_limits<float>::epsilon();
458 return x * x <= prec_sqr * y * y;
464 static __device__ __forceinline__
unsigned int stride()
466 return blockDim.x * blockDim.y * blockDim.z;
469 static __device__ __forceinline__
int
472 return threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x;
475 template<
int CTA_SIZE,
typename T,
class BinOp>
476 static __device__ __forceinline__
void reduce(
volatile T* buffer, BinOp op)
481 if (CTA_SIZE >= 1024) {
if (tid < 512) buffer[tid] = val = op(val, buffer[tid + 512]); __syncthreads(); }
482 if (CTA_SIZE >= 512) {
if (tid < 256) buffer[tid] = val = op(val, buffer[tid + 256]); __syncthreads(); }
483 if (CTA_SIZE >= 256) {
if (tid < 128) buffer[tid] = val = op(val, buffer[tid + 128]); __syncthreads(); }
484 if (CTA_SIZE >= 128) {
if (tid < 64) buffer[tid] = val = op(val, buffer[tid + 64]); __syncthreads(); }
488 if (CTA_SIZE >= 64) { buffer[tid] = val = op(val, buffer[tid + 32]); }
489 if (CTA_SIZE >= 32) { buffer[tid] = val = op(val, buffer[tid + 16]); }
490 if (CTA_SIZE >= 16) { buffer[tid] = val = op(val, buffer[tid + 8]); }
491 if (CTA_SIZE >= 8) { buffer[tid] = val = op(val, buffer[tid + 4]); }
492 if (CTA_SIZE >= 4) { buffer[tid] = val = op(val, buffer[tid + 2]); }
493 if (CTA_SIZE >= 2) { buffer[tid] = val = op(val, buffer[tid + 1]); }
497 template<
int CTA_SIZE,
typename T,
class BinOp>
498 static __device__ __forceinline__ T
reduce(
volatile T* buffer, T init, BinOp op)
501 T val = buffer[tid] = init;
504 if (CTA_SIZE >= 1024) {
if (tid < 512) buffer[tid] = val = op(val, buffer[tid + 512]); __syncthreads(); }
505 if (CTA_SIZE >= 512) {
if (tid < 256) buffer[tid] = val = op(val, buffer[tid + 256]); __syncthreads(); }
506 if (CTA_SIZE >= 256) {
if (tid < 128) buffer[tid] = val = op(val, buffer[tid + 128]); __syncthreads(); }
507 if (CTA_SIZE >= 128) {
if (tid < 64) buffer[tid] = val = op(val, buffer[tid + 64]); __syncthreads(); }
511 if (CTA_SIZE >= 64) { buffer[tid] = val = op(val, buffer[tid + 32]); }
512 if (CTA_SIZE >= 32) { buffer[tid] = val = op(val, buffer[tid + 16]); }
513 if (CTA_SIZE >= 16) { buffer[tid] = val = op(val, buffer[tid + 8]); }
514 if (CTA_SIZE >= 8) { buffer[tid] = val = op(val, buffer[tid + 4]); }
515 if (CTA_SIZE >= 4) { buffer[tid] = val = op(val, buffer[tid + 2]); }
516 if (CTA_SIZE >= 2) { buffer[tid] = val = op(val, buffer[tid + 1]); }
533 static __device__ __forceinline__
unsigned int
537 asm(
"mov.u32 %0, %laneid;" :
"=r"(ret) );
541 static __device__ __forceinline__
unsigned int id()
543 int tid = threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x;
547 static __device__ __forceinline__
551 asm(
"mov.u32 %0, %lanemask_lt;" :
"=r"(ret) );
564 static __device__ __forceinline__
int
567 const unsigned int lane = tid & 31;
571 int partial = ptr[tid];
573 ptr[tid] = partial = partial + ptr[tid + 16];
574 ptr[tid] = partial = partial + ptr[tid + 8];
575 ptr[tid] = partial = partial + ptr[tid + 4];
576 ptr[tid] = partial = partial + ptr[tid + 2];
577 ptr[tid] = partial = partial + ptr[tid + 1];
579 return ptr[tid - lane];
582 static __forceinline__ __device__
int
583 Ballot(
int predicate,
volatile int* cta_buffer)
586 #if CUDA_VERSION >= 9000
587 return __ballot_sync (__activemask (), predicate);
589 return __ballot(predicate);
593 static __forceinline__ __device__
bool
594 All(
int predicate,
volatile int* cta_buffer)
597 #if CUDA_VERSION >= 9000
598 return __all_sync (__activemask (), predicate);
600 return __all(predicate);
__device__ __forceinline__ float3 operator+(const float3 &v1, const float3 &v2)
__device__ __forceinline__ float3 operator*(const Mat33 &m, const float3 &vec)
__device__ __forceinline__ float3 & operator*=(float3 &vec, const float &v)
__device__ __forceinline__ float3 operator-(const float3 &v1, const float3 &v2)
__device__ __forceinline__ float3 normalized(const float3 &v)
__device__ __forceinline__ float dot(const float3 &v1, const float3 &v2)
__device__ __forceinline__ void computeRoots3(float c0, float c1, float c2, float3 &roots)
__device__ __forceinline__ float3 & operator+=(float3 &vec, const float &v)
__device__ __host__ __forceinline__ float norm(const float3 &v1, const float3 &v2)
__device__ __forceinline__ void computeRoots2(const float &b, const float &c, float3 &roots)
__device__ __host__ __forceinline__ void swap(T &a, T &b)
__device__ __host__ __forceinline__ float3 cross(const float3 &v1, const float3 &v2)
void ignore(const T &...)
Utility function to eliminate unused variable warnings.
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()
static __device__ __forceinline__ int flattenedThreadId()
__device__ __host__ __forceinline__ const float3 & operator[](int i) const
__device__ __host__ __forceinline__ float3 & operator[](int i)
__device__ __forceinline__ void compute(Mat33 &tmp, Mat33 &vec_tmp, Mat33 &evecs, float3 &evals)
static __forceinline__ __device__ float3 unitOrthogonal(const float3 &src)
__device__ __forceinline__ Eigen33(volatile float *mat_pkg_arg)
static __forceinline__ __device__ int Ballot(int predicate, volatile int *cta_buffer)
static __forceinline__ __device__ bool All(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 binaryExclScan(int ballot_mask)
static __device__ __forceinline__ int laneMaskLt()
static __device__ __forceinline__ unsigned int id()