From 0ee128c582a2f77b80da7470542bfae7ef9db3bd Mon Sep 17 00:00:00 2001
From: Oliver Sander <oliver.sander@tu-dresden.de>
Date: Sat, 30 Jan 2016 15:20:59 +0100
Subject: [PATCH] Add a class that reads grids from VTK files

This is an initial version; it currently only works for a very limited set of
cases.  I tested it for triangle grids, and that works.
---
 dune/gfe/vtkreader.hh | 54 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 dune/gfe/vtkreader.hh

diff --git a/dune/gfe/vtkreader.hh b/dune/gfe/vtkreader.hh
new file mode 100644
index 00000000..37954a97
--- /dev/null
+++ b/dune/gfe/vtkreader.hh
@@ -0,0 +1,54 @@
+// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+// vi: set et ts=4 sw=2 sts=2:
+#ifndef DUNE_GFE_VTKREADER_HH
+#define DUNE_GFE_VTKREADER_HH
+
+#include <memory>
+
+#include <dune/gfe/vtkfile.hh>
+
+/** \brief Read a grid from a VTK file
+ */
+template <class GridType>
+class VTKReader
+{
+public:
+
+  /** \brief Read a grid from a VTK file */
+  static std::unique_ptr<GridType> read(std::string filename)
+  {
+    constexpr auto dimworld = GridType::dimensionworld;
+
+    Dune::GFE::VTKFile vtkFile;
+
+    // Read test file from disk
+    vtkFile.read(filename);
+
+    Dune::GridFactory<GridType> factory;
+
+    for (const auto& v : vtkFile.points_)
+    {
+      // use the first dimworld components as vertex coordinate; discard the rest
+      Dune::FieldVector<typename GridType::ctype, dimworld> pos;
+      for (int i=0; i<dimworld; i++)
+        pos[i] = v[i];
+      factory.insertVertex(pos);
+    }
+
+    Dune::GeometryType triangle;
+    triangle.makeTriangle();
+
+    for (int i=0; i<vtkFile.cellConnectivity_.size(); i+=3)
+    {
+      factory.insertElement(triangle, {vtkFile.cellConnectivity_[i],
+                                       vtkFile.cellConnectivity_[i+1],
+                                       vtkFile.cellConnectivity_[i+2]});
+
+    }
+
+    return std::unique_ptr<GridType>(factory.createGrid());
+  }
+
+};
+
+#endif
\ No newline at end of file
-- 
GitLab