00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #ifndef CUTIL_MATH_BUGFIXES_H
00047 #define CUTIL_MATH_BUGFIXES_H
00048
00049 #ifdef CUTIL_MATH_H //Danny
00050
00051
00052
00053 #error cutil_math_bugfixes.h has to be included before cutil_math.h!!!
00054 #endif
00055 #define CUTIL_MATH_H //make sure that the buggy cutil_math.h will not be included
00056
00057 #include "cuda_runtime.h"
00058
00060 typedef unsigned int uint;
00061 typedef unsigned short ushort;
00062
00063 #ifndef __CUDACC__
00064 #include <math.h>
00065
00066 inline float fminf(float a, float b)
00067 {
00068 return a < b ? a : b;
00069 }
00070
00071 inline float fmaxf(float a, float b)
00072 {
00073 return a < b ? a : b;
00074 }
00075
00076 inline int max(int a, int b)
00077 {
00078 return a > b ? a : b;
00079 }
00080
00081 inline int min(int a, int b)
00082 {
00083 return a < b ? a : b;
00084 }
00085 #endif
00086
00087
00089
00090
00091 inline __device__ __host__ float lerp(float a, float b, float t)
00092 {
00093 return a + t*(b-a);
00094 }
00095
00096
00097 inline __device__ __host__ float clamp(float f, float a, float b)
00098 {
00099 return fmaxf(a, fminf(f, b));
00100 }
00101
00102
00104
00105
00106 inline __host__ __device__ int2 operator+(int2 a, int2 b)
00107 {
00108 return make_int2(a.x + b.x, a.y + b.y);
00109 }
00110 inline __host__ __device__ void operator+=(int2 &a, int2 b)
00111 {
00112 a.x += b.x; a.y += b.y;
00113 }
00114
00115
00116 inline __host__ __device__ int2 operator-(int2 a, int2 b)
00117 {
00118 return make_int2(a.x - b.x, a.y - b.y);
00119 }
00120 inline __host__ __device__ void operator-=(int2 &a, int2 b)
00121 {
00122 a.x -= b.x; a.y -= b.y;
00123 }
00124
00125
00126 inline __host__ __device__ int2 operator*(int2 a, int2 b)
00127 {
00128 return make_int2(a.x * b.x, a.y * b.y);
00129 }
00130 inline __host__ __device__ int2 operator*(int2 a, int s)
00131 {
00132 return make_int2(a.x * s, a.y * s);
00133 }
00134 inline __host__ __device__ int2 operator*(int s, int2 a)
00135 {
00136 return make_int2(a.x * s, a.y * s);
00137 }
00138 inline __host__ __device__ void operator*=(int2 &a, int s)
00139 {
00140 a.x *= s; a.y *= s;
00141 }
00142
00143
00145
00146
00147 inline __host__ __device__ float2 make_float2(float s)
00148 {
00149 return make_float2(s, s);
00150 }
00151 inline __host__ __device__ float2 make_float2(int2 a)
00152 {
00153 return make_float2(float(a.x), float(a.y));
00154 }
00155
00156
00157 inline __host__ __device__ float2 operator+(float2 a, float2 b)
00158 {
00159 return make_float2(a.x + b.x, a.y + b.y);
00160 }
00161 inline __host__ __device__ void operator+=(float2 &a, float2 b)
00162 {
00163 a.x += b.x; a.y += b.y;
00164 }
00165
00166
00167 inline __host__ __device__ float2 operator-(float2 a, float2 b)
00168 {
00169 return make_float2(a.x - b.x, a.y - b.y);
00170 }
00171 inline __host__ __device__ void operator-=(float2 &a, float2 b)
00172 {
00173 a.x -= b.x; a.y -= b.y;
00174 }
00175
00176
00177 inline __host__ __device__ float2 operator*(float2 a, float2 b)
00178 {
00179 return make_float2(a.x * b.x, a.y * b.y);
00180 }
00181 inline __host__ __device__ float2 operator*(float2 a, float s)
00182 {
00183 return make_float2(a.x * s, a.y * s);
00184 }
00185 inline __host__ __device__ float2 operator*(float s, float2 a)
00186 {
00187 return make_float2(a.x * s, a.y * s);
00188 }
00189 inline __host__ __device__ void operator*=(float2 &a, float s)
00190 {
00191 a.x *= s; a.y *= s;
00192 }
00193
00194
00195 inline __host__ __device__ float2 operator/(float2 a, float2 b)
00196 {
00197 return make_float2(a.x / b.x, a.y / b.y);
00198 }
00199 inline __host__ __device__ float2 operator/(float2 a, float s)
00200 {
00201 float inv = 1.0f / s;
00202 return a * inv;
00203 }
00204 inline __host__ __device__ float2 operator/(float s, float2 a)
00205 {
00206
00207
00208 return make_float2(s / a.x, s / a.y);
00209 }
00210 inline __host__ __device__ void operator/=(float2 &a, float s)
00211 {
00212 float inv = 1.0f / s;
00213 a *= inv;
00214 }
00215
00216
00217 inline __device__ __host__ float2 lerp(float2 a, float2 b, float t)
00218 {
00219 return a + t*(b-a);
00220 }
00221
00222
00223 inline __device__ __host__ float2 clamp(float2 v, float a, float b)
00224 {
00225 return make_float2(clamp(v.x, a, b), clamp(v.y, a, b));
00226 }
00227
00228 inline __device__ __host__ float2 clamp(float2 v, float2 a, float2 b)
00229 {
00230 return make_float2(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y));
00231 }
00232
00233
00234 inline __host__ __device__ float dot(float2 a, float2 b)
00235 {
00236 return a.x * b.x + a.y * b.y;
00237 }
00238
00239
00240 inline __host__ __device__ float length(float2 v)
00241 {
00242 return sqrtf(dot(v, v));
00243 }
00244
00245
00246 inline __host__ __device__ float2 normalize(float2 v)
00247 {
00248 float invLen = 1.0f / sqrtf(dot(v, v));
00249 return v * invLen;
00250 }
00251
00252
00253 inline __host__ __device__ float2 floor(const float2 v)
00254 {
00255 return make_float2(floor(v.x), floor(v.y));
00256 }
00257
00258
00259 inline __host__ __device__ float2 reflect(float2 i, float2 n)
00260 {
00261 return i - 2.0f * n * dot(n,i);
00262 }
00263
00264
00266
00267
00268 inline __host__ __device__ float3 make_float3(float s)
00269 {
00270 return make_float3(s, s, s);
00271 }
00272 inline __host__ __device__ float3 make_float3(float2 a)
00273 {
00274 return make_float3(a.x, a.y, 0.0f);
00275 }
00276 inline __host__ __device__ float3 make_float3(float2 a, float s)
00277 {
00278 return make_float3(a.x, a.y, s);
00279 }
00280 inline __host__ __device__ float3 make_float3(float4 a)
00281 {
00282 return make_float3(a.x, a.y, a.z);
00283 }
00284 inline __host__ __device__ float3 make_float3(int3 a)
00285 {
00286 return make_float3(float(a.x), float(a.y), float(a.z));
00287 }
00288
00289
00290 static __inline__ __host__ __device__ float3 fminf(float3 a, float3 b)
00291 {
00292 return make_float3(fminf(a.x,b.x), fminf(a.y,b.y), fminf(a.z,b.z));
00293 }
00294
00295
00296 static __inline__ __host__ __device__ float3 fmaxf(float3 a, float3 b)
00297 {
00298 return make_float3(fmaxf(a.x,b.x), fmaxf(a.y,b.y), fmaxf(a.z,b.z));
00299 }
00300
00301
00302 inline __host__ __device__ float3 operator+(float3 a, float3 b)
00303 {
00304 return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
00305 }
00306 inline __host__ __device__ float3 operator+(float3 a, float b)
00307 {
00308 return make_float3(a.x + b, a.y + b, a.z + b);
00309 }
00310 inline __host__ __device__ void operator+=(float3 &a, float3 b)
00311 {
00312 a.x += b.x; a.y += b.y; a.z += b.z;
00313 }
00314
00315
00316 inline __host__ __device__ float3 operator-(float3 a, float3 b)
00317 {
00318 return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
00319 }
00320 inline __host__ __device__ float3 operator-(float3 a, float b)
00321 {
00322 return make_float3(a.x - b, a.y - b, a.z - b);
00323 }
00324 inline __host__ __device__ void operator-=(float3 &a, float3 b)
00325 {
00326 a.x -= b.x; a.y -= b.y; a.z -= b.z;
00327 }
00328
00329
00330 inline __host__ __device__ float3 operator*(float3 a, float3 b)
00331 {
00332 return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
00333 }
00334 inline __host__ __device__ float3 operator*(float3 a, float s)
00335 {
00336 return make_float3(a.x * s, a.y * s, a.z * s);
00337 }
00338 inline __host__ __device__ float3 operator*(float s, float3 a)
00339 {
00340 return make_float3(a.x * s, a.y * s, a.z * s);
00341 }
00342 inline __host__ __device__ void operator*=(float3 &a, float s)
00343 {
00344 a.x *= s; a.y *= s; a.z *= s;
00345 }
00346
00347
00348 inline __host__ __device__ float3 operator/(float3 a, float3 b)
00349 {
00350 return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
00351 }
00352 inline __host__ __device__ float3 operator/(float3 a, float s)
00353 {
00354 float inv = 1.0f / s;
00355 return a * inv;
00356 }
00357 inline __host__ __device__ float3 operator/(float s, float3 a)
00358 {
00359
00360
00361 return make_float3(s / a.x, s / a.y, s / a.z);
00362 }
00363 inline __host__ __device__ void operator/=(float3 &a, float s)
00364 {
00365 float inv = 1.0f / s;
00366 a *= inv;
00367 }
00368
00369
00370 inline __device__ __host__ float3 lerp(float3 a, float3 b, float t)
00371 {
00372 return a + t*(b-a);
00373 }
00374
00375
00376 inline __device__ __host__ float3 clamp(float3 v, float a, float b)
00377 {
00378 return make_float3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b));
00379 }
00380
00381 inline __device__ __host__ float3 clamp(float3 v, float3 a, float3 b)
00382 {
00383 return make_float3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z));
00384 }
00385
00386
00387 inline __host__ __device__ float dot(float3 a, float3 b)
00388 {
00389 return a.x * b.x + a.y * b.y + a.z * b.z;
00390 }
00391
00392
00393 inline __host__ __device__ float3 cross(float3 a, float3 b)
00394 {
00395 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);
00396 }
00397
00398
00399 inline __host__ __device__ float length(float3 v)
00400 {
00401 return sqrtf(dot(v, v));
00402 }
00403
00404
00405 inline __host__ __device__ float3 normalize(float3 v)
00406 {
00407 float invLen = 1.0f / sqrtf(dot(v, v));
00408 return v * invLen;
00409 }
00410
00411
00412 inline __host__ __device__ float3 floor(const float3 v)
00413 {
00414 return make_float3(floor(v.x), floor(v.y), floor(v.z));
00415 }
00416
00417
00418 inline __host__ __device__ float3 reflect(float3 i, float3 n)
00419 {
00420 return i - 2.0f * n * dot(n,i);
00421 }
00422
00423
00425
00426
00427 inline __host__ __device__ float4 make_float4(float s)
00428 {
00429 return make_float4(s, s, s, s);
00430 }
00431 inline __host__ __device__ float4 make_float4(float3 a)
00432 {
00433 return make_float4(a.x, a.y, a.z, 0.0f);
00434 }
00435 inline __host__ __device__ float4 make_float4(float3 a, float w)
00436 {
00437 return make_float4(a.x, a.y, a.z, w);
00438 }
00439 inline __host__ __device__ float4 make_float4(int4 a)
00440 {
00441 return make_float4(float(a.x), float(a.y), float(a.z), float(a.w));
00442 }
00443
00444
00445 static __inline__ __host__ __device__ float4 fminf(float4 a, float4 b)
00446 {
00447 return make_float4(fminf(a.x,b.x), fminf(a.y,b.y), fminf(a.z,b.z), fminf(a.w,b.w));
00448 }
00449
00450
00451 static __inline__ __host__ __device__ float4 fmaxf(float4 a, float4 b)
00452 {
00453 return make_float4(fmaxf(a.x,b.x), fmaxf(a.y,b.y), fmaxf(a.z,b.z), fmaxf(a.w,b.w));
00454 }
00455
00456
00457 inline __host__ __device__ float4 operator+(float4 a, float4 b)
00458 {
00459 return make_float4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
00460 }
00461 inline __host__ __device__ void operator+=(float4 &a, float4 b)
00462 {
00463 a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w;
00464 }
00465
00466
00467 inline __host__ __device__ float4 operator-(float4 a, float4 b)
00468 {
00469 return make_float4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
00470 }
00471 inline __host__ __device__ void operator-=(float4 &a, float4 b)
00472 {
00473 a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w;
00474 }
00475
00476
00477 inline __host__ __device__ float4 operator*(float4 a, float s)
00478 {
00479 return make_float4(a.x * s, a.y * s, a.z * s, a.w * s);
00480 }
00481 inline __host__ __device__ float4 operator*(float s, float4 a)
00482 {
00483 return make_float4(a.x * s, a.y * s, a.z * s, a.w * s);
00484 }
00485 inline __host__ __device__ void operator*=(float4 &a, float s)
00486 {
00487 a.x *= s; a.y *= s; a.z *= s; a.w *= s;
00488 }
00489
00490
00491 inline __host__ __device__ float4 operator/(float4 a, float4 b)
00492 {
00493 return make_float4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
00494 }
00495 inline __host__ __device__ float4 operator/(float4 a, float s)
00496 {
00497 float inv = 1.0f / s;
00498 return a * inv;
00499 }
00500 inline __host__ __device__ float4 operator/(float s, float4 a)
00501 {
00502
00503
00504 return make_float4(s / a.x, s / a.y, s / a.z, s / a.w);
00505 }
00506 inline __host__ __device__ void operator/=(float4 &a, float s)
00507 {
00508 float inv = 1.0f / s;
00509 a *= inv;
00510 }
00511
00512
00513 inline __device__ __host__ float4 lerp(float4 a, float4 b, float t)
00514 {
00515 return a + t*(b-a);
00516 }
00517
00518
00519 inline __device__ __host__ float4 clamp(float4 v, float a, float b)
00520 {
00521 return make_float4(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b), clamp(v.w, a, b));
00522 }
00523
00524 inline __device__ __host__ float4 clamp(float4 v, float4 a, float4 b)
00525 {
00526 return make_float4(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));
00527 }
00528
00529
00530 inline __host__ __device__ float dot(float4 a, float4 b)
00531 {
00532 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
00533 }
00534
00535
00536 inline __host__ __device__ float length(float4 r)
00537 {
00538 return sqrtf(dot(r, r));
00539 }
00540
00541
00542 inline __host__ __device__ float4 normalize(float4 v)
00543 {
00544 float invLen = 1.0f / sqrtf(dot(v, v));
00545 return v * invLen;
00546 }
00547
00548
00549 inline __host__ __device__ float4 floor(const float4 v)
00550 {
00551 return make_float4(floor(v.x), floor(v.y), floor(v.z), floor(v.w));
00552 }
00553
00554
00556
00557
00558 inline __host__ __device__ int3 make_int3(int s)
00559 {
00560 return make_int3(s, s, s);
00561 }
00562 inline __host__ __device__ int3 make_int3(float3 a)
00563 {
00564 return make_int3(int(a.x), int(a.y), int(a.z));
00565 }
00566
00567
00568 inline __host__ __device__ int3 min(int3 a, int3 b)
00569 {
00570 return make_int3(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z));
00571 }
00572
00573
00574 inline __host__ __device__ int3 max(int3 a, int3 b)
00575 {
00576 return make_int3(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z));
00577 }
00578
00579
00580 inline __host__ __device__ int3 operator+(int3 a, int3 b)
00581 {
00582 return make_int3(a.x + b.x, a.y + b.y, a.z + b.z);
00583 }
00584 inline __host__ __device__ void operator+=(int3 &a, int3 b)
00585 {
00586 a.x += b.x; a.y += b.y; a.z += b.z;
00587 }
00588
00589
00590 inline __host__ __device__ int3 operator-(int3 a, int3 b)
00591 {
00592 return make_int3(a.x - b.x, a.y - b.y, a.z - b.z);
00593 }
00594
00595 inline __host__ __device__ void operator-=(int3 &a, int3 b)
00596 {
00597 a.x -= b.x; a.y -= b.y; a.z -= b.z;
00598 }
00599
00600
00601 inline __host__ __device__ int3 operator*(int3 a, int3 b)
00602 {
00603 return make_int3(a.x * b.x, a.y * b.y, a.z * b.z);
00604 }
00605 inline __host__ __device__ int3 operator*(int3 a, int s)
00606 {
00607 return make_int3(a.x * s, a.y * s, a.z * s);
00608 }
00609 inline __host__ __device__ int3 operator*(int s, int3 a)
00610 {
00611 return make_int3(a.x * s, a.y * s, a.z * s);
00612 }
00613 inline __host__ __device__ void operator*=(int3 &a, int s)
00614 {
00615 a.x *= s; a.y *= s; a.z *= s;
00616 }
00617
00618
00619 inline __host__ __device__ int3 operator/(int3 a, int3 b)
00620 {
00621 return make_int3(a.x / b.x, a.y / b.y, a.z / b.z);
00622 }
00623 inline __host__ __device__ int3 operator/(int3 a, int s)
00624 {
00625 return make_int3(a.x / s, a.y / s, a.z / s);
00626 }
00627 inline __host__ __device__ int3 operator/(int s, int3 a)
00628 {
00629 return make_int3(a.x / s, a.y / s, a.z / s);
00630 }
00631 inline __host__ __device__ void operator/=(int3 &a, int s)
00632 {
00633 a.x /= s; a.y /= s; a.z /= s;
00634 }
00635
00636
00637 inline __device__ __host__ int clamp(int f, int a, int b)
00638 {
00639 return max(a, min(f, b));
00640 }
00641
00642 inline __device__ __host__ int3 clamp(int3 v, int a, int b)
00643 {
00644 return make_int3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b));
00645 }
00646
00647 inline __device__ __host__ int3 clamp(int3 v, int3 a, int3 b)
00648 {
00649 return make_int3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z));
00650 }
00651
00652
00653
00655
00656
00657 inline __host__ __device__ uint3 make_uint3(uint s)
00658 {
00659 return make_uint3(s, s, s);
00660 }
00661 inline __host__ __device__ uint3 make_uint3(float3 a)
00662 {
00663 return make_uint3(uint(a.x), uint(a.y), uint(a.z));
00664 }
00665
00666
00667 inline __host__ __device__ uint3 min(uint3 a, uint3 b)
00668 {
00669 return make_uint3(min(a.x,b.x), min(a.y,b.y), min(a.z,b.z));
00670 }
00671
00672
00673 inline __host__ __device__ uint3 max(uint3 a, uint3 b)
00674 {
00675 return make_uint3(max(a.x,b.x), max(a.y,b.y), max(a.z,b.z));
00676 }
00677
00678
00679 inline __host__ __device__ uint3 operator+(uint3 a, uint3 b)
00680 {
00681 return make_uint3(a.x + b.x, a.y + b.y, a.z + b.z);
00682 }
00683 inline __host__ __device__ void operator+=(uint3 &a, uint3 b)
00684 {
00685 a.x += b.x; a.y += b.y; a.z += b.z;
00686 }
00687
00688
00689 inline __host__ __device__ uint3 operator-(uint3 a, uint3 b)
00690 {
00691 return make_uint3(a.x - b.x, a.y - b.y, a.z - b.z);
00692 }
00693
00694 inline __host__ __device__ void operator-=(uint3 &a, uint3 b)
00695 {
00696 a.x -= b.x; a.y -= b.y; a.z -= b.z;
00697 }
00698
00699
00700 inline __host__ __device__ uint3 operator*(uint3 a, uint3 b)
00701 {
00702 return make_uint3(a.x * b.x, a.y * b.y, a.z * b.z);
00703 }
00704 inline __host__ __device__ uint3 operator*(uint3 a, uint s)
00705 {
00706 return make_uint3(a.x * s, a.y * s, a.z * s);
00707 }
00708 inline __host__ __device__ uint3 operator*(uint s, uint3 a)
00709 {
00710 return make_uint3(a.x * s, a.y * s, a.z * s);
00711 }
00712 inline __host__ __device__ void operator*=(uint3 &a, uint s)
00713 {
00714 a.x *= s; a.y *= s; a.z *= s;
00715 }
00716
00717
00718 inline __host__ __device__ uint3 operator/(uint3 a, uint3 b)
00719 {
00720 return make_uint3(a.x / b.x, a.y / b.y, a.z / b.z);
00721 }
00722 inline __host__ __device__ uint3 operator/(uint3 a, uint s)
00723 {
00724 return make_uint3(a.x / s, a.y / s, a.z / s);
00725 }
00726 inline __host__ __device__ uint3 operator/(uint s, uint3 a)
00727 {
00728 return make_uint3(a.x / s, a.y / s, a.z / s);
00729 }
00730 inline __host__ __device__ void operator/=(uint3 &a, uint s)
00731 {
00732 a.x /= s; a.y /= s; a.z /= s;
00733 }
00734
00735
00736 inline __device__ __host__ uint clamp(uint f, uint a, uint b)
00737 {
00738 return max(a, min(f, b));
00739 }
00740
00741 inline __device__ __host__ uint3 clamp(uint3 v, uint a, uint b)
00742 {
00743 return make_uint3(clamp(v.x, a, b), clamp(v.y, a, b), clamp(v.z, a, b));
00744 }
00745
00746 inline __device__ __host__ uint3 clamp(uint3 v, uint3 a, uint3 b)
00747 {
00748 return make_uint3(clamp(v.x, a.x, b.x), clamp(v.y, a.y, b.y), clamp(v.z, a.z, b.z));
00749 }
00750
00751 #endif