-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgeometry.lisp
285 lines (244 loc) · 10.5 KB
/
geometry.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
(in-package #:org.shirakumo.alloy)
(defgeneric x (geometry))
(defgeneric y (geometry))
(defgeneric w (geometry))
(defgeneric h (geometry))
(defgeneric l (geometry))
(defgeneric u (geometry))
(defgeneric r (geometry))
(defgeneric b (geometry))
(defgeneric contained-p (point extent))
(defun pxx (geometry) (to-px (x geometry)))
(defun pxy (geometry) (to-px (y geometry)))
(defun pxw (geometry) (to-px (w geometry)))
(defun pxh (geometry) (to-px (h geometry)))
(defun pxl (geometry) (to-px (l geometry)))
(defun pxu (geometry) (to-px (u geometry)))
(defun pxr (geometry) (to-px (r geometry)))
(defun pxb (geometry) (to-px (b geometry)))
(cl:declaim (inline %point))
(defstruct (point (:constructor %point (x y))
(:copier NIL))
(x NIL :type unit)
(y NIL :type unit))
(defmethod print-object ((point point) stream)
(format stream "~s" (list 'point (point-x point) (point-y point))))
(defmethod make-load-form ((point point) &optional env)
(declare (ignore env))
(list '%point (point-x point) (point-y point)))
(defun point (&optional (x 0) (y 0))
(%point (unit x) (unit y)))
(define-compiler-macro point (&whole whole &optional (x 0) (y 0) &environment env)
(if (and (constantp x env) (constantp y env))
`(load-time-value (%point (unit ,x) (unit ,y)))
whole))
(defun px-point (&optional (x 0) (y 0))
(%point (px x) (px y)))
(defmethod x ((point point)) (point-x point))
(defmethod y ((point point)) (point-y point))
(defmethod l ((point point)) (point-x point))
(defmethod b ((point point)) (point-y point))
(defun point= (a b)
(and (u= (point-x a) (point-x b))
(u= (point-y a) (point-y b))))
(cl:declaim (inline %size))
(defstruct (size (:constructor %size (w h))
(:copier NIL))
(w NIL :type unit)
(h NIL :type unit))
(defmethod print-object ((size size) stream)
(format stream "~s" (list 'size (size-w size) (size-h size))))
(defmethod make-load-form ((size size) &optional env)
(declare (ignore env))
(list '%size (size-w size) (size-h size)))
(defun size (&optional w h)
(cond (h (%size (unit w) (unit h)))
(w (%size (unit w) (unit w)))
(T (%size (unit 0) (unit 0)))))
(define-compiler-macro size (&whole whole &optional (w 0 w-p) (h 0 h-p) &environment env)
(cond (h-p
(if (and (constantp w env) (constantp h env))
`(load-time-value (%size (unit ,w) (unit ,h)))
whole))
(w-p
(if (constantp w env)
`(load-time-value (%size (unit ,w) (unit ,w)))
whole))
(T
`(load-time-value (%size (unit 0) (unit 0))))))
(defun px-size (&optional w h)
(cond (h (%size (px w) (px h)))
(w (%size (px w) (px w)))
(T (%size (px 0) (px 0)))))
(defmethod w ((size size)) (size-w size))
(defmethod h ((size size)) (size-h size))
(defun size= (a b)
(and (u= (size-w a) (size-w b))
(u= (size-h a) (size-h b))))
(cl:declaim (inline %margins))
(defstruct (margins (:constructor %margins (l u r b))
(:copier NIL))
(l NIL :type unit)
(u NIL :type unit)
(r NIL :type unit)
(b NIL :type unit))
(defmethod print-object ((margins margins) stream)
(format stream "~s" (list 'margins (margins-l margins) (margins-u margins) (margins-r margins) (margins-b margins))))
(defmethod make-load-form ((margins margins) &optional env)
(declare (ignore env))
(list '%margins (margins-l margins) (margins-u margins) (margins-r margins) (margins-b margins)))
(defun margins (&optional l u r b)
(cond (b (%margins (unit l) (unit u) (unit r) (unit b)))
(r (%margins (unit l) (unit u) (unit r) (unit 0)))
(u (%margins (unit l) (unit u) (unit l) (unit u)))
(l (%margins (unit l) (unit l) (unit l) (unit l)))
(T (%margins (unit 0) (unit 0) (unit 0) (unit 0)))))
(define-compiler-macro margins (&whole whole &optional (l 0 l-p) (u 0 u-p) (r 0 r-p) (b 0 b-p) &environment env)
(cond (b-p
(if (and (constantp l env) (constantp u env) (constantp r env) (constantp b env))
`(load-time-value (%margins (unit ,l) (unit ,u) (unit ,r) (unit ,b)))
whole))
(r-p
(if (and (constantp l env) (constantp u env) (constantp r env))
`(load-time-value (%margins (unit ,l) (unit ,u) (unit ,r) (unit 0)))
whole))
(u-p
(if (and (constantp l env) (constantp u env))
`(load-time-value (%margins (unit ,l) (unit ,u) (unit ,l) (unit ,u)))
whole))
(l-p
(if (and (constantp l env))
`(load-time-value (%margins (unit ,l) (unit ,l) (unit ,l) (unit ,l)))
whole))
(T
`(load-time-value (%margins (unit 0) (unit 0) (unit 0) (unit 0))))))
(defun px-margins (&optional l u r b)
(cond (b (%margins (px l) (px u) (px r) (px b)))
(r (%margins (px l) (px u) (px r) (px 0)))
(u (%margins (px l) (px u) (px l) (px u)))
(l (%margins (px l) (px l) (px l) (px l)))
(T (%margins (px 0) (px 0) (px 0) (px 0)))))
(defmethod l ((margins margins)) (margins-l margins))
(defmethod u ((margins margins)) (margins-u margins))
(defmethod r ((margins margins)) (margins-r margins))
(defmethod b ((margins margins)) (margins-b margins))
(defun margins= (a b)
(and (u= (margins-l a) (margins-l b))
(u= (margins-u a) (margins-u b))
(u= (margins-r a) (margins-r b))
(u= (margins-b a) (margins-b b))))
(defmacro destructure-margins ((&rest args &key l u r b to-px) margins &body body)
(declare (ignore l u r b))
(let ((marginsg (gensym "EXTENT")))
`(let* ((,marginsg ,margins)
,@(loop for (name func) in '((:l margins-l) (:u margins-u) (:r margins-r) (:b margins-b))
for var = (getf args name)
when var
collect `(,var (,(if to-px 'to-px 'identity) (,func ,marginsg)))))
,@body)))
(cl:declaim (inline %extent))
(defstruct (extent (:include size)
(:constructor %extent (x y w h)))
(x NIL :type unit)
(y NIL :type unit))
(defmethod print-object ((extent extent) stream)
(format stream "~s" (list 'extent (extent-x extent) (extent-y extent) (extent-w extent) (extent-h extent))))
(defmethod make-load-form ((extent extent) &optional env)
(declare (ignore env))
(list '%extent (extent-x extent) (extent-y extent) (extent-w extent) (extent-h extent)))
(defun extent (&optional (x 0) (y 0) (w 0) (h 0))
(%extent (unit x) (unit y) (unit w) (unit h)))
(define-compiler-macro extent (&whole whole &optional (x 0) (y 0) (w 0) (h 0) &environment env)
(if (and (constantp x env) (constantp y env) (constantp w env) (constantp h env))
`(load-time-value (%extent (unit ,x) (unit ,y) (unit ,w) (unit ,h)))
whole))
(defun px-extent (&optional (x 0) (y 0) (w 0) (h 0))
(%extent (px x) (px y) (px w) (px h)))
(defun extent= (a b)
(and (u= (extent-x a) (extent-x b))
(u= (extent-y a) (extent-y b))
(u= (extent-w a) (extent-w b))
(u= (extent-h a) (extent-h b))))
(defmethod x ((extent extent)) (extent-x extent))
(defmethod y ((extent extent)) (extent-y extent))
(defmethod w ((extent extent)) (extent-w extent))
(defmethod h ((extent extent)) (extent-h extent))
(defmethod l ((extent extent)) (extent-x extent))
(defmethod b ((extent extent)) (extent-y extent))
(defmethod contained-p ((point point) (extent extent))
(and (u<= 0 (u- (point-x point) (extent-x extent)) (extent-w extent))
(u<= 0 (u- (point-y point) (extent-y extent)) (extent-h extent))))
(defmethod contained-p ((inner extent) (outer extent))
(and (u<= 0 (u- (extent-x inner) (extent-x outer)) (u- (extent-w outer) (extent-w inner)))
(u<= 0 (u- (extent-y inner) (extent-y outer)) (u- (extent-h outer) (extent-h inner)))))
(defmacro destructure-extent ((&rest args &key x y w h to-px) extent &body body)
(declare (ignore x y w h))
(let ((extentg (gensym "EXTENT")))
`(let* ((,extentg ,extent)
,@(loop for (name func) in '((:x extent-x) (:y extent-y) (:w size-w) (:h size-h))
for var = (getf args name)
when var
collect `(,var (,(if to-px 'to-px 'identity) (,func ,extentg)))))
,@body)))
(defun extent-intersection (a b)
(destructure-extent (:x x1 :y y1 :w w1 :h h1 :to-px T) a
(destructure-extent (:x x2 :y y2 :w w2 :h h2 :to-px T) b
(let* ((r1 (+ x1 w1)) (u1 (+ y1 h1))
(r2 (+ x2 w2)) (u2 (+ y2 h2))
(x (max x1 x2))
(y (max y1 y2))
(w (max 0.0 (- (min r1 r2) x)))
(h (max 0.0 (- (min u1 u2) y))))
(px-extent x y w h)))))
(defun overlapping-p (a b)
(destructure-extent (:x x1 :y y1 :w w1 :h h1 :to-px T) a
(destructure-extent (:x x2 :y y2 :w w2 :h h2 :to-px T) b
(and (< x1 (+ x2 w2)) (< x2 (+ x1 w1))
(< y1 (+ y2 h2)) (< y2 (+ y1 h1))))))
(defun widen (extent margins)
(etypecase extent
(extent
(extent (u- (x extent) (l margins))
(u- (y extent) (b margins))
(u+ (w extent) (l margins) (r margins))
(u+ (h extent) (b margins) (u margins))))
(size
(size (u+ (w extent) (l margins) (r margins))
(u+ (h extent) (b margins) (u margins))))))
;;; Return an EXTENT instance based on EXTENT-ISH. If EXTENT-ISH lacks
;;; information (for example, if EXTENT-ISH is a SIZE, it does not have a
;;; position) and BASE is supplied, use BASE to add the missing information.
(defun ensure-extent (extent-ish &optional base)
(etypecase extent-ish
(extent
extent-ish)
(margins
(etypecase base
(extent
(px-extent
(+ (pxx base) (pxl extent-ish))
(+ (pxy base) (pxb extent-ish))
(max 0.0 (- (pxw base) (pxr extent-ish) (pxl extent-ish)))
(max 0.0 (- (pxh base) (pxu extent-ish) (pxb extent-ish)))))
(size
(px-extent
(pxl extent-ish) (pxb extent-ish)
(max 0.0 (- (pxw base) (pxr extent-ish) (pxl extent-ish)))
(max 0.0 (- (pxh base) (pxu extent-ish) (pxb extent-ish)))))
(null
(px-extent
(pxl extent-ish) (pxb extent-ish)
(max 0.0 (- (pxw *unit-parent*) (pxr extent-ish) (pxl extent-ish)))
(max 0.0 (- (pxh *unit-parent*) (pxu extent-ish) (pxb extent-ish)))))))
(size
(etypecase base
(extent
(extent (x base) (y base) (w extent-ish) (h extent-ish)))
((or null size)
(extent 0 0 (w extent-ish) (h extent-ish)))))
(point
(etypecase base
(size
(extent (x extent-ish) (y extent-ish) (w base) (h base)))
(null
(extent (x extent-ish) (y extent-ish) 0 0))))))