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