changelog shortlog tags changeset files revisions annotate raw

src/DLD-FUNCTIONS/__voronoi__.cc

changeset 10289: 4b124317dc38
parent:2c279308f6ab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (67 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1/*
2
3Copyright (C) 2000, 2007, 2008 Kai Habel
4
5This file is part of Octave.
6
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.
11
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
15for more details.
16
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/>.
20
21*/
22
23/*
2420. Augiust 2000 - Kai Habel: first release
25*/
26
27/*
282003-12-14 Rafael Laboissiere <rafael@laboissiere.net>
29Added optional second argument to pass options to the underlying
30qhull command
31*/
32
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include <cstdio>
38
39#include "lo-ieee.h"
40
41#include "Cell.h"
42#include "defun-dld.h"
43#include "error.h"
44#include "oct-obj.h"
45
46#ifdef HAVE_QHULL
47extern "C" {
48#include <qhull/qhull_a.h>
49}
50
51#ifdef NEED_QHULL_VERSION
52char qh_version[] = "__voronoi__.oct 2007-07-24";
53#endif
54#endif
55
56DEFUN_DLD (__voronoi__, args, ,
57 "-*- texinfo -*-\n\
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\
61@end deftypefn")
62{
63 octave_value_list retval;
64
65#ifdef HAVE_QHULL
66
67 retval(0) = 0.0;
68
69 int nargin = args.length ();
70 if (nargin < 1 || nargin > 2)
71 {
72 print_usage ();
73 return retval;
74 }
75
76 const char *options;
77
78 if (nargin == 2)
79 {
80 if (! args (1).is_string ())
81 {
82 error ("__voronoi__: second argument must be a string");
83 return retval;
84 }
85
86 options = args (1).string_value().c_str ();
87 }
88 else
89 options = "";
90
91 Matrix p (args(0).matrix_value ());
92
93 const octave_idx_type dim = p.columns ();
94 const octave_idx_type np = p.rows ();
95 p = p.transpose ();
96
97 double *pt_array = p.fortran_vec ();
98
99 //double pt_array[dim * np];
100 //for (int i = 0; i < np; i++)
101 // {
102 // for (int j = 0; j < dim; j++)
103 // {
104 // pt_array[j+i*dim] = p(i,j);
105 // }
106 // }
107
108 boolT ismalloc = false;
109
110 OCTAVE_LOCAL_BUFFER (char, flags, 250);
111
112 // hmm lot's of options for qhull here
113 sprintf (flags, "qhull v Fv T0 %s", options);
114
115 // If you want some debugging information replace the 0 pointer
116 // with stdout or some other file open for writing.
117
118 FILE *outfile = 0;
119 FILE *errfile = stderr;
120
121 if (! qh_new_qhull (dim, np, pt_array, ismalloc, flags, outfile, errfile))
122 {
123 facetT *facet;
124 vertexT *vertex;
125
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);
128
129 for (i = 0; i < np; i++)
130 ni[i] = 0;
131 qh_setvoronoi_all ();
132 bool infinity_seen = false;
133 facetT *neighbor, **neighborp;
134 coordT *voronoi_vertex;
135
136 FORALLfacets
137 {
138 facet->seen = false;
139 }
140
141 FORALLvertices
142 {
143 if (qh hull_dim == 3)
144 qh_order_vertexneighbors (vertex);
145 infinity_seen = false;
146
147 FOREACHneighbor_ (vertex)
148 {
149 if (! neighbor->upperdelaunay)
150 {
151 if (! neighbor->seen)
152 {
153 n++;
154 neighbor->seen = true;
155 }
156 ni[k]++;
157 }
158 else if (! infinity_seen)
159 {
160 infinity_seen = true;
161 ni[k]++;
162 }
163 }
164 k++;
165 }
166
167 Matrix v (n, dim);
168 for (octave_idx_type d = 0; d < dim; d++)
169 v(0,d) = octave_Inf;
170
171 boolMatrix AtInf (np, 1);
172 for (i = 0; i < np; i++)
173 AtInf(i) = false;
174 octave_value_list F (np, octave_value ());
175 k = 0;
176 i = 0;
177
178 FORALLfacets
179 {
180 facet->seen = false;
181 }
182
183 FORALLvertices
184 {
185 if (qh hull_dim == 3)
186 qh_order_vertexneighbors(vertex);
187 infinity_seen = false;
188 RowVector facet_list (ni[k++]);
189 m = 0;
190
191 FOREACHneighbor_(vertex)
192 {
193 if (neighbor->upperdelaunay)
194 {
195 if (! infinity_seen)
196 {
197 infinity_seen = true;
198 facet_list(m++) = 1;
199 AtInf(j) = true;
200 }
201 }
202 else
203 {
204 if (! neighbor->seen)
205 {
206 voronoi_vertex = neighbor->center;
207 fidx = neighbor->id;
208 i++;
209 for (octave_idx_type d = 0; d < dim; d++)
210 {
211 v(i,d) = *(voronoi_vertex++);
212 }
213 neighbor->seen = true;
214 neighbor->visitid = i;
215 }
216 facet_list(m++) = neighbor->visitid + 1;
217 }
218 }
219 F(r++) = facet_list;
220 j++;
221 }
222
223 Cell C(r, 1);
224 for (i = 0; i < r; i++)
225 C.elem (i) = F(i);
226
227 retval(0) = v;
228 retval(1) = C;
229 AtInf.resize (r, 1);
230 retval(2) = AtInf;
231
232 // free long memory
233 qh_freeqhull (! qh_ALL);
234
235 // free short memory and memory allocator
236 int curlong, totlong;
237 qh_memfreeshort (&curlong, &totlong);
238
239 if (curlong || totlong)
240 warning ("__voronoi__: did not free %d bytes of long memory (%d pieces)", totlong, curlong);
241 }
242 else
243 error ("__voronoi__: qhull failed");
244
245#else
246 error ("__voronoi__: not available in this version of Octave");
247#endif
248
249 return retval;
250}