r10421: major development
[cl-photo.git] / dof.lisp
index e3793b4969f4af6791880ac09902065d7ba165a5..9cb2486cf719de2373bf9ac45723825423c8c4b0 100755 (executable)
--- a/dof.lisp
+++ b/dof.lisp
@@ -19,7 +19,6 @@
 
 (in-package #:photo)
 
-;; Based on http://www.photostuff.co.uk/dofmstr.htm
 
 (defun coc-format (format &key (lpm 5) (minimum-distance 250) 
                           (viewing-distance 250)
@@ -41,8 +40,8 @@ Default resolving power is 5 lpm at 25cm."
   (let* ((dim (format-dimensions format))
          (pixel-width (/ (car dim) nx))
          (pixel-height (/ (cdr dim) ny))) 
-    (values (coerce (* 2 pixel-width) 'float)
-            (coerce (* 2 pixel-height) 'float))))
+    (values (float (* 2 pixel-width))
+            (float (* 2 pixel-height)))))
 
 (defun coc-sensor-camera (camera &key (format :aps))
   (let ((dim (sensor-dimensions camera :format format)))
@@ -54,13 +53,20 @@ Default resolving power is 5 lpm at 25cm."
          (width (round (sqrt (* aspect-ratio 1000000 megapixels)))))
     (cons width (round (/ width aspect-ratio)))))
 
-(defun sensor-dimensions (camera &key (format :aps))
-  (etypecase camera
+
+(defun sensor-dimensions (sensor-spec &key (format :aps))
+  "Returns the number of pixels for a sensor. 
+CAMERA-SPEC is either a keyword designating the camera or
+the number of megapixels of the sensor.
+FORMAT should be defined if the CAMERA-SPEC is the number of megapixels
+so the proper aspect ratio is used."
+  (etypecase sensor-spec
     (keyword
-     (ecase camera
+     (ecase sensor-spec
        ;; nikon
        (:d2x (cons 4288 2848))
-       (:d2x (cons 2484 1242))
+       (:d100 (cons 3037 2024))
+       (:d2h (cons 2464 1632))
        (:d70 (cons 3008 2000))
 
        ;; canon
@@ -71,42 +77,72 @@ Default resolving power is 5 lpm at 25cm."
 
        ))
     (number
-     (sensor-dimensions-megapixels format camera))))
+     (sensor-dimensions-megapixels format sensor-spec))))
 
-(defun coc-airy-disk (f-stop)
+(defun coc-airy (f-stop)
   "Return the circle of confusion based on the airy disk."
   (let ((airy (/ f-stop 1500)))
-    (coerce (* 2 airy) 'float)))
+    (float (* 2 airy))))
 
-(defun dof (focal-length f-stop distance coc)
-  "Returns depth of field as fives values: 
-near dof, far dof, total dof, near point, far point.
+(defun dof-mm (focal-length f-stop distance coc &key (pupil-factor 1))
+  "Returns depth of field based on focal-length, f-stop, distance, and coc.
+Six values are returned:
+near dof, far dof, total dof, near point, far point, magnification,
+blur size at infinity (mm).
 Circle of confusion can either be a number or keyword designating format."
-  (let* ((aperature (/ focal-length f-stop))
+  (let* ((aperture (/ focal-length f-stop)) 
+         (numerator-1 (* (- pupil-factor 1) (- distance focal-length)
+                         coc focal-length))
+         (numerator-2 (* pupil-factor aperture focal-length distance))
+         (denominator-1 (* pupil-factor coc (- distance focal-length)))
+         (denominator-2 (* pupil-factor aperture focal-length))
+         (near (/ (+ numerator-1 numerator-2)
+                  (+ denominator-1 denominator-2)))
+         (far (/ (- numerator-1 numerator-2)
+                 (- denominator-1 denominator-2)))
+         (mag (float (/ focal-length (- distance focal-length))))
+         (infinity-blur-diameter (/ (* mag focal-length) f-stop))
+         (depth (- far near)))
+    (values near far depth mag infinity-blur-diameter)))
+
+;; Simplified calculation for symmetric lens
+(defun dof-symmetric-mm (focal-length f-stop distance coc)
+  "Returns depth of field based on focal-length, f-stop, distance, and coc.
+Six values are returned:
+near dof, far dof, total dof, near point, far point, magnification,
+blur size at infinity (mm).
+Circle of confusion can either be a number or keyword designating format.
+Pupil factor is the ratio of the exit to enterance pupil diameters."
+  (let* ((aperture (/ focal-length f-stop))
          (numerator (* distance coc (- distance focal-length)))
-         (factor-1 (* focal-length aperature))
+         (factor-1 (* focal-length aperture))
          (factor-2 (* coc (- distance focal-length)))
-         (near (/ numerator (+ factor-1 factor-2)))
-         (far (/ numerator (- factor-1 factor-2)))
-         (depth (+ far near)))
-    (values near far depth (- distance near) (+ distance far))))
-
-(defun dof-feet (focal-length f-stop distance coc)
-  (multiple-value-bind (near-dof far-dof total-dof near-point far-point)
-      (dof focal-length f-stop (feet->mm distance) coc)
-    (values (mm->feet near-dof) (mm->feet far-dof) (mm->feet total-dof)
-            (mm->feet near-point) (mm->feet far-point))))
-
-(defun dof-meters (focal-length f-stop distance coc)
-  (multiple-value-bind (near-dof far-dof total-dof near-point far-point)
-      (dof focal-length f-stop (* 1000 distance) coc)
-    (values (* 0.001 near-dof) (* 0.001 far-dof) (* 0.001 total-dof)
-            (* 0.001 near-point) (* 0.001 far-point))))
-
-(defun hyperfocal (focal-length f-stop coc)
-  (+ focal-length (/ (* focal-length focal-length) (* f-stop coc))))
-
-(defun hyperfocal-feet (focal-length f-stop coc)
-  (mm->feet (hyperfocal focal-length f-stop coc)))
-
-
+         (near (- distance (/ numerator (+ factor-1 factor-2))))
+         (far (+ distance (/ numerator (- factor-1 factor-2))))
+         (mag (magnification focal-length distance))
+         (infinity-blur-diameter (/ (* mag focal-length) f-stop))
+         (depth (- far near)))
+    (values near far depth mag infinity-blur-diameter)))
+
+(defun dof (focal-length f-stop distance coc &key (units :mm) (pupil-factor 1))
+  (multiple-value-bind (near-point far-point total-dof mag blur)
+      (dof-mm focal-length f-stop (length->mm distance units) coc 
+              :pupil-factor pupil-factor)
+    (values (mm->length near-point units)
+            (mm->length far-point units)
+            (mm->length total-dof units)
+            mag blur)))
+
+(defun hyperfocal (focal-length f-stop coc &key (units :mm))
+  (mm->length 
+   (+ focal-length (/ (* focal-length focal-length) f-stop coc))
+   units))
+
+(defun magnification (focal-length distance)
+  (float (/ focal-length (- distance focal-length))))
+
+(defun bellows-factor (focal-length distance)
+  (1+ (magnification focal-length distance)))
+
+(defun effective-aperture (focal-length distance aperture)
+  (* aperture (bellows-factor focal-length distance)))
\ No newline at end of file