#region License /* MIT License Copyright(c) 2017 Mattias Edlund 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.CompilerServices; using UnityEngine; namespace UnityMeshSimplifier { /// /// A double precision 3D vector. /// public struct Vector3d : IEquatable { #region Static Read-Only /// /// The zero vector. /// public static readonly Vector3d zero = new Vector3d(0, 0, 0); #endregion #region Consts /// /// The vector epsilon. /// public const double Epsilon = double.Epsilon; #endregion #region Fields /// /// The x component. /// public double x; /// /// The y component. /// public double y; /// /// The z component. /// public double z; #endregion #region Properties /// /// Gets the magnitude of this vector. /// public double Magnitude { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return System.Math.Sqrt(x * x + y * y + z * z); } } /// /// Gets the squared magnitude of this vector. /// public double MagnitudeSqr { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return (x * x + y * y + z * z); } } /// /// Gets a normalized vector from this vector. /// public Vector3d Normalized { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { Vector3d result; Normalize(ref this, out result); return result; } } /// /// Gets or sets a specific component by index in this vector. /// /// The component index. public double this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { switch (index) { case 0: return x; case 1: return y; case 2: return z; default: throw new IndexOutOfRangeException("Invalid Vector3d index!"); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] set { switch (index) { case 0: x = value; break; case 1: y = value; break; case 2: z = value; break; default: throw new IndexOutOfRangeException("Invalid Vector3d index!"); } } } #endregion #region Constructor /// /// Creates a new vector with one value for all components. /// /// The value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector3d(double value) { this.x = value; this.y = value; this.z = value; } /// /// Creates a new vector. /// /// The x value. /// The y value. /// The z value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector3d(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } /// /// Creates a new vector from a single precision vector. /// /// The single precision vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector3d(Vector3 vector) { this.x = vector.x; this.y = vector.y; this.z = vector.z; } #endregion #region Operators /// /// Adds two vectors. /// /// The first vector. /// The second vector. /// The resulting vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3d operator +(Vector3d a, Vector3d b) { return new Vector3d(a.x + b.x, a.y + b.y, a.z + b.z); } /// /// Subtracts two vectors. /// /// The first vector. /// The second vector. /// The resulting vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3d operator -(Vector3d a, Vector3d b) { return new Vector3d(a.x - b.x, a.y - b.y, a.z - b.z); } /// /// Scales the vector uniformly. /// /// The vector. /// The scaling value. /// The resulting vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3d operator *(Vector3d a, double d) { return new Vector3d(a.x * d, a.y * d, a.z * d); } /// /// Scales the vector uniformly. /// /// The scaling vlaue. /// The vector. /// The resulting vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3d operator *(double d, Vector3d a) { return new Vector3d(a.x * d, a.y * d, a.z * d); } /// /// Divides the vector with a float. /// /// The vector. /// The dividing float value. /// The resulting vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3d operator /(Vector3d a, double d) { return new Vector3d(a.x / d, a.y / d, a.z / d); } /// /// Subtracts the vector from a zero vector. /// /// The vector. /// The resulting vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3d operator -(Vector3d a) { return new Vector3d(-a.x, -a.y, -a.z); } /// /// Returns if two vectors equals eachother. /// /// The left hand side vector. /// The right hand side vector. /// If equals. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(Vector3d lhs, Vector3d rhs) { return (lhs - rhs).MagnitudeSqr < Epsilon; } /// /// Returns if two vectors don't equal eachother. /// /// The left hand side vector. /// The right hand side vector. /// If not equals. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(Vector3d lhs, Vector3d rhs) { return (lhs - rhs).MagnitudeSqr >= Epsilon; } /// /// Implicitly converts from a single-precision vector into a double-precision vector. /// /// The single-precision vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator Vector3d(Vector3 v) { return new Vector3d(v.x, v.y, v.z); } /// /// Implicitly converts from a double-precision vector into a single-precision vector. /// /// The double-precision vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator Vector3(Vector3d v) { return new Vector3((float)v.x, (float)v.y, (float)v.z); } #endregion #region Public Methods #region Instance /// /// Set x, y and z components of an existing vector. /// /// The x value. /// The y value. /// The z value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Set(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } /// /// Multiplies with another vector component-wise. /// /// The vector to multiply with. [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Scale(ref Vector3d scale) { x *= scale.x; y *= scale.y; z *= scale.z; } /// /// Normalizes this vector. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Normalize() { double mag = this.Magnitude; if (mag > Epsilon) { x /= mag; y /= mag; z /= mag; } else { x = y = z = 0; } } /// /// Clamps this vector between a specific range. /// /// The minimum component value. /// The maximum component value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Clamp(double min, double max) { if (x < min) x = min; else if (x > max) x = max; if (y < min) y = min; else if (y > max) y = max; if (z < min) z = min; else if (z > max) z = max; } #endregion #region Object /// /// Returns a hash code for this vector. /// /// The hash code. public override int GetHashCode() { return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2; } /// /// Returns if this vector is equal to another one. /// /// The other vector to compare to. /// If equals. public override bool Equals(object other) { if (!(other is Vector3d)) { return false; } Vector3d vector = (Vector3d)other; return (x == vector.x && y == vector.y && z == vector.z); } /// /// Returns if this vector is equal to another one. /// /// The other vector to compare to. /// If equals. public bool Equals(Vector3d other) { return (x == other.x && y == other.y && z == other.z); } /// /// Returns a nicely formatted string for this vector. /// /// The string. public override string ToString() { return string.Format("({0:F1}, {1:F1}, {2:F1})", x, y, z); } /// /// Returns a nicely formatted string for this vector. /// /// The float format. /// The string. public string ToString(string format) { return string.Format("({0}, {1}, {2})", x.ToString(format), y.ToString(format), z.ToString(format)); } #endregion #region Static /// /// Dot Product of two vectors. /// /// The left hand side vector. /// The right hand side vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double Dot(ref Vector3d lhs, ref Vector3d rhs) { return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; } /// /// Cross Product of two vectors. /// /// The left hand side vector. /// The right hand side vector. /// The resulting vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Cross(ref Vector3d lhs, ref Vector3d rhs, out Vector3d result) { result = new Vector3d(lhs.y * rhs.z - lhs.z * rhs.y, lhs.z * rhs.x - lhs.x * rhs.z, lhs.x * rhs.y - lhs.y * rhs.x); } /// /// Calculates the angle between two vectors. /// /// The from vector. /// The to vector. /// The angle. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double Angle(ref Vector3d from, ref Vector3d to) { Vector3d fromNormalized = from.Normalized; Vector3d toNormalized = to.Normalized; return System.Math.Acos(MathHelper.Clamp(Vector3d.Dot(ref fromNormalized, ref toNormalized), -1.0, 1.0)) * MathHelper.Rad2Degd; } /// /// Performs a linear interpolation between two vectors. /// /// The vector to interpolate from. /// The vector to interpolate to. /// The time fraction. /// The resulting vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Lerp(ref Vector3d a, ref Vector3d b, double t, out Vector3d result) { result = new Vector3d(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t); } /// /// Multiplies two vectors component-wise. /// /// The first vector. /// The second vector. /// The resulting vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Scale(ref Vector3d a, ref Vector3d b, out Vector3d result) { result = new Vector3d(a.x * b.x, a.y * b.y, a.z * b.z); } /// /// Normalizes a vector. /// /// The vector to normalize. /// The resulting normalized vector. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Normalize(ref Vector3d value, out Vector3d result) { double mag = value.Magnitude; if (mag > Epsilon) { result = new Vector3d(value.x / mag, value.y / mag, value.z / mag); } else { result = Vector3d.zero; } } #endregion #endregion } }