Просмотр исходного кода

Adding basic Linq support to memory accessors

Vicente Penades 7 лет назад
Родитель
Сommit
8184abdf23

+ 1 - 1
src/glTF2Sharp.DOM/Memory/Accessors.cs

@@ -6,7 +6,7 @@ using System.Text;
 
 namespace glTF2Sharp.Memory
 {
-    public interface IAccessor<T> // : IReadOnlyList<T>
+    public interface IAccessor<T>
         where T : unmanaged
     {
         int Count { get; }

+ 51 - 10
src/glTF2Sharp.DOM/Memory/FloatingAccessors.cs

@@ -2,13 +2,12 @@
 using System.Collections.Generic;
 using System.Text;
 using System.Numerics;
+using System.Collections;
 
 namespace glTF2Sharp.Memory
 {
     
-    using BYTES = ArraySegment<Byte>;
-
-    
+    using BYTES = ArraySegment<Byte>;    
 
     /// <summary>
     /// Helper structure to access any Byte array as an array of <see cref="Schema2.ComponentType"/>
@@ -183,8 +182,11 @@ namespace glTF2Sharp.Memory
         #endregion
     }
 
-    public struct ScalarAccessor : IAccessor<Single> , IAccessor<Vector4>
+    public struct ScalarAccessor : IAccessor<Single> , IAccessor<Vector4>, IReadOnlyCollection<Single>
     {
+        public ScalarAccessor(Byte[] data, int byteStride, Schema2.ComponentType type, Boolean normalized)
+            : this(new BYTES(data), byteStride, type, normalized) { }
+
         public ScalarAccessor(BYTES data, int byteStride, Schema2.ComponentType type, Boolean normalized)
         {
             _Accesor = FloatingAccessor.Create(data, type, normalized);
@@ -210,11 +212,18 @@ namespace glTF2Sharp.Memory
 
         public void CopyTo(ArraySegment<Single> dst) { AccessorsUtils.Copy<Single>(this, dst); }
 
-        public void CopyTo(ArraySegment<Vector4> dst) { AccessorsUtils.Copy<Vector4>(this, dst); }        
+        public void CopyTo(ArraySegment<Vector4> dst) { AccessorsUtils.Copy<Vector4>(this, dst); }
+
+        public IEnumerator<Single> GetEnumerator() { return new AccessorEnumerator<Single>(this); }
+
+        IEnumerator IEnumerable.GetEnumerator() { return new AccessorEnumerator<Single>(this); }
     }
 
-    public struct Vector2Accessor : IAccessor<Vector2>, IAccessor<Vector4>
+    public struct Vector2Accessor : IAccessor<Vector2>, IAccessor<Vector4>, IReadOnlyCollection<Vector2>
     {
+        public Vector2Accessor(Byte[] data, int byteStride, Schema2.ComponentType type, Boolean normalized)
+            : this(new BYTES(data),byteStride,type,normalized) { }
+        
         public Vector2Accessor(BYTES data, int byteStride, Schema2.ComponentType type, Boolean normalized)
         {
             _Accesor = FloatingAccessor.Create(data, type, normalized);
@@ -251,10 +260,17 @@ namespace glTF2Sharp.Memory
         public void CopyTo(ArraySegment<Vector2> dst) { AccessorsUtils.Copy<Vector2>(this, dst); }
 
         public void CopyTo(ArraySegment<Vector4> dst) { AccessorsUtils.Copy<Vector4>(this, dst); }
+
+        public IEnumerator<Vector2> GetEnumerator() { return new AccessorEnumerator<Vector2>(this); }
+
+        IEnumerator IEnumerable.GetEnumerator() { return new AccessorEnumerator<Vector2>(this); }
     }
 
-    public struct Vector3Accessor: IAccessor<Vector3>, IAccessor<Vector4>
+    public struct Vector3Accessor: IAccessor<Vector3>, IAccessor<Vector4>, IReadOnlyCollection<Vector3>
     {
+        public Vector3Accessor(Byte[] data, int byteStride, Schema2.ComponentType type, Boolean normalized)
+            : this(new BYTES(data), byteStride, type, normalized) { }
+
         public Vector3Accessor(BYTES data, int byteStride, Schema2.ComponentType type, Boolean normalized)
         {
             _Accesor = FloatingAccessor.Create(data, type, normalized);
@@ -292,10 +308,17 @@ namespace glTF2Sharp.Memory
         public void CopyTo(ArraySegment<Vector3> dst) { AccessorsUtils.Copy<Vector3>(this, dst); }
 
         public void CopyTo(ArraySegment<Vector4> dst) { AccessorsUtils.Copy<Vector4>(this, dst); }
+
+        public IEnumerator<Vector3> GetEnumerator() { return new AccessorEnumerator<Vector3>(this); }
+
+        IEnumerator IEnumerable.GetEnumerator() { return new AccessorEnumerator<Vector3>(this); }
     }
 
-    public struct Vector4Accessor: IAccessor<Vector4>
+    public struct Vector4Accessor: IAccessor<Vector4>, IReadOnlyCollection<Vector4>
     {
+        public Vector4Accessor(Byte[] data, int byteStride, Schema2.ComponentType type, Boolean normalized)
+            : this(new BYTES(data), byteStride, type, normalized) { }
+
         public Vector4Accessor(BYTES data, int byteStride, Schema2.ComponentType type, Boolean normalized)
         {
             _Accesor = FloatingAccessor.Create(data, type, normalized);
@@ -326,10 +349,17 @@ namespace glTF2Sharp.Memory
         }        
 
         public void CopyTo(ArraySegment<Vector4> dst) { AccessorsUtils.Copy<Vector4>(this, dst); }
+
+        public IEnumerator<Vector4> GetEnumerator() { return new AccessorEnumerator<Vector4>(this); }
+
+        IEnumerator IEnumerable.GetEnumerator() { return new AccessorEnumerator<Vector4>(this); }
     }
 
-    public struct QuaternionAccessor : IAccessor<Quaternion>, IAccessor<Vector4>
+    public struct QuaternionAccessor : IAccessor<Quaternion>, IAccessor<Vector4>, IReadOnlyCollection<Quaternion>
     {
+        public QuaternionAccessor(Byte[] data, int byteStride, Schema2.ComponentType type, Boolean normalized)
+            : this(new BYTES(data), byteStride, type, normalized) { }
+
         public QuaternionAccessor(BYTES data, int byteStride, Schema2.ComponentType type, Boolean normalized)
         {
             _Accesor = FloatingAccessor.Create(data, type, normalized);
@@ -368,10 +398,17 @@ namespace glTF2Sharp.Memory
         public void CopyTo(ArraySegment<Quaternion> dst) { AccessorsUtils.Copy<Quaternion>(this, dst); }
 
         public void CopyTo(ArraySegment<Vector4> dst) { AccessorsUtils.Copy<Vector4>(this, dst); }
+
+        public IEnumerator<Quaternion> GetEnumerator() { return new AccessorEnumerator<Quaternion>(this); }
+
+        IEnumerator IEnumerable.GetEnumerator() { return new AccessorEnumerator<Quaternion>(this); }
     }
 
-    public struct Matrix4x4Accessor : IAccessor<Matrix4x4>
+    public struct Matrix4x4Accessor : IAccessor<Matrix4x4>, IReadOnlyCollection<Matrix4x4>
     {
+        public Matrix4x4Accessor(Byte[] data, int byteStride, Schema2.ComponentType type, Boolean normalized)
+            : this(new BYTES(data), byteStride, type, normalized) { }
+
         public Matrix4x4Accessor(BYTES data, int byteStride, Schema2.ComponentType type, Boolean normalized)
         {
             _Accesor = FloatingAccessor.Create(data, type, normalized);
@@ -420,5 +457,9 @@ namespace glTF2Sharp.Memory
         }
 
         public void CopyTo(ArraySegment<Matrix4x4> dst) { AccessorsUtils.Copy<Matrix4x4>(this, dst); }
+
+        public IEnumerator<Matrix4x4> GetEnumerator() { return new AccessorEnumerator<Matrix4x4>(this); }
+
+        IEnumerator IEnumerable.GetEnumerator() { return new AccessorEnumerator<Matrix4x4>(this); }
     }
 }

+ 6 - 1
src/glTF2Sharp.DOM/Memory/IntegerAccessor.cs

@@ -4,12 +4,13 @@ using System.Text;
 
 namespace glTF2Sharp.Memory
 {
+    using System.Collections;
     using BYTES = ArraySegment<Byte>;
 
     /// <summary>
     /// Helper structure to access any Byte array as an array of <see cref="Schema2.IndexType"/>
     /// </summary>
-    public struct IntegerAccessor : IAccessor<UInt32>, IAccessor<Int32>
+    public struct IntegerAccessor : IAccessor<UInt32>, IAccessor<Int32>, IReadOnlyCollection<UInt32>
     {
         #region constructors
 
@@ -104,6 +105,10 @@ namespace glTF2Sharp.Memory
 
         public void CopyTo(ArraySegment<Int32> dst) { AccessorsUtils.Copy<Int32>(this, dst); }
 
+        public IEnumerator<UInt32> GetEnumerator() { return new AccessorEnumerator<UInt32>(this); }
+
+        IEnumerator IEnumerable.GetEnumerator() { return new AccessorEnumerator<UInt32>(this); }
+
         #endregion
     }
 }

+ 12 - 0
src/glTF2Sharp.Tests/Memory/MemoryAccessorTests.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Text;
 
 using NUnit.Framework;
@@ -30,5 +31,16 @@ namespace glTF2Sharp.Memory
             Assert.AreEqual(1, FloatingAccessor.Create(new Byte[] { 0,0, 0x80, 0x3f }, Schema2.ComponentType.FLOAT, false)[0]);
         }
 
+        [Test]
+        public void TestLinq()
+        {
+            var buffer = new Byte[] { 1, 52, 43, 6, 23, 234 };
+
+            var accessor = new Vector2Accessor(buffer, 0, Schema2.ComponentType.BYTE, true);
+
+            var result = accessor.ToArray();
+
+            Assert.AreEqual(3, result.Length);
+        }
     }
 }