Last active
December 10, 2015 05:28
-
-
Save dbr/4387325 to your computer and use it in GitHub Desktop.
Camera near and far limit formula
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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