/tags/R2002-05-09/octave-forge/main/geometry/convhulln.cc

# · C++ · 106 lines · 56 code · 16 blank · 34 comment · 5 complexity · f5f2e014bee214fbe9bf58d46e3b5c35 MD5 · raw file

  1. /* Copyright (C) 2000 Kai Habel
  2. **
  3. ** This program is free software; you can redistribute it and/or modify
  4. ** it under the terms of the GNU General Public License as published by
  5. ** the Free Software Foundation; either version 2 of the License, or
  6. ** (at your option) any later version.
  7. **
  8. ** This program is distributed in the hope that it will be useful,
  9. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ** GNU General Public License for more details.
  12. **
  13. ** You should have received a copy of the GNU General Public License
  14. ** along with this program; if not, write to the Free Software
  15. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  16. */
  17. /*
  18. 29. July 2000 - Kai Habel: first release
  19. 2002-04-22 Paul Kienzle
  20. * Use warning(...) function rather than writing to cerr
  21. */
  22. extern "C" {
  23. #include "qhull/qhull_a.h"
  24. }
  25. #include <iostream>
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. #include <octave/oct.h>
  30. char qh_version[] = "convhulln.oct 08. August 2000";
  31. char flags[250];
  32. DEFUN_DLD (convhulln, args, ,
  33. "-*- texinfo -*-\n\
  34. @deftypefn {Loadable Function} {@var{H} =} convhulln (@var{p})\n\
  35. Returns an index vector to the points of the enclosing convex hull.\n\
  36. The input matrix of size [dim, n] contains n points of dimension dim.\n\
  37. @seealso{convhull, delaunayn}\n\
  38. @end deftypefn")
  39. {
  40. octave_value_list retval;
  41. retval(0) = 0.0;
  42. int nargin = args.length();
  43. if (nargin != 1) {
  44. print_usage ("convhulln(p)");
  45. return retval;
  46. }
  47. Matrix p(args(0).matrix_value());
  48. const int dim = p.columns();
  49. const int n = p.rows();
  50. p = p.transpose();
  51. double *pt_array = p.fortran_vec();
  52. /*double pt_array[dim * n];
  53. for (int i = 0; i < n; i++) {
  54. for (int j = 0; j < dim; j++) {
  55. pt_array[j+i*dim] = p(i,j);
  56. }
  57. }*/
  58. boolT ismalloc = False;
  59. // hmm lot's of options for qhull here
  60. sprintf(flags,"qhull s Tcv");
  61. if (!qh_new_qhull (dim,n,pt_array,ismalloc,flags,NULL,stderr)) {
  62. // If you want some debugging information replace the NULL
  63. // pointer with stdout
  64. vertexT *vertex,**vertexp;
  65. facetT *facet;
  66. unsigned int i=0,j=0,n = qh num_facets;
  67. Matrix idx(n,dim);
  68. qh_vertexneighbors();
  69. setT *curr_vtc;
  70. FORALLfacets {
  71. //qh_printfacet(stdout,facet);
  72. curr_vtc = facet->vertices;
  73. FOREACHvertex_ (curr_vtc) {
  74. //qh_printvertex(stdout,vertex);
  75. idx(i,j++)= 1 + qh_pointid(vertex->point);
  76. }
  77. i++;j=0;
  78. }
  79. retval(0)=idx;
  80. }
  81. qh_freeqhull(!qh_ALL);
  82. //free long memory
  83. int curlong, totlong;
  84. qh_memfreeshort (&curlong, &totlong);
  85. //free short memory and memory allocator
  86. if (curlong || totlong) {
  87. warning("convhulln: did not free %d bytes of long memory (%d pieces)", totlong, curlong);
  88. }
  89. return retval;
  90. }