26 #ifndef CUDA_BASICMAT_H 27 #define CUDA_BASICMAT_H 34 #define Pole (sqrt(3.0f)-2.0f) //pole for cubic b-spline 36 inline __host__ __device__
float power(
float base,
int exp) {
37 return powf(base, exp);
40 inline __host__ __device__
double power(
double base,
int exp) {
41 return pow(base, exp);
59 inline __host__ __device__ float2
operator+(float2
a, float2
b)
65 a.x += b.x; a.y += b.y;
67 inline __host__ __device__ double2
operator+(double2
a, double2
b)
69 return make_double2(a.x + b.x, a.y + b.y);
73 a.x += b.x; a.y += b.y;
77 inline __host__ __device__ float2
operator-(float2
a, float2
b)
83 a.x -= b.x; a.y -= b.y;
87 inline __host__ __device__ float2
operator*(float2
a, float2
b)
96 inline __host__ __device__ float2
operator*(float2
a,
float s)
100 inline __host__ __device__ float2
operator*(
float s, float2
a)
108 inline __host__ __device__ double2
operator*(double2
a, double2
b)
110 return make_double2(a.x * b.x, a.y * b.y);
117 inline __host__ __device__ double2
operator*(double2
a,
float s)
119 return make_double2(a.x * s, a.y * s);
121 inline __host__ __device__ double2
operator*(
float s, double2
a)
123 return make_double2(a.x * s, a.y * s);
136 inline __host__ __device__ float2
operator/(float2
a,
float s)
138 float inv = 1.0f / s;
141 inline __host__ __device__ float2
operator/(
float s, float2
a)
147 float inv = 1.0f / s;
152 double inv = 1.0 / s;
156 inline __host__ __device__
float dot(float2
a, float2
b)
158 return a.x * b.x + a.y * b.y;
162 inline __host__ __device__
float length(float2 v)
164 return sqrtf(
dot(v, v));
170 float invLen = 1.0f / sqrtf(
dot(v, v));
175 inline __host__ __device__ float2
floor(
const float2 v)
181 inline __host__ __device__ float2
reflect(float2
i, float2
n)
183 return i - 2.0f * n *
dot(n,i);
188 return a < b ?
a :
b;
193 if (n == 0)
return 0;
195 while ((n & divider) == 0) divider <<= 1;
232 return make_float3(
float(a.x),
float(a.y),
float(a.z));
236 static __inline__ __host__ __device__ float3 fminf(float3
a, float3
b)
238 return make_float3(fminf(
a.x,b.x), fminf(
a.y,b.y), fminf(
a.z,b.z));
242 static __inline__ __host__ __device__ float3 fmaxf(float3
a, float3
b)
244 return make_float3(fmaxf(
a.x,b.x), fmaxf(
a.y,b.y), fmaxf(
a.z,b.z));
250 return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
258 a.x += b.x; a.y += b.y; a.z += b.z;
264 return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
272 a.x -= b.x; a.y -= b.y; a.z -= b.z;
278 return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
280 inline __host__ __device__ float3
operator*(float3
a,
float s)
284 inline __host__ __device__ float3
operator*(
float s, float3
a)
290 a.x *= s; a.y *= s; a.z *= s;
296 return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
298 inline __host__ __device__ float3
operator/(float3
a,
float s)
300 float inv = 1.0f / s;
303 inline __host__ __device__ float3
operator/(
float s, float3
a)
311 float inv = 1.0f / s;
316 inline __host__ __device__
float dot(float3
a, float3
b)
318 return a.x * b.x + a.y * b.y + a.z * b.z;
322 inline __host__ __device__ float3
cross(float3
a, float3
b)
324 return make_float3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
328 inline __host__ __device__
float length(float3 v)
330 return sqrtf(
dot(v, v));
336 float invLen = 1.0f / sqrtf(
dot(v, v));
341 inline __host__ __device__ float3
floor(
const float3 v)
347 inline __host__ __device__ float3
reflect(float3
i, float3
n)
349 return i - 2.0f * n *
dot(n,i);
353 template <
typename T>
354 inline __host__ __device__
355 static bool inRange(T
x, T
min, T
max) {
360 template<
typename T,
typename U>
361 inline __host__ __device__
362 static U clamp(U val, T
min, T
max) {
363 U res = (val >
max) ? max : val;
364 return (res <
min) ?
min : res;
368 template <
typename T>
371 return fma(t, v1, fma(-t, v0, v0));
378 T d1 =
lerp(y0, y1, tx);
379 return lerp(d0, d1, ty);
382 template<
typename T,
typename U>
384 inline T
biLerp(
const T * __restrict__ data,
int sizeX,
int sizeY, U
x, U
y) {
390 data[(y0 * sizeX) + x0],
391 data[(y0 * sizeX) + x1],
392 data[(y1 * sizeX) + x0],
393 data[(y1 * sizeX) + x1],
__host__ __device__ void operator+=(float2 &a, float2 b)
void min(Image< double > &op1, const Image< double > &op2)
__host__ __device__ float2 make_float2(float s)
__host__ __device__ float2 operator/(float2 a, float2 b)
__host__ __device__ float2 floor(const float2 v)
__host__ __device__ float3 make_float3(float s)
__host__ __device__ T lerp(T v0, T v1, T t)
__host__ __device__ T biLerp(T x0, T x1, T y0, T y1, T tx, T ty)
__host__ __device__ void operator-=(float2 &a, float2 b)
__host__ __device__ float2 operator*(float2 a, float2 b)
__host__ __device__ float2 reflect(float2 i, float2 n)
__host__ __device__ float3 cross(float3 a, float3 b)
void max(Image< double > &op1, const Image< double > &op2)
__device__ __host__ uint UMIN(uint a, uint b)
__host__ __device__ float length(float2 v)
__host__ __device__ float2 operator-(float2 a, float2 b)
__host__ __device__ float power(float base, int exp)
__device__ __host__ uint PowTwoDivider(uint n)
__host__ __device__ float2 operator+(float2 a, float2 b)
__host__ __device__ float2 normalize(float2 v)
__host__ __device__ void operator*=(float2 &a, float2 b)
__host__ __device__ void operator/=(float2 &a, float s)
__host__ __device__ float dot(float2 a, float2 b)