Browse Source

add booleans complex types

Bert Temme 2 years ago
parent
commit
acfe0b1e52

+ 6 - 29
src/SharpGLTF.Cesium/BinaryTable.cs

@@ -1,8 +1,8 @@
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.InteropServices;
-using System.Text;
 
 namespace SharpGLTF
 {
@@ -31,10 +31,12 @@ namespace SharpGLTF
             {
                 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 bits = new BitArray(values.Cast<bool>().ToArray());
+                    byte[] ret = new byte[(bits.Length - 1) / 8 + 1];
+                    bits.CopyTo(ret, 0);
+                    return ret;
                 }
+
                 var size = GetSize<T>();
                 var result = new byte[values.Count * size];
                 Buffer.BlockCopy(values.ToArray(), 0, result, 0, result.Length);
@@ -48,13 +50,6 @@ namespace SharpGLTF
             }
         }
 
-        //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 offsets = new List<int>() { 0 };
@@ -76,23 +71,5 @@ namespace SharpGLTF
             int size = Marshal.SizeOf(Activator.CreateInstance(type));
             return size;
         }
-
-        //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);
-
-        //        offsets.Add(offsets.Last() + length);
-        //    }
-        //    return offsets;
-        //}
     }
 }

+ 12 - 4
src/SharpGLTF.Cesium/Schema2/EXTStructuralMetaDataRoot.cs

@@ -81,15 +81,18 @@ namespace SharpGLTF.Schema2
             }
         }
 
-        public static PropertyTableProperty GetPropertyTableProperty<T>(this ModelRoot model, List<List<T>> values)
+        public static PropertyTableProperty GetArrayPropertyTableProperty<T>(this ModelRoot model, List<List<T>> values, bool CreateArrayOffsets = true)
         {
             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;
+            if (CreateArrayOffsets)
+            {
+                var offsets = BinaryTable.GetOffsets(values);
+                int logicalIndexOffsets = GetBufferView(model, offsets);
+                propertyTableProperty.ArrayOffsets = logicalIndexOffsets;
+            }
             return propertyTableProperty;
         }
 
@@ -402,6 +405,11 @@ namespace SharpGLTF.Schema2
 
         }
 
+        public int? Count
+        {
+            get { return _count; }
+            set { _count = value; }
+        }
 
     }
 

+ 7 - 1
tests/SharpGLTF.Cesium.Tests/BinaryTableTests.cs

@@ -18,7 +18,13 @@ namespace SharpGLTF
             //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 }));
+            bytes  = BinaryTable.GetBytes(new List<bool>() { true, false });
+            Assert.That(bytes.Length, Is.EqualTo(1));
+            // create a bit arrat frin the byte array
+            var bits = new System.Collections.BitArray(bytes);
+            Assert.That(bits[0] == true);
+            Assert.That(bits[1] == false);
+
             var ints = new List<List<int>>();
             ints.Add(new List<int>() { 0, 1 });
             Assert.Throws<NotImplementedException>(() => BinaryTable.GetBytes(ints));

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

@@ -60,6 +60,15 @@ namespace SharpGLTF.Cesium
 
             exampleMetadataClass.Properties.Add("example_variable_length_ARRAY_normalized_UINT8", uint8ArrayProperty);
 
+            var fixedLengthBooleanProperty = new ClassProperty();
+            fixedLengthBooleanProperty.Name = "Example fixed-length ARRAY BOOLEAN property";
+            fixedLengthBooleanProperty.Description = "An example property, with type ARRAY, with component type BOOLEAN, and fixed length ";
+            fixedLengthBooleanProperty.Type = ElementType.BOOLEAN;
+            fixedLengthBooleanProperty.Array = true;
+            fixedLengthBooleanProperty.Count = 4;
+
+            exampleMetadataClass.Properties.Add("example_fixed_length_ARRAY_BOOLEAN", fixedLengthBooleanProperty);
+
             schema.Classes.Add("exampleMetadataClass", exampleMetadataClass);
 
             var examplePropertyTable = new PropertyTable("exampleMetadataClass", 1, "Example property table");
@@ -68,9 +77,17 @@ namespace SharpGLTF.Cesium
                 list0
             };
 
-            var property = model.GetPropertyTableProperty(list2);
+            var property = model.GetArrayPropertyTableProperty(list2);
             examplePropertyTable.Properties.Add("example_variable_length_ARRAY_normalized_UINT8", property);
 
+            var booleans = new List<bool>() { true, false, true, false };
+            var booleansList = new List<List<bool>>()
+            {
+                booleans
+            };
+            var propertyBooleansList = model.GetArrayPropertyTableProperty(booleansList, false);
+            examplePropertyTable.Properties.Add("example_fixed_length_ARRAY_BOOLEAN", propertyBooleansList);
+
             // todo add more complex type properties
 
             model.SetPropertyTable(examplePropertyTable, schema);