#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 MeshDecimator.Math; namespace MeshDecimator { /// /// A bone weight. /// public struct BoneWeight : IEquatable { #region Fields /// /// The first bone index. /// public int boneIndex0; /// /// The second bone index. /// public int boneIndex1; /// /// The third bone index. /// public int boneIndex2; /// /// The fourth bone index. /// public int boneIndex3; /// /// The first bone weight. /// public float boneWeight0; /// /// The second bone weight. /// public float boneWeight1; /// /// The third bone weight. /// public float boneWeight2; /// /// The fourth bone weight. /// public float boneWeight3; #endregion #region Constructor /// /// Creates a new bone weight. /// /// The first bone index. /// The second bone index. /// The third bone index. /// The fourth bone index. /// The first bone weight. /// The second bone weight. /// The third bone weight. /// The fourth bone weight. public BoneWeight(int boneIndex0, int boneIndex1, int boneIndex2, int boneIndex3, float boneWeight0, float boneWeight1, float boneWeight2, float boneWeight3) { this.boneIndex0 = boneIndex0; this.boneIndex1 = boneIndex1; this.boneIndex2 = boneIndex2; this.boneIndex3 = boneIndex3; this.boneWeight0 = boneWeight0; this.boneWeight1 = boneWeight1; this.boneWeight2 = boneWeight2; this.boneWeight3 = boneWeight3; } #endregion #region Operators /// /// Returns if two bone weights equals eachother. /// /// The left hand side bone weight. /// The right hand side bone weight. /// If equals. public static bool operator ==(BoneWeight lhs, BoneWeight rhs) { return (lhs.boneIndex0 == rhs.boneIndex0 && lhs.boneIndex1 == rhs.boneIndex1 && lhs.boneIndex2 == rhs.boneIndex2 && lhs.boneIndex3 == rhs.boneIndex3 && new Vector4(lhs.boneWeight0, lhs.boneWeight1, lhs.boneWeight2, lhs.boneWeight3) == new Vector4(rhs.boneWeight0, rhs.boneWeight1, rhs.boneWeight2, rhs.boneWeight3)); } /// /// Returns if two bone weights don't equal eachother. /// /// The left hand side bone weight. /// The right hand side bone weight. /// If not equals. public static bool operator !=(BoneWeight lhs, BoneWeight rhs) { return !(lhs == rhs); } #endregion #region Private Methods private void MergeBoneWeight(int boneIndex, float weight) { if (boneIndex == boneIndex0) { boneWeight0 = (boneWeight0 + weight) * 0.5f; } else if (boneIndex == boneIndex1) { boneWeight1 = (boneWeight1 + weight) * 0.5f; } else if (boneIndex == boneIndex2) { boneWeight2 = (boneWeight2 + weight) * 0.5f; } else if (boneIndex == boneIndex3) { boneWeight3 = (boneWeight3 + weight) * 0.5f; } else if(boneWeight0 == 0f) { boneIndex0 = boneIndex; boneWeight0 = weight; } else if (boneWeight1 == 0f) { boneIndex1 = boneIndex; boneWeight1 = weight; } else if (boneWeight2 == 0f) { boneIndex2 = boneIndex; boneWeight2 = weight; } else if (boneWeight3 == 0f) { boneIndex3 = boneIndex; boneWeight3 = weight; } Normalize(); } private void Normalize() { float mag = (float)System.Math.Sqrt(boneWeight0 * boneWeight0 + boneWeight1 * boneWeight1 + boneWeight2 * boneWeight2 + boneWeight3 * boneWeight3); if (mag > float.Epsilon) { boneWeight0 /= mag; boneWeight1 /= mag; boneWeight2 /= mag; boneWeight3 /= mag; } else { boneWeight0 = boneWeight1 = boneWeight2 = boneWeight3 = 0f; } } #endregion #region Public Methods #region Object /// /// Returns a hash code for this vector. /// /// The hash code. public override int GetHashCode() { return boneIndex0.GetHashCode() ^ boneIndex1.GetHashCode() << 2 ^ boneIndex2.GetHashCode() >> 2 ^ boneIndex3.GetHashCode() >> 1 ^ boneWeight0.GetHashCode() << 5 ^ boneWeight1.GetHashCode() << 4 ^ boneWeight2.GetHashCode() >> 4 ^ boneWeight3.GetHashCode() >> 3; } /// /// Returns if this bone weight is equal to another object. /// /// The other object to compare to. /// If equals. public override bool Equals(object obj) { if (!(obj is BoneWeight)) { return false; } BoneWeight other = (BoneWeight)obj; return (boneIndex0 == other.boneIndex0 && boneIndex1 == other.boneIndex1 && boneIndex2 == other.boneIndex2 && boneIndex3 == other.boneIndex3 && boneWeight0 == other.boneWeight0 && boneWeight1 == other.boneWeight1 && boneWeight2 == other.boneWeight2 && boneWeight3 == other.boneWeight3); } /// /// Returns if this bone weight is equal to another one. /// /// The other bone weight to compare to. /// If equals. public bool Equals(BoneWeight other) { return (boneIndex0 == other.boneIndex0 && boneIndex1 == other.boneIndex1 && boneIndex2 == other.boneIndex2 && boneIndex3 == other.boneIndex3 && boneWeight0 == other.boneWeight0 && boneWeight1 == other.boneWeight1 && boneWeight2 == other.boneWeight2 && boneWeight3 == other.boneWeight3); } /// /// Returns a nicely formatted string for this bone weight. /// /// The string. public override string ToString() { return string.Format("({0}:{4:F1}, {1}:{5:F1}, {2}:{6:F1}, {3}:{7:F1})", boneIndex0, boneIndex1, boneIndex2, boneIndex3, boneWeight0, boneWeight1, boneWeight2, boneWeight3); } #endregion #region Static /// /// Merges two bone weights and stores the merged result in the first parameter. /// /// The first bone weight, also stores result. /// The second bone weight. public static void Merge(ref BoneWeight a, ref BoneWeight b) { if (b.boneWeight0 > 0f) a.MergeBoneWeight(b.boneIndex0, b.boneWeight0); if (b.boneWeight1 > 0f) a.MergeBoneWeight(b.boneIndex1, b.boneWeight1); if (b.boneWeight2 > 0f) a.MergeBoneWeight(b.boneIndex2, b.boneWeight2); if (b.boneWeight3 > 0f) a.MergeBoneWeight(b.boneIndex3, b.boneWeight3); } #endregion #endregion } }