Qhull.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (c) 2008-2015 C.B. Barber. All rights reserved.
4 ** $Id: //main/2015/qhull/src/libqhullcpp/Qhull.cpp#3 $$Change: 2066 $
5 ** $DateTime: 2016/01/18 19:29:17 $$Author: bbarber $
6 **
7 ****************************************************************************/
8 
9 #
10 #
11 
12 #include "libqhullcpp/Qhull.h"
13 
14 #include "libqhullcpp/QhullError.h"
15 #include "libqhullcpp/RboxPoints.h"
16 #include "libqhullcpp/QhullQh.h"
17 #include "libqhullcpp/QhullFacet.h"
19 
20 #include <iostream>
21 
22 using std::cerr;
23 using std::string;
24 using std::vector;
25 using std::ostream;
26 
27 #ifdef _MSC_VER // Microsoft Visual C++ -- warning level 4
28 #pragma warning( disable : 4611) // interaction between '_setjmp' and C++ object destruction is non-portable
29 #pragma warning( disable : 4996) // function was declared deprecated(strcpy, localtime, etc.)
30 #endif
31 
32 namespace orgQhull {
33 
34 #
35 
36 const char s_unsupported_options[]=" Fd TI ";
37 const char s_not_output_options[]= " Fd TI A C d E H P Qb QbB Qbb Qc Qf Qg Qi Qm QJ Qr QR Qs Qt Qv Qx Qz Q0 Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 R Tc TC TM TP TR Tv TV TW U v V W ";
38 
39 #
40 Qhull::
42 : qh_qh(0)
43 , origin_point()
44 , run_called(false)
45 , feasible_point()
46 {
47  allocateQhullQh();
48 }//Qhull
49 
54 Qhull::
55 Qhull(const RboxPoints &rboxPoints, const char *qhullCommand2)
56 : qh_qh(0)
57 , origin_point()
58 , run_called(false)
59 , feasible_point()
60 {
62  runQhull(rboxPoints, qhullCommand2);
63 }//Qhull rbox
64 
68 Qhull::
69 Qhull(const char *inputComment2, int pointDimension, int pointCount, const realT *pointCoordinates, const char *qhullCommand2)
70 : qh_qh(0)
71 , origin_point()
72 , run_called(false)
74 {
76  runQhull(inputComment2, pointDimension, pointCount, pointCoordinates, qhullCommand2);
77 }//Qhull points
78 
79 void Qhull::
81 {
82  QHULL_LIB_CHECK /* Check for compatible library */
83 
84  qh_qh= new QhullQh;
85  void *p= qh_qh;
86  void *p2= static_cast<qhT *>(qh_qh);
87  char *s= static_cast<char *>(p);
88  char *s2= static_cast<char *>(p2);
89  if(s!=s2){
90  throw QhullError(10074, "Qhull error: QhullQh at a different address than base type QhT (%d bytes). Please report compiler to qhull.org", int(s2-s));
91  }
92 }//allocateQhullQh
93 
94 Qhull::
95 ~Qhull() throw()
96 {
97  // Except for cerr, does not throw errors
98  if(qh_qh->hasQhullMessage()){
99  cerr<< "\nQhull output at end\n"; //FIXUP QH11005: where should error and log messages go on ~Qhull?
100  cerr<< qh_qh->qhullMessage();
102  }
103  delete qh_qh;
104  qh_qh= 0;
105 }//~Qhull
106 
107 #
108 
109 void Qhull::
111 {
112  if(!initialized()){ // qh_initqhull_buffers() not called
113  throw QhullError(10023, "Qhull error: checkIfQhullInitialized failed. Call runQhull() first.");
114  }
115 }//checkIfQhullInitialized
116 
121 {
122  Coordinates result;
123  if(qh_qh->feasible_point){
125  }else{
126  result= feasible_point;
127  }
128  return result;
129 }//feasiblePoint
130 
134 {
135  QhullPoint result= origin();
136  result.setDimension(qh_qh->input_dim);
137  return result;
138 }//inputOrigin
139 
140 #
141 
142 double Qhull::
145  if(!qh_qh->hasAreaVolume){
146  QH_TRY_(qh_qh){ // no object creation -- destructors skipped on longjmp()
148  }
149  qh_qh->NOerrexit= true;
150  qh_qh->maybeThrowQhullMessage(QH_TRY_status);
151  }
152  return qh_qh->totarea;
153 }//area
154 
155 double Qhull::
158  if(!qh_qh->hasAreaVolume){
159  QH_TRY_(qh_qh){ // no object creation -- destructors skipped on longjmp()
161  }
162  qh_qh->NOerrexit= true;
163  qh_qh->maybeThrowQhullMessage(QH_TRY_status);
164  }
165  return qh_qh->totvol;
166 }//volume
167 
168 #
169 
170 void Qhull::
176  if(!qh_qh->hasAreaVolume){
177  QH_TRY_(qh_qh){ // no object creation -- destructors skipped on longjmp()
179  }
180  qh_qh->NOerrexit= true;
181  qh_qh->maybeThrowQhullMessage(QH_TRY_status);
182  }
183 }//defineVertexNeighborFacets
184 
186 facetList() const{
187  return QhullFacetList(beginFacet(), endFacet());
188 }//facetList
189 
191 points() const
192 {
194 }//points
195 
197 otherPoints() const
198 {
200 }//otherPoints
201 
204 vertexList() const{
206 }//vertexList
207 
208 #
209 
210 void Qhull::
212 {
214  QH_TRY_(qh_qh){ // no object creation -- destructors skipped on longjmp()
216  }
217  qh_qh->NOerrexit= true;
218  qh_qh->maybeThrowQhullMessage(QH_TRY_status);
219 }//outputQhull
220 
221 void Qhull::
222 outputQhull(const char *outputflags)
223 {
225  string cmd(" "); // qh_checkflags skips first word
226  cmd += outputflags;
227  char *command= const_cast<char*>(cmd.c_str());
228  QH_TRY_(qh_qh){ // no object creation -- destructors skipped on longjmp()
230  char *s = qh_qh->qhull_command + strlen(qh_qh->qhull_command) + 1; //space
231  strncat(qh_qh->qhull_command, command, sizeof(qh_qh->qhull_command)-strlen(qh_qh->qhull_command)-1);
232  qh_checkflags(qh_qh, command, const_cast<char *>(s_not_output_options));
233  qh_initflags(qh_qh, s);
235  if(qh_qh->KEEPminArea < REALmax/2
236  || (0 != qh_qh->KEEParea + qh_qh->KEEPmerge + qh_qh->GOODvertex
238  facetT *facet;
239  qh_qh->ONLYgood= False;
241  facet->good= True;
242  }
244  }
248  }
249  }
250  qh_qh->NOerrexit= true;
251  qh_qh->maybeThrowQhullMessage(QH_TRY_status);
252 }//outputQhull
253 
255 void Qhull::
256 runQhull(const RboxPoints &rboxPoints, const char *qhullCommand2)
257 {
258  runQhull(rboxPoints.comment().c_str(), rboxPoints.dimension(), rboxPoints.count(), &*rboxPoints.coordinates(), qhullCommand2);
259 }//runQhull, RboxPoints
260 
265 void Qhull::
266 runQhull(const char *inputComment2, int pointDimension, int pointCount, const realT *pointCoordinates, const char *qhullCommand2)
267 {
268  if(run_called){
269  throw QhullError(10027, "Qhull error: runQhull called twice. Only one call allowed.");
270  }
271  run_called= true;
272  string s("qhull ");
273  s += qhullCommand2;
274  char *command= const_cast<char*>(s.c_str());
275  /************* Expansion of QH_TRY_ for debugging
276  int QH_TRY_status;
277  if(qh_qh->NOerrexit){
278  qh_qh->NOerrexit= False;
279  QH_TRY_status= setjmp(qh_qh->errexit);
280  }else{
281  QH_TRY_status= QH_TRY_ERROR;
282  }
283  if(!QH_TRY_status){
284  *************/
285  QH_TRY_(qh_qh){ // no object creation -- destructors are skipped on longjmp()
286  qh_checkflags(qh_qh, command, const_cast<char *>(s_unsupported_options));
287  qh_initflags(qh_qh, command);
288  *qh_qh->rbox_command= '\0';
289  strncat( qh_qh->rbox_command, inputComment2, sizeof(qh_qh->rbox_command)-1);
290  if(qh_qh->DELAUNAY){
291  qh_qh->PROJECTdelaunay= True; // qh_init_B() calls qh_projectinput()
292  }
293  pointT *newPoints= const_cast<pointT*>(pointCoordinates);
294  int newDimension= pointDimension;
295  int newIsMalloc= False;
296  if(qh_qh->HALFspace){
297  --newDimension;
298  initializeFeasiblePoint(newDimension);
299  newPoints= qh_sethalfspace_all(qh_qh, pointDimension, pointCount, newPoints, qh_qh->feasible_point);
300  newIsMalloc= True;
301  }
302  qh_init_B(qh_qh, newPoints, pointCount, newDimension, newIsMalloc);
303  qh_qhull(qh_qh);
308  }
309  }
310  qh_qh->NOerrexit= true;
311  for(int k= qh_qh->hull_dim; k--; ){ // Do not move into QH_TRY block. It may throw an error
312  origin_point << 0.0;
313  }
314  qh_qh->maybeThrowQhullMessage(QH_TRY_status);
315 }//runQhull
316 
317 #
318 
319 void Qhull::
324 {
325  if(qh_qh->feasible_string){
326  qh_setfeasible(qh_qh, hulldim);
327  }else{
328  if(feasible_point.isEmpty()){
329  qh_fprintf(qh_qh, qh_qh->ferr, 6209, "qhull error: missing feasible point for halfspace intersection. Use option 'Hn,n' or Qhull::setFeasiblePoint before runQhull()\n");
330  qh_errexit(qh_qh, qh_ERRmem, NULL, NULL);
331  }
332  if(feasible_point.size()!=(size_t)hulldim){
333  qh_fprintf(qh_qh, qh_qh->ferr, 6210, "qhull error: dimension of feasiblePoint should be %d. It is %u", hulldim, feasible_point.size());
334  qh_errexit(qh_qh, qh_ERRmem, NULL, NULL);
335  }
336  if (!(qh_qh->feasible_point= (coordT*)qh_malloc(hulldim * sizeof(coordT)))) {
337  qh_fprintf(qh_qh, qh_qh->ferr, 6202, "qhull error: insufficient memory for feasible point\n");
338  qh_errexit(qh_qh, qh_ERRmem, NULL, NULL);
339  }
341  // No qh_... routines after here -- longjmp() ignores destructor
343  *t++= *p;
344  }
345  }
346 }//initializeFeasiblePoint
347 
348 }//namespace orgQhull
349 
int hull_dim
Definition: libqhull.h:591
Definition: libqhull.h:465
void qh_clear_outputflags(void)
Definition: global.c:167
int KEEPmerge
Definition: libqhull.h:508
void * qh_malloc(size_t size)
Definition: usermem.c:90
realT KEEPminArea
Definition: libqhull.h:509
const char s_unsupported_options[]
Definition: Qhull.cpp:36
QhullPoint inputOrigin()
Return origin point for qh.input_dim.
Definition: Qhull.cpp:133
int STOPpoint
Definition: libqhull.h:567
QhullRidge – Qhull&#39;s ridge structure, ridgeT, as a C++ class.
Definition: Coordinates.cpp:21
bool isEmpty() const
Definition: Coordinates.h:75
char * feasible_string
Definition: libqhull.h:587
QhullQh * qh_qh
Definition: Qhull.h:47
bool hasQhullMessage() const
hasQhullMessage does not throw errors (~Qhull)
Definition: QhullQh.cpp:111
bool initialized() const
Definition: Qhull.h:76
countT count() const
Definition: QhullPoints.h:86
void maybeThrowQhullMessage(int exitCode)
Definition: QhullQh.cpp:118
void qh_prepare_output(void)
Definition: io.c:1071
POD type equivalent to qhT. No virtual members.
Definition: QhullQh.h:58
facetT * facet_list
Definition: libqhull.h:677
t
#define pointT
Definition: libqhull.h:96
QhullPointSet otherPoints() const
Definition: Qhull.cpp:197
void qh_fprintf(FILE *fp, int msgcode, const char *fmt,...)
Definition: userprintf.c:42
double volume()
Definition: Qhull.cpp:156
Qhull()
call runQhull() next
Definition: Qhull.cpp:41
coordT * qh_sethalfspace_all(int dim, int count, coordT *halfspaces, pointT *feasible)
Definition: geom2.c:1918
void qh_check_output(void)
Definition: poly2.c:302
#define QH_TRY_(qh)
Definition: QhullQh.h:31
#define FORALLfacet_(facetlist)
Definition: poly.h:77
pointT * first_point
Definition: libqhull.h:594
qhT qh_qh
Definition: global.c:26
int dimension() const
Definition: QhullPoints.h:92
boolT PROJECTdelaunay
Definition: libqhull.h:545
Coordinates origin_point
qhT for this instance
Definition: Qhull.h:48
boolT DELAUNAY
Definition: libqhull.h:491
void outputQhull()
Definition: Qhull.cpp:211
realT totarea
Definition: libqhull.h:744
bool run_called
origin for qh_qh->hull_dim. Set by runQhull()
Definition: Qhull.h:49
QhullPoints points() const
Definition: Qhull.cpp:191
boolT HALFspace
Definition: libqhull.h:501
QhullVertexList vertexList() const
Return vertices of the convex hull.
Definition: Qhull.cpp:204
void initializeFeasiblePoint(int hulldim)
Definition: Qhull.cpp:323
setT * other_points
Definition: libqhull.h:762
#define REALmax
Definition: user.h:155
Java-style iterator.
Definition: QhullPoints.h:28
FILE * ferr
Definition: libqhull.h:662
void defineVertexNeighborFacets()
Automatically called if merging facets or Voronoi diagram.
Definition: Qhull.cpp:174
QhullVertex beginVertex() const
Definition: Qhull.h:103
#define coordT
Definition: libqhull.h:80
#define True
Definition: libqhull.h:129
QhullFacetList facetList() const
Definition: Qhull.cpp:186
double area()
Definition: Qhull.cpp:143
void qh_check_points(void)
Definition: poly2.c:365
Coordinates feasible_point
True at start of runQhull. Errors if call again.
Definition: Qhull.h:50
int num_points
Definition: libqhull.h:593
boolT SPLITthresholds
Definition: libqhull.h:563
realT totvol
Definition: libqhull.h:745
void clearQhullMessage()
clearQhullMessage does not throw errors (~Qhull)
Definition: QhullQh.cpp:102
QhullFacet beginFacet() const
Definition: Qhull.h:102
void setDimension(int pointDimension)
Definition: QhullPoint.h:99
int GOODpoint
Definition: libqhull.h:495
void checkIfQhullInitialized()
Definition: Qhull.cpp:110
Interface to Qhull from C++.
Definition: Qhull.h:43
void qh_produce_output2(void)
Definition: io.c:52
#define qh_ERRmem
Definition: libqhull.h:197
boolT NOerrexit
Definition: libqhull.h:737
const char s_not_output_options[]
Definition: Qhull.cpp:37
boolT VERIFYoutput
Definition: libqhull.h:580
int STOPcone
Definition: libqhull.h:565
coordT * feasible_point
Definition: libqhull.h:588
boolT hasAreaVolume
Definition: libqhull.h:719
void qh_init_B(coordT *points, int numpoints, int dim, boolT ismalloc)
Definition: global.c:534
size_t size() const
Definition: Coordinates.h:78
void qh_setfeasible(int dim)
Definition: io.c:3967
coordT * coordinates() const
Definition: QhullPoints.h:84
void runQhull(const RboxPoints &rboxPoints, const char *qhullCommand2)
For qhull commands, see http://www.qhull.org/html/qhull.htm or html/qhull.htm.
Definition: Qhull.cpp:256
QhullVertex endVertex() const
Definition: Qhull.h:106
QhullPoint origin()
non-const due to QhullPoint
Definition: Qhull.h:80
void allocateQhullQh()
Definition: Qhull.cpp:80
#define QHULL_LIB_CHECK
Definition: libqhull.h:462
std::string qhullMessage() const
qhullMessage does not throw errors (~Qhull)
Definition: QhullQh.cpp:157
int KEEParea
Definition: libqhull.h:504
Coordinates feasiblePoint() const
Definition: Qhull.cpp:120
boolT ONLYgood
Definition: libqhull.h:522
flagT good
Definition: libqhull.h:332
void qh_errexit(int exitcode, facetT *facet, ridgeT *ridge)
Definition: user.c:213
int input_dim
Definition: libqhull.h:592
void qh_vertexneighbors(void)
Definition: poly2.c:3174
boolT GOODthreshold
Definition: libqhull.h:497
std::string comment() const
void qh_initflags(char *command)
Definition: global.c:615
void qh_getarea(facetT *facetlist)
Definition: geom2.c:702
#define False
Definition: libqhull.h:128
int GOODvertex
Definition: libqhull.h:499
void qh_initqhull_outputflags(void)
Definition: global.c:1722
QhullLinkedList< QhullVertex > QhullVertexList
Definition: QhullVertex.h:28
char qhull_command[256]
Definition: libqhull.h:598
void append(int pointDimension, coordT *c)
Definition: Coordinates.cpp:75
char rbox_command[256]
Definition: libqhull.h:600
#define realT
Definition: user.h:154
QhullFacet endFacet() const
Definition: Qhull.h:105
void qh_checkflags(char *command, char *hiddenflags)
Definition: global.c:86
void qh_qhull(void)
Definition: libqhull.c:60


hpp-fcl
Author(s):
autogenerated on Fri Jun 2 2023 02:39:02