| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [fa5a6a] | 8 | /* | 
|---|
|  | 9 | * PlaneUnittest.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Apr 30, 2010 | 
|---|
|  | 12 | *      Author: crueger | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [fa5a6a] | 20 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 21 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 22 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 23 |  | 
|---|
| [3dcb1f] | 24 | #include <cmath> | 
|---|
| [9b410d] | 25 | #include <limits> | 
|---|
| [3dcb1f] | 26 |  | 
|---|
| [9b410d] | 27 | #include "LinearAlgebra/defs.hpp" | 
|---|
| [5bc8229] | 28 | #include "LinearAlgebra/Line.hpp" | 
|---|
| [9b410d] | 29 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [5bc8229] | 30 |  | 
|---|
|  | 31 | #include "PlaneUnitTest.hpp" | 
|---|
|  | 32 |  | 
|---|
| [fa5a6a] | 33 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 34 | #include "UnitTestMain.hpp" | 
|---|
|  | 35 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 36 |  | 
|---|
|  | 37 | CPPUNIT_TEST_SUITE_REGISTRATION( PlaneUnittest ); | 
|---|
|  | 38 |  | 
|---|
|  | 39 | void PlaneUnittest::setUp(){ | 
|---|
| [407782] | 40 | p1 = new Plane(unitVec[0],unitVec[1],unitVec[2]); | 
|---|
|  | 41 | p2 = new Plane(unitVec[0],unitVec[1],zeroVec); | 
|---|
|  | 42 | p3 = new Plane(unitVec[0],zeroVec,unitVec[2]); | 
|---|
|  | 43 | p4 = new Plane(zeroVec,unitVec[1],unitVec[2]); | 
|---|
| [fa5a6a] | 44 | } | 
|---|
|  | 45 |  | 
|---|
|  | 46 | void PlaneUnittest::tearDown(){ | 
|---|
|  | 47 | delete p1; | 
|---|
|  | 48 | delete p2; | 
|---|
|  | 49 | delete p3; | 
|---|
|  | 50 | delete p4; | 
|---|
|  | 51 | } | 
|---|
|  | 52 |  | 
|---|
|  | 53 | void PlaneUnittest::constructionErrorTest(){ | 
|---|
|  | 54 | // try several method of construction.. | 
|---|
|  | 55 | // see if error checking works | 
|---|
|  | 56 |  | 
|---|
|  | 57 | // three points | 
|---|
| [407782] | 58 | CPPUNIT_ASSERT_NO_THROW(Plane(unitVec[0],unitVec[1],unitVec[2])); | 
|---|
| [fa5a6a] | 59 | // when only two points are differnt this gives an error | 
|---|
| [407782] | 60 | CPPUNIT_ASSERT_THROW(Plane(unitVec[0],unitVec[1],unitVec[1]),LinearDependenceException); | 
|---|
| [fa5a6a] | 61 | // same with only one point | 
|---|
| [407782] | 62 | CPPUNIT_ASSERT_THROW(Plane(unitVec[0],unitVec[0],unitVec[0]),LinearDependenceException); | 
|---|
| [fa5a6a] | 63 |  | 
|---|
|  | 64 | // use two vector giving two directions | 
|---|
| [407782] | 65 | CPPUNIT_ASSERT_NO_THROW(Plane(unitVec[0],unitVec[1],0)); | 
|---|
| [fa5a6a] | 66 | // and again this is actually only one vector | 
|---|
| [407782] | 67 | CPPUNIT_ASSERT_THROW(Plane(unitVec[0],unitVec[0],0),LinearDependenceException); | 
|---|
| [fa5a6a] | 68 | // Zero vector does not give a good direction | 
|---|
| [407782] | 69 | CPPUNIT_ASSERT_THROW(Plane(unitVec[0],zeroVec,0),ZeroVectorException); | 
|---|
| [fa5a6a] | 70 |  | 
|---|
|  | 71 | // use a normalvector and an scalar offset | 
|---|
| [407782] | 72 | CPPUNIT_ASSERT_NO_THROW(Plane(unitVec[0],0)); | 
|---|
| [fa5a6a] | 73 | // The zero vector is no good as a normalvector | 
|---|
|  | 74 | CPPUNIT_ASSERT_THROW(Plane(zeroVec,0),ZeroVectorException); | 
|---|
|  | 75 |  | 
|---|
|  | 76 | // use a normalvector and an offset vector | 
|---|
| [407782] | 77 | CPPUNIT_ASSERT_NO_THROW(Plane(unitVec[0],zeroVec)); | 
|---|
| [fa5a6a] | 78 | // and the bad zeroVector again | 
|---|
|  | 79 | CPPUNIT_ASSERT_THROW(Plane(zeroVec,zeroVec),ZeroVectorException); | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
|  | 82 |  | 
|---|
|  | 83 | // we need to test normals independent of the direction | 
|---|
|  | 84 | bool testNormal(const Vector &normal1, const Vector &normal2){ | 
|---|
|  | 85 | return (normal1==normal2) || (normal1==-1*normal2); | 
|---|
|  | 86 | } | 
|---|
|  | 87 |  | 
|---|
|  | 88 | void PlaneUnittest::constructionResultTest(){ | 
|---|
|  | 89 | { | 
|---|
|  | 90 | // construct with three points on plane | 
|---|
| [407782] | 91 | Plane p1(unitVec[0],unitVec[1],zeroVec); | 
|---|
|  | 92 | CPPUNIT_ASSERT(testNormal(unitVec[2],p1.getNormal())); | 
|---|
| [fa5a6a] | 93 | CPPUNIT_ASSERT_EQUAL(0.,p1.getOffset()); | 
|---|
|  | 94 |  | 
|---|
| [407782] | 95 | Plane p2(unitVec[0],unitVec[2],zeroVec); | 
|---|
|  | 96 | CPPUNIT_ASSERT(testNormal(unitVec[1],p2.getNormal())); | 
|---|
| [fa5a6a] | 97 | CPPUNIT_ASSERT_EQUAL(0.,p2.getOffset()); | 
|---|
|  | 98 |  | 
|---|
| [407782] | 99 | Plane p3(unitVec[1],unitVec[2],zeroVec); | 
|---|
|  | 100 | CPPUNIT_ASSERT(testNormal(unitVec[0],p3.getNormal())); | 
|---|
| [fa5a6a] | 101 | CPPUNIT_ASSERT_EQUAL(0.,p3.getOffset()); | 
|---|
|  | 102 | } | 
|---|
|  | 103 | { | 
|---|
|  | 104 | // construct with two directions + offset | 
|---|
| [407782] | 105 | Plane p1(unitVec[0],unitVec[1],0); | 
|---|
|  | 106 | CPPUNIT_ASSERT(testNormal(unitVec[2],p1.getNormal())); | 
|---|
| [fa5a6a] | 107 | CPPUNIT_ASSERT_EQUAL(0.,p1.getOffset()); | 
|---|
|  | 108 |  | 
|---|
| [407782] | 109 | Plane p2(unitVec[0],unitVec[2],0); | 
|---|
|  | 110 | CPPUNIT_ASSERT(testNormal(unitVec[1],p2.getNormal())); | 
|---|
| [fa5a6a] | 111 | CPPUNIT_ASSERT_EQUAL(0.,p2.getOffset()); | 
|---|
|  | 112 |  | 
|---|
| [407782] | 113 | Plane p3(unitVec[1],unitVec[2],0); | 
|---|
|  | 114 | CPPUNIT_ASSERT(testNormal(unitVec[0],p3.getNormal())); | 
|---|
| [fa5a6a] | 115 | CPPUNIT_ASSERT_EQUAL(0.,p3.getOffset()); | 
|---|
|  | 116 | } | 
|---|
|  | 117 | } | 
|---|
|  | 118 |  | 
|---|
|  | 119 | void PlaneUnittest::pointsTest(){ | 
|---|
|  | 120 | std::vector<Vector> points1 = p1->getPointsOnPlane(); | 
|---|
|  | 121 | CPPUNIT_ASSERT(p1->isContained(points1[0])); | 
|---|
|  | 122 | CPPUNIT_ASSERT(p1->isContained(points1[1])); | 
|---|
|  | 123 | CPPUNIT_ASSERT(p1->isContained(points1[2])); | 
|---|
|  | 124 | // check that the three points differ | 
|---|
|  | 125 | CPPUNIT_ASSERT(points1[0]!=points1[1]); | 
|---|
|  | 126 | CPPUNIT_ASSERT(points1[0]!=points1[2]); | 
|---|
|  | 127 | CPPUNIT_ASSERT(points1[1]!=points1[2]); | 
|---|
|  | 128 |  | 
|---|
|  | 129 |  | 
|---|
|  | 130 | std::vector<Vector> points2 = p2->getPointsOnPlane(); | 
|---|
|  | 131 | CPPUNIT_ASSERT(p2->isContained(points2[0])); | 
|---|
|  | 132 | CPPUNIT_ASSERT(p2->isContained(points2[1])); | 
|---|
|  | 133 | CPPUNIT_ASSERT(p2->isContained(points2[2])); | 
|---|
|  | 134 | // check that the three points differ | 
|---|
|  | 135 | CPPUNIT_ASSERT(points2[0]!=points2[1]); | 
|---|
|  | 136 | CPPUNIT_ASSERT(points2[0]!=points2[2]); | 
|---|
|  | 137 | CPPUNIT_ASSERT(points2[1]!=points2[2]); | 
|---|
|  | 138 |  | 
|---|
|  | 139 | std::vector<Vector> points3 = p3->getPointsOnPlane(); | 
|---|
|  | 140 | CPPUNIT_ASSERT(p3->isContained(points3[0])); | 
|---|
|  | 141 | CPPUNIT_ASSERT(p3->isContained(points3[1])); | 
|---|
|  | 142 | CPPUNIT_ASSERT(p3->isContained(points3[2])); | 
|---|
|  | 143 | // check that the three points differ | 
|---|
|  | 144 | CPPUNIT_ASSERT(points3[0]!=points3[1]); | 
|---|
|  | 145 | CPPUNIT_ASSERT(points3[0]!=points3[2]); | 
|---|
|  | 146 | CPPUNIT_ASSERT(points3[1]!=points3[2]); | 
|---|
|  | 147 |  | 
|---|
|  | 148 | std::vector<Vector> points4 = p4->getPointsOnPlane(); | 
|---|
|  | 149 | CPPUNIT_ASSERT(p4->isContained(points4[0])); | 
|---|
|  | 150 | CPPUNIT_ASSERT(p4->isContained(points4[1])); | 
|---|
|  | 151 | CPPUNIT_ASSERT(p4->isContained(points4[2])); | 
|---|
|  | 152 | // check that the three points differ | 
|---|
|  | 153 | CPPUNIT_ASSERT(points4[0]!=points4[1]); | 
|---|
|  | 154 | CPPUNIT_ASSERT(points4[0]!=points4[2]); | 
|---|
|  | 155 | CPPUNIT_ASSERT(points4[1]!=points4[2]); | 
|---|
|  | 156 | } | 
|---|
|  | 157 |  | 
|---|
|  | 158 |  | 
|---|
|  | 159 | void PlaneUnittest::operationsTest(){ | 
|---|
|  | 160 | { | 
|---|
| [407782] | 161 | Vector t = (1./3.)*(unitVec[0]+unitVec[1]+unitVec[2]); | 
|---|
| [71129f] | 162 | CPPUNIT_ASSERT(fabs(p1->distance(zeroVec)-t.Norm()) <= LINALG_MYEPSILON()); | 
|---|
| [fa5a6a] | 163 | CPPUNIT_ASSERT_EQUAL(t,p1->getClosestPoint(zeroVec)); | 
|---|
|  | 164 | } | 
|---|
|  | 165 |  | 
|---|
| [71129f] | 166 | CPPUNIT_ASSERT(fabs(p2->distance(unitVec[2])-1) <= LINALG_MYEPSILON()); | 
|---|
| [407782] | 167 | CPPUNIT_ASSERT_EQUAL(zeroVec,p2->getClosestPoint(unitVec[2])); | 
|---|
| [71129f] | 168 | CPPUNIT_ASSERT(fabs(p3->distance(unitVec[1])-1) <= LINALG_MYEPSILON()); | 
|---|
| [407782] | 169 | CPPUNIT_ASSERT_EQUAL(zeroVec,p3->getClosestPoint(unitVec[1])); | 
|---|
| [71129f] | 170 | CPPUNIT_ASSERT(fabs(p4->distance(unitVec[0])-1) <= LINALG_MYEPSILON()); | 
|---|
| [407782] | 171 | CPPUNIT_ASSERT_EQUAL(zeroVec,p4->getClosestPoint(unitVec[0])); | 
|---|
| [ccf826] | 172 | } | 
|---|
| [fa5a6a] | 173 |  | 
|---|
| [ccf826] | 174 | void PlaneUnittest::mirrorTest(){ | 
|---|
|  | 175 | Vector fixture; | 
|---|
|  | 176 |  | 
|---|
|  | 177 | // some Vectors that lie on the planes | 
|---|
| [407782] | 178 | fixture = p1->mirrorVector(unitVec[0]); | 
|---|
|  | 179 | CPPUNIT_ASSERT_EQUAL(fixture,unitVec[0]); | 
|---|
|  | 180 | fixture = p1->mirrorVector(unitVec[1]); | 
|---|
|  | 181 | CPPUNIT_ASSERT_EQUAL(fixture,unitVec[1]); | 
|---|
|  | 182 | fixture = p1->mirrorVector(unitVec[2]); | 
|---|
|  | 183 | CPPUNIT_ASSERT_EQUAL(fixture,unitVec[2]); | 
|---|
| [ccf826] | 184 |  | 
|---|
|  | 185 | fixture = p2->mirrorVector(zeroVec); | 
|---|
|  | 186 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec); | 
|---|
| [407782] | 187 | fixture = p2->mirrorVector(unitVec[0]); | 
|---|
|  | 188 | CPPUNIT_ASSERT_EQUAL(fixture,unitVec[0]); | 
|---|
|  | 189 | fixture = p2->mirrorVector(unitVec[1]); | 
|---|
|  | 190 | CPPUNIT_ASSERT_EQUAL(fixture,unitVec[1]); | 
|---|
| [ccf826] | 191 |  | 
|---|
|  | 192 | fixture = p3->mirrorVector(zeroVec); | 
|---|
|  | 193 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec); | 
|---|
| [407782] | 194 | fixture = p3->mirrorVector(unitVec[0]); | 
|---|
|  | 195 | CPPUNIT_ASSERT_EQUAL(fixture,unitVec[0]); | 
|---|
|  | 196 | fixture = p3->mirrorVector(unitVec[2]); | 
|---|
|  | 197 | CPPUNIT_ASSERT_EQUAL(fixture,unitVec[2]); | 
|---|
| [ccf826] | 198 |  | 
|---|
|  | 199 | fixture = p4->mirrorVector(zeroVec); | 
|---|
|  | 200 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec); | 
|---|
| [407782] | 201 | fixture = p4->mirrorVector(unitVec[1]); | 
|---|
|  | 202 | CPPUNIT_ASSERT_EQUAL(fixture,unitVec[1]); | 
|---|
|  | 203 | fixture = p4->mirrorVector(unitVec[2]); | 
|---|
|  | 204 | CPPUNIT_ASSERT_EQUAL(fixture,unitVec[2]); | 
|---|
| [ccf826] | 205 |  | 
|---|
|  | 206 | // some Vectors outside of the planes | 
|---|
|  | 207 | { | 
|---|
| [407782] | 208 | Vector t = (2./3.)*(unitVec[0]+unitVec[1]+unitVec[2]); | 
|---|
| [ccf826] | 209 | fixture = p1->mirrorVector(zeroVec); | 
|---|
|  | 210 | CPPUNIT_ASSERT_EQUAL(fixture,t); | 
|---|
|  | 211 | } | 
|---|
| [fa5a6a] | 212 |  | 
|---|
| [407782] | 213 | fixture = p2->mirrorVector(unitVec[2]); | 
|---|
|  | 214 | CPPUNIT_ASSERT_EQUAL(fixture,-1*unitVec[2]); | 
|---|
|  | 215 | fixture = p3->mirrorVector(unitVec[1]); | 
|---|
|  | 216 | CPPUNIT_ASSERT_EQUAL(fixture,-1*unitVec[1]); | 
|---|
|  | 217 | fixture = p4->mirrorVector(unitVec[0]); | 
|---|
|  | 218 | CPPUNIT_ASSERT_EQUAL(fixture,-1*unitVec[0]); | 
|---|
| [fa5a6a] | 219 | } | 
|---|
| [27ac00] | 220 |  | 
|---|
|  | 221 | void PlaneUnittest::LineIntersectionTest(){ | 
|---|
|  | 222 | Vector fixture; | 
|---|
|  | 223 | // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ??? | 
|---|
|  | 224 | Line l1 = makeLineThrough(zeroVec,Vector(2,1,0)); | 
|---|
| [407782] | 225 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(unitVec[0], zeroVec).GetIntersection(l1) ); | 
|---|
| [27ac00] | 226 | CPPUNIT_ASSERT_EQUAL( zeroVec, fixture ); | 
|---|
|  | 227 |  | 
|---|
|  | 228 | // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ??? | 
|---|
| [407782] | 229 | Line l2 = makeLineThrough(unitVec[0],Vector(0,1,1)); | 
|---|
|  | 230 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(unitVec[1], Vector(2,1,0)).GetIntersection(l2) ); | 
|---|
| [27ac00] | 231 | CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture ); | 
|---|
|  | 232 | } | 
|---|