#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.Runtime.InteropServices;
namespace OpenTK
{
/// Represents a 3D vector using three 32-bit integer values.
///
/// The Vector3i structure is suitable for interoperation with unmanaged code requiring three consecutive integers.
///
[Serializable]
public struct Vector3i : IEquatable
{
#region Fields
///
/// The X component of the Vector3i.
///
public int X;
///
/// The Y component of the Vector3i.
///
public int Y;
///
/// The Z component of the Vector3i.
///
public int Z;
#endregion
#region Constructors
///
/// Constructs a new Vector3i.
///
/// The x coordinate of the net Vector3i.
/// The y coordinate of the net Vector3i.
/// The z coordinate of the net Vector3i.
public Vector3i(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
#endregion
#region Public Members
#region Static
#region Fields
///
/// Defines a unit-length Vector3i that points towards the X-axis.
///
public static readonly Vector3i UnitX = new Vector3i(1, 0, 0);
///
/// Defines a unit-length Vector3i that points towards the Y-axis.
///
public static readonly Vector3i UnitY = new Vector3i(0, 1, 0);
///
/// Defines a unit-length Vector3i that points towards the Z-axis.
///
public static readonly Vector3i UnitZ = new Vector3i(0, 0, 1);
///
/// Defines a zero-length Vector3i.
///
public static readonly Vector3i Zero = new Vector3i(0, 0, 0);
///
/// Defines an instance with all components set to 1.
///
public static readonly Vector3i One = new Vector3i(1, 1, 1);
///
/// Defines the size of the Vector3i struct in bytes.
///
public static readonly int SizeInBytes = Marshal.SizeOf(new Vector3i());
#endregion
#region Add
///
/// Adds two vectors.
///
/// Left operand.
/// Right operand.
/// Result of operation.
public static Vector3i Add(Vector3i a, Vector3i b)
{
Add(ref a, ref b, out a);
return a;
}
///
/// Adds two vectors.
///
/// Left operand.
/// Right operand.
/// Result of operation.
public static void Add(ref Vector3i a, ref Vector3i b, out Vector3i result)
{
result = new Vector3i(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
}
#endregion
#region Subtract
///
/// Subtract one Vector from another
///
/// First operand
/// Second operand
/// Result of subtraction
public static Vector3i Subtract(Vector3i a, Vector3i b)
{
Subtract(ref a, ref b, out a);
return a;
}
///
/// Subtract one Vector from another
///
/// First operand
/// Second operand
/// Result of subtraction
public static void Subtract(ref Vector3i a, ref Vector3i b, out Vector3i result)
{
result = new Vector3i(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
}
#endregion
#region Abs
///
/// Applies component-wise absolute evaluation
///
/// Absolute operation applies to this vector's components
/// A vector whose components have absolute value of given vector
public static Vector3i Abs(Vector3i value)
{
Vector3i result;
Abs(ref value, out result);
return result;
}
///
/// Applies component-wise absolute evaluation
///
/// Absolute operation applies to this vector's components
/// A vector whose components have absolute value of given vector
public static void Abs(ref Vector3i value, out Vector3i result)
{
result = new Vector3i(Math.Abs(value.X), Math.Abs(value.Y), Math.Abs(value.Z));
}
///
/// Applies component-wise absolute evaluation
///
/// Absolute operation applies to this vector's components
public static void Abs(ref Vector3i value)
{
Abs(ref value, out value);
}
#endregion
#region ComponentMin
///
/// Calculate the component-wise minimum of two vectors
///
/// First operand
/// Second operand
/// The component-wise minimum
public static Vector3i ComponentMin(Vector3i a, Vector3i 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;
return a;
}
///
/// Calculate the component-wise minimum of two vectors
///
/// First operand
/// Second operand
/// The component-wise minimum
public static void ComponentMin(ref Vector3i a, ref Vector3i b, out Vector3i 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;
}
#endregion
#region ComponentMax
///
/// Calculate the component-wise maximum of two vectors
///
/// First operand
/// Second operand
/// The component-wise maximum
public static Vector3i ComponentMax(Vector3i a, Vector3i 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;
return a;
}
///
/// Calculate the component-wise maximum of two vectors
///
/// First operand
/// Second operand
/// The component-wise maximum
public static void ComponentMax(ref Vector3i a, ref Vector3i b, out Vector3i 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;
}
#endregion
#region Clamp
///
/// Clamp a vector to the given minimum and maximum vectors
///
/// Input vector
/// Minimum vector
/// Maximum vector
/// The clamped vector
public static Vector3i Clamp(Vector3i vec, Vector3i min, Vector3i max)
{
vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
vec.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
return vec;
}
///
/// Clamp a vector to the given minimum and maximum vectors
///
/// Input vector
/// Minimum vector
/// Maximum vector
/// The clamped vector
public static void Clamp(ref Vector3i vec, ref Vector3i min, ref Vector3i max, out Vector3i result)
{
result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
result.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
}
#endregion
#endregion
#region Operators
///
/// Adds the specified instances.
///
/// Left operand.
/// Right operand.
/// Result of addition.
public static Vector3i operator +(Vector3i left, Vector3i right)
{
left.X += right.X;
left.Y += right.Y;
left.Z += right.Z;
return left;
}
///
/// Subtracts the specified instances.
///
/// Left operand.
/// Right operand.
/// Result of subtraction.
public static Vector3i operator -(Vector3i left, Vector3i right)
{
left.X -= right.X;
left.Y -= right.Y;
left.Z -= right.Z;
return left;
}
///
/// Negates the specified instance.
///
/// Operand.
/// Result of negation.
public static Vector3i operator -(Vector3i vec)
{
vec.X = -vec.X;
vec.Y = -vec.Y;
vec.Z = -vec.Z;
return vec;
}
///
/// Compares the specified instances for equality.
///
/// Left operand.
/// Right operand.
/// True if both instances are equal; false otherwise.
public static bool operator ==(Vector3i left, Vector3i right)
{
return left.Equals(right);
}
///
/// Compares the specified instances for inequality.
///
/// Left operand.
/// Right operand.
/// True if both instances are not equal; false otherwise.
public static bool operator !=(Vector3i left, Vector3i right)
{
return !left.Equals(right);
}
#endregion
#region Overrides
#region public override string ToString()
///
/// Returns a System.String that represents the current Vector3i.
///
///
public override string ToString()
{
return String.Format("({0}, {1}, {2})", X, Y, Z);
}
#endregion
#region public override int GetHashCode()
///
/// Returns the hashcode for this instance.
///
/// A System.Int32 containing the unique hashcode for this instance.
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode();
}
#endregion
#region public override bool Equals(object obj)
///
/// Indicates whether this instance and a specified object are equal.
///
/// The object to compare to.
/// True if the instances are equal; false otherwise.
public override bool Equals(object obj)
{
if (!(obj is Vector3i))
return false;
return this.Equals((Vector3i)obj);
}
#endregion
#endregion
#endregion
#region IEquatable Members
/// Indicates whether the current vector is equal to another vector.
/// A vector to compare with this vector.
/// true if the current vector is equal to the vector parameter; otherwise, false.
public bool Equals(Vector3i other)
{
return
X == other.X &&
Y == other.Y &&
Z == other.Z;
}
#endregion
}
}