graphgif/graphgif.scm

301 lines
8.5 KiB
Scheme
Raw Permalink Normal View History

(define-module (graphgif))
2024-10-16 15:46:16 -05:00
(use-modules (cairo)
2024-10-25 15:40:16 -05:00
(d-)
(srfi srfi-1)
2024-10-22 09:25:54 -05:00
(srfi srfi-9 gnu)
2024-11-11 21:07:19 -06:00
(srfi srfi-11)
2024-10-22 09:25:54 -05:00
(srfi srfi-43))
2024-10-16 13:48:22 -05:00
;;;;;;;;;;;;;;;;;;;
;; Basic Drawing ;;
;;;;;;;;;;;;;;;;;;;
2024-11-19 12:01:07 -06:00
(define-public pi 3.14159) ;; Good enough
(define-public tau (* 2 pi))
(define-public black (cairo-pattern-create-rgb 0 0 0))
(define-public white (cairo-pattern-create-rgb 1 1 1))
2024-11-19 12:01:07 -06:00
(define-public red (cairo-pattern-create-rgb 1 0 0))
(define-immutable-record-type <node>
(node coords edges color)
node?
(coords node-coords set-node-coords)
(edges node-edges set-node-edges)
(color node-color set-node-color))
(export node node?
node-coords set-node-coords
node-edges set-node-edges
node-color set-node-color)
(define (edge-painter cr graph)
2024-11-19 11:39:13 -06:00
"Creates a closure that draws the edges of graph using cr"
(lambda (node)
(cairo-set-source cr black)
2024-11-19 11:39:13 -06:00
(let-values ([(x y) (car+cdr (node-coords node))]
[(edges) (node-edges node)])
(for-each
(lambda (edge)
(let*-values ([(other) (assq-ref graph edge)]
[(ox oy) (car+cdr (node-coords other))])
(cairo-move-to cr x y)
(cairo-line-to cr ox oy)
(cairo-stroke cr)))
edges))))
(define (node-painter cr)
2024-11-19 11:39:13 -06:00
"Creates a closure that draws the nodes of graph using cr"
(lambda (node)
2024-11-19 11:39:13 -06:00
(let-values ([(x y) (car+cdr (node-coords node))]
[(color) (or (node-color node) white)])
(cairo-arc cr x y 4. 0. tau)
(cairo-set-source cr color)
(cairo-fill-preserve cr)
(cairo-set-source cr black)
(cairo-stroke cr))))
(define-public (draw-abstract-graph graph)
2024-11-19 11:39:13 -06:00
"Creates a cairo surface with graph drawn on it"
2024-10-16 15:46:16 -05:00
(let* ([surface (cairo-image-surface-create 'rgb24 400 400)]
[cr (cairo-create surface)])
(define paint-edges (edge-painter cr graph))
(define paint-nodes (node-painter cr))
;; White background
(cairo-rectangle cr 0 0 400 400)
(cairo-set-source cr white)
(cairo-fill cr)
(for-each paint-edges (map cdr graph))
(for-each paint-nodes (map cdr graph))
(cairo-destroy cr)
surface))
2024-10-18 10:23:46 -05:00
;;;;;;;;;;;;;;;;;;;;;;
;; Graph Generation ;;
;;;;;;;;;;;;;;;;;;;;;;
(define (idx->x i w)
2024-11-19 11:39:13 -06:00
"Given i and width w, returns the x value"
2024-10-18 10:23:46 -05:00
(modulo i w))
(define (idx->y i w)
2024-11-19 11:39:13 -06:00
"Given i and width w, returns the y value"
2024-10-18 10:23:46 -05:00
(quotient i w))
(define (xy->idx x y w)
2024-11-19 11:39:13 -06:00
"Returns the index of x y on width x in a linear buffer"
2024-10-18 10:23:46 -05:00
(+ (* y w) x))
(define (idx->edges i w)
2024-11-19 11:39:13 -06:00
"Returns the edges needed to fully connect i on graph width w"
2024-10-18 10:23:46 -05:00
(filter-map
(lambda (offset)
(let* ([x (idx->x i w)]
[y (idx->y i w)]
[ox (+ x (car offset))]
[oy (+ y (cdr offset))])
(and
(not (negative? ox))
(not (negative? oy))
(< ox w)
(xy->idx ox oy w))))
;; Auto-connect these directions if legal indices
2024-11-19 11:39:13 -06:00
;; This combined with forwards and back connections create a
;; fully connected graph
'(( 0 . -1) ;; Above
(-1 . 0) ;; Right
(-1 . -1) ;; Right-above
(+1 . -1) ;; Left above
)))
2024-10-18 10:23:46 -05:00
(define lset-unionq (partial lset-union eq?))
(define (forward-connect node graph idx)
(cons idx
(set-node-edges
node
(lset-unionq
(node-edges node)
(fold (lambda (entry set)
(if (memq idx (node-edges (cdr entry)))
(cons (car entry) set)
set))
'()
graph)))))
(define (back-connect edges graph idx)
(map
(lambda (entry)
(cons (car entry)
(if (memq (car entry) edges)
(set-node-edges
(cdr entry)
(lset-unionq (node-edges (cdr entry)) `(,idx)))
(cdr entry))))
graph))
(define-public (connect-bidirectionally graph)
2024-11-19 11:39:13 -06:00
"Takes a directed graph and connects all edges to make it fully bidirectional"
(if (null? graph) '()
(let* ([idx (caar graph)]
[node (cdar graph)])
(cons (forward-connect node (cdr graph) idx)
(back-connect
(node-edges node)
(connect-bidirectionally (cdr graph))
idx)))))
2024-10-18 10:23:46 -05:00
(define-public (generate-web w h)
2024-11-19 11:39:13 -06:00
"Creates a fully connected graph of width w and height h"
2024-10-18 10:23:46 -05:00
(define (make-node i)
(cons i
(node
(cons (+ (* 30 (idx->x i w)) 10)
(+ (* 30 (idx->y i w)) 10))
(idx->edges i w)
#f)))
2024-10-18 10:23:46 -05:00
(let loop ([i 0]
[lst '()])
(if (>= i (* w h))
(connect-bidirectionally (reverse lst))
2024-10-18 10:23:46 -05:00
(loop (1+ i) (cons (make-node i) lst)))))
2024-10-22 08:51:33 -05:00
(define-public (remove-rect graph w x1 y1 x2 y2)
2024-11-19 11:39:13 -06:00
"Removes a rectangular section of a fully connected rectangular graph"
2024-10-22 08:51:33 -05:00
;; Assumption is that is a graph generated by generate-web
;; and that it has not been re-indexed
;; x1 < x2, y1 < y2
(define edges-to-remove
(unfold (lambda (s) (> s (xy->idx x2 y2 w)))
identity
(lambda (s)
(if (>= (idx->x s w) x2)
(xy->idx x1 (1+ (idx->y s w)) w)
(1+ s)))
2024-10-22 08:51:33 -05:00
(xy->idx x1 y1 w)))
(filter-map
(lambda (idx/node)
(if (memq (car idx/node) edges-to-remove)
#f
(cons (car idx/node)
(set-node-edges
(cdr idx/node)
(remove
(lambda (edge) (memq edge edges-to-remove))
(node-edges (cdr idx/node)))))))
2024-10-22 08:51:33 -05:00
graph))
;;;;;;;;;;;;;;;;;;;
;; ALGORITHMS!!! ;;
;;;;;;;;;;;;;;;;;;;
(define-public (heap-insert priority item heap)
2024-10-25 15:40:16 -05:00
(define (fix-up! i heap)
(if (zero? i)
heap
(let* ([parent (quotient (1- i) 2)]
2024-10-25 15:40:16 -05:00
[i-priority (car (vector-ref heap i))]
[parent-priority (car (vector-ref heap parent))])
2024-11-11 21:07:19 -06:00
(when (< i-priority parent-priority)
2024-10-25 15:40:16 -05:00
(vector-swap! heap i parent))
(fix-up! parent heap))))
2024-11-19 12:01:07 -06:00
(fix-up! (vector-length heap)
(vector-append heap (vector (cons priority item)))))
2024-10-25 15:40:16 -05:00
(define-public (heap-peek heap)
2024-10-25 15:40:16 -05:00
(if (vector-empty? heap) #f (vector-ref heap 0)))
(define-public (heap-pop heap)
2024-10-25 15:40:16 -05:00
(define (fix-down! heap i)
(let* ([len (vector-length heap)]
[left (1+ (* 2 i))]
2024-10-25 15:40:16 -05:00
[right (1+ left)])
(if (< left len)
(let ([min (if (< right len)
2024-11-11 21:07:19 -06:00
(argmin (compose car (partial vector-ref heap)) < i left right)
(argmin (compose car (partial vector-ref heap)) < i left))])
2024-10-25 15:40:16 -05:00
(if (= min i)
heap
(begin
(vector-swap! heap i min)
(fix-down! heap min))))
2024-10-25 15:40:16 -05:00
heap)))
(if (or (vector-empty? heap) (= (vector-length heap) 1))
#()
2024-10-25 15:40:16 -05:00
(let ([new-heap (vector-copy heap 0 (1- (vector-length heap)))])
(vector-set! new-heap 0 (vector-ref heap (1- (vector-length heap))))
(fix-down! new-heap 0))))
2024-11-19 11:39:13 -06:00
(define (make-inf-queue graph)
"Creates a heap of the graph where all priorities are +inf"
(list->vector
(map (lambda (entry)
(cons (inf) (car entry)))
graph)))
(define* (djikstra graph source sink #:optional update)
2024-11-19 11:39:13 -06:00
(define unvisited (make-inf-queue graph))
(let iter ([heap (heap-insert 0 source unvisited)]
[visited '()])
(define-values (dist idx) (car+cdr (heap-peek heap)))
(cond
[(eqv? sink idx) dist]
[(memv idx visited) (iter (heap-pop heap) visited)]
[(inf? dist) #f]
[else
(when update (update visited heap))
(iter (fold
(lambda (edge heap)
(heap-insert (+ dist 1) edge heap))
(heap-pop heap)
(node-edges (assv-ref graph idx)))
(cons idx visited))])))
(export djikstra)
2024-11-11 21:07:19 -06:00
(define* (a* graph source sink #:optional update)
2024-11-19 11:39:13 -06:00
(define (chebychev idx) ;; Uses the Chebychev distance as a heuristic
2024-11-11 21:07:19 -06:00
(let-values ([(x1 y1) (car+cdr (node-coords (assv-ref graph idx)))]
[(x2 y2) (car+cdr (node-coords (assv-ref graph sink)))])
(max (abs (- x1 x2)) (abs (- y1 y2)))))
2024-11-19 11:39:13 -06:00
(define unvisited (make-inf-queue graph))
2024-11-11 21:07:19 -06:00
(let iter ([heap (heap-insert (chebychev source) source unvisited)]
[visited '()])
(define-values (dist idx) (car+cdr (heap-peek heap)))
(cond
[(eqv? sink idx) dist]
[(memv idx visited) (iter (heap-pop heap) visited)]
[(inf? dist) #f]
[else
(when update (update visited heap))
(iter (fold
(lambda (edge heap)
2024-11-19 12:01:07 -06:00
p (heap-insert (+ dist (chebychev edge) 1) edge heap))
2024-11-11 21:07:19 -06:00
(heap-pop heap)
(node-edges (assv-ref graph idx)))
(cons idx visited))])))
(export a*)
2024-10-21 10:51:41 -05:00
;;;;;;;;;;;;
;; Output ;;
;;;;;;;;;;;;
2024-10-29 12:01:56 -05:00
(define (output-to-file filename surface-gen)
2024-11-19 11:39:13 -06:00
"Takes a filename and a coroutine to generate surfaces, and creates an animation"
(define pngdir (mkdtemp "/tmp/graphgif_XXXXXX"))
2024-10-29 12:01:56 -05:00
(let loop ([surface (surface-gen)]
[i 1])
(when surface
(cairo-surface-write-to-png
surface
(string-append pngdir "/img-" (number->string i) ".png"))
(loop (surface-gen) (1+ i))))
2024-10-23 09:46:15 -05:00
(system* "ffmpeg" "-y"
"-i" (string-append pngdir "/img-%d.png")
"-loop" "0" filename))
2024-10-21 10:51:41 -05:00
2024-10-29 12:01:56 -05:00
(define-public (write-graphs-to-file filename graph-gen)
2024-11-19 11:39:13 -06:00
"Takes a filename and a couroutine to generate graphs, and creates an animation"
2024-10-29 12:01:56 -05:00
(define (surface-gen)
(let ([graph (graph-gen)])
(and graph (draw-abstract-graph graph))))
(output-to-file filename surface-gen))