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