Skip to content

Commit

Permalink
adding first ext_structural_metadata test
Browse files Browse the repository at this point in the history
  • Loading branch information
bertt committed Nov 30, 2023
1 parent fbd3d06 commit cac94fe
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 20 deletions.
39 changes: 32 additions & 7 deletions src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -40,15 +37,17 @@ public StructuralMetadataSchema()
_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
Expand All @@ -69,8 +68,34 @@ 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)
{
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;
}
}
}
1 change: 0 additions & 1 deletion src/SharpGLTF.Cesium/Schema2/MeshExtMeshFeatures.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using SharpGLTF.Validation;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace SharpGLTF.Schema2
{
Expand Down
13 changes: 3 additions & 10 deletions tests/SharpGLTF.Cesium.Tests/ExtMeshFeaturesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace SharpGLTF.Cesium
{
using VBTexture1 = VertexBuilder<VertexPosition, VertexTexture1, VertexEmpty>;


[Category("Toolkit.Scenes")]
public class ExtMeshFeaturesTests
{
Expand All @@ -37,9 +36,9 @@ public void FeaturesIdAttributeTest()
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();
Expand Down Expand Up @@ -135,11 +134,5 @@ public void FeaturesIdTextureTest()
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;
}
}
}
47 changes: 46 additions & 1 deletion tests/SharpGLTF.Cesium.Tests/ExtStructuralMetadataTests.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,7 +19,45 @@ public void SetUp()
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");

}
}
}
2 changes: 1 addition & 1 deletion tests/SharpGLTF.Cesium.Tests/SharpGLTF.Cesium.Tests.csproj
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
17 changes: 17 additions & 0 deletions tests/SharpGLTF.Cesium.Tests/VertexBuilder.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
}

0 comments on commit cac94fe

Please sign in to comment.