public static void FastIntersectOrientedBoxPlane( XMVector center, XMVector extents, XMVector axis0, XMVector axis1, XMVector axis2, XMVector plane, out XMVector outside, out XMVector inside) { // Compute the distance to the center of the box. XMVector dist = XMVector4.Dot(center, plane); // Project the axes of the box onto the normal of the plane. Half the // length of the projection (sometime called the "radius") is equal to // h(u) * abs(n dot b(u))) + h(v) * abs(n dot b(v)) + h(w) * abs(n dot b(w)) // where h(i) are extents of the box, n is the plane normal, and b(i) are the // axes of the box. XMVector radius = XMVector3.Dot(plane, axis0); radius.Y = XMVector3.Dot(plane, axis1).Y; radius.Z = XMVector3.Dot(plane, axis2).Z; radius = XMVector3.Dot(extents, radius.Abs()); // Outside the plane? outside = XMVector.Greater(dist, radius); // Fully inside the plane? inside = XMVector.Less(dist, -radius); }
public static bool CalculateEigenVectorsFromCovarianceMatrix( float cxx, float cyy, float czz, float cxy, float cxz, float cyz, out XMVector pV1, out XMVector pV2, out XMVector pV3) { // Calculate the eigenvalues by solving a cubic equation. float e = -(cxx + cyy + czz); float f = (cxx * cyy) + (cyy * czz) + (czz * cxx) - (cxy * cxy) - (cxz * cxz) - (cyz * cyz); float g = (cxy * cxy * czz) + (cxz * cxz * cyy) + (cyz * cyz * cxx) - (cxy * cyz * cxz * 2.0f) - (cxx * cyy * czz); float ev1, ev2, ev3; if (!Internal.SolveCubic(e, f, g, out ev1, out ev2, out ev3)) { // set them to arbitrary orthonormal basis set pV1 = XMGlobalConstants.IdentityR0; pV2 = XMGlobalConstants.IdentityR1; pV3 = XMGlobalConstants.IdentityR2; return(false); } return(Internal.CalculateEigenVectors(cxx, cxy, cxz, cyy, cyz, czz, ev1, ev2, ev3, out pV1, out pV2, out pV3)); }
public static bool XMVector3AllTrue(XMVector v) { // Duplicate the fourth element from the first element. XMVector c = new XMVector(v.X, v.Y, v.Z, v.X); return(XMVector4.EqualIntR(c, XMVector.TrueInt).IsAllTrue); }
public static bool NearEqual(XMVector p1, XMVector p2, XMVector epsilon) { XMVector np1 = XMPlane.Normalize(p1); XMVector np2 = XMPlane.Normalize(p2); return XMVector4.NearEqual(np1, np2, epsilon); }
public static XMVector XMPlaneTransform(XMVector plane, XMVector rotation, XMVector translation) { XMVector v_normal = XMVector3.Rotate(plane, rotation); XMVector vD = XMVector.SplatW(plane) - XMVector3.Dot(v_normal, translation); return(new XMVector(v_normal.X, v_normal.Y, v_normal.Z, vD.W)); }
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)); }
private void LoadCam(object sender, RoutedEventArgs e) { var context = this.DataContext as WorkspaceVM; if (context != null) { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == true) { var lines = File.ReadLines(openFileDialog.FileName); var camPos = lines.ElementAt(0).Split(' '); var scrCen = lines.ElementAt(1).Split(' '); var angle = lines.ElementAt(2); XMVector cameraPosition = new XMVector(); XMVector screenCenter = new XMVector(); int viewAngle = 90; if (camPos.Length >= 3) { cameraPosition = new XMVector(float.Parse(camPos[0], CultureInfo.InvariantCulture), float.Parse(camPos[1], CultureInfo.InvariantCulture), float.Parse(camPos[2], CultureInfo.InvariantCulture), 1); } if (scrCen.Length >= 3) { screenCenter = new XMVector(float.Parse(scrCen[0], CultureInfo.InvariantCulture), float.Parse(scrCen[1], CultureInfo.InvariantCulture), float.Parse(scrCen[2], CultureInfo.InvariantCulture), 1); } int.TryParse(angle, out viewAngle); context.LoadCamera(cameraPosition, screenCenter, viewAngle); } } }
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))); }
public PlaneIntersectionType Intersects(XMVector plane) { Debug.Assert(Internal.XMPlaneIsUnit(plane), "Reviewed"); // Load the box. XMVector v_center = this.center; XMVector v_extents = this.extents; // Set w of the center to one so we can dot4 with a plane. v_center.W = 1.0f; XMVector outside, inside; Internal.FastIntersectAxisAlignedBoxPlane(v_center, v_extents, plane, out outside, out inside); // If the box is outside any plane it is outside. if (XMVector4.EqualInt(outside, XMVector.TrueInt)) { return(PlaneIntersectionType.Front); } // If the box is inside all planes it is inside. if (XMVector4.EqualInt(inside, XMVector.TrueInt)) { return(PlaneIntersectionType.Back); } // The box is not inside all planes or outside a plane it intersects. return(PlaneIntersectionType.Intersecting); }
public ContainmentType Contains(BoundingBox box) { XMVector centerA = this.center; XMVector extentsA = this.extents; XMVector centerB = box.center; XMVector extentsB = box.extents; XMVector minA = centerA - extentsA; XMVector maxA = centerA + extentsA; XMVector minB = centerB - extentsB; XMVector maxB = centerB + extentsB; // for each i in (x, y, z) if a_min(i) > b_max(i) or b_min(i) > a_max(i) then return false XMVector disjoint = XMVector.OrInt(XMVector.Greater(minA, maxB), XMVector.Greater(minB, maxA)); if (Internal.XMVector3AnyTrue(disjoint)) { return(ContainmentType.Disjoint); } // for each i in (x, y, z) if a_min(i) <= b_min(i) and b_max(i) <= a_max(i) then A contains B XMVector inside = XMVector.AndInt(XMVector.LessOrEqual(minA, minB), XMVector.LessOrEqual(maxB, maxA)); return(Internal.XMVector3AllTrue(inside) ? ContainmentType.Contains : ContainmentType.Intersects); }
public ContainmentType Contains(BoundingOrientedBox box) { if (!box.Intersects(this)) { return(ContainmentType.Disjoint); } XMVector boxCenter = this.center; XMVector boxExtents = this.extents; // Subtract off the AABB center to remove a subtract below XMVector o_center = box.Center - boxCenter; XMVector o_extents = box.Extents; XMVector o_orientation = box.Orientation; Debug.Assert(Internal.XMQuaternionIsUnit(o_orientation), "Reviewed"); XMVector inside = XMVector.TrueInt; for (int i = 0; i < BoundingOrientedBox.CornerCount; i++) { XMVector c = XMVector3.Rotate(o_extents * CollisionGlobalConstants.BoxOffsets[i], o_orientation) + o_center; XMVector d = c.Abs(); inside = XMVector.AndInt(inside, XMVector.LessOrEqual(d, boxExtents)); } return(XMVector3.EqualInt(inside, XMVector.TrueInt) ? ContainmentType.Contains : ContainmentType.Intersects); }
public ContainmentType Contains(XMVector point) { XMVector boxCenter = this.center; XMVector boxExtents = this.extents; return(XMVector3.InBounds(point - boxCenter, boxExtents) ? ContainmentType.Contains : ContainmentType.Disjoint); }
public BoundingBox Transform(float scale, XMVector rotation, XMVector translation) { Debug.Assert(Internal.XMQuaternionIsUnit(rotation), "Reviewed"); // Load center and extents. XMVector boxCenter = this.center; XMVector boxExtents = this.extents; XMVector vectorScale = XMVector.Replicate(scale); // Compute and transform the corners and find new min/max bounds. XMVector corner = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[0], boxCenter); corner = XMVector3.Rotate(corner * vectorScale, rotation) + translation; XMVector min, max; min = max = corner; for (int i = 1; i < BoundingBox.CornerCount; i++) { corner = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[i], boxCenter); corner = XMVector3.Rotate(corner * vectorScale, rotation) + translation; min = XMVector.Min(min, corner); max = XMVector.Max(max, corner); } // Store center and extents. return(new BoundingBox((min + max) * 0.5f, (max - min) * 0.5f)); }
public BoundingBox Transform(XMMatrix m) { // Load center and extents. XMVector boxCenter = this.center; XMVector boxExtents = this.extents; // Compute and transform the corners and find new min/max bounds. XMVector corner = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[0], boxCenter); corner = XMVector3.Transform(corner, m); XMVector min, max; min = max = corner; for (int i = 1; i < BoundingBox.CornerCount; i++) { corner = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[i], boxCenter); corner = XMVector3.Transform(corner, m); min = XMVector.Min(min, corner); max = XMVector.Max(max, corner); } // Store center and extents. return(new BoundingBox((min + max) * 0.5f, (max - min) * 0.5f)); }
internal void TransformFrameAbsolute(SdkMeshFile file, double time) { int tick = file.GetAnimationKeyFromTime(time); if (this.AnimationFrameIndex != -1) { SdkMeshAnimationFrame animationFrame = file.AnimationFrames[this.AnimationFrameIndex]; SdkMeshAnimationKey animationKey = animationFrame.AnimationKeys[tick]; SdkMeshAnimationKey animationKeyOrig = animationFrame.AnimationKeys[0]; XMMatrix mTrans1 = XMMatrix.Translation(-animationKeyOrig.Translation.X, -animationKeyOrig.Translation.Y, -animationKeyOrig.Translation.Z); XMMatrix mTrans2 = XMMatrix.Translation(animationKey.Translation.X, animationKey.Translation.Y, animationKey.Translation.Z); XMVector quat1 = animationKeyOrig.Orientation; quat1 = XMQuaternion.Inverse(quat1); XMMatrix mRot1 = XMMatrix.RotationQuaternion(quat1); XMMatrix mInvTo = mTrans1 * mRot1; XMVector quat2 = animationKey.Orientation; XMMatrix mRot2 = XMMatrix.RotationQuaternion(quat2); XMMatrix mFrom = mRot2 * mTrans2; XMMatrix mOutput = mInvTo * mFrom; this.TransformedFrameMatrix = mOutput; } }
public static bool Intersects(XMVector origin, XMVector direction, XMVector v0, XMVector v1, XMVector v2, out float distance) { float uCoordinate; float vCoordinate; return(TriangleTest.Intersects(origin, direction, v0, v1, v2, out uCoordinate, out vCoordinate, out distance)); }
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); }
public static PlaneIntersectionType Intersects(XMVector v0, XMVector v1, XMVector v2, XMVector plane) { XMVector one = XMGlobalConstants.One; Debug.Assert(Internal.XMPlaneIsUnit(plane), "Reviewed"); // Set w of the points to one so we can dot4 with a plane. XMVector tV0 = new XMVector(v0.X, v0.Y, v0.Z, one.W); XMVector tV1 = new XMVector(v1.X, v1.Y, v1.Z, one.W); XMVector tV2 = new XMVector(v2.X, v2.Y, v2.Z, one.W); XMVector outside, inside; Internal.FastIntersectTrianglePlane(tV0, tV1, tV2, plane, out outside, out inside); // If the triangle is outside any plane it is outside. if (XMVector4.EqualInt(outside, XMVector.TrueInt)) { return(PlaneIntersectionType.Front); } // If the triangle is inside all planes it is inside. if (XMVector4.EqualInt(inside, XMVector.TrueInt)) { return(PlaneIntersectionType.Back); } // The triangle is not inside all planes or outside a plane it intersects. return(PlaneIntersectionType.Intersecting); }
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()); }
public void evaluate(XMVector p) { if (p.X < MinX.X) { MinX = new XMVector(p); } if (p.Y < MinY.Y) { MinY = new XMVector(p); } if (p.Z < MinZ.Z) { MinZ = new XMVector(p); } if (p.X > MaxX.X) { MaxX = new XMVector(p); } if (p.Y > MaxY.Y) { MaxY = new XMVector(p); } if (p.Z > MaxZ.Z) { MaxZ = new XMVector(p); } }
public static bool XMVector3AllTrue(XMVector v) { // Duplicate the fourth element from the first element. XMVector c = new XMVector(v.X, v.Y, v.Z, v.X); return XMVector4.EqualIntR(c, XMVector.TrueInt).IsAllTrue; }
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(); }
public static bool EqualInt(XMVector v1, XMVector v2) { return ((uint*)&v1)[0] == ((uint*)&v2)[0] && ((uint*)&v1)[1] == ((uint*)&v2)[1] && ((uint*)&v1)[2] == ((uint*)&v2)[2] && ((uint*)&v1)[3] == ((uint*)&v2)[3]; }
public static XMVector ClampLength(XMVector v, float lengthMin, float lengthMax) { XMVector clampMax = XMVector.Replicate(lengthMax); XMVector clampMin = XMVector.Replicate(lengthMin); return(XMVector4.ClampLengthV(v, clampMin, clampMax)); }
public static bool EqualInt(XMVector v1, XMVector v2) { return(((uint *)&v1)[0] == ((uint *)&v2)[0] && ((uint *)&v1)[1] == ((uint *)&v2)[1] && ((uint *)&v1)[2] == ((uint *)&v2)[2] && ((uint *)&v1)[3] == ((uint *)&v2)[3]); }
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); } }
public static bool IsNaN(XMVector v) { return(XMVector.IsNaN(v.X) || XMVector.IsNaN(v.Y) || XMVector.IsNaN(v.Z) || XMVector.IsNaN(v.W)); }
public static bool IsInfinite(XMVector v) { return(XMVector.IsInfinite(v.X) || XMVector.IsInfinite(v.Y) || XMVector.IsInfinite(v.Z) || XMVector.IsInfinite(v.W)); }
public static bool NotEqualInt(XMVector v1, XMVector v2) { return(((uint *)&v1)[0] != ((uint *)&v2)[0] || ((uint *)&v1)[1] != ((uint *)&v2)[1] || ((uint *)&v1)[2] != ((uint *)&v2)[2] || ((uint *)&v1)[3] != ((uint *)&v2)[3]); }
public static bool InBounds(XMVector v, XMVector bounds) { return(v.X <= bounds.X && v.X >= -bounds.X && v.Y <= bounds.Y && v.Y >= -bounds.Y && v.Z <= bounds.Z && v.Z >= -bounds.Z && v.W <= bounds.W && v.W >= -bounds.W); }
public static bool NearEqual(XMVector p1, XMVector p2, XMVector epsilon) { XMVector np1 = XMPlane.Normalize(p1); XMVector np2 = XMPlane.Normalize(p2); return(XMVector4.NearEqual(np1, np2, epsilon)); }
public static BoundingBox CreateFromPoints(XMFloat3[] points) { if (points == null) { throw new ArgumentNullException("points"); } if (points.Length == 0) { throw new ArgumentOutOfRangeException("points"); } // Find the minimum and maximum x, y, and z XMVector v_min, v_max; v_min = v_max = points[0]; for (int i = 1; i < points.Length; i++) { XMVector point = points[i]; v_min = XMVector.Min(v_min, point); v_max = XMVector.Max(v_max, point); } // Store center and extents. return(new BoundingBox((v_min + v_max) * 0.5f, (v_max - v_min) * 0.5f)); }
public static XMVector AngleBetweenNormals(XMVector n1, XMVector n2) { return(XMVector4 .Dot(n1, n2) .Clamp(XMGlobalConstants.NegativeOne, XMGlobalConstants.One) .ACos()); }
public void Init() { { XMFloat3 vecEye = new XMFloat3(100.0f, 5.0f, 5.0f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, 0.0f); XMFloat3 vecUp = new XMFloat3(0.0f, 1.0f, 0.0f); this.ViewerCameraView = XMMatrix.LookAtLH(vecEye, vecAt, vecUp); this.ViewerCameraProjection = XMMatrix.PerspectiveFovLH(XMMath.PIDivFour, 1.0f, 0.05f, 1.0f); this.ViewerCameraNearClip = 0.05f; this.ViewerCameraFarClip = 1.0f; } { XMFloat3 vecEye = new XMFloat3(-320.0f, 300.0f, -220.3f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, 0.0f); XMFloat3 vecUp = new XMFloat3(0.0f, 1.0f, 0.0f); this.LightCameraWorld = XMMatrix.Identity; this.LightCameraView = XMMatrix.LookAtLH(vecEye, vecAt, vecUp); this.LightCameraProjection = XMMatrix.PerspectiveFovLH(XMMath.PIDivFour, 1.0f, 0.1f, 1000.0f); this.LightCameraEyePoint = vecEye; this.LightCameraLookAtPoint = vecAt; } this.ActiveCameraView = this.ViewerCameraView; this.ActiveCameraProjection = this.ViewerCameraProjection; this.MeshLength = 1.0f; }
public static XMComparisonRecord EqualR(XMVector v1, XMVector v2) { uint cr = 0; if (v1.X == v2.X && v1.Y == v2.Y) { cr = XMComparisonRecord.MaskTrue; } else if (v1.X != v2.X && v1.Y != v2.Y) { cr = XMComparisonRecord.MaskFalse; } return new XMComparisonRecord(cr); }
public static XMComparisonRecord EqualIntR(XMVector v1, XMVector v2) { uint cr = 0; if (((uint*)&v1)[0] == ((uint*)&v2)[0] && ((uint*)&v1)[1] == ((uint*)&v2)[1]) { cr = XMComparisonRecord.MaskTrue; } else if (((uint*)&v1)[0] != ((uint*)&v2)[0] && ((uint*)&v1)[1] != ((uint*)&v2)[1]) { cr = XMComparisonRecord.MaskFalse; } return new XMComparisonRecord(cr); }
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; }
public static XMVector TransformCoord(XMVector v, XMMatrix m) { XMVector y = XMVector.SplatY(v); XMVector x = XMVector.SplatX(v); XMVector result = XMVector.MultiplyAdd(y, ((XMVector*)&m)[1], ((XMVector*)&m)[3]); result = XMVector.MultiplyAdd(x, ((XMVector*)&m)[0], result); XMVector w = XMVector.SplatW(result); return XMVector.Divide(result, w); }
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; }
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); }
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(); }
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; }
public static XMVector Length(XMVector v) { return XMVector2.LengthSquare(v) .Sqrt(); }
public static XMVector Refract(XMVector incident, XMVector normal, float refractionIndex) { XMVector index = XMVector.Replicate(refractionIndex); return XMVector2.RefractV(incident, normal, index); }
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; }
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; }
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); }
public static bool NearEqual(XMVector v1, XMVector v2, XMVector epsilon) { float dx = Math.Abs(v1.X - v2.X); float dy = Math.Abs(v1.Y - v2.Y); return dx <= epsilon.X && dy <= epsilon.Y; }
public static bool InBounds(XMVector v, XMVector bounds) { return v.X <= bounds.X && v.X >= -bounds.X && v.Y <= bounds.Y && v.Y >= -bounds.Y; }
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; }
public static XMVector Cross(XMVector v1, XMVector v2) { //// [ V1.x*V2.y - V1.y*V2.x, V1.x*V2.y - V1.y*V2.x ] float cross = (v1.X * v2.Y) - (v1.Y - v2.X); return XMVector.Replicate(cross); }
public static XMVector LengthSquare(XMVector v) { return XMVector2.Dot(v, v); }
public static bool IsInfinite(XMVector v) { return XMVector.IsInfinite(v.X) || XMVector.IsInfinite(v.Y); }
public static XMVector Dot(XMVector v1, XMVector v2) { return XMVector.Replicate((v1.X * v2.X) + (v1.Y * v2.Y)); }
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)))) float incidentDotNormal = (incident.X * normal.X) + (incident.Y * normal.Y); // R = 1.0f - RefractionIndex * RefractionIndex * (1.0f - IDotN * IDotN) float rY = 1.0f - (incidentDotNormal * incidentDotNormal); float rX = 1.0f - (rY * refractionIndex.X * refractionIndex.X); rY = 1.0f - (rY * refractionIndex.Y * refractionIndex.Y); if (rX >= 0.0f) { rX = (refractionIndex.X * refractionIndex.X) - (normal.X * ((refractionIndex.X * incidentDotNormal) + (float)Math.Sqrt(rX))); } else { rX = 0.0f; } if (rY >= 0.0f) { rY = (refractionIndex.Y * incident.Y) - (normal.Y * ((refractionIndex.Y * incidentDotNormal) + (float)Math.Sqrt(rY))); } else { rY = 0.0f; } return new XMVector(rX, rY, 0.0f, 0.0f); }
public static bool IsNaN(XMVector v) { return XMVector.IsNaN(v.X) || XMVector.IsNaN(v.Y); }
public static XMVector Orthogonal(XMVector v) { return new XMVector(-v.Y, v.X, 0.0f, 0.0f); }
public static XMVector AngleBetweenNormals(XMVector n1, XMVector n2) { return XMVector2.Dot(n1, n2) .Clamp(XMGlobalConstants.NegativeOne, XMGlobalConstants.One) .ACos(); }
public static XMVector ReciprocalLength(XMVector v) { return XMVector2.LengthSquare(v) .ReciprocalSqrt(); }
public static bool Equal(XMVector v1, XMVector v2) { return v1.X == v2.X && v1.Y == v2.Y; }