|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once |
|
|
|
|
|
#if defined(_WIN32) && !defined(__CUDACC__) |
|
|
#if defined(_DEBUG) |
|
|
|
|
|
#define VEC2_VALIDATE() { assert(_finite(x));\ |
|
|
assert(!_isnan(x));\ |
|
|
\ |
|
|
assert(_finite(y));\ |
|
|
assert(!_isnan(y));\ |
|
|
} |
|
|
#else |
|
|
|
|
|
#define VEC2_VALIDATE() {\ |
|
|
assert(isfinite(x));\ |
|
|
assert(isfinite(y)); }\ |
|
|
|
|
|
#endif |
|
|
|
|
|
#else |
|
|
#define VEC2_VALIDATE() |
|
|
#endif |
|
|
|
|
|
#ifdef _DEBUG |
|
|
#define FLOAT_VALIDATE(f) { assert(_finite(f)); assert(!_isnan(f)); } |
|
|
#else |
|
|
#define FLOAT_VALIDATE(f) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
class XVector2 |
|
|
{ |
|
|
public: |
|
|
|
|
|
typedef T value_type; |
|
|
|
|
|
CUDA_CALLABLE XVector2() : x(0.0f), y(0.0f) { VEC2_VALIDATE(); } |
|
|
CUDA_CALLABLE XVector2(T _x) : x(_x), y(_x) { VEC2_VALIDATE(); } |
|
|
CUDA_CALLABLE XVector2(T _x, T _y) : x(_x), y(_y) { VEC2_VALIDATE(); } |
|
|
CUDA_CALLABLE XVector2(const T* p) : x(p[0]), y(p[1]) {} |
|
|
|
|
|
template <typename U> |
|
|
CUDA_CALLABLE explicit XVector2(const XVector2<U>& v) : x(v.x), y(v.y) {} |
|
|
|
|
|
CUDA_CALLABLE operator T* () { return &x; } |
|
|
CUDA_CALLABLE operator const T* () const { return &x; }; |
|
|
|
|
|
CUDA_CALLABLE void Set(T x_, T y_) { VEC2_VALIDATE(); x = x_; y = y_; } |
|
|
|
|
|
CUDA_CALLABLE XVector2<T> operator * (T scale) const { XVector2<T> r(*this); r *= scale; VEC2_VALIDATE(); return r; } |
|
|
CUDA_CALLABLE XVector2<T> operator / (T scale) const { XVector2<T> r(*this); r /= scale; VEC2_VALIDATE(); return r; } |
|
|
CUDA_CALLABLE XVector2<T> operator + (const XVector2<T>& v) const { XVector2<T> r(*this); r += v; VEC2_VALIDATE(); return r; } |
|
|
CUDA_CALLABLE XVector2<T> operator - (const XVector2<T>& v) const { XVector2<T> r(*this); r -= v; VEC2_VALIDATE(); return r; } |
|
|
|
|
|
CUDA_CALLABLE XVector2<T>& operator *=(T scale) {x *= scale; y *= scale; VEC2_VALIDATE(); return *this;} |
|
|
CUDA_CALLABLE XVector2<T>& operator /=(T scale) {T s(1.0f/scale); x *= s; y *= s; VEC2_VALIDATE(); return *this;} |
|
|
CUDA_CALLABLE XVector2<T>& operator +=(const XVector2<T>& v) {x += v.x; y += v.y; VEC2_VALIDATE(); return *this;} |
|
|
CUDA_CALLABLE XVector2<T>& operator -=(const XVector2<T>& v) {x -= v.x; y -= v.y; VEC2_VALIDATE(); return *this;} |
|
|
|
|
|
CUDA_CALLABLE XVector2<T>& operator *=(const XVector2<T>& scale) {x *= scale.x; y *= scale.y; VEC2_VALIDATE(); return *this;} |
|
|
|
|
|
|
|
|
CUDA_CALLABLE XVector2<T> operator -() const { VEC2_VALIDATE(); return XVector2<T>(-x, -y); } |
|
|
|
|
|
|
|
|
CUDA_CALLABLE void Normalize() { *this /= Length(*this); } |
|
|
CUDA_CALLABLE void SafeNormalize(const XVector2<T>& v=XVector2<T>(0.0f,0.0f)) |
|
|
{ |
|
|
T length = Length(*this); |
|
|
*this = (length==0.00001f)?v:(*this /= length); |
|
|
} |
|
|
|
|
|
T x; |
|
|
T y; |
|
|
}; |
|
|
|
|
|
typedef XVector2<float> Vec2; |
|
|
typedef XVector2<float> Vector2; |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
CUDA_CALLABLE XVector2<T> operator *(T lhs, const XVector2<T>& rhs) |
|
|
{ |
|
|
XVector2<T> r(rhs); |
|
|
r *= lhs; |
|
|
return r; |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
CUDA_CALLABLE XVector2<T> operator*(const XVector2<T>& lhs, const XVector2<T>& rhs) |
|
|
{ |
|
|
XVector2<T> r(lhs); |
|
|
r *= rhs; |
|
|
return r; |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
CUDA_CALLABLE bool operator==(const XVector2<T>& lhs, const XVector2<T>& rhs) |
|
|
{ |
|
|
return (lhs.x == rhs.x && lhs.y == rhs.y); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
CUDA_CALLABLE T Dot(const XVector2<T>& v1, const XVector2<T>& v2) |
|
|
{ |
|
|
return v1.x * v2.x + v1.y * v2.y; |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
CUDA_CALLABLE XVector2<T> PerpCCW(const XVector2<T>& v) |
|
|
{ |
|
|
return XVector2<T>(-v.y, v.x); |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
CUDA_CALLABLE XVector2<T> PerpCW(const XVector2<T>& v) |
|
|
{ |
|
|
return XVector2<T>(v.y, -v.x); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
CUDA_CALLABLE XVector2<T> Max(const XVector2<T>& a, const XVector2<T>& b) |
|
|
{ |
|
|
return XVector2<T>(Max(a.x, b.x), Max(a.y, b.y)); |
|
|
} |
|
|
|
|
|
template <typename T> |
|
|
CUDA_CALLABLE XVector2<T> Min(const XVector2<T>& a, const XVector2<T>& b) |
|
|
{ |
|
|
return XVector2<T>(Min(a.x, b.x), Min(a.y, b.y)); |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
CUDA_CALLABLE T Cross(const XVector2<T>& a, const XVector2<T>& b) |
|
|
{ |
|
|
return (a.x*b.y - a.y*b.x); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|