| 1 | /*
|
|---|
| 2 | * PlaneUnittest.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Apr 30, 2010
|
|---|
| 5 | * Author: crueger
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include "PlaneUnittest.hpp"
|
|---|
| 9 |
|
|---|
| 10 | #include <cppunit/CompilerOutputter.h>
|
|---|
| 11 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
|---|
| 12 | #include <cppunit/ui/text/TestRunner.h>
|
|---|
| 13 |
|
|---|
| 14 | #ifdef HAVE_TESTRUNNER
|
|---|
| 15 | #include "UnitTestMain.hpp"
|
|---|
| 16 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 17 |
|
|---|
| 18 | #include "vector.hpp"
|
|---|
| 19 |
|
|---|
| 20 | CPPUNIT_TEST_SUITE_REGISTRATION( PlaneUnittest );
|
|---|
| 21 |
|
|---|
| 22 | void PlaneUnittest::setUp(){
|
|---|
| 23 | p1 = new Plane(e1,e2,e3);
|
|---|
| 24 | p2 = new Plane(e1,e2,zeroVec);
|
|---|
| 25 | p3 = new Plane(e1,zeroVec,e3);
|
|---|
| 26 | p4 = new Plane(zeroVec,e2,e3);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | void PlaneUnittest::tearDown(){
|
|---|
| 30 | delete p1;
|
|---|
| 31 | delete p2;
|
|---|
| 32 | delete p3;
|
|---|
| 33 | delete p4;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | void PlaneUnittest::constructionErrorTest(){
|
|---|
| 37 | // try several method of construction..
|
|---|
| 38 | // see if error checking works
|
|---|
| 39 |
|
|---|
| 40 | // three points
|
|---|
| 41 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,e2,e3));
|
|---|
| 42 | // when only two points are differnt this gives an error
|
|---|
| 43 | CPPUNIT_ASSERT_THROW(Plane(e1,e2,e2),LinearDependenceException);
|
|---|
| 44 | // same with only one point
|
|---|
| 45 | CPPUNIT_ASSERT_THROW(Plane(e1,e1,e1),LinearDependenceException);
|
|---|
| 46 |
|
|---|
| 47 | // use two vector giving two directions
|
|---|
| 48 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,e2,0));
|
|---|
| 49 | // and again this is actually only one vector
|
|---|
| 50 | CPPUNIT_ASSERT_THROW(Plane(e1,e1,0),LinearDependenceException);
|
|---|
| 51 | // Zero vector does not give a good direction
|
|---|
| 52 | CPPUNIT_ASSERT_THROW(Plane(e1,zeroVec,0),ZeroVectorException);
|
|---|
| 53 |
|
|---|
| 54 | // use a normalvector and an scalar offset
|
|---|
| 55 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,0));
|
|---|
| 56 | // The zero vector is no good as a normalvector
|
|---|
| 57 | CPPUNIT_ASSERT_THROW(Plane(zeroVec,0),ZeroVectorException);
|
|---|
| 58 |
|
|---|
| 59 | // use a normalvector and an offset vector
|
|---|
| 60 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,zeroVec));
|
|---|
| 61 | // and the bad zeroVector again
|
|---|
| 62 | CPPUNIT_ASSERT_THROW(Plane(zeroVec,zeroVec),ZeroVectorException);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | // we need to test normals independent of the direction
|
|---|
| 67 | bool testNormal(const Vector &normal1, const Vector &normal2){
|
|---|
| 68 | return (normal1==normal2) || (normal1==-1*normal2);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | void PlaneUnittest::constructionResultTest(){
|
|---|
| 72 | {
|
|---|
| 73 | // construct with three points on plane
|
|---|
| 74 | Plane p1(e1,e2,zeroVec);
|
|---|
| 75 | CPPUNIT_ASSERT(testNormal(e3,p1.getNormal()));
|
|---|
| 76 | CPPUNIT_ASSERT_EQUAL(0.,p1.getOffset());
|
|---|
| 77 |
|
|---|
| 78 | Plane p2(e1,e3,zeroVec);
|
|---|
| 79 | CPPUNIT_ASSERT(testNormal(e2,p2.getNormal()));
|
|---|
| 80 | CPPUNIT_ASSERT_EQUAL(0.,p2.getOffset());
|
|---|
| 81 |
|
|---|
| 82 | Plane p3(e2,e3,zeroVec);
|
|---|
| 83 | CPPUNIT_ASSERT(testNormal(e1,p3.getNormal()));
|
|---|
| 84 | CPPUNIT_ASSERT_EQUAL(0.,p3.getOffset());
|
|---|
| 85 | }
|
|---|
| 86 | {
|
|---|
| 87 | // construct with two directions + offset
|
|---|
| 88 | Plane p1(e1,e2,0);
|
|---|
| 89 | CPPUNIT_ASSERT(testNormal(e3,p1.getNormal()));
|
|---|
| 90 | CPPUNIT_ASSERT_EQUAL(0.,p1.getOffset());
|
|---|
| 91 |
|
|---|
| 92 | Plane p2(e1,e3,0);
|
|---|
| 93 | CPPUNIT_ASSERT(testNormal(e2,p2.getNormal()));
|
|---|
| 94 | CPPUNIT_ASSERT_EQUAL(0.,p2.getOffset());
|
|---|
| 95 |
|
|---|
| 96 | Plane p3(e2,e3,0);
|
|---|
| 97 | CPPUNIT_ASSERT(testNormal(e1,p3.getNormal()));
|
|---|
| 98 | CPPUNIT_ASSERT_EQUAL(0.,p3.getOffset());
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | void PlaneUnittest::pointsTest(){
|
|---|
| 103 | std::vector<Vector> points1 = p1->getPointsOnPlane();
|
|---|
| 104 | CPPUNIT_ASSERT(p1->isContained(points1[0]));
|
|---|
| 105 | CPPUNIT_ASSERT(p1->isContained(points1[1]));
|
|---|
| 106 | CPPUNIT_ASSERT(p1->isContained(points1[2]));
|
|---|
| 107 | // check that the three points differ
|
|---|
| 108 | CPPUNIT_ASSERT(points1[0]!=points1[1]);
|
|---|
| 109 | CPPUNIT_ASSERT(points1[0]!=points1[2]);
|
|---|
| 110 | CPPUNIT_ASSERT(points1[1]!=points1[2]);
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | std::vector<Vector> points2 = p2->getPointsOnPlane();
|
|---|
| 114 | CPPUNIT_ASSERT(p2->isContained(points2[0]));
|
|---|
| 115 | CPPUNIT_ASSERT(p2->isContained(points2[1]));
|
|---|
| 116 | CPPUNIT_ASSERT(p2->isContained(points2[2]));
|
|---|
| 117 | // check that the three points differ
|
|---|
| 118 | CPPUNIT_ASSERT(points2[0]!=points2[1]);
|
|---|
| 119 | CPPUNIT_ASSERT(points2[0]!=points2[2]);
|
|---|
| 120 | CPPUNIT_ASSERT(points2[1]!=points2[2]);
|
|---|
| 121 |
|
|---|
| 122 | std::vector<Vector> points3 = p3->getPointsOnPlane();
|
|---|
| 123 | CPPUNIT_ASSERT(p3->isContained(points3[0]));
|
|---|
| 124 | CPPUNIT_ASSERT(p3->isContained(points3[1]));
|
|---|
| 125 | CPPUNIT_ASSERT(p3->isContained(points3[2]));
|
|---|
| 126 | // check that the three points differ
|
|---|
| 127 | CPPUNIT_ASSERT(points3[0]!=points3[1]);
|
|---|
| 128 | CPPUNIT_ASSERT(points3[0]!=points3[2]);
|
|---|
| 129 | CPPUNIT_ASSERT(points3[1]!=points3[2]);
|
|---|
| 130 |
|
|---|
| 131 | std::vector<Vector> points4 = p4->getPointsOnPlane();
|
|---|
| 132 | CPPUNIT_ASSERT(p4->isContained(points4[0]));
|
|---|
| 133 | CPPUNIT_ASSERT(p4->isContained(points4[1]));
|
|---|
| 134 | CPPUNIT_ASSERT(p4->isContained(points4[2]));
|
|---|
| 135 | // check that the three points differ
|
|---|
| 136 | CPPUNIT_ASSERT(points4[0]!=points4[1]);
|
|---|
| 137 | CPPUNIT_ASSERT(points4[0]!=points4[2]);
|
|---|
| 138 | CPPUNIT_ASSERT(points4[1]!=points4[2]);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | void PlaneUnittest::operationsTest(){
|
|---|
| 143 | {
|
|---|
| 144 | Vector t = (1./3.)*(e1+e2+e3);
|
|---|
| 145 | CPPUNIT_ASSERT(fabs(p1->distance(zeroVec)-t.Norm()) < MYEPSILON);
|
|---|
| 146 | CPPUNIT_ASSERT_EQUAL(t,p1->getClosestPoint(zeroVec));
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | CPPUNIT_ASSERT(fabs(p2->distance(e3)-1) < MYEPSILON);
|
|---|
| 150 | CPPUNIT_ASSERT_EQUAL(zeroVec,p2->getClosestPoint(e3));
|
|---|
| 151 | CPPUNIT_ASSERT(fabs(p3->distance(e2)-1) < MYEPSILON);
|
|---|
| 152 | CPPUNIT_ASSERT_EQUAL(zeroVec,p3->getClosestPoint(e2));
|
|---|
| 153 | CPPUNIT_ASSERT(fabs(p4->distance(e1)-1) < MYEPSILON);
|
|---|
| 154 | CPPUNIT_ASSERT_EQUAL(zeroVec,p4->getClosestPoint(e1));
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 | }
|
|---|