Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Tesselation/unittests/Tesselation_BoundaryTriangleUnitTest.cpp

    r0aa122 r6a7fcbb  
    3030#include "Helpers/defs.hpp"
    3131#include "Atom/TesselPoint.hpp"
     32#include "LinearAlgebra/Plane.hpp"
     33#include "LinearAlgebra/RealSpaceMatrix.hpp"
     34#include "LinearAlgebra/VectorSet.hpp"
    3235#include "Tesselation/BoundaryPointSet.hpp"
    3336#include "Tesselation/BoundaryLineSet.hpp"
     
    4952
    5053
    51 void TesselationBoundaryTriangleTest::setUp()
    52 {
    53   setVerbosity(5);
     54void TesselationBoundaryTriangleTest::createTriangle(const std::vector<Vector> &Vectors)
     55{
     56  CPPUNIT_ASSERT_EQUAL( (size_t)3, Vectors.size() );
    5457
    5558  // create nodes
    56   tesselpoints[0] = new TesselPoint;
    57   tesselpoints[0]->setPosition(Vector(0., 0., 0.));
    58   tesselpoints[0]->setName("1");
    59   tesselpoints[0]->setNr(1);
    60   points[0] = new BoundaryPointSet(tesselpoints[0]);
    61   tesselpoints[1] = new TesselPoint;
    62   tesselpoints[1]->setPosition(Vector(0., 1., 0.));
    63   tesselpoints[1]->setName("2");
    64   tesselpoints[1]->setNr(2);
    65   points[1] = new BoundaryPointSet(tesselpoints[1]);
    66   tesselpoints[2] = new TesselPoint;
    67   tesselpoints[2]->setPosition(Vector(1., 0., 0.));
    68   tesselpoints[2]->setName("3");
    69   tesselpoints[2]->setNr(3);
    70   points[2] = new BoundaryPointSet(tesselpoints[2] );
     59  for (size_t count = 0; count < NDIM; ++count) {
     60    tesselpoints[count] = new TesselPoint;
     61    tesselpoints[count]->setPosition( Vectors[count] );
     62    tesselpoints[count]->setName(toString(count));
     63    tesselpoints[count]->setNr(count);
     64    points[count] = new BoundaryPointSet(tesselpoints[count]);
     65  }
    7166
    7267  // create line
     
    7772  // create triangle
    7873  triangle = new BoundaryTriangleSet(lines, 0);
    79   triangle->GetNormalVector(Vector(0.,0.,1.));
    80 };
    81 
     74  Plane p(Vectors[0], Vectors[1], Vectors[2]);
     75  triangle->GetNormalVector(p.getNormal());
     76}
     77
     78void TesselationBoundaryTriangleTest::setUp()
     79{
     80  setVerbosity(5);
     81
     82  // create nodes
     83  std::vector<Vector> Vectors;
     84  Vectors.push_back( Vector(0., 0., 0.) );
     85  Vectors.push_back( Vector(0., 1., 0.) );
     86  Vectors.push_back( Vector(1., 0., 0.) );
     87
     88  // create triangle
     89  createTriangle(Vectors);
     90}
    8291
    8392void TesselationBoundaryTriangleTest::tearDown()
     
    9099  logger::purgeInstance();
    91100  errorLogger::purgeInstance();
    92 };
     101}
     102
     103/** UnitTest for Tesselation::IsInsideTriangle()
     104 */
     105void TesselationBoundaryTriangleTest::IsInsideTriangleTest()
     106{
     107  // inside points
     108  {
     109    // check each endnode
     110    for (size_t i=0; i< NDIM; ++i) {
     111      const Vector testPoint(triangle->endpoints[i]->node->getPosition());
     112      LOG(1, "INFO: Testing whether " << testPoint << " is an inner point.");
     113      CPPUNIT_ASSERT( triangle->IsInsideTriangle( testPoint ) );
     114    }
     115  }
     116
     117  {
     118    // check points along each BoundaryLine
     119    for (size_t i=0; i< NDIM; ++i) {
     120      Vector offset = triangle->endpoints[i%3]->node->getPosition();
     121      Vector direction = triangle->endpoints[(i+1)%3]->node->getPosition() - offset;
     122      for (double s = 0.1; s < 1.; s+= 0.1) {
     123        Vector testPoint = offset + s*direction;
     124        LOG(1, "INFO: Testing whether " << testPoint << " is an inner point.");
     125        CPPUNIT_ASSERT( triangle->IsInsideTriangle( testPoint ) );
     126      }
     127    }
     128  }
     129
     130  {
     131    // check central point
     132    Vector center;
     133    triangle->GetCenter(center);
     134    LOG(1, "INFO: Testing whether " << center << " is an inner point.");
     135    CPPUNIT_ASSERT( triangle->IsInsideTriangle( center ) );
     136  }
     137
     138  // outside points
     139  {
     140    // check points outside (i.e. those not in xy-plane through origin)
     141    double n[3];
     142    const double boundary = 4.;
     143    const double step = 1.;
     144    // go through the cube and check each point
     145    for (n[0] = -boundary; n[0] <= boundary; n[0]+=step)
     146      for (n[1] = -boundary; n[1] <= boundary; n[1]+=step)
     147        for (n[2] = -boundary; n[2] <= boundary; n[2]+=step) {
     148          const Vector testPoint(n[0], n[1], n[2]);
     149          if (n[2] != 0) {
     150            LOG(1, "INFO: Testing whether " << testPoint << " is not an inner point.");
     151            CPPUNIT_ASSERT( !triangle->IsInsideTriangle( testPoint ) );
     152          }
     153        }
     154  }
     155
     156  {
     157    // check points within the plane but outside of triangle
     158    double n[3];
     159    const double boundary = 4.;
     160    const double step = 1.;
     161    n[2] = 0;
     162    for (n[0] = -boundary; n[0] <= boundary; n[0]+=step)
     163      for (n[1] = -boundary; n[1] <= boundary; n[1]+=step) {
     164        const Vector testPoint(n[0], n[1], n[2]);
     165        if ((n[0] >=0) && (n[1] >=0) && (n[0]<=1) && (n[1]<=1)) {
     166          if (n[0]+n[1] <= 1) {
     167            LOG(1, "INFO: Testing whether " << testPoint << " is an inner point.");
     168            CPPUNIT_ASSERT( triangle->IsInsideTriangle( testPoint) );
     169          } else {
     170            LOG(1, "INFO: Testing whether " << testPoint << " is not an inner point.");
     171            CPPUNIT_ASSERT( !triangle->IsInsideTriangle( testPoint) );
     172          }
     173        } else {
     174          LOG(1, "INFO: Testing whether " << testPoint << " is not an inner point.");
     175          CPPUNIT_ASSERT( !triangle->IsInsideTriangle( testPoint) );
     176        }
     177      }
     178  }
     179}
     180
     181/** UnitTest for Tesselation::IsInsideTriangle()
     182 *
     183 * We test for some specific points that occured in larger examples of the code.
     184 *
     185 */
     186void TesselationBoundaryTriangleTest::IsInsideTriangle_specificTest()
     187{
     188  {
     189    delete triangle;
     190    // test is from --create-micelle 200 --radius 30. --position "0,0,0" of sles.data
     191    // failure is: Intersection (23.1644,24.1867,65.1272) is not inside triangle [659|Na2451,O3652,Na3762].
     192    const Vector testPoint(1.57318,1.57612,10.9874);
     193    const Vector testIntersection(23.1644,24.1867,65.1272);
     194    std::vector<Vector> vectors;
     195    vectors.push_back( Vector(23.0563,30.4673,73.7555) );
     196    vectors.push_back( Vector(25.473,25.1512,68.5467) );
     197    vectors.push_back( Vector(23.1644,24.1867,65.1272) );
     198    createTriangle(vectors);
     199    CPPUNIT_ASSERT( triangle->IsInsideTriangle( testIntersection ) );
     200  }
     201  {
     202    delete triangle;
     203    // test is from --create-micelle 200 --radius 30. --position "0,0,0" of sles.data
     204    // failure is: Intersection (20.6787,70.655,71.5657) is not inside triangle [622|Na1197,Na2166,O3366].
     205    // fix is lower LINALG_MYEPSILON (not std::numeric_limits<doubble>*100 but *1e-4)
     206    const Vector testPoint(1.57318,14.185,61.2155);
     207    const Vector testIntersection(20.67867516517798,70.65496977054023,71.56572984946152);
     208    std::vector<Vector> vectors;
     209    vectors.push_back( Vector(22.9592,68.7791,77.5907) );
     210    vectors.push_back( Vector(18.4729,72.0386,68.08839999999999) );
     211    vectors.push_back( Vector(20.3834,71.0154,70.1443) );
     212    createTriangle(vectors);
     213    CPPUNIT_ASSERT( triangle->IsInsideTriangle( testIntersection ) );
     214  }
     215  {
     216    delete triangle;
     217    // test is from --create-micelle 200 --radius 30. --position "0,0,0" of sles.data
     218    // failure is:Intersection (27.56537519896,13.40256646925,6.672946688134) is not inside triangle [702|Na5016,O6388,Na6498].
     219    // note that the Intersection cannot possibly lie be within the triangle!
     220    // we test now against correct intersection
     221    const Vector testIntersection(14.6872,36.204,39.8043);
     222    std::vector<Vector> vectors;
     223    vectors.push_back( Vector(10.7513,43.4247,48.4127) );
     224    vectors.push_back( Vector(13.7119,37.0827,47.4203) );
     225    vectors.push_back( Vector(14.6872,36.204,39.8043) );
     226    createTriangle(vectors);
     227    CPPUNIT_ASSERT( triangle->IsInsideTriangle( testIntersection ) );
     228  }
     229}
     230
     231/** UnitTest for Tesselation::GetClosestPointInsideTriangle()
     232 *
     233 * We check whether this function always returns a intersection inside the
     234 * triangle.
     235 *
     236 */
     237void TesselationBoundaryTriangleTest::GetClosestPointInsideTriangleTest()
     238{
     239  Vector TestIntersection;
     240
     241  {
     242    // march through a cube mesh on triangle in xy plane
     243    double n[3];
     244    const double boundary = 4.;
     245    const double step = 1.;
     246    // go through the cube and check each point
     247    for (n[0] = -boundary; n[0] <= boundary; n[0]+=step)
     248      for (n[1] = -boundary; n[1] <= boundary; n[1]+=step)
     249        for (n[2] = -boundary; n[2] <= boundary; n[2]+=step) {
     250          const Vector testPoint(n[0], n[1], n[2]);
     251          triangle->GetClosestPointInsideTriangle(testPoint, TestIntersection);
     252          CPPUNIT_ASSERT( triangle->IsInsideTriangle( TestIntersection ));
     253        }
     254  }
     255
     256  delete triangle;
     257  // create better triangle;
     258  VECTORSET(std::vector) Vectors;
     259  Vectors.push_back( Vector(0., 0., 0.) );
     260  Vectors.push_back( Vector(0., 1., 0.) );
     261  Vectors.push_back( Vector(1., 0., 0.) );
     262  RealSpaceMatrix M;
     263  M.setRotation(M_PI/3., M_PI/4., 2.*M_PI/3.);
     264  Vectors.transform(M);
     265  createTriangle(Vectors);
     266
     267  {
     268    // march through a cube mesh on rotated triangle
     269    double n[3];
     270    const double boundary = 4.;
     271    const double step = 1.;
     272    // go through the cube and check each point
     273    for (n[0] = -boundary; n[0] <= boundary; n[0]+=step)
     274      for (n[1] = -boundary; n[1] <= boundary; n[1]+=step)
     275        for (n[2] = -boundary; n[2] <= boundary; n[2]+=step) {
     276          const Vector testPoint(n[0], n[1], n[2]);
     277          triangle->GetClosestPointInsideTriangle(testPoint, TestIntersection);
     278          CPPUNIT_ASSERT( triangle->IsInsideTriangle( TestIntersection ));
     279        }
     280  }
     281}
     282
     283/** UnitTest for Tesselation::GetClosestPointInsideTriangle()
     284 *
     285 * We test for some specific points that occured in larger examples of the code.
     286 *
     287 */
     288void TesselationBoundaryTriangleTest::GetClosestPointInsideTriangle_specificTest()
     289{
     290  {
     291    delete triangle;
     292    // test is from --create-micelle 200 --radius 30. --position "0,0,0" of sles.data
     293    // failure is:Intersection (27.56537519896,13.40256646925,6.672946688134) is not inside triangle [702|Na5016,O6388,Na6498].
     294    // note that the Intersection cannot possibly lie be within the triangle
     295    // however, it is on its plane (off only by 2.7e-12)
     296    // testPoint2 is corrected version
     297    const Vector testPoint(27.56537519896,13.40256646925,6.672946688134);
     298    const Vector testPoint2(14.6872,36.204,39.8043);
     299    Vector testIntersection;
     300    std::vector<Vector> vectors;
     301    vectors.push_back( Vector(10.7513,43.4247,48.4127) );
     302    vectors.push_back( Vector(13.7119,37.0827,47.4203) );
     303    vectors.push_back( Vector(14.6872,36.204,39.8043) );
     304    createTriangle(vectors);
     305    triangle->GetClosestPointInsideTriangle(testPoint, testIntersection);
     306    CPPUNIT_ASSERT ( testPoint != testIntersection );
     307    CPPUNIT_ASSERT ( testPoint2 == testIntersection );
     308  }
     309}
    93310
    94311/** UnitTest for Tesselation::IsInnerPoint()
Note: See TracChangeset for help on using the changeset viewer.