Browse Source

add first complex type array of byte

Bert Temme 2 years ago
parent
commit
877af017d9

+ 33 - 26
src/SharpGLTF.Cesium/BinaryTable.cs

@@ -24,7 +24,8 @@ namespace SharpGLTF
             
             if (typeof(T) == typeof(string))
             {
-                return GetStringsAsBytes(values.Cast<string>().ToArray());
+                // todo: implement string type
+                throw new NotImplementedException();
             }
             else if (typeof(T).IsPrimitive)
             {
@@ -47,17 +48,23 @@ namespace SharpGLTF
             }
         }
 
-        /// <summary>
-        /// Creates a list of offsets for a list of strings
-        /// see https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata#strings
-        /// </summary>
-        /// <param name="strings"></param>
-        /// <returns></returns>
-        public static byte[] GetOffsetBuffer(IReadOnlyList<string> strings)
+        //public static byte[] GetOffsetBuffer<T>(List<List<T>> values)
+        //{
+        //    var offsetBuffer = GetOffsets(values);
+        //    var offsetBytes = GetBytes(offsetBuffer);
+        //    return offsetBytes;
+        //}
+
+        public static List<int> GetOffsets<T>(List<List<T>> values)
         {
-            var offsetBuffer = GetOffsets(strings);
-            var offsetBytes = GetBytes(offsetBuffer);
-            return offsetBytes;
+            var offsets = new List<int>() { 0 };
+            foreach (var value in values)
+            {
+                var length = GetBytes(value).Length;
+
+                offsets.Add(offsets.Last() + (int)length);
+            }
+            return offsets;
         }
 
         public static int GetSize<T>()
@@ -70,22 +77,22 @@ namespace SharpGLTF
             return size;
         }
 
-        private static byte[] GetStringsAsBytes(IReadOnlyList<string> values)
-        {
-            var res = string.Join("", values);
-            return Encoding.UTF8.GetBytes(res);
-        }
+        //private static byte[] GetStringsAsBytes(IReadOnlyList<string> values)
+        //{
+        //    var res = string.Join("", values);
+        //    return Encoding.UTF8.GetBytes(res);
+        //}
 
-        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);
+        //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;
-        }
+        //        offsets.Add(offsets.Last() + length);
+        //    }
+        //    return offsets;
+        //}
     }
 }

+ 34 - 6
src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs

@@ -81,7 +81,19 @@ namespace SharpGLTF.Schema2
             }
         }
 
-        public static PropertyTableProperty GetPropertyTableProperty<T>(this ModelRoot model, IReadOnlyList<T> values)
+        public static PropertyTableProperty GetPropertyTableProperty<T>(this ModelRoot model, List<List<T>> values)
+        {
+            var propertyTableProperty = new PropertyTableProperty();
+            int logicalIndex = GetBufferView(model, values);
+            propertyTableProperty.Values = logicalIndex;
+
+            var offsets = BinaryTable.GetOffsets(values);
+            int logicalIndexOffsets = GetBufferView(model, offsets);
+            propertyTableProperty.ArrayOffsets = logicalIndexOffsets;
+            return propertyTableProperty;
+        }
+
+        public static PropertyTableProperty GetPropertyTableProperty<T>(this ModelRoot model, List<T> values)
         {
             var propertyTableProperty = new PropertyTableProperty();
             int logicalIndex = GetBufferView(model, values);
@@ -89,16 +101,26 @@ namespace SharpGLTF.Schema2
             return propertyTableProperty;
         }
 
-        private static int GetBufferView<T>(this ModelRoot model, IReadOnlyList<T> values)
+        private static int GetBufferView<T>(this ModelRoot model, List<T> values)
         {
-            var bytesFloat32 = BinaryTable.GetBytes(values);
-            var bufferView = model.UseBufferView(bytesFloat32);
+            var bytes = BinaryTable.GetBytes(values);
+            var bufferView = model.UseBufferView(bytes);
             int logicalIndex = bufferView.LogicalIndex;
             return logicalIndex;
         }
 
-
-
+        private static int GetBufferView<T>(this ModelRoot model, List<List<T>> values)
+        {
+            var bytes = new List<byte>();
+            foreach (var value in values)
+            {
+                var b = BinaryTable.GetBytes(value);
+                bytes.AddRange(b);
+            }
+            var bufferView = model.UseBufferView(bytes.ToArray());
+            int logicalIndex = bufferView.LogicalIndex;
+            return logicalIndex;
+        }
     }
 
     public partial class EXTStructuralMetaDataRoot
@@ -428,6 +450,12 @@ namespace SharpGLTF.Schema2
             get { return _values; }
             set { _values = value; }
         }
+
+        public int? ArrayOffsets
+        {
+            get { return _arrayOffsets; }
+            set { _arrayOffsets = value; }
+        }
     }
 }
 

+ 0 - 11
tests/SharpGLTF.Cesium.Tests/BinaryTableTests.cs

@@ -23,17 +23,6 @@ namespace SharpGLTF
             Assert.Throws<NotImplementedException>(() => BinaryTable.GetBytes(ints));
         }
 
-        [Test]
-        public void TestOffsetBufferStrings()
-        {
-            var strings = new List<string> { "hello, ", "world" };
-            var offsetBytes = BinaryTable.GetOffsetBuffer(strings);
-            Assert.That(offsetBytes.Length, Is.EqualTo(12));
-            Assert.That(BitConverter.ToInt32(offsetBytes, 0), Is.EqualTo(0));
-            Assert.That(BitConverter.ToInt32(offsetBytes, 4), Is.EqualTo(strings[0].Length));
-            Assert.That(BitConverter.ToInt32(offsetBytes, 8), Is.EqualTo(strings[0].Length + strings[1].Length));
-        }
-
         private List<T> GetTestArray<T>()
         {
             var l = new List<T>();

+ 13 - 10
tests/SharpGLTF.Cesium.Tests/ExtStructuralMetadataTests.cs

@@ -30,9 +30,9 @@ namespace SharpGLTF.Cesium
             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), 0);
-            var vt1 = VertexBuilder.GetVertexWithFeatureId(new Vector3(10, 0, 0), new Vector3(0, 0, 1), 0);
-            var vt2 = VertexBuilder.GetVertexWithFeatureId(new Vector3(0, 10, 0), new Vector3(0, 0, 1), 0);
+            var vt0 = VertexBuilder.GetVertexWithFeatureId(new Vector3(-1, 0, 0), new Vector3(0, 0, 1), 0);
+            var vt1 = VertexBuilder.GetVertexWithFeatureId(new Vector3(1, 0, 0), new Vector3(0, 0, 1), 0);
+            var vt2 = VertexBuilder.GetVertexWithFeatureId(new Vector3(0, 1, 0), new Vector3(0, 0, 1), 0);
 
             prim.AddTriangle(vt0, vt1, vt2);
 
@@ -55,7 +55,7 @@ namespace SharpGLTF.Cesium
             uint8ArrayProperty.Description = "An example property, with type ARRAY, with component type UINT8, normalized, and variable length";
             uint8ArrayProperty.Type = ElementType.SCALAR;
             uint8ArrayProperty.ComponentType = DataType.UINT8;
-            uint8ArrayProperty.Normalized = true;
+            uint8ArrayProperty.Normalized = false;
             uint8ArrayProperty.Array = true;
 
             exampleMetadataClass.Properties.Add("example_variable_length_ARRAY_normalized_UINT8", uint8ArrayProperty);
@@ -63,21 +63,24 @@ namespace SharpGLTF.Cesium
             schema.Classes.Add("exampleMetadataClass", exampleMetadataClass);
 
             var examplePropertyTable = new PropertyTable("exampleMetadataClass", 1, "Example property table");
-            // todo add the complex type properties
-            var float32Property = model.GetPropertyTableProperty(new List<float>() { 100 });
-            examplePropertyTable.Properties.Add("example_variable_length_ARRAY_normalized_UINT8", float32Property);
+            var list0 = new List<byte>() { 0, 1, 2, 3, 4, 5, 6, 7 };
+            var list2 = new List<List<byte>>() {
+                list0
+            };
+
+            var property = model.GetPropertyTableProperty(list2);
+            examplePropertyTable.Properties.Add("example_variable_length_ARRAY_normalized_UINT8", property);
+
+            // todo add more complex type properties
 
             model.SetPropertyTable(examplePropertyTable, schema);
 
-            // create files
             var ctx = new ValidationResult(model, ValidationMode.Strict, true);
             model.AttachToCurrentTest("cesium_ext_structural_metadata_complex_types.glb");
             model.AttachToCurrentTest("cesium_ext_structural_metadata_complex_types.gltf");
             model.AttachToCurrentTest("cesium_ext_structural_metadata_complex_types.plotly");
         }
 
-
-
         [Test(Description = "ext_structural_metadata with multiple classes")]
         // Sample see https://github.com/CesiumGS/3d-tiles-samples/blob/main/glTF/EXT_structural_metadata/MultipleClasses/MultipleClasses.gltf
         public void MultipleClassesTest()