Canguro.Commands.View.Selection.isCornerInsideTriangle C# (CSharp) Méthode

isCornerInsideTriangle() private méthode

Check if the ray of each corner of the selection box intersects the area Let the area can be divided in two triangles T1 and T2, and the corners of the selection window be named C1, C2, C3 and C4. The equation for the planes defined by T1 and T2 is P + rU + sV = X where P is any point in the triangle, and U and V are the vectors forming the triangle's edges The equation for each corner will be Q - tW = X where Q is any point on the ray and W is the direction vector (perpendicular to the screen) Q's are obtained from corners1 and W from cornersDir This solution involves solving the following linear system of equations: | U0 V0 W0 | | r | | U1 V1 W1 | x | s | = (Q - P) | U2 V2 W2 | | t |
private isCornerInsideTriangle ( System.Vector3 &p, System.Vector3 &u, System.Vector3 &v, System.Vector3 &w, System.Vector3 &qs ) : bool
p System.Vector3 Any point on the plane
u System.Vector3 A vector joining two vertices of a triangle
v System.Vector3 Another vector joining two vertices of a triangle (must be differecnt that u)
w System.Vector3 Viewing direction vector
qs System.Vector3 Array of points to be ckecked if inside the triangle
Résultat bool
        private bool isCornerInsideTriangle(ref Vector3 p, ref Vector3 u, ref Vector3 v, ref Vector3 w, ref Vector3[] qs)
        {
            Vector3 rst;

            float det = u.X * (w.Z * v.Y - v.Z * w.Y) -
                        u.Y * (w.Z * v.X - v.Z * w.X) +
                        u.Z * (w.Y * v.X - v.Y * w.X);

            // If plane and ray are parallel
            if (det < Utility.GeometricUtils.Epsilon && det > -Utility.GeometricUtils.Epsilon)
                return false;

            det = 1f / det;
            Matrix inv = new Matrix();
            inv.M11 = det * (w.Z * v.Y - v.Z * w.Y); inv.M21 = -det * (w.Z * v.X - v.Z * w.X); inv.M31 = det * (w.Y * v.X - v.Y * w.X);
            inv.M12 = -det * (w.Z * u.Y - u.Z * w.Y); inv.M22 = det * (w.Z * u.X - u.Z * w.X); inv.M32 = -det * (w.Y * u.X - u.Y * w.X);
            inv.M13 = det * (v.Z * u.Y - u.Z * v.Y); inv.M23 = -det * (v.Z * u.X - u.Z * v.X); inv.M33 = det * (v.Y * u.X - u.Y * v.X);
            inv.M44 = 1f;

            for (int i = 0; i < qs.Length; i++)
            {
                rst = Vector3.TransformCoordinate(qs[i] - p, inv);
                if ((rst.X > 0) && (rst.Y > 0) && ((rst.X + rst.Y) < 1))
                    return true;
            }

            return false;
        }