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 |
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 16. July 2000 - Kai Habel: first release |
|
25 |
|
26 25. September 2002 - Changes by Rafael Laboissiere <rafael@laboissiere.net> |
|
27 |
|
28 * Added Qbb option to normalize the input and avoid crashes in Octave. |
|
29 * delaunayn accepts now a second (optional) argument that must be a string |
|
30 containing extra options to the qhull command. |
|
31 * Fixed doc string. The dimension of the result matrix is [m, dim+1], and |
|
32 not [n, dim-1]. |
|
33 |
|
34 6. June 2006: Changes by Alexander Barth <abarth@marine.usf.edu> |
|
35 |
|
36 * triangulate non-simplicial facets |
|
37 * allow options to be specified as cell array of strings |
|
38 * change the default options (for compatibility with matlab) |
|
39 */ |
|
40 |
|
41 #include <iostream> |
|
42 #include <string> |
|
43 |
|
44 #ifdef HAVE_CONFIG_H |
|
45 #include <config.h> |
|
46 #endif |
|
47 #include "oct.h" |
|
48 #include "ov-cell.h" |
|
49 |
|
50 #ifdef HAVE_QHULL |
|
51 extern "C" { |
|
52 #include <qhull/qhull_a.h> |
|
53 } |
|
54 |
|
55 #ifdef NEED_QHULL_VERSION |
|
56 char qh_version[] = "__delaunayn__.oct 2007-08-21"; |
|
57 #endif |
|
58 #endif |
|
59 |
|
60 DEFUN_DLD (__delaunayn__, args, , |
|
61 "-*- texinfo -*-\n\ |
|
62 @deftypefn {Loadable Function} {@var{T} =} __delaunayn__ (@var{P}[, @var{opt}])\n\ |
6945
|
63 Undocumented internal function.\n\ |
6823
|
64 @end deftypefn") |
|
65 |
|
66 { |
|
67 octave_value_list retval; |
6945
|
68 |
6823
|
69 #ifdef HAVE_QHULL |
6945
|
70 |
6823
|
71 retval(0) = 0.0; |
|
72 std::string options = ""; |
|
73 |
6945
|
74 int nargin = args.length (); |
6823
|
75 if (nargin < 1 || nargin > 2) |
|
76 { |
|
77 print_usage (); |
|
78 return retval; |
|
79 } |
|
80 |
6945
|
81 Matrix p (args(0).matrix_value ()); |
|
82 const octave_idx_type dim = p.columns (); |
|
83 const octave_idx_type n = p.rows (); |
6823
|
84 |
|
85 // default options |
|
86 if (dim <= 3) |
|
87 options = "Qt Qbb Qc"; |
|
88 else |
|
89 options = "Qt Qbb Qc Qx"; |
|
90 |
|
91 |
|
92 if (nargin == 2) |
|
93 { |
6945
|
94 if (args(1).is_empty ()) |
6823
|
95 { |
|
96 // keep default options |
|
97 } |
6945
|
98 else if (args(1).is_string ()) |
6823
|
99 { |
|
100 // option string is directly provided |
6945
|
101 options = args(1).string_value (); |
6823
|
102 } |
6945
|
103 else if (args(1).is_cell ()) |
6823
|
104 { |
|
105 options = ""; |
|
106 |
6945
|
107 Cell c = args(1).cell_value (); |
|
108 for (octave_idx_type i = 0; i < c.numel (); i++) |
6823
|
109 { |
|
110 |
6945
|
111 if (! c.elem(i).is_string ()) |
6823
|
112 { |
|
113 error ("__delaunayn__: all options must be strings"); |
|
114 return retval; |
|
115 } |
|
116 |
6945
|
117 options = options + c.elem(i).string_value () + " "; |
6823
|
118 } |
|
119 } |
|
120 else |
|
121 { |
|
122 error ("__delaunayn__: second argument must be a string, cell of stringsor empty"); |
|
123 return retval; |
|
124 } |
|
125 } |
|
126 |
|
127 //octave_stdout << "options " << options << std::endl; |
|
128 |
6880
|
129 if (n > dim + 1) |
6823
|
130 { |
6945
|
131 p = p.transpose (); |
|
132 double *pt_array = p.fortran_vec (); |
|
133 boolT ismalloc = false; |
6823
|
134 |
6880
|
135 OCTAVE_LOCAL_BUFFER (char, flags, 250); |
|
136 |
6945
|
137 sprintf (flags, "qhull d %s", options.c_str ()); |
6823
|
138 |
6945
|
139 // If you want some debugging information replace the 0 pointer |
|
140 // with stdout or some other file open for writing. |
6823
|
141 |
6945
|
142 FILE *outfile = 0; |
6880
|
143 FILE *errfile = stderr; |
|
144 |
6945
|
145 if (! qh_new_qhull (dim, n, pt_array, ismalloc, flags, outfile, errfile)) |
6823
|
146 { |
6945
|
147 // triangulate non-simplicial facets |
|
148 qh_triangulate (); |
6823
|
149 |
6945
|
150 facetT *facet; |
|
151 vertexT *vertex, **vertexp; |
|
152 octave_idx_type nf = 0, i = 0; |
6823
|
153 |
6945
|
154 FORALLfacets |
6823
|
155 { |
6945
|
156 if (! facet->upperdelaunay) |
|
157 nf++; |
|
158 |
|
159 // Double check |
|
160 if (! facet->simplicial) |
|
161 { |
|
162 error ("__delaunayn__: Qhull returned non-simplicial facets -- try delaunayn with different options"); |
|
163 break; |
|
164 } |
6823
|
165 } |
6945
|
166 |
|
167 Matrix simpl (nf, dim+1); |
6823
|
168 |
6945
|
169 FORALLfacets |
|
170 { |
|
171 if (! facet->upperdelaunay) |
|
172 { |
|
173 octave_idx_type j = 0; |
|
174 |
|
175 FOREACHvertex_ (facet->vertices) |
|
176 { |
|
177 // if delaunayn crashes, enable this check |
6823
|
178 #if 0 |
6945
|
179 if (j > dim) |
|
180 { |
|
181 error ("__delaunayn__: internal error. Qhull returned non-simplicial facets"); |
|
182 return retval; |
|
183 } |
6823
|
184 #endif |
|
185 |
6945
|
186 simpl(i, j++) = 1 + qh_pointid(vertex->point); |
|
187 } |
|
188 i++; |
|
189 } |
|
190 } |
|
191 |
|
192 retval(0) = simpl; |
|
193 |
|
194 // free long memory |
|
195 qh_freeqhull (! qh_ALL); |
6823
|
196 |
6945
|
197 // free short memory and memory allocator |
|
198 int curlong, totlong; |
|
199 qh_memfreeshort (&curlong, &totlong); |
6823
|
200 |
6945
|
201 if (curlong || totlong) |
|
202 warning ("__delaunay__: did not free %d bytes of long memory (%d pieces)", |
|
203 totlong, curlong); |
|
204 } |
|
205 else |
|
206 error ("__delaunayn__: qhull failed."); |
|
207 } |
6823
|
208 else if (n == dim + 1) |
|
209 { |
|
210 // one should check if nx points span a simplex |
|
211 // I will look at this later. |
6945
|
212 RowVector vec (n); |
6823
|
213 for (octave_idx_type i = 0; i < n; i++) |
6945
|
214 vec(i) = i + 1.0; |
|
215 |
6823
|
216 retval(0) = vec; |
|
217 } |
6945
|
218 |
6823
|
219 #else |
|
220 error ("__delaunayn__: not available in this version of Octave"); |
|
221 #endif |
6945
|
222 |
6823
|
223 return retval; |
|
224 } |