📄 icepoint.h
字号:
}
//! Normalizes the vector
inline_ Point& Normalize()
{
float M = x*x + y*y + z*z;
if(M)
{
M = 1.0f / sqrtf(M);
x *= M;
y *= M;
z *= M;
}
return *this;
}
//! Sets vector length
inline_ Point& SetLength(float length)
{
float NewLength = length / Magnitude();
x *= NewLength;
y *= NewLength;
z *= NewLength;
return *this;
}
//! Clamps vector length
inline_ Point& ClampLength(float limit_length)
{
if(limit_length>=0.0f) // Magnitude must be positive
{
float CurrentSquareLength = SquareMagnitude();
if(CurrentSquareLength > limit_length * limit_length)
{
float Coeff = limit_length / sqrtf(CurrentSquareLength);
x *= Coeff;
y *= Coeff;
z *= Coeff;
}
}
return *this;
}
//! Computes distance to another point
inline_ float Distance(const Point& b) const
{
return sqrtf((x - b.x)*(x - b.x) + (y - b.y)*(y - b.y) + (z - b.z)*(z - b.z));
}
//! Computes square distance to another point
inline_ float SquareDistance(const Point& b) const
{
return ((x - b.x)*(x - b.x) + (y - b.y)*(y - b.y) + (z - b.z)*(z - b.z));
}
//! Dot product dp = this|a
inline_ float Dot(const Point& p) const { return p.x * x + p.y * y + p.z * z; }
//! Cross product this = a x b
inline_ Point& Cross(const Point& a, const Point& b)
{
x = a.y * b.z - a.z * b.y;
y = a.z * b.x - a.x * b.z;
z = a.x * b.y - a.y * b.x;
return *this;
}
//! Vector code ( bitmask = sign(z) | sign(y) | sign(x) )
inline_ udword VectorCode() const
{
return (IR(x)>>31) | ((IR(y)&SIGN_BITMASK)>>30) | ((IR(z)&SIGN_BITMASK)>>29);
}
//! Returns largest axis
inline_ PointComponent LargestAxis() const
{
const float* Vals = &x;
PointComponent m = _X;
if(Vals[_Y] > Vals[m]) m = _Y;
if(Vals[_Z] > Vals[m]) m = _Z;
return m;
}
//! Returns closest axis
inline_ PointComponent ClosestAxis() const
{
const float* Vals = &x;
PointComponent m = _X;
if(AIR(Vals[_Y]) > AIR(Vals[m])) m = _Y;
if(AIR(Vals[_Z]) > AIR(Vals[m])) m = _Z;
return m;
}
//! Returns smallest axis
inline_ PointComponent SmallestAxis() const
{
const float* Vals = &x;
PointComponent m = _X;
if(Vals[_Y] < Vals[m]) m = _Y;
if(Vals[_Z] < Vals[m]) m = _Z;
return m;
}
//! Refracts the point
Point& Refract(const Point& eye, const Point& n, float refractindex, Point& refracted);
//! Projects the point onto a plane
Point& ProjectToPlane(const Plane& p);
//! Projects the point onto the screen
void ProjectToScreen(float halfrenderwidth, float halfrenderheight, const Matrix4x4& mat, HPoint& projected) const;
//! Unfolds the point onto a plane according to edge(a,b)
Point& Unfold(Plane& p, Point& a, Point& b);
//! Hash function from Ville Miettinen
inline_ udword GetHashValue() const
{
const udword* h = (const udword*)(this);
udword f = (h[0]+h[1]*11-(h[2]*17)) & 0x7fffffff; // avoid problems with +-0
return (f>>22)^(f>>12)^(f);
}
//! Stuff magic values in the point, marking it as explicitely not used.
void SetNotUsed();
//! Checks the point is marked as not used
BOOL IsNotUsed() const;
// Arithmetic operators
//! Unary operator for Point Negate = - Point
inline_ Point operator-() const { return Point(-x, -y, -z); }
//! Operator for Point Plus = Point + Point.
inline_ Point operator+(const Point& p) const { return Point(x + p.x, y + p.y, z + p.z); }
//! Operator for Point Minus = Point - Point.
inline_ Point operator-(const Point& p) const { return Point(x - p.x, y - p.y, z - p.z); }
//! Operator for Point Mul = Point * Point.
inline_ Point operator*(const Point& p) const { return Point(x * p.x, y * p.y, z * p.z); }
//! Operator for Point Scale = Point * float.
inline_ Point operator*(float s) const { return Point(x * s, y * s, z * s ); }
//! Operator for Point Scale = float * Point.
inline_ friend Point operator*(float s, const Point& p) { return Point(s * p.x, s * p.y, s * p.z); }
//! Operator for Point Div = Point / Point.
inline_ Point operator/(const Point& p) const { return Point(x / p.x, y / p.y, z / p.z); }
//! Operator for Point Scale = Point / float.
inline_ Point operator/(float s) const { s = 1.0f / s; return Point(x * s, y * s, z * s); }
//! Operator for Point Scale = float / Point.
inline_ friend Point operator/(float s, const Point& p) { return Point(s / p.x, s / p.y, s / p.z); }
//! Operator for float DotProd = Point | Point.
inline_ float operator|(const Point& p) const { return x*p.x + y*p.y + z*p.z; }
//! Operator for Point VecProd = Point ^ Point.
inline_ Point operator^(const Point& p) const
{
return Point(
y * p.z - z * p.y,
z * p.x - x * p.z,
x * p.y - y * p.x );
}
//! Operator for Point += Point.
inline_ Point& operator+=(const Point& p) { x += p.x; y += p.y; z += p.z; return *this; }
//! Operator for Point += float.
inline_ Point& operator+=(float s) { x += s; y += s; z += s; return *this; }
//! Operator for Point -= Point.
inline_ Point& operator-=(const Point& p) { x -= p.x; y -= p.y; z -= p.z; return *this; }
//! Operator for Point -= float.
inline_ Point& operator-=(float s) { x -= s; y -= s; z -= s; return *this; }
//! Operator for Point *= Point.
inline_ Point& operator*=(const Point& p) { x *= p.x; y *= p.y; z *= p.z; return *this; }
//! Operator for Point *= float.
inline_ Point& operator*=(float s) { x *= s; y *= s; z *= s; return *this; }
//! Operator for Point /= Point.
inline_ Point& operator/=(const Point& p) { x /= p.x; y /= p.y; z /= p.z; return *this; }
//! Operator for Point /= float.
inline_ Point& operator/=(float s) { s = 1.0f/s; x *= s; y *= s; z *= s; return *this; }
// Logical operators
//! Operator for "if(Point==Point)"
inline_ bool operator==(const Point& p) const { return ( (IR(x)==IR(p.x))&&(IR(y)==IR(p.y))&&(IR(z)==IR(p.z))); }
//! Operator for "if(Point!=Point)"
inline_ bool operator!=(const Point& p) const { return ( (IR(x)!=IR(p.x))||(IR(y)!=IR(p.y))||(IR(z)!=IR(p.z))); }
// Arithmetic operators
//! Operator for Point Mul = Point * Matrix3x3.
inline_ Point operator*(const Matrix3x3& mat) const
{
class ShadowMatrix3x3{ public: float m[3][3]; }; // To allow inlining
const ShadowMatrix3x3* Mat = (const ShadowMatrix3x3*)&mat;
return Point(
x * Mat->m[0][0] + y * Mat->m[1][0] + z * Mat->m[2][0],
x * Mat->m[0][1] + y * Mat->m[1][1] + z * Mat->m[2][1],
x * Mat->m[0][2] + y * Mat->m[1][2] + z * Mat->m[2][2] );
}
//! Operator for Point Mul = Point * Matrix4x4.
inline_ Point operator*(const Matrix4x4& mat) const
{
class ShadowMatrix4x4{ public: float m[4][4]; }; // To allow inlining
const ShadowMatrix4x4* Mat = (const ShadowMatrix4x4*)&mat;
return Point(
x * Mat->m[0][0] + y * Mat->m[1][0] + z * Mat->m[2][0] + Mat->m[3][0],
x * Mat->m[0][1] + y * Mat->m[1][1] + z * Mat->m[2][1] + Mat->m[3][1],
x * Mat->m[0][2] + y * Mat->m[1][2] + z * Mat->m[2][2] + Mat->m[3][2]);
}
//! Operator for Point *= Matrix3x3.
inline_ Point& operator*=(const Matrix3x3& mat)
{
class ShadowMatrix3x3{ public: float m[3][3]; }; // To allow inlining
const ShadowMatrix3x3* Mat = (const ShadowMatrix3x3*)&mat;
float xp = x * Mat->m[0][0] + y * Mat->m[1][0] + z * Mat->m[2][0];
float yp = x * Mat->m[0][1] + y * Mat->m[1][1] + z * Mat->m[2][1];
float zp = x * Mat->m[0][2] + y * Mat->m[1][2] + z * Mat->m[2][2];
x = xp; y = yp; z = zp;
return *this;
}
//! Operator for Point *= Matrix4x4.
inline_ Point& operator*=(const Matrix4x4& mat)
{
class ShadowMatrix4x4{ public: float m[4][4]; }; // To allow inlining
const ShadowMatrix4x4* Mat = (const ShadowMatrix4x4*)&mat;
float xp = x * Mat->m[0][0] + y * Mat->m[1][0] + z * Mat->m[2][0] + Mat->m[3][0];
float yp = x * Mat->m[0][1] + y * Mat->m[1][1] + z * Mat->m[2][1] + Mat->m[3][1];
float zp = x * Mat->m[0][2] + y * Mat->m[1][2] + z * Mat->m[2][2] + Mat->m[3][2];
x = xp; y = yp; z = zp;
return *this;
}
// Cast operators
//! Cast a Point to a HPoint. w is set to zero.
operator HPoint() const;
inline_ operator const float*() const { return &x; }
inline_ operator float*() { return &x; }
public:
float x, y, z;
};
FUNCTION void Normalize1(Point& a);
FUNCTION void Normalize2(Point& a);
#endif //__ICEPOINT_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -