Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/QhullWrapper.cs @ 10222

Last change on this file since 10222 was 10222, checked in by ascheibe, 10 years ago

#1886

  • improved qhull wrapper
  • cleaned up unit tests
File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Runtime.InteropServices;
26
27namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
28  public static class QhullWrapper {
29    [System.Runtime.InteropServices.DllImportAttribute("HeuristicLab.qhull.dll", EntryPoint = "qhull_volume", CallingConvention = CallingConvention.Cdecl)]
30    public static extern double qhull_volume(int dim, int numpoints, [In]double[] data);
31
32    [System.Runtime.InteropServices.DllImportAttribute("HeuristicLab.qhull.dll", EntryPoint = "qhull_convex_hull", CallingConvention = CallingConvention.Cdecl)]
33    public unsafe static extern IntPtr qhull_convex_hull(int dim, int numpoints, [In]double[] data, [Out] Int32* retCode, [Out] Int32* nrOfFacets);
34
35    [System.Runtime.InteropServices.DllImportAttribute("HeuristicLab.qhull.dll", EntryPoint = "qhull_free", CallingConvention = CallingConvention.Cdecl)]
36    public static extern void qhull_free(IntPtr data);
37
38    public static double CalculateVolume(List<double[]> points) {
39      double result = 0.0;
40      int dimension = points.First().Length;
41      int numPoints = points.Count();
42
43      double[] data = new double[dimension * numPoints];
44
45      for (int i = 0; i < numPoints; i++) {
46        for (int j = 0; j < dimension; j++) {
47          data[i * dimension + j] = points[i][j];
48        }
49      }
50      result = qhull_volume(dimension, numPoints, data);
51      return result;
52    }
53
54    public unsafe static List<int> CalculateConvexHullIndices(List<double[]> points) {
55      int dimension = points.First().Length;
56      int numPoints = points.Count();
57      List<int> convexHullIndices = new List<int>();
58
59      double[] data = new double[dimension * numPoints];
60      for (int i = 0; i < numPoints; i++) {
61        for (int j = 0; j < dimension; j++) {
62          data[i * dimension + j] = points[i][j];
63        }
64      }
65
66      IntPtr result = IntPtr.Zero;
67      Int32 nrOfFacets = -1;
68      Int32 retCode = 0;
69
70      result = qhull_convex_hull(dimension, numPoints, data, &retCode, &nrOfFacets);
71      if (result != IntPtr.Zero && retCode == 0) {
72        try {
73          var faces = new int[nrOfFacets * dimension];
74          Marshal.Copy(result, faces, 0, nrOfFacets * dimension);
75          convexHullIndices.AddRange(faces.Distinct());
76        }
77        finally {
78          qhull_free(result);
79        }
80      }
81      return convexHullIndices;
82    }
83
84    public static List<double[]> Calculate(List<double[]> points) {
85      var ret = new List<double[]>();
86      List<int> indices = CalculateConvexHullIndices(points);
87      foreach (var d in indices) {
88        ret.Add(points[d]);
89      }
90      return ret;
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.