45 #if defined (HAVE_QHULL)
47 # if defined (NEED_QHULL_VERSION)
48 char qh_version[] =
"convhulln.oct 2007-07-24";
65 if (dim > maxval || n > maxval)
67 error (
"%s: dimension too large for Qhull", who);
77 @deftypefn {Loadable Function} {@var{h} =} convhulln (@var{pts})\n\
78 @deftypefnx {Loadable Function} {@var{h} =} convhulln (@var{pts}, @var{options})\n\
79 @deftypefnx {Loadable Function} {[@var{h}, @var{v}] =} convhulln (@dots{})\n\
80 Compute the convex hull of the set of points @var{pts} which is a matrix\n\
81 of size [n, dim] containing n points in a space of dimension dim.\n\
82 The hull @var{h} is an index vector into the set of points and specifies\n\
83 which points form the enclosing hull.\n\
85 An optional second argument, which must be a string or cell array of strings,\n\
86 contains options passed to the underlying qhull command.\n\
87 See the documentation for the Qhull library for details\n\
88 @url{http://www.qhull.org/html/qh-quick.htm#options}.\n\
89 The default options depend on the dimension of the input:\n\
92 @item 2D, 3D, 4D: @var{options} = @code{@{\"Qt\"@}}\n\
94 @item 5D and higher: @var{options} = @code{@{\"Qt\", \"Qx\"@}}\n\
97 If @var{options} is not present or @code{[]} then the default arguments are\n\
98 used. Otherwise, @var{options} replaces the default argument list.\n\
99 To append user options to the defaults it is necessary to repeat the\n\
100 default arguments in @var{options}. Use a null string to pass no arguments.\n\
102 If the second output @var{v} is requested the volume of the enclosing\n\
103 convex hull is calculated.\n\n\
104 @seealso{convhull, delaunayn, voronoin}\n\
109 #if defined (HAVE_QHULL)
111 int nargin = args.
length ();
112 if (nargin < 1 || nargin > 2)
118 Matrix points (args(0).matrix_value ());
136 if (args(1).is_string ())
137 options =
" " + args(1).string_value ();
138 else if (args(1).is_empty ())
140 else if (args(1).is_cellstr ())
147 options +=
" " + tmp(i);
151 error (
"convhulln: OPTIONS must be a string, cell array of strings, or empty");
156 boolT ismalloc =
false;
161 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)
162 FILE *outfile = gnulib::fopen (
"NUL",
"w");
164 FILE *outfile = gnulib::fopen (
"/dev/null",
"w");
166 FILE *errfile = stderr;
172 error (
"convhulln: unable to create temporary file for output");
178 std::string cmd =
"qhull" + options;
182 strcpy (cmd_str, cmd.c_str ());
184 int exitcode = qh_new_qhull (dim, num_points, points.
fortran_vec (),
185 ismalloc, cmd_str, outfile, errfile);
188 bool nonsimp_seen =
false;
202 if (! (nonsimp_seen || facet->simplicial || qh hull_dim == 2))
206 if (cmd.find (
"QJ") != std::string::npos)
209 error (
"convhulln: qhull failed: option 'QJ' returned non-simplicial facet");
216 setT *vertices = qh_facet3vertex (facet);
218 vertexT *vertex, **vertexp;
220 FOREACHvertex_ (vertices)
221 idx(i, j++) = 1 + qh_pointid(vertex->point);
223 qh_settempfree (&vertices);
227 if (facet->toporient ^ qh_ORIENTclock)
229 vertexT *vertex, **vertexp;
231 FOREACHvertex_ (facet->vertices)
232 idx(i, j++) = 1 + qh_pointid(vertex->point);
236 vertexT *vertex, **vertexp;
238 FOREACHvertexreverse12_ (facet->vertices)
239 idx(i, j++) = 1 + qh_pointid(vertex->point);
243 warning (
"convhulln: facet %d only has %d vertices", i, j);
251 idx.
resize (nf, dim, 0.0);
265 if (facet->upperdelaunay && qh ATinfinity)
268 facet->f.area = area = qh_facetarea (facet);
269 facet->isarea = True;
273 if (facet->upperdelaunay == qh UPPERdelaunay)
279 qh_distplane (qh interior_point, facet, &dist);
280 qh totvol += -dist * area/ qh hull_dim;
290 error (
"convhulln: qhull failed");
293 qh_freeqhull (! qh_ALL);
295 int curlong, totlong;
296 qh_memfreeshort (&curlong, &totlong);
298 if (curlong || totlong)
299 warning (
"convhulln: did not free %d bytes of long memory (%d pieces)",
303 error (
"convhulln: not available in this version of Octave");