From 1b2342ff05c5bca25c258cc3fb309c699e80c671 Mon Sep 17 00:00:00 2001
From: Norihiro Watanabe <norihiro.watanabe@ufz.de>
Date: Mon, 13 Jul 2015 22:33:43 +0200
Subject: [PATCH] fix circular dependency between Node and Mesh

---
 Applications/Utils/MeshEdit/AddTopLayer.cpp   |  1 +
 ...CreateBoundaryConditionsAlongPolylines.cpp |  1 +
 MeshLib/Mesh.cpp                              |  3 ++-
 MeshLib/Node.h                                | 24 +++++++------------
 Tests/MeshLib/TestDuplicate.cpp               |  1 +
 5 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/Applications/Utils/MeshEdit/AddTopLayer.cpp b/Applications/Utils/MeshEdit/AddTopLayer.cpp
index c3cdd682190..1098f13e212 100644
--- a/Applications/Utils/MeshEdit/AddTopLayer.cpp
+++ b/Applications/Utils/MeshEdit/AddTopLayer.cpp
@@ -29,6 +29,7 @@
 #include "MeshLib/Mesh.h"
 #include "MeshLib/Node.h"
 #include "MeshLib/Elements/Elements.h"
+#include "MeshLib/MeshSurfaceExtraction.h"
 
 MeshLib::Prism* extrudeElement(std::vector<MeshLib::Node*> const& subsfc_nodes,
 	MeshLib::Tri const*const sfc_elem,
diff --git a/Applications/Utils/MeshEdit/CreateBoundaryConditionsAlongPolylines.cpp b/Applications/Utils/MeshEdit/CreateBoundaryConditionsAlongPolylines.cpp
index 08ccb0cd3a1..55d4cfc1e6e 100644
--- a/Applications/Utils/MeshEdit/CreateBoundaryConditionsAlongPolylines.cpp
+++ b/Applications/Utils/MeshEdit/CreateBoundaryConditionsAlongPolylines.cpp
@@ -36,6 +36,7 @@
 // MeshLib
 #include "MeshLib/Mesh.h"
 #include "MeshLib/Node.h"
+#include "MeshLib/MeshSurfaceExtraction.h"
 
 // MeshGeoToolsLib
 #include "MeshGeoToolsLib/MeshNodeSearcher.h"
diff --git a/MeshLib/Mesh.cpp b/MeshLib/Mesh.cpp
index af0dac3b228..ed6e1ce61c4 100644
--- a/MeshLib/Mesh.cpp
+++ b/MeshLib/Mesh.cpp
@@ -16,6 +16,7 @@
 
 #include "BaseLib/RunTime.h"
 
+#include "Elements/Element.h"
 #include "Elements/Tri.h"
 #include "Elements/Quad.h"
 #include "Elements/Tet.h"
@@ -143,7 +144,7 @@ void Mesh::resetElementsConnectedToNodes()
 {
 	for (auto node = _nodes.begin(); node != _nodes.end(); ++node)
 		if (*node)
-			(*node)->_elements.clear();
+			(*node)->clearElements();
 	this->setElementsConnectedToNodes();
 }
 
diff --git a/MeshLib/Node.h b/MeshLib/Node.h
index cbf789f62e2..6b6573ee0d0 100644
--- a/MeshLib/Node.h
+++ b/MeshLib/Node.h
@@ -17,16 +17,10 @@
 
 #include <cstdlib>
 #include <limits>
-#include <set>
 #include <vector>
 
 #include "MathLib/Point3dWithID.h"
-
-#include "MeshEditing/removeMeshNodes.h"
-#include "MeshGenerators/MeshLayerMapper.h"
-
-#include "MeshSurfaceExtraction.h"
-#include "Mesh.h"
+#include "MathLib/Vector3.h"
 
 namespace MeshLib {
 
@@ -37,12 +31,9 @@ class Element;
  */
 class Node : public MathLib::Point3dWithID
 {
-	/* friend functions: */
-	friend bool MeshLayerMapper::layerMapping(MeshLib::Mesh &mesh, const GeoLib::Raster &raster, double noDataReplacementValue);
-	friend MeshLib::Mesh* MeshSurfaceExtraction::getMeshSurface(const MeshLib::Mesh &mesh, const MathLib::Vector3 &dir, double angle, bool keep3dMeshIds);
-
 	/* friend classes: */
 	friend class Mesh;
+	friend class MeshSurfaceExtraction;
 	friend class MeshRevision;
 
 public:
@@ -79,6 +70,10 @@ public:
 		return Node(_x[0]-v[0], _x[1]-v[1], _x[2]-v[2]);
 	}
 
+	/// Update coordinates of a node.
+	/// This method automatically also updates the areas/volumes of all connected elements.
+	virtual void updateCoordinates(double x, double y, double z);
+
 protected:
 	/**
 	 * Add an element the node is part of.
@@ -86,6 +81,9 @@ protected:
 	 */
 	void addElement(Element* elem) { _elements.push_back(elem); }
 
+	/// clear stored elements connecting to this node
+	void clearElements() { _elements.clear(); }
+
 	/// Resets the connected nodes of this node. The connected nodes are
 	/// generated by Mesh::setNodesConnectedByEdges() and
 	/// Mesh::setNodesConnectedByElements().
@@ -97,10 +95,6 @@ protected:
 	/// Sets the ID of a node to the given value.
 	void setID(std::size_t id) { this->_id = id; }
 
-	/// Update coordinates of a node.
-	/// This method automatically also updates the areas/volumes of all connected elements.
-	virtual void updateCoordinates(double x, double y, double z);
-
 	std::vector<Node*> _connected_nodes;
 	std::vector<Element*> _elements;
 }; /* class */
diff --git a/Tests/MeshLib/TestDuplicate.cpp b/Tests/MeshLib/TestDuplicate.cpp
index 210a6b642ac..9bd7169e50c 100644
--- a/Tests/MeshLib/TestDuplicate.cpp
+++ b/Tests/MeshLib/TestDuplicate.cpp
@@ -19,6 +19,7 @@
 #include "MeshLib/Node.h"
 #include "Elements/Element.h"
 #include "MeshEditing/DuplicateMeshComponents.h"
+#include "MeshEditing/removeMeshNodes.h"
 #include "MeshGenerators/MeshGenerator.h"
 #include "MeshQuality/MeshValidation.h"
 #include "MathTools.h"
-- 
GitLab