AABox.cs 3.4 KB

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