| | 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.Mathematics; |
| | 8 | |
|
| | 9 | | namespace GDX.Collections.Generic |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A three-dimensional array backed a flat array. |
| | 13 | | /// </summary> |
| | 14 | | /// <typeparam name="T">Type of objects.</typeparam> |
| | 15 | | public struct Array3D<T> : IDisposable |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// The backing flat array. |
| | 19 | | /// </summary> |
| | 20 | | public T[] Array; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// The stride of the z-axis segment in <see cref="Array" />. |
| | 24 | | /// </summary> |
| | 25 | | public readonly int Depth; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// The total length of a single <see cref="Depth" />. |
| | 29 | | /// </summary> |
| | 30 | | public readonly int DepthLength; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// The stride of the y-axis segment in <see cref="Array" />. |
| | 34 | | /// </summary> |
| | 35 | | public readonly int Height; |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// The length of <see cref="Array" />. |
| | 39 | | /// </summary> |
| | 40 | | public readonly int Length; |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// The stride of the x-axis segment in <see cref="Array" />. |
| | 44 | | /// </summary> |
| | 45 | | public readonly int Width; |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Create a <see cref="Array3D{T}" /> with a uniform dimensional length. |
| | 49 | | /// </summary> |
| | 50 | | /// <remarks></remarks> |
| | 51 | | /// <param name="width">X-axis length.</param> |
| | 52 | | /// <param name="height">Y-axis length.</param> |
| | 53 | | /// <param name="depth">Z-axis length.</param> |
| | 54 | | public Array3D(int width, int height, int depth) |
| 10 | 55 | | { |
| 10 | 56 | | Width = width; |
| 10 | 57 | | Height = height; |
| 10 | 58 | | DepthLength = width * height; |
| 10 | 59 | | Depth = depth; |
| 10 | 60 | | Length = width * height * depth; |
| 10 | 61 | | Array = new T[Length]; |
| 10 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Access a specific location in the voxel. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="x">X location index.</param> |
| | 68 | | /// <param name="y">Y location index.</param> |
| | 69 | | /// <param name="z">Z location index.</param> |
| | 70 | | public T this[int x, int y, int z] |
| | 71 | | { |
| | 72 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 1 | 73 | | get => Array[x + Width * (y + Height * z)]; |
| | 74 | |
|
| | 75 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 1 | 76 | | set => Array[x + Width * (y + Height * z)] = value; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Access a specific location in the voxel. |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="index">A three-dimensional index.</param> |
| | 83 | | public T this[int3 index] |
| | 84 | | { |
| | 85 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 1 | 86 | | get => Array[index.x + Width * (index.y + Height * index.z)]; |
| | 87 | |
|
| | 88 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 1 | 89 | | set => Array[index.x + Width * (index.y + Height * index.z)] = value; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Properly dispose of the <see cref="Array3D{T}" />. |
| | 94 | | /// </summary> |
| | 95 | | public void Dispose() |
| 0 | 96 | | { |
| 0 | 97 | | Array = default; |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Get the three-dimensional index of a flat array index. |
| | 102 | | /// </summary> |
| | 103 | | /// <param name="index">A flat array index.</param> |
| | 104 | | /// <returns>A three-dimensional voxel index.</returns> |
| | 105 | | public int3 GetFromIndex(int index) |
| 5 | 106 | | { |
| 5 | 107 | | int x = index % Width; |
| 5 | 108 | | int y = (index - x) / Width % Height; |
| 5 | 109 | | int z = index / DepthLength; |
| | 110 | |
|
| 5 | 111 | | return new int3(x, y, z); |
| 5 | 112 | | } |
| | 113 | |
|
| | 114 | | public int GetFromIndex(int3 index) |
| 0 | 115 | | { |
| 0 | 116 | | return index.x + Width * (index.y + Height * index.z); |
| 0 | 117 | | } |
| | 118 | | } |
| | 119 | | } |