Skip to content

Instantly share code, notes, and snippets.

@randallrvr
Created August 20, 2013 18:16
Show Gist options
  • Select an option

  • Save randallrvr/6285132 to your computer and use it in GitHub Desktop.

Select an option

Save randallrvr/6285132 to your computer and use it in GitHub Desktop.
#pragma once
__device__ __forceinline__
inline float3 operator*(const float m[16], const float3 &v)
{
return make_float3(m[0] * v.x + m[4] * v.y + m[ 8] * v.z + m[12],
m[1] * v.x + m[5] * v.y + m[ 9] * v.z + m[13],
m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14]);
}
__device__ __forceinline__
inline int3 operator*(const float m[9], const int3 &v)
{
return make_int3(m[0] * v.x + m[3] * v.y + m[6] * v.z,
m[1] * v.x + m[4] * v.y + m[7] * v.z,
m[2] * v.x + m[5] * v.y + m[8] * v.z);
}
__device__ __forceinline__ float3 operator- (const float3 &a, const float3 &b) { return make_float3(a.x-b.x, a.y-b.y, a.z-b.z); }
__device__ __forceinline__ float3 operator- (const float3 &a) { return make_float3(-a.x, -a.y, -a.z); }
__device__ __forceinline__ float2 operator+ (const float2 &a, float b) { return make_float2(a.x+b, a.y+b); }
__device__ __forceinline__ void operator+= (float3 &a, float b) { a.x+=b; a.y+=b; a.z+=b; }
__device__ __forceinline__ void operator*= (float3 &a, const int3 &b) { a.x*=b.x; a.y*=b.y; a.z*=b.z; }
__device__ __forceinline__ float3 min (const float3 &a, const float3 &b) { return make_float3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z)); }
__device__ __forceinline__ float3 min (const float3 &a, const float3 &b, const float3 &c) { return min(min(a,b), c); }
__device__ __forceinline__ float3 max (const float3 &a, const float3 &b) { return make_float3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)); }
__device__ __forceinline__ float3 max (const float3 &a, const float3 &b, const float3 &c) { return max(max(a,b), c); }
__device__ __forceinline__ float3 floor (const float3 &a) { return make_float3(floor(a.x), floor(a.y), floor(a.z)); }
__device__ __forceinline__ float3 ceil (const float3 &a) { return make_float3(ceil(a.x), ceil(a.y), ceil(a.z)); }
__device__ __forceinline__ int clamp (int a, int b, int c) { return min(c, max(b, a)); }
__device__ __forceinline__ float clamp (const float &a, const float &b, const float &c) { return min(c, max(b, a)); }
__device__ __forceinline__ int3 clamp (const float3 &a, const int3 &b, const int3 &c) { return make_int3(clamp(a.x, float(b.x), float(c.x)), clamp(a.y, float(b.y), float(c.y)), clamp(a.z, float(b.z), float(c.z))); }
__device__ __forceinline__ int3 clamp (const float3 &a, int b, const int3 &c) { return make_int3(clamp(a.x, float(b), float(c.x)), clamp(a.y, float(b), float(c.y)), clamp(a.z, float(b), float(c.z))); }
__device__ __forceinline__ float3 cross (const float3 &a, const float3 &b) { 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); }
__device__ __forceinline__ float dot (const float2 &a, const float2 &b) { return a.x*b.x + a.y*b.y; }
__device__ __forceinline__ float dot (const float3 &a, const float3 &b) { return a.x*b.x + a.y*b.y + a.z*b.z; }
__device__ __forceinline__
inline float triArea2D(float2 v0, float2 v1, float2 v2)
{
return abs(v0.x*(v1.y-v2.y) + v1.x*(v2.y-v0.y) + v2.x*(v0.y-v1.y)) * 0.5;
}
__device__ __forceinline__
inline float triArea3D(float3 v0, float3 v1, float3 v2)
{
return dot(cross(v1-v0, v2-v0),cross(v1-v0, v2-v0)) * 0.5;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment