From 8f39f12549779e776faa68ae8bf933990fbd25e5 Mon Sep 17 00:00:00 2001 From: Harald Christian Joachim Wolff Date: Sat, 6 May 2017 12:34:21 +0200 Subject: [PATCH] static class Geometry: static Geometric helper methods (e.g. intersection) --- org.niclasundharald.engine/Geometry.cs | 56 ++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 org.niclasundharald.engine/Geometry.cs diff --git a/org.niclasundharald.engine/Geometry.cs b/org.niclasundharald.engine/Geometry.cs new file mode 100644 index 0000000..2578ae0 --- /dev/null +++ b/org.niclasundharald.engine/Geometry.cs @@ -0,0 +1,56 @@ +using System; + +using OpenTK; + +namespace org.niclasundharald.engine +{ + public static class Geometry + { + + /** + * Intersect Triangle and Ray: + * + * A + i * (B-A) + j * (C-A) = P + n * V + * A-P + i * (B-A) + j * (C-A) = n * V + * i * (B-A) + j * (C-A) - n * V = P-A + * + * => + * + * -n * V + i * (B-A) + j * (C-A) = P-A + * + * + **/ + public static bool intersectTriangle(Vector3 P,Vector3 V,Vector3 A,Vector3 B,Vector3 C,out Vector3 p){ + Vector3 BA,CA,PA; + + BA = B-A; + CA = C-A; + PA = P-A; + + Console.WriteLine("Interesction:"); + Console.WriteLine("Ray: {0} + i * {1}",P,V); + Console.WriteLine("Triangle: {0}",A); + Console.WriteLine(" {0}",B); + Console.WriteLine(" {0}",C); + + + if (!Linear.Solve4x3(V,BA,CA,PA,out p)){ + return false; + } + + p.X = -p.X; + if (p.X < 0){ + return false; + } + + if ((p.Y < 0) || (p.Y > 1.0f) || (p.Z < 0) || (p.Z > 1.0f)){ + return false; + } + + p = P + (p.X * V); + + return true; + } + + } +}