Mercurial > octave-antonio
annotate src/DLD-FUNCTIONS/__delaunayn__.cc @ 10154:40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 20 Jan 2010 17:33:41 -0500 |
parents | 2c279308f6ab |
children | fd0a3ac60b0e |
rev | line source |
---|---|
6823 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2000, 2007 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 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 | |
9786
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
41 #ifdef HAVE_CONFIG_H |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
42 #include <config.h> |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
43 #endif |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
44 |
6823 | 45 #include <iostream> |
46 #include <string> | |
47 | |
9786
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
48 #include "Cell.h" |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
49 #include "defun-dld.h" |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
50 #include "error.h" |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
51 #include "oct-obj.h" |
6823 | 52 |
53 #ifdef HAVE_QHULL | |
54 extern "C" { | |
55 #include <qhull/qhull_a.h> | |
56 } | |
57 | |
58 #ifdef NEED_QHULL_VERSION | |
59 char qh_version[] = "__delaunayn__.oct 2007-08-21"; | |
60 #endif | |
61 #endif | |
62 | |
63 DEFUN_DLD (__delaunayn__, args, , | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
64 "-*- texinfo -*-\n\ |
6823 | 65 @deftypefn {Loadable Function} {@var{T} =} __delaunayn__ (@var{P}[, @var{opt}])\n\ |
6945 | 66 Undocumented internal function.\n\ |
6823 | 67 @end deftypefn") |
68 | |
69 { | |
70 octave_value_list retval; | |
6945 | 71 |
6823 | 72 #ifdef HAVE_QHULL |
6945 | 73 |
6823 | 74 retval(0) = 0.0; |
75 std::string options = ""; | |
76 | |
6945 | 77 int nargin = args.length (); |
6823 | 78 if (nargin < 1 || nargin > 2) |
79 { | |
80 print_usage (); | |
81 return retval; | |
82 } | |
83 | |
6945 | 84 Matrix p (args(0).matrix_value ()); |
85 const octave_idx_type dim = p.columns (); | |
86 const octave_idx_type n = p.rows (); | |
6823 | 87 |
88 // default options | |
89 if (dim <= 3) | |
90 options = "Qt Qbb Qc"; | |
91 else | |
92 options = "Qt Qbb Qc Qx"; | |
93 | |
94 | |
95 if (nargin == 2) | |
96 { | |
6945 | 97 if (args(1).is_empty ()) |
6823 | 98 { |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
99 // keep default options |
6823 | 100 } |
6945 | 101 else if (args(1).is_string ()) |
6823 | 102 { |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
103 // option string is directly provided |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
104 options = args(1).string_value (); |
6823 | 105 } |
6945 | 106 else if (args(1).is_cell ()) |
6823 | 107 { |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
108 options = ""; |
6823 | 109 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
110 Cell c = args(1).cell_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
111 for (octave_idx_type i = 0; i < c.numel (); i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
112 { |
6823 | 113 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
114 if (! c.elem(i).is_string ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
115 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
116 error ("__delaunayn__: all options must be strings"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
117 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
118 } |
6823 | 119 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
120 options = options + c.elem(i).string_value () + " "; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
121 } |
6823 | 122 } |
123 else | |
124 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
125 error ("__delaunayn__: second argument must be a string, cell of stringsor empty"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
126 return retval; |
6823 | 127 } |
128 } | |
129 | |
130 //octave_stdout << "options " << options << std::endl; | |
131 | |
6880 | 132 if (n > dim + 1) |
6823 | 133 { |
6945 | 134 p = p.transpose (); |
135 double *pt_array = p.fortran_vec (); | |
136 boolT ismalloc = false; | |
6823 | 137 |
6880 | 138 OCTAVE_LOCAL_BUFFER (char, flags, 250); |
139 | |
6945 | 140 sprintf (flags, "qhull d %s", options.c_str ()); |
6823 | 141 |
6945 | 142 // If you want some debugging information replace the 0 pointer |
143 // with stdout or some other file open for writing. | |
6823 | 144 |
6945 | 145 FILE *outfile = 0; |
6880 | 146 FILE *errfile = stderr; |
147 | |
6945 | 148 if (! qh_new_qhull (dim, n, pt_array, ismalloc, flags, outfile, errfile)) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
149 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
150 // triangulate non-simplicial facets |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
151 qh_triangulate (); |
6823 | 152 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
153 facetT *facet; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
154 vertexT *vertex, **vertexp; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
155 octave_idx_type nf = 0, i = 0; |
6823 | 156 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
157 FORALLfacets |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
158 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
159 if (! facet->upperdelaunay) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
160 nf++; |
6945 | 161 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
162 // Double check |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
163 if (! facet->simplicial) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
164 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
165 error ("__delaunayn__: Qhull returned non-simplicial facets -- try delaunayn with different options"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
166 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
167 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
168 } |
6945 | 169 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
170 Matrix simpl (nf, dim+1); |
6823 | 171 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
172 FORALLfacets |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
173 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
174 if (! facet->upperdelaunay) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
175 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
176 octave_idx_type j = 0; |
6945 | 177 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
178 FOREACHvertex_ (facet->vertices) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
179 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
180 // if delaunayn crashes, enable this check |
6823 | 181 #if 0 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
182 if (j > dim) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
183 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
184 error ("__delaunayn__: internal error. Qhull returned non-simplicial facets"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
185 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
186 } |
6823 | 187 #endif |
188 | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
189 simpl(i, j++) = 1 + qh_pointid(vertex->point); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
190 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
191 i++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
192 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
193 } |
6945 | 194 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
195 retval(0) = simpl; |
6945 | 196 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
197 // free long memory |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
198 qh_freeqhull (! qh_ALL); |
6823 | 199 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
200 // free short memory and memory allocator |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
201 int curlong, totlong; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
202 qh_memfreeshort (&curlong, &totlong); |
6823 | 203 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
204 if (curlong || totlong) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
205 warning ("__delaunay__: did not free %d bytes of long memory (%d pieces)", |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
206 totlong, curlong); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
207 } |
6945 | 208 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
209 error ("__delaunayn__: qhull failed."); |
6945 | 210 } |
6823 | 211 else if (n == dim + 1) |
212 { | |
213 // one should check if nx points span a simplex | |
214 // I will look at this later. | |
6945 | 215 RowVector vec (n); |
6823 | 216 for (octave_idx_type i = 0; i < n; i++) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
217 vec(i) = i + 1.0; |
6945 | 218 |
6823 | 219 retval(0) = vec; |
220 } | |
6945 | 221 |
6823 | 222 #else |
223 error ("__delaunayn__: not available in this version of Octave"); | |
224 #endif | |
6945 | 225 |
6823 | 226 return retval; |
227 } |