local p={}local function N(p)return math.sqrt((p.x^2+p.y^2)+p.z^2)/2 end function p.fitBoundingBoxToCamera(p,q,k)local a=N(p)local Y=.5*math.rad(q)if k<1 then Y=math.atan(k*math.tan(Y))end return a/math.sin(Y)end return p
--- Utility methods for cameras
-- @module CameraUtils
-- @author Quenty

local CameraUtils = {}

local function getRadius(size)
	return math.sqrt(size.x^2 + size.y^2 + size.z^2)/2
end

--- Use spherical bounding box to calculate how far back to move a camera
-- See: https://community.khronos.org/t/zoom-to-fit-screen/59857/12
function CameraUtils.fitBoundingBoxToCamera(size, fov, aspectRatio)
	local radius = getRadius(size)

	local halfMinFov = 0.5 * math.rad(fov)
	if aspectRatio < 1 then
		halfMinFov = math.atan(aspectRatio * math.tan(halfMinFov))
	end

	return radius / math.sin(halfMinFov)
end

return CameraUtils