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