forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_r.c
5590 lines (5070 loc) · 215 KB
/
merge_r.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
/*<html><pre> -<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="TOP">-</a>
merge_r.c
merges non-convex facets
see qh-merge_r.htm and merge_r.h
other modules call qh_premerge() and qh_postmerge()
the user may call qh_postmerge() to perform additional merges.
To remove deleted facets and vertices (qhull() in libqhull_r.c):
qh_partitionvisible(qh, !qh_ALL, &numoutside); // visible_list, newfacet_list
qh_deletevisible(); // qh.visible_list
qh_resetlists(qh, False, qh_RESETvisible); // qh.visible_list newvertex_list newfacet_list
assumes qh.CENTERtype= centrum
merges occur in qh_mergefacet and in qh_mergecycle
vertex->neighbors not set until the first merge occurs
Copyright (c) 1993-2020 C.B. Barber.
$Id: //main/2019/qhull/src/libqhull_r/merge_r.c#14 $$Change: 2953 $
$DateTime: 2020/05/21 22:05:32 $$Author: bbarber $
*/
#include "qhull_ra.h"
#ifndef qh_NOmerge
/* MRGnone, etc. */
const char *mergetypes[]= {
"none",
"coplanar",
"anglecoplanar",
"concave",
"concavecoplanar",
"twisted",
"flip",
"dupridge",
"subridge",
"vertices",
"degen",
"redundant",
"mirror",
"coplanarhorizon",
};
/*===== functions(alphabetical after premerge and postmerge) ======*/
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="premerge">-</a>
qh_premerge(qh, apexpointid, maxcentrum )
pre-merge nonconvex facets in qh.newfacet_list for apexpointid
maxcentrum defines coplanar and concave (qh_test_appendmerge)
returns:
deleted facets added to qh.visible_list with facet->visible set
notes:
only called by qh_addpoint
uses globals, qh.MERGEexact, qh.PREmerge
design:
mark dupridges in qh.newfacet_list
merge facet cycles in qh.newfacet_list
merge dupridges and concave facets in qh.newfacet_list
check merged facet cycles for degenerate and redundant facets
merge degenerate and redundant facets
collect coplanar and concave facets
merge concave, coplanar, degenerate, and redundant facets
*/
void qh_premerge(qhT *qh, int apexpointid, realT maxcentrum, realT maxangle /* qh.newfacet_list */) {
boolT othermerge= False;
if (qh->ZEROcentrum && qh_checkzero(qh, !qh_ALL))
return;
trace2((qh, qh->ferr, 2008, "qh_premerge: premerge centrum %2.2g angle %4.4g for apex p%d newfacet_list f%d\n",
maxcentrum, maxangle, apexpointid, getid_(qh->newfacet_list)));
if (qh->IStracing >= 4 && qh->num_facets < 100)
qh_printlists(qh);
qh->centrum_radius= maxcentrum;
qh->cos_max= maxangle;
if (qh->hull_dim >=3) {
qh_mark_dupridges(qh, qh->newfacet_list, qh_ALL); /* facet_mergeset */
qh_mergecycle_all(qh, qh->newfacet_list, &othermerge);
qh_forcedmerges(qh, &othermerge /* qh.facet_mergeset */);
}else /* qh.hull_dim == 2 */
qh_mergecycle_all(qh, qh->newfacet_list, &othermerge);
qh_flippedmerges(qh, qh->newfacet_list, &othermerge);
if (!qh->MERGEexact || zzval_(Ztotmerge)) {
zinc_(Zpremergetot);
qh->POSTmerging= False;
qh_getmergeset_initial(qh, qh->newfacet_list);
qh_all_merges(qh, othermerge, False);
}
} /* premerge */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="postmerge">-</a>
qh_postmerge(qh, reason, maxcentrum, maxangle, vneighbors )
post-merge nonconvex facets as defined by maxcentrum and maxangle
'reason' is for reporting progress
if vneighbors ('Qv'),
calls qh_test_vneighbors at end of qh_all_merge from qh_postmerge
returns:
if first call (qh.visible_list != qh.facet_list),
builds qh.facet_newlist, qh.newvertex_list
deleted facets added to qh.visible_list with facet->visible
qh.visible_list == qh.facet_list
notes:
called by qh_qhull after qh_buildhull
called if a merge may be needed due to
qh.MERGEexact ('Qx'), qh_DIMreduceBuild, POSTmerge (e.g., 'Cn'), or TESTvneighbors ('Qv')
if firstmerge,
calls qh_reducevertices before qh_getmergeset
design:
if first call
set qh.visible_list and qh.newfacet_list to qh.facet_list
add all facets to qh.newfacet_list
mark non-simplicial facets, facet->newmerge
set qh.newvertext_list to qh.vertex_list
add all vertices to qh.newvertex_list
if a pre-merge occurred
set vertex->delridge {will retest the ridge}
if qh.MERGEexact
call qh_reducevertices()
if no pre-merging
merge flipped facets
determine non-convex facets
merge all non-convex facets
*/
void qh_postmerge(qhT *qh, const char *reason, realT maxcentrum, realT maxangle,
boolT vneighbors) {
facetT *newfacet;
boolT othermerges= False;
vertexT *vertex;
if (qh->REPORTfreq || qh->IStracing) {
qh_buildtracing(qh, NULL, NULL);
qh_printsummary(qh, qh->ferr);
if (qh->PRINTstatistics)
qh_printallstatistics(qh, qh->ferr, "reason");
qh_fprintf(qh, qh->ferr, 8062, "\n%s with 'C%.2g' and 'A%.2g'\n",
reason, maxcentrum, maxangle);
}
trace2((qh, qh->ferr, 2009, "qh_postmerge: postmerge. test vneighbors? %d\n",
vneighbors));
qh->centrum_radius= maxcentrum;
qh->cos_max= maxangle;
qh->POSTmerging= True;
if (qh->visible_list != qh->facet_list) { /* first call due to qh_buildhull, multiple calls if qh.POSTmerge */
qh->NEWfacets= True;
qh->visible_list= qh->newfacet_list= qh->facet_list;
FORALLnew_facets { /* all facets are new facets for qh_postmerge */
newfacet->newfacet= True;
if (!newfacet->simplicial)
newfacet->newmerge= True; /* test f.vertices for 'delridge'. 'newmerge' was cleared at end of qh_all_merges */
zinc_(Zpostfacets);
}
qh->newvertex_list= qh->vertex_list;
FORALLvertices
vertex->newfacet= True;
if (qh->VERTEXneighbors) { /* a merge has occurred */
if (qh->MERGEexact && qh->hull_dim <= qh_DIMreduceBuild)
qh_reducevertices(qh); /* qh_all_merges did not call qh_reducevertices for v.delridge */
}
if (!qh->PREmerge && !qh->MERGEexact)
qh_flippedmerges(qh, qh->newfacet_list, &othermerges);
}
qh_getmergeset_initial(qh, qh->newfacet_list);
qh_all_merges(qh, False, vneighbors); /* calls qh_reducevertices before exiting */
FORALLnew_facets
newfacet->newmerge= False; /* Was True if no vertex in f.vertices was 'delridge' */
} /* post_merge */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="all_merges">-</a>
qh_all_merges(qh, othermerge, vneighbors )
merge all non-convex facets
set othermerge if already merged facets (calls qh_reducevertices)
if vneighbors ('Qv' at qh.POSTmerge)
tests vertex neighbors for convexity at end (qh_test_vneighbors)
qh.facet_mergeset lists the non-convex ridges in qh_newfacet_list
qh.degen_mergeset is defined
if qh.MERGEexact && !qh.POSTmerging,
does not merge coplanar facets
returns:
deleted facets added to qh.visible_list with facet->visible
deleted vertices added qh.delvertex_list with vertex->delvertex
notes:
unless !qh.MERGEindependent,
merges facets in independent sets
uses qh.newfacet_list as implicit argument since merges call qh_removefacet()
[apr'19] restored qh_setdellast in place of qh_next_facetmerge. Much faster for post-merge
design:
while merges occur
for each merge in qh.facet_mergeset
unless one of the facets was already merged in this pass
merge the facets
test merged facets for additional merges
add merges to qh.facet_mergeset
if qh.POSTmerging
periodically call qh_reducevertices to reduce extra vertices and redundant vertices
after each pass, if qh.VERTEXneighbors
if qh.POSTmerging or was a merge with qh.hull_dim<=5
call qh_reducevertices
update qh.facet_mergeset if degenredundant merges
if 'Qv' and qh.POSTmerging
test vertex neighbors for convexity
*/
void qh_all_merges(qhT *qh, boolT othermerge, boolT vneighbors) {
facetT *facet1, *facet2, *newfacet;
mergeT *merge;
boolT wasmerge= False, isreduce;
void **freelistp; /* used if !qh_NOmem by qh_memfree_() */
vertexT *vertex;
realT angle, distance;
mergeType mergetype;
int numcoplanar=0, numconcave=0, numconcavecoplanar= 0, numdegenredun= 0, numnewmerges= 0, numtwisted= 0;
trace2((qh, qh->ferr, 2010, "qh_all_merges: starting to merge %d facet and %d degenerate merges for new facets f%d, othermerge? %d\n",
qh_setsize(qh, qh->facet_mergeset), qh_setsize(qh, qh->degen_mergeset), getid_(qh->newfacet_list), othermerge));
while (True) {
wasmerge= False;
while (qh_setsize(qh, qh->facet_mergeset) > 0 || qh_setsize(qh, qh->degen_mergeset) > 0) {
if (qh_setsize(qh, qh->degen_mergeset) > 0) {
numdegenredun += qh_merge_degenredundant(qh);
wasmerge= True;
}
while ((merge= (mergeT *)qh_setdellast(qh->facet_mergeset))) {
facet1= merge->facet1;
facet2= merge->facet2;
vertex= merge->vertex1; /* not used for qh.facet_mergeset*/
mergetype= merge->mergetype;
angle= merge->angle;
distance= merge->distance;
qh_memfree_(qh, merge, (int)sizeof(mergeT), freelistp); /* 'merge' is invalid */
if (facet1->visible || facet2->visible) {
trace3((qh, qh->ferr, 3045, "qh_all_merges: drop merge of f%d (del? %d) into f%d (del? %d) mergetype %d, dist %4.4g, angle %4.4g. One or both facets is deleted\n",
facet1->id, facet1->visible, facet2->id, facet2->visible, mergetype, distance, angle));
continue;
}else if (mergetype == MRGcoplanar || mergetype == MRGanglecoplanar) {
if (qh->MERGEindependent) {
if ((!facet1->tested && facet1->newfacet)
|| (!facet2->tested && facet2->newfacet)) {
trace3((qh, qh->ferr, 3064, "qh_all_merges: drop merge of f%d (tested? %d) into f%d (tested? %d) mergetype %d, dist %2.2g, angle %4.4g. Merge independent sets of coplanar merges\n",
facet1->id, facet1->visible, facet2->id, facet2->visible, mergetype, distance, angle));
continue;
}
}
}
trace3((qh, qh->ferr, 3047, "qh_all_merges: merge f%d and f%d type %d dist %2.2g angle %4.4g\n",
facet1->id, facet2->id, mergetype, distance, angle));
if (mergetype == MRGtwisted)
qh_merge_twisted(qh, facet1, facet2);
else
qh_merge_nonconvex(qh, facet1, facet2, mergetype);
numnewmerges++;
numdegenredun += qh_merge_degenredundant(qh);
wasmerge= True;
if (mergetype == MRGconcave)
numconcave++;
else if (mergetype == MRGconcavecoplanar)
numconcavecoplanar++;
else if (mergetype == MRGtwisted)
numtwisted++;
else if (mergetype == MRGcoplanar || mergetype == MRGanglecoplanar)
numcoplanar++;
else {
qh_fprintf(qh, qh->ferr, 6394, "qhull internal error (qh_all_merges): expecting concave, coplanar, or twisted merge. Got merge f%d f%d v%d mergetype %d\n",
getid_(facet1), getid_(facet2), getid_(vertex), mergetype);
qh_errexit2(qh, qh_ERRqhull, facet1, facet2);
}
} /* while qh_setdellast */
if (qh->POSTmerging && qh->hull_dim <= qh_DIMreduceBuild
&& numnewmerges > qh_MAXnewmerges) {
numnewmerges= 0;
wasmerge= othermerge= False;
qh_reducevertices(qh); /* otherwise large post merges too slow */
}
qh_getmergeset(qh, qh->newfacet_list); /* qh.facet_mergeset */
} /* while facet_mergeset or degen_mergeset */
if (qh->VERTEXneighbors) { /* at least one merge */
isreduce= False;
if (qh->POSTmerging && qh->hull_dim >= 4) {
isreduce= True;
}else if (qh->POSTmerging || !qh->MERGEexact) {
if ((wasmerge || othermerge) && qh->hull_dim > 2 && qh->hull_dim <= qh_DIMreduceBuild)
isreduce= True;
}
if (isreduce) {
wasmerge= othermerge= False;
if (qh_reducevertices(qh)) {
qh_getmergeset(qh, qh->newfacet_list); /* facet_mergeset */
continue;
}
}
}
if (vneighbors && qh_test_vneighbors(qh /* qh.newfacet_list */))
continue;
break;
} /* while (True) */
if (wasmerge || othermerge) {
trace3((qh, qh->ferr, 3033, "qh_all_merges: skip qh_reducevertices due to post-merging, no qh.VERTEXneighbors (%d), or hull_dim %d ==2 or >%d\n", qh->VERTEXneighbors, qh->hull_dim, qh_DIMreduceBuild))
FORALLnew_facets {
newfacet->newmerge= False;
}
}
if (qh->CHECKfrequently && !qh->MERGEexact) {
qh->old_randomdist= qh->RANDOMdist;
qh->RANDOMdist= False;
qh_checkconvex(qh, qh->newfacet_list, qh_ALGORITHMfault);
/* qh_checkconnect(qh); [this is slow and it changes the facet order] */
qh->RANDOMdist= qh->old_randomdist;
}
trace1((qh, qh->ferr, 1009, "qh_all_merges: merged %d coplanar %d concave %d concavecoplanar %d twisted facets and %d degen or redundant facets.\n",
numcoplanar, numconcave, numconcavecoplanar, numtwisted, numdegenredun));
if (qh->IStracing >= 4 && qh->num_facets < 500)
qh_printlists(qh);
} /* all_merges */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="all_vertexmerges">-</a>
qh_all_vertexmerges(qh, apexpointid, facet, &retryfacet )
merge vertices in qh.vertex_mergeset and subsequent merges
returns:
returns retryfacet for facet (if defined)
updates qh.facet_list, qh.num_facets, qh.vertex_list, qh.num_vertices
mergesets are empty
if merges, resets facet lists
notes:
called from qh_qhull, qh_addpoint, and qh_buildcone_mergepinched
vertex merges occur after facet merges and qh_resetlists
design:
while merges in vertex_mergeset (MRGvertices)
merge a pair of pinched vertices
update vertex neighbors
merge non-convex and degenerate facets and check for ridges with duplicate vertices
partition outside points of deleted, "visible" facets
*/
void qh_all_vertexmerges(qhT *qh, int apexpointid, facetT *facet, facetT **retryfacet) {
int numpoints; /* ignore count of partitioned points. Used by qh_addpoint for Zpbalance */
if (retryfacet)
*retryfacet= facet;
while (qh_setsize(qh, qh->vertex_mergeset) > 0) {
trace1((qh, qh->ferr, 1057, "qh_all_vertexmerges: starting to merge %d vertex merges for apex p%d facet f%d\n",
qh_setsize(qh, qh->vertex_mergeset), apexpointid, getid_(facet)));
if (qh->IStracing >= 4 && qh->num_facets < 1000)
qh_printlists(qh);
qh_merge_pinchedvertices(qh, apexpointid /* qh.vertex_mergeset, visible_list, newvertex_list, newfacet_list */);
qh_update_vertexneighbors(qh); /* update neighbors of qh.newvertex_list from qh_newvertices for deleted facets on qh.visible_list */
/* test ridges and merge non-convex facets */
qh_getmergeset(qh, qh->newfacet_list);
qh_all_merges(qh, True, False); /* calls qh_reducevertices */
if (qh->CHECKfrequently)
qh_checkpolygon(qh, qh->facet_list);
qh_partitionvisible(qh, !qh_ALL, &numpoints /* qh.visible_list qh.del_vertices*/);
if (retryfacet)
*retryfacet= qh_getreplacement(qh, *retryfacet);
qh_deletevisible(qh /* qh.visible_list qh.del_vertices*/);
qh_resetlists(qh, False, qh_RESETvisible /* qh.visible_list newvertex_list qh.newfacet_list */);
if (qh->IStracing >= 4 && qh->num_facets < 1000) {
qh_printlists(qh);
qh_checkpolygon(qh, qh->facet_list);
}
}
} /* all_vertexmerges */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="appendmergeset">-</a>
qh_appendmergeset(qh, facet, vertex, neighbor, mergetype, dist, angle )
appends an entry to qh.facet_mergeset or qh.degen_mergeset
if 'dist' is unknown, set it to 0.0
if 'angle' is unknown, set it to 1.0 (coplanar)
returns:
merge appended to facet_mergeset or degen_mergeset
sets ->degenerate or ->redundant if degen_mergeset
notes:
caller collects statistics and/or caller of qh_mergefacet
see: qh_test_appendmerge()
design:
allocate merge entry
if regular merge
append to qh.facet_mergeset
else if degenerate merge and qh.facet_mergeset is all degenerate
append to qh.degen_mergeset
else if degenerate merge
prepend to qh.degen_mergeset (merged last)
else if redundant merge
append to qh.degen_mergeset
*/
void qh_appendmergeset(qhT *qh, facetT *facet, facetT *neighbor, mergeType mergetype, coordT dist, realT angle) {
mergeT *merge, *lastmerge;
void **freelistp; /* used if !qh_NOmem by qh_memalloc_() */
const char *mergename;
if ((facet->redundant && mergetype != MRGmirror) || neighbor->redundant) {
trace3((qh, qh->ferr, 3051, "qh_appendmergeset: f%d is already redundant (%d) or f%d is already redundant (%d). Ignore merge f%d and f%d type %d\n",
facet->id, facet->redundant, neighbor->id, neighbor->redundant, facet->id, neighbor->id, mergetype));
return;
}
if (facet->degenerate && mergetype == MRGdegen) {
trace3((qh, qh->ferr, 3077, "qh_appendmergeset: f%d is already degenerate. Ignore merge f%d type %d (MRGdegen)\n",
facet->id, facet->id, mergetype));
return;
}
if (!qh->facet_mergeset || !qh->degen_mergeset) {
qh_fprintf(qh, qh->ferr, 6403, "qhull internal error (qh_appendmergeset): expecting temp set defined for qh.facet_mergeset (0x%x) and qh.degen_mergeset (0x%x). Got NULL\n",
qh->facet_mergeset, qh->degen_mergeset);
/* otherwise qh_setappend creates a new set that is not freed by qh_freebuild() */
qh_errexit(qh, qh_ERRqhull, NULL, NULL);
}
if (neighbor->flipped && !facet->flipped) {
if (mergetype != MRGdupridge) {
qh_fprintf(qh, qh->ferr, 6355, "qhull internal error (qh_appendmergeset): except for MRGdupridge, cannot merge a non-flipped facet f%d into flipped f%d, mergetype %d, dist %4.4g\n",
facet->id, neighbor->id, mergetype, dist);
qh_errexit(qh, qh_ERRqhull, NULL, NULL);
}else {
trace2((qh, qh->ferr, 2106, "qh_appendmergeset: dupridge will merge a non-flipped facet f%d into flipped f%d, dist %4.4g\n",
facet->id, neighbor->id, dist));
}
}
qh_memalloc_(qh, (int)sizeof(mergeT), freelistp, merge, mergeT);
merge->angle= angle;
merge->distance= dist;
merge->facet1= facet;
merge->facet2= neighbor;
merge->vertex1= NULL;
merge->vertex2= NULL;
merge->ridge1= NULL;
merge->ridge2= NULL;
merge->mergetype= mergetype;
if(mergetype > 0 && mergetype < sizeof(mergetypes)/sizeof(char *))
mergename= mergetypes[mergetype];
else
mergename= mergetypes[MRGnone];
if (mergetype < MRGdegen)
qh_setappend(qh, &(qh->facet_mergeset), merge);
else if (mergetype == MRGdegen) {
facet->degenerate= True;
if (!(lastmerge= (mergeT *)qh_setlast(qh->degen_mergeset))
|| lastmerge->mergetype == MRGdegen)
qh_setappend(qh, &(qh->degen_mergeset), merge);
else
qh_setaddnth(qh, &(qh->degen_mergeset), 0, merge); /* merged last */
}else if (mergetype == MRGredundant) {
facet->redundant= True;
qh_setappend(qh, &(qh->degen_mergeset), merge);
}else /* mergetype == MRGmirror */ {
if (facet->redundant || neighbor->redundant) {
qh_fprintf(qh, qh->ferr, 6092, "qhull internal error (qh_appendmergeset): facet f%d or f%d is already a mirrored facet (i.e., 'redundant')\n",
facet->id, neighbor->id);
qh_errexit2(qh, qh_ERRqhull, facet, neighbor);
}
if (!qh_setequal(facet->vertices, neighbor->vertices)) {
qh_fprintf(qh, qh->ferr, 6093, "qhull internal error (qh_appendmergeset): mirrored facets f%d and f%d do not have the same vertices\n",
facet->id, neighbor->id);
qh_errexit2(qh, qh_ERRqhull, facet, neighbor);
}
facet->redundant= True;
neighbor->redundant= True;
qh_setappend(qh, &(qh->degen_mergeset), merge);
}
if (merge->mergetype >= MRGdegen) {
trace3((qh, qh->ferr, 3044, "qh_appendmergeset: append merge f%d and f%d type %d (%s) to qh.degen_mergeset (size %d)\n",
merge->facet1->id, merge->facet2->id, merge->mergetype, mergename, qh_setsize(qh, qh->degen_mergeset)));
}else {
trace3((qh, qh->ferr, 3027, "qh_appendmergeset: append merge f%d and f%d type %d (%s) dist %2.2g angle %4.4g to qh.facet_mergeset (size %d)\n",
merge->facet1->id, merge->facet2->id, merge->mergetype, mergename, merge->distance, merge->angle, qh_setsize(qh, qh->facet_mergeset)));
}
} /* appendmergeset */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="appendvertexmerge">-</a>
qh_appendvertexmerge(qh, vertex, vertex2, mergetype, distance, ridge1, ridge2 )
appends a vertex merge to qh.vertex_mergeset
MRGsubridge includes two ridges (from MRGdupridge)
MRGvertices includes two ridges
notes:
called by qh_getpinchedmerges for MRGsubridge
called by qh_maybe_duplicateridge and qh_maybe_duplicateridges for MRGvertices
only way to add a vertex merge to qh.vertex_mergeset
checked by qh_next_vertexmerge
*/
void qh_appendvertexmerge(qhT *qh, vertexT *vertex, vertexT *destination, mergeType mergetype, realT distance, ridgeT *ridge1, ridgeT *ridge2) {
mergeT *merge;
void **freelistp; /* used if !qh_NOmem by qh_memalloc_() */
const char *mergename;
if (!qh->vertex_mergeset) {
qh_fprintf(qh, qh->ferr, 6387, "qhull internal error (qh_appendvertexmerge): expecting temp set defined for qh.vertex_mergeset (0x%x). Got NULL\n",
qh->vertex_mergeset);
/* otherwise qh_setappend creates a new set that is not freed by qh_freebuild() */
qh_errexit(qh, qh_ERRqhull, NULL, NULL);
}
qh_memalloc_(qh, (int)sizeof(mergeT), freelistp, merge, mergeT);
merge->angle= qh_ANGLEnone;
merge->distance= distance;
merge->facet1= NULL;
merge->facet2= NULL;
merge->vertex1= vertex;
merge->vertex2= destination;
merge->ridge1= ridge1;
merge->ridge2= ridge2;
merge->mergetype= mergetype;
if(mergetype > 0 && mergetype < sizeof(mergetypes)/sizeof(char *))
mergename= mergetypes[mergetype];
else
mergename= mergetypes[MRGnone];
if (mergetype == MRGvertices) {
if (!ridge1 || !ridge2 || ridge1 == ridge2) {
qh_fprintf(qh, qh->ferr, 6106, "qhull internal error (qh_appendvertexmerge): expecting two distinct ridges for MRGvertices. Got r%d r%d\n",
getid_(ridge1), getid_(ridge2));
qh_errexit(qh, qh_ERRqhull, NULL, ridge1);
}
}
qh_setappend(qh, &(qh->vertex_mergeset), merge);
trace3((qh, qh->ferr, 3034, "qh_appendvertexmerge: append merge v%d into v%d r%d r%d dist %2.2g type %d (%s)\n",
vertex->id, destination->id, getid_(ridge1), getid_(ridge2), distance, merge->mergetype, mergename));
} /* appendvertexmerge */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="basevertices">-</a>
qh_basevertices(qh, samecycle )
return temporary set of base vertices for samecycle
samecycle is first facet in the cycle
assumes apex is SETfirst_( samecycle->vertices )
returns:
vertices(settemp)
all ->seen are cleared
notes:
uses qh_vertex_visit;
design:
for each facet in samecycle
for each unseen vertex in facet->vertices
append to result
*/
setT *qh_basevertices(qhT *qh, facetT *samecycle) {
facetT *same;
vertexT *apex, *vertex, **vertexp;
setT *vertices= qh_settemp(qh, qh->TEMPsize);
apex= SETfirstt_(samecycle->vertices, vertexT);
apex->visitid= ++qh->vertex_visit;
FORALLsame_cycle_(samecycle) {
if (same->mergeridge)
continue;
FOREACHvertex_(same->vertices) {
if (vertex->visitid != qh->vertex_visit) {
qh_setappend(qh, &vertices, vertex);
vertex->visitid= qh->vertex_visit;
vertex->seen= False;
}
}
}
trace4((qh, qh->ferr, 4019, "qh_basevertices: found %d vertices\n",
qh_setsize(qh, vertices)));
return vertices;
} /* basevertices */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="check_dupridge">-</a>
qh_check_dupridge(qh, facet1, dist1, facet2, dist2 )
Check dupridge between facet1 and facet2 for wide merge
dist1 is the maximum distance of facet1's vertices to facet2
dist2 is the maximum distance of facet2's vertices to facet1
returns
Level 1 log of the dupridge with the minimum distance between vertices
Throws error if the merge will increase the maximum facet width by qh_WIDEduplicate (100x)
notes:
only called from qh_forcedmerges
*/
void qh_check_dupridge(qhT *qh, facetT *facet1, realT dist1, facetT *facet2, realT dist2) {
vertexT *vertex, **vertexp, *vertexA, **vertexAp;
realT dist, innerplane, mergedist, outerplane, prevdist, ratio, vertexratio;
realT minvertex= REALmax;
mergedist= fmin_(dist1, dist2);
qh_outerinner(qh, NULL, &outerplane, &innerplane); /* ratio from qh_printsummary */
FOREACHvertex_(facet1->vertices) { /* The dupridge is between facet1 and facet2, so either facet can be tested */
FOREACHvertexA_(facet1->vertices) {
if (vertex > vertexA){ /* Test each pair once */
dist= qh_pointdist(vertex->point, vertexA->point, qh->hull_dim);
minimize_(minvertex, dist);
/* Not quite correct. A facet may have a dupridge and another pair of nearly adjacent vertices. */
}
}
}
prevdist= fmax_(outerplane, innerplane);
maximize_(prevdist, qh->ONEmerge + qh->DISTround);
maximize_(prevdist, qh->MINoutside + qh->DISTround);
ratio= mergedist/prevdist;
vertexratio= minvertex/prevdist;
trace0((qh, qh->ferr, 16, "qh_check_dupridge: dupridge between f%d and f%d (vertex dist %2.2g), dist %2.2g, reverse dist %2.2g, ratio %2.2g while processing p%d\n",
facet1->id, facet2->id, minvertex, dist1, dist2, ratio, qh->furthest_id));
if (ratio > qh_WIDEduplicate) {
qh_fprintf(qh, qh->ferr, 6271, "qhull topology error (qh_check_dupridge): wide merge (%.1fx wider) due to dupridge between f%d and f%d (vertex dist %2.2g), merge dist %2.2g, while processing p%d\n- Allow error with option 'Q12'\n",
ratio, facet1->id, facet2->id, minvertex, mergedist, qh->furthest_id);
if (vertexratio < qh_WIDEpinched)
qh_fprintf(qh, qh->ferr, 8145, "- Experimental option merge-pinched-vertices ('Q14') may avoid this error. It merges nearly adjacent vertices.\n");
if (qh->DELAUNAY)
qh_fprintf(qh, qh->ferr, 8145, "- A bounding box for the input sites may alleviate this error.\n");
if (!qh->ALLOWwide)
qh_errexit2(qh, qh_ERRwide, facet1, facet2);
}
} /* check_dupridge */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="checkconnect">-</a>
qh_checkconnect(qh)
check that new facets are connected
new facets are on qh.newfacet_list
notes:
this is slow and it changes the order of the facets
uses qh.visit_id
design:
move first new facet to end of qh.facet_list
for all newly appended facets
append unvisited neighbors to end of qh.facet_list
for all new facets
report error if unvisited
*/
void qh_checkconnect(qhT *qh /* qh.newfacet_list */) {
facetT *facet, *newfacet, *errfacet= NULL, *neighbor, **neighborp;
facet= qh->newfacet_list;
qh_removefacet(qh, facet);
qh_appendfacet(qh, facet);
facet->visitid= ++qh->visit_id;
FORALLfacet_(facet) {
FOREACHneighbor_(facet) {
if (neighbor->visitid != qh->visit_id) {
qh_removefacet(qh, neighbor);
qh_appendfacet(qh, neighbor);
neighbor->visitid= qh->visit_id;
}
}
}
FORALLnew_facets {
if (newfacet->visitid == qh->visit_id)
break;
qh_fprintf(qh, qh->ferr, 6094, "qhull internal error (qh_checkconnect): f%d is not attached to the new facets\n",
newfacet->id);
errfacet= newfacet;
}
if (errfacet)
qh_errexit(qh, qh_ERRqhull, errfacet, NULL);
} /* checkconnect */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="checkdelfacet">-</a>
qh_checkdelfacet(qh, facet, mergeset )
check that mergeset does not reference facet
*/
void qh_checkdelfacet(qhT *qh, facetT *facet, setT *mergeset) {
mergeT *merge, **mergep;
FOREACHmerge_(mergeset) {
if (merge->facet1 == facet || merge->facet2 == facet) {
qh_fprintf(qh, qh->ferr, 6390, "qhull internal error (qh_checkdelfacet): cannot delete f%d. It is referenced by merge f%d f%d mergetype %d\n",
facet->id, merge->facet1->id, getid_(merge->facet2), merge->mergetype);
qh_errexit2(qh, qh_ERRqhull, merge->facet1, merge->facet2);
}
}
} /* checkdelfacet */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="checkdelridge">-</a>
qh_checkdelridge(qh)
check that qh_delridge_merge is not needed for deleted ridges
notes:
called from qh_mergecycle, qh_makenewfacets, qh_attachnewfacets
errors if qh.vertex_mergeset is non-empty
errors if any visible or new facet has a ridge with r.nonconvex set
assumes that vertex.delfacet is not needed
*/
void qh_checkdelridge(qhT *qh /* qh.visible_facets, vertex_mergeset */) {
facetT *newfacet, *visible;
ridgeT *ridge, **ridgep;
if (!SETempty_(qh->vertex_mergeset)) {
qh_fprintf(qh, qh->ferr, 6382, "qhull internal error (qh_checkdelridge): expecting empty qh.vertex_mergeset in order to avoid calling qh_delridge_merge. Got %d merges\n", qh_setsize(qh, qh->vertex_mergeset));
qh_errexit(qh, qh_ERRqhull, NULL, NULL);
}
FORALLnew_facets {
FOREACHridge_(newfacet->ridges) {
if (ridge->nonconvex) {
qh_fprintf(qh, qh->ferr, 6313, "qhull internal error (qh_checkdelridge): unexpected 'nonconvex' flag for ridge r%d in newfacet f%d. Otherwise need to call qh_delridge_merge\n",
ridge->id, newfacet->id);
qh_errexit(qh, qh_ERRqhull, newfacet, ridge);
}
}
}
FORALLvisible_facets {
FOREACHridge_(visible->ridges) {
if (ridge->nonconvex) {
qh_fprintf(qh, qh->ferr, 6385, "qhull internal error (qh_checkdelridge): unexpected 'nonconvex' flag for ridge r%d in visible facet f%d. Otherwise need to call qh_delridge_merge\n",
ridge->id, visible->id);
qh_errexit(qh, qh_ERRqhull, visible, ridge);
}
}
}
} /* checkdelridge */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="checkzero">-</a>
qh_checkzero(qh, testall )
check that facets are clearly convex for qh.DISTround with qh.MERGEexact
if testall,
test all facets for qh.MERGEexact post-merging
else
test qh.newfacet_list
if qh.MERGEexact,
allows coplanar ridges
skips convexity test while qh.ZEROall_ok
returns:
True if all facets !flipped, !dupridge, normal
if all horizon facets are simplicial
if all vertices are clearly below neighbor
if all opposite vertices of horizon are below
clears qh.ZEROall_ok if any problems or coplanar facets
notes:
called by qh_premerge (qh.CHECKzero, 'C-0') and qh_qhull ('Qx')
uses qh.vertex_visit
horizon facets may define multiple new facets
design:
for all facets in qh.newfacet_list or qh.facet_list
check for flagged faults (flipped, etc.)
for all facets in qh.newfacet_list or qh.facet_list
for each neighbor of facet
skip horizon facets for qh.newfacet_list
test the opposite vertex
if qh.newfacet_list
test the other vertices in the facet's horizon facet
*/
boolT qh_checkzero(qhT *qh, boolT testall) {
facetT *facet, *neighbor;
facetT *horizon, *facetlist;
int neighbor_i, neighbor_n;
vertexT *vertex, **vertexp;
realT dist;
if (testall)
facetlist= qh->facet_list;
else {
facetlist= qh->newfacet_list;
FORALLfacet_(facetlist) {
horizon= SETfirstt_(facet->neighbors, facetT);
if (!horizon->simplicial)
goto LABELproblem;
if (facet->flipped || facet->dupridge || !facet->normal)
goto LABELproblem;
}
if (qh->MERGEexact && qh->ZEROall_ok) {
trace2((qh, qh->ferr, 2011, "qh_checkzero: skip convexity check until first pre-merge\n"));
return True;
}
}
FORALLfacet_(facetlist) {
qh->vertex_visit++;
horizon= NULL;
FOREACHneighbor_i_(qh, facet) {
if (!neighbor_i && !testall) {
horizon= neighbor;
continue; /* horizon facet tested in qh_findhorizon */
}
vertex= SETelemt_(facet->vertices, neighbor_i, vertexT);
vertex->visitid= qh->vertex_visit;
zzinc_(Zdistzero);
qh_distplane(qh, vertex->point, neighbor, &dist);
if (dist >= -2 * qh->DISTround) { /* need 2x for qh_distround and 'Rn' for qh_checkconvex, same as qh.premerge_centrum */
qh->ZEROall_ok= False;
if (!qh->MERGEexact || testall || dist > qh->DISTround)
goto LABELnonconvex;
}
}
if (!testall && horizon) {
FOREACHvertex_(horizon->vertices) {
if (vertex->visitid != qh->vertex_visit) {
zzinc_(Zdistzero);
qh_distplane(qh, vertex->point, facet, &dist);
if (dist >= -2 * qh->DISTround) {
qh->ZEROall_ok= False;
if (!qh->MERGEexact || dist > qh->DISTround)
goto LABELnonconvexhorizon;
}
break;
}
}
}
}
trace2((qh, qh->ferr, 2012, "qh_checkzero: testall %d, facets are %s\n", testall,
(qh->MERGEexact && !testall) ?
"not concave, flipped, or dupridge" : "clearly convex"));
return True;
LABELproblem:
qh->ZEROall_ok= False;
trace2((qh, qh->ferr, 2013, "qh_checkzero: qh_premerge is needed. New facet f%d or its horizon f%d is non-simplicial, flipped, dupridge, or mergehorizon\n",
facet->id, horizon->id));
return False;
LABELnonconvex:
trace2((qh, qh->ferr, 2014, "qh_checkzero: facet f%d and f%d are not clearly convex. v%d dist %.2g\n",
facet->id, neighbor->id, vertex->id, dist));
return False;
LABELnonconvexhorizon:
trace2((qh, qh->ferr, 2060, "qh_checkzero: facet f%d and horizon f%d are not clearly convex. v%d dist %.2g\n",
facet->id, horizon->id, vertex->id, dist));
return False;
} /* checkzero */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="compare_anglemerge">-</a>
qh_compare_anglemerge( mergeA, mergeB )
used by qsort() to order qh.facet_mergeset by mergetype and angle (qh.ANGLEmerge, 'Q1')
lower numbered mergetypes done first (MRGcoplanar before MRGconcave)
notes:
qh_all_merges processes qh.facet_mergeset by qh_setdellast
[mar'19] evaluated various options with eg/q_benchmark and merging of pinched vertices (Q14)
*/
int qh_compare_anglemerge(const void *p1, const void *p2) {
const mergeT *a= *((mergeT *const*)p1), *b= *((mergeT *const*)p2);
if (a->mergetype != b->mergetype)
return (a->mergetype < b->mergetype ? 1 : -1); /* select MRGcoplanar (1) before MRGconcave (3) */
else
return (a->angle > b->angle ? 1 : -1); /* select coplanar merge (1.0) before sharp merge (-0.5) */
} /* compare_anglemerge */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="compare_facetmerge">-</a>
qh_compare_facetmerge( mergeA, mergeB )
used by qsort() to order merges by mergetype, first merge, first
lower numbered mergetypes done first (MRGcoplanar before MRGconcave)
if same merge type, flat merges are first
notes:
qh_all_merges processes qh.facet_mergeset by qh_setdellast
[mar'19] evaluated various options with eg/q_benchmark and merging of pinched vertices (Q14)
*/
int qh_compare_facetmerge(const void *p1, const void *p2) {
const mergeT *a= *((mergeT *const*)p1), *b= *((mergeT *const*)p2);
if (a->mergetype != b->mergetype)
return (a->mergetype < b->mergetype ? 1 : -1); /* select MRGcoplanar (1) before MRGconcave (3) */
else if (a->mergetype == MRGanglecoplanar)
return (a->angle > b->angle ? 1 : -1); /* if MRGanglecoplanar, select coplanar merge (1.0) before sharp merge (-0.5) */
else
return (a->distance < b->distance ? 1 : -1); /* select flat (0.0) merge before wide (1e-10) merge */
} /* compare_facetmerge */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="comparevisit">-</a>
qh_comparevisit( vertexA, vertexB )
used by qsort() to order vertices by their visitid
notes:
only called by qh_find_newvertex
*/
int qh_comparevisit(const void *p1, const void *p2) {
const vertexT *a= *((vertexT *const*)p1), *b= *((vertexT *const*)p2);
if (a->visitid > b->visitid)
return 1;
return -1;
} /* comparevisit */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="copynonconvex">-</a>
qh_copynonconvex(qh, atridge )
set non-convex flag on other ridges (if any) between same neighbors
notes:
may be faster if use smaller ridge set
design:
for each ridge of atridge's top facet
if ridge shares the same neighbor
set nonconvex flag
*/
void qh_copynonconvex(qhT *qh, ridgeT *atridge) {
facetT *facet, *otherfacet;
ridgeT *ridge, **ridgep;
facet= atridge->top;
otherfacet= atridge->bottom;
atridge->nonconvex= False;
FOREACHridge_(facet->ridges) {
if (otherfacet == ridge->top || otherfacet == ridge->bottom) {
if (ridge != atridge) {
ridge->nonconvex= True;
trace4((qh, qh->ferr, 4020, "qh_copynonconvex: moved nonconvex flag from r%d to r%d between f%d and f%d\n",
atridge->id, ridge->id, facet->id, otherfacet->id));
break;
}
}
}
} /* copynonconvex */
/*-<a href="qh-merge_r.htm#TOC"
>-------------------------------</a><a name="degen_redundant_facet">-</a>
qh_degen_redundant_facet(qh, facet )
check for a degenerate (too few neighbors) or redundant (subset of vertices) facet
notes:
called at end of qh_mergefacet, qh_renamevertex, and qh_reducevertices
bumps vertex_visit
called if a facet was redundant but no longer is (qh_merge_degenredundant)
qh_appendmergeset() only appends first reference to facet (i.e., redundant)
see: qh_test_redundant_neighbors, qh_maydropneighbor
design:
test for redundant neighbor
test for degenerate facet
*/
void qh_degen_redundant_facet(qhT *qh, facetT *facet) {
vertexT *vertex, **vertexp;
facetT *neighbor, **neighborp;
trace3((qh, qh->ferr, 3028, "qh_degen_redundant_facet: test facet f%d for degen/redundant\n",
facet->id));
if (facet->flipped) {
trace2((qh, qh->ferr, 3074, "qh_degen_redundant_facet: f%d is flipped, will merge later\n", facet->id));
return;
}
FOREACHneighbor_(facet) {
if (neighbor->flipped) /* disallow merge of non-flipped into flipped, neighbor will be merged later */
continue;
if (neighbor->visible) {
qh_fprintf(qh, qh->ferr, 6357, "qhull internal error (qh_degen_redundant_facet): facet f%d has deleted neighbor f%d (qh.visible_list)\n",
facet->id, neighbor->id);
qh_errexit2(qh, qh_ERRqhull, facet, neighbor);
}
qh->vertex_visit++;
FOREACHvertex_(neighbor->vertices)
vertex->visitid= qh->vertex_visit;
FOREACHvertex_(facet->vertices) {
if (vertex->visitid != qh->vertex_visit)
break;
}
if (!vertex) {
trace2((qh, qh->ferr, 2015, "qh_degen_redundant_facet: f%d is contained in f%d. merge\n", facet->id, neighbor->id));
qh_appendmergeset(qh, facet, neighbor, MRGredundant, 0.0, 1.0);