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; |
6959
|
69 |
6823
|
70 #ifdef HAVE_QHULL |
|
71 std::string options; |
|
72 |
6959
|
73 int nargin = args.length (); |
6823
|
74 if (nargin < 1 || nargin > 2) |
|
75 { |
|
76 print_usage (); |
|
77 return retval; |
|
78 } |
|
79 |
|
80 if (nargin == 2) |
|
81 { |
6959
|
82 if (args (1).is_string ()) |
|
83 options = args(1).string_value (); |
|
84 else if (args(1).is_cell ()) |
6823
|
85 { |
6959
|
86 Cell c = args(1).cell_value (); |
6823
|
87 options = ""; |
6959
|
88 for (octave_idx_type i = 0; i < c.numel (); i++) |
6823
|
89 { |
6959
|
90 if (! c.elem(i).is_string ()) |
6823
|
91 { |
|
92 error ("convhulln: second argument must be a string or cell array of strings"); |
|
93 return retval; |
6959
|
94 } |
6823
|
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 |
6959
|
109 Matrix p (args(0).matrix_value ()); |
6823
|
110 |
6959
|
111 const octave_idx_type dim = p.columns (); |
|
112 const octave_idx_type n = p.rows (); |
|
113 p = p.transpose (); |
6823
|
114 |
6959
|
115 double *pt_array = p.fortran_vec (); |
6823
|
116 |
|
117 boolT ismalloc = False; |
|
118 |
6959
|
119 OCTAVE_LOCAL_BUFFER (char, flags, 250); |
6880
|
120 |
6823
|
121 // hmm, lots of options for qhull here |
|
122 // QJ guarantees that the output will be triangles |
6959
|
123 snprintf (flags, 250, "qhull QJ %s", options.c_str ()); |
6823
|
124 |
6959
|
125 if (! qh_new_qhull (dim, n, pt_array, ismalloc, flags, NULL, stderr)) |
6823
|
126 { |
|
127 // If you want some debugging information replace the NULL |
|
128 // pointer with stdout |
|
129 |
6959
|
130 vertexT *vertex, **vertexp; |
6823
|
131 facetT *facet; |
|
132 setT *vertices; |
|
133 unsigned int nf = qh num_facets; |
|
134 |
6959
|
135 Matrix idx (nf, dim); |
6823
|
136 |
|
137 octave_idx_type j, i = 0; |
|
138 FORALLfacets |
|
139 { |
|
140 j = 0; |
6959
|
141 if (! facet->simplicial) |
6823
|
142 // should never happen with QJ |
6959
|
143 error ("convhulln: non-simplicial facet"); |
6823
|
144 |
|
145 if (dim == 3) |
|
146 { |
|
147 vertices = qh_facet3vertex (facet); |
6959
|
148 FOREACHvertex_ (vertices) |
6823
|
149 idx(i, j++) = 1 + qh_pointid(vertex->point); |
6959
|
150 qh_settempfree (&vertices); |
6823
|
151 } |
|
152 else |
|
153 { |
|
154 if (facet->toporient ^ qh_ORIENTclock) |
|
155 { |
6959
|
156 FOREACHvertex_ (facet->vertices) |
6823
|
157 idx(i, j++) = 1 + qh_pointid(vertex->point); |
|
158 } |
|
159 else |
|
160 { |
6959
|
161 FOREACHvertexreverse12_ (facet->vertices) |
6823
|
162 idx(i, j++) = 1 + qh_pointid(vertex->point); |
|
163 } |
|
164 } |
|
165 if (j < dim) |
|
166 // likewise but less fatal |
6959
|
167 warning ("facet %d only has %d vertices", i, j); |
6823
|
168 i++; |
|
169 } |
6959
|
170 retval(0) = octave_value (idx); |
6823
|
171 } |
6959
|
172 |
|
173 // free long memory |
|
174 qh_freeqhull (! qh_ALL); |
|
175 |
|
176 // free short memory and memory allocator |
6823
|
177 int curlong, totlong; |
|
178 qh_memfreeshort (&curlong, &totlong); |
|
179 |
|
180 if (curlong || totlong) |
6959
|
181 warning ("convhulln: did not free %d bytes of long memory (%d pieces)", |
|
182 totlong, curlong); |
6823
|
183 #else |
|
184 error ("convhulln: not available in this version of Octave"); |
|
185 #endif |
6959
|
186 |
6823
|
187 return retval; |
|
188 } |