Clone of PhatAC @ https://github.com/floaterxk/PhatAC

MathLib.h 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include <math.h>
  3. // #define F_EPSILON (0.0002f)
  4. class Frame;
  5. class Vec2D
  6. {
  7. public:
  8. inline Vec2D() {
  9. }
  10. inline Vec2D(float _x, float _y) {
  11. x = _x;
  12. y = _y;
  13. }
  14. inline Vec2D& operator+=(const Vec2D& quant) {
  15. x += quant.x;
  16. y += quant.y;
  17. return *this;
  18. }
  19. ULONG pack_size();
  20. ULONG Pack(BYTE** ppData, ULONG iSize);
  21. BOOL UnPack(BYTE** ppData, ULONG iSize);
  22. float x, y;
  23. };
  24. Vector cross_product(const Vector& v1, const Vector& v2);
  25. // Imaginary class, either entirely inlined by Turbine or non-existant.
  26. class Quaternion
  27. {
  28. public:
  29. inline Quaternion() {
  30. }
  31. inline Quaternion(float _w, float _x, float _y, float _z) {
  32. w = _w;
  33. x = _x;
  34. y = _y;
  35. z = _z;
  36. }
  37. // Should probably be renamed. It's not the length of the quaternion!!
  38. inline float magnitude() const {
  39. return (float)sqrt((x * x) + (y * y) + (z * z) + (w * w));
  40. }
  41. inline ULONG pack_size() {
  42. return(sizeof(float) * 4);
  43. }
  44. inline ULONG Pack(BYTE** ppData, ULONG iSize) {
  45. // The client performs no size check, but we will.
  46. ULONG PackSize = pack_size();
  47. if (iSize >= PackSize) {
  48. PACK(float, w);
  49. PACK(float, x);
  50. PACK(float, y);
  51. PACK(float, z);
  52. }
  53. return PackSize;
  54. }
  55. inline BOOL UnPack(BYTE** ppData, ULONG iSize) {
  56. if (iSize < pack_size())
  57. return FALSE;
  58. UNPACK(float, w);
  59. UNPACK(float, x);
  60. UNPACK(float, y);
  61. UNPACK(float, z);
  62. return TRUE;
  63. }
  64. BOOL IsValid() const;
  65. void normalize();
  66. float dot_product(const Quaternion& q) const;
  67. float w, x, y, z;
  68. };
  69. class Ray
  70. {
  71. public:
  72. Vector m_origin;
  73. Vector m_direction;
  74. };
  75. class Plane
  76. {
  77. public:
  78. // Constructors
  79. Plane();
  80. Plane(Vector& Vect1, Vector& Vect2);
  81. // Pack Functions
  82. ULONG pack_size();
  83. BOOL UnPack(BYTE** ppData, ULONG iSize);
  84. // Math Functions
  85. float dot_product(const Vector& point);
  86. int which_side(const Vector& point, float near_dist);
  87. BOOL compute_time_of_intersection(const Ray& ray, float *time);
  88. Vector m_normal;
  89. float m_dist;
  90. };