Compare commits
14 Commits
a3f0ee2eb9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| b753c9ee9c | |||
| 3eb52aa430 | |||
| f4b52415dc | |||
| e38cda0261 | |||
| cd2a616ccb | |||
| d0967f0580 | |||
| bcdb95211b | |||
| 641048b56d | |||
| bd704b2c66 | |||
| 151ecb1b1c | |||
| 332b4dc6b3 | |||
| 495e4ac5a5 | |||
| 2da23e143c | |||
| 31f4707fbf |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
*.png
|
*.png
|
||||||
*.gif
|
*.gif
|
||||||
|
*.webp
|
||||||
cgif/libguilecgif.so
|
cgif/libguilecgif.so
|
||||||
cgif/config.scm
|
cgif/config.scm
|
||||||
|
|||||||
30
demo.scm
30
demo.scm
@@ -1,10 +1,26 @@
|
|||||||
(use-modules (graphgif)
|
(use-modules (graphgif)
|
||||||
(srfi srfi-9 gnu)
|
(d-))
|
||||||
(ice-9 copy-tree))
|
|
||||||
|
|
||||||
(define red (create-color 1 0 0))
|
(define graph
|
||||||
|
(~> (generate-web 10 10)
|
||||||
|
(remove-rect 10 1 3 6 3)
|
||||||
|
(remove-rect 10 6 3 6 7)))
|
||||||
|
|
||||||
(define graph1 (generate-web 10 10))
|
(define (color-graph graph visited heap)
|
||||||
(define graph2 (copy-tree graph1))
|
(map (lambda (pair)
|
||||||
(assq-set! graph2 0 (set-node-color (assq-ref graph2 0) red))
|
(cons (car pair)
|
||||||
(write-graphs-to-file (list graph1 graph2) "graph.gif")
|
(if (memq (car pair) visited)
|
||||||
|
(set-node-color (cdr pair) red)
|
||||||
|
(cdr pair))))
|
||||||
|
graph))
|
||||||
|
(define (make-graph-generator f)
|
||||||
|
(generator
|
||||||
|
(f graph 90 9
|
||||||
|
(lambda (visited heap)
|
||||||
|
(yield (color-graph graph visited heap))))
|
||||||
|
#f))
|
||||||
|
|
||||||
|
(define djikstra-generator (make-graph-generator djikstra))
|
||||||
|
(define a*-generator (make-graph-generator a*))
|
||||||
|
(write-graphs-to-file "djikstra.webp" djikstra-generator)
|
||||||
|
(write-graphs-to-file "astar.webp" a*-generator)
|
||||||
|
|||||||
231
graphgif.scm
231
graphgif.scm
@@ -1,20 +1,21 @@
|
|||||||
(define-module (graphgif))
|
(define-module (graphgif))
|
||||||
|
|
||||||
(use-modules (cairo)
|
(use-modules (cairo)
|
||||||
|
(d-)
|
||||||
(srfi srfi-1)
|
(srfi srfi-1)
|
||||||
(srfi srfi-9 gnu))
|
(srfi srfi-9 gnu)
|
||||||
|
(srfi srfi-11)
|
||||||
(re-export (cairo-pattern-create-rgb . create-color))
|
(srfi srfi-43))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;
|
||||||
;; Basic Drawing ;;
|
;; Basic Drawing ;;
|
||||||
;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(define-public pi 3.14159)
|
(define-public pi 3.14159) ;; Good enough
|
||||||
(define-public tau (* 2 pi))
|
(define-public tau (* 2 pi))
|
||||||
|
|
||||||
(define-public black (cairo-pattern-create-rgb 0 0 0))
|
(define-public black (cairo-pattern-create-rgb 0 0 0))
|
||||||
(define-public white (cairo-pattern-create-rgb 1 1 1))
|
(define-public white (cairo-pattern-create-rgb 1 1 1))
|
||||||
|
(define-public red (cairo-pattern-create-rgb 1 0 0))
|
||||||
|
|
||||||
(define-immutable-record-type <node>
|
(define-immutable-record-type <node>
|
||||||
(node coords edges color)
|
(node coords edges color)
|
||||||
@@ -28,26 +29,25 @@
|
|||||||
node-color set-node-color)
|
node-color set-node-color)
|
||||||
|
|
||||||
(define (edge-painter cr graph)
|
(define (edge-painter cr graph)
|
||||||
|
"Creates a closure that draws the edges of graph using cr"
|
||||||
(lambda (node)
|
(lambda (node)
|
||||||
(cairo-set-source cr black)
|
(cairo-set-source cr black)
|
||||||
(let ([x (car (node-coords node))]
|
(let-values ([(x y) (car+cdr (node-coords node))]
|
||||||
[y (cdr (node-coords node))]
|
[(edges) (node-edges node)])
|
||||||
[edges (node-edges node)])
|
|
||||||
(for-each
|
(for-each
|
||||||
(lambda (edge)
|
(lambda (edge)
|
||||||
(let* ([other (assq-ref graph edge)]
|
(let*-values ([(other) (assq-ref graph edge)]
|
||||||
[ox (car (node-coords other))]
|
[(ox oy) (car+cdr (node-coords other))])
|
||||||
[oy (cdr (node-coords other))])
|
|
||||||
(cairo-move-to cr x y)
|
(cairo-move-to cr x y)
|
||||||
(cairo-line-to cr ox oy)
|
(cairo-line-to cr ox oy)
|
||||||
(cairo-stroke cr)))
|
(cairo-stroke cr)))
|
||||||
edges))))
|
edges))))
|
||||||
|
|
||||||
(define (node-painter cr)
|
(define (node-painter cr)
|
||||||
|
"Creates a closure that draws the nodes of graph using cr"
|
||||||
(lambda (node)
|
(lambda (node)
|
||||||
(let ([x (car (node-coords node))]
|
(let-values ([(x y) (car+cdr (node-coords node))]
|
||||||
[y (cdr (node-coords node))]
|
[(color) (or (node-color node) white)])
|
||||||
[color (or (node-color node) white)])
|
|
||||||
(cairo-arc cr x y 4. 0. tau)
|
(cairo-arc cr x y 4. 0. tau)
|
||||||
(cairo-set-source cr color)
|
(cairo-set-source cr color)
|
||||||
(cairo-fill-preserve cr)
|
(cairo-fill-preserve cr)
|
||||||
@@ -55,6 +55,7 @@
|
|||||||
(cairo-stroke cr))))
|
(cairo-stroke cr))))
|
||||||
|
|
||||||
(define-public (draw-abstract-graph graph)
|
(define-public (draw-abstract-graph graph)
|
||||||
|
"Creates a cairo surface with graph drawn on it"
|
||||||
(let* ([surface (cairo-image-surface-create 'rgb24 400 400)]
|
(let* ([surface (cairo-image-surface-create 'rgb24 400 400)]
|
||||||
[cr (cairo-create surface)])
|
[cr (cairo-create surface)])
|
||||||
(define paint-edges (edge-painter cr graph))
|
(define paint-edges (edge-painter cr graph))
|
||||||
@@ -73,15 +74,19 @@
|
|||||||
;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(define (idx->x i w)
|
(define (idx->x i w)
|
||||||
|
"Given i and width w, returns the x value"
|
||||||
(modulo i w))
|
(modulo i w))
|
||||||
|
|
||||||
(define (idx->y i w)
|
(define (idx->y i w)
|
||||||
|
"Given i and width w, returns the y value"
|
||||||
(quotient i w))
|
(quotient i w))
|
||||||
|
|
||||||
(define (xy->idx x y w)
|
(define (xy->idx x y w)
|
||||||
|
"Returns the index of x y on width x in a linear buffer"
|
||||||
(+ (* y w) x))
|
(+ (* y w) x))
|
||||||
|
|
||||||
(define (idx->edges i w)
|
(define (idx->edges i w)
|
||||||
|
"Returns the edges needed to fully connect i on graph width w"
|
||||||
(filter-map
|
(filter-map
|
||||||
(lambda (offset)
|
(lambda (offset)
|
||||||
(let* ([x (idx->x i w)]
|
(let* ([x (idx->x i w)]
|
||||||
@@ -94,12 +99,53 @@
|
|||||||
(< ox w)
|
(< ox w)
|
||||||
(xy->idx ox oy w))))
|
(xy->idx ox oy w))))
|
||||||
;; Auto-connect these directions if legal indices
|
;; Auto-connect these directions if legal indices
|
||||||
'(( 0 . -1)
|
;; This combined with forwards and back connections create a
|
||||||
(-1 . 0)
|
;; fully connected graph
|
||||||
(-1 . -1)
|
'(( 0 . -1) ;; Above
|
||||||
(+1 . -1))))
|
(-1 . 0) ;; Right
|
||||||
|
(-1 . -1) ;; Right-above
|
||||||
|
(+1 . -1) ;; Left above
|
||||||
|
)))
|
||||||
|
|
||||||
|
(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)
|
||||||
|
"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)))))
|
||||||
|
|
||||||
(define-public (generate-web w h)
|
(define-public (generate-web w h)
|
||||||
|
"Creates a fully connected graph of width w and height h"
|
||||||
(define (make-node i)
|
(define (make-node i)
|
||||||
(cons i
|
(cons i
|
||||||
(node
|
(node
|
||||||
@@ -110,26 +156,145 @@
|
|||||||
(let loop ([i 0]
|
(let loop ([i 0]
|
||||||
[lst '()])
|
[lst '()])
|
||||||
(if (>= i (* w h))
|
(if (>= i (* w h))
|
||||||
(reverse lst)
|
(connect-bidirectionally (reverse lst))
|
||||||
(loop (1+ i) (cons (make-node i) lst)))))
|
(loop (1+ i) (cons (make-node i) lst)))))
|
||||||
|
|
||||||
|
(define-public (remove-rect graph w x1 y1 x2 y2)
|
||||||
|
"Removes a rectangular section of a fully connected rectangular graph"
|
||||||
|
;; 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)))
|
||||||
|
(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)))))))
|
||||||
|
graph))
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;
|
||||||
|
;; ALGORITHMS!!! ;;
|
||||||
|
;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
(define-public (heap-insert priority item heap)
|
||||||
|
(define (fix-up! i heap)
|
||||||
|
(if (zero? i)
|
||||||
|
heap
|
||||||
|
(let* ([parent (quotient (1- i) 2)]
|
||||||
|
[i-priority (car (vector-ref heap i))]
|
||||||
|
[parent-priority (car (vector-ref heap parent))])
|
||||||
|
(when (< i-priority parent-priority)
|
||||||
|
(vector-swap! heap i parent))
|
||||||
|
(fix-up! parent heap))))
|
||||||
|
(fix-up! (vector-length heap)
|
||||||
|
(vector-append heap (vector (cons priority item)))))
|
||||||
|
|
||||||
|
(define-public (heap-peek heap)
|
||||||
|
(if (vector-empty? heap) #f (vector-ref heap 0)))
|
||||||
|
|
||||||
|
(define-public (heap-pop heap)
|
||||||
|
(define (fix-down! heap i)
|
||||||
|
(let* ([len (vector-length heap)]
|
||||||
|
[left (1+ (* 2 i))]
|
||||||
|
[right (1+ left)])
|
||||||
|
(if (< left len)
|
||||||
|
(let ([min (if (< right len)
|
||||||
|
(argmin (compose car (partial vector-ref heap)) < i left right)
|
||||||
|
(argmin (compose car (partial vector-ref heap)) < i left))])
|
||||||
|
(if (= min i)
|
||||||
|
heap
|
||||||
|
(begin
|
||||||
|
(vector-swap! heap i min)
|
||||||
|
(fix-down! heap min))))
|
||||||
|
heap)))
|
||||||
|
(if (or (vector-empty? heap) (= (vector-length heap) 1))
|
||||||
|
#()
|
||||||
|
(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))))
|
||||||
|
|
||||||
|
(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)
|
||||||
|
(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)
|
||||||
|
|
||||||
|
(define* (a* graph source sink #:optional update)
|
||||||
|
(define (chebychev idx) ;; Uses the Chebychev distance as a heuristic
|
||||||
|
(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)))))
|
||||||
|
(define unvisited (make-inf-queue graph))
|
||||||
|
(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)
|
||||||
|
p (heap-insert (+ dist (chebychev edge) 1) edge heap))
|
||||||
|
(heap-pop heap)
|
||||||
|
(node-edges (assv-ref graph idx)))
|
||||||
|
(cons idx visited))])))
|
||||||
|
(export a*)
|
||||||
|
|
||||||
;;;;;;;;;;;;
|
;;;;;;;;;;;;
|
||||||
;; Output ;;
|
;; Output ;;
|
||||||
;;;;;;;;;;;;
|
;;;;;;;;;;;;
|
||||||
|
|
||||||
(define (output-to-file surfaces filename)
|
(define (output-to-file filename surface-gen)
|
||||||
|
"Takes a filename and a coroutine to generate surfaces, and creates an animation"
|
||||||
(define pngdir (mkdtemp "/tmp/graphgif_XXXXXX"))
|
(define pngdir (mkdtemp "/tmp/graphgif_XXXXXX"))
|
||||||
(do ([i 1 (1+ i)]
|
(let loop ([surface (surface-gen)]
|
||||||
[surfaces surfaces (cdr surfaces)])
|
[i 1])
|
||||||
((null? surfaces))
|
(when surface
|
||||||
(cairo-surface-write-to-png
|
(cairo-surface-write-to-png
|
||||||
(car surfaces)
|
surface
|
||||||
(string-append pngdir "/img-" (number->string i) ".png")))
|
(string-append pngdir "/img-" (number->string i) ".png"))
|
||||||
(system* "ffmpeg" "-i" (string-append pngdir "/img-%d.png") "-r" "1" filename))
|
(loop (surface-gen) (1+ i))))
|
||||||
|
(system* "ffmpeg" "-y"
|
||||||
|
"-i" (string-append pngdir "/img-%d.png")
|
||||||
|
"-loop" "0" filename))
|
||||||
|
|
||||||
(define-public (write-graphs-to-file graphs filename)
|
(define-public (write-graphs-to-file filename graph-gen)
|
||||||
(output-to-file (map draw-abstract-graph graphs) filename))
|
"Takes a filename and a couroutine to generate graphs, and creates an animation"
|
||||||
|
(define (surface-gen)
|
||||||
;; Local Variables:
|
(let ([graph (graph-gen)])
|
||||||
;; geiser-scheme-implementation: guile
|
(and graph (draw-abstract-graph graph))))
|
||||||
;; End:
|
(output-to-file filename surface-gen))
|
||||||
|
|||||||
20
test.scm
Normal file
20
test.scm
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
(use-modules (rnrs base)
|
||||||
|
(d-)
|
||||||
|
(graphgif))
|
||||||
|
|
||||||
|
(begin
|
||||||
|
(display "Testing heap operations...")
|
||||||
|
(newline)
|
||||||
|
(let ([heap (~>> #()
|
||||||
|
(heap-insert (inf) 1)
|
||||||
|
(heap-insert (inf) 2)
|
||||||
|
(heap-insert (inf) 3)
|
||||||
|
(heap-insert 0 1))])
|
||||||
|
(assert (equal? (heap-peek heap) '(0 . 1)))
|
||||||
|
(set! heap (heap-insert 1 2 (heap-pop heap)))
|
||||||
|
(assert (equal? (heap-peek heap) '(1 . 2)))
|
||||||
|
(set! heap (heap-insert 3 3 heap))
|
||||||
|
(assert (equal? (heap-peek heap) '(1 . 2)))
|
||||||
|
(assert (equal? (heap-peek (heap-pop heap)) '(3 . 3))))
|
||||||
|
(assert (equal? (heap-pop #((0 . 0))) #()))
|
||||||
|
(assert (equal? (heap-peek (heap-pop (heap-pop #((1 . 1) (1 . 10) (+inf.0 . 2) (+inf.0 . 3) (1 . 11))))) '(1 . 11))))
|
||||||
Reference in New Issue
Block a user