| | 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 NativeArray3D<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 stride of the z-axis segment in <see cref="Array" />. |
| | 25 | | /// </summary> |
| | 26 | | public readonly int Depth; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// The total length of a single <see cref="Depth" />. |
| | 30 | | /// </summary> |
| | 31 | | public readonly int DepthLength; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// The stride of the y-axis segment in <see cref="Array" />. |
| | 35 | | /// </summary> |
| | 36 | | public readonly int Height; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// The length of <see cref="Array" />. |
| | 40 | | /// </summary> |
| | 41 | | public readonly int Length; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// The stride of the x-axis segment in <see cref="Array" />. |
| | 45 | | /// </summary> |
| | 46 | | public readonly int Width; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Create a <see cref="NativeArray3D{T}" /> with a uniform dimensional length. |
| | 50 | | /// </summary> |
| | 51 | | /// <remarks></remarks> |
| | 52 | | /// <param name="width">X-axis length.</param> |
| | 53 | | /// <param name="height">Y-axis length.</param> |
| | 54 | | /// <param name="depth">Z-axis length.</param> |
| | 55 | | /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param> |
| | 56 | | /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param> |
| | 57 | | public NativeArray3D(int width, int height, int depth, Allocator allocator, |
| | 58 | | NativeArrayOptions nativeArrayOptions) |
| 10 | 59 | | { |
| 10 | 60 | | Width = width; |
| 10 | 61 | | Height = height; |
| 10 | 62 | | DepthLength = width * height; |
| 10 | 63 | | Depth = depth; |
| 10 | 64 | | Length = width * height * depth; |
| 10 | 65 | | Array = new NativeArray<T>(Length, allocator, nativeArrayOptions); |
| 10 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Access a specific location in the voxel. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="x">X location index.</param> |
| | 72 | | /// <param name="y">Y location index.</param> |
| | 73 | | /// <param name="z">Z location index.</param> |
| | 74 | | public T this[int x, int y, int z] |
| | 75 | | { |
| | 76 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 1 | 77 | | get => Array[x + Width * (y + Height * z)]; |
| | 78 | |
|
| | 79 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 1 | 80 | | set => Array[x + Width * (y + Height * z)] = value; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Access a specific location in the voxel. |
| | 85 | | /// </summary> |
| | 86 | | /// <param name="index">A three-dimensional index.</param> |
| | 87 | | public T this[int3 index] |
| | 88 | | { |
| | 89 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 1 | 90 | | get => Array[index.x + Width * (index.y + Height * index.z)]; |
| | 91 | |
|
| | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 1 | 93 | | set => Array[index.x + Width * (index.y + Height * index.z)] = value; |
| | 94 | | } |
| | 95 | |
|
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Properly dispose of the <see cref="NativeUniformArray3D{T}" />. |
| | 99 | | /// </summary> |
| | 100 | | public void Dispose() |
| 10 | 101 | | { |
| 10 | 102 | | if (!Array.IsCreated) |
| 0 | 103 | | { |
| 0 | 104 | | return; |
| | 105 | | } |
| | 106 | |
|
| 10 | 107 | | Array.Dispose(); |
| 10 | 108 | | Array = default; |
| 10 | 109 | | } |
| | 110 | |
|
| | 111 | | /// <summary> |
| | 112 | | /// Get the three-dimensional index of a flat array index. |
| | 113 | | /// </summary> |
| | 114 | | /// <param name="index">A flat array index.</param> |
| | 115 | | /// <returns>A three-dimensional voxel index.</returns> |
| | 116 | | public int3 GetFromIndex(int index) |
| 5 | 117 | | { |
| 5 | 118 | | int x = index % Width; |
| 5 | 119 | | int y = (index - x) / Width % Height; |
| 5 | 120 | | int z = index / DepthLength; |
| | 121 | |
|
| 5 | 122 | | return new int3(x, y, z); |
| 5 | 123 | | } |
| | 124 | |
|
| | 125 | | public int GetFromIndex(int3 index) |
| 0 | 126 | | { |
| 0 | 127 | | return index.x + Width * (index.y + Height * index.z); |
| 0 | 128 | | } |
| | 129 | | } |
| | 130 | | } |