/// <summary> /// Gets the array index for a local voxel position within a grid. /// </summary> /// <param name="grid">The grid to compare within.</param> /// <returns>The array index at the given location.</returns> /// <exception cref="System.ArgumentOutOfRangeException"> /// If the voxel position is negative or greater than or equal to the /// grid size along any axis. /// </exception> public static int Index(this IVoxelPosition pos, GridSize grid) { if (!pos.IsWithinGrid(grid)) { throw new System.ArgumentOutOfRangeException($"Voxel position ({pos}) is outside of this grid size ({grid})!"); } int size = grid.Value; return(pos.X * size * size + pos.Y * size + pos.Z); }
/// <summary> /// Gets the array index for a local voxel position within a grid. /// </summary> /// <param name="grid">The grid to compare within.</param> /// <returns>The array index at the given location.</returns> public static int Index(this IVoxelPosition pos, GridSize grid) { int size = grid.Value; return(pos.X * size * size + pos.Y * size + pos.Z); }
/// <summary> /// Checks if this voxel position exists within the target grid size. /// </summary> /// <param name="grid">The grid size to test against.</param> /// <returns>True if this position is withint the grid. False otherwise.</returns> public static bool IsWithinGrid(this IVoxelPosition pos, GridSize grid) { return((pos.X & grid.Mask) == pos.X && (pos.Y & grid.Mask) == pos.Y && (pos.Z & grid.Mask) == pos.Z); }