#region --- License --- /* Copyright (c) 2006, 2007 Stefanos Apostolopoulos * See license.txt for license info */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace OpenTK.Math { /// /// Represents a three-dimensional vector. /// [StructLayout(LayoutKind.Sequential)] public struct Vector3 { #region Fields /// /// The X component of the Vector3. /// public float X; /// /// The Y component of the Vector3. /// public float Y; /// /// The Z component of the Vector3. /// public float Z; #endregion #region Constructors /// /// Constructs a new Vector3. /// /// The x component of the Vector3. /// The y component of the Vector3. /// The z component of the Vector3. public Vector3(float x, float y, float z) { X = x; Y = y; Z = z; } /// /// Constructs a new Vector3 from the given Vector2. /// /// The Vector2 to copy components from. public Vector3(Vector2 v) { X = v.X; Y = v.Y; Z = 0.0f; } /// /// Constructs a new Vector3 from the given Vector3. /// /// The Vector3 to copy components from. public Vector3(Vector3 v) { X = v.X; Y = v.Y; Z = v.Z; } /// /// Constructs a new Vector3 from the given Vector4. /// /// The Vector4 to copy components from. public Vector3(Vector4 v) { X = v.X; Y = v.Y; Z = v.Z; } #endregion #region Functions #region Add /// /// Adds the given Vector2 to the current Vector3. /// /// The right operand of the addition. /// A new Vector3 containing the result of the addition. public Vector3 Add(Vector2 right) { return new Vector3(X + right.X, Y + right.Y, Z); } /// /// Adds the given Vector3 to the current Vector3. /// /// The right operand of the addition. /// A new Vector3 containing the result of the addition. public Vector3 Add(Vector3 right) { return new Vector3(X + right.X, Y + right.Y, Z + right.Z); } /// /// Adds the given Vector4 to the current Vector3. W-coordinate remains unaffected. /// /// The right operand of the addition. /// A new Vector4 containing the result of the addition. public Vector4 Add(Vector4 right) { return new Vector4(X + right.X, Y + right.Y, Z + right.Z, right.W); } #endregion #region Sub /// /// Subtracts the given Vector2 from the current Vector3. /// /// The right operand of the subtraction. /// A new Vector3 containing the result of the subtraction. public Vector3 Sub(Vector2 right) { return new Vector3(X - right.X, Y - right.Y, Z); } /// /// Subtracts the given Vector3 from the current Vector3. /// /// The right operand of the subtraction. /// A new Vector3 containing the result of the subtraction. public Vector3 Sub(Vector3 right) { return new Vector3(X - right.X, Y - right.Y, Z - right.Z); } /// /// Subtracts the given Vector4 from the current Vector3. /// /// The right operand of the subtraction. /// A new Vector4 containing the result of the subtraction. public Vector4 Sub(Vector4 right) { return new Vector4(X - right.X, Y - right.Y, Z - right.Z, -right.W); } #endregion #region Dot /// /// Computes the dot product between the current Vector3 and the given Vector2. /// /// The right operand of the dot product. /// A float containing the result of the dot product. public float Dot(Vector2 right) { return X * right.X + Y * right.Y; } /// /// Computes the dot product between the current Vector3 and the given Vector3. /// /// The right operand of the dot product. /// A float containing the result of the dot product. public float Dot(Vector3 right) { return X * right.X + Y * right.Y + Z * right.Z; } /// /// Computes the dot product between the current Vector3 and the given Vector4. /// /// The right operand of the dot product. /// A float containing the result of the dot product. public float Dot(Vector4 right) { return X * right.X + Y * right.Y + Z * right.Z; } #endregion #region Cross /// /// Computes the cross product between the current and the given Vector3. /// /// The right operand of the cross product /// A new Vector3 containing the result of the computation. public Vector3 Cross(Vector3 right) { return new Vector3( Y * right.Z - Z * right.Y, Z * right.X - X * right.Z, X * right.Y - Y * right.X); } #endregion #region public float Length /// /// Gets the length of the vector. /// public float Length { get { return (float)System.Math.Sqrt(this.LengthSquared); } } #endregion #region public float LengthSquared /// /// Gets the square of the vector length. /// public float LengthSquared { get { return X * X + Y * Y + Z * Z; } } #endregion #region public Vector3 Normalize() /// /// Scales the Vector3 to unit length. /// /// The normalized version of the current vector. public Vector3 Normalize() { float length = this.Length; return new Vector3(X / length, Y / Length, Z / length); } #endregion #region public Vector3 Scale(float sx, float sy, float sz) /// /// Scales the current Vector3 by the given amounts. /// /// The scale of the X component. /// The scale of the Y component. /// The scale of the Z component. /// A new, scaled Vector3. public Vector3 Scale(float sx, float sy, float sz) { return new Vector3(X * sx, Y * sy, Z * sz); } #endregion #endregion #region Operator overloads public static Vector3 operator +(Vector3 left, Vector2 right) { return left.Add(right); } public static Vector3 operator +(Vector3 left, Vector3 right) { return left.Add(right); } public static Vector4 operator +(Vector3 left, Vector4 right) { return left.Add(right); } public static Vector3 operator -(Vector3 left, Vector2 right) { return left.Sub(right); } public static Vector3 operator -(Vector3 left, Vector3 right) { return left.Sub(right); } public static Vector4 operator -(Vector3 left, Vector4 right) { return left.Sub(right); } [CLSCompliant(false)] unsafe public static implicit operator float*(Vector3 v) { return &v.X; } #endregion } }