QhullHyperplane_test.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (c) 2009-2015 C.B. Barber. All rights reserved.
4 ** $Id: //main/2015/qhull/src/qhulltest/QhullHyperplane_test.cpp#4 $$Change: 2064 $
5 ** $DateTime: 2016/01/18 12:36:08 $$Author: bbarber $
6 **
7 ****************************************************************************/
8 
9 //pre-compiled headers
10 #include <iostream>
11 #include "qhulltest/RoadTest.h" // QT_VERSION
12 
14 #include "libqhullcpp/QhullError.h"
15 #include "libqhullcpp/RboxPoints.h"
16 #include "libqhullcpp/QhullFacet.h"
19 #include "libqhullcpp/Qhull.h"
20 
21 #include <numeric>
22 #include <vector>
23 
24 using std::cout;
25 using std::endl;
26 using std::ostringstream;
27 using std::ostream;
28 using std::string;
29 
30 namespace orgQhull {
31 
33 {
34  Q_OBJECT
35 
36 #
37 private slots:
38  void cleanup();
39  void t_construct();
40  void t_construct_qh();
41  void t_convert();
42  void t_readonly();
43  void t_define();
44  void t_value();
45  void t_operator();
46  void t_iterator();
47  void t_const_iterator();
49  void t_io();
50 };//QhullHyperplane_test
51 
52 void
54 {
55  new QhullHyperplane_test(); // RoadTest::s_testcases
56 }
57 
58 //Executed after each testcase
61 {
63 }
64 
67 {
68  QhullHyperplane h4;
69  QVERIFY(!h4.isValid());
70  QCOMPARE(h4.dimension(), 0);
71  // Qhull.runQhull() constructs QhullFacets as facetT
72  RboxPoints rcube("c");
73  Qhull q(rcube,"Qt QR0"); // triangulation of rotated unit cube
74  QhullHyperplane h(q);
75  QVERIFY(!h.isValid());
76  QCOMPARE(h.dimension(), 0);
77  QCOMPARE(h.coordinates(),static_cast<double *>(0));
78  QhullFacet f= q.firstFacet();
80  QVERIFY(h2.isValid());
81  QCOMPARE(h2.dimension(), 3);
82  h= h2;
83  QCOMPARE(h, h2);
84  QhullHyperplane h3(q, h2.dimension(), h2.coordinates(), h2.offset());
85  QCOMPARE(h2, h3);
86  QhullHyperplane h5= h2; // copy constructor
87  QVERIFY(h5==h2);
88 }//t_construct
89 
92 {
93  // Qhull.runQhull() constructs QhullFacets as facetT
94  RboxPoints rcube("c");
95  Qhull q(rcube,"Qt QR0"); // triangulation of rotated unit cube
96  QhullFacet f= q.firstFacet();
98  QVERIFY(h2.isValid());
99  QCOMPARE(h2.dimension(), 3);
100  // h= h2; // copy assignment disabled, ambiguous
101  QhullHyperplane h3(q.qh(), h2.dimension(), h2.coordinates(), h2.offset());
102  QCOMPARE(h2, h3);
103  QhullHyperplane h5= h2; // copy constructor
104  QVERIFY(h5==h2);
105 }//t_construct_qh
106 
109 {
110  RboxPoints rcube("c");
111  Qhull q(rcube,"Qt QR0"); // triangulation of rotated unit cube
113  std::vector<double> fs= h.toStdVector();
114  QCOMPARE(fs.size(), 4u);
115  double offset= fs.back();
116  fs.pop_back();
117  QCOMPARE(offset, -0.5);
118 
119  double squareNorm= inner_product(fs.begin(), fs.end(), fs.begin(), 0.0);
120  QCOMPARE(squareNorm, 1.0);
121  QList<double> qs= h.toQList();
122  QCOMPARE(qs.size(), 4);
123  double offset2= qs.takeLast();
124  QCOMPARE(offset2, -0.5);
125  double squareNorm2= std::inner_product(qs.begin(), qs.end(), qs.begin(), 0.0);
126  QCOMPARE(squareNorm2, 1.0);
127 }//t_convert
128 
131 {
132  RboxPoints rcube("c");
133  {
134  Qhull q(rcube,"Qt QR0"); // triangulation of rotated unit cube
135  QhullFacetList fs= q.facetList();
137  while(i.hasNext()){
138  QhullFacet f= i.next();
140  int id= f.id();
141  cout << "h" << id << endl;
142  QVERIFY(h.isValid());
143  QCOMPARE(h.dimension(),3);
144  const coordT *c= h.coordinates();
145  coordT *c2= h.coordinates();
146  QCOMPARE(c, c2);
147  const coordT *c3= h.begin();
148  QCOMPARE(c, c3);
149  QCOMPARE(h.offset(), -0.5);
150  int j= h.end()-h.begin();
151  QCOMPARE(j, 3);
152  double squareNorm= std::inner_product(h.begin(), h.end(), h.begin(), 0.0);
153  QCOMPARE(squareNorm, 1.0);
154  }
155  QhullHyperplane h2= fs.first().hyperplane();
156  QhullHyperplane h3= fs.last().hyperplane();
157  QVERIFY(h2!=h3);
158  QVERIFY(h3.coordinates()!=h2.coordinates());
159  }
160 }//t_readonly
161 
164 {
165  RboxPoints rcube("c");
166  {
167  Qhull q(rcube,"Qt QR0"); // triangulation of rotated unit cube
168  QhullFacetList fs= q.facetList();
169  QhullHyperplane h= fs.first().hyperplane();
170  QhullHyperplane h2= h;
171  QVERIFY(h==h2);
172  QhullHyperplane h3= fs.last().hyperplane();
173  QVERIFY(h2!=h3);
174 
175  QhullHyperplane h4= h3;
176  h4.defineAs(h2);
177  QVERIFY(h2==h4);
178  QhullHyperplane p5= h3;
179  p5.defineAs(h2.dimension(), h2.coordinates(), h2.offset());
180  QVERIFY(h2==p5);
181  QhullHyperplane h6= h3;
182  h6.setCoordinates(h2.coordinates());
183  QCOMPARE(h2.coordinates(), h6.coordinates());
184  h6.setOffset(h2.offset());
185  QCOMPARE(h2.offset(), h6.offset());
186  QVERIFY(h2==h6);
187  h6.setDimension(2);
188  QCOMPARE(h6.dimension(), 2);
189  QVERIFY(h2!=h6);
190  }
191 }//t_define
192 
195 {
196  RboxPoints rcube("c G1");
197  Qhull q(rcube,"Qt QR0"); // triangulation of rotated unit cube
198  QhullFacet f= q.firstFacet();
199  QhullFacet f2= f.neighborFacets().at(0);
200  const QhullHyperplane h= f.hyperplane();
201  const QhullHyperplane h2= f2.hyperplane(); // At right angles
202  double dist= h.distance(q.origin());
203  QCOMPARE(dist, -1.0);
204  double norm= h.norm();
205  QCOMPARE(norm, 1.0);
206  double angle= h.hyperplaneAngle(h2);
207  cout << "angle " << angle << endl;
208  QCOMPARE(angle+1.0, 1.0); // qFuzzyCompare does not work for 0.0
209  QVERIFY(h==h);
210  QVERIFY(h!=h2);
211 }//t_value
212 
215 {
216  RboxPoints rcube("c");
217  Qhull q(rcube,"Qt QR0"); // triangulation of rotated unit cube
218  const QhullHyperplane h= q.firstFacet().hyperplane();
219  //operator== and operator!= tested elsewhere
220  const coordT *c= h.coordinates();
221  for(int k=h.dimension(); k--; ){
222  QCOMPARE(c[k], h[k]);
223  }
224  //h[0]= 10.0; // compiler error, const
226  h2[0]= 10.0; // Overwrites Hyperplane coordinate!
227  QCOMPARE(h2[0], 10.0);
228 }//t_operator
229 
232 {
233  RboxPoints rcube("c");
234  {
235  Qhull q(rcube,"QR0"); // rotated unit cube
237  QCOMPARE(h.count(), 3);
238  QCOMPARE(h.size(), 3u);
241  QVERIFY(i==i2);
242  QVERIFY(i>=i2);
243  QVERIFY(i<=i2);
244  i= h.begin();
245  QVERIFY(i==i2);
246  i2= h.end();
247  QVERIFY(i!=i2);
248  double d3= *i;
249  i2--;
250  double d2= *i2;
251  QCOMPARE(d3, h[0]);
252  QCOMPARE(d2, h[2]);
254  QCOMPARE(*i2, *i3);
255 
256  (i3= i)++;
257  QCOMPARE((*i3), h[1]);
258  QVERIFY(i==i);
259  QVERIFY(i!=i2);
260  QVERIFY(i<i2);
261  QVERIFY(i<=i2);
262  QVERIFY(i2>i);
263  QVERIFY(i2>=i);
264 
266  QVERIFY(i==i4); // iterator COMP const_iterator
267  QVERIFY(i<=i4);
268  QVERIFY(i>=i4);
269  QVERIFY(i4==i); // const_iterator COMP iterator
270  QVERIFY(i4<=i);
271  QVERIFY(i4>=i);
272  QVERIFY(i>=i4);
273  QVERIFY(i4<=i);
274  QVERIFY(i2!=i4);
275  QVERIFY(i2>i4);
276  QVERIFY(i2>=i4);
277  QVERIFY(i4!=i2);
278  QVERIFY(i4<i2);
279  QVERIFY(i4<=i2);
280  ++i4;
281  QVERIFY(i<i4);
282  QVERIFY(i<=i4);
283  QVERIFY(i4>i);
284  QVERIFY(i4>=i);
285 
286  i= h.begin();
287  i2= h.begin();
288  QCOMPARE(i, i2++);
289  QCOMPARE(*i2, h[1]);
290  QCOMPARE(++i, i2);
291  QCOMPARE(i, i2--);
292  QCOMPARE(i2, h.begin());
293  QCOMPARE(--i, i2);
294  QCOMPARE(i2 += 3, h.end());
295  QCOMPARE(i2 -= 3, h.begin());
296  QCOMPARE(i2+0, h.begin());
297  QCOMPARE(i2+3, h.end());
298  i2 += 3;
299  i= i2-0;
300  QCOMPARE(i, i2);
301  i= i2-3;
302  QCOMPARE(i, h.begin());
303  QCOMPARE(i2-i, 3);
304 
305  //h.begin end tested above
306 
307  // QhullHyperplane is const-only
308  }
309 }//t_iterator
310 
313 {
314  RboxPoints rcube("c");
315  {
316  Qhull q(rcube,"QR0"); // rotated unit cube
320  QVERIFY(i==i2);
321  QVERIFY(i>=i2);
322  QVERIFY(i<=i2);
323  i= h.begin();
324  QVERIFY(i==i2);
325  i2= h.end();
326  QVERIFY(i!=i2);
327  double d3= *i;
328  i2--;
329  double d2= *i2;
330  QCOMPARE(d3, h[0]);
331  QCOMPARE(d2, h[2]);
333  QCOMPARE(*i2, *i3);
334 
335  (i3= i)++;
336  QCOMPARE((*i3), h[1]);
337  QVERIFY(i==i);
338  QVERIFY(i!=i2);
339  QVERIFY(i<i2);
340  QVERIFY(i<=i2);
341  QVERIFY(i2>i);
342  QVERIFY(i2>=i);
343 
344  // See t_iterator for const_iterator COMP iterator
345 
346  i= h.begin();
347  i2= h.constBegin();
348  QCOMPARE(i, i2++);
349  QCOMPARE(*i2, h[1]);
350  QCOMPARE(++i, i2);
351  QCOMPARE(i, i2--);
352  QCOMPARE(i2, h.constBegin());
353  QCOMPARE(--i, i2);
354  QCOMPARE(i2+=3, h.constEnd());
355  QCOMPARE(i2-=3, h.constBegin());
356  QCOMPARE(i2+0, h.constBegin());
357  QCOMPARE(i2+3, h.constEnd());
358  i2 += 3;
359  i= i2-0;
360  QCOMPARE(i, i2);
361  i= i2-3;
362  QCOMPARE(i, h.constBegin());
363  QCOMPARE(i2-i, 3);
364 
365  // QhullHyperplane is const-only
366  }
367 }//t_const_iterator
368 
371 {
372  RboxPoints rcube("c");
373  Qhull q(rcube,"QR0"); // rotated unit cube
375  QhullHyperplaneIterator i2(h);
376  QCOMPARE(h.dimension(), 3);
377  QhullHyperplaneIterator i= h;
378  QVERIFY(i2.hasNext());
379  QVERIFY(!i2.hasPrevious());
380  QVERIFY(i.hasNext());
381  QVERIFY(!i.hasPrevious());
382  i2.toBack();
383  i.toFront();
384  QVERIFY(!i2.hasNext());
385  QVERIFY(i2.hasPrevious());
386  QVERIFY(i.hasNext());
387  QVERIFY(!i.hasPrevious());
388 
389  // i at front, i2 at end/back, 3 coordinates
390  QCOMPARE(i.peekNext(), h[0]);
391  QCOMPARE(i2.peekPrevious(), h[2]);
392  QCOMPARE(i2.previous(), h[2]);
393  QCOMPARE(i2.previous(), h[1]);
394  QCOMPARE(i2.previous(), h[0]);
395  QVERIFY(!i2.hasPrevious());
396  QCOMPARE(i.peekNext(), h[0]);
397  // i.peekNext()= 1.0; // compiler error, i is const
398  QCOMPARE(i.next(), h[0]);
399  QCOMPARE(i.peekNext(), h[1]);
400  QCOMPARE(i.next(), h[1]);
401  QCOMPARE(i.next(), h[2]);
402  QVERIFY(!i.hasNext());
403  i.toFront();
404  QCOMPARE(i.next(), h[0]);
405 }//t_qhullHyperplane_iterator
406 
409 {
410  RboxPoints rcube("c");
411  {
412  Qhull q(rcube, "");
414  ostringstream os;
415  os << "Hyperplane:\n";
416  os << h;
417  os << h.print("message");
418  os << h.print(" and a message ", " offset ");
419  cout << os.str();
420  QString s= QString::fromStdString(os.str());
421  QCOMPARE(s.count("1"), 3);
422  // QCOMPARE(s.count(QRegExp("f\\d")), 3*7 + 13*3*2);
423  }
424 }//t_io
425 
426 
427 }//orgQhull
428 
429 #include "moc/QhullHyperplane_test.moc"
QhullHyperplane hyperplane() const
Definition: QhullFacet.h:72
QhullRidge – Qhull&#39;s ridge structure, ridgeT, as a C++ class.
Definition: Coordinates.cpp:21
q
A QhullFacet is the C++ equivalent to Qhull&#39;s facetT*.
Definition: QhullFacet.h:37
QhullFacet firstFacet() const
Definition: Qhull.h:108
countT id() const
Definition: QhullFacet.h:73
PrintHyperplane print(const char *message) const
double distance(const QhullPoint &p) const
c
#define coordT
Definition: libqhull.h:80
QhullFacetList facetList() const
Definition: Qhull.cpp:186
const coordT * coordinates() const
const_iterator constBegin() const
const_iterator constEnd() const
void setDimension(int hyperplaneDimension)
void add_QhullHyperplane_test()
QhullFacetSet neighborFacets() const
Definition: QhullFacet.cpp:180
Interface to Qhull from C++.
Definition: Qhull.h:43
std::vector< coordT > toStdVector() const
double hyperplaneAngle(const QhullHyperplane &other) const
QhullQh * qh() const
Definition: Qhull.h:81
QhullPoint origin()
non-const due to QhullPoint
Definition: Qhull.h:80
void defineAs(int hyperplaneDimension, coordT *c, coordT hyperplaneOffset)
c2
void setCoordinates(coordT *c)
void cleanup()
Executed after each test.
Definition: RoadTest.cpp:38
const T at(countT idx) const
Definition: QhullSet.h:146
int dimension() const
Dimension of input and result.
Definition: Qhull.h:69
QhullHyperplane::iterator Iterator
const coordT * const_iterator
void setOffset(coordT hyperplaneOffset)
QhullHyperplane::const_iterator ConstIterator


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