From 1125ce9d21b050e4595e90358872c24dbb7606b5 Mon Sep 17 00:00:00 2001
From: Karsten Rink <karsten.rink@ufz.de>
Date: Mon, 27 Aug 2012 11:24:09 +0200
Subject: [PATCH] gmsh import is working now

---
 FemLib/ProcessInfo.h            |  2 +-
 FileIO/MeshIO/GMSHInterface.cpp | 61 ++++++++++++---------------------
 FileIO/MeshIO/GMSHInterface.h   |  5 ++-
 Gui/mainwindow.cpp              | 35 +++++++++----------
 MeshLib/Node.h                  |  4 +--
 5 files changed, 41 insertions(+), 66 deletions(-)

diff --git a/FemLib/ProcessInfo.h b/FemLib/ProcessInfo.h
index 229c9ed8161..876a84983a9 100644
--- a/FemLib/ProcessInfo.h
+++ b/FemLib/ProcessInfo.h
@@ -41,7 +41,7 @@ public:
 	 * @param pcs a pointer to the process
 	 * @return
 	 */
-	ProcessInfo (FiniteElement::ProcessType pcs_type, FiniteElement::PrimaryVariable pcs_pv/*, CRFProcess* pcs*/);
+	ProcessInfo (FiniteElement::ProcessType pcs_type, FiniteElement::PrimaryVariable pcs_pv/* TODO6 , CRFProcess* pcs*/);
 
 	/**
 	 * Sets the process type.
diff --git a/FileIO/MeshIO/GMSHInterface.cpp b/FileIO/MeshIO/GMSHInterface.cpp
index 9d68a46e8b1..3287168a514 100644
--- a/FileIO/MeshIO/GMSHInterface.cpp
+++ b/FileIO/MeshIO/GMSHInterface.cpp
@@ -12,6 +12,7 @@
 #include "swap.h"
 #include "Configure.h"
 #include "BuildInfo.h"
+#include "StringTools.h"
 
 // FileIO
 #include "GMSHInterface.h"
@@ -81,7 +82,7 @@ bool GMSHInterface::isGMSHMeshFile(const std::string& fname)
 	return false;
 }
 
-void GMSHInterface::readGMSHMesh(std::string const& fname, MeshLib::Mesh* mesh)
+MeshLib::Mesh* GMSHInterface::readGMSHMesh(std::string const& fname)
 {
 	std::string line;
 	std::ifstream in(fname.c_str(), std::ios::in);
@@ -103,9 +104,12 @@ void GMSHInterface::readGMSHMesh(std::string const& fname, MeshLib::Mesh* mesh)
 			long id;
 			double x, y, z;
 			in >> n_nodes >> std::ws;
+			nodes.resize(n_nodes);
+			std::map<unsigned, unsigned> id_map;
 			for (size_t i = 0; i < n_nodes; i++) {
 				in >> id >> x >> y >> z >> std::ws;
-				nodes.push_back(new MeshLib::Node(x,y,z,id));
+				id_map.insert(std::map<unsigned, unsigned>::value_type(id, i));
+				nodes[i] = new MeshLib::Node(x,y,z,id);
 			}
 			getline(in, line); // End Node keyword $EndNodes
 
@@ -127,38 +131,41 @@ void GMSHInterface::readGMSHMesh(std::string const& fname, MeshLib::Mesh* mesh)
 				switch (type)
 				{
 					case 1:
-						readNodeIDs(in, 2, node_ids);
+						readNodeIDs(in, 2, node_ids, id_map);
 						elem = new MeshLib::Edge(nodes[node_ids[0]], nodes[node_ids[1]], 0);
 						break;
 					case 2:
-						readNodeIDs(in, 3, node_ids);
+						readNodeIDs(in, 3, node_ids, id_map);
 						elem = new MeshLib::Tri(nodes[node_ids[0]], nodes[node_ids[1]], nodes[node_ids[2]], mat_id);
 						break;
 					case 3:
-						readNodeIDs(in, 4, node_ids);
+						readNodeIDs(in, 4, node_ids, id_map);
 						elem = new MeshLib::Quad(nodes[node_ids[0]], nodes[node_ids[1]], nodes[node_ids[2]], nodes[node_ids[3]], mat_id);
 						break;
 					case 4:
-						readNodeIDs(in, 4, node_ids);
+						readNodeIDs(in, 4, node_ids, id_map);
 						elem = new MeshLib::Tet(nodes[node_ids[0]], nodes[node_ids[1]], nodes[node_ids[2]], nodes[node_ids[3]], mat_id);
 						break;
 					case 5:
-						readNodeIDs(in, 8, node_ids);
+						readNodeIDs(in, 8, node_ids, id_map);
 						elem = new MeshLib::Hex(nodes[node_ids[0]], nodes[node_ids[1]], nodes[node_ids[2]], nodes[node_ids[3]], 
 							                    nodes[node_ids[4]], nodes[node_ids[5]], nodes[node_ids[6]], nodes[node_ids[7]], mat_id);
 						break;
 					case 6:
-						readNodeIDs(in, 6, node_ids);
+						readNodeIDs(in, 6, node_ids, id_map);
 						elem = new MeshLib::Prism(nodes[node_ids[0]], nodes[node_ids[1]], nodes[node_ids[2]],
 							                      nodes[node_ids[3]], nodes[node_ids[4]], nodes[node_ids[5]], mat_id);
 						break;
 					case 7:
-						readNodeIDs(in, 5, node_ids);
+						readNodeIDs(in, 5, node_ids, id_map);
 						elem = new MeshLib::Pyramid(nodes[node_ids[0]], nodes[node_ids[1]], nodes[node_ids[2]],
 							                        nodes[node_ids[3]], nodes[node_ids[4]], mat_id);
 						break;
+					case 15:
+						in >> dummy; // skip rest of line
+						continue;
+						break;
 					default:
-						in >> dummy; // skip rest of line (e.g. for type "15" ... whatever that is ...
 						std::cout << "Error in GMSHInterface::readGMSHMesh() - Unknown element type " << type << "." << std::endl;
 				}
 				in >> std::ws;
@@ -168,45 +175,19 @@ void GMSHInterface::readGMSHMesh(std::string const& fname, MeshLib::Mesh* mesh)
 			}
 
 			getline(in, line); // END keyword
-/* TODO6: testen ob es auch so funktioniert
-			// ordering nodes and closing gaps TK
-			std::vector<size_t> gmsh_id;
-			size_t counter(0);
-			for (size_t i = 0; i < mesh->nod_vector.size(); i++) {
-				const size_t diff = mesh->nod_vector[i]->GetIndex() - counter;
-				if (diff == 0) {
-					gmsh_id.push_back(i);
-					counter++;
-				} else {
-					for (size_t j = 0; j < diff; j++) {
-						gmsh_id.push_back(i);
-						counter++;
-					}
-					i--;
-				}
-			}
-
-			for (size_t i = 0; i < mesh->ele_vector.size(); i++)
-				for (long j = 0; j < mesh->ele_vector[i]->GetVertexNumber(); j++)
-					mesh->ele_vector[i]->getNodeIndices()[j]
-									= gmsh_id[mesh->ele_vector[i]->GetNodeIndex(j) + 1];
-
-			for (size_t i = 0; i < mesh->nod_vector.size(); i++)
-				mesh->nod_vector[i]->SetIndex(i);
-			// END OF: ordering nodes and closing gaps TK
-*/
-		} /*End while*/
+		}
 	}
 	in.close();
+	return new MeshLib::Mesh(BaseLib::getFileNameFromPath(fname), nodes, elements);
 }
 
-void GMSHInterface::readNodeIDs(std::ifstream &in, unsigned n_nodes, std::vector<unsigned> &node_ids)
+void GMSHInterface::readNodeIDs(std::ifstream &in, unsigned n_nodes, std::vector<unsigned> &node_ids, std::map<unsigned, unsigned> &id_map)
 {
 	unsigned idx;
 	for (unsigned i=0; i<n_nodes; i++)
 	{
 		in >> idx;
-		node_ids.push_back(--idx);
+		node_ids.push_back(id_map[idx]);
 	}
 }
 
diff --git a/FileIO/MeshIO/GMSHInterface.h b/FileIO/MeshIO/GMSHInterface.h
index 9b969d2aae0..90f599a9cfa 100644
--- a/FileIO/MeshIO/GMSHInterface.h
+++ b/FileIO/MeshIO/GMSHInterface.h
@@ -72,10 +72,9 @@ public:
 	/**
 	 * reads a mesh created by GMSH - this implementation is based on the former function GMSH2MSH
 	 * @param fname the file name of the mesh (including the path)
-	 * @param mesh the new mesh
 	 * @return
 	 */
-	static void readGMSHMesh (std::string const& fname, MeshLib::Mesh* mesh);
+	static MeshLib::Mesh* readGMSHMesh (std::string const& fname);
 
 protected:
 	int write(std::ostream& stream);
@@ -88,7 +87,7 @@ private:
 	 */
 	void writeGMSHInputFile(std::ostream & out);
 
-	static void readNodeIDs(std::ifstream &in, unsigned n_nodes, std::vector<unsigned> &node_ids);
+	static void readNodeIDs(std::ifstream &in, unsigned n_nodes, std::vector<unsigned> &node_ids, std::map<unsigned, unsigned> &id_map);
 
 	void writePoints(std::ostream& out) const;
 
diff --git a/Gui/mainwindow.cpp b/Gui/mainwindow.cpp
index 1bebccf278b..ef813286d67 100644
--- a/Gui/mainwindow.cpp
+++ b/Gui/mainwindow.cpp
@@ -477,19 +477,12 @@ void MainWindow::loadFile(ImportFileType::type t, const QString &fileName)
 	{
 		if (fi.suffix().toLower() == "gli")
 		{
-#ifndef NDEBUG
-			QTime myTimer0;
-			myTimer0.start();
-#endif
 			std::string unique_name;
 			std::vector<std::string> errors;
 			if (! readGLIFileV4(fileName.toStdString(), _geoModels, unique_name, errors)) {
 				for (size_t k(0); k<errors.size(); k++)
 					OGSError::box(QString::fromStdString(errors[k]));
 			}
-#ifndef NDEBUG
-			std::cout << myTimer0.elapsed() << " ms" << std::endl;
-#endif
 		}
 		else if (fi.suffix().toLower() == "gsp")
 		{
@@ -515,16 +508,19 @@ void MainWindow::loadFile(ImportFileType::type t, const QString &fileName)
 		// OpenGeoSys mesh files
 		else if (fi.suffix().toLower() == "msh")
 		{
+#ifndef NDEBUG
 			QTime myTimer0;
 			myTimer0.start();
-
+#endif
 			FileIO::MeshIO meshIO;
 			std::string name = fileName.toStdString();
 			MeshLib::Mesh* msh = meshIO.loadMeshFromFile(name);
 			if (msh)
 			{
 				_meshModels->addMesh(msh);
+#ifndef NDEBUG
 				std::cout << "Total mesh loading time: " << myTimer0.elapsed() << " ms" << std::endl;
+#endif
 			}
 			else
 				OGSError::box("Failed to load a mesh file.");
@@ -555,8 +551,7 @@ void MainWindow::loadFile(ImportFileType::type t, const QString &fileName)
 	}
 	else if (t == ImportFileType::GMS)
 	{
-		// GMS borehole files
-		if (fi.suffix().toLower() == "txt")
+		if (fi.suffix().toLower() == "txt") // GMS borehole files
 		{
 			std::vector<GeoLib::Point*>* boreholes = new std::vector<GeoLib::Point*>();
 			std::string name = fi.baseName().toStdString();
@@ -566,8 +561,7 @@ void MainWindow::loadFile(ImportFileType::type t, const QString &fileName)
 			else
 				OGSError::box("Error reading GMS file.");
 		}
-		// GMS mesh files
-		else if (fi.suffix().toLower() == "3dm")
+		else if (fi.suffix().toLower() == "3dm") // GMS mesh files
 		{
 			std::string name = fileName.toStdString();
 			MeshLib::Mesh* mesh = GMSInterface::readGMS3DMMesh(name);
@@ -577,17 +571,20 @@ void MainWindow::loadFile(ImportFileType::type t, const QString &fileName)
 	}
 	else if (t == ImportFileType::GMSH)
 	{
-		OGSError::box("Interface not yet integrated");
-		// TODO6
+		std::string msh_name (fileName.toStdString());
+		if (FileIO::GMSHInterface::isGMSHMeshFile (msh_name))
+		{
+			MeshLib::Mesh* mesh = FileIO::GMSHInterface::readGMSHMesh(msh_name);
+			if (mesh)
+				_meshModels->addMesh(mesh);
+			return;
+		}
 	}
-	else if (t == ImportFileType::NETCDF)
+	else if (t == ImportFileType::NETCDF)	// CH  01.2012
 	{
-		// NetCDF files
-		// CH  01.2012
-		std::string name = fileName.toStdString();
 		MeshLib::Mesh* mesh (NULL);
 
-		NetCdfConfigureDialog dlg(name);
+		NetCdfConfigureDialog dlg(fileName.toStdString());
 		dlg.exec();
 		if (dlg.getMesh() != NULL)
 		{
diff --git a/MeshLib/Node.h b/MeshLib/Node.h
index 703afbb8d2c..82565618c3f 100644
--- a/MeshLib/Node.h
+++ b/MeshLib/Node.h
@@ -30,9 +30,7 @@ class Element;
 class Node : public GeoLib::PointWithID
 {
 	/* friend functions: */
-	friend class Mesh;//void Mesh::setElementInformationForNodes();
-	//friend void Mesh::addElement(Element*);
-
+	friend class Mesh;
 
 public:
 	/// Constructor using a coordinate array
-- 
GitLab