Uniformly sampling points in a cube using a coordinate system [C#, Unity]

320 Views Asked by At

I have a cube with six vertices of form (x, y, z), in a 3D coordinate system. All information about cube is available, including vertices, edge lengths, etc.

I would like an efficient way to uniformly return n ^ 3 points, with n points on each edge, something like the coordinate system itself. An image is attached (ignore the colouring) (source: Sebastian Lague's video on marching cubes):Evenly distributed points

I've more or less been able to implement this in Python, and I'd like a C# version.

An example of what I want: I've used a unit cube at the origin as an example, what I'm after is pseudocode or logic that works for any cube at any coordinates. More information: I'd like to have code that works for cubes in any orientation, but failing that code that works for cubes aligned with the grid but in any position.

Thanks to Jon Skeet for the help and clarification!

/*
Input:
(0, 0, 0)
(1, 0, 0)
(1, 1, 0)
(1, 1, 1)
(0, 1, 1)
(0, 0, 1)
(0, 1, 0)
(1, 0, 1)

27 points to be generated: n = 3
3 points per edge (INCLUDING VERTICES)

Output:
(0, 0, 0)
(1, 0, 0)
(1, 1, 0)
(1, 1, 1)
(0, 1, 1)
(0, 0, 1)
(0, 1, 0)
(1, 0, 1)
(0, 0.5, 0)
(0.5, 0, 0)
(0, 0, 0.5)
(0.5, 0.5, 0)
(0.5, 0, 0.5)
(0.5, 0.5, 0.5)
(0, 0.5, 0.5)
(1, 0.5, 0)
... etc
*/

I'm using it in Unity, so answers with Vector3 and such would work too.

Some pseudocode:

// MagicPointGenerator is really what I'm after

float[][] GeneratePoints(float[][] vertices, int pointsPerEdge)
    {
        float[][] points = new float[(int)Math.Pow(pointsPerEdge, 3)][3];
        for (int i = 0; i <= pointsPerEdge; i++)
        {
            for (int j = 0; j <= pointsPerEdge; j++)
            {
                for (int k = 0; k <= pointsPerEdge; k++)
                {
                    points[i + j + k] = MagicPointGenerator(i, j, k);
                }
            }
        }
        return points;
    }
0

There are 0 best solutions below