Browse Source

add binarytable tests

Bert Temme 2 years ago
parent
commit
8cef54a968

+ 91 - 13
src/SharpGLTF.Cesium/BinaryTable.cs

@@ -1,11 +1,57 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
 
 
 namespace SharpGLTF
 namespace SharpGLTF
 {
 {
+    /// <summary>
+    /// Function for converting data into binary buffers
+    /// Specs see https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata#binary-table-format
+    /// </summary>
     public static class BinaryTable
     public static class BinaryTable
     {
     {
+        /// <summary>
+        /// Converts a list of primitive types into a byte array
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="values"></param>
+        /// <returns>byte array</returns>
+        public static byte[] GetBytes<T>(IReadOnlyList<T> values)
+        {
+            Guard.IsTrue(values.Count > 0, nameof(values), "values must have at least one element");
+            
+            if (typeof(T) == typeof(string))
+            {
+                return GetStringsAsBytes(values.Cast<string>().ToArray());
+            }
+            else if (typeof(T).IsPrimitive)
+            {
+                if(typeof(T) == typeof(bool))
+                {
+                    // when implementing bool, create a bitstream
+                    // see https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata#booleans
+                    throw new NotImplementedException();
+                }
+                var size = GetSize<T>();
+                var result = new byte[values.Count * size];
+                Buffer.BlockCopy(values.ToArray(), 0, result, 0, result.Length);
+                return result;
+            }
+            else
+            {
+                // other types (like enum, mat2, mat3, mat4, vec2, vec3, vec4, array (fixed length, variable length)) are not implemented
+                // see https://github.com/CesiumGS/3d-tiles/tree/main/specification/Metadata#binary-table-format
+                throw new NotImplementedException();
+            }
+        }
+
+        /// <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(IReadOnlyList<string> strings)
         {
         {
             var offsetBuffer = GetOffsets(strings);
             var offsetBuffer = GetOffsets(strings);
@@ -13,13 +59,28 @@ namespace SharpGLTF
             return offsetBytes;
             return offsetBytes;
         }
         }
 
 
-        public static byte[] GetBytes<T>(IReadOnlyList<T> values)
+        public static int GetSize<T>()
         {
         {
+            var isValueType = typeof(T).IsValueType;
+            Guard.IsTrue(isValueType, nameof(T), "T must be a value type");
+
             var type = typeof(T);
             var type = typeof(T);
             int size = 0;
             int size = 0;
-            if (type == typeof(float))
+            if (type == typeof(sbyte))
             {
             {
-                size = sizeof(float);
+                size = sizeof(sbyte);
+            }
+            else if (type == typeof(byte))
+            {
+                size = sizeof(byte);
+            }
+            else if (type == typeof(short))
+            {
+                size = sizeof(short);
+            }
+            else if (type == typeof(ushort))
+            {
+                size = sizeof(ushort);
             }
             }
             else if (type == typeof(int))
             else if (type == typeof(int))
             {
             {
@@ -29,10 +90,33 @@ namespace SharpGLTF
             {
             {
                 size = sizeof(uint);
                 size = sizeof(uint);
             }
             }
+            else if (type == typeof(long))
+            {
+                size = sizeof(long);
+            }
+            else if (type == typeof(ulong))
+            {
+                size = sizeof(ulong);
+            }
+            else if (type == typeof(float))
+            {
+                size = sizeof(float);
+            }
+            else if (type == typeof(double))
+            {
+                size = sizeof(double);
+            }
+            else if (type == typeof(bool))
+            {
+                size = sizeof(bool);
+            }
+            return size;
+        }
 
 
-            var result = new byte[values.Count * size];
-            System.Buffer.BlockCopy(values.ToArray(), 0, result, 0, result.Length);
-            return result;
+        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)
         private static List<uint> GetOffsets(IReadOnlyList<string> strings)
@@ -46,11 +130,5 @@ namespace SharpGLTF
             }
             }
             return offsets;
             return offsets;
         }
         }
-
-        public static byte[] GetStringsAsBytes(IReadOnlyList<string> values)
-        {
-            var res = string.Join("", values);
-            return Encoding.UTF8.GetBytes(res);
-        }
     }
     }
 }
 }

+ 0 - 16
src/SharpGLTF.Cesium/Validator.cs

@@ -1,16 +0,0 @@
-using SharpGLTF.Schema2;
-
-namespace SharpGLTF
-{
-    internal static class Validator
-    {
-        internal static void ValidateAccessor(ModelRoot model, Accessor accessor)
-        {
-            Guard.NotNull(accessor, nameof(accessor));
-            Guard.MustShareLogicalParent(model, "this", accessor, nameof(accessor));
-            Guard.IsTrue(accessor.Encoding == EncodingType.UNSIGNED_INT, nameof(accessor));
-            Guard.IsTrue(accessor.Dimensions == DimensionType.SCALAR, nameof(accessor));
-            Guard.IsFalse(accessor.Normalized, nameof(accessor));
-        }
-    }
-}

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

@@ -0,0 +1,74 @@
+using NUnit.Framework;
+using System;
+using System.Collections.Generic;
+
+namespace SharpGLTF
+{
+    public class BinaryTableTests
+    {
+        [Test]
+        public void TestBinaryConversion()
+        {
+            var bytes = BinaryTable.GetBytes(GetTestArray<sbyte>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<sbyte>() * 2));
+
+            bytes = BinaryTable.GetBytes(GetTestArray<byte>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<byte>() * 2));
+
+            bytes = BinaryTable.GetBytes(GetTestArray<short>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<short>() * 2));
+
+            bytes = BinaryTable.GetBytes(GetTestArray<ushort>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<ushort>() * 2));
+
+            bytes = BinaryTable.GetBytes(GetTestArray<int>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<int>() * 2));
+
+            bytes = BinaryTable.GetBytes(GetTestArray<uint>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<uint>() * 2));
+
+            bytes = BinaryTable.GetBytes(GetTestArray<long>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<long>() * 2));
+
+            bytes = BinaryTable.GetBytes(GetTestArray<long>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<long>() * 2));
+
+            bytes = BinaryTable.GetBytes(GetTestArray<ulong>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<ulong>() * 2));
+
+            bytes = BinaryTable.GetBytes(GetTestArray<float>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<float>() * 2));
+
+            bytes = BinaryTable.GetBytes(GetTestArray<double>());
+            Assert.That(bytes.Length, Is.EqualTo(BinaryTable.GetSize<double>() * 2));
+
+            bytes = BinaryTable.GetBytes(new List<string>() { "a", "b" });
+            Assert.That(bytes.Length, Is.EqualTo(2));
+
+            Assert.Throws<NotImplementedException>(() => BinaryTable.GetBytes(new List<bool>() { true, false }));
+            var ints = new List<List<int>>();
+            ints.Add(new List<int>() { 0, 1 });
+            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>();
+            l.Add((T)Convert.ChangeType(0, typeof(T)));
+            l.Add((T)Convert.ChangeType(1, typeof(T)));
+            return l;
+        }
+
+    }
+}

+ 0 - 1
tests/SharpGLTF.Cesium.Tests/ExtInstanceFeaturesTests.cs

@@ -4,7 +4,6 @@ using SharpGLTF.Schema2;
 using SharpGLTF.Transforms;
 using SharpGLTF.Transforms;
 using SharpGLTF.Validation;
 using SharpGLTF.Validation;
 using System.Collections.Generic;
 using System.Collections.Generic;
-using System.Linq;
 using System.Numerics;
 using System.Numerics;
 using System.Text.Json.Nodes;
 using System.Text.Json.Nodes;
 
 

+ 2 - 6
tests/SharpGLTF.Cesium.Tests/CesiumTests.cs → tests/SharpGLTF.Cesium.Tests/ExtPrimitiveOutlineTests.cs

@@ -1,10 +1,6 @@
-using System;
-using System.Linq;
+using System.Linq;
 using System.Numerics;
 using System.Numerics;
-using System.Collections.Generic;
-
 using NUnit.Framework;
 using NUnit.Framework;
-
 using SharpGLTF.Schema2;
 using SharpGLTF.Schema2;
 using SharpGLTF.Geometry;
 using SharpGLTF.Geometry;
 using SharpGLTF.Geometry.VertexTypes;
 using SharpGLTF.Geometry.VertexTypes;
@@ -16,7 +12,7 @@ namespace SharpGLTF
 {
 {
 
 
     [Category("Cesium")]
     [Category("Cesium")]
-    public partial class CesiumTests
+    public partial class ExtPrimitiveOutlineTests
     {
     {
         [SetUp]
         [SetUp]
         public void SetUp()
         public void SetUp()