Browse Source

refactor validation

Bert Temme 2 years ago
parent
commit
41a265bafa

+ 45 - 36
src/SharpGLTF.Cesium/Schema2/Ext.InstanceFeatures.cs

@@ -1,6 +1,5 @@
 using SharpGLTF.Validation;
 using System.Collections.Generic;
-using System.Linq;
 namespace SharpGLTF.Schema2
 {
     public partial class MeshExtInstanceFeatures
@@ -33,6 +32,25 @@ namespace SharpGLTF.Schema2
             var extMeshGpInstancing = _node.GetExtension<MeshGpuInstancing>();
             validate.NotNull(nameof(extMeshGpInstancing), extMeshGpInstancing);
 
+            foreach (var instanceFeatureId in FeatureIds)
+            {
+                if (instanceFeatureId.Attribute.HasValue)
+                {
+                    var expectedVertexAttribute = $"_FEATURE_ID_{instanceFeatureId.Attribute}";
+                    var gpuInstancing = _node.GetGpuInstancing();
+                    var featureIdAccessors = gpuInstancing.GetAccessor(expectedVertexAttribute);
+                    Guard.NotNull(featureIdAccessors, expectedVertexAttribute);
+
+                    if (instanceFeatureId.PropertyTable.HasValue)
+                    {
+                        var metadataExtension = _node.LogicalParent.GetExtension<EXTStructuralMetadataRoot>();
+                        Guard.NotNull(metadataExtension, nameof(metadataExtension), "EXT_Structural_Metadata extension is not found.");
+                        Guard.NotNull(metadataExtension.PropertyTables[instanceFeatureId.PropertyTable.Value], nameof(instanceFeatureId.PropertyTable), $"Property table index {instanceFeatureId.PropertyTable.Value} does not exist");
+
+                    }
+                }
+            }
+
             base.OnValidateReferences(validate);
         }
 
@@ -42,6 +60,31 @@ namespace SharpGLTF.Schema2
             validate.NotNull(nameof(FeatureIds), extInstanceFeatures.FeatureIds);
             validate.IsTrue(nameof(FeatureIds), extInstanceFeatures.FeatureIds.Count > 0, "Instance FeatureIds has items");
 
+
+            foreach (var instanceFeatureId in FeatureIds)
+            {
+                Guard.MustBeGreaterThanOrEqualTo((int)instanceFeatureId.FeatureCount, 1, nameof(instanceFeatureId.FeatureCount));
+
+                if (instanceFeatureId.NullFeatureId.HasValue)
+                {
+                    Guard.MustBeGreaterThanOrEqualTo((int)instanceFeatureId.NullFeatureId, 0, nameof(instanceFeatureId.NullFeatureId));
+                }
+                if (instanceFeatureId.Label != null)
+                {
+                    var regex = "^[a-zA-Z_][a-zA-Z0-9_]*$";
+                    Guard.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(instanceFeatureId.Label, regex), nameof(instanceFeatureId.Label));
+                }
+
+                if (instanceFeatureId.Attribute.HasValue)
+                {
+                    Guard.MustBeGreaterThanOrEqualTo((int)instanceFeatureId.Attribute, 0, nameof(instanceFeatureId.Attribute));
+                }
+                if (instanceFeatureId.PropertyTable.HasValue)
+                {
+                    Guard.MustBeGreaterThanOrEqualTo((int)instanceFeatureId.PropertyTable, 0, nameof(instanceFeatureId.PropertyTable));
+                }
+            }
+
             base.OnValidateContent(validate);
         }
     }
@@ -100,45 +143,11 @@ namespace SharpGLTF.Schema2
 
             Guard.NotNullOrEmpty(instanceFeatureIds, nameof(instanceFeatureIds));
 
-            var extMeshGpInstancing = node.Extensions.Where(item => item is MeshGpuInstancing).FirstOrDefault();
+            var extMeshGpInstancing = node.GetExtension<MeshGpuInstancing>();
             Guard.NotNull(extMeshGpInstancing, nameof(extMeshGpInstancing));
 
-            // todo move validate in the validation function
-
-            foreach (var instanceFeatureId in instanceFeatureIds)
-            {
-                ValidateInstanceFeatureId(node, instanceFeatureId);
-            };
-
             var ext = node.UseExtension<MeshExtInstanceFeatures>();
             ext.FeatureIds = instanceFeatureIds;
         }
-
-        private static void ValidateInstanceFeatureId(Node node, MeshExtInstanceFeatureID instanceFeatureId)
-        {
-            Guard.MustBeGreaterThanOrEqualTo((int)instanceFeatureId.FeatureCount, 1, nameof(instanceFeatureId.FeatureCount));
-
-            if (instanceFeatureId.NullFeatureId.HasValue)
-            {
-                Guard.MustBeGreaterThanOrEqualTo((int)instanceFeatureId.NullFeatureId, 0, nameof(instanceFeatureId.NullFeatureId));
-            }
-            if (instanceFeatureId.Label != null)
-            {
-                var regex = "^[a-zA-Z_][a-zA-Z0-9_]*$";
-                Guard.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(instanceFeatureId.Label, regex), nameof(instanceFeatureId.Label));
-            }
-            if (instanceFeatureId.Attribute.HasValue)
-            {
-                Guard.MustBeGreaterThanOrEqualTo((int)instanceFeatureId.Attribute, 0, nameof(instanceFeatureId.Attribute));
-                var expectedVertexAttribute = $"_FEATURE_ID_{instanceFeatureId.Attribute}";
-                var gpuInstancing = node.GetGpuInstancing();
-                var featureIdAccessors = gpuInstancing.GetAccessor(expectedVertexAttribute);
-                Guard.NotNull(featureIdAccessors, expectedVertexAttribute);
-            }
-            if (instanceFeatureId.PropertyTable.HasValue)
-            {
-                Guard.MustBeGreaterThanOrEqualTo((int)instanceFeatureId.PropertyTable, 0, nameof(instanceFeatureId.PropertyTable));
-            }
-        }
     }
 }

+ 0 - 1
src/SharpGLTF.Cesium/Schema2/Ext.MeshFeatures.cs

@@ -176,6 +176,5 @@ namespace SharpGLTF.Schema2
             var ext = primitive.UseExtension<MeshExtMeshFeatures>();
             ext.FeatureIds = featureIds;
         }
-
     }
 }

+ 159 - 36
src/SharpGLTF.Cesium/Schema2/Ext.StructuralMetadataRoot.cs

@@ -181,7 +181,7 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         {
             foreach (var propertyTexture in PropertyTextures)
             {
-                foreach(var propertyTextureProperty in propertyTexture.Properties)
+                foreach (var propertyTextureProperty in propertyTexture.Properties)
                 {
                     var textureId = propertyTextureProperty.Value._LogicalTextureIndex;
                     validate.IsNullOrIndex(nameof(propertyTexture), textureId, modelRoot.LogicalTextures);
@@ -213,7 +213,7 @@ OneOf<StructuralMetadataSchema, Uri> schema)
                 }
             }
 
-            if(Schema!= null)
+            if (Schema != null)
             {
                 foreach (var @class in Schema.Classes)
                 {
@@ -233,7 +233,7 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         protected override void OnValidateContent(ValidationContext result)
         {
             // check schema id is defined and valid
-            if (Schema!=null && !String.IsNullOrEmpty(Schema.Id))
+            if (Schema != null && !String.IsNullOrEmpty(Schema.Id))
             {
                 var regex = "^[a-zA-Z_][a-zA-Z0-9_]*$";
                 Guard.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(Schema.Id, regex), nameof(Schema.Id));
@@ -258,7 +258,7 @@ OneOf<StructuralMetadataSchema, Uri> schema)
                 Guard.IsTrue(propertyTable.Count > 0, nameof(propertyTable.Count), "Count must be greater than 0");
                 Guard.IsTrue(propertyTable.Properties.Count > 0, nameof(propertyTable.Properties), "Properties must be defined");
             }
-            
+
             // Check one of schema or schemaUri is defined, but not both
             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");
@@ -310,13 +310,21 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         public string Class
         {
             get { return _class; }
-            set { _class = value; }
+            set
+            {
+                if (value == null) { _class = null; return; }
+                _class = value;
+            }
         }
 
         public Dictionary<string, PropertyAttributeProperty> Properties
         {
             get { return _properties; }
-            set { _properties = value; }
+            set
+            {
+                if (value == null) { _properties = null; return; }
+                _properties = value;
+            }
         }
 
     }
@@ -326,7 +334,12 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         public string Attribute
         {
             get { return _attribute; }
-            set { _attribute = value; }
+            set
+            {
+
+                if (value == null) { _attribute = null; return; }
+                _attribute = value;
+            }
         }
     }
 
@@ -341,37 +354,61 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         public Dictionary<string, StructuralMetadataClass> Classes
         {
             get { return _classes; }
-            set { _classes = value; }
+            set
+            {
+                if (value == null) { _classes = null; return; }
+                _classes = value;
+            }
         }
 
         public string Id
         {
             get { return _id; }
-            set { _id = value; }
+            set
+            {
+                if (value == null) { _id = null; return; }
+                _id = value;
+            }
         }
 
         public string Version
         {
             get { return _version; }
-            set { _version = value; }
+            set
+            {
+                if (value == null) { _version = null; return; }
+                _version = value;
+            }
         }
 
         public string Name
         {
             get { return _name; }
-            set { _name = value; }
+            set
+            {
+                if (value == null) { _name = null; return; }
+                _name = value;
+            }
         }
 
         public string Description
         {
             get { return _description; }
-            set { _description = value; }
+            set
+            {
+                if (value == null) { _description = null; return; }
+                _description = value;
+            }
         }
 
         public Dictionary<string, StructuralMetadataEnum> Enums
         {
             get { return _enums; }
-            set { _enums = value; }
+            set
+            {
+                if (value == null) { _enums = null; return; }
+                _enums = value;
+            }
         }
     }
 
@@ -384,17 +421,29 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         public string Name
         {
             get { return _name; }
-            set { _name = value; }
+            set
+            {
+                if (value == null) { _name = null; return; }
+                _name = value;
+            }
         }
         public string Description
         {
             get { return _description; }
-            set { _description = value; }
+            set
+            {
+                if (value == null) { _description = null; return; }
+                _description = value;
+            }
         }
         public List<EnumValue> Values
         {
             get { return _values; }
-            set { _values = value; }
+            set
+            {
+                if (value == null) { _values = null; return; }
+                _values = value;
+            }
         }
     }
 
@@ -403,12 +452,19 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         public string Name
         {
             get { return _name; }
-            set { _name = value; }
+            set
+            {
+                if (value == null) { _name = null; return; }
+                _name = value;
+            }
         }
         public int Value
         {
             get { return _value; }
-            set { _value = value; }
+            set
+            {
+                _value = value;
+            }
         }
     }
 
@@ -422,19 +478,31 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         public Dictionary<string, ClassProperty> Properties
         {
             get { return _properties; }
-            set { _properties = value; }
+            set
+            {
+                if (value == null) { _properties = null; return; }
+                _properties = value;
+            }
         }
 
         public string Name
         {
             get { return _name; }
-            set { _name = value; }
+            set
+            {
+                if (value == null) { _name = null; return; }
+                _name = value;
+            }
         }
 
         public string Description
         {
             get { return _description; }
-            set { _description = value; }
+            set
+            {
+                if (value == null) { _description = null; return; }
+                _description = value;
+            }
         }
 
     }
@@ -444,56 +512,92 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         public string Name
         {
             get { return _name; }
-            set { _name = value; }
+            set
+            {
+                if (value == null) { _name = null; return; }
+                _name = value;
+            }
         }
 
         public string Description
         {
             get { return _description; }
-            set { _description = value; }
+            set
+            {
+                if (value == null) { _description = null; return; }
+                _description = value;
+            }
         }
 
         public ElementType Type
         {
             get { return _type; }
-            set { _type = value; }
+            set
+            {
+                _type = value;
+            }
         }
 
         public string EnumType
         {
             get { return _enumType; }
-            set { _enumType = value; }
+            set
+            {
+                if (value == null) { _enumType = null; return; }
+                _enumType = value;
+            }
         }
 
         public DataType? ComponentType
         {
             get { return _componentType; }
-            set { _componentType = value; }
+            set
+            {
+                if (value == null) { _componentType = null; return; }
+                _componentType = value;
+            }
         }
 
         public bool? Required
         {
             get { return _required; }
-            set { _required = value; }
+            set
+            {
+
+                if (value == null) { _required = null; return; }
+                _required = value;
+            }
         }
 
         public bool? Normalized
         {
             get { return _normalized; }
-            set { _normalized = value; }
+            set
+            {
+                if (value == null) { _normalized = null; return; }
+                _normalized = value;
+            }
         }
 
         public bool? Array
         {
             get { return _array; }
-            set { _array = value; }
+            set
+            {
+                if (value == null) { _array = null; return; }
+                _array = value;
+            }
 
         }
 
         public int? Count
         {
             get { return _count; }
-            set { _count = value; }
+            set
+            {
+                if (value == null) { _count = null; return; }
+                _count = value;
+            }
         }
     }
 
@@ -513,25 +617,38 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         public string Name
         {
             get { return _name; }
-            set { _name = value; }
+            set
+            {
+                if (value == null) { _name = null; return; }
+                _name = value;
+            }
         }
 
         public string Class
         {
             get { return _class; }
-            set { _class = value; }
+            set
+            {
+                if (value == null) { _class = null; return; }
+                _class = value;
+            }
         }
 
         public int Count
         {
             get { return _count; }
-            set { _count = value; }
+            set
+            {
+                _count = value;
+            }
         }
 
         public Dictionary<string, PropertyTableProperty> Properties
         {
             get { return _properties; }
-            set { _properties = value; }
+            set {
+                if (value == null) { _properties = null; return; }
+                _properties = value; }
         }
     }
 
@@ -546,13 +663,19 @@ OneOf<StructuralMetadataSchema, Uri> schema)
         public int? ArrayOffsets
         {
             get { return _arrayOffsets; }
-            set { _arrayOffsets = value; }
+            set {
+
+                if (value == null) { _arrayOffsets= null; return; }
+                _arrayOffsets = value; }
         }
 
         public int? StringOffsets
         {
             get { return _stringOffsets; }
-            set { _stringOffsets = value; }
+            set {
+
+                if (value == null) { _stringOffsets = null; return; }
+                _stringOffsets = value; }
         }
     }
 }

+ 2 - 2
tests/SharpGLTF.Cesium.Tests/ExtInstanceFeaturesTests.cs

@@ -38,8 +38,8 @@ namespace SharpGLTF.Cesium
                     WithExtras(JsonNode.Parse("{\"_FEATURE_ID_0\":1}"));
 
 
-            var featureId0 = new MeshExtInstanceFeatureID(2, 0, 0, "Forests", 2);
-            var featureId1 = new MeshExtInstanceFeatureID(9, propertyTable: 1, label: "Trees");
+            var featureId0 = new MeshExtInstanceFeatureID(2, 0, label: "Forests");
+            var featureId1 = new MeshExtInstanceFeatureID(9, label: "Trees");
 
             var featureIds = new List<MeshExtInstanceFeatureID>() { featureId0, featureId1 };