OGS
ProjectPointOnMesh.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5#include "MathLib/Point3d.h"
9
10namespace MeshToolsLib
11{
13{
15 std::vector<const MeshLib::Element*> const& elements,
16 MathLib::Point3d const& node)
17{
18 auto is_right_of = [&node](MeshLib::Node const& a, MeshLib::Node const& b)
19 { return GeoLib::getOrientation(node, a, b) == GeoLib::Orientation::CW; };
20
21 for (auto const* e : elements)
22 {
23 auto const* nodes = e->getNodes();
24 if (e->getGeomType() == MeshLib::MeshElemType::TRIANGLE)
25 {
26 auto const& a = *nodes[0];
27 auto const& b = *nodes[1];
28 auto const& c = *nodes[2];
29 if (!is_right_of(a, b) && !is_right_of(b, c) && !is_right_of(c, a))
30 {
31 return e;
32 }
33 }
34 else if (e->getGeomType() == MeshLib::MeshElemType::QUAD)
35 {
36 auto const& a = *nodes[0];
37 auto const& b = *nodes[1];
38 auto const& c = *nodes[2];
39 auto const& d = *nodes[3];
40 if (!is_right_of(a, b) && !is_right_of(b, c) &&
41 !is_right_of(c, d) && !is_right_of(d, a))
42 {
43 return e;
44 }
45 }
46 }
47 return nullptr;
48}
49
50double getElevation(MeshLib::Element const& element,
51 MathLib::Point3d const& node)
52{
53 // mathematical description of the plane spanned by the 2d element
54 // compute coefficients of the plane equation (Hesse normal form)
55 // d = scalar_product(normal, element_node[0])
56 auto const n = MeshLib::FaceRule::getSurfaceNormal(element).normalized();
57 auto const d = n.dot(element.getNode(0)->asEigenVector3d());
58 // insert node[0] and node[1] into plane equation and transpose the equation
59 // to node[2]
60 return (d - (node[0] * n[0] + node[1] * n[1])) / n[2];
61}
62
63} // namespace ProjectPointOnMesh
64
65} // namespace MeshToolsLib
Eigen::Vector3d const & asEigenVector3d() const
Definition Point3d.h:55
virtual const Node * getNode(unsigned idx) const =0
static Eigen::Vector3d getSurfaceNormal(Element const &e)
Returns the surface normal of a 2D element.
Definition FaceRule.cpp:33
Orientation getOrientation(MathLib::Point3d const &p0, MathLib::Point3d const &p1, MathLib::Point3d const &p2)
double getElevation(MeshLib::Element const &element, MathLib::Point3d const &node)
MeshLib::Element const * getProjectedElement(std::vector< const MeshLib::Element * > const &elements, MathLib::Point3d const &node)