Răsfoiți Sursa

using OneOf

Bert Temme 2 ani în urmă
părinte
comite
f62c3efd7a

+ 22 - 25
src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs

@@ -1,4 +1,5 @@
-using SharpGLTF.Validation;
+using OneOf;
+using SharpGLTF.Validation;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -11,8 +12,7 @@ namespace SharpGLTF.Schema2
         public static void SetPropertyTexture(
             this ModelRoot modelRoot,
             PropertyTexture propertyTexture,
-            StructuralMetadataSchema schema = null,
-            Uri schemaUri = null
+            OneOf<StructuralMetadataSchema, Uri> schema
             )
         {
             if (propertyTexture == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
@@ -20,50 +20,52 @@ namespace SharpGLTF.Schema2
             var ext = modelRoot.UseExtension<EXTStructuralMetaDataRoot>();
             ext.PropertyTextures.Clear();
             ext.PropertyTextures.Add(propertyTexture);
-            ext.AddSchema(schema, schemaUri);
+            ext.AddSchema(schema);
         }
 
         public static void SetPropertyAttribute(
             this ModelRoot modelRoot,
             PropertyAttribute propertyAttribute,
-            StructuralMetadataSchema schema = null,
-            Uri schemaUri = null)
+            OneOf<StructuralMetadataSchema, Uri> schema)
         {
             if (propertyAttribute == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
 
             var ext = modelRoot.UseExtension<EXTStructuralMetaDataRoot>();
             ext.PropertyAttributes.Clear();
             ext.PropertyAttributes.Add(propertyAttribute);
-            ext.AddSchema(schema, schemaUri);
+            ext.AddSchema(schema);
         }
 
         public static void SetPropertyTable(
             this ModelRoot modelRoot,
             Dictionary<string, List<int>> attributes,
-            string name = "PropertyTable",
-            StructuralMetadataSchema schema = null,
-            Uri schemaUri = null
+            OneOf<StructuralMetadataSchema, Uri> schema,
+            string name = "PropertyTable"
             )
         {
             if (attributes == null) { modelRoot.RemoveExtensions<EXTStructuralMetaDataRoot>(); return; }
 
             var ext = modelRoot.UseExtension<EXTStructuralMetaDataRoot>();
             ext.PropertyTables.Clear();
-            ext.PropertyTables.Add(GetPropertyTable(modelRoot, attributes, name, schema));
-            ext.AddSchema(schema, schemaUri);
+            var propertyTable = GetPropertyTable(modelRoot, attributes, schema, name);
+            ext.PropertyTables.Add(propertyTable);
+            ext.AddSchema(schema);
         }
 
 
         private static PropertyTable GetPropertyTable(
             ModelRoot modelRoot,
             Dictionary<string, List<int>> attributes,
-            string name = "PropertyTable",
-            StructuralMetadataSchema schema = null
-            // todo: use? Uri schemaUri = null
+            OneOf<StructuralMetadataSchema, Uri> schema1,
+            string name = "PropertyTable"
             )
         {
             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;
 
             foreach (var property in firstClass.Properties)
@@ -99,17 +101,12 @@ namespace SharpGLTF.Schema2
             _propertyTextures = new List<PropertyTexture>();
         }
 
-        internal void AddSchema(StructuralMetadataSchema schema, Uri schemaUri)
+        internal void AddSchema(OneOf<StructuralMetadataSchema, Uri> schema)
         {
-            if (schema != null)
-            {
-                Schema = schema;
-            }
-
-            if (schemaUri != null)
-            {
-                SchemaUri = schemaUri.ToString();
-            }
+            schema.Switch(
+                StructuralMetadataSchema => _schema = StructuralMetadataSchema,
+                Uri => this.SchemaUri = Uri.ToString()
+                );
         }
 
         internal List<PropertyTable> PropertyTables

+ 4 - 0
src/SharpGLTF.Cesium/SharpGLTF.Cesium.csproj

@@ -20,4 +20,8 @@
     <Folder Include="Schema2\Generated\" />
   </ItemGroup>
 
+  <ItemGroup>
+    <PackageReference Include="OneOf" Version="3.0.263" />
+  </ItemGroup>
+
 </Project>

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

@@ -165,7 +165,8 @@ namespace SharpGLTF.Cesium
             propertyAttribute.Properties["intensity"] = intensityProperty;
             propertyAttribute.Properties["classification"] = classificationProperty;
 
-            model.SetPropertyAttribute(propertyAttribute, schemaUri: new Uri("MetadataSchema.json", UriKind.Relative));
+            var schemaUri = new Uri("MetadataSchema.json", UriKind.Relative);
+            model.SetPropertyAttribute(propertyAttribute, schemaUri );
             var ctx = new ValidationResult(model, ValidationMode.Strict, true);
             model.AttachToCurrentTest("cesium_ext_structural_metadata_with_pointcloud_attributes.glb");
             model.AttachToCurrentTest("cesium_ext_structural_metadata_with_pointcloud_attributes.gltf");