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