Frustum.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. using MatterHackers.VectorMath;
  26. using System;
  27. namespace MatterHackers.VectorMath
  28. {
  29. public enum FrustumIntersection { Inside, Outside, Intersect };
  30. public class Frustum
  31. {
  32. // Plane normals should point out
  33. public Plane[] Planes { get; set; } = new Plane[6];
  34. public Plane Left
  35. {
  36. get { return Planes[0]; }
  37. set { Planes[0] = value; }
  38. }
  39. public Frustum()
  40. {
  41. Planes = new Plane[6];
  42. }
  43. public static Frustum FrustumFromProjectionMatrix(Matrix4X4 projectionMatrix)
  44. {
  45. Frustum createdFrustum = new Frustum();
  46. // left
  47. for (int i = 4; i-- > 0;) createdFrustum.Planes[0][i] = projectionMatrix[i, 3] + projectionMatrix[i, 0];
  48. // right
  49. for (int i = 4; i-- > 0;) createdFrustum.Planes[1][i] = projectionMatrix[i,3] - projectionMatrix[i,0];
  50. // bottom
  51. for (int i = 4; i-- > 0;) createdFrustum.Planes[2][i] = projectionMatrix[i, 3] + projectionMatrix[i, 1];
  52. // top
  53. for (int i = 4; i-- > 0;) createdFrustum.Planes[3][i] = projectionMatrix[i, 3] - projectionMatrix[i, 1];
  54. // front
  55. for (int i = 4; i-- > 0;) createdFrustum.Planes[4][i] = projectionMatrix[i, 3] + projectionMatrix[i, 2];
  56. // back
  57. for (int i = 4; i-- > 0;) createdFrustum.Planes[5][i] = projectionMatrix[i, 3] - projectionMatrix[i, 2];
  58. for(int i=0; i<6; i++)
  59. {
  60. createdFrustum.Planes[i].Normalize();
  61. createdFrustum.Planes[i].DistanceFromOrigin = -createdFrustum.Planes[i].DistanceFromOrigin;
  62. }
  63. return createdFrustum;
  64. }
  65. /// <summary>
  66. /// Modify the start and end points so they fall within the view frustum.
  67. /// </summary>
  68. /// <param name="startPoint"></param>
  69. /// <param name="endPoint"></param>
  70. /// <returns>Returns true if any part of the line is in the frustum else false.</returns>
  71. public bool ClipLine(ref Vector3 startPoint, ref Vector3 endPoint)
  72. {
  73. foreach(Plane plane in Planes)
  74. {
  75. if(!plane.ClipLine(ref startPoint, ref endPoint))
  76. {
  77. // It is entirely behind the plane so
  78. return false;
  79. }
  80. }
  81. // It has been clipped to all planes and there is still some left.
  82. return true;
  83. }
  84. // The front plan is at 0 (the intersection of the 4 side planes). Plane normals should point out.
  85. public Frustum(Vector3 leftNormal, Vector3 rightNormal,
  86. Vector3 bottomNormal, Vector3 topNormal,
  87. Vector3 backNormal, double distanceToBack)
  88. {
  89. Planes = new Plane[6];
  90. Planes[0] = new Plane(leftNormal.GetNormal(), 0);
  91. Planes[1] = new Plane(rightNormal.GetNormal(), 0);
  92. Planes[2] = new Plane(bottomNormal.GetNormal(), 0);
  93. Planes[3] = new Plane(topNormal.GetNormal(), 0);
  94. Planes[4] = new Plane(backNormal.GetNormal(), distanceToBack);
  95. Planes[5] = new Plane(-backNormal.GetNormal(), 0);
  96. }
  97. // Plane normals should point out.
  98. public Frustum(Plane left, Plane right, Plane bottom, Plane top, Plane front, Plane back)
  99. {
  100. Planes = new Plane[6];
  101. Planes[0] = left;
  102. Planes[1] = right;
  103. Planes[2] = bottom;
  104. Planes[3] = top;
  105. Planes[4] = front;
  106. Planes[5] = back;
  107. }
  108. static public Frustum Transform(Frustum frustum, Matrix4X4 matrix)
  109. {
  110. Frustum transformedFrustum = new Frustum();
  111. transformedFrustum.Planes = new Plane[frustum.Planes.Length];
  112. for (int i = 0; i < frustum.Planes.Length; ++i)
  113. {
  114. transformedFrustum.Planes[i] = Plane.Transform(frustum.Planes[i], matrix);
  115. }
  116. return transformedFrustum;
  117. }
  118. public Frustum(Plane[] sixPlanes)
  119. {
  120. Planes = new Plane[6];
  121. if (sixPlanes.Length != 6)
  122. {
  123. throw new Exception("Must create with six planes");
  124. }
  125. for (int i = 0; i < 6; i++)
  126. {
  127. Planes[i] = sixPlanes[i];
  128. }
  129. }
  130. public FrustumIntersection GetIntersect(AxisAlignedBoundingBox boundingBox)
  131. {
  132. int insidePlaneCount = 0;
  133. Vector3 vmin, vmax;
  134. for (int i = 0; i < Planes.Length; ++i)
  135. {
  136. // X axis
  137. if (Planes[i].Normal.X > 0)
  138. {
  139. vmin.X = boundingBox.MinXYZ.X;
  140. vmax.X = boundingBox.MaxXYZ.X;
  141. }
  142. else
  143. {
  144. vmin.X = boundingBox.MaxXYZ.X;
  145. vmax.X = boundingBox.MinXYZ.X;
  146. }
  147. // Y axis
  148. if (Planes[i].Normal.Y > 0)
  149. {
  150. vmin.Y = boundingBox.MinXYZ.Y;
  151. vmax.Y = boundingBox.MaxXYZ.Y;
  152. }
  153. else
  154. {
  155. vmin.Y = boundingBox.MaxXYZ.Y;
  156. vmax.Y = boundingBox.MinXYZ.Y;
  157. }
  158. // Z axis
  159. if (Planes[i].Normal.Z > 0)
  160. {
  161. vmin.Z = boundingBox.MinXYZ.Z;
  162. vmax.Z = boundingBox.MaxXYZ.Z;
  163. }
  164. else
  165. {
  166. vmin.Z = boundingBox.MaxXYZ.Z;
  167. vmax.Z = boundingBox.MinXYZ.Z;
  168. }
  169. if (Planes[i].Normal.Dot(vmin) - Planes[i].DistanceFromOrigin > 0)
  170. {
  171. return FrustumIntersection.Outside;
  172. }
  173. if (Planes[i].Normal.Dot(vmax) - Planes[i].DistanceFromOrigin >= 0)
  174. {
  175. insidePlaneCount++;
  176. }
  177. }
  178. return insidePlaneCount == Planes.Length ? FrustumIntersection.Inside : FrustumIntersection.Intersect;
  179. }
  180. }
  181. }