Преглед изворни кода

adding first ext_structural_metadata test

Bert Temme пре 2 година
родитељ
комит
cac94fea83

+ 32 - 7
src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs

@@ -1,20 +1,17 @@
 using SharpGLTF.Validation;
-using System;
 using System.Collections.Generic;
-using System.Linq;
 
 namespace SharpGLTF.Schema2
 {
     public partial class EXTStructuralMetaDataRoot
     {
+        private ModelRoot modelRoot;
+
         internal EXTStructuralMetaDataRoot(ModelRoot modelRoot)
         {
             this.modelRoot = modelRoot;
         }
 
-        private ModelRoot modelRoot;
-
-
         internal List<PropertyTable> PropertyTables
         {
             get { return _propertyTables; }
@@ -40,15 +37,17 @@ namespace SharpGLTF.Schema2
             _classes = new Dictionary<string, StructuralMetadataClass>();
         }
 
+        public Dictionary<string, StructuralMetadataClass> Classes { get; set; }
     }
 
     partial class PropertyTable
     {
-        public PropertyTable()
+        public PropertyTable(string PropertyTableName, int numberOfFeatures)
         {
+            _class = PropertyTableName;
+            _count = numberOfFeatures;
             _properties = new Dictionary<string, PropertyTableProperty>();
         }
-
     }
 
     partial class PropertyTableProperty
@@ -69,8 +68,34 @@ namespace SharpGLTF.Schema2
 
     public static class ExtStructuralMetadata
     {
+        // Creates EXTStructuralMetaData with Schema and 1 PropertyTable
+        public static void InitializeMetadataExtension(this ModelRoot modelRoot, string propertyTableName, 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 };
+        }
+
         public static void AddMetadata<T>(this ModelRoot modelRoot, string fieldname, List<T> values)
         {
         }
+
+        private static StructuralMetadataSchema GetInitialSchema(string schemaName)
+        {
+            var structuralMetadataSchema = new StructuralMetadataSchema();
+            var structuralMetadataClass = new StructuralMetadataClass();
+
+            structuralMetadataSchema.Classes = new Dictionary<string, StructuralMetadataClass>
+            {
+            { schemaName , structuralMetadataClass }
+            };
+
+            return structuralMetadataSchema;
+        }
     }
 }

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

@@ -1,7 +1,6 @@
 using SharpGLTF.Validation;
 using System.Collections.Generic;
 using System.Linq;
-using System.Xml.Linq;
 
 namespace SharpGLTF.Schema2
 {

+ 3 - 10
tests/SharpGLTF.Cesium.Tests/ExtMeshFeaturesTests.cs

@@ -14,7 +14,6 @@ namespace SharpGLTF.Cesium
 {
     using VBTexture1 = VertexBuilder<VertexPosition, VertexTexture1, VertexEmpty>;
 
-
     [Category("Toolkit.Scenes")]
     public class ExtMeshFeaturesTests
     {
@@ -37,9 +36,9 @@ namespace SharpGLTF.Cesium
             var prim = mesh.UsePrimitive(material);
 
             // All the vertices in the triangle have the same feature ID
-            var vt0 = GetVertexBuilderWithFeatureId(new Vector3(-10, 0, 0), new Vector3(0, 0, 1), featureId);
-            var vt1 = GetVertexBuilderWithFeatureId(new Vector3(10, 0, 0), new Vector3(0, 0, 1), featureId);
-            var vt2 = GetVertexBuilderWithFeatureId(new Vector3(0, 10, 0), new Vector3(0, 0, 1), featureId);
+            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(vt0, vt1, vt2);
             var scene = new SceneBuilder();
@@ -135,11 +134,5 @@ namespace SharpGLTF.Cesium
             scene.AttachToCurrentTest("cesium_ext_mesh_features_feature_id_texture.plotly");
         }
 
-        private static VertexBuilder<VertexPositionNormal, VertexWithFeatureId, VertexEmpty> GetVertexBuilderWithFeatureId(Vector3 position, Vector3 normal, int featureid)
-        {
-            var vp0 = new VertexPositionNormal(position, normal);
-            var vb0 = new VertexBuilder<VertexPositionNormal, VertexWithFeatureId, VertexEmpty>(vp0, featureid);
-            return vb0;
-        }
     }
 }

+ 46 - 1
tests/SharpGLTF.Cesium.Tests/ExtStructuralMetadataTests.cs

@@ -1,7 +1,14 @@
 using NUnit.Framework;
+using SharpGLTF.Geometry;
+using SharpGLTF.Geometry.VertexTypes;
+using SharpGLTF.Materials;
+using SharpGLTF.Scenes;
 using SharpGLTF.Schema2;
+using SharpGLTF.Validation;
+using System.Collections.Generic;
+using System.Numerics;
 
-namespace SharpGLTF
+namespace SharpGLTF.Cesium
 {
     [Category("Toolkit.Scenes")]
     public class ExtStructuralMetadataTests
@@ -12,7 +19,45 @@ namespace SharpGLTF
             CesiumExtensions.RegisterExtensions();
         }
 
+        [Test(Description ="First test with ext_structural_metadata")]
+        public void TriangleWithMetadataTest()
+        {
+            TestContext.CurrentContext.AttachGltfValidatorLinks();
+
+            // Create a triangle with feature ID custom vertex attribute
+            var featureId = 1;
+            var material = MaterialBuilder.CreateDefault().WithDoubleSide(true);
+
+            var mesh = new MeshBuilder<VertexPositionNormal, VertexWithFeatureId, VertexEmpty>("mesh");
+            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(vt0, vt1, vt2);
+            var scene = new SceneBuilder();
+            scene.AddRigidMesh(mesh, Matrix4x4.Identity);
+            var model = scene.ToGltf2();
+
+            var featureIdAttribute = new MeshExtMeshFeatureID(1, 0);
 
+            // Set the FeatureIds
+            var featureIds = new List<MeshExtMeshFeatureID>() { featureIdAttribute };
+            model.LogicalMeshes[0].Primitives[0].SetFeatureIds(featureIds);
 
+            model.InitializeMetadataExtension("propertyTable", 1);
+
+            // todo add metadata
+
+            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.gltf");
+            model.AttachToCurrentTest("cesium_ext_structural_metadata_basic_triangle.plotly");
+
+        }
     }
 }

+ 1 - 1
tests/SharpGLTF.Cesium.Tests/SharpGLTF.Cesium.Tests.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
-    <TargetFrameworks>net471;net6.0-windows;net8.0-windows</TargetFrameworks>
+    <TargetFrameworks>net471;net8.0-windows</TargetFrameworks>
     <IsPackable>false</IsPackable>
     <RootNamespace>SharpGLTF</RootNamespace>
     <LangVersion>latest</LangVersion>

+ 17 - 0
tests/SharpGLTF.Cesium.Tests/VertexBuilder.cs

@@ -0,0 +1,17 @@
+using SharpGLTF.Geometry;
+using SharpGLTF.Geometry.VertexTypes;
+using System.Numerics;
+
+namespace SharpGLTF
+{
+    public static class VertexBuilder
+    {
+        internal static VertexBuilder<VertexPositionNormal, VertexWithFeatureId, VertexEmpty> GetVertexWithFeatureId(Vector3 position, Vector3 normal, int featureid)
+        {
+            var vp0 = new VertexPositionNormal(position, normal);
+            var vb0 = new VertexBuilder<VertexPositionNormal, VertexWithFeatureId, VertexEmpty>(vp0, featureid);
+            return vb0;
+        }
+
+    }
+}