1
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3
* Contains code for 3D vectors.
5
* \author Pierre Terdiman
8
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
12
#ifndef __ICEPOINT_H__
13
#define __ICEPOINT_H__
15
// Forward declarations
21
#define CROSS2D(a, b) (a.x*b.y - b.x*a.y)
23
const float EPSILON2 = 1.0e-20f;
31
//! Constructor from a single float
32
// inline_ Point(float val) : x(val), y(val), z(val) {}
33
// Removed since it introduced the nasty "Point T = *Matrix4x4.GetTrans();" bug.......
34
//! Constructor from floats
35
inline_ Point(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
36
//! Constructor from array
37
inline_ Point(const float f[3]) : x(f[_X]), y(f[_Y]), z(f[_Z]) {}
39
inline_ Point(const Point& p) : x(p.x), y(p.y), z(p.z) {}
44
inline_ Point& Zero() { x = y = z = 0.0f; return *this; }
47
inline_ Point& SetPlusInfinity() { x = y = z = MAX_FLOAT; return *this; }
49
inline_ Point& SetMinusInfinity() { x = y = z = MIN_FLOAT; return *this; }
51
//! Sets positive unit random vector
52
Point& PositiveUnitRandomVector();
53
//! Sets unit random vector
54
Point& UnitRandomVector();
56
//! Assignment from values
57
inline_ Point& Set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; return *this; }
58
//! Assignment from array
59
inline_ Point& Set(const float f[3]) { x = f[_X]; y = f[_Y]; z = f[_Z]; return *this; }
60
//! Assignment from another point
61
inline_ Point& Set(const Point& src) { x = src.x; y = src.y; z = src.z; return *this; }
64
inline_ Point& Add(const Point& p) { x += p.x; y += p.y; z += p.z; return *this; }
66
inline_ Point& Add(float _x, float _y, float _z) { x += _x; y += _y; z += _z; return *this; }
68
inline_ Point& Add(const float f[3]) { x += f[_X]; y += f[_Y]; z += f[_Z]; return *this; }
70
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; }
72
//! Subtracts a vector
73
inline_ Point& Sub(const Point& p) { x -= p.x; y -= p.y; z -= p.z; return *this; }
74
//! Subtracts a vector
75
inline_ Point& Sub(float _x, float _y, float _z) { x -= _x; y -= _y; z -= _z; return *this; }
76
//! Subtracts a vector
77
inline_ Point& Sub(const float f[3]) { x -= f[_X]; y -= f[_Y]; z -= f[_Z]; return *this; }
79
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; }
82
inline_ Point& Neg() { x = -x; y = -y; z = -z; return *this; }
84
inline_ Point& Neg(const Point& a) { x = -a.x; y = -a.y; z = -a.z; return *this; }
86
//! Multiplies by a scalar
87
inline_ Point& Mult(float s) { x *= s; y *= s; z *= s; return *this; }
90
inline_ Point& Mult(const Point& a, float scalar)
98
//! this = a + b * scalar
99
inline_ Point& Mac(const Point& a, const Point& b, float scalar)
101
x = a.x + b.x * scalar;
102
y = a.y + b.y * scalar;
103
z = a.z + b.z * scalar;
107
//! this = this + a * scalar
108
inline_ Point& Mac(const Point& a, float scalar)
116
//! this = a - b * scalar
117
inline_ Point& Msc(const Point& a, const Point& b, float scalar)
119
x = a.x - b.x * scalar;
120
y = a.y - b.y * scalar;
121
z = a.z - b.z * scalar;
125
//! this = this - a * scalar
126
inline_ Point& Msc(const Point& a, float scalar)
134
//! this = a + b * scalarb + c * scalarc
135
inline_ Point& Mac2(const Point& a, const Point& b, float scalarb, const Point& c, float scalarc)
137
x = a.x + b.x * scalarb + c.x * scalarc;
138
y = a.y + b.y * scalarb + c.y * scalarc;
139
z = a.z + b.z * scalarb + c.z * scalarc;
143
//! this = a - b * scalarb - c * scalarc
144
inline_ Point& Msc2(const Point& a, const Point& b, float scalarb, const Point& c, float scalarc)
146
x = a.x - b.x * scalarb - c.x * scalarc;
147
y = a.y - b.y * scalarb - c.y * scalarc;
148
z = a.z - b.z * scalarb - c.z * scalarc;
153
inline_ Point& Mult(const Matrix3x3& mat, const Point& a);
155
//! this = mat1 * a1 + mat2 * a2
156
inline_ Point& Mult2(const Matrix3x3& mat1, const Point& a1, const Matrix3x3& mat2, const Point& a2);
158
//! this = this + mat * a
159
inline_ Point& Mac(const Matrix3x3& mat, const Point& a);
161
//! this = transpose(mat) * a
162
inline_ Point& TransMult(const Matrix3x3& mat, const Point& a);
164
//! Linear interpolate between two vectors: this = a + t * (b - a)
165
inline_ Point& Lerp(const Point& a, const Point& b, float t)
167
x = a.x + t * (b.x - a.x);
168
y = a.y + t * (b.y - a.y);
169
z = a.z + t * (b.z - a.z);
173
//! Hermite interpolate between p1 and p2. p0 and p3 are used for finding gradient at p1 and p2.
174
//! this = p0 * (2t^2 - t^3 - t)/2
175
//! + p1 * (3t^3 - 5t^2 + 2)/2
176
//! + p2 * (4t^2 - 3t^3 + t)/2
177
//! + p3 * (t^3 - t^2)/2
178
inline_ Point& Herp(const Point& p0, const Point& p1, const Point& p2, const Point& p3, float t)
182
float kp0 = (2.0f * t2 - t3 - t) * 0.5f;
183
float kp1 = (3.0f * t3 - 5.0f * t2 + 2.0f) * 0.5f;
184
float kp2 = (4.0f * t2 - 3.0f * t3 + t) * 0.5f;
185
float kp3 = (t3 - t2) * 0.5f;
186
x = p0.x * kp0 + p1.x * kp1 + p2.x * kp2 + p3.x * kp3;
187
y = p0.y * kp0 + p1.y * kp1 + p2.y * kp2 + p3.y * kp3;
188
z = p0.z * kp0 + p1.z * kp1 + p2.z * kp2 + p3.z * kp3;
192
//! this = rotpos * r + linpos
193
inline_ Point& Transform(const Point& r, const Matrix3x3& rotpos, const Point& linpos);
195
//! this = trans(rotpos) * (r - linpos)
196
inline_ Point& InvTransform(const Point& r, const Matrix3x3& rotpos, const Point& linpos);
198
//! Returns MIN(x, y, z);
199
inline_ float Min() const { return MIN(x, MIN(y, z)); }
200
//! Returns MAX(x, y, z);
201
inline_ float Max() const { return MAX(x, MAX(y, z)); }
202
//! Sets each element to be componentwise minimum
203
inline_ Point& Min(const Point& p) { x = MIN(x, p.x); y = MIN(y, p.y); z = MIN(z, p.z); return *this; }
204
//! Sets each element to be componentwise maximum
205
inline_ Point& Max(const Point& p) { x = MAX(x, p.x); y = MAX(y, p.y); z = MAX(z, p.z); return *this; }
207
//! Clamps each element
208
inline_ Point& Clamp(float min, float max)
210
if(x<min) x=min; if(x>max) x=max;
211
if(y<min) y=min; if(y>max) y=max;
212
if(z<min) z=min; if(z>max) z=max;
216
//! Computes square magnitude
217
inline_ float SquareMagnitude() const { return x*x + y*y + z*z; }
218
//! Computes magnitude
219
inline_ float Magnitude() const { return sqrtf(x*x + y*y + z*z); }
221
inline_ float Volume() const { return x * y * z; }
223
//! Checks the point is near zero
224
inline_ bool ApproxZero() const { return SquareMagnitude() < EPSILON2; }
226
//! Tests for exact zero vector
227
inline_ BOOL IsZero() const
229
if(IR(x) || IR(y) || IR(z)) return FALSE;
233
//! Checks point validity
234
inline_ BOOL IsValid() const
236
if(!IsValidFloat(x)) return FALSE;
237
if(!IsValidFloat(y)) return FALSE;
238
if(!IsValidFloat(z)) return FALSE;
242
//! Slighty moves the point
243
void Tweak(udword coord_mask, udword tweak_mask)
245
if(coord_mask&1) { udword Dummy = IR(x); Dummy^=tweak_mask; x = FR(Dummy); }
246
if(coord_mask&2) { udword Dummy = IR(y); Dummy^=tweak_mask; y = FR(Dummy); }
247
if(coord_mask&4) { udword Dummy = IR(z); Dummy^=tweak_mask; z = FR(Dummy); }
250
#define TWEAKMASK 0x3fffff
251
#define TWEAKNOTMASK ~TWEAKMASK
252
//! Slighty moves the point out
253
inline_ void TweakBigger()
255
udword Dummy = (IR(x)&TWEAKNOTMASK); if(!IS_NEGATIVE_FLOAT(x)) Dummy+=TWEAKMASK+1; x = FR(Dummy);
256
Dummy = (IR(y)&TWEAKNOTMASK); if(!IS_NEGATIVE_FLOAT(y)) Dummy+=TWEAKMASK+1; y = FR(Dummy);
257
Dummy = (IR(z)&TWEAKNOTMASK); if(!IS_NEGATIVE_FLOAT(z)) Dummy+=TWEAKMASK+1; z = FR(Dummy);
260
//! Slighty moves the point in
261
inline_ void TweakSmaller()
263
udword Dummy = (IR(x)&TWEAKNOTMASK); if(IS_NEGATIVE_FLOAT(x)) Dummy+=TWEAKMASK+1; x = FR(Dummy);
264
Dummy = (IR(y)&TWEAKNOTMASK); if(IS_NEGATIVE_FLOAT(y)) Dummy+=TWEAKMASK+1; y = FR(Dummy);
265
Dummy = (IR(z)&TWEAKNOTMASK); if(IS_NEGATIVE_FLOAT(z)) Dummy+=TWEAKMASK+1; z = FR(Dummy);
268
//! Normalizes the vector
269
inline_ Point& Normalize()
271
float M = x*x + y*y + z*z;
282
//! Sets vector length
283
inline_ Point& SetLength(float length)
285
float NewLength = length / Magnitude();
292
//! Clamps vector length
293
inline_ Point& ClampLength(float limit_length)
295
if(limit_length>=0.0f) // Magnitude must be positive
297
float CurrentSquareLength = SquareMagnitude();
299
if(CurrentSquareLength > limit_length * limit_length)
301
float Coeff = limit_length / sqrtf(CurrentSquareLength);
310
//! Computes distance to another point
311
inline_ float Distance(const Point& b) const
313
return sqrtf((x - b.x)*(x - b.x) + (y - b.y)*(y - b.y) + (z - b.z)*(z - b.z));
316
//! Computes square distance to another point
317
inline_ float SquareDistance(const Point& b) const
319
return ((x - b.x)*(x - b.x) + (y - b.y)*(y - b.y) + (z - b.z)*(z - b.z));
322
//! Dot product dp = this|a
323
inline_ float Dot(const Point& p) const { return p.x * x + p.y * y + p.z * z; }
325
//! Cross product this = a x b
326
inline_ Point& Cross(const Point& a, const Point& b)
328
x = a.y * b.z - a.z * b.y;
329
y = a.z * b.x - a.x * b.z;
330
z = a.x * b.y - a.y * b.x;
334
//! Vector code ( bitmask = sign(z) | sign(y) | sign(x) )
335
inline_ udword VectorCode() const
337
return (IR(x)>>31) | ((IR(y)&SIGN_BITMASK)>>30) | ((IR(z)&SIGN_BITMASK)>>29);
340
//! Returns largest axis
341
inline_ PointComponent LargestAxis() const
343
const float* Vals = &x;
344
PointComponent m = _X;
345
if(Vals[_Y] > Vals[m]) m = _Y;
346
if(Vals[_Z] > Vals[m]) m = _Z;
350
//! Returns closest axis
351
inline_ PointComponent ClosestAxis() const
353
const float* Vals = &x;
354
PointComponent m = _X;
355
if(AIR(Vals[_Y]) > AIR(Vals[m])) m = _Y;
356
if(AIR(Vals[_Z]) > AIR(Vals[m])) m = _Z;
360
//! Returns smallest axis
361
inline_ PointComponent SmallestAxis() const
363
const float* Vals = &x;
364
PointComponent m = _X;
365
if(Vals[_Y] < Vals[m]) m = _Y;
366
if(Vals[_Z] < Vals[m]) m = _Z;
370
//! Refracts the point
371
Point& Refract(const Point& eye, const Point& n, float refractindex, Point& refracted);
373
//! Projects the point onto a plane
374
Point& ProjectToPlane(const Plane& p);
376
//! Projects the point onto the screen
377
void ProjectToScreen(float halfrenderwidth, float halfrenderheight, const Matrix4x4& mat, HPoint& projected) const;
379
//! Unfolds the point onto a plane according to edge(a,b)
380
Point& Unfold(Plane& p, Point& a, Point& b);
382
//! Hash function from Ville Miettinen
383
inline_ udword GetHashValue() const
385
const udword* h = (const udword*)(this);
386
udword f = (h[0]+h[1]*11-(h[2]*17)) & 0x7fffffff; // avoid problems with +-0
387
return (f>>22)^(f>>12)^(f);
390
//! Stuff magic values in the point, marking it as explicitely not used.
392
//! Checks the point is marked as not used
393
BOOL IsNotUsed() const;
395
// Arithmetic operators
397
//! Unary operator for Point Negate = - Point
398
inline_ Point operator-() const { return Point(-x, -y, -z); }
400
//! Operator for Point Plus = Point + Point.
401
inline_ Point operator+(const Point& p) const { return Point(x + p.x, y + p.y, z + p.z); }
402
//! Operator for Point Minus = Point - Point.
403
inline_ Point operator-(const Point& p) const { return Point(x - p.x, y - p.y, z - p.z); }
405
//! Operator for Point Mul = Point * Point.
406
inline_ Point operator*(const Point& p) const { return Point(x * p.x, y * p.y, z * p.z); }
407
//! Operator for Point Scale = Point * float.
408
inline_ Point operator*(float s) const { return Point(x * s, y * s, z * s ); }
409
//! Operator for Point Scale = float * Point.
410
inline_ friend Point operator*(float s, const Point& p) { return Point(s * p.x, s * p.y, s * p.z); }
412
//! Operator for Point Div = Point / Point.
413
inline_ Point operator/(const Point& p) const { return Point(x / p.x, y / p.y, z / p.z); }
414
//! Operator for Point Scale = Point / float.
415
inline_ Point operator/(float s) const { s = 1.0f / s; return Point(x * s, y * s, z * s); }
416
//! Operator for Point Scale = float / Point.
417
inline_ friend Point operator/(float s, const Point& p) { return Point(s / p.x, s / p.y, s / p.z); }
419
//! Operator for float DotProd = Point | Point.
420
inline_ float operator|(const Point& p) const { return x*p.x + y*p.y + z*p.z; }
421
//! Operator for Point VecProd = Point ^ Point.
422
inline_ Point operator^(const Point& p) const
430
//! Operator for Point += Point.
431
inline_ Point& operator+=(const Point& p) { x += p.x; y += p.y; z += p.z; return *this; }
432
//! Operator for Point += float.
433
inline_ Point& operator+=(float s) { x += s; y += s; z += s; return *this; }
435
//! Operator for Point -= Point.
436
inline_ Point& operator-=(const Point& p) { x -= p.x; y -= p.y; z -= p.z; return *this; }
437
//! Operator for Point -= float.
438
inline_ Point& operator-=(float s) { x -= s; y -= s; z -= s; return *this; }
440
//! Operator for Point *= Point.
441
inline_ Point& operator*=(const Point& p) { x *= p.x; y *= p.y; z *= p.z; return *this; }
442
//! Operator for Point *= float.
443
inline_ Point& operator*=(float s) { x *= s; y *= s; z *= s; return *this; }
445
//! Operator for Point /= Point.
446
inline_ Point& operator/=(const Point& p) { x /= p.x; y /= p.y; z /= p.z; return *this; }
447
//! Operator for Point /= float.
448
inline_ Point& operator/=(float s) { s = 1.0f/s; x *= s; y *= s; z *= s; return *this; }
452
//! Operator for "if(Point==Point)"
453
inline_ bool operator==(const Point& p) const { return ( (IR(x)==IR(p.x))&&(IR(y)==IR(p.y))&&(IR(z)==IR(p.z))); }
454
//! Operator for "if(Point!=Point)"
455
inline_ bool operator!=(const Point& p) const { return ( (IR(x)!=IR(p.x))||(IR(y)!=IR(p.y))||(IR(z)!=IR(p.z))); }
457
// Arithmetic operators
459
//! Operator for Point Mul = Point * Matrix3x3.
460
inline_ Point operator*(const Matrix3x3& mat) const
462
class ShadowMatrix3x3{ public: float m[3][3]; }; // To allow inlining
463
const ShadowMatrix3x3* Mat = (const ShadowMatrix3x3*)&mat;
466
x * Mat->m[0][0] + y * Mat->m[1][0] + z * Mat->m[2][0],
467
x * Mat->m[0][1] + y * Mat->m[1][1] + z * Mat->m[2][1],
468
x * Mat->m[0][2] + y * Mat->m[1][2] + z * Mat->m[2][2] );
471
//! Operator for Point Mul = Point * Matrix4x4.
472
inline_ Point operator*(const Matrix4x4& mat) const
474
class ShadowMatrix4x4{ public: float m[4][4]; }; // To allow inlining
475
const ShadowMatrix4x4* Mat = (const ShadowMatrix4x4*)&mat;
478
x * Mat->m[0][0] + y * Mat->m[1][0] + z * Mat->m[2][0] + Mat->m[3][0],
479
x * Mat->m[0][1] + y * Mat->m[1][1] + z * Mat->m[2][1] + Mat->m[3][1],
480
x * Mat->m[0][2] + y * Mat->m[1][2] + z * Mat->m[2][2] + Mat->m[3][2]);
483
//! Operator for Point *= Matrix3x3.
484
inline_ Point& operator*=(const Matrix3x3& mat)
486
class ShadowMatrix3x3{ public: float m[3][3]; }; // To allow inlining
487
const ShadowMatrix3x3* Mat = (const ShadowMatrix3x3*)&mat;
489
float xp = x * Mat->m[0][0] + y * Mat->m[1][0] + z * Mat->m[2][0];
490
float yp = x * Mat->m[0][1] + y * Mat->m[1][1] + z * Mat->m[2][1];
491
float zp = x * Mat->m[0][2] + y * Mat->m[1][2] + z * Mat->m[2][2];
493
x = xp; y = yp; z = zp;
498
//! Operator for Point *= Matrix4x4.
499
inline_ Point& operator*=(const Matrix4x4& mat)
501
class ShadowMatrix4x4{ public: float m[4][4]; }; // To allow inlining
502
const ShadowMatrix4x4* Mat = (const ShadowMatrix4x4*)&mat;
504
float xp = x * Mat->m[0][0] + y * Mat->m[1][0] + z * Mat->m[2][0] + Mat->m[3][0];
505
float yp = x * Mat->m[0][1] + y * Mat->m[1][1] + z * Mat->m[2][1] + Mat->m[3][1];
506
float zp = x * Mat->m[0][2] + y * Mat->m[1][2] + z * Mat->m[2][2] + Mat->m[3][2];
508
x = xp; y = yp; z = zp;
515
//! Cast a Point to a HPoint. w is set to zero.
516
operator HPoint() const;
518
inline_ operator const float*() const { return &x; }
519
inline_ operator float*() { return &x; }
525
FUNCTION void Normalize1(Point& a);
526
FUNCTION void Normalize2(Point& a);
528
#endif //__ICEPOINT_H__