]> git.jsancho.org Git - gacela.git/blob - gacela_misc.lisp
bd7027a182b4fcbf773ad84f203e26e02e91faca
[gacela.git] / gacela_misc.lisp
1 ;;; Gacela, a GNU Common Lisp extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
8 ;;;
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ;;; GNU General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 (in-package :gacela)
18
19 (defconstant INFINITY MOST-POSITIVE-LONG-FLOAT)
20
21 (defun append-if (new test tree &key (key #'first) (test-if #'equal))
22   (cond ((atom tree) tree)
23         (t (append-if-1
24             new
25             test
26             (mapcar (lambda (x) (append-if new test x :key key :test-if test-if)) tree)
27             :key key
28             :test-if test-if))))
29
30 (defun append-if-1 (new test tree &key (key #'first) (test-if #'equal))
31   (cond ((funcall test-if (funcall key tree) test) (append tree new))
32         (t tree)))
33
34 (defun car+ (var)
35   (if (listp var) (car var) var))
36
37 (defun avg (&rest numbers)
38   (let ((total 0))
39     (dolist (n numbers) (incf total n))
40     (/ total (length numbers))))
41
42 (defun neg (num)
43   (* -1 num))
44
45 (defun signum+ (num)
46   (let ((sig (signum num)))
47     (cond ((= sig 0) 1)
48           (t sig))))
49
50 (defmacro destructure (destructuring-list &body body)
51   (let ((lambda-list nil) (exp-list nil))
52     (dolist (pair destructuring-list)
53       (setq exp-list (cons (car pair) exp-list))
54       (setq lambda-list (cons (cadr pair) lambda-list)))
55     `(destructuring-bind ,lambda-list ,(cons 'list exp-list) ,@body)))
56
57 (defun match-pattern (list pattern)
58   (cond ((and (null list) (null pattern)) t)
59         ((and (consp list) (consp pattern))
60          (and (match-pattern (car list) (car pattern)) (match-pattern (cdr list) (cdr pattern))))
61         ((and (atom list) (atom pattern))
62          (cond ((or (numberp list) (numberp pattern)) (and (numberp list) (numberp pattern)))
63                (t t)))))
64
65 (defun nearest-power-of-two (n)
66   (labels ((power (p n)
67                   (cond ((> (* p 2) n) p)
68                         (t (power (* p 2) n)))))
69           (power 1 n)))
70
71 (defmacro secure-block (output-stream &rest forms)
72   (let ((error-handler #'si::universal-error-handler))
73     `(block secure
74        (defun si::universal-error-handler (error-name correctable function-name continue-format-string error-format-string &rest args)
75          ,(when output-stream
76             `(write-line
77               (cond ((eq error-name :WRONG-TYPE-ARGUMENT) (string error-name))
78                     (t error-format-string))
79               ,output-stream))
80          (setf (symbol-function 'si::universal-error-handler) ,error-handler)
81          (return-from secure))
82        (let (result-eval)
83          (setq result-eval (progn ,@forms))
84          (setf (symbol-function 'si::universal-error-handler) ,error-handler)
85          result-eval))))
86
87 (defmacro persistent-let (name vars &rest forms)
88   (labels ((get-vars (vars)
89                      (cond ((null vars) nil)
90                            (t (cons (if (consp (car vars)) (caar vars) (car vars))
91                                     (get-vars (cdr vars)))))))
92    
93           `(let ,(cond ((functionp name)
94                         (let ((old-vars (funcall name)))
95                           (cond ((equal (get-vars vars) (get-vars old-vars)) old-vars)
96                                 (t vars))))
97                        (t vars))
98              (defun ,name ()
99                ,(let ((lvars (get-vars vars)))
100                   `(mapcar (lambda (x y) (list x y)) ',lvars ,(cons 'list lvars))))
101              ,@forms)))
102
103 ;Geometry
104 (defun dotp (dot)
105   (match-pattern dot '(0 0)))
106
107 (defun vectorp (vector)
108   (match-pattern vector '(0 0)))
109
110 (defun circlep (circle)
111   (match-pattern circle '((0 0) 0)))
112
113 (defun polygonp (polygon)
114   (cond ((consp polygon)
115          (and (dotp (car polygon))
116               (if (null (cdr polygon)) t (polygonp (cdr polygon)))))))
117
118 (defun make-dot (x y)
119   `(,x ,y))
120
121 (defun make-vector (x y)
122   `(,x ,y))
123
124 (defun make-line (dot1 dot2)
125   `(,dot1 ,dot2))
126
127 (defun make-rectangle (x1 y1 x2 y2)
128   `((,x1 ,y1) (,x2 ,y1) (,x2 ,y2) (,x1 ,y2)))
129
130 (defun polygon-center (polygon)
131   (apply #'mapcar #'avg polygon))
132
133 (defun dots-distance (dot1 dot2)
134   (destructure ((dot1 (x1 y1))
135                 (dot2 (x2 y2)))
136                (sqrt (+ (expt (- x2 x1) 2)
137                         (expt (- y2 y1) 2)))))
138
139 (defun dot-line-distance (dot line)
140   (destructure ((line ((ax ay) (bx by)))
141                 (dot (cx cy)))
142                (let* ((r-numerator (+ (* (- cx ax) (- bx ax)) (* (- cy ay) (- by ay))))
143                       (r-denomenator (+ (expt (- bx ax) 2) (expt (- by ay) 2)))
144                       (r (/ r-numerator r-denomenator)))
145                  (values
146                   (* (abs (/ (- (* (- ay cy) (- bx ax)) (* (- ax cx) (- by ay)))
147                              r-denomenator))
148                      (sqrt r-denomenator))
149                   r))))
150
151 (defun dot-segment-distance (dot segment)
152   (multiple-value-bind
153    (dist r) (dot-line-distance dot segment)
154         (cond ((and (>= r 0) (<= r 1)) dist)
155               (t (let ((dist1 (dots-distance dot (car segment)))
156                        (dist2 (dots-distance dot (cadr segment))))
157                    (if (< dist1 dist2) dist1 dist2))))))
158
159 (defun perpendicular-line (dot line)
160   (destructure ((line ((ax ay) (bx by))))
161                (multiple-value-bind
162                 (dist r) (dot-line-distance dot line)
163                 (make-line dot
164                            (make-dot (+ ax (* r (- bx ax)))
165                                      (+ ay (* r (- by ay))))))))
166
167 (defun line-angle (line)
168   (destructure ((line ((ax ay) (bx by))))
169                (let ((x (- bx ax)) (y (- by ay)))
170                  (if (and (= x 0) (= y 0)) 0 (atan y x)))))
171
172 (defun inverse-angle (angle)
173   (cond ((< angle pi) (+ angle pi))
174         (t (- angle pi))))
175
176 (defun translate-dot (dot dx dy)
177   (destructure ((dot (x y)))
178                (list (+ x dx) (+ y dy))))
179
180 (defun translate-circle (circle dx dy)
181   (destructure ((circle (center radius)))
182                (list (translate-dot center dx dy) radius)))
183
184 (defun translate-polygon (pol dx dy)
185   (mapcar (lambda (dot)
186             (translate-dot dot dx dy))
187           pol))
188
189 (defun polygon-edges (pol)
190   (mapcar (lambda (v1 v2) (list v1 v2))
191           pol
192           (union (cdr pol) (list (car pol)))))
193
194 (defun polygon-dot-intersection (polygon dot)
195 ;Eric Haines algorithm
196   (let ((edges (polygon-edges
197                 (translate-polygon polygon (neg (car dot)) (neg (cadr dot)))))
198         (counter 0))
199     (dolist (edge edges)
200       (destructure ((edge ((x1 y1) (x2 y2))))
201                    (cond ((/= (signum+ y1) (signum+ y2))
202                           (cond ((and (> x1 0) (> x2 0)) (incf counter))
203                                 ((and (or (> x1 0) (> x2 0))
204                                       (> (- x1 (* y1 (/ (- x2 x1) (- y2 y1)))) 0))
205                                  (incf counter)))))))
206     (not (evenp counter))))
207
208 (defun circle-segment-intersection (circle segment)
209   (destructure ((circle (center radius)))
210                (<= (dot-segment-distance center segment) radius)))
211
212 (defun circle-edges-intersection (circle polygon)
213   (let ((edges (polygon-edges polygon))
214         (edges-i nil))
215     (dolist (edge edges)
216       (cond ((circle-segment-intersection circle edge) (setq edges-i (cons edge edges-i)))))
217     edges-i))
218
219 (defun circle-polygon-intersection (circle polygon)
220   (or (polygon-dot-intersection polygon (car circle))
221       (circle-edges-intersection circle polygon)))
222
223 (defun circle-circle-intersection (circle1 circle2)
224   (destructure ((circle1 (center1 radius1))
225                 (circle2 (center2 radius2)))
226                (<= (dots-distance center1 center2) (+ r1 r2))))