Plane.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace BansheeEngine
  4. {
  5. /** @addtogroup Math
  6. * @{
  7. */
  8. /// <summary>
  9. /// A plane represented by a normal and a distance.
  10. /// </summary>
  11. [StructLayout(LayoutKind.Sequential), SerializeObject]
  12. public struct Plane // Note: Must match C++ class Plane
  13. {
  14. /// <summary>
  15. /// The "positive side" of the plane is the half space to which the plane normal points. The "negative side" is the
  16. /// other half space. The flag "no side" indicates the plane itself.
  17. /// </summary>
  18. public enum Side
  19. {
  20. None,
  21. Positive,
  22. Negative,
  23. Both
  24. };
  25. public Vector3 normal;
  26. public float d;
  27. /// <summary>
  28. /// Creates a plane from a normal and a distance from the plane.
  29. /// </summary>
  30. /// <param name="normal">Plane normal pointing in the positive half-space.</param>
  31. /// <param name="d">Distance of the plane from the origin, along the normal.</param>
  32. public Plane(Vector3 normal, float d)
  33. {
  34. this.normal = normal;
  35. this.d = d;
  36. }
  37. /// <summary>
  38. /// Creates a plane from three points on the plane. The points must not be colinear.
  39. /// </summary>
  40. /// <param name="a">A point in the plane.</param>
  41. /// <param name="b">A point in the plane.</param>
  42. /// <param name="c">A point in the plane.</param>
  43. public Plane(Vector3 a, Vector3 b, Vector3 c)
  44. {
  45. Vector3 e0 = b - a;
  46. Vector3 e1 = c - a;
  47. normal = Vector3.Cross(e0, e1);
  48. normal.Normalize();
  49. d = Vector3.Dot(normal, a);
  50. }
  51. /// <summary>
  52. /// Returns a distance from point to plane.
  53. /// </summary>
  54. /// <param name="point">Point to test.</param>
  55. /// <returns>
  56. /// Distance to the plane. Will be positive if the point is on the positive side of the plane, negative if on the
  57. /// negative side of the plane, or zero if the point is on the plane.
  58. /// </returns>
  59. float GetDistance(Vector3 point)
  60. {
  61. return Vector3.Dot(normal, point) - d;
  62. }
  63. /// <summary>
  64. /// Returns the side of the plane where the point is located on. No side signifies the point is on the plane.
  65. /// </summary>
  66. /// <param name="point">Point to test.</param>
  67. /// <param name="epsilon">Extra depth of the plane to help with precision issues.</param>
  68. /// <returns>Side on the plane the point is located on.</returns>
  69. public Side GetSide(Vector3 point, float epsilon = 0.0f)
  70. {
  71. float dist = GetDistance(point);
  72. if (dist > epsilon)
  73. return Side.Positive;
  74. if (dist < -epsilon)
  75. return Side.Negative;
  76. return Side.None;
  77. }
  78. }
  79. /** @} */
  80. }