Merged Cake and Abel branched into 2.0.3 (#131)
Co-authored-by: azyges <aaaaaa@aaa.aaa> Co-authored-by: cake <admin@cakeandbanana.nl> Co-authored-by: defnotken <itsdefnotken@gmail.com> Reviewed-on: #131
This commit was merged in pull request #131.
This commit is contained in:
388
LightlessSync/ThirdParty/MeshDecimator/Math/Vector4i.cs
vendored
Normal file
388
LightlessSync/ThirdParty/MeshDecimator/Math/Vector4i.cs
vendored
Normal file
@@ -0,0 +1,388 @@
|
||||
#region License
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright(c) 2017-2018 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.Globalization;
|
||||
|
||||
namespace MeshDecimator.Math
|
||||
{
|
||||
/// <summary>
|
||||
/// A 4D integer vector.
|
||||
/// </summary>
|
||||
public struct Vector4i : IEquatable<Vector4i>
|
||||
{
|
||||
#region Static Read-Only
|
||||
/// <summary>
|
||||
/// The zero vector.
|
||||
/// </summary>
|
||||
public static readonly Vector4i zero = new Vector4i(0, 0, 0, 0);
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
/// <summary>
|
||||
/// The x component.
|
||||
/// </summary>
|
||||
public int x;
|
||||
/// <summary>
|
||||
/// The y component.
|
||||
/// </summary>
|
||||
public int y;
|
||||
/// <summary>
|
||||
/// The z component.
|
||||
/// </summary>
|
||||
public int z;
|
||||
/// <summary>
|
||||
/// The w component.
|
||||
/// </summary>
|
||||
public int w;
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
/// <summary>
|
||||
/// Gets the magnitude of this vector.
|
||||
/// </summary>
|
||||
public int Magnitude
|
||||
{
|
||||
get { return (int)System.Math.Sqrt(x * x + y * y + z * z + w * w); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the squared magnitude of this vector.
|
||||
/// </summary>
|
||||
public int MagnitudeSqr
|
||||
{
|
||||
get { return (x * x + y * y + z * z + w * w); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a specific component by index in this vector.
|
||||
/// </summary>
|
||||
/// <param name="index">The component index.</param>
|
||||
public int this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
return x;
|
||||
case 1:
|
||||
return y;
|
||||
case 2:
|
||||
return z;
|
||||
case 3:
|
||||
return w;
|
||||
default:
|
||||
throw new IndexOutOfRangeException("Invalid Vector4i index!");
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case 0:
|
||||
x = value;
|
||||
break;
|
||||
case 1:
|
||||
y = value;
|
||||
break;
|
||||
case 2:
|
||||
z = value;
|
||||
break;
|
||||
case 3:
|
||||
w = value;
|
||||
break;
|
||||
default:
|
||||
throw new IndexOutOfRangeException("Invalid Vector4i index!");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
/// <summary>
|
||||
/// Creates a new vector with one value for all components.
|
||||
/// </summary>
|
||||
/// <param name="value">The value.</param>
|
||||
public Vector4i(int value)
|
||||
{
|
||||
this.x = value;
|
||||
this.y = value;
|
||||
this.z = value;
|
||||
this.w = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new vector.
|
||||
/// </summary>
|
||||
/// <param name="x">The x value.</param>
|
||||
/// <param name="y">The y value.</param>
|
||||
/// <param name="z">The z value.</param>
|
||||
/// <param name="w">The w value.</param>
|
||||
public Vector4i(int x, int y, int z, int w)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Operators
|
||||
/// <summary>
|
||||
/// Adds two vectors.
|
||||
/// </summary>
|
||||
/// <param name="a">The first vector.</param>
|
||||
/// <param name="b">The second vector.</param>
|
||||
/// <returns>The resulting vector.</returns>
|
||||
public static Vector4i operator +(Vector4i a, Vector4i b)
|
||||
{
|
||||
return new Vector4i(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subtracts two vectors.
|
||||
/// </summary>
|
||||
/// <param name="a">The first vector.</param>
|
||||
/// <param name="b">The second vector.</param>
|
||||
/// <returns>The resulting vector.</returns>
|
||||
public static Vector4i operator -(Vector4i a, Vector4i b)
|
||||
{
|
||||
return new Vector4i(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scales the vector uniformly.
|
||||
/// </summary>
|
||||
/// <param name="a">The vector.</param>
|
||||
/// <param name="d">The scaling value.</param>
|
||||
/// <returns>The resulting vector.</returns>
|
||||
public static Vector4i operator *(Vector4i a, int d)
|
||||
{
|
||||
return new Vector4i(a.x * d, a.y * d, a.z * d, a.w * d);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scales the vector uniformly.
|
||||
/// </summary>
|
||||
/// <param name="d">The scaling value.</param>
|
||||
/// <param name="a">The vector.</param>
|
||||
/// <returns>The resulting vector.</returns>
|
||||
public static Vector4i operator *(int d, Vector4i a)
|
||||
{
|
||||
return new Vector4i(a.x * d, a.y * d, a.z * d, a.w * d);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Divides the vector with a float.
|
||||
/// </summary>
|
||||
/// <param name="a">The vector.</param>
|
||||
/// <param name="d">The dividing float value.</param>
|
||||
/// <returns>The resulting vector.</returns>
|
||||
public static Vector4i operator /(Vector4i a, int d)
|
||||
{
|
||||
return new Vector4i(a.x / d, a.y / d, a.z / d, a.w / d);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subtracts the vector from a zero vector.
|
||||
/// </summary>
|
||||
/// <param name="a">The vector.</param>
|
||||
/// <returns>The resulting vector.</returns>
|
||||
public static Vector4i operator -(Vector4i a)
|
||||
{
|
||||
return new Vector4i(-a.x, -a.y, -a.z, -a.w);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if two vectors equals eachother.
|
||||
/// </summary>
|
||||
/// <param name="lhs">The left hand side vector.</param>
|
||||
/// <param name="rhs">The right hand side vector.</param>
|
||||
/// <returns>If equals.</returns>
|
||||
public static bool operator ==(Vector4i lhs, Vector4i rhs)
|
||||
{
|
||||
return (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.w == rhs.w);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if two vectors don't equal eachother.
|
||||
/// </summary>
|
||||
/// <param name="lhs">The left hand side vector.</param>
|
||||
/// <param name="rhs">The right hand side vector.</param>
|
||||
/// <returns>If not equals.</returns>
|
||||
public static bool operator !=(Vector4i lhs, Vector4i rhs)
|
||||
{
|
||||
return (lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.z || lhs.w != rhs.w);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Explicitly converts from a single-precision vector into an integer vector.
|
||||
/// </summary>
|
||||
/// <param name="v">The single-precision vector.</param>
|
||||
public static explicit operator Vector4i(Vector4 v)
|
||||
{
|
||||
return new Vector4i((int)v.x, (int)v.y, (int)v.z, (int)v.w);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Explicitly converts from a double-precision vector into an integer vector.
|
||||
/// </summary>
|
||||
/// <param name="v">The double-precision vector.</param>
|
||||
public static explicit operator Vector4i(Vector4d v)
|
||||
{
|
||||
return new Vector4i((int)v.x, (int)v.y, (int)v.z, (int)v.w);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
#region Instance
|
||||
/// <summary>
|
||||
/// Set x, y and z components of an existing vector.
|
||||
/// </summary>
|
||||
/// <param name="x">The x value.</param>
|
||||
/// <param name="y">The y value.</param>
|
||||
/// <param name="z">The z value.</param>
|
||||
/// <param name="w">The w value.</param>
|
||||
public void Set(int x, int y, int z, int w)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies with another vector component-wise.
|
||||
/// </summary>
|
||||
/// <param name="scale">The vector to multiply with.</param>
|
||||
public void Scale(ref Vector4i scale)
|
||||
{
|
||||
x *= scale.x;
|
||||
y *= scale.y;
|
||||
z *= scale.z;
|
||||
w *= scale.w;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps this vector between a specific range.
|
||||
/// </summary>
|
||||
/// <param name="min">The minimum component value.</param>
|
||||
/// <param name="max">The maximum component value.</param>
|
||||
public void Clamp(int min, int 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;
|
||||
|
||||
if (w < min) w = min;
|
||||
else if (w > max) w = max;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Object
|
||||
/// <summary>
|
||||
/// Returns a hash code for this vector.
|
||||
/// </summary>
|
||||
/// <returns>The hash code.</returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2 ^ w.GetHashCode() >> 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if this vector is equal to another one.
|
||||
/// </summary>
|
||||
/// <param name="other">The other vector to compare to.</param>
|
||||
/// <returns>If equals.</returns>
|
||||
public override bool Equals(object other)
|
||||
{
|
||||
if (!(other is Vector4i))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Vector4i vector = (Vector4i)other;
|
||||
return (x == vector.x && y == vector.y && z == vector.z && w == vector.w);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if this vector is equal to another one.
|
||||
/// </summary>
|
||||
/// <param name="other">The other vector to compare to.</param>
|
||||
/// <returns>If equals.</returns>
|
||||
public bool Equals(Vector4i other)
|
||||
{
|
||||
return (x == other.x && y == other.y && z == other.z && w == other.w);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a nicely formatted string for this vector.
|
||||
/// </summary>
|
||||
/// <returns>The string.</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("({0}, {1}, {2}, {3})",
|
||||
x.ToString(CultureInfo.InvariantCulture),
|
||||
y.ToString(CultureInfo.InvariantCulture),
|
||||
z.ToString(CultureInfo.InvariantCulture),
|
||||
w.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a nicely formatted string for this vector.
|
||||
/// </summary>
|
||||
/// <param name="format">The integer format.</param>
|
||||
/// <returns>The string.</returns>
|
||||
public string ToString(string format)
|
||||
{
|
||||
return string.Format("({0}, {1}, {2}, {3})",
|
||||
x.ToString(format, CultureInfo.InvariantCulture),
|
||||
y.ToString(format, CultureInfo.InvariantCulture),
|
||||
z.ToString(format, CultureInfo.InvariantCulture),
|
||||
w.ToString(format, CultureInfo.InvariantCulture));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Static
|
||||
/// <summary>
|
||||
/// Multiplies two vectors component-wise.
|
||||
/// </summary>
|
||||
/// <param name="a">The first vector.</param>
|
||||
/// <param name="b">The second vector.</param>
|
||||
/// <param name="result">The resulting vector.</param>
|
||||
public static void Scale(ref Vector4i a, ref Vector4i b, out Vector4i result)
|
||||
{
|
||||
result = new Vector4i(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user