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 29. July 2000 - Kai Habel: first release |
|
26 2002-04-22 Paul Kienzle |
|
27 * Use warning(...) function rather than writing to cerr |
|
28 2006-05-01 Tom Holroyd |
|
29 * add support for consistent winding in all dimensions; output is |
|
30 * guaranteed to be simplicial. |
|
31 */ |
|
32 |
|
33 #include <sstream> |
|
34 |
|
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 #if defined(HAVE__SNPRINTF) && !defined(HAVE_SNPRINTF) |
|
43 #define snprintf _snprintf |
|
44 #endif |
|
45 |
|
46 extern "C" { |
|
47 #include <qhull/qhull_a.h> |
|
48 } |
|
49 |
|
50 #ifdef NEED_QHULL_VERSION |
|
51 char qh_version[] = "convhulln.oct 2007-07-24"; |
|
52 #endif |
|
53 #endif |
|
54 |
|
55 DEFUN_DLD (convhulln, args, , |
|
56 "-*- texinfo -*-\n\ |
|
57 @deftypefn {Loadable Function} {@var{H} =} convhulln (@var{p})\n\ |
|
58 @deftypefnx {Loadable Function} {@var{H} =} convhulln (@var{p}, @var{opt})\n\ |
|
59 Returns an index vector to the points of the enclosing convex hull.\n\ |
|
60 The input matrix of size [n, dim] contains n points of dimension dim.\n\n\ |
|
61 If a second optional argument is given, it must be a string or cell array\n\ |
|
62 of strings containing options for the underlying qhull command. (See\n\ |
|
63 the Qhull documentation for the available options.) The default options\n\ |
|
64 are \"s Qci Tcv\".\n\n\ |
|
65 @seealso{convhull, delaunayn}\n\ |
|
66 @end deftypefn") |
|
67 { |
|
68 octave_value_list retval; |
|
69 #ifdef HAVE_QHULL |
|
70 std::string options; |
|
71 |
|
72 int nargin = args.length(); |
|
73 if (nargin < 1 || nargin > 2) |
|
74 { |
|
75 print_usage (); |
|
76 return retval; |
|
77 } |
|
78 |
|
79 if (nargin == 2) |
|
80 { |
|
81 if (args (1).is_string () ) |
|
82 options = args (1).string_value(); |
|
83 else if (args (1).is_cell ()) |
|
84 { |
|
85 Cell c = args (1). cell_value (); |
|
86 options = ""; |
|
87 for (octave_idx_type i = 0; i < c.numel(); i++) |
|
88 { |
|
89 if (!c.elem(i).is_string()) |
|
90 { |
|
91 error ("convhulln: second argument must be a string or cell array of strings"); |
|
92 return retval; |
|
93 } |
|
94 |
|
95 options = options + c.elem(i).string_value() + " "; |
|
96 } |
|
97 } |
|
98 else |
|
99 { |
|
100 error ("convhulln: second argument must be a string or cell array of strings"); |
|
101 return retval; |
|
102 } |
|
103 } |
|
104 else |
|
105 // turn on some consistency checks |
|
106 options = "s Qci Tcv"; |
|
107 |
|
108 Matrix p(args(0).matrix_value()); |
|
109 |
|
110 const octave_idx_type dim = p.columns(); |
|
111 const octave_idx_type n = p.rows(); |
|
112 p = p.transpose(); |
|
113 |
|
114 double *pt_array = p.fortran_vec(); |
|
115 |
|
116 boolT ismalloc = False; |
|
117 |
6882
|
118 OCTAVE_LOCAL_BUFFER(char, flags, 250); |
6880
|
119 |
6823
|
120 // hmm, lots of options for qhull here |
|
121 // QJ guarantees that the output will be triangles |
6882
|
122 snprintf(flags, 250, "qhull QJ %s", options.c_str()); |
6823
|
123 |
|
124 if (!qh_new_qhull (dim, n, pt_array, ismalloc, flags, NULL, stderr)) |
|
125 { |
|
126 // If you want some debugging information replace the NULL |
|
127 // pointer with stdout |
|
128 |
|
129 vertexT *vertex,**vertexp; |
|
130 facetT *facet; |
|
131 setT *vertices; |
|
132 unsigned int nf = qh num_facets; |
|
133 |
|
134 Matrix idx(nf, dim); |
|
135 |
|
136 octave_idx_type j, i = 0; |
|
137 FORALLfacets |
|
138 { |
|
139 j = 0; |
|
140 if (!facet->simplicial) |
|
141 // should never happen with QJ |
|
142 error("convhulln: non-simplicial facet"); |
|
143 |
|
144 if (dim == 3) |
|
145 { |
|
146 vertices = qh_facet3vertex (facet); |
|
147 FOREACHvertex_(vertices) |
|
148 idx(i, j++) = 1 + qh_pointid(vertex->point); |
|
149 qh_settempfree(&vertices); |
|
150 } |
|
151 else |
|
152 { |
|
153 if (facet->toporient ^ qh_ORIENTclock) |
|
154 { |
|
155 FOREACHvertex_(facet->vertices) |
|
156 idx(i, j++) = 1 + qh_pointid(vertex->point); |
|
157 } |
|
158 else |
|
159 { |
|
160 FOREACHvertexreverse12_(facet->vertices) |
|
161 idx(i, j++) = 1 + qh_pointid(vertex->point); |
|
162 } |
|
163 } |
|
164 if (j < dim) |
|
165 // likewise but less fatal |
|
166 warning("facet %d only has %d vertices",i,j); |
|
167 i++; |
|
168 } |
|
169 retval(0) = octave_value(idx); |
|
170 } |
|
171 qh_freeqhull (!qh_ALL); |
|
172 //free long memory |
|
173 int curlong, totlong; |
|
174 qh_memfreeshort (&curlong, &totlong); |
|
175 //free short memory and memory allocator |
|
176 |
|
177 if (curlong || totlong) |
|
178 { |
|
179 warning("convhulln: did not free %d bytes of long memory (%d pieces)", |
|
180 totlong, curlong); |
|
181 } |
|
182 #else |
|
183 error ("convhulln: not available in this version of Octave"); |
|
184 #endif |
|
185 return retval; |
|
186 } |