Browse Source

add enum complex type

Bert Temme 2 years ago
parent
commit
099ea51bcd

+ 33 - 5
src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs

@@ -51,6 +51,15 @@ namespace SharpGLTF.Schema2
         {
         {
             if (propertyTables == null || propertyTables.Count == 0) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
             if (propertyTables == null || propertyTables.Count == 0) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
 
 
+            schema.Switch(
+                metadataschema =>
+                    CheckSchema(metadataschema),
+                Uri =>
+                {
+                    // do not check here, because schema is not loaded
+                }
+                );
+
             // todo add check if propertyTable.Class is in schema.Classes
             // todo add check if propertyTable.Class is in schema.Classes
             foreach (var propertyTable in propertyTables)
             foreach (var propertyTable in propertyTables)
             {
             {
@@ -59,8 +68,8 @@ namespace SharpGLTF.Schema2
                 Guard.IsTrue(propertyTable.Properties.Count > 0, nameof(propertyTable.Properties), "Properties must be defined");
                 Guard.IsTrue(propertyTable.Properties.Count > 0, nameof(propertyTable.Properties), "Properties must be defined");
 
 
                 schema.Switch(
                 schema.Switch(
-                    StructuralMetadataSchema =>
-                        CheckConsistency(StructuralMetadataSchema, propertyTable),
+                    metadataschema =>
+                        CheckConsistency(metadataschema, propertyTable),
                     Uri =>
                     Uri =>
                     {
                     {
                         // do not check here, because schema is not loaded
                         // do not check here, because schema is not loaded
@@ -73,13 +82,32 @@ namespace SharpGLTF.Schema2
             ext.AddSchema(schema);
             ext.AddSchema(schema);
         }
         }
 
 
-        private static void CheckConsistency(StructuralMetadataSchema StructuralMetadataSchema, PropertyTable propertyTable)
+        private static void CheckSchema(StructuralMetadataSchema schema)
+        {
+            // check if schema class property has type of enum, then the schema enum based on enumtype must be defined
+            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");
+                    }
+                }
+            }
+        }
+
+        private static void CheckConsistency(StructuralMetadataSchema schema, PropertyTable propertyTable)
         {
         {
-            Guard.IsTrue(StructuralMetadataSchema.Classes.ContainsKey(propertyTable.Class), nameof(propertyTable.Class), $"Class {propertyTable.Class} must be defined in schema");
+            Guard.IsTrue(schema.Classes.ContainsKey(propertyTable.Class), nameof(propertyTable.Class), $"Class {propertyTable.Class} must be defined in schema");
             foreach (var property in propertyTable.Properties)
             foreach (var property in propertyTable.Properties)
             {
             {
-                Guard.IsTrue(StructuralMetadataSchema.Classes[propertyTable.Class].Properties.ContainsKey(property.Key), nameof(propertyTable.Properties), $"Property {property.Key} must be defined in schema");
+                Guard.IsTrue(schema.Classes[propertyTable.Class].Properties.ContainsKey(property.Key), nameof(propertyTable.Properties), $"Property {property.Key} must be defined in schema");
             }
             }
+
+            // check if schema class property has type of enum, then the schema enum based on enumtype must be defined
+            // foreach(var )
+
         }
         }
 
 
         public static PropertyTableProperty GetArrayPropertyTableProperty<T>(this ModelRoot model, List<List<T>> values, bool CreateArrayOffsets = true)
         public static PropertyTableProperty GetArrayPropertyTableProperty<T>(this ModelRoot model, List<List<T>> values, bool CreateArrayOffsets = true)

+ 33 - 8
tests/SharpGLTF.Cesium.Tests/ExtStructuralMetadataTests.cs

@@ -50,6 +50,8 @@ namespace SharpGLTF.Cesium
             exampleMetadataClass.Name = "Example metadata class A";
             exampleMetadataClass.Name = "Example metadata class A";
             exampleMetadataClass.Description = "First example metadata class";
             exampleMetadataClass.Description = "First example metadata class";
 
 
+            // class properties
+            
             var uint8ArrayProperty = new ClassProperty();
             var uint8ArrayProperty = new ClassProperty();
             uint8ArrayProperty.Name = "Example variable-length ARRAY normalized INT8 property";
             uint8ArrayProperty.Name = "Example variable-length ARRAY normalized INT8 property";
             uint8ArrayProperty.Description = "An example property, with type ARRAY, with component type UINT8, normalized, and variable length";
             uint8ArrayProperty.Description = "An example property, with type ARRAY, with component type UINT8, normalized, and variable length";
@@ -69,7 +71,6 @@ namespace SharpGLTF.Cesium
 
 
             exampleMetadataClass.Properties.Add("example_fixed_length_ARRAY_BOOLEAN", fixedLengthBooleanProperty);
             exampleMetadataClass.Properties.Add("example_fixed_length_ARRAY_BOOLEAN", fixedLengthBooleanProperty);
 
 
-
             var variableLengthStringArrayProperty = new ClassProperty();
             var variableLengthStringArrayProperty = new ClassProperty();
             variableLengthStringArrayProperty.Name = "Example variable-length ARRAY STRING property";
             variableLengthStringArrayProperty.Name = "Example variable-length ARRAY STRING property";
             variableLengthStringArrayProperty.Description = "An example property, with type ARRAY, with component type STRING, and variable length";
             variableLengthStringArrayProperty.Description = "An example property, with type ARRAY, with component type STRING, and variable length";
@@ -77,35 +78,59 @@ namespace SharpGLTF.Cesium
             variableLengthStringArrayProperty.Array = true;
             variableLengthStringArrayProperty.Array = true;
             exampleMetadataClass.Properties.Add("example_variable_length_ARRAY_STRING", variableLengthStringArrayProperty);
             exampleMetadataClass.Properties.Add("example_variable_length_ARRAY_STRING", variableLengthStringArrayProperty);
 
 
+            var fixed_length_ARRAY_ENUM = new ClassProperty();
+            fixed_length_ARRAY_ENUM.Name = "Example fixed-length ARRAY ENUM property";
+            fixed_length_ARRAY_ENUM.Description = "An example property, with type ARRAY, with component type ENUM, and fixed length";
+            fixed_length_ARRAY_ENUM.Type = ElementType.ENUM;
+            fixed_length_ARRAY_ENUM.Array = true;
+            fixed_length_ARRAY_ENUM.Count = 2;
+            fixed_length_ARRAY_ENUM.EnumType = "exampleEnumType";
+
+            exampleMetadataClass.Properties.Add("example_fixed_length_ARRAY_ENUM", fixed_length_ARRAY_ENUM);
+
             schema.Classes.Add("exampleMetadataClass", exampleMetadataClass);
             schema.Classes.Add("exampleMetadataClass", exampleMetadataClass);
 
 
+            // enums
+
+            var exampleEnum = new StructuralMetadataEnum();
+            exampleEnum.Values.Add(new EnumValue() { Name = "ExampleEnumValueA", Value = 0 });
+            exampleEnum.Values.Add(new EnumValue() { Name = "ExampleEnumValueB", Value = 1 });
+            exampleEnum.Values.Add(new EnumValue() { Name = "ExampleEnumValueC", Value = 2 });
+
+            schema.Enums.Add("exampleEnumType1", exampleEnum);
+
+            // property tables
+
             var examplePropertyTable = new PropertyTable("exampleMetadataClass", 1, "Example property table");
             var examplePropertyTable = new PropertyTable("exampleMetadataClass", 1, "Example property table");
-            var list0 = new List<byte>() { 0, 1, 2, 3, 4, 5, 6, 7 };
             var list2 = new List<List<byte>>() {
             var list2 = new List<List<byte>>() {
-                list0
+                new() { 0, 1, 2, 3, 4, 5, 6, 7 }
             };
             };
 
 
             var property = model.GetArrayPropertyTableProperty(list2);
             var property = model.GetArrayPropertyTableProperty(list2);
             examplePropertyTable.Properties.Add("example_variable_length_ARRAY_normalized_UINT8", property);
             examplePropertyTable.Properties.Add("example_variable_length_ARRAY_normalized_UINT8", property);
 
 
-            var booleans = new List<bool>() { true, false, true, false };
             var booleansList = new List<List<bool>>()
             var booleansList = new List<List<bool>>()
             {
             {
-                booleans
+                new() { true, false, true, false }
             };
             };
             var propertyBooleansList = model.GetArrayPropertyTableProperty(booleansList, false);
             var propertyBooleansList = model.GetArrayPropertyTableProperty(booleansList, false);
             examplePropertyTable.Properties.Add("example_fixed_length_ARRAY_BOOLEAN", propertyBooleansList);
             examplePropertyTable.Properties.Add("example_fixed_length_ARRAY_BOOLEAN", propertyBooleansList);
 
 
-            var strings = new List<string>() { "Example string 1", "Example string 2", "Example string 3" };
             var stringsList = new List<List<string>>()
             var stringsList = new List<List<string>>()
             {
             {
-                strings
+                new() { "Example string 1", "Example string 2", "Example string 3" }
             };
             };
 
 
             var propertyStringsList = model.GetArrayPropertyTableProperty(stringsList);
             var propertyStringsList = model.GetArrayPropertyTableProperty(stringsList);
             examplePropertyTable.Properties.Add("example_variable_length_ARRAY_STRING", propertyStringsList);
             examplePropertyTable.Properties.Add("example_variable_length_ARRAY_STRING", propertyStringsList);
 
 
-            // todo add more complex type properties
+            var enumsList = new List<List<int>>()
+            {
+                new() { 0, 1 }
+            };
+
+            var enumsProperty = model.GetArrayPropertyTableProperty(enumsList, false);
+            examplePropertyTable.Properties.Add("example_fixed_length_ARRAY_ENUM", enumsProperty);
 
 
             model.SetPropertyTable(examplePropertyTable, schema);
             model.SetPropertyTable(examplePropertyTable, schema);