Esempio n. 1
0
        public static XMVector ClampLength(XMVector v, float lengthMin, float lengthMax)
        {
            XMVector clampMax = XMVector.Replicate(lengthMax);
            XMVector clampMin = XMVector.Replicate(lengthMin);

            return(XMVector2.ClampLengthV(v, clampMin, clampMax));
        }
Esempio n. 2
0
        public static XMVector Reflect(XMVector incident, XMVector normal)
        {
            //// Result = Incident - (2 * dot(Incident, Normal)) * Normal

            XMVector result;

            result = XMVector2.Dot(incident, normal);
            result = XMVector.Add(result, result);
            result = XMVector.NegativeMultiplySubtract(result, normal, incident);
            return(result);
        }
Esempio n. 3
0
        public static XMVector NormalizeEst(XMVector v)
        {
            //// XMVector2NormalizeEst uses a reciprocal estimate and
            //// returns QNaN on zero and infinite vectors.

            XMVector result;

            result = XMVector2.ReciprocalLength(v);
            result = XMVector.Multiply(v, result);
            return(result);
        }
Esempio n. 4
0
        public static XMVector AngleBetweenVectors(XMVector v1, XMVector v2)
        {
            XMVector l1  = XMVector2.ReciprocalLength(v1);
            XMVector l2  = XMVector2.ReciprocalLength(v2);
            XMVector dot = XMVector2.Dot(v1, v2);

            l1 = XMVector.Multiply(l1, l2);

            return(XMVector.Multiply(dot, l1)
                   .Clamp(XMGlobalConstants.NegativeOne, XMGlobalConstants.One)
                   .ACos());
        }
Esempio n. 5
0
        public static XMVector Normalize(XMVector v)
        {
            XMVector result = XMVector2.Length(v);
            float    length = result.X;

            // Prevent divide by zero
            if (length > 0)
            {
                length = 1.0f / length;
            }

            result.X = v.X * length;
            result.Y = v.Y * length;
            result.Z = v.Z * length;
            result.W = v.W * length;

            return(result);
        }
Esempio n. 6
0
        public static XMVector ClampLengthV(XMVector v, XMVector lengthMin, XMVector lengthMax)
        {
            Debug.Assert(lengthMin.Y == lengthMin.X, "Reviewed");
            Debug.Assert(lengthMax.Y == lengthMax.X, "Reviewed");
            Debug.Assert(XMVector2.GreaterOrEqual(lengthMin, XMGlobalConstants.Zero), "Reviewed");
            Debug.Assert(XMVector2.GreaterOrEqual(lengthMax, XMGlobalConstants.Zero), "Reviewed");
            Debug.Assert(XMVector2.GreaterOrEqual(lengthMax, lengthMin), "Reviewed");

            XMVector lengthSq         = XMVector2.LengthSquare(v);
            XMVector zero             = XMVector.Zero;
            XMVector reciprocalLength = lengthSq.ReciprocalSqrt();

            XMVector infiniteLength = XMVector.EqualInt(lengthSq, XMGlobalConstants.Infinity);
            XMVector zeroLength     = XMVector.Equal(lengthSq, zero);

            XMVector length = XMVector.Multiply(lengthSq, reciprocalLength);
            XMVector normal = XMVector.Multiply(v, reciprocalLength);

            XMVector select = XMVector.EqualInt(infiniteLength, zeroLength);

            length = XMVector.Select(lengthSq, length, select);
            normal = XMVector.Select(lengthSq, normal, select);

            XMVector controlMax = XMVector.Greater(length, lengthMax);
            XMVector controlMin = XMVector.Less(length, lengthMin);

            XMVector clampLength = XMVector.Select(length, lengthMax, controlMax);

            clampLength = XMVector.Select(clampLength, lengthMin, controlMin);

            XMVector result = XMVector.Multiply(normal, clampLength);

            // Preserve the original vector (with no precision loss) if the length falls within the given range
            XMVector control = XMVector.EqualInt(controlMax, controlMin);

            result = XMVector.Select(result, v, control);

            return(result);
        }
Esempio n. 7
0
        public static XMVector IntersectLine(XMVector line1Point1, XMVector line1Point2, XMVector line2Point1, XMVector line2Point2)
        {
            XMVector v1 = XMVector.Subtract(line1Point2, line1Point1);
            XMVector v2 = XMVector.Subtract(line2Point2, line2Point1);
            XMVector v3 = XMVector.Subtract(line1Point1, line2Point1);

            XMVector c1 = XMVector2.Cross(v1, v2);
            XMVector c2 = XMVector2.Cross(v2, v3);

            XMVector result;
            XMVector zero = XMVector.Zero;

            if (XMVector2.NearEqual(c1, zero, XMGlobalConstants.Epsilon))
            {
                if (XMVector2.NearEqual(c2, zero, XMGlobalConstants.Epsilon))
                {
                    // Coincident
                    result = XMGlobalConstants.Infinity;
                }
                else
                {
                    // Parallel
                    result = XMGlobalConstants.QNaN;
                }
            }
            else
            {
                //// Intersection point = Line1Point1 + V1 * (C2 / C1)

                XMVector scale = c1.Reciprocal();
                scale = XMVector.Multiply(c2, scale);

                result = XMVector.MultiplyAdd(v1, scale, line1Point1);
            }

            return(result);
        }
Esempio n. 8
0
        public static XMVector LinePointDistance(XMVector linePoint1, XMVector linePoint2, XMVector point)
        {
            //// Given a vector PointVector from LinePoint1 to Point and a vector
            //// LineVector from LinePoint1 to LinePoint2, the scaled distance
            //// PointProjectionScale from LinePoint1 to the perpendicular projection
            //// of PointVector onto the line is defined as:
            ////
            ////     PointProjectionScale = dot(PointVector, LineVector) / LengthSq(LineVector)

            XMVector pointVector = XMVector.Subtract(point, linePoint1);
            XMVector lineVector  = XMVector.Subtract(linePoint2, linePoint1);

            XMVector lengthSq = XMVector2.LengthSquare(lineVector);

            XMVector pointProjectionScale = XMVector2.Dot(pointVector, lineVector);

            pointProjectionScale = XMVector.Divide(pointProjectionScale, lengthSq);

            XMVector distanceVector = XMVector.Multiply(lineVector, pointProjectionScale);

            distanceVector = XMVector.Subtract(pointVector, distanceVector);

            return(XMVector2.Length(distanceVector));
        }
Esempio n. 9
0
 public static XMVector AngleBetweenNormals(XMVector n1, XMVector n2)
 {
     return(XMVector2.Dot(n1, n2)
            .Clamp(XMGlobalConstants.NegativeOne, XMGlobalConstants.One)
            .ACos());
 }
Esempio n. 10
0
        public static XMVector Refract(XMVector incident, XMVector normal, float refractionIndex)
        {
            XMVector index = XMVector.Replicate(refractionIndex);

            return(XMVector2.RefractV(incident, normal, index));
        }
Esempio n. 11
0
 public static XMVector Length(XMVector v)
 {
     return(XMVector2.LengthSquare(v)
            .Sqrt());
 }
Esempio n. 12
0
 public static XMVector ReciprocalLength(XMVector v)
 {
     return(XMVector2.LengthSquare(v)
            .ReciprocalSqrt());
 }
Esempio n. 13
0
 public static XMVector LengthSquare(XMVector v)
 {
     return(XMVector2.Dot(v, v));
 }