1 | /* HeuristicLab
|
---|
2 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
3 | *
|
---|
4 | * This file is part of HeuristicLab.
|
---|
5 | *
|
---|
6 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation, either version 3 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | *
|
---|
19 | *
|
---|
20 | * Export qhull functionality for HeuristicLab.
|
---|
21 | * Code based on qhull's user_eg.c as well as
|
---|
22 | * R's geometry package (Rconvhulln.c, Copyright Kai Habel, Raoul Grasman, David Sterratt; licensed under the GNU GPL).
|
---|
23 | *
|
---|
24 | */
|
---|
25 |
|
---|
26 | #define qh_QHimport
|
---|
27 | #include "qhull_a.h"
|
---|
28 |
|
---|
29 | #ifdef __cplusplus
|
---|
30 | extern "C" {
|
---|
31 | #endif
|
---|
32 | __declspec(dllexport) double qhull_volume(int dim, int numpoints, double *data);
|
---|
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);
|
---|
35 | #ifdef __cplusplus
|
---|
36 | }
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | void print_summary (void) {
|
---|
40 | facetT *facet;
|
---|
41 | int k;
|
---|
42 |
|
---|
43 | printf ("\n%d vertices and %d facets with normals:\n",
|
---|
44 | qh num_vertices, qh num_facets);
|
---|
45 | FORALLfacets {
|
---|
46 | for (k=0; k < qh hull_dim; k++)
|
---|
47 | printf ("%6.2g ", facet->normal[k]);
|
---|
48 | printf ("\n");
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | double qhull_volume(int dim, int numpoints, double *data) {
|
---|
53 | boolT ismalloc= False;
|
---|
54 | char flags[250];
|
---|
55 | FILE *outfile= NULL;
|
---|
56 | FILE *errfile= stderr;
|
---|
57 | facetT *facet;
|
---|
58 | int curlong, totlong;
|
---|
59 | int i,j, exitcode;
|
---|
60 | double volume = -1.0;
|
---|
61 |
|
---|
62 | #if qh_QHpointer
|
---|
63 | if (qh_qh){
|
---|
64 | printf ("QH6233: Qhull link error. The global variable qh_qh was not initialized\n\
|
---|
65 | to NULL by global.c. Please compile user_eg.c with -Dqh_QHpointer_dllimport\n\
|
---|
66 | as well as -Dqh_QHpointer, or use libqhullstatic, or use a different tool chain.\n\n");
|
---|
67 | return -1;
|
---|
68 | }
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | sprintf (flags, "qhull s Tv Qt FA");
|
---|
72 |
|
---|
73 | exitcode = qh_new_qhull (dim, numpoints, data, ismalloc, flags, outfile, errfile);
|
---|
74 | if (!exitcode) {
|
---|
75 | // print_summary();
|
---|
76 | qh_vertexneighbors();
|
---|
77 | volume = qh totvol;
|
---|
78 | } else {
|
---|
79 | return -1.0;
|
---|
80 | }
|
---|
81 | qh_freeqhull(!qh_ALL);
|
---|
82 | qh_memfreeshort (&curlong, &totlong);
|
---|
83 | if (curlong || totlong)
|
---|
84 | fprintf (errfile, "qhull internal warning (HeuristicLab_qhull, #1): did not free %d bytes of long memory (%d pieces)\n", totlong, curlong);
|
---|
85 |
|
---|
86 | return volume;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | int* qhull_convex_hull(int dim, int numpoints, double *data, int *retCode, int* nrOfFacets) {
|
---|
91 | boolT ismalloc= False;
|
---|
92 | char flags[250];
|
---|
93 | FILE *outfile= NULL;
|
---|
94 | FILE *errfile= stderr;
|
---|
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 |
|
---|
149 | void qhull_free(int* data) {
|
---|
150 | free(data);
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|