📄 nv_algebra.h
字号:
/*********************************************************************NVMH1****File:nv_algebra.hCopyright (C) 1999, 2002 NVIDIA CorporationThis file is provided without support, instruction, or implied warranty of anykind. NVIDIA makes no guarantee of its fitness for a particular purpose and isnot liable under any circumstances for any damages or loss whatsoever arisingfrom the use or inability to use this file or items derived from it.Comments:******************************************************************************/#ifndef _nv_algebra_h_#define _nv_algebra_h_struct DECLSPEC_NV_MATH vec2{ vec2() { } vec2(nv_scalar x, nv_scalar y) : x(x), y(y) { } vec2(const nv_scalar* xy) : x(xy[0]), y(xy[1]) { } vec2(const vec2& u) : x(u.x), y(u.y) { } vec2(const vec3&); bool operator==(const vec2 & u) const { return (u.x == x && u.y == y) ? true : false; } bool operator!=(const vec2 & u) const { return !(*this == u ); } vec2 & operator*=(const nv_scalar & lambda) { x*= lambda; y*= lambda; return *this; } vec2 & operator-=(const vec2 & u) { x-= u.x; y-= u.y; return *this; } vec2 & operator+=(const vec2 & u) { x+= u.x; y+= u.y; return *this; } nv_scalar & operator[](int i) { return vec_array[i]; } const nv_scalar operator[](int i) const { return vec_array[i]; } union { struct { nv_scalar x,y; // standard names for components }; struct { nv_scalar s,t; // standard names for components }; nv_scalar vec_array[2]; // array access };};inline const vec2 operator+(const vec2& u, const vec2& v){ return vec2(u.x + v.x, u.y + v.y);}inline const vec2 operator-(const vec2& u, const vec2& v){ return vec2(u.x - v.x, u.y - v.y);}inline const vec2 operator*(const nv_scalar s, const vec2& u){ return vec2(s * u.x, s * u.y);}inline const vec2 operator/(const vec2& u, const nv_scalar s){ return vec2(u.x / s, u.y / s);}inline const vec2 operator*(const vec2&u, const vec2&v){ return vec2(u.x * v.x, u.y * v.y);}struct DECLSPEC_NV_MATH vec3{ vec3() { } vec3(nv_scalar x, nv_scalar y, nv_scalar z) : x(x), y(y), z(z) { } vec3(const nv_scalar* xyz) : x(xyz[0]), y(xyz[1]), z(xyz[2]) { } vec3(const vec2& u) : x(u.x), y(u.y), z(1.0f) { } vec3(const vec3& u) : x(u.x), y(u.y), z(u.z) { } vec3(const vec4&); bool operator==(const vec3 & u) const { return (u.x == x && u.y == y && u.z == z) ? true : false; } bool operator!=( const vec3& rhs ) const { return !(*this == rhs ); } vec3 & operator*=(const nv_scalar & lambda) { x*= lambda; y*= lambda; z*= lambda; return *this; } vec3 operator - () const { return vec3(-x, -y, -z); } vec3 & operator-=(const vec3 & u) { x-= u.x; y-= u.y; z-= u.z; return *this; } vec3 & operator+=(const vec3 & u) { x+= u.x; y+= u.y; z+= u.z; return *this; } nv_scalar normalize(); nv_scalar sq_norm() const { return x * x + y * y + z * z; } nv_scalar norm() const { return sqrtf(sq_norm()); } nv_scalar & operator[](int i) { return vec_array[i]; } const nv_scalar operator[](int i) const { return vec_array[i]; } union { struct { nv_scalar x,y,z; // standard names for components }; struct { nv_scalar s,t,r; // standard names for components }; nv_scalar vec_array[3]; // array access };};inline const vec3 operator+(const vec3& u, const vec3& v){ return vec3(u.x + v.x, u.y + v.y, u.z + v.z);}inline const vec3 operator-(const vec3& u, const vec3& v){ return vec3(u.x - v.x, u.y - v.y, u.z - v.z);}inline const vec3 operator^(const vec3& u, const vec3& v){ return vec3(u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x);}inline const vec3 operator*(const nv_scalar s, const vec3& u){ return vec3(s * u.x, s * u.y, s * u.z);}inline const vec3 operator/(const vec3& u, const nv_scalar s){ return vec3(u.x / s, u.y / s, u.z / s);}inline const vec3 operator*(const vec3& u, const vec3& v){ return vec3(u.x * v.x, u.y * v.y, u.z * v.z);}inline vec2::vec2(const vec3& u){ nv_scalar k = 1 / u.z; x = k * u.x; y = k * u.y;}struct DECLSPEC_NV_MATH vec4{ vec4() { } vec4(nv_scalar x, nv_scalar y, nv_scalar z, nv_scalar w) : x(x), y(y), z(z), w(w) { } vec4(const nv_scalar* xyzw) : x(xyzw[0]), y(xyzw[1]), z(xyzw[2]), w(xyzw[3]) { } vec4(const vec3& u) : x(u.x), y(u.y), z(u.z), w(1.0f) { } vec4(const vec4& u) : x(u.x), y(u.y), z(u.z), w(u.w) { } bool operator==(const vec4 & u) const { return (u.x == x && u.y == y && u.z == z && u.w == w) ? true : false; } bool operator!=( const vec4& rhs ) const { return !(*this == rhs ); } vec4 & operator*=(const nv_scalar & lambda) { x*= lambda; y*= lambda; z*= lambda; w*= lambda; return *this; } vec4 & operator-=(const vec4 & u) { x-= u.x; y-= u.y; z-= u.z; w-= u.w; return *this; } vec4 & operator+=(const vec4 & u) { x+= u.x; y+= u.y; z+= u.z; w+= u.w; return *this; } vec4 operator - () const { return vec4(-x, -y, -z, -w); } nv_scalar & operator[](int i) { return vec_array[i]; } const nv_scalar operator[](int i) const { return vec_array[i]; } union { struct { nv_scalar x,y,z,w; // standard names for components }; struct { nv_scalar s,t,r,q; // standard names for components }; nv_scalar vec_array[4]; // array access };};inline const vec4 operator+(const vec4& u, const vec4& v){ return vec4(u.x + v.x, u.y + v.y, u.z + v.z, u.w + v.w);}inline const vec4 operator-(const vec4& u, const vec4& v){ return vec4(u.x - v.x, u.y - v.y, u.z - v.z, u.w - v.w);}inline const vec4 operator*(const nv_scalar s, const vec4& u){ return vec4(s * u.x, s * u.y, s * u.z, s * u.w);}inline const vec4 operator/(const vec4& u, const nv_scalar s){ return vec4(u.x / s, u.y / s, u.z / s, u.w / s);}inline const vec4 operator*(const vec4& u, const vec4& v){ return vec4(u.x * v.x, u.y * v.y, u.z * v.z, u.w * v.w);}inline vec3::vec3(const vec4& u){ x = u.x; y = u.y; z = u.z;}// quaternionstruct quat; /* for all the matrices...a<x><y> indicates the element at row x, col y For example: a01 <-> row 0, col 1 */struct DECLSPEC_NV_MATH mat3{ mat3(); mat3(const nv_scalar * array); mat3(const mat3 & M); mat3( const nv_scalar& f0, const nv_scalar& f1, const nv_scalar& f2, const nv_scalar& f3, const nv_scalar& f4, const nv_scalar& f5, const nv_scalar& f6, const nv_scalar& f7, const nv_scalar& f8 ) : a00( f0 ), a10( f1 ), a20( f2 ), a01( f3 ), a11( f4 ), a21( f5 ), a02( f6 ), a12( f7 ), a22( f8) { } const vec3 col(const int i) const { return vec3(&mat_array[i * 3]); } const vec3 operator[](int i) const { return vec3(mat_array[i], mat_array[i + 3], mat_array[i + 6]); } const nv_scalar& operator()(const int& i, const int& j) const { return mat_array[ j * 3 + i ]; } nv_scalar& operator()(const int& i, const int& j) { return mat_array[ j * 3 + i ]; } void set_row(int i, const vec3 & v) { mat_array[i] = v.x; mat_array[i + 3] = v.y; mat_array[i + 6] = v.z; } void set_col(int i, const vec3 & v) { mat_array[i * 3] = v.x; mat_array[i * 3 + 1] = v.y;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -