-
Notifications
You must be signed in to change notification settings - Fork 6
/
regions.lisp
2406 lines (1985 loc) · 94.7 KB
/
regions.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: CLIM-INTERNALS; -*-
;;; --------------------------------------------------------------------------------------
;;; Title: The CLIM Region Datatype
;;; Created: 1998-12-02 19:26
;;; Author: Gilbert Baumann <[email protected]>
;;; License: LGPL (See file COPYING for details).
;;; $Id: regions.lisp,v 1.39 2009-06-03 20:33:16 ahefner Exp $
;;; --------------------------------------------------------------------------------------
;;; (c) copyright 1998,1999,2001 by Gilbert Baumann
;;; (c) copyright 2001 by Arnaud Rouanet ([email protected])
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
;;;; Changes
;;; When Who What
;;; --------------------------------------------------------------------------------------
;;; 2002-06-27 GB REGION-INTERSECTS-REGION-P has an :around method on bounding
;;; rectangles.
;;; 2002-06-04 APD partially fixed (BOUNDING-RECTANGLE* STANDARD-ELLIPSE)
;;; 2001-07-16 GB added (REGION-CONTAINS-POSITION-P STANDARD-ELLIPSE ..)
;;; added (BOUNDING-RECTANGLE* STANDARD-ELLIPSE)
;;; added (REGION-INTERSECTION LINE STANDARD-ELLIPSE) and vice versa
;;; 2001-07-12 GB fixed bugs in
;;; (BOUNDING-RECTANGLE* STANDARD-REGION-UNION)
;;; (BOUNDING-RECTANGLE* STANDARD-REGION-INTERSECTION)
;;; 2001-07-09 GB maybe fixed a bug in MAP-OVER-SCHNITT-GERADE/POLYGON.
;;; 2001-03-09 AR fixed a bug in MAKE-ELLIPICAL-THING
;;; fixed STANDARD-ELLIPTICAL-ARC defclass
;;; 2001-03-06 AR fixed bug in (REGION-EQUAL STANDARD-RECTANGLE STANDARD-RECTANGLE)
;;; REGION is now a subclass of DESIGN.
;;; 2001-01-21 GB fixed bug in (TRANSFORM-REGION T RECTANGLE-SET)
;;; added some documentation
;;; GB = Gilbert Baumann <[email protected]>
;;; AR = Arnaud Rouanet <[email protected]>
;;; ---- TODO ----------------------------------------------------------------------------
;; - ellipses: The intersection of two ellipses is there, but
;; handling the start/end angle is not implemented.
;; - This code is anything else than well organized.
;; - provide better (faster) implementations for REGION-EQUAL,
;; REGION-CONTAINS-REGION-P, and REGION-INTERSECTS-REGION-P.
;; - Compute a union/intersection/difference of an union of polygon vs another
;; polygon or union of polygons directly via POLYGON-OP.
;; - STANDARD-REGION-UNION should either become a subclass
;; 'STANDARD-DISJUNCT-REGION-UNION' or a flag. Some set operations could take
;; advantage out the information, if the subregions of an union are disjunct.
;; - provide sensible PRINT-OBJECT methods.
;; - while you are are at it; provide a reasonable fast vertical scan routine.
;; polygons should make use of the sweep line algorithm.
;; - implement bounding rectangle cache for polygons and polylines
;; - make REGION-CONTAINS-POSITION-P for polygons faster by handling the special
;; case of the intersection of a horizontal line and the polygons
;; - MAKE-POLY{LINE,GON} should canonise its arguments; no edges of length 0 and
;; no co-linear vertexes. Maybe: canonise rectangles? Also a polygon of less
;; than three vertexes is to be considered empty aka +nowhere+.
(in-package :clim-internals)
(defclass nowhere-region (region nowhere-mixin) ())
(defclass everywhere-region (region everywhere-mixin) ())
;; coordinate is defined in coordinates.lisp
(defvar +everywhere+ (make-instance 'everywhere-region))
(defvar +nowhere+ (make-instance 'nowhere-region))
(defmethod bounding-rectangle* ((x nowhere-region))
(values 0 0 0 0))
;; 2.5.1.1 Region Predicates in CLIM
(defgeneric region-equal (region1 region2))
(defgeneric region-contains-region-p (region1 region2))
(defgeneric region-contains-position-p (region x y))
(defgeneric region-intersects-region-p (region1 region2))
;; 2.5.1.2 Composition of CLIM Regions
(defclass standard-region-union (region-set)
((regions :initarg :regions :reader standard-region-set-regions)))
(defclass standard-region-intersection (region-set)
((regions :initarg :regions :reader standard-region-set-regions)))
(defclass standard-region-difference (region-set)
((a :initarg :a :reader standard-region-difference-a)
(b :initarg :b :reader standard-region-difference-b)))
;; Protocol:
(defgeneric region-set-regions (region &key normalize))
(defgeneric map-over-region-set-regions (function region &key normalize))
(defgeneric region-union (region1 region2))
(defgeneric region-intersection (region1 region2))
(defgeneric region-difference (region1 region2))
;;; ---- 2.5.2 CLIM Point Objects --------------------------------------------------------
(defclass standard-point (point)
((x :type coordinate :initarg :x)
(y :type coordinate :initarg :y)))
(defun make-point (x y)
(make-instance 'standard-point :x (coerce x 'coordinate) :y (coerce y 'coordinate)))
(defmethod print-object ((self standard-point) sink)
(with-slots (x y) self
(format sink "#<~S ~S ~S>" 'standard-point x y)))
;; Point protocol: point-position
(defgeneric point-position (point))
(defmethod point-position ((self standard-point))
(with-slots (x y) self
(values x y)))
(defmethod point-x ((self point))
(nth-value 0 (point-position self)))
(defmethod point-y ((self point))
(nth-value 1 (point-position self)))
(defmethod transform-region (transformation (self standard-point))
(with-slots (x y) self
(multiple-value-bind (x* y*) (transform-position transformation x y)
(make-point x* y*))))
(defmethod region-contains-position-p ((self standard-point) px py)
(with-slots (x y) self
(and (coordinate= x px) (coordinate= y py))))
;;; ---- 2.5.3 Polygons and Polylines in CLIM --------------------------------------------
;; Protocol:
(defclass standard-polyline (polyline)
((points :initarg :points)
(closed :initarg :closed)))
(defclass standard-polygon (polygon)
((points :initarg :points)) )
;;; ---- 2.5.3.1 Constructors for CLIM Polygons and Polylines ---------------------------
(defun coord-seq->point-seq (sequence)
(let ((res nil))
(do-sequence ((x y) sequence)
(push (make-point x y) res))
(nreverse res)))
(defun make-polyline (point-seq &key closed)
(assert (every #'pointp point-seq))
(setq point-seq (coerce point-seq 'list))
(cond ((every (lambda (x) (region-equal x (car point-seq)))
(cdr point-seq))
+nowhere+)
(t
(make-instance 'standard-polyline :points point-seq :closed closed))))
(defun make-polyline* (coord-seq &key closed)
(make-polyline (coord-seq->point-seq coord-seq) :closed closed))
(defun make-polygon (point-seq)
(assert (every #'pointp point-seq))
(setq point-seq (coerce point-seq 'list))
(cond ((every (lambda (x) (region-equal x (car point-seq)))
(cdr point-seq))
+nowhere+)
(t
(make-instance 'standard-polygon :points point-seq))))
(defun make-polygon* (coord-seq)
(make-polygon (coord-seq->point-seq coord-seq)))
(defmethod polygon-points ((self standard-polygon))
(with-slots (points) self
points))
(defmethod map-over-polygon-coordinates (fun (self standard-polygon))
(with-slots (points) self
(mapc (lambda (p) (funcall fun (point-x p) (point-y p))) points)))
(defmethod map-over-polygon-segments (fun (self standard-polygon))
(with-slots (points) self
(do ((q points (cdr q)))
((null (cdr q))
(funcall fun (point-x (car q)) (point-y (car q)) (point-x (car points)) (point-y (car points))))
(funcall fun (point-x (car q)) (point-y (car q)) (point-x (cadr q)) (point-y (cadr q))))))
(defmethod polygon-points ((self standard-polyline))
(with-slots (points) self
points))
(defmethod map-over-polygon-coordinates (fun (self standard-polyline))
(with-slots (points) self
(mapc (lambda (p) (funcall fun (point-x p) (point-y p))) points)))
(defmethod map-over-polygon-segments (fun (self standard-polyline))
(with-slots (points closed) self
(do ((q points (cdr q)))
((null (cdr q))
(when closed
(funcall fun (point-x (car q)) (point-y (car q)) (point-x (car points)) (point-y (car points)))))
(funcall fun (point-x (car q)) (point-y (car q)) (point-x (cadr q)) (point-y (cadr q))))))
(defmethod polyline-closed ((self standard-polyline))
(with-slots (closed) self
closed))
(defmethod transform-region (transformation (self standard-polyline))
(with-slots (points closed) self
(make-polyline (mapcar (lambda (p)
(multiple-value-bind (x* y*) (transform-position transformation (point-x p) (point-y p))
(make-point x* y*)))
points)
:closed closed)))
(defmethod transform-region (transformation (self standard-polygon))
(with-slots (points) self
(make-polygon (mapcar (lambda (p)
(multiple-value-bind (x* y*) (transform-position transformation (point-x p) (point-y p))
(make-point x* y*)))
points))))
(defmethod region-contains-position-p ((self standard-polyline) x y)
(setf x (coerce x 'coordinate)
y (coerce y 'coordinate))
(block nil
(map-over-polygon-segments (lambda (x1 y1 x2 y2)
(when (line-contains-point-p* x1 y1 x2 y2 x y)
(return t)))
self)
nil))
(defun line-contains-point-p* (x1 y1 x2 y2 px py)
(and (or (<= x1 px x2) (>= x1 px x2))
(or (<= y1 py y2) (>= y1 py y2))
(coordinate= (* (- py y1) (- x2 x1))
(* (- px x1) (- y2 y1)))))
(defun line-contains-point-p** (x1 y1 x2 y2 px py)
(coordinate= (* (- py y1) (- x2 x1))
(* (- px x1) (- y2 y1))))
;;; ---- 2.5.4 Lines in CLIM -------------------------------------------------------------
;; Line protocol: line-start-point* line-end-point*
(defclass standard-line (line)
((x1 :type coordinate :initarg :x1)
(y1 :type coordinate :initarg :y1)
(x2 :type coordinate :initarg :x2)
(y2 :type coordinate :initarg :y2)))
(defun make-line (start-point end-point)
(make-line* (point-x start-point) (point-y start-point) (point-x end-point) (point-y end-point)))
(defun make-line* (start-x start-y end-x end-y)
(setf start-x (coerce start-x 'coordinate)
start-y (coerce start-y 'coordinate)
end-x (coerce end-x 'coordinate)
end-y (coerce end-y 'coordinate))
(if (and (coordinate= start-x end-x)
(coordinate= start-y end-y))
+nowhere+
(make-instance 'standard-line :x1 start-x :y1 start-y :x2 end-x :y2 end-y)))
(defmethod line-start-point* ((line standard-line))
(with-slots (x1 y1 x2 y2) line
(values x1 y1)))
(defmethod line-end-point* ((line standard-line))
(with-slots (x1 y1 x2 y2) line
(values x2 y2)))
(defmethod line-start-point ((line line))
(multiple-value-bind (x y) (line-start-point* line)
(make-point x y)))
(defmethod line-end-point ((line line))
(multiple-value-bind (x y) (line-end-point* line)
(make-point x y)))
;; polyline protocol for standard-line's:
(defmethod polygon-points ((line standard-line))
(with-slots (x1 y1 x2 y2) line
(list (make-point x1 y1) (make-point x2 y2))))
(defmethod map-over-polygon-coordinates (fun (line standard-line))
(with-slots (x1 y1 x2 y2) line
(funcall fun x1 y1)
(funcall fun x2 y2)))
(defmethod map-over-polygon-segments (fun (line standard-line))
(with-slots (x1 y1 x2 y2) line
(funcall fun x1 y1 x2 y2)))
(defmethod polyline-closed ((line standard-line))
nil)
(defmethod transform-region (transformation (line standard-line))
(with-slots (x1 y1 x2 y2) line
(multiple-value-bind (x1* y1*) (transform-position transformation x1 y1)
(multiple-value-bind (x2* y2*) (transform-position transformation x2 y2)
(make-line* x1* y1* x2* y2*)))))
(defmethod region-contains-position-p ((self standard-line) x y)
(multiple-value-bind (x1 y1) (line-start-point* self)
(multiple-value-bind (x2 y2) (line-end-point* self)
(line-contains-point-p* x1 y1 x2 y2 x y))))
(defmethod print-object ((self standard-line) sink)
(with-slots (x1 y1 x2 y2) self
(format sink "#<~S ~D ~D ~D ~D>" (type-of self) x1 y1 x2 y2)))
;;; ---- 2.5.5 Rectangles in CLIM --------------------------------------------------------
;; protocol:
;; rectangle-edges*
(defclass standard-rectangle (rectangle)
((coordinates :initform (make-array 4 :element-type 'coordinate))))
(defmethod initialize-instance :after ((obj standard-rectangle)
&key (x1 0.0d0) (y1 0.0d0)
(x2 0.0d0) (y2 0.0d0))
(let ((coords (slot-value obj 'coordinates)))
(setf (aref coords 0) x1)
(setf (aref coords 1) y1)
(setf (aref coords 2) x2)
(setf (aref coords 3) y2)))
(defmacro with-standard-rectangle ((x1 y1 x2 y2) rectangle &body body)
(with-gensyms (coords)
`(let ((,coords (slot-value ,rectangle 'coordinates)))
(declare (type (simple-array coordinate (4)) ,coords))
(let ((,x1 (aref ,coords 0))
(,y1 (aref ,coords 1))
(,x2 (aref ,coords 2))
(,y2 (aref ,coords 3)))
(declare (type coordinate ,x1 ,y1 ,x2 ,y2))
,@body))))
(defmacro with-standard-rectangle* ((&key x1 y1 x2 y2) rectangle &body body)
(with-gensyms (coords)
`(let ((,coords (slot-value ,rectangle 'coordinates)))
(declare (type (simple-array coordinate (4)) ,coords))
(let (,@(and x1 `((,x1 (aref ,coords 0))))
,@(and y1 `((,y1 (aref ,coords 1))))
,@(and x2 `((,x2 (aref ,coords 2))))
,@(and y2 `((,y2 (aref ,coords 3)))))
(declare (type coordinate
,@(and x1 `(,x1))
,@(and y1 `(,y1))
,@(and x2 `(,x2))
,@(and y2 `(,y2))))
,@body))))
(defun make-rectangle (point1 point2)
(make-rectangle* (point-x point1) (point-y point1) (point-x point2) (point-y point2)))
(defun make-rectangle* (x1 y1 x2 y2)
(psetq x1 (coerce (min x1 x2) 'coordinate)
x2 (coerce (max x1 x2) 'coordinate)
y1 (coerce (min y1 y2) 'coordinate)
y2 (coerce (max y1 y2) 'coordinate))
(if (or (coordinate= x1 x2)
(coordinate= y1 y2))
+nowhere+
(make-instance 'standard-rectangle :x1 x1 :x2 x2 :y1 y1 :y2 y2)))
(defmethod rectangle-edges* ((rect standard-rectangle))
(with-standard-rectangle (x1 y1 x2 y2)
rect
(values x1 y1 x2 y2)))
;;; standard-rectangles are immutable and all that, but we still need to set
;;; their positions and dimensions (in output recording)
(defgeneric* (setf rectangle-edges*) (x1 y1 x2 y2 rectangle))
(defmethod* (setf rectangle-edges*)
(x1 y1 x2 y2 (rectangle standard-rectangle))
(let ((coords (slot-value rectangle 'coordinates)))
(declare (type (simple-array coordinate (4)) coords))
(setf (aref coords 0) x1)
(setf (aref coords 1) y1)
(setf (aref coords 2) x2)
(setf (aref coords 3) y2))
(values x1 y1 x2 y2))
(defmethod rectangle-min-point ((rect rectangle))
(multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
(declare (ignore x2 y2))
(make-point x1 y1)))
(defmethod rectangle-min-point ((rect standard-rectangle))
(with-standard-rectangle* (:x1 x1 :y1 y1)
rect
(make-point x1 y1)))
(defmethod rectangle-max-point ((rect rectangle))
(multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
(declare (ignore x1 y1))
(make-point x2 y2)))
(defmethod rectangle-max-point ((rect standard-rectangle))
(with-standard-rectangle* (:x2 x2 :y2 y2)
rect
(make-point x2 y2)))
(defmethod rectangle-min-x ((rect rectangle))
(nth-value 0 (rectangle-edges* rect)))
(defmethod rectangle-min-x ((rect standard-rectangle))
(with-standard-rectangle* (:x1 x1)
rect
x1))
(defmethod rectangle-min-y ((rect rectangle))
(nth-value 1 (rectangle-edges* rect)))
(defmethod rectangle-min-y ((rect standard-rectangle))
(with-standard-rectangle* (:y1 y1)
rect
y1))
(defmethod rectangle-max-x ((rect rectangle))
(nth-value 2 (rectangle-edges* rect)))
(defmethod rectangle-max-x ((rect standard-rectangle))
(with-standard-rectangle* (:x2 x2)
rect
x2))
(defmethod rectangle-max-y ((rect rectangle))
(nth-value 3 (rectangle-edges* rect)))
(defmethod rectangle-max-y ((rect standard-rectangle))
(with-standard-rectangle* (:y2 y2)
rect
y2))
(defmethod rectangle-width ((rect rectangle))
(multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
(declare (ignore y1 y2))
(- x2 x1)))
(defmethod rectangle-width ((rect standard-rectangle))
(with-standard-rectangle* (:x1 x1 :x2 x2)
rect
(- x2 x1)))
(defmethod rectangle-height ((rect rectangle))
(multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
(declare (ignore x1 x2))
(- y2 y1)))
(defmethod rectangle-height ((rect standard-rectangle))
(with-standard-rectangle* (:y1 y1 :y2 y2)
rect
(- y2 y1)))
(defmethod rectangle-size ((rect rectangle))
(multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
(values (- x2 x1) (- y2 y1))))
(defmethod rectangle-size ((rect standard-rectangle))
(with-standard-rectangle (x1 y1 x2 y2)
rect
(values (- x2 x1) (- y2 y1))))
;; polyline/polygon protocol for standard-rectangle's
(defmethod polygon-points ((rect standard-rectangle))
(with-standard-rectangle (x1 y1 x2 y2)
rect
(list (make-point x1 y1)
(make-point x1 y2)
(make-point x2 y2)
(make-point x2 y1))))
(defmethod map-over-polygon-coordinates (fun (rect standard-rectangle))
(with-standard-rectangle (x1 y1 x2 y2)
rect
(funcall fun x1 y1)
(funcall fun x1 y2)
(funcall fun x2 y2)
(funcall fun x2 y1)))
(defmethod map-over-polygon-segments (fun (rect standard-rectangle))
(with-standard-rectangle (x1 y1 x2 y2)
rect
(funcall fun x1 y1 x1 y2)
(funcall fun x1 y2 x2 y2)
(funcall fun x2 y2 x2 y1)
(funcall fun x2 y1 x1 y1)))
(defmethod transform-region (transformation (rect standard-rectangle))
(cond ((rectilinear-transformation-p transformation)
(with-standard-rectangle (x1 y1 x2 y2)
rect
(multiple-value-bind (x1* y1*) (transform-position transformation x1 y1)
(multiple-value-bind (x2* y2*) (transform-position transformation x2 y2)
(make-rectangle* x1* y1* x2* y2*)))))
(t
(make-polygon (mapcar (lambda (p) (transform-region transformation p))
(polygon-points rect)))) ))
(defmethod region-contains-position-p ((self standard-rectangle) x y)
(with-standard-rectangle (x1 y1 x2 y2)
self
(and (<= x1 (coerce x 'coordinate) x2)
(<= y1 (coerce y 'coordinate) y2))))
;;; ---- 2.5.6 Ellipses and Elliptical Arcs in CLIM --------------------------------------
(defclass elliptical-thing ()
((start-angle :initarg :start-angle)
(end-angle :initarg :end-angle)
(tr :initarg :tr))) ;a transformation from the unit circle to get the elliptical object
(defmethod print-object ((ell elliptical-thing) stream)
(with-slots (start-angle end-angle tr) ell
(format stream "#<~A [~A ~A] ~A>"
(type-of ell)
(and start-angle (* (/ 180 pi) start-angle))
(and end-angle (* (/ 180 pi) end-angle))
tr)))
(defclass standard-ellipse (elliptical-thing ellipse) ())
(defclass standard-elliptical-arc (elliptical-thing elliptical-arc) ())
;;; ---- 2.5.6.1 Constructor Functions for Ellipses and Elliptical Arcs in CLIM ---------
(defun make-ellipse (center-point radius-1-dx radius-1-dy radius-2-dx radius-2-dy &key start-angle end-angle)
(make-ellipse* (point-x center-point) (point-y center-point)
radius-1-dx radius-1-dy radius-2-dx radius-2-dy
:start-angle start-angle
:end-angle end-angle))
(defun make-ellipse* (center-x center-y radius-1-dx radius-1-dy radius-2-dx radius-2-dy
&key start-angle end-angle)
(make-ellipical-thing 'standard-ellipse
center-x center-y radius-1-dx radius-1-dy radius-2-dx radius-2-dy
start-angle end-angle))
(defun make-elliptical-arc (center-point radius-1-dx radius-1-dy radius-2-dx radius-2-dy &key start-angle end-angle)
(make-elliptical-arc* (point-x center-point) (point-y center-point)
radius-1-dx radius-1-dy radius-2-dx radius-2-dy
:start-angle start-angle
:end-angle end-angle))
(defun make-elliptical-arc* (center-x center-y radius-1-dx radius-1-dy radius-2-dx radius-2-dy
&key start-angle end-angle)
(make-ellipical-thing 'standard-elliptical-arc
center-x center-y radius-1-dx radius-1-dy radius-2-dx radius-2-dy
start-angle end-angle))
(defun make-ellipical-thing (class
center-x center-y radius-1-dx radius-1-dy radius-2-dx radius-2-dy
start-angle end-angle)
(setf center-x (coerce center-x 'coordinate)
center-y (coerce center-y 'coordinate)
radius-1-dx (coerce radius-1-dx 'coordinate)
radius-1-dy (coerce radius-1-dy 'coordinate)
radius-2-dx (coerce radius-2-dx 'coordinate)
radius-2-dy (coerce radius-2-dy 'coordinate)
start-angle (and start-angle (coerce start-angle 'coordinate))
end-angle (and end-angle (coerce end-angle 'coordinate)) )
(let ((tr (make-3-point-transformation* 0 0 1 0 0 1
center-x center-y
(+ center-x radius-1-dx) (+ center-y radius-1-dy)
(+ center-x radius-2-dx) (+ center-y radius-2-dy))))
(cond ((and (null start-angle) (null end-angle)))
((null start-angle) (setf start-angle 0))
((null end-angle) (setf end-angle (* 2 pi))))
(make-instance class :tr tr :start-angle start-angle :end-angle end-angle) ))
(defmethod transform-region (transformation (self elliptical-thing))
(with-slots (start-angle end-angle tr) self
;; I think this should be untransform-angle below, as the ellipse angles
;; go counter-clockwise in screen coordinates, whereas our transformations
;; rotate clockwise.. -Hefner
(let ((start-angle* (and start-angle (untransform-angle transformation start-angle)))
(end-angle* (and end-angle (untransform-angle transformation end-angle))))
(when (reflection-transformation-p transformation)
(rotatef start-angle* end-angle*))
(make-instance (type-of self)
:tr (compose-transformations transformation tr)
:start-angle start-angle*
:end-angle end-angle*))))
(defmethod region-contains-position-p ((self standard-ellipse) x y)
;; XXX start/end angle still missing
(with-slots (tr) self
(multiple-value-bind (x y) (untransform-position tr x y)
(<= (+ (* x x) (* y y)) 1))))
(defmethod bounding-rectangle* ((region standard-ellipse))
;; XXX start/end angle still missing
(with-slots (tr) region
(flet ((contact-radius* (x y)
"Returns coordinates of the radius of the point, in
which the vector field (x y) touches the ellipse."
(multiple-value-bind (xc yc) (untransform-distance tr x y)
(let* ((d (sqrt (+ (* xc xc) (* yc yc))))
(xn (- (/ yc d)))
(yn (/ xc d)))
(transform-distance tr xn yn)))))
(multiple-value-bind (cx cy) (ellipse-center-point* region)
(if (zerop (ellipse-radii region))
(values cx cy cx cy)
(multiple-value-bind (vdx vdy) (contact-radius* 1 0)
(declare (ignore vdx))
(multiple-value-bind (hdx hdy) (contact-radius* 0 1)
(declare (ignore hdy))
(let ((rx (abs hdx))
(ry (abs vdy)))
(values (- cx rx) (- cy ry)
(+ cx rx) (+ cy ry))))))))))
(defun intersection-line/unit-circle (x1 y1 x2 y2)
"Computes the intersection of the line from (x1,y1) to (x2,y2) and the unit circle.
If the intersection is empty, NIL is returned.
Otherwise four values are returned: x1, y1, x2, y2; the start and end point of the
resulting line."
(let* ((dx (- x2 x1))
(dy (- y2 y1))
(a (+ (expt dx 2) (expt dy 2)))
(b (+ (* 2 x1 dx) (* 2 y1 dy)))
(c (+ (expt x1 2) (expt y1 2) -1)))
(let ((s1 (- (/ (+ (sqrt (- (expt b 2) (* 4 a c))) b) (* 2 a))))
(s2 (- (/ (- b (sqrt (- (expt b 2) (* 4 a c)))) (* 2 a)))))
(cond ((and (realp s1) (realp s2)
(not (and (< s1 0) (< s2 0)))
(not (and (> s1 1) (> s2 1))))
(let ((s1 (max 0 (min 1 s1)))
(s2 (max 0 (min 1 s2))))
(values (+ x1 (* s1 dx))
(+ y1 (* s1 dy))
(+ x1 (* s2 dx))
(+ y1 (* s2 dy)))))
(t
nil)))))
(defmethod region-intersection ((line line) (ellipse standard-ellipse))
(with-slots (tr) ellipse
(multiple-value-bind (x1 y1 x2 y2)
(multiple-value-call #'intersection-line/unit-circle
(multiple-value-call #'untransform-position tr (line-start-point* line))
(multiple-value-call #'untransform-position tr (line-end-point* line)))
(if x1
(multiple-value-call #'make-line*
(transform-position tr x1 y1)
(transform-position tr x2 y2))
+nowhere+))))
(defmethod region-intersection ((ellipse standard-ellipse) (line standard-line))
(region-intersection ellipse line))
;;; ---- 2.5.6.2 Accessors for CLIM Elliptical Objects -----------------------------------
(defmethod ellipse-center-point* ((self elliptical-thing))
(with-slots (tr) self
(transform-position tr 0 0)))
(defmethod ellipse-center-point ((self elliptical-thing))
(with-slots (tr) self
(transform-region tr (make-point 0 0))))
(defmethod ellipse-radii ((self elliptical-thing))
(with-slots (tr) self
(multiple-value-bind (dx1 dy1) (transform-distance tr 1 0)
(multiple-value-bind (dx2 dy2) (transform-distance tr 0 1)
(values dx1 dy1 dx2 dy2)))))
(defmethod ellipse-start-angle ((self elliptical-thing))
(with-slots (start-angle) self
start-angle))
(defmethod ellipse-end-angle ((self elliptical-thing))
(with-slots (end-angle) self
end-angle))
(defun ellipse-coefficients (ell)
;; Returns the coefficients of the equation specifing the ellipse as in
;; ax^2 + by^2 + cxy + dx + dy - f = 0
;; Note 1:
;; The `f' here may seem to be superfluous, since you
;; could simply multiply the whole equation by 1/f. But this is
;; not the case, since `f' may as well be 0.
;; Note 2:
;; In the literature you often find something like
;; (x^2)/a + (y^2)/b - 1 = 0 for an axis aligned ellipse, but
;; I rather choose to treat all coefficients as simple factors instead
;; of denominators.
(with-slots (tr) ell
;;warum die inverse hier?
(multiple-value-bind (a b d e c f) (get-transformation (invert-transformation tr))
(values
(+ (* a a) (* d d)) ; x**2
(+ (* b b) (* e e)) ; y**2
(+ (* 2 a b) (* 2 d e)) ; xy
(+ (* 2 a c) (* 2 d f)) ; x
(+ (* 2 b c) (* 2 e f)) ; y
(+ (* c c) (* f f) -1)))) )
;;; Straight from the horse's mouth -- moore
;;;
;;; Axis of an ellipse
;;; -------------------------
;; Given an ellipse with its center at the origin, as
;; ax^2 + by^2 + cxy - 1 = 0
;; The two axis of an ellipse are characterized by minimizing and
;; maximizing the radius. Let (x,y) be a point on the delimiter of the
;; ellipse. It's radius (distance from the origin) then is:
;; r^2 = x^2 + y^2
;; To find the axis can now be stated as an minimization problem with
;; constraints. So mechanically construct the auxiliarry function H:
;; H = x^2 + y^2 - k(ax^2 + by^2 + cxy - 1)
;; So the following set of equations remain to be solved
;; (I) dH/dx = 0 = 2x + 2kax + kcy
;; (II) dH/dy = 0 = 2y + 2kby + kcx
;; (III) dH/dk = 0 = ax^2 + by^2 + cxy - 1
;; Unfortunately, as I always do the math work - hopelessly, even -
;; Maxima is the tool of my choice:
;; g1: 2*x + 2*k*a*x + k*c*y$
;; g2: 2*y + 2*k*b*y + k*c*x$
;; g3: a*x*x + b*y*y + c*x*y -1$
;; sol1: solve ([g1,g2],[k,y])$
;; /* This yields two solutions because of the squares with occur. The
;; * last equation (G3) must therefore be handled for both solutions for
;; * y.
;; */
;; y1: rhs(first(rest(first(sol1))))$
;; y2: rhs(first(rest(first(rest(sol1)))))$
;; /* Substitute the 'y' found. */
;; sol2: solve(subst(y1,y,g3),x);
;; x11: rhs(first(sol2));
;; x12: rhs(first(rest(sol2)));
;; sol3: solve(subst(y2,y,g3),x);
;; x21: rhs(first(sol3));
;; x22: rhs(first(rest(sol3)));
;; /* dump everything */
;; dumpsol([[x=x11,y=y1], [x=x12,y=y1], [x=x21,y=y2], [x=x22,y=y2]]);
(defun ellipse-normal-radii* (ell)
(multiple-value-bind (a b c) (ellipse-coefficients ell)
(cond ((coordinate= 0 c)
;; this is the unit circle
(values 0 (sqrt (/ 1 b))
(sqrt (/ 1 a)) 0))
(t
(let* ((x1 (- (/ c
(sqrt (+ (- (* (* c c)
(sqrt (+ (* c c)
(* b b)
(- (* 2 a b)) (* a a)))))
(- (* 2 (* b b)
(sqrt (+ (* c c) (* b b)
(- (* 2 a b)) (* a a)))))
(* 2 a b (sqrt (+ (* c c) (* b b)
(- (* 2 a b))
(* a a))))
(* 2 b (* c c))
(* 2 (expt b 3))
(- (* 4 a (* b b))) (* 2 (* a a) b))))))
(y1 (- (/ (+ (* (sqrt (+ (* c c)
(* b b)
(- (* 2 a b))
(* a a)))
x1)
(- (* b x1)) (* a x1))
c)))
(x2 (- (/ c
(sqrt (+ (* (* c c)
(sqrt (+ (* c c)
(* b b)
(- (* 2 a b))
(* a a))))
(* 2 (* b b) (sqrt (+ (* c c)
(* b b)
(- (* 2 a b))
(* a a))))
(- (* 2 a b (sqrt (+ (* c c)
(* b b)
(- (* 2 a b))
(* a a)))))
(* 2 b (* c c))
(* 2 (expt b 3))
(- (* 4 a (* b b))) (* 2 (* a a) b))))))
(y2 (- (/ (+ (- (* (sqrt (+ (* c c)
(* b b)
(- (* 2 a b))
(* a a)))
x2))
(- (* b x2)) (* a x2))
c))))
(values x1 y1 x2 y2))))))
;;; ---- Intersection of Ellipse vs. Ellipse ---------------------------------------------
;; Das ganze ist so unverstaendlich, ich muss noch mal nach meinen Notizen
;; fanden, um die Herleitung der Loesung fuer das Schnittproblem praesentieren
;; zu koennen.
(defun intersection-ellipse/ellipse (e1 e2)
;; Eine der beiden Ellipsen fuehren wir zuerst auf den Einheitskreis zurueck.
(let ((a (invert-transformation (slot-value e1 'tr))))
(let ((r (intersection-ellipse/unit-circle (transform-region a e2))))
(if (atom r)
r
(mapcar (lambda (p)
(multiple-value-bind (x y) (transform-position (slot-value e1 'tr) (car p) (cdr p))
(make-point x y)))
r)))))
(defun intersection-ellipse/unit-circle (ell)
(multiple-value-bind (a b c d e f) (ellipse-coefficients ell)
(let ((pn (elli-polynom ell)))
(cond ((= (length pn) 0)
:coincident)
(t
(let ((ys (newton-iteration pn 0d0))
(res nil))
(dolist (y ys)
(let ((x (sqrt (- 1 (* y y)))))
(when (realp x)
(when (coordinate= 0 (ellipse-equation a b c d e f x y))
(pushnew (cons x y) res :test #'equal))
(when (coordinate= 0 (ellipse-equation a b c d e f (- x) y))
(pushnew (cons (- x) y) res :test #'equal)) )))
res)) ))))
(defun ellipse-equation (a b c d e f x y)
(+ (* a x x) (* b y y) (* c x y) (* d x) (* e y) f))
(defun elli-polynom (ell)
;; Was ganz lustig ist, ist dass wir bei Kreisen immer ein Polynom
;; vom Grade zwei bekommen.
(multiple-value-bind (a b c d e f) (ellipse-coefficients ell)
(canonize-polynom
(vector (+ (* (- b a) (- b a)) (* c c))
(+ (* 2 b e) (* -2 a e) (* 2 c d))
(+ (* e e) (* 2 (- b a) (+ a f)) (* -1 c c) (* d d))
(+ (* 2 e a) (* 2 e f) (* -2 c d))
(+ (* (+ a f) (+ a f)) (* -1 d d)) ))) )
;; Wir basteln uns mal eine einfache Newtoniteration. Manchmal
;; scheitern wir noch hoffungslos an lokalen Minima. Ansonsten ist das
;; Konvergenzverhalten fuer unsere Aufgabe schon ganz gut. Aber wir
;; handeln uns durch das Abdividieren der Nullstellen z.T. noch
;; beachtliche Fehler ein; ich versuche das zu mildern in dem ich nach
;; Finden einer Nullstell noch eine paar Newtonschritte mit dem
;; Original-Polynom mache (newton-ziel-gerade).
;; Ich sollte man nicht so faul sein und die reichhaltige Literatur zu
;; Rate ziehen tun; es muss auch etwas bessers als Newtoniteration
;; geben. Ich habe da noch so vage Erinnerungen an die
;; Numerik-Vorlesung ...
(defun newton-ziel-gerade (pn x &optional (n 4))
(cond ((= n 0) x)
((multiple-value-bind (f p2) (horner-schema pn x)
(multiple-value-bind (f*) (horner-schema p2 x)
(newton-ziel-gerade pn (- x (/ f f*)) (- n 1)))))))
(defun solve-p1 (b c)
(if (= b 0)
nil
(list (- (/ c b)))))
(defun solve-p2 (a b c)
(cond ((= a 0)
(solve-p1 b c))
(t
(let* ((p (/ b a))
(q (/ c a))
(d (- (/ (* p p) 4) q)))
(cond ((< d 0)
nil)
((= d 0)
(list (/ p 2)))
(t
(list (+ (/ p 2) (sqrt d))
(- (/ p 2) (sqrt d))))))) ))
(defun maybe-solve-polynom-trivially (pn)
(case (length pn)
(0 (values nil t))
(1 (values nil t))
(2 (values (solve-p1 (aref pn 0) (aref pn 1)) t))
(3 (values (solve-p2 (aref pn 0) (aref pn 1) (aref pn 2)) t))
(t (values nil nil))))
(defun canonize-polynom (pn)
(cond ((= (length pn) 0) pn)
((coordinate= (aref pn 0) 0)
(canonize-polynom (subseq pn 1)))
(t pn)))
(defun newton-iteration (polynom x-start)
;; ACHTUNG: Speziell auf unser problem angepasst, nicht ohne lesen uebernehmen!
(multiple-value-bind (sol done?) (maybe-solve-polynom-trivially polynom)
(cond (done?
sol)
(t
(let ((x x-start)
x1
(n 0)
(pn polynom)
(eps-f 0d0)
(eps-f* 0d-16)
(eps-x 1d-20)
(m 20) ;maximal zahl schritte
(res nil) )
(loop
(cond ((> n m)
(return)))
(multiple-value-bind (f p2) (horner-schema pn x)
(multiple-value-bind (f*) (horner-schema p2 x)
(cond ((<= (abs f*) eps-f*)
;; Wir haengen an einer Extremstelle fest -- mit zufaelligem Startwert weiter.
(setf x1 (+ 1d0 (random 2d0))))
(t
(setf x1 (- x (/ f f*)))
(cond ((or (<= (abs f) eps-f)
(<= (abs (- x1 x)) eps-x))
;; noch ein paar newton schritte, um das ergebnis zu verbessern
(setf x1 (newton-ziel-gerade polynom x1))
(push x1 res)
;; abdividieren
(multiple-value-bind (f p2) (horner-schema pn x1)
f
(setq pn (canonize-polynom p2))
(multiple-value-bind (sol done?) (maybe-solve-polynom-trivially pn)
(when done?
;; Hier trotzdem noch nachiterieren -- ist das eine gute Idee?
(setf sol (mapcar (lambda (x) (newton-ziel-gerade polynom x)) sol))
(setf res (nconc sol res))
(return))))
(setf x1 x-start)
(setq n 0)) ))))
(setf x (min 1d0 (max -1d0 x1))) ;Darf man das machen?
(incf n)))
res)) )))
(defun horner-schema (polynom x)
;; Wertet das polynom `polynom' mit Hilfe des Hornerschemas an der
;; Stelle `x' aus; Gibt zwei Werte zurueck: