From 969d66e9f127856d157af3efc2fd3884d87da228 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Tue, 9 Dec 2008 20:45:18 +0000 Subject: [PATCH] Added OpenEXR terms to the License. Added Half and Vector[234]h structs. Added Vector[234]d, Matrix4d and Quaterniond structs. --- Documentation/License.txt | 31 +- Source/OpenTK/Math/Half.cs | 588 ++++++ Source/OpenTK/Math/Matrix3d.cs | 59 +- Source/OpenTK/Math/Matrix4.cs | 1008 +++++----- Source/OpenTK/Math/Matrix4d.cs | 2906 ++++++----------------------- Source/OpenTK/Math/Quaternion.cs | 599 +++--- Source/OpenTK/Math/Quaterniond.cs | Bin 48728 -> 77476 bytes Source/OpenTK/Math/Vector2.cs | 783 ++++---- Source/OpenTK/Math/Vector2d.cs | Bin 63870 -> 38062 bytes Source/OpenTK/Math/Vector2h.cs | 337 ++++ Source/OpenTK/Math/Vector3.cs | 914 ++++----- Source/OpenTK/Math/Vector3d.cs | Bin 91336 -> 58538 bytes Source/OpenTK/Math/Vector3h.cs | 370 ++++ Source/OpenTK/Math/Vector4.cs | 796 ++++---- Source/OpenTK/Math/Vector4d.cs | 1832 +++++++----------- Source/OpenTK/Math/Vector4h.cs | 397 ++++ 16 files changed, 5124 insertions(+), 5496 deletions(-) create mode 100644 Source/OpenTK/Math/Half.cs create mode 100644 Source/OpenTK/Math/Vector2h.cs create mode 100644 Source/OpenTK/Math/Vector3h.cs create mode 100644 Source/OpenTK/Math/Vector4h.cs diff --git a/Documentation/License.txt b/Documentation/License.txt index 108b5a66..0e18e31a 100644 --- a/Documentation/License.txt +++ b/Documentation/License.txt @@ -1,6 +1,6 @@ -The Open Toolkit Library License +The Open Toolkit library license -Copyright (c) 2006 - 2008 the Open Toolkit library, except where noted. +Copyright (c) 2006 - 2008 The Open Toolkit library. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -10,4 +10,29 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI -The Open Toolkit library contains code from the Mono class library. This code is distributed under the terms of the MIT/X11 license (displayed above) and is copyright (c) 2004 Novell, Inc. + +Third parties + + +The Open Toolkit library includes portions of the Mono class library, which are covered by the following license: + +Copyright (c) 2004 Novell, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + + +Half-to-Single and Single-to-Half conversions are covered by the following license: + +Copyright (c) 2002, Industrial Light & Magic, a division of Lucas Digital Ltd. LLC. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +* Neither the name of Industrial Light & Magic nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/Source/OpenTK/Math/Half.cs b/Source/OpenTK/Math/Half.cs new file mode 100644 index 00000000..e75a6770 --- /dev/null +++ b/Source/OpenTK/Math/Half.cs @@ -0,0 +1,588 @@ +#region --- License --- +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +/* +The conversion functions are derived from OpenEXR's implementation and are +governed by the following license: + +Copyright (c) 2002, Industrial Light & Magic, a division of Lucas +Digital Ltd. LLC + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: +* Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. +* Neither the name of Industrial Light & Magic nor the names of +its contributors may be used to endorse or promote products derived +from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#endregion --- License --- + +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; + +namespace OpenTK.Math +{ + + /// + /// The name Half is derived from half-precision floating-point number. + /// It occupies only 16 Bits, which are split into 1 Sign bit, 5 Exponent bits and 10 Mantissa bits. + /// + /// + /// Quote from ARB_half_float_pixel specification: + /// Any representable 16-bit floating-point value is legal as input to a GL command that accepts 16-bit floating-point data. The + /// result of providing a value that is not a floating-point number (such as infinity or NaN) to such a command is unspecified, + /// but must not lead to GL interruption or termination. Providing a denormalized number or negative zero to GL must yield + /// predictable results. + /// + [Serializable, StructLayout(LayoutKind.Sequential)] + public struct Half : ISerializable, IComparable, IFormattable, IEquatable + { + #region Internal Field + + UInt16 bits; + + #endregion Internal Field + + #region Properties + + /// Returns true if the Half is zero. + public bool IsZero { get { return (bits == 0) || (bits == 0x8000); } } + + /// Returns true if the Half represents Not A Number (NaN) + public bool IsNaN { get { return (((bits & 0x7C00) == 0x7C00) && (bits & 0x03FF) != 0x0000); } } + + /// Returns true if the Half represents positive infinity. + public bool IsPositiveInfinity { get { return (bits == 31744); } } + + /// Returns true if the Half represents negative infinity. + public bool IsNegativeInfinity { get { return (bits == 64512); } } + + #endregion Properties + + #region Constructors + + /// + /// The new Half instance will convert the parameter into 16-Bit Half precision floating point. + /// + /// 32-Bit Single precision floating point number. + public Half(Single f) + : this() + { + unsafe + { + bits = SingleToHalf(*(int*)&f); + } + } + + /// + /// The new Half instance will convert the parameter into 16-Bit Half precision floating point. + /// + /// 32-Bit Single precision floating point number. + /// Enable checks that will throw if the conversion result is not meaningful. + public Half(Single f, bool throwOnError) + : this(f) + { + if (throwOnError) + { + // handle cases that cause overflow rather than silently ignoring it + if (f > Half.MaxValue) throw new ArithmeticException("Half: Positive maximum value exceeded."); + if (f < -Half.MaxValue) throw new ArithmeticException("Half: Negative minimum value exceeded."); + + // handle cases that make no sense + if (Single.IsNaN(f)) throw new ArithmeticException("Half: input is Not a Number (NaN)."); + if (Single.IsPositiveInfinity(f)) throw new ArithmeticException("Half: input is +infinity."); + if (Single.IsNegativeInfinity(f)) throw new ArithmeticException("Half: input is -infinity."); + } + } + + /// + /// The new Half instance will convert the parameter into 16-Bit Half precision floating point. + /// + /// 64-Bit Double precision floating point number. + public Half(Double d) : this((Single)d) { } + + /// + /// The new Half instance will convert the parameter into 16-Bit Half precision floating point. + /// + /// 64-Bit Double precision floating point number. + /// Enable checks that will throw if the conversion result is not meaningful. + public Half(Double d, bool throwOnError) : this((Single)d, throwOnError) { } + + #endregion Constructors + + #region Single -> Half + + /// Ported from OpenEXR's IlmBase 1.0.1 + private UInt16 SingleToHalf(Int32 si32) + { + // Our floating point number, F, is represented by the bit pattern in integer i. + // Disassemble that bit pattern into the sign, S, the exponent, E, and the significand, M. + // Shift S into the position where it will go in in the resulting half number. + // Adjust E, accounting for the different exponent bias of float and half (127 versus 15). + + Int32 sign = (si32 >> 16) & 0x00008000; + Int32 exponent = ((si32 >> 23) & 0x000000ff) - (127 - 15); + Int32 mantissa = si32 & 0x007fffff; + + // Now reassemble S, E and M into a half: + + if (exponent <= 0) + { + if (exponent < -10) + { + // E is less than -10. The absolute value of F is less than Half.MinValue + // (F may be a small normalized float, a denormalized float or a zero). + // + // We convert F to a half zero with the same sign as F. + + return (UInt16)sign; + } + + // E is between -10 and 0. F is a normalized float whose magnitude is less than Half.MinNormalizedValue. + // + // We convert F to a denormalized half. + + // Add an explicit leading 1 to the significand. + + mantissa = mantissa | 0x00800000; + + // Round to M to the nearest (10+E)-bit value (with E between -10 and 0); in case of a tie, round to the nearest even value. + // + // Rounding may cause the significand to overflow and make our number normalized. Because of the way a half's bits + // are laid out, we don't have to treat this case separately; the code below will handle it correctly. + + Int32 t = 14 - exponent; + Int32 a = (1 << (t - 1)) - 1; + Int32 b = (mantissa >> t) & 1; + + mantissa = (mantissa + a + b) >> t; + + // Assemble the half from S, E (==zero) and M. + + return (UInt16)(sign | mantissa); + } + else if (exponent == 0xff - (127 - 15)) + { + if (mantissa == 0) + { + // F is an infinity; convert F to a half infinity with the same sign as F. + + return (UInt16)(sign | 0x7c00); + } + else + { + // F is a NAN; we produce a half NAN that preserves the sign bit and the 10 leftmost bits of the + // significand of F, with one exception: If the 10 leftmost bits are all zero, the NAN would turn + // into an infinity, so we have to set at least one bit in the significand. + + mantissa >>= 13; + return (UInt16)(sign | 0x7c00 | mantissa | ((mantissa == 0) ? 1 : 0)); + } + } + else + { + // E is greater than zero. F is a normalized float. We try to convert F to a normalized half. + + // Round to M to the nearest 10-bit value. In case of a tie, round to the nearest even value. + + mantissa = mantissa + 0x00000fff + ((mantissa >> 13) & 1); + + if ((mantissa & 0x00800000) == 1) + { + mantissa = 0; // overflow in significand, + exponent += 1; // adjust exponent + } + + // exponent overflow + if (exponent > 30) throw new ArithmeticException("Half: hardware floating point overflow."); + + // Assemble the half from S, E and M. + + return (UInt16)(sign | (exponent << 10) | (mantissa >> 13)); + } + } + + #endregion Single -> Half + + #region Half -> Single + + /// Converts the 16-Bit half to 32-Bit floating point. + /// A Single precision floating point Number. + public Single ToSingle() + { + int i = HalfToFloat(bits); + + unsafe + { + return *(float*)&i; + } + } + + /// Ported from OpenEXR's IlmBase 1.0.1 + private Int32 HalfToFloat(UInt16 ui16) + { + + Int32 sign = (ui16 >> 15) & 0x00000001; + Int32 exponent = (ui16 >> 10) & 0x0000001f; + Int32 mantissa = ui16 & 0x000003ff; + + if (exponent == 0) + { + if (mantissa == 0) + { + // Plus or minus zero + + return sign << 31; + } + else + { + // Denormalized number -- renormalize it + + while ((mantissa & 0x00000400) == 0) + { + mantissa <<= 1; + exponent -= 1; + } + + exponent += 1; + mantissa &= ~0x00000400; + } + } + else if (exponent == 31) + { + if (mantissa == 0) + { + // Positive or negative infinity + + return (sign << 31) | 0x7f800000; + } + else + { + // Nan -- preserve sign and significand bits + + return (sign << 31) | 0x7f800000 | (mantissa << 13); + } + } + + // Normalized number + + exponent = exponent + (127 - 15); + mantissa = mantissa << 13; + + // Assemble S, E and M. + + return (sign << 31) | (exponent << 23) | mantissa; + } + + #endregion Half -> Single + + #region Conversions + + /// + /// Converts a System.Single to a OpenTK.Math.Half. + /// + /// The value to convert. + /// A + /// + /// The result of the conversion. + /// A + /// + public static explicit operator Half(float f) + { + return new Half(f); + } + + /// + /// Converts a System.Double to a OpenTK.Math.Half. + /// + /// The value to convert. + /// A + /// + /// The result of the conversion. + /// A + /// + public static explicit operator Half(double d) + { + return new Half(d); + } + + /// + /// Converts a OpenTK.Math.Half to a System.Single. + /// + /// The value to convert. + /// A + /// + /// The result of the conversion. + /// A + /// + public static implicit operator float(Half h) + { + return h.ToSingle(); + } + + /// + /// Converts a OpenTK.Math.Half to a System.Double. + /// + /// The value to convert. + /// A + /// + /// The result of the conversion. + /// A + /// + public static implicit operator double(Half h) + { + return (double)h.ToSingle(); + } + + #endregion Conversions + + #region Constants + + /// The size in bytes for an instance of the Half struct. + public static readonly Int32 SizeInBytes = 2; + + /// Smallest positive half + public static readonly Single MinValue = 5.96046448e-08f; + + /// Smallest positive normalized half + public static readonly Single MinNormalizedValue = 6.10351562e-05f; + + /// Largest positive half + public static readonly Single MaxValue = 65504.0f; + + /// Smallest positive e for which half (1.0 + e) != half (1.0) + public static readonly Single Epsilon = 0.00097656f; + + #endregion Constants + + #region ISerializable + + /// Constructor used by ISerializable to deserialize the object. + /// + /// + public Half(SerializationInfo info, StreamingContext context) + { + this.bits = (ushort)info.GetValue("bits", typeof(ushort)); + } + + /// Used by ISerialize to serialize the object. + /// + /// + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue("bits", this.bits); + } + + #endregion ISerializable + + #region Binary dump + + /// Updates the Half by reading from a Stream. + /// A BinaryReader instance associated with an open Stream. + public void FromBinaryStream(BinaryReader bin) + { + this.bits = bin.ReadUInt16(); + + } + + /// Writes the Half into a Stream. + /// A BinaryWriter instance associated with an open Stream. + public void ToBinaryStream(BinaryWriter bin) + { + bin.Write(this.bits); + } + + #endregion Binary dump + + #region IEquatable Members + + const int maxUlps = 1; + + /// + /// Returns a value indicating whether this instance is equal to a specified OpenTK.Math.Half value. + /// + /// OpenTK.Math.Half object to compare to this instance.. + /// True, if other is equal to this instance; false otherwise. + public bool Equals(Half other) + { + short aInt, bInt; + unchecked { aInt = (short)other.bits; } + unchecked { bInt = (short)this.bits; } + + // Make aInt lexicographically ordered as a twos-complement int + if (aInt < 0) + aInt = (short)(0x8000 - aInt); + + // Make bInt lexicographically ordered as a twos-complement int + if (bInt < 0) + bInt = (short)(0x8000 - bInt); + + short intDiff = System.Math.Abs((short)(aInt - bInt)); + + if (intDiff <= maxUlps) + return true; + + return false; + } + + #endregion + + #region IComparable Members + + /// + /// Compares this instance to a specified half-precision floating-point number + /// and returns an integer that indicates whether the value of this instance + /// is less than, equal to, or greater than the value of the specified half-precision + /// floating-point number. + /// + /// A half-precision floating-point number to compare. + /// + /// A signed number indicating the relative values of this instance and value. If the number is: + /// Less than zero, then this instance is less than other, or this instance is not a number + /// (OpenTK.Math.Half.NaN) and other is a number. + /// Zero: this instance is equal to value, or both this instance and other + /// are not a number (OpenTK.Math.Half.NaN), OpenTK.Math.Half.PositiveInfinity, or + /// OpenTK.Math.Half.NegativeInfinity. + /// Greater than zero: this instance is greater than othrs, or this instance is a number + /// and other is not a number (OpenTK.Math.Half.NaN). + /// + public int CompareTo(Half other) + { + return ((float)this).CompareTo((float)other); + } + + #endregion IComparable Members + + #region IFormattable Members + + /// Converts this Half into a human-legible string representation. + /// The string representation of this instance. + public override string ToString() + { + return this.ToSingle().ToString(); + } + + /// Converts this Half into a human-legible string representation. + /// formatting for the output string. + /// Culture-specific formatting information. + /// The string representation of this instance. + public string ToString(string format, IFormatProvider formatProvider) + { + return this.ToSingle().ToString(format, formatProvider); + } + + #endregion IFormattable Members + + #region String -> Half + + /// Converts the string representation of a number to a Half precision floating point equivalent. + /// string representation of the number to convert. + /// A new Half instance. + public static Half Parse(string s) + { + return (Half)Single.Parse(s); + } + + /// Converts the string representation of a number to a Half precision floating point equivalent. + /// string representation of the number to convert. + /// specifies the format of s. + /// Culture-specific formatting information. + /// A new Half instance. + public static Half Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider) + { + return (Half)Single.Parse(s, style, provider); + } + + /// Converts the string representation of a number to a Half precision floating point equivalent. Returns success. + /// string representation of the number to convert. + /// The Half instance to write to. + /// Success. + public static bool TryParse(string s, out Half result) + { + float f; + bool b = Single.TryParse(s, out f); + result = (Half)f; + return b; + } + + /// Converts the string representation of a number to a Half precision floating point equivalent. Returns success. + /// string representation of the number to convert. + /// specifies the format of s. + /// Culture-specific formatting information. + /// The Half instance to write to. + /// Success. + public static bool TryParse(string s, System.Globalization.NumberStyles style, IFormatProvider provider, out Half result) + { + float f; + bool b = Single.TryParse(s, style, provider, out f); + result = (Half)f; + return b; + } + + #endregion String -> Half + + #region BitConverter + + /// Returns the Half as an array of bytes. + /// The Half to convert. + /// The input as byte array. + public static byte[] GetBytes(Half h) + { + return BitConverter.GetBytes(h.bits); + } + + /// Converts an array of bytes into Half. + /// A Half in it's byte[] representation. + /// The starting position within value. + /// A new Half instance. + public static Half FromBytes(byte[] value, int startIndex) + { + Half h; + h.bits = BitConverter.ToUInt16(value, startIndex); + return h; + } + + #endregion BitConverter + } +} \ No newline at end of file diff --git a/Source/OpenTK/Math/Matrix3d.cs b/Source/OpenTK/Math/Matrix3d.cs index e142d853..3ab944b9 100644 --- a/Source/OpenTK/Math/Matrix3d.cs +++ b/Source/OpenTK/Math/Matrix3d.cs @@ -1,10 +1,24 @@ #region --- License --- -/* Licensed under the MIT/X11 license. - * Copyright (c) 2006-2008 the OpenTK Team. - * This notice may not be removed from any source distribution. - * See license.txt for licensing detailed licensing details. - * - * Contributions by James Talton. +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ #endregion @@ -15,10 +29,10 @@ namespace OpenTK.Math { // Todo: Remove this warning when the code goes public. #pragma warning disable 3019 - +#if false [Serializable] [StructLayout(LayoutKind.Sequential)] - internal struct Matrix3d : IEquatable + public struct Matrix3d : IEquatable { #region Fields & Access @@ -275,20 +289,19 @@ namespace OpenTK.Math /// Constructs left matrix from the given quaternion. /// The quaternion to use to construct the martix. - public Matrix3d(ref Quaterniond quaternion) + public Matrix3d(Quaterniond quaternion) { - Quaterniond quaternionNormalized; - quaternion.Normalize(out quaternionNormalized); + quaternion.Normalize(); - double xx = quaternionNormalized.X * quaternionNormalized.X; - double yy = quaternionNormalized.Y * quaternionNormalized.Y; - double zz = quaternionNormalized.Z * quaternionNormalized.Z; - double xy = quaternionNormalized.X * quaternionNormalized.Y; - double xz = quaternionNormalized.X * quaternionNormalized.Z; - double yz = quaternionNormalized.Y * quaternionNormalized.Z; - double wx = quaternionNormalized.W * quaternionNormalized.X; - double wy = quaternionNormalized.W * quaternionNormalized.Y; - double wz = quaternionNormalized.W * quaternionNormalized.Z; + double xx = quaternion.X * quaternion.X; + double yy = quaternion.Y * quaternion.Y; + double zz = quaternion.Z * quaternion.Z; + double xy = quaternion.X * quaternion.Y; + double xz = quaternion.X * quaternion.Z; + double yz = quaternion.Y * quaternion.Z; + double wx = quaternion.W * quaternion.X; + double wy = quaternion.W * quaternion.Y; + double wz = quaternion.W * quaternion.Z; R0C0 = 1 - 2 * (yy + zz); R0C1 = 2 * (xy - wz); @@ -752,9 +765,9 @@ namespace OpenTK.Math result.R2C2 = 1; } - public void Quaternion(out Quaterniond quaternion) + public Quaterniond ToQuaternion() { - quaternion = new Quaterniond(ref this); + //return new Quaterniond(ref this); } #endregion @@ -810,6 +823,6 @@ namespace OpenTK.Math #endregion } - +#endif #pragma warning restore 3019 } diff --git a/Source/OpenTK/Math/Matrix4.cs b/Source/OpenTK/Math/Matrix4.cs index ca56a0fa..e25a791d 100644 --- a/Source/OpenTK/Math/Matrix4.cs +++ b/Source/OpenTK/Math/Matrix4.cs @@ -1,14 +1,28 @@ #region --- License --- -/* Copyright (c) 2006, 2007 the OpenTK team - * See license.txt for license info - * - * Implemented by Andy Gill +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ #endregion using System; -using System.Collections.Generic; -using System.Text; using System.Runtime.InteropServices; namespace OpenTK.Math @@ -65,93 +79,504 @@ namespace OpenTK.Math #endregion - #region Functions + #region Public Members - #region public void Invert() + #region Properties - public void Invert() - { - this = Matrix4.Invert(this); - } - - #endregion - - #region public void Transpose() - - public void Transpose() - { - this = Matrix4.Transpose(this); - } - - #endregion - - #endregion - - #region Properties - - /// - /// The determinant of this matrix - /// - public float Determinant - { - get - { - return + /// + /// The determinant of this matrix + /// + public float Determinant + { + get + { + return Row0.X * Row1.Y * Row2.Z * Row3.W - Row0.X * Row1.Y * Row2.W * Row3.Z + Row0.X * Row1.Z * Row2.W * Row3.Y - Row0.X * Row1.Z * Row2.Y * Row3.W - + Row0.X * Row1.W * Row2.Y * Row3.Z - Row0.X * Row1.W * Row2.Z * Row3.Y - Row0.Y * Row1.Z * Row2.W * Row3.X + Row0.Y * Row1.Z * Row2.X * Row3.W - - Row0.Y * Row1.W * Row2.X * Row3.Z + Row0.Y * Row1.W * Row2.Z * Row3.X - Row0.Y * Row1.X * Row2.Z * Row3.W + Row0.Y * Row1.X * Row2.W * Row3.Z - + Row0.Z * Row1.W * Row2.X * Row3.Y - Row0.Z * Row1.W * Row2.Y * Row3.X + Row0.Z * Row1.X * Row2.Y * Row3.W - Row0.Z * Row1.X * Row2.W * Row3.Y - + Row0.Z * Row1.Y * Row2.W * Row3.X - Row0.Z * Row1.Y * Row2.X * Row3.W - Row0.W * Row1.X * Row2.Y * Row3.Z + Row0.W * Row1.X * Row2.Z * Row3.Y - - Row0.W * Row1.Y * Row2.Z * Row3.X + Row0.W * Row1.Y * Row2.X * Row3.Z - Row0.W * Row1.Z * Row2.X * Row3.Y + Row0.W * Row1.Z * Row2.Y * Row3.X; - } - } + + Row0.X * Row1.W * Row2.Y * Row3.Z - Row0.X * Row1.W * Row2.Z * Row3.Y - Row0.Y * Row1.Z * Row2.W * Row3.X + Row0.Y * Row1.Z * Row2.X * Row3.W + - Row0.Y * Row1.W * Row2.X * Row3.Z + Row0.Y * Row1.W * Row2.Z * Row3.X - Row0.Y * Row1.X * Row2.Z * Row3.W + Row0.Y * Row1.X * Row2.W * Row3.Z + + Row0.Z * Row1.W * Row2.X * Row3.Y - Row0.Z * Row1.W * Row2.Y * Row3.X + Row0.Z * Row1.X * Row2.Y * Row3.W - Row0.Z * Row1.X * Row2.W * Row3.Y + + Row0.Z * Row1.Y * Row2.W * Row3.X - Row0.Z * Row1.Y * Row2.X * Row3.W - Row0.W * Row1.X * Row2.Y * Row3.Z + Row0.W * Row1.X * Row2.Z * Row3.Y + - Row0.W * Row1.Y * Row2.Z * Row3.X + Row0.W * Row1.Y * Row2.X * Row3.Z - Row0.W * Row1.Z * Row2.X * Row3.Y + Row0.W * Row1.Z * Row2.Y * Row3.X; + } + } - /// - /// The first column of this matrix - /// - public Vector4 Column0 - { - get { return new Vector4(Row0.X, Row1.X, Row2.X, Row3.X); } - } + /// + /// The first column of this matrix + /// + public Vector4 Column0 + { + get { return new Vector4(Row0.X, Row1.X, Row2.X, Row3.X); } + } - /// - /// The second column of this matrix - /// - public Vector4 Column1 - { - get { return new Vector4(Row0.Y, Row1.Y, Row2.Y, Row3.Y); } - } + /// + /// The second column of this matrix + /// + public Vector4 Column1 + { + get { return new Vector4(Row0.Y, Row1.Y, Row2.Y, Row3.Y); } + } - /// - /// The third column of this matrix - /// - public Vector4 Column2 - { - get { return new Vector4(Row0.Z, Row1.Z, Row2.Z, Row3.Z); } - } + /// + /// The third column of this matrix + /// + public Vector4 Column2 + { + get { return new Vector4(Row0.Z, Row1.Z, Row2.Z, Row3.Z); } + } - /// - /// The fourth column of this matrix - /// - public Vector4 Column3 - { - get { return new Vector4(Row0.W, Row1.W, Row2.W, Row3.W); } - } + /// + /// The fourth column of this matrix + /// + public Vector4 Column3 + { + get { return new Vector4(Row0.W, Row1.W, Row2.W, Row3.W); } + } - #endregion + #endregion - #region Operator overloads + #region Instance - /// - /// Matrix multiplication - /// - /// left-hand operand - /// right-hand operand - /// A new Matrix44 which holds the result of the multiplication - public static Matrix4 operator *(Matrix4 left, Matrix4 right) - { - return Matrix4.Mult(left, right); - } + #region public void Invert() + + public void Invert() + { + this = Matrix4.Invert(this); + } + + #endregion + + #region public void Transpose() + + public void Transpose() + { + this = Matrix4.Transpose(this); + } + + #endregion + + #endregion + + #region Static + + #region Scale Functions + + /// + /// Build a scaling matrix + /// + /// Single scale factor for x,y and z axes + /// A scaling matrix + public static Matrix4 Scale(float scale) + { + return Scale(scale, scale, scale); + } + + /// + /// Build a scaling matrix + /// + /// Scale factors for x,y and z axes + /// A scaling matrix + public static Matrix4 Scale(Vector3 scale) + { + return Scale(scale.X, scale.Y, scale.Z); + } + + /// + /// Build a scaling matrix + /// + /// Scale factor for x-axis + /// Scale factor for y-axis + /// Scale factor for z-axis + /// A scaling matrix + public static Matrix4 Scale(float x, float y, float z) + { + Matrix4 result; + result.Row0 = Vector4.UnitX * x; + result.Row1 = Vector4.UnitY * y; + result.Row2 = Vector4.UnitZ * z; + result.Row3 = Vector4.UnitW; + return result; + } + + #endregion + + #region Translation Functions + + /// + /// Build a translation matrix with the given translation + /// + /// The vector to translate along + /// A Translation matrix + public static Matrix4 Translation(Vector3 trans) + { + return Translation(trans.X, trans.Y, trans.Z); + } + + /// + /// Build a translation matrix with the given translation + /// + /// X translation + /// Y translation + /// Z translation + /// A Translation matrix + public static Matrix4 Translation(float x, float y, float z) + { + Matrix4 result = Identity; + result.Row3 = new Vector4(x, y, z, 1.0f); + return result; + } + + #endregion + + #region Rotation Functions + + /// + /// Build a rotation matrix that rotates about the x-axis + /// + /// angle in radians to rotate counter-clockwise around the x-axis + /// A rotation matrix + public static Matrix4 RotateX(float angle) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + Matrix4 result; + result.Row0 = Vector4.UnitX; + result.Row1 = new Vector4(0.0f, cos, sin, 0.0f); + result.Row2 = new Vector4(0.0f, -sin, cos, 0.0f); + result.Row3 = Vector4.UnitW; + return result; + } + + /// + /// Build a rotation matrix that rotates about the y-axis + /// + /// angle in radians to rotate counter-clockwise around the y-axis + /// A rotation matrix + public static Matrix4 RotateY(float angle) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + Matrix4 result; + result.Row0 = new Vector4(cos, 0.0f, -sin, 0.0f); + result.Row1 = Vector4.UnitY; + result.Row2 = new Vector4(sin, 0.0f, cos, 0.0f); + result.Row3 = Vector4.UnitW; + return result; + } + + /// + /// Build a rotation matrix that rotates about the z-axis + /// + /// angle in radians to rotate counter-clockwise around the z-axis + /// A rotation matrix + public static Matrix4 RotateZ(float angle) + { + float cos = (float)System.Math.Cos(angle); + float sin = (float)System.Math.Sin(angle); + + Matrix4 result; + result.Row0 = new Vector4(cos, sin, 0.0f, 0.0f); + result.Row1 = new Vector4(-sin, cos, 0.0f, 0.0f); + result.Row2 = Vector4.UnitZ; + result.Row3 = Vector4.UnitW; + return result; + } + + /// + /// Build a rotation matrix to rotate about the given axis + /// + /// the axis to rotate about + /// angle in radians to rotate counter-clockwise (looking in the direction of the given axis) + /// A rotation matrix + public static Matrix4 Rotate(Vector3 axis, float angle) + { + float cos = (float)System.Math.Cos(-angle); + float sin = (float)System.Math.Sin(-angle); + float t = 1.0f - cos; + + axis.Normalize(); + + Matrix4 result; + result.Row0 = new Vector4(t * axis.X * axis.X + cos, t * axis.X * axis.Y - sin * axis.Z, t * axis.X * axis.Z + sin * axis.Y, 0.0f); + result.Row1 = new Vector4(t * axis.X * axis.Y + sin * axis.Z, t * axis.Y * axis.Y + cos, t * axis.Y * axis.Z - sin * axis.X, 0.0f); + result.Row2 = new Vector4(t * axis.X * axis.Z - sin * axis.Y, t * axis.Y * axis.Z + sin * axis.X, t * axis.Z * axis.Z + cos, 0.0f); + result.Row3 = Vector4.UnitW; + return result; + } + + /// + /// Build a rotation matrix from a quaternion + /// + /// the quaternion + /// A rotation matrix + public static Matrix4 Rotate(Quaternion q) + { + Vector3 axis; + float angle; + q.ToAxisAngle(out axis, out angle); + return Rotate(axis, angle); + } + + #endregion + + #region Camera Helper Functions + + /// + /// Build a world space to camera space matrix + /// + /// Eye (camera) position in world space + /// Target position in world space + /// Up vector in world space (should not be parallel to the camera direction, that is target - eye) + /// A Matrix that transforms world space to camera space + public static Matrix4 LookAt(Vector3 eye, Vector3 target, Vector3 up) + { + Vector3 z = Vector3.Normalize(eye - target); + Vector3 x = Vector3.Normalize(Vector3.Cross(up, z)); + Vector3 y = Vector3.Normalize(Vector3.Cross(z, x)); + + Matrix4 rot = new Matrix4(new Vector4(x.X, y.X, z.X, 0.0f), + new Vector4(x.Y, y.Y, z.Y, 0.0f), + new Vector4(x.Z, y.Z, z.Z, 0.0f), + Vector4.UnitW); + + Matrix4 trans = Matrix4.Translation(-eye); + + return trans * rot; + } + + /// + /// Build a projection matrix + /// + /// Left edge of the view frustum + /// Right edge of the view frustum + /// Bottom edge of the view frustum + /// Top edge of the view frustum + /// Distance to the near clip plane + /// Distance to the far clip plane + /// A projection matrix that transforms camera space to raster space + public static Matrix4 Frustum(float left, float right, float bottom, float top, float near, float far) + { + float invRL = 1.0f / (right - left); + float invTB = 1.0f / (top - bottom); + float invFN = 1.0f / (far - near); + return new Matrix4(new Vector4(2.0f * near * invRL, 0.0f, 0.0f, 0.0f), + new Vector4(0.0f, 2.0f * near * invTB, 0.0f, 0.0f), + new Vector4((right + left) * invRL, (top + bottom) * invTB, -(far + near) * invFN, -1.0f), + new Vector4(0.0f, 0.0f, -2.0f * far * near * invFN, 0.0f)); + } + + /// + /// Build a projection matrix + /// + /// Angle of the field of view in the y direction (in radians) + /// Aspect ratio of the view (width / height) + /// Distance to the near clip plane + /// Distance to the far clip plane + /// A projection matrix that transforms camera space to raster space + public static Matrix4 Perspective(float fovy, float aspect, float near, float far) + { + float yMax = near * (float)System.Math.Tan(0.5f * fovy); + float yMin = -yMax; + float xMin = yMin * aspect; + float xMax = yMax * aspect; + + return Frustum(xMin, xMax, yMin, yMax, near, far); + } + + #endregion + + #region Multiply Functions + + /// + /// Post multiply this matrix by another matrix + /// + /// The matrix to multiply + /// A new Matrix44 that is the result of the multiplication + public static Matrix4 Mult(Matrix4 left, Matrix4 right) + { + Vector4 col0 = right.Column0; + Vector4 col1 = right.Column1; + Vector4 col2 = right.Column2; + Vector4 col3 = right.Column3; + + left.Row0 = new Vector4(Vector4.Dot(left.Row0, col0), Vector4.Dot(left.Row0, col1), Vector4.Dot(left.Row0, col2), Vector4.Dot(left.Row0, col3)); + left.Row1 = new Vector4(Vector4.Dot(left.Row1, col0), Vector4.Dot(left.Row1, col1), Vector4.Dot(left.Row1, col2), Vector4.Dot(left.Row1, col3)); + left.Row2 = new Vector4(Vector4.Dot(left.Row2, col0), Vector4.Dot(left.Row2, col1), Vector4.Dot(left.Row2, col2), Vector4.Dot(left.Row2, col3)); + left.Row3 = new Vector4(Vector4.Dot(left.Row3, col0), Vector4.Dot(left.Row3, col1), Vector4.Dot(left.Row3, col2), Vector4.Dot(left.Row3, col3)); + return left; + } + + public static void Mult(ref Matrix4 left, ref Matrix4 right, out Matrix4 result) + { + Vector4 col0 = right.Column0; + Vector4 col1 = right.Column1; + Vector4 col2 = right.Column2; + Vector4 col3 = right.Column3; + + result.Row0 = new Vector4(Vector4.Dot(left.Row0, col0), Vector4.Dot(left.Row0, col1), Vector4.Dot(left.Row0, col2), Vector4.Dot(left.Row0, col3)); + result.Row1 = new Vector4(Vector4.Dot(left.Row1, col0), Vector4.Dot(left.Row1, col1), Vector4.Dot(left.Row1, col2), Vector4.Dot(left.Row1, col3)); + result.Row2 = new Vector4(Vector4.Dot(left.Row2, col0), Vector4.Dot(left.Row2, col1), Vector4.Dot(left.Row2, col2), Vector4.Dot(left.Row2, col3)); + result.Row3 = new Vector4(Vector4.Dot(left.Row3, col0), Vector4.Dot(left.Row3, col1), Vector4.Dot(left.Row3, col2), Vector4.Dot(left.Row3, col3)); + } + + #endregion + + #region Invert Functions + + /// + /// Calculate the inverse of the given matrix + /// + /// The matrix to invert + /// The inverse of the given matrix if it has one, or the input if it is singular + /// Thrown if the Matrix4 is singular. + public static Matrix4 Invert(Matrix4 mat) + { + int[] colIdx = { 0, 0, 0, 0 }; + int[] rowIdx = { 0, 0, 0, 0 }; + int[] pivotIdx = { -1, -1, -1, -1 }; + + // convert the matrix to an array for easy looping + float[,] inverse = {{mat.Row0.X, mat.Row0.Y, mat.Row0.Z, mat.Row0.W}, + {mat.Row1.X, mat.Row1.Y, mat.Row1.Z, mat.Row1.W}, + {mat.Row2.X, mat.Row2.Y, mat.Row2.Z, mat.Row2.W}, + {mat.Row3.X, mat.Row3.Y, mat.Row3.Z, mat.Row3.W} }; + int icol = 0; + int irow = 0; + for (int i = 0; i < 4; i++) + { + // Find the largest pivot value + float maxPivot = 0.0f; + for (int j = 0; j < 4; j++) + { + if (pivotIdx[j] != 0) + { + for (int k = 0; k < 4; ++k) + { + if (pivotIdx[k] == -1) + { + float absVal = System.Math.Abs(inverse[j, k]); + if (absVal > maxPivot) + { + maxPivot = absVal; + irow = j; + icol = k; + } + } + else if (pivotIdx[k] > 0) + { + return mat; + } + } + } + } + + ++(pivotIdx[icol]); + + // Swap rows over so pivot is on diagonal + if (irow != icol) + { + for (int k = 0; k < 4; ++k) + { + float f = inverse[irow, k]; + inverse[irow, k] = inverse[icol, k]; + inverse[icol, k] = f; + } + } + + rowIdx[i] = irow; + colIdx[i] = icol; + + float pivot = inverse[icol, icol]; + // check for singular matrix + if (pivot == 0.0f) + { + throw new InvalidOperationException("Matrix is singular and cannot be inverted."); + //return mat; + } + + // Scale row so it has a unit diagonal + float oneOverPivot = 1.0f / pivot; + inverse[icol, icol] = 1.0f; + for (int k = 0; k < 4; ++k) + inverse[icol, k] *= oneOverPivot; + + // Do elimination of non-diagonal elements + for (int j = 0; j < 4; ++j) + { + // check this isn't on the diagonal + if (icol != j) + { + float f = inverse[j, icol]; + inverse[j, icol] = 0.0f; + for (int k = 0; k < 4; ++k) + inverse[j, k] -= inverse[icol, k] * f; + } + } + } + + for (int j = 3; j >= 0; --j) + { + int ir = rowIdx[j]; + int ic = colIdx[j]; + for (int k = 0; k < 4; ++k) + { + float f = inverse[k, ir]; + inverse[k, ir] = inverse[k, ic]; + inverse[k, ic] = f; + } + } + + mat.Row0 = new Vector4(inverse[0, 0], inverse[0, 1], inverse[0, 2], inverse[0, 3]); + mat.Row1 = new Vector4(inverse[1, 0], inverse[1, 1], inverse[1, 2], inverse[1, 3]); + mat.Row2 = new Vector4(inverse[2, 0], inverse[2, 1], inverse[2, 2], inverse[2, 3]); + mat.Row3 = new Vector4(inverse[3, 0], inverse[3, 1], inverse[3, 2], inverse[3, 3]); + return mat; + } + + #endregion + + #region Transpose + + /// + /// Calculate the transpose of the given matrix + /// + /// The matrix to transpose + /// The transpose of the given matrix + public static Matrix4 Transpose(Matrix4 mat) + { + return new Matrix4(mat.Column0, mat.Column1, mat.Column2, mat.Column3); + } + + + /// + /// Calculate the transpose of the given matrix + /// + /// The matrix to transpose + public static void Transpose(ref Matrix4 mat, out Matrix4 result) + { + result.Row0 = mat.Column0; + result.Row1 = mat.Column1; + result.Row2 = mat.Column2; + result.Row3 = mat.Column3; + } + + #endregion + + #endregion + + #region Operators + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix44 which holds the result of the multiplication + public static Matrix4 operator *(Matrix4 left, Matrix4 right) + { + return Matrix4.Mult(left, right); + } public static bool operator ==(Matrix4 left, Matrix4 right) { @@ -165,418 +590,11 @@ namespace OpenTK.Math #endregion - #region Static functions + #region Overrides - #region Scale Functions + #region public override string ToString() - /// - /// Build a scaling matrix - /// - /// Single scale factor for x,y and z axes - /// A scaling matrix - public static Matrix4 Scale(float scale) - { - return Scale(scale, scale, scale); - } - - /// - /// Build a scaling matrix - /// - /// Scale factors for x,y and z axes - /// A scaling matrix - public static Matrix4 Scale(Vector3 scale) - { - return Scale(scale.X, scale.Y, scale.Z); - } - - /// - /// Build a scaling matrix - /// - /// Scale factor for x-axis - /// Scale factor for y-axis - /// Scale factor for z-axis - /// A scaling matrix - public static Matrix4 Scale(float x, float y, float z) - { - Matrix4 result; - result.Row0 = Vector4.UnitX * x; - result.Row1 = Vector4.UnitY * y; - result.Row2 = Vector4.UnitZ * z; - result.Row3 = Vector4.UnitW; - return result; - } - - #endregion - - #region Translation Functions - - /// - /// Build a translation matrix with the given translation - /// - /// The vector to translate along - /// A Translation matrix - public static Matrix4 Translation(Vector3 trans) - { - return Translation(trans.X, trans.Y, trans.Z); - } - - /// - /// Build a translation matrix with the given translation - /// - /// X translation - /// Y translation - /// Z translation - /// A Translation matrix - public static Matrix4 Translation(float x, float y, float z) - { - Matrix4 result = Identity; - result.Row3 = new Vector4(x, y, z, 1.0f); - return result; - } - - #endregion - - #region Rotation Functions - - /// - /// Build a rotation matrix that rotates about the x-axis - /// - /// angle in radians to rotate counter-clockwise around the x-axis - /// A rotation matrix - public static Matrix4 RotateX(float angle) - { - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - - Matrix4 result; - result.Row0 = Vector4.UnitX; - result.Row1 = new Vector4(0.0f, cos, sin, 0.0f); - result.Row2 = new Vector4(0.0f, -sin, cos, 0.0f); - result.Row3 = Vector4.UnitW; - return result; - } - - /// - /// Build a rotation matrix that rotates about the y-axis - /// - /// angle in radians to rotate counter-clockwise around the y-axis - /// A rotation matrix - public static Matrix4 RotateY(float angle) - { - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - - Matrix4 result; - result.Row0 = new Vector4(cos, 0.0f, -sin, 0.0f); - result.Row1 = Vector4.UnitY; - result.Row2 = new Vector4(sin, 0.0f, cos, 0.0f); - result.Row3 = Vector4.UnitW; - return result; - } - - /// - /// Build a rotation matrix that rotates about the z-axis - /// - /// angle in radians to rotate counter-clockwise around the z-axis - /// A rotation matrix - public static Matrix4 RotateZ(float angle) - { - float cos = (float)System.Math.Cos(angle); - float sin = (float)System.Math.Sin(angle); - - Matrix4 result; - result.Row0 = new Vector4(cos, sin, 0.0f, 0.0f); - result.Row1 = new Vector4(-sin, cos, 0.0f, 0.0f); - result.Row2 = Vector4.UnitZ; - result.Row3 = Vector4.UnitW; - return result; - } - - /// - /// Build a rotation matrix to rotate about the given axis - /// - /// the axis to rotate about - /// angle in radians to rotate counter-clockwise (looking in the direction of the given axis) - /// A rotation matrix - public static Matrix4 Rotate(Vector3 axis, float angle) - { - float cos = (float)System.Math.Cos(-angle); - float sin = (float)System.Math.Sin(-angle); - float t = 1.0f - cos; - - axis.Normalize(); - - Matrix4 result; - result.Row0 = new Vector4(t * axis.X * axis.X + cos, t * axis.X * axis.Y - sin * axis.Z, t * axis.X * axis.Z + sin * axis.Y, 0.0f); - result.Row1 = new Vector4(t * axis.X * axis.Y + sin * axis.Z, t * axis.Y * axis.Y + cos, t * axis.Y * axis.Z - sin * axis.X, 0.0f); - result.Row2 = new Vector4(t * axis.X * axis.Z - sin * axis.Y, t * axis.Y * axis.Z + sin * axis.X, t * axis.Z * axis.Z + cos, 0.0f); - result.Row3 = Vector4.UnitW; - return result; - } - - /// - /// Build a rotation matrix from a quaternion - /// - /// the quaternion - /// A rotation matrix - public static Matrix4 Rotate(Quaternion q) - { - Vector3 axis; - float angle; - q.ToAxisAngle(out axis, out angle); - return Rotate(axis, angle); - } - - #endregion - - #region Camera Helper Functions - - /// - /// Build a world space to camera space matrix - /// - /// Eye (camera) position in world space - /// Target position in world space - /// Up vector in world space (should not be parallel to the camera direction, that is target - eye) - /// A Matrix that transforms world space to camera space - public static Matrix4 LookAt(Vector3 eye, Vector3 target, Vector3 up) - { - Vector3 z = Vector3.Normalize(eye - target); - Vector3 x = Vector3.Normalize(Vector3.Cross(up, z)); - Vector3 y = Vector3.Normalize(Vector3.Cross(z, x)); - - Matrix4 rot = new Matrix4(new Vector4(x.X, y.X, z.X, 0.0f), - new Vector4(x.Y, y.Y, z.Y, 0.0f), - new Vector4(x.Z, y.Z, z.Z, 0.0f), - Vector4.UnitW); - - Matrix4 trans = Matrix4.Translation(-eye); - - return trans * rot; - } - - /// - /// Build a projection matrix - /// - /// Left edge of the view frustum - /// Right edge of the view frustum - /// Bottom edge of the view frustum - /// Top edge of the view frustum - /// Distance to the near clip plane - /// Distance to the far clip plane - /// A projection matrix that transforms camera space to raster space - public static Matrix4 Frustum(float left, float right, float bottom, float top, float near, float far) - { - float invRL = 1.0f / (right - left); - float invTB = 1.0f / (top - bottom); - float invFN = 1.0f / (far - near); - return new Matrix4(new Vector4(2.0f * near * invRL, 0.0f, 0.0f, 0.0f), - new Vector4(0.0f, 2.0f * near * invTB, 0.0f, 0.0f), - new Vector4((right + left) * invRL, (top + bottom) * invTB, -(far + near) * invFN, -1.0f), - new Vector4(0.0f, 0.0f, -2.0f * far * near * invFN, 0.0f)); - } - - /// - /// Build a projection matrix - /// - /// Angle of the field of view in the y direction (in radians) - /// Aspect ratio of the view (width / height) - /// Distance to the near clip plane - /// Distance to the far clip plane - /// A projection matrix that transforms camera space to raster space - public static Matrix4 Perspective(float fovy, float aspect, float near, float far) - { - float yMax = near * (float)System.Math.Tan(0.5f * fovy); - float yMin = -yMax; - float xMin = yMin * aspect; - float xMax = yMax * aspect; - - return Frustum(xMin, xMax, yMin, yMax, near, far); - } - - #endregion - - #region Multiply Functions - - /// - /// Post multiply this matrix by another matrix - /// - /// The matrix to multiply - /// A new Matrix44 that is the result of the multiplication - public static Matrix4 Mult(Matrix4 left, Matrix4 right) - { - Vector4 col0 = right.Column0; - Vector4 col1 = right.Column1; - Vector4 col2 = right.Column2; - Vector4 col3 = right.Column3; - - left.Row0 = new Vector4(Vector4.Dot(left.Row0, col0), Vector4.Dot(left.Row0, col1), Vector4.Dot(left.Row0, col2), Vector4.Dot(left.Row0, col3)); - left.Row1 = new Vector4(Vector4.Dot(left.Row1, col0), Vector4.Dot(left.Row1, col1), Vector4.Dot(left.Row1, col2), Vector4.Dot(left.Row1, col3)); - left.Row2 = new Vector4(Vector4.Dot(left.Row2, col0), Vector4.Dot(left.Row2, col1), Vector4.Dot(left.Row2, col2), Vector4.Dot(left.Row2, col3)); - left.Row3 = new Vector4(Vector4.Dot(left.Row3, col0), Vector4.Dot(left.Row3, col1), Vector4.Dot(left.Row3, col2), Vector4.Dot(left.Row3, col3)); - return left; - } - - public static void Mult(ref Matrix4 left, ref Matrix4 right, out Matrix4 result) - { - Vector4 col0 = right.Column0; - Vector4 col1 = right.Column1; - Vector4 col2 = right.Column2; - Vector4 col3 = right.Column3; - - result.Row0 = new Vector4(Vector4.Dot(left.Row0, col0), Vector4.Dot(left.Row0, col1), Vector4.Dot(left.Row0, col2), Vector4.Dot(left.Row0, col3)); - result.Row1 = new Vector4(Vector4.Dot(left.Row1, col0), Vector4.Dot(left.Row1, col1), Vector4.Dot(left.Row1, col2), Vector4.Dot(left.Row1, col3)); - result.Row2 = new Vector4(Vector4.Dot(left.Row2, col0), Vector4.Dot(left.Row2, col1), Vector4.Dot(left.Row2, col2), Vector4.Dot(left.Row2, col3)); - result.Row3 = new Vector4(Vector4.Dot(left.Row3, col0), Vector4.Dot(left.Row3, col1), Vector4.Dot(left.Row3, col2), Vector4.Dot(left.Row3, col3)); - } - - #endregion - - #region Invert Functions - - /// - /// Calculate the inverse of the given matrix - /// - /// The matrix to invert - /// The inverse of the given matrix if it has one, or the input if it is singular - /// Thrown if the Matrix4 is singular. - public static Matrix4 Invert(Matrix4 mat) - { - int[] colIdx = { 0, 0, 0, 0 }; - int[] rowIdx = { 0, 0, 0, 0 }; - int[] pivotIdx = { -1, -1, -1, -1 }; - - // convert the matrix to an array for easy looping - float[,] inverse = {{mat.Row0.X, mat.Row0.Y, mat.Row0.Z, mat.Row0.W}, - {mat.Row1.X, mat.Row1.Y, mat.Row1.Z, mat.Row1.W}, - {mat.Row2.X, mat.Row2.Y, mat.Row2.Z, mat.Row2.W}, - {mat.Row3.X, mat.Row3.Y, mat.Row3.Z, mat.Row3.W} }; - int icol = 0; - int irow = 0; - for (int i = 0; i < 4; i++) - { - // Find the largest pivot value - float maxPivot = 0.0f; - for (int j = 0; j < 4; j++) - { - if (pivotIdx[j] != 0) - { - for (int k = 0; k < 4; ++k) - { - if (pivotIdx[k] == -1) - { - float absVal = System.Math.Abs(inverse[j, k]); - if (absVal > maxPivot) - { - maxPivot = absVal; - irow = j; - icol = k; - } - } - else if (pivotIdx[k] > 0) - { - return mat; - } - } - } - } - - ++(pivotIdx[icol]); - - // Swap rows over so pivot is on diagonal - if (irow != icol) - { - for (int k = 0; k < 4; ++k) - { - float f = inverse[irow, k]; - inverse[irow, k] = inverse[icol, k]; - inverse[icol, k] = f; - } - } - - rowIdx[i] = irow; - colIdx[i] = icol; - - float pivot = inverse[icol, icol]; - // check for singular matrix - if (pivot == 0.0f) - { - throw new InvalidOperationException("Matrix is singular and cannot be inverted."); - //return mat; - } - - // Scale row so it has a unit diagonal - float oneOverPivot = 1.0f / pivot; - inverse[icol, icol] = 1.0f; - for (int k = 0; k < 4; ++k) - inverse[icol, k] *= oneOverPivot; - - // Do elimination of non-diagonal elements - for (int j = 0; j < 4; ++j) - { - // check this isn't on the diagonal - if (icol != j) - { - float f = inverse[j, icol]; - inverse[j, icol] = 0.0f; - for (int k = 0; k < 4; ++k) - inverse[j, k] -= inverse[icol, k] * f; - } - } - } - - for (int j = 3; j >= 0; --j) - { - int ir = rowIdx[j]; - int ic = colIdx[j]; - for (int k = 0; k < 4; ++k) - { - float f = inverse[k, ir]; - inverse[k, ir] = inverse[k, ic]; - inverse[k, ic] = f; - } - } - - mat.Row0 = new Vector4(inverse[0, 0], inverse[0, 1], inverse[0, 2], inverse[0, 3]); - mat.Row1 = new Vector4(inverse[1, 0], inverse[1, 1], inverse[1, 2], inverse[1, 3]); - mat.Row2 = new Vector4(inverse[2, 0], inverse[2, 1], inverse[2, 2], inverse[2, 3]); - mat.Row3 = new Vector4(inverse[3, 0], inverse[3, 1], inverse[3, 2], inverse[3, 3]); - return mat; - } - - #endregion - - #region Transpose - - /// - /// Calculate the transpose of the given matrix - /// - /// The matrix to transpose - /// The transpose of the given matrix - public static Matrix4 Transpose(Matrix4 mat) - { - return new Matrix4(mat.Column0, mat.Column1, mat.Column2, mat.Column3); - } - - - /// - /// Calculate the transpose of the given matrix - /// - /// The matrix to transpose - public static void Transpose(ref Matrix4 mat, out Matrix4 result) - { - result.Row0 = mat.Column0; - result.Row1 = mat.Column1; - result.Row2 = mat.Column2; - result.Row3 = mat.Column3; - } - - #endregion - - #endregion - - #region public override string ToString() - - /// + /// /// Returns a System.String that represents the current Matrix44. /// /// @@ -617,6 +635,10 @@ namespace OpenTK.Math #endregion + #endregion + + #endregion + #region IEquatable Members /// Indicates whether the current matrix is equal to another matrix. diff --git a/Source/OpenTK/Math/Matrix4d.cs b/Source/OpenTK/Math/Matrix4d.cs index a55a0352..c4d3bc1a 100644 --- a/Source/OpenTK/Math/Matrix4d.cs +++ b/Source/OpenTK/Math/Matrix4d.cs @@ -1,10 +1,24 @@ #region --- License --- -/* Licensed under the MIT/X11 license. - * Copyright (c) 2006-2008 the OpenTK Team. - * This notice may not be removed from any source distribution. - * See license.txt for licensing detailed licensing details. - * - * Contributions by James Talton. +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ #endregion @@ -13,2358 +27,664 @@ using System.Runtime.InteropServices; namespace OpenTK.Math { - // Todo: Remove this warning when the code goes public. -#pragma warning disable 3019 - + /// + /// Represents a 4x4 Matrix with double-precision components. + /// [Serializable] [StructLayout(LayoutKind.Sequential)] - internal struct Matrix4d : IEquatable + public struct Matrix4d : IEquatable { - #region Fields & Access + #region Fields - /// Row 0, Column 0 - public double R0C0; - - /// Row 0, Column 1 - public double R0C1; - - /// Row 0, Column 2 - public double R0C2; - - /// Row 0, Column 3 - public double R0C3; - - /// Row 1, Column 0 - public double R1C0; - - /// Row 1, Column 1 - public double R1C1; - - /// Row 1, Column 2 - public double R1C2; - - /// Row 1, Column 3 - public double R1C3; - - /// Row 2, Column 0 - public double R2C0; - - /// Row 2, Column 1 - public double R2C1; - - /// Row 2, Column 2 - public double R2C2; - - /// Row 2, Column 3 - public double R2C3; - - /// Row 3, Column 0 - public double R3C0; - - /// Row 3, Column 1 - public double R3C1; - - /// Row 3, Column 2 - public double R3C2; - - /// Row 3, Column 3 - public double R3C3; - - /// Gets the component at the given row and column in the matrix. - /// The row of the matrix. - /// The column of the matrix. - /// The component at the given row and column in the matrix. - public double this[int row, int column] - { - get - { - switch (row) - { - case 0: - switch (column) - { - case 0: return R0C0; - case 1: return R0C1; - case 2: return R0C2; - case 3: return R0C3; - } - break; - - case 1: - switch (column) - { - case 0: return R1C0; - case 1: return R1C1; - case 2: return R1C2; - case 3: return R1C3; - } - break; - - case 2: - switch (column) - { - case 0: return R2C0; - case 1: return R2C1; - case 2: return R2C2; - case 3: return R2C3; - } - break; - - case 3: - switch (column) - { - case 0: return R3C0; - case 1: return R3C1; - case 2: return R3C2; - case 3: return R3C3; - } - break; - } - - throw new IndexOutOfRangeException(); - } - set - { - switch (row) - { - case 0: - switch (column) - { - case 0: R0C0 = value; return; - case 1: R0C1 = value; return; - case 2: R0C2 = value; return; - case 3: R0C3 = value; return; - } - break; - - case 1: - switch (column) - { - case 0: R1C0 = value; return; - case 1: R1C1 = value; return; - case 2: R1C2 = value; return; - case 3: R1C3 = value; return; - } - break; - - case 2: - switch (column) - { - case 0: R2C0 = value; return; - case 1: R2C1 = value; return; - case 2: R2C2 = value; return; - case 3: R2C3 = value; return; - } - break; - - case 3: - switch (column) - { - case 0: R3C0 = value; return; - case 1: R3C1 = value; return; - case 2: R3C2 = value; return; - case 3: R3C3 = value; return; - } - break; - } - - throw new IndexOutOfRangeException(); - } - } - - /// Gets the component at the index into the matrix. - /// The index into the components of the matrix. - /// The component at the given index into the matrix. - public double this[int index] - { - get - { - switch (index) - { - case 0: return R0C0; - case 1: return R0C1; - case 2: return R0C2; - case 3: return R0C3; - case 4: return R1C0; - case 5: return R1C1; - case 6: return R1C2; - case 7: return R1C3; - case 8: return R2C0; - case 9: return R2C1; - case 10: return R2C2; - case 11: return R2C3; - case 12: return R3C0; - case 13: return R3C1; - case 14: return R3C2; - case 15: return R3C3; - default: throw new IndexOutOfRangeException(); - } - } - set - { - switch (index) - { - case 0: R0C0 = value; return; - case 1: R0C1 = value; return; - case 2: R0C2 = value; return; - case 3: R0C3 = value; return; - case 4: R1C0 = value; return; - case 5: R1C1 = value; return; - case 6: R1C2 = value; return; - case 7: R1C3 = value; return; - case 8: R2C0 = value; return; - case 9: R2C1 = value; return; - case 10: R2C2 = value; return; - case 11: R2C3 = value; return; - case 12: R3C0 = value; return; - case 13: R3C1 = value; return; - case 14: R3C2 = value; return; - case 15: R3C3 = value; return; - default: throw new IndexOutOfRangeException(); - } - } - } - - /// Converts the matrix into an IntPtr. - /// The matrix to convert. - /// An IntPtr for the matrix. - public static explicit operator IntPtr(Matrix4d matrix) - { - unsafe - { - return (IntPtr)(&matrix.R0C0); - } - } - - /// Converts the matrix into left double*. - /// The matrix to convert. - /// A double* for the matrix. - [CLSCompliant(false)] - unsafe public static explicit operator double*(Matrix4d matrix) - { - return &matrix.R0C0; - } - - /// Converts the matrix into an array of doubles. - /// The matrix to convert. - /// An array of doubles for the matrix. - public static explicit operator double[](Matrix4d matrix) - { - return new double[16] - { - matrix.R0C0, - matrix.R0C1, - matrix.R0C2, - matrix.R0C3, - matrix.R1C0, - matrix.R1C1, - matrix.R1C2, - matrix.R1C3, - matrix.R2C0, - matrix.R2C1, - matrix.R2C2, - matrix.R2C3, - matrix.R3C0, - matrix.R3C1, - matrix.R3C2, - matrix.R3C3 - }; - } + /// + /// Top row of the matrix + /// + public Vector4d Row0; + /// + /// 2nd row of the matrix + /// + public Vector4d Row1; + /// + /// 3rd row of the matrix + /// + public Vector4d Row2; + /// + /// Bottom row of the matrix + /// + public Vector4d Row3; + + /// + /// The identity matrix + /// + public static Matrix4d Identity = new Matrix4d(Vector4d .UnitX, Vector4d .UnitY, Vector4d .UnitZ, Vector4d .UnitW); #endregion #region Constructors - /// Constructs left matrix with the same components as the given matrix. - /// The matrix whose components to copy. - public Matrix4d(ref Matrix4d matrix) - { - this.R0C0 = matrix.R0C0; - this.R0C1 = matrix.R0C1; - this.R0C2 = matrix.R0C2; - this.R0C3 = matrix.R0C3; - this.R1C0 = matrix.R1C0; - this.R1C1 = matrix.R1C1; - this.R1C2 = matrix.R1C2; - this.R1C3 = matrix.R1C3; - this.R2C0 = matrix.R2C0; - this.R2C1 = matrix.R2C1; - this.R2C2 = matrix.R2C2; - this.R2C3 = matrix.R2C3; - this.R3C0 = matrix.R3C0; - this.R3C1 = matrix.R3C1; - this.R3C2 = matrix.R3C2; - this.R3C3 = matrix.R3C3; - } - - /// Constructs left matrix with the given values. - /// The value for row 0 column 0. - /// The value for row 0 column 1. - /// The value for row 0 column 2. - /// The value for row 0 column 3. - /// The value for row 1 column 0. - /// The value for row 1 column 1. - /// The value for row 1 column 2. - /// The value for row 1 column 3. - /// The value for row 2 column 0. - /// The value for row 2 column 1. - /// The value for row 2 column 2. - /// The value for row 2 column 3. - /// The value for row 3 column 0. - /// The value for row 3 column 1. - /// The value for row 3 column 2. - /// The value for row 3 column 3. - public Matrix4d - ( - double r0c0, - double r0c1, - double r0c2, - double r0c3, - double r1c0, - double r1c1, - double r1c2, - double r1c3, - double r2c0, - double r2c1, - double r2c2, - double r2c3, - double r3c0, - double r3c1, - double r3c2, - double r3c3 - ) - { - this.R0C0 = r0c0; - this.R0C1 = r0c1; - this.R0C2 = r0c2; - this.R0C3 = r0c3; - this.R1C0 = r1c0; - this.R1C1 = r1c1; - this.R1C2 = r1c2; - this.R1C3 = r1c3; - this.R2C0 = r2c0; - this.R2C1 = r2c1; - this.R2C2 = r2c2; - this.R2C3 = r2c3; - this.R3C0 = r3c0; - this.R3C1 = r3c1; - this.R3C2 = r3c2; - this.R3C3 = r3c3; - } - - /// Constructs left matrix from the given array of double-precision floating point numbers. - /// The array of doubles for the components of the matrix. - public Matrix4d(double[] doubleArray) - { - if (doubleArray == null || doubleArray.GetLength(0) < 16) throw new MissingFieldException(); - - this.R0C0 = doubleArray[0]; - this.R0C1 = doubleArray[1]; - this.R0C2 = doubleArray[2]; - this.R0C3 = doubleArray[3]; - this.R1C0 = doubleArray[4]; - this.R1C1 = doubleArray[5]; - this.R1C2 = doubleArray[6]; - this.R1C3 = doubleArray[7]; - this.R2C0 = doubleArray[8]; - this.R2C1 = doubleArray[9]; - this.R2C2 = doubleArray[10]; - this.R2C3 = doubleArray[11]; - this.R3C0 = doubleArray[12]; - this.R3C1 = doubleArray[13]; - this.R3C2 = doubleArray[14]; - this.R3C3 = doubleArray[15]; - } - - /// Constructs left matrix from the given quaternion. - /// The quaternion to use to construct the martix. - public Matrix4d(ref Quaterniond quaternion) - { - Quaterniond quaternionNormalized; - quaternion.Normalize(out quaternionNormalized); - - double xx = quaternionNormalized.X * quaternionNormalized.X; - double yy = quaternionNormalized.Y * quaternionNormalized.Y; - double zz = quaternionNormalized.Z * quaternionNormalized.Z; - double xy = quaternionNormalized.X * quaternionNormalized.Y; - double xz = quaternionNormalized.X * quaternionNormalized.Z; - double yz = quaternionNormalized.Y * quaternionNormalized.Z; - double wx = quaternionNormalized.W * quaternionNormalized.X; - double wy = quaternionNormalized.W * quaternionNormalized.Y; - double wz = quaternionNormalized.W * quaternionNormalized.Z; - - R0C0 = 1 - 2 * (yy + zz); - R0C1 = 2 * (xy - wz); - R0C2 = 2 * (xz + wy); - R0C3 = 0; - - R1C0 = 2 * (xy + wz); - R1C1 = 1 - 2 * (xx + zz); - R1C2 = 2 * (yz - wx); - R1C3 = 0; - - R2C0 = 2 * (xz - wy); - R2C1 = 2 * (yz + wx); - R2C2 = 1 - 2 * (xx + yy); - R2C3 = 0; - - R3C0 = 0; - R3C1 = 0; - R3C2 = 0; - R3C3 = 1; - } + /// + /// Construct a new matrix from 4 vectors representing each row. + /// + /// Top row of the matrix + /// 2nd row of the matrix + /// 3rd row of the matrix + /// Bottom row of the matrix + public Matrix4d(Vector4d row0, Vector4d row1, Vector4d row2, Vector4d row3) + { + Row0 = row0; + Row1 = row1; + Row2 = row2; + Row3 = row3; + } #endregion - #region Equality + #region Public Members - /// Indicates whether the current matrix is equal to another matrix. - /// An matrix to compare with this matrix. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - [CLSCompliant(false)] - public bool Equals(Matrix4d matrix) - { - return - R0C0 == matrix.R0C0 && - R0C1 == matrix.R0C1 && - R0C2 == matrix.R0C2 && - R0C3 == matrix.R0C3 && - R1C0 == matrix.R1C0 && - R1C1 == matrix.R1C1 && - R1C2 == matrix.R1C2 && - R1C3 == matrix.R1C3 && - R2C0 == matrix.R2C0 && - R2C1 == matrix.R2C1 && - R2C2 == matrix.R2C2 && - R2C3 == matrix.R2C3 && - R3C0 == matrix.R3C0 && - R3C1 == matrix.R3C1 && - R3C2 == matrix.R3C2 && - R3C3 == matrix.R3C3; - - } - - /// Indicates whether the current matrix is equal to another matrix. - /// The OpenTK.Math.Matrix4d structure to compare to. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public bool Equals(ref Matrix4d matrix) - { - return - R0C0 == matrix.R0C0 && - R0C1 == matrix.R0C1 && - R0C2 == matrix.R0C2 && - R0C3 == matrix.R0C3 && - R1C0 == matrix.R1C0 && - R1C1 == matrix.R1C1 && - R1C2 == matrix.R1C2 && - R1C3 == matrix.R1C3 && - R2C0 == matrix.R2C0 && - R2C1 == matrix.R2C1 && - R2C2 == matrix.R2C2 && - R2C3 == matrix.R2C3 && - R3C0 == matrix.R3C0 && - R3C1 == matrix.R3C1 && - R3C2 == matrix.R3C2 && - R3C3 == matrix.R3C3; - - } - - /// Indicates whether the current matrix is equal to another matrix. - /// The left-hand operand. - /// The right-hand operand. - /// true if the current matrix is equal to the matrix parameter; otherwise, false. - public static bool Equals(ref Matrix4d left, ref Matrix4d right) - { - return - left.R0C0 == right.R0C0 && - left.R0C1 == right.R0C1 && - left.R0C2 == right.R0C2 && - left.R0C3 == right.R0C3 && - left.R1C0 == right.R1C0 && - left.R1C1 == right.R1C1 && - left.R1C2 == right.R1C2 && - left.R1C3 == right.R1C3 && - left.R2C0 == right.R2C0 && - left.R2C1 == right.R2C1 && - left.R2C2 == right.R2C2 && - left.R2C3 == right.R2C3 && - left.R3C0 == right.R3C0 && - left.R3C1 == right.R3C1 && - left.R3C2 == right.R3C2 && - left.R3C3 == right.R3C3; - } - - /// Indicates whether the current matrix is approximately equal to another matrix. - /// The OpenTK.Math.Matrix4d structure to compare with. - /// The limit below which the matrices are considered equal. - /// true if the current matrix is approximately equal to the matrix parameter; otherwise, false. - public bool EqualsApprox(ref Matrix4d matrix, double tolerance) - { - return - System.Math.Abs(R0C0 - matrix.R0C0) <= tolerance && - System.Math.Abs(R0C1 - matrix.R0C1) <= tolerance && - System.Math.Abs(R0C2 - matrix.R0C2) <= tolerance && - System.Math.Abs(R0C3 - matrix.R0C3) <= tolerance && - System.Math.Abs(R1C0 - matrix.R1C0) <= tolerance && - System.Math.Abs(R1C1 - matrix.R1C1) <= tolerance && - System.Math.Abs(R1C2 - matrix.R1C2) <= tolerance && - System.Math.Abs(R1C3 - matrix.R1C3) <= tolerance && - System.Math.Abs(R2C0 - matrix.R2C0) <= tolerance && - System.Math.Abs(R2C1 - matrix.R2C1) <= tolerance && - System.Math.Abs(R2C2 - matrix.R2C2) <= tolerance && - System.Math.Abs(R2C3 - matrix.R2C3) <= tolerance && - System.Math.Abs(R3C0 - matrix.R3C0) <= tolerance && - System.Math.Abs(R3C1 - matrix.R3C1) <= tolerance && - System.Math.Abs(R3C2 - matrix.R3C2) <= tolerance && - System.Math.Abs(R3C3 - matrix.R3C3) <= tolerance; - } - - /// Indicates whether the current matrix is approximately equal to another matrix. - /// The left-hand operand. - /// The right-hand operand. - /// The limit below which the matrices are considered equal. - /// true if the current matrix is approximately equal to the matrix parameter; otherwise, false. - public static bool EqualsApprox(ref Matrix4d left, ref Matrix4d right, double tolerance) - { - return - System.Math.Abs(left.R0C0 - right.R0C0) <= tolerance && - System.Math.Abs(left.R0C1 - right.R0C1) <= tolerance && - System.Math.Abs(left.R0C2 - right.R0C2) <= tolerance && - System.Math.Abs(left.R0C3 - right.R0C3) <= tolerance && - System.Math.Abs(left.R1C0 - right.R1C0) <= tolerance && - System.Math.Abs(left.R1C1 - right.R1C1) <= tolerance && - System.Math.Abs(left.R1C2 - right.R1C2) <= tolerance && - System.Math.Abs(left.R1C3 - right.R1C3) <= tolerance && - System.Math.Abs(left.R2C0 - right.R2C0) <= tolerance && - System.Math.Abs(left.R2C1 - right.R2C1) <= tolerance && - System.Math.Abs(left.R2C2 - right.R2C2) <= tolerance && - System.Math.Abs(left.R2C3 - right.R2C3) <= tolerance && - System.Math.Abs(left.R3C0 - right.R3C0) <= tolerance && - System.Math.Abs(left.R3C1 - right.R3C1) <= tolerance && - System.Math.Abs(left.R3C2 - right.R3C2) <= tolerance && - System.Math.Abs(left.R3C3 - right.R3C3) <= tolerance; - } - - #endregion - - #region Arithmetic Operators - - - /// Add left matrix to this matrix. - /// The matrix to add. - public void Add(ref Matrix4d matrix) - { - R0C0 = R0C0 + matrix.R0C0; - R0C1 = R0C1 + matrix.R0C1; - R0C2 = R0C2 + matrix.R0C2; - R0C3 = R0C3 + matrix.R0C3; - R1C0 = R1C0 + matrix.R1C0; - R1C1 = R1C1 + matrix.R1C1; - R1C2 = R1C2 + matrix.R1C2; - R1C3 = R1C3 + matrix.R1C3; - R2C0 = R2C0 + matrix.R2C0; - R2C1 = R2C1 + matrix.R2C1; - R2C2 = R2C2 + matrix.R2C2; - R2C3 = R2C3 + matrix.R2C3; - R3C0 = R3C0 + matrix.R3C0; - R3C1 = R3C1 + matrix.R3C1; - R3C2 = R3C2 + matrix.R3C2; - R3C3 = R3C3 + matrix.R3C3; - } - - /// Add left matrix to this matrix. - /// The matrix to add. - /// The resulting matrix of the addition. - public void Add(ref Matrix4d matrix, out Matrix4d result) - { - result.R0C0 = R0C0 + matrix.R0C0; - result.R0C1 = R0C1 + matrix.R0C1; - result.R0C2 = R0C2 + matrix.R0C2; - result.R0C3 = R0C3 + matrix.R0C3; - result.R1C0 = R1C0 + matrix.R1C0; - result.R1C1 = R1C1 + matrix.R1C1; - result.R1C2 = R1C2 + matrix.R1C2; - result.R1C3 = R1C3 + matrix.R1C3; - result.R2C0 = R2C0 + matrix.R2C0; - result.R2C1 = R2C1 + matrix.R2C1; - result.R2C2 = R2C2 + matrix.R2C2; - result.R2C3 = R2C3 + matrix.R2C3; - result.R3C0 = R3C0 + matrix.R3C0; - result.R3C1 = R3C1 + matrix.R3C1; - result.R3C2 = R3C2 + matrix.R3C2; - result.R3C3 = R3C3 + matrix.R3C3; - } - - /// Add left matrix to left matrix. - /// The matrix on the matrix side of the equation. - /// The matrix on the right side of the equation - /// The resulting matrix of the addition. - public static void Add(ref Matrix4d left, ref Matrix4d right, out Matrix4d result) - { - result.R0C0 = left.R0C0 + right.R0C0; - result.R0C1 = left.R0C1 + right.R0C1; - result.R0C2 = left.R0C2 + right.R0C2; - result.R0C3 = left.R0C3 + right.R0C3; - result.R1C0 = left.R1C0 + right.R1C0; - result.R1C1 = left.R1C1 + right.R1C1; - result.R1C2 = left.R1C2 + right.R1C2; - result.R1C3 = left.R1C3 + right.R1C3; - result.R2C0 = left.R2C0 + right.R2C0; - result.R2C1 = left.R2C1 + right.R2C1; - result.R2C2 = left.R2C2 + right.R2C2; - result.R2C3 = left.R2C3 + right.R2C3; - result.R3C0 = left.R3C0 + right.R3C0; - result.R3C1 = left.R3C1 + right.R3C1; - result.R3C2 = left.R3C2 + right.R3C2; - result.R3C3 = left.R3C3 + right.R3C3; - } - - - /// Subtract left matrix from this matrix. - /// The matrix to subtract. - public void Subtract(ref Matrix4d matrix) - { - R0C0 = R0C0 + matrix.R0C0; - R0C1 = R0C1 + matrix.R0C1; - R0C2 = R0C2 + matrix.R0C2; - R0C3 = R0C3 + matrix.R0C3; - R1C0 = R1C0 + matrix.R1C0; - R1C1 = R1C1 + matrix.R1C1; - R1C2 = R1C2 + matrix.R1C2; - R1C3 = R1C3 + matrix.R1C3; - R2C0 = R2C0 + matrix.R2C0; - R2C1 = R2C1 + matrix.R2C1; - R2C2 = R2C2 + matrix.R2C2; - R2C3 = R2C3 + matrix.R2C3; - R3C0 = R3C0 + matrix.R3C0; - R3C1 = R3C1 + matrix.R3C1; - R3C2 = R3C2 + matrix.R3C2; - R3C3 = R3C3 + matrix.R3C3; - } - - /// Subtract left matrix from this matrix. - /// The matrix to subtract. - /// The resulting matrix of the subtraction. - public void Subtract(ref Matrix4d matrix, out Matrix4d result) - { - result.R0C0 = R0C0 + matrix.R0C0; - result.R0C1 = R0C1 + matrix.R0C1; - result.R0C2 = R0C2 + matrix.R0C2; - result.R0C3 = R0C3 + matrix.R0C3; - result.R1C0 = R1C0 + matrix.R1C0; - result.R1C1 = R1C1 + matrix.R1C1; - result.R1C2 = R1C2 + matrix.R1C2; - result.R1C3 = R1C3 + matrix.R1C3; - result.R2C0 = R2C0 + matrix.R2C0; - result.R2C1 = R2C1 + matrix.R2C1; - result.R2C2 = R2C2 + matrix.R2C2; - result.R2C3 = R2C3 + matrix.R2C3; - result.R3C0 = R3C0 + matrix.R3C0; - result.R3C1 = R3C1 + matrix.R3C1; - result.R3C2 = R3C2 + matrix.R3C2; - result.R3C3 = R3C3 + matrix.R3C3; - } - - /// Subtract left matrix from left matrix. - /// The matrix on the matrix side of the equation. - /// The matrix on the right side of the equation - /// The resulting matrix of the subtraction. - public static void Subtract(ref Matrix4d left, ref Matrix4d right, out Matrix4d result) - { - result.R0C0 = left.R0C0 + right.R0C0; - result.R0C1 = left.R0C1 + right.R0C1; - result.R0C2 = left.R0C2 + right.R0C2; - result.R0C3 = left.R0C3 + right.R0C3; - result.R1C0 = left.R1C0 + right.R1C0; - result.R1C1 = left.R1C1 + right.R1C1; - result.R1C2 = left.R1C2 + right.R1C2; - result.R1C3 = left.R1C3 + right.R1C3; - result.R2C0 = left.R2C0 + right.R2C0; - result.R2C1 = left.R2C1 + right.R2C1; - result.R2C2 = left.R2C2 + right.R2C2; - result.R2C3 = left.R2C3 + right.R2C3; - result.R3C0 = left.R3C0 + right.R3C0; - result.R3C1 = left.R3C1 + right.R3C1; - result.R3C2 = left.R3C2 + right.R3C2; - result.R3C3 = left.R3C3 + right.R3C3; - } - - - /// Multiply left martix times this matrix. - /// The matrix to multiply. - public void Multiply(ref Matrix4d matrix) - { - double r0c0 = matrix.R0C0 * R0C0 + matrix.R0C1 * R1C0 + matrix.R0C2 * R2C0 + matrix.R0C3 * R3C0; - double r0c1 = matrix.R0C0 * R0C1 + matrix.R0C1 * R1C1 + matrix.R0C2 * R2C1 + matrix.R0C3 * R3C1; - double r0c2 = matrix.R0C0 * R0C2 + matrix.R0C1 * R1C2 + matrix.R0C2 * R2C2 + matrix.R0C3 * R3C2; - double r0c3 = matrix.R0C0 * R0C3 + matrix.R0C1 * R1C3 + matrix.R0C2 * R2C3 + matrix.R0C3 * R3C3; - double r1c0 = matrix.R1C0 * R0C0 + matrix.R1C1 * R1C0 + matrix.R1C2 * R2C0 + matrix.R1C3 * R3C0; - double r1c1 = matrix.R1C0 * R0C1 + matrix.R1C1 * R1C1 + matrix.R1C2 * R2C1 + matrix.R1C3 * R3C1; - double r1c2 = matrix.R1C0 * R0C2 + matrix.R1C1 * R1C2 + matrix.R1C2 * R2C2 + matrix.R1C3 * R3C2; - double r1c3 = matrix.R1C0 * R0C3 + matrix.R1C1 * R1C3 + matrix.R1C2 * R2C3 + matrix.R1C3 * R3C3; - double r2c0 = matrix.R2C0 * R0C0 + matrix.R2C1 * R1C0 + matrix.R2C2 * R2C0 + matrix.R2C3 * R3C0; - double r2c1 = matrix.R2C0 * R0C1 + matrix.R2C1 * R1C1 + matrix.R2C2 * R2C1 + matrix.R2C3 * R3C1; - double r2c2 = matrix.R2C0 * R0C2 + matrix.R2C1 * R1C2 + matrix.R2C2 * R2C2 + matrix.R2C3 * R3C2; - double r2c3 = matrix.R2C0 * R0C3 + matrix.R2C1 * R1C3 + matrix.R2C2 * R2C3 + matrix.R2C3 * R3C3; - - R3C0 = matrix.R3C0 * R0C0 + matrix.R3C1 * R1C0 + matrix.R3C2 * R2C0 + matrix.R3C3 * R3C0; - R3C1 = matrix.R3C0 * R0C1 + matrix.R3C1 * R1C1 + matrix.R3C2 * R2C1 + matrix.R3C3 * R3C1; - R3C2 = matrix.R3C0 * R0C2 + matrix.R3C1 * R1C2 + matrix.R3C2 * R2C2 + matrix.R3C3 * R3C2; - R3C3 = matrix.R3C0 * R0C3 + matrix.R3C1 * R1C3 + matrix.R3C2 * R2C3 + matrix.R3C3 * R3C3; - - R0C0 = r0c0; - R0C1 = r0c1; - R0C2 = r0c2; - R0C3 = r0c3; - R1C0 = r1c0; - R1C1 = r1c1; - R1C2 = r1c2; - R1C3 = r1c3; - R2C0 = r2c0; - R2C1 = r2c1; - R2C2 = r2c2; - R2C3 = r2c3; - } - - /// Multiply matrix times this matrix. - /// The matrix to multiply. - /// The resulting matrix of the multiplication. - public void Multiply(ref Matrix4d matrix, out Matrix4d result) - { - result.R0C0 = matrix.R0C0 * R0C0 + matrix.R0C1 * R1C0 + matrix.R0C2 * R2C0 + matrix.R0C3 * R3C0; - result.R0C1 = matrix.R0C0 * R0C1 + matrix.R0C1 * R1C1 + matrix.R0C2 * R2C1 + matrix.R0C3 * R3C1; - result.R0C2 = matrix.R0C0 * R0C2 + matrix.R0C1 * R1C2 + matrix.R0C2 * R2C2 + matrix.R0C3 * R3C2; - result.R0C3 = matrix.R0C0 * R0C3 + matrix.R0C1 * R1C3 + matrix.R0C2 * R2C3 + matrix.R0C3 * R3C3; - result.R1C0 = matrix.R1C0 * R0C0 + matrix.R1C1 * R1C0 + matrix.R1C2 * R2C0 + matrix.R1C3 * R3C0; - result.R1C1 = matrix.R1C0 * R0C1 + matrix.R1C1 * R1C1 + matrix.R1C2 * R2C1 + matrix.R1C3 * R3C1; - result.R1C2 = matrix.R1C0 * R0C2 + matrix.R1C1 * R1C2 + matrix.R1C2 * R2C2 + matrix.R1C3 * R3C2; - result.R1C3 = matrix.R1C0 * R0C3 + matrix.R1C1 * R1C3 + matrix.R1C2 * R2C3 + matrix.R1C3 * R3C3; - result.R2C0 = matrix.R2C0 * R0C0 + matrix.R2C1 * R1C0 + matrix.R2C2 * R2C0 + matrix.R2C3 * R3C0; - result.R2C1 = matrix.R2C0 * R0C1 + matrix.R2C1 * R1C1 + matrix.R2C2 * R2C1 + matrix.R2C3 * R3C1; - result.R2C2 = matrix.R2C0 * R0C2 + matrix.R2C1 * R1C2 + matrix.R2C2 * R2C2 + matrix.R2C3 * R3C2; - result.R2C3 = matrix.R2C0 * R0C3 + matrix.R2C1 * R1C3 + matrix.R2C2 * R2C3 + matrix.R2C3 * R3C3; - result.R3C0 = matrix.R3C0 * R0C0 + matrix.R3C1 * R1C0 + matrix.R3C2 * R2C0 + matrix.R3C3 * R3C0; - result.R3C1 = matrix.R3C0 * R0C1 + matrix.R3C1 * R1C1 + matrix.R3C2 * R2C1 + matrix.R3C3 * R3C1; - result.R3C2 = matrix.R3C0 * R0C2 + matrix.R3C1 * R1C2 + matrix.R3C2 * R2C2 + matrix.R3C3 * R3C2; - result.R3C3 = matrix.R3C0 * R0C3 + matrix.R3C1 * R1C3 + matrix.R3C2 * R2C3 + matrix.R3C3 * R3C3; - - } - - /// Multiply left matrix times left matrix. - /// The matrix on the matrix side of the equation. - /// The matrix on the right side of the equation - /// The resulting matrix of the multiplication. - public static void Multiply(ref Matrix4d left, ref Matrix4d right, out Matrix4d result) - { - result.R0C0 = right.R0C0 * left.R0C0 + right.R0C1 * left.R1C0 + right.R0C2 * left.R2C0 + right.R0C3 * left.R3C0; - result.R0C1 = right.R0C0 * left.R0C1 + right.R0C1 * left.R1C1 + right.R0C2 * left.R2C1 + right.R0C3 * left.R3C1; - result.R0C2 = right.R0C0 * left.R0C2 + right.R0C1 * left.R1C2 + right.R0C2 * left.R2C2 + right.R0C3 * left.R3C2; - result.R0C3 = right.R0C0 * left.R0C3 + right.R0C1 * left.R1C3 + right.R0C2 * left.R2C3 + right.R0C3 * left.R3C3; - result.R1C0 = right.R1C0 * left.R0C0 + right.R1C1 * left.R1C0 + right.R1C2 * left.R2C0 + right.R1C3 * left.R3C0; - result.R1C1 = right.R1C0 * left.R0C1 + right.R1C1 * left.R1C1 + right.R1C2 * left.R2C1 + right.R1C3 * left.R3C1; - result.R1C2 = right.R1C0 * left.R0C2 + right.R1C1 * left.R1C2 + right.R1C2 * left.R2C2 + right.R1C3 * left.R3C2; - result.R1C3 = right.R1C0 * left.R0C3 + right.R1C1 * left.R1C3 + right.R1C2 * left.R2C3 + right.R1C3 * left.R3C3; - result.R2C0 = right.R2C0 * left.R0C0 + right.R2C1 * left.R1C0 + right.R2C2 * left.R2C0 + right.R2C3 * left.R3C0; - result.R2C1 = right.R2C0 * left.R0C1 + right.R2C1 * left.R1C1 + right.R2C2 * left.R2C1 + right.R2C3 * left.R3C1; - result.R2C2 = right.R2C0 * left.R0C2 + right.R2C1 * left.R1C2 + right.R2C2 * left.R2C2 + right.R2C3 * left.R3C2; - result.R2C3 = right.R2C0 * left.R0C3 + right.R2C1 * left.R1C3 + right.R2C2 * left.R2C3 + right.R2C3 * left.R3C3; - result.R3C0 = right.R3C0 * left.R0C0 + right.R3C1 * left.R1C0 + right.R3C2 * left.R2C0 + right.R3C3 * left.R3C0; - result.R3C1 = right.R3C0 * left.R0C1 + right.R3C1 * left.R1C1 + right.R3C2 * left.R2C1 + right.R3C3 * left.R3C1; - result.R3C2 = right.R3C0 * left.R0C2 + right.R3C1 * left.R1C2 + right.R3C2 * left.R2C2 + right.R3C3 * left.R3C2; - result.R3C3 = right.R3C0 * left.R0C3 + right.R3C1 * left.R1C3 + right.R3C2 * left.R2C3 + right.R3C3 * left.R3C3; - } - - - /// Multiply matrix times this matrix. - /// The matrix to multiply. - public void Multiply(double scalar) - { - R0C0 = scalar * R0C0; - R0C1 = scalar * R0C1; - R0C2 = scalar * R0C2; - R0C3 = scalar * R0C3; - R1C0 = scalar * R1C0; - R1C1 = scalar * R1C1; - R1C2 = scalar * R1C2; - R1C3 = scalar * R1C3; - R2C0 = scalar * R2C0; - R2C1 = scalar * R2C1; - R2C2 = scalar * R2C2; - R2C3 = scalar * R2C3; - R3C0 = scalar * R3C0; - R3C1 = scalar * R3C1; - R3C2 = scalar * R3C2; - R3C3 = scalar * R3C3; - } - - /// Multiply matrix times this matrix. - /// The matrix to multiply. - /// The resulting matrix of the multiplication. - public void Multiply(double scalar, out Matrix4d result) - { - result.R0C0 = scalar * R0C0; - result.R0C1 = scalar * R0C1; - result.R0C2 = scalar * R0C2; - result.R0C3 = scalar * R0C3; - result.R1C0 = scalar * R1C0; - result.R1C1 = scalar * R1C1; - result.R1C2 = scalar * R1C2; - result.R1C3 = scalar * R1C3; - result.R2C0 = scalar * R2C0; - result.R2C1 = scalar * R2C1; - result.R2C2 = scalar * R2C2; - result.R2C3 = scalar * R2C3; - result.R3C0 = scalar * R3C0; - result.R3C1 = scalar * R3C1; - result.R3C2 = scalar * R3C2; - result.R3C3 = scalar * R3C3; - } - - /// Multiply left matrix times left matrix. - /// The matrix on the matrix side of the equation. - /// The matrix on the right side of the equation - /// The resulting matrix of the multiplication. - public static void Multiply(ref Matrix4d matrix, double scalar, out Matrix4d result) - { - result.R0C0 = scalar * matrix.R0C0; - result.R0C1 = scalar * matrix.R0C1; - result.R0C2 = scalar * matrix.R0C2; - result.R0C3 = scalar * matrix.R0C3; - result.R1C0 = scalar * matrix.R1C0; - result.R1C1 = scalar * matrix.R1C1; - result.R1C2 = scalar * matrix.R1C2; - result.R1C3 = scalar * matrix.R1C3; - result.R2C0 = scalar * matrix.R2C0; - result.R2C1 = scalar * matrix.R2C1; - result.R2C2 = scalar * matrix.R2C2; - result.R2C3 = scalar * matrix.R2C3; - result.R3C0 = scalar * matrix.R3C0; - result.R3C1 = scalar * matrix.R3C1; - result.R3C2 = scalar * matrix.R3C2; - result.R3C3 = scalar * matrix.R3C3; - } - - - #endregion - - #region Functions - - public double Cofacter(int row, int column) - { - switch (row) - { - case 0: - switch (column) - { - case 0: return +(R1C1 * R2C2 * R3C3 - R1C1 * R2C3 * R3C2 - R1C2 * R2C1 * R3C3 + R1C3 * R2C1 * R3C2 + R1C2 * R2C3 * R3C1 - R1C3 * R2C2 * R3C1); - case 1: return -(R1C0 * R2C2 * R3C3 - R1C0 * R2C3 * R3C2 - R1C2 * R2C0 * R3C3 + R1C3 * R2C0 * R3C2 + R1C2 * R2C3 * R3C0 - R1C3 * R2C2 * R3C0); - case 2: return +(R1C0 * R2C1 * R3C3 - R1C0 * R2C3 * R3C1 - R1C1 * R2C0 * R3C3 + R1C3 * R2C0 * R3C1 + R1C1 * R2C3 * R3C0 - R1C3 * R2C1 * R3C0); - case 3: return -(R1C0 * R2C1 * R3C2 - R1C0 * R2C2 * R3C1 - R1C1 * R2C0 * R3C2 + R1C2 * R2C0 * R3C1 + R1C1 * R2C2 * R3C0 - R1C2 * R2C1 * R3C0); - } - break; - - case 1: - switch (column) - { - case 0: return -(R0C1 * R2C2 * R3C3 - R0C1 * R2C3 * R3C2 - R0C2 * R2C1 * R3C3 + R0C3 * R2C1 * R3C2 + R0C2 * R2C3 * R3C1 - R0C3 * R2C2 * R3C1); - case 1: return +(R0C0 * R2C2 * R3C3 - R0C0 * R2C3 * R3C2 - R0C2 * R2C0 * R3C3 + R0C3 * R2C0 * R3C2 + R0C2 * R2C3 * R3C0 - R0C3 * R2C2 * R3C0); - case 2: return -(R0C0 * R2C1 * R3C3 - R0C0 * R2C3 * R3C1 - R0C1 * R2C0 * R3C3 + R0C3 * R2C0 * R3C1 + R0C1 * R2C3 * R3C0 - R0C3 * R2C1 * R3C0); - case 3: return +(R0C0 * R2C1 * R3C2 - R0C0 * R2C2 * R3C1 - R0C1 * R2C0 * R3C2 + R0C2 * R2C0 * R3C1 + R0C1 * R2C2 * R3C0 - R0C2 * R2C1 * R3C0); - } - break; - - case 2: - switch (column) - { - case 0: return +(R0C1 * R1C2 * R3C3 - R0C1 * R1C3 * R3C2 - R0C2 * R1C1 * R3C3 + R0C3 * R1C1 * R3C2 + R0C2 * R1C3 * R3C1 - R0C3 * R1C2 * R3C1); - case 1: return -(R0C0 * R1C2 * R3C3 - R0C0 * R1C3 * R3C2 - R0C2 * R1C0 * R3C3 + R0C3 * R1C0 * R3C2 + R0C2 * R1C3 * R3C0 - R0C3 * R1C2 * R3C0); - case 2: return +(R0C0 * R1C1 * R3C3 - R0C0 * R1C3 * R3C1 - R0C1 * R1C0 * R3C3 + R0C3 * R1C0 * R3C1 + R0C1 * R1C3 * R3C0 - R0C3 * R1C1 * R3C0); - case 3: return -(R0C0 * R1C1 * R3C2 - R0C0 * R1C2 * R3C1 - R0C1 * R1C0 * R3C2 + R0C2 * R1C0 * R3C1 + R0C1 * R1C2 * R3C0 - R0C2 * R1C1 * R3C0); - } - break; - - case 3: - switch (column) - { - case 0: return -(R0C1 * R1C2 * R2C3 - R0C1 * R1C3 * R2C2 - R0C2 * R1C1 * R2C3 + R0C3 * R1C1 * R2C2 + R0C2 * R1C3 * R2C1 - R0C3 * R1C2 * R2C1); - case 1: return +(R0C0 * R1C2 * R2C3 - R0C0 * R1C3 * R2C2 - R0C2 * R1C0 * R2C3 + R0C3 * R1C0 * R2C2 + R0C2 * R1C3 * R2C0 - R0C3 * R1C2 * R2C0); - case 2: return -(R0C0 * R1C1 * R2C3 - R0C0 * R1C3 * R2C1 - R0C1 * R1C0 * R2C3 + R0C3 * R1C0 * R2C1 + R0C1 * R1C3 * R2C0 - R0C3 * R1C1 * R2C0); - case 3: return +(R0C0 * R1C1 * R2C2 - R0C0 * R1C2 * R2C1 - R0C1 * R1C0 * R2C2 + R0C2 * R1C0 * R2C1 + R0C1 * R1C2 * R2C0 - R0C2 * R1C1 * R2C0); - } - break; - } - - throw new IndexOutOfRangeException(); - } + #region Properties + /// + /// The determinant of this matrix + /// public double Determinant { get { return - R2C3 * R3C2 * R0C1 * R1C0 - R2C2 * R3C3 * R0C1 * R1C0 - R2C3 * R3C1 * R0C2 * R1C0 + R2C1 * R3C3 * R0C2 * R1C0 + - R2C2 * R3C1 * R0C3 * R1C0 - R2C1 * R3C2 * R0C3 * R1C0 - R0C0 * R2C3 * R3C2 * R1C1 + R0C0 * R2C2 * R3C3 * R1C1 + - R2C3 * R3C0 * R0C2 * R1C1 - R2C2 * R3C0 * R0C3 * R1C1 + R0C0 * R2C3 * R3C1 * R1C2 - R0C0 * R2C1 * R3C3 * R1C2 - - R2C3 * R3C0 * R0C1 * R1C2 + R2C1 * R3C0 * R0C3 * R1C2 - R0C0 * R2C2 * R3C1 * R1C3 + R0C0 * R2C1 * R3C2 * R1C3 + - R2C2 * R3C0 * R0C1 * R1C3 - R2C1 * R3C0 * R0C2 * R1C3 - R3C3 * R0C2 * R1C1 * R2C0 + R3C2 * R0C3 * R1C1 * R2C0 + - R3C3 * R0C1 * R1C2 * R2C0 - R3C1 * R0C3 * R1C2 * R2C0 - R3C2 * R0C1 * R1C3 * R2C0 + R3C1 * R0C2 * R1C3 * R2C0; + Row0.X * Row1.Y * Row2.Z * Row3.W - Row0.X * Row1.Y * Row2.W * Row3.Z + Row0.X * Row1.Z * Row2.W * Row3.Y - Row0.X * Row1.Z * Row2.Y * Row3.W + + Row0.X * Row1.W * Row2.Y * Row3.Z - Row0.X * Row1.W * Row2.Z * Row3.Y - Row0.Y * Row1.Z * Row2.W * Row3.X + Row0.Y * Row1.Z * Row2.X * Row3.W + - Row0.Y * Row1.W * Row2.X * Row3.Z + Row0.Y * Row1.W * Row2.Z * Row3.X - Row0.Y * Row1.X * Row2.Z * Row3.W + Row0.Y * Row1.X * Row2.W * Row3.Z + + Row0.Z * Row1.W * Row2.X * Row3.Y - Row0.Z * Row1.W * Row2.Y * Row3.X + Row0.Z * Row1.X * Row2.Y * Row3.W - Row0.Z * Row1.X * Row2.W * Row3.Y + + Row0.Z * Row1.Y * Row2.W * Row3.X - Row0.Z * Row1.Y * Row2.X * Row3.W - Row0.W * Row1.X * Row2.Y * Row3.Z + Row0.W * Row1.X * Row2.Z * Row3.Y + - Row0.W * Row1.Y * Row2.Z * Row3.X + Row0.W * Row1.Y * Row2.X * Row3.Z - Row0.W * Row1.Z * Row2.X * Row3.Y + Row0.W * Row1.Z * Row2.Y * Row3.X; } } - public void Minor(int row, int column, out Matrix3d result) + /// + /// The first column of this matrix + /// + public Vector4d Column0 { - switch (row) + get { return new Vector4d (Row0.X, Row1.X, Row2.X, Row3.X); } + } + + /// + /// The second column of this matrix + /// + public Vector4d Column1 + { + get { return new Vector4d (Row0.Y, Row1.Y, Row2.Y, Row3.Y); } + } + + /// + /// The third column of this matrix + /// + public Vector4d Column2 + { + get { return new Vector4d (Row0.Z, Row1.Z, Row2.Z, Row3.Z); } + } + + /// + /// The fourth column of this matrix + /// + public Vector4d Column3 + { + get { return new Vector4d (Row0.W, Row1.W, Row2.W, Row3.W); } + } + + public double this[int i, int j] + { + get { - case 0: - switch (column) - { - case 0: - result.R0C0 = R1C1; - result.R0C1 = R1C2; - result.R0C2 = R1C3; - result.R1C0 = R2C1; - result.R1C1 = R2C2; - result.R1C2 = R2C3; - result.R2C0 = R3C1; - result.R2C1 = R3C2; - result.R2C2 = R3C3; - return; + if (i < 0 || i > 3) + throw new ArgumentOutOfRangeException("i"); - case 1: - result.R0C0 = R1C0; - result.R0C1 = R1C2; - result.R0C2 = R1C3; - result.R1C0 = R2C0; - result.R1C1 = R2C2; - result.R1C2 = R2C3; - result.R2C0 = R3C0; - result.R2C1 = R3C2; - result.R2C2 = R3C3; - return; - - case 2: - result.R0C0 = R1C0; - result.R0C1 = R1C1; - result.R0C2 = R1C3; - result.R1C0 = R2C0; - result.R1C1 = R2C1; - result.R1C2 = R2C3; - result.R2C0 = R3C0; - result.R2C1 = R3C1; - result.R2C2 = R3C3; - return; - - case 3: - result.R0C0 = R1C0; - result.R0C1 = R1C1; - result.R0C2 = R1C2; - result.R1C0 = R2C0; - result.R1C1 = R2C1; - result.R1C2 = R2C2; - result.R2C0 = R3C0; - result.R2C1 = R3C1; - result.R2C2 = R3C2; - return; - } - break; - - case 1: - switch (column) - { - case 0: - result.R0C0 = R0C1; - result.R0C1 = R0C2; - result.R0C2 = R0C3; - result.R1C0 = R2C1; - result.R1C1 = R2C2; - result.R1C2 = R2C3; - result.R2C0 = R3C1; - result.R2C1 = R3C2; - result.R2C2 = R3C3; - return; - - case 1: - result.R0C0 = R0C0; - result.R0C1 = R0C2; - result.R0C2 = R0C3; - result.R1C0 = R2C0; - result.R1C1 = R2C2; - result.R1C2 = R2C3; - result.R2C0 = R3C0; - result.R2C1 = R3C2; - result.R2C2 = R3C3; - return; - - case 2: - result.R0C0 = R0C0; - result.R0C1 = R0C1; - result.R0C2 = R0C3; - result.R1C0 = R2C0; - result.R1C1 = R2C1; - result.R1C2 = R2C3; - result.R2C0 = R3C0; - result.R2C1 = R3C1; - result.R2C2 = R3C3; - return; - } - break; - - case 2: - switch (column) - { - case 0: - result.R0C0 = R0C1; - result.R0C1 = R0C2; - result.R0C2 = R0C3; - result.R1C0 = R1C1; - result.R1C1 = R1C2; - result.R1C2 = R1C3; - result.R2C0 = R3C1; - result.R2C1 = R3C2; - result.R2C2 = R3C3; - return; - - case 1: - result.R0C0 = R0C0; - result.R0C1 = R0C2; - result.R0C2 = R0C3; - result.R1C0 = R1C0; - result.R1C1 = R1C2; - result.R1C2 = R1C3; - result.R2C0 = R3C0; - result.R2C1 = R3C2; - result.R2C2 = R3C3; - return; - - case 2: - result.R0C0 = R0C0; - result.R0C1 = R0C1; - result.R0C2 = R0C3; - result.R1C0 = R1C0; - result.R1C1 = R1C1; - result.R1C2 = R1C3; - result.R2C0 = R3C0; - result.R2C1 = R3C1; - result.R2C2 = R3C3; - return; - } - break; + if (j < 0 || j > 3) + throw new ArgumentOutOfRangeException("j"); + unsafe + { + fixed (Matrix4d* ptr = &this) + return *((double*)ptr + i + j * 4); + } } - - throw new IndexOutOfRangeException(); - } - public static void Minor(ref Matrix4d matrix, int row, int column, out Matrix3d result) - { - switch (row) + set { - case 0: - switch (column) - { - case 0: - result.R0C0 = matrix.R1C1; - result.R0C1 = matrix.R1C2; - result.R0C2 = matrix.R1C3; - result.R1C0 = matrix.R2C1; - result.R1C1 = matrix.R2C2; - result.R1C2 = matrix.R2C3; - result.R2C0 = matrix.R3C1; - result.R2C1 = matrix.R3C2; - result.R2C2 = matrix.R3C3; - return; + if (i < 0 || i > 3) + throw new ArgumentOutOfRangeException("i"); - case 1: - result.R0C0 = matrix.R1C0; - result.R0C1 = matrix.R1C2; - result.R0C2 = matrix.R1C3; - result.R1C0 = matrix.R2C0; - result.R1C1 = matrix.R2C2; - result.R1C2 = matrix.R2C3; - result.R2C0 = matrix.R3C0; - result.R2C1 = matrix.R3C2; - result.R2C2 = matrix.R3C3; - return; + if (j < 0 || j > 3) + throw new ArgumentOutOfRangeException("j"); - case 2: - result.R0C0 = matrix.R1C0; - result.R0C1 = matrix.R1C1; - result.R0C2 = matrix.R1C3; - result.R1C0 = matrix.R2C0; - result.R1C1 = matrix.R2C1; - result.R1C2 = matrix.R2C3; - result.R2C0 = matrix.R3C0; - result.R2C1 = matrix.R3C1; - result.R2C2 = matrix.R3C3; - return; - } - break; - - case 1: - switch (column) - { - case 0: - result.R0C0 = matrix.R0C1; - result.R0C1 = matrix.R0C2; - result.R0C2 = matrix.R0C3; - result.R1C0 = matrix.R2C1; - result.R1C1 = matrix.R2C2; - result.R1C2 = matrix.R2C3; - result.R2C0 = matrix.R3C1; - result.R2C1 = matrix.R3C2; - result.R2C2 = matrix.R3C3; - return; - - case 1: - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C2; - result.R0C2 = matrix.R0C3; - result.R1C0 = matrix.R2C0; - result.R1C1 = matrix.R2C2; - result.R1C2 = matrix.R2C3; - result.R2C0 = matrix.R3C0; - result.R2C1 = matrix.R3C2; - result.R2C2 = matrix.R3C3; - return; - - case 2: - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C1; - result.R0C2 = matrix.R0C3; - result.R1C0 = matrix.R2C0; - result.R1C1 = matrix.R2C1; - result.R1C2 = matrix.R2C3; - result.R2C0 = matrix.R3C0; - result.R2C1 = matrix.R3C1; - result.R2C2 = matrix.R3C3; - return; - } - break; - - case 2: - switch (column) - { - case 0: - result.R0C0 = matrix.R0C1; - result.R0C1 = matrix.R0C2; - result.R0C2 = matrix.R0C3; - result.R1C0 = matrix.R1C1; - result.R1C1 = matrix.R1C2; - result.R1C2 = matrix.R1C3; - result.R2C0 = matrix.R3C1; - result.R2C1 = matrix.R3C2; - result.R2C2 = matrix.R3C3; - return; - - case 1: - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C2; - result.R0C2 = matrix.R0C3; - result.R1C0 = matrix.R1C0; - result.R1C1 = matrix.R1C2; - result.R1C2 = matrix.R1C3; - result.R2C0 = matrix.R3C0; - result.R2C1 = matrix.R3C2; - result.R2C2 = matrix.R3C3; - return; - - case 2: - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C1; - result.R0C2 = matrix.R0C3; - result.R1C0 = matrix.R1C0; - result.R1C1 = matrix.R1C1; - result.R1C2 = matrix.R1C3; - result.R2C0 = matrix.R3C0; - result.R2C1 = matrix.R3C1; - result.R2C2 = matrix.R3C3; - return; - - case 3: - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C1; - result.R0C2 = matrix.R0C2; - result.R1C0 = matrix.R1C0; - result.R1C1 = matrix.R1C1; - result.R1C2 = matrix.R1C2; - result.R2C0 = matrix.R3C0; - result.R2C1 = matrix.R3C1; - result.R2C2 = matrix.R3C2; - return; - } - break; - - case 3: - switch (column) - { - case 0: - result.R0C0 = matrix.R0C1; - result.R0C1 = matrix.R0C2; - result.R0C2 = matrix.R0C3; - result.R1C0 = matrix.R1C1; - result.R1C1 = matrix.R1C2; - result.R1C2 = matrix.R1C3; - result.R2C0 = matrix.R2C1; - result.R2C1 = matrix.R2C2; - result.R2C2 = matrix.R2C3; - return; - - case 1: - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C2; - result.R0C2 = matrix.R0C3; - result.R1C0 = matrix.R1C0; - result.R1C1 = matrix.R1C2; - result.R1C2 = matrix.R1C3; - result.R2C0 = matrix.R2C0; - result.R2C1 = matrix.R2C2; - result.R2C2 = matrix.R2C3; - return; - - case 2: - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C1; - result.R0C2 = matrix.R0C3; - result.R1C0 = matrix.R1C0; - result.R1C1 = matrix.R1C1; - result.R1C2 = matrix.R1C3; - result.R2C0 = matrix.R2C0; - result.R2C1 = matrix.R2C1; - result.R2C2 = matrix.R2C3; - return; - - case 3: - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C1; - result.R0C2 = matrix.R0C2; - result.R1C0 = matrix.R1C0; - result.R1C1 = matrix.R1C1; - result.R1C2 = matrix.R1C2; - result.R2C0 = matrix.R2C0; - result.R2C1 = matrix.R2C1; - result.R2C2 = matrix.R2C2; - return; - } - break; + unsafe + { + fixed (Matrix4d* ptr = &this) + *((double*)ptr + i + j * 4) = value; + } } - - throw new IndexOutOfRangeException(); } - public void CofacterMatrix(out Matrix4d result) + #endregion + + #region Instance + + #region public void Invert() + + public void Invert() { - result.R0C0 = Cofacter(0, 0); - result.R0C1 = Cofacter(0, 1); - result.R0C2 = Cofacter(0, 2); - result.R0C3 = Cofacter(0, 3); - result.R1C0 = Cofacter(1, 0); - result.R1C1 = Cofacter(1, 1); - result.R1C2 = Cofacter(1, 2); - result.R1C3 = Cofacter(1, 3); - result.R2C0 = Cofacter(2, 0); - result.R2C1 = Cofacter(2, 1); - result.R2C2 = Cofacter(2, 2); - result.R2C3 = Cofacter(2, 3); - result.R3C0 = Cofacter(3, 0); - result.R3C1 = Cofacter(3, 1); - result.R3C2 = Cofacter(3, 2); - result.R3C3 = Cofacter(3, 3); - } - public static void CofacterMatrix(ref Matrix4d matrix, out Matrix4d result) - { - result.R0C0 = matrix.Cofacter(0, 0); - result.R0C1 = matrix.Cofacter(0, 1); - result.R0C2 = matrix.Cofacter(0, 2); - result.R0C3 = matrix.Cofacter(0, 3); - result.R1C0 = matrix.Cofacter(1, 0); - result.R1C1 = matrix.Cofacter(1, 1); - result.R1C2 = matrix.Cofacter(1, 2); - result.R1C3 = matrix.Cofacter(1, 3); - result.R2C0 = matrix.Cofacter(2, 0); - result.R2C1 = matrix.Cofacter(2, 1); - result.R2C2 = matrix.Cofacter(2, 2); - result.R2C3 = matrix.Cofacter(2, 3); - result.R3C0 = matrix.Cofacter(3, 0); - result.R3C1 = matrix.Cofacter(3, 1); - result.R3C2 = matrix.Cofacter(3, 2); - result.R3C3 = matrix.Cofacter(3, 3); + this = Matrix4d.Invert(this); } + #endregion + + #region public void Transpose() + public void Transpose() { - Functions.Swap(ref R0C1, ref R1C0); - Functions.Swap(ref R0C2, ref R2C0); - Functions.Swap(ref R0C3, ref R3C0); - Functions.Swap(ref R1C2, ref R2C1); - Functions.Swap(ref R1C3, ref R3C1); - Functions.Swap(ref R2C3, ref R3C2); - } - public void Transpose(out Matrix4d result) - { - result.R0C0 = R0C0; - result.R0C1 = R1C0; - result.R0C2 = R2C0; - result.R0C3 = R3C0; - result.R1C0 = R0C1; - result.R1C1 = R1C1; - result.R1C2 = R2C1; - result.R1C3 = R3C1; - result.R2C0 = R0C2; - result.R2C1 = R1C2; - result.R2C2 = R2C2; - result.R2C3 = R3C2; - result.R3C0 = R0C3; - result.R3C1 = R1C3; - result.R3C2 = R2C3; - result.R3C3 = R3C3; - } - public static void Transpose(ref Matrix4d matrix, out Matrix4d result) - { - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R1C0; - result.R0C2 = matrix.R2C0; - result.R0C3 = matrix.R3C0; - result.R1C0 = matrix.R0C1; - result.R1C1 = matrix.R1C1; - result.R1C2 = matrix.R2C1; - result.R1C3 = matrix.R3C1; - result.R2C0 = matrix.R0C2; - result.R2C1 = matrix.R1C2; - result.R2C2 = matrix.R2C2; - result.R2C3 = matrix.R3C2; - result.R3C0 = matrix.R0C3; - result.R3C1 = matrix.R1C3; - result.R3C2 = matrix.R2C3; - result.R3C3 = matrix.R3C3; - } - - public void Adjoint(out Matrix4d result) - { - CofacterMatrix(out result); - result.Transpose(); - } - public static void Adjoint(ref Matrix4d matrix, out Matrix4d result) - { - matrix.CofacterMatrix(out result); - result.Transpose(); - } - - public void Inverse(out Matrix4d result) - { - CofacterMatrix(out result); - result.Transpose(); - result.Multiply(1 / Determinant); - } - public static void Inverse(ref Matrix4d matrix, out Matrix4d result) - { - matrix.CofacterMatrix(out result); - result.Transpose(); - result.Multiply(1 / result.Determinant); + this = Matrix4d.Transpose(this); } #endregion - #region Transformation Functions + #endregion - public void Transform(ref Vector4d vector) + #region Static + + #region Scale Functions + + /// + /// Build a scaling matrix + /// + /// Single scale factor for x,y and z axes + /// A scaling matrix + public static Matrix4d Scale(double scale) { - double x = R0C0 * vector.X + R0C1 * vector.Y + R0C2 * vector.Z + R0C3 * vector.W; - double y = R1C0 * vector.X + R1C1 * vector.Y + R1C2 * vector.Z + R1C3 * vector.W; - double z = R2C0 * vector.X + R2C1 * vector.Y + R2C2 * vector.Z + R2C3 * vector.W; - vector.W = R3C0 * vector.X + R3C1 * vector.Y + R3C2 * vector.Z + R3C3 * vector.W; - vector.X = x; - vector.Y = y; - vector.Z = z; - } - public static void Transform(ref Matrix4d matrix, ref Vector4d vector) - { - double x = matrix.R0C0 * vector.X + matrix.R0C1 * vector.Y + matrix.R0C2 * vector.Z + matrix.R0C3 * vector.W; - double y = matrix.R1C0 * vector.X + matrix.R1C1 * vector.Y + matrix.R1C2 * vector.Z + matrix.R1C3 * vector.W; - double z = matrix.R2C0 * vector.X + matrix.R2C1 * vector.Y + matrix.R2C2 * vector.Z + matrix.R2C3 * vector.W; - vector.W = matrix.R3C0 * vector.X + matrix.R3C1 * vector.Y + matrix.R3C2 * vector.Z + matrix.R3C3 * vector.W; - vector.X = x; - vector.Y = y; - vector.Z = z; - } - public void Transform(ref Vector4d vector, out Vector4d result) - { - result.X = R0C0 * vector.X + R0C1 * vector.Y + R0C2 * vector.Z + R0C3 * vector.W; - result.Y = R1C0 * vector.X + R1C1 * vector.Y + R1C2 * vector.Z + R1C3 * vector.W; - result.Z = R2C0 * vector.X + R2C1 * vector.Y + R2C2 * vector.Z + R2C3 * vector.W; - result.W = R3C0 * vector.X + R3C1 * vector.Y + R3C2 * vector.Z + R3C3 * vector.W; - } - public static void Transform(ref Matrix4d matrix, ref Vector4d vector, out Vector4d result) - { - result.X = matrix.R0C0 * vector.X + matrix.R0C1 * vector.Y + matrix.R0C2 * vector.Z + matrix.R0C3 * vector.W; - result.Y = matrix.R1C0 * vector.X + matrix.R1C1 * vector.Y + matrix.R1C2 * vector.Z + matrix.R1C3 * vector.W; - result.Z = matrix.R2C0 * vector.X + matrix.R2C1 * vector.Y + matrix.R2C2 * vector.Z + matrix.R2C3 * vector.W; - result.W = matrix.R3C0 * vector.X + matrix.R3C1 * vector.Y + matrix.R3C2 * vector.Z + matrix.R3C3 * vector.W; - } - public void Transform(ref Vector3d vector) - { - double x = R0C0 * vector.X + R0C1 * vector.Y + R0C2 * vector.Z; - double y = R1C0 * vector.X + R1C1 * vector.Y + R1C2 * vector.Z; - vector.Z = R2C0 * vector.X + R2C1 * vector.Y + R2C2 * vector.Z; - vector.X = x; - vector.Y = y; - } - public static void Transform(ref Matrix4d matrix, ref Vector3d vector) - { - double x = matrix.R0C0 * vector.X + matrix.R0C1 * vector.Y + matrix.R0C2 * vector.Z; - double y = matrix.R1C0 * vector.X + matrix.R1C1 * vector.Y + matrix.R1C2 * vector.Z; - vector.Z = matrix.R2C0 * vector.X + matrix.R2C1 * vector.Y + matrix.R2C2 * vector.Z; - vector.X = x; - vector.Y = y; - } - public void Transform(ref Vector3d vector, out Vector3d result) - { - result.X = R0C0 * vector.X + R0C1 * vector.Y + R0C2 * vector.Z; - result.Y = R1C0 * vector.X + R1C1 * vector.Y + R1C2 * vector.Z; - result.Z = R2C0 * vector.X + R2C1 * vector.Y + R2C2 * vector.Z; - } - public static void Transform(ref Matrix4d matrix, ref Vector3d vector, out Vector3d result) - { - result.X = matrix.R0C0 * vector.X + matrix.R0C1 * vector.Y + matrix.R0C2 * vector.Z; - result.Y = matrix.R1C0 * vector.X + matrix.R1C1 * vector.Y + matrix.R1C2 * vector.Z; - result.Z = matrix.R2C0 * vector.X + matrix.R2C1 * vector.Y + matrix.R2C2 * vector.Z; + return Scale(scale, scale, scale); } - public void RotateX(double angle) + /// + /// Build a scaling matrix + /// + /// Scale factors for x,y and z axes + /// A scaling matrix + public static Matrix4d Scale(Vector3d scale) { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - double r1c0 = cos * R1C0 + sin * R2C0; - double r1c1 = cos * R1C1 + sin * R2C1; - double r1c2 = cos * R1C2 + sin * R2C2; - double r1c3 = cos * R1C3 + sin * R2C3; - - R2C0 = cos * R2C0 - sin * R1C0; - R2C1 = cos * R2C1 - sin * R1C1; - R2C2 = cos * R2C2 - sin * R1C2; - R2C3 = cos * R2C3 - sin * R1C3; - - R1C0 = r1c0; - R1C1 = r1c1; - R1C2 = r1c2; - R1C3 = r1c3; - } - public void RotateX(double angle, out Matrix4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.R0C0 = R0C0; - result.R0C1 = R0C1; - result.R0C2 = R0C2; - result.R0C3 = R0C3; - result.R1C0 = cos * R1C0 + sin * R2C0; - result.R1C1 = cos * R1C1 + sin * R2C1; - result.R1C2 = cos * R1C2 + sin * R2C2; - result.R1C3 = cos * R1C3 + sin * R2C3; - result.R2C0 = cos * R2C0 - sin * R1C0; - result.R2C1 = cos * R2C1 - sin * R1C1; - result.R2C2 = cos * R2C2 - sin * R1C2; - result.R2C3 = cos * R2C3 - sin * R1C3; - result.R3C0 = R3C0; - result.R3C1 = R3C1; - result.R3C2 = R3C2; - result.R3C3 = R3C3; - } - public static void RotateX(ref Matrix4d matrix, double angle, out Matrix4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C1; - result.R0C2 = matrix.R0C2; - result.R0C3 = matrix.R0C3; - result.R1C0 = cos * matrix.R1C0 + sin * matrix.R2C0; - result.R1C1 = cos * matrix.R1C1 + sin * matrix.R2C1; - result.R1C2 = cos * matrix.R1C2 + sin * matrix.R2C2; - result.R1C3 = cos * matrix.R1C3 + sin * matrix.R2C3; - result.R2C0 = cos * matrix.R2C0 - sin * matrix.R1C0; - result.R2C1 = cos * matrix.R2C1 - sin * matrix.R1C1; - result.R2C2 = cos * matrix.R2C2 - sin * matrix.R1C2; - result.R2C3 = cos * matrix.R2C3 - sin * matrix.R1C3; - result.R3C0 = matrix.R3C0; - result.R3C1 = matrix.R3C1; - result.R3C2 = matrix.R3C2; - result.R3C3 = matrix.R3C3; - } - public static void RotateXMatrix(double angle, out Matrix4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.R0C0 = 1; - result.R0C1 = 0; - result.R0C2 = 0; - result.R0C3 = 0; - result.R1C0 = 0; - result.R1C1 = cos; - result.R1C2 = sin; - result.R1C3 = 0; - result.R2C0 = 0; - result.R2C1 = -sin; - result.R2C2 = cos; - result.R2C3 = 0; - result.R3C0 = 0; - result.R3C1 = 0; - result.R3C2 = 0; - result.R3C3 = 1; + return Scale(scale.X, scale.Y, scale.Z); } - public void RotateY(double angle) + /// + /// Build a scaling matrix + /// + /// Scale factor for x-axis + /// Scale factor for y-axis + /// Scale factor for z-axis + /// A scaling matrix + public static Matrix4d Scale(double x, double y, double z) { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - double r0c0 = cos * R0C0 - sin * R2C0; - double r0c1 = cos * R0C1 - sin * R2C1; - double r0c2 = cos * R0C2 - sin * R2C2; - double r0c3 = cos * R0C3 - sin * R2C3; - - R2C0 = sin * R0C0 + cos * R2C0; - R2C1 = sin * R0C1 + cos * R2C1; - R2C2 = sin * R0C2 + cos * R2C2; - R2C3 = sin * R0C3 + cos * R2C3; - - R0C0 = r0c0; - R0C1 = r0c1; - R0C2 = r0c2; - R0C3 = r0c3; - - } - public void RotateY(double angle, out Matrix4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.R0C0 = cos * R0C0 - sin * R2C0; - result.R0C1 = cos * R0C1 - sin * R2C1; - result.R0C2 = cos * R0C2 - sin * R2C2; - result.R0C3 = cos * R0C3 - sin * R2C3; - result.R1C0 = R1C0; - result.R1C1 = R1C1; - result.R1C2 = R1C2; - result.R1C3 = R1C3; - result.R2C0 = sin * R0C0 + cos * R2C0; - result.R2C1 = sin * R0C1 + cos * R2C1; - result.R2C2 = sin * R0C2 + cos * R2C2; - result.R2C3 = sin * R0C3 + cos * R2C3; - result.R3C0 = R3C0; - result.R3C1 = R3C1; - result.R3C2 = R3C2; - result.R3C3 = R3C3; - } - public static void RotateY(ref Matrix4d matrix, double angle, out Matrix4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.R0C0 = cos * matrix.R0C0 - sin * matrix.R2C0; - result.R0C1 = cos * matrix.R0C1 - sin * matrix.R2C1; - result.R0C2 = cos * matrix.R0C2 - sin * matrix.R2C2; - result.R0C3 = cos * matrix.R0C3 - sin * matrix.R2C3; - result.R1C0 = matrix.R1C0; - result.R1C1 = matrix.R1C1; - result.R1C2 = matrix.R1C2; - result.R1C3 = matrix.R1C3; - result.R2C0 = sin * matrix.R0C0 + cos * matrix.R2C0; - result.R2C1 = sin * matrix.R0C1 + cos * matrix.R2C1; - result.R2C2 = sin * matrix.R0C2 + cos * matrix.R2C2; - result.R2C3 = sin * matrix.R0C3 + cos * matrix.R2C3; - result.R3C0 = matrix.R3C0; - result.R3C1 = matrix.R3C1; - result.R3C2 = matrix.R3C2; - result.R3C3 = matrix.R3C3; - } - public static void RotateYMatrix(double angle, out Matrix4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.R0C0 = cos; - result.R0C1 = 0; - result.R0C2 = -sin; - result.R0C3 = 0; - result.R1C0 = 0; - result.R1C1 = 1; - result.R1C2 = 0; - result.R1C3 = 0; - result.R2C0 = sin; - result.R2C1 = 0; - result.R2C2 = cos; - result.R2C3 = 0; - result.R3C0 = 0; - result.R3C1 = 0; - result.R3C2 = 0; - result.R3C3 = 1; - } - - public void RotateZ(double angle) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - double r0c0 = cos * R0C0 + sin * R1C0; - double r0c1 = cos * R0C1 + sin * R1C1; - double r0c2 = cos * R0C2 + sin * R1C2; - double r0c3 = cos * R0C3 + sin * R1C3; - - R1C0 = cos * R1C0 - sin * R0C0; - R1C1 = cos * R1C1 - sin * R0C1; - R1C2 = cos * R1C2 - sin * R0C2; - R1C3 = cos * R1C3 - sin * R0C3; - - R0C0 = r0c0; - R0C1 = r0c1; - R0C2 = r0c2; - R0C3 = r0c3; - } - public void RotateZ(double angle, out Matrix4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.R0C0 = cos * R0C0 + sin * R1C0; - result.R0C1 = cos * R0C1 + sin * R1C1; - result.R0C2 = cos * R0C2 + sin * R1C2; - result.R0C3 = cos * R0C3 + sin * R1C3; - result.R1C0 = cos * R1C0 - sin * R0C0; - result.R1C1 = cos * R1C1 - sin * R0C1; - result.R1C2 = cos * R1C2 - sin * R0C2; - result.R1C3 = cos * R1C3 - sin * R0C3; - result.R2C0 = R2C0; - result.R2C1 = R2C1; - result.R2C2 = R2C2; - result.R2C3 = R2C3; - result.R3C0 = R3C0; - result.R3C1 = R3C1; - result.R3C2 = R3C2; - result.R3C3 = R3C3; - } - public static void RotateZ(ref Matrix4d matrix, double angle, out Matrix4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.R0C0 = cos * matrix.R0C0 + sin * matrix.R1C0; - result.R0C1 = cos * matrix.R0C1 + sin * matrix.R1C1; - result.R0C2 = cos * matrix.R0C2 + sin * matrix.R1C2; - result.R0C3 = cos * matrix.R0C3 + sin * matrix.R1C3; - result.R1C0 = cos * matrix.R1C0 - sin * matrix.R0C0; - result.R1C1 = cos * matrix.R1C1 - sin * matrix.R0C1; - result.R1C2 = cos * matrix.R1C2 - sin * matrix.R0C2; - result.R1C3 = cos * matrix.R1C3 - sin * matrix.R0C3; - result.R2C0 = matrix.R2C0; - result.R2C1 = matrix.R2C1; - result.R2C2 = matrix.R2C2; - result.R2C3 = matrix.R2C3; - result.R3C0 = matrix.R3C0; - result.R3C1 = matrix.R3C1; - result.R3C2 = matrix.R3C2; - result.R3C3 = matrix.R3C3; - } - public static void RotateZMatrix(double angle, out Matrix4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.R0C0 = cos; - result.R0C1 = sin; - result.R0C2 = 0; - result.R0C3 = 0; - result.R1C0 = -sin; - result.R1C1 = cos; - result.R1C2 = 0; - result.R1C3 = 0; - result.R2C0 = 0; - result.R2C1 = 0; - result.R2C2 = 1; - result.R2C3 = 0; - result.R3C0 = 0; - result.R3C1 = 0; - result.R3C2 = 0; - result.R3C3 = 1; - } - - public void Rotate(ref Vector3d axis, double angle) - { - Vector3d axisNormalized; - axis.Normalize(out axisNormalized); - double x = axisNormalized.X; - double y = axisNormalized.Y; - double z = axisNormalized.Z; - double angleRadians = Functions.DTOR * angle; - double cos = (double)System.Math.Cos(angleRadians); - double sin = (double)System.Math.Sin(angleRadians); - double oneMinusCos = 1 - cos; - double xOneMinusCos = x * oneMinusCos; - double yOneMinusCos = y * oneMinusCos; - double zOneMinusCos = z * oneMinusCos; - double xxOneMinusCos = x * xOneMinusCos; - double xyOneMinusCos = x * yOneMinusCos; - double xzOneMinusCos = x * zOneMinusCos; - double yyOneMinusCos = y * yOneMinusCos; - double yzOneMinusCos = y * zOneMinusCos; - double zzOneMinusCos = z * zOneMinusCos; - double xSin = x * sin; - double ySin = y * sin; - double zSin = z * sin; - - double rotateR0C0 = xxOneMinusCos + cos; - double rotateR0C1 = xyOneMinusCos + zSin; - double rotateR0C2 = xzOneMinusCos - ySin; - double rotateR1C0 = xyOneMinusCos - zSin; - double rotateR1C1 = yyOneMinusCos + cos; - double rotateR1C2 = yzOneMinusCos + xSin; - double rotateR2C0 = xzOneMinusCos + ySin; - double rotateR2C1 = yzOneMinusCos - xSin; - double rotateR2C2 = zzOneMinusCos + cos; - - double r0c0 = rotateR0C0 * R0C0 + rotateR0C1 * R1C0 + rotateR0C2 * R2C0; - double r0c1 = rotateR0C0 * R0C1 + rotateR0C1 * R1C1 + rotateR0C2 * R2C1; - double r0c2 = rotateR0C0 * R0C2 + rotateR0C1 * R1C2 + rotateR0C2 * R2C2; - double r0c3 = rotateR0C0 * R0C3 + rotateR0C1 * R1C3 + rotateR0C2 * R2C3; - - double r1c0 = rotateR1C0 * R0C0 + rotateR1C1 * R1C0 + rotateR1C2 * R2C0; - double r1c1 = rotateR1C0 * R0C1 + rotateR1C1 * R1C1 + rotateR1C2 * R2C1; - double r1c2 = rotateR1C0 * R0C2 + rotateR1C1 * R1C2 + rotateR1C2 * R2C2; - double r1c3 = rotateR1C0 * R0C3 + rotateR1C1 * R1C3 + rotateR1C2 * R2C3; - - R2C0 = rotateR2C0 * R0C0 + rotateR2C1 * R1C0 + rotateR2C2 * R2C0; - R2C1 = rotateR2C0 * R0C1 + rotateR2C1 * R1C1 + rotateR2C2 * R2C1; - R2C2 = rotateR2C0 * R0C2 + rotateR2C1 * R1C2 + rotateR2C2 * R2C2; - R2C3 = rotateR2C0 * R0C3 + rotateR2C1 * R1C3 + rotateR2C2 * R2C3; - - R0C0 = r0c0; - R0C1 = r0c1; - R0C2 = r0c2; - R0C3 = r0c3; - - R1C0 = r1c0; - R1C1 = r1c1; - R1C2 = r1c2; - R1C3 = r1c3; - } - public void Rotate(ref Vector3d axis, double angle, out Matrix4d result) - { - Vector3d axisNormalized; - axis.Normalize(out axisNormalized); - double x = axisNormalized.X; - double y = axisNormalized.Y; - double z = axisNormalized.Z; - double angleRadians = Functions.DTOR * angle; - double cos = (double)System.Math.Cos(angleRadians); - double sin = (double)System.Math.Sin(angleRadians); - double oneMinusCos = 1 - cos; - double xOneMinusCos = x * oneMinusCos; - double yOneMinusCos = y * oneMinusCos; - double zOneMinusCos = z * oneMinusCos; - double xxOneMinusCos = x * xOneMinusCos; - double xyOneMinusCos = x * yOneMinusCos; - double xzOneMinusCos = x * zOneMinusCos; - double yyOneMinusCos = y * yOneMinusCos; - double yzOneMinusCos = y * zOneMinusCos; - double zzOneMinusCos = z * zOneMinusCos; - double xSin = x * sin; - double ySin = y * sin; - double zSin = z * sin; - - double rotateR0C0 = xxOneMinusCos + cos; - double rotateR0C1 = xyOneMinusCos + zSin; - double rotateR0C2 = xzOneMinusCos - ySin; - double rotateR1C0 = xyOneMinusCos - zSin; - double rotateR1C1 = yyOneMinusCos + cos; - double rotateR1C2 = yzOneMinusCos + xSin; - double rotateR2C0 = xzOneMinusCos + ySin; - double rotateR2C1 = yzOneMinusCos - xSin; - double rotateR2C2 = zzOneMinusCos + cos; - - result.R0C0 = rotateR0C0 * R0C0 + rotateR0C1 * R1C0 + rotateR0C2 * R2C0; - result.R0C1 = rotateR0C0 * R0C1 + rotateR0C1 * R1C1 + rotateR0C2 * R2C1; - result.R0C2 = rotateR0C0 * R0C2 + rotateR0C1 * R1C2 + rotateR0C2 * R2C2; - result.R0C3 = rotateR0C0 * R0C3 + rotateR0C1 * R1C3 + rotateR0C2 * R2C3; - result.R1C0 = rotateR1C0 * R0C0 + rotateR1C1 * R1C0 + rotateR1C2 * R2C0; - result.R1C1 = rotateR1C0 * R0C1 + rotateR1C1 * R1C1 + rotateR1C2 * R2C1; - result.R1C2 = rotateR1C0 * R0C2 + rotateR1C1 * R1C2 + rotateR1C2 * R2C2; - result.R1C3 = rotateR1C0 * R0C3 + rotateR1C1 * R1C3 + rotateR1C2 * R2C3; - result.R2C0 = rotateR2C0 * R0C0 + rotateR2C1 * R1C0 + rotateR2C2 * R2C0; - result.R2C1 = rotateR2C0 * R0C1 + rotateR2C1 * R1C1 + rotateR2C2 * R2C1; - result.R2C2 = rotateR2C0 * R0C2 + rotateR2C1 * R1C2 + rotateR2C2 * R2C2; - result.R2C3 = rotateR2C0 * R0C3 + rotateR2C1 * R1C3 + rotateR2C2 * R2C3; - result.R3C0 = R3C0; - result.R3C1 = R3C1; - result.R3C2 = R3C2; - result.R3C3 = R3C3; - } - public static void Rotate(ref Matrix4d matrix, ref Vector3d axis, double angle, out Matrix4d result) - { - Vector3d axisNormalized; - axis.Normalize(out axisNormalized); - double x = axisNormalized.X; - double y = axisNormalized.Y; - double z = axisNormalized.Z; - double angleRadians = Functions.DTOR * angle; - double cos = (double)System.Math.Cos(angleRadians); - double sin = (double)System.Math.Sin(angleRadians); - double oneMinusCos = 1 - cos; - double xOneMinusCos = x * oneMinusCos; - double yOneMinusCos = y * oneMinusCos; - double zOneMinusCos = z * oneMinusCos; - double xxOneMinusCos = x * xOneMinusCos; - double xyOneMinusCos = x * yOneMinusCos; - double xzOneMinusCos = x * zOneMinusCos; - double yyOneMinusCos = y * yOneMinusCos; - double yzOneMinusCos = y * zOneMinusCos; - double zzOneMinusCos = z * zOneMinusCos; - double xSin = x * sin; - double ySin = y * sin; - double zSin = z * sin; - - double rotateR0C0 = xxOneMinusCos + cos; - double rotateR0C1 = xyOneMinusCos + zSin; - double rotateR0C2 = xzOneMinusCos - ySin; - double rotateR1C0 = xyOneMinusCos - zSin; - double rotateR1C1 = yyOneMinusCos + cos; - double rotateR1C2 = yzOneMinusCos + xSin; - double rotateR2C0 = xzOneMinusCos + ySin; - double rotateR2C1 = yzOneMinusCos - xSin; - double rotateR2C2 = zzOneMinusCos + cos; - - - result.R0C0 = rotateR0C0 * matrix.R0C0 + rotateR0C1 * matrix.R1C0 + rotateR0C2 * matrix.R2C0; - result.R0C1 = rotateR0C0 * matrix.R0C1 + rotateR0C1 * matrix.R1C1 + rotateR0C2 * matrix.R2C1; - result.R0C2 = rotateR0C0 * matrix.R0C2 + rotateR0C1 * matrix.R1C2 + rotateR0C2 * matrix.R2C2; - result.R0C3 = rotateR0C0 * matrix.R0C3 + rotateR0C1 * matrix.R1C3 + rotateR0C2 * matrix.R2C3; - result.R1C0 = rotateR1C0 * matrix.R0C0 + rotateR1C1 * matrix.R1C0 + rotateR1C2 * matrix.R2C0; - result.R1C1 = rotateR1C0 * matrix.R0C1 + rotateR1C1 * matrix.R1C1 + rotateR1C2 * matrix.R2C1; - result.R1C2 = rotateR1C0 * matrix.R0C2 + rotateR1C1 * matrix.R1C2 + rotateR1C2 * matrix.R2C2; - result.R1C3 = rotateR1C0 * matrix.R0C3 + rotateR1C1 * matrix.R1C3 + rotateR1C2 * matrix.R2C3; - result.R2C0 = rotateR2C0 * matrix.R0C0 + rotateR2C1 * matrix.R1C0 + rotateR2C2 * matrix.R2C0; - result.R2C1 = rotateR2C0 * matrix.R0C1 + rotateR2C1 * matrix.R1C1 + rotateR2C2 * matrix.R2C1; - result.R2C2 = rotateR2C0 * matrix.R0C2 + rotateR2C1 * matrix.R1C2 + rotateR2C2 * matrix.R2C2; - result.R2C3 = rotateR2C0 * matrix.R0C3 + rotateR2C1 * matrix.R1C3 + rotateR2C2 * matrix.R2C3; - result.R3C0 = matrix.R3C0; - result.R3C1 = matrix.R3C1; - result.R3C2 = matrix.R3C2; - result.R3C3 = matrix.R3C3; - } - public static void RotateMatrix(ref Vector3d axis, double angle, out Matrix4d result) - { - Vector3d axisNormalized; - axis.Normalize(out axisNormalized); - double x = axisNormalized.X; - double y = axisNormalized.Y; - double z = axisNormalized.Z; - double angleRadians = Functions.DTOR * angle; - double cos = (double)System.Math.Cos(angleRadians); - double sin = (double)System.Math.Sin(angleRadians); - double oneMinusCos = 1 - cos; - double xOneMinusCos = x * oneMinusCos; - double yOneMinusCos = y * oneMinusCos; - double zOneMinusCos = z * oneMinusCos; - double xxOneMinusCos = x * xOneMinusCos; - double xyOneMinusCos = x * yOneMinusCos; - double xzOneMinusCos = x * zOneMinusCos; - double yyOneMinusCos = y * yOneMinusCos; - double yzOneMinusCos = y * zOneMinusCos; - double zzOneMinusCos = z * zOneMinusCos; - double xSin = x * sin; - double ySin = y * sin; - double zSin = z * sin; - - result.R0C0 = xxOneMinusCos + cos; - result.R0C1 = xyOneMinusCos + zSin; - result.R0C2 = xzOneMinusCos - ySin; - result.R0C3 = 0; - result.R1C0 = xyOneMinusCos - zSin; - result.R1C1 = yyOneMinusCos + cos; - result.R1C2 = yzOneMinusCos + xSin; - result.R1C3 = 0; - result.R2C0 = xzOneMinusCos + ySin; - result.R2C1 = yzOneMinusCos - xSin; - result.R2C2 = zzOneMinusCos + cos; - result.R2C3 = 0; - result.R3C0 = 0; - result.R3C1 = 0; - result.R3C2 = 0; - result.R3C3 = 1; - } - - public void Translate(double x, double y, double z) - { - R3C0 = x * R0C0 + y * R1C0 + z * R2C0 + R3C0; - R3C1 = x * R0C1 + y * R1C1 + z * R2C1 + R3C1; - R3C2 = x * R0C2 + y * R1C2 + z * R2C2 + R3C2; - R3C3 = x * R0C3 + y * R1C3 + z * R2C3 + R3C3; - } - public void Translate(double x, double y, double z, out Matrix4d result) - { - result.R0C0 = R0C0; - result.R0C1 = R0C1; - result.R0C2 = R0C2; - result.R0C3 = R0C3; - result.R1C0 = R1C0; - result.R1C1 = R1C1; - result.R1C2 = R1C2; - result.R1C3 = R1C3; - result.R2C0 = R2C0; - result.R2C1 = R2C1; - result.R2C2 = R2C2; - result.R2C3 = R2C3; - result.R3C0 = x * R0C0 + y * R1C0 + z * R2C0 + R3C0; - result.R3C1 = x * R0C1 + y * R1C1 + z * R2C1 + R3C1; - result.R3C2 = x * R0C2 + y * R1C2 + z * R2C2 + R3C2; - result.R3C3 = x * R0C3 + y * R1C3 + z * R2C3 + R3C3; - } - public static void Translate(ref Matrix4d matrix, double x, double y, double z, out Matrix4d result) - { - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C1; - result.R0C2 = matrix.R0C2; - result.R0C3 = matrix.R0C3; - result.R1C0 = matrix.R1C0; - result.R1C1 = matrix.R1C1; - result.R1C2 = matrix.R1C2; - result.R1C3 = matrix.R1C3; - result.R2C0 = matrix.R2C0; - result.R2C1 = matrix.R2C1; - result.R2C2 = matrix.R2C2; - result.R2C3 = matrix.R2C3; - result.R3C0 = x * matrix.R0C0 + y * matrix.R1C0 + z * matrix.R2C0 + matrix.R3C0; - result.R3C1 = x * matrix.R0C1 + y * matrix.R1C1 + z * matrix.R2C1 + matrix.R3C1; - result.R3C2 = x * matrix.R0C2 + y * matrix.R1C2 + z * matrix.R2C2 + matrix.R3C2; - result.R3C3 = x * matrix.R0C3 + y * matrix.R1C3 + z * matrix.R2C3 + matrix.R3C3; - } - public static void TranslateMatrix(double x, double y, double z, out Matrix4d result) - { - result.R0C0 = 1; - result.R0C1 = 0; - result.R0C2 = 0; - result.R0C3 = 0; - result.R1C0 = 0; - result.R1C1 = 1; - result.R1C2 = 0; - result.R1C3 = 0; - result.R2C0 = 0; - result.R2C1 = 0; - result.R2C2 = 1; - result.R2C3 = 0; - result.R3C0 = x; - result.R3C1 = y; - result.R3C2 = z; - result.R3C3 = 1; - } - - public void Translate(ref Vector3d vector) - { - R3C0 = vector.X * R0C0 + vector.Y * R1C0 + vector.Z * R2C0 + R3C0; - R3C1 = vector.X * R0C1 + vector.Y * R1C1 + vector.Z * R2C1 + R3C1; - R3C2 = vector.X * R0C2 + vector.Y * R1C2 + vector.Z * R2C2 + R3C2; - R3C3 = vector.X * R0C3 + vector.Y * R1C3 + vector.Z * R2C3 + R3C3; - } - public void Translate(ref Vector3d vector, out Matrix4d result) - { - result.R0C0 = R0C0; - result.R0C1 = R0C1; - result.R0C2 = R0C2; - result.R0C3 = R0C3; - result.R1C0 = R1C0; - result.R1C1 = R1C1; - result.R1C2 = R1C2; - result.R1C3 = R1C3; - result.R2C0 = R2C0; - result.R2C1 = R2C1; - result.R2C2 = R2C2; - result.R2C3 = R2C3; - result.R3C0 = vector.X * R0C0 + vector.Y * R1C0 + vector.Z * R2C0 + R3C0; - result.R3C1 = vector.X * R0C1 + vector.Y * R1C1 + vector.Z * R2C1 + R3C1; - result.R3C2 = vector.X * R0C2 + vector.Y * R1C2 + vector.Z * R2C2 + R3C2; - result.R3C3 = vector.X * R0C3 + vector.Y * R1C3 + vector.Z * R2C3 + R3C3; - } - public static void Translate(ref Matrix4d matrix, ref Vector3d vector, out Matrix4d result) - { - result.R0C0 = matrix.R0C0; - result.R0C1 = matrix.R0C1; - result.R0C2 = matrix.R0C2; - result.R0C3 = matrix.R0C3; - result.R1C0 = matrix.R1C0; - result.R1C1 = matrix.R1C1; - result.R1C2 = matrix.R1C2; - result.R1C3 = matrix.R1C3; - result.R2C0 = matrix.R2C0; - result.R2C1 = matrix.R2C1; - result.R2C2 = matrix.R2C2; - result.R2C3 = matrix.R2C3; - result.R3C0 = vector.X * matrix.R0C0 + vector.Y * matrix.R1C0 + vector.Z * matrix.R2C0 + matrix.R3C0; - result.R3C1 = vector.X * matrix.R0C1 + vector.Y * matrix.R1C1 + vector.Z * matrix.R2C1 + matrix.R3C1; - result.R3C2 = vector.X * matrix.R0C2 + vector.Y * matrix.R1C2 + vector.Z * matrix.R2C2 + matrix.R3C2; - result.R3C3 = vector.X * matrix.R0C3 + vector.Y * matrix.R1C3 + vector.Z * matrix.R2C3 + matrix.R3C3; - } - public static void TranslateMatrix(ref Vector3d vector, out Matrix4d result) - { - result.R0C0 = 1; - result.R0C1 = 0; - result.R0C2 = 0; - result.R0C3 = 0; - result.R1C0 = 0; - result.R1C1 = 1; - result.R1C2 = 0; - result.R1C3 = 0; - result.R2C0 = 0; - result.R2C1 = 0; - result.R2C2 = 1; - result.R2C3 = 0; - result.R3C0 = vector.X; - result.R3C1 = vector.Y; - result.R3C2 = vector.Z; - result.R3C3 = 1; - } - - public void Scale(double x, double y, double z) - { - R0C0 = x * R0C0; - R0C1 = x * R0C1; - R0C2 = x * R0C2; - R0C3 = x * R0C3; - R1C0 = y * R1C0; - R1C1 = y * R1C1; - R1C2 = y * R1C2; - R1C3 = y * R1C3; - R2C0 = z * R2C0; - R2C1 = z * R2C1; - R2C2 = z * R2C2; - R2C3 = z * R2C3; - } - public void Scale(double x, double y, double z, out Matrix4d result) - { - result.R0C0 = x * R0C0; - result.R0C1 = x * R0C1; - result.R0C2 = x * R0C2; - result.R0C3 = x * R0C3; - result.R1C0 = y * R1C0; - result.R1C1 = y * R1C1; - result.R1C2 = y * R1C2; - result.R1C3 = y * R1C3; - result.R2C0 = z * R2C0; - result.R2C1 = z * R2C1; - result.R2C2 = z * R2C2; - result.R2C3 = z * R2C3; - result.R3C0 = R3C0; - result.R3C1 = R3C1; - result.R3C2 = R3C2; - result.R3C3 = R3C3; - } - public static void Scale(ref Matrix4d matrix, double x, double y, double z, out Matrix4d result) - { - result.R0C0 = x * matrix.R0C0; - result.R0C1 = x * matrix.R0C1; - result.R0C2 = x * matrix.R0C2; - result.R0C3 = x * matrix.R0C3; - result.R1C0 = y * matrix.R1C0; - result.R1C1 = y * matrix.R1C1; - result.R1C2 = y * matrix.R1C2; - result.R1C3 = y * matrix.R1C3; - result.R2C0 = z * matrix.R2C0; - result.R2C1 = z * matrix.R2C1; - result.R2C2 = z * matrix.R2C2; - result.R2C3 = z * matrix.R2C3; - result.R3C0 = matrix.R3C0; - result.R3C1 = matrix.R3C1; - result.R3C2 = matrix.R3C2; - result.R3C3 = matrix.R3C3; - } - public static void ScaleMatrix(double x, double y, double z, out Matrix4d result) - { - result.R0C0 = x; - result.R0C1 = 0; - result.R0C2 = 0; - result.R0C3 = 0; - result.R1C0 = 0; - result.R1C1 = y; - result.R1C2 = 0; - result.R1C3 = 0; - result.R2C0 = 0; - result.R2C1 = 0; - result.R2C2 = z; - result.R2C3 = 0; - result.R3C0 = 0; - result.R3C1 = 0; - result.R3C2 = 0; - result.R3C3 = 1; - } - - public void Scale(ref Vector3d vector) - { - R0C0 = vector.X * R0C0; - R0C1 = vector.X * R0C1; - R0C2 = vector.X * R0C2; - R0C3 = vector.X * R0C3; - R1C0 = vector.Y * R1C0; - R1C1 = vector.Y * R1C1; - R1C2 = vector.Y * R1C2; - R1C3 = vector.Y * R1C3; - R2C0 = vector.Z * R2C0; - R2C1 = vector.Z * R2C1; - R2C2 = vector.Z * R2C2; - R2C3 = vector.Z * R2C3; - } - public void Scale(ref Vector3d vector, out Matrix4d result) - { - result.R0C0 = vector.X * R0C0; - result.R0C1 = vector.X * R0C1; - result.R0C2 = vector.X * R0C2; - result.R0C3 = vector.X * R0C3; - result.R1C0 = vector.Y * R1C0; - result.R1C1 = vector.Y * R1C1; - result.R1C2 = vector.Y * R1C2; - result.R1C3 = vector.Y * R1C3; - result.R2C0 = vector.Z * R2C0; - result.R2C1 = vector.Z * R2C1; - result.R2C2 = vector.Z * R2C2; - result.R2C3 = vector.Z * R2C3; - result.R3C0 = R3C0; - result.R3C1 = R3C1; - result.R3C2 = R3C2; - result.R3C3 = R3C3; - } - public static void Scale(ref Matrix4d matrix, ref Vector3d vector, out Matrix4d result) - { - result.R0C0 = vector.X * matrix.R0C0; - result.R0C1 = vector.X * matrix.R0C1; - result.R0C2 = vector.X * matrix.R0C2; - result.R0C3 = vector.X * matrix.R0C3; - result.R1C0 = vector.Y * matrix.R1C0; - result.R1C1 = vector.Y * matrix.R1C1; - result.R1C2 = vector.Y * matrix.R1C2; - result.R1C3 = vector.Y * matrix.R1C3; - result.R2C0 = vector.Z * matrix.R2C0; - result.R2C1 = vector.Z * matrix.R2C1; - result.R2C2 = vector.Z * matrix.R2C2; - result.R2C3 = vector.Z * matrix.R2C3; - result.R3C0 = matrix.R3C0; - result.R3C1 = matrix.R3C1; - result.R3C2 = matrix.R3C2; - result.R3C3 = matrix.R3C3; - } - public static void ScaleMatrix(ref Vector3d vector, out Matrix4d result) - { - result.R0C0 = vector.X; - result.R0C1 = 0; - result.R0C2 = 0; - result.R0C3 = 0; - result.R1C0 = 0; - result.R1C1 = vector.Y; - result.R1C2 = 0; - result.R1C3 = 0; - result.R2C0 = 0; - result.R2C1 = 0; - result.R2C2 = vector.Z; - result.R2C3 = 0; - result.R3C0 = 0; - result.R3C1 = 0; - result.R3C2 = 0; - result.R3C3 = 1; - } - - /// Gets left viewing matrix derived from an eye point, left reference point indicating the center of the scene, and an UP vector. - /// The eye position X coordinate. - /// The eye position Y coordinate. - /// The eye position Z coordinate. - /// The center position X coordinate. - /// The center position Y coordinate. - /// The center position Z coordinate. - /// The up direction X coordinate. - /// The up direction Y coordinate. - /// The up direction Z coordinate. - /// A viewing matrix derived from an eye point, left reference point indicating the center of the scene, and an UP vector. - public static void LookAt(double eyeX, double eyeY, double eyeZ, double centerX, double centerY, double centerZ, double upX, double upY, double upZ, out Matrix4d result) - { - Vector3d eye = new Vector3d(eyeX, eyeY, eyeZ); - Vector3d center = new Vector3d(centerX, centerY, centerZ); - Vector3d up = new Vector3d(upX, upY, upZ); - LookAt(ref eye, ref center, ref up, out result); - } - - /// Gets left viewing matrix derived from an eye point, left reference point indicating the center of the scene, and an UP vector. - /// The position of the eye. - /// The position of reference point. - /// The direction of the up vector - /// A viewing matrix derived from an eye point, left reference point indicating the center of the scene, and an UP vector. - public static void LookAt(ref Vector3d eye, ref Vector3d center, ref Vector3d up, out Matrix4d result) - { - Vector3d f; - center.Subtract(ref eye, out f); - f.Normalize(); - - Vector3d upNormalized; - up.Normalize(out upNormalized); - - Vector3d s; - Vector3d.CrossProduct(ref f, ref upNormalized, out s); - s.Normalize(); - - Vector3d u; - Vector3d.CrossProduct(ref s, ref f, out u); - - result.R0C0 = s.X; - result.R0C1 = u.X; - result.R0C2 = -f.X; - result.R0C3 = 0; - result.R1C0 = s.Y; - result.R1C1 = u.Y; - result.R1C2 = -f.Y; - result.R1C3 = 0; - result.R2C0 = s.Z; - result.R2C1 = u.Z; - result.R2C2 = -f.Z; - result.R2C3 = 0; - result.R3C0 = -eye.X * s.X - eye.Y * s.Y - eye.Z * s.Z; - result.R3C1 = -eye.X * u.X - eye.Y * u.Y - eye.Z * u.Z; - result.R3C2 = +eye.X * f.X + eye.Y * f.Y + eye.Z * f.Z; - result.R3C3 = 1; - } - - /// Gets left perspective matrix that produces left perspective projection. - /// The matrix vertical clipping plane. - /// The right vertical clipping plane. - /// The bottom horizontal clipping plane. - /// The top horizontal clipping plane. - /// The distances to the near depth clipping plane. Must be positive. - /// The distances to the far depth clipping plane. Must be positive. - /// A perspective matrix for the perspective projection. - public static void Frustum(double left, double right, double bottom, double top, double near, double far, out Matrix4d result) - { - double horizontalDelta = right - left; - double verticalDelta = top - bottom; - double negativeDepthDelta = -(far - near); - double near2 = near * 2; - - result.R0C0 = near2 / horizontalDelta; - result.R0C1 = 0; - result.R0C2 = 0; - result.R0C3 = 0; - result.R1C0 = 0; - result.R1C1 = near2 / verticalDelta; - result.R1C2 = 0; - result.R1C3 = 0; - result.R2C0 = (right + left) / horizontalDelta; - result.R2C1 = (top + bottom) / verticalDelta; - result.R2C2 = (far + near) / negativeDepthDelta; - result.R2C3 = -1; - result.R3C0 = 0; - result.R3C1 = 0; - result.R3C2 = near2 * far / negativeDepthDelta; - result.R3C3 = 0; - } - - /// Gets left viewing frustum into the world coordinate system. - /// The field of view angle, in degrees, in the y direction. - /// the aspect ratio that determines the field of view in the x direction. The aspect ratio is the ratio of x (width) to y (height). - /// the distance from the viewer to the near clipping plane (always positive). - /// the distance from the viewer to the far clipping plane (always positive). - /// A viewing frustum into the world coordinate system. - public static void Perspective(double fovy, double aspect, double near, double far, out Matrix4d result) - { - double cot = System.Math.Tan(fovy * Functions.DTOR / 2.0d); - double f = 1 / cot; - - result.R0C0 = f / aspect; - result.R0C1 = 0; - result.R0C2 = 0; - result.R0C3 = 0; - result.R1C0 = 0; - result.R1C1 = f; - result.R1C2 = 0; - result.R1C3 = 0; - result.R2C0 = 0; - result.R2C1 = 0; - result.R2C2 = (far + near) / (near - far); - result.R2C3 = -1; - result.R3C0 = 0; - result.R3C1 = 0; - result.R3C2 = (2 * far * near) / (near - far); - result.R3C3 = 0; - } - - public void Quaternion(out Quaterniond quaternion) - { - quaternion = new Quaterniond(ref this); + Matrix4d result; + result.Row0 = Vector4d .UnitX * x; + result.Row1 = Vector4d .UnitY * y; + result.Row2 = Vector4d .UnitZ * z; + result.Row3 = Vector4d .UnitW; + return result; } #endregion - #region Constants + #region Translation Functions - /// The identity matrix. - public static readonly Matrix4d Identity = new Matrix4d - ( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1 - ); - - /// A matrix of all zeros. - public static readonly Matrix4d Zero = new Matrix4d - ( - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 - ); - - #endregion - - #region HashCode - - /// Returns the hash code for this instance. - /// A 32-bit signed integer that is the hash code for this instance. - public override int GetHashCode() + /// + /// Build a translation matrix with the given translation + /// + /// The vector to translate along + /// A Translation matrix + public static Matrix4d Translation(Vector3d trans) { - return - R0C0.GetHashCode() ^ R0C1.GetHashCode() ^ R0C2.GetHashCode() ^ R0C3.GetHashCode() ^ - R1C0.GetHashCode() ^ R1C1.GetHashCode() ^ R1C2.GetHashCode() ^ R1C3.GetHashCode() ^ - R2C0.GetHashCode() ^ R2C1.GetHashCode() ^ R2C2.GetHashCode() ^ R2C3.GetHashCode() ^ - R3C0.GetHashCode() ^ R3C1.GetHashCode() ^ R3C2.GetHashCode() ^ R3C3.GetHashCode(); + return Translation(trans.X, trans.Y, trans.Z); + } + + /// + /// Build a translation matrix with the given translation + /// + /// X translation + /// Y translation + /// Z translation + /// A Translation matrix + public static Matrix4d Translation(double x, double y, double z) + { + Matrix4d result = Identity; + result.Row3 = new Vector4d (x, y, z, 1.0f); + return result; } #endregion - #region String + #region Rotation Functions - /// Returns the fully qualified type name of this instance. - /// A System.String containing left fully qualified type name. + /// + /// Build a rotation matrix that rotates about the x-axis + /// + /// angle in radians to rotate counter-clockwise around the x-axis + /// A rotation matrix + public static Matrix4d RotateX(double angle) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + Matrix4d result; + result.Row0 = Vector4d .UnitX; + result.Row1 = new Vector4d (0.0f, cos, sin, 0.0f); + result.Row2 = new Vector4d (0.0f, -sin, cos, 0.0f); + result.Row3 = Vector4d .UnitW; + return result; + } + + /// + /// Build a rotation matrix that rotates about the y-axis + /// + /// angle in radians to rotate counter-clockwise around the y-axis + /// A rotation matrix + public static Matrix4d RotateY(double angle) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + Matrix4d result; + result.Row0 = new Vector4d (cos, 0.0f, -sin, 0.0f); + result.Row1 = Vector4d .UnitY; + result.Row2 = new Vector4d (sin, 0.0f, cos, 0.0f); + result.Row3 = Vector4d .UnitW; + return result; + } + + /// + /// Build a rotation matrix that rotates about the z-axis + /// + /// angle in radians to rotate counter-clockwise around the z-axis + /// A rotation matrix + public static Matrix4d RotateZ(double angle) + { + double cos = (double)System.Math.Cos(angle); + double sin = (double)System.Math.Sin(angle); + + Matrix4d result; + result.Row0 = new Vector4d (cos, sin, 0.0f, 0.0f); + result.Row1 = new Vector4d (-sin, cos, 0.0f, 0.0f); + result.Row2 = Vector4d .UnitZ; + result.Row3 = Vector4d .UnitW; + return result; + } + + /// + /// Build a rotation matrix to rotate about the given axis + /// + /// the axis to rotate about + /// angle in radians to rotate counter-clockwise (looking in the direction of the given axis) + /// A rotation matrix + public static Matrix4d Rotate(Vector3d axis, double angle) + { + double cos = (double)System.Math.Cos(-angle); + double sin = (double)System.Math.Sin(-angle); + double t = 1.0f - cos; + + axis.Normalize(); + + Matrix4d result; + result.Row0 = new Vector4d (t * axis.X * axis.X + cos, t * axis.X * axis.Y - sin * axis.Z, t * axis.X * axis.Z + sin * axis.Y, 0.0f); + result.Row1 = new Vector4d (t * axis.X * axis.Y + sin * axis.Z, t * axis.Y * axis.Y + cos, t * axis.Y * axis.Z - sin * axis.X, 0.0f); + result.Row2 = new Vector4d (t * axis.X * axis.Z - sin * axis.Y, t * axis.Y * axis.Z + sin * axis.X, t * axis.Z * axis.Z + cos, 0.0f); + result.Row3 = Vector4d .UnitW; + return result; + } + + /// + /// Build a rotation matrix from a quaternion + /// + /// the quaternion + /// A rotation matrix + public static Matrix4d Rotate(Quaterniond q) + { + Vector3d axis; + double angle; + q.ToAxisAngle(out axis, out angle); + return Rotate(axis, angle); + } + + #endregion + + #region Camera Helper Functions + + /// + /// Build a world space to camera space matrix + /// + /// Eye (camera) position in world space + /// Target position in world space + /// Up vector in world space (should not be parallel to the camera direction, that is target - eye) + /// A Matrix that transforms world space to camera space + public static Matrix4d LookAt(Vector3d eye, Vector3d target, Vector3d up) + { + Vector3d z = Vector3d.Normalize(eye - target); + Vector3d x = Vector3d.Normalize(Vector3d.Cross(up, z)); + Vector3d y = Vector3d.Normalize(Vector3d.Cross(z, x)); + + Matrix4d rot = new Matrix4d(new Vector4d (x.X, y.X, z.X, 0.0f), + new Vector4d (x.Y, y.Y, z.Y, 0.0f), + new Vector4d (x.Z, y.Z, z.Z, 0.0f), + Vector4d .UnitW); + + Matrix4d trans = Matrix4d.Translation(-eye); + + return trans * rot; + } + + /// + /// Build a projection matrix + /// + /// Left edge of the view frustum + /// Right edge of the view frustum + /// Bottom edge of the view frustum + /// Top edge of the view frustum + /// Distance to the near clip plane + /// Distance to the far clip plane + /// A projection matrix that transforms camera space to raster space + public static Matrix4d Frustum(double left, double right, double bottom, double top, double near, double far) + { + double invRL = 1.0f / (right - left); + double invTB = 1.0f / (top - bottom); + double invFN = 1.0f / (far - near); + return new Matrix4d(new Vector4d (2.0f * near * invRL, 0.0f, 0.0f, 0.0f), + new Vector4d (0.0f, 2.0f * near * invTB, 0.0f, 0.0f), + new Vector4d ((right + left) * invRL, (top + bottom) * invTB, -(far + near) * invFN, -1.0f), + new Vector4d (0.0f, 0.0f, -2.0f * far * near * invFN, 0.0f)); + } + + /// + /// Build a projection matrix + /// + /// Angle of the field of view in the y direction (in radians) + /// Aspect ratio of the view (width / height) + /// Distance to the near clip plane + /// Distance to the far clip plane + /// A projection matrix that transforms camera space to raster space + public static Matrix4d Perspective(double fovy, double aspect, double near, double far) + { + double yMax = near * (double)System.Math.Tan(0.5f * fovy); + double yMin = -yMax; + double xMin = yMin * aspect; + double xMax = yMax * aspect; + + return Frustum(xMin, xMax, yMin, yMax, near, far); + } + + #endregion + + #region Multiply Functions + + /// + /// Post multiply this matrix by another matrix + /// + /// The matrix to multiply + /// A new Matrix44 that is the result of the multiplication + public static Matrix4d Mult(Matrix4d left, Matrix4d right) + { + Vector4d col0 = right.Column0; + Vector4d col1 = right.Column1; + Vector4d col2 = right.Column2; + Vector4d col3 = right.Column3; + + left.Row0 = new Vector4d (Vector4d .Dot(left.Row0, col0), Vector4d .Dot(left.Row0, col1), Vector4d .Dot(left.Row0, col2), Vector4d .Dot(left.Row0, col3)); + left.Row1 = new Vector4d (Vector4d .Dot(left.Row1, col0), Vector4d .Dot(left.Row1, col1), Vector4d .Dot(left.Row1, col2), Vector4d .Dot(left.Row1, col3)); + left.Row2 = new Vector4d (Vector4d .Dot(left.Row2, col0), Vector4d .Dot(left.Row2, col1), Vector4d .Dot(left.Row2, col2), Vector4d .Dot(left.Row2, col3)); + left.Row3 = new Vector4d (Vector4d .Dot(left.Row3, col0), Vector4d .Dot(left.Row3, col1), Vector4d .Dot(left.Row3, col2), Vector4d .Dot(left.Row3, col3)); + return left; + } + + public static void Mult(ref Matrix4d left, ref Matrix4d right, out Matrix4d result) + { + Vector4d col0 = right.Column0; + Vector4d col1 = right.Column1; + Vector4d col2 = right.Column2; + Vector4d col3 = right.Column3; + + result.Row0 = new Vector4d (Vector4d .Dot(left.Row0, col0), Vector4d .Dot(left.Row0, col1), Vector4d .Dot(left.Row0, col2), Vector4d .Dot(left.Row0, col3)); + result.Row1 = new Vector4d (Vector4d .Dot(left.Row1, col0), Vector4d .Dot(left.Row1, col1), Vector4d .Dot(left.Row1, col2), Vector4d .Dot(left.Row1, col3)); + result.Row2 = new Vector4d (Vector4d .Dot(left.Row2, col0), Vector4d .Dot(left.Row2, col1), Vector4d .Dot(left.Row2, col2), Vector4d .Dot(left.Row2, col3)); + result.Row3 = new Vector4d (Vector4d .Dot(left.Row3, col0), Vector4d .Dot(left.Row3, col1), Vector4d .Dot(left.Row3, col2), Vector4d .Dot(left.Row3, col3)); + } + + #endregion + + #region Invert Functions + + /// + /// Calculate the inverse of the given matrix + /// + /// The matrix to invert + /// The inverse of the given matrix if it has one, or the input if it is singular + /// Thrown if the Matrix4d is singular. + public static Matrix4d Invert(Matrix4d mat) + { + int[] colIdx = { 0, 0, 0, 0 }; + int[] rowIdx = { 0, 0, 0, 0 }; + int[] pivotIdx = { -1, -1, -1, -1 }; + + // convert the matrix to an array for easy looping + double[,] inverse = {{mat.Row0.X, mat.Row0.Y, mat.Row0.Z, mat.Row0.W}, + {mat.Row1.X, mat.Row1.Y, mat.Row1.Z, mat.Row1.W}, + {mat.Row2.X, mat.Row2.Y, mat.Row2.Z, mat.Row2.W}, + {mat.Row3.X, mat.Row3.Y, mat.Row3.Z, mat.Row3.W} }; + int icol = 0; + int irow = 0; + for (int i = 0; i < 4; i++) + { + // Find the largest pivot value + double maxPivot = 0.0f; + for (int j = 0; j < 4; j++) + { + if (pivotIdx[j] != 0) + { + for (int k = 0; k < 4; ++k) + { + if (pivotIdx[k] == -1) + { + double absVal = System.Math.Abs(inverse[j, k]); + if (absVal > maxPivot) + { + maxPivot = absVal; + irow = j; + icol = k; + } + } + else if (pivotIdx[k] > 0) + { + return mat; + } + } + } + } + + ++(pivotIdx[icol]); + + // Swap rows over so pivot is on diagonal + if (irow != icol) + { + for (int k = 0; k < 4; ++k) + { + double f = inverse[irow, k]; + inverse[irow, k] = inverse[icol, k]; + inverse[icol, k] = f; + } + } + + rowIdx[i] = irow; + colIdx[i] = icol; + + double pivot = inverse[icol, icol]; + // check for singular matrix + if (pivot == 0.0f) + { + throw new InvalidOperationException("Matrix is singular and cannot be inverted."); + //return mat; + } + + // Scale row so it has a unit diagonal + double oneOverPivot = 1.0f / pivot; + inverse[icol, icol] = 1.0f; + for (int k = 0; k < 4; ++k) + inverse[icol, k] *= oneOverPivot; + + // Do elimination of non-diagonal elements + for (int j = 0; j < 4; ++j) + { + // check this isn't on the diagonal + if (icol != j) + { + double f = inverse[j, icol]; + inverse[j, icol] = 0.0f; + for (int k = 0; k < 4; ++k) + inverse[j, k] -= inverse[icol, k] * f; + } + } + } + + for (int j = 3; j >= 0; --j) + { + int ir = rowIdx[j]; + int ic = colIdx[j]; + for (int k = 0; k < 4; ++k) + { + double f = inverse[k, ir]; + inverse[k, ir] = inverse[k, ic]; + inverse[k, ic] = f; + } + } + + mat.Row0 = new Vector4d (inverse[0, 0], inverse[0, 1], inverse[0, 2], inverse[0, 3]); + mat.Row1 = new Vector4d (inverse[1, 0], inverse[1, 1], inverse[1, 2], inverse[1, 3]); + mat.Row2 = new Vector4d (inverse[2, 0], inverse[2, 1], inverse[2, 2], inverse[2, 3]); + mat.Row3 = new Vector4d (inverse[3, 0], inverse[3, 1], inverse[3, 2], inverse[3, 3]); + return mat; + } + + #endregion + + #region Transpose + + /// + /// Calculate the transpose of the given matrix + /// + /// The matrix to transpose + /// The transpose of the given matrix + public static Matrix4d Transpose(Matrix4d mat) + { + return new Matrix4d(mat.Column0, mat.Column1, mat.Column2, mat.Column3); + } + + + /// + /// Calculate the transpose of the given matrix + /// + /// The matrix to transpose + public static void Transpose(ref Matrix4d mat, out Matrix4d result) + { + result.Row0 = mat.Column0; + result.Row1 = mat.Column1; + result.Row2 = mat.Column2; + result.Row3 = mat.Column3; + } + + #endregion + + #endregion + + #region Operators + + /// + /// Matrix multiplication + /// + /// left-hand operand + /// right-hand operand + /// A new Matrix44 which holds the result of the multiplication + public static Matrix4d operator *(Matrix4d left, Matrix4d right) + { + return Matrix4d.Mult(left, right); + } + + public static bool operator ==(Matrix4d left, Matrix4d right) + { + return left.Equals(right); + } + + public static bool operator !=(Matrix4d left, Matrix4d right) + { + return !left.Equals(right); + } + + #endregion + + #region Overrides + + #region public override string ToString() + + /// + /// Returns a System.String that represents the current Matrix44. + /// + /// public override string ToString() { - return String.Format( - "|{00}, {01}, {02}, {03}|\n" + - "|{04}, {05}, {06}, {07}|\n" + - "|{08}, {09}, {10}, {11}|\n" + - "|{12}, {13}, {14}, {15}|", - R0C0, R0C1, R0C2, R0C3, - R1C0, R1C1, R1C2, R1C3, - R2C0, R2C1, R2C2, R2C3, - R3C0, R3C1, R3C2, R3C3); + return String.Format("{0}\n{1}\n{2}\n{3}", Row0, Row1, Row2, Row3); + } + + #endregion + + #region public override int GetHashCode() + + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. + public override int GetHashCode() + { + return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode(); + } + + #endregion + + #region public override bool Equals(object obj) + + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare to. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Matrix4d)) + return false; + + return this.Equals((Matrix4d)obj); + } + + #endregion + + #endregion + + #endregion + + #region IEquatable Members + + /// Indicates whether the current matrix is equal to another matrix. + /// An matrix to compare with this matrix. + /// true if the current matrix is equal to the matrix parameter; otherwise, false. + public bool Equals(Matrix4d other) + { + return + Row0 == other.Row0 && + Row1 == other.Row1 && + Row2 == other.Row2 && + Row3 == other.Row3; } #endregion } - - #pragma warning restore 3019 } \ No newline at end of file diff --git a/Source/OpenTK/Math/Quaternion.cs b/Source/OpenTK/Math/Quaternion.cs index 4cb76afb..15a9da10 100644 --- a/Source/OpenTK/Math/Quaternion.cs +++ b/Source/OpenTK/Math/Quaternion.cs @@ -1,14 +1,28 @@ #region --- License --- -/* Copyright (c) 2006, 2007 the OpenTK team - * See license.txt for license info - * - * Implemented by Andy Gill +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ #endregion using System; -using System.Collections.Generic; -using System.Text; using System.Runtime.InteropServices; namespace OpenTK.Math @@ -26,13 +40,12 @@ namespace OpenTK.Math /// The vector part of the quaternion /// public Vector3 XYZ; + /// /// The w component of the quaternion /// public float W; - public static Quaternion Identity = new Quaternion(0, 0, 0, 1); - #endregion #region Constructors @@ -63,11 +76,44 @@ namespace OpenTK.Math #endregion - #region Functions + #region Public Members - #region pubilc void ToAxisAngle(out Vector3 axis, out float angle) + #region Properties - /// + /// + /// Gets or sets the X component of this instance. + /// + public float X + { + get { return XYZ.X; } + set { XYZ.X = value; } + } + + /// + /// Gets or sets the Y component of this instance. + /// + public float Y + { + get { return XYZ.Y; } + set { XYZ.Y = value; } + } + + /// + /// Gets or sets the Z component of this instance. + /// + public float Z + { + get { return XYZ.Z; } + set { XYZ.Z = value; } + } + + #endregion + + #region Instance + + #region pubilc void ToAxisAngle(out Vector3 axis, out float angle) + + /// /// Convert the current quaternion to axis angle representation /// /// The resultant axis @@ -148,7 +194,272 @@ namespace OpenTK.Math #endregion - #region Operator overloads + #region Static + + #region Fields + + /// + /// Defines the identity quaternion. + /// + public static Quaternion Identity = new Quaternion(0, 0, 0, 1); + + #endregion + + #region Add + + /// + /// Add two quaternions + /// + /// The first operand + /// The second operand + /// The result of the addition + public static Quaternion Add(Quaternion left, Quaternion right) + { + left.XYZ += right.XYZ; + left.W += right.W; + return left; + } + + /// + /// Add two quaternions + /// + /// The first operand + /// The second operand + /// The result of the addition + public static void Add(ref Quaternion left, ref Quaternion right, out Quaternion result) + { + result.XYZ = left.XYZ + right.XYZ; + result.W = left.W + right.W; + } + + #endregion + + #region Sub + + public static Quaternion Sub(Quaternion left, Quaternion right) + { + left.XYZ -= right.XYZ; + left.W -= right.W; + return left; + } + + public static void Sub(ref Quaternion left, ref Quaternion right, out Quaternion result) + { + result.XYZ = left.XYZ - right.XYZ; + result.W = left.W - right.W; + } + + #endregion + + #region Mult + + public static Quaternion Mult(Quaternion left, Quaternion right) + { + float w = left.W * right.W - Vector3.Dot(left.XYZ, right.XYZ); + left.XYZ = right.W * left.XYZ + left.W * right.XYZ + Vector3.Cross(left.XYZ, right.XYZ); + left.W = w; + return left; + } + + public static void Mult(ref Quaternion left, ref Quaternion right, out Quaternion result) + { + result.W = left.W * right.W - Vector3.Dot(left.XYZ, right.XYZ); + result.XYZ = right.W * left.XYZ + left.W * right.XYZ + Vector3.Cross(left.XYZ, right.XYZ); + } + + #endregion + + #region Conjugate + + /// + /// Get the conjugate of the given quaternion + /// + /// The quaternion + /// The conjugate of the given quaternion + public static Quaternion Conjugate(Quaternion q) + { + q.XYZ = -q.XYZ; + return q; + } + + /// + /// Get the conjugate of the given quaternion + /// + /// The quaternion + /// The conjugate of the given quaternion + public static void Conjugate(ref Quaternion q, out Quaternion result) + { + result.XYZ = -q.XYZ; + result.W = q.W; + } + + #endregion + + #region Invert + + /// + /// Get the inverse of the given quaternion + /// + /// The quaternion to invert + /// The inverse of the given quaternion + public static Quaternion Invert(Quaternion q) + { + float lengthSq = q.LengthSquared; + if (lengthSq != 0.0) + { + float i = 1.0f / lengthSq; + q.XYZ *= -i; + q.W *= i; + } + return q; + } + + /// + /// Get the inverse of the given quaternion + /// + /// The quaternion to invert + /// The inverse of the given quaternion + public static void Invert(ref Quaternion q, out Quaternion result) + { + float lengthSq = q.LengthSquared; + if (lengthSq != 0.0) + { + float i = 1.0f / lengthSq; + result.XYZ = q.XYZ * -i; + result.W = q.W * i; + } + else + { + result = q; + } + } + + #endregion + + #region Normalize + + /// + /// Scale the given quaternion to unit length + /// + /// The quaternion to normalize + /// The normalized quaternion + public static Quaternion Normalize(Quaternion q) + { + float scale = 1.0f / q.Length; + q.XYZ *= scale; + q.W *= scale; + return q; + } + + /// + /// Scale the given quaternion to unit length + /// + /// The quaternion to normalize + /// The normalized quaternion + public static void Normalize(ref Quaternion q, out Quaternion result) + { + float scale = 1.0f / q.Length; + result.XYZ = q.XYZ * scale; + result.W = q.W * scale; + } + + #endregion + + #region FromAxisAngle + + /// + /// Build a quaternion from the given axis and angle + /// + /// The axis to rotate about + /// The rotation angle in radians + /// + public static Quaternion FromAxisAngle(Vector3 axis, float angle) + { + if (axis.LengthSquared == 0.0f) + return Identity; + + Quaternion result = Identity; + + angle *= 0.5f; + axis.Normalize(); + result.XYZ = axis * (float)System.Math.Sin(angle); + result.W = (float)System.Math.Cos(angle); + + return Normalize(result); + } + + #endregion + + #region Slerp + + /// + /// Do Spherical linear interpolation between two quaternions + /// + /// The first quaternion + /// The second quaternion + /// The blend factor + /// A smooth blend between the given quaternions + public static Quaternion Slerp(Quaternion q1, Quaternion q2, float blend) + { + // if either input is zero, return the other. + if (q1.LengthSquared == 0.0f) + { + if (q2.LengthSquared == 0.0f) + { + return Identity; + } + return q2; + } + else if (q2.LengthSquared == 0.0f) + { + return q1; + } + + + float cosHalfAngle = q1.W * q2.W + Vector3.Dot(q1.XYZ, q2.XYZ); + + if (cosHalfAngle >= 1.0f || cosHalfAngle <= -1.0f) + { + // angle = 0.0f, so just return one input. + return q1; + } + else if (cosHalfAngle < 0.0f) + { + q2.XYZ = -q2.XYZ; + q2.W = -q2.W; + cosHalfAngle = -cosHalfAngle; + } + + float blendA; + float blendB; + if (cosHalfAngle < 0.99f) + { + // do proper slerp for big angles + float halfAngle = (float)System.Math.Acos(cosHalfAngle); + float sinHalfAngle = (float)System.Math.Sin(halfAngle); + float oneOverSinHalfAngle = 1.0f / sinHalfAngle; + blendA = (float)System.Math.Sin(halfAngle * (1.0f - blend)) * oneOverSinHalfAngle; + blendB = (float)System.Math.Sin(halfAngle * blend) * oneOverSinHalfAngle; + } + else + { + // do lerp if angle is really small. + blendA = 1.0f - blend; + blendB = blend; + } + + Quaternion result = new Quaternion(blendA * q1.XYZ + blendB * q2.XYZ, blendA * q1.W + blendB * q2.W); + if (result.LengthSquared > 0.0f) + return Normalize(result); + else + return Identity; + } + + #endregion + + #endregion + + #region Operators public static Quaternion operator +(Quaternion left, Quaternion right) { @@ -188,265 +499,11 @@ namespace OpenTK.Math #endregion - #region Static functions + #region Overrides - #region Add + #region public override string ToString() - /// - /// Add two quaternions - /// - /// The first operand - /// The second operand - /// The result of the addition - public static Quaternion Add(Quaternion left, Quaternion right) - { - left.XYZ += right.XYZ; - left.W += right.W; - return left; - } - - /// - /// Add two quaternions - /// - /// The first operand - /// The second operand - /// The result of the addition - public static void Add(ref Quaternion left, ref Quaternion right, out Quaternion result) - { - result.XYZ = left.XYZ + right.XYZ; - result.W = left.W + right.W; - } - - #endregion - - #region Sub - - public static Quaternion Sub(Quaternion left, Quaternion right) - { - left.XYZ -= right.XYZ; - left.W -= right.W; - return left; - } - - public static void Sub(ref Quaternion left, ref Quaternion right, out Quaternion result) - { - result.XYZ = left.XYZ - right.XYZ; - result.W = left.W - right.W; - } - - #endregion - - #region Mult - - public static Quaternion Mult(Quaternion left, Quaternion right) - { - float w = left.W * right.W - Vector3.Dot(left.XYZ, right.XYZ); - left.XYZ = right.W * left.XYZ + left.W * right.XYZ + Vector3.Cross(left.XYZ, right.XYZ); - left.W = w; - return left; - } - - public static void Mult(ref Quaternion left, ref Quaternion right, out Quaternion result) - { - result.W = left.W * right.W - Vector3.Dot(left.XYZ, right.XYZ); - result.XYZ = right.W * left.XYZ + left.W * right.XYZ + Vector3.Cross(left.XYZ, right.XYZ); - } - - #endregion - - #region Conjugate - - /// - /// Get the conjugate of the given quaternion - /// - /// The quaternion - /// The conjugate of the given quaternion - public static Quaternion Conjugate(Quaternion q) - { - q.XYZ = -q.XYZ; - return q; - } - - /// - /// Get the conjugate of the given quaternion - /// - /// The quaternion - /// The conjugate of the given quaternion - public static void Conjugate(ref Quaternion q, out Quaternion result) - { - result.XYZ = -q.XYZ; - result.W = q.W; - } - - #endregion - - #region Invert - - /// - /// Get the inverse of the given quaternion - /// - /// The quaternion to invert - /// The inverse of the given quaternion - public static Quaternion Invert(Quaternion q) - { - float lengthSq = q.LengthSquared; - if (lengthSq != 0.0) - { - float i = 1.0f / lengthSq; - q.XYZ *= -i; - q.W *= i; - } - return q; - } - - /// - /// Get the inverse of the given quaternion - /// - /// The quaternion to invert - /// The inverse of the given quaternion - public static void Invert(ref Quaternion q, out Quaternion result) - { - float lengthSq = q.LengthSquared; - if (lengthSq != 0.0) - { - float i = 1.0f / lengthSq; - result.XYZ = q.XYZ * -i; - result.W = q.W * i; - } - else - { - result = q; - } - } - - #endregion - - #region Normalize - - /// - /// Scale the given quaternion to unit length - /// - /// The quaternion to normalize - /// The normalized quaternion - public static Quaternion Normalize(Quaternion q) - { - float scale = 1.0f / q.Length; - q.XYZ *= scale; - q.W *= scale; - return q; - } - - /// - /// Scale the given quaternion to unit length - /// - /// The quaternion to normalize - /// The normalized quaternion - public static void Normalize(ref Quaternion q, out Quaternion result) - { - float scale = 1.0f / q.Length; - result.XYZ = q.XYZ * scale; - result.W = q.W * scale; - } - - #endregion - - #region FromAxisAngle - - /// - /// Build a quaternion from the given axis and angle - /// - /// The axis to rotate about - /// The rotation angle in radians - /// - public static Quaternion FromAxisAngle(Vector3 axis, float angle) - { - if (axis.LengthSquared == 0.0f) - return Identity; - - Quaternion result = Identity; - - angle *= 0.5f; - axis.Normalize(); - result.XYZ = axis * (float)System.Math.Sin(angle); - result.W = (float)System.Math.Cos(angle); - - return Normalize(result); - } - - #endregion - - #region Slerp - - /// - /// Do Spherical linear interpolation between two quaternions - /// - /// The first quaternion - /// The second quaternion - /// The blend factor - /// A smooth blend between the given quaternions - public static Quaternion Slerp(Quaternion q1, Quaternion q2, float blend) - { - // if either input is zero, return the other. - if (q1.LengthSquared == 0.0f) - { - if (q2.LengthSquared == 0.0f) - { - return Identity; - } - return q2; - } - else if (q2.LengthSquared == 0.0f) - { - return q1; - } - - - float cosHalfAngle = q1.W * q2.W + Vector3.Dot(q1.XYZ, q2.XYZ); - - if (cosHalfAngle >= 1.0f || cosHalfAngle <= -1.0f) - { - // angle = 0.0f, so just return one input. - return q1; - } - else if (cosHalfAngle < 0.0f) - { - q2.XYZ = -q2.XYZ; - q2.W = -q2.W; - cosHalfAngle = -cosHalfAngle; - } - - float blendA; - float blendB; - if (cosHalfAngle < 0.99f) - { - // do proper slerp for big angles - float halfAngle = (float)System.Math.Acos(cosHalfAngle); - float sinHalfAngle = (float)System.Math.Sin(halfAngle); - float oneOverSinHalfAngle = 1.0f / sinHalfAngle; - blendA = (float)System.Math.Sin(halfAngle * (1.0f - blend)) * oneOverSinHalfAngle; - blendB = (float)System.Math.Sin(halfAngle * blend) * oneOverSinHalfAngle; - } - else - { - // do lerp if angle is really small. - blendA = 1.0f - blend; - blendB = blend; - } - - Quaternion result = new Quaternion(blendA * q1.XYZ + blendB * q2.XYZ, blendA * q1.W + blendB * q2.W); - if (result.LengthSquared > 0.0f) - return Normalize(result); - else - return Identity; - } - - #endregion - - #endregion - - #region public override string ToString() - - /// + /// /// Returns a System.String that represents the current Quaternion. /// /// @@ -456,5 +513,9 @@ namespace OpenTK.Math } #endregion + + #endregion + + #endregion } } diff --git a/Source/OpenTK/Math/Quaterniond.cs b/Source/OpenTK/Math/Quaterniond.cs index e014d0e5488b4ec6f88bf8d3ab1d5e61a819bc52..596ff2b1c6959a295be0ad6bed550fb72041a634 100644 GIT binary patch literal 77476 zcmeHQ`*Rh^b-urns{9YvIF%BPrG;H572CTc0+uypUP0`#4?9(WK#KziB!PI?|9X=1 z^|_x;pP7Em+_{o)sVLERX1Y(GKJPx=)AN7-`^(wh?8$6r_H4F0dpVoW*2BN~?55m# zEKgp}_T_)x|Hs)s%s!j_YKHQvw0JdpFYTX6`=4hAQfp1xT#)kf*+u#HZ=oHp{#~x_ zOFcllBP{_NcXs99^V$Ch6hL?`z4xU$!vS}Yq|4O?mUrybLr`+{Qp>50RE2L!}9~VKM!rt9+>S3T&2gZKs}IW z&jcd=c0x_S(9tVQV0a)<6(6*HD%Xdh7xW8!wnGW*cct|+xu+P;*K@g#ajwaIrQiA4R;Q2iLU$X>wzPOC?dH<@WuPHjcH7MbOHlr2f%itrd7#z4 z`~qJeOR1OuHefv#sBZ*g(xTfM`~#Q#DA>En3dRGvgK}?Vbjk@*6}`QXr|9RYa8<9t z7xL_tw0abz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)w^+w z4pwKRZ^kBhQZzmuSi~f~9N%*BslZ3qkVKkFQ$rGI)P$HQzNlzA`OLw` zM1kEW^cnI=^rf8rA2=whbV4S-SS;d08Z(x_PGBga4=uLfdRkJ*Ugu37iJnm`la+{< zkQ>+nis+sX_fXeZVCnIIu?9Q&gH}lHcfgVpqAUMng_b^Gb>x2Rh2kz)_T6Ogf3lk>^KzKm{}jk?>s!&?zfx!` zg|@j**ADdO!O);f>kaRqy%B?G1o=#_0dxCI3?_0$z`&i;`OrL=Nfx*j^WfmrJUR(9 z>$x8$5D?@qr~zzq@PC~F1(zf^c`=)eV@)C?aUi6aO(7&*XSQLAXiVB5DP|)=@XA?( z+`Ow(Ao97(%=6AA4;~eQ7_WrrY&74}%lX>S;5Geh$X~Ec@YnbDBs>rBs1{v_0Q#HQ z-Nr;P;GkN;_^_R7?;7--X6YyeN;;Y% zr9g+7ye<>uB9GeV;Ig`(3i^ z*H2{ch@|es@3Zj6k4|_%;)^Cs@#3we2`^0Tc;9JbPH~5+Q@30BbXCsaQVQK^>cSH< z_&F=xK*G|ad{VNIN*r8CHn^0igO73XjZ%kaaa*`Ci#JQgjU_ESBZpUEgLYWdUN^V? E1t8IBq5uE@ diff --git a/Source/OpenTK/Math/Vector2.cs b/Source/OpenTK/Math/Vector2.cs index 0af1ce9f..2cc8776a 100644 --- a/Source/OpenTK/Math/Vector2.cs +++ b/Source/OpenTK/Math/Vector2.cs @@ -1,14 +1,28 @@ #region --- License --- -/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos - * See license.txt for license info - * - * Contributions by Andy Gill, Georg Wächter. +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ #endregion using System; -using System.Collections.Generic; -using System.Text; using System.Runtime.InteropServices; namespace OpenTK.Math @@ -35,26 +49,6 @@ namespace OpenTK.Math /// public float Y; - /// - /// Defines a unit-length Vector2 that points towards the X-axis. - /// - public static Vector2 UnitX = new Vector2(1, 0); - - /// - /// Defines a unit-length Vector2 that points towards the Y-axis. - /// - public static Vector2 UnitY = new Vector2(0, 1); - - /// - /// Defines a zero-length Vector2. - /// - public static Vector2 Zero = new Vector2(0, 0); - - /// - /// Defines the size of the Vector2 struct in bytes. - /// - public static readonly int SizeInBytes = Marshal.SizeOf(new Vector2()); - #endregion #region Constructors @@ -74,35 +68,40 @@ namespace OpenTK.Math /// Constructs a new Vector2 from the given Vector2. /// /// The Vector2 to copy components from. + [Obsolete] public Vector2(Vector2 v) { - X = v.X; - Y = v.Y; + X = v.X; + Y = v.Y; } /// /// Constructs a new Vector2 from the given Vector3. /// /// The Vector3 to copy components from. Z is discarded. + [Obsolete] public Vector2(Vector3 v) { - X = v.X; - Y = v.Y; + X = v.X; + Y = v.Y; } /// /// Constructs a new Vector2 from the given Vector4. /// /// The Vector4 to copy components from. Z and W are discarded. + [Obsolete] public Vector2(Vector4 v) { - X = v.X; - Y = v.Y; + X = v.X; + Y = v.Y; } #endregion - #region Functions + #region Public Members + + #region Instance #region public float Length @@ -239,7 +238,381 @@ namespace OpenTK.Math #endregion - #region Operator overloads + #region Static + + #region Fields + + /// + /// Defines a unit-length Vector2 that points towards the X-axis. + /// + public static readonly Vector2 UnitX = new Vector2(1, 0); + + /// + /// Defines a unit-length Vector2 that points towards the Y-axis. + /// + public static readonly Vector2 UnitY = new Vector2(0, 1); + + /// + /// Defines a zero-length Vector2. + /// + public static readonly Vector2 Zero = new Vector2(0, 0); + + /// + /// Defines the size of the Vector2 struct in bytes. + /// + public static readonly int SizeInBytes = Marshal.SizeOf(new Vector2()); + + #endregion + + #region Add + + /// + /// Add two Vectors + /// + /// First operand + /// Second operand + /// Result of addition + public static Vector2 Add(Vector2 a, Vector2 b) + { + a.X += b.X; + a.Y += b.Y; + return a; + } + + /// + /// Add two Vectors + /// + /// First operand + /// Second operand + /// Result of addition + public static void Add(ref Vector2 a, ref Vector2 b, out Vector2 result) + { + result.X = a.X + b.X; + result.Y = a.Y + b.Y; + } + + #endregion + + #region Sub + + /// + /// Subtract one Vector from another + /// + /// First operand + /// Second operand + /// Result of subtraction + public static Vector2 Sub(Vector2 a, Vector2 b) + { + a.X -= b.X; + a.Y -= b.Y; + return a; + } + + /// + /// Subtract one Vector from another + /// + /// First operand + /// Second operand + /// Result of subtraction + public static void Sub(ref Vector2 a, ref Vector2 b, out Vector2 result) + { + result.X = a.X - b.X; + result.Y = a.Y - b.Y; + } + + #endregion + + #region Mult + + /// + /// Multiply a vector and a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the multiplication + public static Vector2 Mult(Vector2 a, float f) + { + a.X *= f; + a.Y *= f; + return a; + } + + /// + /// Multiply a vector and a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the multiplication + public static void Mult(ref Vector2 a, float f, out Vector2 result) + { + result.X = a.X * f; + result.Y = a.Y * f; + } + + #endregion + + #region Div + + /// + /// Divide a vector by a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the division + public static Vector2 Div(Vector2 a, float f) + { + float mult = 1.0f / f; + a.X *= mult; + a.Y *= mult; + return a; + } + + /// + /// Divide a vector by a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the division + public static void Div(ref Vector2 a, float f, out Vector2 result) + { + float mult = 1.0f / f; + result.X = a.X * mult; + result.Y = a.Y * mult; + } + + #endregion + + #region ComponentMin + + /// + /// Calculate the component-wise minimum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise minimum + public static Vector2 ComponentMin(Vector2 a, Vector2 b) + { + a.X = a.X < b.X ? a.X : b.X; + a.Y = a.Y < b.Y ? a.Y : b.Y; + return a; + } + + /// + /// Calculate the component-wise minimum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise minimum + public static void ComponentMin(ref Vector2 a, ref Vector2 b, out Vector2 result) + { + result.X = a.X < b.X ? a.X : b.X; + result.Y = a.Y < b.Y ? a.Y : b.Y; + } + + #endregion + + #region ComponentMax + + /// + /// Calculate the component-wise maximum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise maximum + public static Vector2 ComponentMax(Vector2 a, Vector2 b) + { + a.X = a.X > b.X ? a.X : b.X; + a.Y = a.Y > b.Y ? a.Y : b.Y; + return a; + } + + /// + /// Calculate the component-wise maximum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise maximum + public static void ComponentMax(ref Vector2 a, ref Vector2 b, out Vector2 result) + { + result.X = a.X > b.X ? a.X : b.X; + result.Y = a.Y > b.Y ? a.Y : b.Y; + } + + #endregion + + #region Min + + /// + /// Returns the Vector3 with the minimum magnitude + /// + /// Left operand + /// Right operand + /// The minimum Vector3 + public static Vector2 Min(Vector2 left, Vector2 right) + { + return left.LengthSquared < right.LengthSquared ? left : right; + } + + #endregion + + #region Max + + /// + /// Returns the Vector3 with the minimum magnitude + /// + /// Left operand + /// Right operand + /// The minimum Vector3 + public static Vector2 Max(Vector2 left, Vector2 right) + { + return left.LengthSquared >= right.LengthSquared ? left : right; + } + + #endregion + + #region Clamp + + /// + /// Clamp a vector to the given minimum and maximum vectors + /// + /// Input vector + /// Minimum vector + /// Maximum vector + /// The clamped vector + public static Vector2 Clamp(Vector2 vec, Vector2 min, Vector2 max) + { + vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; + vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; + return vec; + } + + /// + /// Clamp a vector to the given minimum and maximum vectors + /// + /// Input vector + /// Minimum vector + /// Maximum vector + /// The clamped vector + public static void Clamp(ref Vector2 vec, ref Vector2 min, ref Vector2 max, out Vector2 result) + { + result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; + result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; + } + + #endregion + + #region Normalize + + /// + /// Scale a vector to unit length + /// + /// The input vector + /// The normalized vector + public static Vector2 Normalize(Vector2 vec) + { + float scale = 1.0f / vec.Length; + vec.X *= scale; + vec.Y *= scale; + return vec; + } + + /// + /// Scale a vector to unit length + /// + /// The input vector + /// The normalized vector + public static void Normalize(ref Vector2 vec, out Vector2 result) + { + float scale = 1.0f / vec.Length; + result.X = vec.X * scale; + result.Y = vec.Y * scale; + } + + #endregion + + #region NormalizeFast + + /// + /// Scale a vector to approximately unit length + /// + /// The input vector + /// The normalized vector + public static Vector2 NormalizeFast(Vector2 vec) + { + float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y); + vec.X *= scale; + vec.Y *= scale; + return vec; + } + + /// + /// Scale a vector to approximately unit length + /// + /// The input vector + /// The normalized vector + public static void NormalizeFast(ref Vector2 vec, out Vector2 result) + { + float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y); + result.X = vec.X * scale; + result.Y = vec.Y * scale; + } + + #endregion + + #region Dot + + /// + /// Caclulate the dot (scalar) product of two vectors + /// + /// First operand + /// Second operand + /// The dot product of the two inputs + public static float Dot(Vector2 left, Vector2 right) + { + return left.X * right.X + left.Y * right.Y; + } + + #endregion + + #region Lerp + + /// + /// Returns a new Vector that is the linear blend of the 2 given Vectors + /// + /// First input vector + /// Second input vector + /// The blend factor + /// a when blend=0, b when blend=1, and a linear combination otherwise + public static Vector2 Lerp(Vector2 a, Vector2 b, float blend) + { + a.X = blend * (b.X - a.X) + a.X; + a.Y = blend * (b.Y - a.Y) + a.Y; + return a; + } + + #endregion + + #region Barycentric + + /// + /// Interpolate 3 Vectors using Barycentric coordinates + /// + /// First input Vector + /// Second input Vector + /// Third input Vector + /// First Barycentric Coordinate + /// Second Barycentric Coordinate + /// a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise + public static Vector2 BaryCentric(Vector2 a, Vector2 b, Vector2 c, float u, float v) + { + return a + u * (b - a) + v * (c - a); + } + + #endregion + + #endregion + + #region Operators public static Vector2 operator +(Vector2 left, Vector2 right) { @@ -294,345 +667,13 @@ namespace OpenTK.Math return !left.Equals(right); } - [CLSCompliant(false)] - unsafe public static explicit operator float*(Vector2 v) - { - return &v.X; - } - - public static explicit operator IntPtr(Vector2 v) - { - unsafe - { - return (IntPtr)(&v.X); - } - } - #endregion - #region Static functions + #region Overrides - #region Add + #region public override string ToString() - /// - /// Add two Vectors - /// - /// First operand - /// Second operand - /// Result of addition - public static Vector2 Add(Vector2 a, Vector2 b) - { - a.X += b.X; - a.Y += b.Y; - return a; - } - - /// - /// Add two Vectors - /// - /// First operand - /// Second operand - /// Result of addition - public static void Add(ref Vector2 a, ref Vector2 b, out Vector2 result) - { - result.X = a.X + b.X; - result.Y = a.Y + b.Y; - } - - #endregion - - #region Sub - - /// - /// Subtract one Vector from another - /// - /// First operand - /// Second operand - /// Result of subtraction - public static Vector2 Sub(Vector2 a, Vector2 b) - { - a.X -= b.X; - a.Y -= b.Y; - return a; - } - - /// - /// Subtract one Vector from another - /// - /// First operand - /// Second operand - /// Result of subtraction - public static void Sub(ref Vector2 a, ref Vector2 b, out Vector2 result) - { - result.X = a.X - b.X; - result.Y = a.Y - b.Y; - } - - #endregion - - #region Mult - - /// - /// Multiply a vector and a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the multiplication - public static Vector2 Mult(Vector2 a, float f) - { - a.X *= f; - a.Y *= f; - return a; - } - - /// - /// Multiply a vector and a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the multiplication - public static void Mult(ref Vector2 a, float f, out Vector2 result) - { - result.X = a.X * f; - result.Y = a.Y * f; - } - - #endregion - - #region Div - - /// - /// Divide a vector by a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the division - public static Vector2 Div(Vector2 a, float f) - { - float mult = 1.0f / f; - a.X *= mult; - a.Y *= mult; - return a; - } - - /// - /// Divide a vector by a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the division - public static void Div(ref Vector2 a, float f, out Vector2 result) - { - float mult = 1.0f / f; - result.X = a.X * mult; - result.Y = a.Y * mult; - } - - #endregion - - #region Min - - /// - /// Calculate the component-wise minimum of two vectors - /// - /// First operand - /// Second operand - /// The component-wise minimum - public static Vector2 Min(Vector2 a, Vector2 b) - { - a.X = a.X < b.X ? a.X : b.X; - a.Y = a.Y < b.Y ? a.Y : b.Y; - return a; - } - - /// - /// Calculate the component-wise minimum of two vectors - /// - /// First operand - /// Second operand - /// The component-wise minimum - public static void Min(ref Vector2 a, ref Vector2 b, out Vector2 result) - { - result.X = a.X < b.X ? a.X : b.X; - result.Y = a.Y < b.Y ? a.Y : b.Y; - } - - #endregion - - #region Max - - /// - /// Calculate the component-wise maximum of two vectors - /// - /// First operand - /// Second operand - /// The component-wise maximum - public static Vector2 Max(Vector2 a, Vector2 b) - { - a.X = a.X > b.X ? a.X : b.X; - a.Y = a.Y > b.Y ? a.Y : b.Y; - return a; - } - - /// - /// Calculate the component-wise maximum of two vectors - /// - /// First operand - /// Second operand - /// The component-wise maximum - public static void Max(ref Vector2 a, ref Vector2 b, out Vector2 result) - { - result.X = a.X > b.X ? a.X : b.X; - result.Y = a.Y > b.Y ? a.Y : b.Y; - } - - #endregion - - #region Clamp - - /// - /// Clamp a vector to the given minimum and maximum vectors - /// - /// Input vector - /// Minimum vector - /// Maximum vector - /// The clamped vector - public static Vector2 Clamp(Vector2 vec, Vector2 min, Vector2 max) - { - vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; - vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; - return vec; - } - - /// - /// Clamp a vector to the given minimum and maximum vectors - /// - /// Input vector - /// Minimum vector - /// Maximum vector - /// The clamped vector - public static void Clamp(ref Vector2 vec, ref Vector2 min, ref Vector2 max, out Vector2 result) - { - result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; - result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; - } - - #endregion - - #region Normalize - - /// - /// Scale a vector to unit length - /// - /// The input vector - /// The normalized vector - public static Vector2 Normalize(Vector2 vec) - { - float scale = 1.0f / vec.Length; - vec.X *= scale; - vec.Y *= scale; - return vec; - } - - /// - /// Scale a vector to unit length - /// - /// The input vector - /// The normalized vector - public static void Normalize(ref Vector2 vec, out Vector2 result) - { - float scale = 1.0f / vec.Length; - result.X = vec.X * scale; - result.Y = vec.Y * scale; - } - - #endregion - - #region NormalizeFast - - /// - /// Scale a vector to approximately unit length - /// - /// The input vector - /// The normalized vector - public static Vector2 NormalizeFast(Vector2 vec) - { - float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y); - vec.X *= scale; - vec.Y *= scale; - return vec; - } - - /// - /// Scale a vector to approximately unit length - /// - /// The input vector - /// The normalized vector - public static void NormalizeFast(ref Vector2 vec, out Vector2 result) - { - float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y); - result.X = vec.X * scale; - result.Y = vec.Y * scale; - } - - #endregion - - #region Dot - - /// - /// Caclulate the dot (scalar) product of two vectors - /// - /// First operand - /// Second operand - /// The dot product of the two inputs - public static float Dot(Vector2 left, Vector2 right) - { - return left.X * right.X + left.Y * right.Y; - } - - #endregion - - #region Lerp - - /// - /// Returns a new Vector that is the linear blend of the 2 given Vectors - /// - /// First input vector - /// Second input vector - /// The blend factor - /// a when blend=0, b when blend=1, and a linear combination otherwise - public static Vector2 Lerp(Vector2 a, Vector2 b, float blend) - { - a.X = blend * (b.X - a.X) + a.X; - a.Y = blend * (b.Y - a.Y) + a.Y; - return a; - } - - #endregion - - #region Barycentric - - /// - /// Interpolate 3 Vectors using Barycentric coordinates - /// - /// First input Vector - /// Second input Vector - /// Third input Vector - /// First Barycentric Coordinate - /// Second Barycentric Coordinate - /// a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise - public static Vector2 BaryCentric(Vector2 a, Vector2 b, Vector2 c, float u, float v) - { - return a + u * (b - a) + v * (c - a); - } - - #endregion - - #endregion - - #region public override string ToString() - - /// + /// /// Returns a System.String that represents the current Vector2. /// /// @@ -673,6 +714,10 @@ namespace OpenTK.Math #endregion + #endregion + + #endregion + #region IEquatable Members /// Indicates whether the current vector is equal to another vector. diff --git a/Source/OpenTK/Math/Vector2d.cs b/Source/OpenTK/Math/Vector2d.cs index 4e1d0edd6a3639e7d211a3f6bfb4822dd8cb91d4..bf9f9a54803f97cc3e8f8450da3660e9d74b277c 100644 GIT binary patch literal 38062 zcmeI5`)}MvlE?XL7TEt`;sq9Rc4g-Q91a^N2S<(*BbFUVve&-D9iWGmcq2<*y%HzL zzrMTQPuG;Xo73cwGo+;43c_fHWOsGFtGc>L{_`JysE(^w)pynF>Zm%bCe>>An^cc= zW>;4Zs}ueA^S`ftSKX~XsQ6w}kGIu3^?$AY-&dzSGwQt#I~XBbX&g)?WO4xTXDsjl1j`}%dD^U%4X z^Txkh)n;vsTjG19Wlue})o-HShk=LQv)*qaEaCk3f_J9xN#NCqej(RgeH$jg2G*{i zo(W^;MZGujLrOUnK6=y@=z;HW?o8B~B%G^^_C{A3XJ1maW8~s8d#heML0e9QJJ)IJ zc<%&S4Ccv2%c#$^H<{ekD91VigM+{^!{K${IoO%bI0be7MM$nkra#7mT$`L`cn-hc zXxt{aaGUn~!v0u%=Lp%OMW(N44fvr?U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-Jf{5PXk3&wQvSKd#`)ee!cpGuma(u5 z9eVEJQA69u8Gat(f;sF7P*0a5;8v?s{vwwH<`#?MRp{EPi-QUS( z*R0P!R%v}J;sY-u=iZI#5C0hTh?jwux1v|QOFZQ-I)%ot3KD7#W8$3kn6WY2SEa`6 z=1*5elgmJJh~OzLAdc!)u?O}W$ZdAW8vV1a-L1oWpc?-^wm(>g75N#rV}iCQd!6{^ z7}oP~B!zqNHE~PRqvx#7UYFxiJW~9Q6qo6JWrKmPDa|c!=T_&bJo`~L4&LO z9g>8PgkE23fvxhW(?*^rj;9^cfV{bre@9MvOcHrd$7gihryuOe{WsFwTJsz7DOV)Z zb$a>>o}ACXv+)_WZhzlX@>2bs%niEmuv`2j|~4&bZc#bE!t%@_1pi5s>Z_-(GrI3L%2mUTsc z&F~Bf0l&BxeEcJMi7V$FZgm_NF%vrLKkkRZxYNbg{~(EaXp&U&XVq`_2v#$q^^f{{ybh=x^96yaPVb8NrYlz%q3dkHwKv z^khC~&F>toP*Y<>&liN#IS#8^h1Z%-tR4_NrK)2|OeFyG;e;i-Ohw!>J%6Jg9?tb)PG0XFjI?Q0& zvU%??gcya4_>hjsUBcaRzzw3f&tfna$sEE_Vmu9%&r>%Zdt!+=ZRu<{>1<@Bld4&y r7HeDgmlV_RzVUq*VP+KdP;K~ZoZ}QfQ-ZwsYtqj~^DAe&Ozr;xkn$y9 diff --git a/Source/OpenTK/Math/Vector2h.cs b/Source/OpenTK/Math/Vector2h.cs new file mode 100644 index 00000000..aec2b4b9 --- /dev/null +++ b/Source/OpenTK/Math/Vector2h.cs @@ -0,0 +1,337 @@ +#region --- License --- +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +#endregion + +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; + +namespace OpenTK.Math +{ + + /// 2-component Vector of the Half type. Occupies 4 Byte total. + [Serializable, StructLayout(LayoutKind.Sequential)] + public struct Vector2h : ISerializable, IEquatable + { + #region Fields + + /// The X component of the Half2. + public Half X; + + /// The Y component of the Half2. + public Half Y; + + #endregion + + #region Constructors + + /// + /// The new Half2 instance will avoid conversion and copy directly from the Half parameters. + /// + /// An Half instance of a 16-Bit half precision floating point number. + /// An Half instance of a 16-Bit half precision floating point number. + public Vector2h(Half x, Half y) + { + X = x; + Y = y; + } + + /// + /// The new Half2 instance will convert the 2 parameters into 16-Bit Half precision floating point. + /// + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + public Vector2h(Single x, Single y) + { + X = new Half(x); + Y = new Half(y); + } + + /// + /// The new Half2 instance will convert the 2 parameters into 16-Bit Half precision floating point. + /// + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + /// Enable checks that will throw if the conversion result is not meaningful. + public Vector2h(Single x, Single y, bool throwOnError) + { + X = new Half(x, throwOnError); + Y = new Half(y, throwOnError); + } + + /// + /// The new Half2 instance will convert the Vector2 into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector2 + [CLSCompliant(false)] + public Vector2h(Vector2 v) + { + X = new Half(v.X); + Y = new Half(v.Y); + } + + /// + /// The new Half2 instance will convert the Vector2 into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector2 + /// Enable checks that will throw if the conversion result is not meaningful. + [CLSCompliant(false)] + public Vector2h(Vector2 v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + } + + /// + /// The new Half2 instance will convert the Vector2 into 16-Bit Half precision floating point. + /// This is the fastest constructor. + /// + /// OpenTK.Math.Vector2 + public Vector2h(ref Vector2 v) + { + X = new Half(v.X); + Y = new Half(v.Y); + } + + /// + /// The new Half2 instance will convert the Vector2 into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector2 + /// Enable checks that will throw if the conversion result is not meaningful. + public Vector2h(ref Vector2 v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + } + + /// + /// The new Half2 instance will convert the Vector2d into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector2d + public Vector2h(Vector2d v) + { + X = new Half(v.X); + Y = new Half(v.Y); + } + + /// + /// The new Half2 instance will convert the Vector2d into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector2d + /// Enable checks that will throw if the conversion result is not meaningful. + public Vector2h(Vector2d v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + } + + /// + /// The new Half2 instance will convert the Vector2d into 16-Bit Half precision floating point. + /// This is the faster constructor. + /// + /// OpenTK.Math.Vector2d + [CLSCompliant(false)] + public Vector2h(ref Vector2d v) + { + X = new Half(v.X); + Y = new Half(v.Y); + } + + /// + /// The new Half2 instance will convert the Vector2d into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector2d + /// Enable checks that will throw if the conversion result is not meaningful. + [CLSCompliant(false)] + public Vector2h(ref Vector2d v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + } + + #endregion Constructors + + #region Half -> Single + + /// + /// Returns this Half2 instance's contents as Vector2. + /// + /// OpenTK.Math.Vector2 + public Vector2 ToVector2() + { + return new Vector2(X, Y); + } + + /// + /// Returns this Half2 instance's contents as Vector2d. + /// + /// OpenTK.Math.Vector2d + public Vector2d ToVector2d() + { + return new Vector2d(X, Y); + } + + #endregion Half -> Single + + #region Conversions + + /// Converts OpenTK.Math.Vector2 to OpenTK.Math.Half2. + /// The Vector2 to convert. + /// The resulting Half vector. + public static explicit operator Vector2h(Vector2 v) + { + return new Vector2h(v); + } + + /// Converts OpenTK.Math.Vector2d to OpenTK.Math.Half2. + /// The Vector2d to convert. + /// The resulting Half vector. + public static explicit operator Vector2h(Vector2d v) + { + return new Vector2h(v); + } + + /// Converts OpenTK.Math.Half2 to OpenTK.Math.Vector2. + /// The Half2 to convert. + /// The resulting Vector2. + public static explicit operator Vector2(Vector2h h) + { + return new Vector2(h.X, h.Y); + } + + /// Converts OpenTK.Math.Half2 to OpenTK.Math.Vector2d. + /// The Half2 to convert. + /// The resulting Vector2d. + public static explicit operator Vector2d(Vector2h h) + { + return new Vector2d(h.X, h.Y); + } + + #endregion Conversions + + #region Constants + + /// The size in bytes for an instance of the Half2 struct is 4. + public static readonly int SizeInBytes = 4; + + #endregion Constants + + #region ISerializable + + /// Constructor used by ISerializable to deserialize the object. + /// + /// + public Vector2h(SerializationInfo info, StreamingContext context) + { + this.X = (Half)info.GetValue("X", typeof(Half)); + this.Y = (Half)info.GetValue("Y", typeof(Half)); + } + + /// Used by ISerialize to serialize the object. + /// + /// + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue("X", this.X); + info.AddValue("Y", this.Y); + } + + #endregion ISerializable + + #region Binary dump + + /// Updates the X and Y components of this instance by reading from a Stream. + /// A BinaryReader instance associated with an open Stream. + public void FromBinaryStream(BinaryReader bin) + { + X.FromBinaryStream(bin); + Y.FromBinaryStream(bin); + } + + /// Writes the X and Y components of this instance into a Stream. + /// A BinaryWriter instance associated with an open Stream. + public void ToBinaryStream(BinaryWriter bin) + { + X.ToBinaryStream(bin); + Y.ToBinaryStream(bin); + } + + #endregion Binary dump + + #region IEquatable Members + + /// Returns a value indicating whether this instance is equal to a specified OpenTK.Math.Half2 vector. + /// OpenTK.Math.Half2 to compare to this instance.. + /// True, if other is equal to this instance; false otherwise. + public bool Equals(Vector2h other) + { + return (this.X.Equals(other.X) && this.Y.Equals(other.Y)); + } + + #endregion + + #region ToString() + + /// Returns a string that contains this Half2's numbers in human-legible form. + public override string ToString() + { + return String.Format("({0}, {1})", X.ToString(), Y.ToString()); + } + + #endregion ToString() + + #region BitConverter + + /// Returns the Half2 as an array of bytes. + /// The Half2 to convert. + /// The input as byte array. + public static byte[] GetBytes(Vector2h h) + { + byte[] result = new byte[SizeInBytes]; + + byte[] temp = Half.GetBytes(h.X); + result[0] = temp[0]; + result[1] = temp[1]; + temp = Half.GetBytes(h.Y); + result[2] = temp[0]; + result[3] = temp[1]; + + return result; + } + + /// Converts an array of bytes into Half2. + /// A Half2 in it's byte[] representation. + /// The starting position within value. + /// A new Half2 instance. + public static Vector2h FromBytes(byte[] value, int startIndex) + { + Vector2h h2 = new Vector2h(); + h2.X = Half.FromBytes(value, startIndex); + h2.Y = Half.FromBytes(value, startIndex + 2); + return h2; + } + + #endregion BitConverter + } +} \ No newline at end of file diff --git a/Source/OpenTK/Math/Vector3.cs b/Source/OpenTK/Math/Vector3.cs index d62b2835..6d033582 100644 --- a/Source/OpenTK/Math/Vector3.cs +++ b/Source/OpenTK/Math/Vector3.cs @@ -1,14 +1,28 @@ #region --- License --- -/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos - * See license.txt for license info - * - * Contributions by Andy Gill. +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ #endregion using System; -using System.Collections.Generic; -using System.Text; using System.Runtime.InteropServices; namespace OpenTK.Math @@ -37,31 +51,6 @@ namespace OpenTK.Math /// public float Z; - /// - /// Defines a unit-length Vector3 that points towards the X-axis. - /// - public static readonly Vector3 UnitX = new Vector3(1, 0, 0); - - /// - /// Defines a unit-length Vector3 that points towards the Y-axis. - /// - public static readonly Vector3 UnitY = new Vector3(0, 1, 0); - - /// - /// /// Defines a unit-length Vector3 that points towards the Z-axis. - /// - public static readonly Vector3 UnitZ = new Vector3(0, 0, 1); - - /// - /// Defines a zero-length Vector3. - /// - public static readonly Vector3 Zero = new Vector3(0, 0, 0); - - /// - /// Defines the size of the Vector3 struct in bytes. - /// - public static readonly int SizeInBytes = Marshal.SizeOf(new Vector3()); - #endregion #region Constructors @@ -114,33 +103,9 @@ namespace OpenTK.Math #endregion - #region Functions + #region Public Members - #region This property - - public float this[int index] - { - get - { - switch (index) - { - case 0: return X; - case 1: return Y; - case 2: return Z; - default: throw new ArgumentOutOfRangeException("index", index, "Should be 0, 1 or 2."); - } - /* - unsafe - { - fixed (float* ptr = &this.X) - return *(ptr + index); - } - - */ - } - } - - #endregion + #region Instance #region public float Length @@ -251,196 +216,162 @@ namespace OpenTK.Math #endregion - #region Operator overloads + #region Static - public static Vector3 operator +(Vector3 left, Vector3 right) + #region Fields + + /// + /// Defines a unit-length Vector3 that points towards the X-axis. + /// + public static readonly Vector3 UnitX = new Vector3(1, 0, 0); + + /// + /// Defines a unit-length Vector3 that points towards the Y-axis. + /// + public static readonly Vector3 UnitY = new Vector3(0, 1, 0); + + /// + /// /// Defines a unit-length Vector3 that points towards the Z-axis. + /// + public static readonly Vector3 UnitZ = new Vector3(0, 0, 1); + + /// + /// Defines a zero-length Vector3. + /// + public static readonly Vector3 Zero = new Vector3(0, 0, 0); + + /// + /// Defines the size of the Vector3 struct in bytes. + /// + public static readonly int SizeInBytes = Marshal.SizeOf(new Vector3()); + + #endregion + + #region Add + + /// + /// Add two Vectors + /// + /// First operand + /// Second operand + /// Result of addition + public static Vector3 Add(Vector3 a, Vector3 b) { - left.X += right.X; - left.Y += right.Y; - left.Z += right.Z; - return left; + a.X += b.X; + a.Y += b.Y; + a.Z += b.Z; + return a; } - public static Vector3 operator -(Vector3 left, Vector3 right) + /// + /// Add two Vectors + /// + /// First operand + /// Second operand + /// Result of addition + public static void Add(ref Vector3 a, ref Vector3 b, out Vector3 result) { - left.X -= right.X; - left.Y -= right.Y; - left.Z -= right.Z; - return left; - } - - public static Vector3 operator -(Vector3 vec) - { - vec.X = -vec.X; - vec.Y = -vec.Y; - vec.Z = -vec.Z; - return vec; - } - - public static Vector3 operator *(Vector3 vec, float f) - { - vec.X *= f; - vec.Y *= f; - vec.Z *= f; - return vec; - } - - public static Vector3 operator *(float f, Vector3 vec) - { - vec.X *= f; - vec.Y *= f; - vec.Z *= f; - return vec; - } - - public static Vector3 operator /(Vector3 vec, float f) - { - float mult = 1.0f / f; - vec.X *= mult; - vec.Y *= mult; - vec.Z *= mult; - return vec; - } - - public static bool operator ==(Vector3 left, Vector3 right) - { - return left.Equals(right); - } - - public static bool operator !=(Vector3 left, Vector3 right) - { - return !left.Equals(right); + result.X = a.X + b.X; + result.Y = a.Y + b.Y; + result.Z = a.Z + b.Z; } #endregion - #region Static functions + #region Sub - #region Add + /// + /// Subtract one Vector from another + /// + /// First operand + /// Second operand + /// Result of subtraction + public static Vector3 Sub(Vector3 a, Vector3 b) + { + a.X -= b.X; + a.Y -= b.Y; + a.Z -= b.Z; + return a; + } - /// - /// Add two Vectors - /// - /// First operand - /// Second operand - /// Result of addition - public static Vector3 Add(Vector3 a, Vector3 b) - { - a.X += b.X; - a.Y += b.Y; - a.Z += b.Z; - return a; - } + /// + /// Subtract one Vector from another + /// + /// First operand + /// Second operand + /// Result of subtraction + public static void Sub(ref Vector3 a, ref Vector3 b, out Vector3 result) + { + result.X = a.X - b.X; + result.Y = a.Y - b.Y; + result.Z = a.Z - b.Z; + } - /// - /// Add two Vectors - /// - /// First operand - /// Second operand - /// Result of addition - public static void Add(ref Vector3 a, ref Vector3 b, out Vector3 result) - { - result.X = a.X + b.X; - result.Y = a.Y + b.Y; - result.Z = a.Z + b.Z; - } + #endregion - #endregion + #region Mult - #region Sub + /// + /// Multiply a vector and a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the multiplication + public static Vector3 Mult(Vector3 a, float f) + { + a.X *= f; + a.Y *= f; + a.Z *= f; + return a; + } - /// - /// Subtract one Vector from another - /// - /// First operand - /// Second operand - /// Result of subtraction - public static Vector3 Sub(Vector3 a, Vector3 b) - { - a.X -= b.X; - a.Y -= b.Y; - a.Z -= b.Z; - return a; - } + /// + /// Multiply a vector and a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the multiplication + public static void Mult(ref Vector3 a, float f, out Vector3 result) + { + result.X = a.X * f; + result.Y = a.Y * f; + result.Z = a.Z * f; + } - /// - /// Subtract one Vector from another - /// - /// First operand - /// Second operand - /// Result of subtraction - public static void Sub(ref Vector3 a, ref Vector3 b, out Vector3 result) - { - result.X = a.X - b.X; - result.Y = a.Y - b.Y; - result.Z = a.Z - b.Z; - } + #endregion - #endregion + #region Div - #region Mult + /// + /// Divide a vector by a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the division + public static Vector3 Div(Vector3 a, float f) + { + float mult = 1.0f / f; + a.X *= mult; + a.Y *= mult; + a.Z *= mult; + return a; + } - /// - /// Multiply a vector and a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the multiplication - public static Vector3 Mult(Vector3 a, float f) - { - a.X *= f; - a.Y *= f; - a.Z *= f; - return a; - } + /// + /// Divide a vector by a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the division + public static void Div(ref Vector3 a, float f, out Vector3 result) + { + float mult = 1.0f / f; + result.X = a.X * mult; + result.Y = a.Y * mult; + result.Z = a.Z * mult; + } - /// - /// Multiply a vector and a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the multiplication - public static void Mult(ref Vector3 a, float f, out Vector3 result) - { - result.X = a.X * f; - result.Y = a.Y * f; - result.Z = a.Z * f; - } - - #endregion - - #region Div - - /// - /// Divide a vector by a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the division - public static Vector3 Div(Vector3 a, float f) - { - float mult = 1.0f / f; - a.X *= mult; - a.Y *= mult; - a.Z *= mult; - return a; - } - - /// - /// Divide a vector by a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the division - public static void Div(ref Vector3 a, float f, out Vector3 result) - { - float mult = 1.0f / f; - result.X = a.X * mult; - result.Y = a.Y * mult; - result.Z = a.Z * mult; - } - - #endregion + #endregion #region ComponentMin @@ -534,125 +465,125 @@ namespace OpenTK.Math #endregion - #region Clamp + #region Clamp - /// - /// Clamp a vector to the given minimum and maximum vectors - /// - /// Input vector - /// Minimum vector - /// Maximum vector - /// The clamped vector - public static Vector3 Clamp(Vector3 vec, Vector3 min, Vector3 max) - { - vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; - vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; - vec.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; - return vec; - } + /// + /// Clamp a vector to the given minimum and maximum vectors + /// + /// Input vector + /// Minimum vector + /// Maximum vector + /// The clamped vector + public static Vector3 Clamp(Vector3 vec, Vector3 min, Vector3 max) + { + vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; + vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; + vec.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; + return vec; + } - /// - /// Clamp a vector to the given minimum and maximum vectors - /// - /// Input vector - /// Minimum vector - /// Maximum vector - /// The clamped vector - public static void Clamp(ref Vector3 vec, ref Vector3 min, ref Vector3 max, out Vector3 result) - { - result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; - result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; - result.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; - } + /// + /// Clamp a vector to the given minimum and maximum vectors + /// + /// Input vector + /// Minimum vector + /// Maximum vector + /// The clamped vector + public static void Clamp(ref Vector3 vec, ref Vector3 min, ref Vector3 max, out Vector3 result) + { + result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; + result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; + result.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; + } - #endregion + #endregion - #region Normalize + #region Normalize - /// - /// Scale a vector to unit length - /// - /// The input vector - /// The normalized vector - public static Vector3 Normalize(Vector3 vec) + /// + /// Scale a vector to unit length + /// + /// The input vector + /// The normalized vector + public static Vector3 Normalize(Vector3 vec) { float scale = 1.0f / vec.Length; vec.X *= scale; vec.Y *= scale; vec.Z *= scale; - return vec; + return vec; } - /// - /// Scale a vector to unit length - /// - /// The input vector - /// The normalized vector - public static void Normalize(ref Vector3 vec, out Vector3 result) - { + /// + /// Scale a vector to unit length + /// + /// The input vector + /// The normalized vector + public static void Normalize(ref Vector3 vec, out Vector3 result) + { float scale = 1.0f / vec.Length; result.X = vec.X * scale; result.Y = vec.Y * scale; result.Z = vec.Z * scale; - } + } - #endregion + #endregion - #region NormalizeFast + #region NormalizeFast - /// - /// Scale a vector to approximately unit length - /// - /// The input vector - /// The normalized vector - public static Vector3 NormalizeFast(Vector3 vec) + /// + /// Scale a vector to approximately unit length + /// + /// The input vector + /// The normalized vector + public static Vector3 NormalizeFast(Vector3 vec) { float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z); vec.X *= scale; vec.Y *= scale; vec.Z *= scale; - return vec; + return vec; } - /// - /// Scale a vector to approximately unit length - /// - /// The input vector - /// The normalized vector - public static void NormalizeFast(ref Vector3 vec, out Vector3 result) - { + /// + /// Scale a vector to approximately unit length + /// + /// The input vector + /// The normalized vector + public static void NormalizeFast(ref Vector3 vec, out Vector3 result) + { float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z); result.X = vec.X * scale; result.Y = vec.Y * scale; result.Z = vec.Z * scale; - } + } - #endregion + #endregion - #region Dot + #region Dot - /// - /// Caclulate the dot (scalar) product of two vectors - /// - /// First operand - /// Second operand - /// The dot product of the two inputs - public static float Dot(Vector3 left, Vector3 right) + /// + /// Caclulate the dot (scalar) product of two vectors + /// + /// First operand + /// Second operand + /// The dot product of the two inputs + public static float Dot(Vector3 left, Vector3 right) { return left.X * right.X + left.Y * right.Y + left.Z * right.Z; - } + } - #endregion + #endregion - #region Cross + #region Cross - /// - /// Caclulate the cross (vector) product of two vectors - /// - /// First operand - /// Second operand - /// The cross product of the two inputs - public static Vector3 Cross(Vector3 left, Vector3 right) + /// + /// Caclulate the cross (vector) product of two vectors + /// + /// First operand + /// Second operand + /// The cross product of the two inputs + public static Vector3 Cross(Vector3 left, Vector3 right) { float x = left.Y * right.Z - left.Z * right.Y, @@ -664,179 +595,244 @@ namespace OpenTK.Math return left; } - /// - /// Caclulate the cross (vector) product of two vectors - /// - /// First operand - /// Second operand - /// The cross product of the two inputs - /// The cross product of the two inputs + /// + /// Caclulate the cross (vector) product of two vectors + /// + /// First operand + /// Second operand + /// The cross product of the two inputs + /// The cross product of the two inputs public static void Cross(ref Vector3 left, ref Vector3 right, out Vector3 result) { - result.X = left.Y * right.Z - left.Z * right.Y; + result.X = left.Y * right.Z - left.Z * right.Y; result.Y = left.Z * right.X - left.X * right.Z; result.Z = left.X * right.Y - left.Y * right.X; - } + } - #endregion + #endregion - #region Lerp + #region Lerp - /// - /// Returns a new Vector that is the linear blend of the 2 given Vectors - /// - /// First input vector - /// Second input vector - /// The blend factor - /// a when blend=0, b when blend=1, and a linear combination otherwise - public static Vector3 Lerp(Vector3 a, Vector3 b, float blend) + /// + /// Returns a new Vector that is the linear blend of the 2 given Vectors + /// + /// First input vector + /// Second input vector + /// The blend factor + /// a when blend=0, b when blend=1, and a linear combination otherwise + public static Vector3 Lerp(Vector3 a, Vector3 b, float blend) + { + a.X = blend * (b.X - a.X) + a.X; + a.Y = blend * (b.Y - a.Y) + a.Y; + a.Z = blend * (b.Z - a.Z) + a.Z; + return a; + } + + #endregion + + #region Barycentric + + /// + /// Interpolate 3 Vectors using Barycentric coordinates + /// + /// First input Vector + /// Second input Vector + /// Third input Vector + /// First Barycentric Coordinate + /// Second Barycentric Coordinate + /// a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise + public static Vector3 BaryCentric(Vector3 a, Vector3 b, Vector3 c, float u, float v) + { + return a + u * (b - a) + v * (c - a); + } + + #endregion + + #region Transform + + /// + /// Transform a direction vector by the given Matrix + /// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored. + /// + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static Vector3 TransformVector(Vector3 vec, Matrix4 mat) + { + Vector3 v; + v.X = Vector3.Dot(vec, new Vector3(mat.Column0)); + v.Y = Vector3.Dot(vec, new Vector3(mat.Column1)); + v.Z = Vector3.Dot(vec, new Vector3(mat.Column2)); + return v; + } + + /// + /// Transform a Normal by the given Matrix + /// + /// + /// This calculates the inverse of the given matrix, use TransformNormalInverse if you + /// already have the inverse to avoid this extra calculation + /// + /// The normal to transform + /// The desired transformation + /// The transformed normal + public static Vector3 TransformNormal(Vector3 norm, Matrix4 mat) + { + mat.Invert(); + return TransformNormalInverse(norm, mat); + } + + /// + /// Transform a Normal by the (transpose of the) given Matrix + /// + /// + /// This version doesn't calculate the inverse matrix. + /// Use this version if you already have the inverse of the desired transform to hand + /// + /// The normal to transform + /// The inverse of the desired transformation + /// The transformed normal + public static Vector3 TransformNormalInverse(Vector3 norm, Matrix4 invMat) + { + Vector3 n; + n.X = Vector3.Dot(norm, new Vector3(invMat.Row0)); + n.Y = Vector3.Dot(norm, new Vector3(invMat.Row1)); + n.Z = Vector3.Dot(norm, new Vector3(invMat.Row2)); + return n; + } + + /// + /// Transform a Position by the given Matrix + /// + /// The position to transform + /// The desired transformation + /// The transformed position + public static Vector3 TransformPosition(Vector3 pos, Matrix4 mat) + { + Vector3 p; + p.X = Vector3.Dot(pos, new Vector3(mat.Column0)) + mat.Row3.X; + p.Y = Vector3.Dot(pos, new Vector3(mat.Column1)) + mat.Row3.Y; + p.Z = Vector3.Dot(pos, new Vector3(mat.Column2)) + mat.Row3.Z; + return p; + } + + /// + /// Transform a Vector by the given Matrix + /// + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static Vector4 Transform(Vector3 vec, Matrix4 mat) + { + Vector4 v4 = new Vector4(vec.X, vec.Y, vec.Z, 1.0f); + Vector4 result; + result.X = Vector4.Dot(v4, mat.Column0); + result.Y = Vector4.Dot(v4, mat.Column1); + result.Z = Vector4.Dot(v4, mat.Column2); + result.W = Vector4.Dot(v4, mat.Column3); + return result; + } + + /// + /// Transform a Vector3 by the given Matrix, and project the resulting Vector4 back to a Vector3 + /// + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static Vector3 TransformPerspective(Vector3 vec, Matrix4 mat) + { + Vector4 h = Transform(vec, mat); + return new Vector3(h.X / h.W, h.Y / h.W, h.Z / h.W); + } + + #endregion + + #region CalculateAngle + + /// + /// Calculates the angle (in radians) between two vectors. + /// + /// The first vector. + /// The second vector. + /// Angle (in radians) between the vectors. + /// Note that the returned angle is never bigger than the constant Pi. + public static float CalculateAngle(Vector3 first, Vector3 second) + { + return (float)System.Math.Acos((Vector3.Dot(first, second)) / (first.Length * second.Length)); + } + + #endregion + + #endregion + + #region Operators + + public static Vector3 operator +(Vector3 left, Vector3 right) + { + left.X += right.X; + left.Y += right.Y; + left.Z += right.Z; + return left; + } + + public static Vector3 operator -(Vector3 left, Vector3 right) + { + left.X -= right.X; + left.Y -= right.Y; + left.Z -= right.Z; + return left; + } + + public static Vector3 operator -(Vector3 vec) { - a.X = blend * (b.X - a.X) + a.X; - a.Y = blend * (b.Y - a.Y) + a.Y; - a.Z = blend * (b.Z - a.Z) + a.Z; - return a; + vec.X = -vec.X; + vec.Y = -vec.Y; + vec.Z = -vec.Z; + return vec; } - #endregion - - #region Barycentric - - /// - /// Interpolate 3 Vectors using Barycentric coordinates - /// - /// First input Vector - /// Second input Vector - /// Third input Vector - /// First Barycentric Coordinate - /// Second Barycentric Coordinate - /// a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise - public static Vector3 BaryCentric(Vector3 a, Vector3 b, Vector3 c, float u, float v) + public static Vector3 operator *(Vector3 vec, float f) { - return a + u * (b - a) + v * (c - a); + vec.X *= f; + vec.Y *= f; + vec.Z *= f; + return vec; } - #endregion - - #region Transform - - /// - /// Transform a direction vector by the given Matrix - /// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored. - /// - /// The vector to transform - /// The desired transformation - /// The transformed vector - public static Vector3 TransformVector(Vector3 vec, Matrix4 mat) + public static Vector3 operator *(float f, Vector3 vec) { - Vector3 v; - v.X = Vector3.Dot(vec, new Vector3(mat.Column0)); - v.Y = Vector3.Dot(vec, new Vector3(mat.Column1)); - v.Z = Vector3.Dot(vec, new Vector3(mat.Column2)); - return v; + vec.X *= f; + vec.Y *= f; + vec.Z *= f; + return vec; } - /// - /// Transform a Normal by the given Matrix - /// - /// - /// This calculates the inverse of the given matrix, use TransformNormalInverse if you - /// already have the inverse to avoid this extra calculation - /// - /// The normal to transform - /// The desired transformation - /// The transformed normal - public static Vector3 TransformNormal(Vector3 norm, Matrix4 mat) + public static Vector3 operator /(Vector3 vec, float f) { - mat.Invert(); - return TransformNormalInverse(norm, mat); + float mult = 1.0f / f; + vec.X *= mult; + vec.Y *= mult; + vec.Z *= mult; + return vec; } - /// - /// Transform a Normal by the (transpose of the) given Matrix - /// - /// - /// This version doesn't calculate the inverse matrix. - /// Use this version if you already have the inverse of the desired transform to hand - /// - /// The normal to transform - /// The inverse of the desired transformation - /// The transformed normal - public static Vector3 TransformNormalInverse(Vector3 norm, Matrix4 invMat) - { - Vector3 n; - n.X = Vector3.Dot(norm, new Vector3(invMat.Row0)); - n.Y = Vector3.Dot(norm, new Vector3(invMat.Row1)); - n.Z = Vector3.Dot(norm, new Vector3(invMat.Row2)); - return n; - } + public static bool operator ==(Vector3 left, Vector3 right) + { + return left.Equals(right); + } - /// - /// Transform a Position by the given Matrix - /// - /// The position to transform - /// The desired transformation - /// The transformed position - public static Vector3 TransformPosition(Vector3 pos, Matrix4 mat) - { - Vector3 p; - p.X = Vector3.Dot(pos, new Vector3(mat.Column0)) + mat.Row3.X; - p.Y = Vector3.Dot(pos, new Vector3(mat.Column1)) + mat.Row3.Y; - p.Z = Vector3.Dot(pos, new Vector3(mat.Column2)) + mat.Row3.Z; - return p; - } + public static bool operator !=(Vector3 left, Vector3 right) + { + return !left.Equals(right); + } - /// - /// Transform a Vector by the given Matrix - /// - /// The vector to transform - /// The desired transformation - /// The transformed vector - public static Vector4 Transform(Vector3 vec, Matrix4 mat) - { - Vector4 v4 = new Vector4(vec.X, vec.Y, vec.Z, 1.0f); - Vector4 result; - result.X = Vector4.Dot(v4, mat.Column0); - result.Y = Vector4.Dot(v4, mat.Column1); - result.Z = Vector4.Dot(v4, mat.Column2); - result.W = Vector4.Dot(v4, mat.Column3); - return result; - } + #endregion - /// - /// Transform a Vector3 by the given Matrix, and project the resulting Vector4 back to a Vector3 - /// - /// The vector to transform - /// The desired transformation - /// The transformed vector - public static Vector3 TransformPerspective(Vector3 vec, Matrix4 mat) - { - Vector4 h = Transform(vec, mat); - return new Vector3(h.X / h.W, h.Y / h.W, h.Z / h.W); - } + #region Overrides - #endregion + #region public override string ToString() - #region CalculateAngle - - /// - /// Calculates the angle (in radians) between two vectors. - /// - /// The first vector. - /// The second vector. - /// Angle (in radians) between the vectors. - /// Note that the returned angle is never bigger than the constant Pi. - public static float CalculateAngle(Vector3 first, Vector3 second) - { - return (float)System.Math.Acos((Vector3.Dot(first, second)) / (first.Length * second.Length)); - } - - #endregion - - #endregion - - #region public override string ToString() - - /// + /// /// Returns a System.String that represents the current Vector3. /// /// @@ -877,6 +873,10 @@ namespace OpenTK.Math #endregion + #endregion + + #endregion + #region IEquatable Members /// Indicates whether the current vector is equal to another vector. diff --git a/Source/OpenTK/Math/Vector3d.cs b/Source/OpenTK/Math/Vector3d.cs index 8a0ea6beba2229eefec2cefc47bb22ddd52f55f8..9558639662e45b92ca7f1a99d0755dd26dcf0cdf 100644 GIT binary patch literal 58538 zcmeI5{cjXUlgH=RJks+&Y>4!nNgfOd(dooPI1va53I@bm0;f9(8=LqEHpFiMqUT?~ zyZUtP*WEqSGrO}pyEef>W_D(}tE;Q4zExe_GxLA{`&qSDZB{R}2+>Ydf!w)($R2Ucpu>YTUZSJfr^_wS*e&i=#B z9$7i4-Lsm|=E`gP_p18Us6gSB9oLQKbNhQ&t%jCp?Y@=Yt9I<}zWo!ooQ(oada-Ub zM#c-Zca7R+^`iQ_UD>iLoAz&HEp6HV8&(7QFYOxl5A6CV)S*6{?HOH{$7`c{VE49- zB7ZMKNoaWQohBR}7**$ox?6VsduWAr;pas-!u@Njy=~W=!;#(L%AxfRnb6vS-S_sl z?AI&1PCr-dy36le^|02)Ig@+r%L}XV-0F?2_HK}&YS!zGj7uc{i_tr@<0!~#-+nQ! z8+LR~pbf1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OYZDJr|a5h^HmE{IOt zIn@ErFU3Q9L)TO;0<>$<^_xH!%c&zXik**EPG1Yx_5>efji@p~#~-ztNbR(kjg?Qp z(7M%Qfgikf;MdM6O{hgW>at!EcRU=6wRkQ#!AkLWoO#5jEBqwSTk?D+-#Eea&%(K# z=AXo}oCr;K<>my~bA1E*=HI+Kd!texAbl^^j5R#m0EYX~Fy2M^J>W!&l=j{6G~qAI z9Pp`gZ)9e$gh%oPJi*UH840uE?0zdVL7RXNbT@DXHVoDc8u6t0h5WCCjY<>jx$Xhd zl^a`2+%@ob9L}H+EH?TAkG~e{anf3iiv!1n#|Ib3kNbAixQq_QkNd7L?sESAUxm|0 zA|u$%1|I^;JIwlmf5CqS|Ni_GvRYmBPful(!&9kT)`Nf5T#>x_kw`HtJZ?pO*2+ID z9jpzk9yFZTzz*O)HbR*)1878`O5Adnyt(4JYJS(k>T+rhZ+=f~Pa68F`A?zA{fagZ zvTfoO{pDmDvW!?0-UpmM8X?t)0!x0AUMoZn_yjCiTi>@nD)kcK{U;>qVQbqCPNU|# z^2V>gKR6rTdb4i39f{K5WBHTTeVE?&DfCmTZSboP1;ZEeM6BH1@eU0`{({ z)Vn7cl8#wN!Rb=A90d_Ea5n3s;Qg~POB7VvsSJL377Y#8)8#0L*k}C|d~h~q9R*3p zoEE1lC#+lb{ncj@K_7}XKWpRJmBiSnc|hB>dIF0L8x9Kyef>%-?&aoJtv6C={|`zR B$jSf! diff --git a/Source/OpenTK/Math/Vector3h.cs b/Source/OpenTK/Math/Vector3h.cs new file mode 100644 index 00000000..277f1c3f --- /dev/null +++ b/Source/OpenTK/Math/Vector3h.cs @@ -0,0 +1,370 @@ +#region --- License --- +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +#endregion + +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; + +namespace OpenTK.Math +{ + + /// 3-component Vector of the Half type. Occupies 6 Byte total. + [Serializable, StructLayout(LayoutKind.Sequential)] + public struct Vector3h : ISerializable, IEquatable + { + #region Public Fields + + /// The X component of the Half3. + public Half X; + + /// The Y component of the Half3. + public Half Y; + + /// The Z component of the Half3. + public Half Z; + + #endregion Public Fields + + #region Constructors + + /// + /// The new Half3 instance will avoid conversion and copy directly from the Half parameters. + /// + /// An Half instance of a 16-Bit half precision floating point number. + /// An Half instance of a 16-Bit half precision floating point number. + /// An Half instance of a 16-Bit half precision floating point number. + public Vector3h(Half x, Half y, Half z) + { + this.X = x; + this.Y = y; + this.Z = z; + } + + /// + /// The new Half3 instance will convert the 3 parameters into 16-Bit Half precision floating point. + /// + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + public Vector3h(Single x, Single y, Single z) + { + X = new Half(x); + Y = new Half(y); + Z = new Half(z); + } + + /// + /// The new Half3 instance will convert the 3 parameters into 16-Bit Half precision floating point. + /// + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + /// Enable checks that will throw if the conversion result is not meaningful. + public Vector3h(Single x, Single y, Single z, bool throwOnError) + { + X = new Half(x, throwOnError); + Y = new Half(y, throwOnError); + Z = new Half(z, throwOnError); + } + + /// + /// The new Half3 instance will convert the Vector3 into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector3 + [CLSCompliant(false)] + public Vector3h(Vector3 v) + { + X = new Half(v.X); + Y = new Half(v.Y); + Z = new Half(v.Z); + } + + /// + /// The new Half3 instance will convert the Vector3 into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector3 + /// Enable checks that will throw if the conversion result is not meaningful. + [CLSCompliant(false)] + public Vector3h(Vector3 v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + Z = new Half(v.Z, throwOnError); + } + + /// + /// The new Half3 instance will convert the Vector3 into 16-Bit Half precision floating point. + /// This is the fastest constructor. + /// + /// OpenTK.Math.Vector3 + public Vector3h(ref Vector3 v) + { + X = new Half(v.X); + Y = new Half(v.Y); + Z = new Half(v.Z); + } + + /// + /// The new Half3 instance will convert the Vector3 into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector3 + /// Enable checks that will throw if the conversion result is not meaningful. + public Vector3h(ref Vector3 v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + Z = new Half(v.Z, throwOnError); + } + + /// + /// The new Half3 instance will convert the Vector3d into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector3d + public Vector3h(Vector3d v) + { + X = new Half(v.X); + Y = new Half(v.Y); + Z = new Half(v.Z); + } + + /// + /// The new Half3 instance will convert the Vector3d into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector3d + /// Enable checks that will throw if the conversion result is not meaningful. + public Vector3h(Vector3d v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + Z = new Half(v.Z, throwOnError); + } + + /// + /// The new Half3 instance will convert the Vector3d into 16-Bit Half precision floating point. + /// This is the faster constructor. + /// + /// OpenTK.Math.Vector3d + [CLSCompliant(false)] + public Vector3h(ref Vector3d v) + { + X = new Half(v.X); + Y = new Half(v.Y); + Z = new Half(v.Z); + } + + /// + /// The new Half3 instance will convert the Vector3d into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector3d + /// Enable checks that will throw if the conversion result is not meaningful. + [CLSCompliant(false)] + public Vector3h(ref Vector3d v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + Z = new Half(v.Z, throwOnError); + } + + #endregion Constructors + + #region Half -> Single + + /// + /// Returns this Half3 instance's contents as Vector3. + /// + /// OpenTK.Math.Vector3 + public Vector3 ToVector3() + { + return new Vector3(X, Y, Z); + } + + /// + /// Returns this Half3 instance's contents as Vector3d. + /// + /// OpenTK.Math.Vector3d + public Vector3d ToVector3d() + { + return new Vector3d(X, Y, Z); + } + + #endregion Half -> Single + + #region Conversions + + /// Converts OpenTK.Math.Vector3 to OpenTK.Math.Half3. + /// The Vector3 to convert. + /// The resulting Half vector. + public static explicit operator Vector3h(Vector3 v3f) + { + return new Vector3h(v3f); + } + + /// Converts OpenTK.Math.Vector3d to OpenTK.Math.Half3. + /// The Vector3d to convert. + /// The resulting Half vector. + public static explicit operator Vector3h(Vector3d v3d) + { + return new Vector3h(v3d); + } + + /// Converts OpenTK.Math.Half3 to OpenTK.Math.Vector3. + /// The Half3 to convert. + /// The resulting Vector3. + public static explicit operator Vector3(Vector3h h3) + { + Vector3 result = new Vector3(); + result.X = h3.X.ToSingle(); + result.Y = h3.Y.ToSingle(); + result.Z = h3.Z.ToSingle(); + return result; + } + + /// Converts OpenTK.Math.Half3 to OpenTK.Math.Vector3d. + /// The Half3 to convert. + /// The resulting Vector3d. + public static explicit operator Vector3d(Vector3h h3) + { + Vector3d result = new Vector3d(); + result.X = h3.X.ToSingle(); + result.Y = h3.Y.ToSingle(); + result.Z = h3.Z.ToSingle(); + return result; + } + + #endregion Conversions + + #region Constants + + /// The size in bytes for an instance of the Half3 struct is 6. + public static readonly int SizeInBytes = 6; + + #endregion Constants + + #region ISerializable + + /// Constructor used by ISerializable to deserialize the object. + /// + /// + public Vector3h(SerializationInfo info, StreamingContext context) + { + this.X = (Half)info.GetValue("X", typeof(Half)); + this.Y = (Half)info.GetValue("Y", typeof(Half)); + this.Z = (Half)info.GetValue("Z", typeof(Half)); + } + + /// Used by ISerialize to serialize the object. + /// + /// + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue("X", this.X); + info.AddValue("Y", this.Y); + info.AddValue("Z", this.Z); + } + + #endregion ISerializable + + #region Binary dump + + /// Updates the X,Y and Z components of this instance by reading from a Stream. + /// A BinaryReader instance associated with an open Stream. + public void FromBinaryStream(BinaryReader bin) + { + X.FromBinaryStream(bin); + Y.FromBinaryStream(bin); + Z.FromBinaryStream(bin); + } + + /// Writes the X,Y and Z components of this instance into a Stream. + /// A BinaryWriter instance associated with an open Stream. + public void ToBinaryStream(BinaryWriter bin) + { + X.ToBinaryStream(bin); + Y.ToBinaryStream(bin); + Z.ToBinaryStream(bin); + } + + #endregion Binary dump + + #region IEquatable Members + + /// Returns a value indicating whether this instance is equal to a specified OpenTK.Math.Half3 vector. + /// OpenTK.Math.Half3 to compare to this instance.. + /// True, if other is equal to this instance; false otherwise. + public bool Equals(Vector3h other) + { + return (this.X.Equals(other.X) && this.Y.Equals(other.Y) && this.Z.Equals(other.Z)); + } + + #endregion + + #region ToString() + + /// Returns a string that contains this Half3's numbers in human-legible form. + public override string ToString() + { + return String.Format("({0}, {1}, {2})", X.ToString(), Y.ToString(), Z.ToString()); + } + + #endregion ToString() + + #region BitConverter + + /// Returns the Half3 as an array of bytes. + /// The Half3 to convert. + /// The input as byte array. + public static byte[] GetBytes(Vector3h h) + { + byte[] result = new byte[SizeInBytes]; + + byte[] temp = Half.GetBytes(h.X); + result[0] = temp[0]; + result[1] = temp[1]; + temp = Half.GetBytes(h.Y); + result[2] = temp[0]; + result[3] = temp[1]; + temp = Half.GetBytes(h.Z); + result[4] = temp[0]; + result[5] = temp[1]; + + return result; + } + + /// Converts an array of bytes into Half3. + /// A Half3 in it's byte[] representation. + /// The starting position within value. + /// A new Half3 instance. + public static Vector3h FromBytes(byte[] value, int startIndex) + { + Vector3h h3 = new Vector3h(); + h3.X = Half.FromBytes(value, startIndex); + h3.Y = Half.FromBytes(value, startIndex + 2); + h3.Z = Half.FromBytes(value, startIndex + 4); + return h3; + } + + #endregion BitConverter + } +} diff --git a/Source/OpenTK/Math/Vector4.cs b/Source/OpenTK/Math/Vector4.cs index 2227c8ba..18d68d75 100644 --- a/Source/OpenTK/Math/Vector4.cs +++ b/Source/OpenTK/Math/Vector4.cs @@ -1,14 +1,28 @@ #region --- License --- -/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos - * See license.txt for license info - * - * Contributions by Andy Gill. +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ #endregion using System; -using System.Collections.Generic; -using System.Text; using System.Runtime.InteropServices; namespace OpenTK.Math @@ -129,7 +143,9 @@ namespace OpenTK.Math #endregion - #region Functions + #region Public Members + + #region Instance #region public float Length @@ -244,7 +260,385 @@ namespace OpenTK.Math #endregion - #region Operator overloads + #region Static + + #region Add + + /// + /// Add two Vectors + /// + /// First operand + /// Second operand + /// Result of addition + public static Vector4 Add(Vector4 a, Vector4 b) + { + a.X += b.X; + a.Y += b.Y; + a.Z += b.Z; + a.W += b.W; + return a; + } + + /// + /// Add two Vectors + /// + /// First operand + /// Second operand + /// Result of addition + public static void Add(ref Vector4 a, ref Vector4 b, out Vector4 result) + { + result.X = a.X + b.X; + result.Y = a.Y + b.Y; + result.Z = a.Z + b.Z; + result.W = a.W + b.W; + } + + #endregion + + #region Sub + + /// + /// Subtract one Vector from another + /// + /// First operand + /// Second operand + /// Result of subtraction + public static Vector4 Sub(Vector4 a, Vector4 b) + { + a.X -= b.X; + a.Y -= b.Y; + a.Z -= b.Z; + a.W -= b.W; + return a; + } + + /// + /// Subtract one Vector from another + /// + /// First operand + /// Second operand + /// Result of subtraction + public static void Sub(ref Vector4 a, ref Vector4 b, out Vector4 result) + { + result.X = a.X - b.X; + result.Y = a.Y - b.Y; + result.Z = a.Z - b.Z; + result.W = a.W - b.W; + } + + #endregion + + #region Mult + + /// + /// Multiply a vector and a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the multiplication + public static Vector4 Mult(Vector4 a, float f) + { + a.X *= f; + a.Y *= f; + a.Z *= f; + a.W *= f; + return a; + } + + /// + /// Multiply a vector and a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the multiplication + public static void Mult(ref Vector4 a, float f, out Vector4 result) + { + result.X = a.X * f; + result.Y = a.Y * f; + result.Z = a.Z * f; + result.W = a.W * f; + } + + #endregion + + #region Div + + /// + /// Divide a vector by a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the division + public static Vector4 Div(Vector4 a, float f) + { + float mult = 1.0f / f; + a.X *= mult; + a.Y *= mult; + a.Z *= mult; + a.W *= mult; + return a; + } + + /// + /// Divide a vector by a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the division + public static void Div(ref Vector4 a, float f, out Vector4 result) + { + float mult = 1.0f / f; + result.X = a.X * mult; + result.Y = a.Y * mult; + result.Z = a.Z * mult; + result.W = a.W * mult; + } + + #endregion + + #region Min + + /// + /// Calculate the component-wise minimum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise minimum + public static Vector4 Min(Vector4 a, Vector4 b) + { + a.X = a.X < b.X ? a.X : b.X; + a.Y = a.Y < b.Y ? a.Y : b.Y; + a.Z = a.Z < b.Z ? a.Z : b.Z; + a.W = a.W < b.W ? a.W : b.W; + return a; + } + + /// + /// Calculate the component-wise minimum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise minimum + public static void Min(ref Vector4 a, ref Vector4 b, out Vector4 result) + { + result.X = a.X < b.X ? a.X : b.X; + result.Y = a.Y < b.Y ? a.Y : b.Y; + result.Z = a.Z < b.Z ? a.Z : b.Z; + result.W = a.W < b.W ? a.W : b.W; + } + + #endregion + + #region Max + + /// + /// Calculate the component-wise maximum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise maximum + public static Vector4 Max(Vector4 a, Vector4 b) + { + a.X = a.X > b.X ? a.X : b.X; + a.Y = a.Y > b.Y ? a.Y : b.Y; + a.Z = a.Z > b.Z ? a.Z : b.Z; + a.W = a.W > b.W ? a.W : b.W; + return a; + } + + /// + /// Calculate the component-wise maximum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise maximum + public static void Max(ref Vector4 a, ref Vector4 b, out Vector4 result) + { + result.X = a.X > b.X ? a.X : b.X; + result.Y = a.Y > b.Y ? a.Y : b.Y; + result.Z = a.Z > b.Z ? a.Z : b.Z; + result.W = a.W > b.W ? a.W : b.W; + } + + #endregion + + #region Clamp + + /// + /// Clamp a vector to the given minimum and maximum vectors + /// + /// Input vector + /// Minimum vector + /// Maximum vector + /// The clamped vector + public static Vector4 Clamp(Vector4 vec, Vector4 min, Vector4 max) + { + vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; + vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; + vec.Z = vec.X < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; + vec.W = vec.Y < min.W ? min.W : vec.W > max.W ? max.W : vec.W; + return vec; + } + + /// + /// Clamp a vector to the given minimum and maximum vectors + /// + /// Input vector + /// Minimum vector + /// Maximum vector + /// The clamped vector + public static void Clamp(ref Vector4 vec, ref Vector4 min, ref Vector4 max, out Vector4 result) + { + result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; + result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; + result.Z = vec.X < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; + result.W = vec.Y < min.W ? min.W : vec.W > max.W ? max.W : vec.W; + } + + #endregion + + #region Normalize + + /// + /// Scale a vector to unit length + /// + /// The input vector + /// The normalized vector + public static Vector4 Normalize(Vector4 vec) + { + float scale = 1.0f / vec.Length; + vec.X *= scale; + vec.Y *= scale; + vec.Z *= scale; + vec.W *= scale; + return vec; + } + + /// + /// Scale a vector to unit length + /// + /// The input vector + /// The normalized vector + public static void Normalize(ref Vector4 vec, out Vector4 result) + { + float scale = 1.0f / vec.Length; + result.X = vec.X * scale; + result.Y = vec.Y * scale; + result.Z = vec.Z * scale; + result.W = vec.W * scale; + } + + #endregion + + #region NormalizeFast + + /// + /// Scale a vector to approximately unit length + /// + /// The input vector + /// The normalized vector + public static Vector4 NormalizeFast(Vector4 vec) + { + float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z + vec.W * vec.W); + vec.X *= scale; + vec.Y *= scale; + vec.Z *= scale; + vec.W *= scale; + return vec; + } + + /// + /// Scale a vector to approximately unit length + /// + /// The input vector + /// The normalized vector + public static void NormalizeFast(ref Vector4 vec, out Vector4 result) + { + float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z + vec.W * vec.W); + result.X = vec.X * scale; + result.Y = vec.Y * scale; + result.Z = vec.Z * scale; + result.W = vec.W * scale; + } + + #endregion + + #region Dot + + /// + /// Caclulate the dot product of two vectors + /// + /// First operand + /// Second operand + /// The dot product of the two inputs + public static float Dot(Vector4 left, Vector4 right) + { + return left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W; + } + + #endregion + + #region Lerp + + /// + /// Returns a new Vector that is the linear blend of the 2 given Vectors + /// + /// First input vector + /// Second input vector + /// The blend factor + /// a when blend=0, b when blend=1, and a linear combination otherwise + public static Vector4 Lerp(Vector4 a, Vector4 b, float blend) + { + a.X = blend * (b.X - a.X) + a.X; + a.Y = blend * (b.Y - a.Y) + a.Y; + a.Z = blend * (b.Z - a.Z) + a.Z; + a.W = blend * (b.W - a.W) + a.W; + return a; + } + + #endregion + + #region Barycentric + + /// + /// Interpolate 3 Vectors using Barycentric coordinates + /// + /// First input Vector + /// Second input Vector + /// Third input Vector + /// First Barycentric Coordinate + /// Second Barycentric Coordinate + /// a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise + public static Vector4 BaryCentric(Vector4 a, Vector4 b, Vector4 c, float u, float v) + { + return a + u * (b - a) + v * (c - a); + } + + #endregion + + #region Transform + + /// + /// Transform a Vector by the given Matrix + /// + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static Vector4 Transform(Vector4 vec, Matrix4 mat) + { + Vector4 result; + result.X = Vector4.Dot(vec, mat.Column0); + result.Y = Vector4.Dot(vec, mat.Column1); + result.Z = Vector4.Dot(vec, mat.Column2); + result.W = Vector4.Dot(vec, mat.Column3); + return result; + } + + #endregion + + #endregion + + #region Operators public static Vector4 operator +(Vector4 left, Vector4 right) { @@ -327,387 +721,11 @@ namespace OpenTK.Math #endregion - #region Static functions + #region Overrides - #region Add + #region public override string ToString() - /// - /// Add two Vectors - /// - /// First operand - /// Second operand - /// Result of addition - public static Vector4 Add(Vector4 a, Vector4 b) - { - a.X += b.X; - a.Y += b.Y; - a.Z += b.Z; - a.W += b.W; - return a; - } - - /// - /// Add two Vectors - /// - /// First operand - /// Second operand - /// Result of addition - public static void Add(ref Vector4 a, ref Vector4 b, out Vector4 result) - { - result.X = a.X + b.X; - result.Y = a.Y + b.Y; - result.Z = a.Z + b.Z; - result.W = a.W + b.W; - } - - #endregion - - #region Sub - - /// - /// Subtract one Vector from another - /// - /// First operand - /// Second operand - /// Result of subtraction - public static Vector4 Sub(Vector4 a, Vector4 b) - { - a.X -= b.X; - a.Y -= b.Y; - a.Z -= b.Z; - a.W -= b.W; - return a; - } - - /// - /// Subtract one Vector from another - /// - /// First operand - /// Second operand - /// Result of subtraction - public static void Sub(ref Vector4 a, ref Vector4 b, out Vector4 result) - { - result.X = a.X - b.X; - result.Y = a.Y - b.Y; - result.Z = a.Z - b.Z; - result.W = a.W - b.W; - } - - #endregion - - #region Mult - - /// - /// Multiply a vector and a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the multiplication - public static Vector4 Mult(Vector4 a, float f) - { - a.X *= f; - a.Y *= f; - a.Z *= f; - a.W *= f; - return a; - } - - /// - /// Multiply a vector and a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the multiplication - public static void Mult(ref Vector4 a, float f, out Vector4 result) - { - result.X = a.X * f; - result.Y = a.Y * f; - result.Z = a.Z * f; - result.W = a.W * f; - } - - #endregion - - #region Div - - /// - /// Divide a vector by a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the division - public static Vector4 Div(Vector4 a, float f) - { - float mult = 1.0f / f; - a.X *= mult; - a.Y *= mult; - a.Z *= mult; - a.W *= mult; - return a; - } - - /// - /// Divide a vector by a scalar - /// - /// Vector operand - /// Scalar operand - /// Result of the division - public static void Div(ref Vector4 a, float f, out Vector4 result) - { - float mult = 1.0f / f; - result.X = a.X * mult; - result.Y = a.Y * mult; - result.Z = a.Z * mult; - result.W = a.W * mult; - } - - #endregion - - #region Min - - /// - /// Calculate the component-wise minimum of two vectors - /// - /// First operand - /// Second operand - /// The component-wise minimum - public static Vector4 Min(Vector4 a, Vector4 b) - { - a.X = a.X < b.X ? a.X : b.X; - a.Y = a.Y < b.Y ? a.Y : b.Y; - a.Z = a.Z < b.Z ? a.Z : b.Z; - a.W = a.W < b.W ? a.W : b.W; - return a; - } - - /// - /// Calculate the component-wise minimum of two vectors - /// - /// First operand - /// Second operand - /// The component-wise minimum - public static void Min(ref Vector4 a, ref Vector4 b, out Vector4 result) - { - result.X = a.X < b.X ? a.X : b.X; - result.Y = a.Y < b.Y ? a.Y : b.Y; - result.Z = a.Z < b.Z ? a.Z : b.Z; - result.W = a.W < b.W ? a.W : b.W; - } - - #endregion - - #region Max - - /// - /// Calculate the component-wise maximum of two vectors - /// - /// First operand - /// Second operand - /// The component-wise maximum - public static Vector4 Max(Vector4 a, Vector4 b) - { - a.X = a.X > b.X ? a.X : b.X; - a.Y = a.Y > b.Y ? a.Y : b.Y; - a.Z = a.Z > b.Z ? a.Z : b.Z; - a.W = a.W > b.W ? a.W : b.W; - return a; - } - - /// - /// Calculate the component-wise maximum of two vectors - /// - /// First operand - /// Second operand - /// The component-wise maximum - public static void Max(ref Vector4 a, ref Vector4 b, out Vector4 result) - { - result.X = a.X > b.X ? a.X : b.X; - result.Y = a.Y > b.Y ? a.Y : b.Y; - result.Z = a.Z > b.Z ? a.Z : b.Z; - result.W = a.W > b.W ? a.W : b.W; - } - - #endregion - - #region Clamp - - /// - /// Clamp a vector to the given minimum and maximum vectors - /// - /// Input vector - /// Minimum vector - /// Maximum vector - /// The clamped vector - public static Vector4 Clamp(Vector4 vec, Vector4 min, Vector4 max) - { - vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; - vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; - vec.Z = vec.X < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; - vec.W = vec.Y < min.W ? min.W : vec.W > max.W ? max.W : vec.W; - return vec; - } - - /// - /// Clamp a vector to the given minimum and maximum vectors - /// - /// Input vector - /// Minimum vector - /// Maximum vector - /// The clamped vector - public static void Clamp(ref Vector4 vec, ref Vector4 min, ref Vector4 max, out Vector4 result) - { - result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; - result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; - result.Z = vec.X < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; - result.W = vec.Y < min.W ? min.W : vec.W > max.W ? max.W : vec.W; - } - - #endregion - - #region Normalize - - /// - /// Scale a vector to unit length - /// - /// The input vector - /// The normalized vector - public static Vector4 Normalize(Vector4 vec) - { - float scale = 1.0f / vec.Length; - vec.X *= scale; - vec.Y *= scale; - vec.Z *= scale; - vec.W *= scale; - return vec; - } - - /// - /// Scale a vector to unit length - /// - /// The input vector - /// The normalized vector - public static void Normalize(ref Vector4 vec, out Vector4 result) - { - float scale = 1.0f / vec.Length; - result.X = vec.X * scale; - result.Y = vec.Y * scale; - result.Z = vec.Z * scale; - result.W = vec.W * scale; - } - - #endregion - - #region NormalizeFast - - /// - /// Scale a vector to approximately unit length - /// - /// The input vector - /// The normalized vector - public static Vector4 NormalizeFast(Vector4 vec) - { - float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z + vec.W * vec.W); - vec.X *= scale; - vec.Y *= scale; - vec.Z *= scale; - vec.W *= scale; - return vec; - } - - /// - /// Scale a vector to approximately unit length - /// - /// The input vector - /// The normalized vector - public static void NormalizeFast(ref Vector4 vec, out Vector4 result) - { - float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z + vec.W * vec.W); - result.X = vec.X * scale; - result.Y = vec.Y * scale; - result.Z = vec.Z * scale; - result.W = vec.W * scale; - } - - #endregion - - #region Dot - - /// - /// Caclulate the dot product of two vectors - /// - /// First operand - /// Second operand - /// The dot product of the two inputs - public static float Dot(Vector4 left, Vector4 right) - { - return left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W; - } - - #endregion - - #region Lerp - - /// - /// Returns a new Vector that is the linear blend of the 2 given Vectors - /// - /// First input vector - /// Second input vector - /// The blend factor - /// a when blend=0, b when blend=1, and a linear combination otherwise - public static Vector4 Lerp(Vector4 a, Vector4 b, float blend) - { - a.X = blend * (b.X - a.X) + a.X; - a.Y = blend * (b.Y - a.Y) + a.Y; - a.Z = blend * (b.Z - a.Z) + a.Z; - a.W = blend * (b.W - a.W) + a.W; - return a; - } - - #endregion - - #region Barycentric - - /// - /// Interpolate 3 Vectors using Barycentric coordinates - /// - /// First input Vector - /// Second input Vector - /// Third input Vector - /// First Barycentric Coordinate - /// Second Barycentric Coordinate - /// a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise - public static Vector4 BaryCentric(Vector4 a, Vector4 b, Vector4 c, float u, float v) - { - return a + u * (b - a) + v * (c - a); - } - - #endregion - - #region Transform - - /// - /// Transform a Vector by the given Matrix - /// - /// The vector to transform - /// The desired transformation - /// The transformed vector - public static Vector4 Transform(Vector4 vec, Matrix4 mat) - { - Vector4 result; - result.X = Vector4.Dot(vec, mat.Column0); - result.Y = Vector4.Dot(vec, mat.Column1); - result.Z = Vector4.Dot(vec, mat.Column2); - result.W = Vector4.Dot(vec, mat.Column3); - return result; - } - - #endregion - - #endregion - - #region public override string ToString() - - /// + /// /// Returns a System.String that represents the current Vector4. /// /// @@ -748,6 +766,10 @@ namespace OpenTK.Math #endregion + #endregion + + #endregion + #region IEquatable Members /// Indicates whether the current vector is equal to another vector. diff --git a/Source/OpenTK/Math/Vector4d.cs b/Source/OpenTK/Math/Vector4d.cs index ba01a11a..8a09c2c3 100644 --- a/Source/OpenTK/Math/Vector4d.cs +++ b/Source/OpenTK/Math/Vector4d.cs @@ -1,310 +1,200 @@ #region --- License --- -/* Licensed under the MIT/X11 license. - * Copyright (c) 2006-2008 the OpenTK Team. - * This notice may not be removed from any source distribution. - * See license.txt for licensing detailed licensing details. - * - * Contributions by James Talton. +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. */ #endregion using System; -using System.Diagnostics; -using System.Collections.Generic; using System.Runtime.InteropServices; -using System.Text.RegularExpressions; namespace OpenTK.Math { - // Todo: Remove this warning when the code goes public. - #pragma warning disable 3019 - /// A 4-dimensional vector using double-precision floating point numbers. [Serializable] [StructLayout(LayoutKind.Sequential)] - internal struct Vector4d : IEquatable, IComparer, IComparable + public struct Vector4d : IEquatable { - #region Fields & Access + #region Fields - /// The X coordinate of the vector. + /// + /// The X component of the Vector4d. + /// public double X; - /// The Y coordinate of the vector. + /// + /// The Y component of the Vector4d. + /// public double Y; - /// The Z coordinate of the vector. + /// + /// The Z component of the Vector4d. + /// public double Z; - /// The W coordinate of the vector. + /// + /// The Z component of the Vector4d. + /// public double W; - /// The coordinate at the index of the vector. - public double this[int index] - { - get - { - switch( index ) - { - case 0: - return X; + /// + /// Defines a unit-length Vector4d that points towards the X-axis. + /// + public static Vector4d UnitX = new Vector4d(1, 0, 0, 0); - case 1: - return Y; + /// + /// Defines a unit-length Vector4d that points towards the Y-axis. + /// + public static Vector4d UnitY = new Vector4d(0, 1, 0, 0); - case 2: - return Z; + /// + /// Defines a unit-length Vector4d that points towards the Z-axis. + /// + public static Vector4d UnitZ = new Vector4d(0, 0, 1, 0); - case 3: - return W; - } + /// + /// Defines a unit-length Vector4d that points towards the W-axis. + /// + public static Vector4d UnitW = new Vector4d(0, 0, 0, 1); - throw new IndexOutOfRangeException(); - } - set - { - switch( index ) - { - case 0: - X = value; - return; + /// + /// Defines a zero-length Vector4d. + /// + public static Vector4d Zero = new Vector4d(0, 0, 0, 0); - case 1: - Y = value; - return; - - case 2: - Z = value; - return; - - case 3: - W = value; - return; - } - - throw new IndexOutOfRangeException(); - } - } - - /// Converts the vector into an array of double-precision floating point numbers. - /// The vector being converted. - /// An array of double-precision floating point numbers representing the vector coordinates. - public static explicit operator double[](Vector4d vector) - { - return new double[4]{vector.X, vector.Y, vector.Z, vector.W}; - } - - /// Converts the vector into left double-precision floating point number pointer. - /// The vector being converted. - /// A double-precision floating point number pointer to the vector coordinates. - //unsafe public static explicit operator double*(Vector4d vector) - //{ - // return &vector.X; - //} - - /// Converts the vector into an IntPtr. - /// The vector being converted. - /// An IntPtr to the vector coordinates. - //public static explicit operator IntPtr(Vector4d vector) - //{ - // unsafe - // { - // return (IntPtr)(&vector.X); - // } - //} + /// + /// Defines the size of the Vector4d struct in bytes. + /// + public static readonly int SizeInBytes = Marshal.SizeOf(new Vector4d()); #endregion - #region Constructors - /// Constructs left vector with the given coordinates. - /// The X coordinate. - /// The Y coordinate. - /// The Z coordinate. - /// The W coordinate. + /// + /// Constructs a new Vector4d. + /// + /// The x component of the Vector4d. + /// The y component of the Vector4d. + /// The z component of the Vector4d. + /// The z component of the Vector4d. public Vector4d(double x, double y, double z, double w) { - this.X = x; - this.Y = y; - this.Z = z; - this.W = w; + X = x; + Y = y; + Z = z; + W = w; } - /// Constructs left vector with the same coordinates as the given vector. - /// The vector whose coordinates to copy. - public Vector4d(ref Vector2d vector) + /// + /// Constructs a new Vector4d from the given Vector2. + /// + /// The Vector2 to copy components from. + public Vector4d(Vector2 v) { - this.X = vector.X; - this.Y = vector.Y; - this.Z = 0; - this.W = 0; + X = v.X; + Y = v.Y; + Z = 0.0f; + W = 0.0f; } - /// Constructs left vector with the same coordinates as the given vector. - /// The vector whose coordinates to copy. - public Vector4d(ref Vector3d vector) + /// + /// Constructs a new Vector4d from the given Vector3. + /// + /// The Vector3 to copy components from. + public Vector4d(Vector3 v) { - this.X = vector.X; - this.Y = vector.Y; - this.Z = vector.Z; - this.W = 0; + X = v.X; + Y = v.Y; + Z = v.Z; + W = 0.0f; } - /// Constructs left vector with the same coordinates as the given vector. - /// The vector whose coordinates to copy. - public Vector4d(ref Vector4d vector) + /// + /// Constructs a new Vector4d from the given Vector4d. + /// + /// The Vector4d to copy components from. + public Vector4d(Vector4d v) { - this.X = vector.X; - this.Y = vector.Y; - this.Z = vector.Z; - this.W = vector.W; - } - - /// Constructs left vector from the given array of double-precision floating point numbers. - /// The array of doubles for the coordinates of the vector. - public Vector4d(double[] coordinateArray) - { - if( coordinateArray == null || coordinateArray.GetLength(0) < 4 ) throw new Exception("Invalid parameter."); - - this.X = coordinateArray[0]; - this.Y = coordinateArray[1]; - this.Z = coordinateArray[2]; - this.W = coordinateArray[3]; + X = v.X; + Y = v.Y; + Z = v.Z; + W = v.W; } #endregion + #region Public Members - #region Equality + #region Instance - /// Indicates whether the current vector is equal to another vector. - /// An vector to compare with this vector. - /// true if the current vector is equal to the vector parameter; otherwise, false. - [CLSCompliant(false)] - public bool Equals(Vector4d vector) - { - return - X == vector.X && - Y == vector.Y && - Z == vector.Z && - W == vector.W; - } + #region public double Length - /// Indicates whether the current vector is equal to another vector. - /// An vector to compare with this vector. - /// true if the current vector is equal to the vector parameter; otherwise, false. - public bool Equals(ref Vector4d vector) - { - return - X == vector.X && - Y == vector.Y && - Z == vector.Z && - W == vector.W; - } - - /// Indicates whether two vectors are approximately equal to each other. - /// The first vector. - /// The second vector. - /// true if the vectors are approximately equal; otherwise, false. - public static bool Equals(ref Vector4d left, ref Vector4d right) - { - return - left.X == right.X && - left.Y == right.Y && - left.Z == right.Z && - left.W == right.W; - } - - /// Indicates whether the current vector is equal to another vector. - /// An vector to compare with this vector. - /// true if the current vector is equal to the vector parameter; otherwise, false. - public bool EqualsApprox(ref Vector4d vector, double tolerance) - { - return - System.Math.Abs(X - vector.X) <= tolerance && - System.Math.Abs(Y - vector.Y) <= tolerance && - System.Math.Abs(Z - vector.Z) <= tolerance && - System.Math.Abs(W - vector.W) <= tolerance; - } - - /// Indicates whether two vectors are approximately equal to each other within left given tolerance. - /// The first vector. - /// The second vector. - /// The tolerance for the approximation. - /// true if the vectors are approximately equal; otherwise, false. - public static bool EqualsApprox(ref Vector4d left, ref Vector4d right, double tolerance) - { - return - System.Math.Abs(left.X - right.X) <= tolerance && - System.Math.Abs(left.Y - right.Y) <= tolerance && - System.Math.Abs(left.Z - right.Z) <= tolerance && - System.Math.Abs(left.W - right.W) <= tolerance; - } - - #endregion - - - #region IComparer - - /// Compares two vectors and returns left value indicating whether one is less than, equal to, or greater than the other. - public int Compare(Vector4d left, Vector4d right) - { - if (left.X != right.X) - { - if (left.X < right.X) return -1; - else return 1; - } - else if (left.Y != right.Y) - { - if (left.Y < right.Y) return -1; - else return 1; - } - else if (left.Z != right.Z) - { - if (left.Z < right.Z) return -1; - else return 1; - } - else if (left.W != right.W) - { - if (left.W < right.W) return -1; - else return 1; - } - - return 0; - } - - #endregion - - - #region IComparable - - /// Compares the vector with another vector and returns left value indicating whether it is less than, equal to, or greater than the other vector. - public int CompareTo(Vector4d vector) { return Compare(this, vector); } - - #endregion - - - #region Length - - /// Gets the length of the vector. + /// + /// Gets the length (magnitude) of the vector. + /// + /// + /// public double Length { get { - double lengthSquared = LengthSquared; - - if (lengthSquared == 1) - { - return 1; - } - else - { - return System.Math.Sqrt(lengthSquared); - } + return (double)System.Math.Sqrt(X * X + Y * Y + Z * Z + W * W); } } - /// Gets the squared length of the vector. + #endregion + + #region public double LengthFast + + /// + /// Gets an approximation of the vector length (magnitude). + /// + /// + /// This property uses an approximation of the square root function to calculate vector magnitude, with + /// an upper error bound of 0.001. + /// + /// + /// + /// + public double LengthFast + { + get + { + return 1.0f / OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y + Z * Z + W * W); + } + } + + #endregion + + #region public double LengthSquared + + /// + /// Gets the square of the vector length (magnitude). + /// + /// + /// This property avoids the costly square root operation required by the Length property. This makes it more suitable + /// for comparisons. + /// + /// + /// public double LengthSquared { get @@ -313,1017 +203,543 @@ namespace OpenTK.Math } } - /// Gets the approimate length of the vector. - public double LengthApprox + #endregion + + #region public void Normalize() + + /// + /// Scales the Vector4d to unit length. + /// + public void Normalize() { - get - { - return 1.0d / Functions.InverseSqrtFast(X * X + Y * Y + Z * Z + W * W); - } + double scale = 1.0f / this.Length; + X *= scale; + Y *= scale; + Z *= scale; + W *= scale; } #endregion + #region public void NormalizeFast() - #region Distance - - /// Gets the distance from this vector to the given vector. - /// The vector to which to find the distance. - /// The distance from this vector to the given vector. - public double DistanceTo(ref Vector4d vector) + /// + /// Scales the Vector4d to approximately unit length. + /// + public void NormalizeFast() { - double deltaX = vector.X - X; - double deltaY = vector.Y - Y; - double deltaZ = vector.Z - Z; - double deltaW = vector.W - W; - return System.Math.Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ + deltaW * deltaW); - } - - /// Gets the squared distance from this vector to the given vector. - /// The vector to which to find the squared distance. - /// The squared distance from this vector to the given vector. - public double DistanceSquaredTo(ref Vector4d vector) - { - double deltaX = vector.X - X; - double deltaY = vector.Y - Y; - double deltaZ = vector.Z - Z; - double deltaW = vector.W - W; - return deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ + deltaW * deltaW; - } - - /// Gets the approximate distance from this vector to the given vector. - /// The vector to which to find the approximate distance. - /// The approximate distance from this vector to the given vector. - public double DistanceApproxTo(ref Vector4d vector) - { - double deltaX = vector.X - X; - double deltaY = vector.Y - Y; - double deltaZ = vector.Z - Z; - double deltaW = vector.W - W; - return 1.0d / Functions.InverseSqrtFast(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ + deltaW * deltaW); + double scale = Functions.InverseSqrtFast(X * X + Y * Y + Z * Z + W * W); + X *= scale; + Y *= scale; + Z *= scale; + W *= scale; } #endregion + #region public void Scale(double sx, double sy, double sz, double sw) + + /// + /// Scales the current Vector4d by the given amounts. + /// + /// The scale of the X component. + /// The scale of the Y component. + /// The scale of the Z component. + /// The scale of the Z component. + public void Scale(double sx, double sy, double sz, double sw) + { + this.X = X * sx; + this.Y = Y * sy; + this.Z = Z * sz; + this.W = W * sw; + } + + #endregion + + #endregion + + #region Static + + #region Add + + /// + /// Add two Vectors + /// + /// First operand + /// Second operand + /// Result of addition + public static Vector4d Add(Vector4d a, Vector4d b) + { + a.X += b.X; + a.Y += b.Y; + a.Z += b.Z; + a.W += b.W; + return a; + } + + /// + /// Add two Vectors + /// + /// First operand + /// Second operand + /// Result of addition + public static void Add(ref Vector4d a, ref Vector4d b, out Vector4d result) + { + result.X = a.X + b.X; + result.Y = a.Y + b.Y; + result.Z = a.Z + b.Z; + result.W = a.W + b.W; + } + + #endregion + + #region Sub + + /// + /// Subtract one Vector from another + /// + /// First operand + /// Second operand + /// Result of subtraction + public static Vector4d Sub(Vector4d a, Vector4d b) + { + a.X -= b.X; + a.Y -= b.Y; + a.Z -= b.Z; + a.W -= b.W; + return a; + } + + /// + /// Subtract one Vector from another + /// + /// First operand + /// Second operand + /// Result of subtraction + public static void Sub(ref Vector4d a, ref Vector4d b, out Vector4d result) + { + result.X = a.X - b.X; + result.Y = a.Y - b.Y; + result.Z = a.Z - b.Z; + result.W = a.W - b.W; + } + + #endregion + + #region Mult + + /// + /// Multiply a vector and a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the multiplication + public static Vector4d Mult(Vector4d a, double f) + { + a.X *= f; + a.Y *= f; + a.Z *= f; + a.W *= f; + return a; + } + + /// + /// Multiply a vector and a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the multiplication + public static void Mult(ref Vector4d a, double f, out Vector4d result) + { + result.X = a.X * f; + result.Y = a.Y * f; + result.Z = a.Z * f; + result.W = a.W * f; + } + + #endregion + + #region Div + + /// + /// Divide a vector by a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the division + public static Vector4d Div(Vector4d a, double f) + { + double mult = 1.0f / f; + a.X *= mult; + a.Y *= mult; + a.Z *= mult; + a.W *= mult; + return a; + } + + /// + /// Divide a vector by a scalar + /// + /// Vector operand + /// Scalar operand + /// Result of the division + public static void Div(ref Vector4d a, double f, out Vector4d result) + { + double mult = 1.0f / f; + result.X = a.X * mult; + result.Y = a.Y * mult; + result.Z = a.Z * mult; + result.W = a.W * mult; + } + + #endregion + + #region Min + + /// + /// Calculate the component-wise minimum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise minimum + public static Vector4d Min(Vector4d a, Vector4d b) + { + a.X = a.X < b.X ? a.X : b.X; + a.Y = a.Y < b.Y ? a.Y : b.Y; + a.Z = a.Z < b.Z ? a.Z : b.Z; + a.W = a.W < b.W ? a.W : b.W; + return a; + } + + /// + /// Calculate the component-wise minimum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise minimum + public static void Min(ref Vector4d a, ref Vector4d b, out Vector4d result) + { + result.X = a.X < b.X ? a.X : b.X; + result.Y = a.Y < b.Y ? a.Y : b.Y; + result.Z = a.Z < b.Z ? a.Z : b.Z; + result.W = a.W < b.W ? a.W : b.W; + } + + #endregion + + #region Max + + /// + /// Calculate the component-wise maximum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise maximum + public static Vector4d Max(Vector4d a, Vector4d b) + { + a.X = a.X > b.X ? a.X : b.X; + a.Y = a.Y > b.Y ? a.Y : b.Y; + a.Z = a.Z > b.Z ? a.Z : b.Z; + a.W = a.W > b.W ? a.W : b.W; + return a; + } + + /// + /// Calculate the component-wise maximum of two vectors + /// + /// First operand + /// Second operand + /// The component-wise maximum + public static void Max(ref Vector4d a, ref Vector4d b, out Vector4d result) + { + result.X = a.X > b.X ? a.X : b.X; + result.Y = a.Y > b.Y ? a.Y : b.Y; + result.Z = a.Z > b.Z ? a.Z : b.Z; + result.W = a.W > b.W ? a.W : b.W; + } + + #endregion + + #region Clamp + + /// + /// Clamp a vector to the given minimum and maximum vectors + /// + /// Input vector + /// Minimum vector + /// Maximum vector + /// The clamped vector + public static Vector4d Clamp(Vector4d vec, Vector4d min, Vector4d max) + { + vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; + vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; + vec.Z = vec.X < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; + vec.W = vec.Y < min.W ? min.W : vec.W > max.W ? max.W : vec.W; + return vec; + } + + /// + /// Clamp a vector to the given minimum and maximum vectors + /// + /// Input vector + /// Minimum vector + /// Maximum vector + /// The clamped vector + public static void Clamp(ref Vector4d vec, ref Vector4d min, ref Vector4d max, out Vector4d result) + { + result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; + result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; + result.Z = vec.X < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z; + result.W = vec.Y < min.W ? min.W : vec.W > max.W ? max.W : vec.W; + } + + #endregion #region Normalize - /// Normalize this vector. - public void Normalize() + /// + /// Scale a vector to unit length + /// + /// The input vector + /// The normalized vector + public static Vector4d Normalize(Vector4d vec) { - double lengthSquared = LengthSquared; - - if (lengthSquared != 1 && lengthSquared != 0) - { - double length = System.Math.Sqrt(lengthSquared); - X = X / length; - Y = Y / length; - Z = Z / length; - W = W / length; - } + double scale = 1.0f / vec.Length; + vec.X *= scale; + vec.Y *= scale; + vec.Z *= scale; + vec.W *= scale; + return vec; } - /// Get the normalized version of this vector. - /// The resulting normalized vector. - public void Normalize(out Vector4d result) + /// + /// Scale a vector to unit length + /// + /// The input vector + /// The normalized vector + public static void Normalize(ref Vector4d vec, out Vector4d result) { - double lengthSquared = LengthSquared; - - if (lengthSquared == 1) - { - result.X = X; - result.Y = Y; - result.Z = Z; - result.W = W; - } - else if (lengthSquared == 0) - { - result.X = 0; - result.Y = 0; - result.Z = 0; - result.W = 0; - } - else - { - double length = System.Math.Sqrt(lengthSquared); - result.X = X / length; - result.Y = Y / length; - result.Z = Z / length; - result.W = W / length; - } - } - - public static void Normalize(ref Vector4d vector, out Vector4d result) - { - double lengthSquared = vector.LengthSquared; - - if (lengthSquared == 1) - { - result.X = vector.X; - result.Y = vector.Y; - result.Z = vector.Z; - result.W = vector.W; - } - else if (lengthSquared == 0) - { - result.X = 0; - result.Y = 0; - result.Z = 0; - result.W = 0; - } - else - { - double length = System.Math.Sqrt(lengthSquared); - result.X = vector.X / length; - result.Y = vector.Y / length; - result.Z = vector.Z / length; - result.W = vector.W / length; - } - } - - public void NormalizeApprox() - { - double inverseSquare = Functions.InverseSqrtFast(X * X + Y * Y + Z * Z + W * W); - X = X * inverseSquare; - Y = Y * inverseSquare; - Z = Z * inverseSquare; - W = W * inverseSquare; - } - - /// Gets left approximately normalized vector of the vector. - public void NormalizedApprox(out Vector4d result) - { - double inverseSquare = Functions.InverseSqrtFast(X * X + Y * Y + Z * Z + W * W); - result.X = X * inverseSquare; - result.Y = Y * inverseSquare; - result.Z = Z * inverseSquare; - result.W = W * inverseSquare; - } - - public static void NormalizeApprox(ref Vector4d vector, out Vector4d result) - { - double inverseSquare = Functions.InverseSqrtFast(vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W); - result.X = vector.X * inverseSquare; - result.Y = vector.Y * inverseSquare; - result.Z = vector.Z * inverseSquare; - result.W = vector.W * inverseSquare; + double scale = 1.0f / vec.Length; + result.X = vec.X * scale; + result.Y = vec.Y * scale; + result.Z = vec.Z * scale; + result.W = vec.W * scale; } #endregion + #region NormalizeFast - /// Gets the dot product of two vectors. - /// The first vector. - /// The second vector. - /// The dot product of two vectors. - public static double DotProduct(ref Vector4d left, ref Vector4d right) + /// + /// Scale a vector to approximately unit length + /// + /// The input vector + /// The normalized vector + public static Vector4d NormalizeFast(Vector4d vec) + { + double scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z + vec.W * vec.W); + vec.X *= scale; + vec.Y *= scale; + vec.Z *= scale; + vec.W *= scale; + return vec; + } + + /// + /// Scale a vector to approximately unit length + /// + /// The input vector + /// The normalized vector + public static void NormalizeFast(ref Vector4d vec, out Vector4d result) + { + double scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z + vec.W * vec.W); + result.X = vec.X * scale; + result.Y = vec.Y * scale; + result.Z = vec.Z * scale; + result.W = vec.W * scale; + } + + #endregion + + #region Dot + + /// + /// Caclulate the dot product of two vectors + /// + /// First operand + /// Second operand + /// The dot product of the two inputs + public static double Dot(Vector4d left, Vector4d right) { return left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W; } + #endregion - #region Abs + #region Lerp - public void Abs() + /// + /// Returns a new Vector that is the linear blend of the 2 given Vectors + /// + /// First input vector + /// Second input vector + /// The blend factor + /// a when blend=0, b when blend=1, and a linear combination otherwise + public static Vector4d Lerp(Vector4d a, Vector4d b, double blend) { - X = System.Math.Abs(X); - Y = System.Math.Abs(Y); - Z = System.Math.Abs(Z); - W = System.Math.Abs(W); - } - public void Abs(out Vector4d result) - { - result.X = System.Math.Abs(X); - result.Y = System.Math.Abs(Y); - result.Z = System.Math.Abs(Z); - result.W = System.Math.Abs(W); - } - public static void Abs(ref Vector4d vector, out Vector4d result) - { - result.X = System.Math.Abs(vector.X); - result.Y = System.Math.Abs(vector.Y); - result.Z = System.Math.Abs(vector.Z); - result.W = System.Math.Abs(vector.W); + a.X = blend * (b.X - a.X) + a.X; + a.Y = blend * (b.Y - a.Y) + a.Y; + a.Z = blend * (b.Z - a.Z) + a.Z; + a.W = blend * (b.W - a.W) + a.W; + return a; } #endregion + #region Barycentric - #region Inverse - - public void Inverse() + /// + /// Interpolate 3 Vectors using Barycentric coordinates + /// + /// First input Vector + /// Second input Vector + /// Third input Vector + /// First Barycentric Coordinate + /// Second Barycentric Coordinate + /// a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise + public static Vector4d BaryCentric(Vector4d a, Vector4d b, Vector4d c, double u, double v) { - X = -X; - Y = -Y; - Z = -Z; - W = -W; - } - public void Inverse(out Vector4d result) - { - result.X = -X; - result.Y = -Y; - result.Z = -Z; - result.W = -W; - } - public static void Inverse(ref Vector4d vector, out Vector4d result) - { - result.X = -vector.X; - result.Y = -vector.Y; - result.Z = -vector.Z; - result.W = -vector.W; + return a + u * (b - a) + v * (c - a); } #endregion + #region Transform - #region Arithmatic - - public void Add(ref Vector4d vector) + /// + /// Transform a Vector by the given Matrix + /// + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static Vector4d Transform(Vector4d vec, Matrix4d mat) { - X = X + vector.X; - Y = Y + vector.Y; - Z = Z + vector.Z; - W = W + vector.W; - } - public void Add(ref Vector4d vector, out Vector4d result) - { - result.X = X + vector.X; - result.Y = Y + vector.Y; - result.Z = Z + vector.Z; - result.W = W + vector.W; - } - public static void Add(ref Vector4d left, ref Vector4d right, out Vector4d result) - { - result.X = left.X + right.X; - result.Y = left.Y + right.Y; - result.Z = left.Z + right.Z; - result.W = left.W + right.W; - } - - public void Add(double x, double y, double z, double w) - { - X = X + x; - Y = Y + y; - Z = Z + z; - W = W + w; - } - public void Add(double x, double y, double z, double w, out Vector4d result) - { - result.X = X + x; - result.Y = Y + y; - result.Z = Z + z; - result.W = W + w; - } - public static void Add(ref Vector4d vector, double x, double y, double z, double w, out Vector4d result) - { - result.X = vector.X + x; - result.Y = vector.Y + y; - result.Z = vector.Z + z; - result.W = vector.W + w; - } - - public void Subtract(ref Vector4d vector) - { - X = X - vector.X; - Y = Y - vector.Y; - Z = Z - vector.Z; - W = W - vector.W; - } - public void Subtract(ref Vector4d vector, out Vector4d result) - { - result.X = X - vector.X; - result.Y = Y - vector.Y; - result.Z = Z - vector.Z; - result.W = W - vector.W; - } - public static void Subtract(ref Vector4d left, ref Vector4d right, out Vector4d result) - { - result.X = left.X - right.X; - result.Y = left.Y - right.Y; - result.Z = left.Z - right.Z; - result.W = left.W - right.W; - } - - public void Subtract(double x, double y, double z, double w) - { - X = X - x; - Y = Y - y; - Z = Z - z; - W = W - w; - } - public void Subtract(double x, double y, double z, double w, out Vector4d result) - { - result.X = X - x; - result.Y = Y - y; - result.Z = Z - z; - result.W = W - w; - } - public static void Subtract(ref Vector4d vector, double x, double y, double z, double w, out Vector4d result) - { - result.X = vector.X - x; - result.Y = vector.Y - y; - result.Z = vector.Z - z; - result.W = vector.W - w; - } - - public void Multiply(double scalar) - { - X = X * scalar; - Y = Y * scalar; - Z = Z * scalar; - W = W * scalar; - } - public void Multiply(double scalar, out Vector4d result) - { - result.X = X * scalar; - result.Y = Y * scalar; - result.Z = Z * scalar; - result.W = W * scalar; - } - public static void Multiply(ref Vector4d vector, double scalar, out Vector4d result) - { - result.X = vector.X * scalar; - result.Y = vector.Y * scalar; - result.Z = vector.Z * scalar; - result.W = vector.W * scalar; - } - - public void Multiply(ref Vector4d vector) - { - X = X * vector.X; - Y = Y * vector.Y; - Z = Z * vector.Z; - W = W * vector.W; - } - public void Multiply(ref Vector4d vector, out Vector4d result) - { - result.X = X * vector.X; - result.Y = Y * vector.Y; - result.Z = Z * vector.Z; - result.W = W * vector.W; - } - public static void Multiply(ref Vector4d left, ref Vector4d right, out Vector4d result) - { - result.X = left.X * right.X; - result.Y = left.Y * right.Y; - result.Z = left.Z * right.Z; - result.W = left.W * right.W; - } - - public void Multiply(double x, double y, double z, double w) - { - X = X * x; - Y = Y * y; - Z = Z * z; - W = W * w; - } - public void Multiply(double x, double y, double z, double w, out Vector4d result) - { - result.X = X * x; - result.Y = Y * y; - result.Z = Z * z; - result.W = W * w; - } - public static void Multiply(ref Vector4d vector, double x, double y, double z, double w, out Vector4d result) - { - result.X = vector.X * x; - result.Y = vector.Y * y; - result.Z = vector.Z * z; - result.W = vector.W * w; - } - - public void Divide(double scalar) - { - X = X / scalar; - Y = Y / scalar; - Z = X / scalar; - W = W / scalar; - } - public void Divide(double scalar, out Vector4d result) - { - result.X = X / scalar; - result.Y = Y / scalar; - result.Z = X / scalar; - result.W = W / scalar; - } - public static void Divide(ref Vector4d vector, double scalar, out Vector4d result) - { - result.X = vector.X / scalar; - result.Y = vector.Y / scalar; - result.Z = vector.Z / scalar; - result.W = vector.W / scalar; - } - - public void Divide(ref Vector4d vector) - { - X = X / vector.X; - Y = Y / vector.Y; - Z = Z / vector.Z; - W = W / vector.W; - } - public void Divide(ref Vector4d vector, out Vector4d result) - { - result.X = X / vector.X; - result.Y = Y / vector.Y; - result.Z = Z / vector.Z; - result.W = W / vector.W; - } - public static void Divide(ref Vector4d left, ref Vector4d right, out Vector4d result) - { - result.X = left.X / right.X; - result.Y = left.Y / right.Y; - result.Z = left.Z / right.Z; - result.W = left.W / right.W; - } - - public void Divide(double x, double y, double z, double w) - { - X = X / x; - Y = Y / y; - Z = Z / z; - W = W / w; - } - public void Divide(double x, double y, double z, double w, out Vector4d result) - { - result.X = X / x; - result.Y = Y / y; - result.Z = Z / z; - result.W = W / w; - } - public static void Divide(ref Vector4d vector, double x, double y, double z, double w, out Vector4d result) - { - result.X = vector.X / x; - result.Y = vector.Y / y; - result.Z = vector.Z / z; - result.W = vector.W / w; + Vector4d result; + result.X = Vector4d.Dot(vec, mat.Column0); + result.Y = Vector4d.Dot(vec, mat.Column1); + result.Z = Vector4d.Dot(vec, mat.Column2); + result.W = Vector4d.Dot(vec, mat.Column3); + return result; } #endregion + #endregion - #region Transformations + #region Operators - public void Transform(ref Matrix4d matrix) + public static Vector4d operator +(Vector4d left, Vector4d right) { - double x = matrix.R0C0 * X + matrix.R0C1 * Y + matrix.R0C2 * Z + matrix.R0C3 * W; - double y = matrix.R1C0 * X + matrix.R1C1 * Y + matrix.R1C2 * Z + matrix.R1C3 * W; - double z = matrix.R2C0 * X + matrix.R2C1 * Y + matrix.R2C2 * Z + matrix.R2C3 * W; - W = matrix.R3C0 * X + matrix.R3C1 * Y + matrix.R3C2 * Z + matrix.R3C3 * W; - X = x; - Y = y; - Z = z; - } - public void Transform(ref Matrix4d matrix, out Vector4d result) - { - result.X = matrix.R0C0 * X + matrix.R0C1 * Y + matrix.R0C2 * Z + matrix.R0C3 * W; - result.Y = matrix.R1C0 * X + matrix.R1C1 * Y + matrix.R1C2 * Z + matrix.R1C3 * W; - result.Z = matrix.R2C0 * X + matrix.R2C1 * Y + matrix.R2C2 * Z + matrix.R2C3 * W; - result.W = matrix.R3C0 * X + matrix.R3C1 * Y + matrix.R3C2 * Z + matrix.R3C3 * W; - } - public static void Transform(ref Vector4d vector, ref Matrix4d matrix, out Vector4d result) - { - result.X = matrix.R0C0 * vector.X + matrix.R0C1 * vector.Y + matrix.R0C2 * vector.Z + matrix.R0C3 * vector.W; - result.Y = matrix.R1C0 * vector.X + matrix.R1C1 * vector.Y + matrix.R1C2 * vector.Z + matrix.R1C3 * vector.W; - result.Z = matrix.R2C0 * vector.X + matrix.R2C1 * vector.Y + matrix.R2C2 * vector.Z + matrix.R2C3 * vector.W; - result.W = matrix.R3C0 * vector.X + matrix.R3C1 * vector.Y + matrix.R3C2 * vector.Z + matrix.R3C3 * vector.W; + left.X += right.X; + left.Y += right.Y; + left.Z += right.Z; + left.W += right.W; + return left; } - public void Translate(ref Vector4d vector) + public static Vector4d operator -(Vector4d left, Vector4d right) { - X = X + vector.X; - Y = Y + vector.Y; - Z = Z + vector.Z; - W = W + vector.W; - } - public void Translate(ref Vector4d vector, out Vector4d result) - { - result.X = X + vector.X; - result.Y = Y + vector.Y; - result.Z = Z + vector.Z; - result.W = W + vector.W; - } - public static void Translate(ref Vector4d left, ref Vector4d right, out Vector4d result) - { - result.X = left.X + right.X; - result.Y = left.Y + right.Y; - result.Z = left.Z + right.Z; - result.W = left.W + right.W; + left.X -= right.X; + left.Y -= right.Y; + left.Z -= right.Z; + left.W -= right.W; + return left; } - public void Translate(double x, double y, double z, double w) + public static Vector4d operator -(Vector4d vec) + { + vec.X = -vec.X; + vec.Y = -vec.Y; + vec.Z = -vec.Z; + vec.W = -vec.W; + return vec; + } + + public static Vector4d operator *(Vector4d vec, double f) + { + vec.X *= f; + vec.Y *= f; + vec.Z *= f; + vec.W *= f; + return vec; + } + + public static Vector4d operator *(double f, Vector4d vec) + { + vec.X *= f; + vec.Y *= f; + vec.Z *= f; + vec.W *= f; + return vec; + } + + public static Vector4d operator /(Vector4d vec, double f) + { + double mult = 1.0f / f; + vec.X *= mult; + vec.Y *= mult; + vec.Z *= mult; + vec.W *= mult; + return vec; + } + + public static bool operator ==(Vector4d left, Vector4d right) { - X = X + x; - Y = Y + y; - Z = Z + z; - W = W + w; - } - public void Translate(double x, double y, double z, double w, out Vector4d result) - { - result.X = X + x; - result.Y = Y + y; - result.Z = Z + z; - result.W = W + w; - } - public static void Translate(ref Vector4d vector, double x, double y, double z, double w, out Vector4d result) - { - result.X = vector.X + x; - result.Y = vector.Y + y; - result.Z = vector.Z + z; - result.W = vector.W + w; + return left.Equals(right); } - public void Scale(ref Vector4d vector) + public static bool operator !=(Vector4d left, Vector4d right) { - X = X * vector.X; - Y = Y * vector.Y; - Z = Z * vector.Z; - W = W * vector.W; - } - public void Scale(ref Vector4d vector, out Vector4d result) - { - result.X = X * vector.X; - result.Y = Y * vector.Y; - result.Z = Z * vector.Z; - result.W = W * vector.W; - } - public static void Scale(ref Vector4d left, ref Vector4d right, out Vector4d result) - { - result.X = left.X * right.X; - result.Y = left.Y * right.Y; - result.Z = left.Z * right.Z; - result.W = left.W * right.W; + return !left.Equals(right); } - public void Scale(double x, double y, double z, double w) + [CLSCompliant(false)] + unsafe public static explicit operator double*(Vector4d v) { - X = X * x; - Y = Y * y; - Z = Z * z; - W = W * w; - } - public void Scale(double x, double y, double z, double w, out Vector4d result) - { - result.X = X * x; - result.Y = Y * y; - result.Z = Z * z; - result.W = W * w; - } - public static void Scale(ref Vector4d vector, double x, double y, double z, double w, out Vector4d result) - { - result.X = vector.X * x; - result.Y = vector.Y * y; - result.Z = vector.Z * z; - result.W = vector.W * w; + return &v.X; } - public void RotateX(double angle) + public static explicit operator IntPtr(Vector4d v) { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - double y = cos * Y + sin * Z; - Z = cos * Z - sin * Y; - Y = y; - } - public void RotateX(double angle, out Vector4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.X = X; - result.Y = cos * Y + sin * Z; - result.Z = cos * Z - sin * Y; - result.W = W; - } - public static void RotateX(ref Vector4d vector, double angle, out Vector4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.X = vector.X; - result.Y = cos * vector.Y + sin * vector.Z; - result.Z = cos * vector.Z - sin * vector.Y; - result.W = vector.W; - } - - public void RotateY(double angle) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - double x = cos * X - sin * Z; - Z = sin * X + cos * Z; - X = x; - } - public void RotateY(double angle, out Vector4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.X = cos * X - sin * Z; - result.Y = Y; - result.Z = sin * Y + cos * Z; - result.W = W; - } - public static void RotateY(ref Vector4d vector, double angle, out Vector4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.X = cos * vector.X - sin * vector.Z; - result.Y = vector.Y; - result.Z = sin * vector.Y + cos * vector.Z; - result.W = vector.W; - } - - public void RotateZ(double angle) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - double x = cos * X + sin * Y; - Y = cos * Y - sin * X; - X = x; - } - public void RotateZ(double angle, out Vector4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.X = cos * X + sin * Y; - result.Y = cos * Y - sin * X; - result.Z = Z; - result.W = W; - } - public static void RotateZ(ref Vector4d vector, double angle, out Vector4d result) - { - double angleRadians = Functions.DTOR * angle; - double sin = (double)System.Math.Sin(angleRadians); - double cos = (double)System.Math.Cos(angleRadians); - - result.X = cos * vector.X + sin * vector.Y; - result.Y = cos * vector.Y - sin * vector.X; - result.Z = vector.Z; - result.W = vector.W; - } - - public void Rotate(ref Vector3d axis, double angle) - { - Vector3d axisNormalized; - axis.Normalize(out axisNormalized); - double x = axisNormalized.X; - double y = axisNormalized.Y; - double z = axisNormalized.Z; - double angleRadians = Functions.DTOR * angle; - double cos = (double)System.Math.Cos(angleRadians); - double sin = (double)System.Math.Sin(angleRadians); - double oneMinusCos = 1 - cos; - double xOneMinusCos = x * oneMinusCos; - double yOneMinusCos = y * oneMinusCos; - double zOneMinusCos = z * oneMinusCos; - double xxOneMinusCos = x * xOneMinusCos; - double xyOneMinusCos = x * yOneMinusCos; - double xzOneMinusCos = x * zOneMinusCos; - double yyOneMinusCos = y * yOneMinusCos; - double yzOneMinusCos = y * zOneMinusCos; - double zzOneMinusCos = z * zOneMinusCos; - double xSin = x * sin; - double ySin = y * sin; - double zSin = z * sin; - - double tx = (xxOneMinusCos + cos) * X + (xyOneMinusCos + zSin) * Y + (xzOneMinusCos - ySin) * Z; - double ty = (xyOneMinusCos - zSin) * X + (yyOneMinusCos + cos) * Y + (yzOneMinusCos + xSin) * Z; - Z = (xzOneMinusCos + ySin) * X + (yzOneMinusCos - xSin) * Y + (zzOneMinusCos + cos) * Z; - X = tx; - Y = ty; - } - public void Rotate(ref Vector3d axis, double angle, out Vector4d result) - { - Vector3d axisNormalized; - axis.Normalize(out axisNormalized); - double x = axisNormalized.X; - double y = axisNormalized.Y; - double z = axisNormalized.Z; - double angleRadians = Functions.DTOR * angle; - double cos = (double)System.Math.Cos(angleRadians); - double sin = (double)System.Math.Sin(angleRadians); - double oneMinusCos = 1 - cos; - double xOneMinusCos = x * oneMinusCos; - double yOneMinusCos = y * oneMinusCos; - double zOneMinusCos = z * oneMinusCos; - double xxOneMinusCos = x * xOneMinusCos; - double xyOneMinusCos = x * yOneMinusCos; - double xzOneMinusCos = x * zOneMinusCos; - double yyOneMinusCos = y * yOneMinusCos; - double yzOneMinusCos = y * zOneMinusCos; - double zzOneMinusCos = z * zOneMinusCos; - double xSin = x * sin; - double ySin = y * sin; - double zSin = z * sin; - - result.X = (xxOneMinusCos + cos) * X + (xyOneMinusCos + zSin) * Y + (xzOneMinusCos - ySin) * Z; - result.Y = (xyOneMinusCos - zSin) * X + (yyOneMinusCos + cos) * Y + (yzOneMinusCos + xSin) * Z; - result.Z = (xzOneMinusCos + ySin) * X + (yzOneMinusCos - xSin) * Y + (zzOneMinusCos + cos) * Z; - result.W = W; - } - public static void Rotate(ref Vector4d vector, ref Vector3d axis, double angle, out Vector4d result) - { - Vector3d axisNormalized; - axis.Normalize(out axisNormalized); - double x = axisNormalized.X; - double y = axisNormalized.Y; - double z = axisNormalized.Z; - double angleRadians = Functions.DTOR * angle; - double cos = (double)System.Math.Cos(angleRadians); - double sin = (double)System.Math.Sin(angleRadians); - double oneMinusCos = 1 - cos; - double xOneMinusCos = x * oneMinusCos; - double yOneMinusCos = y * oneMinusCos; - double zOneMinusCos = z * oneMinusCos; - double xxOneMinusCos = x * xOneMinusCos; - double xyOneMinusCos = x * yOneMinusCos; - double xzOneMinusCos = x * zOneMinusCos; - double yyOneMinusCos = y * yOneMinusCos; - double yzOneMinusCos = y * zOneMinusCos; - double zzOneMinusCos = z * zOneMinusCos; - double xSin = x * sin; - double ySin = y * sin; - double zSin = z * sin; - - result.X = (xxOneMinusCos + cos ) * vector.X + (xyOneMinusCos + zSin) * vector.Y + (xzOneMinusCos - ySin) * vector.Z; - result.Y = (xyOneMinusCos - zSin) * vector.X + (yyOneMinusCos + cos) * vector.Y + (yzOneMinusCos + xSin) * vector.Z; - result.Z = (xzOneMinusCos + ySin) * vector.X + (yzOneMinusCos - xSin) * vector.Y + (zzOneMinusCos + cos ) * vector.Z; - result.W = vector.W; + unsafe + { + return (IntPtr)(&v.X); + } } #endregion + #region Overrides - #region Min & Max + #region public override string ToString() - public void Min(ref Vector4d vector) + /// + /// Returns a System.String that represents the current Vector4d. + /// + /// + public override string ToString() { - double lengthSquared = X * X + Y * Y + Z * Z + W * W; - double vectorLengthSquared = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W; + return String.Format("({0}, {1}, {2}, {3})", X, Y, Z, W); + } - if (vectorLengthSquared < lengthSquared) - { - X = vector.X; - Y = vector.Y; - Z = vector.Z; - W = vector.W; - } - } - public void Min(ref Vector4d vector, out Vector4d result) - { - double lengthSquared = X * X + Y * Y + Z * Z + W * W; - double vectorLengthSquared = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W; + #endregion - if (vectorLengthSquared < lengthSquared) - { - result.X = vector.X; - result.Y = vector.Y; - result.Z = vector.Z; - result.W = vector.W; - } - else - { - result.X = X; - result.Y = Y; - result.Z = Z; - result.W = W; - } - } - public static void Min(ref Vector4d left, ref Vector4d right, out Vector4d result) - { - double leftLengthSquared = left.X * left.X + left.Y * left.Y + left.Z * left.Z + left.W * left.W; - double rightLengthSquared = right.X * right.X + right.Y * right.Y + right.Z * right.Z + right.W * right.W; + #region public override int GetHashCode() - if (rightLengthSquared < leftLengthSquared) - { - result.X = right.X; - result.Y = right.Y; - result.Z = right.Z; - result.W = right.W; - } - else - { - result.X = left.X; - result.Y = left.Y; - result.Z = left.Z; - result.W = left.W; - } - } - - public void Max(ref Vector4d vector) - { - double lengthSquared = X * X + Y * Y + Z * Z + W * W; - double vectorLengthSquared = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W; - - if (vectorLengthSquared > lengthSquared) - { - X = vector.X; - Y = vector.Y; - Z = vector.Z; - W = vector.W; - } - } - public void Max(ref Vector4d vector, out Vector4d result) - { - double lengthSquared = X * X + Y * Y + Z * Z + W * W; - double vectorLengthSquared = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W; - - if (vectorLengthSquared > lengthSquared) - { - result.X = vector.X; - result.Y = vector.Y; - result.Z = vector.Z; - result.W = vector.W; - } - else - { - result.X = X; - result.Y = Y; - result.Z = Z; - result.W = W; - } - } - public static void Max(ref Vector4d left, ref Vector4d right, out Vector4d result) - { - double leftLengthSquared = left.X * left.X + left.Y * left.Y + left.Z * left.Z + left.W * left.W; - double rightLengthSquared = right.X * right.X + right.Y * right.Y + right.Z * right.Z + right.W * right.W; - - if (rightLengthSquared > leftLengthSquared) - { - result.X = right.X; - result.Y = right.Y; - result.Z = right.Z; - result.W = right.W; - } - else - { - result.X = left.X; - result.Y = left.Y; - result.Z = left.Z; - result.W = left.W; - } - } - - public void CoordinateMin(ref Vector4d vector) - { - X = System.Math.Min(X, vector.X); - Y = System.Math.Min(Y, vector.Y); - Z = System.Math.Min(Z, vector.Z); - W = System.Math.Min(W, vector.W); - } - public void CoordinateMin(ref Vector4d vector, out Vector4d result) - { - result.X = System.Math.Min(X, vector.X); - result.Y = System.Math.Min(Y, vector.Y); - result.Z = System.Math.Min(Z, vector.Z); - result.W = System.Math.Min(W, vector.W); - } - public static void CoordinateMin(ref Vector4d left, ref Vector4d right, out Vector4d result) - { - result.X = System.Math.Min(left.X, right.X); - result.Y = System.Math.Min(left.Y, right.Y); - result.Z = System.Math.Min(left.Z, right.Z); - result.W = System.Math.Min(left.W, right.W); - } - - public void CoordinateMax(ref Vector4d vector) - { - X = System.Math.Max(X, vector.X); - Y = System.Math.Max(Y, vector.Y); - Z = System.Math.Max(Z, vector.Z); - W = System.Math.Max(W, vector.W); - } - public void CoordinateMax(ref Vector4d vector, out Vector4d result) - { - result.X = System.Math.Max(X, vector.X); - result.Y = System.Math.Max(Y, vector.Y); - result.Z = System.Math.Max(Z, vector.Z); - result.W = System.Math.Max(W, vector.W); - } - public static void CoordinateMax(ref Vector4d left, ref Vector4d right, out Vector4d result) - { - result.X = System.Math.Max(left.X, right.X); - result.Y = System.Math.Max(left.Y, right.Y); - result.Z = System.Math.Max(left.Z, right.Z); - result.W = System.Math.Max(left.W, right.W); - } - - public void Clamp(ref Vector4d min, ref Vector4d max) - { - X = System.Math.Max(System.Math.Min(X, min.X), max.X); - Y = System.Math.Max(System.Math.Min(Y, min.Y), max.Y); - Z = System.Math.Max(System.Math.Min(Z, min.Z), max.Z); - W = System.Math.Max(System.Math.Min(W, min.W), max.W); - } - public void Clamp(ref Vector4d min, ref Vector4d max, out Vector4d result) - { - result.X = System.Math.Max(System.Math.Min(X, min.X), max.X); - result.Y = System.Math.Max(System.Math.Min(Y, min.Y), max.Y); - result.Z = System.Math.Max(System.Math.Min(Z, min.Z), max.Z); - result.W = System.Math.Max(System.Math.Min(W, min.W), max.W); - } - public static void Clamp(ref Vector4d vector, ref Vector4d min, ref Vector4d max, out Vector4d result) - { - result.X = System.Math.Max(System.Math.Min(vector.X, min.X), max.X); - result.Y = System.Math.Max(System.Math.Min(vector.Y, min.Y), max.Y); - result.Z = System.Math.Max(System.Math.Min(vector.Z, min.Z), max.Z); - result.W = System.Math.Max(System.Math.Min(vector.W, min.W), max.W); - } - - #endregion - - - #region Interpolation - - public void Lerp(ref Vector4d end, double blend) - { - X = X + (end.X - X) * blend; - Y = Y + (end.Y - Y) * blend; - Z = Z + (end.Z - Z) * blend; - W = W + (end.W - W) * blend; - } - public void Lerp(ref Vector4d end, double blend, out Vector4d result) - { - result.X = X + (end.X - X) * blend; - result.Y = Y + (end.Y - Y) * blend; - result.Z = Z + (end.Z - Z) * blend; - result.W = W + (end.W - W) * blend; - } - public static void Lerp(ref Vector4d start, ref Vector4d end, double blend, out Vector4d result) - { - result.X = start.X + (end.X - start.X) * blend; - result.Y = start.Y + (end.Y - start.Y) * blend; - result.Z = start.Z + (end.Z - start.Z) * blend; - result.W = start.W + (end.W - start.W) * blend; - } - - public void BaryCentric(ref Vector4d endU, ref Vector4d endV, double u, double v) - { - X = X + (endU.X - X) * u + (endV.X - X) * v; - Y = Y + (endU.Y - Y) * u + (endV.Y - Y) * v; - Z = Z + (endU.Z - Z) * u + (endV.Z - Z) * v; - W = W + (endU.W - W) * u + (endV.W - W) * v; - } - public void BaryCentric(ref Vector4d endU, ref Vector4d endV, double u, double v, out Vector4d result) - { - result.X = X + (endU.X - X) * u + (endV.X - X) * v; - result.Y = Y + (endU.Y - Y) * u + (endV.Y - Y) * v; - result.Z = Z + (endU.Z - Z) * u + (endV.Z - Z) * v; - result.W = W + (endU.W - W) * u + (endV.W - W) * v; - } - public static void BaryCentric(ref Vector4d start, ref Vector4d endU, ref Vector4d endV, double u, double v, out Vector4d result) - { - result.X = start.X + (endU.X - start.X) * u + (endV.X - start.X) * v; - result.Y = start.Y + (endU.Y - start.Y) * u + (endV.Y - start.Y) * v; - result.Z = start.Z + (endU.Z - start.Z) * u + (endV.Z - start.Z) * v; - result.W = start.W + (endU.W - start.W) * u + (endV.W - start.W) * v; - } - - #endregion - - - #region String and Parse - - /// Returns the fully qualified type name of this instance. - /// A System.String containing left fully qualified type name. - public override string ToString() - { - return String.Format("{0} {1} {2} {3}", X, Y, Z, W); - } - - /// Parse left string to convert it to left vector. - /// The string to parse. - /// The vector represented by the string. - public static void Parse(string str, out Vector4d result) - { - Match match = new Regex(@"(?.*) (?.*) (?.*) (?.*)", RegexOptions.None).Match(str); - if (!match.Success) throw new Exception("Parse failed!"); - result.X = double.Parse(match.Result("${x}")); - result.Y = double.Parse(match.Result("${y}")); - result.Z = double.Parse(match.Result("${z}")); - result.W = double.Parse(match.Result("${w}")); - } - - #endregion - - - #region HashCode - - /// Returns the hash code for this instance. - /// A 32-bit signed integer that is the hash code for this instance. + /// + /// Returns the hashcode for this instance. + /// + /// A System.Int32 containing the unique hashcode for this instance. public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode() ^ W.GetHashCode(); @@ -1331,29 +747,41 @@ namespace OpenTK.Math #endregion + #region public override bool Equals(object obj) - #region Constants + /// + /// Indicates whether this instance and a specified object are equal. + /// + /// The object to compare to. + /// True if the instances are equal; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Vector4d)) + return false; - /// A vector representing left zero vector. - public static readonly Vector4d Zero = new Vector4d(0, 0, 0, 0); + return this.Equals((Vector4d)obj); + } - /// A vector with all coordinates set to one. - public static readonly Vector4d One = new Vector4d(1, 1, 1, 1); + #endregion - /// A unit normal vector representing the positive X Axis. - public static readonly Vector4d XAxis = new Vector4d(1, 0, 0, 0); + #endregion - /// A unit normal vector representing the positive Y Axis. - public static readonly Vector4d YAxis = new Vector4d(0, 1, 0, 0); + #endregion - /// A unit normal vector representing the positive Z Axis. - public static readonly Vector4d ZAxis = new Vector4d(0, 0, 1, 0); + #region IEquatable Members - /// A unit normal vector representing the positive W Axis. - public static readonly Vector4d WAxis = new Vector4d(0, 0, 0, 1); + /// Indicates whether the current vector is equal to another vector. + /// A vector to compare with this vector. + /// true if the current vector is equal to the vector parameter; otherwise, false. + public bool Equals(Vector4d other) + { + return + X == other.X && + Y == other.Y && + Z == other.Z && + W == other.W; + } #endregion } - - #pragma warning restore 3019 } \ No newline at end of file diff --git a/Source/OpenTK/Math/Vector4h.cs b/Source/OpenTK/Math/Vector4h.cs new file mode 100644 index 00000000..7bd6198c --- /dev/null +++ b/Source/OpenTK/Math/Vector4h.cs @@ -0,0 +1,397 @@ +#region --- License --- +/* +Copyright (c) 2006 - 2008 The Open Toolkit library. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +#endregion + +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; + +namespace OpenTK.Math +{ + + /// 4-component Vector of the Half type. Occupies 8 Byte total. + [Serializable, StructLayout(LayoutKind.Sequential)] + public struct Vector4h : ISerializable, IEquatable + { + #region Public Fields + + /// The X component of the Half4. + public Half X; + + /// The Y component of the Half4. + public Half Y; + + /// The Z component of the Half4. + public Half Z; + + /// The W component of the Half4. + public Half W; + + #endregion Public Fields + + #region Constructors + + /// + /// The new Half4 instance will avoid conversion and copy directly from the Half parameters. + /// + /// An Half instance of a 16-Bit half precision floating point number. + /// An Half instance of a 16-Bit half precision floating point number. + /// An Half instance of a 16-Bit half precision floating point number. + /// An Half instance of a 16-Bit half precision floating point number. + public Vector4h(Half x, Half y, Half z, Half w) + { + this.X = x; + this.Y = y; + this.Z = z; + this.W = w; + } + + /// + /// The new Half4 instance will convert the 4 parameters into 16-Bit Half precision floating point. + /// + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + public Vector4h(Single x, Single y, Single z, Single w) + { + X = new Half(x); + Y = new Half(y); + Z = new Half(z); + W = new Half(w); + } + + /// + /// The new Half4 instance will convert the 4 parameters into 16-Bit Half precision floating point. + /// + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + /// 32-Bit Single precision floating point number. + /// Enable checks that will throw if the conversion result is not meaningful. + public Vector4h(Single x, Single y, Single z, Single w, bool throwOnError) + { + X = new Half(x, throwOnError); + Y = new Half(y, throwOnError); + Z = new Half(z, throwOnError); + W = new Half(w, throwOnError); + } + + /// + /// The new Half4 instance will convert the Vector4 into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector4 + [CLSCompliant(false)] + public Vector4h(Vector4 v) + { + X = new Half(v.X); + Y = new Half(v.Y); + Z = new Half(v.Z); + W = new Half(v.W); + } + + /// + /// The new Half4 instance will convert the Vector4 into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector4 + /// Enable checks that will throw if the conversion result is not meaningful. + [CLSCompliant(false)] + public Vector4h(Vector4 v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + Z = new Half(v.Z, throwOnError); + W = new Half(v.W, throwOnError); + } + + /// + /// The new Half4 instance will convert the Vector4 into 16-Bit Half precision floating point. + /// This is the fastest constructor. + /// + /// OpenTK.Math.Vector4 + public Vector4h(ref Vector4 v) + { + X = new Half(v.X); + Y = new Half(v.Y); + Z = new Half(v.Z); + W = new Half(v.W); + } + + /// + /// The new Half4 instance will convert the Vector4 into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector4 + /// Enable checks that will throw if the conversion result is not meaningful. + public Vector4h(ref Vector4 v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + Z = new Half(v.Z, throwOnError); + W = new Half(v.W, throwOnError); + } + + /// + /// The new Half4 instance will convert the Vector4d into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector4d + public Vector4h(Vector4d v) + { + X = new Half(v.X); + Y = new Half(v.Y); + Z = new Half(v.Z); + W = new Half(v.W); + } + + /// + /// The new Half4 instance will convert the Vector4d into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector4d + /// Enable checks that will throw if the conversion result is not meaningful. + public Vector4h(Vector4d v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + Z = new Half(v.Z, throwOnError); + W = new Half(v.W, throwOnError); + } + + /// + /// The new Half4 instance will convert the Vector4d into 16-Bit Half precision floating point. + /// This is the faster constructor. + /// + /// OpenTK.Math.Vector4d + [CLSCompliant(false)] + public Vector4h(ref Vector4d v) + { + X = new Half(v.X); + Y = new Half(v.Y); + Z = new Half(v.Z); + W = new Half(v.W); + } + + /// + /// The new Half4 instance will convert the Vector4d into 16-Bit Half precision floating point. + /// + /// OpenTK.Math.Vector4d + /// Enable checks that will throw if the conversion result is not meaningful. + [CLSCompliant(false)] + public Vector4h(ref Vector4d v, bool throwOnError) + { + X = new Half(v.X, throwOnError); + Y = new Half(v.Y, throwOnError); + Z = new Half(v.Z, throwOnError); + W = new Half(v.W, throwOnError); + } + + #endregion Constructors + + #region Half -> Single + + /// + /// Returns this Half4 instance's contents as Vector4. + /// + /// OpenTK.Math.Vector4 + public Vector4 ToVector4() + { + return new Vector4(X, Y, Z, W); + } + + /// + /// Returns this Half4 instance's contents as Vector4d. + /// + /// OpenTK.Math.Vector4d + public Vector4d ToVector4d() + { + return new Vector4d(X, Y, Z, W); + } + + #endregion Half -> Single + + #region Conversions + + /// Converts OpenTK.Math.Vector4 to OpenTK.Math.Half4. + /// The Vector4 to convert. + /// The resulting Half vector. + public static explicit operator Vector4h(Vector4 v4f) + { + return new Vector4h(v4f); + } + + /// Converts OpenTK.Math.Vector4d to OpenTK.Math.Half4. + /// The Vector4d to convert. + /// The resulting Half vector. + public static explicit operator Vector4h(Vector4d v4d) + { + return new Vector4h(v4d); + } + + /// Converts OpenTK.Math.Half4 to OpenTK.Math.Vector4. + /// The Half4 to convert. + /// The resulting Vector4. + public static explicit operator Vector4(Vector4h h4) + { + Vector4 result = new Vector4(); + result.X = h4.X.ToSingle(); + result.Y = h4.Y.ToSingle(); + result.Z = h4.Z.ToSingle(); + result.W = h4.W.ToSingle(); + return result; + } + + /// Converts OpenTK.Math.Half4 to OpenTK.Math.Vector4d. + /// The Half4 to convert. + /// The resulting Vector4d. + public static explicit operator Vector4d(Vector4h h4) + { + Vector4d result = new Vector4d(); + result.X = h4.X.ToSingle(); + result.Y = h4.Y.ToSingle(); + result.Z = h4.Z.ToSingle(); + result.W = h4.W.ToSingle(); + return result; + } + + #endregion Conversions + + #region Constants + + /// The size in bytes for an instance of the Half4 struct is 8. + public static readonly int SizeInBytes = 8; + + #endregion Constants + + #region ISerializable + + /// Constructor used by ISerializable to deserialize the object. + /// + /// + public Vector4h(SerializationInfo info, StreamingContext context) + { + this.X = (Half)info.GetValue("X", typeof(Half)); + this.Y = (Half)info.GetValue("Y", typeof(Half)); + this.Z = (Half)info.GetValue("Z", typeof(Half)); + this.W = (Half)info.GetValue("W", typeof(Half)); + } + + /// Used by ISerialize to serialize the object. + /// + /// + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue("X", this.X); + info.AddValue("Y", this.Y); + info.AddValue("Z", this.Z); + info.AddValue("W", this.W); + } + + #endregion ISerializable + + #region Binary dump + + /// Updates the X,Y,Z and W components of this instance by reading from a Stream. + /// A BinaryReader instance associated with an open Stream. + public void FromBinaryStream(BinaryReader bin) + { + X.FromBinaryStream(bin); + Y.FromBinaryStream(bin); + Z.FromBinaryStream(bin); + W.FromBinaryStream(bin); + } + + /// Writes the X,Y,Z and W components of this instance into a Stream. + /// A BinaryWriter instance associated with an open Stream. + public void ToBinaryStream(BinaryWriter bin) + { + X.ToBinaryStream(bin); + Y.ToBinaryStream(bin); + Z.ToBinaryStream(bin); + W.ToBinaryStream(bin); + } + + #endregion Binary dump + + #region IEquatable Members + + /// Returns a value indicating whether this instance is equal to a specified OpenTK.Math.Half4 vector. + /// OpenTK.Math.Half4 to compare to this instance.. + /// True, if other is equal to this instance; false otherwise. + public bool Equals(Vector4h other) + { + return (this.X.Equals(other.X) && this.Y.Equals(other.Y) && this.Z.Equals(other.Z) && this.W.Equals(other.W)); + } + + #endregion + + #region ToString() + + /// Returns a string that contains this Half4's numbers in human-legible form. + public override string ToString() + { + return String.Format("({0}, {1}, {2}, {3})", X.ToString(), Y.ToString(), Z.ToString(), W.ToString()); + } + + #endregion ToString() + + #region BitConverter + + /// Returns the Half4 as an array of bytes. + /// The Half4 to convert. + /// The input as byte array. + public static byte[] GetBytes(Vector4h h) + { + byte[] result = new byte[SizeInBytes]; + + byte[] temp = Half.GetBytes(h.X); + result[0] = temp[0]; + result[1] = temp[1]; + temp = Half.GetBytes(h.Y); + result[2] = temp[0]; + result[3] = temp[1]; + temp = Half.GetBytes(h.Z); + result[4] = temp[0]; + result[5] = temp[1]; + temp = Half.GetBytes(h.W); + result[6] = temp[0]; + result[7] = temp[1]; + + return result; + } + + /// Converts an array of bytes into Half4. + /// A Half4 in it's byte[] representation. + /// The starting position within value. + /// A new Half4 instance. + public static Vector4h FromBytes(byte[] value, int startIndex) + { + Vector4h h4 = new Vector4h(); + h4.X = Half.FromBytes(value, startIndex); + h4.Y = Half.FromBytes(value, startIndex + 2); + h4.Z = Half.FromBytes(value, startIndex + 4); + h4.W = Half.FromBytes(value, startIndex + 6); + return h4; + } + + #endregion BitConverter + } +}