Browse Source

Improve array types

Bert Temme 1 year ago
parent
commit
7800d11a5f

+ 39 - 10
src/SharpGLTF.Ext.3DTiles/Schema2/Ext.StructuralMetadataRoot.cs

@@ -680,14 +680,21 @@ namespace SharpGLTF.Schema2
 
 
             private ModelRoot _GetModelRoot() => LogicalParent.LogicalParent.LogicalParent;
             private ModelRoot _GetModelRoot() => LogicalParent.LogicalParent.LogicalParent;
 
 
-            public void SetValues2D<T>(List<List<T>> values, bool HasVariableLength = true)
+            public void SetArrayValues<T>(List<List<T>> values)
             {
             {
+                var className = LogicalParent.ClassName;
+                var metadataProperty = GetProperty<T>(className, LogicalKey);
+
+                metadataProperty.Array = true;
+
                 var root = _GetModelRoot();
                 var root = _GetModelRoot();
-                
+
                 int logicalIndex = GetBufferView(root, values);
                 int logicalIndex = GetBufferView(root, values);
                 Values = logicalIndex;
                 Values = logicalIndex;
 
 
-                if (HasVariableLength)
+                var hasVariableLength = HasVariableLength<T>(values);
+
+                if (HasVariableLength<T>(values) || typeof(T) == typeof(string))
                 {
                 {
                     // if the array has items of variable length, create arraysOffsets bufferview
                     // if the array has items of variable length, create arraysOffsets bufferview
                     var arrayOffsets = BinaryTable.GetArrayOffsets(values);
                     var arrayOffsets = BinaryTable.GetArrayOffsets(values);
@@ -701,18 +708,17 @@ namespace SharpGLTF.Schema2
                         int offsets = GetBufferView(root, stringOffsets);
                         int offsets = GetBufferView(root, stringOffsets);
                         StringOffsets = offsets;
                         StringOffsets = offsets;
                     }
                     }
-                }                
+                }
+                else
+                {
+                    metadataProperty.Count = values[0].Count;
+                }
             }
             }
 
 
             public void SetValues<T>(params T[] values)
             public void SetValues<T>(params T[] values)
             {
             {
                 var className = LogicalParent.ClassName;
                 var className = LogicalParent.ClassName;
-                var metadataClass = LogicalParent.LogicalParent.Schema.Classes[className];
-                Guard.IsTrue(metadataClass != null, nameof(className), $"Schema class {className} must be defined");
-                metadataClass.Properties.TryGetValue(LogicalKey, out var metadataProperty);
-                Guard.IsTrue(metadataProperty != null, nameof(LogicalKey), $"Property {LogicalKey} in {className} must be defined");
-
-                CheckElementTypes<T>(metadataProperty);
+                GetProperty<T>(className, LogicalKey);
 
 
                 var root = _GetModelRoot();
                 var root = _GetModelRoot();
 
 
@@ -731,6 +737,29 @@ namespace SharpGLTF.Schema2
                 }
                 }
             }
             }
 
 
+            private StructuralMetadataClassProperty GetProperty<T>(string className, string key)
+            {
+                var metadataClass = LogicalParent.LogicalParent.Schema.Classes[className];
+                Guard.IsTrue(metadataClass != null, nameof(className), $"Schema class {className} must be defined");
+                metadataClass.Properties.TryGetValue(key, out var metadataProperty);
+                Guard.IsTrue(metadataProperty != null, nameof(key), $"Property {key} in {className} must be defined");
+
+                CheckElementTypes<T>(metadataProperty);
+
+                return metadataProperty;
+            }
+
+            private bool HasVariableLength<T>(List<List<T>> values)
+            {
+                int length = values[0].Count;
+                for (int i = 1; i < values.Count; i++)
+                {
+                    if (values[i].Count != length) return true;
+                }
+                return false;
+            }
+
+
             private void CheckElementTypes<T>(StructuralMetadataClassProperty metadataProperty)
             private void CheckElementTypes<T>(StructuralMetadataClassProperty metadataProperty)
             {
             {
                 var elementType = metadataProperty.Type;
                 var elementType = metadataProperty.Type;

+ 6 - 6
tests/SharpGLTF.Cesium.Tests/ExtStructuralMetadataTests.cs

@@ -587,26 +587,26 @@ namespace SharpGLTF.Schema2.Tiles3D
             bytes.Add(new List<byte>() { 0, 1, 2, 3, 4, 5, 6, 7 });
             bytes.Add(new List<byte>() { 0, 1, 2, 3, 4, 5, 6, 7 });
             examplePropertyTable
             examplePropertyTable
                 .UseProperty(uint8ArrayProperty)
                 .UseProperty(uint8ArrayProperty)
-                .SetValues2D(bytes);
+                .SetArrayValues(bytes);
 
 
             var bools = new List<List<bool>>();
             var bools = new List<List<bool>>();
             bools.Add(new List<bool>() { true, false, true, false });
             bools.Add(new List<bool>() { true, false, true, false });
             examplePropertyTable
             examplePropertyTable
                 .UseProperty(fixedLengthBooleanProperty)
                 .UseProperty(fixedLengthBooleanProperty)
-                .SetValues2D(bools, false);
+                .SetArrayValues(bools);
 
 
             var strings = new List<List<string>>();
             var strings = new List<List<string>>();
             strings.Add(new List<string>() { "Example string 1", "Example string 2", "Example string 3" });
             strings.Add(new List<string>() { "Example string 1", "Example string 2", "Example string 3" });
             examplePropertyTable
             examplePropertyTable
                 .UseProperty(variableLengthStringArrayProperty)
                 .UseProperty(variableLengthStringArrayProperty)
-                .SetValues2D(strings);
+                .SetArrayValues(strings);
 
 
             // Fill property table with enum values
             // Fill property table with enum values
-            var ints = new List<List<int>>();
-            ints.Add(new List<int>() { 0, 1 });
+            var shorts = new List<List<short>>();
+            shorts.Add(new List<short>() { 0, 1 });
             examplePropertyTable
             examplePropertyTable
                 .UseProperty(fixed_length_ARRAY_ENUM)
                 .UseProperty(fixed_length_ARRAY_ENUM)
-                .SetValues2D(ints, false);
+                .SetArrayValues(shorts);
 
 
             // add to primitive            
             // add to primitive            
             var featureId = new FeatureIDBuilder(1, 0, examplePropertyTable);
             var featureId = new FeatureIDBuilder(1, 0, examplePropertyTable);