-
Notifications
You must be signed in to change notification settings - Fork 40
/
art_svp_wind.c
1550 lines (1352 loc) · 39.9 KB
/
art_svp_wind.c
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
/* Libart_LGPL - library of basic graphic primitives
* Copyright (C) 1998-2000 Raph Levien
*
* 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.
*/
/* Primitive intersection and winding number operations on sorted
vector paths.
These routines are internal to libart, used to construct operations
like intersection, union, and difference. */
#include "config.h"
#include "art_svp_wind.h"
#include <stdio.h> /* for fprintf of debugging info */
#include <string.h> /* for memcpy */
#include <math.h>
#include "art_misc.h"
#include "art_rect.h"
#include "art_svp.h"
#ifdef NOSTDERR
# define STDERR stdout
#else
# define STDERR stderr
#endif
#define noVERBOSE
#define PT_EQ(p1,p2) ((p1).x == (p2).x && (p1).y == (p2).y)
#define PT_CLOSE(p1,p2) (fabs ((p1).x - (p2).x) < 1e-6 && fabs ((p1).y - (p2).y) < 1e-6)
/* return nonzero and set *p to the intersection point if the lines
z0-z1 and z2-z3 intersect each other. */
static int
intersect_lines (ArtPoint z0, ArtPoint z1, ArtPoint z2, ArtPoint z3,
ArtPoint *p)
{
double a01, b01, c01;
double a23, b23, c23;
double d0, d1, d2, d3;
double det;
/* if the vectors share an endpoint, they don't intersect */
if (PT_EQ (z0, z2) || PT_EQ (z0, z3) || PT_EQ (z1, z2) || PT_EQ (z1, z3))
return 0;
#if 0
if (PT_CLOSE (z0, z2) || PT_CLOSE (z0, z3) || PT_CLOSE (z1, z2) || PT_CLOSE (z1, z3))
return 0;
#endif
/* find line equations ax + by + c = 0 */
a01 = z0.y - z1.y;
b01 = z1.x - z0.x;
c01 = -(z0.x * a01 + z0.y * b01);
/* = -((z0.y - z1.y) * z0.x + (z1.x - z0.x) * z0.y)
= (z1.x * z0.y - z1.y * z0.x) */
d2 = a01 * z2.x + b01 * z2.y + c01;
d3 = a01 * z3.x + b01 * z3.y + c01;
if ((d2 > 0) == (d3 > 0))
return 0;
a23 = z2.y - z3.y;
b23 = z3.x - z2.x;
c23 = -(z2.x * a23 + z2.y * b23);
d0 = a23 * z0.x + b23 * z0.y + c23;
d1 = a23 * z1.x + b23 * z1.y + c23;
if ((d0 > 0) == (d1 > 0))
return 0;
/* now we definitely know that the lines intersect */
/* solve the two linear equations ax + by + c = 0 */
det = 1.0 / (a01 * b23 - a23 * b01);
p->x = det * (c23 * b01 - c01 * b23);
p->y = det * (c01 * a23 - c23 * a01);
return 1;
}
#define EPSILON 1e-6
static double
trap_epsilon (double v)
{
const double epsilon = EPSILON;
if (v < epsilon && v > -epsilon) return 0;
else return v;
}
/* Determine the order of line segments z0-z1 and z2-z3.
Return +1 if z2-z3 lies entirely to the right of z0-z1,
-1 if entirely to the left,
or 0 if overlap.
The case analysis in this function is quite ugly. The fact that it's
almost 200 lines long is ridiculous.
Ok, so here's the plan to cut it down:
First, do a bounding line comparison on the x coordinates. This is pretty
much the common case, and should go quickly. It also takes care of the
case where both lines are horizontal.
Then, do d0 and d1 computation, but only if a23 is nonzero.
Finally, do d2 and d3 computation, but only if a01 is nonzero.
Fall through to returning 0 (this will happen when both lines are
horizontal and they overlap).
*/
static int
x_order (ArtPoint z0, ArtPoint z1, ArtPoint z2, ArtPoint z3)
{
double a01, b01, c01;
double a23, b23, c23;
double d0, d1, d2, d3;
if (z0.y == z1.y)
{
if (z2.y == z3.y)
{
double x01min, x01max;
double x23min, x23max;
if (z0.x > z1.x)
{
x01min = z1.x;
x01max = z0.x;
}
else
{
x01min = z0.x;
x01max = z1.x;
}
if (z2.x > z3.x)
{
x23min = z3.x;
x23max = z2.x;
}
else
{
x23min = z2.x;
x23max = z3.x;
}
if (x23min >= x01max) return 1;
else if (x01min >= x23max) return -1;
else return 0;
}
else
{
/* z0-z1 is horizontal, z2-z3 isn't */
a23 = z2.y - z3.y;
b23 = z3.x - z2.x;
c23 = -(z2.x * a23 + z2.y * b23);
if (z3.y < z2.y)
{
a23 = -a23;
b23 = -b23;
c23 = -c23;
}
d0 = trap_epsilon (a23 * z0.x + b23 * z0.y + c23);
d1 = trap_epsilon (a23 * z1.x + b23 * z1.y + c23);
if (d0 > 0)
{
if (d1 >= 0) return 1;
else return 0;
}
else if (d0 == 0)
{
if (d1 > 0) return 1;
else if (d1 < 0) return -1;
else fprintf (STDERR,"case 1 degenerate\n");
return 0;
}
else /* d0 < 0 */
{
if (d1 <= 0) return -1;
else return 0;
}
}
}
else if (z2.y == z3.y)
{
/* z2-z3 is horizontal, z0-z1 isn't */
a01 = z0.y - z1.y;
b01 = z1.x - z0.x;
c01 = -(z0.x * a01 + z0.y * b01);
/* = -((z0.y - z1.y) * z0.x + (z1.x - z0.x) * z0.y)
= (z1.x * z0.y - z1.y * z0.x) */
if (z1.y < z0.y)
{
a01 = -a01;
b01 = -b01;
c01 = -c01;
}
d2 = trap_epsilon (a01 * z2.x + b01 * z2.y + c01);
d3 = trap_epsilon (a01 * z3.x + b01 * z3.y + c01);
if (d2 > 0)
{
if (d3 >= 0) return -1;
else return 0;
}
else if (d2 == 0)
{
if (d3 > 0) return -1;
else if (d3 < 0) return 1;
else fprintf (STDERR,"case 2 degenerate\n");
return 0;
}
else /* d2 < 0 */
{
if (d3 <= 0) return 1;
else return 0;
}
}
/* find line equations ax + by + c = 0 */
a01 = z0.y - z1.y;
b01 = z1.x - z0.x;
c01 = -(z0.x * a01 + z0.y * b01);
/* = -((z0.y - z1.y) * z0.x + (z1.x - z0.x) * z0.y)
= -(z1.x * z0.y - z1.y * z0.x) */
if (a01 > 0)
{
a01 = -a01;
b01 = -b01;
c01 = -c01;
}
/* so now, (a01, b01) points to the left, thus a01 * x + b01 * y + c01
is negative if the point lies to the right of the line */
d2 = trap_epsilon (a01 * z2.x + b01 * z2.y + c01);
d3 = trap_epsilon (a01 * z3.x + b01 * z3.y + c01);
if (d2 > 0)
{
if (d3 >= 0) return -1;
}
else if (d2 == 0)
{
if (d3 > 0) return -1;
else if (d3 < 0) return 1;
else
fprintf (STDERR, "colinear!\n");
}
else /* d2 < 0 */
{
if (d3 <= 0) return 1;
}
a23 = z2.y - z3.y;
b23 = z3.x - z2.x;
c23 = -(z2.x * a23 + z2.y * b23);
if (a23 > 0)
{
a23 = -a23;
b23 = -b23;
c23 = -c23;
}
d0 = trap_epsilon (a23 * z0.x + b23 * z0.y + c23);
d1 = trap_epsilon (a23 * z1.x + b23 * z1.y + c23);
if (d0 > 0)
{
if (d1 >= 0) return 1;
}
else if (d0 == 0)
{
if (d1 > 0) return 1;
else if (d1 < 0) return -1;
else
fprintf (STDERR, "colinear!\n");
}
else /* d0 < 0 */
{
if (d1 <= 0) return -1;
}
return 0;
}
/* similar to x_order, but to determine whether point z0 + epsilon lies to
the left of the line z2-z3 or to the right */
static int
x_order_2 (ArtPoint z0, ArtPoint z1, ArtPoint z2, ArtPoint z3)
{
double a23, b23, c23;
double d0, d1;
a23 = z2.y - z3.y;
b23 = z3.x - z2.x;
c23 = -(z2.x * a23 + z2.y * b23);
if (a23 > 0)
{
a23 = -a23;
b23 = -b23;
c23 = -c23;
}
d0 = a23 * z0.x + b23 * z0.y + c23;
if (d0 > EPSILON)
return -1;
else if (d0 < -EPSILON)
return 1;
d1 = a23 * z1.x + b23 * z1.y + c23;
if (d1 > EPSILON)
return -1;
else if (d1 < -EPSILON)
return 1;
if (z0.x == z1.x && z1.x == z2.x && z2.x == z3.x)
{
fprintf (STDERR, "x_order_2: colinear and horizontally aligned!\n");
return 0;
}
if (z0.x <= z2.x && z1.x <= z2.x && z0.x <= z3.x && z1.x <= z3.x)
return -1;
if (z0.x >= z2.x && z1.x >= z2.x && z0.x >= z3.x && z1.x >= z3.x)
return 1;
fprintf (STDERR, "x_order_2: colinear!\n");
return 0;
}
#ifdef DEAD_CODE
/* Traverse the vector path, keeping it in x-sorted order.
This routine doesn't actually do anything - it's just here for
explanatory purposes. */
void
traverse (ArtSVP *vp)
{
int *active_segs;
int n_active_segs;
int *cursor;
int seg_idx;
double y;
int tmp1, tmp2;
int asi;
int i, j;
active_segs = art_new (int, vp->n_segs);
cursor = art_new (int, vp->n_segs);
n_active_segs = 0;
seg_idx = 0;
y = vp->segs[0].points[0].y;
while (seg_idx < vp->n_segs || n_active_segs > 0)
{
fprintf (STDERR,"y = %g\n", y);
/* delete segments ending at y from active list */
for (i = 0; i < n_active_segs; i++)
{
asi = active_segs[i];
if (vp->segs[asi].n_points - 1 == cursor[asi] &&
vp->segs[asi].points[cursor[asi]].y == y)
{
fprintf (STDERR,"deleting %d\n", asi);
n_active_segs--;
for (j = i; j < n_active_segs; j++)
active_segs[j] = active_segs[j + 1];
i--;
}
}
/* insert new segments into the active list */
while (seg_idx < vp->n_segs && y == vp->segs[seg_idx].points[0].y)
{
cursor[seg_idx] = 0;
fprintf (STDERR,"inserting %d\n", seg_idx);
for (i = 0; i < n_active_segs; i++)
{
asi = active_segs[i];
if (x_order (vp->segs[asi].points[cursor[asi]],
vp->segs[asi].points[cursor[asi] + 1],
vp->segs[seg_idx].points[0],
vp->segs[seg_idx].points[1]) == -1)
break;
}
tmp1 = seg_idx;
for (j = i; j < n_active_segs; j++)
{
tmp2 = active_segs[j];
active_segs[j] = tmp1;
tmp1 = tmp2;
}
active_segs[n_active_segs] = tmp1;
n_active_segs++;
seg_idx++;
}
/* all active segs cross the y scanline (considering segs to be
closed on top and open on bottom) */
for (i = 0; i < n_active_segs; i++)
{
asi = active_segs[i];
fprintf (STDERR,"%d (%g, %g) - (%g, %g) %s\n", asi,
vp->segs[asi].points[cursor[asi]].x,
vp->segs[asi].points[cursor[asi]].y,
vp->segs[asi].points[cursor[asi] + 1].x,
vp->segs[asi].points[cursor[asi] + 1].y,
vp->segs[asi].dir ? "v" : "^");
}
/* advance y to the next event */
if (n_active_segs == 0)
{
if (seg_idx < vp->n_segs)
y = vp->segs[seg_idx].points[0].y;
/* else we're done */
}
else
{
asi = active_segs[0];
y = vp->segs[asi].points[cursor[asi] + 1].y;
for (i = 1; i < n_active_segs; i++)
{
asi = active_segs[i];
if (y > vp->segs[asi].points[cursor[asi] + 1].y)
y = vp->segs[asi].points[cursor[asi] + 1].y;
}
if (seg_idx < vp->n_segs && y > vp->segs[seg_idx].points[0].y)
y = vp->segs[seg_idx].points[0].y;
}
/* advance cursors to reach new y */
for (i = 0; i < n_active_segs; i++)
{
asi = active_segs[i];
while (cursor[asi] < vp->segs[asi].n_points - 1 &&
y >= vp->segs[asi].points[cursor[asi] + 1].y)
cursor[asi]++;
}
fprintf (STDERR,"\n");
}
art_free (cursor);
art_free (active_segs);
}
#endif
/* I believe that the loop will always break with i=1.
I think I'll want to change this from a simple sorted list to a
modified stack. ips[*][0] will get its own data structure, and
ips[*] will in general only be allocated if there is an intersection.
Finally, the segment can be traced through the initial point
(formerly ips[*][0]), backwards through the stack, and finally
to cursor + 1.
This change should cut down on allocation bandwidth, and also
eliminate the iteration through n_ipl below.
*/
static void
insert_ip (int seg_i, int *n_ips, int *n_ips_max, ArtPoint **ips, ArtPoint ip)
{
int i;
ArtPoint tmp1, tmp2;
int n_ipl;
ArtPoint *ipl;
n_ipl = n_ips[seg_i]++;
if (n_ipl == n_ips_max[seg_i])
art_expand (ips[seg_i], ArtPoint, n_ips_max[seg_i]);
ipl = ips[seg_i];
for (i = 1; i < n_ipl; i++)
if (ipl[i].y > ip.y)
break;
tmp1 = ip;
for (; i <= n_ipl; i++)
{
tmp2 = ipl[i];
ipl[i] = tmp1;
tmp1 = tmp2;
}
}
/* test active segment (i - 1) against i for intersection, if
so, add intersection point to both ips lists. */
static void
intersect_neighbors (int i, int *active_segs,
int *n_ips, int *n_ips_max, ArtPoint **ips,
int *cursor, ArtSVP *vp)
{
ArtPoint z0, z1, z2, z3;
int asi01, asi23;
ArtPoint ip;
asi01 = active_segs[i - 1];
z0 = ips[asi01][0];
if (n_ips[asi01] == 1)
z1 = vp->segs[asi01].points[cursor[asi01] + 1];
else
z1 = ips[asi01][1];
asi23 = active_segs[i];
z2 = ips[asi23][0];
if (n_ips[asi23] == 1)
z3 = vp->segs[asi23].points[cursor[asi23] + 1];
else
z3 = ips[asi23][1];
if (intersect_lines (z0, z1, z2, z3, &ip))
{
#ifdef VERBOSE
fprintf (STDERR,"new intersection point: (%g, %g)\n", ip.x, ip.y);
#endif
insert_ip (asi01, n_ips, n_ips_max, ips, ip);
insert_ip (asi23, n_ips, n_ips_max, ips, ip);
}
}
/* Add a new point to a segment in the svp.
Here, we also check to make sure that the segments satisfy nocross.
However, this is only valuable for debugging, and could possibly be
removed.
*/
static void
svp_add_point (ArtSVP *svp, int *n_points_max,
ArtPoint p, int *seg_map, int *active_segs, int n_active_segs,
int i)
{
int asi, asi_left, asi_right;
int n_points, n_points_left, n_points_right;
ArtSVPSeg *seg;
asi = seg_map[active_segs[i]];
seg = &svp->segs[asi];
n_points = seg->n_points;
/* find out whether neighboring segments share a point */
if (i > 0)
{
asi_left = seg_map[active_segs[i - 1]];
n_points_left = svp->segs[asi_left].n_points;
if (n_points_left > 1 &&
PT_EQ (svp->segs[asi_left].points[n_points_left - 2],
svp->segs[asi].points[n_points - 1]))
{
/* ok, new vector shares a top point with segment to the left -
now, check that it satisfies ordering invariant */
if (x_order (svp->segs[asi_left].points[n_points_left - 2],
svp->segs[asi_left].points[n_points_left - 1],
svp->segs[asi].points[n_points - 1],
p) < 1)
{
#ifdef VERBOSE
fprintf (STDERR,"svp_add_point: cross on left!\n");
#endif
}
}
}
if (i + 1 < n_active_segs)
{
asi_right = seg_map[active_segs[i + 1]];
n_points_right = svp->segs[asi_right].n_points;
if (n_points_right > 1 &&
PT_EQ (svp->segs[asi_right].points[n_points_right - 2],
svp->segs[asi].points[n_points - 1]))
{
/* ok, new vector shares a top point with segment to the right -
now, check that it satisfies ordering invariant */
if (x_order (svp->segs[asi_right].points[n_points_right - 2],
svp->segs[asi_right].points[n_points_right - 1],
svp->segs[asi].points[n_points - 1],
p) > -1)
{
#ifdef VERBOSE
fprintf (STDERR,"svp_add_point: cross on right!\n");
#endif
}
}
}
if (n_points_max[asi] == n_points)
art_expand (seg->points, ArtPoint, n_points_max[asi]);
seg->points[n_points] = p;
if (p.x < seg->bbox.x0)
seg->bbox.x0 = p.x;
else if (p.x > seg->bbox.x1)
seg->bbox.x1 = p.x;
seg->bbox.y1 = p.y;
seg->n_points++;
}
#if 0
/* find where the segment (currently at i) is supposed to go, and return
the target index - if equal to i, then there is no crossing problem.
"Where it is supposed to go" is defined as following:
Delete element i, re-insert at position target (bumping everything
target and greater to the right).
*/
static int
find_crossing (int i, int *active_segs, int n_active_segs,
int *cursor, ArtPoint **ips, int *n_ips, ArtSVP *vp)
{
int asi, asi_left, asi_right;
ArtPoint p0, p1;
ArtPoint p0l, p1l;
ArtPoint p0r, p1r;
int target;
asi = active_segs[i];
p0 = ips[asi][0];
if (n_ips[asi] == 1)
p1 = vp->segs[asi].points[cursor[asi] + 1];
else
p1 = ips[asi][1];
for (target = i; target > 0; target--)
{
asi_left = active_segs[target - 1];
p0l = ips[asi_left][0];
if (n_ips[asi_left] == 1)
p1l = vp->segs[asi_left].points[cursor[asi_left] + 1];
else
p1l = ips[asi_left][1];
if (!PT_EQ (p0, p0l))
break;
#ifdef VERBOSE
fprintf (STDERR,"point matches on left (%g, %g) - (%g, %g) x (%g, %g) - (%g, %g)!\n",
p0l.x, p0l.y, p1l.x, p1l.y, p0.x, p0.y, p1.x, p1.y);
#endif
if (x_order (p0l, p1l, p0, p1) == 1)
break;
#ifdef VERBOSE
fprintf (STDERR,"scanning to the left (i=%d, target=%d)\n", i, target);
#endif
}
if (target < i) return target;
for (; target < n_active_segs - 1; target++)
{
asi_right = active_segs[target + 1];
p0r = ips[asi_right][0];
if (n_ips[asi_right] == 1)
p1r = vp->segs[asi_right].points[cursor[asi_right] + 1];
else
p1r = ips[asi_right][1];
if (!PT_EQ (p0, p0r))
break;
#ifdef VERBOSE
fprintf (STDERR,"point matches on left (%g, %g) - (%g, %g) x (%g, %g) - (%g, %g)!\n",
p0.x, p0.y, p1.x, p1.y, p0r.x, p0r.y, p1r.x, p1r.y);
#endif
if (x_order (p0r, p1r, p0, p1) == 1)
break;
#ifdef VERBOSE
fprintf (STDERR,"scanning to the right (i=%d, target=%d)\n", i, target);
#endif
}
return target;
}
#endif
/* This routine handles the case where the segment changes its position
in the active segment list. Generally, this will happen when the
segment (defined by i and cursor) shares a top point with a neighbor,
but breaks the ordering invariant.
Essentially, this routine sorts the lines [start..end), all of which
share a top point. This is implemented as your basic insertion sort.
This routine takes care of intersecting the appropriate neighbors,
as well.
A first argument of -1 immediately returns, which helps reduce special
casing in the main unwind routine.
*/
static void
fix_crossing (int start, int end, int *active_segs, int n_active_segs,
int *cursor, ArtPoint **ips, int *n_ips, int *n_ips_max,
ArtSVP *vp, int *seg_map,
ArtSVP **p_new_vp, int *pn_segs_max,
int **pn_points_max)
{
int i, j;
int target;
int asi, asj;
ArtPoint p0i, p1i;
ArtPoint p0j, p1j;
int swap = 0;
#ifdef VERBOSE
int k;
#endif
ArtPoint *pts;
#ifdef VERBOSE
fprintf (STDERR,"fix_crossing: [%d..%d)", start, end);
for (k = 0; k < n_active_segs; k++)
fprintf (STDERR," %d", active_segs[k]);
fprintf (STDERR,"\n");
#endif
if (start == -1)
return;
for (i = start + 1; i < end; i++)
{
asi = active_segs[i];
if (cursor[asi] < vp->segs[asi].n_points - 1) {
p0i = ips[asi][0];
if (n_ips[asi] == 1)
p1i = vp->segs[asi].points[cursor[asi] + 1];
else
p1i = ips[asi][1];
for (j = i - 1; j >= start; j--)
{
asj = active_segs[j];
if (cursor[asj] < vp->segs[asj].n_points - 1)
{
p0j = ips[asj][0];
if (n_ips[asj] == 1)
p1j = vp->segs[asj].points[cursor[asj] + 1];
else
p1j = ips[asj][1];
/* we _hope_ p0i = p0j */
if (x_order_2 (p0j, p1j, p0i, p1i) == -1)
break;
}
}
target = j + 1;
/* target is where active_seg[i] _should_ be in active_segs */
if (target != i)
{
swap = 1;
#ifdef VERBOSE
fprintf (STDERR,"fix_crossing: at %i should be %i\n", i, target);
#endif
/* let's close off all relevant segments */
for (j = i; j >= target; j--)
{
asi = active_segs[j];
/* First conjunct: this isn't the last point in the original
segment.
Second conjunct: this isn't the first point in the new
segment (i.e. already broken).
*/
if (cursor[asi] < vp->segs[asi].n_points - 1 &&
(*p_new_vp)->segs[seg_map[asi]].n_points != 1)
{
int seg_num;
/* so break here */
#ifdef VERBOSE
fprintf (STDERR,"closing off %d\n", j);
#endif
pts = art_new (ArtPoint, 16);
pts[0] = ips[asi][0];
seg_num = art_svp_add_segment (p_new_vp, pn_segs_max,
pn_points_max,
1, vp->segs[asi].dir,
pts,
NULL);
(*pn_points_max)[seg_num] = 16;
seg_map[asi] = seg_num;
}
}
/* now fix the ordering in active_segs */
asi = active_segs[i];
for (j = i; j > target; j--)
active_segs[j] = active_segs[j - 1];
active_segs[j] = asi;
}
}
}
if (swap && start > 0)
{
int as_start;
as_start = active_segs[start];
if (cursor[as_start] < vp->segs[as_start].n_points)
{
#ifdef VERBOSE
fprintf (STDERR,"checking intersection of %d, %d\n", start - 1, start);
#endif
intersect_neighbors (start, active_segs,
n_ips, n_ips_max, ips,
cursor, vp);
}
}
if (swap && end < n_active_segs)
{
int as_end;
as_end = active_segs[end - 1];
if (cursor[as_end] < vp->segs[as_end].n_points)
{
#ifdef VERBOSE
fprintf (STDERR,"checking intersection of %d, %d\n", end - 1, end);
#endif
intersect_neighbors (end, active_segs,
n_ips, n_ips_max, ips,
cursor, vp);
}
}
if (swap)
{
#ifdef VERBOSE
fprintf (STDERR,"fix_crossing return: [%d..%d)", start, end);
for (k = 0; k < n_active_segs; k++)
fprintf (STDERR," %d", active_segs[k]);
fprintf (STDERR,"\n");
#endif
}
}
/* Return a new sorted vector that covers the same area as the
argument, but which satisfies the nocross invariant.
Basically, this routine works by finding the intersection points,
and cutting the segments at those points.
Status of this routine:
Basic correctness: Seems ok.
Numerical stability: known problems in the case of points falling
on lines, and colinear lines. For actual use, randomly perturbing
the vertices is currently recommended.
Speed: pretty good, although a more efficient priority queue, as
well as bbox culling of potential intersections, are two
optimizations that could help.
Precision: pretty good, although the numerical stability problems
make this routine unsuitable for precise calculations of
differences.
*/
/* Here is a more detailed description of the algorithm. It follows
roughly the structure of traverse (above), but is obviously quite
a bit more complex.
Here are a few important data structures:
A new sorted vector path (new_svp).
For each (active) segment in the original, a list of intersection
points.
Of course, the original being traversed.
The following invariants hold (in addition to the invariants
of the traverse procedure).
The new sorted vector path lies entirely above the y scan line.
The new sorted vector path keeps the nocross invariant.
For each active segment, the y scan line crosses the line from the
first to the second of the intersection points (where the second
point is cursor + 1 if there is only one intersection point).
The list of intersection points + the (cursor + 1) point is kept
in nondecreasing y order.
Of the active segments, none of the lines from first to second
intersection point cross the 1st ip..2nd ip line of the left or
right neighbor. (However, such a line may cross further
intersection points of the neighbors, or segments past the
immediate neighbors).
Of the active segments, all lines from 1st ip..2nd ip are in
strictly increasing x_order (this is very similar to the invariant
of the traverse procedure, but is explicitly stated here in terms
of ips). (this basically says that nocross holds on the active
segments)
The combination of the new sorted vector path, the path through all
the intersection points to cursor + 1, and [cursor + 1, n_points)
covers the same area as the argument.
Another important data structure is mapping from original segment
number to new segment number.
The algorithm is perhaps best understood as advancing the cursors
while maintaining these invariants. Here's roughly how it's done.
When deleting segments from the active list, those segments are added
to the new sorted vector path. In addition, the neighbors may intersect
each other, so they are intersection tested (see below).
When inserting new segments, they are intersection tested against
their neighbors. The top point of the segment becomes the first
intersection point.
Advancing the cursor is just a bit different from the traverse
routine, as the cursor may advance through the intersection points
as well. Only when there is a single intersection point in the list
does the cursor advance in the original segment. In either case,
the new vector is intersection tested against both neighbors. It
also causes the vector over which the cursor is advancing to be
added to the new svp.
Two steps need further clarification:
Intersection testing: the 1st ip..2nd ip lines of the neighbors
are tested to see if they cross (using intersect_lines). If so,
then the intersection point is added to the ip list of both
segments, maintaining the invariant that the list of intersection
points is nondecreasing in y).
Adding vector to new svp: if the new vector shares a top x
coordinate with another vector, then it is checked to see whether
it is in order. If not, then both segments are "broken," and then
restarted. Note: in the case when both segments are in the same
order, they may simply be swapped without breaking.
For the time being, I'm going to put some of these operations into
subroutines. If it turns out to be a performance problem, I could
try to reorganize the traverse procedure so that each is only
called once, and inline them. But if it's not a performance
problem, I'll just keep it this way, because it will probably help
to make the code clearer, and I believe this code could use all the
clarity it can get. */
/**
* art_svp_uncross: Resolve self-intersections of an svp.
* @vp: The original svp.
*
* Finds all the intersections within @vp, and constructs a new svp
* with new points added at these intersections.
*
* This routine needs to be redone from scratch with numerical robustness
* in mind. I'm working on it.
*
* Return value: The new svp.
**/
ArtSVP *
art_svp_uncross (ArtSVP *vp)
{
int *active_segs;
int n_active_segs;
int *cursor;
int seg_idx;
double y;
int tmp1, tmp2;
int asi;
int i, j;
/* new data structures */
/* intersection points; invariant: *ips[i] is only allocated if
i is active */
int *n_ips, *n_ips_max;
ArtPoint **ips;
/* new sorted vector path */
int n_segs_max, seg_num;
ArtSVP *new_vp;