4 (:export
#:scan-convert-line3
))
6 (in-package :bresenham
)
9 (defun scan-convert-line3x (x1 y1 z1 x2 y2 z2 vol
)
10 (declare (fixnum x1 y1 z1 x2 y2 z2
)
11 ((simple-array (unsigned-byte 8) 3) vol
)
12 (values (simple-array (unsigned-byte 8) 3) &optional
))
13 ;; x2 - x1 has to be the biggest difference between endpoint
17 ;; initialization for y
22 (dy (- (* 2 dely
) delx
)) ;; decision variable along y
23 (yinc1 (* 2 dely
)) ;; increment along y for dy<0
24 (yinc2 (* 2 (- dely delx
))) ;; inc along y for dy>=0
25 ;; same initialization for z
30 (dz (- (* 2 delz
) delx
))
32 (zinc2 (* 2 (- delz delx
))))
34 (error "x2 is <= x1."))
35 (setf (aref vol z y x
) 255)
36 (loop while
(< x x2
) do
38 (if (< dy
0) ;; then no change in y
39 (incf dy yinc1
) ;; update dy
41 (incf dy yinc2
) ;; update dy, and
42 (incf y ysign
)));; increment/decrement y
49 (setf (aref vol z y x
) 255)))
51 ;; start from scan-convert-line3x and replace x->$, y->x, $->y
52 (defun scan-convert-line3y (x1 y1 z1 x2 y2 z2 vol
)
53 (declare (fixnum x1 y1 z1 x2 y2 z2
)
54 ((simple-array (unsigned-byte 8) 3) vol
)
55 (values (simple-array (unsigned-byte 8) 3) &optional
))
62 (dx (- (* 2 delx
) dely
))
64 (xinc2 (* 2 (- delx dely
)))
69 (dz (- (* 2 delz
) dely
))
71 (zinc2 (* 2 (- delz dely
))))
73 (error "y2 is <= y1."))
74 (setf (aref vol z y x
) 255)
75 (loop while
(< y y2
) do
87 (setf (aref vol z y x
) 255)))
89 ;; replace x->$, z->x, $->z
90 (defun scan-convert-line3z (x1 y1 z1 x2 y2 z2 vol
)
91 (declare (fixnum x1 y1 z1 x2 y2 z2
)
92 ((simple-array (unsigned-byte 8) 3) vol
)
93 (values (simple-array (unsigned-byte 8) 3) &optional
))
100 (dy (- (* 2 dely
) delz
))
102 (yinc2 (* 2 (- dely delz
)))
107 (dx (- (* 2 delx
) delz
))
109 (xinc2 (* 2 (- delx delz
))))
111 (error "z2 is <= z1."))
112 (setf (aref vol z y x
) 255)
113 (loop while
(< z z2
) do
126 (setf (aref vol z y x
) 255)))
129 (defun scan-convert-line3 (start end vol
)
130 (declare (vec-i start end
)
131 ((simple-array (unsigned-byte 8) 3) vol
)
132 (values (simple-array (unsigned-byte 8) 3) &optional
))
133 (let* ((diff (v--i end start
))
134 (ls (list (list (vec-i-x diff
) 2)
135 (list (vec-i-y diff
) 1)
136 (list (vec-i-z diff
) 0)))
137 (diffa (mapcar #'(lambda (e) (list (abs (first e
))
139 ;; find the direction with the biggest difference
140 (sorted-diff-a (sort diffa
#'> :key
#'car
))
141 (main-direction (second (first sorted-diff-a
))) ;; 2 corresponds to x, 1->y, 0->z
142 ;; find the order in which to deliver the points
143 (main-diff (aref diff main-direction
))
144 ;; we have to swap the points when main-diff is negative
145 (swap-points?
(< main-diff
0))
146 ;; create the function name to dispatch to
147 (function (ecase main-direction
148 (2 #'scan-convert-line3x
)
149 (1 #'scan-convert-line3y
)
150 (0 #'scan-convert-line3z
))))
151 (when (eq 0 main-diff
)
152 (error "start and end point are the same."))
174 (let ((vol (make-array (list 128 128 128) :element-type
'(unsigned-byte 8))))
175 (save-stack-ub8 "/home/martin/tmp/line"
176 (scan-convert-line3 (make-vec-i :x
0 :y
0 :z
0)
177 (make-vec-i :x
120 :y
127 :z
127)