فهرست منبع

refactor propertyTables

Bert Temme 2 سال پیش
والد
کامیت
0d4d8ddf54

+ 34 - 39
src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs

@@ -38,55 +38,43 @@ namespace SharpGLTF.Schema2
 
         public static void SetPropertyTable(
             this ModelRoot modelRoot,
-            Dictionary<string, List<int>> attributes,
-            OneOf<StructuralMetadataSchema, Uri> schema,
-            string name = "PropertyTable"
-            )
+            PropertyTable propertyTable,
+            OneOf<StructuralMetadataSchema, Uri> schema)
         {
-            if (attributes == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
+            if (propertyTable == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
+            SetPropertyTables(modelRoot, new List<PropertyTable>() { propertyTable }, schema);
+        }
+
+        public static void SetPropertyTables(
+            this ModelRoot modelRoot,
+            List<PropertyTable> propertyTables,
+            OneOf<StructuralMetadataSchema, Uri> schema)
+        {
+            if (propertyTables == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
 
             var ext = modelRoot.UseExtension<EXTStructuralMetaDataRoot>();
-            ext.PropertyTables.Clear();
-            var propertyTable = GetPropertyTable(modelRoot, attributes, schema, name);
-            ext.PropertyTables.Add(propertyTable);
+            ext.PropertyTables = propertyTables;
             ext.AddSchema(schema);
         }
 
-
-        private static PropertyTable GetPropertyTable(
-            ModelRoot modelRoot,
-            Dictionary<string, List<int>> attributes,
-            OneOf<StructuralMetadataSchema, Uri> schema1,
-            string name = "PropertyTable"
-            )
+        public static PropertyTableProperty GetPropertyTableProperty<T>(this ModelRoot model, IReadOnlyList<T> values)
         {
-            var propertyTable = new PropertyTable(name, attributes.FirstOrDefault().Value.Count);
-
-            // todo: what if schema is Uri?
-
-            var structuralMetadataSchema = schema1.TryPickT0(out var schema, out var remainder);
-
-            var firstClass = schema.Classes.FirstOrDefault().Value;
+            var propertyTableProperty = new PropertyTableProperty();
+            int logicalIndex = GetBufferView(model, values);
+            propertyTableProperty.Values = logicalIndex;
+            return propertyTableProperty;
+        }
 
-            foreach (var property in firstClass.Properties)
-            {
-                var id = property.Key;
-                var type = property.Value.Type;
+        private static int GetBufferView<T>(this ModelRoot model, IReadOnlyList<T> values)
+        {
+            var bytesFloat32 = BinaryTable.GetBytes(values);
+            var bufferView = model.UseBufferView(bytesFloat32);
+            int logicalIndex = bufferView.LogicalIndex;
+            return logicalIndex;
+        }
 
-                // Todo check type, for example string
-                var attribute = attributes[id];
-                var list = attribute.ConvertAll(x => (int)x);
 
-                byte[] bytes = BinaryTable.GetBytes(list);
-                var bufferView = modelRoot.UseBufferView(bytes);
-                int logicalIndex = bufferView.LogicalIndex;
-                var propertyTableProperty = new PropertyTableProperty();
-                propertyTableProperty.Values = logicalIndex;
-                propertyTable.Properties[id] = propertyTableProperty;
-            }
 
-            return propertyTable;
-        }
     }
 
     public partial class EXTStructuralMetaDataRoot
@@ -367,10 +355,17 @@ namespace SharpGLTF.Schema2
         {
             _properties = new Dictionary<string, PropertyTableProperty>();
         }
-        public PropertyTable(string Class, int Count) : this()
+        public PropertyTable(string Class, int Count, string Name = "") : this()
         {
             _class = Class;
             _count = Count;
+            _name = Name;
+        }
+
+        public string Name
+        {
+            get { return _name; }
+            set { _name = value; }
         }
 
         public string Class

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

@@ -57,25 +57,44 @@ namespace SharpGLTF.Cesium
 
             schema.Classes = classes;
             var exampleEnum = new StructuralMetadataEnum();
-            exampleEnum.Values.Add(new EnumValue() {  Name= "ExampleEnumValueA", Value = 0 });
+            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("exampleEnumType", exampleEnum);
 
-            // Todo: create the property tables (First example property table, Second example property table)
+            var firstPropertyTable = GetFirstPropertyTable(model);
+            var secondPropertyTable = GetSecondPropertyTable(model);
 
-            
-            // model.SetPropertyAttribute(propertyAttribute, schema);
-
-
-            // todo: add metadata
+            var propertyTables = new List<PropertyTable>() { firstPropertyTable, secondPropertyTable };
+            model.SetPropertyTables(propertyTables, schema);
+            model.SaveGLTF(@"d:\aaa\bertclass.gltf");
             var ctx = new ValidationResult(model, ValidationMode.Strict, true);
             model.AttachToCurrentTest("cesium_ext_structural_metadata_multiple_classes.glb");
             model.AttachToCurrentTest("cesium_ext_structural_metadata_multiple_classes.gltf");
             model.AttachToCurrentTest("cesium_ext_structural_metadata_multiple_classes.plotly");
         }
 
+        private static PropertyTable GetFirstPropertyTable(ModelRoot model)
+        {
+            var firstPropertyTable = new PropertyTable("exampleMetadataClassA", 1, "First example property table");
+            var float32Property = model.GetPropertyTableProperty(new List<float>() { 100 });
+            firstPropertyTable.Properties.Add("example_FLOAT32", float32Property);
+            var int64Property = model.GetPropertyTableProperty(new List<long>() { 101 });
+            firstPropertyTable.Properties.Add("example_INT64", int64Property);
+            return firstPropertyTable;
+        }
+
+        private static PropertyTable GetSecondPropertyTable(ModelRoot model)
+        {
+            var secondPropertyTable = new PropertyTable("exampleMetadataClassB", 1, "First example property table");
+            var uint16Property = model.GetPropertyTableProperty(new List<ushort>() { 102 });
+            secondPropertyTable.Properties.Add("example_UINT16", uint16Property);
+            var float64Property = model.GetPropertyTableProperty(new List<double>() { 103 });
+            secondPropertyTable.Properties.Add("example_FLOAT64", float64Property);
+            return secondPropertyTable;
+        }
+
         private static StructuralMetadataClass GetExampleClassB()
         {
             var classB = new StructuralMetadataClass();
@@ -176,29 +195,9 @@ namespace SharpGLTF.Cesium
 
         [Test(Description = "First test with ext_structural_metadata")]
         public void TriangleWithMetadataTest()
-        {
-            var model = GetTriangleModel();
-
-            var schema = GetSampleSchema();
-
-            var attribute = new List<int>() { 100 };
-            var dict = new Dictionary<string, List<int>>();
-            dict["age"] = attribute;
-            model.SetPropertyTable(dict, schema: schema);
-
-            // create files
-            var ctx = new ValidationResult(model, ValidationMode.Strict, true);
-            model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.glb");
-            model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.gltf");
-            model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.plotly");
-        }
-
-        private static ModelRoot GetTriangleModel()
         {
             TestContext.CurrentContext.AttachGltfValidatorLinks();
-
             var material = MaterialBuilder.CreateDefault().WithDoubleSide(true);
-
             var mesh = new MeshBuilder<VertexPosition>("mesh");
             var prim = mesh.UsePrimitive(material);
 
@@ -207,11 +206,7 @@ namespace SharpGLTF.Cesium
             var scene = new SceneBuilder();
             scene.AddRigidMesh(mesh, Matrix4x4.Identity);
             var model = scene.ToGltf2();
-            return model;
-        }
-
-        private static StructuralMetadataSchema GetSampleSchema()
-        {
+            
             var schema = new StructuralMetadataSchema();
             schema.Id = "schema_001";
             schema.Name = "schema 001";
@@ -231,7 +226,18 @@ namespace SharpGLTF.Cesium
             treeClass.Properties.Add("age", ageProperty);
 
             schema.Classes = classes;
-            return schema;
+
+            var propertyTable = new PropertyTable("tree", 1, "PropertyTable");
+            var agePropertyTableProperty = model.GetPropertyTableProperty(new List<int>() { 100 });
+            propertyTable.Properties.Add("age", agePropertyTableProperty);
+
+            model.SetPropertyTable(propertyTable, schema);
+
+            // create files
+            var ctx = new ValidationResult(model, ValidationMode.Strict, true);
+            model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.glb");
+            model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.gltf");
+            model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.plotly");
         }
     }
 }