From 7e05b4deaac7000883396dba1d6dd622a0a2cbcf Mon Sep 17 00:00:00 2001 From: Karsten Rink <karsten.rink@ufz.de> Date: Tue, 23 Sep 2014 16:12:54 +0200 Subject: [PATCH] added tests --- GeoLib/AnalyticalGeometry.cpp | 10 +-- MeshLib/Elements/TemplateLine-impl.h | 4 +- Tests/MeshLib/TestPntInElement.cpp | 96 ++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 Tests/MeshLib/TestPntInElement.cpp diff --git a/GeoLib/AnalyticalGeometry.cpp b/GeoLib/AnalyticalGeometry.cpp index a386e90a42b..00ae380c172 100644 --- a/GeoLib/AnalyticalGeometry.cpp +++ b/GeoLib/AnalyticalGeometry.cpp @@ -245,22 +245,22 @@ bool isPointInTriangle(GeoLib::Point const& q, bool isPointInTetrahedron(GeoLib::Point const& p, GeoLib::Point const& a, GeoLib::Point const& b, GeoLib::Point const& c, GeoLib::Point const& d) { - double const d0 (orient3d(a,b,c,d)); + double const d0 (orient3d(d,a,b,c)); // if tetrahedron is not coplanar if (d0 != 0) { bool const d0_sign (d0>0); // if p is on the same side of bcd as a - if (d0_sign != (orient3d(d, p, b, c)>0)) + if (d0_sign != (orient3d(d, p, b, c)>=0)) return false; // if p is on the same side of acd as b - if (d0_sign != (orient3d(d, a, p, c)>0)) + if (d0_sign != (orient3d(d, a, p, c)>=0)) return false; // if p is on the same side of abd as c - if (d0_sign != (orient3d(d, a, b, p)>0)) + if (d0_sign != (orient3d(d, a, b, p)>=0)) return false; // if p is on the same side of abc as d - if (d0_sign != (orient3d(p, a, b, c)>0)) + if (d0_sign != (orient3d(p, a, b, c)>=0)) return false; return true; } diff --git a/MeshLib/Elements/TemplateLine-impl.h b/MeshLib/Elements/TemplateLine-impl.h index b954d3e238d..b22ba9a7fa1 100644 --- a/MeshLib/Elements/TemplateLine-impl.h +++ b/MeshLib/Elements/TemplateLine-impl.h @@ -59,8 +59,8 @@ template <unsigned NNODES, CellType CELLLINETYPE> bool TemplateLine<NNODES,CELLLINETYPE>::isPntInElement(GeoLib::Point const& pnt) const { double tmp; - double dist(0); - MathLib::calcProjPntToLineAndDists(pnt.getCoords(), _nodes[0]->getCoords(), _nodes[1]->getCoords(), tmp, dist); + double tmp_dst(0); + double const dist =MathLib::calcProjPntToLineAndDists(pnt.getCoords(), _nodes[0]->getCoords(), _nodes[1]->getCoords(), tmp, tmp_dst); return (dist < std::numeric_limits<double>::epsilon()); } diff --git a/Tests/MeshLib/TestPntInElement.cpp b/Tests/MeshLib/TestPntInElement.cpp new file mode 100644 index 00000000000..b4d6b097d16 --- /dev/null +++ b/Tests/MeshLib/TestPntInElement.cpp @@ -0,0 +1,96 @@ +/** + * @file TestPntInElement.cpp + * @author Karsten Rink + * @date 2014-09-23 + * @brief Tests for check if a point is located inside an element + * + * @copyright + * Copyright (c) 2012-2014, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/LICENSE.txt + */ + +#include "gtest/gtest.h" + +#include "Mesh.h" +#include "Node.h" +#include "MeshEditing/MeshRevision.h" +#include "Elements/Element.h" +#include "Elements/Tri.h" +#include "Elements/Quad.h" +#include "Elements/Tet.h" +#include "Elements/Hex.h" +#include "Elements/Pyramid.h" +#include "Elements/Prism.h" + + +std::vector<MeshLib::Node*> createNodes() +{ + std::vector<MeshLib::Node*> nodes; + nodes.push_back(new MeshLib::Node(0,0,0)); + nodes.push_back(new MeshLib::Node(1,0,0)); + nodes.push_back(new MeshLib::Node(1,1,0)); + nodes.push_back(new MeshLib::Node(0,1,0)); + nodes.push_back(new MeshLib::Node(0,0,1)); + nodes.push_back(new MeshLib::Node(1,0,1)); + nodes.push_back(new MeshLib::Node(1,1,1)); + nodes.push_back(new MeshLib::Node(0,1,1)); + return nodes; +} + +TEST(IsPntInElement, Line) +{ + GeoLib::Point pnt; + std::vector<MeshLib::Node*> nodes (createNodes()); + std::array<MeshLib::Node*, 2> line_nodes = { nodes[0], nodes[4] }; + MeshLib::Line line(line_nodes); + pnt = GeoLib::Point(0,0,0.7); + ASSERT_EQ(true, line.isPntInElement(pnt)); + pnt = GeoLib::Point(0,0.1,0.7); + ASSERT_EQ(false, line.isPntInElement(pnt)); +} +TEST(IsPntInElement, Tri) +{ + GeoLib::Point pnt; + std::vector<MeshLib::Node*> nodes (createNodes()); + std::array<MeshLib::Node*, 3> tri_nodes = { nodes[0], nodes[1], nodes[4] }; + MeshLib::Tri tri(tri_nodes); + + pnt = GeoLib::Point(0.1,0,.1); + ASSERT_EQ(true, tri.isPntInElement(pnt)); + pnt = GeoLib::Point(0.9,0,.7); + ASSERT_EQ(false, tri.isPntInElement(pnt)); + +} + +TEST(IsPntInElement, Tet) +{ + GeoLib::Point pnt; + std::vector<MeshLib::Node*> nodes (createNodes()); + std::array<MeshLib::Node*, 4> tet_nodes = { nodes[0], nodes[1], nodes[2], nodes[4] }; + MeshLib::Tet tet(tet_nodes); + + pnt = GeoLib::Point(0.5,0.3,0.1); + ASSERT_EQ(true, tet.isPntInElement(pnt)); + pnt = GeoLib::Point(0.5,0.6,0.1); + ASSERT_EQ(false, tet.isPntInElement(pnt)); +} + +TEST(IsPntInElement, Hex) +{ + GeoLib::Point pnt; + std::vector<MeshLib::Node*> nodes (createNodes()); + std::array<MeshLib::Node*, 8> hex_nodes; + std::copy(nodes.begin(), nodes.end(), hex_nodes.begin()); + MeshLib::Hex hex(hex_nodes); + + pnt = GeoLib::Point(0.99,0.99,0.99); + ASSERT_EQ(true, hex.isPntInElement(pnt)); + pnt = GeoLib::Point(0.99,0,0); + ASSERT_EQ(true, hex.isPntInElement(pnt)); + pnt = GeoLib::Point(0.0, 0.0, 0.0); + ASSERT_EQ(true, hex.isPntInElement(pnt)); + pnt = GeoLib::Point(1.01,0.99,0.99); + ASSERT_EQ(false, hex.isPntInElement(pnt)); +} \ No newline at end of file -- GitLab