/// Copyright (c) 2021 Iiro Iivanainen, Harri Linna, Jere Pakkanen, Riikka Vilavaara /// /// Permission is hereby granted, free of charge, to any person obtaining /// a copy of this software and associated documentation files (the /// "Software"), to deal in the Software without restriction, including /// without limitation the rights to use, copy, modify, merge, publish, /// distribute, sublicense, and/or sell copies of the Software, and to /// permit persons to whom the Software is furnished to do so, subject to /// the following conditions: /// /// The above copyright notice and this permission notice shall be included /// in all copies or substantial portions of the Software. /// /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, /// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF /// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. /// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY /// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, /// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE /// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using System; using System.Numerics; namespace WPFOpenGL { public static class RayCast { public static bool IntersectPlane(Vector3 rayOrigin, Vector3 rayDirection, Vector3 planeCenter, Vector3 planeNormal, out Vector3 hit, out float distance, bool backculling) { rayOrigin = MathHelper.RotateX(rayOrigin, MathHelper.DegreesToRadians(-MainWindow.eulerAngle.X)); rayOrigin = MathHelper.RotateY(rayOrigin, MathHelper.DegreesToRadians(-MainWindow.eulerAngle.Y)); rayOrigin = MathHelper.RotateZ(rayOrigin, MathHelper.DegreesToRadians(-MainWindow.eulerAngle.Z)); float epsilon = 1E-6f; hit = Vector3.Zero; distance = float.MaxValue; float denom = Vector3.Dot(planeNormal, rayDirection); if (denom > epsilon || (backculling && denom < -epsilon)) { Vector3 p0r0 = planeCenter - rayOrigin; distance = Vector3.Dot(p0r0, planeNormal) / denom; hit = rayOrigin + (distance * rayDirection); return true; } return false; } public static bool IntersectQuad(Vector3 rayOrigin, Vector3 rayDirection, Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, out Vector3 hit, out float distance) { if (IntersectTriangle(rayOrigin, rayDirection, v0, v1, v2, out hit, out distance)) { return true; } if (IntersectTriangle(rayOrigin, rayDirection, v1, v2, v3, out hit, out distance)) { return true; } return false; } public static bool IntersectTriangle(Vector3 rayOrigin, Vector3 rayDirection, Vector3 v0, Vector3 v1, Vector3 v2, out Vector3 hit, out float distance) { rayOrigin = MathHelper.RotateX(rayOrigin, MathHelper.DegreesToRadians(-MainWindow.eulerAngle.X)); rayOrigin = MathHelper.RotateY(rayOrigin, MathHelper.DegreesToRadians(-MainWindow.eulerAngle.Y)); rayOrigin = MathHelper.RotateZ(rayOrigin, MathHelper.DegreesToRadians(-MainWindow.eulerAngle.Z)); Vector3 v0v1 = v1 - v0; Vector3 v0v2 = v2 - v0; Vector3 pvec = Vector3.Cross(rayDirection, v0v2); float det = Vector3.Dot(v0v1, pvec); float epsilon = 1E-6f; hit = new Vector3(); distance = float.MaxValue; //if (det < kEpsilon) return false; if (MathF.Abs(det) < epsilon) return false; float invDet = 1f / det; Vector3 tvec = rayOrigin - v0; float u = Vector3.Dot(tvec, pvec) * invDet; if (u < 0 || u > 1) return false; Vector3 qvec = Vector3.Cross(tvec, v0v1); float v = Vector3.Dot(rayDirection, qvec) * invDet; if (v < 0 || u + v > 1) return false; distance = Vector3.Dot(v0v2, qvec) * invDet; hit = rayOrigin + distance * rayDirection; return true; } } }