diff --git a/FileIO/CMakeLists.txt b/FileIO/CMakeLists.txt index 521a7681ec3f3a136ce6dac2ea53f80bab4be174..4e5dd80bf2dffc124265bf49277644fba8288528 100644 --- a/FileIO/CMakeLists.txt +++ b/FileIO/CMakeLists.txt @@ -1,13 +1,18 @@ # Source files # GET_SOURCE_FILES(SOURCES_FILEIO) SET( SOURCES - Gmsh2GeoIO.cpp + GMSInterface.h GMSInterface.cpp + PetrelInterface.h PetrelInterface.cpp + readMeshFromFile.h readMeshFromFile.cpp + readNonBlankLineFromInputStream.h readNonBlankLineFromInputStream.cpp + Writer.h Writer.cpp ) + GET_SOURCE_FILES(SOURCES_LEGACY Legacy) GET_SOURCE_FILES(SOURCES_MESHIO MeshIO) GET_SOURCE_FILES(SOURCES_RAPID_XML RapidXmlIO) diff --git a/FileIO/Gmsh2GeoIO.cpp b/FileIO/Gmsh2GeoIO.cpp deleted file mode 100644 index 976301e0daf2cd1c59e8f48508bb540ed30b02bb..0000000000000000000000000000000000000000 --- a/FileIO/Gmsh2GeoIO.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/** - * \file - * \author Thomas Fischer - * \date 2011-08-18 - * \brief Implementation of the Gmsh2GeoIO class. - * - * \copyright - * Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org) - * Distributed under a Modified BSD License. - * See accompanying file LICENSE.txt or - * http://www.opengeosys.org/project/license - * - * \file Gmsh2GeoIO.cpp - * - * Created on 2011-08-18 by Thomas Fischer - */ - -#include <fstream> -#include <vector> - -// ThirdParty/logog -#include "logog/include/logog.hpp" - -// BaseLib -#include "StringTools.h" - -#include "GEOObjects.h" -#include "Gmsh2GeoIO.h" - -namespace FileIO -{ -void Gmsh2GeoIO::loadMeshAsGeometry (std::string & fname, GeoLib::GEOObjects* geo) -{ - // open file - std::ifstream ins (fname.c_str()); - if (!ins) - { - WARN("Gmsh2GeoIO::loadMeshAsGeometry(): could not open file %s", fname.c_str()); - return; - } - - std::string line; - // read gmsh header - getline (ins, line); // $MeshFormat - getline (ins, line); - getline (ins, line); // $EndMeshFormat - - // read nodes tag - getline (ins, line); - // read number of nodes - getline (ins, line); - const size_t n_pnts (BaseLib::str2number<size_t>(line)); - std::vector<GeoLib::Point*>* pnts (new std::vector<GeoLib::Point*>); - for (size_t k(0); k < n_pnts; k++) - { - getline (ins, line); - // parse id - size_t pos_beg(0); - size_t pos_end (line.find(" ")); - // the sub string line.substr(pos_beg, pos_end-pos_beg) represents the id - // parse x coordinate - pos_beg = pos_end + 1; - pos_end = line.find(" ", pos_beg); - double x (BaseLib::str2number<double>(line.substr(pos_beg, pos_end - pos_beg))); - // parse y coordinate - pos_beg = pos_end + 1; - pos_end = line.find(" ", pos_beg); - double y (BaseLib::str2number<double>(line.substr(pos_beg, pos_end - pos_beg))); - // parse z coordinate - pos_beg = pos_end + 1; - pos_end = line.find("\n", pos_beg); - double z (BaseLib::str2number<double>(line.substr(pos_beg, pos_end - pos_beg))); - - pnts->push_back (new GeoLib::Point (x,y,z)); - } - // read end nodes tag - getline (ins, line); - - geo->addPointVec (pnts, fname); - - std::vector<size_t> const& pnt_id_map (geo->getPointVecObj(fname)->getIDMap()); - // read element tag - getline (ins, line); - // read number of elements - getline (ins, line); - const size_t n_elements (BaseLib::str2number<size_t>(line)); - GeoLib::Surface* sfc (new GeoLib::Surface (*pnts)); - for (size_t k(0); k < n_elements; k++) - { - getline (ins, line); - // parse id - size_t pos_beg(0); - size_t pos_end (line.find(" ")); - // the sub string line.substr(pos_beg, pos_end-pos_beg) represents the id - // parse element type - pos_beg = pos_end + 1; - pos_end = line.find(" ", pos_beg); - size_t ele_type (BaseLib::str2number<size_t>(line.substr(pos_beg, pos_end - pos_beg))); - if (ele_type == 2) // read 3 node triangle - { // parse number of tags - pos_beg = pos_end + 1; - pos_end = line.find(" ", pos_beg); - const size_t n_tags (BaseLib::str2number<size_t>(line.substr(pos_beg, - pos_end - pos_beg))); - // (over) read tags - for (size_t j(0); j < n_tags; j++) - { - pos_beg = pos_end + 1; - pos_end = line.find(" ", pos_beg); - } - // parse first id of triangle - pos_beg = pos_end + 1; - pos_end = line.find(" ", pos_beg); - const size_t id0 (BaseLib::str2number<size_t>(line.substr(pos_beg, - pos_end - pos_beg)) - 1); // shift -1! - // parse second id of triangle - pos_beg = pos_end + 1; - pos_end = line.find(" ", pos_beg); - const size_t id1 (BaseLib::str2number<size_t>(line.substr(pos_beg, - pos_end - pos_beg)) - 1); // shift -1! - // parse third id of triangle - pos_beg = pos_end + 1; - pos_end = line.find(" ", pos_beg); - const size_t id2 (BaseLib::str2number<size_t>(line.substr(pos_beg, - pos_end - pos_beg)) - 1); // shift -1! - sfc->addTriangle (pnt_id_map[id0], pnt_id_map[id1], pnt_id_map[id2]); - } - } - // read end element tag - getline (ins, line); - - std::vector<GeoLib::Surface*>* sfcs (new std::vector<GeoLib::Surface*>); - sfcs->push_back(sfc); - geo->addSurfaceVec (sfcs, fname); -} -} // end namespace FileIO diff --git a/FileIO/Gmsh2GeoIO.h b/FileIO/Gmsh2GeoIO.h deleted file mode 100644 index 4ba7afbb412f5dda8f1de6915765be7ae456f591..0000000000000000000000000000000000000000 --- a/FileIO/Gmsh2GeoIO.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * \file - * \author Thomas Fischer - * \date 2011-08-18 - * \brief Definition of the Gmsh2GeoIO class. - * - * \copyright - * Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org) - * Distributed under a Modified BSD License. - * See accompanying file LICENSE.txt or - * http://www.opengeosys.org/project/license - * - * \file Gmsh2GeoIO.h - * - * Created on 2011-08-18 by Thomas Fischer - */ - -#ifndef GMSH2GEOIO_H_ -#define GMSH2GEOIO_H_ - -#include <string> - -namespace GeoLib -{ -class GEOObjects; -} - -namespace FileIO -{ -class Gmsh2GeoIO -{ -public: - /** - * load a surface mesh (gmsh format) as a geometric surface - * @param fname file name of the surface mesh - * @param geo the object that manages all geometries, - * new surface will be put into this container - */ - static void loadMeshAsGeometry (std::string & fname, GeoLib::GEOObjects* geo); -}; -} // end namespace FileIO - -#endif /* GMSH2GEOIO_H_ */ diff --git a/Gui/DataView/MshView.cpp b/Gui/DataView/MshView.cpp index 6c5aa57bab5715ae693f3d0d7a334f06df644892..1717f296bca2dba47a89fc45b3ee00a70141f107 100644 --- a/Gui/DataView/MshView.cpp +++ b/Gui/DataView/MshView.cpp @@ -122,11 +122,16 @@ void MshView::contextMenuEvent( QContextMenuEvent* event ) QAction* surfaceMeshAction (NULL); if (mesh_dim==3) surfaceMeshAction = menu.addAction("Extract surface"); + QAction* mesh2geoAction (NULL); QAction* shapeExportAction (NULL); -#ifdef Shapelib_FOUND if (mesh_dim==2) + { + mesh2geoAction = menu.addAction("Convert to geometry"); +#ifdef Shapelib_FOUND shapeExportAction = menu.addAction("Export to Shapefile..."); #endif + } + menu.addSeparator(); menu.addMenu(&direct_cond_menu); QAction* addDirectAction = direct_cond_menu.addAction("Add..."); @@ -139,10 +144,13 @@ void MshView::contextMenuEvent( QContextMenuEvent* event ) connect(surfaceMeshAction, SIGNAL(triggered()), this, SLOT(extractSurfaceMesh())); connect(addDirectAction, SIGNAL(triggered()), this, SLOT(addDIRECTSourceTerms())); connect(loadDirectAction, SIGNAL(triggered()), this, SLOT(loadDIRECTSourceTerms())); -#ifdef Shapelib_FOUND if (mesh_dim==2) + { + connect(mesh2geoAction, SIGNAL(triggered()), this, SLOT(convertMeshToGeometry())); +#ifdef Shapelib_FOUND connect(shapeExportAction, SIGNAL(triggered()), this, SLOT(exportToShapefile())); #endif + } menu.exec(event->globalPos()); } } @@ -183,6 +191,13 @@ void MshView::extractSurfaceMesh() static_cast<MshModel*>(this->model())->addMesh( MeshLib::MeshSurfaceExtraction::getMeshSurface(*mesh, dir) ); } +void MshView::convertMeshToGeometry() +{ + QModelIndex index = this->selectionModel()->currentIndex(); + const MeshLib::Mesh* mesh = static_cast<MshModel*>(this->model())->getMesh(index); + emit requestMeshToGeometryConversion(mesh); +} + #ifdef Shapelib_FOUND void MshView::exportToShapefile() const { diff --git a/Gui/DataView/MshView.h b/Gui/DataView/MshView.h index 2f629e93f722cde5f25b6e276d0a8d67e0f63494..9457096fafae5c52e192a82a85e21b73371e1b87 100644 --- a/Gui/DataView/MshView.h +++ b/Gui/DataView/MshView.h @@ -75,6 +75,8 @@ private slots: void loadDIRECTSourceTerms(); + void convertMeshToGeometry(); + #ifdef Shapelib_FOUND void exportToShapefile() const; #endif @@ -99,6 +101,7 @@ signals: void removeSelectedMeshComponent(); void requestCondSetupDialog(const std::string&, const GeoLib::GEOTYPE, const std::size_t, bool on_points); void requestMeshRemoval(const QModelIndex&); + void requestMeshToGeometryConversion(const MeshLib::Mesh*); void requestDIRECTSourceTerms(const std::string, const std::vector<GeoLib::Point*>*); void saveMeshAction(); diff --git a/Gui/mainwindow.cpp b/Gui/mainwindow.cpp index 4c64b24e65e1d8eb15bcb23aa75f9d8b578d675b..cce67b3269daa4080006531be59c8268c71716ab 100644 --- a/Gui/mainwindow.cpp +++ b/Gui/mainwindow.cpp @@ -68,7 +68,6 @@ // FileIO includes // TODO6 #include "FEFLOWInterface.h" #include "GMSInterface.h" -#include "Gmsh2GeoIO.h" #include "Legacy/MeshIO.h" #include "Legacy/OGSIOVer4.h" #include "MeshIO/GMSHInterface.h" @@ -87,6 +86,7 @@ #include "Elements/Element.h" #include "MeshSurfaceExtraction.h" #include "readMeshFromFile.h" +#include "convertMeshToGeo.h" // Qt includes #include <QDesktopWidget> @@ -197,6 +197,8 @@ MainWindow::MainWindow(QWidget* parent /* = 0*/) _elementModel, SLOT(clearView())); connect(mshTabWidget->treeView, SIGNAL(qualityCheckRequested(VtkMeshSource*)), this, SLOT(showMshQualitySelectionDialog(VtkMeshSource*))); + connect(mshTabWidget->treeView, SIGNAL(requestMeshToGeometryConversion(const MeshLib::Mesh*)), + this, SLOT(convertMeshToGeometry(const MeshLib::Mesh*))); connect(mshTabWidget->treeView, SIGNAL(requestCondSetupDialog(const std::string&, const GeoLib::GEOTYPE, std::size_t, bool)), this, SLOT(showCondSetupDialog(const std::string&, const GeoLib::GEOTYPE, std::size_t, bool))); connect(mshTabWidget->treeView, SIGNAL(elementSelected(vtkUnstructuredGridAlgorithm const*const, unsigned, bool)), @@ -207,10 +209,10 @@ MainWindow::MainWindow(QWidget* parent /* = 0*/) mshTabWidget->elementView, SLOT(updateView())); connect(mshTabWidget->treeView, SIGNAL(elementSelected(vtkUnstructuredGridAlgorithm const*const, unsigned, bool)), (QObject*) (visualizationWidget->interactorStyle()), SLOT(removeHighlightActor())); - connect(mshTabWidget->elementView, SIGNAL(nodeSelected(vtkUnstructuredGridAlgorithm const*const, unsigned, bool)), - (QObject*) (visualizationWidget->interactorStyle()), SLOT(removeHighlightActor())); connect(mshTabWidget->treeView, SIGNAL(removeSelectedMeshComponent()), _vtkVisPipeline, SLOT(removeHighlightedMeshComponent())); + connect(mshTabWidget->elementView, SIGNAL(nodeSelected(vtkUnstructuredGridAlgorithm const*const, unsigned, bool)), + (QObject*) (visualizationWidget->interactorStyle()), SLOT(removeHighlightActor())); connect(mshTabWidget->elementView, SIGNAL(nodeSelected(vtkUnstructuredGridAlgorithm const*const, unsigned, bool)), _vtkVisPipeline, SLOT(highlightMeshComponent(vtkUnstructuredGridAlgorithm const*const, unsigned, bool))); connect(mshTabWidget->elementView, SIGNAL(removeSelectedMeshComponent()), @@ -957,6 +959,11 @@ void MainWindow::mapGeometry(const std::string &geo_name) } } +void MainWindow::convertMeshToGeometry(const MeshLib::Mesh* mesh) +{ + MeshLib::convertMeshToGeo(*mesh, this->_geoModels); +} + void MainWindow::exportBoreholesToGMS(std::string listName, std::string fileName) { diff --git a/Gui/mainwindow.h b/Gui/mainwindow.h index 1b65cb513072fbafe6d1d65e4c179aebd5d6b846..7116d220a05387786f2dbdce6ad5681f67fc8c8b 100644 --- a/Gui/mainwindow.h +++ b/Gui/mainwindow.h @@ -76,6 +76,7 @@ protected slots: void loadPetrelFiles(); void loadFEMConditions(std::string geoName); void mapGeometry(const std::string &geo_name); + void convertMeshToGeometry(const MeshLib::Mesh* mesh); void openRecentFile(); void about(); void showAddPipelineFilterItemDialog(QModelIndex parentIndex); diff --git a/MeshLib/convertMeshToGeo.cpp b/MeshLib/convertMeshToGeo.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff46ecc90a840cf13ba4c78ff6c6f33bbe7978d9 --- /dev/null +++ b/MeshLib/convertMeshToGeo.cpp @@ -0,0 +1,75 @@ +/** + * \file + * \author Karsten Rink + * \date 2013-07-05 + * \brief Implementation of of mesh to geometry conversion. + * + * \copyright + * Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#include "convertMeshToGeo.h" + +// ThirdParty/logog +#include "logog/include/logog.hpp" + +#include "GEOObjects.h" +#include "Surface.h" + +#include "Mesh.h" +#include "Elements/Tri.h" +#include "Elements/Quad.h" +#include "Node.h" + +namespace MeshLib { + +bool convertMeshToGeo(const MeshLib::Mesh &mesh, GeoLib::GEOObjects* geo_objects) +{ + if (mesh.getDimension() != 2) + { + ERR ("Mesh to geometry conversion is only working for 2D meshes."); + return false; + } + + // nodes to points conversion + const std::size_t nNodes (mesh.getNNodes()); + std::vector<GeoLib::Point*> *points = new std::vector<GeoLib::Point*>(nNodes); + const std::vector<MeshLib::Node*> &nodes = mesh.getNodes(); + + for (unsigned i=0; i<nNodes; ++i) + (*points)[i] = new GeoLib::Point(static_cast<GeoLib::Point>(*nodes[i])); + + // elements to surface triangles conversion + const std::vector<MeshLib::Element*> &elements = mesh.getElements(); + GeoLib::Surface* sfc = new GeoLib::Surface(*points); + const std::size_t nElems (mesh.getNElements()); + + for (unsigned i=0; i<nElems; ++i) + { + MeshLib::Element* e (elements[i]); + if (e->getGeomType() == MshElemType::TRIANGLE) + sfc->addTriangle(e->getNodeIndex(0), e->getNodeIndex(1), e->getNodeIndex(2)); + if (e->getGeomType() == MshElemType::QUAD) + { + sfc->addTriangle(e->getNodeIndex(0), e->getNodeIndex(1), e->getNodeIndex(2)); + sfc->addTriangle(e->getNodeIndex(0), e->getNodeIndex(2), e->getNodeIndex(3)); + } + // all other element types are ignored (i.e. lines) + } + + std::vector<GeoLib::Surface*> *sfcs = new std::vector<GeoLib::Surface*>(1); + (*sfcs)[0] = sfc; + + std::string mesh_name (mesh.getName()); + geo_objects->addPointVec(points, mesh_name); + geo_objects->addSurfaceVec(sfcs, mesh_name); + return true; +} + + +} + diff --git a/MeshLib/convertMeshToGeo.h b/MeshLib/convertMeshToGeo.h new file mode 100644 index 0000000000000000000000000000000000000000..2953460aa6526ff203b35999e2994dcf18f9d50b --- /dev/null +++ b/MeshLib/convertMeshToGeo.h @@ -0,0 +1,39 @@ +/** + * \file + * \author Karsten Rink + * \date 2013-07-05 + * \brief Definition of mesh to geometry conversion. + * + * \copyright + * Copyright (c) 2013, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + * + */ + +#ifndef CONVERTMESHTOGEO_H_ +#define CONVERTMESHTOGEO_H_ + +namespace GeoLib +{ +class GEOObjects; +} + +namespace MeshLib +{ + + class Mesh; + + /** + * Converts a 2D mesh into a geometry. + * A new geometry with the name of the mesh will be inserted into geo_objects, consisting + * of points identical with mesh nodes and one surface representing the mesh. Triangles are + * converted to geometric triangles, quads are split into two triangles, all other elements + * are ignored. + */ + bool convertMeshToGeo(const MeshLib::Mesh &mesh, GeoLib::GEOObjects* geo_objects); + +} // namespace + +#endif /* CONVERTMESHTOGEO_H_ */