Calculate corners of a frustum cluster

  c++, frustum, math, rendering

I am working on debug visualization for rendering, and one of the processes involves splitting the view frustum into clusters and visualizing them. The clusters are similar to the ones shown in the right half of the image below:

enter image description here

I have a camera with its transform and projection matrices, and a 3D integer vector that defines how many "splits" the frustum has along each axis. I want to write a process that takes the indices along all three axes to select a frustum cluster (e.g the one in the above image would be "[3, 2, 3]"), and it returns the world space coordinates of all 8 corners of said cluster, which I can then use to render a wireframe/box/etc.

I provided pseudocode below:

std::vector<float3> getClusterCorners(Matrix world, Matrix projection, int3 segmentation, int3 cluster)
    // What happens here?
    // ...

// Get the inputs
Matrix cameraWorld;
Matrix cameraProjection;
int3 frustumSegmentation;
int3 selectedCluster;

// Calculate the corners
std::vector<float3> cornerCoordinates = clusterCorners(cameraWorld, cameraProjection, frustumSegmentation, selectedCluster);

// Visualize the cluster

What is the most elegant way to implement getClusterCorners? I already thought of a naive way, which calculates the point in XY on the near and far planes, gets the line connecting them, and intersects it with the Z slice. Is there a more efficient approach, perhaps by using the projection matrix?

Source: Windows Questions C++