AABox.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Runtime.InteropServices;
  2. namespace BansheeEngine
  3. {
  4. /// <summary>
  5. /// Axis aligned box represented by minimum and maximum point.
  6. /// </summary>
  7. [StructLayout(LayoutKind.Sequential), SerializeObject]
  8. public struct AABox // Note: Must match C++ class AABox
  9. {
  10. [SerializeField]
  11. private Vector3 minimum;
  12. [SerializeField]
  13. private Vector3 maximum;
  14. /// <summary>
  15. /// Corner of the box with minimum values (opposite to maximum corner).
  16. /// </summary>
  17. public Vector3 Minimum
  18. {
  19. get { return minimum; }
  20. set { minimum = value; }
  21. }
  22. /// <summary>
  23. /// Corner of the box with maximum values (opposite to minimum corner).
  24. /// </summary>
  25. public Vector3 Maximum
  26. {
  27. get { return maximum; }
  28. set { maximum = value; }
  29. }
  30. /// <summary>
  31. /// Returns the center of the box.
  32. /// </summary>
  33. public Vector3 Center
  34. {
  35. get
  36. {
  37. return new Vector3((maximum.x + minimum.x) * 0.5f,
  38. (maximum.y + minimum.y) * 0.5f,
  39. (maximum.z + minimum.z) * 0.5f);
  40. }
  41. }
  42. /// <summary>
  43. /// Returns the width, height and depth of the box.
  44. /// </summary>
  45. public Vector3 Size
  46. {
  47. get
  48. {
  49. return maximum - minimum;
  50. }
  51. }
  52. /// <summary>
  53. /// Creates a new axis aligned box.
  54. /// </summary>
  55. /// <param name="min">Corner of the box with minimum values.</param>
  56. /// <param name="max">Corner of the box with maximum values.</param>
  57. public AABox(Vector3 min, Vector3 max)
  58. {
  59. minimum = min;
  60. maximum = max;
  61. }
  62. /// <summary>
  63. /// Transforms the bounding box by the given matrix.
  64. ///
  65. /// As the resulting box will no longer be axis aligned, an axis align box is instead created by encompassing the
  66. /// transformed oriented bounding box. Retrieving the value as an actual OBB would provide a tighter fit.
  67. /// </summary>
  68. /// <param name="tfrm">Affine matrix to transform the box with.</param>
  69. public void TransformAffine(Matrix4 tfrm)
  70. {
  71. Vector3 center = Center;
  72. Vector3 halfSize = Size*0.5f;
  73. Vector3 newCenter = tfrm.MultiplyAffine(center);
  74. Vector3 newHalfSize = new Vector3(
  75. MathEx.Abs(tfrm.m00) * halfSize.x + MathEx.Abs(tfrm.m01) * halfSize.y + MathEx.Abs(tfrm.m02) * halfSize.z,
  76. MathEx.Abs(tfrm.m10) * halfSize.x + MathEx.Abs(tfrm.m11) * halfSize.y + MathEx.Abs(tfrm.m12) * halfSize.z,
  77. MathEx.Abs(tfrm.m20) * halfSize.x + MathEx.Abs(tfrm.m21) * halfSize.y + MathEx.Abs(tfrm.m22) * halfSize.z);
  78. minimum = newCenter - newHalfSize;
  79. maximum = newCenter + newHalfSize;
  80. }
  81. /// <inheritdoc/>
  82. public override string ToString()
  83. {
  84. return "Min: " + minimum + ". Max: " + maximum;
  85. }
  86. };
  87. }