| 1 | /* | 
|---|
| 2 | * tesselation_boundarytriangleunittest.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Jan 13, 2010 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | using namespace std; | 
|---|
| 9 |  | 
|---|
| 10 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 11 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 12 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 13 |  | 
|---|
| 14 | #include <cstring> | 
|---|
| 15 |  | 
|---|
| 16 | #include "defs.hpp" | 
|---|
| 17 | #include "tesselation.hpp" | 
|---|
| 18 | #include "tesselation_boundarytriangleunittest.hpp" | 
|---|
| 19 |  | 
|---|
| 20 | #define SPHERERADIUS 2. | 
|---|
| 21 |  | 
|---|
| 22 | /********************************************** Test classes **************************************/ | 
|---|
| 23 |  | 
|---|
| 24 | // Registers the fixture into the 'registry' | 
|---|
| 25 | CPPUNIT_TEST_SUITE_REGISTRATION( TesselationBoundaryTriangleTest ); | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | void TesselationBoundaryTriangleTest::setUp() | 
|---|
| 29 | { | 
|---|
| 30 | setVerbosity(5); | 
|---|
| 31 |  | 
|---|
| 32 | // create nodes | 
|---|
| 33 | TesselPoint *Walker = NULL; | 
|---|
| 34 | Walker = new TesselPoint; | 
|---|
| 35 | Walker->node = new Vector(0., 0., 0.); | 
|---|
| 36 | Walker->Name = Malloc<char>(3, "TesselationBoundaryTriangleTest::setUp - *Name"); | 
|---|
| 37 | strcpy(Walker->Name, "1"); | 
|---|
| 38 | Walker->nr = 1; | 
|---|
| 39 | points[0] = new BoundaryPointSet(Walker); | 
|---|
| 40 | Walker = new TesselPoint; | 
|---|
| 41 | Walker->node = new Vector(0., 1., 0.); | 
|---|
| 42 | Walker->Name = Malloc<char>(3, "TesselationBoundaryTriangleTest::setUp - *Name"); | 
|---|
| 43 | strcpy(Walker->Name, "2"); | 
|---|
| 44 | Walker->nr = 2; | 
|---|
| 45 | points[1] = new BoundaryPointSet(Walker); | 
|---|
| 46 | Walker = new TesselPoint; | 
|---|
| 47 | Walker->node = new Vector(1., 0., 0.); | 
|---|
| 48 | Walker->Name = Malloc<char>(3, "TesselationBoundaryTriangleTest::setUp - *Name"); | 
|---|
| 49 | strcpy(Walker->Name, "3"); | 
|---|
| 50 | Walker->nr = 3; | 
|---|
| 51 | points[2] = new BoundaryPointSet(Walker); | 
|---|
| 52 |  | 
|---|
| 53 | // create line | 
|---|
| 54 | lines[0] = new BoundaryLineSet(points[0], points[1], 0); | 
|---|
| 55 | lines[1] = new BoundaryLineSet(points[1], points[2], 1); | 
|---|
| 56 | lines[2] = new BoundaryLineSet(points[0], points[2], 2); | 
|---|
| 57 |  | 
|---|
| 58 | // create triangle | 
|---|
| 59 | triangle = new BoundaryTriangleSet(lines, 0); | 
|---|
| 60 | triangle->GetNormalVector(Vector(0.,0.,1.)); | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | void TesselationBoundaryTriangleTest::tearDown() | 
|---|
| 65 | { | 
|---|
| 66 | delete(triangle); | 
|---|
| 67 | MemoryUsageObserver::purgeInstance(); | 
|---|
| 68 | logger::purgeInstance(); | 
|---|
| 69 | errorLogger::purgeInstance(); | 
|---|
| 70 | }; | 
|---|
| 71 |  | 
|---|
| 72 | /** UnitTest for Tesselation::IsInnerPoint() | 
|---|
| 73 | */ | 
|---|
| 74 | void TesselationBoundaryTriangleTest::GetClosestPointOnPlaneTest() | 
|---|
| 75 | { | 
|---|
| 76 | Vector TestIntersection; | 
|---|
| 77 | Vector Point; | 
|---|
| 78 |  | 
|---|
| 79 | // simple test on y line | 
|---|
| 80 | Point.Init(-1.,0.5,0.); | 
|---|
| 81 | CPPUNIT_ASSERT_EQUAL( 1., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 82 | Point.Init(0.,0.5,0.); | 
|---|
| 83 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 84 | Point.Init(-4.,0.5,0.); | 
|---|
| 85 | CPPUNIT_ASSERT_EQUAL( 16., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 86 | Point.Init(0.,0.5,0.); | 
|---|
| 87 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 88 |  | 
|---|
| 89 | // simple test on x line | 
|---|
| 90 | Point.Init(0.5,-1.,0.); | 
|---|
| 91 | CPPUNIT_ASSERT_EQUAL( 1., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 92 | Point.Init(0.5,0.,0.); | 
|---|
| 93 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 94 | Point.Init(0.5,-6.,0.); | 
|---|
| 95 | CPPUNIT_ASSERT_EQUAL( 36., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 96 | Point.Init(0.5,0.,0.); | 
|---|
| 97 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 98 |  | 
|---|
| 99 | // simple test on slanted line | 
|---|
| 100 | Point.Init(1.,1.,0.); | 
|---|
| 101 | CPPUNIT_ASSERT_EQUAL( 0.5, triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 102 | Point.Init(0.5,0.5,0.); | 
|---|
| 103 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 104 | Point.Init(5.,5.,0.); | 
|---|
| 105 | CPPUNIT_ASSERT_EQUAL( 40.5, triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 106 | Point.Init(0.5,0.5,0.); | 
|---|
| 107 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 108 |  | 
|---|
| 109 | // simple test on first node | 
|---|
| 110 | Point.Init(-1.,-1.,0.); | 
|---|
| 111 | CPPUNIT_ASSERT_EQUAL( 2., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 112 | Point.Init(0.,0.,0.); | 
|---|
| 113 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 114 |  | 
|---|
| 115 | // simple test on second node | 
|---|
| 116 | Point.Init(0.,2.,0.); | 
|---|
| 117 | CPPUNIT_ASSERT_EQUAL( 1., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 118 | Point.Init(0.,1.,0.); | 
|---|
| 119 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 120 |  | 
|---|
| 121 | // simple test on third node | 
|---|
| 122 | Point.Init(2.,0.,0.); | 
|---|
| 123 | CPPUNIT_ASSERT_EQUAL( 1., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 124 | Point.Init(1.,0.,0.); | 
|---|
| 125 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 126 | }; | 
|---|
| 127 |  | 
|---|
| 128 | /** UnitTest for Tesselation::IsInnerPoint() | 
|---|
| 129 | */ | 
|---|
| 130 | void TesselationBoundaryTriangleTest::GetClosestPointOffPlaneTest() | 
|---|
| 131 | { | 
|---|
| 132 | Vector TestIntersection; | 
|---|
| 133 | Vector Point; | 
|---|
| 134 |  | 
|---|
| 135 | // straight down/up | 
|---|
| 136 | Point.Init(1./3.,1./3.,+5.); | 
|---|
| 137 | CPPUNIT_ASSERT_EQUAL( 25. , triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 138 | Point.Init(1./3.,1./3.,0.); | 
|---|
| 139 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 140 | Point.Init(1./3.,1./3.,-5.); | 
|---|
| 141 | CPPUNIT_ASSERT_EQUAL( 25. , triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 142 | Point.Init(1./3.,1./3.,0.); | 
|---|
| 143 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 144 |  | 
|---|
| 145 | // simple test on y line | 
|---|
| 146 | Point.Init(-1.,0.5,+2.); | 
|---|
| 147 | CPPUNIT_ASSERT_EQUAL( 5., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 148 | Point.Init(0.,0.5,0.); | 
|---|
| 149 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 150 | Point.Init(-1.,0.5,-3.); | 
|---|
| 151 | CPPUNIT_ASSERT_EQUAL( 10., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 152 | Point.Init(0.,0.5,0.); | 
|---|
| 153 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 154 |  | 
|---|
| 155 | // simple test on x line | 
|---|
| 156 | Point.Init(0.5,-1.,+1.); | 
|---|
| 157 | CPPUNIT_ASSERT_EQUAL( 2., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 158 | Point.Init(0.5,0.,0.); | 
|---|
| 159 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 160 | Point.Init(0.5,-1.,-2.); | 
|---|
| 161 | CPPUNIT_ASSERT_EQUAL( 5., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 162 | Point.Init(0.5,0.,0.); | 
|---|
| 163 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 164 |  | 
|---|
| 165 | // simple test on slanted line | 
|---|
| 166 | Point.Init(1.,1.,+3.); | 
|---|
| 167 | CPPUNIT_ASSERT_EQUAL( 9.5, triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 168 | Point.Init(0.5,0.5,0.); | 
|---|
| 169 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 170 | Point.Init(1.,1.,-4.); | 
|---|
| 171 | CPPUNIT_ASSERT_EQUAL( 16.5, triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 172 | Point.Init(0.5,0.5,0.); | 
|---|
| 173 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 174 |  | 
|---|
| 175 | // simple test on first node | 
|---|
| 176 | Point.Init(-1.,-1.,5.); | 
|---|
| 177 | CPPUNIT_ASSERT_EQUAL( 27., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 178 | Point.Init(0.,0.,0.); | 
|---|
| 179 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 180 |  | 
|---|
| 181 | // simple test on second node | 
|---|
| 182 | Point.Init(0.,2.,5.); | 
|---|
| 183 | CPPUNIT_ASSERT_EQUAL( 26., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 184 | Point.Init(0.,1.,0.); | 
|---|
| 185 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 186 |  | 
|---|
| 187 | // simple test on third node | 
|---|
| 188 | Point.Init(2.,0.,5.); | 
|---|
| 189 | CPPUNIT_ASSERT_EQUAL( 26., triangle->GetClosestPointInsideTriangle(&Point, &TestIntersection) ); | 
|---|
| 190 | Point.Init(1.,0.,0.); | 
|---|
| 191 | CPPUNIT_ASSERT_EQUAL( true , Point == TestIntersection ); | 
|---|
| 192 | }; | 
|---|
| 193 |  | 
|---|
| 194 |  | 
|---|
| 195 | /********************************************** Main routine **************************************/ | 
|---|
| 196 |  | 
|---|
| 197 | int main(int argc, char **argv) | 
|---|
| 198 | { | 
|---|
| 199 | // Get the top level suite from the registry | 
|---|
| 200 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); | 
|---|
| 201 |  | 
|---|
| 202 | // Adds the test to the list of test to run | 
|---|
| 203 | CppUnit::TextUi::TestRunner runner; | 
|---|
| 204 | runner.addTest( suite ); | 
|---|
| 205 |  | 
|---|
| 206 | // Change the default outputter to a compiler error format outputter | 
|---|
| 207 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), | 
|---|
| 208 | std::cerr ) ); | 
|---|
| 209 | // Run the tests. | 
|---|
| 210 | bool wasSucessful = runner.run(); | 
|---|
| 211 |  | 
|---|
| 212 | // Return error code 1 if the one of test failed. | 
|---|
| 213 | return wasSucessful ? 0 : 1; | 
|---|
| 214 | }; | 
|---|