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