Given a grid of rhombic dodecahedrons and a point in 3D Cartesian space, how can I find the coordinates of the dodecahedron which encloses the point?

31 Views Asked by At

I have found a couple of (very similar) papers discussing a coordinate system using a grid of rhombic dodecahedrons, but the equations they give for converting from Cartesian space to this new coordinate system cannot be easily transformed to return the coordinates of the enclosing dodecahedron. The coordinate system used does not matter to me so long as each dodecahedron can be uniquely identified using its coordinates.

I have found this very helpful website with instructions on how to find the coordinates of the enclosing hexagon given a grid of hexagons. I would like to do something similar in 3D.

An answer in C would be best, but at least the theory would be greatly appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

Thanks to C614's comment I was able to write this code to solve my problem:

int3 CartesianToRDG(in float3 c)
{
    int x, y, z;

    // Round coordinates to the nearest integer.
    x = round(c.x);
    y = round(c.y);
    z = round(c.z);

    // Get the magnitude of the fractional component of each coordinate.
    float diff_x = abs(c.x - x);
    float diff_y = abs(c.y - y);
    float diff_z = abs(c.z - z);

    // Find the coordinate with the highest magnitude and round it such
    //  that all coordinates round to an even number.
    if (diff_x > diff_y && diff_x > diff_z)
    {
        if ((ceil(c.x) + y + z) % 2 == 0)
        {
            x = ceil(c.x);
        }
        else
        {
            x = floor(c.x);
        }
    }
    else if (diff_y > diff_z)
    {
        if ((x + ceil(c.y) + z) % 2 == 0)
        {
            y = ceil(c.y);
        }
        else
        {
            y = floor(c.y);
        }   
    }
    else
    {
        if ((x + y + ceil(c.z)) % 2 == 0)
        {
            z = ceil(c.z);
        }
        else
        {
            z = floor(c.z);
        } 
    }

    return int3(x, y, z);
}
```