5 #include <helper_math.h>
10 template<
typename Type>
11 inline __host__ __device__ Type sqr(Type a) {
return a*a;}
17 inline __host__ __device__ uchar2 operator+(uchar2 a, uchar2 b)
19 return make_uchar2(a.x + b.x, a.y + b.y);
22 inline __host__ __device__ uchar3 operator+(uchar3 a, uchar3 b)
24 return make_uchar3(a.x + b.x, a.y + b.y, a.z + b.z);
27 inline __host__ __device__ uchar4 operator+(uchar4 a, uchar4 b)
29 return make_uchar4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
37 inline __host__ __device__ uchar2 operator*(uchar2 a, uchar2 b)
39 return make_uchar2(a.x * b.x, a.y * b.y);
42 inline __host__ __device__ uchar3 operator*(uchar3 a, uchar3 b)
44 return make_uchar3(a.x * b.x, a.y * b.y, a.z * b.z);
47 inline __host__ __device__ uchar4 operator*(uchar4 a, uchar4 b)
49 return make_uchar4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
56 inline __host__ std::ostream& operator<< (std::ostream & out, float2
const& v)
58 out <<
"[" << v.x <<
", " << v.y <<
"]";
62 inline __host__ std::ostream& operator<< (std::ostream & out, double2
const& v)
64 out <<
"[" << v.x <<
", " << v.y <<
"]";
68 inline __host__ std::ostream& operator<< (std::ostream & out, int2
const& v)
70 out <<
"[" << v.x <<
", " << v.y <<
"]";
74 inline __host__ std::ostream& operator<< (std::ostream & out, uint2
const& v)
76 out <<
"[" << v.x <<
", " << v.y <<
"]";
80 inline __host__ std::ostream& operator<< (std::ostream & out, float3
const& v)
82 out <<
"[" << v.x <<
", " << v.y <<
", " << v.z <<
"]";
86 inline __host__ std::ostream& operator<< (std::ostream & out, double3
const& v)
88 out <<
"[" << v.x <<
", " << v.y <<
", " << v.z <<
"]";
92 inline __host__ std::ostream& operator<< (std::ostream & out, int3
const& v)
94 out <<
"[" << v.x <<
", " << v.y <<
", " << v.z <<
"]";
98 inline __host__ std::ostream& operator<< (std::ostream & out, uint3
const& v)
100 out <<
"[" << v.x <<
", " << v.y <<
", " << v.z <<
"]";
104 inline __host__ std::ostream& operator<< (std::ostream & out, float4
const& v)
106 out <<
"[" << v.x <<
", " << v.y <<
", " << v.z <<
", " << v.w <<
"]";
110 inline __host__ std::ostream& operator<< (std::ostream & out, double4
const& v)
112 out <<
"[" << v.x <<
", " << v.y <<
", " << v.z <<
", " << v.w <<
"]";
116 inline __host__ std::ostream& operator<< (std::ostream & out, int4
const& v)
118 out <<
"[" << v.x <<
", " << v.y <<
", " << v.z <<
", " << v.w <<
"]";
122 inline __host__ std::ostream& operator<< (std::ostream & out, uint4
const& v)
124 out <<
"[" << v.x <<
", " << v.y <<
", " << v.z <<
", " << v.w <<
"]";
136 inline double dmind(
double a,
double b)
138 return a < b ? a : b;
141 inline double dmaxd(
double a,
double b)
143 return a > b ? a : b;
146 inline int dmind(
int a,
int b)
148 return a < b ? a : b;
155 inline __host__ __device__ double2 make_double2(
double s)
157 return make_double2(s, s);
159 inline __host__ __device__ double2 make_double2(double3 a)
161 return make_double2(a.x, a.y);
163 inline __host__ __device__ double2 make_double2(int2 a)
165 return make_double2(
double(a.x),
double(a.y));
167 inline __host__ __device__ double2 make_double2(uint2 a)
169 return make_double2(
double(a.x),
double(a.y));
171 inline __host__ __device__ double3 make_double3(
double s)
173 return make_double3(s, s, s);
175 inline __host__ __device__ double3 make_double3(double2 a)
177 return make_double3(a.x, a.y, 0.0f);
179 inline __host__ __device__ double3 make_double3(double2 a,
double s)
181 return make_double3(a.x, a.y, s);
183 inline __host__ __device__ double3 make_double3(double4 a)
185 return make_double3(a.x, a.y, a.z);
187 inline __host__ __device__ double3 make_double3(int3 a)
189 return make_double3(
double(a.x),
double(a.y),
double(a.z));
191 inline __host__ __device__ double3 make_double3(uint3 a)
193 return make_double3(
double(a.x),
double(a.y),
double(a.z));
195 inline __host__ __device__ double4 make_double4(
double s)
197 return make_double4(s, s, s, s);
199 inline __host__ __device__ double4 make_double4(double3 a)
201 return make_double4(a.x, a.y, a.z, 0.0f);
203 inline __host__ __device__ double4 make_double4(double3 a,
double w)
205 return make_double4(a.x, a.y, a.z, w);
207 inline __host__ __device__ double4 make_double4(int4 a)
209 return make_double4(
double(a.x),
double(a.y),
double(a.z),
double(a.w));
211 inline __host__ __device__ double4 make_double4(uint4 a)
213 return make_double4(
double(a.x),
double(a.y),
double(a.z),
double(a.w));
220 inline __host__ __device__ double2 operator-(double2 &a)
222 return make_double2(-a.x, -a.y);
224 inline __host__ __device__ double3 operator-(double3 &a)
226 return make_double3(-a.x, -a.y, -a.z);
228 inline __host__ __device__ double4 operator-(double4 &a)
230 return make_double4(-a.x, -a.y, -a.z, -a.w);
237 inline __host__ __device__ double2 operator+(double2 a, double2 b)
239 return make_double2(a.x + b.x, a.y + b.y);
241 inline __host__ __device__
void operator+=(double2 &a, double2 b)
246 inline __host__ __device__ double2 operator+(double2 a,
double b)
248 return make_double2(a.x + b, a.y + b);
250 inline __host__ __device__ double2 operator+(
double b, double2 a)
252 return make_double2(a.x + b, a.y + b);
254 inline __host__ __device__
void operator+=(double2 &a,
double b)
259 inline __host__ __device__ double3 operator+(double3 a, double3 b)
261 return make_double3(a.x + b.x, a.y + b.y, a.z + b.z);
263 inline __host__ __device__
void operator+=(double3 &a, double3 b)
269 inline __host__ __device__ double3 operator+(double3 a,
double b)
271 return make_double3(a.x + b, a.y + b, a.z + b);
273 inline __host__ __device__
void operator+=(double3 &a,
double b)
280 inline __host__ __device__ double3 operator+(
double b, double3 a)
282 return make_double3(a.x + b, a.y + b, a.z + b);
285 inline __host__ __device__ double4 operator+(double4 a, double4 b)
287 return make_double4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
289 inline __host__ __device__
void operator+=(double4 &a, double4 b)
296 inline __host__ __device__ double4 operator+(double4 a,
double b)
298 return make_double4(a.x + b, a.y + b, a.z + b, a.w + b);
300 inline __host__ __device__ double4 operator+(
double b, double4 a)
302 return make_double4(a.x + b, a.y + b, a.z + b, a.w + b);
304 inline __host__ __device__
void operator+=(double4 &a,
double b)
316 inline __host__ __device__ double2 operator-(double2 a, double2 b)
318 return make_double2(a.x - b.x, a.y - b.y);
320 inline __host__ __device__
void operator-=(double2 &a, double2 b)
325 inline __host__ __device__ double2 operator-(double2 a,
double b)
327 return make_double2(a.x - b, a.y - b);
329 inline __host__ __device__ double2 operator-(
double b, double2 a)
331 return make_double2(b - a.x, b - a.y);
333 inline __host__ __device__
void operator-=(double2 &a,
double b)
339 inline __host__ __device__ double3 operator-(double3 a, double3 b)
341 return make_double3(a.x - b.x, a.y - b.y, a.z - b.z);
343 inline __host__ __device__
void operator-=(double3 &a, double3 b)
349 inline __host__ __device__ double3 operator-(double3 a,
double b)
351 return make_double3(a.x - b, a.y - b, a.z - b);
353 inline __host__ __device__ double3 operator-(
double b, double3 a)
355 return make_double3(b - a.x, b - a.y, b - a.z);
357 inline __host__ __device__
void operator-=(double3 &a,
double b)
364 inline __host__ __device__ double4 operator-(double4 a, double4 b)
366 return make_double4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
368 inline __host__ __device__
void operator-=(double4 &a, double4 b)
375 inline __host__ __device__ double4 operator-(double4 a,
double b)
377 return make_double4(a.x - b, a.y - b, a.z - b, a.w - b);
379 inline __host__ __device__
void operator-=(double4 &a,
double b)
391 inline __host__ __device__ double2 operator*(double2 a, double2 b)
393 return make_double2(a.x * b.x, a.y * b.y);
395 inline __host__ __device__
void operator*=(double2 &a, double2 b)
400 inline __host__ __device__ double2 operator*(double2 a,
double b)
402 return make_double2(a.x * b, a.y * b);
404 inline __host__ __device__ double2 operator*(
double b, double2 a)
406 return make_double2(b * a.x, b * a.y);
408 inline __host__ __device__
void operator*=(double2 &a,
double b)
414 inline __host__ __device__ double3 operator*(double3 a, double3 b)
416 return make_double3(a.x * b.x, a.y * b.y, a.z * b.z);
418 inline __host__ __device__
void operator*=(double3 &a, double3 b)
424 inline __host__ __device__ double3 operator*(double3 a,
double b)
426 return make_double3(a.x * b, a.y * b, a.z * b);
428 inline __host__ __device__ double3 operator*(
double b, double3 a)
430 return make_double3(b * a.x, b * a.y, b * a.z);
432 inline __host__ __device__
void operator*=(double3 &a,
double b)
439 inline __host__ __device__ double4 operator*(double4 a, double4 b)
441 return make_double4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
443 inline __host__ __device__
void operator*=(double4 &a, double4 b)
450 inline __host__ __device__ double4 operator*(double4 a,
double b)
452 return make_double4(a.x * b, a.y * b, a.z * b, a.w * b);
454 inline __host__ __device__ double4 operator*(
double b, double4 a)
456 return make_double4(b * a.x, b * a.y, b * a.z, b * a.w);
458 inline __host__ __device__
void operator*=(double4 &a,
double b)
470 inline __host__ __device__ double2 operator/(double2 a, double2 b)
472 return make_double2(a.x / b.x, a.y / b.y);
474 inline __host__ __device__
void operator/=(double2 &a, double2 b)
479 inline __host__ __device__ double2 operator/(double2 a,
double b)
481 return make_double2(a.x / b, a.y / b);
483 inline __host__ __device__
void operator/=(double2 &a,
double b)
488 inline __host__ __device__ double2 operator/(
double b, double2 a)
490 return make_double2(b / a.x, b / a.y);
493 inline __host__ __device__ double3 operator/(double3 a, double3 b)
495 return make_double3(a.x / b.x, a.y / b.y, a.z / b.z);
497 inline __host__ __device__
void operator/=(double3 &a, double3 b)
503 inline __host__ __device__ double3 operator/(double3 a,
double b)
505 return make_double3(a.x / b, a.y / b, a.z / b);
507 inline __host__ __device__
void operator/=(double3 &a,
double b)
513 inline __host__ __device__ double3 operator/(
double b, double3 a)
515 return make_double3(b / a.x, b / a.y, b / a.z);
518 inline __host__ __device__ double4 operator/(double4 a, double4 b)
520 return make_double4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
522 inline __host__ __device__
void operator/=(double4 &a, double4 b)
529 inline __host__ __device__ double4 operator/(double4 a,
double b)
531 return make_double4(a.x / b, a.y / b, a.z / b, a.w / b);
533 inline __host__ __device__
void operator/=(double4 &a,
double b)
540 inline __host__ __device__ double4 operator/(
double b, double4 a)
542 return make_double4(b / a.x, b / a.y, b / a.z, b / a.w);
549 inline __host__ __device__ double2 dmind(double2 a, double2 b)
551 return make_double2(fmin(a.x,b.x), fmin(a.y,b.y));
553 inline __host__ __device__ double3 dmind(double3 a, double3 b)
555 return make_double3(fmin(a.x,b.x), fmin(a.y,b.y), fmin(a.z,b.z));
557 inline __host__ __device__ double4 dmind(double4 a, double4 b)
559 return make_double4(fmin(a.x,b.x), fmin(a.y,b.y), fmin(a.z,b.z), fmin(a.w,b.w));
566 inline __host__ __device__ double2 dmaxd(double2 a, double2 b)
568 return make_double2(fmax(a.x,b.x), fmax(a.y,b.y));
570 inline __host__ __device__ double3 dmaxd(double3 a, double3 b)
572 return make_double3(fmax(a.x,b.x), fmax(a.y,b.y), fmax(a.z,b.z));
574 inline __host__ __device__ double4 dmaxd(double4 a, double4 b)
576 return make_double4(fmax(a.x,b.x), fmax(a.y,b.y), fmax(a.z,b.z), fmax(a.w,b.w));
584 inline __device__ __host__
double lerp(
double a,
double b,
double t)
588 inline __device__ __host__ double2 lerp(double2 a, double2 b,
double t)
592 inline __device__ __host__ double3 lerp(double3 a, double3 b,
double t)
596 inline __device__ __host__ double4 lerp(double4 a, double4 b,
double t)
606 inline __device__ __host__
double clamp(
double f,
double a,
double b)
608 return fmax(a, fmin(f, b));
610 inline __device__ __host__ double2 clamp(double2 v,
double a,
double b)
612 return make_double2(clamp(v.x, a, b), clamp(v.y, a, b));
614 inline __device__ __host__ double2 clamp(double2 v, double2 a, double2 b)
616 return make_double2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y));
618 inline __device__ __host__ double3 clamp(double3 v,
double a,
double b)
620 return make_double3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b));
622 inline __device__ __host__ double3 clamp(double3 v, double3 a, double3 b)
624 return make_double3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z));
626 inline __device__ __host__ double4 clamp(double4 v,
double a,
double b)
628 return make_double4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b));
630 inline __device__ __host__ double4 clamp(double4 v, double4 a, double4 b)
632 return make_double4(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z), clamp(v.w, a.w, b.w));
639 inline __host__ __device__
double dot(double2 a, double2 b)
641 return a.x * b.x + a.y * b.y;
643 inline __host__ __device__
double dot(double3 a, double3 b)
645 return a.x * b.x + a.y * b.y + a.z * b.z;
647 inline __host__ __device__
double dot(double4 a, double4 b)
649 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
656 inline __host__ __device__
double length(double2 v)
658 return sqrt(dot(v, v));
660 inline __host__ __device__
double length(double3 v)
662 return sqrt(dot(v, v));
664 inline __host__ __device__
double length(double4 v)
666 return sqrt(dot(v, v));
673 inline __host__ __device__ double2 normalize(double2 v)
675 double invLen = 1.0f / sqrt(dot(v, v));
678 inline __host__ __device__ double3 normalize(double3 v)
680 double invLen = 1.0f / sqrt(dot(v, v));
683 inline __host__ __device__ double4 normalize(double4 v)
685 double invLen = 1.0f / sqrt(dot(v, v));
693 inline __host__ __device__ double2 floor(double2 v)
695 return make_double2(floor(v.x), floor(v.y));
697 inline __host__ __device__ double3 floor(double3 v)
699 return make_double3(floor(v.x), floor(v.y), floor(v.z));
701 inline __host__ __device__ double4 floor(double4 v)
703 return make_double4(floor(v.x), floor(v.y), floor(v.z), floor(v.w));
710 inline __host__ __device__
double frac(
double v)
714 inline __host__ __device__ double2 frac(double2 v)
716 return make_double2(frac(v.x), frac(v.y));
718 inline __host__ __device__ double3 frac(double3 v)
720 return make_double3(frac(v.x), frac(v.y), frac(v.z));
722 inline __host__ __device__ double4 frac(double4 v)
724 return make_double4(frac(v.x), frac(v.y), frac(v.z), frac(v.w));
731 inline __host__ __device__ double2 fmod(double2 a, double2 b)
733 return make_double2(fmod(a.x, b.x), fmod(a.y, b.y));
735 inline __host__ __device__ double3 fmod(double3 a, double3 b)
737 return make_double3(fmod(a.x, b.x), fmod(a.y, b.y), fmod(a.z, b.z));
739 inline __host__ __device__ double4 fmod(double4 a, double4 b)
741 return make_double4(fmod(a.x, b.x), fmod(a.y, b.y), fmod(a.z, b.z), fmod(a.w, b.w));
748 inline __host__ __device__ double2
abs(double2 v)
750 return make_double2(fabs(v.x), fabs(v.y));
752 inline __host__ __device__ double3
abs(double3 v)
754 return make_double3(fabs(v.x), fabs(v.y), fabs(v.z));
756 inline __host__ __device__ double4
abs(double4 v)
758 return make_double4(fabs(v.x), fabs(v.y), fabs(v.z), fabs(v.w));
768 inline __host__ __device__ double3 reflect(double3 i, double3 n)
770 return i - 2.0f * n * dot(n,i);
777 inline __host__ __device__ double3 cross(double3 a, double3 b)
779 return make_double3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
789 inline __device__ __host__
double smoothstep(
double a,
double b,
double x)
791 double y = clamp((x - a) / (b - a), 0.0, 1.0);
792 return (y*y*(3.0 - (2.0*y)));
794 inline __device__ __host__ double2 smoothstep(double2 a, double2 b, double2 x)
796 double2 y = clamp((x - a) / (b - a), 0.0, 1.0);
797 return (y*y*(make_double2(3.0) - (make_double2(2.0)*y)));
799 inline __device__ __host__ double3 smoothstep(double3 a, double3 b, double3 x)
801 double3 y = clamp((x - a) / (b - a), 0.0, 1.0);
802 return (y*y*(make_double3(3.0f) - (make_double3(2.0)*y)));
804 inline __device__ __host__ double4 smoothstep(double4 a, double4 b, double4 x)
806 double4 y = clamp((x - a) / (b - a), 0.0, 1.0);
807 return (y*y*(make_double4(3.0) - (make_double4(2.0)*y)));
IUMATH_DLLAPI void abs(iu::VolumeCpu_32f_C2 &complex, iu::VolumeCpu_32f_C1 &real)