AABox.cs 3.4 KB

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