QhullQh.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/QhullQh.cpp#5 $$Change: 2066 $
5 ** $DateTime: 2016/01/18 19:29:17 $$Author: bbarber $
6 **
7 ****************************************************************************/
8 
9 #
10 
11 
12 #include "libqhullcpp/QhullQh.h"
13 
14 #include "libqhullcpp/QhullError.h"
15 #include "libqhullcpp/QhullStat.h"
16 
17 #include <sstream>
18 #include <iostream>
19 
20 #include <stdarg.h>
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 const double QhullQh::
36 default_factor_epsilon= 1.0;
37 
38 #
39 
43 : qhull_status(qh_ERRnone)
44 , qhull_message()
45 , error_stream(0)
46 , output_stream(0)
47 , factor_epsilon(QhullQh::default_factor_epsilon)
48 , use_output_stream(false)
49 {
50  // NOerrors: TRY_QHULL_ not needed since these routines do not call qh_errexit()
51  qh_meminit(this, NULL);
52  qh_initstatistics(this);
53  qh_initqhull_start2(this, NULL, NULL, qh_FILEstderr); // Initialize qhT
54  this->ISqhullQh= True;
55 }//QhullQh
56 
59 {
61 }//~QhullQh
62 
63 #
64 
65 void QhullQh::
72 {
73 #ifdef qh_NOmem
74  qh_freeqhull(this, qh_ALL);
75 #else
76  qh_memcheck(this);
77  qh_freeqhull(this, !qh_ALL);
78  countT curlong;
79  countT totlong;
80  qh_memfreeshort(this, &curlong, &totlong);
81  if (curlong || totlong)
82  throw QhullError(10026, "Qhull error: qhull did not free %d bytes of long memory (%d pieces).", totlong, curlong);
83 #endif
84 }//checkAndFreeQhullMemory
85 
86 #
87 
88 void QhullQh::
89 appendQhullMessage(const string &s)
90 {
91  if(output_stream && use_output_stream && this->USEstdout){
92  *output_stream << s;
93  }else if(error_stream){
94  *error_stream << s;
95  }else{
96  qhull_message += s;
97  }
98 }//appendQhullMessage
99 
101 void QhullQh::
103 {
105  qhull_message.clear();
107 }//clearQhullMessage
108 
110 bool QhullQh::
112 {
113  return (!qhull_message.empty() || qhull_status!=qh_ERRnone);
114  //FIXUP QH11006 -- inconsistent usage with Rbox. hasRboxMessage just tests rbox_status. No appendRboxMessage()
115 }
116 
117 void QhullQh::
119 {
120  if(!NOerrexit){
121  if(qhull_message.size()>0){
122  qhull_message.append("\n");
123  }
124  if(exitCode || qhull_status==qh_ERRnone){
125  qhull_status= 10073;
126  }else{
127  qhull_message.append("QH10073: ");
128  }
129  qhull_message.append("Cannot call maybeThrowQhullMessage() from QH_TRY_(). Or missing 'qh->NOerrexit=true;' after QH_TRY_(){...}.");
130  }
132  qhull_status= exitCode;
133  }
137  throw e; // FIXUP QH11007: copy constructor is expensive if logging
138  }
139 }//maybeThrowQhullMessage
140 
141 void QhullQh::
142 maybeThrowQhullMessage(int exitCode, int noThrow) throw()
143 {
144  QHULL_UNUSED(noThrow);
145 
146  if(qhull_status==qh_ERRnone){
147  qhull_status= exitCode;
148  }
149  if(qhull_status!=qh_ERRnone){
150  QhullError e(qhull_status, qhull_message);
151  e.logErrorLastResort();
152  }
153 }//maybeThrowQhullMessage
154 
156 std::string QhullQh::
158 {
159  if(qhull_message.empty() && qhull_status!=qh_ERRnone){
160  return "qhull: no message for error. Check cerr or error stream\n";
161  }else{
162  return qhull_message;
163  }
164 }//qhullMessage
165 
166 int QhullQh::
167 qhullStatus() const
168 {
169  return qhull_status;
170 }//qhullStatus
171 
172 void QhullQh::
173 setErrorStream(ostream *os)
174 {
175  error_stream= os;
176 }//setErrorStream
177 
179 void QhullQh::
180 setOutputStream(ostream *os)
181 {
182  output_stream= os;
183  use_output_stream= (os!=0);
184 }//setOutputStream
185 
186 }//namespace orgQhull
187 
188 /*-<a href="qh_qh-user.htm#TOC"
189  >-------------------------------</a><a name="qh_fprintf">-</a>
190 
191  qh_fprintf(qhT *qh, fp, msgcode, format, list of args )
192  replaces qh_fprintf() in userprintf_r.c
193 
194 notes:
195  only called from libqhull
196  same as fprintf() and RboxPoints.qh_fprintf_rbox()
197  fgets() is not trapped like fprintf()
198  Do not throw errors from here. Use qh_errexit;
199 */
200 extern "C"
201 void qh_fprintf(qhT *qh, FILE *fp, int msgcode, const char *fmt, ... ) {
202  va_list args;
203 
204  using namespace orgQhull;
205 
206  if(!qh->ISqhullQh){
207  qh_fprintf_stderr(10025, "Qhull error: qh_fprintf called from a Qhull instance without QhullQh defined\n");
208  qh_exit(10025);
209  }
210  QhullQh *qhullQh= static_cast<QhullQh *>(qh);
211  va_start(args, fmt);
212  if(msgcode<MSG_OUTPUT || fp == qh_FILEstderr){
213  if(msgcode>=MSG_ERROR && msgcode<MSG_WARNING){
214  if(qhullQh->qhull_status<MSG_ERROR || qhullQh->qhull_status>=MSG_WARNING){
215  qhullQh->qhull_status= msgcode;
216  }
217  }
218  char newMessage[MSG_MAXLEN];
219  // RoadError will add the message tag
220  vsnprintf(newMessage, sizeof(newMessage), fmt, args);
221  qhullQh->appendQhullMessage(newMessage);
222  va_end(args);
223  return;
224  }
225  if(qhullQh->output_stream && qhullQh->use_output_stream){
226  char newMessage[MSG_MAXLEN];
227  vsnprintf(newMessage, sizeof(newMessage), fmt, args);
228  *qhullQh->output_stream << newMessage;
229  va_end(args);
230  return;
231  }
232  // FIXUP QH11008: how do users trap messages and handle input? A callback?
233  char newMessage[MSG_MAXLEN];
234  vsnprintf(newMessage, sizeof(newMessage), fmt, args);
235  qhullQh->appendQhullMessage(newMessage);
236  va_end(args);
237 } /* qh_fprintf */
countT
int countT
Definition: user_r.h:182
orgQhull::RoadError::clearGlobalLog
static void clearGlobalLog()
Definition: RoadError.h:67
obb.fmt
fmt
Definition: obb.py:145
qh_fprintf_stderr
void qh_fprintf_stderr(int msgcode, const char *fmt,...)
Definition: usermem.c:57
qh_initqhull_start2
void qh_initqhull_start2(FILE *infile, FILE *outfile, FILE *errfile)
Definition: global.c:1856
qh_ALL
#define qh_ALL
Definition: libqhull.h:180
orgQhull
QhullRidge – Qhull's ridge structure, ridgeT, as a C++ class.
Definition: Coordinates.cpp:21
orgQhull::QhullQh::appendQhullMessage
void appendQhullMessage(const std::string &s)
Definition: QhullQh.cpp:89
orgQhull::QhullQh::output_stream
std::ostream * output_stream
send output to stream
Definition: QhullQh.h:67
orgQhull::RoadError::logErrorLastResort
void logErrorLastResort() const
Definition: RoadError.cpp:151
qhT
Definition: libqhull.h:465
orgQhull::QhullQh::qhullStatus
int qhullStatus() const
Definition: QhullQh.cpp:167
qh_memcheck
void qh_memcheck(void)
Definition: mem.c:203
qh_FILEstderr
#define qh_FILEstderr
Definition: libqhull.h:207
MSG_WARNING
#define MSG_WARNING
Definition: user.h:75
orgQhull::QhullQh::qhull_status
int qhull_status
qh_ERRnone if valid
Definition: QhullQh.h:64
qh_initstatistics
void qh_initstatistics(void)
Definition: stat.c:450
doxygen_xml_parser.args
args
Definition: doxygen_xml_parser.py:884
qh_ERRnone
#define qh_ERRnone
Definition: libqhull.h:193
MSG_ERROR
#define MSG_ERROR
Definition: user.h:74
orgQhull::QhullQh::clearQhullMessage
void clearQhullMessage()
clearQhullMessage does not throw errors (~Qhull)
Definition: QhullQh.cpp:102
orgQhull::QhullQh::qhull_message
std::string qhull_message
Returned messages from libqhull_r.
Definition: QhullQh.h:65
orgQhull::QhullQh::QhullQh
QhullQh()
Derived from qh_new_qhull[user.c].
Definition: QhullQh.cpp:42
qh_fprintf
void qh_fprintf(qhT *qh, FILE *fp, int msgcode, const char *fmt,...)
Definition: QhullQh.cpp:201
MSG_OUTPUT
#define MSG_OUTPUT
Definition: user.h:77
orgQhull::QhullQh::error_stream
std::ostream * error_stream
overrides errorMessage, use appendQhullMessage()
Definition: QhullQh.h:66
QhullStat.h
qh
#define qh
Definition: libqhull.h:457
qh_freeqhull
void qh_freeqhull(boolT allmem)
Definition: global.c:425
qhT::NOerrexit
boolT NOerrexit
Definition: libqhull.h:737
orgQhull::QhullQh
POD type equivalent to qhT. No virtual members.
Definition: QhullQh.h:58
orgQhull::QhullQh::use_output_stream
bool use_output_stream
True if using output_stream.
Definition: QhullQh.h:69
qh_exit
void qh_exit(int exitcode)
Definition: usermem_r-cpp.cpp:36
orgQhull::QhullQh::setErrorStream
void setErrorStream(std::ostream *os)
Definition: QhullQh.cpp:173
orgQhull::QhullQh::~QhullQh
~QhullQh()
Definition: QhullQh.cpp:58
orgQhull::QhullQh::checkAndFreeQhullMemory
void checkAndFreeQhullMemory()
Definition: QhullQh.cpp:71
orgQhull::QhullQh::qhullMessage
std::string qhullMessage() const
qhullMessage does not throw errors (~Qhull)
Definition: QhullQh.cpp:157
QHULL_UNUSED
#define QHULL_UNUSED(x)
Definition: qhull_a.h:109
qh_memfreeshort
void qh_memfreeshort(int *curlong, int *totlong)
Definition: mem.c:288
MSG_MAXLEN
#define MSG_MAXLEN
Definition: user.h:80
QhullQh.h
orgQhull::QhullQh::setOutputStream
void setOutputStream(std::ostream *os)
Updates use_output_stream.
Definition: QhullQh.cpp:180
orgQhull::QhullQh::hasQhullMessage
bool hasQhullMessage() const
hasQhullMessage does not throw errors (~Qhull)
Definition: QhullQh.cpp:111
orgQhull::QhullError
Definition: QhullError.h:26
qh_meminit
void qh_meminit(FILE *ferr)
Definition: mem.c:317
orgQhull::QhullQh::maybeThrowQhullMessage
void maybeThrowQhullMessage(int exitCode)
Definition: QhullQh.cpp:118
qhT::USEstdout
boolT USEstdout
Definition: libqhull.h:579
QhullError.h
qhT::ISqhullQh
boolT ISqhullQh
Definition: libqhull.h:502
True
#define True
Definition: libqhull.h:129


hpp-fcl
Author(s):
autogenerated on Fri Jan 26 2024 03:46:15