📄 icepoint.h
字号:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Contains code for 3D vectors.
* \file IcePoint.h
* \author Pierre Terdiman
* \date April, 4, 2000
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Include Guard
#ifndef __ICEPOINT_H__
#define __ICEPOINT_H__
// Forward declarations
class HPoint;
class Plane;
class Matrix3x3;
class Matrix4x4;
#define CROSS2D(a, b) (a.x*b.y - b.x*a.y)
const float EPSILON2 = 1.0e-20f;
class Point
{
public:
//! Empty constructor
inline_ Point() {}
//! Constructor from a single float
// inline_ Point(float val) : x(val), y(val), z(val) {}
// Removed since it introduced the nasty "Point T = *Matrix4x4.GetTrans();" bug.......
//! Constructor from floats
inline_ Point(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
//! Constructor from array
inline_ Point(const float f[3]) : x(f[_X]), y(f[_Y]), z(f[_Z]) {}
//! Copy constructor
inline_ Point(const Point& p) : x(p.x), y(p.y), z(p.z) {}
//! Destructor
inline_ ~Point() {}
//! Clears the vector
inline_ Point& Zero() { x = y = z = 0.0f; return *this; }
//! + infinity
inline_ Point& SetPlusInfinity() { x = y = z = MAX_FLOAT; return *this; }
//! - infinity
inline_ Point& SetMinusInfinity() { x = y = z = MIN_FLOAT; return *this; }
//! Sets positive unit random vector
Point& PositiveUnitRandomVector();
//! Sets unit random vector
Point& UnitRandomVector();
//! Assignment from values
inline_ Point& Set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; return *this; }
//! Assignment from array
inline_ Point& Set(const float f[3]) { x = f[_X]; y = f[_Y]; z = f[_Z]; return *this; }
//! Assignment from another point
inline_ Point& Set(const Point& src) { x = src.x; y = src.y; z = src.z; return *this; }
//! Adds a vector
inline_ Point& Add(const Point& p) { x += p.x; y += p.y; z += p.z; return *this; }
//! Adds a vector
inline_ Point& Add(float _x, float _y, float _z) { x += _x; y += _y; z += _z; return *this; }
//! Adds a vector
inline_ Point& Add(const float f[3]) { x += f[_X]; y += f[_Y]; z += f[_Z]; return *this; }
//! Adds vectors
inline_ Point& Add(const Point& p, const Point& q) { x = p.x+q.x; y = p.y+q.y; z = p.z+q.z; return *this; }
//! Subtracts a vector
inline_ Point& Sub(const Point& p) { x -= p.x; y -= p.y; z -= p.z; return *this; }
//! Subtracts a vector
inline_ Point& Sub(float _x, float _y, float _z) { x -= _x; y -= _y; z -= _z; return *this; }
//! Subtracts a vector
inline_ Point& Sub(const float f[3]) { x -= f[_X]; y -= f[_Y]; z -= f[_Z]; return *this; }
//! Subtracts vectors
inline_ Point& Sub(const Point& p, const Point& q) { x = p.x-q.x; y = p.y-q.y; z = p.z-q.z; return *this; }
//! this = -this
inline_ Point& Neg() { x = -x; y = -y; z = -z; return *this; }
//! this = -a
inline_ Point& Neg(const Point& a) { x = -a.x; y = -a.y; z = -a.z; return *this; }
//! Multiplies by a scalar
inline_ Point& Mult(float s) { x *= s; y *= s; z *= s; return *this; }
//! this = a * scalar
inline_ Point& Mult(const Point& a, float scalar)
{
x = a.x * scalar;
y = a.y * scalar;
z = a.z * scalar;
return *this;
}
//! this = a + b * scalar
inline_ Point& Mac(const Point& a, const Point& b, float scalar)
{
x = a.x + b.x * scalar;
y = a.y + b.y * scalar;
z = a.z + b.z * scalar;
return *this;
}
//! this = this + a * scalar
inline_ Point& Mac(const Point& a, float scalar)
{
x += a.x * scalar;
y += a.y * scalar;
z += a.z * scalar;
return *this;
}
//! this = a - b * scalar
inline_ Point& Msc(const Point& a, const Point& b, float scalar)
{
x = a.x - b.x * scalar;
y = a.y - b.y * scalar;
z = a.z - b.z * scalar;
return *this;
}
//! this = this - a * scalar
inline_ Point& Msc(const Point& a, float scalar)
{
x -= a.x * scalar;
y -= a.y * scalar;
z -= a.z * scalar;
return *this;
}
//! this = a + b * scalarb + c * scalarc
inline_ Point& Mac2(const Point& a, const Point& b, float scalarb, const Point& c, float scalarc)
{
x = a.x + b.x * scalarb + c.x * scalarc;
y = a.y + b.y * scalarb + c.y * scalarc;
z = a.z + b.z * scalarb + c.z * scalarc;
return *this;
}
//! this = a - b * scalarb - c * scalarc
inline_ Point& Msc2(const Point& a, const Point& b, float scalarb, const Point& c, float scalarc)
{
x = a.x - b.x * scalarb - c.x * scalarc;
y = a.y - b.y * scalarb - c.y * scalarc;
z = a.z - b.z * scalarb - c.z * scalarc;
return *this;
}
//! this = mat * a
inline_ Point& Mult(const Matrix3x3& mat, const Point& a);
//! this = mat1 * a1 + mat2 * a2
inline_ Point& Mult2(const Matrix3x3& mat1, const Point& a1, const Matrix3x3& mat2, const Point& a2);
//! this = this + mat * a
inline_ Point& Mac(const Matrix3x3& mat, const Point& a);
//! this = transpose(mat) * a
inline_ Point& TransMult(const Matrix3x3& mat, const Point& a);
//! Linear interpolate between two vectors: this = a + t * (b - a)
inline_ Point& Lerp(const Point& a, const Point& b, float t)
{
x = a.x + t * (b.x - a.x);
y = a.y + t * (b.y - a.y);
z = a.z + t * (b.z - a.z);
return *this;
}
//! Hermite interpolate between p1 and p2. p0 and p3 are used for finding gradient at p1 and p2.
//! this = p0 * (2t^2 - t^3 - t)/2
//! + p1 * (3t^3 - 5t^2 + 2)/2
//! + p2 * (4t^2 - 3t^3 + t)/2
//! + p3 * (t^3 - t^2)/2
inline_ Point& Herp(const Point& p0, const Point& p1, const Point& p2, const Point& p3, float t)
{
float t2 = t * t;
float t3 = t2 * t;
float kp0 = (2.0f * t2 - t3 - t) * 0.5f;
float kp1 = (3.0f * t3 - 5.0f * t2 + 2.0f) * 0.5f;
float kp2 = (4.0f * t2 - 3.0f * t3 + t) * 0.5f;
float kp3 = (t3 - t2) * 0.5f;
x = p0.x * kp0 + p1.x * kp1 + p2.x * kp2 + p3.x * kp3;
y = p0.y * kp0 + p1.y * kp1 + p2.y * kp2 + p3.y * kp3;
z = p0.z * kp0 + p1.z * kp1 + p2.z * kp2 + p3.z * kp3;
return *this;
}
//! this = rotpos * r + linpos
inline_ Point& Transform(const Point& r, const Matrix3x3& rotpos, const Point& linpos);
//! this = trans(rotpos) * (r - linpos)
inline_ Point& InvTransform(const Point& r, const Matrix3x3& rotpos, const Point& linpos);
//! Returns MIN(x, y, z);
inline_ float Min() const { return MIN(x, MIN(y, z)); }
//! Returns MAX(x, y, z);
inline_ float Max() const { return MAX(x, MAX(y, z)); }
//! Sets each element to be componentwise minimum
inline_ Point& Min(const Point& p) { x = MIN(x, p.x); y = MIN(y, p.y); z = MIN(z, p.z); return *this; }
//! Sets each element to be componentwise maximum
inline_ Point& Max(const Point& p) { x = MAX(x, p.x); y = MAX(y, p.y); z = MAX(z, p.z); return *this; }
//! Clamps each element
inline_ Point& Clamp(float min, float max)
{
if(x<min) x=min; if(x>max) x=max;
if(y<min) y=min; if(y>max) y=max;
if(z<min) z=min; if(z>max) z=max;
return *this;
}
//! Computes square magnitude
inline_ float SquareMagnitude() const { return x*x + y*y + z*z; }
//! Computes magnitude
inline_ float Magnitude() const { return sqrtf(x*x + y*y + z*z); }
//! Computes volume
inline_ float Volume() const { return x * y * z; }
//! Checks the point is near zero
inline_ bool ApproxZero() const { return SquareMagnitude() < EPSILON2; }
//! Tests for exact zero vector
inline_ BOOL IsZero() const
{
if(IR(x) || IR(y) || IR(z)) return FALSE;
return TRUE;
}
//! Checks point validity
inline_ BOOL IsValid() const
{
if(!IsValidFloat(x)) return FALSE;
if(!IsValidFloat(y)) return FALSE;
if(!IsValidFloat(z)) return FALSE;
return TRUE;
}
//! Slighty moves the point
void Tweak(udword coord_mask, udword tweak_mask)
{
if(coord_mask&1) { udword Dummy = IR(x); Dummy^=tweak_mask; x = FR(Dummy); }
if(coord_mask&2) { udword Dummy = IR(y); Dummy^=tweak_mask; y = FR(Dummy); }
if(coord_mask&4) { udword Dummy = IR(z); Dummy^=tweak_mask; z = FR(Dummy); }
}
#define TWEAKMASK 0x3fffff
#define TWEAKNOTMASK ~TWEAKMASK
//! Slighty moves the point out
inline_ void TweakBigger()
{
udword Dummy = (IR(x)&TWEAKNOTMASK); if(!IS_NEGATIVE_FLOAT(x)) Dummy+=TWEAKMASK+1; x = FR(Dummy);
Dummy = (IR(y)&TWEAKNOTMASK); if(!IS_NEGATIVE_FLOAT(y)) Dummy+=TWEAKMASK+1; y = FR(Dummy);
Dummy = (IR(z)&TWEAKNOTMASK); if(!IS_NEGATIVE_FLOAT(z)) Dummy+=TWEAKMASK+1; z = FR(Dummy);
}
//! Slighty moves the point in
inline_ void TweakSmaller()
{
udword Dummy = (IR(x)&TWEAKNOTMASK); if(IS_NEGATIVE_FLOAT(x)) Dummy+=TWEAKMASK+1; x = FR(Dummy);
Dummy = (IR(y)&TWEAKNOTMASK); if(IS_NEGATIVE_FLOAT(y)) Dummy+=TWEAKMASK+1; y = FR(Dummy);
Dummy = (IR(z)&TWEAKNOTMASK); if(IS_NEGATIVE_FLOAT(z)) Dummy+=TWEAKMASK+1; z = FR(Dummy);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -