Point Cloud Library (PCL)  1.14.0-dev
vector_math.hpp
1 /*
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 * Author: Anatoly Baskeheev, Itseez Ltd, (myname.mysurname@mycompany.com)
35 */
36 
37 #ifndef PCL_GPU_UTILS_DEVICE_VECTOR_MATH_HPP_
38 #define PCL_GPU_UTILS_DEVICE_VECTOR_MATH_HPP_
39 
40 namespace pcl
41 {
42  namespace device
43  {
44  ////////////////////////////////
45  // one element vectors
46 
47 
48  ////////////////////////////////
49  // two element vectors
50 
51 
52  ////////////////////////////////
53  // three element vectors
54 
55 #define PCL_GPU_IMPLEMENT_COMPOUND_VEC3_OP(type, scalar, op) \
56  __device__ __host__ __forceinline__ type & operator op (type & v1, const type & v2) { v1.x op v2.x; v1.y op v2.y; v1.z op v2.z; return v1; } \
57  __device__ __host__ __forceinline__ type & operator op (type & v, scalar val) { v.x op val; v.y op val; v.z op val; return v; }
58 
59  PCL_GPU_IMPLEMENT_COMPOUND_VEC3_OP(float3, float, -=)
60  PCL_GPU_IMPLEMENT_COMPOUND_VEC3_OP(float3, float, +=)
61  PCL_GPU_IMPLEMENT_COMPOUND_VEC3_OP(float3, float, *=)
62 
63  PCL_GPU_IMPLEMENT_COMPOUND_VEC3_OP(short3, short, -=)
64 
65  PCL_GPU_IMPLEMENT_COMPOUND_VEC3_OP(int3, int, +=)
66 
67 #undef PCL_GPU_IMPLEMENT_COMPOUND_VEC3_OP
68 
69  __device__ __host__ __forceinline__ float dot(const float3& v1, const float3& v2)
70  {
71  return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
72  }
73 
74  __device__ __host__ __forceinline__ float3 cross(const float3& v1, const float3& v2)
75  {
76  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);
77  }
78 
79  ////////////////////////////////
80  // four element vectors
81 
82  __device__ __host__ __forceinline__ float dot(const float4& v1, const float4& v2)
83  {
84  return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
85  }
86 
87  ////////////////////////////////
88  // alltype binary operarators
89 
90 #define PCL_GPU_IMPLEMENT_VEC_BINOP(type, scalar, op, cop) \
91  __device__ __host__ __forceinline__ type operator op (const type & v1, const type & v2) { type r = v1; r cop v2; return r; } \
92  __device__ __host__ __forceinline__ type operator op (const type & v1, scalar c) { type r = v1; r cop c; return r; }
93 
94  PCL_GPU_IMPLEMENT_VEC_BINOP(float3, float, -, -=)
95  PCL_GPU_IMPLEMENT_VEC_BINOP(float3, float, +, +=)
96  PCL_GPU_IMPLEMENT_VEC_BINOP(float3, float, *, *=)
97 
98  PCL_GPU_IMPLEMENT_VEC_BINOP(short3, short, -, -=)
99 
100  PCL_GPU_IMPLEMENT_VEC_BINOP(int3, int, +, +=)
101 
102 #undef PCL_GPU_IMPLEMENT_VEC_BINOP
103 
104 
105  ////////////////////////////////
106  // templated operations vectors
107 
108  template<typename T> __device__ __host__ __forceinline__ float norm(const T& val)
109  {
110  return sqrtf(dot(val, val));
111  }
112 
113  template<typename T> __host__ __device__ __forceinline__ float inverse_norm(const T& v)
114  {
115  return rsqrtf(dot(v, v));
116  }
117 
118  template<typename T> __host__ __device__ __forceinline__ T normalized(const T& v)
119  {
120  return v * inverse_norm(v);
121  }
122 
123  template<typename T> __host__ __device__ __forceinline__ T normalized_safe(const T& v)
124  {
125  return (dot(v, v) > 0) ? (v * rsqrtf(dot(v, v))) : v;
126  }
127  }
128 }
129 
130 #endif /* PCL_GPU_UTILS_DEVICE_VECTOR_MATH_HPP_ */
131 
__host__ __device__ __forceinline__ float inverse_norm(const T &v)
__host__ __device__ __forceinline__ T normalized_safe(const T &v)
__device__ __forceinline__ float3 normalized(const float3 &v)
Definition: utils.hpp:101
__device__ __forceinline__ float dot(const float3 &v1, const float3 &v2)
Definition: utils.hpp:59
__device__ __host__ __forceinline__ float norm(const float3 &v1, const float3 &v2)
__device__ __host__ __forceinline__ float3 cross(const float3 &v1, const float3 &v2)
Definition: utils.hpp:107