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