User Tools

Site Tools


Calculate Field of View (FOV)

Summary

Below is an example demonstrating how to calculate the Field of View (FOV) of the camera for the active viewport, via script.

API Areas of Interest

Example

Calculate_FOV.dsa
(function(){
 
	/***********************************************************************/
	// Number : Function for getting the length of the diagonal for a given rectangle
	function diagonal( nWidth, nHeight )
	{
		// Return the length of the hypotenuse; Pythagorean Theorem
		return Math.sqrt( (nWidth * nWidth) + (nHeight * nHeight) );
	};
 
	/***********************************************************************/
	// Number : Function for converting radians to degrees
	function radiansToDegrees( nRadians )
	{
		// Return the conversion
		return nRadians * 57.2957795;
	};
 
	/***********************************************************************/
	// Number : Function for calculating the field of view of a camera
	function fieldOfView( nDimension, nFocalLength )
	{
		// Return the field of view for the desired dimension
		return radiansToDegrees( 2 * Math.atan( nDimension / (2 * nFocalLength) ) );
	};
 
	/***********************************************************************/
	// String : Function for formatting a float value to string
	function floatToString( sPrefix, nValue, sSuffix, nPrecision )
	{
		return String("%1%2%3")
			.arg( sPrefix )
			.arg( parseFloat( String("%1").argDec( nValue, 0, "f", nPrecision ) ) )
			.arg( sSuffix );
	};
 
	/***********************************************************************/
	// Get the viewport manager
	var oViewMgr = MainWindow.getViewportMgr();
	// Get the active viewport
	var oView = oViewMgr.getActiveViewport();
	// Get the 3D viewport
	var o3DView = oView.get3DViewport();
	// Get the active camera
	var oCamera = o3DView.getCamera();
 
	// Declare working variables
	var nFrameWidth;
	var nAspect;
	var oRenderMgr;
	var oOptions;
 
	// If the version of the application is 4.6.4.70 or newer
	if( App.version64 >= 0x0004000600040046 ){
		// Get the frame (film/sensor) width of the camera
		nFrameWidth = oCamera.frameWidth;
 
		// Get the aspect from the camera
		nAspect = oCamera.aspectRatio;
	// If the version of the application is older than 4.6.4.70
	} else {
		// If the version of the application is 4.6.4.69 or newer
		if( App.version64 >= 0x0004000600040045 ){
			nFrameWidth = oCamera.frameWidth;
		// If the version of the application is older than 4.6.4.69
		} else {
			// The frame (film/sensor) width is hard coded at 35mm
			nFrameWidth = 35;
		}
 
		// Get the render manager
		oRenderMgr = App.getRenderMgr();
		// Get the render options
		oOptions = oRenderMgr.getRenderOptions();
		// Get the aspect of the render options
		nAspect = oOptions.aspect;
	}
 
	// Calculate the height of the frame (film/sensor)
	var nFrameHeight = nFrameWidth * (1 / nAspect);
	// Get the diagonal length of the frame
	var nFrameDiagonal = diagonal( nFrameWidth, nFrameHeight );
 
	// Display dimension information
	print( floatToString( "Frame Width:\t", nFrameWidth, " mm", 1 ) );
	print( floatToString( "Frame Height:\t", nFrameHeight, " mm", 1 ) );
	print( floatToString( "Frame Diagonal:\t", nFrameDiagonal, " mm", 1 ) );
	print( floatToString( "Frame Aspect:\t", nAspect, "", 2 ) );
 
	// Display focal length
	print( floatToString( "Focal Length:\t", oCamera.focalLength, " mm", 1 ) );
 
	// Get the field of view, for each dimension
	var nFovH = fieldOfView( nFrameWidth, oCamera.focalLength );
	var nFovV = fieldOfView( nFrameHeight, oCamera.focalLength );
	var nFovD = fieldOfView( nFrameDiagonal, oCamera.focalLength );
 
	// Display FOV information
	print( floatToString( "FOV Horiz:\t", nFovH, " deg", 1 ) );
	print( floatToString( "FOV Vert:\t", nFovV, " deg", 1 ) );
	print( floatToString( "FOV Diag:\t", nFovD, " deg", 1 ) );
 
	// Declare working variables
	var nFlm;
	var nFlmFL;
 
	// Display focal length adjustment information
	if( nFrameWidth == 35 ){
		print( "---" );
 
		// Calculate focal length multiplier
		nFlm = 43.3/nFrameDiagonal;
		nFlmFL = oCamera.focalLength * nFlm;
 
		print( floatToString( "135 FLM:\t", nFlm, " X", 2 ) );
		print( floatToString( "135 FOV FL:\t", nFlmFL, " mm", 1 ) );
	}
 
})();