diff --git a/ANX.Framework.TestCenter/Strukturen/PlaneTest.cs b/ANX.Framework.TestCenter/Strukturen/PlaneTest.cs index a5bbea14..bd97f24d 100644 --- a/ANX.Framework.TestCenter/Strukturen/PlaneTest.cs +++ b/ANX.Framework.TestCenter/Strukturen/PlaneTest.cs @@ -17,6 +17,12 @@ using ANXVector3 = ANX.Framework.Vector3; using XNAVector4 = Microsoft.Xna.Framework.Vector4; using ANXVector4 = ANX.Framework.Vector4; +using XNAMatrix = Microsoft.Xna.Framework.Matrix; +using ANXMatrix = ANX.Framework.Matrix; + +using XNAQuaternion = Microsoft.Xna.Framework.Quaternion; +using ANXQuaternion = ANX.Framework.Quaternion; + using XNABoundingBox = Microsoft.Xna.Framework.BoundingBox; using ANXBoundingBox = ANX.Framework.BoundingBox; @@ -34,6 +40,12 @@ namespace ANX.Framework.TestCenter.Strukturen [TestFixture] class PlaneTest { + static object[] eightfloats = + { + new object[] { 1, 2, 3, 4, 5, 6, 7, 8 }, + new object[] { 8, 7, 6, 5, 4, 3, 2, 1 } + }; + [Test] public void IntersectsSphere() { @@ -44,5 +56,251 @@ namespace ANX.Framework.TestCenter.Strukturen Assert.AreEqual(xna.Intersects(xSphere).ToString(), anx.Intersects(aSphere).ToString()); } + + #region Dot + [Test, TestCaseSource("eightfloats")] + public static void DotVector4( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + XNAVector4 xnaVector = new XNAVector4(x1, y1, z1, w1); + float xnaResult = xnaPlane.Dot(xnaVector); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + ANXVector4 anxVector = new ANXVector4(x1, y1, z1, w1); + float anxResult = anxPlane.Dot(anxVector); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "DotVector4"); + } + + [Test, TestCaseSource("eightfloats")] + public static void DotVector4Ref( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + XNAVector4 xnaVector = new XNAVector4(x1, y1, z1, w1); + float xnaResult; + xnaPlane.Dot(ref xnaVector, out xnaResult); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + ANXVector4 anxVector = new ANXVector4(x1, y1, z1, w1); + float anxResult; + anxPlane.Dot(ref anxVector, out anxResult); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "DotVector4Ref"); + } + + [Test, TestCaseSource("eightfloats")] + public static void DotCoordinateVector3( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + XNAVector3 xnaVector = new XNAVector3(x1, y1, z1); + float xnaResult = xnaPlane.DotCoordinate(xnaVector); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + ANXVector3 anxVector = new ANXVector3(x1, y1, z1); + float anxResult = anxPlane.DotCoordinate(anxVector); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "DotCoordinateVector3"); + } + + [Test, TestCaseSource("eightfloats")] + public static void DotCoordinateVector3Ref( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + XNAVector3 xnaVector = new XNAVector3(x1, y1, z1); + float xnaResult; + xnaPlane.DotCoordinate(ref xnaVector, out xnaResult); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + ANXVector3 anxVector = new ANXVector3(x1, y1, z1); + float anxResult; + anxPlane.DotCoordinate(ref anxVector, out anxResult); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "DotCoordinateVector3Ref"); + } + + + [Test, TestCaseSource("eightfloats")] + public static void DotNormalVector3( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + XNAVector3 xnaVector = new XNAVector3(x1, y1, z1); + float xnaResult = xnaPlane.DotNormal(xnaVector); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + ANXVector3 anxVector = new ANXVector3(x1, y1, z1); + float anxResult = anxPlane.DotNormal(anxVector); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "DotNormalVector3"); + } + + [Test, TestCaseSource("eightfloats")] + public static void DotNormalVector3Ref( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + XNAVector3 xnaVector = new XNAVector3(x1, y1, z1); + float xnaResult; + xnaPlane.DotNormal(ref xnaVector, out xnaResult); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + ANXVector3 anxVector = new ANXVector3(x1, y1, z1); + float anxResult; + anxPlane.DotNormal(ref anxVector, out anxResult); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "DotNormalVector3Ref"); + } + #endregion + + [Test, TestCaseSource("eightfloats")] + public static void GetHashCode( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + int xnaResult = xnaPlane.GetHashCode(); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + int anxResult = anxPlane.GetHashCode(); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "GetHashCode"); + } + + [Test, TestCaseSource("eightfloats")] + public static void ToString( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + + AssertHelper.ConvertEquals(xnaPlane.ToString(), anxPlane.ToString(), "ToString"); + } + + #region Normalize + [Test, TestCaseSource("eightfloats")] + public static void Normalize( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + xnaPlane.Normalize(); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + anxPlane.Normalize(); + + AssertHelper.ConvertEquals(xnaPlane, anxPlane, "Normalize"); + } + + [Test, TestCaseSource("eightfloats")] + public static void NormalizePlane( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + xnaPlane = XNAPlane.Normalize(xnaPlane); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + anxPlane = ANXPlane.Normalize(anxPlane); + + AssertHelper.ConvertEquals(xnaPlane, anxPlane, "NormalizePlane"); + } + + [Test, TestCaseSource("eightfloats")] + public static void NormalizePlaneRef( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xna = new XNAPlane(x1, y1, z1, w1); + XNAPlane xnaPlane; + XNAPlane.Normalize(ref xna, out xnaPlane); + + ANXPlane anx = new ANXPlane(x1, y1, z1, w1); + ANXPlane anxPlane; + ANXPlane.Normalize(ref anx, out anxPlane); + + AssertHelper.ConvertEquals(xnaPlane, anxPlane, "NormalizePlaneRef"); + } + #endregion + + #region Transform + [Test, TestCaseSource("eightfloats")] + public static void TransformMatrix( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + XNAMatrix xnaMatrix = new XNAMatrix(x1, y1, z1, w1, x2, y2, z2, w2, x1, y1, z1, w1, x2, y2, z2, w2); + XNAPlane xnaResult = XNAPlane.Transform(xnaPlane, xnaMatrix); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + ANXMatrix anxMatrix = new ANXMatrix(x1, y1, z1, w1, x2, y2, z2, w2, x1, y1, z1, w1, x2, y2, z2, w2); + ANXPlane anxResult = ANXPlane.Transform(anxPlane, anxMatrix); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "TransformMatrix"); + } + + [Test, TestCaseSource("eightfloats")] + public static void TransformMatrixRef( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + XNAMatrix xnaMatrix = new XNAMatrix(x1, y1, z1, w1, x2, y2, z2, w2, x1, y1, z1, w1, x2, y2, z2, w2); + XNAPlane xnaResult; + XNAPlane.Transform(ref xnaPlane, ref xnaMatrix, out xnaResult); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + ANXMatrix anxMatrix = new ANXMatrix(x1, y1, z1, w1, x2, y2, z2, w2, x1, y1, z1, w1, x2, y2, z2, w2); + ANXPlane anxResult; + ANXPlane.Transform(ref anxPlane, ref anxMatrix, out anxResult); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "TransformMatrixRef"); + } + + [Test, TestCaseSource("eightfloats")] + public static void TransformQuaternion( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + XNAQuaternion xnaQuat = new XNAQuaternion(x2, y2, z2, w2); + XNAPlane xnaResult = XNAPlane.Transform(xnaPlane, xnaQuat); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + ANXQuaternion anxQuat = new ANXQuaternion(x2, y2, z2, w2); + ANXPlane anxResult = ANXPlane.Transform(anxPlane, anxQuat); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "TransformQuaternion"); + } + + [Test, TestCaseSource("eightfloats")] + public static void TransformQuaternionRef( + float x1, float y1, float z1, float w1, + float x2, float y2, float z2, float w2) + { + XNAPlane xnaPlane = new XNAPlane(x1, y1, z1, w1); + XNAQuaternion xnaQuat = new XNAQuaternion(x2, y2, z2, w2); + XNAPlane xnaResult; + XNAPlane.Transform(ref xnaPlane, ref xnaQuat, out xnaResult); + + ANXPlane anxPlane = new ANXPlane(x1, y1, z1, w1); + ANXQuaternion anxQuat = new ANXQuaternion(x2, y2, z2, w2); + ANXPlane anxResult; + ANXPlane.Transform(ref anxPlane, ref anxQuat, out anxResult); + + AssertHelper.ConvertEquals(xnaResult, anxResult, "TransformQuaternionRef"); + } + #endregion } } diff --git a/ANX.Framework.TestCenter/Strukturen/Vector2Test.cs b/ANX.Framework.TestCenter/Strukturen/Vector2Test.cs index f32bd60c..0a4627e2 100644 --- a/ANX.Framework.TestCenter/Strukturen/Vector2Test.cs +++ b/ANX.Framework.TestCenter/Strukturen/Vector2Test.cs @@ -608,6 +608,21 @@ namespace ANX.Framework.TestCenter.Strukturen AssertHelper.ConvertEquals(xnaR, anxR, "EqualsOperator"); } + [Test] + public void EqualsOperatorNaN() + { + XNAVector2 xna1 = new XNAVector2(float.NaN, float.NaN); + XNAVector2 xna2 = new XNAVector2(float.NaN, float.NaN); + + ANXVector2 anx1 = new ANXVector2(float.NaN, float.NaN); + ANXVector2 anx2 = new ANXVector2(float.NaN, float.NaN); + + bool xnaR = (xna1 == xna2); + bool anxR = (anx1 == anx2); + + AssertHelper.ConvertEquals(xnaR, anxR, "EqualsOperator"); + } + [Test, TestCaseSource("ninefloats")] public void UnequalsOperator(float x1, float y1, float z1, float w1, float x2, float y2, float z2, float w2, float nop1) { diff --git a/ANX.Framework.TestCenter/Strukturen/Vector4Test.cs b/ANX.Framework.TestCenter/Strukturen/Vector4Test.cs index 81dcb72d..d8bfc683 100644 --- a/ANX.Framework.TestCenter/Strukturen/Vector4Test.cs +++ b/ANX.Framework.TestCenter/Strukturen/Vector4Test.cs @@ -689,7 +689,43 @@ namespace ANX.Framework.TestCenter.Strukturen } [Test, TestCaseSource("twentyfourFloats")] - public void TransformStaticQuaternion( + public void TransformStaticQuaternionVector2( + float x, float y, float z, float w, + float xQ, float yQ, float zQ, float wQ, + float nop0, float nop1, float nop2, float nop3, float nop4, float nop5, float nop6, float nop7, float nop8, float nop9, float nop10, float nop11, float nop12, float nop13, float nop14, float nop15) + { + XNAVector2 xnaVector = new XNAVector2(x, y); + XNAQuaternion xnaQuaternion = new XNAQuaternion(xQ, yQ, zQ, wQ); + + ANXVector2 anxVector = new ANXVector2(x, y); + ANXQuaternion anxQuaternion = new ANXQuaternion(xQ, yQ, zQ, wQ); + + XNAVector4 xna = XNAVector4.Transform(xnaVector, xnaQuaternion); + ANXVector4 anx = ANXVector4.Transform(anxVector, anxQuaternion); + + AssertHelper.ConvertEquals(xna, anx, "TransformStaticQuaternion"); + } + + [Test, TestCaseSource("twentyfourFloats")] + public void TransformStaticQuaternionVector3( + float x, float y, float z, float w, + float xQ, float yQ, float zQ, float wQ, + float nop0, float nop1, float nop2, float nop3, float nop4, float nop5, float nop6, float nop7, float nop8, float nop9, float nop10, float nop11, float nop12, float nop13, float nop14, float nop15) + { + XNAVector3 xnaVector = new XNAVector3(x, y, z); + XNAQuaternion xnaQuaternion = new XNAQuaternion(xQ, yQ, zQ, wQ); + + ANXVector3 anxVector = new ANXVector3(x, y, z); + ANXQuaternion anxQuaternion = new ANXQuaternion(xQ, yQ, zQ, wQ); + + XNAVector4 xna = XNAVector4.Transform(xnaVector, xnaQuaternion); + ANXVector4 anx = ANXVector4.Transform(anxVector, anxQuaternion); + + AssertHelper.ConvertEquals(xna, anx, "TransformStaticQuaternion"); + } + + [Test, TestCaseSource("twentyfourFloats")] + public void TransformStaticQuaternionVector4( float x, float y, float z, float w, float xQ, float yQ, float zQ, float wQ, float nop0, float nop1, float nop2, float nop3, float nop4, float nop5, float nop6, float nop7, float nop8, float nop9, float nop10, float nop11, float nop12, float nop13, float nop14, float nop15) diff --git a/ANX.Framework/BoundingBox.cs b/ANX.Framework/BoundingBox.cs index b11e4dae..ba3dd843 100644 --- a/ANX.Framework/BoundingBox.cs +++ b/ANX.Framework/BoundingBox.cs @@ -53,7 +53,7 @@ namespace ANX.Framework result = ContainmentType.Contains; return; } - + result = ContainmentType.Intersects; } @@ -312,17 +312,9 @@ namespace ANX.Framework public void Intersects(ref BoundingBox box, out bool result) { - if ((this.Max.X < box.Min.X || this.Min.X > box.Max.X) || - (this.Max.Y < box.Min.Y || this.Min.Y > box.Max.Y) || - (this.Max.Z < box.Min.Z || this.Min.Z > box.Max.Z) - ) - { - result = false; - } - else - { - result = true; - } + result = (this.Max.X >= box.Min.X && this.Min.X <= box.Max.X) && + (this.Max.Y >= box.Min.Y && this.Min.Y <= box.Max.Y) && + (this.Max.Z >= box.Min.Z && this.Min.Z <= box.Max.Z); } public bool Intersects(BoundingFrustum frustum) @@ -456,7 +448,7 @@ namespace ANX.Framework result = null; return; } - + t1 = (this.Min.Z - ray.Position.Z) - ray.Direction.Z; t2 = (this.Max.Z - ray.Position.Z) - ray.Direction.Z; @@ -483,17 +475,11 @@ namespace ANX.Framework } public override string ToString() - { - // This may look a bit more ugly, but String.Format should - // be avoided cause of it's bad performance! - return "{Min:" + Min.ToString() + - " Max:" + Max.ToString() + "}"; - - // return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{{Min:{0} Max:{1}}}", new object[] - //{ - // this.Min.ToString(), - // this.Max.ToString() - //}); + { + // This may look a bit more ugly, but String.Format should + // be avoided cause of it's bad performance! + return "{Min:" + Min.ToString() + + " Max:" + Max.ToString() + "}"; } #endregion diff --git a/ANX.Framework/Plane.cs b/ANX.Framework/Plane.cs index 772933fb..4a452f94 100644 --- a/ANX.Framework/Plane.cs +++ b/ANX.Framework/Plane.cs @@ -33,7 +33,7 @@ namespace ANX.Framework { // calculate 2 vectos spanning the plane and cross them to get the normal, then normalize this.Normal = Vector3.Normalize(Vector3.Cross(Vector3.Subtract(point2, point1), Vector3.Subtract(point3, point1))); - // now calculate d + // now calculate d this.D = Vector3.Dot(point1, this.Normal); } public Plane(Vector4 value) @@ -47,21 +47,16 @@ namespace ANX.Framework #region public methods public float Dot(Vector4 value) { - float result; - this.Dot(ref value, out result); - return result; + return this.Normal.X * value.X + this.Normal.Y * value.Y + this.Normal.Z * value.Z + this.D * value.W; } public void Dot(ref Vector4 value, out float result) { - //taken from vektor result = this.Normal.X * value.X + this.Normal.Y * value.Y + this.Normal.Z * value.Z + this.D * value.W; } public float DotCoordinate(Vector3 value) { - float result; - this.DotCoordinate(ref value, out result); - return result; + return this.Normal.X * value.X + this.Normal.Y * value.Y + this.Normal.Z * value.Z + this.D; } public void DotCoordinate(ref Vector3 value, out float result) { @@ -70,18 +65,16 @@ namespace ANX.Framework public float DotNormal(Vector3 value) { - float result; - this.DotNormal(ref value, out result); - return result; + return this.Normal.X * value.X + this.Normal.Y * value.Y + this.Normal.Z * value.Z; } public void DotNormal(ref Vector3 value, out float result) { - result = this.Normal.X * value.X + this.Normal.Y * value.Y + this.Normal.Z * value.Z ; + result = this.Normal.X * value.X + this.Normal.Y * value.Y + this.Normal.Z * value.Z; } public override int GetHashCode() { - return this.D.GetHashCode() + this.Normal.GetHashCode(); + return this.D.GetHashCode() + this.Normal.GetHashCode(); } public PlaneIntersectionType Intersects(BoundingBox box) @@ -90,10 +83,38 @@ namespace ANX.Framework this.Intersects(ref box, out result); return result; } + public void Intersects(ref BoundingBox box, out PlaneIntersectionType result) { - throw new Exception("method has not yet been implemented"); + Vector3 p; + + p.X = this.Normal.X >= 0f ? box.Min.X : box.Max.X; + p.Y = this.Normal.Y >= 0f ? box.Min.Y : box.Max.X; + p.Z = this.Normal.Z >= 0f ? box.Min.Z : box.Max.X; + + float dot = this.Normal.X * p.X + this.Normal.Y * p.Y + this.Normal.Z * p.Z; + + if (dot + this.D > 0f) + { + result = PlaneIntersectionType.Front; + return; + } + + p.X = this.Normal.X >= 0f ? box.Max.X : box.Min.X; + p.Y = this.Normal.Y >= 0f ? box.Max.Y : box.Min.X; + p.Z = this.Normal.Z >= 0f ? box.Max.Z : box.Min.X; + + dot = this.Normal.X * p.X + this.Normal.Y * p.Y + this.Normal.Z * p.Z; + + if (dot + this.D < 0f) + { + result = PlaneIntersectionType.Back; + return; + } + + result = PlaneIntersectionType.Intersecting; } + public PlaneIntersectionType Intersects(BoundingFrustum frustum) { PlaneIntersectionType result; @@ -147,7 +168,6 @@ namespace ANX.Framework } else { - if (distanceSquared_Sphere_Origin > distanceSquared_Plane_Origin) { if (distanceSquared_Sphere_Origin - sphere.Radius < distanceSquared_Plane_Origin) @@ -177,55 +197,149 @@ namespace ANX.Framework } //else distance sphere == distance plane result = PlaneIntersectionType.Intersecting; - - } public void Normalize() { - throw new Exception("method has not yet been implemented"); + float l = Normal.X * Normal.X + Normal.Y * Normal.Y + Normal.Z * Normal.Z; + if (Math.Abs(1.0f - l) < float.Epsilon) + { + return; + } + l = 1.0f / (float)Math.Sqrt(l); + Normal.X = Normal.X * l; + Normal.Y = Normal.Y * l; + Normal.Z = Normal.Z * l; + this.D = this.D * l; } + public static Plane Normalize(Plane value) { + Vector3 n = value.Normal; + float l = n.X * n.X + n.Y * n.Y + n.Z * n.Z; + if (Math.Abs(1.0f - l) < float.Epsilon) + { + return new Plane(n, value.D); + } + l = 1.0f / (float)Math.Sqrt(l); Plane result; - Plane.Normalize(ref value, out result); + result.Normal.X = value.Normal.X * l; + result.Normal.Y = value.Normal.Y * l; + result.Normal.Z = value.Normal.Z * l; + result.D = value.D * l; return result; } + public static void Normalize(ref Plane value, out Plane result) { - throw new Exception("method has not yet been implemented"); + Vector3 n = value.Normal; + float l = n.X * n.X + n.Y * n.Y + n.Z * n.Z; + if (Math.Abs(1.0f - l) < float.Epsilon) + { + result.Normal = n; + result.D = value.D; + return; + } + l = 1.0f / (float)Math.Sqrt(l); + result.Normal.X = value.Normal.X * l; + result.Normal.Y = value.Normal.Y * l; + result.Normal.Z = value.Normal.Z * l; + result.D = value.D * l; } public override string ToString() - { - var culture = CultureInfo.CurrentCulture; - // This may look a bit more ugly, but String.Format should - // be avoided cause of it's bad performance! - return "{Normal:" + Normal.ToString() + - " D:" + D.ToString(culture) + "}"; - - //return string.Format(culture, "{{Normal:{0} D:{1}}}", new object[] - //{ - // this.Normal.ToString(), - // this.D.ToString(culture) - //}); + { + var culture = CultureInfo.CurrentCulture; + // This may look a bit more ugly, but String.Format should + // be avoided cause of it's bad performance! + return "{Normal:" + Normal.ToString() + + " D:" + D.ToString(culture) + "}"; } public static Plane Transform(Plane plane, Matrix matrix) { - throw new Exception("method has not yet been implemented"); + // multiply by the inverse transpose of the matrix + Matrix m; + Matrix.Invert(ref matrix, out m); + + Vector3 n = plane.Normal; + Plane result; + result.Normal.X = n.X * m.M11 + n.Y * m.M12 + n.Z * m.M13 + plane.D * m.M14; + result.Normal.Y = n.X * m.M21 + n.Y * m.M22 + n.Z * m.M23 + plane.D * m.M24; + result.Normal.Z = n.X * m.M31 + n.Y * m.M32 + n.Z * m.M33 + plane.D * m.M34; + result.D = n.X * m.M41 + n.Y * m.M42 + n.Z * m.M43 + plane.D * m.M44; + return result; } + public static void Transform(ref Plane plane, ref Matrix matrix, out Plane result) { - throw new Exception("method has not yet been implemented"); + // multiply by the inverse transpose of the matrix + Matrix m; + Matrix.Invert(ref matrix, out m); + + Vector3 n = plane.Normal; + result.Normal.X = n.X * m.M11 + n.Y * m.M12 + n.Z * m.M13 + plane.D * m.M14; + result.Normal.Y = n.X * m.M21 + n.Y * m.M22 + n.Z * m.M23 + plane.D * m.M24; + result.Normal.Z = n.X * m.M31 + n.Y * m.M32 + n.Z * m.M33 + plane.D * m.M34; + result.D = n.X * m.M41 + n.Y * m.M42 + n.Z * m.M43 + plane.D * m.M44; } + public static Plane Transform(Plane plane, Quaternion rotation) { - throw new Exception("method has not yet been implemented"); + float twoX = rotation.X + rotation.X; + float twoY = rotation.Y + rotation.Y; + float twoZ = rotation.Z + rotation.Z; + + float twoXX = twoX * rotation.X; + float twoXY = twoX * rotation.Y; + float twoXZ = twoX * rotation.Z; + float twoXW = twoX * rotation.W; + + float twoYY = twoY * rotation.Y; + float twoYZ = twoY * rotation.Z; + float twoYW = twoY * rotation.W; + + float twoZZ = twoZ * rotation.Z; + float twoZW = twoZ * rotation.W; + + float x = plane.Normal.X; + float y = plane.Normal.Y; + float z = plane.Normal.Z; + + Plane result; + result.Normal.X = x * (1.0f - twoYY - twoZZ) + y * (twoXY - twoZW) + z * (twoXZ + twoYW); + result.Normal.Y = x * (twoXY + twoZW) + y * (1.0f - twoXX - twoZZ) + z * (twoYZ - twoXW); + result.Normal.Z = x * (twoXZ - twoYW) + y * (twoYZ + twoXW) + z * (1.0f - twoXX - twoYY); + result.D = plane.D; + return result; } + public static void Transform(ref Plane plane, ref Quaternion rotation, out Plane result) { - throw new Exception("method has not yet been implemented"); + float twoX = rotation.X + rotation.X; + float twoY = rotation.Y + rotation.Y; + float twoZ = rotation.Z + rotation.Z; + + float twoXX = twoX * rotation.X; + float twoXY = twoX * rotation.Y; + float twoXZ = twoX * rotation.Z; + float twoXW = twoX * rotation.W; + + float twoYY = twoY * rotation.Y; + float twoYZ = twoY * rotation.Z; + float twoYW = twoY * rotation.W; + + float twoZZ = twoZ * rotation.Z; + float twoZW = twoZ * rotation.W; + + float x = plane.Normal.X; + float y = plane.Normal.Y; + float z = plane.Normal.Z; + + result.Normal.X = x * (1.0f - twoYY - twoZZ) + y * (twoXY - twoZW) + z * (twoXZ + twoYW); + result.Normal.Y = x * (twoXY + twoZW) + y * (1.0f - twoXX - twoZZ) + z * (twoYZ - twoXW); + result.Normal.Z = x * (twoXZ - twoYW) + y * (twoYZ + twoXW) + z * (1.0f - twoXX - twoYY); + result.D = plane.D; } #endregion @@ -235,6 +349,7 @@ namespace ANX.Framework { return (obj is Plane) ? this.Equals((Plane)obj) : false; } + public bool Equals(Plane other) { return this.D == other.D && Normal.Equals(other.Normal); @@ -247,6 +362,7 @@ namespace ANX.Framework { return lhs.D.Equals(rhs.D) && lhs.Normal.Equals(rhs.Normal); } + public static bool operator !=(Plane lhs, Plane rhs) { return !lhs.D.Equals(rhs.D) || !lhs.Normal.Equals(rhs.Normal); diff --git a/ANX.Framework/Quaternion.cs b/ANX.Framework/Quaternion.cs index 64158cba..fcd8c694 100644 --- a/ANX.Framework/Quaternion.cs +++ b/ANX.Framework/Quaternion.cs @@ -38,6 +38,7 @@ namespace ANX.Framework this.Z = z; this.W = w; } + public Quaternion(Vector3 vectorPart, float scalarPart) { this.X = vectorPart.X; @@ -51,11 +52,9 @@ namespace ANX.Framework #region public methods public static Quaternion Add(Quaternion quaternion1, Quaternion quaternion2) { - Quaternion result; Quaternion.Add(ref quaternion1, ref quaternion2, out result); return result; - } public static void Add(ref Quaternion quaternion1, ref Quaternion quaternion2, out Quaternion result) @@ -71,7 +70,6 @@ namespace ANX.Framework Quaternion result; Quaternion.Concatenate(ref value1, ref value2, out result); return result; - } public static void Concatenate(ref Quaternion value1, ref Quaternion value2, out Quaternion result) @@ -81,10 +79,9 @@ namespace ANX.Framework public void Conjugate() { - this.X = -this.X; //should be =this.X; + this.X = -this.X; this.Y = -this.Y; this.Z = -this.Z; - } public static Quaternion Conjugate(Quaternion value) @@ -372,11 +369,9 @@ namespace ANX.Framework public static Quaternion Slerp(Quaternion quaternion1, Quaternion quaternion2, float amount) { - Quaternion result; Quaternion.Slerp(ref quaternion1, ref quaternion2, amount, out result); return result; throw new NotImplementedException(); - } public static void Slerp(ref Quaternion quaternion1, ref Quaternion quaternion2, float amount, out Quaternion result) @@ -412,22 +407,12 @@ namespace ANX.Framework } public override string ToString() - { - var culture = CultureInfo.CurrentCulture; - // This may look a bit more ugly, but String.Format should - // be avoided cause of it's bad performance! - return "{X:" + X.ToString(culture) + - " Y:" + Y.ToString(culture) + - " Z:" + Z.ToString(culture) + - " W:" + W.ToString(culture) + "}"; - - //return string.Format(culture, "{{X:{0} Y:{1} Z:{2} W:{3}}}", new object[] - //{ - // this.X.ToString(culture), - // this.Y.ToString(culture), - // this.Z.ToString(culture), - // this.W.ToString(culture) - //}); + { + var culture = CultureInfo.CurrentCulture; + return "{X:" + X.ToString(culture) + + " Y:" + Y.ToString(culture) + + " Z:" + Z.ToString(culture) + + " W:" + W.ToString(culture) + "}"; } #endregion diff --git a/ANX.Framework/Ray.cs b/ANX.Framework/Ray.cs index 73210959..1e30d081 100644 --- a/ANX.Framework/Ray.cs +++ b/ANX.Framework/Ray.cs @@ -172,17 +172,10 @@ namespace ANX.Framework /// public override string ToString() { - // This may look a bit more ugly, but String.Format should - // be avoided cause of it's bad performance! - return "{Position:" + Position.ToString() + - " Direction:" + Direction.ToString() + "}"; - - //var currentCulture = System.Globalization.CultureInfo.CurrentCulture; - //return string.Format(currentCulture, "{{Position:{0} Direction:{1}}}", new object[] - //{ - // this.Position.ToString(), - // this.Direction.ToString() - //}); + // This may look a bit more ugly, but String.Format should + // be avoided cause of it's bad performance! + return "{Position:" + Position.ToString() + + " Direction:" + Direction.ToString() + "}"; } #endregion diff --git a/ANX.Framework/Rectangle.cs b/ANX.Framework/Rectangle.cs index 3a307363..aba19283 100644 --- a/ANX.Framework/Rectangle.cs +++ b/ANX.Framework/Rectangle.cs @@ -11,9 +11,9 @@ namespace ANX.Framework { #region fields public int X; - public int Y; - public int Width; - public int Height; + public int Y; + public int Width; + public int Height; #endregion #region properties @@ -57,13 +57,13 @@ namespace ANX.Framework } } - public int Left - { - get - { - return this.X; - } - } + public int Left + { + get + { + return this.X; + } + } public int Right { @@ -81,13 +81,13 @@ namespace ANX.Framework } } - public int Bottom - { - get - { - return this.Y + Height; - } - } + public int Bottom + { + get + { + return this.Y + Height; + } + } #endregion #region constructors @@ -123,9 +123,9 @@ namespace ANX.Framework private void Contains(ref int x, ref int y, out bool result) { result = x > this.X && - x < this.Right && - y > this.Y && - y < this.Bottom; + x < this.Right && + y > this.Y && + y < this.Bottom; } public bool Contains(Rectangle value) @@ -148,15 +148,12 @@ namespace ANX.Framework return this.X + this.Y + this.Width + this.Height; } - - public void Inflate(int horizontalAmount, int verticalAmount) { this.X -= horizontalAmount; this.Y -= verticalAmount; this.Width += horizontalAmount * 2; this.Height += verticalAmount * 2; - } public bool Intersects(Rectangle value) @@ -172,9 +169,9 @@ namespace ANX.Framework //outer if (value.X > this.Right || - value.Y > this.Bottom || - value.X + value.Width < this.X || - value.Y + value.Height < this.Y) + value.Y > this.Bottom || + value.X + value.Width < this.X || + value.Y + value.Height < this.Y) { result = false; return; @@ -186,7 +183,6 @@ namespace ANX.Framework return; } result = true; - } public void Offset(int offsetX, int offsetY) @@ -203,22 +199,14 @@ namespace ANX.Framework } public override string ToString() - { - var culture = CultureInfo.CurrentCulture; - // This may look a bit more ugly, but String.Format should - // be avoided cause of it's bad performance! - return "{X:" + X.ToString(culture) + - " Y:" + Y.ToString(culture) + - " Width:" + Width.ToString(culture) + - " Height:" + Height.ToString(culture) + "}"; - - //return string.Format(culture, "{{X:{0} Y:{1} Width:{2} Height:{3}}}", new object[] - //{ - // this.X.ToString(culture), - // this.Y.ToString(culture), - // this.Width.ToString(culture), - // this.Height.ToString(culture) - //}); + { + var culture = CultureInfo.CurrentCulture; + // This may look a bit more ugly, but String.Format should + // be avoided cause of it's bad performance! + return "{X:" + X.ToString(culture) + + " Y:" + Y.ToString(culture) + + " Width:" + Width.ToString(culture) + + " Height:" + Height.ToString(culture) + "}"; } #endregion @@ -370,20 +358,20 @@ namespace ANX.Framework #region operator overloading public static bool operator ==(Rectangle a, Rectangle b) - { - // NOTE: Duplicated code is better than copying 4 floats around! - return a.Height == b.Height && - a.Width == b.Width && - a.X == b.X && - a.Y == b.Y; + { + // NOTE: Duplicated code is better than copying 4 floats around! + return a.Height == b.Height && + a.Width == b.Width && + a.X == b.X && + a.Y == b.Y; } public static bool operator !=(Rectangle a, Rectangle b) - { - // NOTE: Duplicated code is better than copying 4 floats around! - return a.Height != b.Height || - a.Width != b.Width || - a.X != b.X || - a.Y != b.Y; + { + // NOTE: Duplicated code is better than copying 4 floats around! + return a.Height != b.Height || + a.Width != b.Width || + a.X != b.X || + a.Y != b.Y; } #endregion } diff --git a/ANX.Framework/Vector2.cs b/ANX.Framework/Vector2.cs index 1d7a06a0..86522693 100644 --- a/ANX.Framework/Vector2.cs +++ b/ANX.Framework/Vector2.cs @@ -23,7 +23,7 @@ namespace ANX.Framework { get { - return new Vector2(1f, 1f); + return new Vector2(1f, 1f); } } #endregion @@ -36,7 +36,7 @@ namespace ANX.Framework { get { - return new Vector2(0f, 0f); + return new Vector2(0f, 0f); } } #endregion @@ -49,7 +49,7 @@ namespace ANX.Framework { get { - return new Vector2(1f, 0f); + return new Vector2(1f, 0f); } } #endregion @@ -62,7 +62,7 @@ namespace ANX.Framework { get { - return new Vector2(0f, 1f); + return new Vector2(0f, 1f); } } #endregion @@ -83,8 +83,8 @@ namespace ANX.Framework #endregion #region public methods - #region Add - public static Vector2 Add(Vector2 value1, Vector2 value2) + #region Add + public static Vector2 Add(Vector2 value1, Vector2 value2) { Vector2 result; Vector2.Add(ref value1, ref value2, out result); @@ -92,55 +92,49 @@ namespace ANX.Framework } public static void Add(ref Vector2 value1, ref Vector2 value2, - out Vector2 result) + out Vector2 result) { result.X = value1.X + value2.X; result.Y = value1.Y + value2.Y; } - #endregion + #endregion - #region Barycentric - public static Vector2 Barycentric(Vector2 value1, Vector2 value2, - Vector2 value3, float amount1, float amount2) + #region Barycentric + public static Vector2 Barycentric(Vector2 value1, Vector2 value2, + Vector2 value3, float amount1, float amount2) { Vector2 result; - Vector2.Barycentric(ref value1, ref value2, ref value3, amount1, - amount2, out result); + Vector2.Barycentric(ref value1, ref value2, ref value3, amount1, amount2, out result); return result; } public static void Barycentric(ref Vector2 value1, ref Vector2 value2, - ref Vector2 value3, float amount1, float amount2, out Vector2 result) + ref Vector2 value3, float amount1, float amount2, out Vector2 result) { - result.X = MathHelper.Barycentric(value1.X, value2.X, value3.X, - amount1, amount2); - result.Y = MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, - amount1, amount2); - } - #endregion + result.X = MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2); + result.Y = MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2); + } + #endregion - #region CatmullRom - public static Vector2 CatmullRom(Vector2 value1, Vector2 value2, - Vector2 value3, Vector2 value4, float amount) + #region CatmullRom + public static Vector2 CatmullRom(Vector2 value1, Vector2 value2, + Vector2 value3, Vector2 value4, float amount) { Vector2 result; - Vector2.CatmullRom(ref value1, ref value2, ref value3, ref value4, - amount, out result); + Vector2.CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out result); return result; } public static void CatmullRom(ref Vector2 value1, ref Vector2 value2, - ref Vector2 value3, ref Vector2 value4, float amount, out Vector2 result) + ref Vector2 value3, ref Vector2 value4, float amount, out Vector2 result) { - result.X = MathHelper.CatmullRom(value1.X, value2.X, value3.X, - value4.X, amount); - result.Y = MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, - value4.Y, amount); - } - #endregion + result.X = MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount); + result.Y = MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount); + } + #endregion - #region Clamp - public static Vector2 Clamp(Vector2 value1, Vector2 min, Vector2 max) + #region Clamp + public static Vector2 Clamp(Vector2 value1, Vector2 min, Vector2 max) { Vector2 result; Vector2.Clamp(ref value1, ref min, ref max, out result); @@ -148,15 +142,15 @@ namespace ANX.Framework } public static void Clamp(ref Vector2 value1, ref Vector2 min, - ref Vector2 max, out Vector2 result) + ref Vector2 max, out Vector2 result) { result.X = MathHelper.Clamp(value1.X, min.X, max.X); result.Y = MathHelper.Clamp(value1.Y, min.Y, max.Y); } - #endregion + #endregion - #region Distance - public static float Distance(Vector2 value1, Vector2 value2) + #region Distance + public static float Distance(Vector2 value1, Vector2 value2) { float result; Vector2.Distance(ref value1, ref value2, out result); @@ -164,16 +158,16 @@ namespace ANX.Framework } public static void Distance(ref Vector2 value1, ref Vector2 value2, - out float result) + out float result) { Vector2 tmp; Vector2.Subtract(ref value1, ref value2, out tmp); result = tmp.Length(); - } - #endregion + } + #endregion - #region DistanceSquared - public static float DistanceSquared(Vector2 value1, Vector2 value2) + #region DistanceSquared + public static float DistanceSquared(Vector2 value1, Vector2 value2) { float result; Vector2.DistanceSquared(ref value1, ref value2, out result); @@ -181,16 +175,16 @@ namespace ANX.Framework } public static void DistanceSquared(ref Vector2 value1, - ref Vector2 value2, out float result) + ref Vector2 value2, out float result) { Vector2 tmp; Vector2.Subtract(ref value1, ref value2, out tmp); result = tmp.LengthSquared(); - } - #endregion + } + #endregion - #region Divide - public static Vector2 Divide(Vector2 value1, float divider) + #region Divide + public static Vector2 Divide(Vector2 value1, float divider) { Vector2 result; Vector2.Divide(ref value1, divider, out result); @@ -198,7 +192,7 @@ namespace ANX.Framework } public static void Divide(ref Vector2 value1, float divider, - out Vector2 result) + out Vector2 result) { divider = 1f / divider; result.X = value1.X * divider; @@ -213,70 +207,70 @@ namespace ANX.Framework } public static void Divide(ref Vector2 value1, ref Vector2 value2, - out Vector2 result) + out Vector2 result) { result.X = value1.X / value2.X; result.Y = value1.Y / value2.Y; - } - #endregion + } + #endregion - #region Dot - public static float Dot(Vector2 value1, Vector2 value2) + #region Dot + public static float Dot(Vector2 value1, Vector2 value2) { - return value1.X * value2.X + value1.Y * value2.Y; + return value1.X * value2.X + value1.Y * value2.Y; } public static void Dot(ref Vector2 value1, ref Vector2 value2, - out float result) + out float result) { result = value1.X * value2.X + value1.Y * value2.Y; - } - #endregion + } + #endregion - #region GetHashCode - public override int GetHashCode() + #region GetHashCode + public override int GetHashCode() { return this.X.GetHashCode() + this.Y.GetHashCode(); - } - #endregion + } + #endregion - #region Hermite - public static Vector2 Hermite(Vector2 value1, Vector2 tangent1, - Vector2 value2, Vector2 tangent2, float amount) + #region Hermite + public static Vector2 Hermite(Vector2 value1, Vector2 tangent1, + Vector2 value2, Vector2 tangent2, float amount) { Vector2 result; Vector2.Hermite(ref value1, ref tangent1, ref value2, ref tangent2, - amount, out result); + amount, out result); return result; } public static void Hermite(ref Vector2 value1, ref Vector2 tangent1, - ref Vector2 value2, ref Vector2 tangent2, float amount, - out Vector2 result) + ref Vector2 value2, ref Vector2 tangent2, float amount, + out Vector2 result) { result.X = MathHelper.Hermite(value1.X, tangent1.X, value2.X, - tangent2.X, amount); + tangent2.X, amount); result.Y = MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, - tangent2.Y, amount); - } - #endregion + tangent2.Y, amount); + } + #endregion - #region Length - public float Length() + #region Length + public float Length() { return (float)Math.Sqrt((this.X * this.X) + (this.Y * this.Y)); - } - #endregion + } + #endregion - #region LengthSquared - public float LengthSquared() + #region LengthSquared + public float LengthSquared() { return (this.X * this.X) + (this.Y * this.Y); - } - #endregion + } + #endregion - #region Lerp - public static Vector2 Lerp(Vector2 value1, Vector2 value2, float amount) + #region Lerp + public static Vector2 Lerp(Vector2 value1, Vector2 value2, float amount) { Vector2 result; Vector2.Lerp(ref value1, ref value2, amount, out result); @@ -284,15 +278,15 @@ namespace ANX.Framework } public static void Lerp(ref Vector2 value1, ref Vector2 value2, - float amount, out Vector2 result) + float amount, out Vector2 result) { - result.X = value1.X + (value2.X - value1.X) * amount; - result.Y = value1.Y + (value2.Y - value1.Y) * amount; - } - #endregion + result.X = value1.X + (value2.X - value1.X) * amount; + result.Y = value1.Y + (value2.Y - value1.Y) * amount; + } + #endregion - #region Max - public static Vector2 Max(Vector2 value1, Vector2 value2) + #region Max + public static Vector2 Max(Vector2 value1, Vector2 value2) { Vector2 result; Vector2.Max(ref value1, ref value2, out result); @@ -300,15 +294,15 @@ namespace ANX.Framework } public static void Max(ref Vector2 value1, ref Vector2 value2, - out Vector2 result) + out Vector2 result) { result.X = (value1.X > value2.X) ? value1.X : value2.X; result.Y = (value1.Y > value2.Y) ? value1.Y : value2.Y; - } - #endregion + } + #endregion - #region Min - public static Vector2 Min(Vector2 value1, Vector2 value2) + #region Min + public static Vector2 Min(Vector2 value1, Vector2 value2) { Vector2 result; Vector2.Min(ref value1, ref value2, out result); @@ -316,15 +310,15 @@ namespace ANX.Framework } public static void Min(ref Vector2 value1, ref Vector2 value2, - out Vector2 result) + out Vector2 result) { result.X = (value1.X < value2.X) ? value1.X : value2.X; result.Y = (value1.Y < value2.Y) ? value1.Y : value2.Y; - } - #endregion + } + #endregion - #region Multiply - public static Vector2 Multiply(Vector2 value1, float scaleFactor) + #region Multiply + public static Vector2 Multiply(Vector2 value1, float scaleFactor) { Vector2 result; Vector2.Multiply(ref value1, scaleFactor, out result); @@ -332,7 +326,7 @@ namespace ANX.Framework } public static void Multiply(ref Vector2 value1, float scaleFactor, - out Vector2 result) + out Vector2 result) { result.X = value1.X * scaleFactor; result.Y = value1.Y * scaleFactor; @@ -346,30 +340,30 @@ namespace ANX.Framework } public static void Multiply(ref Vector2 value1, ref Vector2 value2, - out Vector2 result) + out Vector2 result) { result.X = value1.X * value2.X; result.Y = value1.Y * value2.Y; - } - #endregion + } + #endregion - #region Negate - public static Vector2 Negate(Vector2 value) + #region Negate + public static Vector2 Negate(Vector2 value) { - // Is a bit faster than copying the vector floats to the operator method. - return new Vector2(-value.X, -value.Y); + // Is a bit faster than copying the vector floats to the operator method. + return new Vector2(-value.X, -value.Y); } - public static void Negate(ref Vector2 value, out Vector2 result) - { - // Is a bit faster than copying the vector floats to the operator method. - result.X = -value.X; - result.Y = -value.Y; - } - #endregion + public static void Negate(ref Vector2 value, out Vector2 result) + { + // Is a bit faster than copying the vector floats to the operator method. + result.X = -value.X; + result.Y = -value.Y; + } + #endregion - #region Normalize - public void Normalize() + #region Normalize + public void Normalize() { float divider = 1f / this.Length(); this.X *= divider; @@ -387,33 +381,32 @@ namespace ANX.Framework float divider = 1f / value.Length(); result.X = value.X * divider; result.Y = value.Y * divider; - } - #endregion + } + #endregion - /* + /* Vect2 = Vect1 - 2 * WallN * (WallN DOT Vect1) Formula from : http://www.gamedev.net/topic/165537-2d-vector-reflection-/ */ - #region Reflect - public static Vector2 Reflect(Vector2 vector, Vector2 normal) + #region Reflect + public static Vector2 Reflect(Vector2 vector, Vector2 normal) { Vector2 result; Vector2.Reflect(ref vector, ref normal, out result); return result; } - public static void Reflect(ref Vector2 vector, ref Vector2 normal, - out Vector2 result) + public static void Reflect(ref Vector2 vector, ref Vector2 normal, out Vector2 result) { float sub = 2 * Vector2.Dot(vector, normal); result.X = vector.X - (sub * normal.X); result.Y = vector.Y - (sub * normal.Y); - } - #endregion + } + #endregion - #region SmoothStep - public static Vector2 SmoothStep(Vector2 value1, Vector2 value2, - float amount) + #region SmoothStep + public static Vector2 SmoothStep(Vector2 value1, Vector2 value2, + float amount) { Vector2 result; Vector2.SmoothStep(ref value1, ref value2, amount, out result); @@ -421,48 +414,40 @@ namespace ANX.Framework } public static void SmoothStep(ref Vector2 value1, ref Vector2 value2, - float amount, out Vector2 result) + float amount, out Vector2 result) { result.X = MathHelper.SmoothStep(value1.X, value2.X, amount); result.Y = MathHelper.SmoothStep(value1.Y, value2.Y, amount); - } - #endregion + } + #endregion - #region Subtract - public static Vector2 Subtract(Vector2 value1, Vector2 value2) + #region Subtract + public static Vector2 Subtract(Vector2 value1, Vector2 value2) { Vector2 result; Vector2.Subtract(ref value1, ref value2, out result); return result; } - public static void Subtract(ref Vector2 value1, ref Vector2 value2, - out Vector2 result) - { - result.X = value1.X - value2.X; - result.Y = value1.Y - value2.Y; - } - #endregion + public static void Subtract(ref Vector2 value1, ref Vector2 value2, out Vector2 result) + { + result.X = value1.X - value2.X; + result.Y = value1.Y - value2.Y; + } + #endregion - #region ToString - public override string ToString() - { - var culture = CultureInfo.CurrentCulture; - // This may look a bit more ugly, but String.Format should - // be avoided cause of it's bad performance! - return "{X:" + X.ToString(culture) + - " Y:" + Y.ToString(culture) + "}"; + #region ToString + public override string ToString() + { + var culture = CultureInfo.CurrentCulture; + // This may look a bit more ugly, but String.Format should + // be avoided cause of it's bad performance! + return "{X:" + X.ToString(culture) + " Y:" + Y.ToString(culture) + "}"; + } + #endregion - //return string.Format(culture, "{{X:{0} Y:{1}}}", new object[] - //{ - // this.X.ToString(culture), - // this.Y.ToString(culture) - //}); - } - #endregion - - #region Transform - public static Vector2 Transform(Vector2 position, Matrix matrix) + #region Transform + public static Vector2 Transform(Vector2 position, Matrix matrix) { Vector2 result; Transform(ref position, ref matrix, out result); @@ -470,12 +455,10 @@ namespace ANX.Framework } public static void Transform(ref Vector2 position, ref Matrix matrix, - out Vector2 result) + out Vector2 result) { - result.X = (position.X * matrix.M11) + (position.Y * matrix.M21) + - matrix.M41; - result.Y = (position.X * matrix.M12) + (position.Y * matrix.M22) + - matrix.M42; + result.X = (position.X * matrix.M11) + (position.Y * matrix.M21) + matrix.M41; + result.Y = (position.X * matrix.M12) + (position.Y * matrix.M22) + matrix.M42; } public static Vector2 Transform(Vector2 value, Quaternion rotation) @@ -486,7 +469,7 @@ namespace ANX.Framework } public static void Transform(ref Vector2 value, ref Quaternion rotation, - out Vector2 result) + out Vector2 result) { float x = 2 * (-rotation.Z * value.Y); float y = 2 * (rotation.Z * value.X); @@ -497,50 +480,50 @@ namespace ANX.Framework } public static void Transform(Vector2[] sourceArray, int sourceIndex, - ref Matrix matrix, Vector2[] destinationArray, int destinationIndex, - int length) + ref Matrix matrix, Vector2[] destinationArray, int destinationIndex, + int length) { length += sourceIndex; - for (int i = sourceIndex; i < length; i++, destinationIndex++) - { - Transform(ref sourceArray[i], ref matrix, - out destinationArray[destinationIndex]); - } + for (int i = sourceIndex; i < length; i++, destinationIndex++) + { + Transform(ref sourceArray[i], ref matrix, + out destinationArray[destinationIndex]); + } } public static void Transform(Vector2[] sourceArray, int sourceIndex, - ref Quaternion rotation, Vector2[] destinationArray, - int destinationIndex, int length) + ref Quaternion rotation, Vector2[] destinationArray, + int destinationIndex, int length) { length += sourceIndex; - for (int i = sourceIndex; i < length; i++, destinationIndex++) - { - Transform(ref sourceArray[i], ref rotation, - out destinationArray[destinationIndex]); - } + for (int i = sourceIndex; i < length; i++, destinationIndex++) + { + Transform(ref sourceArray[i], ref rotation, + out destinationArray[destinationIndex]); + } } public static void Transform(Vector2[] sourceArray, ref Matrix matrix, - Vector2[] destinationArray) + Vector2[] destinationArray) { - for (int i = 0; i < sourceArray.Length; i++) - { - Transform(ref sourceArray[i], ref matrix, out destinationArray[i]); - } + for (int i = 0; i < sourceArray.Length; i++) + { + Transform(ref sourceArray[i], ref matrix, out destinationArray[i]); + } } public static void Transform(Vector2[] sourceArray, ref Quaternion rotation, - Vector2[] destinationArray) + Vector2[] destinationArray) { - for (int i = 0; i < sourceArray.Length; i++) - { - Transform(ref sourceArray[i], ref rotation, out destinationArray[i]); - } - } - #endregion + for (int i = 0; i < sourceArray.Length; i++) + { + Transform(ref sourceArray[i], ref rotation, out destinationArray[i]); + } + } + #endregion - #region TransformNormal - public static Vector2 TransformNormal(Vector2 normal, Matrix matrix) + #region TransformNormal + public static Vector2 TransformNormal(Vector2 normal, Matrix matrix) { Vector2 result; TransformNormal(ref normal, ref matrix, out result); @@ -548,34 +531,34 @@ namespace ANX.Framework } public static void TransformNormal(ref Vector2 normal, ref Matrix matrix, - out Vector2 result) + out Vector2 result) { result.X = (normal.X * matrix.M11) + (normal.Y * matrix.M21); result.Y = (normal.X * matrix.M12) + (normal.Y * matrix.M22); } public static void TransformNormal(Vector2[] sourceArray, - int sourceIndex, ref Matrix matrix, Vector2[] destinationArray, - int destinationIndex, int length) + int sourceIndex, ref Matrix matrix, Vector2[] destinationArray, + int destinationIndex, int length) { length += sourceIndex; - for (int i = sourceIndex; i < length; i++, destinationIndex++) - { - TransformNormal(ref sourceArray[i], ref matrix, - out destinationArray[destinationIndex]); - } + for (int i = sourceIndex; i < length; i++, destinationIndex++) + { + TransformNormal(ref sourceArray[i], ref matrix, + out destinationArray[destinationIndex]); + } } public static void TransformNormal(Vector2[] sourceArray, - ref Matrix matrix, Vector2[] destinationArray) + ref Matrix matrix, Vector2[] destinationArray) { - for (int i = 0; i < sourceArray.Length; i++) - { - TransformNormal(ref sourceArray[i], ref matrix, - out destinationArray[i]); - } - } - #endregion + for (int i = 0; i < sourceArray.Length; i++) + { + TransformNormal(ref sourceArray[i], ref matrix, + out destinationArray[i]); + } + } + #endregion #endregion #region IEquatable implementation @@ -607,13 +590,11 @@ namespace ANX.Framework public static bool operator ==(Vector2 value1, Vector2 value2) { - return value1.X.Equals(value2.X) && - value1.Y.Equals(value2.Y); + return (value1.X == value2.X) && (value1.Y == value2.Y); } public static bool operator !=(Vector2 value1, Vector2 value2) { - return value1.X.Equals(value2.X) == false || - value1.Y.Equals(value2.Y) == false; + return (value1.X != value2.X) || (value1.Y != value2.Y); } public static Vector2 operator *(float scaleFactor, Vector2 value) diff --git a/ANX.Framework/Vector3.cs b/ANX.Framework/Vector3.cs index 69842582..5ea9bf9f 100644 --- a/ANX.Framework/Vector3.cs +++ b/ANX.Framework/Vector3.cs @@ -155,25 +155,24 @@ namespace ANX.Framework #region Public Static Methods - #region Add - public static Vector3 Add(Vector3 value1, Vector3 value2) + #region Add + public static Vector3 Add(Vector3 value1, Vector3 value2) { Vector3 result; Add(ref value1, ref value2, out result); return result; } - public static void Add(ref Vector3 value1, ref Vector3 value2, - out Vector3 result) + public static void Add(ref Vector3 value1, ref Vector3 value2, out Vector3 result) { result.X = value1.X + value2.X; result.Y = value1.Y + value2.Y; result.Z = value1.Z + value2.Z; } - #endregion + #endregion - #region Subtract - public static Vector3 Subtract(Vector3 value1, Vector3 value2) + #region Subtract + public static Vector3 Subtract(Vector3 value1, Vector3 value2) { Vector3 result; Subtract(ref value1, ref value2, out result); @@ -181,16 +180,16 @@ namespace ANX.Framework } public static void Subtract(ref Vector3 value1, ref Vector3 value2, - out Vector3 result) + out Vector3 result) { result.X = value1.X - value2.X; result.Y = value1.Y - value2.Y; result.Z = value1.Z - value2.Z; } - #endregion + #endregion - #region Multiply - public static Vector3 Multiply(Vector3 value1, float scaleFactor) + #region Multiply + public static Vector3 Multiply(Vector3 value1, float scaleFactor) { Vector3 result; Multiply(ref value1, scaleFactor, out result); @@ -198,12 +197,12 @@ namespace ANX.Framework } public static void Multiply(ref Vector3 value1, float scaleFactor, - out Vector3 result) + out Vector3 result) { result.X = value1.X * scaleFactor; result.Y = value1.Y * scaleFactor; result.Z = value1.Z * scaleFactor; - } + } public static Vector3 Multiply(Vector3 value1, Vector3 value2) { @@ -213,16 +212,16 @@ namespace ANX.Framework } public static void Multiply(ref Vector3 value1, ref Vector3 value2, - out Vector3 result) + out Vector3 result) { result.X = value1.X * value2.X; result.Y = value1.Y * value2.Y; result.Z = value1.Z * value2.Z; - } - #endregion + } + #endregion - #region Divide - public static Vector3 Divide(Vector3 value1, float value2) + #region Divide + public static Vector3 Divide(Vector3 value1, float value2) { Vector3 result; Divide(ref value1, value2, out result); @@ -230,7 +229,7 @@ namespace ANX.Framework } public static void Divide(ref Vector3 value1, float value2, - out Vector3 result) + out Vector3 result) { float factor = 1f / value2; result.X = value1.X * factor; @@ -246,16 +245,16 @@ namespace ANX.Framework } public static void Divide(ref Vector3 value1, ref Vector3 value2, - out Vector3 result) + out Vector3 result) { result.X = value1.X / value2.X; result.Y = value1.Y / value2.Y; result.Z = value1.Z / value2.Z; - } - #endregion + } + #endregion - #region Dot - public static float Dot(Vector3 vector1, Vector3 vector2) + #region Dot + public static float Dot(Vector3 vector1, Vector3 vector2) { float result; Dot(ref vector1, ref vector2, out result); @@ -263,15 +262,15 @@ namespace ANX.Framework } public static void Dot(ref Vector3 vector1, ref Vector3 vector2, - out float result) + out float result) { result = vector1.X * vector2.X + vector1.Y * vector2.Y + - vector1.Z * vector2.Z; - } - #endregion + vector1.Z * vector2.Z; + } + #endregion - #region Cross - public static Vector3 Cross(Vector3 vector1, Vector3 vector2) + #region Cross + public static Vector3 Cross(Vector3 vector1, Vector3 vector2) { Vector3 result; Cross(ref vector1, ref vector2, out result); @@ -279,16 +278,16 @@ namespace ANX.Framework } public static void Cross(ref Vector3 vector1, ref Vector3 vector2, - out Vector3 result) + out Vector3 result) { result.X = vector1.Y * vector2.Z - vector1.Z * vector2.Y; result.Y = vector1.Z * vector2.X - vector1.X * vector2.Z; result.Z = vector1.X * vector2.Y - vector1.Y * vector2.X; } - #endregion + #endregion - #region Normalize - public static Vector3 Normalize(Vector3 value) + #region Normalize + public static Vector3 Normalize(Vector3 value) { Vector3 result; Normalize(ref value, out result); @@ -301,11 +300,11 @@ namespace ANX.Framework result.X = value.X * factor; result.Y = value.Y * factor; result.Z = value.Z * factor; - } - #endregion + } + #endregion - #region Distance - public static float Distance(Vector3 value1, Vector3 value2) + #region Distance + public static float Distance(Vector3 value1, Vector3 value2) { float result; Distance(ref value1, ref value2, out result); @@ -313,16 +312,16 @@ namespace ANX.Framework } public static void Distance(ref Vector3 value1, ref Vector3 value2, - out float result) + out float result) { Vector3 resultVector; Subtract(ref value1, ref value2, out resultVector); result = resultVector.Length(); - } - #endregion + } + #endregion - #region DistanceSquared - public static float DistanceSquared(Vector3 value1, Vector3 value2) + #region DistanceSquared + public static float DistanceSquared(Vector3 value1, Vector3 value2) { float result; DistanceSquared(ref value1, ref value2, out result); @@ -330,59 +329,52 @@ namespace ANX.Framework } public static void DistanceSquared(ref Vector3 value1, - ref Vector3 value2, out float result) + ref Vector3 value2, out float result) { Vector3 resultVector; Subtract(ref value1, ref value2, out resultVector); result = resultVector.LengthSquared(); - } - #endregion + } + #endregion - #region Barycentric - public static Vector3 Barycentric(Vector3 value1, Vector3 value2, - Vector3 value3, float amount1, float amount2) + #region Barycentric + public static Vector3 Barycentric(Vector3 value1, Vector3 value2, + Vector3 value3, float amount1, float amount2) { Vector3 result; Barycentric(ref value1, ref value2, ref value3, amount1, amount2, - out result); + out result); return result; } public static void Barycentric(ref Vector3 value1, ref Vector3 value2, - ref Vector3 value3, float amount1, float amount2, out Vector3 result) + ref Vector3 value3, float amount1, float amount2, out Vector3 result) { - result.X = MathHelper.Barycentric(value1.X, value2.X, value3.X, - amount1, amount2); - result.Y = MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, - amount1, amount2); - result.Z = MathHelper.Barycentric(value1.Z, value2.Z, value3.Z, - amount1, amount2); + result.X = MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2); + result.Y = MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2); + result.Z = MathHelper.Barycentric(value1.Z, value2.Z, value3.Z, amount1, amount2); } - #endregion - - #region CatmullRom + #endregion + + #region CatmullRom public static Vector3 CatmullRom(Vector3 value1, Vector3 value2, - Vector3 value3, Vector3 value4, float amount) + Vector3 value3, Vector3 value4, float amount) { Vector3 result; - CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, - out result); + CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out result); return result; } public static void CatmullRom(ref Vector3 value1, ref Vector3 value2, - ref Vector3 value3, ref Vector3 value4, float amount, out Vector3 result) + ref Vector3 value3, ref Vector3 value4, float amount, out Vector3 result) { - result.X = MathHelper.CatmullRom(value1.X, value2.X, value3.X, - value4.X, amount); - result.Y = MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, - value4.Y, amount); - result.Z = MathHelper.CatmullRom(value1.Z, value2.Z, value3.Z, - value4.Z, amount); + result.X = MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount); + result.Y = MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount); + result.Z = MathHelper.CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount); } - #endregion - - #region Clamp + #endregion + + #region Clamp public static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max) { Vector3 result; @@ -391,37 +383,33 @@ namespace ANX.Framework } public static void Clamp(ref Vector3 value1, ref Vector3 min, - ref Vector3 max, out Vector3 result) + ref Vector3 max, out Vector3 result) { result.X = MathHelper.Clamp(value1.X, min.X, max.X); result.Y = MathHelper.Clamp(value1.Y, min.Y, max.Y); result.Z = MathHelper.Clamp(value1.Z, min.Z, max.Z); } - #endregion - - #region Hermite + #endregion + + #region Hermite public static Vector3 Hermite(Vector3 value1, Vector3 tangent1, - Vector3 value2, Vector3 tangent2, float amount) + Vector3 value2, Vector3 tangent2, float amount) { Vector3 result; - Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, - out result); + Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result); return result; } public static void Hermite(ref Vector3 value1, ref Vector3 tangent1, - ref Vector3 value2, ref Vector3 tangent2, float amount, out Vector3 result) + ref Vector3 value2, ref Vector3 tangent2, float amount, out Vector3 result) { - result.X = MathHelper.Hermite(value1.X, tangent1.X, value2.X, - tangent2.X, amount); - result.Y = MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, - tangent2.Y, amount); - result.Z = MathHelper.Hermite(value1.Z, tangent1.Z, value2.Z, - tangent2.Z, amount); + result.X = MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount); + result.Y = MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount); + result.Z = MathHelper.Hermite(value1.Z, tangent1.Z, value2.Z, tangent2.Z, amount); } - #endregion - - #region Lerp + #endregion + + #region Lerp public static Vector3 Lerp(Vector3 value1, Vector3 value2, float amount) { Vector3 result; @@ -430,15 +418,15 @@ namespace ANX.Framework } public static void Lerp(ref Vector3 value1, ref Vector3 value2, - float amount, out Vector3 result) + float amount, out Vector3 result) { result.X = MathHelper.Lerp(value1.X, value2.X, amount); result.Y = MathHelper.Lerp(value1.Y, value2.Y, amount); result.Z = MathHelper.Lerp(value1.Z, value2.Z, amount); } - #endregion - - #region Max + #endregion + + #region Max public static Vector3 Max(Vector3 value1, Vector3 value2) { Vector3 result; @@ -447,15 +435,15 @@ namespace ANX.Framework } public static void Max(ref Vector3 value1, ref Vector3 value2, - out Vector3 result) + out Vector3 result) { result.X = value1.X > value2.X ? value1.X : value2.X; result.Y = value1.Y > value2.Y ? value1.Y : value2.Y; result.Z = value1.Z > value2.Z ? value1.Z : value2.Z; } - #endregion - - #region Min + #endregion + + #region Min public static Vector3 Min(Vector3 value1, Vector3 value2) { Vector3 result; @@ -464,15 +452,15 @@ namespace ANX.Framework } public static void Min(ref Vector3 value1, ref Vector3 value2, - out Vector3 result) + out Vector3 result) { result.X = value1.X < value2.X ? value1.X : value2.X; result.Y = value1.Y < value2.Y ? value1.Y : value2.Y; result.Z = value1.Z < value2.Z ? value1.Z : value2.Z; } - #endregion - - #region Negate + #endregion + + #region Negate public static Vector3 Negate(Vector3 value) { Vector3 result; @@ -486,9 +474,9 @@ namespace ANX.Framework result.Y = -value.Y; result.Z = -value.Z; } - #endregion - - #region Reflect + #endregion + + #region Reflect public static Vector3 Reflect(Vector3 vector, Vector3 normal) { Vector3 result; @@ -497,18 +485,17 @@ namespace ANX.Framework } public static void Reflect(ref Vector3 vector, ref Vector3 normal, - out Vector3 result) + out Vector3 result) { float scaleFactor = Vector3.Dot(vector, normal) * 2; Vector3 tmp; Multiply(ref normal, scaleFactor, out tmp); Subtract(ref vector, ref tmp, out result); } - #endregion + #endregion - #region SmoothStep - public static Vector3 SmoothStep(Vector3 value1, Vector3 value2, - float amount) + #region SmoothStep + public static Vector3 SmoothStep(Vector3 value1, Vector3 value2, float amount) { Vector3 result; Vector3.SmoothStep(ref value1, ref value2, amount, out result); @@ -516,13 +503,13 @@ namespace ANX.Framework } public static void SmoothStep(ref Vector3 value1, ref Vector3 value2, - float amount, out Vector3 result) + float amount, out Vector3 result) { result.X = MathHelper.SmoothStep(value1.X, value2.X, amount); result.Y = MathHelper.SmoothStep(value1.Y, value2.Y, amount); result.Z = MathHelper.SmoothStep(value1.Z, value2.Z, amount); } - #endregion + #endregion #region Transformations public static Vector3 Transform(Vector3 position, Matrix matrix) @@ -549,35 +536,39 @@ namespace ANX.Framework public static void Transform(ref Vector3 value, ref Quaternion rotation, out Vector3 result) { - float x = 2* (rotation.Y * value.Z - rotation.Z * value.Y); - float y = 2* (rotation.Z * value.X - rotation.X * value.Z); - float z = 2* (rotation.X * value.Y - rotation.Y * value.X); + float x = 2 * (rotation.Y * value.Z - rotation.Z * value.Y); + float y = 2 * (rotation.Z * value.X - rotation.X * value.Z); + float z = 2 * (rotation.X * value.Y - rotation.Y * value.X); result.X = value.X + x * rotation.W + (rotation.Y * z - rotation.Z * y); result.Y = value.Y + y * rotation.W + (rotation.Z * x - rotation.X * z); result.Z = value.Z + z * rotation.W + (rotation.X * y - rotation.Y * x); } - public static void Transform(Vector3[] sourceArray, ref Matrix matrix, Vector3[] destinationArray) + public static void Transform(Vector3[] sourceArray, ref Matrix matrix, + Vector3[] destinationArray) { for (int i = 0; i < sourceArray.Length; i++) Transform(ref sourceArray[i], ref matrix, out destinationArray[i]); } - public static void Transform(Vector3[] sourceArray, ref Quaternion rotation, Vector3[] destinationArray) + public static void Transform(Vector3[] sourceArray, ref Quaternion rotation, + Vector3[] destinationArray) { for (int i = 0; i < sourceArray.Length; i++) Transform(ref sourceArray[i], ref rotation, out destinationArray[i]); } - public static void Transform(Vector3[] sourceArray, int sourceIndex, ref Matrix matrix, Vector3[] destinationArray, int destinationIndex, int length) + public static void Transform(Vector3[] sourceArray, int sourceIndex, ref Matrix matrix, + Vector3[] destinationArray, int destinationIndex, int length) { length += sourceIndex; for (int i = sourceIndex; i < length; i++, destinationIndex++) Transform(ref sourceArray[i], ref matrix, out destinationArray[destinationIndex]); } - public static void Transform(Vector3[] sourceArray, int sourceIndex, ref Quaternion rotation, Vector3[] destinationArray, int destinationIndex, int length) + public static void Transform(Vector3[] sourceArray, int sourceIndex, ref Quaternion rotation, + Vector3[] destinationArray, int destinationIndex, int length) { length += sourceIndex; for (int i = sourceIndex; i < length; i++, destinationIndex++) @@ -604,7 +595,8 @@ namespace ANX.Framework TransformNormal(ref sourceArray[i], ref matrix, out destinationArray[i]); } - public static void TransformNormal(Vector3[] sourceArray, int sourceIndex, ref Matrix matrix, Vector3[] destinationArray, int destinationIndex, int length) + public static void TransformNormal(Vector3[] sourceArray, int sourceIndex, ref Matrix matrix, + Vector3[] destinationArray, int destinationIndex, int length) { length += sourceIndex; for (int i = sourceIndex; i < length; i++, destinationIndex++) @@ -618,120 +610,113 @@ namespace ANX.Framework public static Vector3 operator +(Vector3 value1, Vector3 value2) { - return new Vector3(value1.X + value2.X, value1.Y + value2.Y, - value1.Z + value2.Z); + return new Vector3(value1.X + value2.X, value1.Y + value2.Y, value1.Z + value2.Z); } public static Vector3 operator /(Vector3 value1, float divider) - { - float factor = 1f / divider; - return new Vector3(value1.X * factor, value1.Y * factor, - value1.Z * factor); + { + float factor = 1f / divider; + return new Vector3(value1.X * factor, value1.Y * factor, + value1.Z * factor); } public static Vector3 operator /(Vector3 value1, Vector3 value2) { - return new Vector3(value1.X / value2.X, value1.Y / value2.Y, - value1.Z / value2.Z); + return new Vector3(value1.X / value2.X, value1.Y / value2.Y, + value1.Z / value2.Z); } public static bool operator ==(Vector3 value1, Vector3 value2) - { - return value1.X == value2.X && - value1.Y == value2.Y && - value1.Z == value2.Z; + { + return value1.X == value2.X && + value1.Y == value2.Y && + value1.Z == value2.Z; } public static bool operator !=(Vector3 value1, Vector3 value2) - { - return value1.X != value2.X || - value1.Y != value2.Y || - value1.Z != value2.Z; + { + return value1.X != value2.X || + value1.Y != value2.Y || + value1.Z != value2.Z; } public static Vector3 operator *(Vector3 value, float scaleFactor) { - return new Vector3(value.X * scaleFactor, value.Y * scaleFactor, - value.Z * scaleFactor); + return new Vector3(value.X * scaleFactor, value.Y * scaleFactor, + value.Z * scaleFactor); } public static Vector3 operator *(float scaleFactor, Vector3 value) - { - return new Vector3(value.X * scaleFactor, value.Y * scaleFactor, - value.Z * scaleFactor); + { + return new Vector3(value.X * scaleFactor, value.Y * scaleFactor, + value.Z * scaleFactor); } public static Vector3 operator *(Vector3 value1, Vector3 value2) { - return new Vector3(value1.X * value2.X, value1.Y * value2.Y, - value1.Z * value2.Z); + return new Vector3(value1.X * value2.X, value1.Y * value2.Y, + value1.Z * value2.Z); } public static Vector3 operator -(Vector3 value1, Vector3 value2) - { - return new Vector3(value1.X - value2.X, value1.Y - value2.Y, - value1.Z - value2.Z); + { + return new Vector3(value1.X - value2.X, value1.Y - value2.Y, + value1.Z - value2.Z); } public static Vector3 operator -(Vector3 value) { - return new Vector3(-value.X, -value.Y, -value.Z); + return new Vector3(-value.X, -value.Y, -value.Z); } #endregion #region Public Methods - #region Length - public float Length() + #region Length + public float Length() { return (float)Math.Sqrt(this.X * this.X + this.Y * this.Y + this.Z * this.Z); } - #endregion + #endregion - #region LengthSquared - public float LengthSquared() + #region LengthSquared + public float LengthSquared() { return this.X * this.X + this.Y * this.Y + this.Z * this.Z; - } - #endregion + } + #endregion - #region Normalize - public void Normalize() + #region Normalize + public void Normalize() { float factor = 1f / this.Length(); this.X *= factor; this.Y *= factor; this.Z *= factor; } - #endregion + #endregion - #region GetHashCode - public override int GetHashCode() + #region GetHashCode + public override int GetHashCode() { - return this.X.GetHashCode() + this.Y.GetHashCode() + - this.Z.GetHashCode(); + return this.X.GetHashCode() + + this.Y.GetHashCode() + + this.Z.GetHashCode(); } - #endregion + #endregion - #region ToString - public override string ToString() - { - var culture = CultureInfo.CurrentCulture; - // This may look a bit more ugly, but String.Format should - // be avoided cause of it's bad performance! - return "{X:" + X.ToString(culture) + - " Y:" + Y.ToString(culture) + - " Z:" + Z.ToString(culture) + "}"; - - //return string.Format(culture, "{{X:{0} Y:{1} Z:{2}}}", new object[] - //{ - // this.X.ToString(culture), - // this.Y.ToString(culture), - // this.Z.ToString(culture) - //}); - } - #endregion + #region ToString + public override string ToString() + { + var culture = CultureInfo.CurrentCulture; + // This may look a bit more ugly, but String.Format should + // be avoided cause of it's bad performance! + return "{X:" + X.ToString(culture) + + " Y:" + Y.ToString(culture) + + " Z:" + Z.ToString(culture) + "}"; + } + #endregion #endregion @@ -745,8 +730,8 @@ namespace ANX.Framework public bool Equals(Vector3 other) { return this.X == other.X && - this.Y == other.Y && - this.Z == other.Z; + this.Y == other.Y && + this.Z == other.Z; } #endregion diff --git a/ANX.Framework/Vector4.cs b/ANX.Framework/Vector4.cs index 334e630f..017cb49a 100644 --- a/ANX.Framework/Vector4.cs +++ b/ANX.Framework/Vector4.cs @@ -433,7 +433,26 @@ namespace ANX.Framework public static void Transform(ref Vector2 value, ref Quaternion rotation, out Vector4 result) { - throw new NotImplementedException(); + float twoX = rotation.X + rotation.X; + float twoY = rotation.Y + rotation.Y; + float twoZ = rotation.Z + rotation.Z; + + float twoXX = twoX * rotation.X; + float twoXY = twoX * rotation.Y; + float twoXZ = twoX * rotation.Z; + float twoXW = twoX * rotation.W; + + float twoYY = twoY * rotation.Y; + float twoYZ = twoY * rotation.Z; + float twoYW = twoY * rotation.W; + + float twoZZ = twoZ * rotation.Z; + float twoZW = twoZ * rotation.W; + + result.X = value.X * (1.0f - twoYY - twoZZ) + value.Y * (twoXY - twoZW); + result.Y = value.X * (twoXY + twoZW) + value.Y * (1.0f - twoXX - twoZZ); + result.Z = value.X * (twoXZ - twoYW) + value.Y * (twoYZ + twoXW); + result.W = 1.0f; } public static Vector4 Transform(Vector3 position, Matrix matrix) @@ -460,7 +479,26 @@ namespace ANX.Framework public static void Transform(ref Vector3 value, ref Quaternion rotation, out Vector4 result) { - throw new NotImplementedException(); + float twoX = rotation.X + rotation.X; + float twoY = rotation.Y + rotation.Y; + float twoZ = rotation.Z + rotation.Z; + + float twoXX = twoX * rotation.X; + float twoXY = twoX * rotation.Y; + float twoXZ = twoX * rotation.Z; + float twoXW = twoX * rotation.W; + + float twoYY = twoY * rotation.Y; + float twoYZ = twoY * rotation.Z; + float twoYW = twoY * rotation.W; + + float twoZZ = twoZ * rotation.Z; + float twoZW = twoZ * rotation.W; + + result.X = value.X * (1.0f - twoYY - twoZZ) + value.Y * (twoXY - twoZW) + value.Z * (twoXZ + twoYW); + result.Y = value.X * (twoXY + twoZW) + value.Y * (1.0f - twoXX - twoZZ) + value.Z * (twoYZ - twoXW); + result.Z = value.X * (twoXZ - twoYW) + value.Y * (twoYZ + twoXW) + value.Z * (1.0f - twoXX - twoYY); + result.W = 1.0f; } public static Vector4 Transform(Vector4 vector, Matrix matrix) @@ -487,13 +525,25 @@ namespace ANX.Framework public static void Transform(ref Vector4 value, ref Quaternion rotation, out Vector4 result) { - float x = 2 * (rotation.Y * value.Z - rotation.Z * value.Y); - float y = 2 * (rotation.Z * value.X - rotation.X * value.Z); - float z = 2 * (rotation.X * value.Y - rotation.Y * value.X); + float twoX = rotation.X + rotation.X; + float twoY = rotation.Y + rotation.Y; + float twoZ = rotation.Z + rotation.Z; - result.X = value.X + x * rotation.W + (rotation.Y * z - rotation.Z * y); - result.Y = value.Y + y * rotation.W + (rotation.Z * x - rotation.X * z); - result.Z = value.Z + z * rotation.W + (rotation.X * y - rotation.Y * x); + float twoXX = twoX * rotation.X; + float twoXY = twoX * rotation.Y; + float twoXZ = twoX * rotation.Z; + float twoXW = twoX * rotation.W; + + float twoYY = twoY * rotation.Y; + float twoYZ = twoY * rotation.Z; + float twoYW = twoY * rotation.W; + + float twoZZ = twoZ * rotation.Z; + float twoZW = twoZ * rotation.W; + + result.X = value.X * (1.0f - twoYY - twoZZ) + value.Y * (twoXY - twoZW) + value.Z * (twoXZ + twoYW); + result.Y = value.X * (twoXY + twoZW) + value.Y * (1.0f - twoXX - twoZZ) + value.Z * (twoYZ - twoXW); + result.Z = value.X * (twoXZ - twoYW) + value.Y * (twoYZ + twoXW) + value.Z * (1.0f - twoXX - twoYY); result.W = value.W; } @@ -541,22 +591,22 @@ namespace ANX.Framework } public override string ToString() - { - var culture = CultureInfo.CurrentCulture; - // This may look a bit more ugly, but String.Format should - // be avoided cause of it's bad performance! - return "{X:" + X.ToString(culture) + - " Y:" + Y.ToString(culture) + - " Z:" + Z.ToString(culture) + - " W:" + W.ToString(culture) + "}"; + { + var culture = CultureInfo.CurrentCulture; + // This may look a bit more ugly, but String.Format should + // be avoided cause of it's bad performance! + return "{X:" + X.ToString(culture) + + " Y:" + Y.ToString(culture) + + " Z:" + Z.ToString(culture) + + " W:" + W.ToString(culture) + "}"; - //return string.Format(culture, "{{X:{0} Y:{1} Z:{2} W:{3}}}", new object[] - //{ - // this.X.ToString(culture), - // this.Y.ToString(culture), - // this.Z.ToString(culture), - // this.W.ToString(culture) - //}); + //return string.Format(culture, "{{X:{0} Y:{1} Z:{2} W:{3}}}", new object[] + //{ + // this.X.ToString(culture), + // this.Y.ToString(culture), + // this.Z.ToString(culture), + // this.W.ToString(culture) + //}); } public float Length()