Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/__voronoi__.cc @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | fa12c67683d3 |
children | 0631d397fbe0 |
rev | line source |
---|---|
6823 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2000, 2007, 2008 Kai Habel |
6823 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
6823 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
6823 | 20 |
21 */ | |
22 | |
23 /* | |
24 20. Augiust 2000 - Kai Habel: first release | |
25 */ | |
26 | |
27 /* | |
28 2003-12-14 Rafael Laboissiere <rafael@laboissiere.net> | |
29 Added optional second argument to pass options to the underlying | |
30 qhull command | |
31 */ | |
32 | |
33 #include <iostream> | |
34 #ifdef HAVE_CONFIG_H | |
35 #include <config.h> | |
36 #endif | |
37 #include "oct.h" | |
38 #include "Cell.h" | |
39 | |
40 #ifdef HAVE_QHULL | |
41 extern "C" { | |
42 #include <qhull/qhull_a.h> | |
43 } | |
44 | |
45 #ifdef NEED_QHULL_VERSION | |
46 char qh_version[] = "__voronoi__.oct 2007-07-24"; | |
47 #endif | |
48 #endif | |
49 | |
50 DEFUN_DLD (__voronoi__, args, , | |
51 "-*- texinfo -*-\n\ | |
52 @deftypefn {Loadable Function} {@var{tri} =} __voronoi__ (@var{point})\n\ | |
53 @deftypefnx {Loadable Function} {@var{tri} =} __voronoi__ (@var{point}, @var{options})\n\ | |
6945 | 54 Undocumented internal function.\n\ |
6823 | 55 @end deftypefn") |
56 { | |
57 octave_value_list retval; | |
6945 | 58 |
6823 | 59 #ifdef HAVE_QHULL |
6945 | 60 |
6823 | 61 retval(0) = 0.0; |
62 | |
6945 | 63 int nargin = args.length (); |
6823 | 64 if (nargin < 1 || nargin > 2) |
65 { | |
66 print_usage (); | |
67 return retval; | |
68 } | |
69 | |
6880 | 70 const char *options; |
71 | |
6823 | 72 if (nargin == 2) |
73 { | |
6945 | 74 if (! args (1).is_string ()) |
6823 | 75 { |
76 error ("__voronoi__: second argument must be a string"); | |
77 return retval; | |
78 } | |
6945 | 79 |
80 options = args (1).string_value().c_str (); | |
6823 | 81 } |
82 else | |
83 options = ""; | |
84 | |
6945 | 85 Matrix p (args(0).matrix_value ()); |
6823 | 86 |
6945 | 87 const octave_idx_type dim = p.columns (); |
88 const octave_idx_type np = p.rows (); | |
89 p = p.transpose (); | |
6823 | 90 |
6945 | 91 double *pt_array = p.fortran_vec (); |
92 | |
6823 | 93 //double pt_array[dim * np]; |
94 //for (int i = 0; i < np; i++) | |
95 // { | |
96 // for (int j = 0; j < dim; j++) | |
97 // { | |
98 // pt_array[j+i*dim] = p(i,j); | |
99 // } | |
100 // } | |
101 | |
102 boolT ismalloc = false; | |
103 | |
6945 | 104 OCTAVE_LOCAL_BUFFER (char, flags, 250); |
6880 | 105 |
6823 | 106 // hmm lot's of options for qhull here |
6945 | 107 sprintf (flags, "qhull v Fv T0 %s", options); |
6880 | 108 |
6945 | 109 // If you want some debugging information replace the 0 pointer |
110 // with stdout or some other file open for writing. | |
111 | |
112 FILE *outfile = 0; | |
6880 | 113 FILE *errfile = stderr; |
114 | |
6945 | 115 if (! qh_new_qhull (dim, np, pt_array, ismalloc, flags, outfile, errfile)) |
6823 | 116 { |
117 facetT *facet; | |
118 vertexT *vertex; | |
119 | |
120 octave_idx_type i = 0, n = 1, k = 0, m = 0, fidx = 0, j = 0, r = 0; | |
6945 | 121 OCTAVE_LOCAL_BUFFER (octave_idx_type, ni, np); |
6823 | 122 |
123 for (i = 0; i < np; i++) | |
124 ni[i] = 0; | |
6945 | 125 qh_setvoronoi_all (); |
6823 | 126 bool infinity_seen = false; |
6945 | 127 facetT *neighbor, **neighborp; |
6823 | 128 coordT *voronoi_vertex; |
6945 | 129 |
6823 | 130 FORALLfacets |
131 { | |
132 facet->seen = false; | |
133 } | |
6945 | 134 |
6823 | 135 FORALLvertices |
136 { | |
137 if (qh hull_dim == 3) | |
6945 | 138 qh_order_vertexneighbors (vertex); |
6823 | 139 infinity_seen = false; |
6945 | 140 |
141 FOREACHneighbor_ (vertex) | |
6823 | 142 { |
6945 | 143 if (! neighbor->upperdelaunay) |
6823 | 144 { |
6945 | 145 if (! neighbor->seen) |
6823 | 146 { |
147 n++; | |
6945 | 148 neighbor->seen = true; |
6823 | 149 } |
150 ni[k]++; | |
151 } | |
6945 | 152 else if (! infinity_seen) |
6823 | 153 { |
154 infinity_seen = true; | |
155 ni[k]++; | |
156 } | |
157 } | |
158 k++; | |
159 } | |
160 | |
6945 | 161 Matrix v (n, dim); |
6823 | 162 for (octave_idx_type d = 0; d < dim; d++) |
163 v(0,d) = octave_Inf; | |
164 | |
6945 | 165 boolMatrix AtInf (np, 1); |
6823 | 166 for (i = 0; i < np; i++) |
167 AtInf(i) = false; | |
8415
fa12c67683d3
Reszie third arg of __voronoi__ before retuning it
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
168 octave_value_list F (np, octave_value ()); |
6823 | 169 k = 0; |
170 i = 0; | |
6945 | 171 |
6823 | 172 FORALLfacets |
173 { | |
174 facet->seen = false; | |
175 } | |
6945 | 176 |
6823 | 177 FORALLvertices |
178 { | |
179 if (qh hull_dim == 3) | |
180 qh_order_vertexneighbors(vertex); | |
181 infinity_seen = false; | |
6945 | 182 RowVector facet_list (ni[k++]); |
6823 | 183 m = 0; |
6945 | 184 |
6823 | 185 FOREACHneighbor_(vertex) |
186 { | |
187 if (neighbor->upperdelaunay) | |
188 { | |
6945 | 189 if (! infinity_seen) |
6823 | 190 { |
191 infinity_seen = true; | |
192 facet_list(m++) = 1; | |
193 AtInf(j) = true; | |
194 } | |
195 } | |
196 else | |
197 { | |
6945 | 198 if (! neighbor->seen) |
6823 | 199 { |
200 voronoi_vertex = neighbor->center; | |
201 fidx = neighbor->id; | |
202 i++; | |
203 for (octave_idx_type d = 0; d < dim; d++) | |
204 { | |
205 v(i,d) = *(voronoi_vertex++); | |
206 } | |
207 neighbor->seen = true; | |
208 neighbor->visitid = i; | |
209 } | |
6945 | 210 facet_list(m++) = neighbor->visitid + 1; |
6823 | 211 } |
212 } | |
213 F(r++) = facet_list; | |
214 j++; | |
215 } | |
216 | |
217 Cell C(r, 1); | |
218 for (i = 0; i < r; i++) | |
219 C.elem (i) = F(i); | |
220 | |
221 retval(0) = v; | |
222 retval(1) = C; | |
8415
fa12c67683d3
Reszie third arg of __voronoi__ before retuning it
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
223 AtInf.resize (r, 1); |
6823 | 224 retval(2) = AtInf; |
225 | |
6945 | 226 // free long memory |
227 qh_freeqhull (! qh_ALL); | |
6823 | 228 |
6945 | 229 // free short memory and memory allocator |
6823 | 230 int curlong, totlong; |
231 qh_memfreeshort (&curlong, &totlong); | |
232 | |
6945 | 233 if (curlong || totlong) |
234 warning ("__voronoi__: did not free %d bytes of long memory (%d pieces)", totlong, curlong); | |
6823 | 235 } |
6945 | 236 else |
237 error ("__voronoi__: qhull failed"); | |
238 | |
6823 | 239 #else |
240 error ("__voronoi__: not available in this version of Octave"); | |
241 #endif | |
6945 | 242 |
6823 | 243 return retval; |
244 } |