| 1 | /*
 | 
|---|
| 2 |  * tesselationunittest.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Aug 26, 2009
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | 
 | 
|---|
| 9 | using namespace std;
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 12 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 13 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include <cstring>
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include "defs.hpp"
 | 
|---|
| 18 | #include "tesselation.hpp"
 | 
|---|
| 19 | #include "tesselationunittest.hpp"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #define SPHERERADIUS 2.
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /********************************************** Test classes **************************************/
 | 
|---|
| 24 | 
 | 
|---|
| 25 | // Registers the fixture into the 'registry'
 | 
|---|
| 26 | CPPUNIT_TEST_SUITE_REGISTRATION( TesselationTest );
 | 
|---|
| 27 | 
 | 
|---|
| 28 | 
 | 
|---|
| 29 | void TesselationTest::setUp()
 | 
|---|
| 30 | {
 | 
|---|
| 31 |   // create corners
 | 
|---|
| 32 |   class TesselPoint *Walker;
 | 
|---|
| 33 |   Walker = new TesselPoint;
 | 
|---|
| 34 |   Walker->node = new Vector(1., 0., -1.);
 | 
|---|
| 35 |   Walker->Name = Malloc<char>(3, "TesselationTest::setUp");
 | 
|---|
| 36 |   strcpy(Walker->Name, "1");
 | 
|---|
| 37 |   Walker->nr = 1;
 | 
|---|
| 38 |   Corners.push_back(Walker);
 | 
|---|
| 39 |   Walker = new TesselPoint;
 | 
|---|
| 40 |   Walker->node = new Vector(-1., 1., -1.);
 | 
|---|
| 41 |   Walker->Name = Malloc<char>(3, "TesselationTest::setUp");
 | 
|---|
| 42 |   strcpy(Walker->Name, "2");
 | 
|---|
| 43 |   Walker->nr = 2;
 | 
|---|
| 44 |   Corners.push_back(Walker);
 | 
|---|
| 45 |   Walker = new TesselPoint;
 | 
|---|
| 46 |   Walker->node = new Vector(-1., -1., -1.);
 | 
|---|
| 47 |   Walker->Name = Malloc<char>(3, "TesselationTest::setUp");
 | 
|---|
| 48 |   strcpy(Walker->Name, "3");
 | 
|---|
| 49 |   Walker->nr = 3;
 | 
|---|
| 50 |   Corners.push_back(Walker);
 | 
|---|
| 51 |   Walker = new TesselPoint;
 | 
|---|
| 52 |   Walker->node = new Vector(-1., 0., 1.);
 | 
|---|
| 53 |   Walker->Name = Malloc<char>(3, "TesselationTest::setUp");
 | 
|---|
| 54 |   strcpy(Walker->Name, "4");
 | 
|---|
| 55 |   Walker->nr = 4;
 | 
|---|
| 56 |   Corners.push_back(Walker);
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   // create linkedcell
 | 
|---|
| 59 |   LinkedList = new LinkedCell(&Corners, 2.*SPHERERADIUS);
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   // create tesselation
 | 
|---|
| 62 |   TesselStruct = new Tesselation;
 | 
|---|
| 63 |   CPPUNIT_ASSERT_EQUAL( true, TesselStruct->PointsOnBoundary.empty() );
 | 
|---|
| 64 |   CPPUNIT_ASSERT_EQUAL( true, TesselStruct->LinesOnBoundary.empty() );
 | 
|---|
| 65 |   CPPUNIT_ASSERT_EQUAL( true, TesselStruct->TrianglesOnBoundary.empty() );
 | 
|---|
| 66 |   TesselStruct->FindStartingTriangle(SPHERERADIUS, LinkedList);
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   CandidateForTesselation *baseline = NULL;
 | 
|---|
| 69 |   BoundaryTriangleSet *T = NULL;
 | 
|---|
| 70 |   bool OneLoopWithoutSuccessFlag = true;
 | 
|---|
| 71 |   bool TesselationFailFlag = false;
 | 
|---|
| 72 |   while ((!TesselStruct->OpenLines.empty()) && (OneLoopWithoutSuccessFlag)) {
 | 
|---|
| 73 |     // 2a. fill all new OpenLines
 | 
|---|
| 74 |     for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++) {
 | 
|---|
| 75 |       baseline = Runner->second;
 | 
|---|
| 76 |       if (baseline->pointlist.empty()) {
 | 
|---|
| 77 |         T = (((baseline->BaseLine->triangles.begin()))->second);
 | 
|---|
| 78 |         TesselationFailFlag = TesselStruct->FindNextSuitableTriangle(*baseline, *T, SPHERERADIUS, LinkedList); //the line is there, so there is a triangle, but only one.
 | 
|---|
| 79 |       }
 | 
|---|
| 80 |     }
 | 
|---|
| 81 | 
 | 
|---|
| 82 |     // 2b. search for smallest ShortestAngle among all candidates
 | 
|---|
| 83 |     double ShortestAngle = 4.*M_PI;
 | 
|---|
| 84 |     for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++) {
 | 
|---|
| 85 |       if (Runner->second->ShortestAngle < ShortestAngle) {
 | 
|---|
| 86 |         baseline = Runner->second;
 | 
|---|
| 87 |         ShortestAngle = baseline->ShortestAngle;
 | 
|---|
| 88 |       }
 | 
|---|
| 89 |     }
 | 
|---|
| 90 |     if ((ShortestAngle == 4.*M_PI) || (baseline->pointlist.empty()))
 | 
|---|
| 91 |       OneLoopWithoutSuccessFlag = false;
 | 
|---|
| 92 |     else {
 | 
|---|
| 93 |       TesselStruct->AddCandidatePolygon(*baseline, SPHERERADIUS, LinkedList);
 | 
|---|
| 94 |     }
 | 
|---|
| 95 |   }
 | 
|---|
| 96 | };
 | 
|---|
| 97 | 
 | 
|---|
| 98 | 
 | 
|---|
| 99 | void TesselationTest::tearDown()
 | 
|---|
| 100 | {
 | 
|---|
| 101 |   delete(LinkedList);
 | 
|---|
| 102 |   delete(TesselStruct);
 | 
|---|
| 103 |   for (LinkedCell::LinkedNodes::iterator Runner = Corners.begin(); Runner != Corners.end(); Runner++) {
 | 
|---|
| 104 |     delete((*Runner)->node);
 | 
|---|
| 105 |     delete(*Runner);
 | 
|---|
| 106 |   }
 | 
|---|
| 107 |   Corners.clear();
 | 
|---|
| 108 |   MemoryUsageObserver::purgeInstance();
 | 
|---|
| 109 |   logger::purgeInstance();
 | 
|---|
| 110 |   errorLogger::purgeInstance();
 | 
|---|
| 111 | };
 | 
|---|
| 112 | 
 | 
|---|
| 113 | /** UnitTest for Contains...()
 | 
|---|
| 114 |  *
 | 
|---|
| 115 |  */
 | 
|---|
| 116 | void TesselationTest::ContainmentTest()
 | 
|---|
| 117 | {
 | 
|---|
| 118 |   class BoundaryPointSet *point = NULL;
 | 
|---|
| 119 |   class BoundaryLineSet *line = NULL;
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   // check ContainsBoundaryPoint
 | 
|---|
| 122 |   for(LineMap::iterator Runner = TesselStruct->LinesOnBoundary.begin(); Runner != TesselStruct->LinesOnBoundary.end(); Runner++) {
 | 
|---|
| 123 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryPoint((Runner->second)->endpoints[0]));
 | 
|---|
| 124 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryPoint((Runner->second)->endpoints[1]));
 | 
|---|
| 125 |   }
 | 
|---|
| 126 |   for(TriangleMap::iterator Runner = TesselStruct->TrianglesOnBoundary.begin(); Runner != TesselStruct->TrianglesOnBoundary.end(); Runner++) {
 | 
|---|
| 127 |     for(PointMap::iterator PointRunner = TesselStruct->PointsOnBoundary.begin(); PointRunner != TesselStruct->PointsOnBoundary.end(); PointRunner++) {
 | 
|---|
| 128 |       point = PointRunner->second;
 | 
|---|
| 129 |       for (int i=0;i<3;i++)
 | 
|---|
| 130 |         if (point == (Runner->second)->endpoints[i])
 | 
|---|
| 131 |           point = NULL;
 | 
|---|
| 132 |       if (point != NULL)
 | 
|---|
| 133 |         break;
 | 
|---|
| 134 |     }
 | 
|---|
| 135 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryPoint((Runner->second)->endpoints[0]));
 | 
|---|
| 136 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryPoint((Runner->second)->endpoints[1]));
 | 
|---|
| 137 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryPoint((Runner->second)->endpoints[2]));
 | 
|---|
| 138 |     CPPUNIT_ASSERT_EQUAL( false, (Runner->second)->ContainsBoundaryPoint(point));
 | 
|---|
| 139 |   }
 | 
|---|
| 140 | 
 | 
|---|
| 141 |   // check ContainsBoundaryLine
 | 
|---|
| 142 |   for(TriangleMap::iterator Runner = TesselStruct->TrianglesOnBoundary.begin(); Runner != TesselStruct->TrianglesOnBoundary.end(); Runner++) {
 | 
|---|
| 143 |     for(LineMap::iterator LineRunner = TesselStruct->LinesOnBoundary.begin(); LineRunner != TesselStruct->LinesOnBoundary.end(); LineRunner++) {
 | 
|---|
| 144 |       line = LineRunner->second;
 | 
|---|
| 145 |       for (int i=0;i<3;i++)
 | 
|---|
| 146 |         if (line == (Runner->second)->lines[i])
 | 
|---|
| 147 |           line = NULL;
 | 
|---|
| 148 |       if (line != NULL)
 | 
|---|
| 149 |         break;
 | 
|---|
| 150 |     }
 | 
|---|
| 151 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryLine((Runner->second)->lines[0]));
 | 
|---|
| 152 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryLine((Runner->second)->lines[1]));
 | 
|---|
| 153 |     CPPUNIT_ASSERT_EQUAL( true, (Runner->second)->ContainsBoundaryLine((Runner->second)->lines[2]));
 | 
|---|
| 154 |     CPPUNIT_ASSERT_EQUAL( false, (Runner->second)->ContainsBoundaryLine(line));
 | 
|---|
| 155 |   }
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   // check IsPresentTupel
 | 
|---|
| 158 |   CPPUNIT_ASSERT_EQUAL( true, true );
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | /** UnitTest for Tesselation::GetAllTriangles()
 | 
|---|
| 162 |  *
 | 
|---|
| 163 |  */
 | 
|---|
| 164 | void TesselationTest::GetAllTrianglesTest()
 | 
|---|
| 165 | {
 | 
|---|
| 166 |   class BoundaryPointSet *Walker = NULL;
 | 
|---|
| 167 | 
 | 
|---|
| 168 |   // check that there are three adjacent triangles for every boundary point
 | 
|---|
| 169 |   for (PointMap::iterator Runner = TesselStruct->PointsOnBoundary.begin(); Runner != TesselStruct->PointsOnBoundary.end(); Runner++) {
 | 
|---|
| 170 |     Walker = Runner->second;
 | 
|---|
| 171 |     set<BoundaryTriangleSet*> *triangles = TesselStruct->GetAllTriangles(Walker);
 | 
|---|
| 172 |     CPPUNIT_ASSERT_EQUAL( (size_t)3, triangles->size() );
 | 
|---|
| 173 |     // check that the returned triangle all contain the Walker
 | 
|---|
| 174 |     for (set<BoundaryTriangleSet*>::iterator TriangleRunner = triangles->begin(); TriangleRunner != triangles->end(); TriangleRunner++)
 | 
|---|
| 175 |       CPPUNIT_ASSERT_EQUAL( true, (*TriangleRunner)->ContainsBoundaryPoint(Walker) );
 | 
|---|
| 176 |     delete(triangles);
 | 
|---|
| 177 |   }
 | 
|---|
| 178 | }
 | 
|---|
| 179 | 
 | 
|---|
| 180 | /********************************************** Main routine **************************************/
 | 
|---|
| 181 | 
 | 
|---|
| 182 | int main(int argc, char **argv)
 | 
|---|
| 183 | {
 | 
|---|
| 184 |   // Get the top level suite from the registry
 | 
|---|
| 185 |   CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
 | 
|---|
| 186 | 
 | 
|---|
| 187 |   // Adds the test to the list of test to run
 | 
|---|
| 188 |   CppUnit::TextUi::TestRunner runner;
 | 
|---|
| 189 |   runner.addTest( suite );
 | 
|---|
| 190 | 
 | 
|---|
| 191 |   // Change the default outputter to a compiler error format outputter
 | 
|---|
| 192 |   runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
 | 
|---|
| 193 |                                                        std::cerr ) );
 | 
|---|
| 194 |   // Run the tests.
 | 
|---|
| 195 |   bool wasSucessful = runner.run();
 | 
|---|
| 196 | 
 | 
|---|
| 197 |   // Return error code 1 if the one of test failed.
 | 
|---|
| 198 |   return wasSucessful ? 0 : 1;
 | 
|---|
| 199 | };
 | 
|---|