Added OpenEXR terms to the License.

Added Half and Vector[234]h structs.
Added Vector[234]d, Matrix4d and Quaterniond structs.
This commit is contained in:
the_fiddler 2008-12-09 20:45:18 +00:00
Родитель 033d4722af
Коммит 969d66e9f1
16 изменённых файлов: 5124 добавлений и 5496 удалений

Просмотреть файл

@ -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.

588
Source/OpenTK/Math/Half.cs Normal file
Просмотреть файл

@ -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
{
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Half : ISerializable, IComparable<Half>, IFormattable, IEquatable<Half>
{
#region Internal Field
UInt16 bits;
#endregion Internal Field
#region Properties
/// <summary>Returns true if the Half is zero.</summary>
public bool IsZero { get { return (bits == 0) || (bits == 0x8000); } }
/// <summary>Returns true if the Half represents Not A Number (NaN)</summary>
public bool IsNaN { get { return (((bits & 0x7C00) == 0x7C00) && (bits & 0x03FF) != 0x0000); } }
/// <summary>Returns true if the Half represents positive infinity.</summary>
public bool IsPositiveInfinity { get { return (bits == 31744); } }
/// <summary>Returns true if the Half represents negative infinity.</summary>
public bool IsNegativeInfinity { get { return (bits == 64512); } }
#endregion Properties
#region Constructors
/// <summary>
/// The new Half instance will convert the parameter into 16-Bit Half precision floating point.
/// </summary>
/// <param name="f">32-Bit Single precision floating point number.</param>
public Half(Single f)
: this()
{
unsafe
{
bits = SingleToHalf(*(int*)&f);
}
}
/// <summary>
/// The new Half instance will convert the parameter into 16-Bit Half precision floating point.
/// </summary>
/// <param name="f">32-Bit Single precision floating point number.</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
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.");
}
}
/// <summary>
/// The new Half instance will convert the parameter into 16-Bit Half precision floating point.
/// </summary>
/// <param name="d">64-Bit Double precision floating point number.</param>
public Half(Double d) : this((Single)d) { }
/// <summary>
/// The new Half instance will convert the parameter into 16-Bit Half precision floating point.
/// </summary>
/// <param name="d">64-Bit Double precision floating point number.</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
public Half(Double d, bool throwOnError) : this((Single)d, throwOnError) { }
#endregion Constructors
#region Single -> Half
/// <summary>Ported from OpenEXR's IlmBase 1.0.1</summary>
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
/// <summary>Converts the 16-Bit half to 32-Bit floating point.</summary>
/// <returns>A Single precision floating point Number.</returns>
public Single ToSingle()
{
int i = HalfToFloat(bits);
unsafe
{
return *(float*)&i;
}
}
/// <summary>Ported from OpenEXR's IlmBase 1.0.1</summary>
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
/// <summary>
/// Converts a System.Single to a OpenTK.Math.Half.
/// </summary>
/// <param name="f">The value to convert.
/// A <see cref="System.Single"/>
/// </param>
/// <returns>The result of the conversion.
/// A <see cref="Half"/>
/// </returns>
public static explicit operator Half(float f)
{
return new Half(f);
}
/// <summary>
/// Converts a System.Double to a OpenTK.Math.Half.
/// </summary>
/// <param name="d">The value to convert.
/// A <see cref="System.Double"/>
/// </param>
/// <returns>The result of the conversion.
/// A <see cref="Half"/>
/// </returns>
public static explicit operator Half(double d)
{
return new Half(d);
}
/// <summary>
/// Converts a OpenTK.Math.Half to a System.Single.
/// </summary>
/// <param name="h">The value to convert.
/// A <see cref="Half"/>
/// </param>
/// <returns>The result of the conversion.
/// A <see cref="System.Single"/>
/// </returns>
public static implicit operator float(Half h)
{
return h.ToSingle();
}
/// <summary>
/// Converts a OpenTK.Math.Half to a System.Double.
/// </summary>
/// <param name="h">The value to convert.
/// A <see cref="Half"/>
/// </param>
/// <returns>The result of the conversion.
/// A <see cref="System.Double"/>
/// </returns>
public static implicit operator double(Half h)
{
return (double)h.ToSingle();
}
#endregion Conversions
#region Constants
/// <summary>The size in bytes for an instance of the Half struct.</summary>
public static readonly Int32 SizeInBytes = 2;
/// <summary>Smallest positive half</summary>
public static readonly Single MinValue = 5.96046448e-08f;
/// <summary>Smallest positive normalized half</summary>
public static readonly Single MinNormalizedValue = 6.10351562e-05f;
/// <summary>Largest positive half</summary>
public static readonly Single MaxValue = 65504.0f;
/// <summary>Smallest positive e for which half (1.0 + e) != half (1.0)</summary>
public static readonly Single Epsilon = 0.00097656f;
#endregion Constants
#region ISerializable
/// <summary>Constructor used by ISerializable to deserialize the object.</summary>
/// <param name="info"></param>
/// <param name="context"></param>
public Half(SerializationInfo info, StreamingContext context)
{
this.bits = (ushort)info.GetValue("bits", typeof(ushort));
}
/// <summary>Used by ISerialize to serialize the object.</summary>
/// <param name="info"></param>
/// <param name="context"></param>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("bits", this.bits);
}
#endregion ISerializable
#region Binary dump
/// <summary>Updates the Half by reading from a Stream.</summary>
/// <param name="bin">A BinaryReader instance associated with an open Stream.</param>
public void FromBinaryStream(BinaryReader bin)
{
this.bits = bin.ReadUInt16();
}
/// <summary>Writes the Half into a Stream.</summary>
/// <param name="bin">A BinaryWriter instance associated with an open Stream.</param>
public void ToBinaryStream(BinaryWriter bin)
{
bin.Write(this.bits);
}
#endregion Binary dump
#region IEquatable<Half> Members
const int maxUlps = 1;
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified OpenTK.Math.Half value.
/// </summary>
/// <param name="other">OpenTK.Math.Half object to compare to this instance..</param>
/// <returns>True, if other is equal to this instance; false otherwise.</returns>
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<Half> Members
/// <summary>
/// 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.
/// </summary>
/// <param name="other">A half-precision floating-point number to compare.</param>
/// <returns>
/// A signed number indicating the relative values of this instance and value. If the number is:
/// <para>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.</para>
/// <para>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.</para>
/// <para>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).</para>
/// </returns>
public int CompareTo(Half other)
{
return ((float)this).CompareTo((float)other);
}
#endregion IComparable<Half> Members
#region IFormattable Members
/// <summary>Converts this Half into a human-legible string representation.</summary>
/// <returns>The string representation of this instance.</returns>
public override string ToString()
{
return this.ToSingle().ToString();
}
/// <summary>Converts this Half into a human-legible string representation.</summary>
/// <param name="format">formatting for the output string.</param>
/// <param name="formatProvider">Culture-specific formatting information.</param>
/// <returns>The string representation of this instance.</returns>
public string ToString(string format, IFormatProvider formatProvider)
{
return this.ToSingle().ToString(format, formatProvider);
}
#endregion IFormattable Members
#region String -> Half
/// <summary>Converts the string representation of a number to a Half precision floating point equivalent.</summary>
/// <param name="s">string representation of the number to convert.</param>
/// <returns>A new Half instance.</returns>
public static Half Parse(string s)
{
return (Half)Single.Parse(s);
}
/// <summary>Converts the string representation of a number to a Half precision floating point equivalent.</summary>
/// <param name="s">string representation of the number to convert.</param>
/// <param name="style">specifies the format of s.</param>
/// <param name="provider">Culture-specific formatting information.</param>
/// <returns>A new Half instance.</returns>
public static Half Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider)
{
return (Half)Single.Parse(s, style, provider);
}
/// <summary>Converts the string representation of a number to a Half precision floating point equivalent. Returns success.</summary>
/// <param name="s">string representation of the number to convert.</param>
/// <param name="result">The Half instance to write to.</param>
/// <returns>Success.</returns>
public static bool TryParse(string s, out Half result)
{
float f;
bool b = Single.TryParse(s, out f);
result = (Half)f;
return b;
}
/// <summary>Converts the string representation of a number to a Half precision floating point equivalent. Returns success.</summary>
/// <param name="s">string representation of the number to convert.</param>
/// <param name="style">specifies the format of s.</param>
/// <param name="provider">Culture-specific formatting information.</param>
/// <param name="result">The Half instance to write to.</param>
/// <returns>Success.</returns>
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
/// <summary>Returns the Half as an array of bytes.</summary>
/// <param name="h">The Half to convert.</param>
/// <returns>The input as byte array.</returns>
public static byte[] GetBytes(Half h)
{
return BitConverter.GetBytes(h.bits);
}
/// <summary>Converts an array of bytes into Half.</summary>
/// <param name="value">A Half in it's byte[] representation.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A new Half instance.</returns>
public static Half FromBytes(byte[] value, int startIndex)
{
Half h;
h.bits = BitConverter.ToUInt16(value, startIndex);
return h;
}
#endregion BitConverter
}
}

Просмотреть файл

@ -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<Matrix3d>
public struct Matrix3d : IEquatable<Matrix3d>
{
#region Fields & Access
@ -275,20 +289,19 @@ namespace OpenTK.Math
/// <summary>Constructs left matrix from the given quaternion.</summary>
/// <param name="quaternion">The quaternion to use to construct the martix.</param>
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
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -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
/// </summary>
public Vector3 XYZ;
/// <summary>
/// The w component of the quaternion
/// </summary>
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
/// <summary>
/// <summary>
/// Gets or sets the X component of this instance.
/// </summary>
public float X
{
get { return XYZ.X; }
set { XYZ.X = value; }
}
/// <summary>
/// Gets or sets the Y component of this instance.
/// </summary>
public float Y
{
get { return XYZ.Y; }
set { XYZ.Y = value; }
}
/// <summary>
/// Gets or sets the Z component of this instance.
/// </summary>
public float Z
{
get { return XYZ.Z; }
set { XYZ.Z = value; }
}
#endregion
#region Instance
#region pubilc void ToAxisAngle(out Vector3 axis, out float angle)
/// <summary>
/// Convert the current quaternion to axis angle representation
/// </summary>
/// <param name="axis">The resultant axis</param>
@ -148,7 +194,272 @@ namespace OpenTK.Math
#endregion
#region Operator overloads
#region Static
#region Fields
/// <summary>
/// Defines the identity quaternion.
/// </summary>
public static Quaternion Identity = new Quaternion(0, 0, 0, 1);
#endregion
#region Add
/// <summary>
/// Add two quaternions
/// </summary>
/// <param name="left">The first operand</param>
/// <param name="right">The second operand</param>
/// <returns>The result of the addition</returns>
public static Quaternion Add(Quaternion left, Quaternion right)
{
left.XYZ += right.XYZ;
left.W += right.W;
return left;
}
/// <summary>
/// Add two quaternions
/// </summary>
/// <param name="left">The first operand</param>
/// <param name="right">The second operand</param>
/// <param name="result">The result of the addition</param>
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
/// <summary>
/// Get the conjugate of the given quaternion
/// </summary>
/// <param name="q">The quaternion</param>
/// <returns>The conjugate of the given quaternion</returns>
public static Quaternion Conjugate(Quaternion q)
{
q.XYZ = -q.XYZ;
return q;
}
/// <summary>
/// Get the conjugate of the given quaternion
/// </summary>
/// <param name="q">The quaternion</param>
/// <param name="result">The conjugate of the given quaternion</param>
public static void Conjugate(ref Quaternion q, out Quaternion result)
{
result.XYZ = -q.XYZ;
result.W = q.W;
}
#endregion
#region Invert
/// <summary>
/// Get the inverse of the given quaternion
/// </summary>
/// <param name="q">The quaternion to invert</param>
/// <returns>The inverse of the given quaternion</returns>
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;
}
/// <summary>
/// Get the inverse of the given quaternion
/// </summary>
/// <param name="q">The quaternion to invert</param>
/// <param name="result">The inverse of the given quaternion</param>
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
/// <summary>
/// Scale the given quaternion to unit length
/// </summary>
/// <param name="q">The quaternion to normalize</param>
/// <returns>The normalized quaternion</returns>
public static Quaternion Normalize(Quaternion q)
{
float scale = 1.0f / q.Length;
q.XYZ *= scale;
q.W *= scale;
return q;
}
/// <summary>
/// Scale the given quaternion to unit length
/// </summary>
/// <param name="q">The quaternion to normalize</param>
/// <param name="result">The normalized quaternion</param>
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
/// <summary>
/// Build a quaternion from the given axis and angle
/// </summary>
/// <param name="axis">The axis to rotate about</param>
/// <param name="angle">The rotation angle in radians</param>
/// <returns></returns>
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
/// <summary>
/// Do Spherical linear interpolation between two quaternions
/// </summary>
/// <param name="q1">The first quaternion</param>
/// <param name="q2">The second quaternion</param>
/// <param name="blend">The blend factor</param>
/// <returns>A smooth blend between the given quaternions</returns>
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()
/// <summary>
/// Add two quaternions
/// </summary>
/// <param name="left">The first operand</param>
/// <param name="right">The second operand</param>
/// <returns>The result of the addition</returns>
public static Quaternion Add(Quaternion left, Quaternion right)
{
left.XYZ += right.XYZ;
left.W += right.W;
return left;
}
/// <summary>
/// Add two quaternions
/// </summary>
/// <param name="left">The first operand</param>
/// <param name="right">The second operand</param>
/// <param name="result">The result of the addition</param>
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
/// <summary>
/// Get the conjugate of the given quaternion
/// </summary>
/// <param name="q">The quaternion</param>
/// <returns>The conjugate of the given quaternion</returns>
public static Quaternion Conjugate(Quaternion q)
{
q.XYZ = -q.XYZ;
return q;
}
/// <summary>
/// Get the conjugate of the given quaternion
/// </summary>
/// <param name="q">The quaternion</param>
/// <param name="result">The conjugate of the given quaternion</param>
public static void Conjugate(ref Quaternion q, out Quaternion result)
{
result.XYZ = -q.XYZ;
result.W = q.W;
}
#endregion
#region Invert
/// <summary>
/// Get the inverse of the given quaternion
/// </summary>
/// <param name="q">The quaternion to invert</param>
/// <returns>The inverse of the given quaternion</returns>
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;
}
/// <summary>
/// Get the inverse of the given quaternion
/// </summary>
/// <param name="q">The quaternion to invert</param>
/// <param name="result">The inverse of the given quaternion</param>
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
/// <summary>
/// Scale the given quaternion to unit length
/// </summary>
/// <param name="q">The quaternion to normalize</param>
/// <returns>The normalized quaternion</returns>
public static Quaternion Normalize(Quaternion q)
{
float scale = 1.0f / q.Length;
q.XYZ *= scale;
q.W *= scale;
return q;
}
/// <summary>
/// Scale the given quaternion to unit length
/// </summary>
/// <param name="q">The quaternion to normalize</param>
/// <param name="result">The normalized quaternion</param>
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
/// <summary>
/// Build a quaternion from the given axis and angle
/// </summary>
/// <param name="axis">The axis to rotate about</param>
/// <param name="angle">The rotation angle in radians</param>
/// <returns></returns>
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
/// <summary>
/// Do Spherical linear interpolation between two quaternions
/// </summary>
/// <param name="q1">The first quaternion</param>
/// <param name="q2">The second quaternion</param>
/// <param name="blend">The blend factor</param>
/// <returns>A smooth blend between the given quaternions</returns>
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()
/// <summary>
/// <summary>
/// Returns a System.String that represents the current Quaternion.
/// </summary>
/// <returns></returns>
@ -456,5 +513,9 @@ namespace OpenTK.Math
}
#endregion
#endregion
#endregion
}
}

Двоичные данные
Source/OpenTK/Math/Quaterniond.cs

Двоичный файл не отображается.

Просмотреть файл

@ -1,14 +1,28 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*
* Contributions by Andy Gill, Georg ¤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
/// </summary>
public float Y;
/// <summary>
/// Defines a unit-length Vector2 that points towards the X-axis.
/// </summary>
public static Vector2 UnitX = new Vector2(1, 0);
/// <summary>
/// Defines a unit-length Vector2 that points towards the Y-axis.
/// </summary>
public static Vector2 UnitY = new Vector2(0, 1);
/// <summary>
/// Defines a zero-length Vector2.
/// </summary>
public static Vector2 Zero = new Vector2(0, 0);
/// <summary>
/// Defines the size of the Vector2 struct in bytes.
/// </summary>
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.
/// </summary>
/// <param name="v">The Vector2 to copy components from.</param>
[Obsolete]
public Vector2(Vector2 v)
{
X = v.X;
Y = v.Y;
X = v.X;
Y = v.Y;
}
/// <summary>
/// Constructs a new Vector2 from the given Vector3.
/// </summary>
/// <param name="v">The Vector3 to copy components from. Z is discarded.</param>
[Obsolete]
public Vector2(Vector3 v)
{
X = v.X;
Y = v.Y;
X = v.X;
Y = v.Y;
}
/// <summary>
/// Constructs a new Vector2 from the given Vector4.
/// </summary>
/// <param name="v">The Vector4 to copy components from. Z and W are discarded.</param>
[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
/// <summary>
/// Defines a unit-length Vector2 that points towards the X-axis.
/// </summary>
public static readonly Vector2 UnitX = new Vector2(1, 0);
/// <summary>
/// Defines a unit-length Vector2 that points towards the Y-axis.
/// </summary>
public static readonly Vector2 UnitY = new Vector2(0, 1);
/// <summary>
/// Defines a zero-length Vector2.
/// </summary>
public static readonly Vector2 Zero = new Vector2(0, 0);
/// <summary>
/// Defines the size of the Vector2 struct in bytes.
/// </summary>
public static readonly int SizeInBytes = Marshal.SizeOf(new Vector2());
#endregion
#region Add
/// <summary>
/// Add two Vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>Result of addition</returns>
public static Vector2 Add(Vector2 a, Vector2 b)
{
a.X += b.X;
a.Y += b.Y;
return a;
}
/// <summary>
/// Add two Vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">Result of addition</param>
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
/// <summary>
/// Subtract one Vector from another
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>Result of subtraction</returns>
public static Vector2 Sub(Vector2 a, Vector2 b)
{
a.X -= b.X;
a.Y -= b.Y;
return a;
}
/// <summary>
/// Subtract one Vector from another
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">Result of subtraction</param>
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
/// <summary>
/// Multiply a vector and a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <returns>Result of the multiplication</returns>
public static Vector2 Mult(Vector2 a, float f)
{
a.X *= f;
a.Y *= f;
return a;
}
/// <summary>
/// Multiply a vector and a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <param name="result">Result of the multiplication</param>
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
/// <summary>
/// Divide a vector by a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <returns>Result of the division</returns>
public static Vector2 Div(Vector2 a, float f)
{
float mult = 1.0f / f;
a.X *= mult;
a.Y *= mult;
return a;
}
/// <summary>
/// Divide a vector by a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <param name="result">Result of the division</param>
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
/// <summary>
/// Calculate the component-wise minimum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>The component-wise minimum</returns>
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;
}
/// <summary>
/// Calculate the component-wise minimum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">The component-wise minimum</param>
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
/// <summary>
/// Calculate the component-wise maximum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>The component-wise maximum</returns>
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;
}
/// <summary>
/// Calculate the component-wise maximum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">The component-wise maximum</param>
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
/// <summary>
/// Returns the Vector3 with the minimum magnitude
/// </summary>
/// <param name="left">Left operand</param>
/// <param name="right">Right operand</param>
/// <returns>The minimum Vector3</returns>
public static Vector2 Min(Vector2 left, Vector2 right)
{
return left.LengthSquared < right.LengthSquared ? left : right;
}
#endregion
#region Max
/// <summary>
/// Returns the Vector3 with the minimum magnitude
/// </summary>
/// <param name="left">Left operand</param>
/// <param name="right">Right operand</param>
/// <returns>The minimum Vector3</returns>
public static Vector2 Max(Vector2 left, Vector2 right)
{
return left.LengthSquared >= right.LengthSquared ? left : right;
}
#endregion
#region Clamp
/// <summary>
/// Clamp a vector to the given minimum and maximum vectors
/// </summary>
/// <param name="vec">Input vector</param>
/// <param name="min">Minimum vector</param>
/// <param name="max">Maximum vector</param>
/// <returns>The clamped vector</returns>
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;
}
/// <summary>
/// Clamp a vector to the given minimum and maximum vectors
/// </summary>
/// <param name="vec">Input vector</param>
/// <param name="min">Minimum vector</param>
/// <param name="max">Maximum vector</param>
/// <param name="result">The clamped vector</param>
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
/// <summary>
/// Scale a vector to unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <returns>The normalized vector</returns>
public static Vector2 Normalize(Vector2 vec)
{
float scale = 1.0f / vec.Length;
vec.X *= scale;
vec.Y *= scale;
return vec;
}
/// <summary>
/// Scale a vector to unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <param name="result">The normalized vector</param>
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
/// <summary>
/// Scale a vector to approximately unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <returns>The normalized vector</returns>
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;
}
/// <summary>
/// Scale a vector to approximately unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <param name="result">The normalized vector</param>
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
/// <summary>
/// Caclulate the dot (scalar) product of two vectors
/// </summary>
/// <param name="left">First operand</param>
/// <param name="right">Second operand</param>
/// <returns>The dot product of the two inputs</returns>
public static float Dot(Vector2 left, Vector2 right)
{
return left.X * right.X + left.Y * right.Y;
}
#endregion
#region Lerp
/// <summary>
/// Returns a new Vector that is the linear blend of the 2 given Vectors
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor</param>
/// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns>
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
/// <summary>
/// Interpolate 3 Vectors using Barycentric coordinates
/// </summary>
/// <param name="a">First input Vector</param>
/// <param name="b">Second input Vector</param>
/// <param name="c">Third input Vector</param>
/// <param name="u">First Barycentric Coordinate</param>
/// <param name="v">Second Barycentric Coordinate</param>
/// <returns>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</returns>
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()
/// <summary>
/// Add two Vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>Result of addition</returns>
public static Vector2 Add(Vector2 a, Vector2 b)
{
a.X += b.X;
a.Y += b.Y;
return a;
}
/// <summary>
/// Add two Vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">Result of addition</param>
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
/// <summary>
/// Subtract one Vector from another
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>Result of subtraction</returns>
public static Vector2 Sub(Vector2 a, Vector2 b)
{
a.X -= b.X;
a.Y -= b.Y;
return a;
}
/// <summary>
/// Subtract one Vector from another
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">Result of subtraction</param>
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
/// <summary>
/// Multiply a vector and a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <returns>Result of the multiplication</returns>
public static Vector2 Mult(Vector2 a, float f)
{
a.X *= f;
a.Y *= f;
return a;
}
/// <summary>
/// Multiply a vector and a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <param name="result">Result of the multiplication</param>
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
/// <summary>
/// Divide a vector by a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <returns>Result of the division</returns>
public static Vector2 Div(Vector2 a, float f)
{
float mult = 1.0f / f;
a.X *= mult;
a.Y *= mult;
return a;
}
/// <summary>
/// Divide a vector by a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <param name="result">Result of the division</param>
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
/// <summary>
/// Calculate the component-wise minimum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>The component-wise minimum</returns>
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;
}
/// <summary>
/// Calculate the component-wise minimum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">The component-wise minimum</param>
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
/// <summary>
/// Calculate the component-wise maximum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>The component-wise maximum</returns>
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;
}
/// <summary>
/// Calculate the component-wise maximum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">The component-wise maximum</param>
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
/// <summary>
/// Clamp a vector to the given minimum and maximum vectors
/// </summary>
/// <param name="vec">Input vector</param>
/// <param name="min">Minimum vector</param>
/// <param name="max">Maximum vector</param>
/// <returns>The clamped vector</returns>
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;
}
/// <summary>
/// Clamp a vector to the given minimum and maximum vectors
/// </summary>
/// <param name="vec">Input vector</param>
/// <param name="min">Minimum vector</param>
/// <param name="max">Maximum vector</param>
/// <param name="result">The clamped vector</param>
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
/// <summary>
/// Scale a vector to unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <returns>The normalized vector</returns>
public static Vector2 Normalize(Vector2 vec)
{
float scale = 1.0f / vec.Length;
vec.X *= scale;
vec.Y *= scale;
return vec;
}
/// <summary>
/// Scale a vector to unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <param name="result">The normalized vector</param>
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
/// <summary>
/// Scale a vector to approximately unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <returns>The normalized vector</returns>
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;
}
/// <summary>
/// Scale a vector to approximately unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <param name="result">The normalized vector</param>
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
/// <summary>
/// Caclulate the dot (scalar) product of two vectors
/// </summary>
/// <param name="left">First operand</param>
/// <param name="right">Second operand</param>
/// <returns>The dot product of the two inputs</returns>
public static float Dot(Vector2 left, Vector2 right)
{
return left.X * right.X + left.Y * right.Y;
}
#endregion
#region Lerp
/// <summary>
/// Returns a new Vector that is the linear blend of the 2 given Vectors
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor</param>
/// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns>
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
/// <summary>
/// Interpolate 3 Vectors using Barycentric coordinates
/// </summary>
/// <param name="a">First input Vector</param>
/// <param name="b">Second input Vector</param>
/// <param name="c">Third input Vector</param>
/// <param name="u">First Barycentric Coordinate</param>
/// <param name="v">Second Barycentric Coordinate</param>
/// <returns>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</returns>
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()
/// <summary>
/// <summary>
/// Returns a System.String that represents the current Vector2.
/// </summary>
/// <returns></returns>
@ -673,6 +714,10 @@ namespace OpenTK.Math
#endregion
#endregion
#endregion
#region IEquatable<Vector2> Members
/// <summary>Indicates whether the current vector is equal to another vector.</summary>

Двоичные данные
Source/OpenTK/Math/Vector2d.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
{
/// <summary>2-component Vector of the Half type. Occupies 4 Byte total.</summary>
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Vector2h : ISerializable, IEquatable<Vector2h>
{
#region Fields
/// <summary>The X component of the Half2.</summary>
public Half X;
/// <summary>The Y component of the Half2.</summary>
public Half Y;
#endregion
#region Constructors
/// <summary>
/// The new Half2 instance will avoid conversion and copy directly from the Half parameters.
/// </summary>
/// <param name="x">An Half instance of a 16-Bit half precision floating point number.</param>
/// <param name="y">An Half instance of a 16-Bit half precision floating point number.</param>
public Vector2h(Half x, Half y)
{
X = x;
Y = y;
}
/// <summary>
/// The new Half2 instance will convert the 2 parameters into 16-Bit Half precision floating point.
/// </summary>
/// <param name="x">32-Bit Single precision floating point number.</param>
/// <param name="y">32-Bit Single precision floating point number.</param>
public Vector2h(Single x, Single y)
{
X = new Half(x);
Y = new Half(y);
}
/// <summary>
/// The new Half2 instance will convert the 2 parameters into 16-Bit Half precision floating point.
/// </summary>
/// <param name="x">32-Bit Single precision floating point number.</param>
/// <param name="y">32-Bit Single precision floating point number.</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
public Vector2h(Single x, Single y, bool throwOnError)
{
X = new Half(x, throwOnError);
Y = new Half(y, throwOnError);
}
/// <summary>
/// The new Half2 instance will convert the Vector2 into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector2</param>
[CLSCompliant(false)]
public Vector2h(Vector2 v)
{
X = new Half(v.X);
Y = new Half(v.Y);
}
/// <summary>
/// The new Half2 instance will convert the Vector2 into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector2</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
[CLSCompliant(false)]
public Vector2h(Vector2 v, bool throwOnError)
{
X = new Half(v.X, throwOnError);
Y = new Half(v.Y, throwOnError);
}
/// <summary>
/// The new Half2 instance will convert the Vector2 into 16-Bit Half precision floating point.
/// This is the fastest constructor.
/// </summary>
/// <param name="v">OpenTK.Math.Vector2</param>
public Vector2h(ref Vector2 v)
{
X = new Half(v.X);
Y = new Half(v.Y);
}
/// <summary>
/// The new Half2 instance will convert the Vector2 into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector2</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
public Vector2h(ref Vector2 v, bool throwOnError)
{
X = new Half(v.X, throwOnError);
Y = new Half(v.Y, throwOnError);
}
/// <summary>
/// The new Half2 instance will convert the Vector2d into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector2d</param>
public Vector2h(Vector2d v)
{
X = new Half(v.X);
Y = new Half(v.Y);
}
/// <summary>
/// The new Half2 instance will convert the Vector2d into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector2d</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
public Vector2h(Vector2d v, bool throwOnError)
{
X = new Half(v.X, throwOnError);
Y = new Half(v.Y, throwOnError);
}
/// <summary>
/// The new Half2 instance will convert the Vector2d into 16-Bit Half precision floating point.
/// This is the faster constructor.
/// </summary>
/// <param name="v">OpenTK.Math.Vector2d</param>
[CLSCompliant(false)]
public Vector2h(ref Vector2d v)
{
X = new Half(v.X);
Y = new Half(v.Y);
}
/// <summary>
/// The new Half2 instance will convert the Vector2d into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector2d</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
[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
/// <summary>
/// Returns this Half2 instance's contents as Vector2.
/// </summary>
/// <returns>OpenTK.Math.Vector2</returns>
public Vector2 ToVector2()
{
return new Vector2(X, Y);
}
/// <summary>
/// Returns this Half2 instance's contents as Vector2d.
/// </summary>
/// <param name="v">OpenTK.Math.Vector2d</param>
public Vector2d ToVector2d()
{
return new Vector2d(X, Y);
}
#endregion Half -> Single
#region Conversions
/// <summary>Converts OpenTK.Math.Vector2 to OpenTK.Math.Half2.</summary>
/// <param name="v4f">The Vector2 to convert.</param>
/// <returns>The resulting Half vector.</returns>
public static explicit operator Vector2h(Vector2 v)
{
return new Vector2h(v);
}
/// <summary>Converts OpenTK.Math.Vector2d to OpenTK.Math.Half2.</summary>
/// <param name="v4d">The Vector2d to convert.</param>
/// <returns>The resulting Half vector.</returns>
public static explicit operator Vector2h(Vector2d v)
{
return new Vector2h(v);
}
/// <summary>Converts OpenTK.Math.Half2 to OpenTK.Math.Vector2.</summary>
/// <param name="h4">The Half2 to convert.</param>
/// <returns>The resulting Vector2.</returns>
public static explicit operator Vector2(Vector2h h)
{
return new Vector2(h.X, h.Y);
}
/// <summary>Converts OpenTK.Math.Half2 to OpenTK.Math.Vector2d.</summary>
/// <param name="h4">The Half2 to convert.</param>
/// <returns>The resulting Vector2d.</returns>
public static explicit operator Vector2d(Vector2h h)
{
return new Vector2d(h.X, h.Y);
}
#endregion Conversions
#region Constants
/// <summary>The size in bytes for an instance of the Half2 struct is 4.</summary>
public static readonly int SizeInBytes = 4;
#endregion Constants
#region ISerializable
/// <summary>Constructor used by ISerializable to deserialize the object.</summary>
/// <param name="info"></param>
/// <param name="context"></param>
public Vector2h(SerializationInfo info, StreamingContext context)
{
this.X = (Half)info.GetValue("X", typeof(Half));
this.Y = (Half)info.GetValue("Y", typeof(Half));
}
/// <summary>Used by ISerialize to serialize the object.</summary>
/// <param name="info"></param>
/// <param name="context"></param>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("X", this.X);
info.AddValue("Y", this.Y);
}
#endregion ISerializable
#region Binary dump
/// <summary>Updates the X and Y components of this instance by reading from a Stream.</summary>
/// <param name="bin">A BinaryReader instance associated with an open Stream.</param>
public void FromBinaryStream(BinaryReader bin)
{
X.FromBinaryStream(bin);
Y.FromBinaryStream(bin);
}
/// <summary>Writes the X and Y components of this instance into a Stream.</summary>
/// <param name="bin">A BinaryWriter instance associated with an open Stream.</param>
public void ToBinaryStream(BinaryWriter bin)
{
X.ToBinaryStream(bin);
Y.ToBinaryStream(bin);
}
#endregion Binary dump
#region IEquatable<Half2> Members
/// <summary>Returns a value indicating whether this instance is equal to a specified OpenTK.Math.Half2 vector.</summary>
/// <param name="other">OpenTK.Math.Half2 to compare to this instance..</param>
/// <returns>True, if other is equal to this instance; false otherwise.</returns>
public bool Equals(Vector2h other)
{
return (this.X.Equals(other.X) && this.Y.Equals(other.Y));
}
#endregion
#region ToString()
/// <summary>Returns a string that contains this Half2's numbers in human-legible form.</summary>
public override string ToString()
{
return String.Format("({0}, {1})", X.ToString(), Y.ToString());
}
#endregion ToString()
#region BitConverter
/// <summary>Returns the Half2 as an array of bytes.</summary>
/// <param name="h">The Half2 to convert.</param>
/// <returns>The input as byte array.</returns>
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;
}
/// <summary>Converts an array of bytes into Half2.</summary>
/// <param name="value">A Half2 in it's byte[] representation.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A new Half2 instance.</returns>
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
}
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Двоичные данные
Source/OpenTK/Math/Vector3d.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
{
/// <summary>3-component Vector of the Half type. Occupies 6 Byte total.</summary>
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Vector3h : ISerializable, IEquatable<Vector3h>
{
#region Public Fields
/// <summary>The X component of the Half3.</summary>
public Half X;
/// <summary>The Y component of the Half3.</summary>
public Half Y;
/// <summary>The Z component of the Half3.</summary>
public Half Z;
#endregion Public Fields
#region Constructors
/// <summary>
/// The new Half3 instance will avoid conversion and copy directly from the Half parameters.
/// </summary>
/// <param name="x">An Half instance of a 16-Bit half precision floating point number.</param>
/// <param name="y">An Half instance of a 16-Bit half precision floating point number.</param>
/// <param name="z">An Half instance of a 16-Bit half precision floating point number.</param>
public Vector3h(Half x, Half y, Half z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
/// <summary>
/// The new Half3 instance will convert the 3 parameters into 16-Bit Half precision floating point.
/// </summary>
/// <param name="x">32-Bit Single precision floating point number.</param>
/// <param name="y">32-Bit Single precision floating point number.</param>
/// <param name="z">32-Bit Single precision floating point number.</param>
public Vector3h(Single x, Single y, Single z)
{
X = new Half(x);
Y = new Half(y);
Z = new Half(z);
}
/// <summary>
/// The new Half3 instance will convert the 3 parameters into 16-Bit Half precision floating point.
/// </summary>
/// <param name="x">32-Bit Single precision floating point number.</param>
/// <param name="y">32-Bit Single precision floating point number.</param>
/// <param name="z">32-Bit Single precision floating point number.</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
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);
}
/// <summary>
/// The new Half3 instance will convert the Vector3 into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector3</param>
[CLSCompliant(false)]
public Vector3h(Vector3 v)
{
X = new Half(v.X);
Y = new Half(v.Y);
Z = new Half(v.Z);
}
/// <summary>
/// The new Half3 instance will convert the Vector3 into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector3</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
[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);
}
/// <summary>
/// The new Half3 instance will convert the Vector3 into 16-Bit Half precision floating point.
/// This is the fastest constructor.
/// </summary>
/// <param name="v">OpenTK.Math.Vector3</param>
public Vector3h(ref Vector3 v)
{
X = new Half(v.X);
Y = new Half(v.Y);
Z = new Half(v.Z);
}
/// <summary>
/// The new Half3 instance will convert the Vector3 into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector3</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
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);
}
/// <summary>
/// The new Half3 instance will convert the Vector3d into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector3d</param>
public Vector3h(Vector3d v)
{
X = new Half(v.X);
Y = new Half(v.Y);
Z = new Half(v.Z);
}
/// <summary>
/// The new Half3 instance will convert the Vector3d into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector3d</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
public Vector3h(Vector3d v, bool throwOnError)
{
X = new Half(v.X, throwOnError);
Y = new Half(v.Y, throwOnError);
Z = new Half(v.Z, throwOnError);
}
/// <summary>
/// The new Half3 instance will convert the Vector3d into 16-Bit Half precision floating point.
/// This is the faster constructor.
/// </summary>
/// <param name="v">OpenTK.Math.Vector3d</param>
[CLSCompliant(false)]
public Vector3h(ref Vector3d v)
{
X = new Half(v.X);
Y = new Half(v.Y);
Z = new Half(v.Z);
}
/// <summary>
/// The new Half3 instance will convert the Vector3d into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector3d</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
[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
/// <summary>
/// Returns this Half3 instance's contents as Vector3.
/// </summary>
/// <returns>OpenTK.Math.Vector3</returns>
public Vector3 ToVector3()
{
return new Vector3(X, Y, Z);
}
/// <summary>
/// Returns this Half3 instance's contents as Vector3d.
/// </summary>
/// <param name="v">OpenTK.Math.Vector3d</param>
public Vector3d ToVector3d()
{
return new Vector3d(X, Y, Z);
}
#endregion Half -> Single
#region Conversions
/// <summary>Converts OpenTK.Math.Vector3 to OpenTK.Math.Half3.</summary>
/// <param name="v4f">The Vector3 to convert.</param>
/// <returns>The resulting Half vector.</returns>
public static explicit operator Vector3h(Vector3 v3f)
{
return new Vector3h(v3f);
}
/// <summary>Converts OpenTK.Math.Vector3d to OpenTK.Math.Half3.</summary>
/// <param name="v4d">The Vector3d to convert.</param>
/// <returns>The resulting Half vector.</returns>
public static explicit operator Vector3h(Vector3d v3d)
{
return new Vector3h(v3d);
}
/// <summary>Converts OpenTK.Math.Half3 to OpenTK.Math.Vector3.</summary>
/// <param name="h4">The Half3 to convert.</param>
/// <returns>The resulting Vector3.</returns>
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;
}
/// <summary>Converts OpenTK.Math.Half3 to OpenTK.Math.Vector3d.</summary>
/// <param name="h4">The Half3 to convert.</param>
/// <returns>The resulting Vector3d.</returns>
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
/// <summary>The size in bytes for an instance of the Half3 struct is 6.</summary>
public static readonly int SizeInBytes = 6;
#endregion Constants
#region ISerializable
/// <summary>Constructor used by ISerializable to deserialize the object.</summary>
/// <param name="info"></param>
/// <param name="context"></param>
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));
}
/// <summary>Used by ISerialize to serialize the object.</summary>
/// <param name="info"></param>
/// <param name="context"></param>
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
/// <summary>Updates the X,Y and Z components of this instance by reading from a Stream.</summary>
/// <param name="bin">A BinaryReader instance associated with an open Stream.</param>
public void FromBinaryStream(BinaryReader bin)
{
X.FromBinaryStream(bin);
Y.FromBinaryStream(bin);
Z.FromBinaryStream(bin);
}
/// <summary>Writes the X,Y and Z components of this instance into a Stream.</summary>
/// <param name="bin">A BinaryWriter instance associated with an open Stream.</param>
public void ToBinaryStream(BinaryWriter bin)
{
X.ToBinaryStream(bin);
Y.ToBinaryStream(bin);
Z.ToBinaryStream(bin);
}
#endregion Binary dump
#region IEquatable<Half3> Members
/// <summary>Returns a value indicating whether this instance is equal to a specified OpenTK.Math.Half3 vector.</summary>
/// <param name="other">OpenTK.Math.Half3 to compare to this instance..</param>
/// <returns>True, if other is equal to this instance; false otherwise.</returns>
public bool Equals(Vector3h other)
{
return (this.X.Equals(other.X) && this.Y.Equals(other.Y) && this.Z.Equals(other.Z));
}
#endregion
#region ToString()
/// <summary>Returns a string that contains this Half3's numbers in human-legible form.</summary>
public override string ToString()
{
return String.Format("({0}, {1}, {2})", X.ToString(), Y.ToString(), Z.ToString());
}
#endregion ToString()
#region BitConverter
/// <summary>Returns the Half3 as an array of bytes.</summary>
/// <param name="h">The Half3 to convert.</param>
/// <returns>The input as byte array.</returns>
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;
}
/// <summary>Converts an array of bytes into Half3.</summary>
/// <param name="value">A Half3 in it's byte[] representation.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A new Half3 instance.</returns>
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
}
}

Просмотреть файл

@ -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
/// <summary>
/// Add two Vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>Result of addition</returns>
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;
}
/// <summary>
/// Add two Vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">Result of addition</param>
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
/// <summary>
/// Subtract one Vector from another
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>Result of subtraction</returns>
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;
}
/// <summary>
/// Subtract one Vector from another
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">Result of subtraction</param>
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
/// <summary>
/// Multiply a vector and a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <returns>Result of the multiplication</returns>
public static Vector4 Mult(Vector4 a, float f)
{
a.X *= f;
a.Y *= f;
a.Z *= f;
a.W *= f;
return a;
}
/// <summary>
/// Multiply a vector and a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <param name="result">Result of the multiplication</param>
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
/// <summary>
/// Divide a vector by a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <returns>Result of the division</returns>
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;
}
/// <summary>
/// Divide a vector by a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <param name="result">Result of the division</param>
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
/// <summary>
/// Calculate the component-wise minimum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>The component-wise minimum</returns>
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;
}
/// <summary>
/// Calculate the component-wise minimum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">The component-wise minimum</param>
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
/// <summary>
/// Calculate the component-wise maximum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>The component-wise maximum</returns>
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;
}
/// <summary>
/// Calculate the component-wise maximum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">The component-wise maximum</param>
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
/// <summary>
/// Clamp a vector to the given minimum and maximum vectors
/// </summary>
/// <param name="vec">Input vector</param>
/// <param name="min">Minimum vector</param>
/// <param name="max">Maximum vector</param>
/// <returns>The clamped vector</returns>
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;
}
/// <summary>
/// Clamp a vector to the given minimum and maximum vectors
/// </summary>
/// <param name="vec">Input vector</param>
/// <param name="min">Minimum vector</param>
/// <param name="max">Maximum vector</param>
/// <param name="result">The clamped vector</param>
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
/// <summary>
/// Scale a vector to unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <returns>The normalized vector</returns>
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;
}
/// <summary>
/// Scale a vector to unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <param name="result">The normalized vector</param>
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
/// <summary>
/// Scale a vector to approximately unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <returns>The normalized vector</returns>
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;
}
/// <summary>
/// Scale a vector to approximately unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <param name="result">The normalized vector</param>
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
/// <summary>
/// Caclulate the dot product of two vectors
/// </summary>
/// <param name="left">First operand</param>
/// <param name="right">Second operand</param>
/// <returns>The dot product of the two inputs</returns>
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
/// <summary>
/// Returns a new Vector that is the linear blend of the 2 given Vectors
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor</param>
/// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns>
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
/// <summary>
/// Interpolate 3 Vectors using Barycentric coordinates
/// </summary>
/// <param name="a">First input Vector</param>
/// <param name="b">Second input Vector</param>
/// <param name="c">Third input Vector</param>
/// <param name="u">First Barycentric Coordinate</param>
/// <param name="v">Second Barycentric Coordinate</param>
/// <returns>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</returns>
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
/// <summary>
/// Transform a Vector by the given Matrix
/// </summary>
/// <param name="pos">The vector to transform</param>
/// <param name="mat">The desired transformation</param>
/// <returns>The transformed vector</returns>
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()
/// <summary>
/// Add two Vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>Result of addition</returns>
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;
}
/// <summary>
/// Add two Vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">Result of addition</param>
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
/// <summary>
/// Subtract one Vector from another
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>Result of subtraction</returns>
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;
}
/// <summary>
/// Subtract one Vector from another
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">Result of subtraction</param>
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
/// <summary>
/// Multiply a vector and a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <returns>Result of the multiplication</returns>
public static Vector4 Mult(Vector4 a, float f)
{
a.X *= f;
a.Y *= f;
a.Z *= f;
a.W *= f;
return a;
}
/// <summary>
/// Multiply a vector and a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <param name="result">Result of the multiplication</param>
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
/// <summary>
/// Divide a vector by a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <returns>Result of the division</returns>
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;
}
/// <summary>
/// Divide a vector by a scalar
/// </summary>
/// <param name="a">Vector operand</param>
/// <param name="f">Scalar operand</param>
/// <param name="result">Result of the division</param>
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
/// <summary>
/// Calculate the component-wise minimum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>The component-wise minimum</returns>
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;
}
/// <summary>
/// Calculate the component-wise minimum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">The component-wise minimum</param>
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
/// <summary>
/// Calculate the component-wise maximum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <returns>The component-wise maximum</returns>
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;
}
/// <summary>
/// Calculate the component-wise maximum of two vectors
/// </summary>
/// <param name="a">First operand</param>
/// <param name="b">Second operand</param>
/// <param name="result">The component-wise maximum</param>
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
/// <summary>
/// Clamp a vector to the given minimum and maximum vectors
/// </summary>
/// <param name="vec">Input vector</param>
/// <param name="min">Minimum vector</param>
/// <param name="max">Maximum vector</param>
/// <returns>The clamped vector</returns>
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;
}
/// <summary>
/// Clamp a vector to the given minimum and maximum vectors
/// </summary>
/// <param name="vec">Input vector</param>
/// <param name="min">Minimum vector</param>
/// <param name="max">Maximum vector</param>
/// <param name="result">The clamped vector</param>
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
/// <summary>
/// Scale a vector to unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <returns>The normalized vector</returns>
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;
}
/// <summary>
/// Scale a vector to unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <param name="result">The normalized vector</param>
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
/// <summary>
/// Scale a vector to approximately unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <returns>The normalized vector</returns>
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;
}
/// <summary>
/// Scale a vector to approximately unit length
/// </summary>
/// <param name="vec">The input vector</param>
/// <param name="result">The normalized vector</param>
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
/// <summary>
/// Caclulate the dot product of two vectors
/// </summary>
/// <param name="left">First operand</param>
/// <param name="right">Second operand</param>
/// <returns>The dot product of the two inputs</returns>
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
/// <summary>
/// Returns a new Vector that is the linear blend of the 2 given Vectors
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor</param>
/// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns>
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
/// <summary>
/// Interpolate 3 Vectors using Barycentric coordinates
/// </summary>
/// <param name="a">First input Vector</param>
/// <param name="b">Second input Vector</param>
/// <param name="c">Third input Vector</param>
/// <param name="u">First Barycentric Coordinate</param>
/// <param name="v">Second Barycentric Coordinate</param>
/// <returns>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</returns>
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
/// <summary>
/// Transform a Vector by the given Matrix
/// </summary>
/// <param name="pos">The vector to transform</param>
/// <param name="mat">The desired transformation</param>
/// <returns>The transformed vector</returns>
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()
/// <summary>
/// <summary>
/// Returns a System.String that represents the current Vector4.
/// </summary>
/// <returns></returns>
@ -748,6 +766,10 @@ namespace OpenTK.Math
#endregion
#endregion
#endregion
#region IEquatable<Vector4> Members
/// <summary>Indicates whether the current vector is equal to another vector.</summary>

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -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
{
/// <summary>4-component Vector of the Half type. Occupies 8 Byte total.</summary>
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Vector4h : ISerializable, IEquatable<Vector4h>
{
#region Public Fields
/// <summary>The X component of the Half4.</summary>
public Half X;
/// <summary>The Y component of the Half4.</summary>
public Half Y;
/// <summary>The Z component of the Half4.</summary>
public Half Z;
/// <summary>The W component of the Half4.</summary>
public Half W;
#endregion Public Fields
#region Constructors
/// <summary>
/// The new Half4 instance will avoid conversion and copy directly from the Half parameters.
/// </summary>
/// <param name="x">An Half instance of a 16-Bit half precision floating point number.</param>
/// <param name="y">An Half instance of a 16-Bit half precision floating point number.</param>
/// <param name="z">An Half instance of a 16-Bit half precision floating point number.</param>
/// <param name="w">An Half instance of a 16-Bit half precision floating point number.</param>
public Vector4h(Half x, Half y, Half z, Half w)
{
this.X = x;
this.Y = y;
this.Z = z;
this.W = w;
}
/// <summary>
/// The new Half4 instance will convert the 4 parameters into 16-Bit Half precision floating point.
/// </summary>
/// <param name="x">32-Bit Single precision floating point number.</param>
/// <param name="y">32-Bit Single precision floating point number.</param>
/// <param name="z">32-Bit Single precision floating point number.</param>
/// <param name="w">32-Bit Single precision floating point number.</param>
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);
}
/// <summary>
/// The new Half4 instance will convert the 4 parameters into 16-Bit Half precision floating point.
/// </summary>
/// <param name="x">32-Bit Single precision floating point number.</param>
/// <param name="y">32-Bit Single precision floating point number.</param>
/// <param name="z">32-Bit Single precision floating point number.</param>
/// <param name="w">32-Bit Single precision floating point number.</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
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);
}
/// <summary>
/// The new Half4 instance will convert the Vector4 into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector4</param>
[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);
}
/// <summary>
/// The new Half4 instance will convert the Vector4 into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector4</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
[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);
}
/// <summary>
/// The new Half4 instance will convert the Vector4 into 16-Bit Half precision floating point.
/// This is the fastest constructor.
/// </summary>
/// <param name="v">OpenTK.Math.Vector4</param>
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);
}
/// <summary>
/// The new Half4 instance will convert the Vector4 into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector4</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
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);
}
/// <summary>
/// The new Half4 instance will convert the Vector4d into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector4d</param>
public Vector4h(Vector4d v)
{
X = new Half(v.X);
Y = new Half(v.Y);
Z = new Half(v.Z);
W = new Half(v.W);
}
/// <summary>
/// The new Half4 instance will convert the Vector4d into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector4d</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
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);
}
/// <summary>
/// The new Half4 instance will convert the Vector4d into 16-Bit Half precision floating point.
/// This is the faster constructor.
/// </summary>
/// <param name="v">OpenTK.Math.Vector4d</param>
[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);
}
/// <summary>
/// The new Half4 instance will convert the Vector4d into 16-Bit Half precision floating point.
/// </summary>
/// <param name="v">OpenTK.Math.Vector4d</param>
/// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
[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
/// <summary>
/// Returns this Half4 instance's contents as Vector4.
/// </summary>
/// <returns>OpenTK.Math.Vector4</returns>
public Vector4 ToVector4()
{
return new Vector4(X, Y, Z, W);
}
/// <summary>
/// Returns this Half4 instance's contents as Vector4d.
/// </summary>
/// <param name="v">OpenTK.Math.Vector4d</param>
public Vector4d ToVector4d()
{
return new Vector4d(X, Y, Z, W);
}
#endregion Half -> Single
#region Conversions
/// <summary>Converts OpenTK.Math.Vector4 to OpenTK.Math.Half4.</summary>
/// <param name="v4f">The Vector4 to convert.</param>
/// <returns>The resulting Half vector.</returns>
public static explicit operator Vector4h(Vector4 v4f)
{
return new Vector4h(v4f);
}
/// <summary>Converts OpenTK.Math.Vector4d to OpenTK.Math.Half4.</summary>
/// <param name="v4d">The Vector4d to convert.</param>
/// <returns>The resulting Half vector.</returns>
public static explicit operator Vector4h(Vector4d v4d)
{
return new Vector4h(v4d);
}
/// <summary>Converts OpenTK.Math.Half4 to OpenTK.Math.Vector4.</summary>
/// <param name="h4">The Half4 to convert.</param>
/// <returns>The resulting Vector4.</returns>
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;
}
/// <summary>Converts OpenTK.Math.Half4 to OpenTK.Math.Vector4d.</summary>
/// <param name="h4">The Half4 to convert.</param>
/// <returns>The resulting Vector4d.</returns>
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
/// <summary>The size in bytes for an instance of the Half4 struct is 8.</summary>
public static readonly int SizeInBytes = 8;
#endregion Constants
#region ISerializable
/// <summary>Constructor used by ISerializable to deserialize the object.</summary>
/// <param name="info"></param>
/// <param name="context"></param>
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));
}
/// <summary>Used by ISerialize to serialize the object.</summary>
/// <param name="info"></param>
/// <param name="context"></param>
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
/// <summary>Updates the X,Y,Z and W components of this instance by reading from a Stream.</summary>
/// <param name="bin">A BinaryReader instance associated with an open Stream.</param>
public void FromBinaryStream(BinaryReader bin)
{
X.FromBinaryStream(bin);
Y.FromBinaryStream(bin);
Z.FromBinaryStream(bin);
W.FromBinaryStream(bin);
}
/// <summary>Writes the X,Y,Z and W components of this instance into a Stream.</summary>
/// <param name="bin">A BinaryWriter instance associated with an open Stream.</param>
public void ToBinaryStream(BinaryWriter bin)
{
X.ToBinaryStream(bin);
Y.ToBinaryStream(bin);
Z.ToBinaryStream(bin);
W.ToBinaryStream(bin);
}
#endregion Binary dump
#region IEquatable<Half4> Members
/// <summary>Returns a value indicating whether this instance is equal to a specified OpenTK.Math.Half4 vector.</summary>
/// <param name="other">OpenTK.Math.Half4 to compare to this instance..</param>
/// <returns>True, if other is equal to this instance; false otherwise.</returns>
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()
/// <summary>Returns a string that contains this Half4's numbers in human-legible form.</summary>
public override string ToString()
{
return String.Format("({0}, {1}, {2}, {3})", X.ToString(), Y.ToString(), Z.ToString(), W.ToString());
}
#endregion ToString()
#region BitConverter
/// <summary>Returns the Half4 as an array of bytes.</summary>
/// <param name="h">The Half4 to convert.</param>
/// <returns>The input as byte array.</returns>
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;
}
/// <summary>Converts an array of bytes into Half4.</summary>
/// <param name="value">A Half4 in it's byte[] representation.</param>
/// <param name="startIndex">The starting position within value.</param>
/// <returns>A new Half4 instance.</returns>
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
}
}