Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IcePoint.h
Go to the documentation of this file.
1 
8 
11 // Include Guard
12 #ifndef __ICEPOINT_H__
13 #define __ICEPOINT_H__
14 
15  // Forward declarations
16  class HPoint;
17  class Plane;
18  class Matrix3x3;
19  class Matrix4x4;
20 
21  #define CROSS2D(a, b) (a.x*b.y - b.x*a.y)
22 
23  const float EPSILON2 = 1.0e-20f;
24 
26  {
27  public:
28 
32 // inline_ Point(float val) : x(val), y(val), z(val) {}
33 // Removed since it introduced the nasty "Point T = *Matrix4x4.GetTrans();" bug.......
35  inline_ Point(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
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) {}
42 
44  inline_ Point& Zero() { x = y = z = 0.0f; return *this; }
45 
47  inline_ Point& SetPlusInfinity() { x = y = z = MAX_FLOAT; return *this; }
49  inline_ Point& SetMinusInfinity() { x = y = z = MIN_FLOAT; return *this; }
50 
52  Point& PositiveUnitRandomVector();
54  Point& UnitRandomVector();
55 
57  inline_ Point& Set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; return *this; }
59  inline_ Point& Set(const float f[3]) { x = f[_X]; y = f[_Y]; z = f[_Z]; return *this; }
61  inline_ Point& Set(const Point& src) { x = src.x; y = src.y; z = src.z; return *this; }
62 
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; }
71 
73  inline_ Point& Sub(const Point& p) { x -= p.x; y -= p.y; z -= p.z; return *this; }
75  inline_ Point& Sub(float _x, float _y, float _z) { x -= _x; y -= _y; z -= _z; return *this; }
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; }
80 
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; }
85 
87  inline_ Point& Mult(float s) { x *= s; y *= s; z *= s; return *this; }
88 
90  inline_ Point& Mult(const Point& a, float scalar)
91  {
92  x = a.x * scalar;
93  y = a.y * scalar;
94  z = a.z * scalar;
95  return *this;
96  }
97 
99  inline_ Point& Mac(const Point& a, const Point& b, float scalar)
100  {
101  x = a.x + b.x * scalar;
102  y = a.y + b.y * scalar;
103  z = a.z + b.z * scalar;
104  return *this;
105  }
106 
108  inline_ Point& Mac(const Point& a, float scalar)
109  {
110  x += a.x * scalar;
111  y += a.y * scalar;
112  z += a.z * scalar;
113  return *this;
114  }
115 
117  inline_ Point& Msc(const Point& a, const Point& b, float scalar)
118  {
119  x = a.x - b.x * scalar;
120  y = a.y - b.y * scalar;
121  z = a.z - b.z * scalar;
122  return *this;
123  }
124 
126  inline_ Point& Msc(const Point& a, float scalar)
127  {
128  x -= a.x * scalar;
129  y -= a.y * scalar;
130  z -= a.z * scalar;
131  return *this;
132  }
133 
135  inline_ Point& Mac2(const Point& a, const Point& b, float scalarb, const Point& c, float scalarc)
136  {
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;
140  return *this;
141  }
142 
144  inline_ Point& Msc2(const Point& a, const Point& b, float scalarb, const Point& c, float scalarc)
145  {
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;
149  return *this;
150  }
151 
153  inline_ Point& Mult(const Matrix3x3& mat, const Point& a);
154 
156  inline_ Point& Mult2(const Matrix3x3& mat1, const Point& a1, const Matrix3x3& mat2, const Point& a2);
157 
159  inline_ Point& Mac(const Matrix3x3& mat, const Point& a);
160 
162  inline_ Point& TransMult(const Matrix3x3& mat, const Point& a);
163 
165  inline_ Point& Lerp(const Point& a, const Point& b, float t)
166  {
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);
170  return *this;
171  }
172 
178  inline_ Point& Herp(const Point& p0, const Point& p1, const Point& p2, const Point& p3, float t)
179  {
180  float t2 = t * t;
181  float t3 = t2 * 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;
189  return *this;
190  }
191 
193  inline_ Point& Transform(const Point& r, const Matrix3x3& rotpos, const Point& linpos);
194 
196  inline_ Point& InvTransform(const Point& r, const Matrix3x3& rotpos, const Point& linpos);
197 
199  inline_ float Min() const { return MIN(x, MIN(y, z)); }
201  inline_ float Max() const { return MAX(x, MAX(y, z)); }
203  inline_ Point& Min(const Point& p) { x = MIN(x, p.x); y = MIN(y, p.y); z = MIN(z, p.z); return *this; }
205  inline_ Point& Max(const Point& p) { x = MAX(x, p.x); y = MAX(y, p.y); z = MAX(z, p.z); return *this; }
206 
208  inline_ Point& Clamp(float min, float max)
209  {
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;
213  return *this;
214  }
215 
217  inline_ float SquareMagnitude() const { return x*x + y*y + z*z; }
219  inline_ float Magnitude() const { return sqrtf(x*x + y*y + z*z); }
221  inline_ float Volume() const { return x * y * z; }
222 
224  inline_ bool ApproxZero() const { return SquareMagnitude() < EPSILON2; }
225 
227  inline_ bool IsZero() const
228  {
229  if(IR(x) || IR(y) || IR(z)) return FALSE;
230  return TRUE;
231  }
232 
234  inline_ bool IsValid() const
235  {
236  if(!IsValidFloat(x)) return FALSE;
237  if(!IsValidFloat(y)) return FALSE;
238  if(!IsValidFloat(z)) return FALSE;
239  return TRUE;
240  }
241 
243  void Tweak(udword coord_mask, udword tweak_mask)
244  {
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); }
248  }
249 
250  #define TWEAKMASK 0x3fffff
251  #define TWEAKNOTMASK ~TWEAKMASK
252  inline_ void TweakBigger()
254  {
255  udword Dummy = (IR(x)&TWEAKNOTMASK); if(x >= 0.0f) Dummy+=TWEAKMASK+1; x = FR(Dummy);
256  Dummy = (IR(y)&TWEAKNOTMASK); if(y >= 0.0f) Dummy+=TWEAKMASK+1; y = FR(Dummy);
257  Dummy = (IR(z)&TWEAKNOTMASK); if(z >= 0.0f) Dummy+=TWEAKMASK+1; z = FR(Dummy);
258  }
259 
262  {
263  udword Dummy = (IR(x)&TWEAKNOTMASK); if(x < 0.0f) Dummy+=TWEAKMASK+1; x = FR(Dummy);
264  Dummy = (IR(y)&TWEAKNOTMASK); if(y < 0.0f) Dummy+=TWEAKMASK+1; y = FR(Dummy);
265  Dummy = (IR(z)&TWEAKNOTMASK); if(z < 0.0f) Dummy+=TWEAKMASK+1; z = FR(Dummy);
266  }
267 
270  {
271  float M = x*x + y*y + z*z;
272  if(M)
273  {
274  M = 1.0f / sqrtf(M);
275  x *= M;
276  y *= M;
277  z *= M;
278  }
279  return *this;
280  }
281 
283  inline_ Point& SetLength(float length)
284  {
285  float NewLength = length / Magnitude();
286  x *= NewLength;
287  y *= NewLength;
288  z *= NewLength;
289  return *this;
290  }
291 
293  inline_ Point& ClampLength(float limit_length)
294  {
295  if(limit_length>=0.0f) // Magnitude must be positive
296  {
297  float CurrentSquareLength = SquareMagnitude();
298 
299  if(CurrentSquareLength > limit_length * limit_length)
300  {
301  float Coeff = limit_length / sqrtf(CurrentSquareLength);
302  x *= Coeff;
303  y *= Coeff;
304  z *= Coeff;
305  }
306  }
307  return *this;
308  }
309 
311  inline_ float Distance(const Point& b) const
312  {
313  return sqrtf((x - b.x)*(x - b.x) + (y - b.y)*(y - b.y) + (z - b.z)*(z - b.z));
314  }
315 
317  inline_ float SquareDistance(const Point& b) const
318  {
319  return ((x - b.x)*(x - b.x) + (y - b.y)*(y - b.y) + (z - b.z)*(z - b.z));
320  }
321 
323  inline_ float Dot(const Point& p) const { return p.x * x + p.y * y + p.z * z; }
324 
326  inline_ Point& Cross(const Point& a, const Point& b)
327  {
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;
331  return *this;
332  }
333 
336  {
337  return (IR(x)>>31) | ((IR(y)&SIGN_BITMASK)>>30) | ((IR(z)&SIGN_BITMASK)>>29);
338  }
339 
342  {
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;
347  return m;
348  }
349 
352  {
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;
357  return m;
358  }
359 
362  {
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;
367  return m;
368  }
369 
371  Point& Refract(const Point& eye, const Point& n, float refractindex, Point& refracted);
372 
374  Point& ProjectToPlane(const Plane& p);
375 
377  void ProjectToScreen(float halfrenderwidth, float halfrenderheight, const Matrix4x4& mat, HPoint& projected) const;
378 
380  Point& Unfold(Plane& p, Point& a, Point& b);
381 
384  {
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);
388  }
389 
391  void SetNotUsed();
393  bool IsNotUsed() const;
394 
395  // Arithmetic operators
396 
398  inline_ Point operator-() const { return Point(-x, -y, -z); }
399 
401  inline_ Point operator+(const Point& p) const { return Point(x + p.x, y + p.y, z + p.z); }
403  inline_ Point operator-(const Point& p) const { return Point(x - p.x, y - p.y, z - p.z); }
404 
406  inline_ Point operator*(const Point& p) const { return Point(x * p.x, y * p.y, z * p.z); }
408  inline_ Point operator*(float s) const { return Point(x * s, y * s, z * s ); }
410  inline_ friend Point operator*(float s, const Point& p) { return Point(s * p.x, s * p.y, s * p.z); }
411 
413  inline_ Point operator/(const Point& p) const { return Point(x / p.x, y / p.y, z / p.z); }
415  inline_ Point operator/(float s) const { s = 1.0f / s; return Point(x * s, y * s, z * s); }
417  inline_ friend Point operator/(float s, const Point& p) { return Point(s / p.x, s / p.y, s / p.z); }
418 
420  inline_ float operator|(const Point& p) const { return x*p.x + y*p.y + z*p.z; }
422  inline_ Point operator^(const Point& p) const
423  {
424  return Point(
425  y * p.z - z * p.y,
426  z * p.x - x * p.z,
427  x * p.y - y * p.x );
428  }
429 
431  inline_ Point& operator+=(const Point& p) { x += p.x; y += p.y; z += p.z; return *this; }
433  inline_ Point& operator+=(float s) { x += s; y += s; z += s; return *this; }
434 
436  inline_ Point& operator-=(const Point& p) { x -= p.x; y -= p.y; z -= p.z; return *this; }
438  inline_ Point& operator-=(float s) { x -= s; y -= s; z -= s; return *this; }
439 
441  inline_ Point& operator*=(const Point& p) { x *= p.x; y *= p.y; z *= p.z; return *this; }
443  inline_ Point& operator*=(float s) { x *= s; y *= s; z *= s; return *this; }
444 
446  inline_ Point& operator/=(const Point& p) { x /= p.x; y /= p.y; z /= p.z; return *this; }
448  inline_ Point& operator/=(float s) { s = 1.0f/s; x *= s; y *= s; z *= s; return *this; }
449 
450  // Logical operators
451 
453  inline_ bool operator==(const Point& p) const { return ( (IR(x)==IR(p.x))&&(IR(y)==IR(p.y))&&(IR(z)==IR(p.z))); }
455  inline_ bool operator!=(const Point& p) const { return ( (IR(x)!=IR(p.x))||(IR(y)!=IR(p.y))||(IR(z)!=IR(p.z))); }
456 
457  // Arithmetic operators
458 
460  inline_ Point operator*(const Matrix3x3& mat) const
461  {
462  class ShadowMatrix3x3{ public: float m[3][3]; }; // To allow inlining
463  const ShadowMatrix3x3* Mat = (const ShadowMatrix3x3*)&mat;
464 
465  return Point(
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] );
469  }
470 
472  inline_ Point operator*(const Matrix4x4& mat) const
473  {
474  class ShadowMatrix4x4{ public: float m[4][4]; }; // To allow inlining
475  const ShadowMatrix4x4* Mat = (const ShadowMatrix4x4*)&mat;
476 
477  return Point(
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]);
481  }
482 
485  {
486  class ShadowMatrix3x3{ public: float m[3][3]; }; // To allow inlining
487  const ShadowMatrix3x3* Mat = (const ShadowMatrix3x3*)&mat;
488 
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];
492 
493  x = xp; y = yp; z = zp;
494 
495  return *this;
496  }
497 
500  {
501  class ShadowMatrix4x4{ public: float m[4][4]; }; // To allow inlining
502  const ShadowMatrix4x4* Mat = (const ShadowMatrix4x4*)&mat;
503 
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];
507 
508  x = xp; y = yp; z = zp;
509 
510  return *this;
511  }
512 
513  // Cast operators
514 
516  operator HPoint() const;
517 
518  inline_ float operator[](int n) const { return *(&x + n); }
519  inline_ float& operator[](int n) { return *(&x + n); }
520 
521  public:
522  float x, y, z;
523  };
524 
527 
528 #endif //__ICEPOINT_H__