1 /*<html><pre>  -<a                             href="../libqhull_r/qh-qhull_r.htm"
2   >-------------------------------</a><a name="TOP">-</a>
3 
4    unix_r.c
5      command line interface to qhull
6          includes SIOUX interface for Macintoshes
7 
8    see qh-qhull.htm
9 
10    Copyright (c) 1993-2019 The Geometry Center.
11    $Id: //main/2019/qhull/src/qhull/unix_r.c#4 $$Change: 2710 $
12    $DateTime: 2019/06/27 14:24:04 $$Author: bbarber $
13 */
14 
15 #include "libqhull_r/libqhull_r.h"
16 
17 #include <ctype.h>
18 #include <math.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25   int isatty(int);
26 }
27 
28 #elif defined(_MSC_VER)
29 #include <io.h>
30 #define isatty _isatty
31 /* int _isatty(int); */
32 
33 #else
34 int isatty(int);  /* returns 1 if stdin is a tty
35                    if "Undefined symbol" this can be deleted along with call in main() */
36 #endif
37 
38 /*-<a                             href="../libqhull_r/qh-qhull_r.htm#TOC"
39   >-------------------------------</a><a name="prompt">-</a>
40 
41   qh_prompt
42     long prompt for qhull
43 
44   notes:
45     see concise prompt below
46     same text as qconvex_r.c, qdelanay_r.c, qhalf_r.c, qvoronoi_r.c
47     limit maximum literal to 1800 characters
48 */
49 char qh_prompta[]= "\n\
50 qhull -- compute convex hulls and related structures.\n\
51     http://www.qhull.org  %s\n\
52 \n\
53 input (stdin):\n\
54     first lines: dimension and number of points (or vice-versa).\n\
55     other lines: point coordinates, best if one point per line\n\
56     comments:    start with a non-numeric character\n\
57     halfspaces:  use dim plus one and put offset after coefficients.\n\
58                  May be preceded by a single interior point ('H').\n\
59 \n\
60 options:\n\
61     d    - Delaunay triangulation by lifting points to a paraboloid\n\
62     d Qu - furthest-site Delaunay triangulation (upper convex hull)\n\
63     Hn,n,... - halfspace intersection about point [n,n,0,...]\n\
64     Qc   - keep coplanar points with nearest facet\n\
65     Qi   - keep interior points with nearest facet\n\
66     QJ   - joggled input instead of merged facets\n\
67     Qt   - triangulated output\n\
68     v    - Voronoi diagram (dual of the Delaunay triangulation)\n\
69     v Qu - furthest-site Voronoi diagram\n\
70 \n\
71 Qhull control options:\n\
72     Qa   - allow input with fewer or more points than coordinates\n\
73     Qbk:n   - scale coord k so that low bound is n\n\
74       QBk:n - scale coord k so that upper bound is n (QBk is %2.2g)\n\
75     QbB  - scale input to unit cube centered at the origin\n\
76     Qbb  - scale last coordinate to [0,m] for Delaunay triangulations\n\
77     Qbk:0Bk:0 - remove k-th coordinate from input\n\
78     QJn  - randomly joggle input in range [-n,n]\n\
79     QRn  - random rotation (n=seed, n=0 time, n=-1 time/no rotate)\n\
80     Qs   - search all points for the initial simplex\n\
81     Qu   - for 'd' or 'v', compute upper hull without point at-infinity\n\
82               returns furthest-site Delaunay triangulation\n\
83     QVn  - good facet if it includes point n, -n if not\n\
84     Qx   - exact pre-merges (skips coplanar and angle-coplanar facets)\n\
85     Qz   - add point-at-infinity to Delaunay triangulation\n\
86 \n\
87 %s%s%s%s";  /* split up qh_prompt for Visual C++ */
88 char qh_promptb[]= "\
89 Qhull extra options:\n\
90     Qf   - partition point to furthest outside facet\n\
91     Qg   - only build good facets (needs 'QGn', 'QVn', or 'PdD')\n\
92     QGn  - good facet if visible from point n, -n for not visible\n\
93     Qm   - only process points that would increase max_outside\n\
94     Qr   - process random outside points instead of furthest ones\n\
95     Qv   - test vertex neighbors for convexity\n\
96     Qw   - allow option warnings\n\
97     Q0   - turn off default premerge with 'C-0'/'Qx'\n\
98     Q1   - merge by mergetype/angle instead of mergetype/distance\n\
99     Q2   - merge all coplanar facets instead of merging independent sets\n\
100     Q3   - do not merge redundant vertices\n\
101     Q4   - avoid old->new merges\n\
102     Q5   - do not correct outer planes at end of qhull\n\
103     Q6   - do not pre-merge concave or coplanar facets\n\
104     Q7   - depth-first processing instead of breadth-first\n\
105     Q8   - do not process near-inside points\n\
106     Q9   - process furthest of furthest points\n\
107     Q10  - no special processing for narrow distributions\n\
108     Q11  - copy normals and recompute centrums for tricoplanar facets\n\
109     Q12  - allow wide facets and wide dupridge\n\
110     Q14  - merge pinched vertices that create a dupridge\n\
111     Q15  - check for duplicate ridges with the same vertices\n\
112 \n\
113 T options:\n\
114     TFn  - report summary when n or more facets created\n\
115     TI file - input file, may be enclosed in single quotes\n\
116     TO file - output file, may be enclosed in single quotes\n\
117     Ts   - statistics\n\
118     Tv   - verify result: structure, convexity, and point inclusion\n\
119     Tz   - send all output to stdout\n\
120 \n\
121 ";
122 char qh_promptc[]= "\
123 Trace options:\n\
124     T4   - trace at level n, 4=all, 5=mem/gauss, -1= events\n\
125     Ta   - annotate output with message codes\n\
126     TAn  - stop qhull after adding n vertices\n\
127      TCn - stop qhull after building cone for point n\n\
128      TVn - stop qhull after adding point n, -n for before\n\
129     Tc   - check frequently during execution\n\
130     Tf   - flush each qh_fprintf for debugging segfaults\n\
131     TPn  - turn on tracing when point n added to hull\n\
132      TP-1  turn on tracing after qh_buildhull and qh_postmerge\n\
133      TMn - turn on tracing at merge n\n\
134      TWn - trace merge facets when width > n\n\
135     TRn  - rerun qhull n times for statistics to adjust 'QJn'\n\
136 \n\
137 Precision options:\n\
138     Cn   - radius of centrum (roundoff added).  Merge facets if non-convex\n\
139      An  - cosine of maximum angle.  Merge facets if cosine > n or non-convex\n\
140            C-0 roundoff, A-0.99/C-0.01 pre-merge, A0.99/C0.01 post-merge\n\
141     En   - max roundoff error for distance computation\n\
142     Rn   - randomly perturb computations by a factor of [1-n,1+n]\n\
143     Vn   - min distance above plane for a visible facet (default 3C-n or En)\n\
144     Un   - max distance below plane for a new, coplanar point (default Vn)\n\
145     Wn   - min facet width for outside point (before roundoff, default 2Vn)\n\
146 \n\
147 Output formats (may be combined; if none, produces a summary to stdout):\n\
148     f    - facet dump\n\
149     G    - Geomview output (see below)\n\
150     i    - vertices incident to each facet\n\
151     m    - Mathematica output (2-d and 3-d)\n\
152     n    - normals with offsets\n\
153     o    - OFF format (dim, points and facets; Voronoi regions)\n\
154     p    - vertex coordinates or Voronoi vertices (coplanar points if 'Qc')\n\
155     s    - summary (stderr)\n\
156 \n\
157 ";
158 char qh_promptd[]= "\
159 More formats:\n\
160     Fa   - area for each facet\n\
161     FA   - compute total area and volume for option 's'\n\
162     Fc   - count plus coplanar points for each facet\n\
163            use 'Qc' (default) for coplanar and 'Qi' for interior\n\
164     FC   - centrum or Voronoi center for each facet\n\
165     Fd   - use cdd format for input (homogeneous with offset first)\n\
166     FD   - use cdd format for numeric output (offset first)\n\
167     FF   - facet dump without ridges\n\
168     Fi   - inner plane for each facet\n\
169            for 'v', separating hyperplanes for bounded Voronoi regions\n\
170     FI   - ID of each facet\n\
171     Fm   - merge count for each facet (511 max)\n\
172     FM   - Maple output (2-d and 3-d)\n\
173     Fn   - count plus neighboring facets for each facet\n\
174     FN   - count plus neighboring facets for each point\n\
175     Fo   - outer plane (or max_outside) for each facet\n\
176            for 'v', separating hyperplanes for unbounded Voronoi regions\n\
177     FO   - options and precision constants\n\
178     Fp   - dim, count, and intersection coordinates (halfspace only)\n\
179     FP   - nearest vertex and distance for each coplanar point\n\
180     FQ   - command used for qhull\n\
181     Fs   - summary: #int (8), dimension, #points, tot vertices, tot facets,\n\
182                       output: #vertices, #facets, #coplanars, #nonsimplicial\n\
183                     #real (2), max outer plane, min vertex\n\
184     FS   - sizes:   #int (0)\n\
185                     #real (2) tot area, tot volume\n\
186     Ft   - triangulation with centrums for non-simplicial facets (OFF format)\n\
187     Fv   - count plus vertices for each facet\n\
188            for 'v', Voronoi diagram as Voronoi vertices for pairs of sites\n\
189     FV   - average of vertices (a feasible point for 'H')\n\
190     Fx   - extreme points (in order for 2-d)\n\
191 \n\
192 ";
193 char qh_prompte[]= "\
194 Geomview output (2-d, 3-d, and 4-d; 2-d Voronoi)\n\
195     Ga   - all points as dots\n\
196      Gp  -  coplanar points and vertices as radii\n\
197      Gv  -  vertices as spheres\n\
198     Gc   - centrums\n\
199     GDn  - drop dimension n in 3-d and 4-d output\n\
200     Gh   - hyperplane intersections\n\
201     Gi   - inner planes only\n\
202      Gn  -  no planes\n\
203      Go  -  outer planes only\n\
204     Gr   - ridges\n\
205     Gt   - for 3-d 'd', transparent outer ridges\n\
206 \n\
207 Print options:\n\
208     PAn  - keep n largest facets by area\n\
209     Pdk:n - drop facet if normal[k] <= n (default 0.0)\n\
210     PDk:n - drop facet if normal[k] >= n\n\
211     PFn  - keep facets whose area is at least n\n\
212     Pg   - print good facets (needs 'QGn' or 'QVn')\n\
213     PG   - print neighbors of good facets\n\
214     PMn  - keep n facets with most merges\n\
215     Po   - force output.  If error, output neighborhood of facet\n\
216     Pp   - do not report precision problems\n\
217 \n\
218     .    - list of all options\n\
219     -    - one line descriptions of all options\n\
220     -?   - help with examples\n\
221     -V   - version\n\
222 ";
223 /* for opts, don't assign 'e' or 'E' to a flag (already used for exponent) */
224 
225 /*-<a                             href="../libqhull_r/qh-qhull_r.htm#TOC"
226   >-------------------------------</a><a name="prompt2">-</a>
227 
228   qh_prompt2
229     synopsis for qhull
230 
231   notes:
232     limit maximum literal to 1800 characters
233 */
234 char qh_prompt2a[]= "\n\
235 qhull -- compute convex hulls and related structures.  Qhull %s\n\
236     input (stdin): dimension, number of points, point coordinates\n\
237     comments start with a non-numeric character\n\
238     halfspace: use dim+1 and put offsets after coefficients\n\
239 \n\
240 options (qh-quick.htm):\n\
241     d    - Delaunay triangulation by lifting points to a paraboloid\n\
242     d Qu - furthest-site Delaunay triangulation (upper convex hull)\n\
243     v    - Voronoi diagram as the dual of the Delaunay triangulation\n\
244     v Qu - furthest-site Voronoi diagram\n\
245     H1,1 - Halfspace intersection about [1,1,0,...] via polar duality\n\
246     Qt   - triangulated output\n\
247     QJ   - joggled input instead of merged facets\n\
248     Tv   - verify result: structure, convexity, and point inclusion\n\
249     .    - concise list of all options\n\
250     -    - one-line description of each option\n\
251     -?   - this message\n\
252     -V   - version\n\
253 \n\
254 Output options (subset):\n\
255     s    - summary of results (default)\n\
256     i    - vertices incident to each facet\n\
257     n    - normals with offsets\n\
258     p    - vertex coordinates (if 'Qc', includes coplanar points)\n\
259            if 'v', Voronoi vertices\n\
260     FA   - report total area and volume\n\
261     Fp   - halfspace intersections\n\
262     FS   - total area and volume\n\
263     Fx   - extreme points (convex hull vertices)\n\
264     G    - Geomview output (2-d, 3-d and 4-d)\n\
265     m    - Mathematica output (2-d and 3-d)\n\
266     o    - OFF format (if 'v', outputs Voronoi regions)\n\
267     QVn  - print facets that include point n, -n if not\n\
268     TI file - input file, may be enclosed in single quotes\n\
269     TO file - output file, may be enclosed in single quotes\n\
270 \n\
271 %s";  /* split literal */
272 char qh_prompt2b[]= "\
273 examples:\n\
274     rbox D4 | qhull Tv                        rbox 1000 s | qhull Tv s FA\n\
275     rbox 10 D2 | qhull d QJ s i TO result     rbox 10 D2 | qhull v Qbb Qt p\n\
276     rbox 10 D2 | qhull d Qu QJ m              rbox 10 D2 | qhull v Qu QJ o\n\
277     rbox c d D2 | qhull Qc s f Fx | more      rbox c | qhull FV n | qhull H Fp\n\
278     rbox d D12 | qhull QR0 FA                 rbox c D7 | qhull FA TF1000\n\
279     rbox y 1000 W0 | qhull Qc                 rbox c | qhull n\n\
280 \n\
281 ";
282 /* for opts, don't assign 'e' or 'E' to a flag (already used for exponent) */
283 
284 /*-<a                             href="../libqhull_r/qh-qhull_r.htm#TOC"
285   >-------------------------------</a><a name="prompt3">-</a>
286 
287   qh_prompt3
288     concise prompt for qhull
289 
290   notes:
291     limit maximum literal to 1800 characters
292 */
293 char qh_prompt3a[]= "\n\
294 Qhull %s\n\
295 Except for 'F.' and 'PG', upper-case options take an argument.\n\
296 \n\
297  delaunay       facet-dump     Geomview       H0,0-interior  Halfspace\n\
298  incidences     mathematica    normals        off-format     points\n\
299  summary        voronoi\n\
300 \n\
301  Farea          FArea-total    Fcoplanars     FCentrums      Fd-cdd-in\n\
302  FD-cdd-out     FFacets-xridge Finner         FIDs           Fmerges\n\
303  FMaple         Fneighbors     FNeigh-vertex  Fouter         FOptions\n\
304  Fpoint-intersect  FPoint-near FQhull         Fsummary       FSize\n\
305  Ftriangles     Fvertices      Fvoronoi       FVertex-ave    Fxtremes\n\
306 \n\
307  Gall-points    Gcentrums      GDrop-dim      Ghyperplanes   Ginner\n\
308  Gno-planes     Gouter         Gpoints        Gridges        Gtransparent\n\
309  Gvertices\n\
310 \n\
311  PArea-keep     Pdrop-d0:0D0   PFacet-area-keep  Pgood       PGood-neighbors\n\
312  PMerge-keep    Poutput-forced Pprecision-not\n\
313 \n\
314  Qallow-short   QbBound-0:0.5  QbB-scale-box  Qbb-scale-last Qbk:0Bk:0-drop\n\
315  Qcoplanar      Qinterior      QJoggle        QRotate        Qsearch-all\n\
316  Qtriangulate   QupperDelaunay Qwarn-allow    Qxact-merge    Qzinfinite\n\
317 \n\
318  Qfurthest      Qgood-only     QGood-point    Qmax-outside   Qrandom\n\
319  Qvneighbors    QVertex-good\n\
320 \n\
321 %s"; /* split literal */
322 char qh_prompt3b[]= "\
323  Q0-no-premerge Q1-angle-merge     Q2-no-independ  Q3-no-redundant\n\
324  Q4-no-old      Q5-no-check-out    Q6-no-concave   Q7-depth-first\n\
325  Q8-no-near-in  Q9-pick-furthest   Q10-no-narrow   Q11-trinormals\n\
326  Q12-allow-wide Q14-merge-pinched  Q15-duplicates\n\
327 \n\
328  TFacet-log     TInput-file    TOutput-file   Tstatistics    Tverify\n\
329  Tz-stdout\n\
330 \n\
331  T4-trace       Tannotate      TAdd-stop      Tcheck-often   TCone-stop\n\
332  Tflush         TMerge-trace   TPoint-trace   TRerun         TVertex-stop\n\
333  TWide-trace\n\
334 \n\
335  Angle-max      Centrum-size   Error-round    Random-dist    Ucoplanar-max\n\
336  Visible-min    Wide-outside\n\
337 ";
338 
339 /*-<a                             href="../libqhull_r/qh-qhull_r.htm#TOC"
340   >-------------------------------</a><a name="main">-</a>
341 
342   main( argc, argv )
343     processes the command line, calls qhull() to do the work, and exits
344 
345   design:
346     initializes data structures
347     reads points
348     finishes initialization
349     computes convex hull and other structures
350     checks the result
351     writes the output
352     frees memory
353 */
main(int argc,char * argv[])354 int main(int argc, char *argv[]) {
355   int curlong, totlong; /* used !qh_NOmem */
356   int exitcode, numpoints, dim;
357   coordT *points;
358   boolT ismalloc;
359   qhT qh_qh;
360   qhT *qh= &qh_qh;
361 
362   QHULL_LIB_CHECK /* Check for compatible library */
363 
364   if ((argc == 1) && isatty( 0 /*stdin*/)) {
365     fprintf(stdout, qh_prompt2a, qh_version, qh_prompt2b);
366     exit(qh_ERRnone);
367   }
368   if (argc > 1 && *argv[1] == '-' && (*(argv[1] + 1) == '?' || *(argv[1] + 1) == '-')) { /* -? or --help */
369     fprintf(stdout, qh_prompt2a, qh_version, qh_prompt2b);
370     exit(qh_ERRnone);
371   }
372   if (argc > 1 && *argv[1] == '-' && (*(argv[1] + 1) == '?' || *(argv[1] + 1) == '-')) { /* -? or --help */
373     fprintf(stdout, qh_prompt2a, qh_version, qh_prompt2b);
374     exit(qh_ERRnone);
375   }
376   if (argc > 1 && *argv[1] == '-' && !*(argv[1]+1)) {
377     fprintf(stdout, qh_prompta, qh_version, qh_DEFAULTbox,
378                 qh_promptb, qh_promptc, qh_promptd, qh_prompte);
379     exit(qh_ERRnone);
380   }
381   if (argc > 1 && *argv[1] == '.' && !*(argv[1]+1)) {
382     fprintf(stdout, qh_prompt3a, qh_version, qh_prompt3b);
383     exit(qh_ERRnone);
384   }
385   if (argc > 1 && *argv[1] == '-' && *(argv[1]+1)=='V') {
386       fprintf(stdout, "%s\n", qh_version2);
387       exit(qh_ERRnone);
388   }
389   qh_init_A(qh, stdin, stdout, stderr, argc, argv);  /* sets qh->qhull_command */
390   exitcode= setjmp(qh->errexit); /* simple statement for CRAY J916 */
391   if (!exitcode) {
392     qh->NOerrexit = False;
393     qh_initflags(qh, qh->qhull_command);
394     points= qh_readpoints(qh, &numpoints, &dim, &ismalloc);
395     qh_init_B(qh, points, numpoints, dim, ismalloc);
396     qh_qhull(qh);
397     qh_check_output(qh);
398     qh_produce_output(qh);
399     if (qh->VERIFYoutput && !qh->FORCEoutput && !qh->STOPadd && !qh->STOPcone && !qh->STOPpoint)
400       qh_check_points(qh);
401     exitcode= qh_ERRnone;
402   }
403   qh->NOerrexit= True;  /* no more setjmp */
404 #ifdef qh_NOmem
405   qh_freeqhull(qh, qh_ALL);
406 #else
407   qh_freeqhull(qh, !qh_ALL);
408   qh_memfreeshort(qh, &curlong, &totlong);
409   if (curlong || totlong)
410     qh_fprintf_stderr(7079, "qhull internal warning (main): did not free %d bytes of long memory(%d pieces)\n",
411        totlong, curlong);
412 #endif
413   return exitcode;
414 } /* main */
415 
416