| [fa5a6a] | 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" | 
|---|
| [27ac00] | 19 | #include "Line.hpp" | 
|---|
| [fa5a6a] | 20 |  | 
|---|
|  | 21 | CPPUNIT_TEST_SUITE_REGISTRATION( PlaneUnittest ); | 
|---|
|  | 22 |  | 
|---|
|  | 23 | void PlaneUnittest::setUp(){ | 
|---|
|  | 24 | p1 = new Plane(e1,e2,e3); | 
|---|
|  | 25 | p2 = new Plane(e1,e2,zeroVec); | 
|---|
|  | 26 | p3 = new Plane(e1,zeroVec,e3); | 
|---|
|  | 27 | p4 = new Plane(zeroVec,e2,e3); | 
|---|
|  | 28 | } | 
|---|
|  | 29 |  | 
|---|
|  | 30 | void PlaneUnittest::tearDown(){ | 
|---|
|  | 31 | delete p1; | 
|---|
|  | 32 | delete p2; | 
|---|
|  | 33 | delete p3; | 
|---|
|  | 34 | delete p4; | 
|---|
|  | 35 | } | 
|---|
|  | 36 |  | 
|---|
|  | 37 | void PlaneUnittest::constructionErrorTest(){ | 
|---|
|  | 38 | // try several method of construction.. | 
|---|
|  | 39 | // see if error checking works | 
|---|
|  | 40 |  | 
|---|
|  | 41 | // three points | 
|---|
|  | 42 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,e2,e3)); | 
|---|
|  | 43 | // when only two points are differnt this gives an error | 
|---|
|  | 44 | CPPUNIT_ASSERT_THROW(Plane(e1,e2,e2),LinearDependenceException); | 
|---|
|  | 45 | // same with only one point | 
|---|
|  | 46 | CPPUNIT_ASSERT_THROW(Plane(e1,e1,e1),LinearDependenceException); | 
|---|
|  | 47 |  | 
|---|
|  | 48 | // use two vector giving two directions | 
|---|
|  | 49 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,e2,0)); | 
|---|
|  | 50 | // and again this is actually only one vector | 
|---|
|  | 51 | CPPUNIT_ASSERT_THROW(Plane(e1,e1,0),LinearDependenceException); | 
|---|
|  | 52 | // Zero vector does not give a good direction | 
|---|
|  | 53 | CPPUNIT_ASSERT_THROW(Plane(e1,zeroVec,0),ZeroVectorException); | 
|---|
|  | 54 |  | 
|---|
|  | 55 | // use a normalvector and an scalar offset | 
|---|
|  | 56 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,0)); | 
|---|
|  | 57 | // The zero vector is no good as a normalvector | 
|---|
|  | 58 | CPPUNIT_ASSERT_THROW(Plane(zeroVec,0),ZeroVectorException); | 
|---|
|  | 59 |  | 
|---|
|  | 60 | // use a normalvector and an offset vector | 
|---|
|  | 61 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,zeroVec)); | 
|---|
|  | 62 | // and the bad zeroVector again | 
|---|
|  | 63 | CPPUNIT_ASSERT_THROW(Plane(zeroVec,zeroVec),ZeroVectorException); | 
|---|
|  | 64 | } | 
|---|
|  | 65 |  | 
|---|
|  | 66 |  | 
|---|
|  | 67 | // we need to test normals independent of the direction | 
|---|
|  | 68 | bool testNormal(const Vector &normal1, const Vector &normal2){ | 
|---|
|  | 69 | return (normal1==normal2) || (normal1==-1*normal2); | 
|---|
|  | 70 | } | 
|---|
|  | 71 |  | 
|---|
|  | 72 | void PlaneUnittest::constructionResultTest(){ | 
|---|
|  | 73 | { | 
|---|
|  | 74 | // construct with three points on plane | 
|---|
|  | 75 | Plane p1(e1,e2,zeroVec); | 
|---|
|  | 76 | CPPUNIT_ASSERT(testNormal(e3,p1.getNormal())); | 
|---|
|  | 77 | CPPUNIT_ASSERT_EQUAL(0.,p1.getOffset()); | 
|---|
|  | 78 |  | 
|---|
|  | 79 | Plane p2(e1,e3,zeroVec); | 
|---|
|  | 80 | CPPUNIT_ASSERT(testNormal(e2,p2.getNormal())); | 
|---|
|  | 81 | CPPUNIT_ASSERT_EQUAL(0.,p2.getOffset()); | 
|---|
|  | 82 |  | 
|---|
|  | 83 | Plane p3(e2,e3,zeroVec); | 
|---|
|  | 84 | CPPUNIT_ASSERT(testNormal(e1,p3.getNormal())); | 
|---|
|  | 85 | CPPUNIT_ASSERT_EQUAL(0.,p3.getOffset()); | 
|---|
|  | 86 | } | 
|---|
|  | 87 | { | 
|---|
|  | 88 | // construct with two directions + offset | 
|---|
|  | 89 | Plane p1(e1,e2,0); | 
|---|
|  | 90 | CPPUNIT_ASSERT(testNormal(e3,p1.getNormal())); | 
|---|
|  | 91 | CPPUNIT_ASSERT_EQUAL(0.,p1.getOffset()); | 
|---|
|  | 92 |  | 
|---|
|  | 93 | Plane p2(e1,e3,0); | 
|---|
|  | 94 | CPPUNIT_ASSERT(testNormal(e2,p2.getNormal())); | 
|---|
|  | 95 | CPPUNIT_ASSERT_EQUAL(0.,p2.getOffset()); | 
|---|
|  | 96 |  | 
|---|
|  | 97 | Plane p3(e2,e3,0); | 
|---|
|  | 98 | CPPUNIT_ASSERT(testNormal(e1,p3.getNormal())); | 
|---|
|  | 99 | CPPUNIT_ASSERT_EQUAL(0.,p3.getOffset()); | 
|---|
|  | 100 | } | 
|---|
|  | 101 | } | 
|---|
|  | 102 |  | 
|---|
|  | 103 | void PlaneUnittest::pointsTest(){ | 
|---|
|  | 104 | std::vector<Vector> points1 = p1->getPointsOnPlane(); | 
|---|
|  | 105 | CPPUNIT_ASSERT(p1->isContained(points1[0])); | 
|---|
|  | 106 | CPPUNIT_ASSERT(p1->isContained(points1[1])); | 
|---|
|  | 107 | CPPUNIT_ASSERT(p1->isContained(points1[2])); | 
|---|
|  | 108 | // check that the three points differ | 
|---|
|  | 109 | CPPUNIT_ASSERT(points1[0]!=points1[1]); | 
|---|
|  | 110 | CPPUNIT_ASSERT(points1[0]!=points1[2]); | 
|---|
|  | 111 | CPPUNIT_ASSERT(points1[1]!=points1[2]); | 
|---|
|  | 112 |  | 
|---|
|  | 113 |  | 
|---|
|  | 114 | std::vector<Vector> points2 = p2->getPointsOnPlane(); | 
|---|
|  | 115 | CPPUNIT_ASSERT(p2->isContained(points2[0])); | 
|---|
|  | 116 | CPPUNIT_ASSERT(p2->isContained(points2[1])); | 
|---|
|  | 117 | CPPUNIT_ASSERT(p2->isContained(points2[2])); | 
|---|
|  | 118 | // check that the three points differ | 
|---|
|  | 119 | CPPUNIT_ASSERT(points2[0]!=points2[1]); | 
|---|
|  | 120 | CPPUNIT_ASSERT(points2[0]!=points2[2]); | 
|---|
|  | 121 | CPPUNIT_ASSERT(points2[1]!=points2[2]); | 
|---|
|  | 122 |  | 
|---|
|  | 123 | std::vector<Vector> points3 = p3->getPointsOnPlane(); | 
|---|
|  | 124 | CPPUNIT_ASSERT(p3->isContained(points3[0])); | 
|---|
|  | 125 | CPPUNIT_ASSERT(p3->isContained(points3[1])); | 
|---|
|  | 126 | CPPUNIT_ASSERT(p3->isContained(points3[2])); | 
|---|
|  | 127 | // check that the three points differ | 
|---|
|  | 128 | CPPUNIT_ASSERT(points3[0]!=points3[1]); | 
|---|
|  | 129 | CPPUNIT_ASSERT(points3[0]!=points3[2]); | 
|---|
|  | 130 | CPPUNIT_ASSERT(points3[1]!=points3[2]); | 
|---|
|  | 131 |  | 
|---|
|  | 132 | std::vector<Vector> points4 = p4->getPointsOnPlane(); | 
|---|
|  | 133 | CPPUNIT_ASSERT(p4->isContained(points4[0])); | 
|---|
|  | 134 | CPPUNIT_ASSERT(p4->isContained(points4[1])); | 
|---|
|  | 135 | CPPUNIT_ASSERT(p4->isContained(points4[2])); | 
|---|
|  | 136 | // check that the three points differ | 
|---|
|  | 137 | CPPUNIT_ASSERT(points4[0]!=points4[1]); | 
|---|
|  | 138 | CPPUNIT_ASSERT(points4[0]!=points4[2]); | 
|---|
|  | 139 | CPPUNIT_ASSERT(points4[1]!=points4[2]); | 
|---|
|  | 140 | } | 
|---|
|  | 141 |  | 
|---|
|  | 142 |  | 
|---|
|  | 143 | void PlaneUnittest::operationsTest(){ | 
|---|
|  | 144 | { | 
|---|
|  | 145 | Vector t = (1./3.)*(e1+e2+e3); | 
|---|
|  | 146 | CPPUNIT_ASSERT(fabs(p1->distance(zeroVec)-t.Norm()) < MYEPSILON); | 
|---|
|  | 147 | CPPUNIT_ASSERT_EQUAL(t,p1->getClosestPoint(zeroVec)); | 
|---|
|  | 148 | } | 
|---|
|  | 149 |  | 
|---|
|  | 150 | CPPUNIT_ASSERT(fabs(p2->distance(e3)-1) < MYEPSILON); | 
|---|
|  | 151 | CPPUNIT_ASSERT_EQUAL(zeroVec,p2->getClosestPoint(e3)); | 
|---|
|  | 152 | CPPUNIT_ASSERT(fabs(p3->distance(e2)-1) < MYEPSILON); | 
|---|
|  | 153 | CPPUNIT_ASSERT_EQUAL(zeroVec,p3->getClosestPoint(e2)); | 
|---|
|  | 154 | CPPUNIT_ASSERT(fabs(p4->distance(e1)-1) < MYEPSILON); | 
|---|
|  | 155 | CPPUNIT_ASSERT_EQUAL(zeroVec,p4->getClosestPoint(e1)); | 
|---|
| [ccf826] | 156 | } | 
|---|
| [fa5a6a] | 157 |  | 
|---|
| [ccf826] | 158 | void PlaneUnittest::mirrorTest(){ | 
|---|
|  | 159 | Vector fixture; | 
|---|
|  | 160 |  | 
|---|
|  | 161 | // some Vectors that lie on the planes | 
|---|
|  | 162 | fixture = p1->mirrorVector(e1); | 
|---|
|  | 163 | CPPUNIT_ASSERT_EQUAL(fixture,e1); | 
|---|
|  | 164 | fixture = p1->mirrorVector(e2); | 
|---|
|  | 165 | CPPUNIT_ASSERT_EQUAL(fixture,e2); | 
|---|
|  | 166 | fixture = p1->mirrorVector(e3); | 
|---|
|  | 167 | CPPUNIT_ASSERT_EQUAL(fixture,e3); | 
|---|
|  | 168 |  | 
|---|
|  | 169 | fixture = p2->mirrorVector(zeroVec); | 
|---|
|  | 170 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec); | 
|---|
|  | 171 | fixture = p2->mirrorVector(e1); | 
|---|
|  | 172 | CPPUNIT_ASSERT_EQUAL(fixture,e1); | 
|---|
|  | 173 | fixture = p2->mirrorVector(e2); | 
|---|
|  | 174 | CPPUNIT_ASSERT_EQUAL(fixture,e2); | 
|---|
|  | 175 |  | 
|---|
|  | 176 | fixture = p3->mirrorVector(zeroVec); | 
|---|
|  | 177 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec); | 
|---|
|  | 178 | fixture = p3->mirrorVector(e1); | 
|---|
|  | 179 | CPPUNIT_ASSERT_EQUAL(fixture,e1); | 
|---|
|  | 180 | fixture = p3->mirrorVector(e3); | 
|---|
|  | 181 | CPPUNIT_ASSERT_EQUAL(fixture,e3); | 
|---|
|  | 182 |  | 
|---|
|  | 183 | fixture = p4->mirrorVector(zeroVec); | 
|---|
|  | 184 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec); | 
|---|
|  | 185 | fixture = p4->mirrorVector(e2); | 
|---|
|  | 186 | CPPUNIT_ASSERT_EQUAL(fixture,e2); | 
|---|
|  | 187 | fixture = p4->mirrorVector(e3); | 
|---|
|  | 188 | CPPUNIT_ASSERT_EQUAL(fixture,e3); | 
|---|
|  | 189 |  | 
|---|
|  | 190 | // some Vectors outside of the planes | 
|---|
|  | 191 | { | 
|---|
|  | 192 | Vector t = (2./3.)*(e1+e2+e3); | 
|---|
|  | 193 | fixture = p1->mirrorVector(zeroVec); | 
|---|
|  | 194 | CPPUNIT_ASSERT_EQUAL(fixture,t); | 
|---|
|  | 195 | } | 
|---|
| [fa5a6a] | 196 |  | 
|---|
| [ccf826] | 197 | fixture = p2->mirrorVector(e3); | 
|---|
|  | 198 | CPPUNIT_ASSERT_EQUAL(fixture,-1*e3); | 
|---|
|  | 199 | fixture = p3->mirrorVector(e2); | 
|---|
|  | 200 | CPPUNIT_ASSERT_EQUAL(fixture,-1*e2); | 
|---|
|  | 201 | fixture = p4->mirrorVector(e1); | 
|---|
|  | 202 | CPPUNIT_ASSERT_EQUAL(fixture,-1*e1); | 
|---|
| [fa5a6a] | 203 | } | 
|---|
| [27ac00] | 204 |  | 
|---|
|  | 205 | void PlaneUnittest::LineIntersectionTest(){ | 
|---|
|  | 206 | Vector fixture; | 
|---|
|  | 207 | // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ??? | 
|---|
|  | 208 | Line l1 = makeLineThrough(zeroVec,Vector(2,1,0)); | 
|---|
|  | 209 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(e1, zeroVec).GetIntersection(l1) ); | 
|---|
|  | 210 | CPPUNIT_ASSERT_EQUAL( zeroVec, fixture ); | 
|---|
|  | 211 |  | 
|---|
|  | 212 | // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ??? | 
|---|
|  | 213 | Line l2 = makeLineThrough(e1,Vector(0,1,1)); | 
|---|
|  | 214 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(e2, Vector(2,1,0)).GetIntersection(l2) ); | 
|---|
|  | 215 | CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture ); | 
|---|
|  | 216 | } | 
|---|