Skip to content

Commit ae87250

Browse files
committed
fix list of booleans serialization
1 parent 5c4bd96 commit ae87250

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/SharpGLTF.Ext.3DTiles/Memory/BinaryTable.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public static class BinaryTable
1616
{
1717
public static List<byte> GetBytesForArray<T>(List<List<T>> values)
1818
{
19+
var type = typeof(T);
20+
if(type == typeof(bool))
21+
{
22+
var booleanBytes = GetBytesBooleans(values.Cast<List<bool>>().ToList());
23+
return booleanBytes;
24+
}
1925
var bytes = new List<byte>();
2026
foreach (var value in values)
2127
{
@@ -26,7 +32,6 @@ public static List<byte> GetBytesForArray<T>(List<List<T>> values)
2632
return bytes;
2733
}
2834

29-
3035
/// <summary>
3136
/// Converts a list of primitive types into a byte array
3237
/// </summary>
@@ -81,6 +86,14 @@ public static byte[] GetBytes<T>(IReadOnlyList<T> values)
8186
}
8287
}
8388

89+
private static List<byte> GetBytesBooleans(List<List<bool>> values)
90+
{
91+
var bits = new BitArray(values.SelectMany(x => x).ToArray());
92+
var boolBytes = new byte[(bits.Length - 1) / 8 + 1];
93+
bits.CopyTo(boolBytes, 0);
94+
return boolBytes.ToList();
95+
}
96+
8497
private static byte[] Matrix4x4ToBytes<T>(IReadOnlyList<T> values)
8598
{
8699
var result = new byte[values.Count * 64];

tests/SharpGLTF.Ext.3DTiles.Tests/Memory/BinaryTableTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ namespace SharpGLTF.Memory
77
{
88
public class BinaryTableTests
99
{
10+
[Test]
11+
public void ConvertListofListofBoolsToBytes()
12+
{
13+
var values = new List<List<bool>>();
14+
values.Add(new List<bool>() { true, false });
15+
values.Add(new List<bool>() { false, true });
16+
var bytes = BinaryTable.GetBytesForArray(values);
17+
// Check size, should be 1 byte for 4 bits
18+
Assert.That(bytes.Count, Is.EqualTo(1));
19+
20+
// read the bits back
21+
var bits = new System.Collections.BitArray(bytes.ToArray());
22+
Assert.That(bits[0], Is.EqualTo(true));
23+
Assert.That(bits[1], Is.EqualTo(false));
24+
Assert.That(bits[2], Is.EqualTo(false));
25+
Assert.That(bits[3], Is.EqualTo(true));
26+
}
27+
1028
[Test]
1129
public void ConvertListofListofIntToBytes()
1230
{

0 commit comments

Comments
 (0)