File: intersect2.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (1572 lines) | stat: -rw-r--r-- 49,156 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
/*!
   \file lib/vector/Vlib/intersect2.c

   \brief Vector library - intersection

   Higher level functions for reading/writing/manipulating vectors.

   Some parts of code taken from grass50 v.spag/linecros.c

   Based on the following:

   <code>
   (ax2-ax1)r1 - (bx2-bx1)r2 = ax2 - ax1
   (ay2-ay1)r1 - (by2-by1)r2 = ay2 - ay1
   </code>

   Solving for r1 and r2, if r1 and r2 are between 0 and 1, then line
   segments (ax1,ay1)(ax2,ay2) and (bx1,by1)(bx2,by2) intersect.

   Intersect 2 line segments.

   Returns: 0 - do not intersect
   1 - intersect at one point
   <pre>
   \  /    \  /  \  /
   \/      \/    \/
   /\             \
   /  \             \
   2 - partial overlap         ( \/                      )
   ------      a          (    distance < threshold )
   ------   b          (                         )
   3 - a contains b            ( /\                      )
   ----------  a    ----------- a
   ----     b          ----- b
   4 - b contains a
   ----     a          ----- a
   ----------  b    ----------- b
   5 - identical
   ----------  a
   ----------  b
   </pre>
   Intersection points:
   <pre>
   return   point1 breaks: point2 breaks:     distance1 on:  distance2 on:
   0        -              -                  -              -
   1        a,b            -                  a              b
   2        a              b                  a              b
   3        a              a                  a              a
   4        b              b                  b              b
   5        -              -                  -              -
   </pre>
   Sometimes (often) is important to get the same coordinates for a x
   b and b x a.  To reach this, the segments a,b are 'sorted' at the
   beginning, so that for the same switched segments, results are
   identical. (reason is that double values are always rounded because
   of limited number of decimal places and for different order of
   coordinates, the results would be different)

   (C) 2001-2014, 2022 by the GRASS Development Team

   This program is free software under the GNU General Public License
   (>=v2).  Read the file COPYING that comes with GRASS for details.

   \author Original author CERL, probably Dave Gerdes or Mike Higgins.
   \author Update to GRASS 5.7 Radim Blazek.
   \author Update to GRASS 7 Markus Metz.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <grass/vector.h>
#include <grass/rbtree.h>
#include <grass/glocale.h>

/* function prototypes */
static int cmp_cross(const void *pa, const void *pb);
static void add_cross(int asegment, double adistance, int bsegment,
                      double bdistance, double x, double y);
static double dist2(double x1, double y1, double x2, double y2);

static int debug_level = -1;

#if 0
static int ident(double x1, double y1, double x2, double y2, double thresh);
#endif
static int snap_cross(int asegment, double *adistance, int bsegment,
                      double *bdistance, double *xc, double *yc);
static int cross_seg(int i, int j, int b);
static int find_cross(int i, int j, int b);
int line_check_intersection2(struct line_pnts *APoints,
                             struct line_pnts *BPoints, int with_z, int all);

typedef struct {    /* in arrays 0 - A line , 1 - B line */
    int segment[2]; /* segment number, start from 0 for first */
    double distance[2];
    double x, y, z;
} CROSS;

/* Current line in arrays is for some functions like cmp() set by: */
static int current;
static int second; /* line which is not current */

static int a_cross = 0;
static int n_cross;
static CROSS *cross = NULL;
static int *use_cross = NULL;

/* static double rethresh = 0.000001; */ /* TODO */

static double d_ulp(double a, double b)
{
    double fa = fabs(a);
    double fb = fabs(b);
    double dmax, result;
    int exp;

    dmax = fa;
    if (dmax < fb)
        dmax = fb;

    /* unit in the last place (ULP):
     * smallest representable difference
     * shift of the exponent
     * float: 23, double: 52, middle: 37.5 */
    result = frexp(dmax, &exp);
    exp -= 38;
    result = ldexp(result, exp);

    return result;
}

static void add_cross(int asegment, double adistance, int bsegment,
                      double bdistance, double x, double y)
{
    if (n_cross == a_cross) {
        /* Must be space + 1, used later for last line point, do it better */
        cross =
            (CROSS *)G_realloc((void *)cross, (a_cross + 101) * sizeof(CROSS));
        use_cross =
            (int *)G_realloc((void *)use_cross, (a_cross + 101) * sizeof(int));
        a_cross += 100;
    }

    G_debug(
        5,
        "  add new cross: aseg/dist = %d/%f bseg/dist = %d/%f, x = %f y = %f",
        asegment, adistance, bsegment, bdistance, x, y);
    cross[n_cross].segment[0] = asegment;
    cross[n_cross].distance[0] = adistance;
    cross[n_cross].segment[1] = bsegment;
    cross[n_cross].distance[1] = bdistance;
    cross[n_cross].x = x;
    cross[n_cross].y = y;
    n_cross++;
}

static int cmp_cross(const void *pa, const void *pb)
{
    CROSS *p1 = (CROSS *)pa;
    CROSS *p2 = (CROSS *)pb;

    if (p1->segment[current] < p2->segment[current])
        return -1;
    if (p1->segment[current] > p2->segment[current])
        return 1;
    /* the same segment */
    if (p1->distance[current] < p2->distance[current])
        return -1;
    if (p1->distance[current] > p2->distance[current])
        return 1;
    return 0;
}

static double dist2(double x1, double y1, double x2, double y2)
{
    double dx, dy;

    dx = x2 - x1;
    dy = y2 - y1;
    return (dx * dx + dy * dy);
}

#if 0
/* returns 1 if points are identical */
static int ident(double x1, double y1, double x2, double y2, double thresh)
{
    double dx, dy;

    dx = x2 - x1;
    dy = y2 - y1;
    if ((dx * dx + dy * dy) <= thresh * thresh)
        return 1;

    return 0;
}
#endif

/* shared by Vect_line_intersection, Vect_line_check_intersection, cross_seg,
 * find_cross */
static struct line_pnts *APnts, *BPnts, *ABPnts[2], *IPnts;

/* Snap breaks to nearest vertices within RE threshold */
/* Calculate distances along segments */
static int snap_cross(int asegment, double *adistance, int bsegment,
                      double *bdistance, double *xc, double *yc)
{
    int seg;
    double x, y;
    double dist, curdist, dthresh;

    /* 1. of A seg */
    seg = asegment;
    curdist = dist2(*xc, *yc, APnts->x[seg], APnts->y[seg]);
    x = APnts->x[seg];
    y = APnts->y[seg];

    *adistance = curdist;

    /* 2. of A seg */
    dist = dist2(*xc, *yc, APnts->x[seg + 1], APnts->y[seg + 1]);
    if (dist < curdist) {
        curdist = dist;
        x = APnts->x[seg + 1];
        y = APnts->y[seg + 1];
    }

    /* 1. of B seg */
    seg = bsegment;
    dist = dist2(*xc, *yc, BPnts->x[seg], BPnts->y[seg]);
    *bdistance = dist;

    if (dist < curdist) {
        curdist = dist;
        x = BPnts->x[seg];
        y = BPnts->y[seg];
    }
    /* 2. of B seg */
    dist = dist2(*xc, *yc, BPnts->x[seg + 1], BPnts->y[seg + 1]);
    if (dist < curdist) {
        curdist = dist;
        x = BPnts->x[seg + 1];
        y = BPnts->y[seg + 1];
    }

    /* the threshold should not be too small, otherwise we get
     * too many tiny new segments
     * the threshold should not be too large, otherwise we might
     * introduce new crossings
     * the smallest difference representable with
     * single precision floating point works well with pathological input
     * regular input is not affected */
    dthresh = d_ulp(x, y);
    if (curdist < dthresh * dthresh) { /* was rethresh * rethresh */
        *xc = x;
        *yc = y;

        /* Update distances along segments */
        seg = asegment;
        *adistance = dist2(*xc, *yc, APnts->x[seg], APnts->y[seg]);
        seg = bsegment;
        *bdistance = dist2(*xc, *yc, BPnts->x[seg], BPnts->y[seg]);

        return 1;
    }

    return 0;
}

/* break segments */
static int cross_seg(int i, int j, int b)
{
    double x1, y1, z1, x2, y2, z2;
    double y1min, y1max, y2min, y2max;
    double adist, bdist;
    int ret;

    y1min = APnts->y[i];
    y1max = APnts->y[i + 1];
    if (APnts->y[i] > APnts->y[i + 1]) {
        y1min = APnts->y[i + 1];
        y1max = APnts->y[i];
    }

    y2min = BPnts->y[j];
    y2max = BPnts->y[j + 1];
    if (BPnts->y[j] > BPnts->y[j + 1]) {
        y2min = BPnts->y[j + 1];
        y2max = BPnts->y[j];
    }

    if (y1min > y2max || y1max < y2min)
        return 0;

    if (b) {
        ret = Vect_segment_intersection(
            APnts->x[i], APnts->y[i], APnts->z[i], APnts->x[i + 1],
            APnts->y[i + 1], APnts->z[i + 1], BPnts->x[j], BPnts->y[j],
            BPnts->z[j], BPnts->x[j + 1], BPnts->y[j + 1], BPnts->z[j + 1], &x1,
            &y1, &z1, &x2, &y2, &z2, 0);
    }
    else {
        ret = Vect_segment_intersection(
            BPnts->x[j], BPnts->y[j], BPnts->z[j], BPnts->x[j + 1],
            BPnts->y[j + 1], BPnts->z[j + 1], APnts->x[i], APnts->y[i],
            APnts->z[i], APnts->x[i + 1], APnts->y[i + 1], APnts->z[i + 1], &x1,
            &y1, &z1, &x2, &y2, &z2, 0);
    }

    /* add ALL (including end points and duplicates), clean later */
    if (ret > 0) {
        G_debug(2, "  -> %d x %d: intersection type = %d", i, j, ret);
        if (ret == 1) { /* one intersection on segment A */
            G_debug(3, "    in %f, %f ", x1, y1);
            /* snap intersection only once */
            snap_cross(i, &adist, j, &bdist, &x1, &y1);
            add_cross(i, adist, j, bdist, x1, y1);
            if (APnts == BPnts)
                add_cross(j, bdist, i, adist, x1, y1);
        }
        else if (ret == 2 || ret == 3 || ret == 4 || ret == 5) {
            /*  partial overlap; a broken in one, b broken in one
             *  or a contains b; a is broken in 2 points (but 1 may be end)
             *  or b contains a; b is broken in 2 points (but 1 may be end)
             *  or identical */
            G_debug(3, "    in %f, %f; %f, %f", x1, y1, x2, y2);
            snap_cross(i, &adist, j, &bdist, &x1, &y1);
            add_cross(i, adist, j, bdist, x1, y1);
            if (APnts == BPnts)
                add_cross(j, bdist, i, adist, x1, y1);
            snap_cross(i, &adist, j, &bdist, &x2, &y2);
            add_cross(i, adist, j, bdist, x2, y2);
            if (APnts == BPnts)
                add_cross(j, bdist, i, adist, x2, y2);
        }
    }
    return 1; /* keep going */
}

/* event queue for Bentley-Ottmann */
#define QEVT_IN          1
#define QEVT_OUT         2
#define QEVT_CRS         3

#define GET_PARENT(p, c) ((p) = (int)(((c)-2) / 3 + 1))
#define GET_CHILD(c, p)  ((c) = (int)(((p)*3) - 1))

struct qitem {
    int l; /* line 0 - A line , 1 - B line */
    int s; /* segment index */
    int p; /* point index */
    int e; /* event type */
};

struct boq {
    int count;
    int alloc;
    struct qitem *i;
};

/* compare two queue points */
/* return 1 if a < b else 0 */
static int cmp_q_x(struct qitem *a, struct qitem *b)
{
    double x1, y1, z1, x2, y2, z2;

    x1 = ABPnts[a->l]->x[a->p];
    y1 = ABPnts[a->l]->y[a->p];
    z1 = ABPnts[a->l]->z[a->p];

    x2 = ABPnts[b->l]->x[b->p];
    y2 = ABPnts[b->l]->y[b->p];
    z2 = ABPnts[b->l]->z[b->p];

    if (x1 < x2)
        return 1;
    if (x1 > x2)
        return 0;

    if (y1 < y2)
        return 1;
    if (y1 > y2)
        return 0;

    if (z1 < z2)
        return 1;
    if (z1 > z2)
        return 0;

    if (a->e < b->e)
        return 1;

    if (a->l < b->l)
        return 1;

    if (a->s < b->s)
        return 1;

    return 0;
}

/* sift up routine for min heap */
static int sift_up(struct boq *q, int start)
{
    register int parent, child;
    struct qitem a, *b;

    child = start;
    a = q->i[child];

    while (child > 1) {
        GET_PARENT(parent, child);

        b = &q->i[parent];
        /* child smaller */
        if (cmp_q_x(&a, b)) {
            /* push parent point down */
            q->i[child] = q->i[parent];
            child = parent;
        }
        else
            /* no more sifting up, found new slot for child */
            break;
    }

    /* put point in new slot */
    if (child < start) {
        q->i[child] = a;
    }

    return child;
}

static int boq_add(struct boq *q, struct qitem *i)
{
    if (q->count + 2 >= q->alloc) {
        q->alloc = q->count + 100;
        q->i = G_realloc(q->i, q->alloc * sizeof(struct qitem));
    }
    q->i[q->count + 1] = *i;
    sift_up(q, q->count + 1);

    q->count++;

    return 1;
}

/* drop point routine for min heap */
static int boq_drop(struct boq *q, struct qitem *qi)
{
    register int child, childr, parent;
    register int i;
    struct qitem *a, *b;

    if (q->count == 0)
        return 0;

    *qi = q->i[1];

    if (q->count == 1) {
        q->count = 0;
        return 1;
    }

    /* start with root */
    parent = 1;

    /* sift down: move hole back towards bottom of heap */

    while (GET_CHILD(child, parent) <= q->count) {
        a = &q->i[child];
        i = child + 3;
        for (childr = child + 1; childr <= q->count && childr < i; childr++) {
            b = &q->i[childr];
            if (cmp_q_x(b, a)) {
                child = childr;
                a = &q->i[child];
            }
        }

        /* move hole down */
        q->i[parent] = q->i[child];
        parent = child;
    }

    /* hole is in lowest layer, move to heap end */
    if (parent < q->count) {
        q->i[parent] = q->i[q->count];

        /* sift up last swapped point, only necessary if hole moved to heap end
         */
        sift_up(q, parent);
    }

    /* the actual drop */
    q->count--;

    return 1;
}

/* compare two tree points */
/* return -1 if a < b, 1 if a > b, 0 if a == b */
static int cmp_t_y(const void *aa, const void *bb)
{
    double x1, y1, z1, x2, y2, z2;
    struct qitem *a = (struct qitem *)aa;
    struct qitem *b = (struct qitem *)bb;

    x1 = ABPnts[a->l]->x[a->p];
    y1 = ABPnts[a->l]->y[a->p];
    z1 = ABPnts[a->l]->z[a->p];

    x2 = ABPnts[b->l]->x[b->p];
    y2 = ABPnts[b->l]->y[b->p];
    z2 = ABPnts[b->l]->z[b->p];

    if (y1 < y2)
        return -1;
    if (y1 > y2)
        return 1;

    if (x1 < x2)
        return -1;
    if (x1 > x2)
        return 1;

    if (z1 < z2)
        return -1;
    if (z1 > z2)
        return 1;

    if (a->s < b->s)
        return -1;
    if (a->s > b->s)
        return 1;

    return 0;
}

static int boq_load(struct boq *q, struct line_pnts *Pnts,
                    struct bound_box *abbox, int l, int with_z)
{
    int i, loaded;
    int vi, vo;
    double x1, y1, z1, x2, y2, z2;
    struct bound_box box;
    struct qitem qi;

    /* load Pnts to queue */
    qi.l = l;
    loaded = 0;

    for (i = 0; i < Pnts->n_points - 1; i++) {
        x1 = Pnts->x[i];
        y1 = Pnts->y[i];
        z1 = Pnts->z[i];

        x2 = Pnts->x[i + 1];
        y2 = Pnts->y[i + 1];
        z2 = Pnts->z[i + 1];

        if (x1 == x2 && y1 == y2 && (!with_z || z1 == z2))
            continue;

        if (x1 < x2) {
            box.W = x1;
            box.E = x2;
        }
        else {
            box.E = x1;
            box.W = x2;
        }
        if (y1 < y2) {
            box.S = y1;
            box.N = y2;
        }
        else {
            box.N = y1;
            box.S = y2;
        }
        if (z1 < z2) {
            box.B = z1;
            box.T = z2;
        }
        else {
            box.T = z1;
            box.B = z2;
        }
        box.W -= d_ulp(box.W, box.W);
        box.S -= d_ulp(box.S, box.S);
        box.B -= d_ulp(box.B, box.B);
        box.E += d_ulp(box.E, box.E);
        box.N += d_ulp(box.N, box.N);
        box.T += d_ulp(box.T, box.T);

        if (!Vect_box_overlap(abbox, &box))
            continue;

        vi = i;
        vo = i + 1;

        if (x1 < x2) {
            vi = i;
            vo = i + 1;
        }
        else if (x1 > x2) {
            vi = i + 1;
            vo = i;
        }
        else {
            if (y1 < y2) {
                vi = i;
                vo = i + 1;
            }
            else if (y1 > y2) {
                vi = i + 1;
                vo = i;
            }
            else {
                if (z1 < z2) {
                    vi = i;
                    vo = i + 1;
                }
                else if (z1 > z2) {
                    vi = i + 1;
                    vo = i;
                }
                else {
                    G_fatal_error("Identical points");
                }
            }
        }

        qi.s = i;

        /* event in */
        qi.e = QEVT_IN;
        qi.p = vi;
        boq_add(q, &qi);

        /* event out */
        qi.e = QEVT_OUT;
        qi.p = vo;
        boq_add(q, &qi);

        loaded += 2;
    }

    return loaded;
}

/*!
 * \brief Intersect 2 lines.
 *
 * Creates array of new lines created from original A line, by
 * intersection with B line. Points (Points->n_points == 1) are not
 * supported. If B line is NULL, A line is intersected with itself.
 *
 * simplified Bentley–Ottmann Algorithm:
 * similar to Vect_line_intersection(), but faster
 * additionally, self-intersections of a line are handled more efficiently
 *
 * \param APoints first input line
 * \param BPoints second input line or NULL
 * \param[out] ALines array of new lines created from original A line
 * \param[out] BLines array of new lines created from original B line
 * \param[out] nalines number of new lines (ALines)
 * \param[out] nblines number of new lines (BLines)
 * \param with_z 3D, not supported!
 *
 * \return 0 no intersection
 * \return 1 intersection found
 */
int Vect_line_intersection2(struct line_pnts *APoints,
                            struct line_pnts *BPoints, struct bound_box *pABox,
                            struct bound_box *pBBox, struct line_pnts ***ALines,
                            struct line_pnts ***BLines, int *nalines,
                            int *nblines, int with_z)
{
    int i, j, k, l, nl, last_seg, seg, last;
    int n_alive_cross;
    double dist, last_x, last_y, last_z;
    struct line_pnts **XLines, *Points;
    struct line_pnts *Points1, *Points2; /* first, second points */
    int seg1, seg2, vert1, vert2;
    struct bound_box ABox, BBox, abbox;
    struct boq bo_queue;
    struct qitem qi, *found;
    struct RB_TREE *bo_ta, *bo_tb;
    struct RB_TRAV bo_t_trav;
    int same = 0;

    if (debug_level == -1) {
        const char *dstr = G_getenv_nofatal("DEBUG");

        if (dstr != NULL)
            debug_level = atoi(dstr);
        else
            debug_level = 0;
    }

    n_cross = 0;
    APnts = APoints;
    BPnts = BPoints;

    same = 0;
    if (!BPoints) {
        BPnts = APoints;
        same = 1;
    }

    ABPnts[0] = APnts;
    ABPnts[1] = BPnts;

    *nalines = 0;
    *nblines = 0;

    /* RE (representation error).
     *  RE thresh above is nonsense of course, the RE threshold should be based
     * on number of significant digits for double (IEEE-754) which is 15 or 16
     * and exponent. The number above is in fact not required threshold, and
     * will not work for example: equator length is 40.075,695 km (8 digits),
     * units are m (+3) and we want precision in mm (+ 3) = 14 -> minimum
     * rethresh may be around 0.001 ?Maybe all nonsense? Use rounding error of
     * the unit in the last place ? max of fabs(x), fabs(y) rethresh = pow(2,
     * log2(max) - 53) */

    /* Warning: This function is also used to intersect the line by itself i.e.
     * APoints and BPoints are identical. I am not sure if it is clever, but it
     * seems to work, but we have to keep this in mind and handle some special
     * cases (maybe) */

    /* TODO: 3D, RE threshold, GV_POINTS (line x point) */

    /* Take each segment from A and intersect by each segment from B.
     *
     *  All intersections are found first and saved to array, then sorted by a
     * distance along the line, and then the line is split to pieces.
     *
     *  Note: If segments are collinear, check if previous/next segments are
     * also collinear, in that case do not break:
     *  +----------+
     *  +----+-----+  etc.
     *  doesn't need to be broken
     *
     *  Note: If 2 adjacent segments of line B have common vertex exactly (or
     * within thresh) on line A, intersection points for these B segments may
     * differ due to RE:
     *  ------------ a       ----+--+----            ----+--+----
     *      /\         =>       /    \     or maybe       \/
     *  b0 /  \ b1             /      \      even:        /\
     *
     *  -> solution: snap all breaks to nearest vertices first within RE
     * threshold
     *
     *  Question: Snap all breaks to each other within RE threshold?
     *
     *  Note: If a break is snapped to end point or two breaks are snapped to
     * the same vertex resulting new line is degenerated => before line is added
     * to array, it must be checked if line is not degenerated
     *
     *  Note: to snap to vertices is important for cases where line A is broken
     * by B and C line at the same point: \  /  b   no snap     \    /
     *    \/       could    ----+--+----
     *  ------ a   result
     *    /\       in ?:         /\
     *   /  \  c                /  \
     *
     *  Note: once we snap breaks to vertices, we have to do that for both lines
     * A and B in the same way and because we cannot be sure that A children
     * will not change a bit by break(s) we have to break both A and B  at once
     * i.e. in one Vect_line_intersection () call.
     */

    /* don't modify original bboxes: make a copy of the bboxes */
    ABox = *pABox;
    BBox = *pBBox;
    if (!with_z) {
        ABox.T = BBox.T = PORT_DOUBLE_MAX;
        ABox.B = BBox.B = -PORT_DOUBLE_MAX;
    }

    if (!same && !Vect_box_overlap(&ABox, &BBox)) {
        return 0;
    }

    /* overlap box of A line and B line */
    abbox = BBox;
    if (!same) {
        if (abbox.N > ABox.N)
            abbox.N = ABox.N;
        if (abbox.S < ABox.S)
            abbox.S = ABox.S;
        if (abbox.E > ABox.E)
            abbox.E = ABox.E;
        if (abbox.W < ABox.W)
            abbox.W = ABox.W;

        if (with_z) {
            if (abbox.T > BBox.T)
                abbox.T = BBox.T;
            if (abbox.B < BBox.B)
                abbox.B = BBox.B;
        }
    }

    abbox.N += d_ulp(abbox.N, abbox.N);
    abbox.S -= d_ulp(abbox.S, abbox.S);
    abbox.E += d_ulp(abbox.E, abbox.E);
    abbox.W -= d_ulp(abbox.W, abbox.W);
    if (with_z) {
        abbox.T += d_ulp(abbox.T, abbox.T);
        abbox.B -= d_ulp(abbox.B, abbox.B);
    }

    if (APnts->n_points < 2 || BPnts->n_points < 2) {
        G_fatal_error("Intersection with points is not yet supported");
        return 0;
    }

    /* initialize queue */
    bo_queue.count = 0;
    bo_queue.alloc = 2 * (APnts->n_points + BPnts->n_points);
    bo_queue.i = G_malloc(bo_queue.alloc * sizeof(struct qitem));

    /* load APnts to queue */
    boq_load(&bo_queue, APnts, &abbox, 0, with_z);

    if (!same) {
        /* load BPnts to queue */
        boq_load(&bo_queue, BPnts, &abbox, 1, with_z);
    }

    /* initialize search tree */
    bo_ta = rbtree_create(cmp_t_y, sizeof(struct qitem));
    if (same)
        bo_tb = bo_ta;
    else
        bo_tb = rbtree_create(cmp_t_y, sizeof(struct qitem));

    /* find intersections */
    while (boq_drop(&bo_queue, &qi)) {

        if (qi.e == QEVT_IN) {
            /* not the original Bentley-Ottmann algorithm */
            if (qi.l == 0) {
                /* test for intersection of s with all segments in T */
                rbtree_init_trav(&bo_t_trav, bo_tb);
                while ((found = rbtree_traverse(&bo_t_trav))) {
                    cross_seg(qi.s, found->s, 0);
                }

                /* insert s in T */
                rbtree_insert(bo_ta, &qi);
            }
            else {
                /* test for intersection of s with all segments in T */
                rbtree_init_trav(&bo_t_trav, bo_ta);
                while ((found = rbtree_traverse(&bo_t_trav))) {
                    cross_seg(found->s, qi.s, 1);
                }

                /* insert s in T */
                rbtree_insert(bo_tb, &qi);
            }
        }
        else if (qi.e == QEVT_OUT) {
            /* remove from T */

            /* adjust p */
            if (qi.p == qi.s)
                qi.p++;
            else
                qi.p--;

            if (qi.l == 0) {
                if (!rbtree_remove(bo_ta, &qi))
                    G_fatal_error("RB tree error!");
            }
            else {
                if (!rbtree_remove(bo_tb, &qi))
                    G_fatal_error("RB tree error!");
            }
        }
    }
    G_free(bo_queue.i);
    rbtree_destroy(bo_ta);
    if (!same)
        rbtree_destroy(bo_tb);

    G_debug(2, "n_cross = %d", n_cross);
    /* Lines do not cross each other */
    if (n_cross == 0) {
        return 0;
    }

    /* l = 1 ~ line A, l = 2 ~ line B */
    nl = 3;
    if (same)
        nl = 2;
    for (l = 1; l < nl; l++) {
        for (i = 0; i < n_cross; i++)
            use_cross[i] = 1;

        /* Create array of lines */
        XLines = G_malloc((n_cross + 1) * sizeof(struct line_pnts *));

        if (l == 1) {
            G_debug(2, "Clean and create array for line A");
            Points = APnts;
            Points1 = APnts;
            Points2 = BPnts;
            current = 0;
            second = 1;
        }
        else {
            G_debug(2, "Clean and create array for line B");
            Points = BPnts;
            Points1 = BPnts;
            Points2 = APnts;
            current = 1;
            second = 0;
        }

        /* Sort points along lines */
        qsort((void *)cross, sizeof(char) * n_cross, sizeof(CROSS), cmp_cross);

        /* Print all (raw) breaks */
        /* avoid loop when not debugging */
        if (debug_level > 2) {
            for (i = 0; i < n_cross; i++) {
                G_debug(
                    3,
                    "  cross = %d seg1/dist1 = %d/%f seg2/dist2 = %d/%f x = %f "
                    "y = %f",
                    i, cross[i].segment[current],
                    sqrt(cross[i].distance[current]), cross[i].segment[second],
                    sqrt(cross[i].distance[second]), cross[i].x, cross[i].y);
            }
        }

        /* Remove breaks on first/last line vertices */
        for (i = 0; i < n_cross; i++) {
            if (use_cross[i] == 1) {
                j = Points1->n_points - 1;

                /* Note: */
                if ((cross[i].segment[current] == 0 &&
                     cross[i].x == Points1->x[0] &&
                     cross[i].y == Points1->y[0]) ||
                    (cross[i].segment[current] == j - 1 &&
                     cross[i].x == Points1->x[j] &&
                     cross[i].y == Points1->y[j])) {
                    use_cross[i] = 0; /* first/last */
                    G_debug(3, "cross %d deleted (first/last point)", i);
                }
            }
        }

        /* Remove breaks with collinear previous and next segments on 1 and 2 */
        /* Note: breaks with collinear previous and nex must be remove
         * duplicates, otherwise some cross may be lost. Example (+ is vertex):
         *             B          first cross intersections: A/B  segment:
         *             |               0/0, 0/1, 1/0, 1/1 - collinear previous
         * and next AB -----+----+--- A     0/4, 0/5, 1/4, 1/5 - OK
         *              \___|
         *                B
         *  This should not influence that break is always on first segment, see
         * below (I hope)
         */
        /* TODO: this doesn't find identical with breaks on revious/next */
        for (i = 0; i < n_cross; i++) {
            if (use_cross[i] == 0)
                continue;
            G_debug(3, "  is %d between colinear?", i);

            seg1 = cross[i].segment[current];
            seg2 = cross[i].segment[second];

            /* Is it vertex on 1, which? */
            if (cross[i].x == Points1->x[seg1] &&
                cross[i].y == Points1->y[seg1]) {
                vert1 = seg1;
            }
            else if (cross[i].x == Points1->x[seg1 + 1] &&
                     cross[i].y == Points1->y[seg1 + 1]) {
                vert1 = seg1 + 1;
            }
            else {
                G_debug(3, "  -> is not vertex on 1. line");
                continue;
            }

            /* Is it vertex on 2, which? */
            /* For 1. line it is easy, because breaks on vertex are always at
             * end vertex for 2. line we need to find which vertex is on break
             * if any (vert2 starts from 0) */
            if (cross[i].x == Points2->x[seg2] &&
                cross[i].y == Points2->y[seg2]) {
                vert2 = seg2;
            }
            else if (cross[i].x == Points2->x[seg2 + 1] &&
                     cross[i].y == Points2->y[seg2 + 1]) {
                vert2 = seg2 + 1;
            }
            else {
                G_debug(3, "  -> is not vertex on 2. line");
                continue;
            }
            G_debug(3, "    seg1/vert1 = %d/%d  seg2/ver2 = %d/%d", seg1, vert1,
                    seg2, vert2);

            /* Check if the second vertex is not first/last */
            if (vert2 == 0 || vert2 == Points2->n_points - 1) {
                G_debug(3, "  -> vertex 2 (%d) is first/last", vert2);
                continue;
            }

            /* Are there first vertices of this segment identical */
            if (!((Points1->x[vert1 - 1] == Points2->x[vert2 - 1] &&
                   Points1->y[vert1 - 1] == Points2->y[vert2 - 1] &&
                   Points1->x[vert1 + 1] == Points2->x[vert2 + 1] &&
                   Points1->y[vert1 + 1] == Points2->y[vert2 + 1]) ||
                  (Points1->x[vert1 - 1] == Points2->x[vert2 + 1] &&
                   Points1->y[vert1 - 1] == Points2->y[vert2 + 1] &&
                   Points1->x[vert1 + 1] == Points2->x[vert2 - 1] &&
                   Points1->y[vert1 + 1] == Points2->y[vert2 - 1]))) {
                G_debug(3, "  -> previous/next are not identical");
                continue;
            }

            use_cross[i] = 0;

            G_debug(3, "    -> collinear -> remove");
        }

        /* Remove duplicates, i.e. merge all identical breaks to one.
         *  We must be careful because two points with identical coordinates may
         * be distant if measured along the line: |         Segments b0 and b1
         * overlap, b0 runs up, b1 down. |         Two inersections may be
         * merged for a, because they are identical,
         *  -----+---- a   but cannot be merged for b, because both b0 and b1
         * must be broken. |         I.e. Breaks on b have identical
         * coordinates, but there are not identical b0 | b1      if measured
         * along line b.
         *
         *      -> Breaks may be merged as identical if lay on the same segment,
         * or on vertex connecting 2 adjacent segments the points lay on
         *
         *  Note: if duplicate is on a vertex, the break is removed from next
         * segment => break on vertex is always on first segment of this vertex
         * (used below)
         */
        last = -1;
        for (i = 0; i < n_cross; i++) {
            if (use_cross[i] == 0)
                continue;
            if (last == -1) { /* set first alive */
                last = i;
                continue;
            }
            seg = cross[i].segment[current];
            /* compare with last */
            G_debug(3, "  duplicate ?: cross = %d seg = %d dist = %f", i,
                    cross[i].segment[current], cross[i].distance[current]);
            if ((cross[i].segment[current] == cross[last].segment[current] &&
                 cross[i].distance[current] == cross[last].distance[current]) ||
                (cross[i].segment[current] ==
                     cross[last].segment[current] + 1 &&
                 cross[i].distance[current] == 0 &&
                 cross[i].x == cross[last].x && cross[i].y == cross[last].y)) {
                G_debug(3, "  cross %d identical to last -> removed", i);
                use_cross[i] = 0; /* identical */
            }
            else {
                last = i;
            }
        }

        /* Create array of new lines */
        /* Count alive crosses */
        n_alive_cross = 0;
        G_debug(3, "  alive crosses:");
        for (i = 0; i < n_cross; i++) {
            if (use_cross[i] == 1) {
                G_debug(3, "  %d", i);
                n_alive_cross++;
            }
        }

        k = 0;
        if (n_alive_cross > 0) {
            /* Add last line point at the end of cross array (cross alley) */
            use_cross[n_cross] = 1;
            j = Points->n_points - 1;
            cross[n_cross].x = Points->x[j];
            cross[n_cross].y = Points->y[j];
            cross[n_cross].segment[current] = Points->n_points - 2;

            last_seg = 0;
            last_x = Points->x[0];
            last_y = Points->y[0];
            last_z = Points->z[0];
            /* Go through all cross (+last line point) and create for each new
             * line starting at last_* and ending at cross (last point) */
            for (i = 0; i <= n_cross; i++) { /* i.e. n_cross + 1 new lines */
                seg = cross[i].segment[current];
                G_debug(2, "%d seg = %d dist = %f", i, seg,
                        cross[i].distance[current]);
                if (use_cross[i] == 0) {
                    G_debug(3, "   removed -> next");
                    continue;
                }

                G_debug(2, " New line:");
                XLines[k] = Vect_new_line_struct();
                /* add last intersection or first point first */
                Vect_append_point(XLines[k], last_x, last_y, last_z);
                G_debug(2, "   append last vert: %f %f", last_x, last_y);

                /* add first points of segments between last and current seg */
                for (j = last_seg + 1; j <= seg; j++) {
                    G_debug(2, "  segment j = %d", j);
                    /* skipp vertex identical to last break */
                    if ((j == last_seg + 1) && Points->x[j] == last_x &&
                        Points->y[j] == last_y) {
                        G_debug(2, "   -> skip (identical to last break)");
                        continue;
                    }
                    Vect_append_point(XLines[k], Points->x[j], Points->y[j],
                                      Points->z[j]);
                    G_debug(2, "   append first of seg: %f %f", Points->x[j],
                            Points->y[j]);
                }

                last_seg = seg;
                last_x = cross[i].x;
                last_y = cross[i].y;
                last_z = 0;
                /* calculate last_z */
                if (Points->z[last_seg] == Points->z[last_seg + 1]) {
                    last_z = Points->z[last_seg + 1];
                }
                else if (last_x == Points->x[last_seg] &&
                         last_y == Points->y[last_seg]) {
                    last_z = Points->z[last_seg];
                }
                else if (last_x == Points->x[last_seg + 1] &&
                         last_y == Points->y[last_seg + 1]) {
                    last_z = Points->z[last_seg + 1];
                }
                else {
                    dist = dist2(Points->x[last_seg], Points->x[last_seg + 1],
                                 Points->y[last_seg], Points->y[last_seg + 1]);
                    if (with_z) {
                        last_z = (Points->z[last_seg] *
                                      sqrt(cross[i].distance[current]) +
                                  Points->z[last_seg + 1] *
                                      (sqrt(dist) -
                                       sqrt(cross[i].distance[current]))) /
                                 sqrt(dist);
                    }
                }

                /* add current cross or end point */
                Vect_append_point(XLines[k], cross[i].x, cross[i].y, last_z);
                G_debug(2, "   append cross / last point: %f %f", cross[i].x,
                        cross[i].y);

                /* Check if line is degenerate */
                if (dig_line_degenerate(XLines[k]) > 0) {
                    G_debug(2, "   line is degenerate -> skipped");
                    Vect_destroy_line_struct(XLines[k]);
                }
                else {
                    k++;
                }
            }
        }
        if (l == 1) {
            *nalines = k;
            *ALines = XLines;
        }
        else {
            *nblines = k;
            *BLines = XLines;
        }
    }

    return 1;
}

/* static int cross_found; */ /* set by find_cross() */

/* find segment intersection, used by Vect_line_check_intersection2 */
static int find_cross(int i, int j, int b)
{
    double x1, y1, z1, x2, y2, z2;
    double y1min, y1max, y2min, y2max;
    int ret;

    y1min = APnts->y[i];
    y1max = APnts->y[i + 1];
    if (APnts->y[i] > APnts->y[i + 1]) {
        y1min = APnts->y[i + 1];
        y1max = APnts->y[i];
    }

    y2min = BPnts->y[j];
    y2max = BPnts->y[j + 1];
    if (BPnts->y[j] > BPnts->y[j + 1]) {
        y2min = BPnts->y[j + 1];
        y2max = BPnts->y[j];
    }

    if (y1min > y2max || y1max < y2min)
        return 0;

    if (b) {
        ret = Vect_segment_intersection(
            APnts->x[i], APnts->y[i], APnts->z[i], APnts->x[i + 1],
            APnts->y[i + 1], APnts->z[i + 1], BPnts->x[j], BPnts->y[j],
            BPnts->z[j], BPnts->x[j + 1], BPnts->y[j + 1], BPnts->z[j + 1], &x1,
            &y1, &z1, &x2, &y2, &z2, 0);
    }
    else {
        ret = Vect_segment_intersection(
            BPnts->x[j], BPnts->y[j], BPnts->z[j], BPnts->x[j + 1],
            BPnts->y[j + 1], BPnts->z[j + 1], APnts->x[i], APnts->y[i],
            APnts->z[i], APnts->x[i + 1], APnts->y[i + 1], APnts->z[i + 1], &x1,
            &y1, &z1, &x2, &y2, &z2, 0);
    }

    if (!IPnts)
        IPnts = Vect_new_line_struct();

    /* add ALL (including end points and duplicates), clean later */
    switch (ret) {
    case 0:
    case 5:
        break;
    case 1:
        if (0 > Vect_append_point(IPnts, x1, y1, z1))
            G_warning(_("Error while adding point to array. Out of memory"));
        break;
    case 2:
    case 3:
    case 4:
        if (0 > Vect_append_point(IPnts, x1, y1, z1))
            G_warning(_("Error while adding point to array. Out of memory"));
        if (0 > Vect_append_point(IPnts, x2, y2, z2))
            G_warning(_("Error while adding point to array. Out of memory"));
        break;
    }

    return ret;
}

int line_check_intersection2(struct line_pnts *APoints,
                             struct line_pnts *BPoints, int with_z, int all)
{
    double dist;
    struct bound_box ABox, BBox, abbox;
    struct boq bo_queue;
    struct qitem qi, *found;
    struct RB_TREE *bo_ta, *bo_tb;
    struct RB_TRAV bo_t_trav;
    int ret, intersect;
    double xa1, ya1, xa2, ya2, xb1, yb1, xb2, yb2, xi, yi;

    APnts = APoints;
    BPnts = BPoints;

    ABPnts[0] = APnts;
    ABPnts[1] = BPnts;

    /* TODO: 3D, RE (representation error) threshold, GV_POINTS (line x point)
     */

    if (!IPnts)
        IPnts = Vect_new_line_struct();
    Vect_reset_line(IPnts);

    /* If one or both are point (Points->n_points == 1) */
    if (APoints->n_points == 1 && BPoints->n_points == 1) {
        if (APoints->x[0] == BPoints->x[0] && APoints->y[0] == BPoints->y[0]) {
            if (!with_z) {
                if (all && 0 > Vect_append_point(IPnts, APoints->x[0],
                                                 APoints->y[0], APoints->z[0]))
                    G_warning(
                        _("Error while adding point to array. Out of memory"));
                return 1;
            }
            else {
                if (APoints->z[0] == BPoints->z[0]) {
                    if (all &&
                        0 > Vect_append_point(IPnts, APoints->x[0],
                                              APoints->y[0], APoints->z[0]))
                        G_warning(_("Error while adding point to array. Out of "
                                    "memory"));
                    return 1;
                }
                else
                    return 0;
            }
        }
        else {
            return 0;
        }
    }

    if (APoints->n_points == 1) {
        Vect_line_distance(BPoints, APoints->x[0], APoints->y[0], APoints->z[0],
                           with_z, NULL, NULL, NULL, &dist, NULL, NULL);

        if (dist <= d_ulp(APoints->x[0], APoints->y[0])) {
            if (all && 0 > Vect_append_point(IPnts, APoints->x[0],
                                             APoints->y[0], APoints->z[0]))
                G_warning(
                    _("Error while adding point to array. Out of memory"));
            return 1;
        }
        else {
            return 0;
        }
    }

    if (BPoints->n_points == 1) {
        Vect_line_distance(APoints, BPoints->x[0], BPoints->y[0], BPoints->z[0],
                           with_z, NULL, NULL, NULL, &dist, NULL, NULL);

        if (dist <= d_ulp(BPoints->x[0], BPoints->y[0])) {
            if (all && 0 > Vect_append_point(IPnts, BPoints->x[0],
                                             BPoints->y[0], BPoints->z[0]))
                G_warning(
                    _("Error while adding point to array. Out of memory"));
            return 1;
        }
        else
            return 0;
    }

    /* Take each segment from A and find if intersects any segment from B. */

    dig_line_box(APoints, &ABox);
    dig_line_box(BPoints, &BBox);
    if (!with_z) {
        ABox.T = BBox.T = PORT_DOUBLE_MAX;
        ABox.B = BBox.B = -PORT_DOUBLE_MAX;
    }

    if (!Vect_box_overlap(&ABox, &BBox)) {
        return 0;
    }

    /* overlap box */
    abbox = BBox;
    if (abbox.N > ABox.N)
        abbox.N = ABox.N;
    if (abbox.S < ABox.S)
        abbox.S = ABox.S;
    if (abbox.E > ABox.E)
        abbox.E = ABox.E;
    if (abbox.W < ABox.W)
        abbox.W = ABox.W;

    abbox.N += d_ulp(abbox.N, abbox.N);
    abbox.S -= d_ulp(abbox.S, abbox.S);
    abbox.E += d_ulp(abbox.E, abbox.E);
    abbox.W -= d_ulp(abbox.W, abbox.W);

    if (with_z) {
        if (abbox.T > ABox.T)
            abbox.T = ABox.T;
        if (abbox.B < ABox.B)
            abbox.B = ABox.B;

        abbox.T += d_ulp(abbox.T, abbox.T);
        abbox.B -= d_ulp(abbox.B, abbox.B);
    }

    /* initialize queue */
    bo_queue.count = 0;
    bo_queue.alloc = 2 * (APnts->n_points + BPnts->n_points);
    bo_queue.i = G_malloc(bo_queue.alloc * sizeof(struct qitem));

    /* load APnts to queue */
    if (!boq_load(&bo_queue, APnts, &abbox, 0, with_z)) {
        G_free(bo_queue.i);
        return 0;
    }

    /* load BPnts to queue */
    if (!boq_load(&bo_queue, BPnts, &abbox, 1, with_z)) {
        G_free(bo_queue.i);
        return 0;
    }

    /* initialize search tree */
    bo_ta = rbtree_create(cmp_t_y, sizeof(struct qitem));
    bo_tb = rbtree_create(cmp_t_y, sizeof(struct qitem));

    /* find intersection */
    xa1 = APnts->x[0];
    ya1 = APnts->y[0];
    xa2 = APnts->x[APnts->n_points - 1];
    ya2 = APnts->y[APnts->n_points - 1];
    xb1 = BPnts->x[0];
    yb1 = BPnts->y[0];
    xb2 = BPnts->x[BPnts->n_points - 1];
    yb2 = BPnts->y[BPnts->n_points - 1];
    intersect = 0;
    while (boq_drop(&bo_queue, &qi)) {

        if (qi.e == QEVT_IN) {
            /* not the original Bentley-Ottmann algorithm */
            if (qi.l == 0) {
                /* test for intersection of s with all segments in T */
                rbtree_init_trav(&bo_t_trav, bo_tb);
                while ((found = rbtree_traverse(&bo_t_trav))) {
                    ret = find_cross(qi.s, found->s, 0);

                    if (ret > 0) {
                        if (ret != 1) {
                            intersect = 1;
                        }
                        /* intersect at one point */
                        else if (intersect != 1) {
                            intersect = 1;
                            xi = IPnts->x[IPnts->n_points - 1];
                            yi = IPnts->y[IPnts->n_points - 1];
                            if (xi == xa1 && yi == ya1) {
                                if ((xi == xb1 && yi == yb1) ||
                                    (xi == xb2 && yi == yb2)) {
                                    intersect = 2;
                                }
                            }
                            else if (xi == xa2 && yi == ya2) {
                                if ((xi == xb1 && yi == yb1) ||
                                    (xi == xb2 && yi == yb2)) {
                                    intersect = 2;
                                }
                            }
                        }
                    }

                    if (intersect == 1) {
                        break;
                    }
                }

                /* insert s in T */
                rbtree_insert(bo_ta, &qi);
            }
            else {
                /* test for intersection of s with all segments in T */
                rbtree_init_trav(&bo_t_trav, bo_ta);
                while ((found = rbtree_traverse(&bo_t_trav))) {
                    ret = find_cross(found->s, qi.s, 1);

                    if (ret > 0) {
                        if (ret != 1) {
                            intersect = 1;
                        }
                        /* intersect at one point */
                        else if (intersect != 1) {
                            intersect = 1;
                            xi = IPnts->x[IPnts->n_points - 1];
                            yi = IPnts->y[IPnts->n_points - 1];
                            if (xi == xa1 && yi == ya1) {
                                if ((xi == xb1 && yi == yb1) ||
                                    (xi == xb2 && yi == yb2)) {
                                    intersect = 2;
                                }
                            }
                            else if (xi == xa2 && yi == ya2) {
                                if ((xi == xb1 && yi == yb1) ||
                                    (xi == xb2 && yi == yb2)) {
                                    intersect = 2;
                                }
                            }
                        }
                    }

                    if (intersect == 1) {
                        break;
                    }
                }

                /* insert s in T */
                rbtree_insert(bo_tb, &qi);
            }
        }
        else if (qi.e == QEVT_OUT) {
            /* remove from T */

            /* adjust p */
            if (qi.p == qi.s)
                qi.p++;
            else
                qi.p--;

            if (qi.l == 0) {
                if (!rbtree_remove(bo_ta, &qi))
                    G_fatal_error("RB tree error!");
            }
            else {
                if (!rbtree_remove(bo_tb, &qi))
                    G_fatal_error("RB tree error!");
            }
        }
        if (!all && intersect == 1) {
            break;
        }
    }
    G_free(bo_queue.i);
    rbtree_destroy(bo_ta);
    rbtree_destroy(bo_tb);

    return intersect;
}

/*!
 * \brief Check if 2 lines intersect.
 *
 * Points (Points->n_points == 1) are also supported.
 *
 * simplified Bentley–Ottmann Algorithm:
 * similar to Vect_line_check_intersection(), but faster
 *
 * \param APoints first input line
 * \param BPoints second input line
 * \param with_z 3D, not supported (only if one or both are points)!
 *
 * \return 0 no intersection
 * \return 1 intersection
 * \return 2 end points only
 */
int Vect_line_check_intersection2(struct line_pnts *APoints,
                                  struct line_pnts *BPoints, int with_z)
{
    return line_check_intersection2(APoints, BPoints, with_z, 0);
}

/*!
 * \brief Get 2 lines intersection points.
 *
 * A wrapper around Vect_line_check_intersection2() function.
 *
 * simplified Bentley–Ottmann Algorithm:
 * similar to Vect_line_get_intersections(), but faster
 *
 * \param APoints first input line
 * \param BPoints second input line
 * \param[out] IPoints output with intersection points
 * \param with_z 3D, not supported (only if one or both are points)!
 *
 * \return 0 no intersection
 * \return 1 intersection found
 */
int Vect_line_get_intersections2(struct line_pnts *APoints,
                                 struct line_pnts *BPoints,
                                 struct line_pnts *IPoints, int with_z)
{
    int ret;

    IPnts = IPoints;
    ret = line_check_intersection2(APoints, BPoints, with_z, 1);

    return ret;
}