/branches/pkienzle/octave-forge/main/geometry/__voronoi__.cc

# · C++ · 157 lines · 108 code · 18 blank · 31 comment · 18 complexity · c74ab33b37e4512a570b24675caf91ee 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. 20. Augiust 2000 - Kai Habel: first release
  19. */
  20. extern "C" {
  21. #include "qhull/qhull_a.h"
  22. }
  23. #include <iostream>
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <octave/oct.h>
  28. char qh_version[] = "__voronoi__.oct 20. August 2000";
  29. FILE *outfile = stdout;
  30. FILE *errfile = stderr;
  31. char flags[250];
  32. DEFUN_DLD (__voronoi__, args, ,
  33. "-*- texinfo -*-\n\
  34. @deftypefn {Loadable Function} {@var{tri}=} __voronoi__ (@var{point})\n\
  35. @end deftypefn")
  36. {
  37. octave_value_list retval;
  38. retval(0) = 0.0;
  39. int nargin = args.length();
  40. if (nargin != 1) {
  41. print_usage ("__voronoi__ (points)");
  42. return retval;
  43. }
  44. Matrix p(args(0).matrix_value());
  45. const int dim = p.columns();
  46. const int np = p.rows();
  47. p = p.transpose();
  48. double *pt_array = p.fortran_vec();
  49. /*double pt_array[dim * np];
  50. for (int i = 0; i < np; i++) {
  51. for (int j = 0; j < dim; j++) {
  52. pt_array[j+i*dim] = p(i,j);
  53. }
  54. }*/
  55. boolT ismalloc = False;
  56. // hmm lot's of options for qhull here
  57. sprintf(flags,"qhull v Fv T0");
  58. if (!qh_new_qhull (dim, np, pt_array, ismalloc, flags, NULL, errfile)) {
  59. /*If you want some debugging information replace the NULL
  60. pointer with outfile.
  61. */
  62. facetT *facet;
  63. vertexT *vertex;
  64. unsigned int i=0,n=0,k=0,ni[np],m=0,fidx=0,j=0,r=0;
  65. for (int i=0;i<np;i++) ni[i]=0;
  66. qh_setvoronoi_all();
  67. bool infinity_seen = false;
  68. facetT *neighbor,**neighborp;
  69. coordT *voronoi_vertex;
  70. FORALLfacets {
  71. facet->seen = False;
  72. }
  73. FORALLvertices {
  74. if (qh hull_dim == 3)
  75. qh_order_vertexneighbors(vertex);
  76. FOREACHneighbor_(vertex) {
  77. if (!neighbor->upperdelaunay) {
  78. if (!neighbor->seen) {
  79. n++;
  80. neighbor->seen=True;
  81. }
  82. ni[k]++;
  83. }
  84. }
  85. k++;
  86. }
  87. Matrix v(n,dim);
  88. ColumnVector AtInf(np);
  89. for (int i=0;i < np;i++) AtInf(i)=0;
  90. octave_value_list F;
  91. k=0;
  92. FORALLfacets {
  93. facet->seen = False;
  94. }
  95. FORALLvertices {
  96. if (qh hull_dim == 3)
  97. qh_order_vertexneighbors(vertex);
  98. infinity_seen = false;
  99. RowVector facet_list(ni[k++]);
  100. m = 0;
  101. FOREACHneighbor_(vertex) {
  102. if (neighbor->upperdelaunay) {
  103. if (!infinity_seen) {
  104. infinity_seen = true;
  105. AtInf(j) = 1;
  106. }
  107. } else {
  108. if (!neighbor->seen) {
  109. voronoi_vertex = neighbor->center;
  110. fidx = neighbor->id;
  111. for (int d=0; d<dim; d++) {
  112. v(i,d) = *(voronoi_vertex++);
  113. }
  114. i++;
  115. neighbor->seen = True;
  116. neighbor->visitid = i;
  117. }
  118. facet_list(m++)=neighbor->visitid;
  119. }
  120. }
  121. F(r++)=facet_list;
  122. j++;
  123. }
  124. retval(0) = v;
  125. retval(1) = F;
  126. retval(2) = AtInf;
  127. qh_freeqhull(!qh_ALL);
  128. //free long memory
  129. int curlong, totlong;
  130. qh_memfreeshort (&curlong, &totlong);
  131. //free short memory and memory allocator
  132. if (curlong || totlong) {
  133. cerr << "qhull internal warning (delaunay): did not free ";
  134. cerr << totlong << " bytes of long memory (";
  135. cerr << curlong << " pieces)" << endl;
  136. }
  137. }
  138. return retval;
  139. }