-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhelpers.lisp
178 lines (146 loc) · 4.26 KB
/
helpers.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
(in-package :gcode)
;; general helper functions
(defun mm-per-sec-to-mm-per-minute (mm-per-sec)
(* 60 mm-per-sec))
(defun mm-per-minute-to-mm-per-sec (mm-per-minute)
(/ mm-per-minute 60.0))
(defun realize-number (x)
(let ((num (if (numberp x)
(coerce x 'float)
x)))
(if (< (abs num) 0.001)
0
num)))
(defun sign (number)
(if (< number 0)
-1
+1))
(defun null-offset-p (x)
(or (null x)
(= x 0)))
;; trigonometry matrix stuff for transformations
(defparameter *PI* 3.141592653589793238462643)
(defparameter *unity-matrix*
(let ((res (make-array '(3 3) :initial-element 0)))
(setf (aref res 0 0) 1
(aref res 1 1) 1
(aref res 2 2) 1)
res))
(defun deg-to-radians (angle)
(* (/ angle 360.0) 2 *PI*))
(defun radians-to-deg (angle)
(* 360.0 (/ angle (* 2 *PI*))))
(defun rotation-matrix (angle)
(let ((res (make-array '(3 3) :initial-element 0)))
(setf (aref res 0 0) (cos (deg-to-radians angle))
(aref res 0 1) (sin (deg-to-radians angle))
(aref res 1 0) (- (sin (deg-to-radians angle)))
(aref res 1 1) (cos (deg-to-radians angle))
(aref res 2 2) 1)
res))
(defun mirror-matrix (p1)
(let ((res (make-array '(3 3) :initial-element 0))
(x (2d-point-x p1))
(y (2d-point-y p1)))
(setf (aref res 2 2) 1
(aref res 0 0) (- (square x) (square y))
(aref res 0 1) (* 2 x y)
(aref res 1 0) (* 2 x y)
(aref res 1 1) (- (square y) (square x)))
(matrix-scale res (/ 1 (+ (square x) (square y))))))
(defun translation-matrix (x y)
(let ((res (copy-matrix *unity-matrix*)))
(setf (aref res 0 2) x
(aref res 1 2) y)
res))
(defun matrix-multiply (a b)
(let ((res (make-array '(3 3) :initial-element 0)))
(dotimes (i 3)
(dotimes (j 3)
(dotimes (k 3)
(incf (aref res i j)
(* (aref a i k)
(aref b k j))))))
res))
(defun copy-matrix (matrix)
(let ((res (make-array (array-dimensions matrix))))
(dotimes (i (array-dimension matrix 0))
(dotimes (j (array-dimension matrix 1))
(setf (aref res i j)
(aref matrix i j))))
res))
(defun matrix-scale (matrix a)
(let ((res (copy-matrix matrix)))
(dotimes (i 3)
(dotimes (j 3)
(setf (aref res i j)
(* a (aref res i j)))))
res))
(defun invert-matrix (matrix)
(let* ((a (aref matrix 0 0))
(b (aref matrix 0 1))
(c (aref matrix 0 2))
(d (aref matrix 1 0))
(e (aref matrix 1 1))
(f (aref matrix 1 2))
(g (aref matrix 2 0))
(h (aref matrix 2 1))
(i (aref matrix 2 2))
(factor (/ 1 (+ (* a (- (* e i) (* f h)))
(- (* b (- (* d i) (* f g))))
(* c (- (* d h) (* e g)))))))
(let ((res (make-array '(3 3))))
(setf (aref res 0 0)
(* factor (- (* e i) (* f h))))
(setf (aref res 0 1)
(* factor (- (* c h) (* b i))))
(setf (aref res 0 2)
(* factor (- (* b f) (* c e))))
(setf (aref res 1 0)
(* factor (- (* f g) (* d i))))
(setf (aref res 1 1)
(* factor (- (* a i) (* c g))))
(setf (aref res 1 2)
(* factor (- (* c d) (* a f))))
(setf (aref res 2 0)
(* factor (- (* d h) (* e g))))
(setf (aref res 2 1)
(* factor (- (* b g) (* a h))))
(setf (aref res 2 2)
(* factor (- (* a e) (* b d))))
res)))
(defun scaling-matrix (scale)
(let ((res (copy-matrix *unity-matrix*)))
(setf (aref res 0 0) scale
(aref res 1 1) scale)
res))
(defun vector-angle (x1 y1 x2 y2)
(let ((xdiff (- x2 x1))
(ydiff (- y2 y1)))
(radians-to-deg
(- (atan ydiff xdiff)
(atan 0 1)))))
(defun vector-length (x1 y1 x2 y2)
(let ((xdiff (- x2 x1))
(ydiff (- y2 y1)))
(sqrt (+ (* xdiff xdiff)
(* ydiff ydiff)))))
(defun rotate-vector (x y matrix)
(list (+ (* x (aref matrix 0 0))
(* y (aref matrix 0 1)))
(+ (* x (aref matrix 1 0))
(* y (aref matrix 1 1)))))
(defun transform-vector (x y matrix)
(list (+ (* x (aref matrix 0 0))
(* y (aref matrix 0 1))
(aref matrix 0 2))
(+ (* x (aref matrix 1 0))
(* y (aref matrix 1 1))
(aref matrix 1 2))))
(defparameter *epsilon* 0.01)
(defun epsilon-= (a b &key (epsilon *epsilon*))
(< (abs (- a b)) epsilon))
(defvar *current-transform* *unity-matrix*)
(defmacro with-transform ((transform) &rest body)
`(let ((*current-transform* (matrix-multiply *current-transform* ,transform)))
,@body))