]> git.jsancho.org Git - gacela.git/blob - gacela_misc.lisp
(no commit message)
[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 (defconstant INFINITY MOST-POSITIVE-LONG-FLOAT)
18
19 (defun append-if (new test tree &key (key #'first) (test-if #'equal))
20   (cond ((atom tree) tree)
21         (t (append-if-1
22             new
23             test
24             (mapcar (lambda (x) (append-if new test x :key key :test-if test-if)) tree)
25             :key key
26             :test-if test-if))))
27
28 (defun append-if-1 (new test tree &key (key #'first) (test-if #'equal))
29   (cond ((funcall test-if (funcall key tree) test) (append tree new))
30         (t tree)))
31
32 (defun car+ (var)
33   (if (listp var) (car var) var))
34
35 (defun avg (&rest numbers)
36   (let ((total 0))
37     (dolist (n numbers) (incf total n))
38     (/ total (length numbers))))
39
40 (defun neg (num)
41   (* -1 num))
42
43 (defun signum+ (num)
44   (let ((sig (signum num)))
45     (cond ((= sig 0) 1)
46           (t sig))))
47
48 (defmacro destructure (destructuring-list &body body)
49   (let ((lambda-list nil) (exp-list nil))
50     (dolist (pair destructuring-list)
51       (setq exp-list (cons (car pair) exp-list))
52       (setq lambda-list (cons (cadr pair) lambda-list)))
53     `(destructuring-bind ,lambda-list ,(cons 'list exp-list) ,@body)))
54
55 (defun match-pattern (list pattern)
56   (cond ((and (null list) (null pattern)) t)
57         ((and (consp list) (consp pattern))
58          (and (match-pattern (car list) (car pattern)) (match-pattern (cdr list) (cdr pattern))))
59         ((and (atom list) (atom pattern))
60          (cond ((or (numberp list) (numberp pattern)) (and (numberp list) (numberp pattern)))
61                (t t)))))
62
63 (defun nearest-power-of-two (n)
64   (labels ((power (p n)
65                   (cond ((> (* p 2) n) p)
66                         (t (power (* p 2) n)))))
67           (power 1 n)))
68
69 (defmacro secure-block (output-stream &rest forms)
70   (let ((error-handler #'si::universal-error-handler))
71     `(block secure
72        (defun si::universal-error-handler (error-name correctable function-name continue-format-string error-format-string &rest args)
73          ,(when output-stream
74             `(write-line
75               (cond ((eq error-name :WRONG-TYPE-ARGUMENT) (string error-name))
76                     (t error-format-string))
77               ,output-stream))
78          (setf (symbol-function 'si::universal-error-handler) ,error-handler)
79          (return-from secure))
80        (let (result-eval)
81          (setq result-eval (progn ,@forms))
82          (setf (symbol-function 'si::universal-error-handler) ,error-handler)
83          result-eval))))
84
85 (defmacro persistent-let (name vars &rest forms)
86   (labels ((get-vars (vars)
87                      (cond ((null vars) nil)
88                            (t (cons (if (consp (car vars)) (caar vars) (car vars))
89                                     (get-vars (cdr vars)))))))
90    
91           `(let ,(cond ((functionp name)
92                         (let ((old-vars (funcall name)))
93                           (cond ((equal (get-vars vars) (get-vars old-vars)) old-vars)
94                                 (t vars))))
95                        (t vars))
96              (defun ,name ()
97                ,(let ((lvars (get-vars vars)))
98                   `(mapcar (lambda (x y) (list x y)) ',lvars ,(cons 'list lvars))))
99              ,@forms)))
100
101 ;Geometry
102 (defun dotp (dot)
103   (match-pattern dot '(0 0)))
104
105 (defun vectorp (vector)
106   (match-pattern vector '(0 0)))
107
108 (defun circlep (circle)
109   (match-pattern circle '((0 0) 0)))
110
111 (defun polygonp (polygon)
112   (cond ((consp polygon)
113          (and (dotp (car polygon))
114               (if (null (cdr polygon)) t (polygonp (cdr polygon)))))))
115
116 (defun make-dot (x y)
117   `(,x ,y))
118
119 (defun make-vector (x y)
120   `(,x ,y))
121
122 (defun make-line (dot1 dot2)
123   `(,dot1 ,dot2))
124
125 (defun make-rectangle (x1 y1 x2 y2)
126   `((,x1 ,y1) (,x2 ,y1) (,x2 ,y2) (,x1 ,y2)))
127
128 (defun polygon-center (polygon)
129   (apply #'mapcar #'avg polygon))
130
131 (defun dots-distance (dot1 dot2)
132   (destructure ((dot1 (x1 y1))
133                 (dot2 (x2 y2)))
134                (sqrt (+ (expt (- x2 x1) 2)
135                         (expt (- y2 y1) 2)))))
136
137 (defun dot-line-distance (dot line)
138   (destructure ((line ((ax ay) (bx by)))
139                 (dot (cx cy)))
140                (let* ((r-numerator (+ (* (- cx ax) (- bx ax)) (* (- cy ay) (- by ay))))
141                       (r-denomenator (+ (expt (- bx ax) 2) (expt (- by ay) 2)))
142                       (r (/ r-numerator r-denomenator)))
143                  (values
144                   (* (abs (/ (- (* (- ay cy) (- bx ax)) (* (- ax cx) (- by ay)))
145                              r-denomenator))
146                      (sqrt r-denomenator))
147                   r))))
148
149 (defun dot-segment-distance (dot segment)
150   (multiple-value-bind
151    (dist r) (dot-line-distance dot segment)
152         (cond ((and (>= r 0) (<= r 1)) dist)
153               (t (let ((dist1 (dots-distance dot (car segment)))
154                        (dist2 (dots-distance dot (cadr segment))))
155                    (if (< dist1 dist2) dist1 dist2))))))
156
157 (defun perpendicular-line (dot line)
158   (destructure ((line ((ax ay) (bx by))))
159                (multiple-value-bind
160                 (dist r) (dot-line-distance dot line)
161                 (make-line dot
162                            (make-dot (+ ax (* r (- bx ax)))
163                                      (+ ay (* r (- by ay))))))))
164
165 (defun line-angle (line)
166   (destructure ((line ((ax ay) (bx by))))
167                (let ((x (- bx ax)) (y (- by ay)))
168                  (if (and (= x 0) (= y 0)) 0 (atan y x)))))
169
170 (defun inverse-angle (angle)
171   (cond ((< angle pi) (+ angle pi))
172         (t (- angle pi))))
173
174 (defun translate-dot (dot dx dy)
175   (destructure ((dot (x y)))
176                (list (+ x dx) (+ y dy))))
177
178 (defun translate-circle (circle dx dy)
179   (destructure ((circle (center radius)))
180                (list (translate-dot center dx dy) radius)))
181
182 (defun translate-polygon (pol dx dy)
183   (mapcar (lambda (dot)
184             (translate-dot dot dx dy))
185           pol))
186
187 (defun polygon-edges (pol)
188   (mapcar (lambda (v1 v2) (list v1 v2))
189           pol
190           (union (cdr pol) (list (car pol)))))
191
192 (defun polygon-dot-intersection (polygon dot)
193 ;Eric Haines algorithm
194   (let ((edges (polygon-edges
195                 (translate-polygon polygon (neg (car dot)) (neg (cadr dot)))))
196         (counter 0))
197     (dolist (edge edges)
198       (destructure ((edge ((x1 y1) (x2 y2))))
199                    (cond ((/= (signum+ y1) (signum+ y2))
200                           (cond ((and (> x1 0) (> x2 0)) (incf counter))
201                                 ((and (or (> x1 0) (> x2 0))
202                                       (> (- x1 (* y1 (/ (- x2 x1) (- y2 y1)))) 0))
203                                  (incf counter)))))))
204     (not (evenp counter))))
205
206 (defun circle-segment-intersection (circle segment)
207   (destructure ((circle (center radius)))
208                (<= (dot-segment-distance center segment) radius)))
209
210 (defun circle-edges-intersection (circle polygon)
211   (let ((edges (polygon-edges polygon))
212         (edges-i nil))
213     (dolist (edge edges)
214       (cond ((circle-segment-intersection circle edge) (setq edges-i (cons edge edges-i)))))
215     edges-i))
216
217 (defun circle-polygon-intersection (circle polygon)
218   (or (polygon-dot-intersection polygon (car circle))
219       (circle-edges-intersection circle polygon)))
220
221 (defun circle-circle-intersection (circle1 circle2)
222   (destructure ((circle1 (center1 radius1))
223                 (circle2 (center2 radius2)))
224                (<= (dots-distance center1 center2) (+ r1 r2))))