Skip to content

Commit 8fcde99

Browse files
add a packer
1 parent b6167a9 commit 8fcde99

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Mathematics/CoordinatePacker.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: Mathematics
4+
* FILE: CoordinatePacker.cs
5+
* PURPOSE: A more clever way to handle some 3D coordinate Stuff
6+
* PROGRAMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
namespace Mathematics
10+
{
11+
using System.Runtime.CompilerServices;
12+
13+
/// <summary>
14+
/// A coordinate packer for 3D coordinates, allowing packing and unpacking of x, y, z coordinates into a single ulong value.
15+
/// </summary>
16+
public static class CoordinatePacker
17+
{
18+
/// <summary>
19+
/// Packs the specified x.
20+
/// </summary>
21+
/// <param name="x">The x.</param>
22+
/// <param name="y">The y.</param>
23+
/// <param name="z">The z.</param>
24+
/// <returns>The packed 3D coordinate as a ulong.</returns>
25+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
26+
public static ulong Pack(uint x, uint y, uint z)
27+
{
28+
return (x & 0x1FFFFF) |
29+
(((ulong)y & 0x1FFFFF) << 21) |
30+
(((ulong)z & 0x1FFFFF) << 42);
31+
}
32+
33+
/// <summary>
34+
/// Unpacks the specified key.
35+
/// </summary>
36+
/// <param name="key">The key.</param>
37+
/// <param name="x">The x.</param>
38+
/// <param name="y">The y.</param>
39+
/// <param name="z">The z.</param>
40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41+
public static void Unpack(ulong key, out uint x, out uint y, out uint z)
42+
{
43+
x = (uint)(key & 0x1FFFFF);
44+
y = (uint)((key >> 21) & 0x1FFFFF);
45+
z = (uint)((key >> 42) & 0x1FFFFF);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)