From b65ad8c1fa04658ab59bae4f130175d9afb5b980 Mon Sep 17 00:00:00 2001
From: Karsten Rink <karsten.rink@ufz.de>
Date: Tue, 18 Mar 2014 12:05:48 +0100
Subject: [PATCH] added possible signature and allowing for different number of
 rows and columns

---
 Gui/mainwindow.cpp                       |  3 ++
 MeshLib/MeshGenerators/MeshGenerator.cpp | 48 +++++++++++++-----------
 MeshLib/MeshGenerators/MeshGenerator.h   | 36 +++++++++++++++++-
 3 files changed, 65 insertions(+), 22 deletions(-)

diff --git a/Gui/mainwindow.cpp b/Gui/mainwindow.cpp
index 85198b7f265..e41dced3544 100644
--- a/Gui/mainwindow.cpp
+++ b/Gui/mainwindow.cpp
@@ -11,6 +11,7 @@
  *              http://www.opengeosys.org/project/license
  *
  */
+#include "MeshGenerators\MeshGenerator.h"
 
 #include "Configure.h"
 #include "mainwindow.h"
@@ -1284,6 +1285,8 @@ void MainWindow::showDataExplorerSettingsDialog()
 
 void MainWindow::FEMTestStart()
 {
+	_meshModels->addMesh( MeshLib::MeshGenerator::generateRegularQuadMesh(10, 10) );
+
 }
 
 
diff --git a/MeshLib/MeshGenerators/MeshGenerator.cpp b/MeshLib/MeshGenerators/MeshGenerator.cpp
index 5093e719c96..f8ed1e60509 100644
--- a/MeshLib/MeshGenerators/MeshGenerator.cpp
+++ b/MeshLib/MeshGenerators/MeshGenerator.cpp
@@ -55,43 +55,49 @@ Mesh* MeshGenerator::generateRegularQuadMesh(
         const std::size_t subdivision,
         const GeoLib::Point& origin)
 {
-	const double dx = length / subdivision;
+	return generateRegularQuadMesh(subdivision, subdivision, length/subdivision, origin);
+}
 
+Mesh* MeshGenerator::generateRegularQuadMesh(const unsigned n_x_cells,
+	                          const unsigned n_y_cells,
+                              const double   cell_size,
+                              GeoLib::Point const& origin,
+							  std::string   const& mesh_name)
+{
 	//nodes
-	const std::size_t n_nodes = subdivision + 1;
-	std::vector<Node*> nodes(n_nodes * n_nodes);
+	const unsigned n_x_nodes (n_x_cells+1);
+	const unsigned n_y_nodes (n_y_cells+1);
+	std::vector<Node*> nodes;
+	nodes.reserve(n_x_nodes * n_y_nodes);
+	
 
-	for (std::size_t i = 0, node_id = 0; i < n_nodes; i++)
-		for (std::size_t j = 0; j < n_nodes; j++)
-		{
-			nodes[node_id] = new Node(origin[0] + dx * j,
-									  origin[1] + dx * i,
-									  origin[2],
-									  node_id);
-			node_id++;
-		}
+	for (std::size_t i = 0, node_id = 0; i < n_y_nodes; i++)
+	{
+		const double x_offset (origin[0] + cell_size * i);
+		for (std::size_t j = 0; j < n_x_nodes; j++)
+			nodes.push_back (new Node(origin[1] + cell_size * j, x_offset, origin[2]));
+	}
 
 	//elements
-	std::size_t const n_eles = subdivision;
-	std::vector<Element*> elements(n_eles * n_eles);
-	std::size_t elem_id = 0;
+	std::vector<Element*> elements;
+	elements.reserve(n_x_cells * n_y_cells);
 
-	for (std::size_t j = 0; j < subdivision; j++)
+	for (std::size_t j = 0; j < n_y_cells; j++)
 	{
-		const std::size_t offset_y1 = j * n_nodes;
-		const std::size_t offset_y2 = (j + 1) * n_nodes;
-		for (std::size_t k = 0; k < subdivision; k++)
+		const std::size_t offset_y1 = j * n_x_nodes;
+		const std::size_t offset_y2 = (j + 1) * n_x_nodes;
+		for (std::size_t k = 0; k < n_x_cells; k++)
 		{
 			std::array<Node*, 4> element_nodes;
 			element_nodes[0] = nodes[offset_y1 + k];
 			element_nodes[1] = nodes[offset_y1 + k + 1];
 			element_nodes[2] = nodes[offset_y2 + k + 1];
 			element_nodes[3] = nodes[offset_y2 + k];
-			elements[elem_id++] = new Quad(element_nodes);
+			elements.push_back (new Quad(element_nodes));
 		}
 	}
 
-	return new Mesh("mesh", nodes, elements);
+	return new Mesh(mesh_name, nodes, elements);
 }
 
 Mesh* MeshGenerator::generateRegularHexMesh(
diff --git a/MeshLib/MeshGenerators/MeshGenerator.h b/MeshLib/MeshGenerators/MeshGenerator.h
index cfefa948ef7..4f159865630 100644
--- a/MeshLib/MeshGenerators/MeshGenerator.h
+++ b/MeshLib/MeshGenerators/MeshGenerator.h
@@ -14,6 +14,8 @@
 #ifndef MESHGENERATOR_H_
 #define MESHGENERATOR_H_
 
+#include <string>
+
 #include "GeoLib/Point.h"
 #include "MeshLib/Mesh.h"
 
@@ -26,7 +28,7 @@ namespace MeshGenerator
  *
  * \param length Mesh's length in x-direction.
  * \param subdivision Number of subdivisions.
- * \param origin Optional mesh's origin (the most left point) with GeoLib::ORIGIN default.
+ * \param origin Optional mesh's origin (the left-most point) with GeoLib::ORIGIN default.
  */
 Mesh* generateLineMesh(const double length,
                        const std::size_t subdivision,
@@ -44,6 +46,21 @@ Mesh* generateRegularQuadMesh(const double length,
                               const std::size_t subdivision,
                               GeoLib::Point const& origin = GeoLib::ORIGIN);
 
+/**
+ * Generate a regular 2D Quad-Element mesh. The mesh is generated in the
+ * x-y-plane.
+ *
+ * \param n_x_cells Number of cells in x-direction.
+ * \param n_y_cells Number of cells in y-direction.
+ * \param cell_size Edge length of Quad elements
+ * \param origin Optional mesh's origin (the lower left corner) with GeoLib::ORIGIN default.
+ */
+Mesh* generateRegularQuadMesh(const unsigned n_x_cells,
+	                          const unsigned n_y_cells,
+                              const double   cell_size,
+                              GeoLib::Point const& origin = GeoLib::ORIGIN,
+							  std::string   const& mesh_name = "mesh");
+
 /**
  * Generate a regular 3D Hex-Element mesh.
  *
@@ -54,6 +71,23 @@ Mesh* generateRegularQuadMesh(const double length,
 Mesh* generateRegularHexMesh(const double length,
                               const std::size_t subdivision,
                               GeoLib::Point const& origin = GeoLib::ORIGIN);
+
+/**
+ * Generate a regular 3D Hex-Element mesh.
+ *
+ * \param n_x_cells Number of cells in x-direction.
+ * \param n_y_cells Number of cells in y-direction.
+ * \param n_z_cells Number of cells in z-direction.
+ * \param cell_size Edge length of Hex elements
+ * \param origin Optional mesh's origin (the lower left corner) with GeoLib::ORIGIN default.
+ */
+Mesh* generateRegularQuadMesh(const unsigned n_x_cells,
+	                          const unsigned n_y_cells,
+							  const unsigned n_z_cells,
+                              const double   cell_size,
+                              GeoLib::Point const& origin = GeoLib::ORIGIN);
+
+
 }  //MeshGenerator
 } //MeshLib
 
-- 
GitLab