Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cesium ext structural metadata #206

Merged
merged 61 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
fdb0680
initial coding ext_structural_metadata
bertt Nov 29, 2023
8611594
got ext_structural_ematdata code generated
bertt Nov 29, 2023
9a893a7
update branch
bertt Nov 30, 2023
fbd3d06
start testing ext_structural_metadata
bertt Nov 30, 2023
cac94fe
adding first ext_structural_metadata test
bertt Nov 30, 2023
2f7d29a
update branch
bertt Dec 5, 2023
dd29197
code cleanup in EXT_Instance_Features
bertt Dec 5, 2023
4b4a974
got the ext_structural_metada - propertyTable working
bertt Dec 5, 2023
8cef54a
add binarytable tests
bertt Dec 6, 2023
22c9b41
add StructuralMetadataSchema
bertt Dec 6, 2023
02f51f3
add more metadata testing
bertt Dec 7, 2023
cf54bdf
code cleanup
bertt Dec 8, 2023
64c3782
add pointcloud test
bertt Dec 12, 2023
d58067b
trying to add color
bertt Dec 13, 2023
1e3de30
implement setColor and MaxColors to 1
bertt Dec 13, 2023
02780d7
fix the pointcloud colors
bertt Dec 13, 2023
1691d5d
get the pointcloud demo with custom attributes working
bertt Dec 13, 2023
9f86549
use schema or schemaUri
bertt Dec 13, 2023
98b56c4
start multiple classes test
bertt Dec 13, 2023
9097ac7
update branch
bertt Dec 13, 2023
06b5950
change null check
bertt Dec 13, 2023
1702dd7
add more to MultipleClasses test
bertt Dec 13, 2023
efb9534
fix the test
bertt Dec 13, 2023
f62c3ef
using OneOf
bertt Dec 14, 2023
dbbf989
improve check one of schema versus schemaUri
bertt Dec 14, 2023
0d4d8dd
refactor propertyTables
bertt Dec 14, 2023
14958a6
fix tests
bertt Dec 14, 2023
dc2ea46
finsih MultipleTables test
bertt Dec 14, 2023
84c7a40
starting with complextypes test
bertt Dec 14, 2023
46a033e
fix tests
bertt Dec 14, 2023
877af01
add first complex type array of byte
bertt Dec 15, 2023
b0e9423
fix the tests
bertt Dec 15, 2023
acfe0b1
add booleans complex types
bertt Dec 15, 2023
4e2344d
add array strings complex type
bertt Dec 18, 2023
98363a6
Merge branch 'master' into add_cesium_ext_structural_metadata
bertt Dec 19, 2023
242ee28
add trimming attribute to custom vertices
bertt Dec 19, 2023
099ea51
add enum complex type
bertt Dec 19, 2023
0feba60
add vector2/vector3/vector4 attributes
bertt Dec 19, 2023
c24b072
add Matrix4x4 as attribute
bertt Dec 19, 2023
09f3ffd
add some tests for binary conversion
bertt Dec 19, 2023
4459b77
add test with multiple feature ids and properties
bertt Dec 19, 2023
815e7c7
fix tests
bertt Dec 19, 2023
965d2a0
fix test 2
bertt Dec 19, 2023
c16d6e6
add textures and propertytable test
bertt Dec 20, 2023
b9a8e7f
fix the test
bertt Dec 20, 2023
3dab41c
add checks for textures
bertt Dec 20, 2023
bce7499
fix simplepropertytexture
bertt Dec 20, 2023
2afa25e
add StructuralMetadataPrimitive
bertt Dec 21, 2023
9d85738
fix the tests
bertt Dec 21, 2023
bd754e3
working on validation
bertt Dec 21, 2023
72a56be
refactor validation
bertt Dec 21, 2023
41a265b
refactor validation
bertt Dec 22, 2023
0183497
use WithMetallicRoughness(imageBuilder)
bertt Dec 22, 2023
5bd1e1a
fixed null checks
bertt Dec 22, 2023
67694cc
add validation on count
bertt Dec 22, 2023
c570bd5
fix references check in ext_instance_features
bertt Dec 22, 2023
13fd9d7
fix verbose codegen project
bertt Dec 27, 2023
187e405
remove Activator.CreateInstance to get size
bertt Dec 27, 2023
6dfc9a2
use RuntimeHelpers.IsReferenceOrContainsReferences<T>
bertt Dec 27, 2023
e75dd9d
reverse target framework netstandard20 check
bertt Dec 27, 2023
a9af095
move BinaryTable class to Memory workspace
bertt Dec 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/SharpGLTF.Cesium/BinaryTable.cs
Original file line number Diff line number Diff line change
@@ -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);
}
bertt marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
}
82 changes: 23 additions & 59 deletions src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,56 @@ public partial class EXTStructuralMetaDataRoot
internal EXTStructuralMetaDataRoot(ModelRoot modelRoot)
{
this.modelRoot = modelRoot;
_propertyTables = new List<PropertyTable>();
}

internal List<PropertyTable> PropertyTables
public List<PropertyTable> 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)
{
}
}

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;
_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; }
}
}
}
2 changes: 0 additions & 2 deletions src/SharpGLTF.Cesium/Schema2/MeshExtInstanceFeatures.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using SharpGLTF.Validation;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace SharpGLTF.Schema2
{
public partial class MeshExtInstanceFeatures
Expand Down
3 changes: 3 additions & 0 deletions src/SharpGLTF.Cesium/Schema2/MeshExtMeshFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public MeshExtMeshFeatureIDTexture(List<int> channels, int? index = null, int? t

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)
{
_featureCount = featureCount;
Expand Down
28 changes: 10 additions & 18 deletions tests/SharpGLTF.Cesium.Tests/ExtStructuralMetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,32 @@ 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 mesh = new MeshBuilder<VertexPosition>("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(new VertexPosition(-10, 0, 0), new VertexPosition(10, 0, 0), new VertexPosition(0, 10, 0));

prim.AddTriangle(vt0, vt1, vt2);
var scene = new SceneBuilder();
scene.AddRigidMesh(mesh, Matrix4x4.Identity);
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);
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");

}
}
}
Loading