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