#region License /* MIT License Copyright(c) 2017-2020 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 System.Runtime.InteropServices; namespace UnityMeshSimplifier { /// /// A symmetric matrix. /// [StructLayout(LayoutKind.Sequential)] public struct SymmetricMatrix { #region Fields /// /// The m11 component. /// public double m0; /// /// The m12 component. /// public double m1; /// /// The m13 component. /// public double m2; /// /// The m14 component. /// public double m3; /// /// The m22 component. /// public double m4; /// /// The m23 component. /// public double m5; /// /// The m24 component. /// public double m6; /// /// The m33 component. /// public double m7; /// /// The m34 component. /// public double m8; /// /// The m44 component. /// public double m9; #endregion #region Properties /// /// Gets the component value with a specific index. /// /// The component index. /// The value. public double this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { switch (index) { case 0: return m0; case 1: return m1; case 2: return m2; case 3: return m3; case 4: return m4; case 5: return m5; case 6: return m6; case 7: return m7; case 8: return m8; case 9: return m9; default: throw new ArgumentOutOfRangeException(nameof(index)); } } } #endregion #region Constructor /// /// Creates a symmetric matrix with a value in each component. /// /// The component value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public SymmetricMatrix(double c) { this.m0 = c; this.m1 = c; this.m2 = c; this.m3 = c; this.m4 = c; this.m5 = c; this.m6 = c; this.m7 = c; this.m8 = c; this.m9 = c; } /// /// Creates a symmetric matrix. /// /// The m11 component. /// The m12 component. /// The m13 component. /// The m14 component. /// The m22 component. /// The m23 component. /// The m24 component. /// The m33 component. /// The m34 component. /// The m44 component. [MethodImpl(MethodImplOptions.AggressiveInlining)] public SymmetricMatrix(double m0, double m1, double m2, double m3, double m4, double m5, double m6, double m7, double m8, double m9) { this.m0 = m0; this.m1 = m1; this.m2 = m2; this.m3 = m3; this.m4 = m4; this.m5 = m5; this.m6 = m6; this.m7 = m7; this.m8 = m8; this.m9 = m9; } /// /// Creates a symmetric matrix from a plane. /// /// The plane x-component. /// The plane y-component /// The plane z-component /// The plane w-component [MethodImpl(MethodImplOptions.AggressiveInlining)] public SymmetricMatrix(double a, double b, double c, double d) { this.m0 = a * a; this.m1 = a * b; this.m2 = a * c; this.m3 = a * d; this.m4 = b * b; this.m5 = b * c; this.m6 = b * d; this.m7 = c * c; this.m8 = c * d; this.m9 = d * d; } #endregion #region Operators /// /// Adds two matrixes together. /// /// The left hand side. /// The right hand side. /// The resulting matrix. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static SymmetricMatrix operator +(SymmetricMatrix a, SymmetricMatrix b) { return new SymmetricMatrix( a.m0 + b.m0, a.m1 + b.m1, a.m2 + b.m2, a.m3 + b.m3, a.m4 + b.m4, a.m5 + b.m5, a.m6 + b.m6, a.m7 + b.m7, a.m8 + b.m8, a.m9 + b.m9 ); } #endregion #region Internal Methods /// /// Determinant(0, 1, 2, 1, 4, 5, 2, 5, 7) /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] internal double Determinant1() { double det = m0 * m4 * m7 + m2 * m1 * m5 + m1 * m5 * m2 - m2 * m4 * m2 - m0 * m5 * m5 - m1 * m1 * m7; return det; } /// /// Determinant(1, 2, 3, 4, 5, 6, 5, 7, 8) /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] internal double Determinant2() { double det = m1 * m5 * m8 + m3 * m4 * m7 + m2 * m6 * m5 - m3 * m5 * m5 - m1 * m6 * m7 - m2 * m4 * m8; return det; } /// /// Determinant(0, 2, 3, 1, 5, 6, 2, 7, 8) /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] internal double Determinant3() { double det = m0 * m5 * m8 + m3 * m1 * m7 + m2 * m6 * m2 - m3 * m5 * m2 - m0 * m6 * m7 - m2 * m1 * m8; return det; } /// /// Determinant(0, 1, 3, 1, 4, 6, 2, 5, 8) /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] internal double Determinant4() { double det = m0 * m4 * m8 + m3 * m1 * m5 + m1 * m6 * m2 - m3 * m4 * m2 - m0 * m6 * m5 - m1 * m1 * m8; return det; } #endregion #region Public Methods /// /// Computes the determinant of this matrix. /// /// The a11 index. /// The a12 index. /// The a13 index. /// The a21 index. /// The a22 index. /// The a23 index. /// The a31 index. /// The a32 index. /// The a33 index. /// The determinant value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public double Determinant(int a11, int a12, int a13, int a21, int a22, int a23, int a31, int a32, int a33) { double det = this[a11] * this[a22] * this[a33] + this[a13] * this[a21] * this[a32] + this[a12] * this[a23] * this[a31] - this[a13] * this[a22] * this[a31] - this[a11] * this[a23] * this[a32] - this[a12] * this[a21] * this[a33]; return det; } #endregion } }