Box.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright (C) 2006, 2008 Sony Computer Entertainment Inc.
  3. All rights reserved.
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef __BOX_H__
  14. #define __BOX_H__
  15. #ifndef PE_REF
  16. #define PE_REF(a) a&
  17. #endif
  18. #include <math.h>
  19. #include "../PlatformDefinitions.h"
  20. enum FeatureType { F, E, V };
  21. //----------------------------------------------------------------------------
  22. // Box
  23. //----------------------------------------------------------------------------
  24. ///The Box is an internal class used by the boxBoxDistance calculation.
  25. class Box
  26. {
  27. public:
  28. vmVector3 mHalf;
  29. inline Box()
  30. {}
  31. inline Box(PE_REF(vmVector3) half_);
  32. inline Box(float hx, float hy, float hz);
  33. inline void Set(PE_REF(vmVector3) half_);
  34. inline void Set(float hx, float hy, float hz);
  35. inline vmVector3 GetAABB(const vmMatrix3& rotation) const;
  36. };
  37. inline
  38. Box::Box(PE_REF(vmVector3) half_)
  39. {
  40. Set(half_);
  41. }
  42. inline
  43. Box::Box(float hx, float hy, float hz)
  44. {
  45. Set(hx, hy, hz);
  46. }
  47. inline
  48. void
  49. Box::Set(PE_REF(vmVector3) half_)
  50. {
  51. mHalf = half_;
  52. }
  53. inline
  54. void
  55. Box::Set(float hx, float hy, float hz)
  56. {
  57. mHalf = vmVector3(hx, hy, hz);
  58. }
  59. inline
  60. vmVector3
  61. Box::GetAABB(const vmMatrix3& rotation) const
  62. {
  63. return absPerElem(rotation) * mHalf;
  64. }
  65. //-------------------------------------------------------------------------------------------------
  66. // BoxPoint
  67. //-------------------------------------------------------------------------------------------------
  68. ///The BoxPoint class is an internally used class to contain feature information for boxBoxDistance calculation.
  69. class BoxPoint
  70. {
  71. public:
  72. BoxPoint() : localPoint(0.0f) {}
  73. vmPoint3 localPoint;
  74. FeatureType featureType;
  75. int featureIdx;
  76. inline void setVertexFeature(int plusX, int plusY, int plusZ);
  77. inline void setEdgeFeature(int dim0, int plus0, int dim1, int plus1);
  78. inline void setFaceFeature(int dim, int plus);
  79. inline void getVertexFeature(int & plusX, int & plusY, int & plusZ) const;
  80. inline void getEdgeFeature(int & dim0, int & plus0, int & dim1, int & plus1) const;
  81. inline void getFaceFeature(int & dim, int & plus) const;
  82. };
  83. inline
  84. void
  85. BoxPoint::setVertexFeature(int plusX, int plusY, int plusZ)
  86. {
  87. featureType = V;
  88. featureIdx = plusX << 2 | plusY << 1 | plusZ;
  89. }
  90. inline
  91. void
  92. BoxPoint::setEdgeFeature(int dim0, int plus0, int dim1, int plus1)
  93. {
  94. featureType = E;
  95. if (dim0 > dim1) {
  96. featureIdx = plus1 << 5 | dim1 << 3 | plus0 << 2 | dim0;
  97. } else {
  98. featureIdx = plus0 << 5 | dim0 << 3 | plus1 << 2 | dim1;
  99. }
  100. }
  101. inline
  102. void
  103. BoxPoint::setFaceFeature(int dim, int plus)
  104. {
  105. featureType = F;
  106. featureIdx = plus << 2 | dim;
  107. }
  108. inline
  109. void
  110. BoxPoint::getVertexFeature(int & plusX, int & plusY, int & plusZ) const
  111. {
  112. plusX = featureIdx >> 2;
  113. plusY = featureIdx >> 1 & 1;
  114. plusZ = featureIdx & 1;
  115. }
  116. inline
  117. void
  118. BoxPoint::getEdgeFeature(int & dim0, int & plus0, int & dim1, int & plus1) const
  119. {
  120. plus0 = featureIdx >> 5;
  121. dim0 = featureIdx >> 3 & 3;
  122. plus1 = featureIdx >> 2 & 1;
  123. dim1 = featureIdx & 3;
  124. }
  125. inline
  126. void
  127. BoxPoint::getFaceFeature(int & dim, int & plus) const
  128. {
  129. plus = featureIdx >> 2;
  130. dim = featureIdx & 3;
  131. }
  132. #endif /* __BOX_H__ */