4dcb7e7586fc2ed2002b03d00d707d5f53ad1463
[cl-photo.git] / dof.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10; Package: photo -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          dof.lisp
6 ;;;; Purpose:       Depth of field functions for cl-photo
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  April 2005
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of cl-photo, is Copyright (c) 2005 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; cl-photo users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the GNU General Public License v2
16 ;;;; (http://www.gnu.org/licenses/gpl.html)
17 ;;;;
18 ;;;; *************************************************************************
19
20 (in-package #:photo)
21
22
23 (defun coc-format (format &key (lpm 5) (minimum-distance 250) 
24                           (viewing-distance 250)
25                           (print-size (format-dimensions :8x10in)))
26   "Returns circle of confusion in mm and print magnification for a format. 
27 Default resolving power is 5 lpm at 25cm."
28
29   (let* ((format-size (format-dimensions format))
30          (format-diagonal (diagonal (car format-size) (cdr format-size)))
31          (print-diagonal (diagonal (car print-size) (cdr print-size)))
32          (resolution-factor (/ (* lpm print-diagonal minimum-distance)
33                                (* format-diagonal viewing-distance)))
34          (coc (/ 1.0d0 resolution-factor))
35          (print-magnification (/ print-diagonal format-diagonal)))
36     (values coc print-magnification)))
37
38 (defun coc-sensor (format nx ny)
39   "Returns circle of confusion based on pixel size."
40   (let* ((dim (format-dimensions format))
41          (pixel-width (/ (car dim) nx))
42          (pixel-height (/ (cdr dim) ny))) 
43     (values (float (* 2 pixel-width))
44             (float (* 2 pixel-height)))))
45
46 (defun coc-sensor-camera (camera &key (format :aps))
47   (let ((dim (sensor-dimensions camera :format format)))
48     (coc-sensor format (car dim) (cdr dim))))
49
50
51
52
53
54 (defun coc-airy (f-stop)
55   "Return the circle of confusion based on the airy disk."
56   (let ((airy (/ f-stop 1500)))
57     (float (* 2 airy))))
58
59 (defun dof-mm (focal-length f-stop distance coc &key (pupil-factor 1))
60   "Returns depth of field based on focal-length, f-stop, distance, and coc.
61 Six values are returned:
62 near dof, far dof, total dof, near point, far point, magnification,
63 blur size at infinity (mm).
64 Circle of confusion can either be a number or keyword designating format."
65   (let* ((aperture (/ focal-length f-stop)) 
66          (numerator-1 (* (- pupil-factor 1) (- distance focal-length)
67                          coc focal-length))
68          (numerator-2 (* pupil-factor aperture focal-length distance))
69          (denominator-1 (* pupil-factor coc (- distance focal-length)))
70          (denominator-2 (* pupil-factor aperture focal-length))
71          (near (/ (+ numerator-1 numerator-2)
72                   (+ denominator-1 denominator-2)))
73          (far (/ (- numerator-1 numerator-2)
74                  (- denominator-1 denominator-2)))
75          (mag (float (/ focal-length (- distance focal-length))))
76          (infinity-blur-diameter (/ (* mag focal-length) f-stop))
77          (depth (- far near)))
78     (values near far depth mag infinity-blur-diameter)))
79
80 ;; Simplified calculation for symmetric lens
81 (defun dof-symmetric-mm (focal-length f-stop distance coc)
82   "Returns depth of field based on focal-length, f-stop, distance, and coc.
83 Six values are returned:
84 near dof, far dof, total dof, near point, far point, magnification,
85 blur size at infinity (mm).
86 Circle of confusion can either be a number or keyword designating format.
87 Pupil factor is the ratio of the exit to enterance pupil diameters."
88   (let* ((aperture (/ focal-length f-stop))
89          (numerator (* distance coc (- distance focal-length)))
90          (factor-1 (* focal-length aperture))
91          (factor-2 (* coc (- distance focal-length)))
92          (near (- distance (/ numerator (+ factor-1 factor-2))))
93          (far (+ distance (/ numerator (- factor-1 factor-2))))
94          (mag (magnification focal-length distance))
95          (infinity-blur-diameter (/ (* mag focal-length) f-stop))
96          (depth (- far near)))
97     (values near far depth mag infinity-blur-diameter)))
98
99 (defun dof (focal-length f-stop distance coc &key (units :mm) (pupil-factor 1))
100   (multiple-value-bind (near-point far-point total-dof mag blur)
101       (dof-mm focal-length f-stop (length->mm distance units) coc 
102               :pupil-factor pupil-factor)
103     (values (mm->length near-point units)
104             (mm->length far-point units)
105             (mm->length total-dof units)
106             mag blur)))
107
108 (defun hyperfocal (focal-length f-stop coc &key (units :mm))
109   (mm->length 
110    (+ focal-length (/ (* focal-length focal-length) f-stop coc))
111    units))
112
113 (defun magnification (focal-length distance)
114   (float (/ focal-length (- distance focal-length))))
115
116 (defun bellows-factor (focal-length distance)
117   (1+ (magnification focal-length distance)))
118
119 (defun effective-aperture (focal-length distance aperture)
120   (* aperture (bellows-factor focal-length distance)))