FloatingArrays.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Numerics;
  5. using System.Collections;
  6. using System.Linq;
  7. namespace SharpGLTF.Memory
  8. {
  9. using BYTES = ArraySegment<Byte>;
  10. using ENCODING = Schema2.ComponentType;
  11. /// <summary>
  12. /// Wraps a <see cref="ArraySegment{Byte}"/> containing encoded floating point values
  13. /// </summary>
  14. struct FloatingAccessor
  15. {
  16. #region constructors
  17. public FloatingAccessor(BYTES data, int byteOffset, int itemsCount, int byteStride, int dimensions, ENCODING encoding, Boolean normalized)
  18. {
  19. var enclen = encoding.ByteLength();
  20. this._Data = data.Slice(byteOffset);
  21. this._Getter = null;
  22. this._Setter = null;
  23. this._ByteStride = Math.Max(byteStride, enclen * dimensions);
  24. this._EncodedLen = enclen;
  25. this._ItemCount = this._Data.Count / this._ByteStride;
  26. // strided buffers usually have room for an extra item
  27. if ((_Data.Count % _ByteStride) >= enclen * dimensions) ++_ItemCount;
  28. _ItemCount = Math.Min(itemsCount, _ItemCount);
  29. if (encoding == ENCODING.FLOAT)
  30. {
  31. this._Setter = this._SetValue<Single>;
  32. this._Getter = this._GetValue<Single>;
  33. return;
  34. }
  35. if (normalized)
  36. {
  37. switch (encoding)
  38. {
  39. case ENCODING.BYTE:
  40. {
  41. this._Setter = this._SetNormalizedS8;
  42. this._Getter = this._GetNormalizedS8;
  43. break;
  44. }
  45. case ENCODING.UNSIGNED_BYTE:
  46. {
  47. this._Setter = this._SetNormalizedU8;
  48. this._Getter = this._GetNormalizedU8;
  49. break;
  50. }
  51. case ENCODING.SHORT:
  52. {
  53. this._Setter = this._SetNormalizedS16;
  54. this._Getter = this._GetNormalizedS16;
  55. break;
  56. }
  57. case ENCODING.UNSIGNED_SHORT:
  58. {
  59. this._Setter = this._SetNormalizedU16;
  60. this._Getter = this._GetNormalizedU16;
  61. break;
  62. }
  63. default: throw new ArgumentException(nameof(encoding));
  64. }
  65. }
  66. else
  67. {
  68. switch (encoding)
  69. {
  70. case ENCODING.BYTE:
  71. {
  72. this._Setter = this._SetValueS8;
  73. this._Getter = this._GetValueS8;
  74. break;
  75. }
  76. case ENCODING.UNSIGNED_BYTE:
  77. {
  78. this._Setter = this._SetValueU8;
  79. this._Getter = this._GetValueU8;
  80. break;
  81. }
  82. case ENCODING.SHORT:
  83. {
  84. this._Setter = this._SetValueS16;
  85. this._Getter = this._GetValueS16;
  86. break;
  87. }
  88. case ENCODING.UNSIGNED_SHORT:
  89. {
  90. this._Setter = this._SetValueU16;
  91. this._Getter = this._GetValueU16;
  92. break;
  93. }
  94. case ENCODING.UNSIGNED_INT:
  95. {
  96. this._Setter = this._SetValueU32;
  97. this._Getter = this._GetValueU32;
  98. break;
  99. }
  100. case ENCODING.FLOAT:
  101. break;
  102. default: throw new ArgumentException(nameof(encoding));
  103. }
  104. }
  105. }
  106. #endregion
  107. #region encoding / decoding
  108. private Single _GetValueU8(int byteOffset) { return _GetValue<Byte>(byteOffset); }
  109. private void _SetValueU8(int byteOffset, Single value) { _SetValue<Byte>(byteOffset, (Byte)value); }
  110. private Single _GetValueS8(int byteOffset) { return _GetValue<SByte>(byteOffset); }
  111. private void _SetValueS8(int byteOffset, Single value) { _SetValue<SByte>(byteOffset, (SByte)value); }
  112. private Single _GetValueU16(int byteOffset) { return _GetValue<UInt16>(byteOffset); }
  113. private void _SetValueU16(int byteOffset, Single value) { _SetValue<UInt16>(byteOffset, (UInt16)value); }
  114. private Single _GetValueS16(int byteOffset) { return _GetValue<Int16>(byteOffset); }
  115. private void _SetValueS16(int byteOffset, Single value) { _SetValue<Int16>(byteOffset, (Int16)value); }
  116. private Single _GetValueU32(int byteOffset) { return _GetValue<UInt32>(byteOffset); }
  117. private void _SetValueU32(int byteOffset, Single value) { _SetValue<UInt32>(byteOffset, (UInt32)value); }
  118. private Single _GetNormalizedU8(int byteOffset) { return _GetValueU8(byteOffset) / 255.0f; }
  119. private void _SetNormalizedU8(int byteOffset, Single value) { _SetValueU8(byteOffset, value * 255.0f); }
  120. private Single _GetNormalizedS8(int byteOffset) { return Math.Max(_GetValueS8(byteOffset) / 127.0f, -1); }
  121. private void _SetNormalizedS8(int byteOffset, Single value) { _SetValueS8(byteOffset, (Single)Math.Round(value * 127.0f)); }
  122. private Single _GetNormalizedU16(int byteOffset) { return _GetValueU16(byteOffset) / 65535.0f; }
  123. private void _SetNormalizedU16(int byteOffset, Single value) { _SetValueU16(byteOffset, value * 65535.0f); }
  124. private Single _GetNormalizedS16(int byteOffset) { return Math.Max(_GetValueS16(byteOffset) / 32767.0f, -1); }
  125. private void _SetNormalizedS16(int byteOffset, Single value) { _SetValueS16(byteOffset, (Single)Math.Round(value * 32767.0f)); }
  126. private T _GetValue<T>(int byteOffset)
  127. where T : unmanaged
  128. {
  129. return System.Runtime.InteropServices.MemoryMarshal.Read<T>(_Data.AsSpan(byteOffset));
  130. }
  131. private void _SetValue<T>(int byteOffset, T value)
  132. where T : unmanaged
  133. {
  134. System.Runtime.InteropServices.MemoryMarshal.Write<T>(_Data.AsSpan(byteOffset), ref value);
  135. }
  136. #endregion
  137. #region data
  138. delegate Single _GetterCallback(int byteOffset);
  139. delegate void _SetterCallback(int byteOffset, Single value);
  140. private readonly BYTES _Data;
  141. private readonly int _ByteStride;
  142. private readonly int _EncodedLen;
  143. private readonly int _ItemCount;
  144. private readonly _GetterCallback _Getter;
  145. private readonly _SetterCallback _Setter;
  146. #endregion
  147. #region API
  148. public int ByteLength => _Data.Count;
  149. public int Count => _ItemCount;
  150. public Single this[int index]
  151. {
  152. get => _Getter(index * _ByteStride);
  153. set => _Setter(index * _ByteStride, value);
  154. }
  155. public Single this[int rowIndex, int subIndex]
  156. {
  157. get => _Getter((rowIndex * _ByteStride) + (subIndex * _EncodedLen));
  158. set => _Setter((rowIndex * _ByteStride) + (subIndex * _EncodedLen), value);
  159. }
  160. #endregion
  161. }
  162. /// <summary>
  163. /// Wraps an encoded byte array and exposes it as a collection of Single Scalar values
  164. /// </summary>
  165. [System.Diagnostics.DebuggerDisplay("Scalar Accessor {Count}")]
  166. public struct ScalarArray : IEncodedArray<Single>
  167. {
  168. #region constructors
  169. public ScalarArray(BYTES data, int byteStride = 0, ENCODING encoding = ENCODING.FLOAT, Boolean normalized = false)
  170. : this(data, 0, int.MaxValue, byteStride, encoding, normalized) { }
  171. public ScalarArray(BYTES data, int byteOffset, int itemsCount, int byteStride, ENCODING encoding, Boolean normalized)
  172. {
  173. _Accesor = new FloatingAccessor(data, byteOffset, itemsCount, byteStride, 1, encoding, normalized);
  174. }
  175. #endregion
  176. #region data
  177. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  178. private FloatingAccessor _Accesor;
  179. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
  180. private Single[] _DebugItems => this.ToArray();
  181. #endregion
  182. #region API
  183. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  184. public int Count => _Accesor.Count;
  185. public Single this[int index]
  186. {
  187. get => _Accesor[index, 0];
  188. set => _Accesor[index, 0] = value;
  189. }
  190. public void CopyTo(ArraySegment<Single> dst) { EncodedArrayUtils.Copy<Single>(this, dst); }
  191. public IEnumerator<Single> GetEnumerator() { return new EncodedArrayEnumerator<Single>(this); }
  192. IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator<Single>(this); }
  193. public (Single, Single) GetBounds() { return EncodedArrayUtils.GetBounds(this); }
  194. public IEncodedArray<Vector4> AsVector4() { return new _MapScalarToVector4(this); }
  195. #endregion
  196. }
  197. /// <summary>
  198. /// Wraps an encoded byte array and exposes it as a collection of Vector2 values
  199. /// </summary>
  200. [System.Diagnostics.DebuggerDisplay("Vector2 Accessor {Count}")]
  201. public struct Vector2Array : IEncodedArray<Vector2>
  202. {
  203. #region constructors
  204. public Vector2Array(BYTES data, int byteStride = 0, ENCODING encoding = ENCODING.FLOAT, Boolean normalized = false)
  205. : this(data, 0, int.MaxValue, byteStride, encoding, normalized) { }
  206. public Vector2Array(BYTES data, int byteOffset, int itemsCount, int byteStride, ENCODING encoding, Boolean normalized)
  207. {
  208. _Accesor = new FloatingAccessor(data, byteOffset, itemsCount, byteStride, 2, encoding, normalized);
  209. }
  210. #endregion
  211. #region data
  212. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  213. private FloatingAccessor _Accesor;
  214. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
  215. private Vector2[] _DebugItems => this.ToArray();
  216. #endregion
  217. #region API
  218. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  219. public int Count => _Accesor.Count;
  220. public Vector2 this[int index]
  221. {
  222. get
  223. {
  224. return new Vector2(_Accesor[index, 0], _Accesor[index, 1]);
  225. }
  226. set
  227. {
  228. _Accesor[index, 0] = value.X;
  229. _Accesor[index, 1] = value.Y;
  230. }
  231. }
  232. public void CopyTo(ArraySegment<Vector2> dst) { EncodedArrayUtils.Copy<Vector2>(this, dst); }
  233. public IEnumerator<Vector2> GetEnumerator() { return new EncodedArrayEnumerator<Vector2>(this); }
  234. IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator<Vector2>(this); }
  235. public (Vector2, Vector2) GetBounds() { return EncodedArrayUtils.GetBounds(this); }
  236. public IEncodedArray<Vector4> AsVector4() { return new _MapVector2ToVector4(this); }
  237. #endregion
  238. }
  239. /// <summary>
  240. /// Wraps an encoded byte array and exposes it as a collection of Vector3 values
  241. /// </summary>
  242. [System.Diagnostics.DebuggerDisplay("Vector3 Accessor {Count}")]
  243. public struct Vector3Array : IEncodedArray<Vector3>
  244. {
  245. #region constructors
  246. public Vector3Array(BYTES data, int byteStride = 0, ENCODING encoding = ENCODING.FLOAT, Boolean normalized = false)
  247. : this(data, 0, int.MaxValue, byteStride, encoding, normalized) { }
  248. public Vector3Array(BYTES data, int byteOffset, int itemsCount, int byteStride, ENCODING encoding, Boolean normalized)
  249. {
  250. _Accesor = new FloatingAccessor(data, byteOffset, itemsCount, byteStride, 3, encoding, normalized);
  251. }
  252. #endregion
  253. #region data
  254. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  255. private FloatingAccessor _Accesor;
  256. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
  257. private Vector3[] _DebugItems => this.ToArray();
  258. #endregion
  259. #region API
  260. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  261. public int Count => _Accesor.Count;
  262. public Vector3 this[int index]
  263. {
  264. get
  265. {
  266. return new Vector3(_Accesor[index, 0], _Accesor[index, 1], _Accesor[index, 2]);
  267. }
  268. set
  269. {
  270. _Accesor[index, 0] = value.X;
  271. _Accesor[index, 1] = value.Y;
  272. _Accesor[index, 2] = value.Z;
  273. }
  274. }
  275. public void CopyTo(ArraySegment<Vector3> dst) { EncodedArrayUtils.Copy<Vector3>(this, dst); }
  276. public IEnumerator<Vector3> GetEnumerator() { return new EncodedArrayEnumerator<Vector3>(this); }
  277. IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator<Vector3>(this); }
  278. public (Vector3, Vector3) GetBounds() { return EncodedArrayUtils.GetBounds(this); }
  279. public IEncodedArray<Vector4> AsVector4() { return new _MapVector3ToVector4(this); }
  280. #endregion
  281. }
  282. /// <summary>
  283. /// Wraps an encoded byte array and exposes it as a collection of Vector4 values
  284. /// </summary>
  285. [System.Diagnostics.DebuggerDisplay("Vector4 Accessor {Count}")]
  286. public struct Vector4Array : IEncodedArray<Vector4>
  287. {
  288. #region constructors
  289. public Vector4Array(BYTES data, int byteStride = 0, ENCODING encoding = ENCODING.FLOAT, Boolean normalized = false)
  290. : this(data, 0, int.MaxValue, byteStride, encoding, normalized) { }
  291. public Vector4Array(BYTES data, int byteOffset, int itemsCount, int byteStride, ENCODING encoding, Boolean normalized)
  292. {
  293. _Accesor = new FloatingAccessor(data, byteOffset, itemsCount, byteStride, 4, encoding, normalized);
  294. }
  295. #endregion
  296. #region data
  297. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  298. private FloatingAccessor _Accesor;
  299. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
  300. private Vector4[] _DebugItems => this.ToArray();
  301. #endregion
  302. #region API
  303. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  304. public int Count => _Accesor.Count;
  305. public Vector4 this[int index]
  306. {
  307. get
  308. {
  309. return new Vector4(_Accesor[index, 0], _Accesor[index, 1], _Accesor[index, 2], _Accesor[index, 3]);
  310. }
  311. set
  312. {
  313. _Accesor[index, 0] = value.X;
  314. _Accesor[index, 1] = value.Y;
  315. _Accesor[index, 2] = value.Z;
  316. _Accesor[index, 3] = value.W;
  317. }
  318. }
  319. public void CopyTo(ArraySegment<Vector4> dst) { EncodedArrayUtils.Copy<Vector4>(this, dst); }
  320. public IEnumerator<Vector4> GetEnumerator() { return new EncodedArrayEnumerator<Vector4>(this); }
  321. IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator<Vector4>(this); }
  322. public (Vector4, Vector4) GetBounds() { return EncodedArrayUtils.GetBounds(this); }
  323. public IEncodedArray<Vector4> AsVector4() { return this; }
  324. #endregion
  325. }
  326. /// <summary>
  327. /// Wraps an encoded byte array and exposes it as a collection of Quaternion values
  328. /// </summary>
  329. [System.Diagnostics.DebuggerDisplay("Quaternion Accessor {Count}")]
  330. public struct QuaternionArray : IEncodedArray<Quaternion>
  331. {
  332. #region constructors
  333. public QuaternionArray(BYTES data, int byteStride = 0, ENCODING encoding = ENCODING.FLOAT, Boolean normalized = false)
  334. : this(data, 0, int.MaxValue, byteStride, encoding, normalized) { }
  335. public QuaternionArray(BYTES data, int byteOffset, int itemsCount, int byteStride, ENCODING encoding, Boolean normalized)
  336. {
  337. _Accesor = new FloatingAccessor(data, byteOffset, itemsCount, byteStride, 4, encoding, normalized);
  338. }
  339. #endregion
  340. #region data
  341. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  342. private FloatingAccessor _Accesor;
  343. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
  344. private Quaternion[] _DebugItems => this.ToArray();
  345. #endregion
  346. #region API
  347. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  348. public int Count => _Accesor.Count;
  349. public Quaternion this[int index]
  350. {
  351. get
  352. {
  353. return new Quaternion(_Accesor[index, 0], _Accesor[index, 1], _Accesor[index, 2], _Accesor[index, 3]);
  354. }
  355. set
  356. {
  357. _Accesor[index, 0] = value.X;
  358. _Accesor[index, 1] = value.Y;
  359. _Accesor[index, 2] = value.Z;
  360. _Accesor[index, 3] = value.W;
  361. }
  362. }
  363. public void CopyTo(ArraySegment<Quaternion> dst) { EncodedArrayUtils.Copy<Quaternion>(this, dst); }
  364. public IEnumerator<Quaternion> GetEnumerator() { return new EncodedArrayEnumerator<Quaternion>(this); }
  365. IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator<Quaternion>(this); }
  366. public (Quaternion, Quaternion) GetBounds() { throw new NotImplementedException(); }
  367. public IEncodedArray<Vector4> AsVector4() { return new _MapQuaternionToVector4(this); }
  368. #endregion
  369. }
  370. /// <summary>
  371. /// Wraps an encoded byte array and exposes it as a collection of Matrix4x4 values
  372. /// </summary>
  373. [System.Diagnostics.DebuggerDisplay("MAtrix4x4 Accessor {Count}")]
  374. public struct Matrix4x4Array : IEncodedArray<Matrix4x4>
  375. {
  376. #region constructors
  377. public Matrix4x4Array(BYTES data, int byteStride = 0, ENCODING encoding = ENCODING.FLOAT, Boolean normalized = false)
  378. : this(data, 0, int.MaxValue, byteStride, encoding, normalized) { }
  379. public Matrix4x4Array(BYTES data, int byteOffset, int itemsCount, int byteStride, ENCODING encoding, Boolean normalized)
  380. {
  381. _Accesor = new FloatingAccessor(data, byteOffset, itemsCount, byteStride, 16, encoding, normalized);
  382. }
  383. #endregion
  384. #region data
  385. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  386. private FloatingAccessor _Accesor;
  387. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
  388. private Matrix4x4[] _DebugItems => this.ToArray();
  389. #endregion
  390. #region API
  391. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  392. public int Count => _Accesor.Count;
  393. public Matrix4x4 this[int index]
  394. {
  395. get
  396. {
  397. return new Matrix4x4
  398. (
  399. _Accesor[index, 0], _Accesor[index, 1], _Accesor[index, 2], _Accesor[index, 3],
  400. _Accesor[index, 4], _Accesor[index, 5], _Accesor[index, 6], _Accesor[index, 7],
  401. _Accesor[index, 8], _Accesor[index, 9], _Accesor[index, 10], _Accesor[index, 11],
  402. _Accesor[index, 12], _Accesor[index, 13], _Accesor[index, 14], _Accesor[index, 15]
  403. );
  404. }
  405. set
  406. {
  407. _Accesor[index, 0] = value.M11;
  408. _Accesor[index, 1] = value.M12;
  409. _Accesor[index, 2] = value.M13;
  410. _Accesor[index, 3] = value.M14;
  411. _Accesor[index, 4] = value.M21;
  412. _Accesor[index, 5] = value.M22;
  413. _Accesor[index, 6] = value.M23;
  414. _Accesor[index, 7] = value.M24;
  415. _Accesor[index, 8] = value.M31;
  416. _Accesor[index, 9] = value.M32;
  417. _Accesor[index, 10] = value.M33;
  418. _Accesor[index, 11] = value.M34;
  419. _Accesor[index, 12] = value.M41;
  420. _Accesor[index, 13] = value.M42;
  421. _Accesor[index, 14] = value.M43;
  422. _Accesor[index, 15] = value.M44;
  423. }
  424. }
  425. public void CopyTo(ArraySegment<Matrix4x4> dst) { EncodedArrayUtils.Copy<Matrix4x4>(this, dst); }
  426. public IEnumerator<Matrix4x4> GetEnumerator() { return new EncodedArrayEnumerator<Matrix4x4>(this); }
  427. IEnumerator IEnumerable.GetEnumerator() { return new EncodedArrayEnumerator<Matrix4x4>(this); }
  428. public (Matrix4x4, Matrix4x4) GetBounds() { throw new NotImplementedException(); }
  429. #endregion
  430. }
  431. }