// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) // Distributed under the MIT license. See the LICENSE.md file in the project root for more information. // // ----------------------------------------------------------------------------- // Original code from SlimMath project. http://code.google.com/p/slimmath/ // Greetings to SlimDX Group. Original code published with the following license: // ----------------------------------------------------------------------------- /* * Copyright (c) 2007-2011 SlimDX Group * * 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. */ using System; using System.Globalization; using System.Runtime.InteropServices; using System.Runtime.Serialization; namespace math { /// /// Represents a three dimensional mathematical vector. /// [DataContract( Name = "Int3" )] [DataStyle( DataStyle.Compact )] [StructLayout( LayoutKind.Sequential, Pack = 4 )] public struct Int3 : IEquatable, IFormattable { /// /// The size of the type, in bytes. /// public static readonly int SizeInBytes = lib.Util.SizeOf(); /// /// A with all of its components set to zero. /// public static readonly Int3 Zero = new Int3(); /// /// The X unit (1, 0, 0). /// public static readonly Int3 UnitX = new Int3( 1, 0, 0 ); /// /// The Y unit (0, 1, 0). /// public static readonly Int3 UnitY = new Int3( 0, 1, 0 ); /// /// The Z unit (0, 0, 1). /// public static readonly Int3 UnitZ = new Int3( 0, 0, 1 ); /// /// A with all of its components set to one. /// public static readonly Int3 One = new Int3( 1, 1, 1 ); /// /// The X component of the vector. /// [DataMember( Order = 0 )] public int X; /// /// The Y component of the vector. /// [DataMember( Order = 1 )] public int Y; /// /// The Z component of the vector. /// [DataMember( Order = 2 )] public int Z; /// /// Initializes a new instance of the struct. /// /// The value that will be assigned to all components. public Int3( int value ) { X = value; Y = value; Z = value; } /// /// Initializes a new instance of the struct. /// /// Initial value for the X component of the vector. /// Initial value for the Y component of the vector. /// Initial value for the Z component of the vector. public Int3( int x, int y, int z ) { X = x; Y = y; Z = z; } /// /// Initializes a new instance of the struct. /// /// A vector containing the values with which to initialize the X and Y components. /// Initial value for the Z component of the vector. public Int3( Vec2 value, int z ) { X = (int)value.X; Y = (int)value.Y; Z = z; } /// /// Initializes a new instance of the struct. /// /// The values to assign to the X, Y, and Z components of the vector. This must be an array with three elements. /// Thrown when is null. /// Thrown when contains more or less than three elements. public Int3( int[] values ) { if( values == null ) throw new ArgumentNullException( "values" ); if( values.Length != 3 ) throw new ArgumentOutOfRangeException( "values", "There must be three and only three input values for Int3." ); X = values[0]; Y = values[1]; Z = values[2]; } /// /// Gets or sets the component at the specified index. /// /// The value of the X, Y, or Z component, depending on the index. /// The index of the component to access. Use 0 for the X component, 1 for the Y component, and 2 for the Z component. /// The value of the component at the specified index. /// Thrown when the is out of the range [0, 2]. public int this[int index] { get { switch( index ) { case 0: return X; case 1: return Y; case 2: return Z; } throw new ArgumentOutOfRangeException( "index", "Indices for Int3 run from 0 to 2, inclusive." ); } set { switch( index ) { case 0: X = value; break; case 1: Y = value; break; case 2: Z = value; break; default: throw new ArgumentOutOfRangeException( "index", "Indices for Int3 run from 0 to 2, inclusive." ); } } } /// /// Calculates the length of the vector. /// /// The length of the vector. /// /// may be preferred when only the relative length is needed /// and speed is of the essence. /// public int Length() { return (int)Math.Sqrt( ( X * X ) + ( Y * Y ) + ( Z * Z ) ); } /// /// Calculates the squared length of the vector. /// /// The squared length of the vector. /// /// This method may be preferred to when only a relative length is needed /// and speed is of the essence. /// public int LengthSquared() { return ( X * X ) + ( Y * Y ) + ( Z * Z ); } /// /// Raises the exponent for each components. /// /// The exponent. public void Pow( int exponent ) { X = (int)Math.Pow( X, exponent ); Y = (int)Math.Pow( Y, exponent ); Z = (int)Math.Pow( Z, exponent ); } /// /// Creates an array containing the elements of the vector. /// /// A three-element array containing the components of the vector. public int[] ToArray() { return new int[] { X, Y, Z }; } /// /// Adds two vectors. /// /// The first vector to add. /// The second vector to add. /// When the method completes, contains the sum of the two vectors. public static void Add( ref Int3 left, ref Int3 right, out Int3 result ) { result = new Int3( left.X + right.X, left.Y + right.Y, left.Z + right.Z ); } /// /// Adds two vectors. /// /// The first vector to add. /// The second vector to add. /// The sum of the two vectors. public static Int3 Add( Int3 left, Int3 right ) { return new Int3( left.X + right.X, left.Y + right.Y, left.Z + right.Z ); } /// /// Subtracts two vectors. /// /// The first vector to subtract. /// The second vector to subtract. /// When the method completes, contains the difference of the two vectors. public static void Subtract( ref Int3 left, ref Int3 right, out Int3 result ) { result = new Int3( left.X - right.X, left.Y - right.Y, left.Z - right.Z ); } /// /// Subtracts two vectors. /// /// The first vector to subtract. /// The second vector to subtract. /// The difference of the two vectors. public static Int3 Subtract( Int3 left, Int3 right ) { return new Int3( left.X - right.X, left.Y - right.Y, left.Z - right.Z ); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// When the method completes, contains the scaled vector. public static void Multiply( ref Int3 value, int scale, out Int3 result ) { result = new Int3( value.X * scale, value.Y * scale, value.Z * scale ); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int3 Multiply( Int3 value, int scale ) { return new Int3( value.X * scale, value.Y * scale, value.Z * scale ); } /// /// Modulates a vector with another by performing component-wise multiplication. /// /// The first vector to modulate. /// The second vector to modulate. /// When the method completes, contains the modulated vector. public static void Modulate( ref Int3 left, ref Int3 right, out Int3 result ) { result = new Int3( left.X * right.X, left.Y * right.Y, left.Z * right.Z ); } /// /// Modulates a vector with another by performing component-wise multiplication. /// /// The first vector to modulate. /// The second vector to modulate. /// The modulated vector. public static Int3 Modulate( Int3 left, Int3 right ) { return new Int3( left.X * right.X, left.Y * right.Y, left.Z * right.Z ); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// When the method completes, contains the scaled vector. public static void Divide( ref Int3 value, int scale, out Int3 result ) { result = new Int3( value.X / scale, value.Y / scale, value.Z / scale ); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int3 Divide( Int3 value, int scale ) { return new Int3( value.X / scale, value.Y / scale, value.Z / scale ); } /// /// Reverses the direction of a given vector. /// /// The vector to negate. /// When the method completes, contains a vector facing in the opposite direction. public static void Negate( ref Int3 value, out Int3 result ) { result = new Int3( -value.X, -value.Y, -value.Z ); } /// /// Reverses the direction of a given vector. /// /// The vector to negate. /// A vector facing in the opposite direction. public static Int3 Negate( Int3 value ) { return new Int3( -value.X, -value.Y, -value.Z ); } /// /// Restricts a value to be within a specified range. /// /// The value to clamp. /// The minimum value. /// The maximum value. /// When the method completes, contains the clamped value. public static void Clamp( ref Int3 value, ref Int3 min, ref Int3 max, out Int3 result ) { int x = value.X; x = ( x > max.X ) ? max.X : x; x = ( x < min.X ) ? min.X : x; int y = value.Y; y = ( y > max.Y ) ? max.Y : y; y = ( y < min.Y ) ? min.Y : y; int z = value.Z; z = ( z > max.Z ) ? max.Z : z; z = ( z < min.Z ) ? min.Z : z; result = new Int3( x, y, z ); } /// /// Restricts a value to be within a specified range. /// /// The value to clamp. /// The minimum value. /// The maximum value. /// The clamped value. public static Int3 Clamp( Int3 value, Int3 min, Int3 max ) { Int3 result; Clamp( ref value, ref min, ref max, out result ); return result; } /// /// Calculates the dot product of two vectors. /// /// First source vector. /// Second source vector. /// When the method completes, contains the dot product of the two vectors. public static void Dot( ref Int3 left, ref Int3 right, out int result ) { result = ( left.X * right.X ) + ( left.Y * right.Y ) + ( left.Z * right.Z ); } /// /// Calculates the dot product of two vectors. /// /// First source vector. /// Second source vector. /// The dot product of the two vectors. public static int Dot( Int3 left, Int3 right ) { return ( left.X * right.X ) + ( left.Y * right.Y ) + ( left.Z * right.Z ); } /// /// Performs a linear interpolation between two vectors. /// /// Start vector. /// End vector. /// Value between 0 and 1 indicating the weight of . /// When the method completes, contains the linear interpolation of the two vectors. /// /// This method performs the linear interpolation based on the following formula. /// start + (end - start) * amount /// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned. /// public static void Lerp( ref Int3 start, ref Int3 end, float amount, out Int3 result ) { result.X = (int)( start.X + ( ( end.X - start.X ) * amount ) ); result.Y = (int)( start.Y + ( ( end.Y - start.Y ) * amount ) ); result.Z = (int)( start.Z + ( ( end.Z - start.Z ) * amount ) ); } /// /// Performs a linear interpolation between two vectors. /// /// Start vector. /// End vector. /// Value between 0 and 1 indicating the weight of . /// The linear interpolation of the two vectors. /// /// This method performs the linear interpolation based on the following formula. /// start + (end - start) * amount /// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned. /// public static Int3 Lerp( Int3 start, Int3 end, float amount ) { Int3 result; Lerp( ref start, ref end, amount, out result ); return result; } /// /// Performs a cubic interpolation between two vectors. /// /// Start vector. /// End vector. /// Value between 0 and 1 indicating the weight of . /// When the method completes, contains the cubic interpolation of the two vectors. public static void SmoothStep( ref Int3 start, ref Int3 end, float amount, out Int3 result ) { amount = ( amount > 1 ) ? 1 : ( ( amount < 0 ) ? 0 : amount ); amount = ( amount * amount ) * ( 3 - ( 2 * amount ) ); result.X = (int)( start.X + ( ( end.X - start.X ) * amount ) ); result.Y = (int)( start.Y + ( ( end.Y - start.Y ) * amount ) ); result.Z = (int)( start.Z + ( ( end.Z - start.Z ) * amount ) ); } /// /// Performs a cubic interpolation between two vectors. /// /// Start vector. /// End vector. /// Value between 0 and 1 indicating the weight of . /// The cubic interpolation of the two vectors. public static Int3 SmoothStep( Int3 start, Int3 end, float amount ) { Int3 result; SmoothStep( ref start, ref end, amount, out result ); return result; } /// /// Returns a vector containing the smallest components of the specified vectors. /// /// The first source vector. /// The second source vector. /// When the method completes, contains an new vector composed of the largest components of the source vectors. public static void Max( ref Int3 left, ref Int3 right, out Int3 result ) { result.X = ( left.X > right.X ) ? left.X : right.X; result.Y = ( left.Y > right.Y ) ? left.Y : right.Y; result.Z = ( left.Z > right.Z ) ? left.Z : right.Z; } /// /// Returns a vector containing the largest components of the specified vectors. /// /// The first source vector. /// The second source vector. /// A vector containing the largest components of the source vectors. public static Int3 Max( Int3 left, Int3 right ) { Int3 result; Max( ref left, ref right, out result ); return result; } /// /// Returns a vector containing the smallest components of the specified vectors. /// /// The first source vector. /// The second source vector. /// When the method completes, contains an new vector composed of the smallest components of the source vectors. public static void Min( ref Int3 left, ref Int3 right, out Int3 result ) { result.X = ( left.X < right.X ) ? left.X : right.X; result.Y = ( left.Y < right.Y ) ? left.Y : right.Y; result.Z = ( left.Z < right.Z ) ? left.Z : right.Z; } /// /// Returns a vector containing the smallest components of the specified vectors. /// /// The first source vector. /// The second source vector. /// A vector containing the smallest components of the source vectors. public static Int3 Min( Int3 left, Int3 right ) { Int3 result; Min( ref left, ref right, out result ); return result; } /// /// Adds two vectors. /// /// The first vector to add. /// The second vector to add. /// The sum of the two vectors. public static Int3 operator +( Int3 left, Int3 right ) { return new Int3( left.X + right.X, left.Y + right.Y, left.Z + right.Z ); } /// /// Assert a vector (return it unchanged). /// /// The vector to assert (unchange). /// The asserted (unchanged) vector. public static Int3 operator +( Int3 value ) { return value; } /// /// Subtracts two vectors. /// /// The first vector to subtract. /// The second vector to subtract. /// The difference of the two vectors. public static Int3 operator -( Int3 left, Int3 right ) { return new Int3( left.X - right.X, left.Y - right.Y, left.Z - right.Z ); } /// /// Reverses the direction of a given vector. /// /// The vector to negate. /// A vector facing in the opposite direction. public static Int3 operator -( Int3 value ) { return new Int3( -value.X, -value.Y, -value.Z ); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int3 operator *( float scale, Int3 value ) { return new Int3( (int)( value.X * scale ), (int)( value.Y * scale ), (int)( value.Z * scale ) ); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int3 operator *( Int3 value, float scale ) { return new Int3( (int)( value.X * scale ), (int)( value.Y * scale ), (int)( value.Z * scale ) ); } /// /// Scales a vector by the given value. /// /// The vector to scale. /// The amount by which to scale the vector. /// The scaled vector. public static Int3 operator /( Int3 value, float scale ) { return new Int3( (int)( value.X / scale ), (int)( value.Y / scale ), (int)( value.Z / scale ) ); } /// /// Tests for equality between two objects. /// /// The first value to compare. /// The second value to compare. /// true if has the same value as ; otherwise, false. public static bool operator ==( Int3 left, Int3 right ) { return left.Equals( right ); } /// /// Tests for inequality between two objects. /// /// The first value to compare. /// The second value to compare. /// true if has a different value than ; otherwise, false. public static bool operator !=( Int3 left, Int3 right ) { return !left.Equals( right ); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Vec2( Int3 value ) { return new Vec2( value.X, value.Y ); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Vec3( Int3 value ) { return new Vec3( value.X, value.Y, value.Z ); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Vec4( Int3 value ) { return new Vec4( value.X, value.Y, value.Z, 0 ); } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { return string.Format( CultureInfo.CurrentCulture, "X:{0} Y:{1} Z:{2}", X, Y, Z ); } /// /// Returns a that represents this instance. /// /// The format. /// /// A that represents this instance. /// public string ToString( string format ) { if( format == null ) return ToString(); return string.Format( CultureInfo.CurrentCulture, "X:{0} Y:{1} Z:{2}", X.ToString( format, CultureInfo.CurrentCulture ), Y.ToString( format, CultureInfo.CurrentCulture ), Z.ToString( format, CultureInfo.CurrentCulture ) ); } /// /// Returns a that represents this instance. /// /// The format provider. /// /// A that represents this instance. /// public string ToString( IFormatProvider formatProvider ) { return string.Format( formatProvider, "X:{0} Y:{1} Z:{2}", X, Y, Z ); } /// /// Returns a that represents this instance. /// /// The format. /// The format provider. /// /// A that represents this instance. /// public string ToString( string format, IFormatProvider formatProvider ) { if( format == null ) return ToString( formatProvider ); return string.Format( formatProvider, "X:{0} Y:{1} Z:{2}", X.ToString( format, formatProvider ), Y.ToString( format, formatProvider ), Z.ToString( format, formatProvider ) ); } /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { return X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode(); } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public bool Equals( Int3 other ) { return ( (float)Math.Abs( other.X - X ) < MathUtil.ZeroTolerance && (float)Math.Abs( other.Y - Y ) < MathUtil.ZeroTolerance && (float)Math.Abs( other.Z - Z ) < MathUtil.ZeroTolerance ); } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals( object value ) { if( value == null ) return false; if( value.GetType() != GetType() ) return false; return Equals( (Int3)value ); } #if WPFInterop /// /// Performs an implicit conversion from to . /// /// The value. /// The result of the conversion. public static implicit operator System.Windows.Media.Media3D.Int3D(Int3 value) { return new System.Windows.Media.Media3D.Int3D(value.X, value.Y, value.Z); } /// /// Performs an explicit conversion from to . /// /// The value. /// The result of the conversion. public static explicit operator Int3(System.Windows.Media.Media3D.Int3D value) { return new Int3((float)value.X, (float)value.Y, (float)value.Z); } #endif #if XnaInterop /// /// Performs an implicit conversion from to . /// /// The value. /// The result of the conversion. public static implicit operator Microsoft.Xna.Framework.Int3(Int3 value) { return new Microsoft.Xna.Framework.Int3(value.X, value.Y, value.Z); } /// /// Performs an implicit conversion from to . /// /// The value. /// The result of the conversion. public static implicit operator Int3(Microsoft.Xna.Framework.Int3 value) { return new Int3(value.X, value.Y, value.Z); } #endif } }