path-{translate,rotate,scale} can now take list of paths.
[cl-vectors.git] / paths.lisp
blobeb090fac1edaa67eb3aea519c6d609c8fed68036
1 ;;;; cl-vectors -- Rasterizer and paths manipulation library
2 ;;;; Copyright (C) 2007 Frédéric Jolliton <frederic@jolliton.com>
3 ;;;;
4 ;;;; This library is free software; you can redistribute it and/or
5 ;;;; modify it under the terms of the Lisp Lesser GNU Public License
6 ;;;; (http://opensource.franz.com/preamble.html), known as the LLGPL.
7 ;;;;
8 ;;;; This library is distributed in the hope that it will be useful, but
9 ;;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lisp
11 ;;;; Lesser GNU Public License for more details.
13 ;;;; This file provides facilities to create and manipulate vectorial paths.
15 #+nil(error "This file assume that #+NIL is never defined.")
17 (in-package #:net.tuxee.paths)
19 (defvar *bezier-distance-tolerance* 0.5
20 "The default distance tolerance used when rendering Bezier
21 curves.")
23 (defvar *bezier-angle-tolerance* 0.05
24 "The default angle tolerance (in radian) used when rendering
25 Bezier curves")
27 (defvar *arc-length-tolerance* 1.0
28 "The maximum length of segment describing an arc.")
30 (defvar *miter-limit* 4.0
31 "Miter limit before reverting to bevel joint. Must be >=1.0.")
33 ;;;--[ Math utilities ]------------------------------------------------------
35 ;;; http://mathworld.wolfram.com/Line-LineIntersection.html
36 (defun line-intersection (x1 y1 x2 y2
37 x3 y3 x4 y4)
38 "Compute the intersection between 2 lines (x1,y1)-(x2,y2)
39 and (x3,y3)-(x4,y4). Return the coordinates of the intersection
40 points as 2 values. If the 2 lines are colinears, return NIL."
41 (flet ((det (a b c d)
42 (- (* a d)
43 (* b c))))
44 (let* ((dx1 (- x2 x1))
45 (dy1 (- y2 y1))
46 (dx2 (- x4 x3))
47 (dy2 (- y4 y3))
48 (d (det dx2 dy2 dx1 dy1)))
49 (unless (zerop d)
50 (let ((a (det x1 y1 x2 y2))
51 (b (det x3 y3 x4 y4)))
52 (values (/ (det a dx1 b dx2) d)
53 (/ (det a dy1 b dy2) d)))))))
55 (defun line-intersection/delta (x1 y1 dx1 dy1
56 x2 y2 dx2 dy2)
57 "Compute the intersection between the line by (x1,y1) and
58 direction (dx1,dy1) and the line by (x2,y2) and
59 direction (dx2,dy2). Return the coordinates of the intersection
60 points as 2 values. If the 2 lines are colinears, return NIL."
61 (flet ((det (a b c d)
62 (- (* a d)
63 (* b c))))
64 (let ((d (det dx2 dy2 dx1 dy1)))
65 (unless (zerop d)
66 (let ((a (det x1 y1 (+ x1 dx1) (+ y1 dy1)))
67 (b (det x2 y2 (+ x2 dx2) (+ y2 dy2))))
68 (values (/ (det a dx1 b dx2) d)
69 (/ (det a dy1 b dy2) d)))))))
71 (defun normalize (x y &optional (length 1.0))
72 "Normalize the vector (X,Y) such that its length is LENGTH (or
73 1.0 if unspecified.) Return the component of the resulting vector
74 as 2 values. Return NIL if the input vector had a null length."
75 (if (zerop length)
76 (values 0.0 0.0)
77 (let ((norm (/ (sqrt (+ (* x x) (* y y))) length)))
78 (unless (zerop norm)
79 (values (/ x norm) (/ y norm))))))
81 (defun line-normal (x1 y1 x2 y2)
82 "Normalize the vector (X2-X1,Y2-Y1). See NORMALIZE."
83 (normalize (- x2 x1) (- y2 y1)))
85 ;;;--[ Points ]--------------------------------------------------------------
87 ;;; Points are supposed to be immutable
89 (declaim (inline make-point point-x point-y))
90 (defun make-point (x y) (cons x y))
91 (defun point-x (point) (car point))
92 (defun point-y (point) (cdr point))
94 ;;; Utility functions for points
96 (defun p+ (p1 p2)
97 (make-point (+ (point-x p1) (point-x p2))
98 (+ (point-y p1) (point-y p2))))
100 (defun p- (p1 p2)
101 (make-point (- (point-x p1) (point-x p2))
102 (- (point-y p1) (point-y p2))))
104 (defun p* (point scale &optional (scale-y scale))
105 (make-point (* (point-x point) scale)
106 (* (point-y point) scale-y)))
108 (defun point-rotate (point angle)
109 "Rotate POINT by ANGLE radian around the origin."
110 (let ((x (point-x point))
111 (y (point-y point)))
112 (make-point (- (* x (cos angle)) (* y (sin angle)))
113 (+ (* y (cos angle)) (* x (sin angle))))))
115 (defun point-angle (point)
116 "Compute the angle of POINT relatively to the X axis."
117 (atan (point-y point) (point-x point)))
119 (defun point-norm (point)
120 "Compute the distance of POINT from origin."
121 (sqrt (+ (expt (point-x point) 2)
122 (expt (point-y point) 2))))
124 ;; (point-norm (p- p2 p1))
125 (defun point-distance (p1 p2)
126 "Compute the distance between P1 and P2."
127 (sqrt (+ (expt (- (point-x p2) (point-x p1)) 2)
128 (expt (- (point-y p2) (point-y p1)) 2))))
130 ;; (p* (p+ p1 p2) 0.5)
131 (defun point-middle (p1 p2)
132 "Compute the point between P1 and P2."
133 (make-point (/ (+ (point-x p1) (point-x p2)) 2.0)
134 (/ (+ (point-y p1) (point-y p2)) 2.0)))
136 ;;;--[ Paths ]---------------------------------------------------------------
138 (defstruct path
139 (type :open-polyline :type (member :open-polyline :closed-polyline :polygon))
140 (orientation :unknown :type (member :unknown :cw :ccw))
141 (knots (make-array 0 :adjustable t :fill-pointer 0))
142 (interpolations (make-array 0 :adjustable t :fill-pointer 0)))
144 (defun create-path (type)
145 "Create a new path of the given type. The type must be one of
146 the following keyword:
148 :open-polyline -- An open polyline path,
149 :closed-polyline -- A closed polyline path,
150 :polygon -- Like :closed-polyline, but implicitly filled."
151 (assert (member type '(:open-polyline :closed-polyline :polygon)))
152 (make-path :type type))
154 (defun path-clear (path)
155 "Clear the path such that it is empty."
156 (setf (path-orientation path) :unknown
157 (fill-pointer (path-knots path)) 0
158 (fill-pointer (path-interpolations path)) 0))
160 (defun path-reset (path knot)
161 "Reset the path such that it is a single knot."
162 (path-clear path)
163 (vector-push-extend knot (path-knots path))
164 (vector-push-extend (make-straight-line) (path-interpolations path)))
166 (defun path-extend (path interpolation knot)
167 "Extend the path to KNOT, with INTERPOLATION."
168 (vector-push-extend interpolation (path-interpolations path))
169 (vector-push-extend knot (path-knots path))
170 ;; Extending the path can change how the orientation is
171 ;; auto-detected.
172 (setf (path-orientation path) :unknown))
174 (defun path-concatenate (path interpolation other-path)
175 "Append OTHER-PATH to PATH, joined by INTERPOLATION."
176 (let ((interpolations (path-interpolations other-path))
177 (knots (path-knots other-path)))
178 (loop for i below (length knots)
179 do (path-extend path
180 (interpolation-clone (if (and (zerop i) interpolation)
181 interpolation
182 (aref interpolations i)))
183 (aref knots i)))))
185 (defun path-replace (path other-path)
186 "Replace PATH with contents of OTHER-PATH."
187 (path-clear path)
188 (path-concatenate path nil other-path))
190 (defun path-size (path)
191 "Return the number of knots on the path."
192 (length (path-knots path)))
194 (defun path-last-knot (path)
195 "Return the last knot of the path. Return NIL if the path is
196 empty."
197 (let ((knots (path-knots path)))
198 (when (plusp (length knots))
199 (aref knots (1- (length knots))))))
201 (defun path-guess-orientation (path)
202 "Guess the orientation of the path.
204 This is implemented loosely because we don't take care about
205 interpolations. We only consider a polygon described by the
206 knots. However, it should work..
208 Update path orientation flag, and returns either :CW or :CCW."
209 (let ((knots (path-knots path)))
210 (let ((loose-area (loop for last-knot-index = (1- (length knots)) then knot-index
211 for knot-index below (length knots)
212 sum (- (* (point-x (aref knots last-knot-index))
213 (point-y (aref knots knot-index)))
214 (* (point-x (aref knots knot-index))
215 (point-y (aref knots last-knot-index)))))))
216 (setf (path-orientation path) (if (plusp loose-area) :ccw :cw)))))
218 (defun path-orient (path orientation &optional other-paths)
219 "Orient the path in the given orientation.
221 If OTHER-PATHS is specified, then the paths are reversed
222 inconditionnaly if PATH is also reversed."
223 (assert (member orientation '(:cw :ccw)) (orientation) "Expected either :CW or :CCW")
224 (when (eq (path-orientation path) :unknown)
225 (path-guess-orientation path))
226 (unless (eq (path-orientation path) orientation)
227 (path-reverse path)
228 (map nil #'path-reverse other-paths))
229 (values))
231 ;;; Iterators
233 (defgeneric path-iterator-reset (iterator)
234 (:documentation "Reset the iterator before the first knot."))
236 (defgeneric path-iterator-next (iterator)
237 (:documentation "Move the iterator to the next knot, and return
238 3 values: INTERPOLATION, KNOT and END-P. INTERPOLATION is the
239 interpolation between the previous knot and the current one. For
240 the first iteration, INTERPOLATION is usually the implicit
241 straight line between the last knot and the first knot. KNOT and
242 INTERPOLATION are null if the path is empty. END-P is true if the
243 knot is the last on the path or if the path is empty."))
245 (defun path-from-iterator (iterator type)
246 "Construct a new path from the given iterator."
247 (let ((path (create-path type)))
248 (loop
249 (multiple-value-bind (iterator knot end-p) (path-iterator-next iterator)
250 (path-extend path iterator knot)
251 (when end-p
252 (return path))))))
254 ;;; Classic iterator
256 (defstruct path-iterator-state
257 path index)
259 (defun path-iterator (path)
260 (make-path-iterator-state :path path :index nil))
262 (defmethod path-iterator-reset ((iterator path-iterator-state))
263 (setf (path-iterator-state-index iterator) nil))
265 (defmethod path-iterator-next ((iterator path-iterator-state))
266 (let* ((index (path-iterator-state-index iterator))
267 (path (path-iterator-state-path iterator))
268 (knots (path-knots path))
269 (interpolations (path-interpolations path)))
270 (cond
271 ((zerop (length knots))
272 (values nil nil t))
274 ;; Update index to the next place
275 (setf index
276 (setf (path-iterator-state-index iterator)
277 (if (null index) 0 (mod (1+ index) (length knots)))))
278 (values (aref interpolations index)
279 (aref knots index)
280 (= index (1- (length knots))))))))
282 ;;; Segmented iterator
284 ;;; This iterator iterate over segmented interpolation, if the
285 ;;; interpolation is matched by the predicate. This is useful for
286 ;;; algorithms that doesn't handle certain type of interpolations.
287 ;;; The predicate could test the type, but also certain type of
288 ;;; interpolation (such as arc of circle vs arc of ellipse, or degree
289 ;;; of the Bezier curves.)
291 ;;; Note: I use PI prefix instead of PATH-ITERATOR to shorten names.
293 (defstruct pi-segmented-state
294 path index predicate end-p queue)
296 (defun path-iterator-segmented (path &optional (predicate (constantly t)))
297 (make-pi-segmented-state :path path :index nil
298 :predicate predicate
299 :end-p nil :queue nil))
301 (defmethod path-iterator-reset ((iterator pi-segmented-state))
302 (setf (pi-segmented-state-index iterator) nil
303 (pi-segmented-state-queue iterator) nil))
305 (defmethod path-iterator-next ((iterator pi-segmented-state))
306 (flet ((update-queue (interpolation k1 k2 last-p)
307 (let (new-queue)
308 (interpolation-segment interpolation k1 k2 (lambda (p) (push p new-queue)))
309 (push k2 new-queue)
310 (setf (pi-segmented-state-end-p iterator) last-p
311 (pi-segmented-state-queue iterator) (nreverse new-queue))))
312 (dequeue ()
313 (let* ((knot (pop (pi-segmented-state-queue iterator)))
314 (end-p (and (pi-segmented-state-end-p iterator)
315 (null (pi-segmented-state-queue iterator)))))
316 (values (make-straight-line) knot (when end-p t)))))
317 (cond
318 ((pi-segmented-state-queue iterator)
319 ;; Queue is not empty, process it first.
320 (dequeue))
322 ;; Either refill the queue, or return the next straight line
323 ;; from the sub iterator.
324 (let* ((index (pi-segmented-state-index iterator))
325 (path (pi-segmented-state-path iterator))
326 (knots (path-knots path))
327 (interpolations (path-interpolations path)))
328 (cond
329 ((zerop (length knots))
330 ;; Empty path.
331 (values nil nil t))
333 ;; Update index to the next place
334 (setf index
335 (setf (pi-segmented-state-index iterator)
336 (if (null index) 0 (mod (1+ index) (length knots)))))
337 (let ((interpolation (aref interpolations index))
338 (knot (aref knots index))
339 (end-p (= index (1- (length knots)))))
340 ;; Check if we have to segment the next interpolation
341 (if (funcall (pi-segmented-state-predicate iterator)
342 interpolation)
343 (let ((previous-index (mod (1- index) (length knots))))
344 (update-queue interpolation
345 (aref knots previous-index)
346 knot end-p)
347 (dequeue))
348 (values interpolation knot end-p))))))))))
350 ;;; Iterate distinct
352 ;;; This iterator filter out identical knots. That is, the knots with
353 ;;; the same positions, with any interpolation. (All interpolations
354 ;;; currently implemented are empty when knot around them are not
355 ;;; distinct.)
357 ;;; When cyclic-p is true, the first knot of the iterator is the first
358 ;;; knot distinct from the first knot of the reference iterator.
360 ;;; When cyclic-p is false, the first knot of the iterator if the
361 ;;; first knot of the reference iterator, and if the path ends with a
362 ;;; knot which is not distinct from the first, it is kept.
364 (defclass filter-distinct-state ()
365 ((iterator :initarg :iterator)
366 (cyclic-p :initarg :cyclic-p)
367 (fixed :initarg :fixed)
368 (next :initarg :next)
369 (next-is-end-p)))
371 (defun filter-distinct (iterator &optional (preserve-cyclic-end-p nil))
372 (make-instance 'filter-distinct-state
373 :iterator iterator
374 :cyclic-p (not preserve-cyclic-end-p)
375 :fixed nil
376 :next nil))
378 (defmethod path-iterator-reset ((iterator filter-distinct-state))
379 (with-slots ((sub iterator) next next-is-end-p) iterator
380 (path-iterator-reset sub)
381 (setf next nil
382 next-is-end-p nil)))
384 (defmethod path-iterator-next ((iterator filter-distinct-state))
385 (with-slots ((sub iterator) cyclic-p fixed next next-is-end-p) iterator
386 (when fixed
387 ;; constant result cached
388 (return-from path-iterator-next (values-list fixed)))
389 (labels ((get-next ()
390 "Get the next knot information as a list (not as
391 multiple values)."
392 (multiple-value-list (path-iterator-next sub)))
393 (distinct-p (a b)
394 "Test if A and B have distinct knots."
395 (not (zerop (point-distance (second a) (second b)))))
396 (move-to-next (previous loop-p)
397 "Move iterator to find a knot distinct from the
398 PREVIOUS. Also indicate if the resulting knot is
399 the first of the sub iterator, and if end of path
400 was encountered. This is needed to compute the
401 effective END-P flag for the resulting iterator."
402 (loop
403 with first-p = (third previous)
404 with end-encountered-p = (third previous)
405 for current = (get-next)
406 until (or (distinct-p previous current)
407 (and (not loop-p) first-p))
408 do (setf first-p (third current))
409 when (third current)
410 do (setf end-encountered-p t)
411 finally (return (values current first-p end-encountered-p)))))
412 (let (result)
413 (unless next
414 ;; First time we iterate.
415 (setf next-is-end-p nil)
416 (let ((first (get-next)))
417 (cond
418 ((or (not (second first))
419 (third first))
420 ;; It was an empty path or a single knot path. Cache it
421 ;; and returns it for each further iterations.
422 (setf fixed first
423 result first))
424 (cyclic-p
425 (multiple-value-bind (first-in-cycle first-p end-p) (move-to-next first nil)
426 (declare (ignore first-p))
427 (cond
428 (end-p
429 (setf (third first) t
430 fixed first
431 result first))
433 (setf next first-in-cycle)))))
435 (setf next first)))))
436 (unless result
437 ;; We copy NEXT because we need to modify RESULT, and since
438 ;; NEXT is kept for the next iteration, we take care of not
439 ;; modifying it.
440 (setf result (copy-seq next)
441 (third result) next-is-end-p)
442 (multiple-value-bind (current first-p end-encountered-p) (move-to-next next cyclic-p)
443 (setf next current)
444 ;; Set end marker
445 (cond
446 (cyclic-p
447 (setf next-is-end-p first-p)
448 (when (and end-encountered-p (not first-p))
449 (setf (third result) t)))
451 (setf (third result) end-encountered-p)))))
452 (values-list result)))))
454 ;;; Misc
456 (defun path-clone (path)
457 (let ((new-interpolations (copy-seq (path-interpolations path))))
458 (loop for i below (length new-interpolations)
459 do (setf (aref new-interpolations i)
460 (interpolation-clone (aref new-interpolations i))))
461 (let ((new-path (create-path (path-type path))))
462 (setf (path-knots new-path) (copy-seq (path-knots path))
463 (path-interpolations new-path) new-interpolations
464 (path-orientation new-path) (path-orientation path))
465 new-path)))
467 (defun path-reverse (path)
468 ;; reverse the order of knots
469 (setf (path-knots path) (nreverse (path-knots path)))
470 ;; reverse the order of interpolations 1..n (not the first one,
471 ;; which is the implicit straight line.)
472 (loop with interpolations = (path-interpolations path)
473 with length = (length interpolations)
474 for i from 1 upto (floor (1- length) 2)
475 do (rotatef (aref interpolations i)
476 (aref interpolations (- length i))))
477 ;; reverse each interpolation
478 (loop for interpolation across (path-interpolations path)
479 do (interpolation-reverse interpolation))
480 (unless (eq (path-orientation path) :unknown)
481 (setf (path-orientation path) (ecase (path-orientation path)
482 (:cw :ccw)
483 (:ccw :cw))))
484 path)
486 (defun path-reversed (path)
487 (let ((new-path (path-clone path)))
488 (path-reverse new-path)
489 new-path))
491 (defmacro do-path ((path interpolation knot) &body body)
492 (let ((path-sym (gensym))
493 (knots (gensym))
494 (interpolations (gensym))
495 (index (gensym)))
496 `(symbol-macrolet ((,interpolation (aref ,interpolations ,index))
497 (,knot (aref ,knots ,index)))
498 (loop
499 with ,path-sym = ,path
500 with ,knots = (path-knots ,path-sym)
501 with ,interpolations = (path-interpolations ,path-sym)
502 for ,index below (length ,knots)
503 do (progn ,@body)))))
505 (defun path-translate (path vector)
506 "Translate the whole path accordingly to VECTOR."
507 (if (listp path)
508 (dolist (path-item path)
509 (path-translate path-item vector))
510 (unless (and (zerop (point-x vector))
511 (zerop (point-y vector)))
512 (do-path (path interpolation knot)
513 (setf knot (p+ knot vector))
514 (interpolation-translate interpolation vector))))
515 path)
517 (defun path-rotate (path angle &optional center)
518 "Rotate the whole path by ANGLE radian around CENTER (which is
519 the origin if unspecified.)"
520 (if (listp path)
521 (dolist (path-item path)
522 (path-rotate path-item angle center))
523 (unless (zerop angle)
524 (when center
525 (path-translate path (p* center -1.0)))
526 (do-path (path interpolation knot)
527 (setf knot (point-rotate knot angle))
528 (interpolation-rotate interpolation angle))
529 (when center
530 (path-translate path center))))
531 path)
533 (defun path-scale (path scale-x scale-y &optional center)
534 "Scale the whole path by (SCALE-X,SCALE-Y) from CENTER (which
535 is the origin if unspecified.) Warning: not all interpolations
536 support non uniform scaling (when scale-x /= scale-y)."
537 ;;; FIXME: What to do about path-orientation?
538 (if (listp path)
539 (dolist (path-item path)
540 (path-scale path-item scale-x scale-y center))
541 (progn
542 (when center
543 (path-translate path (p* center -1.0)))
544 (do-path (path interpolation knot)
545 (setf knot (p* knot scale-x scale-y))
546 (interpolation-scale interpolation scale-x scale-y))
547 (when center
548 (path-translate path center))
549 (when (minusp (* scale-x scale-y))
550 (path-reverse path))))
551 path)
553 ;;;--[ Interpolations ]------------------------------------------------------
555 (defgeneric interpolation-segment (interpolation k1 k2 function)
556 (:documentation "Segment the path between K1 and K2 described
557 by the INTERPOLATION. Call FUNCTION for each generated point on
558 the interpolation path."))
560 (defgeneric interpolation-normal (interpolation k1 k2 side)
561 (:documentation "Compute the normal, going \"outside\" at
562 either K1 (if SIDE is false) or K2 (if SIDE is true). Return NIL
563 if the normal cannot be computed. Return a point otherwise."))
565 (defgeneric interpolation-clone (interpolation)
566 (:documentation "Duplicate INTERPOLATION."))
568 (defgeneric interpolation-reverse (interpolation)
569 (:documentation "Reverse the path described by INTERPOLATION
570 in-place."))
572 (defgeneric interpolation-reversed (interpolation)
573 (:method (interpolation)
574 (let ((cloned-interpolation (interpolation-clone interpolation)))
575 (interpolation-reversed cloned-interpolation)
576 cloned-interpolation))
577 (:documentation "Duplicate and reverse the INTERPOLATION."))
579 (defgeneric interpolation-translate (interpolation vector))
581 (defgeneric interpolation-rotate (interpolation angle))
583 (defgeneric interpolation-scale (interpolation scale-x scale-y))
585 ;;; Straight lines
587 (defun make-straight-line ()
588 :straight-line)
590 (defun straight-line-p (value)
591 (eq value :straight-line))
593 (defmethod interpolation-segment ((interpolation (eql :straight-line)) k1 k2 function)
594 (declare (ignore interpolation k1 k2 function)))
596 (defmethod interpolation-normal ((interpolation (eql :straight-line)) k1 k2 side)
597 (let* ((x1 (point-x k1))
598 (y1 (point-y k1))
599 (x2 (point-x k2))
600 (y2 (point-y k2))
601 (dx (- x2 x1))
602 (dy (- y2 y1))
603 (dist (sqrt (+ (expt dx 2) (expt dy 2)))))
604 (when (plusp dist)
605 (if side
606 (make-point (/ dx dist)
607 (/ dy dist))
608 (make-point (- (/ dx dist))
609 (- (/ dy dist)))))))
611 (defmethod interpolation-clone ((interpolation (eql :straight-line)))
612 (make-straight-line))
614 (defmethod interpolation-reverse ((interpolation (eql :straight-line)))
615 (declare (ignore interpolation)))
617 (defmethod interpolation-translate ((interpolation (eql :straight-line)) vector)
618 (declare (ignore interpolation vector)))
620 (defmethod interpolation-rotate ((interpolation (eql :straight-line)) angle)
621 (declare (ignore interpolation angle)))
623 (defmethod interpolation-scale ((interpolation (eql :straight-line)) scale-x scale-y)
624 (declare (ignore interpolation scale-x scale-y)))
626 ;;; Arc (SVG style)
628 (defclass arc ()
629 ((rx :initarg rx)
630 (ry :initarg ry)
631 (x-axis-rotation :initarg x-axis-rotation)
632 (large-arc-flag :initarg large-arc-flag) ; t = choose the longest arc, nil = choose the smallest arc
633 (sweep-flag :initarg sweep-flag))) ; t = arc on the right, nil = arc on the left
635 (defun make-arc (rx ry &key (x-axis-rotation 0.0) (large-arc-flag nil) (sweep-flag nil))
636 (make-instance 'arc
637 'rx rx
638 'ry ry
639 'x-axis-rotation x-axis-rotation
640 'large-arc-flag large-arc-flag
641 'sweep-flag sweep-flag))
643 (defun svg-arc-parameters/reverse (center rx ry rotation start-angle delta-angle)
644 "Conversion from center to endpoint parameterization of SVG arc.
646 Returns values P1, P2, LARGE-ARC-FLAG-P, SWEEP-FLAG-P."
647 (let ((p1 (point-rotate (make-point rx 0) start-angle))
648 (p2 (point-rotate (make-point rx 0) (+ start-angle delta-angle))))
649 (flet ((transform (p)
651 (point-rotate
652 (p* p 1.0 (/ rx ry))
653 rotation)
654 center)))
655 (values (transform p1) (transform p2)
656 (> (abs delta-angle) pi)
657 (plusp delta-angle)))))
659 (defun svg-arc-parameters (p1 p2 rx ry rotation large-arc-flag-p sweep-flag-p)
660 "Conversion from endpoint to center parameterization of SVG arc.
662 Returns values RC, RX, RY, START-ANGLE and DELTA-ANGLE, where RC is
663 the center of the ellipse, RX and RY are the normalized
664 radii (needed if scaling was necessary)."
665 (when (and (/= rx 0)
666 (/= ry 0))
667 ;; [SVG] "If rX or rY have negative signs, these are dropped; the
668 ;; absolute value is used instead."
669 (setf rx (abs rx)
670 ry (abs ry))
671 ;; normalize boolean value to nil/t
672 (setf large-arc-flag-p (when large-arc-flag-p t)
673 sweep-flag-p (when sweep-flag-p t))
674 ;; rp1 and rp2 are p1 and p2 into the coordinate system such
675 ;; that rotation is cancelled and ellipse ratio is 1 (a circle.)
676 (let* ((rp1 (p* (point-rotate p1 (- rotation)) 1.0 (/ rx ry)))
677 (rp2 (p* (point-rotate p2 (- rotation)) 1.0 (/ rx ry)))
678 (rm (point-middle rp1 rp2))
679 (drp1 (p- rm rp1))
680 (dist (point-norm drp1)))
681 (when (plusp dist)
682 (let ((diff-sq (- (expt rx 2) (expt dist 2)))
684 (cond
685 ((not (plusp diff-sq))
686 ;; a/ scale the arc if it is too small to touch the points
687 (setf ry (* dist (/ ry rx))
688 rx dist
689 rc rm))
691 ;; b/ otherwise compute the center of the circle
692 (let ((d (/ (sqrt diff-sq) dist)))
693 (unless (eq large-arc-flag-p sweep-flag-p)
694 (setf d (- d)))
695 (setf rc (make-point (+ (point-x rm) (* (point-y drp1) d))
696 (- (point-y rm) (* (point-x drp1) d)))))))
697 (let* ((start-angle (point-angle (p- rp1 rc)))
698 (end-angle (point-angle (p- rp2 rc)))
699 (delta-angle (- end-angle start-angle)))
700 (when (minusp delta-angle)
701 (incf delta-angle (* 2 pi)))
702 (unless sweep-flag-p
703 (decf delta-angle (* 2 pi)))
704 (values (point-rotate (p* rc 1.0 (/ ry rx)) rotation) rx ry start-angle delta-angle)))))))
706 (defmethod interpolation-segment ((interpolation arc) k1 k2 function)
707 (let ((rotation (slot-value interpolation 'x-axis-rotation)))
708 (multiple-value-bind (rc rx ry start-angle delta-angle)
709 (svg-arc-parameters k1 k2
710 (slot-value interpolation 'rx)
711 (slot-value interpolation 'ry)
712 rotation
713 (slot-value interpolation 'large-arc-flag)
714 (slot-value interpolation 'sweep-flag))
715 (when rc
716 (loop with n = (max 3 (* (max rx ry) (abs delta-angle)))
717 for i from 1 below n
718 for angle = (+ start-angle (/ (* delta-angle i) n))
719 for p = (p+ (point-rotate
721 (make-point (* rx (cos angle))
722 (* rx (sin angle)))
723 1.0 (/ ry rx))
724 rotation)
726 do (funcall function p))))))
728 (defmethod interpolation-normal ((interpolation arc) k1 k2 side)
729 (let ((rotation (slot-value interpolation 'x-axis-rotation)))
730 (multiple-value-bind (rc rx ry start-angle delta-angle)
731 (svg-arc-parameters k1 k2
732 (slot-value interpolation 'rx)
733 (slot-value interpolation 'ry)
734 rotation
735 (slot-value interpolation 'large-arc-flag)
736 (slot-value interpolation 'sweep-flag))
737 (flet ((adjust (normal)
738 (let* ((p (point-rotate (p* normal 1.0 (/ ry rx)) rotation))
739 (d (point-norm p)))
740 (when (plusp delta-angle)
741 (setf d (- d)))
742 (make-point (/ (point-x p) d) (/ (point-y p) d)))))
743 (when rc
744 (let ((end-angle (+ start-angle delta-angle)))
745 (adjust (if side
746 (make-point (sin end-angle)
747 (- (cos end-angle)))
748 (make-point (- (sin start-angle))
749 (cos start-angle))))))))))
751 (defmethod interpolation-clone ((interpolation arc))
752 (make-arc (slot-value interpolation 'rx)
753 (slot-value interpolation 'ry)
754 :x-axis-rotation (slot-value interpolation 'x-axis-rotation)
755 :large-arc-flag (slot-value interpolation 'large-arc-flag)
756 :sweep-flag (slot-value interpolation 'sweep-flag)))
758 (defmethod interpolation-reverse ((interpolation arc))
759 (setf (slot-value interpolation 'sweep-flag)
760 (not (slot-value interpolation 'sweep-flag))))
762 (defmethod interpolation-translate ((interpolation arc) vector)
763 (declare (ignore interpolation vector)))
765 (defmethod interpolation-rotate ((interpolation arc) angle)
766 (incf (slot-value interpolation 'x-axis-rotation) angle))
768 (defmethod interpolation-scale ((interpolation arc) scale-x scale-y)
769 ;; FIXME: Return :segment-me if scaling is not possible?
770 (assert (and (not (zerop scale-x))
771 (= scale-x scale-y)))
772 (with-slots (rx ry) interpolation
773 (setf rx (* rx scale-x)
774 ry (* ry scale-y))))
776 ;;; Catmull-Rom
778 (defclass catmull-rom ()
779 ((head
780 :initarg head)
781 (control-points
782 :initform (make-array 0)
783 :initarg control-points)
784 (queue
785 :initarg queue)))
787 (defun make-catmull-rom (head control-points queue)
788 (make-instance 'catmull-rom
789 'head head
790 'control-points (coerce control-points 'vector)
791 'queue queue))
793 (defmethod interpolation-segment ((interpolation catmull-rom) k1 k2 function)
794 (let* ((control-points (slot-value interpolation 'control-points))
795 (points (make-array (+ (length control-points) 4))))
796 (replace points control-points :start1 2)
797 (setf (aref points 0) (slot-value interpolation 'head)
798 (aref points 1) k1
799 (aref points (- (length points) 2)) k2
800 (aref points (- (length points) 1)) (slot-value interpolation 'queue))
801 (labels ((eval-catmull-rom (a b c d p)
802 ;; http://www.mvps.org/directx/articles/catmull/
803 (* 0.5
804 (+ (* 2 b)
805 (* (+ (- a) c) p)
806 (* (+ (* 2 a) (* -5 b) (* 4 c) (- d)) (expt p 2))
807 (* (+ (- a) (* 3 b) (* -3 c) d) (expt p 3))))))
808 (loop for s below (- (length points) 3)
809 for a = (aref points (+ s 0)) then b
810 for b = (aref points (+ s 1)) then c
811 for c = (aref points (+ s 2)) then d
812 for d = (aref points (+ s 3))
813 do (funcall function b)
814 (loop with n = 32
815 for i from 1 below n
816 for p = (/ (coerce i 'float) n)
817 for x = (eval-catmull-rom (point-x a)
818 (point-x b)
819 (point-x c)
820 (point-x d)
822 for y = (eval-catmull-rom (point-y a)
823 (point-y b)
824 (point-y c)
825 (point-y d)
827 do (funcall function (make-point x y)))
828 (funcall function c)))))
830 (defmethod interpolation-normal ((interpolation catmull-rom) k1 k2 side)
831 (with-slots (head control-points queue) interpolation
832 (let (a b)
833 (if (zerop (length control-points))
834 (if side
835 (setf a k1
836 b queue)
837 (setf a k2
838 b head))
839 (if side
840 (setf a (aref control-points (1- (length control-points)))
841 b queue)
842 (setf a (aref control-points 0)
843 b head)))
844 (let* ((x1 (point-x a))
845 (y1 (point-y a))
846 (x2 (point-x b))
847 (y2 (point-y b))
848 (dx (- x2 x1))
849 (dy (- y2 y1))
850 (dist (sqrt (+ (expt dx 2) (expt dy 2)))))
851 (when (plusp dist)
852 (make-point (/ dx dist)
853 (/ dy dist)))))))
855 (defmethod interpolation-clone ((interpolation catmull-rom))
856 (make-catmull-rom (slot-value interpolation 'head)
857 (copy-seq (slot-value interpolation 'control-points))
858 (slot-value interpolation 'queue)))
860 (defmethod interpolation-reverse ((interpolation catmull-rom))
861 (rotatef (slot-value interpolation 'head)
862 (slot-value interpolation 'queue))
863 (nreverse (slot-value interpolation 'control-points)))
865 (defmethod interpolation-translate ((interpolation catmull-rom) vector)
866 (with-slots (head control-points queue) interpolation
867 (setf head (p+ head vector)
868 queue (p+ queue vector))
869 (loop for i below (length control-points)
870 do (setf (aref control-points i) (p+ (aref control-points i) vector)))))
872 (defmethod interpolation-rotate ((interpolation catmull-rom) angle)
873 (with-slots (head control-points queue) interpolation
874 (setf head (point-rotate head angle)
875 queue (point-rotate queue angle))
876 (loop for i below (length control-points)
877 do (setf (aref control-points i) (point-rotate (aref control-points i) angle)))))
879 (defmethod interpolation-scale ((interpolation catmull-rom) scale-x scale-y)
880 (with-slots (head control-points queue) interpolation
881 (setf head (p* head scale-x scale-y)
882 queue (p* queue scale-x scale-y))
883 (loop for i below (length control-points)
884 do (setf (aref control-points i) (p* (aref control-points i)
885 scale-x scale-y)))))
887 ;;; Bezier curves
889 ;;; [http://www.fho-emden.de/~hoffmann/bezier18122002.pdf]
891 (defclass bezier ()
892 ((control-points
893 :initform (make-array 0)
894 :initarg control-points)))
896 (defun make-bezier-curve (control-points)
897 (make-instance 'bezier
898 'control-points (make-array (length control-points)
899 :initial-contents control-points)))
901 (defun split-bezier (points &optional (position 0.5))
902 "Split the Bezier curve described by POINTS at POSITION into
903 two Bezier curves of the same degree. Returns the curves as 2
904 values."
905 (let* ((size (length points))
906 (stack (make-array size))
907 (current points))
908 (setf (aref stack 0) points)
909 (loop for j from 1 below size
910 for next-size from (1- size) downto 1
911 do (let ((next (make-array next-size)))
912 (loop for i below next-size
913 for a = (aref current i)
914 for b = (aref current (1+ i))
915 do (setf (aref next i)
916 (make-point (+ (* (- 1.0 position) (point-x a))
917 (* position (point-x b)))
918 (+ (* (- 1.0 position) (point-y a))
919 (* position (point-y b))))))
920 (setf (aref stack j) next
921 current next)))
922 (let ((left (make-array (length points)))
923 (right (make-array (length points))))
924 (loop for i from 0 below size
925 for j from (1- size) downto 0
926 do (setf (aref left i) (aref (aref stack i) 0)
927 (aref right i) (aref (aref stack j) i)))
928 (values left right))))
930 (defun evaluate-bezier (points position)
931 "Evaluate the point at POSITION on the Bezier curve described
932 by POINTS."
933 (let* ((size (length points))
934 (temp (make-array (1- size))))
935 (loop for current = points then temp
936 for i from (length temp) downto 1
937 do (loop for j below i
938 for a = (aref current j)
939 for b = (aref current (1+ j))
940 do (setf (aref temp j)
941 (make-point (+ (* (- 1.0 position) (point-x a))
942 (* position (point-x b)))
943 (+ (* (- 1.0 position) (point-y a))
944 (* position (point-y b)))))))
945 (let ((p (aref temp 0)))
946 (values (point-x p) (point-y p)))))
948 (defun discrete-bezier-curve (points function
949 &key
950 (include-ends t)
951 (min-subdivide nil)
952 (max-subdivide 10)
953 (distance-tolerance *bezier-distance-tolerance*)
954 (angle-tolerance *bezier-angle-tolerance*))
955 "Subdivize Bezier curve up to certain criterions."
956 ;; FIXME: Handle cusps correctly!
957 (unless min-subdivide
958 (setf min-subdivide (floor (log (1+ (length points)) 2))))
959 (labels ((norm (a b)
960 (sqrt (+ (expt a 2) (expt b 2))))
961 (refine-bezier (points depth)
962 (let* ((a (aref points 0))
963 (b (aref points (1- (length points))))
964 (middle-straight (point-middle a b)))
965 (multiple-value-bind (bx by) (evaluate-bezier points 0.5)
966 (when (or (< depth min-subdivide)
967 (and (<= depth max-subdivide)
968 (or (> (norm (- bx (point-x middle-straight))
969 (- by (point-y middle-straight)))
970 distance-tolerance)
971 (> (abs (- (atan (- by (point-y a)) (- bx (point-x a)))
972 (atan (- (point-y b) by) (- (point-x b) bx))))
973 angle-tolerance))))
974 (multiple-value-bind (a b) (split-bezier points 0.5)
975 (refine-bezier a (1+ depth))
976 (funcall function bx by)
977 (refine-bezier b (1+ depth))))))))
978 (when include-ends
979 (let ((p (aref points 0)))
980 (funcall function (point-x p) (point-y p))))
981 (refine-bezier points 0)
982 (when include-ends
983 (let ((p (aref points (1- (length points)))))
984 (funcall function (point-x p) (point-y p)))))
985 (values))
987 (defmethod interpolation-segment ((interpolation bezier) k1 k2 function)
988 (with-slots (control-points) interpolation
989 (let ((points (make-array (+ 2 (length control-points)))))
990 (replace points control-points :start1 1)
991 (setf (aref points 0) k1
992 (aref points (1- (length points))) k2)
993 (discrete-bezier-curve points
994 (lambda (x y) (funcall function (make-point x y)))
995 :include-ends nil))))
997 (defmethod interpolation-normal ((interpolation bezier) k1 k2 side)
998 (let ((control-points (slot-value interpolation 'control-points))
999 a b)
1000 (if (zerop (length control-points))
1001 (if side
1002 (setf a k1
1003 b k2)
1004 (setf a k2
1005 b k1))
1006 (if side
1007 (setf a (aref control-points (1- (length control-points)))
1008 b k2)
1009 (setf a (aref control-points 0)
1010 b k1)))
1011 (let* ((x1 (point-x a))
1012 (y1 (point-y a))
1013 (x2 (point-x b))
1014 (y2 (point-y b))
1015 (dx (- x2 x1))
1016 (dy (- y2 y1))
1017 (dist (sqrt (+ (expt dx 2) (expt dy 2)))))
1018 (when (plusp dist)
1019 (make-point (/ dx dist)
1020 (/ dy dist))))))
1022 (defmethod interpolation-clone ((interpolation bezier))
1023 (let ((control-points (copy-seq (slot-value interpolation 'control-points))))
1024 (loop for i below (length control-points)
1025 do (setf (aref control-points i) (aref control-points i)))
1026 (make-bezier-curve control-points)))
1028 (defmethod interpolation-reverse ((interpolation bezier))
1029 (nreverse (slot-value interpolation 'control-points)))
1031 (defmethod interpolation-translate ((interpolation bezier) vector)
1032 (with-slots (control-points) interpolation
1033 (loop for i below (length control-points)
1034 do (setf (aref control-points i) (p+ (aref control-points i) vector)))))
1036 (defmethod interpolation-rotate ((interpolation bezier) angle)
1037 (with-slots (control-points) interpolation
1038 (loop for i below (length control-points)
1039 do (setf (aref control-points i) (point-rotate (aref control-points i) angle)))))
1041 (defmethod interpolation-scale ((interpolation bezier) scale-x scale-y)
1042 (with-slots (control-points) interpolation
1043 (loop for i below (length control-points)
1044 do (setf (aref control-points i) (p* (aref control-points i)
1045 scale-x scale-y)))))
1047 ;;;--[ Building paths ]------------------------------------------------------
1049 (defun make-discrete-path (path)
1050 "Construct a path with only straight lines."
1051 (let ((result (create-path (path-type path)))
1052 (knots (path-knots path))
1053 (interpolations (path-interpolations path)))
1054 (when (plusp (length knots))
1055 ;; nicer, but slower too.. (But not profiled. Premature optimization?)
1056 #+nil(loop with iterator = (path-iterator-segmented path)
1057 for (interpolation knot end-p) = (multiple-value-list (path-iterator-next iterator))
1058 do (path-extend result interpolation knot)
1059 until end-p)
1060 (path-reset result (aref knots 0))
1061 (loop
1062 for i below (1- (length knots))
1063 for k1 = (aref knots i)
1064 for k2 = (aref knots (1+ i))
1065 for interpolation = (aref interpolations (1+ i))
1066 do (interpolation-segment interpolation k1 k2
1067 (lambda (knot)
1068 (path-extend result
1069 (make-straight-line)
1070 knot)))
1071 do (path-extend result (make-straight-line) k2)
1072 finally (unless (eq (path-type path) :open-polyline)
1073 (interpolation-segment (aref interpolations 0) k2 (aref knots 0)
1074 (lambda (knot)
1075 (path-extend result
1076 (make-straight-line)
1077 knot))))))
1078 result))
1080 (defun make-circle-path (cx cy radius &optional (radius-y radius) (x-axis-rotation 0.0))
1081 "Construct a path to represent a circle centered at CX,CY of
1082 the specified RADIUS."
1083 ;; Note: We represent the circle with 2 arcs
1084 (let ((path (create-path :polygon)))
1085 (setf radius (abs radius)
1086 radius-y (abs radius-y))
1087 (when (= radius radius-y)
1088 (setf x-axis-rotation 0.0))
1089 (when (and (plusp radius) (plusp radius-y))
1090 (let* ((center (make-point cx cy))
1091 (p (point-rotate (make-point radius 0) x-axis-rotation))
1092 (left (p+ center p))
1093 (right (p- center p)))
1094 (path-extend path (make-arc radius radius-y :x-axis-rotation x-axis-rotation) left)
1095 (path-extend path (make-arc radius radius-y :x-axis-rotation x-axis-rotation) right)))
1096 path))
1098 (defun make-rectangle-path (x1 y1 x2 y2
1099 &key (round nil) (round-x nil) (round-y nil))
1100 ;; FIXME: Instead: center + width + height + rotation ?
1101 ;; FIXME: Round corners? (rx, ry)
1102 (when (> x1 x2)
1103 (rotatef x1 x2))
1104 (when (> y1 y2)
1105 (rotatef y1 y2))
1106 (let ((path (create-path :closed-polyline))
1107 (round-x (or round-x round))
1108 (round-y (or round-y round)))
1109 (cond
1110 ((and round-x (plusp round-x)
1111 round-y (plusp round-y))
1112 (path-reset path (make-point (+ x1 round-x) y1))
1113 (path-extend path (make-arc round-x round-y) (make-point x1 (+ y1 round-y)))
1114 (path-extend path (make-straight-line) (make-point x1 (- y2 round-y)))
1115 (path-extend path (make-arc round-x round-y) (make-point (+ x1 round-x) y2))
1116 (path-extend path (make-straight-line) (make-point (- x2 round-x) y2))
1117 (path-extend path (make-arc round-x round-y) (make-point x2 (- y2 round-y)))
1118 (path-extend path (make-straight-line) (make-point x2 (+ y1 round-y)))
1119 (path-extend path (make-arc round-x round-y) (make-point (- x2 round-x) y1)))
1121 (path-reset path (make-point x1 y1))
1122 (path-extend path (make-straight-line) (make-point x1 y2))
1123 (path-extend path (make-straight-line) (make-point x2 y2))
1124 (path-extend path (make-straight-line) (make-point x2 y1))))
1125 path))
1127 (defun make-rectangle-path/center (x y dx dy &rest args)
1128 (apply #'make-rectangle-path (- x dx) (- y dy) (+ x dx) (+ y dy) args))
1130 (defun make-regular-polygon-path (x y radius sides &optional (start-angle 0.0))
1131 (let ((path (create-path :closed-polyline)))
1132 (loop for i below sides
1133 for angle = (+ start-angle (/ (* i 2 pi) sides))
1134 do (path-extend path (make-straight-line)
1135 (make-point (+ x (* (cos angle) radius))
1136 (- y (* (sin angle) radius)))))
1137 path))
1139 (defun make-simple-path (points &optional (type :open-polyline))
1140 "Create a path with only straight line, by specifying only knots."
1141 (let ((path (create-path type)))
1142 (dolist (point points)
1143 (path-extend path (make-straight-line) point))
1144 path))
1146 ;;;--[ Transformations ]-----------------------------------------------------
1148 (defmacro define-for-multiple-paths (name-multiple name-single &optional documentation)
1149 "Define a new function named by NAME-MULTIPLE which accepts
1150 either a single path or a list of paths as input from a function
1151 named by NAME-SINGLE accepting only a single path and producing a
1152 list of paths."
1153 `(defun ,name-multiple (paths &rest args)
1154 ,@(when documentation (list documentation))
1155 (loop for path in (if (listp paths) paths (list paths))
1156 nconc (apply #',name-single path args))))
1158 ;;; Stroke
1160 (defun stroke-path/1 (path thickness
1161 &key (caps :butt) (joint :none) (inner-joint :none)
1162 assume-type)
1163 "Stroke the path."
1164 (setf thickness (abs thickness))
1165 (let ((half-thickness (/ thickness 2.0))
1166 target)
1167 ;; TARGET is the path updated by the function LINE-TO and
1168 ;; EXTEND-TO below.
1169 (labels ((filter-interpolation (interpolation)
1170 ;; We handle only straight-line and arc of circle. The
1171 ;; rest will be segmented.
1172 (not (or (straight-line-p interpolation)
1173 (and (typep interpolation 'arc)
1174 (= (slot-value interpolation 'rx)
1175 (slot-value interpolation 'ry))))))
1176 (det (a b c d)
1177 (- (* a d) (* b c)))
1178 (arc (model)
1179 "Make a new arc similar to MODEL but with a radius
1180 updated to match the stroke."
1181 (assert (= (slot-value model 'rx)
1182 (slot-value model 'ry)))
1183 (let ((shift (if (slot-value model 'sweep-flag)
1184 (- half-thickness)
1185 half-thickness)))
1186 (make-arc (+ (slot-value model 'rx) shift)
1187 (+ (slot-value model 'ry) shift)
1188 :sweep-flag (slot-value model 'sweep-flag)
1189 :large-arc-flag (slot-value model 'large-arc-flag))))
1190 (line-to (p)
1191 "Extend the path to knot P with a straight line."
1192 (path-extend target (make-straight-line) p))
1193 (extend-to (i p)
1194 "EXtend the path to knot P with the given interpolation."
1195 (path-extend target i p))
1196 (do-single (k1)
1197 "Produce the resulting path when the input path
1198 contains a single knot."
1199 (ecase caps
1200 (:butt
1201 nil)
1202 (:square
1203 (path-replace target
1204 (make-rectangle-path/center (point-x k1)
1205 (point-y k1)
1206 half-thickness
1207 half-thickness)))
1208 (:round
1209 (path-replace target
1210 (make-circle-path (point-x k1)
1211 (point-y k1)
1212 half-thickness)))))
1213 (do-first (k1 i2 k2)
1214 "Process the first interpolation."
1215 (let* ((normal (interpolation-normal i2 k1 k2 nil))
1216 (n (p* normal half-thickness))
1217 (d (point-rotate n (/ pi 2))))
1218 (ecase caps
1219 (:butt
1220 (line-to (p- k1 d)))
1221 (:square
1222 (line-to (p+ (p+ k1 d) n))
1223 (line-to (p+ (p- k1 d) n))
1224 (unless (straight-line-p i2)
1225 (line-to (p- k1 d))))
1226 (:round
1227 (extend-to (make-arc half-thickness half-thickness) (p- k1 d))))))
1228 (do-last (k1 i2 k2)
1229 "Process the last interpolation."
1230 (let* ((normal (interpolation-normal i2 k1 k2 t))
1231 (d (p* (point-rotate normal (/ pi 2)) half-thickness)))
1232 (cond
1233 ((typep i2 'arc)
1234 (extend-to (arc i2) (p+ k2 d)))
1235 ((straight-line-p i2)
1236 (unless (eq caps :square)
1237 (line-to (p+ k2 d))))
1239 (error "unexpected interpolation")))))
1240 (do-segment (k1 i2 k2 i3 k3)
1241 "Process intermediate interpolation."
1242 (let* ((normal-a (interpolation-normal i2 k1 k2 t))
1243 (normal-b (interpolation-normal i3 k2 k3 nil))
1244 (outer-p (plusp (det (point-x normal-a) (point-y normal-a)
1245 (point-x normal-b) (point-y normal-b))))
1246 (d-a (p* (point-rotate normal-a (/ pi 2)) half-thickness))
1247 (d-b (p* (point-rotate normal-b (/ pi -2)) half-thickness)))
1248 (cond
1249 ((and (not outer-p)
1250 (eq inner-joint :miter)
1251 (straight-line-p i2)
1252 (straight-line-p i3))
1253 ;; Miter inner joint between 2 straight lines
1254 (multiple-value-bind (xi yi)
1255 (line-intersection/delta
1256 (point-x (p+ k2 d-a)) (point-y (p+ k2 d-a))
1257 (point-x normal-a) (point-y normal-a)
1258 (point-x (p+ k2 d-b)) (point-y (p+ k2 d-b))
1259 (point-x normal-b) (point-y normal-b))
1260 (cond
1261 ((and xi
1262 (plusp (+ (* (- xi (point-x k1))
1263 (point-x normal-a))
1264 (* (- yi (point-y k1))
1265 (point-y normal-a))))
1266 (plusp (+ (* (- xi (point-x k3))
1267 (point-x normal-b))
1268 (* (- yi (point-y k3))
1269 (point-y normal-b)))))
1270 ;; ok, intersection point
1271 ;; is behind segments
1272 ;; ends
1273 (extend-to (make-straight-line) (make-point xi yi)))
1275 ;; revert to basic joint
1276 (line-to (p+ k2 d-a))
1277 (line-to (p+ k2 d-b))))))
1278 ((and outer-p
1279 (eq joint :miter)
1280 (straight-line-p i2)
1281 (straight-line-p i3))
1282 ;; Miter outer joint between 2 straight lines
1283 (multiple-value-bind (xi yi)
1284 (line-intersection/delta
1285 (point-x (p+ k2 d-a)) (point-y (p+ k2 d-a))
1286 (point-x normal-a) (point-y normal-a)
1287 (point-x (p+ k2 d-b)) (point-y (p+ k2 d-b))
1288 (point-x normal-b) (point-y normal-b))
1289 (let ((i (make-point xi yi)))
1290 (cond
1291 ((and xi
1292 (<= (point-distance i k2)
1293 (* half-thickness *miter-limit*)))
1294 (line-to (make-point xi yi)))
1296 ;; FIXME: Ugh. My math skill show its
1297 ;; limits. This is probably possible to
1298 ;; compute the same thing with less steps.
1299 (let* ((p (p+ k2 (point-middle d-a d-b)))
1300 (a (point-distance (p+ k2 d-a) i))
1301 (b (- (* half-thickness *miter-limit*)
1302 (point-distance k2 p)))
1303 (c (point-distance p i))
1304 (d (/ (* a b) c))
1305 (p1 (p+ (p+ k2 d-a) (p* normal-a d)))
1306 (p2 (p+ (p+ k2 d-b) (p* normal-b d))))
1307 (line-to p1)
1308 (line-to p2)))))))
1310 (extend-to (if (typep i2 'arc)
1311 (arc i2)
1312 (make-straight-line))
1313 (p+ k2 d-a))
1314 ;; joint
1315 (if outer-p
1316 (ecase joint
1317 ((:none :miter)
1318 (line-to (p+ k2 d-b)))
1319 (:round
1320 (extend-to (make-arc half-thickness half-thickness
1321 :sweep-flag nil)
1322 (p+ k2 d-b))))
1323 (ecase inner-joint
1324 ((:none :miter)
1325 (line-to (p+ k2 d-b)))
1326 (:round
1327 (extend-to (make-arc half-thickness half-thickness
1328 :sweep-flag t)
1329 (p+ k2 d-b)))))))))
1330 (do-contour-half (path new-target first-half-p)
1331 (setf target new-target)
1332 (let ((iterator (filter-distinct (path-iterator-segmented path #'filter-interpolation)
1333 t)))
1334 (flet ((next ()
1335 (path-iterator-next iterator)))
1336 (multiple-value-bind (i1 k1 e1) (next)
1337 (when k1
1338 (cond
1340 (when first-half-p
1341 (do-single k1)))
1343 ;; at least 2 knots
1344 (multiple-value-bind (i2 k2 e2) (next)
1345 (do-first k1 i2 k2)
1346 ;; rest of the path
1347 (unless e2
1348 (loop
1349 (multiple-value-bind (i3 k3 e3) (next)
1350 (do-segment k1 i2 k2 i3 k3)
1351 (shiftf i1 i2 i3)
1352 (shiftf k1 k2 k3)
1353 (when e3
1354 (return)))))
1355 (do-last k1 i2 k2)))))))))
1356 (do-contour-polygon (path new-target first-p)
1357 (setf target new-target)
1358 (let ((iterator (filter-distinct (path-iterator-segmented path #'filter-interpolation))))
1359 (flet ((next ()
1360 (path-iterator-next iterator)))
1361 (multiple-value-bind (i1 k1 e1) (next)
1362 (when k1
1363 (cond
1365 (when first-p
1366 (do-single k1)))
1368 ;; at least 2 knots
1369 (multiple-value-bind (i2 k2 e2) (next)
1370 ;; rest of the path
1371 (let (extra-iteration)
1372 (when e2
1373 (setf extra-iteration 2))
1374 (loop
1375 (multiple-value-bind (i3 k3 e3) (next)
1376 (when (and extra-iteration (zerop extra-iteration))
1377 (return))
1378 (do-segment k1 i2 k2 i3 k3)
1379 (shiftf i1 i2 i3)
1380 (shiftf k1 k2 k3)
1381 (cond
1382 (extra-iteration
1383 (decf extra-iteration))
1385 (setf extra-iteration 2)))))))))))))))
1386 (when (plusp half-thickness)
1387 (ecase (or assume-type (path-type path))
1388 (:open-polyline
1389 (let ((result (create-path :polygon)))
1390 (do-contour-half path result t)
1391 (do-contour-half (path-reversed path) result nil)
1392 (list result)))
1393 (:closed-polyline
1394 (let ((result-a (create-path :polygon))
1395 (result-b (create-path :polygon)))
1396 ;; FIXME: What happen for single knot path?
1397 (do-contour-polygon path result-a t)
1398 (do-contour-polygon (path-reversed path) result-b nil)
1399 (list result-a result-b)))
1400 (:polygon
1401 (let ((result (create-path :polygon)))
1402 (do-contour-polygon path result t)
1403 (list result))))))))
1405 (define-for-multiple-paths stroke-path stroke-path/1)
1407 ;;; Dash
1409 (defun dash-path/1 (path sizes &key (toggle-p nil) (cycle-index 0))
1410 "Dash path. If TOGGLE-P is true, segments of odd indices are
1411 kept, while if TOGGLE-P is false, segments of even indices are
1412 kept. CYCLE indicate where to cycle the SIZES once the end is
1413 reached."
1414 (assert (<= 0 cycle-index (1- (length sizes)))
1415 (cycle-index) "Invalid cycle index")
1416 (assert (loop for size across sizes never (minusp size))
1417 (sizes) "All sizes must be non-negative.")
1418 (assert (loop for size across sizes thereis (plusp size))
1419 (sizes) "At least one size must be positive.")
1420 (flet ((interpolation-filter (interpolation)
1421 (or (not (typep interpolation 'arc))
1422 (/= (slot-value interpolation 'rx)
1423 (slot-value interpolation 'ry)))))
1424 (let (result
1425 (current (create-path :open-polyline))
1426 (current-length 0.0)
1427 (toggle (not toggle-p))
1428 (index 0)
1429 (size (aref sizes 0))
1430 (iterator (path-iterator-segmented path #'interpolation-filter)))
1431 (flet ((flush ()
1432 (when toggle
1433 (push current result))
1434 (setf toggle (not toggle))
1435 (setf current (create-path :open-polyline)
1436 current-length 0.0)
1437 (incf index)
1438 (when (= index (length sizes))
1439 (setf index cycle-index))
1440 (setf size (aref sizes index)))
1441 (extend (interpolation knot length)
1442 (path-extend current interpolation knot)
1443 (incf current-length length)))
1444 (loop
1445 for previous-knot = nil then knot
1446 for stop-p = nil then end-p
1447 for (interpolation knot end-p) = (multiple-value-list (path-iterator-next iterator))
1448 if (not previous-knot)
1449 do (path-reset current knot)
1450 else
1451 do (etypecase interpolation
1452 ((eql :straight-line)
1453 (let* ((delta (p- knot previous-knot))
1454 (length (point-norm delta))
1455 (pos 0.0))
1456 (loop
1457 (let ((missing (- size current-length))
1458 (available (- length pos)))
1459 (when (> missing available)
1460 (extend (make-straight-line) knot available)
1461 (return))
1462 (incf pos missing)
1463 (let ((end (p+ previous-knot (p* delta (/ pos length)))))
1464 (extend (make-straight-line) end missing)
1465 (flush)
1466 (path-reset current end))))))
1467 (arc
1468 (with-slots (rx ry x-axis-rotation large-arc-flag sweep-flag) interpolation
1469 (assert (= rx ry))
1470 (multiple-value-bind (rc nrx nry start-angle delta-angle)
1471 (svg-arc-parameters previous-knot knot rx ry
1472 x-axis-rotation
1473 large-arc-flag
1474 sweep-flag)
1475 (let* ((length (* (abs delta-angle) nrx))
1476 (pos 0.0))
1477 (loop
1478 (let ((missing (- size current-length))
1479 (available (- length pos)))
1480 (when (> missing available)
1481 (extend (make-arc nrx nry
1482 :x-axis-rotation x-axis-rotation
1483 :large-arc-flag (>= (/ available nrx) pi)
1484 :sweep-flag sweep-flag)
1485 knot
1486 available)
1487 (return))
1488 (incf pos missing)
1489 (let ((end (p+
1490 (point-rotate (make-point nrx 0)
1491 (+ x-axis-rotation
1492 (if (plusp delta-angle)
1493 (+ start-angle (/ pos nrx))
1494 (- start-angle (/ pos nrx)))))
1495 rc)))
1496 (extend (make-arc nrx nry
1497 :x-axis-rotation x-axis-rotation
1498 :large-arc-flag (>= (/ missing nrx) pi)
1499 :sweep-flag sweep-flag)
1501 missing)
1502 (flush)
1503 (path-reset current end)))))))))
1504 until (if (eq (path-type path) :open-polyline) end-p stop-p))
1505 (flush))
1506 (nreverse result))))
1508 (define-for-multiple-paths dash-path dash-path/1)
1510 ;;; Clip path
1512 (defun clip-path/1 (path x y dx dy)
1513 (let (result
1514 (current (create-path (path-type path)))
1515 (iterator (path-iterator-segmented path)))
1516 (labels ((next ()
1517 (path-iterator-next iterator))
1518 (det (a b c d)
1519 (- (* a d) (* b c)))
1520 (inside-p (p)
1521 (plusp (det (- (point-x p) x)
1522 (- (point-y p) y)
1523 dx dy)))
1524 (clip-left (k1 k2)
1525 (let ((k1-inside-p (when (inside-p k1) t))
1526 (k2-inside-p (when (inside-p k2) t)))
1527 (when k1-inside-p
1528 (path-extend current (make-straight-line) k1))
1529 (when (not (eq k1-inside-p k2-inside-p))
1530 (multiple-value-bind (xi yi)
1531 (line-intersection/delta x y dx dy
1532 (point-x k1) (point-y k1)
1533 (- (point-x k2) (point-x k1))
1534 (- (point-y k2) (point-y k1)))
1535 (when xi
1536 (path-extend current (make-straight-line) (make-point xi yi))))))))
1537 (multiple-value-bind (i1 k1 e1) (next)
1538 (let ((first-knot k1))
1539 (when k1
1540 (cond
1542 (when (inside-p k1)
1543 (path-reset current k1)))
1545 (loop
1546 (multiple-value-bind (i2 k2 e2) (next)
1547 (clip-left k1 k2)
1548 (when e2
1549 (if (eq (path-type path) :open-polyline)
1550 (when (inside-p k2)
1551 (path-extend current (make-straight-line) k2))
1552 (clip-left k2 first-knot))
1553 (return))
1554 (setf i1 i2)
1555 (setf k1 k2)))))))))
1556 (push current result)
1557 result))
1559 (define-for-multiple-paths clip-path clip-path/1)
1561 (defun clip-path/path/1 (path limit)
1562 (let ((iterator (filter-distinct (path-iterator-segmented limit)))
1563 (result (list path)))
1564 (multiple-value-bind (i1 k1 e1) (path-iterator-next iterator)
1565 (declare (ignore i1))
1566 (when (and k1 (not e1))
1567 (let ((stop-p nil))
1568 (loop
1569 (multiple-value-bind (i2 k2 e2) (path-iterator-next iterator)
1570 (declare (ignore i2))
1571 (setq result (loop for path in result
1572 nconc (clip-path path
1573 (point-x k1) (point-y k1)
1574 (point-x (p- k2 k1)) (point-y (p- k2 k1)))))
1575 (when stop-p
1576 (return result))
1577 (when e2
1578 (setf stop-p t))
1579 (setf k1 k2))))))))
1581 (define-for-multiple-paths clip-path/path clip-path/path/1)
1584 ;;; Round path
1586 (defun round-path/1 (path &optional max-radius)
1587 (declare (ignore max-radius))
1588 (list path))
1590 (define-for-multiple-paths round-path round-path/1)