QhullHyperplane_test.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (c) 2009-2011 C.B. Barber. All rights reserved.
00004 ** $Id: //main/2011/qhull/src/qhulltest/QhullHyperplane_test.cpp#4 $$Change: 1382 $
00005 ** $DateTime: 2011/05/14 10:45:42 $$Author: bbarber $
00006 **
00007 ****************************************************************************/
00008 
00009 //pre-compiled headers
00010 #include <iostream>
00011 #include <vector>
00012 #include "../road/RoadTest.h"
00013 
00014 #include "QhullHyperplane.h"
00015 #include "QhullError.h"
00016 #include "RboxPoints.h"
00017 #include "QhullFacet.h"
00018 #include "QhullFacetList.h"
00019 #include "Qhull.h"
00020 #include <numeric>
00021 
00022 using std::cout;
00023 using std::endl;
00024 using std::ostringstream;
00025 using std::ostream;
00026 using std::string;
00027 
00028 namespace orgQhull {
00029 
00030 class QhullHyperplane_test : public RoadTest
00031 {
00032     Q_OBJECT
00033 
00034 #//Test slots
00035 private slots:
00036     void cleanup();
00037     void t_construct();
00038     void t_convert();
00039     void t_readonly();
00040     void t_define();
00041     void t_value();
00042     void t_operator();
00043     void t_iterator();
00044     void t_const_iterator();
00045     void t_qhullHyperplane_iterator();
00046     void t_io();
00047 };//QhullHyperplane_test
00048 
00049 void
00050 add_QhullHyperplane_test()
00051 {
00052     new QhullHyperplane_test();
00053 }
00054 
00055 //Executed after each testcase
00056 void QhullHyperplane_test::
00057 cleanup()
00058 {
00059     UsingLibQhull::checkQhullMemoryEmpty();
00060     RoadTest::cleanup();
00061 }
00062 
00063 void QhullHyperplane_test::
00064 t_construct()
00065 {
00066     // Qhull.runQhull() constructs QhullFacets as facetT
00067     QhullHyperplane h;
00068     QVERIFY(!h.isDefined());
00069     QCOMPARE(h.dimension(),0);
00070     QCOMPARE(h.coordinates(),static_cast<double *>(0));
00071     RboxPoints rcube("c");
00072     Qhull q(rcube,"Qt QR0");  // triangulation of rotated unit cube
00073     QhullFacet f= q.firstFacet();
00074     QhullHyperplane h2(f.hyperplane());
00075     QVERIFY(h2.isDefined());
00076     QCOMPARE(h2.dimension(),3);
00077     // h= h2;  // copy assignment disabled, ambiguous
00078     QhullHyperplane h3(h2.dimension(), h2.coordinates(), h2.offset());
00079     QCOMPARE(h2, h3);
00080     QhullHyperplane h5= h2; // copy constructor
00081     QVERIFY(h5==h2);
00082 }//t_construct
00083 
00084 void QhullHyperplane_test::
00085 t_convert()
00086 {
00087     RboxPoints rcube("c");
00088     Qhull q(rcube,"Qt QR0");  // triangulation of rotated unit cube
00089     QhullHyperplane h= q.firstFacet().hyperplane();
00090     std::vector<double> fs= h.toStdVector();
00091     QCOMPARE(fs.size(), 4u);
00092     double offset= fs.back();
00093     fs.pop_back();
00094     QCOMPARE(offset, -0.5);
00095 
00096     double squareNorm= inner_product(fs.begin(), fs.end(), fs.begin(), 0.0);
00097     QCOMPARE(squareNorm, 1.0);
00098     QList<double> qs= h.toQList();
00099     QCOMPARE(qs.size(), 4);
00100     double offset2= qs.takeLast();
00101     QCOMPARE(offset2, -0.5);
00102     double squareNorm2= std::inner_product(qs.begin(), qs.end(), qs.begin(), 0.0);
00103     QCOMPARE(squareNorm2, 1.0);
00104 }//t_convert
00105 
00106 void QhullHyperplane_test::
00107 t_readonly()
00108 {
00109     RboxPoints rcube("c");
00110     {
00111         Qhull q(rcube,"Qt QR0");  // triangulation of rotated unit cube
00112         QhullFacetList fs= q.facetList();
00113         QhullFacetListIterator i(fs);
00114         while(i.hasNext()){
00115             QhullFacet f= i.next();
00116             QhullHyperplane h= f.hyperplane();
00117             int id= f.id();
00118             cout << "h" << id << endl;
00119             QVERIFY(h.isDefined());
00120             QCOMPARE(h.dimension(),3);
00121             const coordT *c= h.coordinates();
00122             coordT *c2= h.coordinates();
00123             QCOMPARE(c, c2);
00124             const coordT *c3= h.begin();
00125             QCOMPARE(c, c3);
00126             QCOMPARE(h.offset(), -0.5);
00127             int j= h.end()-h.begin();
00128             QCOMPARE(j, 3);
00129             double squareNorm= std::inner_product(h.begin(), h.end(), h.begin(), 0.0);
00130             QCOMPARE(squareNorm, 1.0);
00131         }
00132         QhullHyperplane h2= fs.first().hyperplane();
00133         QhullHyperplane h3= fs.last().hyperplane();
00134         QVERIFY(h2!=h3);
00135         QVERIFY(h3.coordinates()!=h2.coordinates());
00136     }
00137 }//t_readonly
00138 
00139 void QhullHyperplane_test::
00140 t_define()
00141 {
00142     RboxPoints rcube("c");
00143     {
00144         Qhull q(rcube,"Qt QR0");  // triangulation of rotated unit cube
00145         QhullFacetList fs= q.facetList();
00146         QhullHyperplane h= fs.first().hyperplane();
00147         QhullHyperplane h2= h;
00148         QVERIFY(h==h2);
00149         QhullHyperplane h3= fs.last().hyperplane();
00150         QVERIFY(h2!=h3);
00151 
00152         QhullHyperplane h4= h3;
00153         h4.defineAs(h2);
00154         QVERIFY(h2==h4);
00155         QhullHyperplane p5= h3;
00156         p5.defineAs(h2.dimension(), h2.coordinates(), h2.offset());
00157         QVERIFY(h2==p5);
00158         QhullHyperplane h6= h3;
00159         h6.setCoordinates(h2.coordinates());
00160         QCOMPARE(h2.coordinates(), h6.coordinates());
00161         h6.setOffset(h2.offset());
00162         QCOMPARE(h2.offset(), h6.offset());
00163         QVERIFY(h2==h6);
00164         h6.setDimension(2);
00165         QCOMPARE(h6.dimension(), 2);
00166         QVERIFY(h2!=h6);
00167     }
00168 }//t_define
00169 
00170 void QhullHyperplane_test::
00171 t_value()
00172 {
00173     RboxPoints rcube("c G1");
00174     Qhull q(rcube,"Qt QR0");  // triangulation of rotated unit cube
00175     const QhullHyperplane h= q.firstFacet().hyperplane();
00176     double dist= h.distance(q.origin());
00177     QCOMPARE(dist, -1.0);
00178     double norm= h.norm();
00179     QCOMPARE(norm, 1.0);
00180 }//t_value
00181 
00182 void QhullHyperplane_test::
00183 t_operator()
00184 {
00185     RboxPoints rcube("c");
00186     Qhull q(rcube,"Qt QR0");  // triangulation of rotated unit cube
00187     const QhullHyperplane h= q.firstFacet().hyperplane();
00188     //operator== and operator!= tested elsewhere
00189     const coordT *c= h.coordinates();
00190     for(int k=h.dimension(); k--; ){
00191         QCOMPARE(c[k], h[k]);
00192     }
00193     //h[0]= 10.0; // compiler error, const
00194     QhullHyperplane h2= q.firstFacet().hyperplane();
00195     h2[0]= 10.0;  // Overwrites Hyperplane coordinate!
00196     QCOMPARE(h2[0], 10.0);
00197 }//t_operator
00198 
00199 void QhullHyperplane_test::
00200 t_iterator()
00201 {
00202     RboxPoints rcube("c");
00203     {
00204         QhullHyperplane h2;
00205         QCOMPARE(h2.begin(), h2.end());
00206         QCOMPARE(h2.count(), 0);
00207         QCOMPARE(h2.size(), 0u);
00208         Qhull q(rcube,"QR0");  // rotated unit cube
00209         QhullHyperplane h= q.firstFacet().hyperplane();
00210         QCOMPARE(h.count(), 3);
00211         QCOMPARE(h.size(), 3u);
00212         QhullHyperplane::Iterator i= h.begin();
00213         QhullHyperplane::iterator i2= h.begin();
00214         QVERIFY(i==i2);
00215         QVERIFY(i>=i2);
00216         QVERIFY(i<=i2);
00217         i= h.begin();
00218         QVERIFY(i==i2);
00219         i2= h.end();
00220         QVERIFY(i!=i2);
00221         double d3= *i;
00222         i2--;
00223         double d2= *i2;
00224         QCOMPARE(d3, h[0]);
00225         QCOMPARE(d2, h[2]);
00226         QhullHyperplane::Iterator i3(i2);
00227         QCOMPARE(*i2, *i3);
00228 
00229         (i3= i)++;
00230         QCOMPARE((*i3), h[1]);
00231         QVERIFY(i==i);
00232         QVERIFY(i!=i2);
00233         QVERIFY(i<i2);
00234         QVERIFY(i<=i2);
00235         QVERIFY(i2>i);
00236         QVERIFY(i2>=i);
00237 
00238         QhullHyperplane::ConstIterator i4= h.begin();
00239         QVERIFY(i==i4); // iterator COMP const_iterator
00240         QVERIFY(i<=i4);
00241         QVERIFY(i>=i4);
00242         QVERIFY(i4==i); // const_iterator COMP iterator
00243         QVERIFY(i4<=i);
00244         QVERIFY(i4>=i);
00245         QVERIFY(i>=i4);
00246         QVERIFY(i4<=i);
00247         QVERIFY(i2!=i4);
00248         QVERIFY(i2>i4);
00249         QVERIFY(i2>=i4);
00250         QVERIFY(i4!=i2);
00251         QVERIFY(i4<i2);
00252         QVERIFY(i4<=i2);
00253         ++i4;
00254         QVERIFY(i<i4);
00255         QVERIFY(i<=i4);
00256         QVERIFY(i4>i);
00257         QVERIFY(i4>=i);
00258 
00259         i= h.begin();
00260         i2= h.begin();
00261         QCOMPARE(i, i2++);
00262         QCOMPARE(*i2, h[1]);
00263         QCOMPARE(++i, i2);
00264         QCOMPARE(i, i2--);
00265         QCOMPARE(i2, h.begin());
00266         QCOMPARE(--i, i2);
00267         QCOMPARE(i2 += 3, h.end());
00268         QCOMPARE(i2 -= 3, h.begin());
00269         QCOMPARE(i2+0, h.begin());
00270         QCOMPARE(i2+3, h.end());
00271         i2 += 3;
00272         i= i2-0;
00273         QCOMPARE(i, i2);
00274         i= i2-3;
00275         QCOMPARE(i, h.begin());
00276         QCOMPARE(i2-i, 3);
00277 
00278         //h.begin end tested above
00279 
00280         // QhullHyperplane is const-only
00281     }
00282 }//t_iterator
00283 
00284 void QhullHyperplane_test::
00285 t_const_iterator()
00286 {
00287     RboxPoints rcube("c");
00288     {
00289         Qhull q(rcube,"QR0");  // rotated unit cube
00290         QhullHyperplane h= q.firstFacet().hyperplane();
00291         QhullHyperplane::ConstIterator i= h.begin();
00292         QhullHyperplane::const_iterator i2= h.begin();
00293         QVERIFY(i==i2);
00294         QVERIFY(i>=i2);
00295         QVERIFY(i<=i2);
00296         i= h.begin();
00297         QVERIFY(i==i2);
00298         i2= h.end();
00299         QVERIFY(i!=i2);
00300         double d3= *i;
00301         i2--;
00302         double d2= *i2;
00303         QCOMPARE(d3, h[0]);
00304         QCOMPARE(d2, h[2]);
00305         QhullHyperplane::ConstIterator i3(i2);
00306         QCOMPARE(*i2, *i3);
00307 
00308         (i3= i)++;
00309         QCOMPARE((*i3), h[1]);
00310         QVERIFY(i==i);
00311         QVERIFY(i!=i2);
00312         QVERIFY(i<i2);
00313         QVERIFY(i<=i2);
00314         QVERIFY(i2>i);
00315         QVERIFY(i2>=i);
00316 
00317         // See t_iterator for const_iterator COMP iterator
00318 
00319         i= h.begin();
00320         i2= h.constBegin();
00321         QCOMPARE(i, i2++);
00322         QCOMPARE(*i2, h[1]);
00323         QCOMPARE(++i, i2);
00324         QCOMPARE(i, i2--);
00325         QCOMPARE(i2, h.constBegin());
00326         QCOMPARE(--i, i2);
00327         QCOMPARE(i2+=3, h.constEnd());
00328         QCOMPARE(i2-=3, h.constBegin());
00329         QCOMPARE(i2+0, h.constBegin());
00330         QCOMPARE(i2+3, h.constEnd());
00331         i2 += 3;
00332         i= i2-0;
00333         QCOMPARE(i, i2);
00334         i= i2-3;
00335         QCOMPARE(i, h.constBegin());
00336         QCOMPARE(i2-i, 3);
00337 
00338         // QhullHyperplane is const-only
00339     }
00340 }//t_const_iterator
00341 
00342 void QhullHyperplane_test::
00343 t_qhullHyperplane_iterator()
00344 {
00345     QhullHyperplane h2;
00346     QhullHyperplaneIterator i= h2;
00347     QCOMPARE(h2.dimension(), 0);
00348     QVERIFY(!i.hasNext());
00349     QVERIFY(!i.hasPrevious());
00350     i.toBack();
00351     QVERIFY(!i.hasNext());
00352     QVERIFY(!i.hasPrevious());
00353 
00354     RboxPoints rcube("c");
00355     Qhull q(rcube,"QR0");  // rotated unit cube
00356     QhullHyperplane h = q.firstFacet().hyperplane();
00357     QhullHyperplaneIterator i2(h);
00358     QCOMPARE(h.dimension(), 3);
00359     i= h;
00360     QVERIFY(i2.hasNext());
00361     QVERIFY(!i2.hasPrevious());
00362     QVERIFY(i.hasNext());
00363     QVERIFY(!i.hasPrevious());
00364     i2.toBack();
00365     i.toFront();
00366     QVERIFY(!i2.hasNext());
00367     QVERIFY(i2.hasPrevious());
00368     QVERIFY(i.hasNext());
00369     QVERIFY(!i.hasPrevious());
00370 
00371     // i at front, i2 at end/back, 3 coordinates
00372     QCOMPARE(i.peekNext(), h[0]);
00373     QCOMPARE(i2.peekPrevious(), h[2]);
00374     QCOMPARE(i2.previous(), h[2]);
00375     QCOMPARE(i2.previous(), h[1]);
00376     QCOMPARE(i2.previous(), h[0]);
00377     QVERIFY(!i2.hasPrevious());
00378     QCOMPARE(i.peekNext(), h[0]);
00379     // i.peekNext()= 1.0; // compiler error, i is const
00380     QCOMPARE(i.next(), h[0]);
00381     QCOMPARE(i.peekNext(), h[1]);
00382     QCOMPARE(i.next(), h[1]);
00383     QCOMPARE(i.next(), h[2]);
00384     QVERIFY(!i.hasNext());
00385     i.toFront();
00386     QCOMPARE(i.next(), h[0]);
00387 }//t_qhullHyperplane_iterator
00388 
00389 void QhullHyperplane_test::
00390 t_io()
00391 {
00392     RboxPoints rcube("c");
00393     {
00394         Qhull q(rcube, "");
00395         QhullHyperplane h= q.firstFacet().hyperplane();
00396         ostringstream os;
00397         os << "Hyperplane:\n";
00398         os << h;
00399         os << "Hyperplane w/ runId:\n";
00400         os << h.print();
00401         os << h.print(" and a message ", " offset ");
00402         cout << os.str();
00403         QString s= QString::fromStdString(os.str());
00404         QCOMPARE(s.count("1"), 3);
00405         // QCOMPARE(s.count(QRegExp("f\\d")), 3*7 + 13*3*2);
00406     }
00407 }//t_io
00408 
00409 
00410 }//orgQhull
00411 
00412 #include "moc/QhullHyperplane_test.moc"
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


libqhull
Author(s): Robert Krug
autogenerated on Tue Jun 18 2013 12:38:50