Browse Source

refactor validation

Bert Temme 2 years ago
parent
commit
72a56bedee

+ 9 - 1
src/SharpGLTF.Cesium/Schema2/Ext.InstanceFeatures.cs

@@ -26,13 +26,19 @@ namespace SharpGLTF.Schema2
             }
         }
 
-        protected override void OnValidateContent(ValidationContext validate)
+        protected override void OnValidateReferences(ValidationContext validate)
         {
             var extInstanceFeatures = _node.GetExtension<MeshExtInstanceFeatures>();
             validate.NotNull(nameof(extInstanceFeatures), extInstanceFeatures);
             var extMeshGpInstancing = _node.GetExtension<MeshGpuInstancing>();
             validate.NotNull(nameof(extMeshGpInstancing), extMeshGpInstancing);
 
+            base.OnValidateReferences(validate);
+        }
+
+        protected override void OnValidateContent(ValidationContext validate)
+        {
+            var extInstanceFeatures = _node.GetExtension<MeshExtInstanceFeatures>();
             validate.NotNull(nameof(FeatureIds), extInstanceFeatures.FeatureIds);
             validate.IsTrue(nameof(FeatureIds), extInstanceFeatures.FeatureIds.Count > 0, "Instance FeatureIds has items");
 
@@ -97,6 +103,8 @@ namespace SharpGLTF.Schema2
             var extMeshGpInstancing = node.Extensions.Where(item => item is MeshGpuInstancing).FirstOrDefault();
             Guard.NotNull(extMeshGpInstancing, nameof(extMeshGpInstancing));
 
+            // todo move validate in the validation function
+
             foreach (var instanceFeatureId in instanceFeatureIds)
             {
                 ValidateInstanceFeatureId(node, instanceFeatureId);

+ 57 - 45
src/SharpGLTF.Cesium/Schema2/Ext.MeshFeatures.cs

@@ -24,15 +24,67 @@ namespace SharpGLTF.Schema2
             }
         }
 
+        protected override void OnValidateReferences(ValidationContext validate)
+        {
+            foreach (var featureId in _featureIds)
+            {
+                if (featureId.Attribute.HasValue)
+                {
+                    var expectedVertexAttribute = $"_FEATURE_ID_{featureId.Attribute}";
+                    Guard.NotNull(_meshPrimitive.GetVertexAccessor(expectedVertexAttribute), expectedVertexAttribute);
+                }
+                if (featureId.PropertyTable.HasValue)
+                {
+                    var metadataExtension = _meshPrimitive.LogicalParent.LogicalParent.GetExtension<EXTStructuralMetadataRoot>();
+                    Guard.NotNull(metadataExtension, nameof(metadataExtension), "EXT_Structural_Metadata extension is not found.");
+                    Guard.NotNull(metadataExtension.PropertyTables[featureId.PropertyTable.Value], nameof(featureId.PropertyTable), $"Property table index {featureId.PropertyTable.Value} does not exist");
+                }
+                if (featureId.Texture != null)
+                {
+                    var expectedTexCoordAttribute = $"TEXCOORD_{featureId.Texture.TextureCoordinate}";
+                    Guard.NotNull(_meshPrimitive.GetVertexAccessor(expectedTexCoordAttribute), expectedTexCoordAttribute);
+
+                    var modelRoot = _meshPrimitive.LogicalParent.LogicalParent;
+                    validate.IsNullOrIndex(nameof(featureId.Texture), featureId.Texture.TextureCoordinate, modelRoot.LogicalTextures);
+                }
+            }
+
+            base.OnValidateReferences(validate);
+        }
+
+
         protected override void OnValidateContent(ValidationContext validate)
         {
             var extMeshFeatures = _meshPrimitive.Extensions.Where(item => item is MeshExtMeshFeatures).FirstOrDefault();
             validate.NotNull(nameof(extMeshFeatures), extMeshFeatures);
-            var ext = (MeshExtMeshFeatures)extMeshFeatures;
-            validate.NotNull(nameof(FeatureIds), ext.FeatureIds);
-            validate.IsTrue(nameof(FeatureIds), ext.FeatureIds.Count > 0, "FeatureIds has items");
+            validate.NotNull(nameof(FeatureIds), _featureIds);
+            validate.IsTrue(nameof(FeatureIds), _featureIds.Count > 0, "FeatureIds has items");
 
-            base.OnValidateContent(validate);
+            foreach (var featureId in _featureIds)
+            {
+                if (featureId.NullFeatureId.HasValue)
+                {
+                    Guard.MustBeGreaterThanOrEqualTo((int)featureId.NullFeatureId, 0, nameof(featureId.NullFeatureId));
+                }
+                if (featureId.Label != null)
+                {
+                    var regex = "^[a-zA-Z_][a-zA-Z0-9_]*$";
+                    Guard.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(featureId.Label, regex), nameof(featureId.Label));
+                }
+                if (featureId.Attribute.HasValue)
+                {
+                    Guard.MustBeGreaterThanOrEqualTo((int)featureId.Attribute, 0, nameof(featureId.Attribute));
+                }
+                if (featureId.PropertyTable.HasValue)
+                {
+                    Guard.MustBeGreaterThanOrEqualTo((int)featureId.PropertyTable, 0, nameof(featureId.PropertyTable));
+                }
+                if (featureId.Texture != null)
+                {
+                    Guard.MustBeGreaterThanOrEqualTo(featureId.Texture.TextureCoordinate, 0, nameof(featureId.Texture.TextureCoordinate));
+                }
+                base.OnValidateContent(validate);
+            }
         }
     }
 
@@ -121,49 +173,9 @@ namespace SharpGLTF.Schema2
         {
             if (featureIds == null || featureIds.Count == 0) { primitive.RemoveExtensions<MeshExtMeshFeatures>(); return; }
 
-            foreach (var featureId in featureIds)
-            {
-                ValidateFeature(primitive, featureId);
-            };
-
             var ext = primitive.UseExtension<MeshExtMeshFeatures>();
             ext.FeatureIds = featureIds;
         }
 
-        private static void ValidateFeature(MeshPrimitive primitive, MeshExtMeshFeatureID item)
-        {
-            Guard.MustBeGreaterThanOrEqualTo((int)item.FeatureCount, 1, nameof(item.FeatureCount));
-
-            if (item.NullFeatureId.HasValue)
-            {
-                Guard.MustBeGreaterThanOrEqualTo((int)item.NullFeatureId, 0, nameof(item.NullFeatureId));
-            }
-            if (item.Label != null)
-            {
-                var regex = "^[a-zA-Z_][a-zA-Z0-9_]*$";
-                Guard.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(item.Label, regex), nameof(item.Label));
-            }
-            if (item.Attribute.HasValue)
-            {
-                Guard.MustBeGreaterThanOrEqualTo((int)item.Attribute, 0, nameof(item.Attribute));
-                // Guard that the custom vertex attribute (_FEATURE_ID_{attribute}) exists when FeatureID has attribute set
-                var expectedVertexAttribute = $"_FEATURE_ID_{item.Attribute}";
-                Guard.NotNull(primitive.GetVertexAccessor(expectedVertexAttribute), expectedVertexAttribute);
-            }
-            if (item.PropertyTable.HasValue)
-            {
-                Guard.MustBeGreaterThanOrEqualTo((int)item.PropertyTable, 0, nameof(item.PropertyTable));
-                var metadataExtension = primitive.LogicalParent.LogicalParent.GetExtension<EXTStructuralMetadataRoot>();
-                Guard.NotNull(metadataExtension, nameof(metadataExtension), "EXT_Structural_Metadata extension is not found.");
-                Guard.NotNull(metadataExtension.PropertyTables[item.PropertyTable.Value], nameof(item.PropertyTable), $"Property table index {item.PropertyTable.Value} does not exist");
-            }
-            if (item.Texture != null)
-            {
-                Guard.MustBeGreaterThanOrEqualTo((int)item.Texture.TextureCoordinate, 0, nameof(item.Texture.TextureCoordinate));
-                var expectedTexCoordAttribute = $"TEXCOORD_{item.Texture.TextureCoordinate}";
-                Guard.NotNull(primitive.GetVertexAccessor(expectedTexCoordAttribute), expectedTexCoordAttribute);
-                Guard.NotNull(primitive.LogicalParent.LogicalParent.LogicalTextures[item.Texture.Index], nameof(primitive.LogicalParent.LogicalParent.LogicalTextures), $"Texture {item.Texture.Index} does not exist");
-            }
-        }
     }
-}
+}

+ 31 - 9
src/SharpGLTF.Cesium/Schema2/Ext.StructuralMetadataRoot.cs

@@ -190,12 +190,42 @@ OneOf<StructuralMetadataSchema, Uri> schema)
 
             foreach (var propertyTable in PropertyTables)
             {
+                Guard.NotNull(Schema.Classes[propertyTable.Class], nameof(propertyTable.Class), $"Schema must have class {propertyTable.Class}");
+
                 foreach (var property in propertyTable.Properties)
                 {
-                    Guard.IsTrue(Schema.Classes[propertyTable.Class].Properties.ContainsKey(property.Key), nameof(propertyTable.Properties), $"Property {property.Key} must be defined in schema");
+                    Guard.NotNull(Schema.Classes[propertyTable.Class].Properties[property.Key], nameof(property.Key), $"Schema must have property {property.Key}");
+
+                    var values = property.Value.Values;
+                    validate.IsNullOrIndex(nameof(propertyTable), values, modelRoot.LogicalBufferViews);
+
+                    if (property.Value.ArrayOffsets.HasValue)
+                    {
+                        var arrayOffsets = property.Value.ArrayOffsets.Value;
+                        validate.IsNullOrIndex(nameof(propertyTable), arrayOffsets, modelRoot.LogicalBufferViews);
+                    }
+
+                    if (property.Value.StringOffsets.HasValue)
+                    {
+                        var stringOffsets = property.Value.StringOffsets.Value;
+                        validate.IsNullOrIndex(nameof(propertyTable), stringOffsets, modelRoot.LogicalBufferViews);
+                    }
                 }
             }
 
+            if(Schema!= null)
+            {
+                foreach (var @class in Schema.Classes)
+                {
+                    foreach (var property in @class.Value.Properties)
+                    {
+                        if (property.Value.Type == ElementType.ENUM)
+                        {
+                            Guard.IsTrue(Schema.Enums.ContainsKey(property.Value.EnumType), nameof(property.Value.EnumType), $"Enum {property.Value.EnumType} must be defined in schema");
+                        }
+                    }
+                }
+            }
 
             base.OnValidateReferences(validate);
         }
@@ -233,14 +263,6 @@ OneOf<StructuralMetadataSchema, Uri> schema)
             Guard.IsFalse(Schema != null && SchemaUri != null, "Schema/SchemaUri", "Schema and SchemaUri cannot both be defined");
             Guard.IsFalse(Schema == null && SchemaUri == null, "Schema/SchemaUri", "One of Schema and SchemaUri must be defined");
 
-            
-            // check if the propertyTable id is set, then the propertyTable must be defined
-
-            // loop through all the primitive and check if propertyTable is defined, then the propertyTable must be defined
-            
-
-
-
             base.OnValidateContent(result);
         }
     }