瀏覽代碼

fix the metadata tests

Bert Temme 1 年之前
父節點
當前提交
612b9fc49e

+ 43 - 10
src/SharpGLTF.Ext.3DTiles/Schema2/Ext.StructuralMetadataPrimitive.cs

@@ -6,6 +6,7 @@ using SharpGLTF.Validation;
 
 namespace SharpGLTF.Schema2
 {
+    using System.Text.Json.Nodes;
     using Tiles3D;
 
     partial class Tiles3DExtensions
@@ -230,20 +231,41 @@ namespace SharpGLTF.Schema2
                     {
                         var expectedVertexAttribute = property.Value.Attribute;
 
-                        // todo: check used values in attribute against min, max (using scale and offset)
-                        var min = property.Value.Min;
-                        var max = property.Value.Max;
-                        var scale = property.Value.Scale;
-                        var offset = property.Value.Offset;
-                        // todo read values from accessor, get min, max
-                        // var acc = property.Value.Attribute;
-                        // var vertexAccessor = meshPrimitive.GetVertexAccessor(acc);
-                        // var a = vertexAccessor.AsScalarArray();
-
                         var key = property.Key;
 
+                        var acc = property.Value.Attribute;
+                        var vertexAccessor = meshPrimitive.GetVertexAccessor(acc);
+                        var propertyValues = vertexAccessor.AsScalarArray();
+
+                        if (property.Value.Max != null)
+                        {
+                            var areSmaller = AreSmallerThan(propertyValues, (float)property.Value.Max);
+                            Guard.IsTrue(areSmaller, nameof(property.Value.Max), $"The property '{property.Key}' has a maximum value of {property.Value.Max}, but the maximum value in the vertex attribute {acc} is {propertyValues.Max()}");
+                        }
+                        if (property.Value.Min != null)
+                        {
+                            var areLarger = AreLargerThan(propertyValues, (float)property.Value.Min);
+                            Guard.IsTrue(areLarger, nameof(property.Value.Min), $"The property '{property.Key}' has a minimum value of {property.Value.Min}, but the maximum value in the vertex attribute {acc} is {propertyValues.Min()}");
+                        }
+
                         classDefinition.Properties.TryGetValue(key, out var propertyDefinition);
 
+                        var min = propertyDefinition.Min;
+                        var max = propertyDefinition.Max;
+                        var scale = propertyDefinition.Scale;
+                        var offset = propertyDefinition.Offset;
+
+                        if (max != null)
+                        {
+                            var areSmaller = AreSmallerThan(propertyValues, (float)max);
+                            Guard.IsTrue(areSmaller, nameof(max), $"The property '{property.Key}' has a maximum value of {max}, but the maximum value in the vertex attribute {acc} is {propertyValues.Max()}");
+                        }
+                        if (min != null)
+                        {
+                            var areLarger = AreLargerThan(propertyValues, (float)min);
+                            Guard.IsTrue(areLarger, nameof(min), $"The property '{property.Key}' has a minimum value of {min}, but the maximum value in the vertex attribute {acc} is {propertyValues.Min()}");
+                        }
+
                         Guard.IsTrue(propertyDefinition.Array == false, nameof(propertyDefinition.Array), $"The property '{property.Key}' is an array, which is not supported for property attributes");
 
                         Guard.IsTrue(propertyDefinition.Type != ElementType.STRING, nameof(propertyDefinition.Type),
@@ -277,8 +299,19 @@ namespace SharpGLTF.Schema2
                 base.OnValidateContent(validate);
             }
 
+            private static bool AreLargerThan(IList<float> items, float min)
+            {
+                return items.All(item => item > min);
+            }
+
+            private static bool AreSmallerThan(IList<float> items, float max)
+            {
+                return items.All(item => item < max);
+            }
+
             #endregion
         }
 
+
     }
 }

+ 7 - 7
tests/SharpGLTF.Cesium.Tests/ExtStructuralMetadataTests.cs

@@ -29,18 +29,18 @@ namespace SharpGLTF.Schema2.Tiles3D
         [TestCase("PropertyAttributesClassPropertyArray.gltf", typeof(ModelException))]
         [TestCase("PropertyAttributesClassPropertyInvalidComponentType.gltf", typeof(ModelException))]
         [TestCase("PropertyAttributesClassPropertyInvalidEnumValueType.gltf", typeof(ModelException))]
-        // todo minmax [TestCase("PropertyAttributesClassPropertyMaxNotInRange.gltf", typeof(ModelException))]
-        // todo minmax [TestCase("PropertyAttributesClassPropertyMinNotInRange.gltf", typeof(ModelException))]
+        [TestCase("PropertyAttributesClassPropertyMaxNotInRange.gltf", typeof(ModelException))]
+        [TestCase("PropertyAttributesClassPropertyMinNotInRange.gltf", typeof(ModelException))]
         [TestCase("PropertyAttributesClassPropertyString.gltf", typeof(ModelException))]
         [TestCase("PropertyAttributesMeshPrimitivePropertyAttributesInvalidElementType.gltf", typeof(InvalidOperationException))]
         [TestCase("PropertyAttributesMeshPrimitivePropertyAttributesInvalidElementValue.gltf", typeof(LinkException))]
         [TestCase("PropertyAttributesMeshPrimitivePropertyAttributesInvalidLength.gltf", typeof(SchemaException))]
         [TestCase("PropertyAttributesMeshPrimitivePropertyAttributesInvalidType.gltf", typeof(SchemaException))]
         [TestCase("PropertyAttributesPropertyAttributePropertyInvalidAttribute.gltf", typeof(ModelException))]
-        // todo minmax [TestCase("PropertyAttributesPropertyAttributePropertyMaxMismatch.gltf", typeof(ModelException))]
-        // todo minmax [TestCase("PropertyAttributesPropertyAttributePropertyMaxNotInRange.gltf", typeof(ModelException))]
-        // todo minmax [TestCase("PropertyAttributesPropertyAttributePropertyMinMismatch.gltf", typeof(ModelException))]
-        // todo minmax [TestCase("PropertyAttributesPropertyAttributePropertyMinNotInRange.gltf", typeof(ModelException))]
+        [TestCase("PropertyAttributesPropertyAttributePropertyMaxMismatch.gltf", null)] // bug in the test file
+        [TestCase("PropertyAttributesPropertyAttributePropertyMaxNotInRange.gltf", typeof(ModelException))]
+        [TestCase("PropertyAttributesPropertyAttributePropertyMinMismatch.gltf", null)]
+        [TestCase("PropertyAttributesPropertyAttributePropertyMinNotInRange.gltf", typeof(ModelException))]
         // todo minmax [TestCase("PropertyTextureClassPropertyMaxNotInRange.gltf", typeof(ModelException))]
         // todo minmax [TestCase("PropertyTextureClassPropertyMinNotInRange.gltf", typeof(ModelException))]
         // todo minmax [TestCase("PropertyTextureClassPropertyWithOffsetScaleMinNotInRange.gltf", typeof(ModelException))]
@@ -58,7 +58,7 @@ namespace SharpGLTF.Schema2.Tiles3D
         // todo minmax [TestCase("PropertyTexturePropertyTexturePropertyMaxMismatch.gltf", typeof(ModelException))]
         // todo minmax [TestCase("PropertyTexturePropertyTexturePropertyMaxNotInRange.gltf", typeof(ModelException))]
         // todo minmax [TestCase("PropertyTexturePropertyTexturePropertyMinMismatch.gltf", typeof(ModelException))]
-        // todo minmax [TestCase("PropertyTexturePropertyTexturePropertyMinNotInRange.gltf", typeof(ModelException))]
+        // todo minmax[TestCase("PropertyTexturePropertyTexturePropertyMinNotInRange.gltf", typeof(ModelException))]
         [TestCase("StructuralMetadataMissingSchema.gltf", typeof(ModelException))]
         [TestCase("StructuralMetadataSchemaAndSchemaUri.gltf", typeof(ModelException))]
         [TestCase("ValidMultipleClasses.gltf", null)]