From d43994e84e0582d827b4e665fd25ee718c9eec99 Mon Sep 17 00:00:00 2001
From: Karsten Rink <karsten.rink@ufz.de>
Date: Mon, 7 May 2012 13:15:42 +0200
Subject: [PATCH] fixed memory allocation for element nodearrays

---
 MeshLib/Elements/Cell.cpp    |  4 ++--
 MeshLib/Elements/Cell.h      |  3 ++-
 MeshLib/Elements/Edge.cpp    | 13 ++++++++-----
 MeshLib/Elements/Element.cpp |  5 +++--
 MeshLib/Elements/Element.h   |  5 +++--
 MeshLib/Elements/Face.cpp    |  4 ++--
 MeshLib/Elements/Face.h      |  3 ++-
 MeshLib/Elements/FemElem.h   |  4 ++--
 MeshLib/Elements/Hex.cpp     |  9 +++++----
 MeshLib/Elements/Prism.cpp   | 18 ++++++++++++------
 MeshLib/Elements/Pyramid.cpp | 23 ++++++++++++++---------
 MeshLib/Elements/Quad.cpp    | 15 ++++++++++-----
 MeshLib/Elements/Tet.cpp     | 15 ++++++++++-----
 MeshLib/Elements/Tet10.cpp   | 30 +++++++++++++++++-------------
 MeshLib/Elements/Tet10.h     |  2 +-
 MeshLib/Elements/Tet4.cpp    | 17 +++++++----------
 MeshLib/Elements/Tet4.h      |  2 +-
 MeshLib/Elements/Tri.cpp     | 14 +++++++++-----
 18 files changed, 110 insertions(+), 76 deletions(-)

diff --git a/MeshLib/Elements/Cell.cpp b/MeshLib/Elements/Cell.cpp
index 87438c4ee49..307bc9be0eb 100644
--- a/MeshLib/Elements/Cell.cpp
+++ b/MeshLib/Elements/Cell.cpp
@@ -8,12 +8,12 @@
 #include "Cell.h"
 
 namespace MeshLib {
-
+/*
 Cell::Cell(Node** nodes, MshElemType::type type, size_t value)
 	: Element(nodes, type, value)
 {
 }
-
+*/
 Cell::Cell(MshElemType::type type, size_t value)
 	: Element(type, value)
 {
diff --git a/MeshLib/Elements/Cell.h b/MeshLib/Elements/Cell.h
index 55f19d91b36..81f434cb885 100644
--- a/MeshLib/Elements/Cell.h
+++ b/MeshLib/Elements/Cell.h
@@ -28,9 +28,10 @@ public:
 	virtual ~Cell();
 
 protected:
+/*
 	/// Constructor for a generic mesh element containing an array of mesh nodes.
 	Cell(Node** nodes, MshElemType::type type, size_t value = 0);
-
+*/
 	/// Constructor for a generic mesh element without an array of mesh nodes.
 	Cell(MshElemType::type type, size_t value = 0);
 
diff --git a/MeshLib/Elements/Edge.cpp b/MeshLib/Elements/Edge.cpp
index d257c8c45af..836f3eb3627 100644
--- a/MeshLib/Elements/Edge.cpp
+++ b/MeshLib/Elements/Edge.cpp
@@ -13,16 +13,18 @@
 namespace MeshLib {
 
 Edge::Edge(Node* nodes[2], size_t value)
-	: Element(nodes, MshElemType::LINE, value)
+	: Element(MshElemType::LINE, value)
 {
+	_nodes = nodes;
 	this->_length = this->calcLength();
 }
 
 Edge::Edge(Node* n0, Node* n1, size_t value)
 	: Element(MshElemType::LINE, value)
 {
-	Node* nodes[2] = { n0, n1 };
-	_nodes = nodes;
+	_nodes = new Node*[2];
+	_nodes[0] = n0;
+	_nodes[1] = n1;
 
 	this->_length = this->calcLength();
 }
@@ -30,8 +32,9 @@ Edge::Edge(Node* n0, Node* n1, size_t value)
 Edge::Edge(const Edge &edge)
 	: Element(MshElemType::LINE, edge.getValue())
 {
-	Node* nodes[2] = { new Node(*edge.getNode(0)), new Node(*edge.getNode(1)) };
-	_nodes = nodes;
+	_nodes = new Node*[2];
+	_nodes[0] = edge._nodes[0];
+	_nodes[1] = edge._nodes[1];
 	_length = edge.getLength();
 }
 
diff --git a/MeshLib/Elements/Element.cpp b/MeshLib/Elements/Element.cpp
index 66cb6195a03..6f21f44c46a 100644
--- a/MeshLib/Elements/Element.cpp
+++ b/MeshLib/Elements/Element.cpp
@@ -12,13 +12,14 @@
 
 namespace MeshLib {
 
+/*
 Element::Element(Node** nodes, MshElemType::type type, size_t value)
 	: _nodes(nodes), _type(type), _value(value)
 {
 }
-
+*/
 Element::Element(MshElemType::type type, size_t value)
-	: _type(type), _value(value)
+	: _nodes(NULL), _type(type), _value(value)
 {
 }
 
diff --git a/MeshLib/Elements/Element.h b/MeshLib/Elements/Element.h
index e84058b1255..f611738f44e 100644
--- a/MeshLib/Elements/Element.h
+++ b/MeshLib/Elements/Element.h
@@ -47,9 +47,10 @@ public:
 	virtual ~Element();
 
 protected:
+/*
 	/// Constructor for a generic mesh element containing an array of mesh nodes.
 	Element(Node** nodes, MshElemType::type type, size_t value = 0);
-
+*/
 	/// Constructor for a generic mesh element without an array of mesh nodes.
 	Element(MshElemType::type type, size_t value = 0);
 
@@ -59,9 +60,9 @@ protected:
 	 */
 	Node* getNode(size_t i);
 
+	Node** _nodes;
 	MshElemType::type _type;
 	size_t _value;
-	Node** _nodes;
 	std::vector<Element*> _neighbors;
 
 private:
diff --git a/MeshLib/Elements/Face.cpp b/MeshLib/Elements/Face.cpp
index 0e4836a2c58..9692e245b37 100644
--- a/MeshLib/Elements/Face.cpp
+++ b/MeshLib/Elements/Face.cpp
@@ -8,12 +8,12 @@
 #include "Face.h"
 
 namespace MeshLib {
-
+/*
 Face::Face(Node** nodes, MshElemType::type type, size_t value)
 	: Element(nodes, type, value)
 {
 }
-
+*/
 Face::Face(MshElemType::type type, size_t value)
 	: Element(type, value)
 {
diff --git a/MeshLib/Elements/Face.h b/MeshLib/Elements/Face.h
index 0fef6c30f81..489acacf6e1 100644
--- a/MeshLib/Elements/Face.h
+++ b/MeshLib/Elements/Face.h
@@ -28,9 +28,10 @@ public:
 	virtual ~Face();
 
 protected:
+/*
 	/// Constructor for a generic mesh element containing an array of mesh nodes.
 	Face(Node** nodes, MshElemType::type type, size_t value = 0);
-
+*/
 	/// Constructor for a generic mesh element without an array of mesh nodes.
 	Face(MshElemType::type type, size_t value = 0);
 
diff --git a/MeshLib/Elements/FemElem.h b/MeshLib/Elements/FemElem.h
index 109048929ed..8d7127b2ea8 100644
--- a/MeshLib/Elements/FemElem.h
+++ b/MeshLib/Elements/FemElem.h
@@ -22,7 +22,7 @@ public:
 	virtual ~FemElem();
 
 	/// Get the number of nodes for this element.
-	const GEOLIB::Point getCentreOfGravity() const { return _centre_of_gravity; };
+	const GEOLIB::Point& getCentreOfGravity() const { return _centre_of_gravity; };
 
 protected:
 	/// Constructor.
@@ -32,7 +32,7 @@ protected:
 	FemElem(const FemElem &elem);
 
 	/// Calculate centre of gravity
-	virtual GEOLIB::Point calcCoG() = 0;
+	virtual void calcCoG() = 0;
 
 	GEOLIB::Point _centre_of_gravity;
 
diff --git a/MeshLib/Elements/Hex.cpp b/MeshLib/Elements/Hex.cpp
index 2a001f6b1a8..c4bdbcfd870 100644
--- a/MeshLib/Elements/Hex.cpp
+++ b/MeshLib/Elements/Hex.cpp
@@ -14,17 +14,18 @@
 namespace MeshLib {
 
 Hex::Hex(Node* nodes[8], size_t value)
-	: Cell(nodes, MshElemType::HEXAHEDRON, value)
+	: Cell(MshElemType::HEXAHEDRON, value)
 {
+	_nodes = nodes;
 	this->_volume = this->calcVolume();
 }
 
 Hex::Hex(const Hex &hex)
 	: Cell(MshElemType::HEXAHEDRON, hex.getValue())
 {
-	Node* nodes[8] = { new Node(*hex.getNode(0)), new Node(*hex.getNode(1)), new Node(*hex.getNode(2)), new Node(*hex.getNode(3)),
-	                   new Node(*hex.getNode(4)), new Node(*hex.getNode(5)), new Node(*hex.getNode(6)), new Node(*hex.getNode(7)) };
-	_nodes = nodes;
+	_nodes = new Node*[8];
+	for (size_t i=0; i<8; i++)
+		_nodes[i] = hex._nodes[i];
 	_volume = hex.getVolume();
 }
 
diff --git a/MeshLib/Elements/Prism.cpp b/MeshLib/Elements/Prism.cpp
index da4f51e200b..ebcc2dfc49c 100644
--- a/MeshLib/Elements/Prism.cpp
+++ b/MeshLib/Elements/Prism.cpp
@@ -13,16 +13,22 @@
 namespace MeshLib {
 
 Prism::Prism(Node* nodes[6], size_t value)
-	: Cell(nodes, MshElemType::PRISM, value)
+	: Cell(MshElemType::PRISM, value)
 {
+	_nodes = _nodes;
 	this->_volume = this->calcVolume();
 }
 
 Prism::Prism(Node* n0, Node* n1, Node* n2, Node* n3, Node* n4, Node* n5, size_t value)
 	: Cell(MshElemType::PRISM, value)
 {
-	Node* nodes[6] = { n0, n1, n2, n3, n4, n5 };
-	_nodes = nodes;
+	_nodes = new Node*[6];
+	_nodes[0] = n0;
+	_nodes[1] = n1;
+	_nodes[2] = n2;
+	_nodes[3] = n3;
+	_nodes[4] = n4;
+	_nodes[5] = n5;
 
 	this->_volume = this->calcVolume();
 }
@@ -30,9 +36,9 @@ Prism::Prism(Node* n0, Node* n1, Node* n2, Node* n3, Node* n4, Node* n5, size_t
 Prism::Prism(const Prism &prism)
 	: Cell(MshElemType::PRISM, prism.getValue())
 {
-	Node* nodes[6] = { new Node(*prism.getNode(0)), new Node(*prism.getNode(1)), new Node(*prism.getNode(2)), 
-		               new Node(*prism.getNode(3)), new Node(*prism.getNode(4)), new Node(*prism.getNode(5)) };
-	_nodes = nodes;
+	_nodes = new Node*[6];
+	for (size_t i=0; i<6; i++)
+		_nodes[i] = prism._nodes[i];
 	_volume = prism.getVolume();
 }
 
diff --git a/MeshLib/Elements/Pyramid.cpp b/MeshLib/Elements/Pyramid.cpp
index 0a0f1f963e8..ac19c7ab39f 100644
--- a/MeshLib/Elements/Pyramid.cpp
+++ b/MeshLib/Elements/Pyramid.cpp
@@ -13,27 +13,32 @@
 namespace MeshLib {
 
 Pyramid::Pyramid(Node* nodes[5], size_t value)
-	: Cell(nodes, MshElemType::PYRAMID, value)
+	: Cell(MshElemType::PYRAMID, value)
 {
+	_nodes = nodes;
 	this->_volume = this->calcVolume();
 }
 
 Pyramid::Pyramid(Node* n0, Node* n1, Node* n2, Node* n3, Node* n4, size_t value)
 	: Cell(MshElemType::PYRAMID, value)
 {
-	Node* nodes[5] = { n0, n1, n2, n3, n4 };
-	_nodes = nodes;
+	_nodes = new Node*[5];
+	_nodes[0] = n0;
+	_nodes[1] = n1;
+	_nodes[2] = n2;
+	_nodes[3] = n3;
+	_nodes[4] = n4;
 
 	this->_volume = this->calcVolume();
 }
 
-Pyramid::Pyramid(const Pyramid &prism)
-	: Cell(MshElemType::PYRAMID, prism.getValue())
+Pyramid::Pyramid(const Pyramid &pyramid)
+	: Cell(MshElemType::PYRAMID, pyramid.getValue())
 {
-	Node* nodes[5] = { new Node(*prism.getNode(0)), new Node(*prism.getNode(1)), new Node(*prism.getNode(2)), 
-		               new Node(*prism.getNode(3)), new Node(*prism.getNode(4)) };
-	_nodes = nodes;
-	_volume = prism.getVolume();
+	_nodes = new Node*[5];
+	for (size_t i=0; i<5; i++)
+		_nodes[i] = pyramid._nodes[i];
+	_volume = pyramid.getVolume();
 }
 
 Pyramid::~Pyramid()
diff --git a/MeshLib/Elements/Quad.cpp b/MeshLib/Elements/Quad.cpp
index fa037c9f56b..99d99e1bbf2 100644
--- a/MeshLib/Elements/Quad.cpp
+++ b/MeshLib/Elements/Quad.cpp
@@ -13,16 +13,20 @@
 namespace MeshLib {
 
 Quad::Quad(Node* nodes[4], size_t value)
-	: Face(nodes, MshElemType::TRIANGLE, value)
+	: Face(MshElemType::TRIANGLE, value)
 {
+	_nodes = nodes;
 	this->_area = this->calcArea();
 }
 
 Quad::Quad(Node* n0, Node* n1, Node* n2, Node* n3, size_t value)
 	: Face(MshElemType::TRIANGLE, value)
 {
-	Node* nodes[4] = { n0, n1, n2, n3 };
-	_nodes = nodes;
+	_nodes = new Node*[4];
+	_nodes[0] = n0;
+	_nodes[1] = n1;
+	_nodes[2] = n2;
+	_nodes[3] = n3;
 
 	this->_area = this->calcArea();
 }
@@ -30,8 +34,9 @@ Quad::Quad(Node* n0, Node* n1, Node* n2, Node* n3, size_t value)
 Quad::Quad(const Quad &quad)
 	: Face(MshElemType::QUAD, quad.getValue())
 {
-	Node* nodes[4] = { new Node(*quad.getNode(0)), new Node(*quad.getNode(1)), new Node(*quad.getNode(2)), new Node(*quad.getNode(3)) };
-	_nodes = nodes;
+	_nodes = new Node*[4];
+	for (size_t i=0; i<4; i++)
+		_nodes[i] = quad._nodes[i];
 	_area = quad.getArea();
 }
 
diff --git a/MeshLib/Elements/Tet.cpp b/MeshLib/Elements/Tet.cpp
index 901444d373d..9bf8caa0ebf 100644
--- a/MeshLib/Elements/Tet.cpp
+++ b/MeshLib/Elements/Tet.cpp
@@ -13,16 +13,20 @@
 namespace MeshLib {
 
 Tet::Tet(Node* nodes[4], size_t value)
-	: Cell(nodes, MshElemType::TETRAHEDRON, value)
+	: Cell(MshElemType::TETRAHEDRON, value)
 {
+	_nodes = nodes;
 	this->_volume = this->calcVolume();
 }
 
 Tet::Tet(Node* n0, Node* n1, Node* n2, Node* n3, size_t value)
 	: Cell(MshElemType::TETRAHEDRON, value)
 {
-	Node* nodes[4] = { n0, n1, n2, n3 };
-	_nodes = nodes;
+	_nodes = new Node*[4];
+	_nodes[0] = n0;
+	_nodes[1] = n1;
+	_nodes[2] = n2;
+	_nodes[3] = n3;
 
 	this->_volume = this->calcVolume();
 }
@@ -35,8 +39,9 @@ Tet::Tet(size_t value)
 Tet::Tet(const Tet &tet)
 	: Cell(MshElemType::TETRAHEDRON, tet.getValue())
 {
-	Node* nodes[4] = { new Node(*tet.getNode(0)), new Node(*tet.getNode(1)), new Node(*tet.getNode(2)), new Node(*tet.getNode(3)) };
-	_nodes = nodes;
+	_nodes = new Node*[4];
+	for (size_t i=0; i<4; i++)
+		_nodes[i] = tet._nodes[i];
 	_volume = tet.getVolume();
 }
 
diff --git a/MeshLib/Elements/Tet10.cpp b/MeshLib/Elements/Tet10.cpp
index 93881abac93..15c0abb5230 100644
--- a/MeshLib/Elements/Tet10.cpp
+++ b/MeshLib/Elements/Tet10.cpp
@@ -15,27 +15,32 @@ Tet10::Tet10(Node* nodes[10], size_t value)
 {
 	_nodes = nodes;
 	this->_volume = this->calcVolume();
-	_centre_of_gravity = this->calcCoG();
+	this->calcCoG();
 }
 
 Tet10::Tet10(const Tet &tet)
 	: Tet(tet.getValue()), FemElem()
 {
-	Node* nodes[10]; //= { n0, n1, n2, n3 };
-	//TODO: Calculate additional nodes!
-	_nodes = nodes;
+	_nodes = new Node*[10];
+	size_t nNodes (tet.getNNodes());
+	for (size_t i=0; i<nNodes; i++)
+		_nodes[i] = const_cast<Node*>(tet.getNode(i));
+
+	if (nNodes < this->getNNodes())
+	{
+		//TODO: Calculate additional nodes!
+	}
+
 	this->_volume = this->calcVolume();
-	_centre_of_gravity = this->calcCoG();
+	this->calcCoG();
 }
 
 Tet10::Tet10(const Tet10 &tet)
 	: Tet(tet.getValue()), FemElem()
 {
-	Node* nodes[10] = { new Node(*tet.getNode(0)), new Node(*tet.getNode(1)), new Node(*tet.getNode(2)), 
-		                new Node(*tet.getNode(3)), new Node(*tet.getNode(4)), new Node(*tet.getNode(5)), 
-						new Node(*tet.getNode(6)), new Node(*tet.getNode(7)), new Node(*tet.getNode(8)), 
-						new Node(*tet.getNode(9)) };
-	_nodes = nodes;
+	_nodes = new Node*[10];
+	for (size_t i=0; i<10; i++)
+		_nodes[i] = tet._nodes[i];
 	_centre_of_gravity = tet.getCentreOfGravity();
 }
 
@@ -43,11 +48,10 @@ Tet10::~Tet10()
 {
 }
 
-GEOLIB::Point Tet10::calcCoG()
+void Tet10::calcCoG()
 {
-	GEOLIB::Point cog(0,0,0);
 	//TODO calculation!
-	return cog;
+	_centre_of_gravity = 0;
 }
 
 }
diff --git a/MeshLib/Elements/Tet10.h b/MeshLib/Elements/Tet10.h
index 4f9dc7e00a3..e718e359209 100644
--- a/MeshLib/Elements/Tet10.h
+++ b/MeshLib/Elements/Tet10.h
@@ -58,7 +58,7 @@ public:
 
 protected:
 	/// Calculates the volume of a tetrahedron via the determinant of the matrix given by its four points.
-	GEOLIB::Point calcCoG();
+	void calcCoG();
 
 }; /* class */
 
diff --git a/MeshLib/Elements/Tet4.cpp b/MeshLib/Elements/Tet4.cpp
index 95d92adbb80..84d79b57360 100644
--- a/MeshLib/Elements/Tet4.cpp
+++ b/MeshLib/Elements/Tet4.cpp
@@ -13,22 +13,20 @@ namespace MeshLib {
 Tet4::Tet4(Node* nodes[4], size_t value)
 	: Tet(nodes, value), FemElem()
 {
-	_centre_of_gravity = this->calcCoG();
+	this->calcCoG();
 }
 
 Tet4::Tet4(const Tet &tet)
-	: Tet(tet.getValue()), FemElem()
+	: Tet(tet), FemElem()
 {
-	Node* nodes[4] = { new Node(*tet.getNode(0)), new Node(*tet.getNode(1)), new Node(*tet.getNode(2)), new Node(*tet.getNode(3)) };
-	_nodes = nodes;
-	_centre_of_gravity = this->calcCoG();
+	this->calcCoG();
 }
 
 Tet4::Tet4(const Tet4 &tet)
 	: Tet(tet.getValue()), FemElem()
 {
-	Node* nodes[4] = { new Node(*tet.getNode(0)), new Node(*tet.getNode(1)), new Node(*tet.getNode(2)), new Node(*tet.getNode(3)) };
-	_nodes = nodes;
+	for (size_t i=0; i<4; i++)
+		_nodes[i] = tet._nodes[i];
 	_centre_of_gravity = tet.getCentreOfGravity();
 }
 
@@ -36,11 +34,10 @@ Tet4::~Tet4()
 {
 }
 
-GEOLIB::Point Tet4::calcCoG()
+void Tet4::calcCoG()
 {
-	GEOLIB::Point cog(0,0,0);
+	_centre_of_gravity = 0;
 	//TODO calculation!
-	return cog;
 }
 
 }
diff --git a/MeshLib/Elements/Tet4.h b/MeshLib/Elements/Tet4.h
index d2a9ad82c40..3c31b5d7d77 100644
--- a/MeshLib/Elements/Tet4.h
+++ b/MeshLib/Elements/Tet4.h
@@ -51,7 +51,7 @@ public:
 
 protected:
 	/// Calculates the volume of a tetrahedron via the determinant of the matrix given by its four points.
-	GEOLIB::Point calcCoG();
+	void calcCoG();
 
 }; /* class */
 
diff --git a/MeshLib/Elements/Tri.cpp b/MeshLib/Elements/Tri.cpp
index 1ce114fc545..925f45e1b7e 100644
--- a/MeshLib/Elements/Tri.cpp
+++ b/MeshLib/Elements/Tri.cpp
@@ -13,16 +13,19 @@
 namespace MeshLib {
 
 Tri::Tri(Node* nodes[3], size_t value)
-	: Face(nodes, MshElemType::TRIANGLE, value)
+	: Face(MshElemType::TRIANGLE, value)
 {
+	_nodes = nodes;
 	this->_area = this->calcArea();
 }
 
 Tri::Tri(Node* n0, Node* n1, Node* n2, size_t value)
 	: Face(MshElemType::TRIANGLE, value)
 {
-	Node* nodes[3] = { n0, n1, n2 };
-	_nodes = nodes;
+	_nodes = new Node*[3];
+	_nodes[0] = n0;
+	_nodes[1] = n1;
+	_nodes[2] = n2;
 
 	this->_area = this->calcArea();
 }
@@ -30,8 +33,9 @@ Tri::Tri(Node* n0, Node* n1, Node* n2, size_t value)
 Tri::Tri(const Tri &tri)
 	: Face(MshElemType::TRIANGLE, tri.getValue())
 {
-	Node* nodes[3] = { new Node(*tri.getNode(0)), new Node(*tri.getNode(1)), new Node(*tri.getNode(2)) };
-	_nodes = nodes;
+	_nodes = new Node*[3];
+	for (size_t i=0; i<3; i++)
+		_nodes[i] = tri._nodes[i];
 	_area = tri.getArea();
 }
 
-- 
GitLab