Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10211


Ignore:
Timestamp:
12/09/13 22:42:34 (11 years ago)
Author:
ascheibe
Message:

#1886 added a wrapper for calculating the convex hull using qhull

Location:
branches/HeuristicLab.Analysis.AlgorithmBehavior
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers-3.3.csproj

    r10208 r10211  
    2222    <ErrorReport>prompt</ErrorReport>
    2323    <WarningLevel>4</WarningLevel>
     24    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    2425  </PropertyGroup>
    2526  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/QhullWrapper.cs

    r10208 r10211  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    4243    }
    4344
     45    public unsafe static List<int> CalculateConvexHull(List<double[]> points) {
     46      int dimension = points.First().Length;
     47      int numPoints = points.Count();
     48      List<int> convexHullIndices = new List<int>();
     49
     50      double[] data = new double[dimension * numPoints];
     51      for (int i = 0; i < numPoints; i++) {
     52        for (int j = 0; j < dimension; j++) {
     53          data[i * dimension + j] = points[i][j];
     54        }
     55      }
     56
     57      IntPtr result = IntPtr.Zero;
     58      Int32 nrOfFacets = -1;
     59      Int32 retCode = 0;
     60
     61      result = qhull_convex_hull(dimension, numPoints, data, &retCode, &nrOfFacets);
     62      if (result != IntPtr.Zero && retCode == 0) {
     63        try {
     64          var faces = new int[nrOfFacets * dimension];
     65          Marshal.Copy(result, faces, 0, nrOfFacets * dimension);
     66          convexHullIndices.AddRange(faces.Distinct());
     67        }
     68        finally {
     69          qhull_free(result);
     70        }
     71      }
     72      return convexHullIndices;
     73    }
     74
    4475    [System.Runtime.InteropServices.DllImportAttribute("HeuristicLab.qhull.dll", EntryPoint = "qhull_volume", CallingConvention = CallingConvention.Cdecl)]
    4576    public static extern double qhull_volume(int dim, int numpoints, [In]double[] data);
     77
     78    [System.Runtime.InteropServices.DllImportAttribute("HeuristicLab.qhull.dll", EntryPoint = "qhull_convex_hull", CallingConvention = CallingConvention.Cdecl)]
     79    public unsafe static extern IntPtr qhull_convex_hull(int dim, int numpoints, [In]double[] data, [Out] Int32* retCode, [Out] Int32* nrOfFacets);
     80
     81    [System.Runtime.InteropServices.DllImportAttribute("HeuristicLab.qhull.dll", EntryPoint = "qhull_free", CallingConvention = CallingConvention.Cdecl)]
     82    public static extern void qhull_free(IntPtr data);
    4683  }
    4784}
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/PerformanceTests/Program.cs

    r10208 r10211  
    4949      volume = Math.Round(volume, 6);
    5050      Console.WriteLine("Volume is: " + volume);
     51
     52      QhullWrapper.CalculateConvexHull(points);
    5153    }
    5254
  • branches/HeuristicLab.Analysis.AlgorithmBehavior/qhull-2012.1/src/HeuristicLab.qhull/HeuristicLab_qhull.c

    r10208 r10211  
    3131#endif
    3232  __declspec(dllexport) double qhull_volume(int dim, int numpoints, double *data);
    33   void print_summary (void);
     33  __declspec(dllexport) int* qhull_convex_hull(int dim, int numpoints, double *data, int *retCode, int* nrOfFacets);
     34  __declspec(dllexport) void qhull_free(int* data);
    3435#ifdef __cplusplus
    3536}
     
    7374  if (!exitcode) {                     
    7475    print_summary(); 
     76    qh_vertexneighbors();
    7577    volume = qh totvol;
    7678  } else {
    77    return -1.0;
     79    return -1.0;
    7880  }
    7981  qh_freeqhull(!qh_ALL);           
     
    8587}
    8688
     89
     90int* qhull_convex_hull(int dim, int numpoints, double *data, int *retCode, int* nrOfFacets) {
     91  boolT ismalloc= False;   
     92  char flags[250];       
     93  FILE *outfile= stdout;   
     94  FILE *errfile= stdout;   
     95  facetT *facet;           
     96  int curlong, totlong;   
     97  int i,j, exitcode;
     98  int* result = NULL;
     99
     100#if qh_QHpointer 
     101  if (qh_qh){
     102    printf ("QH6233: Qhull link error.  The global variable qh_qh was not initialized\n\
     103            to NULL by global.c.  Please compile user_eg.c with -Dqh_QHpointer_dllimport\n\
     104            as well as -Dqh_QHpointer, or use libqhullstatic, or use a different tool chain.\n\n");
     105    *retCode = -1;
     106    return NULL;
     107  }
     108#endif
     109
     110  sprintf (flags, "qhull s Tv Qt FA");
     111
     112  exitcode= qh_new_qhull (dim, numpoints, data, ismalloc, flags, outfile, errfile);
     113  if (!exitcode) {     
     114    facetT *facet;
     115    vertexT *vertex, **vertexp;
     116    unsigned int n = qh num_facets;
     117    *nrOfFacets = n;
     118
     119    print_summary();
     120    qh_vertexneighbors();
     121
     122    result = (int *) malloc(n*dim*sizeof(int));     
     123
     124    i=0;
     125    FORALLfacets {
     126      j=0;     
     127      FOREACHvertex_ (facet->vertices) {       
     128        if (j >= dim)
     129          fprintf (errfile,"extra vertex %d of facet %d = %d",j++,i,/*1+*/qh_pointid(vertex->point));    //TODO: handle in return value
     130        else
     131          result[i+n*j++] = qh_pointid(vertex->point);
     132      }
     133      if (j < dim) fprintf (errfile,"facet %d only has %d vertices",i,j);
     134      i++;
     135    }
     136  } else {
     137    *retCode = -1;
     138    return NULL;
     139  }
     140  qh_freeqhull(!qh_ALL);           
     141  qh_memfreeshort (&curlong, &totlong);
     142  if (curlong || totlong)
     143    fprintf (errfile, "qhull internal warning (HeuristicLab_qhull, #1): did not free %d bytes of long memory (%d pieces)\n", totlong, curlong);
     144
     145  return result;
     146}
     147
     148
     149void qhull_free(int* data) {
     150  free(data);
     151}
     152
     153
Note: See TracChangeset for help on using the changeset viewer.