2 (:use
:cl
:vol
:vector
)
3 (:export
#:scan-convert-line3
))
5 (in-package :bresenham
)
7 (declaim (ftype (function (fixnum fixnum fixnum
9 (simple-array (unsigned-byte 8) 3))
10 (values (simple-array (unsigned-byte 8) 3) &optional
))
11 scan-convert-line3x scan-convert-line3y scan-convert-line3z
))
13 (defun scan-convert-line3x (x1 y1 z1 x2 y2 z2 vol
)
14 ;; x2 - x1 has to be the biggest difference between endpoint
18 ;; initialization for y
23 (dy (- (* 2 dely
) delx
)) ;; decision variable along y
24 (yinc1 (* 2 dely
)) ;; increment along y for dy<0
25 (yinc2 (* 2 (- dely delx
))) ;; inc along y for dy>=0
26 ;; same initialization for z
31 (dz (- (* 2 delz
) delx
))
33 (zinc2 (* 2 (- delz delx
))))
35 (error "x2 is <= x1."))
36 (setf (aref vol z y x
) 255)
37 (loop while
(< x x2
) do
39 (if (< dy
0) ;; then no change in y
40 (incf dy yinc1
) ;; update dy
42 (incf dy yinc2
) ;; update dy, and
43 (incf y ysign
)));; increment/decrement y
50 (setf (aref vol z y x
) 255)))
52 ;; start from scan-convert-line3x and replace x->$, y->x, $->y
53 (defun scan-convert-line3y (x1 y1 z1 x2 y2 z2 vol
)
60 (dx (- (* 2 delx
) dely
))
62 (xinc2 (* 2 (- delx dely
)))
67 (dz (- (* 2 delz
) dely
))
69 (zinc2 (* 2 (- delz dely
))))
71 (error "y2 is <= y1."))
72 (setf (aref vol z y x
) 255)
73 (loop while
(< y y2
) do
85 (setf (aref vol z y x
) 255)))
87 ;; replace x->$, z->x, $->z
88 (defun scan-convert-line3z (x1 y1 z1 x2 y2 z2 vol
)
95 (dy (- (* 2 dely
) delz
))
97 (yinc2 (* 2 (- dely delz
)))
102 (dx (- (* 2 delx
) delz
))
104 (xinc2 (* 2 (- delx delz
))))
106 (error "z2 is <= z1."))
107 (setf (aref vol z y x
) 255)
108 (loop while
(< z z2
) do
121 (setf (aref vol z y x
) 255)))
124 (declaim (ftype (function (vec-i vec-i
(simple-array (unsigned-byte 8) 3))
125 (values (simple-array (unsigned-byte 8) 3) &optional
))
127 (defun scan-convert-line3 (start end vol
)
128 (let* ((diff (v--i end start
))
129 (ls (list (list (vec-i-x diff
) 2)
130 (list (vec-i-y diff
) 1)
131 (list (vec-i-z diff
) 0)))
132 (diffa (mapcar #'(lambda (e) (list (abs (first e
))
134 ;; find the direction with the biggest difference
135 (sorted-diff-a (sort diffa
#'> :key
#'car
))
136 (main-direction (second (first sorted-diff-a
))) ;; 2 corresponds to x, 1->y, 0->z
137 ;; find the order in which to deliver the points
138 (main-diff (aref diff main-direction
))
139 ;; we have to swap the points when main-diff is negative
140 (swap-points?
(< main-diff
0))
141 ;; create the function name to dispatch to
142 (function (intern (format nil
"SCAN-CONVERT-LINE3~a" (ecase main-direction
146 (when (eq 0 main-diff
)
147 (error "start and end point are the same."))
169 (let ((vol (make-array (list 128 128 128) :element-type
'(unsigned-byte 8))))
170 (save-stack-ub8 "/home/martin/tmp/line"
171 (scan-convert-line3 (make-vec-i :x
0 :y
0 :z
0)
172 (make-vec-i :x
120 :y
127 :z
127)