| [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 | 
 | 
|---|
| [d04966] | 8 | /*
 | 
|---|
| [f844ef] | 9 |  * TesselationUnitTest.cpp
 | 
|---|
| [d04966] | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Aug 26, 2009
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
| [d04966] | 19 | 
 | 
|---|
 | 20 | using namespace std;
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 24 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
 | 25 | 
 | 
|---|
| [49e1ae] | 26 | #include <cstring>
 | 
|---|
 | 27 | 
 | 
|---|
| [d74077] | 28 | #include "BoundaryLineSet.hpp"
 | 
|---|
 | 29 | #include "BoundaryTriangleSet.hpp"
 | 
|---|
 | 30 | #include "CandidateForTesselation.hpp"
 | 
|---|
| [34c43a] | 31 | #include "Helpers/defs.hpp"
 | 
|---|
 | 32 | #include "PointCloudAdaptor.hpp"
 | 
|---|
 | 33 | #include "TesselPoint.hpp"
 | 
|---|
| [f844ef] | 34 | 
 | 
|---|
 | 35 | #include "TesselationUnitTest.hpp"
 | 
|---|
| [d04966] | 36 | 
 | 
|---|
| [9b6b2f] | 37 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 38 | #include "UnitTestMain.hpp"
 | 
|---|
 | 39 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
 | 40 | 
 | 
|---|
| [88b400] | 41 | const double TesselationTest::SPHERERADIUS=2.;
 | 
|---|
| [d04966] | 42 | 
 | 
|---|
 | 43 | /********************************************** Test classes **************************************/
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | // Registers the fixture into the 'registry'
 | 
|---|
 | 46 | CPPUNIT_TEST_SUITE_REGISTRATION( TesselationTest );
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | void TesselationTest::setUp()
 | 
|---|
 | 50 | {
 | 
|---|
 | 51 |   // create corners
 | 
|---|
 | 52 |   class TesselPoint *Walker;
 | 
|---|
 | 53 |   Walker = new TesselPoint;
 | 
|---|
| [d74077] | 54 |   Walker->setPosition(Vector(1., 0., -1.));
 | 
|---|
| [68f03d] | 55 |   Walker->setName("1");
 | 
|---|
| [a479fa] | 56 |   Walker->setNr(1);
 | 
|---|
| [d04966] | 57 |   Corners.push_back(Walker);
 | 
|---|
 | 58 |   Walker = new TesselPoint;
 | 
|---|
| [d74077] | 59 |   Walker->setPosition(Vector(-1., 1., -1.));
 | 
|---|
| [68f03d] | 60 |   Walker->setName("2");
 | 
|---|
| [a479fa] | 61 |   Walker->setNr(2);
 | 
|---|
| [d04966] | 62 |   Corners.push_back(Walker);
 | 
|---|
 | 63 |   Walker = new TesselPoint;
 | 
|---|
| [d74077] | 64 |   Walker->setPosition(Vector(-1., -1., -1.));
 | 
|---|
| [68f03d] | 65 |   Walker->setName("3");
 | 
|---|
| [a479fa] | 66 |   Walker->setNr(3);
 | 
|---|
| [d04966] | 67 |   Corners.push_back(Walker);
 | 
|---|
 | 68 |   Walker = new TesselPoint;
 | 
|---|
| [d74077] | 69 |   Walker->setPosition(Vector(-1., 0., 1.));
 | 
|---|
| [68f03d] | 70 |   Walker->setName("4");
 | 
|---|
| [a479fa] | 71 |   Walker->setNr(4);
 | 
|---|
| [d04966] | 72 |   Corners.push_back(Walker);
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |   // create linkedcell
 | 
|---|
| [caa06ef] | 75 |   PointCloudAdaptor<TesselPointSTLList> cloud(&Corners, "TesselPointSTLList");
 | 
|---|
| [34c43a] | 76 |   LinkedList = new LinkedCell(cloud, 2.*SPHERERADIUS);
 | 
|---|
| [d04966] | 77 | 
 | 
|---|
 | 78 |   // create tesselation
 | 
|---|
 | 79 |   TesselStruct = new Tesselation;
 | 
|---|
| [c15ca2] | 80 |   CPPUNIT_ASSERT_EQUAL( true, TesselStruct->PointsOnBoundary.empty() );
 | 
|---|
 | 81 |   CPPUNIT_ASSERT_EQUAL( true, TesselStruct->LinesOnBoundary.empty() );
 | 
|---|
 | 82 |   CPPUNIT_ASSERT_EQUAL( true, TesselStruct->TrianglesOnBoundary.empty() );
 | 
|---|
| [e138de] | 83 |   TesselStruct->FindStartingTriangle(SPHERERADIUS, LinkedList);
 | 
|---|
| [d04966] | 84 | 
 | 
|---|
| [c15ca2] | 85 |   CandidateForTesselation *baseline = NULL;
 | 
|---|
 | 86 |   BoundaryTriangleSet *T = NULL;
 | 
|---|
 | 87 |   bool OneLoopWithoutSuccessFlag = true;
 | 
|---|
 | 88 |   bool TesselationFailFlag = false;
 | 
|---|
 | 89 |   while ((!TesselStruct->OpenLines.empty()) && (OneLoopWithoutSuccessFlag)) {
 | 
|---|
 | 90 |     // 2a. fill all new OpenLines
 | 
|---|
 | 91 |     for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++) {
 | 
|---|
 | 92 |       baseline = Runner->second;
 | 
|---|
 | 93 |       if (baseline->pointlist.empty()) {
 | 
|---|
 | 94 |         T = (((baseline->BaseLine->triangles.begin()))->second);
 | 
|---|
 | 95 |         TesselationFailFlag = TesselStruct->FindNextSuitableTriangle(*baseline, *T, SPHERERADIUS, LinkedList); //the line is there, so there is a triangle, but only one.
 | 
|---|
 | 96 |       }
 | 
|---|
| [d04966] | 97 |     }
 | 
|---|
| [c15ca2] | 98 | 
 | 
|---|
 | 99 |     // 2b. search for smallest ShortestAngle among all candidates
 | 
|---|
 | 100 |     double ShortestAngle = 4.*M_PI;
 | 
|---|
 | 101 |     for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++) {
 | 
|---|
 | 102 |       if (Runner->second->ShortestAngle < ShortestAngle) {
 | 
|---|
 | 103 |         baseline = Runner->second;
 | 
|---|
 | 104 |         ShortestAngle = baseline->ShortestAngle;
 | 
|---|
 | 105 |       }
 | 
|---|
 | 106 |     }
 | 
|---|
 | 107 |     if ((ShortestAngle == 4.*M_PI) || (baseline->pointlist.empty()))
 | 
|---|
 | 108 |       OneLoopWithoutSuccessFlag = false;
 | 
|---|
 | 109 |     else {
 | 
|---|
| [474961] | 110 |       TesselStruct->AddCandidatePolygon(*baseline, SPHERERADIUS, LinkedList);
 | 
|---|
| [d04966] | 111 |     }
 | 
|---|
 | 112 |   }
 | 
|---|
 | 113 | };
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 | void TesselationTest::tearDown()
 | 
|---|
 | 117 | {
 | 
|---|
 | 118 |   delete(LinkedList);
 | 
|---|
 | 119 |   delete(TesselStruct);
 | 
|---|
| [34c43a] | 120 |   for (TesselPointSTLList::iterator Runner = Corners.begin(); Runner != Corners.end(); Runner++)
 | 
|---|
| [d04966] | 121 |     delete(*Runner);
 | 
|---|
 | 122 |   Corners.clear();
 | 
|---|
| [c15ca2] | 123 |   logger::purgeInstance();
 | 
|---|
 | 124 |   errorLogger::purgeInstance();
 | 
|---|
| [d04966] | 125 | };
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 | /** UnitTest for Contains...()
 | 
|---|
 | 128 |  *
 | 
|---|
 | 129 |  */
 | 
|---|
 | 130 | void TesselationTest::ContainmentTest()
 | 
|---|
 | 131 | {
 | 
|---|
 | 132 |   class BoundaryPointSet *point = NULL;
 | 
|---|
 | 133 |   class BoundaryLineSet *line = NULL;
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 |   // check ContainsBoundaryPoint
 | 
|---|
 | 136 |   for(LineMap::iterator Runner = TesselStruct->LinesOnBoundary.begin(); Runner != TesselStruct->LinesOnBoundary.end(); Runner++) {
 | 
|---|
 | 137 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryPoint((Runner->second)->endpoints[0]));
 | 
|---|
 | 138 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryPoint((Runner->second)->endpoints[1]));
 | 
|---|
 | 139 |   }
 | 
|---|
 | 140 |   for(TriangleMap::iterator Runner = TesselStruct->TrianglesOnBoundary.begin(); Runner != TesselStruct->TrianglesOnBoundary.end(); Runner++) {
 | 
|---|
 | 141 |     for(PointMap::iterator PointRunner = TesselStruct->PointsOnBoundary.begin(); PointRunner != TesselStruct->PointsOnBoundary.end(); PointRunner++) {
 | 
|---|
 | 142 |       point = PointRunner->second;
 | 
|---|
 | 143 |       for (int i=0;i<3;i++)
 | 
|---|
 | 144 |         if (point == (Runner->second)->endpoints[i])
 | 
|---|
 | 145 |           point = NULL;
 | 
|---|
 | 146 |       if (point != NULL)
 | 
|---|
 | 147 |         break;
 | 
|---|
 | 148 |     }
 | 
|---|
 | 149 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryPoint((Runner->second)->endpoints[0]));
 | 
|---|
 | 150 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryPoint((Runner->second)->endpoints[1]));
 | 
|---|
 | 151 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryPoint((Runner->second)->endpoints[2]));
 | 
|---|
 | 152 |     CPPUNIT_ASSERT_EQUAL( false, (Runner->second)->ContainsBoundaryPoint(point));
 | 
|---|
 | 153 |   }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 |   // check ContainsBoundaryLine
 | 
|---|
 | 156 |   for(TriangleMap::iterator Runner = TesselStruct->TrianglesOnBoundary.begin(); Runner != TesselStruct->TrianglesOnBoundary.end(); Runner++) {
 | 
|---|
 | 157 |     for(LineMap::iterator LineRunner = TesselStruct->LinesOnBoundary.begin(); LineRunner != TesselStruct->LinesOnBoundary.end(); LineRunner++) {
 | 
|---|
 | 158 |       line = LineRunner->second;
 | 
|---|
 | 159 |       for (int i=0;i<3;i++)
 | 
|---|
 | 160 |         if (line == (Runner->second)->lines[i])
 | 
|---|
 | 161 |           line = NULL;
 | 
|---|
 | 162 |       if (line != NULL)
 | 
|---|
 | 163 |         break;
 | 
|---|
 | 164 |     }
 | 
|---|
 | 165 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryLine((Runner->second)->lines[0]));
 | 
|---|
 | 166 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryLine((Runner->second)->lines[1]));
 | 
|---|
 | 167 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryLine((Runner->second)->lines[2]));
 | 
|---|
 | 168 |     CPPUNIT_ASSERT_EQUAL( false, (Runner->second)->ContainsBoundaryLine(line));
 | 
|---|
 | 169 |   }
 | 
|---|
 | 170 | 
 | 
|---|
 | 171 |   // check IsPresentTupel
 | 
|---|
 | 172 |   CPPUNIT_ASSERT_EQUAL( true, true );
 | 
|---|
 | 173 | }
 | 
|---|
 | 174 | 
 | 
|---|
 | 175 | /** UnitTest for Tesselation::GetAllTriangles()
 | 
|---|
 | 176 |  *
 | 
|---|
 | 177 |  */
 | 
|---|
 | 178 | void TesselationTest::GetAllTrianglesTest()
 | 
|---|
 | 179 | {
 | 
|---|
 | 180 |   class BoundaryPointSet *Walker = NULL;
 | 
|---|
 | 181 | 
 | 
|---|
 | 182 |   // check that there are three adjacent triangles for every boundary point
 | 
|---|
 | 183 |   for (PointMap::iterator Runner = TesselStruct->PointsOnBoundary.begin(); Runner != TesselStruct->PointsOnBoundary.end(); Runner++) {
 | 
|---|
 | 184 |     Walker = Runner->second;
 | 
|---|
| [e138de] | 185 |     set<BoundaryTriangleSet*> *triangles = TesselStruct->GetAllTriangles(Walker);
 | 
|---|
| [d04966] | 186 |     CPPUNIT_ASSERT_EQUAL( (size_t)3, triangles->size() );
 | 
|---|
 | 187 |     // check that the returned triangle all contain the Walker
 | 
|---|
 | 188 |     for (set<BoundaryTriangleSet*>::iterator TriangleRunner = triangles->begin(); TriangleRunner != triangles->end(); TriangleRunner++)
 | 
|---|
 | 189 |       CPPUNIT_ASSERT_EQUAL( true, (*TriangleRunner)->ContainsBoundaryPoint(Walker) );
 | 
|---|
| [c26f44] | 190 |     delete(triangles);
 | 
|---|
| [d04966] | 191 |   }
 | 
|---|
 | 192 | }
 | 
|---|