Browse Source

Updated matrix validation so node transforms are fully validated, while IBMs only validate matrix inversion.

Vicente Penades 5 years ago
parent
commit
d45d96a300

+ 3 - 3
src/Shared/_Extensions.cs

@@ -177,11 +177,11 @@ namespace SharpGLTF
             return dst;
             return dst;
         }
         }
 
 
-        internal static bool IsValid(this in Matrix4x4 matrix)
+        internal static bool IsValid(this in Matrix4x4 matrix, bool mustDecompose = true, bool mustInvert = true)
         {
         {
             if (!matrix._IsFinite()) return false;
             if (!matrix._IsFinite()) return false;
-            if (!Matrix4x4.Decompose(matrix, out _, out _, out _)) return false;
-            if (!Matrix4x4.Invert(matrix, out _)) return false;
+            if (mustDecompose && !Matrix4x4.Decompose(matrix, out _, out _, out _)) return false;
+            if (mustInvert && !Matrix4x4.Invert(matrix, out _)) return false;
 
 
             return true;
             return true;
         }
         }

+ 0 - 1
src/SharpGLTF.Core/Memory/MemoryImage.cs

@@ -158,7 +158,6 @@ namespace SharpGLTF.Memory
                     src.CopyTo(dst);
                     src.CopyTo(dst);
                 }
                 }
             }
             }
-
         }
         }
 
 
         /// <summary>
         /// <summary>

+ 2 - 2
src/SharpGLTF.Core/Schema2/gltf.Accessors.cs

@@ -494,7 +494,7 @@ namespace SharpGLTF.Schema2
             validate.IsAnyOf(nameof(Format), Format, (DimensionType.VEC4, EncodingType.UNSIGNED_BYTE, true), (DimensionType.VEC4, EncodingType.UNSIGNED_SHORT, true), DimensionType.VEC4);
             validate.IsAnyOf(nameof(Format), Format, (DimensionType.VEC4, EncodingType.UNSIGNED_BYTE, true), (DimensionType.VEC4, EncodingType.UNSIGNED_SHORT, true), DimensionType.VEC4);
         }
         }
 
 
-        internal void ValidateMatrices(VALIDATIONCTX validate)
+        internal void ValidateMatrices(VALIDATIONCTX validate, bool mustDecompose = true, bool mustInvert = true)
         {
         {
             validate = validate.GetContext(this);
             validate = validate.GetContext(this);
 
 
@@ -506,7 +506,7 @@ namespace SharpGLTF.Schema2
 
 
             for (int i = 0; i < matrices.Count; ++i)
             for (int i = 0; i < matrices.Count; ++i)
             {
             {
-                validate.IsNullOrMatrix("Matrices", matrices[i]);
+                validate.IsNullOrMatrix("Matrices", matrices[i], mustDecompose, mustInvert);
             }
             }
         }
         }
 
 

+ 1 - 1
src/SharpGLTF.Core/Schema2/gltf.Node.cs

@@ -485,7 +485,7 @@ namespace SharpGLTF.Schema2
                 .IsPosition("Scale", _scale.AsValue(Vector3.One))
                 .IsPosition("Scale", _scale.AsValue(Vector3.One))
                 .IsRotation("Rotation", _rotation.AsValue(Quaternion.Identity))
                 .IsRotation("Rotation", _rotation.AsValue(Quaternion.Identity))
                 .IsPosition("Translation", _translation.AsValue(Vector3.Zero))
                 .IsPosition("Translation", _translation.AsValue(Vector3.Zero))
-                .IsNullOrMatrix("Rotation", _matrix);
+                .IsNullOrMatrix("Rotation", _matrix, true, true);
         }
         }
 
 
         private static void _ValidateMeshAndSkin(Validation.ValidationContext validate, Mesh mesh, Skin skin, List<Double> weights)
         private static void _ValidateMeshAndSkin(Validation.ValidationContext validate, Mesh mesh, Skin skin, List<Double> weights)

+ 1 - 1
src/SharpGLTF.Core/Schema2/gltf.Skin.cs

@@ -275,7 +275,7 @@ namespace SharpGLTF.Schema2
             {
             {
                 validate.AreEqual("InverseBindMatrices", _joints.Count, ibxAccessor.Count);
                 validate.AreEqual("InverseBindMatrices", _joints.Count, ibxAccessor.Count);
 
 
-                ibxAccessor.ValidateMatrices(validate);
+                ibxAccessor.ValidateMatrices(validate, false, true);
             }
             }
 
 
             if (_skeleton.HasValue)
             if (_skeleton.HasValue)

+ 17 - 16
src/SharpGLTF.Core/Transforms/Matrix4x4Double.cs

@@ -7,6 +7,8 @@ using System.Text;
 
 
 namespace SharpGLTF.Transforms
 namespace SharpGLTF.Transforms
 {
 {
+    #pragma warning disable SA1407 // Arithmetic expressions should declare precedence
+
     // stripped from https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.cs
     // stripped from https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.cs
 
 
     [StructLayout(LayoutKind.Sequential)]
     [StructLayout(LayoutKind.Sequential)]
@@ -253,85 +255,81 @@ namespace SharpGLTF.Transforms
         /// <summary>
         /// <summary>
         /// Value at row 1, column 1 of the matrix.
         /// Value at row 1, column 1 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M11;
         public Double M11;
+
         /// <summary>
         /// <summary>
         /// Value at row 1, column 2 of the matrix.
         /// Value at row 1, column 2 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M12;
         public Double M12;
+
         /// <summary>
         /// <summary>
         /// Value at row 1, column 3 of the matrix.
         /// Value at row 1, column 3 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M13;
         public Double M13;
+
         /// <summary>
         /// <summary>
         /// Value at row 1, column 4 of the matrix.
         /// Value at row 1, column 4 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M14;
         public Double M14;
 
 
         /// <summary>
         /// <summary>
         /// Value at row 2, column 1 of the matrix.
         /// Value at row 2, column 1 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M21;
         public Double M21;
+
         /// <summary>
         /// <summary>
         /// Value at row 2, column 2 of the matrix.
         /// Value at row 2, column 2 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M22;
         public Double M22;
+
         /// <summary>
         /// <summary>
         /// Value at row 2, column 3 of the matrix.
         /// Value at row 2, column 3 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M23;
         public Double M23;
+
         /// <summary>
         /// <summary>
         /// Value at row 2, column 4 of the matrix.
         /// Value at row 2, column 4 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M24;
         public Double M24;
 
 
         /// <summary>
         /// <summary>
         /// Value at row 3, column 1 of the matrix.
         /// Value at row 3, column 1 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M31;
         public Double M31;
+
         /// <summary>
         /// <summary>
         /// Value at row 3, column 2 of the matrix.
         /// Value at row 3, column 2 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M32;
         public Double M32;
+
         /// <summary>
         /// <summary>
         /// Value at row 3, column 3 of the matrix.
         /// Value at row 3, column 3 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M33;
         public Double M33;
+
         /// <summary>
         /// <summary>
         /// Value at row 3, column 4 of the matrix.
         /// Value at row 3, column 4 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M34;
         public Double M34;
 
 
         /// <summary>
         /// <summary>
         /// Value at row 4, column 1 of the matrix.
         /// Value at row 4, column 1 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M41;
         public Double M41;
+
         /// <summary>
         /// <summary>
         /// Value at row 4, column 2 of the matrix.
         /// Value at row 4, column 2 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M42;
         public Double M42;
+
         /// <summary>
         /// <summary>
         /// Value at row 4, column 3 of the matrix.
         /// Value at row 4, column 3 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M43;
         public Double M43;
+
         /// <summary>
         /// <summary>
         /// Value at row 4, column 4 of the matrix.
         /// Value at row 4, column 4 of the matrix.
         /// </summary>
         /// </summary>
-        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
         public Double M44;
         public Double M44;
 
 
         /// <summary>
         /// <summary>
@@ -610,6 +608,7 @@ namespace SharpGLTF.Transforms
             Matrix4x4Double m;
             Matrix4x4Double m;
 
 
             // First row
             // First row
+
             m.M11 = value1.M11 * value2.M11 + value1.M12 * value2.M21 + value1.M13 * value2.M31 + value1.M14 * value2.M41;
             m.M11 = value1.M11 * value2.M11 + value1.M12 * value2.M21 + value1.M13 * value2.M31 + value1.M14 * value2.M41;
             m.M12 = value1.M11 * value2.M12 + value1.M12 * value2.M22 + value1.M13 * value2.M32 + value1.M14 * value2.M42;
             m.M12 = value1.M11 * value2.M12 + value1.M12 * value2.M22 + value1.M13 * value2.M32 + value1.M14 * value2.M42;
             m.M13 = value1.M11 * value2.M13 + value1.M12 * value2.M23 + value1.M13 * value2.M33 + value1.M14 * value2.M43;
             m.M13 = value1.M11 * value2.M13 + value1.M12 * value2.M23 + value1.M13 * value2.M33 + value1.M14 * value2.M43;
@@ -638,4 +637,6 @@ namespace SharpGLTF.Transforms
 
 
         #endregion
         #endregion
     }
     }
+
+    #pragma warning restore SA1407 // Arithmetic expressions should declare precedence
 }
 }

+ 0 - 1
src/SharpGLTF.Core/Transforms/SparseWeight8.cs

@@ -218,7 +218,6 @@ namespace SharpGLTF.Transforms
             Weight7 = sparse.Weight7 * scale;
             Weight7 = sparse.Weight7 * scale;
         }
         }
 
 
-
         internal static (SparseWeight8 TangentIn, SparseWeight8 Value, SparseWeight8 TangentOut) AsTuple(float[] tangentIn, float[] value, float[] tangentOut)
         internal static (SparseWeight8 TangentIn, SparseWeight8 Value, SparseWeight8 TangentOut) AsTuple(float[] tangentIn, float[] value, float[] tangentOut)
         {
         {
             return (Create(tangentIn), Create(value), Create(tangentOut));
             return (Create(tangentIn), Create(value), Create(tangentOut));

+ 4 - 4
src/SharpGLTF.Core/Validation/ValidationContext.Guards.cs

@@ -235,10 +235,10 @@ namespace SharpGLTF.Validation
             return this;
             return this;
         }
         }
 
 
-        public OUTTYPE IsNullOrMatrix(PARAMNAME pname, System.Numerics.Matrix4x4? matrix)
+        public OUTTYPE IsNullOrMatrix(PARAMNAME pname, System.Numerics.Matrix4x4? matrix, bool mustDecompose = true, bool mustInvert = true)
         {
         {
             if (!matrix.HasValue) return this;
             if (!matrix.HasValue) return this;
-            return IsMatrix(pname, matrix.Value);
+            return IsMatrix(pname, matrix.Value, mustDecompose, mustInvert);
         }
         }
 
 
         public OUTTYPE IsPosition(PARAMNAME pname, in System.Numerics.Vector3 position)
         public OUTTYPE IsPosition(PARAMNAME pname, in System.Numerics.Vector3 position)
@@ -259,9 +259,9 @@ namespace SharpGLTF.Validation
             return this;
             return this;
         }
         }
 
 
-        public OUTTYPE IsMatrix(PARAMNAME pname, in System.Numerics.Matrix4x4 matrix)
+        public OUTTYPE IsMatrix(PARAMNAME pname, in System.Numerics.Matrix4x4 matrix, bool mustDecompose = true, bool mustInvert = true)
         {
         {
-            if (!matrix.IsValid()) _DataThrow(pname, "Invalid Matrix");
+            if (!matrix.IsValid(mustDecompose, mustInvert)) _DataThrow(pname, "Invalid Matrix");
             return this;
             return this;
         }
         }