| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Numerics;
- using System.Text;
- namespace SharpGLTF.Memory
- {
- public interface IEncodedArray<T> : IReadOnlyCollection<T>
- where T : unmanaged
- {
- T this[int index] { get; set; }
- void CopyTo(ArraySegment<T> dst);
- // void CopyTo(IEncodedArray<T> dst);
- (T, T) GetBounds();
- }
- struct EncodedArrayEnumerator<T> : IEnumerator<T>
- where T : unmanaged
- {
- #region lifecycle
- public EncodedArrayEnumerator(IEncodedArray<T> accessor)
- {
- this._Accessor = accessor;
- this._Count = accessor.Count;
- this._Index = -1;
- }
- public void Dispose()
- {
- }
- #endregion
- #region data
- private readonly IEncodedArray<T> _Accessor;
- private readonly int _Count;
- private int _Index;
- #endregion
- #region API
- public T Current => _Accessor[_Index];
- object IEnumerator.Current => _Accessor[_Index];
- public bool MoveNext()
- {
- ++_Index;
- return _Index < _Count;
- }
- public void Reset()
- {
- _Index = -1;
- }
- #endregion
- }
- public static class EncodedArrayUtils
- {
- public static IntegerArray IndicesRange(int start, int count)
- {
- var array = new IntegerArray( new ArraySegment<Byte>(new Byte[count * 4]), Schema2.IndexType.UNSIGNED_INT);
- for (int i = 0; i < count; ++i)
- {
- array[i] = (UInt32)(start + i);
- }
- return array;
- }
- public static void FillFrom(this IEncodedArray<UInt32> dst, int dstIndex, IEnumerable<Int32> src)
- {
- using (var ator = src.GetEnumerator())
- {
- while (dstIndex < dst.Count && ator.MoveNext())
- {
- dst[dstIndex++] = (UInt32)ator.Current;
- }
- }
- }
- public static void FillFrom<T>(this IEncodedArray<T> dst, int dstIndex, IEnumerable<T> src)
- where T : unmanaged
- {
- using (var ator = src.GetEnumerator())
- {
- while (dstIndex < dst.Count && ator.MoveNext())
- {
- dst[dstIndex++] = ator.Current;
- }
- }
- }
- public static void FillFrom<T>(this IEncodedArray<T> dst, int dstIndex, params T[] src)
- where T : unmanaged
- {
- for (int i = 0; i < src.Length; ++i)
- {
- dst[dstIndex + i] = src[i];
- }
- }
- public static void CopyTo<T>(IEncodedArray<T> src, IEncodedArray<T> dst, int dstOffset = 0)
- where T : unmanaged
- {
- for (int i = 0; i < src.Count; ++i)
- {
- dst[i + dstOffset] = src[i];
- }
- }
- public static void CopyTo<T>(T[] src, IEncodedArray<T> dst, int dstOffset = 0)
- where T : unmanaged
- {
- for (int i = 0; i < src.Length; ++i)
- {
- dst[i + dstOffset] = src[i];
- }
- }
- public static void Copy<T>(IEncodedArray<T> src, T[] dst)
- where T : unmanaged
- {
- Copy<T>(src, new ArraySegment<T>(dst));
- }
- public static void Copy<T>(IEncodedArray<T> src, ArraySegment<T> dst)
- where T : unmanaged
- {
- var c = src.Count;
- for (int i = 0; i < c; ++i) dst.Array[dst.Offset + i] = src[i];
- }
- public static (Single, Single) GetBounds(ScalarArray accesor)
- {
- var min = Single.MaxValue;
- var max = Single.MinValue;
- int c = accesor.Count;
- for (int i = 0; i < c; ++i)
- {
- var v = accesor[i];
- min = Math.Min(min, v);
- max = Math.Max(max, v);
- }
- return (min, max);
- }
- public static (Vector2, Vector2) GetBounds(Vector2Array accesor)
- {
- var min = new Vector2(Single.MaxValue);
- var max = new Vector2(Single.MinValue);
- int c = accesor.Count;
- for (int i = 0; i < c; ++i)
- {
- var v = accesor[i];
- min = Vector2.Min(min, v);
- max = Vector2.Max(max, v);
- }
- return (min, max);
- }
- public static (Vector3, Vector3) GetBounds(Vector3Array accesor)
- {
- var min = new Vector3(Single.MaxValue);
- var max = new Vector3(Single.MinValue);
- int c = accesor.Count;
- for (int i = 0; i < c; ++i)
- {
- var v = accesor[i];
- min = Vector3.Min(min, v);
- max = Vector3.Max(max, v);
- }
- return (min, max);
- }
- public static (Vector4, Vector4) GetBounds(IEncodedArray<Vector4> accesor)
- {
- var min = new Vector4(Single.MaxValue);
- var max = new Vector4(Single.MinValue);
- int c = accesor.Count;
- for (int i = 0; i < c; ++i)
- {
- var v = accesor[i];
- min = Vector4.Min(min, v);
- max = Vector4.Max(max, v);
- }
- return (min, max);
- }
- }
- /// <summary>
- /// Wraps a collection of Scalar values and exposes it as a collection of Vector4 values
- /// </summary>
- struct _MapScalarToVector4 : IEncodedArray<Vector4>
- {
- public _MapScalarToVector4(ScalarArray source)
- {
- _Accessor = source;
- }
- private ScalarArray _Accessor;
- public int Count => _Accessor.Count;
- public Vector4 this[int index]
- {
- get => new Vector4(_Accessor[index], 0, 0, 0);
- set => _Accessor[index] = value.X;
- }
- public void CopyTo(ArraySegment<Vector4> dst) { EncodedArrayUtils.Copy(this, dst); }
- public IEnumerator<Vector4> GetEnumerator() { return new EncodedArrayEnumerator<Vector4>(this); }
- IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator<Vector4>(this); }
- public (Vector4, Vector4) GetBounds() { return EncodedArrayUtils.GetBounds(this); }
- }
- /// <summary>
- /// Wraps a collection of Vector2 values and exposes it as a collection of Vector4 values
- /// </summary>
- struct _MapVector2ToVector4 : IEncodedArray<Vector4>
- {
- public _MapVector2ToVector4(Vector2Array source)
- {
- _Accessor = source;
- }
- private Vector2Array _Accessor;
- public int Count => _Accessor.Count;
- public Vector4 this[int index]
- {
- get { var v = _Accessor[index]; return new Vector4(v.X, v.Y, 0, 0); }
- set => _Accessor[index] = new Vector2(value.X, value.Y);
- }
- public void CopyTo(ArraySegment<Vector4> dst) { EncodedArrayUtils.Copy(this, dst); }
- public IEnumerator<Vector4> GetEnumerator() { return new EncodedArrayEnumerator<Vector4>(this); }
- IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator<Vector4>(this); }
- public (Vector4, Vector4) GetBounds() { return EncodedArrayUtils.GetBounds(this); }
- }
- /// <summary>
- /// Wraps a collection of Vector3 values and exposes it as a collection of Vector4 values
- /// </summary>
- struct _MapVector3ToVector4 : IEncodedArray<Vector4>
- {
- public _MapVector3ToVector4(Vector3Array source)
- {
- _Accessor = source;
- }
- private Vector3Array _Accessor;
- public int Count => _Accessor.Count;
- public Vector4 this[int index]
- {
- get { var v = _Accessor[index]; return new Vector4(v.X, v.Y, v.Z, 0); }
- set => _Accessor[index] = new Vector3(value.X, value.Y, value.Z);
- }
- public void CopyTo(ArraySegment<Vector4> dst) { EncodedArrayUtils.Copy(this, dst); }
- public IEnumerator<Vector4> GetEnumerator() { return new EncodedArrayEnumerator<Vector4>(this); }
- IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator<Vector4>(this); }
- public (Vector4, Vector4) GetBounds() { return EncodedArrayUtils.GetBounds(this); }
- }
- /// <summary>
- /// Wraps a collection of Quaternion values and exposes it as a collection of Vector4 values
- /// </summary>
- struct _MapQuaternionToVector4 : IEncodedArray<Vector4>
- {
- public _MapQuaternionToVector4(QuaternionArray source)
- {
- _Accessor = source;
- }
- private QuaternionArray _Accessor;
- public int Count => _Accessor.Count;
- public Vector4 this[int index]
- {
- get { var v = _Accessor[index]; return new Vector4(v.X, v.Y, v.Z, v.W); }
- set => _Accessor[index] = new Quaternion(value.X, value.Y, value.Z, value.W);
- }
- public void CopyTo(ArraySegment<Vector4> dst) { EncodedArrayUtils.Copy(this, dst); }
- public IEnumerator<Vector4> GetEnumerator() { return new EncodedArrayEnumerator<Vector4>(this); }
- IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator<Vector4>(this); }
- public (Vector4, Vector4) GetBounds() { return EncodedArrayUtils.GetBounds(this); }
- }
- }
|