6823
|
1 /* |
|
2 |
|
3 Copyright (C) 2000 Kai Habel |
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 /* |
|
25 20. Augiust 2000 - Kai Habel: first release |
|
26 */ |
|
27 |
|
28 /* |
|
29 2003-12-14 Rafael Laboissiere <rafael@laboissiere.net> |
|
30 Added optional second argument to pass options to the underlying |
|
31 qhull command |
|
32 */ |
|
33 |
|
34 #include <iostream> |
|
35 #ifdef HAVE_CONFIG_H |
|
36 #include <config.h> |
|
37 #endif |
|
38 #include "oct.h" |
|
39 #include "Cell.h" |
|
40 |
|
41 #ifdef HAVE_QHULL |
|
42 extern "C" { |
|
43 #include <qhull/qhull_a.h> |
|
44 } |
|
45 |
|
46 #ifdef NEED_QHULL_VERSION |
|
47 char qh_version[] = "__voronoi__.oct 2007-07-24"; |
|
48 #endif |
|
49 |
|
50 FILE *outfile = stdout; |
|
51 FILE *errfile = stderr; |
|
52 char flags[250]; |
|
53 const char *options; |
|
54 #endif |
|
55 |
|
56 DEFUN_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\ |
|
60 Internal function for voronoi.\n\ |
|
61 @end deftypefn") |
|
62 { |
|
63 octave_value_list retval; |
|
64 #ifdef HAVE_QHULL |
|
65 retval(0) = 0.0; |
|
66 |
|
67 int nargin = args.length(); |
|
68 if (nargin < 1 || nargin > 2) |
|
69 { |
|
70 print_usage (); |
|
71 return retval; |
|
72 } |
|
73 |
|
74 if (nargin == 2) |
|
75 { |
|
76 if (!args (1).is_string ()) |
|
77 { |
|
78 error ("__voronoi__: second argument must be a string"); |
|
79 return retval; |
|
80 } |
|
81 options = args (1).string_value().c_str(); |
|
82 } |
|
83 else |
|
84 options = ""; |
|
85 |
|
86 Matrix p(args(0).matrix_value()); |
|
87 |
|
88 const octave_idx_type dim = p.columns(); |
|
89 const octave_idx_type np = p.rows(); |
|
90 p = p.transpose(); |
|
91 |
|
92 double *pt_array = p.fortran_vec(); |
|
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 |
|
104 // hmm lot's of options for qhull here |
|
105 sprintf(flags,"qhull v Fv T0 %s",options); |
|
106 if (!qh_new_qhull (dim, np, pt_array, ismalloc, flags, NULL, errfile)) |
|
107 { |
|
108 |
|
109 // If you want some debugging information replace the NULL |
|
110 // pointer with outfile. |
|
111 facetT *facet; |
|
112 vertexT *vertex; |
|
113 |
|
114 octave_idx_type i = 0, n = 1, k = 0, m = 0, fidx = 0, j = 0, r = 0; |
|
115 OCTAVE_LOCAL_BUFFER(octave_idx_type, ni, np); |
|
116 |
|
117 for (i = 0; i < np; i++) |
|
118 ni[i] = 0; |
|
119 qh_setvoronoi_all(); |
|
120 bool infinity_seen = false; |
|
121 facetT *neighbor,**neighborp; |
|
122 coordT *voronoi_vertex; |
|
123 FORALLfacets |
|
124 { |
|
125 facet->seen = false; |
|
126 } |
|
127 FORALLvertices |
|
128 { |
|
129 if (qh hull_dim == 3) |
|
130 qh_order_vertexneighbors(vertex); |
|
131 infinity_seen = false; |
|
132 FOREACHneighbor_(vertex) |
|
133 { |
|
134 if (!neighbor->upperdelaunay) |
|
135 { |
|
136 if (!neighbor->seen) |
|
137 { |
|
138 n++; |
|
139 neighbor->seen=True; |
|
140 } |
|
141 ni[k]++; |
|
142 } |
|
143 else if (!infinity_seen) |
|
144 { |
|
145 infinity_seen = true; |
|
146 ni[k]++; |
|
147 } |
|
148 } |
|
149 k++; |
|
150 } |
|
151 |
|
152 Matrix v(n, dim); |
|
153 for (octave_idx_type d = 0; d < dim; d++) |
|
154 v(0,d) = octave_Inf; |
|
155 |
|
156 boolMatrix AtInf(np, 1); |
|
157 for (i = 0; i < np; i++) |
|
158 AtInf(i) = false; |
|
159 octave_value_list F; |
|
160 k = 0; |
|
161 i = 0; |
|
162 FORALLfacets |
|
163 { |
|
164 facet->seen = false; |
|
165 } |
|
166 FORALLvertices |
|
167 { |
|
168 if (qh hull_dim == 3) |
|
169 qh_order_vertexneighbors(vertex); |
|
170 infinity_seen = false; |
|
171 RowVector facet_list(ni[k++]); |
|
172 m = 0; |
|
173 FOREACHneighbor_(vertex) |
|
174 { |
|
175 if (neighbor->upperdelaunay) |
|
176 { |
|
177 if (!infinity_seen) |
|
178 { |
|
179 infinity_seen = true; |
|
180 facet_list(m++) = 1; |
|
181 AtInf(j) = true; |
|
182 } |
|
183 } |
|
184 else |
|
185 { |
|
186 if (!neighbor->seen) |
|
187 { |
|
188 voronoi_vertex = neighbor->center; |
|
189 fidx = neighbor->id; |
|
190 i++; |
|
191 for (octave_idx_type d = 0; d < dim; d++) |
|
192 { |
|
193 v(i,d) = *(voronoi_vertex++); |
|
194 } |
|
195 neighbor->seen = true; |
|
196 neighbor->visitid = i; |
|
197 } |
|
198 facet_list(m++)=neighbor->visitid + 1; |
|
199 } |
|
200 } |
|
201 F(r++) = facet_list; |
|
202 j++; |
|
203 } |
|
204 |
|
205 Cell C(r, 1); |
|
206 for (i = 0; i < r; i++) |
|
207 C.elem (i) = F(i); |
|
208 |
|
209 retval(0) = v; |
|
210 retval(1) = C; |
|
211 retval(2) = AtInf; |
|
212 |
|
213 //free long memory |
|
214 qh_freeqhull(!qh_ALL); |
|
215 |
|
216 //free short memory and memory allocator |
|
217 int curlong, totlong; |
|
218 qh_memfreeshort (&curlong, &totlong); |
|
219 |
|
220 if (curlong || totlong) { |
|
221 warning("__voronoi__: did not free %d bytes of long memory (%d pieces)", totlong, curlong); |
|
222 } |
|
223 } |
|
224 #else |
|
225 error ("__voronoi__: not available in this version of Octave"); |
|
226 #endif |
|
227 return retval; |
|
228 } |