Browse Source

got the ext_structural_metada - propertyTable working

Bert Temme 2 years ago
parent
commit
4b4a9746ef

+ 56 - 0
src/SharpGLTF.Cesium/BinaryTable.cs

@@ -0,0 +1,56 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace SharpGLTF
+{
+    public static class BinaryTable
+    {
+        public static byte[] GetOffsetBuffer(IReadOnlyList<string> strings)
+        {
+            var offsetBuffer = GetOffsets(strings);
+            var offsetBytes = GetBytes(offsetBuffer);
+            return offsetBytes;
+        }
+
+        public static byte[] GetBytes<T>(IReadOnlyList<T> values)
+        {
+            var type = typeof(T);
+            int size = 0;
+            if (type == typeof(float))
+            {
+                size = sizeof(float);
+            }
+            else if (type == typeof(int))
+            {
+                size = sizeof(int);
+            }
+            else if (type == typeof(uint))
+            {
+                size = sizeof(uint);
+            }
+
+            var result = new byte[values.Count * size];
+            System.Buffer.BlockCopy(values.ToArray(), 0, result, 0, result.Length);
+            return result;
+        }
+
+        private static List<uint> GetOffsets(IReadOnlyList<string> strings)
+        {
+            var offsets = new List<uint>() { 0 };
+            foreach (string s in strings)
+            {
+                var length = (uint)Encoding.UTF8.GetByteCount(s);
+
+                offsets.Add(offsets.Last() + length);
+            }
+            return offsets;
+        }
+
+        public static byte[] GetStringsAsBytes(IReadOnlyList<string> values)
+        {
+            var res = string.Join("", values);
+            return Encoding.UTF8.GetBytes(res);
+        }
+    }
+}

+ 23 - 59
src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs

@@ -10,92 +10,56 @@ namespace SharpGLTF.Schema2
         internal EXTStructuralMetaDataRoot(ModelRoot modelRoot)
         internal EXTStructuralMetaDataRoot(ModelRoot modelRoot)
         {
         {
             this.modelRoot = modelRoot;
             this.modelRoot = modelRoot;
+            _propertyTables = new List<PropertyTable>();
         }
         }
 
 
-        internal List<PropertyTable> PropertyTables
+        public List<PropertyTable> PropertyTables
         {
         {
             get { return _propertyTables; }
             get { return _propertyTables; }
-            set { if (value == null) { _propertyTables = null; return; } _propertyTables = value; }
+            set { _propertyTables = value; }
         }
         }
 
 
-        internal StructuralMetadataSchema Schema
-        {
-            get { return _schema; }
-            set { if (value == null) { _schema = null; return; } _schema = value; }
-        }
-
-
         protected override void OnValidateContent(ValidationContext validate)
         protected override void OnValidateContent(ValidationContext validate)
         {
         {
         }
         }
     }
     }
 
 
-    partial class StructuralMetadataSchema
+    public partial class PropertyTable
     {
     {
-        public StructuralMetadataSchema()
+        public PropertyTable()
         {
         {
-            _classes = new Dictionary<string, StructuralMetadataClass>();
+            _properties = new Dictionary<string, PropertyTableProperty>();
         }
         }
-
-        public Dictionary<string, StructuralMetadataClass> Classes { get; set; }
-    }
-
-    partial class PropertyTable
-    {
-        public PropertyTable(string PropertyTableName, int numberOfFeatures)
+        public PropertyTable(string PropertyTableName, int NumberOfFeatures): this()
         {
         {
             _class = PropertyTableName;
             _class = PropertyTableName;
-            _count = numberOfFeatures;
-            _properties = new Dictionary<string, PropertyTableProperty>();
+            _count = NumberOfFeatures;
         }
         }
-    }
-
-    partial class PropertyTableProperty
-    {
-    }
 
 
-    partial class StructuralMetadataClass
-    {
-        public StructuralMetadataClass()
+        public string PropertyTableName
         {
         {
-            _properties = new Dictionary<string, ClassProperty>();
+            get { return _class; }
+            set { _class = value; }
         }
         }
-    }
 
 
-    partial class ClassProperty
-    {
-    }
-
-    public static class ExtStructuralMetadata
-    {
-        // Creates EXTStructuralMetaData with Schema and 1 PropertyTable
-        public static void InitializeMetadataExtension(this ModelRoot modelRoot, string propertyTableName, int numberOfFeatures)
+        public int NumberOfFeatures
         {
         {
-            if (propertyTableName == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
-
-            var ext = modelRoot.UseExtension<EXTStructuralMetaDataRoot>();
-
-            var schema = GetInitialSchema(propertyTableName);
-            ext.Schema = schema;
-            var propertyTable = new PropertyTable(propertyTableName, numberOfFeatures);
-            ext.PropertyTables = new List<PropertyTable>() { propertyTable };
+            get { return _count; }
+            set { _count = value; }
         }
         }
 
 
-        public static void AddMetadata<T>(this ModelRoot modelRoot, string fieldname, List<T> values)
+        public Dictionary<string, PropertyTableProperty> Properties
         {
         {
+            get { return _properties; }
+            set { _properties = value; }
         }
         }
+    }
 
 
-        private static StructuralMetadataSchema GetInitialSchema(string schemaName)
-        {
-            var structuralMetadataSchema = new StructuralMetadataSchema();
-            var structuralMetadataClass = new StructuralMetadataClass();
-
-            structuralMetadataSchema.Classes = new Dictionary<string, StructuralMetadataClass>
-            {
-            { schemaName , structuralMetadataClass }
-            };
-
-            return structuralMetadataSchema;
+    public partial class PropertyTableProperty
+    {
+        public int Values {
+            get { return _values; }
+            set { _values = value; }
         }
         }
     }
     }
 }
 }

+ 0 - 2
src/SharpGLTF.Cesium/Schema2/MeshExtInstanceFeatures.cs

@@ -1,8 +1,6 @@
 using SharpGLTF.Validation;
 using SharpGLTF.Validation;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
-using System.Xml.Linq;
-
 namespace SharpGLTF.Schema2
 namespace SharpGLTF.Schema2
 {
 {
     public partial class MeshExtInstanceFeatures
     public partial class MeshExtInstanceFeatures

+ 3 - 0
src/SharpGLTF.Cesium/Schema2/MeshExtMeshFeatures.cs

@@ -54,6 +54,9 @@ namespace SharpGLTF.Schema2
 
 
     public partial class MeshExtMeshFeatureID
     public partial class MeshExtMeshFeatureID
     {
     {
+        public MeshExtMeshFeatureID()
+        {
+        }
         public MeshExtMeshFeatureID(int featureCount, int? attribute = null, int? propertyTable = null, string label = null, int? nullFeatureId = null, MeshExtMeshFeatureIDTexture texture = null)
         public MeshExtMeshFeatureID(int featureCount, int? attribute = null, int? propertyTable = null, string label = null, int? nullFeatureId = null, MeshExtMeshFeatureIDTexture texture = null)
         {
         {
             _featureCount = featureCount;
             _featureCount = featureCount;

+ 10 - 18
tests/SharpGLTF.Cesium.Tests/ExtStructuralMetadataTests.cs

@@ -24,40 +24,32 @@ namespace SharpGLTF.Cesium
         {
         {
             TestContext.CurrentContext.AttachGltfValidatorLinks();
             TestContext.CurrentContext.AttachGltfValidatorLinks();
 
 
-            // Create a triangle with feature ID custom vertex attribute
-            var featureId = 1;
             var material = MaterialBuilder.CreateDefault().WithDoubleSide(true);
             var material = MaterialBuilder.CreateDefault().WithDoubleSide(true);
 
 
-            var mesh = new MeshBuilder<VertexPositionNormal, VertexWithFeatureId, VertexEmpty>("mesh");
+            var mesh = new MeshBuilder<VertexPosition>("mesh");
             var prim = mesh.UsePrimitive(material);
             var prim = mesh.UsePrimitive(material);
 
 
-            // All the vertices in the triangle have the same feature ID
-            var vt0 = VertexBuilder.GetVertexWithFeatureId(new Vector3(-10, 0, 0), new Vector3(0, 0, 1), featureId);
-            var vt1 = VertexBuilder.GetVertexWithFeatureId(new Vector3(10, 0, 0), new Vector3(0, 0, 1), featureId);
-            var vt2 = VertexBuilder.GetVertexWithFeatureId(new Vector3(0, 10, 0), new Vector3(0, 0, 1), featureId);
+            prim.AddTriangle(new VertexPosition(-10, 0, 0), new VertexPosition(10, 0, 0), new VertexPosition(0, 10, 0));
 
 
-            prim.AddTriangle(vt0, vt1, vt2);
             var scene = new SceneBuilder();
             var scene = new SceneBuilder();
             scene.AddRigidMesh(mesh, Matrix4x4.Identity);
             scene.AddRigidMesh(mesh, Matrix4x4.Identity);
             var model = scene.ToGltf2();
             var model = scene.ToGltf2();
 
 
-            var featureIdAttribute = new MeshExtMeshFeatureID(1, 0);
+            var bytes = BinaryTable.GetBytes(new List<int>() { 100 });
+            var bufferView = model.UseBufferView(bytes);
 
 
-            // Set the FeatureIds
-            var featureIds = new List<MeshExtMeshFeatureID>() { featureIdAttribute };
-            model.LogicalMeshes[0].Primitives[0].SetFeatureIds(featureIds);
+            var ext = model.UseExtension<EXTStructuralMetaDataRoot>();
 
 
-            model.InitializeMetadataExtension("propertyTable", 1);
-
-            // todo add metadata
+            var propertyTableProperty = new PropertyTableProperty();
+            propertyTableProperty.Values = bufferView.LogicalIndex;
+            var propertyTable = new PropertyTable("propertyTable", 1);
+            propertyTable.Properties["id1"] = propertyTableProperty;
+            ext.PropertyTables.Add( propertyTable);
 
 
             var ctx = new ValidationResult(model, ValidationMode.Strict, true);
             var ctx = new ValidationResult(model, ValidationMode.Strict, true);
-            model.ValidateContent(ctx.GetContext());
-
             model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.glb");
             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.gltf");
             model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.plotly");
             model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.plotly");
-
         }
         }
     }
     }
 }
 }