| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Runtime.CompilerServices; |
| | 7 | | using Unity.Collections; |
| | 8 | | using Unity.Mathematics; |
| | 9 | |
|
| | 10 | | namespace GDX.Collections.Generic |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A three-dimensional <see cref="NativeArray{T}" /> backed array. |
| | 14 | | /// </summary> |
| | 15 | | /// <typeparam name="T">Type of objects.</typeparam> |
| | 16 | | public struct NativeUniformArray3D<T> : IDisposable where T : struct |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The backing <see cref="NativeArray{T}" />. |
| | 20 | | /// </summary> |
| | 21 | | public NativeArray<T> Array; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// The length of <see cref="Array" />. |
| | 25 | | /// </summary> |
| | 26 | | public readonly int Length; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// The stride of each dimensional segment in <see cref="Array" />. |
| | 30 | | /// </summary> |
| | 31 | | public readonly int Stride; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Stores a cached copy of the stride squared. |
| | 35 | | /// </summary> |
| | 36 | | public readonly int StrideSquared; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Create a <see cref="NativeUniformArray3D{T}" /> with a uniform dimensional length. |
| | 40 | | /// </summary> |
| | 41 | | /// <remarks></remarks> |
| | 42 | | /// <param name="stride">X length, Y length and Z length will all be set to this value.</param> |
| | 43 | | /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> |
| | 44 | | /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> |
| | 45 | | public NativeUniformArray3D(int stride, Allocator allocator, NativeArrayOptions nativeArrayOptions) |
| 2 | 46 | | { |
| 2 | 47 | | Stride = stride; |
| 2 | 48 | | StrideSquared = stride * stride; |
| 2 | 49 | | Length = StrideSquared * stride; |
| | 50 | |
|
| 2 | 51 | | Array = new NativeArray<T>(Length, allocator, nativeArrayOptions); |
| 2 | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Access a specific location in the voxel. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="x">X location index.</param> |
| | 58 | | /// <param name="y">Y location index.</param> |
| | 59 | | /// <param name="z">Z location index.</param> |
| | 60 | | public T this[int x, int y, int z] |
| | 61 | | { |
| | 62 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 1 | 63 | | get => Array[z * StrideSquared + y * Stride + x]; |
| | 64 | |
|
| | 65 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 0 | 66 | | set => Array[z * StrideSquared + y * Stride + x] = value; |
| | 67 | | } |
| | 68 | |
|
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Access a specific location in the voxel. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="index">A 3-Dimensional index.</param> |
| | 74 | | public T this[int3 index] |
| | 75 | | { |
| | 76 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 0 | 77 | | get => Array[index.z * StrideSquared + index.y * Stride + index.x]; |
| | 78 | |
|
| | 79 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 0 | 80 | | set => Array[index.z * StrideSquared + index.y * Stride + index.x] = value; |
| | 81 | | } |
| | 82 | |
|
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Properly dispose of the <see cref="NativeUniformArray3D{T}" />. |
| | 86 | | /// </summary> |
| | 87 | | public void Dispose() |
| 2 | 88 | | { |
| 2 | 89 | | if (!Array.IsCreated) |
| 0 | 90 | | { |
| 0 | 91 | | return; |
| | 92 | | } |
| | 93 | |
|
| 2 | 94 | | Array.Dispose(); |
| 2 | 95 | | Array = default; |
| 2 | 96 | | } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Get the three-dimensional index of a flat array index. |
| | 100 | | /// </summary> |
| | 101 | | /// <param name="index">A flat array index.</param> |
| | 102 | | /// <returns>A three-dimensional voxel index.</returns> |
| | 103 | | public int3 GetFromIndex(int index) |
| 0 | 104 | | { |
| 0 | 105 | | int x = index % Stride; |
| 0 | 106 | | int y = (index - x) / Stride % Stride; |
| 0 | 107 | | int z = (index - x - Stride * y) / StrideSquared; |
| 0 | 108 | | return new int3(x, y, z); |
| 0 | 109 | | } |
| | 110 | | } |
| | 111 | | } |