3Copyright (C) 2000, 2007, 2008 Kai Habel
5This file is part of Octave.
7Octave is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
12Octave is distributed in the hope that it will be useful, but WITHOUT
13ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17You should have received a copy of the GNU General Public License
18along with Octave; see the file COPYING. If not, see
19<http://www.gnu.org/licenses/>.
2420. Augiust 2000 - Kai Habel: first release
282003-12-14 Rafael Laboissiere <rafael@laboissiere.net>
29Added optional second argument to pass options to the underlying
48#include <qhull/qhull_a.h>
51#ifdef NEED_QHULL_VERSION
52char qh_version[] = "__voronoi__.oct 2007-07-24";
56DEFUN_DLD (__voronoi__, args, ,
58@deftypefn {Loadable Function} {@var{tri} =} __voronoi__ (@var{point})\n\
59@deftypefnx {Loadable Function} {@var{tri} =} __voronoi__ (@var{point}, @var{options})\n\
60Undocumented internal function.\n\
63 octave_value_list retval;
69 int nargin = args.length ();
70 if (nargin < 1 || nargin > 2)
80 if (! args (1).is_string ())
82 error ("__voronoi__: second argument must be a string");
86 options = args (1).string_value().c_str ();
91 Matrix p (args(0).matrix_value ());
93 const octave_idx_type dim = p.columns ();
94 const octave_idx_type np = p.rows ();
97 double *pt_array = p.fortran_vec ();
99 //double pt_array[dim * np];
100 //for (int i = 0; i < np; i++)
102 // for (int j = 0; j < dim; j++)
104 // pt_array[j+i*dim] = p(i,j);
108 boolT ismalloc = false;
110 OCTAVE_LOCAL_BUFFER (char, flags, 250);
112 // hmm lot's of options for qhull here
113 sprintf (flags, "qhull v Fv T0 %s", options);
115 // If you want some debugging information replace the 0 pointer
116 // with stdout or some other file open for writing.
119 FILE *errfile = stderr;
121 if (! qh_new_qhull (dim, np, pt_array, ismalloc, flags, outfile, errfile))
126 octave_idx_type i = 0, n = 1, k = 0, m = 0, fidx = 0, j = 0, r = 0;
127 OCTAVE_LOCAL_BUFFER (octave_idx_type, ni, np);
129 for (i = 0; i < np; i++)
131 qh_setvoronoi_all ();
132 bool infinity_seen = false;
133 facetT *neighbor, **neighborp;
134 coordT *voronoi_vertex;
143 if (qh hull_dim == 3)
144 qh_order_vertexneighbors (vertex);
145 infinity_seen = false;
147 FOREACHneighbor_ (vertex)
149 if (! neighbor->upperdelaunay)
151 if (! neighbor->seen)
154 neighbor->seen = true;
158 else if (! infinity_seen)
160 infinity_seen = true;
168 for (octave_idx_type d = 0; d < dim; d++)
171 boolMatrix AtInf (np, 1);
172 for (i = 0; i < np; i++)
174 octave_value_list F (np, octave_value ());
185 if (qh hull_dim == 3)
186 qh_order_vertexneighbors(vertex);
187 infinity_seen = false;
188 RowVector facet_list (ni[k++]);
191 FOREACHneighbor_(vertex)
193 if (neighbor->upperdelaunay)
197 infinity_seen = true;
204 if (! neighbor->seen)
206 voronoi_vertex = neighbor->center;
209 for (octave_idx_type d = 0; d < dim; d++)
211 v(i,d) = *(voronoi_vertex++);
213 neighbor->seen = true;
214 neighbor->visitid = i;
216 facet_list(m++) = neighbor->visitid + 1;
224 for (i = 0; i < r; i++)
233 qh_freeqhull (! qh_ALL);
235 // free short memory and memory allocator
236 int curlong, totlong;
237 qh_memfreeshort (&curlong, &totlong);
239 if (curlong || totlong)
240 warning ("__voronoi__: did not free %d bytes of long memory (%d pieces)", totlong, curlong);
243 error ("__voronoi__: qhull failed");
246 error ("__voronoi__: not available in this version of Octave");