PlaneFloat.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright (c) 2014, Lars Brubaker
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  15. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  18. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. The views and conclusions contained in the software and documentation are those
  22. of the authors and should not be interpreted as representing official policies,
  23. either expressed or implied, of the FreeBSD Project.
  24. */
  25. namespace MatterHackers.VectorMath
  26. {
  27. public class PlaneFloat
  28. {
  29. public float DistanceFromOrigin { get; private set; }
  30. public Vector3Float Normal { get; private set; }
  31. private const float TreatAsZero = .000001f;
  32. public PlaneFloat(Vector3Float planeNormal, float distanceFromOrigin)
  33. {
  34. this.Normal = planeNormal.GetNormal();
  35. this.DistanceFromOrigin = distanceFromOrigin;
  36. }
  37. public PlaneFloat(Vector3Float point0, Vector3Float point1, Vector3Float point2)
  38. {
  39. this.Normal = (point1 - point0).Cross((point2 - point0)).GetNormal();
  40. this.DistanceFromOrigin = Normal.Dot(point0);
  41. }
  42. public float GetDistanceFromPlane(Vector3Float positionToCheck)
  43. {
  44. float distanceToPointFromOrigin = positionToCheck.Dot(Normal);
  45. return distanceToPointFromOrigin - DistanceFromOrigin;
  46. }
  47. public float GetDistanceToIntersection(Ray ray, out bool inFront)
  48. {
  49. inFront = false;
  50. float normalDotRayDirection = Normal.Dot(new Vector3Float(ray.directionNormal));
  51. if (normalDotRayDirection < TreatAsZero && normalDotRayDirection > -TreatAsZero) // the ray is parallel to the plane
  52. {
  53. return float.PositiveInfinity;
  54. }
  55. if (normalDotRayDirection < 0)
  56. {
  57. inFront = true;
  58. }
  59. return (DistanceFromOrigin - Normal.Dot(new Vector3Float(ray.origin))) / normalDotRayDirection;
  60. }
  61. public float GetDistanceToIntersection(Vector3Float pointOnLine, Vector3Float lineDirection)
  62. {
  63. float normalDotRayDirection = Normal.Dot(lineDirection);
  64. if (normalDotRayDirection < TreatAsZero && normalDotRayDirection > -TreatAsZero) // the ray is parallel to the plane
  65. {
  66. return float.PositiveInfinity;
  67. }
  68. float planeNormalDotPointOnLine = Normal.Dot(pointOnLine);
  69. return (DistanceFromOrigin - planeNormalDotPointOnLine) / normalDotRayDirection;
  70. }
  71. public bool RayHitPlane(Ray ray, out float distanceToHit, out bool hitFrontOfPlane)
  72. {
  73. distanceToHit = float.PositiveInfinity;
  74. hitFrontOfPlane = false;
  75. float normalDotRayDirection = Normal.Dot(new Vector3Float(ray.directionNormal));
  76. if (normalDotRayDirection < TreatAsZero && normalDotRayDirection > -TreatAsZero) // the ray is parallel to the plane
  77. {
  78. return false;
  79. }
  80. if (normalDotRayDirection < 0)
  81. {
  82. hitFrontOfPlane = true;
  83. }
  84. float distanceToRayOriginFromOrigin = Normal.Dot(new Vector3Float(ray.origin));
  85. float distanceToPlaneFromRayOrigin = DistanceFromOrigin - distanceToRayOriginFromOrigin;
  86. bool originInFrontOfPlane = distanceToPlaneFromRayOrigin < 0;
  87. bool originAndHitAreOnSameSide = originInFrontOfPlane == hitFrontOfPlane;
  88. if (!originAndHitAreOnSameSide)
  89. {
  90. return false;
  91. }
  92. distanceToHit = distanceToPlaneFromRayOrigin / normalDotRayDirection;
  93. return true;
  94. }
  95. }
  96. }