Skip to content

Instantly share code, notes, and snippets.

@dbr
Last active December 10, 2015 05:28
Show Gist options
  • Save dbr/4387325 to your computer and use it in GitHub Desktop.
Save dbr/4387325 to your computer and use it in GitHub Desktop.
Camera near and far limit formula
from math import sqrt
# Camera parameters
focal = 25 #mm
fstop = 2.8 # f number
focus_distance = 5 # Unsure of unit? Probably mm, maybe metre?
# Circle of confusion diameter limit, as per
# http://en.wikipedia.org/wiki/Zeiss_formula (with modern 1500 constant)
filmback_width = 35
filmback_height = 24
coc_size = sqrt(filmback_width**2 + filmback_height**2) / 1500
# Calculations, as based on
# http://www.nikonians.org/html/resources/guides/dof/hyperfocal5.html
hyperfocal = focal**2 / (fstop * coc_size)
near_focus_limit = (
(hyperfocal * focus_distance)
/ (hyperfocal + (focus_distance - focal)))
far_focus_limit = (
(hyperfocal * focus_distance)
/ (hyperfocal + (focus_distance + focal)))
dof = far_focus_limit - near_focus_limit
# To visualise, for a camera that faces down -z axis by default
far_locator = (0, 0, -(focus_distance + far_focus_limit))
near_locator = (0, 0, -(focus_distance + near_focus_limit))
# ..then multiply the two vectors by the inverse of the
# world-to-camera matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment