From c3d48a31fee2b737ae6165d93e1468e455f2bd1d Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Tue, 26 Jun 2018 10:33:11 +0200
Subject: [PATCH] [MeL] Impl. of calculateNormalizedSurfaceNormal().

---
 MeshLib/Elements/Utils.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/MeshLib/Elements/Utils.h b/MeshLib/Elements/Utils.h
index 70e1e20eded..df3fa27524d 100644
--- a/MeshLib/Elements/Utils.h
+++ b/MeshLib/Elements/Utils.h
@@ -16,6 +16,7 @@
 #include "MeshLib/Node.h"
 
 #include "Element.h"
+#include "FaceRule.h"
 
 namespace MeshLib
 {
@@ -39,4 +40,34 @@ inline std::vector<Node*> getBaseNodes(std::vector<Element*> const& elements)
     return base_nodes;
 }
 
+inline MathLib::Vector3 calculateNormalizedSurfaceNormal(
+    MeshLib::Element const& surface_element,
+    MeshLib::Element const& bulk_element)
+{
+    MathLib::Vector3 surface_element_normal;
+    if (surface_element.getDimension() < 2)
+    {
+        auto const bulk_element_normal = MeshLib::FaceRule::getSurfaceNormal(
+            &bulk_element);
+        MathLib::Vector3 const edge_vector(*surface_element.getNode(0),
+                                           *surface_element.getNode(1));
+        surface_element_normal =
+            MathLib::crossProduct(bulk_element_normal, edge_vector);
+    }
+    else
+    {
+        surface_element_normal =
+            MeshLib::FaceRule::getSurfaceNormal(&surface_element);
+    }
+
+    surface_element_normal.normalize();
+    // At the moment (2018-04-26) the surface normal is not oriented
+    // according to the right hand rule
+    // for correct results it is necessary to multiply the normal with
+    // -1
+    surface_element_normal *= -1;
+
+    return surface_element_normal;
+}
+
 }  // namespace MeshLib
-- 
GitLab