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)
73 , feasible_point()
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 
qhT::feasible_point
coordT * feasible_point
Definition: libqhull.h:588
orgQhull::QhullPoints::coordinates
coordT * coordinates() const
Definition: QhullPoints.h:90
QhullFacet.h
orgQhull::QhullPoint::setDimension
void setDimension(int pointDimension)
Definition: QhullPoint.h:105
QHULL_LIB_CHECK
#define QHULL_LIB_CHECK
Definition: libqhull.h:462
coordT
#define coordT
Definition: libqhull.h:80
orgQhull::Qhull::endVertex
QhullVertex endVertex() const
Definition: Qhull.h:112
QH_TRY_
#define QH_TRY_(qh)
Definition: QhullQh.h:31
FORALLfacet_
#define FORALLfacet_(facetlist)
Definition: poly.h:77
orgQhull::Qhull::otherPoints
QhullPointSet otherPoints() const
Definition: Qhull.cpp:197
orgQhull::Qhull::inputOrigin
QhullPoint inputOrigin()
Return origin point for qh.input_dim.
Definition: Qhull.cpp:133
orgQhull
QhullRidge – Qhull's ridge structure, ridgeT, as a C++ class.
Definition: Coordinates.cpp:21
orgQhull::QhullVertexList
QhullLinkedList< QhullVertex > QhullVertexList
Definition: QhullVertex.h:34
qhT::hull_dim
int hull_dim
Definition: libqhull.h:591
orgQhull::Qhull::runQhull
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
orgQhull::Qhull::area
double area()
Definition: Qhull.cpp:143
orgQhull::QhullPoints
Java-style iterator.
Definition: QhullPoints.h:34
orgQhull::s_unsupported_options
const char s_unsupported_options[]
Definition: Qhull.cpp:36
orgQhull::Qhull::outputQhull
void outputQhull()
Definition: Qhull.cpp:211
qhT::totarea
realT totarea
Definition: libqhull.h:744
orgQhull::QhullPoint
Definition: QhullPoint.h:39
qh_check_output
void qh_check_output(void)
Definition: poly2.c:302
qh_initqhull_outputflags
void qh_initqhull_outputflags(void)
Definition: global.c:1722
realT
#define realT
Definition: user.h:154
qh_checkflags
void qh_checkflags(char *command, char *hiddenflags)
Definition: global.c:86
facetT::good
flagT good
Definition: libqhull.h:332
qhT::rbox_command
char rbox_command[256]
Definition: libqhull.h:600
qhT::STOPpoint
int STOPpoint
Definition: libqhull.h:567
orgQhull::QhullPointSet
Definition: QhullPointSet.h:37
Qhull.h
qhT::PROJECTdelaunay
boolT PROJECTdelaunay
Definition: libqhull.h:545
qhT::feasible_string
char * feasible_string
Definition: libqhull.h:587
orgQhull::Qhull::endFacet
QhullFacet endFacet() const
Definition: Qhull.h:111
qhT::DELAUNAY
boolT DELAUNAY
Definition: libqhull.h:491
orgQhull::Qhull::origin_point
Coordinates origin_point
qhT for this instance
Definition: Qhull.h:54
qh_malloc
void * qh_malloc(size_t size)
Definition: usermem.c:90
qhT
Definition: libqhull.h:465
orgQhull::Qhull::volume
double volume()
Definition: Qhull.cpp:156
orgQhull::Qhull::allocateQhullQh
void allocateQhullQh()
Definition: Qhull.cpp:80
orgQhull::Coordinates::end
iterator end()
Definition: Coordinates.h:107
qh_clear_outputflags
void qh_clear_outputflags(void)
Definition: global.c:167
qhT::KEEPminArea
realT KEEPminArea
Definition: libqhull.h:509
orgQhull::PointCoordinates::comment
std::string comment() const
Definition: PointCoordinates.h:82
QhullFacetList.h
facetT
Definition: libqhull.h:262
orgQhull::Qhull::vertexList
QhullVertexList vertexList() const
Return vertices of the convex hull.
Definition: Qhull.cpp:204
qhT::totvol
realT totvol
Definition: libqhull.h:745
orgQhull::QhullFacetList
Definition: QhullFacetList.h:41
qh_check_points
void qh_check_points(void)
Definition: poly2.c:365
qhT::HALFspace
boolT HALFspace
Definition: libqhull.h:501
qh_qh
qhT qh_qh
Definition: global.c:26
orgQhull::Coordinates::size
size_t size() const
Definition: Coordinates.h:84
orgQhull::Qhull::run_called
bool run_called
origin for qh_qh->hull_dim. Set by runQhull()
Definition: Qhull.h:55
orgQhull::Coordinates::append
void append(int pointDimension, coordT *c)
Definition: Coordinates.cpp:81
qhT::num_points
int num_points
Definition: libqhull.h:593
orgQhull::Qhull
Interface to Qhull from C++.
Definition: Qhull.h:49
orgQhull::QhullQh::clearQhullMessage
void clearQhullMessage()
clearQhullMessage does not throw errors (~Qhull)
Definition: QhullQh.cpp:102
orgQhull::Coordinates::isEmpty
bool isEmpty() const
Definition: Coordinates.h:81
False
#define False
Definition: libqhull.h:128
REALmax
#define REALmax
Definition: user.h:155
orgQhull::Qhull::checkIfQhullInitialized
void checkIfQhullInitialized()
Definition: Qhull.cpp:110
orgQhull::Qhull::initialized
bool initialized() const
Definition: Qhull.h:82
orgQhull::Qhull::beginVertex
QhullVertex beginVertex() const
Definition: Qhull.h:109
qhT::facet_list
facetT * facet_list
Definition: libqhull.h:677
qhT::SPLITthresholds
boolT SPLITthresholds
Definition: libqhull.h:563
qh_errexit
void qh_errexit(int exitcode, facetT *facet, ridgeT *ridge)
Definition: user.c:213
qhT::NOerrexit
boolT NOerrexit
Definition: libqhull.h:737
qhT::GOODpoint
int GOODpoint
Definition: libqhull.h:495
orgQhull::QhullQh
POD type equivalent to qhT. No virtual members.
Definition: QhullQh.h:58
qh_setfeasible
void qh_setfeasible(int dim)
Definition: io.c:3967
orgQhull::Qhull::defineVertexNeighborFacets
void defineVertexNeighborFacets()
Automatically called if merging facets or Voronoi diagram.
Definition: Qhull.cpp:174
qh_ERRmem
#define qh_ERRmem
Definition: libqhull.h:197
qh_sethalfspace_all
coordT * qh_sethalfspace_all(int dim, int count, coordT *halfspaces, pointT *feasible)
Definition: geom2.c:1918
orgQhull::Qhull::beginFacet
QhullFacet beginFacet() const
Definition: Qhull.h:108
t
tuple t
orgQhull::Qhull::Qhull
Qhull()
call runQhull() next
Definition: Qhull.cpp:41
qhT::qhull_command
char qhull_command[256]
Definition: libqhull.h:598
qhT::STOPcone
int STOPcone
Definition: libqhull.h:565
qhT::first_point
pointT * first_point
Definition: libqhull.h:594
orgQhull::Qhull::qh_qh
QhullQh * qh_qh
Definition: Qhull.h:53
qhT::ferr
FILE * ferr
Definition: libqhull.h:662
orgQhull::Qhull::facetList
QhullFacetList facetList() const
Definition: Qhull.cpp:186
qhT::GOODvertex
int GOODvertex
Definition: libqhull.h:499
qhT::ONLYgood
boolT ONLYgood
Definition: libqhull.h:522
orgQhull::Qhull::points
QhullPoints points() const
Definition: Qhull.cpp:191
orgQhull::QhullPoints::count
countT count() const
Definition: QhullPoints.h:92
qh_prepare_output
void qh_prepare_output(void)
Definition: io.c:1071
orgQhull::Qhull::feasiblePoint
Coordinates feasiblePoint() const
Definition: Qhull.cpp:120
orgQhull::RboxPoints
Definition: RboxPoints.h:37
qhT::GOODthreshold
boolT GOODthreshold
Definition: libqhull.h:497
qh_getarea
void qh_getarea(facetT *facetlist)
Definition: geom2.c:702
qh_produce_output2
void qh_produce_output2(void)
Definition: io.c:52
orgQhull::Qhull::~Qhull
~Qhull()
Definition: Qhull.cpp:95
orgQhull::QhullQh::qhullMessage
std::string qhullMessage() const
qhullMessage does not throw errors (~Qhull)
Definition: QhullQh.cpp:157
qh_qhull
void qh_qhull(void)
Definition: libqhull.c:60
orgQhull::Coordinates::const_iterator
Definition: Coordinates.h:200
pointT
#define pointT
Definition: libqhull.h:96
orgQhull::QhullPoints::dimension
int dimension() const
Definition: QhullPoints.h:98
QhullQh.h
qh_initflags
void qh_initflags(char *command)
Definition: global.c:615
orgQhull::Coordinates
Definition: Coordinates.h:38
orgQhull::QhullLinkedList
Definition: QhullLinkedList.h:50
orgQhull::QhullQh::hasQhullMessage
bool hasQhullMessage() const
hasQhullMessage does not throw errors (~Qhull)
Definition: QhullQh.cpp:111
qhT::KEEParea
int KEEParea
Definition: libqhull.h:504
orgQhull::s_not_output_options
const char s_not_output_options[]
Definition: Qhull.cpp:37
orgQhull::QhullError
Definition: QhullError.h:26
orgQhull::Qhull::initializeFeasiblePoint
void initializeFeasiblePoint(int hulldim)
Definition: Qhull.cpp:323
qhT::input_dim
int input_dim
Definition: libqhull.h:592
orgQhull::Coordinates::begin
iterator begin()
Definition: Coordinates.h:103
qh_fprintf
void qh_fprintf(FILE *fp, int msgcode, const char *fmt,...)
Definition: userprintf.c:42
orgQhull::Qhull::feasible_point
Coordinates feasible_point
True at start of runQhull. Errors if call again.
Definition: Qhull.h:56
qh_init_B
void qh_init_B(coordT *points, int numpoints, int dim, boolT ismalloc)
Definition: global.c:534
qhT::hasAreaVolume
boolT hasAreaVolume
Definition: libqhull.h:719
orgQhull::QhullQh::maybeThrowQhullMessage
void maybeThrowQhullMessage(int exitCode)
Definition: QhullQh.cpp:118
RboxPoints.h
QhullError.h
qhT::VERIFYoutput
boolT VERIFYoutput
Definition: libqhull.h:580
qhT::other_points
setT * other_points
Definition: libqhull.h:762
orgQhull::Qhull::origin
QhullPoint origin()
non-const due to QhullPoint
Definition: Qhull.h:86
qh_vertexneighbors
void qh_vertexneighbors(void)
Definition: poly2.c:3174
qhT::KEEPmerge
int KEEPmerge
Definition: libqhull.h:508
True
#define True
Definition: libqhull.h:129


hpp-fcl
Author(s):
autogenerated on Fri Aug 2 2024 02:45:14