static private Dot2D ( Microsoft.Xna.Framework.Vector3 &left, Microsoft.Xna.Framework.Vector3 &right ) : float | ||
left | Microsoft.Xna.Framework.Vector3 | A vector. |
right | Microsoft.Xna.Framework.Vector3 | Another vector |
return | float |
/// <summary> /// Finds the distance between a point and triangle ABC. /// </summary> /// <param name="p">A point.</param> /// <param name="a">The first vertex of the triangle.</param> /// <param name="b">The second vertex of the triangle.</param> /// <param name="c">The third vertex of the triangle.</param> /// <param name="height">The height between the point and the triangle.</param> /// <returns>A value indicating whether the point is contained within the triangle.</returns> internal static bool PointToTriangle(Vector3 p, Vector3 a, Vector3 b, Vector3 c, out float height) { Vector3 v0 = c - a; Vector3 v1 = b - a; Vector3 v2 = p - a; float dot00, dot01, dot02, dot11, dot12; Vector3Extensions.Dot2D(ref v0, ref v0, out dot00); Vector3Extensions.Dot2D(ref v0, ref v1, out dot01); Vector3Extensions.Dot2D(ref v0, ref v2, out dot02); Vector3Extensions.Dot2D(ref v1, ref v1, out dot11); Vector3Extensions.Dot2D(ref v1, ref v2, out dot12); //compute barycentric coordinates float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01); float u = (dot11 * dot02 - dot01 * dot12) * invDenom; float v = (dot00 * dot12 - dot01 * dot02) * invDenom; const float EPS = 1E-4f; //if point lies inside triangle, return interpolated y-coordinate if (u >= -EPS && v >= -EPS && (u + v) <= 1 + EPS) { height = a.Y + v0.Y * u + v1.Y * v; return(true); } height = float.MaxValue; return(false); }
internal static void ProjectPoly(Vector3 axis, Vector3[] poly, int npoly, out float rmin, out float rmax) { Vector3Extensions.Dot2D(ref axis, ref poly[0], out rmin); Vector3Extensions.Dot2D(ref axis, ref poly[0], out rmax); for (int i = 1; i < npoly; i++) { float d; Vector3Extensions.Dot2D(ref axis, ref poly[i], out d); rmin = Math.Min(rmin, d); rmax = Math.Max(rmax, d); } }