# Compute perspective camera distance based on an axis aligned bounding box

I have tried going through a lot of source material to find a clear answer to this but i have failed to do so. I have an ellipsoidal wgs84 projected earth model on which i have several geometries. I want to set the viewing frustum on one of these geometries. These geometries are 3D so i basically find the axis aligned bounding box of my geometry in world coordinates. I dont really have direct access to the camera so modifying the projection or the model view matrices of the camera directly is not an option. What i do know is the fovy and the bounding box and from this i intend to “fit” this bounding box to the viewing frustum.

I have put together a few steps which i think needs to be followed for this

1. Find the rotation from the bounding box center to the z-axis.
2. Transform the axis aligned bounding box to an oriented bounding box by multiplying with this rotation.
3. Consider the corners of the bounding box which were originally corner1(xmin,ymin,zmin) and corner2(xmin,ymax,zmax) and find the new values of this after rotation.
4. Find the distance between corner 1 and corner2 extentDistance. Then do cameraDistance = (extentDistance/2)/tan(fovy/2).

Is this entire procedure fine, or are there some fundamental issues with it?