diff --git a/BaseLib/StringTools.cpp b/BaseLib/StringTools.cpp index 3fdc8c31c888ca0ce8bc5c109ec77c774e1919c1..709b1645c3591e3f7b75c9f4473946a69bf4a727 100644 --- a/BaseLib/StringTools.cpp +++ b/BaseLib/StringTools.cpp @@ -65,6 +65,12 @@ std::string getFileNameFromPath(const std::string &str, bool with_extension) return file.substr(0,end); } +std::string getSuffixFromPath(const std::string &str) +{ + std::string::size_type beg (str.find_last_of('.')); + return str.substr(beg+1, str.length()-beg-1); +} + std::string copyPathToFileName(const std::string &file_name, const std::string &source) { // check if file_name already contains a full path diff --git a/BaseLib/StringTools.h b/BaseLib/StringTools.h index 81683a79c63b927c75cd256f09b890d5a00c9d50..3f11c0fbbf224cfd5139d7651de5dfc253a98dce 100644 --- a/BaseLib/StringTools.h +++ b/BaseLib/StringTools.h @@ -76,6 +76,12 @@ namespace BaseLib { */ std::string getFileNameFromPath(const std::string &str, bool with_extension = false); +/** + * Extract the file type / suffix from a path + */ +std::string getSuffixFromPath(const std::string &str); + + /** * Checks if file_name already contains a qualified path and if not copies the path from source. */ diff --git a/FileIO/CMakeLists.txt b/FileIO/CMakeLists.txt index ebbe4712691076cced51b6bc9273c2ddf7419c28..ec8801dd3a398c6c83a6b10a635882bbe17bf8e9 100644 --- a/FileIO/CMakeLists.txt +++ b/FileIO/CMakeLists.txt @@ -4,6 +4,7 @@ SET( SOURCES Gmsh2GeoIO.cpp GMSInterface.cpp PetrelInterface.cpp + readMeshFromFile.cpp readNonBlankLineFromInputStream.cpp Writer.cpp ) diff --git a/FileIO/XmlIO/VTKInterface.cpp b/FileIO/XmlIO/VTKInterface.cpp index 160d3efba64cb3869fa5e7894c4c0bba816823c0..4fd735e43fbe894ae1798f716f5c2b13f4f88de9 100644 --- a/FileIO/XmlIO/VTKInterface.cpp +++ b/FileIO/XmlIO/VTKInterface.cpp @@ -45,10 +45,11 @@ VTKInterface::~VTKInterface() MeshLib::Mesh* VTKInterface::readVTUFile(const std::string &file_name) { + std::cout << "Reading OGS legacy mesh ... " << std::endl; std::ifstream in(file_name.c_str()); if (in.fail()) { - std::cout << "VTKInterface::readVTUFile() - Can't open xml-file." << std::endl; + std::cout << "\nVTKInterface::readVTUFile() - Can't open xml-file." << std::endl; return NULL; } @@ -172,6 +173,9 @@ MeshLib::Mesh* VTKInterface::readVTUFile(const std::string &file_name) return NULL; } + std::cout << "finished." << std::endl; + std::cout << "Nr. Nodes: " << nodes.size() << std::endl; + std::cout << "Nr. Elements: " << elements.size() << std::endl; return new MeshLib::Mesh(BaseLib::getFileNameFromPath(file_name), nodes, elements); } else diff --git a/FileIO/readMeshFromFile.cpp b/FileIO/readMeshFromFile.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3906f6db59bc3d41b9aff03d9dc9bf45a57c8bd7 --- /dev/null +++ b/FileIO/readMeshFromFile.cpp @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2012, 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 readMeshFromFile.cpp + * + * Created on 2012-09-27 by Karsten Rink + */ + +#include "readMeshFromFile.h" +#include "StringTools.h" +#include "Mesh.h" +#include "XmlIO/VTKInterface.h" +#include "Legacy/MeshIO.h" + +namespace FileIO { + +MeshLib::Mesh* readMeshFromFile(const std::string &file_name) +{ + MeshLib::Mesh* mesh (NULL); + std::string suffix (BaseLib::getSuffixFromPath(file_name)); + + if (suffix.compare("msh") == 0 || suffix.compare("MSH") == 0) + { + FileIO::MeshIO meshIO; + mesh = meshIO.loadMeshFromFile(file_name); + } + else if (suffix.compare("vtu") || suffix.compare("VTU") == 0) + mesh = FileIO::VTKInterface::readVTUFile(file_name); + else + std::cout << "Unknown mesh file format" << std::endl; + + return mesh; +} + +} \ No newline at end of file diff --git a/FileIO/readMeshFromFile.h b/FileIO/readMeshFromFile.h new file mode 100644 index 0000000000000000000000000000000000000000..0b5e58d5e85efb0892e76b89f5a2037be2024e22 --- /dev/null +++ b/FileIO/readMeshFromFile.h @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2012, 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 readMeshFromFile.h + * + * Created on 2012-09-27 by Karsten Rink + */ + +#ifndef READMESHFROMFILE_H +#define READMESHFROMFILE_H + +#include <string> + +namespace MeshLib { class Mesh; } + +namespace FileIO { + MeshLib::Mesh* readMeshFromFile(const std::string &file_name); +} + +#endif // READMESHFROMFILE_H \ No newline at end of file diff --git a/Gui/DataView/CMakeLists.txt b/Gui/DataView/CMakeLists.txt index 71b27bd7e0f282dd18c2de34a982ec05b825449c..fb0e89bd894347f2f8e2be220de07b8abd36e063 100644 --- a/Gui/DataView/CMakeLists.txt +++ b/Gui/DataView/CMakeLists.txt @@ -45,7 +45,6 @@ set( MOC_HEADERS DataView.h ElementTreeModel.h FEMConditionSetupDialog.h - GeoMapper.h GEOModels.h GeoTabWidget.h GeoTreeModel.h @@ -76,6 +75,7 @@ set( HEADERS CondItem.h DirectConditionGenerator.h CondObjectListItem.h + GeoMapper.h GeoObjectListItem.h GeoTreeItem.h ModelTreeItem.h diff --git a/Gui/DataView/GeoMapper.cpp b/Gui/DataView/GeoMapper.cpp index 4a0a0f71d9a68e8d04328980b8b23ca8d9c35622..17d92a8e2de2e22d8a3fd30f8c0c21f757fce1c3 100644 --- a/Gui/DataView/GeoMapper.cpp +++ b/Gui/DataView/GeoMapper.cpp @@ -16,12 +16,12 @@ #include "MshEditor.h" #include "PointWithID.h" #include "VtkRaster.h" -#include "Legacy/MeshIO.h" +#include "readMeshFromFile.h" GeoMapper::GeoMapper(GeoLib::GEOObjects &geo_objects, const std::string &geo_name) : _geo_objects(geo_objects), _geo_name(geo_name), _grid(NULL), _mesh(NULL), - _origin_x(0), _origin_y(0), _cellsize(0), _width(0), _height(0) + _origin_x(0), _origin_y(0), _cellsize(0), _width(0), _height(0), _img_data(NULL) { } @@ -39,9 +39,9 @@ void GeoMapper::mapOnDEM(const std::string &file_name) void GeoMapper::mapOnMesh(const std::string &file_name) { - FileIO::MeshIO mesh_io; - MeshLib::Mesh* mesh (mesh_io.loadMeshFromFile(file_name)); - this->mapOnMesh(mesh); + _mesh = FileIO::readMeshFromFile(file_name); + _grid = this->getFlatGrid(); + this->mapData(); } void GeoMapper::mapOnMesh(const MeshLib::Mesh* mesh) @@ -111,12 +111,23 @@ float GeoMapper::getMeshElevation(double x, double y) const GeoLib::Grid<GeoLib::PointWithID>* GeoMapper::getFlatGrid() const { - const std::vector<GeoLib::PointWithID*> sfc_points = MeshLib::MshEditor::getSurfaceNodes(*_mesh); + std::vector<GeoLib::PointWithID*> sfc_points; + if (_mesh->getDimension()<3) //much faster + { + size_t nNodes (_mesh->getNNodes()); + sfc_points.resize(nNodes); + const std::vector<MeshLib::Node*> nodes (_mesh->getNodes()); + for (unsigned i(0); i<nNodes; ++i) + sfc_points[i] = new GeoLib::PointWithID(nodes[i]->getCoords(), nodes[i]->getID()); + } + else + sfc_points = MeshLib::MshEditor::getSurfaceNodes(*_mesh); size_t nPoints (sfc_points.size()); for (unsigned i=0; i<nPoints; ++i) { GeoLib::PointWithID* pnt (sfc_points[i]); (*pnt)[2] = 0; } + //TODO - does Grid delete the objects in the vector or do I need to do this? return new GeoLib::Grid<GeoLib::PointWithID>(sfc_points.begin(), sfc_points.end()); } diff --git a/Gui/mainwindow.cpp b/Gui/mainwindow.cpp index 4386667c0f6c51fb977ae13e4226c320e4489da7..95a8de263c462e7866e57bb679e84edf1dd134f2 100644 --- a/Gui/mainwindow.cpp +++ b/Gui/mainwindow.cpp @@ -75,6 +75,7 @@ #include "Mesh.h" #include "Node.h" #include "MshEditor.h" +#include "readMeshFromFile.h" //test #include "VtkMeshConverter.h" @@ -513,32 +514,20 @@ void MainWindow::loadFile(ImportFileType::type t, const QString &fileName) xml.readFile(fileName); } // OpenGeoSys mesh files - else if (fi.suffix().toLower() == "msh") + else if (fi.suffix().toLower() == "msh" || fi.suffix().toLower() == "vtu") { #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); + MeshLib::Mesh* mesh (FileIO::readMeshFromFile(fileName.toStdString())); #ifndef NDEBUG - std::cout << "Total mesh loading time: " << myTimer0.elapsed() << " ms" << std::endl; + std::cout << "Mesh loading time: " << myTimer0.elapsed() << " ms" << std::endl; #endif - } - else - OGSError::box("Failed to load a mesh file."); - } - else if (fi.suffix().toLower() == "vtu") - { - MeshLib::Mesh* msh = FileIO::VTKInterface::readVTUFile(fileName.toStdString()); - if (msh) - _meshModels->addMesh(msh); + if (mesh) + _meshModels->addMesh(mesh); else - OGSError::box("Failed to load a mesh file."); + OGSError::box("Failed to load mesh file."); } else if (fi.suffix().toLower() == "cnd") { @@ -905,27 +894,19 @@ void MainWindow::writeStationListToFile(QString listName, QString fileName) void MainWindow::mapGeometry(const std::string &geo_name) { QSettings settings("UFZ", "OpenGeoSys-5"); - QString mesh_name = QFileDialog::getOpenFileName( this, "Select a mesh file for mapping", + QString file_name = QFileDialog::getOpenFileName( this, "Select file for mapping", settings.value("lastOpenedFileDirectory").toString(), - "OpenGeoSys mesh files (*.msh)"); - if (mesh_name.compare("") != 0) - { - MeshLib::Mesh* mesh (NULL); - QFileInfo fi(mesh_name); - if (fi.suffix().toLower() == "vtu") - mesh = VTKInterface::readVTUFile(mesh_name.toStdString()); - else if (fi.suffix().toLower() == "msh") - { - FileIO::MeshIO mesh_io; - mesh = mesh_io.loadMeshFromFile(mesh_name.toStdString()); - } + "Raster files(*.asc *.grd);;OpenGeoSys mesh files (*.vtu *.msh)"); + GeoMapper geo_mapper(*_geoModels, geo_name); - if (mesh) - { - GeoMapper geo_mapper(*_geoModels, geo_name); - geo_mapper.mapOnMesh(mesh); - this->_geoModels->updateGeometry(geo_name); - } + if (file_name.compare("") != 0) + { + QFileInfo fi(file_name); + if (fi.suffix().toLower() == "asc" || fi.suffix().toLower() == "grd") + geo_mapper.mapOnDEM(file_name.toStdString()); + else if (fi.suffix().toLower() == "vtu" || fi.suffix().toLower() == "msh") + geo_mapper.mapOnMesh(file_name.toStdString()); + this->_geoModels->updateGeometry(geo_name); } }