Exemplo n.º 1
0
        public static XMVector RotationRollPitchYawFromVector(XMVector angles)
        {
            XMVector sign       = XMVector.FromFloat(1.0f, -1.0f, -1.0f, 1.0f);
            XMVector halfAngles = XMVector.Multiply(angles, XMGlobalConstants.OneHalf);

            XMVector sinAngles;
            XMVector cosAngles;

            halfAngles.SinCos(out sinAngles, out cosAngles);

            XMVector p0 = new XMVector(sinAngles.X, cosAngles.X, cosAngles.X, cosAngles.X);
            XMVector y0 = new XMVector(cosAngles.Y, sinAngles.Y, cosAngles.Y, cosAngles.Y);
            XMVector r0 = new XMVector(cosAngles.Z, cosAngles.Z, sinAngles.Z, cosAngles.Z);
            XMVector p1 = new XMVector(cosAngles.X, sinAngles.X, sinAngles.X, sinAngles.X);
            XMVector y1 = new XMVector(sinAngles.Y, cosAngles.Y, sinAngles.Y, sinAngles.Y);
            XMVector r1 = new XMVector(sinAngles.Z, sinAngles.Z, cosAngles.Z, sinAngles.Z);

            XMVector q1 = XMVector.Multiply(p1, sign);
            XMVector q0 = XMVector.Multiply(p0, y0);

            q1 = XMVector.Multiply(q1, y1);
            q0 = XMVector.Multiply(q0, r0);

            return(XMVector.MultiplyAdd(q1, r1, q0));
        }
Exemplo n.º 2
0
        public static XMVector RefractV(XMVector incident, XMVector normal, XMVector refractionIndex)
        {
            //// Result = RefractionIndex * Incident - Normal * (RefractionIndex * dot(Incident, Normal) +
            //// sqrt(1 - RefractionIndex * RefractionIndex * (1 - dot(Incident, Normal) * dot(Incident, Normal))))

            XMVector zero = XMVector.Zero;
            XMVector incidentDotNormal = XMVector4.Dot(incident, normal);

            // R = 1.0f - RefractionIndex * RefractionIndex * (1.0f - IDotN * IDotN)
            XMVector r = XMVector.NegativeMultiplySubtract(incidentDotNormal, incidentDotNormal, XMGlobalConstants.One);

            r = XMVector.Multiply(r, refractionIndex);
            r = XMVector.NegativeMultiplySubtract(r, refractionIndex, XMGlobalConstants.One);

            if (XMVector4.LessOrEqual(r, zero))
            {
                // Total internal reflection
                return(zero);
            }
            else
            {
                // R = RefractionIndex * IDotN + sqrt(R)
                r = r.Sqrt();
                r = XMVector.MultiplyAdd(refractionIndex, incidentDotNormal, r);

                // Result = RefractionIndex * Incident - Normal * R
                XMVector result = XMVector.Multiply(refractionIndex, incident);
                result = XMVector.NegativeMultiplySubtract(normal, r, result);

                return(result);
            }
        }
Exemplo n.º 3
0
        public static XMVector FresnelTerm(XMVector cosIncidentAngle, XMVector refractionIndex)
        {
            Debug.Assert(!XMVector4.IsInfinite(cosIncidentAngle), "Reviewed");

            //// Result = 0.5f * (g - c)^2 / (g + c)^2 * ((c * (g + c) - 1)^2 / (c * (g - c) + 1)^2 + 1) where
            //// c = CosIncidentAngle
            //// g = sqrt(c^2 + RefractionIndex^2 - 1)

            XMVector g = XMVector.MultiplyAdd(refractionIndex, refractionIndex, XMGlobalConstants.NegativeOne);

            g = XMVector.MultiplyAdd(cosIncidentAngle, cosIncidentAngle, g);
            g = g.Abs().Sqrt();

            XMVector s = XMVector.Add(g, cosIncidentAngle);
            XMVector d = XMVector.Subtract(g, cosIncidentAngle);

            XMVector v0 = XMVector.Multiply(d, d);
            XMVector v1 = XMVector.Multiply(s, s).Reciprocal();

            v0 = XMVector.Multiply(XMGlobalConstants.OneHalf, v0);
            v0 = XMVector.Multiply(v0, v1);

            XMVector v2 = XMVector.MultiplyAdd(cosIncidentAngle, s, XMGlobalConstants.NegativeOne);
            XMVector v3 = XMVector.MultiplyAdd(cosIncidentAngle, d, XMGlobalConstants.One);

            v2 = XMVector.Multiply(v2, v2);
            v3 = XMVector.Multiply(v3, v3);
            v3 = v3.Reciprocal();
            v2 = XMVector.MultiplyAdd(v2, v3, XMGlobalConstants.One);

            return(XMVector.Multiply(v0, v2).Saturate());
        }
Exemplo n.º 4
0
        public bool Intersects(BoundingSphere sh)
        {
            XMVector sphereCenter = sh.Center;
            XMVector sphereRadius = XMVector.Replicate(sh.Radius);

            XMVector boxCenter  = this.center;
            XMVector boxExtents = this.extents;

            XMVector boxMin = boxCenter - boxExtents;
            XMVector boxMax = boxCenter + boxExtents;

            //// Find the distance to the nearest point on the box.
            //// for each i in (x, y, z)
            //// if (SphereCenter(i) < BoxMin(i)) d2 += (SphereCenter(i) - BoxMin(i)) ^ 2
            //// else if (SphereCenter(i) > BoxMax(i)) d2 += (SphereCenter(i) - BoxMax(i)) ^ 2

            XMVector d = XMGlobalConstants.Zero;

            // Compute d for each dimension.
            XMVector lessThanMin    = XMVector.Less(sphereCenter, boxMin);
            XMVector greaterThanMax = XMVector.Greater(sphereCenter, boxMax);

            XMVector minDelta = sphereCenter - boxMin;
            XMVector maxDelta = sphereCenter - boxMax;

            // Choose value for each dimension based on the comparison.
            d = XMVector.Select(d, minDelta, lessThanMin);
            d = XMVector.Select(d, maxDelta, greaterThanMax);

            // Use a dot-product to square them and sum them together.
            XMVector d2 = XMVector3.Dot(d, d);

            return(XMVector3.LessOrEqual(d2, XMVector.Multiply(sphereRadius, sphereRadius)));
        }
Exemplo n.º 5
0
        public static XMVector BaryCentricV(XMVector q0, XMVector q1, XMVector q2, XMVector f, XMVector g)
        {
            Debug.Assert(f.Y == f.X && f.Z == f.X && f.W == f.X, "Reviewed");
            Debug.Assert(g.Y == g.X && g.Z == g.X && g.W == g.X, "Reviewed");

            XMVector epsilon = XMVector.FromSplatConstant(1, 16);
            XMVector s       = XMVector.Add(f, g);

            XMVector result;

            if (XMVector4.InBounds(s, epsilon))
            {
                result = q0;
            }
            else
            {
                XMVector q01 = XMQuaternion.SlerpV(q0, q1, s);
                XMVector q02 = XMQuaternion.SlerpV(q0, q2, s);
                XMVector gs  = s.Reciprocal();
                gs = XMVector.Multiply(g, gs);

                result = XMQuaternion.SlerpV(q01, q02, gs);
            }

            return(result);
        }
Exemplo n.º 6
0
        public static void ComponentsFromNormal(out XMVector parallel, out XMVector perpendicular, XMVector v, XMVector normal)
        {
            XMVector scale     = XMVector3.Dot(v, normal);
            XMVector parallelV = XMVector.Multiply(normal, scale);

            parallel      = parallelV;
            perpendicular = XMVector.Subtract(v, parallelV);
        }
Exemplo n.º 7
0
        public static XMVector NormalizeEst(XMVector p)
        {
            //// XMPlaneNormalizeEst uses a reciprocal estimate and
            //// returns QNaN on zero and infinite vectors.

            XMVector result = XMVector3.ReciprocalLengthEst(p);

            return(XMVector.Multiply(p, result));
        }
        public ContainmentType Contains(XMVector point)
        {
            XMVector v_center = this.center;
            XMVector v_radius = XMVector.Replicate(this.radius);

            XMVector distanceSquared = XMVector3.LengthSquare(point - v_center);
            XMVector radiusSquared   = XMVector.Multiply(v_radius, v_radius);

            return(XMVector3.LessOrEqual(distanceSquared, radiusSquared) ? ContainmentType.Contains : ContainmentType.Disjoint);
        }
Exemplo n.º 9
0
        public static XMVector NormalizeEst(XMVector v)
        {
            //// XMVector3NormalizeEst uses a reciprocal estimate and
            //// returns QNaN on zero and infinite vectors.

            XMVector result;

            result = XMVector3.ReciprocalLength(v);
            result = XMVector.Multiply(v, result);
            return(result);
        }
Exemplo n.º 10
0
        public static XMVector TransformNormal(XMVector v, XMMatrix m)
        {
            XMVector y = XMVector.SplatY(v);
            XMVector x = XMVector.SplatX(v);

            XMVector result = XMVector.Multiply(y, ((XMVector *)&m)[1]);

            result = XMVector.MultiplyAdd(x, ((XMVector *)&m)[0], result);

            return(result);
        }
Exemplo n.º 11
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());
        }
Exemplo n.º 12
0
        public static XMVector RgbToXyz(XMVector rgb)
        {
            XMVector scale0 = XMVector.FromFloat(0.4887180f, 0.1762044f, 0.0000000f, 0.0f);
            XMVector scale1 = XMVector.FromFloat(0.3106803f, 0.8129847f, 0.0102048f, 0.0f);
            XMVector scale2 = XMVector.FromFloat(0.2006017f, 0.0108109f, 0.9897952f, 0.0f);
            XMVector scale  = XMVector.FromFloat(1.0f / 0.17697f, 1.0f / 0.17697f, 1.0f / 0.17697f, 0.0f);

            XMMatrix m   = new XMMatrix(scale0, scale1, scale2, XMGlobalConstants.Zero);
            XMVector clr = XMVector.Multiply(XMVector3.Transform(rgb, m), scale);

            return(XMVector.Select(rgb, clr, XMGlobalConstants.Select1110));
        }
Exemplo n.º 13
0
        public static XMVector XyzToRgb(XMVector xyz)
        {
            XMVector scale0 = XMVector.FromFloat(2.3706743f, -0.5138850f, 0.0052982f, 0.0f);
            XMVector scale1 = XMVector.FromFloat(-0.9000405f, 1.4253036f, -0.0146949f, 0.0f);
            XMVector scale2 = XMVector.FromFloat(-0.4706338f, 0.0885814f, 1.0093968f, 0.0f);
            XMVector scale  = XMVector.FromFloat(0.17697f, 0.17697f, 0.17697f, 0.0f);

            XMMatrix m   = new XMMatrix(scale0, scale1, scale2, XMGlobalConstants.Zero);
            XMVector clr = XMVector3.Transform(XMVector.Multiply(xyz, scale), m);

            return(XMVector.Select(xyz, clr, XMGlobalConstants.Select1110));
        }
Exemplo n.º 14
0
        public static XMVector RotationNormal(XMVector normalAxis, float angle)
        {
            XMVector n = XMVector.Select(XMGlobalConstants.One, normalAxis, XMGlobalConstants.Select1110);

            float sinV;
            float cosV;

            XMScalar.SinCos(out sinV, out cosV, 0.5f * angle);

            XMVector scale = new XMVector(sinV, sinV, sinV, cosV);

            return(XMVector.Multiply(n, scale));
        }
Exemplo n.º 15
0
        public static XMVector SlerpV(XMVector q0, XMVector q1, XMVector t)
        {
            Debug.Assert(t.Y == t.X && t.Z == t.X && t.W == t.X, "Reviewed");

            //// Result = Q0 * sin((1.0 - t) * Omega) / sin(Omega) + Q1 * sin(t * Omega) / sin(Omega)

            XMVector oneMinusEpsilon = XMVector.FromFloat(1.0f - 0.00001f, 1.0f - 0.00001f, 1.0f - 0.00001f, 1.0f - 0.00001f);

            XMVector cosOmega = XMQuaternion.Dot(q0, q1);
            XMVector zero     = XMVector.Zero;
            XMVector control  = XMVector.Less(cosOmega, zero);
            XMVector sign     = XMVector.Select(XMGlobalConstants.One, XMGlobalConstants.NegativeOne, control);

            cosOmega = XMVector.Multiply(cosOmega, sign);
            control  = XMVector.Less(cosOmega, oneMinusEpsilon);

            XMVector sinOmega = XMVector
                                .NegativeMultiplySubtract(cosOmega, cosOmega, XMGlobalConstants.One)
                                .Sqrt();

            XMVector omega = XMVector.ATan2(sinOmega, cosOmega);

            XMVector signMask = XMVector.SignMask;
            XMVector v01      = XMVector.ShiftLeft(t, zero, 2);

            signMask = XMVector.ShiftLeft(signMask, zero, 3);
            v01      = XMVector.XorInt(v01, signMask);
            v01      = XMVector.Add(XMGlobalConstants.IdentityR0, v01);

            XMVector invSinOmega = sinOmega.Reciprocal();

            XMVector s0 = XMVector
                          .Multiply(v01, omega)
                          .Sin();

            s0 = XMVector.Multiply(s0, invSinOmega);
            s0 = XMVector.Select(v01, s0, control);

            XMVector s1 = XMVector.SplatY(s0);

            s0 = XMVector.SplatX(s0);
            s1 = XMVector.Multiply(s1, sign);

            XMVector result = XMVector.Multiply(q0, s0);

            result = XMVector.MultiplyAdd(q1, s1, result);

            return(result);
        }
Exemplo n.º 16
0
        public static XMVector Transform(XMVector p, XMMatrix m)
        {
            XMVector w = XMVector.SplatW(p);
            XMVector z = XMVector.SplatZ(p);
            XMVector y = XMVector.SplatY(p);
            XMVector x = XMVector.SplatX(p);

            XMVector result = XMVector.Multiply(w, ((XMVector *)&m)[3]);

            result = XMVector.MultiplyAdd(z, ((XMVector *)&m)[2], result);
            result = XMVector.MultiplyAdd(y, ((XMVector *)&m)[1], result);
            result = XMVector.MultiplyAdd(x, ((XMVector *)&m)[0], result);

            return(result);
        }
Exemplo n.º 17
0
        public static XMVector SquadV(XMVector q0, XMVector q1, XMVector q2, XMVector q3, XMVector t)
        {
            Debug.Assert(t.Y == t.X && t.Z == t.X && t.W == t.X, "Reviewed");

            XMVector tp  = t;
            XMVector two = XMVector.FromSplatConstant(2, 0);

            XMVector q03 = XMQuaternion.SlerpV(q0, q3, t);
            XMVector q12 = XMQuaternion.SlerpV(q1, q2, t);

            tp = XMVector.NegativeMultiplySubtract(tp, tp, tp);
            tp = XMVector.Multiply(tp, two);

            return(XMQuaternion.SlerpV(q03, q12, tp));
        }
Exemplo n.º 18
0
        public static XMVector SrgbToRgb(XMVector srgb)
        {
            XMVector cutoff    = XMVector.FromFloat(0.04045f, 0.04045f, 0.04045f, 1.0f);
            XMVector invLinear = XMVector.FromFloat(1.0f / 12.92f, 1.0f / 12.92f, 1.0f / 12.92f, 1.0f);
            XMVector scale     = XMVector.FromFloat(1.0f / 1.055f, 1.0f / 1.055f, 1.0f / 1.055f, 1.0f);
            XMVector bias      = XMVector.FromFloat(0.055f, 0.055f, 0.055f, 0.0f);
            XMVector gamma     = XMVector.FromFloat(2.4f, 2.4f, 2.4f, 1.0f);

            XMVector v  = srgb.Saturate();
            XMVector v0 = XMVector.Multiply(v, invLinear);
            XMVector v1 = XMVector.Pow(XMVector.Multiply(XMVector.Add(v, bias), scale), gamma);

            XMVector select = XMVector.Greater(v, cutoff);

            v = XMVector.Select(v0, v1, select);
            return(XMVector.Select(srgb, v, XMGlobalConstants.Select1110));
        }
Exemplo n.º 19
0
        public static XMVector RgbToSrgb(XMVector rgb)
        {
            XMVector cutoff   = XMVector.FromFloat(0.0031308f, 0.0031308f, 0.0031308f, 1.0f);
            XMVector linear   = XMVector.FromFloat(12.92f, 12.92f, 12.92f, 1.0f);
            XMVector scale    = XMVector.FromFloat(1.055f, 1.055f, 1.055f, 1.0f);
            XMVector bias     = XMVector.FromFloat(0.055f, 0.055f, 0.055f, 0.0f);
            XMVector invGamma = XMVector.FromFloat(1.0f / 2.4f, 1.0f / 2.4f, 1.0f / 2.4f, 1.0f);

            XMVector v  = rgb.Saturate();
            XMVector v0 = XMVector.Multiply(v, linear);
            XMVector v1 = XMVector.Subtract(XMVector.Multiply(scale, XMVector.Pow(v, invGamma)), bias);

            XMVector select = XMVector.Less(v, cutoff);

            v = XMVector.Select(v1, v0, select);
            return(XMVector.Select(rgb, v, XMGlobalConstants.Select1110));
        }
Exemplo n.º 20
0
        public static void SquadSetup(out XMVector a, out XMVector b, out XMVector c, XMVector q0, XMVector q1, XMVector q2, XMVector q3)
        {
            XMVector ls12 = XMQuaternion.LengthSquare(XMVector.Add(q1, q2));
            XMVector ld12 = XMQuaternion.LengthSquare(XMVector.Subtract(q1, q2));
            XMVector sq2  = q2.Negate();

            XMVector control1 = XMVector.Less(ls12, ld12);

            sq2 = XMVector.Select(q2, sq2, control1);

            XMVector ls01 = XMQuaternion.LengthSquare(XMVector.Add(q0, q1));
            XMVector ld01 = XMQuaternion.LengthSquare(XMVector.Subtract(q0, q1));
            XMVector sq0  = q0.Negate();

            XMVector ls23 = XMQuaternion.LengthSquare(XMVector.Add(sq2, q3));
            XMVector ld23 = XMQuaternion.LengthSquare(XMVector.Subtract(sq2, q3));
            XMVector sq3  = q3.Negate();

            XMVector control0 = XMVector.Less(ls01, ld01);
            XMVector control2 = XMVector.Less(ls23, ld23);

            sq0 = XMVector.Select(q0, sq0, control0);
            sq3 = XMVector.Select(q3, sq3, control2);

            XMVector invQ1 = XMQuaternion.Inverse(q1);
            XMVector invQ2 = XMQuaternion.Inverse(sq2);

            XMVector ln_q0 = XMQuaternion.Ln(XMQuaternion.Multiply(invQ1, sq0));
            XMVector ln_q2 = XMQuaternion.Ln(XMQuaternion.Multiply(invQ1, sq2));
            XMVector ln_q1 = XMQuaternion.Ln(XMQuaternion.Multiply(invQ2, q1));
            XMVector ln_q3 = XMQuaternion.Ln(XMQuaternion.Multiply(invQ2, sq3));

            XMVector negativeOneQuarter = XMVector.FromSplatConstant(-1, 2);

            XMVector expQ02 = XMVector.Multiply(XMVector.Add(ln_q0, ln_q2), negativeOneQuarter);
            XMVector expQ13 = XMVector.Multiply(XMVector.Add(ln_q1, ln_q3), negativeOneQuarter);

            expQ02 = XMQuaternion.Exp(expQ02);
            expQ13 = XMQuaternion.Exp(expQ13);

            a = XMQuaternion.Multiply(q1, expQ02);
            b = XMQuaternion.Multiply(sq2, expQ13);
            c = sq2;
        }
Exemplo n.º 21
0
        public static XMVector Ln(XMVector q)
        {
            XMVector oneMinusEpsilon = XMVector.FromFloat(1.0f - 0.00001f, 1.0f - 0.00001f, 1.0f - 0.00001f, 1.0f - 0.00001f);

            XMVector qw       = XMVector.SplatW(q);
            XMVector q0       = XMVector.Select(XMGlobalConstants.Select1110, q, XMGlobalConstants.Select1110);
            XMVector controlW = qw.InBounds(oneMinusEpsilon);

            XMVector theta    = qw.ACos();
            XMVector sinTheta = theta.Sin();

            XMVector s = XMVector.Divide(theta, sinTheta);

            XMVector result = XMVector.Multiply(q0, s);

            result = XMVector.Select(q0, result, controlW);

            return(result);
        }
Exemplo n.º 22
0
        public static void IntersectPlane(out XMVector linePoint1, out XMVector linePoint2, XMVector p1, XMVector p2)
        {
            XMVector v1       = XMVector3.Cross(p2, p1);
            XMVector lengthSq = XMVector3.LengthSquare(v1);
            XMVector v2       = XMVector3.Cross(p2, v1);
            XMVector p1W      = XMVector.SplatW(p1);
            XMVector point    = XMVector.Multiply(v2, p1W);
            XMVector v3       = XMVector3.Cross(v1, p1);
            XMVector p2W      = XMVector.SplatW(p2);

            point = XMVector.MultiplyAdd(v3, p2W, point);

            XMVector lineP1 = XMVector.Divide(point, lengthSq);
            XMVector lineP2 = XMVector.Add(lineP1, v1);

            XMVector control = XMVector.LessOrEqual(lengthSq, XMGlobalConstants.Epsilon);

            linePoint1 = XMVector.Select(lineP1, XMGlobalConstants.QNaN, control);
            linePoint2 = XMVector.Select(lineP2, XMGlobalConstants.QNaN, control);
        }
Exemplo n.º 23
0
        public static XMVector Exp(XMVector q)
        {
            XMVector theta = XMVector3.Length(q);

            XMVector sinTheta;
            XMVector cosTheta;

            theta.SinCos(out sinTheta, out cosTheta);

            XMVector s      = XMVector.Divide(sinTheta, theta);
            XMVector result = XMVector.Multiply(q, s);

            XMVector zero    = XMVector.Zero;
            XMVector control = XMVector.NearEqual(theta, zero, XMGlobalConstants.Epsilon);

            result = XMVector.Select(result, q, control);
            result = XMVector.Select(cosTheta, result, XMGlobalConstants.Select1110);

            return(result);
        }
Exemplo n.º 24
0
        public bool Intersects(BoundingSphere sh)
        {
            // Load A.
            XMVector v_centerA = this.center;
            XMVector v_radiusA = XMVector.Replicate(this.radius);

            // Load B.
            XMVector v_centerB = sh.center;
            XMVector v_radiusB = XMVector.Replicate(sh.radius);

            // Distance squared between centers.
            XMVector delta           = v_centerB - v_centerA;
            XMVector distanceSquared = XMVector3.LengthSquare(delta);

            // Sum of the radii squared.
            XMVector radiusSquared = XMVector.Add(v_radiusA, v_radiusB);

            radiusSquared = XMVector.Multiply(radiusSquared, radiusSquared);

            return(XMVector3.LessOrEqual(distanceSquared, radiusSquared));
        }
Exemplo n.º 25
0
        private static XMVector HueToClr(XMVector p, XMVector q, XMVector h)
        {
            XMVector oneSixth  = XMVector.FromFloat(1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f);
            XMVector twoThirds = XMVector.FromFloat(2.0f / 3.0f, 2.0f / 3.0f, 2.0f / 3.0f, 2.0f / 3.0f);

            XMVector t = h;

            if (XMVector3.Less(t, XMGlobalConstants.Zero))
            {
                t = XMVector.Add(t, XMGlobalConstants.One);
            }

            if (XMVector3.Greater(t, XMGlobalConstants.One))
            {
                t = XMVector.Subtract(t, XMGlobalConstants.One);
            }

            if (XMVector3.Less(t, oneSixth))
            {
                // p + (q - p) * 6 * t
                XMVector t1 = XMVector.Subtract(q, p);
                XMVector t2 = XMVector.Multiply(XMGlobalConstants.Six, t);
                return(XMVector.MultiplyAdd(t1, t2, p));
            }

            if (XMVector3.Less(t, XMGlobalConstants.OneHalf))
            {
                return(q);
            }

            if (XMVector3.Less(t, twoThirds))
            {
                // p + (q - p) * 6 * (2/3 - t)
                XMVector t1 = XMVector.Subtract(q, p);
                XMVector t2 = XMVector.Multiply(XMGlobalConstants.Six, XMVector.Subtract(twoThirds, t));
                return(XMVector.MultiplyAdd(t1, t2, p));
            }

            return(p);
        }
Exemplo n.º 26
0
        public static XMVector ClampLengthV(XMVector v, XMVector lengthMin, XMVector lengthMax)
        {
            Debug.Assert(lengthMin.Y == lengthMin.X && lengthMin.Z == lengthMin.X && lengthMin.W == lengthMin.X, "Reviewed");
            Debug.Assert(lengthMax.Y == lengthMax.X && lengthMax.Z == lengthMax.X && lengthMax.W == lengthMax.X, "Reviewed");
            Debug.Assert(XMVector4.GreaterOrEqual(lengthMin, XMGlobalConstants.Zero), "Reviewed");
            Debug.Assert(XMVector4.GreaterOrEqual(lengthMax, XMGlobalConstants.Zero), "Reviewed");
            Debug.Assert(XMVector4.GreaterOrEqual(lengthMax, lengthMin), "Reviewed");

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

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

            XMVector normal = XMVector.Multiply(v, reciprocalLength);
            XMVector length = XMVector.Multiply(lengthSq, 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);
        }
Exemplo n.º 27
0
        public ContainmentType Contains(XMVector v0, XMVector v1, XMVector v2)
        {
            if (!this.Intersects(v0, v1, v2))
            {
                return(ContainmentType.Disjoint);
            }

            XMVector v_center      = this.center;
            XMVector v_radius      = XMVector.Replicate(this.radius);
            XMVector radiusSquared = XMVector.Multiply(v_radius, v_radius);

            XMVector distanceSquared = XMVector3.LengthSquare(v0 - v_center);
            XMVector inside          = XMVector.LessOrEqual(distanceSquared, radiusSquared);

            distanceSquared = XMVector3.LengthSquare(v1 - v_center);
            inside          = XMVector.AndInt(inside, XMVector.LessOrEqual(distanceSquared, radiusSquared));

            distanceSquared = XMVector3.LengthSquare(v2 - v_center);
            inside          = XMVector.AndInt(inside, XMVector.LessOrEqual(distanceSquared, radiusSquared));

            return(XMVector3.EqualInt(inside, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects);
        }
Exemplo n.º 28
0
        public static XMVector XyzToSrgb(XMVector xyz)
        {
            XMVector scale0 = XMVector.FromFloat(3.2406f, -0.9689f, 0.0557f, 0.0f);
            XMVector scale1 = XMVector.FromFloat(-1.5372f, 1.8758f, -0.2040f, 0.0f);
            XMVector scale2 = XMVector.FromFloat(-0.4986f, 0.0415f, 1.0570f, 0.0f);
            XMVector cutoff = XMVector.FromFloat(0.0031308f, 0.0031308f, 0.0031308f, 0.0f);
            XMVector exp    = XMVector.FromFloat(1.0f / 2.4f, 1.0f / 2.4f, 1.0f / 2.4f, 1.0f);

            XMMatrix m    = new XMMatrix(scale0, scale1, scale2, XMGlobalConstants.Zero);
            XMVector lclr = XMVector3.Transform(xyz, m);
            XMVector sel  = XMVector.Greater(lclr, cutoff);

            // clr = 12.92 * lclr for lclr <= 0.0031308f
            XMVector smallC = XMVector.Multiply(lclr, XMGlobalConstants.MsrgbScale);

            // clr = (1+a)*pow(lclr, 1/2.4) - a for lclr > 0.0031308 (where a = 0.055)
            XMVector largeC = XMVector.Subtract(XMVector.Multiply(XMGlobalConstants.MsrgbA1, XMVector.Pow(lclr, exp)), XMGlobalConstants.MsrgbA);

            XMVector clr = XMVector.Select(smallC, largeC, sel);

            return(XMVector.Select(xyz, clr, XMGlobalConstants.Select1110));
        }
Exemplo n.º 29
0
        public static XMVector HslToRgb(XMVector hsl)
        {
            XMVector oneThird = XMVector.FromFloat(1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f);

            XMVector s = XMVector.SplatY(hsl);
            XMVector l = XMVector.SplatZ(hsl);

            if (XMVector3.NearEqual(s, XMGlobalConstants.Zero, XMGlobalConstants.Epsilon))
            {
                // Achromatic
                return(XMVector.Select(hsl, l, XMGlobalConstants.Select1110));
            }
            else
            {
                XMVector h = XMVector.SplatX(hsl);

                XMVector q;

                if (XMVector3.Less(l, XMGlobalConstants.OneHalf))
                {
                    q = XMVector.Multiply(l, XMVector.Add(XMGlobalConstants.One, s));
                }
                else
                {
                    q = XMVector.Subtract(XMVector.Add(l, s), XMVector.Multiply(l, s));
                }

                XMVector p = XMVector.Subtract(XMVector.Multiply(XMGlobalConstants.Two, l), q);

                XMVector r = XMColor.HueToClr(p, q, XMVector.Add(h, oneThird));
                XMVector g = XMColor.HueToClr(p, q, h);
                XMVector b = XMColor.HueToClr(p, q, XMVector.Subtract(h, oneThird));

                XMVector rg = XMVector.Select(g, r, XMGlobalConstants.Select1000);
                XMVector ba = XMVector.Select(hsl, b, XMGlobalConstants.Select1110);

                return(XMVector.Select(ba, rg, XMGlobalConstants.Select1100));
            }
        }
Exemplo n.º 30
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);
        }