OPC_Common.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /*
  3. * OPCODE - Optimized Collision Detection
  4. * Copyright (C) 2001 Pierre Terdiman
  5. * Homepage: http://www.codercorner.com/Opcode.htm
  6. */
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /**
  10. * Contains common classes & defs used in OPCODE.
  11. * \file OPC_Common.h
  12. * \author Pierre Terdiman
  13. * \date March, 20, 2001
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Include Guard
  18. #ifndef __OPC_COMMON_H__
  19. #define __OPC_COMMON_H__
  20. // [GOTTFRIED]: Just a small change for readability.
  21. #ifdef OPC_CPU_COMPARE
  22. #define GREATER(x, y) AIR(x) > IR(y)
  23. #else
  24. #define GREATER(x, y) fabsf(x) > (y)
  25. #endif
  26. class OPCODE_API CollisionAABB
  27. {
  28. public:
  29. //! Constructor
  30. inline_ CollisionAABB() {}
  31. //! Constructor
  32. inline_ CollisionAABB(const AABB& b) { b.GetCenter(mCenter); b.GetExtents(mExtents); }
  33. //! Destructor
  34. inline_ ~CollisionAABB() {}
  35. //! Get min point of the box
  36. inline_ void GetMin(Point& min) const { min = mCenter - mExtents; }
  37. //! Get max point of the box
  38. inline_ void GetMax(Point& max) const { max = mCenter + mExtents; }
  39. //! Get component of the box's min point along a given axis
  40. inline_ float GetMin(udword axis) const { return mCenter[axis] - mExtents[axis]; }
  41. //! Get component of the box's max point along a given axis
  42. inline_ float GetMax(udword axis) const { return mCenter[axis] + mExtents[axis]; }
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. /**
  45. * Setups an AABB from min & max vectors.
  46. * \param min [in] the min point
  47. * \param max [in] the max point
  48. */
  49. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. inline_ void SetMinMax(const Point& min, const Point& max) { mCenter = (max + min)*0.5f; mExtents = (max - min)*0.5f; }
  51. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. /**
  53. * Checks a box is inside another box.
  54. * \param box [in] the other box
  55. * \return true if current box is inside input box
  56. */
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. inline_ BOOL IsInside(const CollisionAABB& box) const
  59. {
  60. if(box.GetMin(0)>GetMin(0)) return FALSE;
  61. if(box.GetMin(1)>GetMin(1)) return FALSE;
  62. if(box.GetMin(2)>GetMin(2)) return FALSE;
  63. if(box.GetMax(0)<GetMax(0)) return FALSE;
  64. if(box.GetMax(1)<GetMax(1)) return FALSE;
  65. if(box.GetMax(2)<GetMax(2)) return FALSE;
  66. return TRUE;
  67. }
  68. Point mCenter; //!< Box center
  69. Point mExtents; //!< Box extents
  70. };
  71. class OPCODE_API QuantizedAABB
  72. {
  73. public:
  74. //! Constructor
  75. inline_ QuantizedAABB() {}
  76. //! Destructor
  77. inline_ ~QuantizedAABB() {}
  78. sword mCenter[3]; //!< Quantized center
  79. uword mExtents[3]; //!< Quantized extents
  80. };
  81. //! Quickly rotates & translates a vector
  82. inline_ void TransformPoint(Point& dest, const Point& source, const Matrix3x3& rot, const Point& trans)
  83. {
  84. dest.x = trans.x + source.x * rot.m[0][0] + source.y * rot.m[1][0] + source.z * rot.m[2][0];
  85. dest.y = trans.y + source.x * rot.m[0][1] + source.y * rot.m[1][1] + source.z * rot.m[2][1];
  86. dest.z = trans.z + source.x * rot.m[0][2] + source.y * rot.m[1][2] + source.z * rot.m[2][2];
  87. }
  88. #endif //__OPC_COMMON_H__