diff --git a/CMakeLists.txt b/CMakeLists.txt index 8843c9a4178495f39093189b40114538461fe0a9..16e29c604e9d4dc3cdc0766a2e820ac04a1bbfbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -129,6 +129,7 @@ ENDIF() # OGS_BUILD_GUI IF(OGS_BUILD_UTILS) ADD_SUBDIRECTORY( Utils/SimpleMeshCreation ) ADD_SUBDIRECTORY( Utils/FileConverter ) + ADD_SUBDIRECTORY( Utils/MeshEdit ) ENDIF() # OGS_BUILD_UTILS CONFIGURE_FILE (BaseLib/BuildInfo.h.in ${PROJECT_BINARY_DIR}/BaseLib/BuildInfo.h) diff --git a/Gui/DataView/DiagramView/DiagramList.cpp b/Gui/DataView/DiagramView/DiagramList.cpp index b2505c549b69d3d30f7f2c38bc9eba347075ead5..2293c3b48edd087ca0f16fc44ff18f5b44c535ae 100644 --- a/Gui/DataView/DiagramView/DiagramList.cpp +++ b/Gui/DataView/DiagramView/DiagramList.cpp @@ -174,10 +174,12 @@ int DiagramList::readList(const QString &path, std::vector<DiagramList*> &lists) double value(0); QString stringDate(""); QDateTime startDate, currentDate; + unsigned line_count (1); while (!in.atEnd()) { line = in.readLine(); + line_count++; fields = line.split('\t'); if (fields.size() >= (nLists + 1)) { @@ -201,7 +203,7 @@ int DiagramList::readList(const QString &path, std::vector<DiagramList*> &lists) } else { - qDebug("Unexpected file format..."); + std::cout << "Unexpected format in line " << line_count << "..." << std::endl; file.close(); return 0; } diff --git a/Gui/DataView/GeoMapper.cpp b/Gui/DataView/GeoMapper.cpp index 84aa0d254656b7fcc7f278c92a27f610c3df4039..5b38f2dc03799eb17e6293c2d6d7fe08564704d6 100644 --- a/Gui/DataView/GeoMapper.cpp +++ b/Gui/DataView/GeoMapper.cpp @@ -37,8 +37,8 @@ GeoMapper::~GeoMapper() void GeoMapper::mapOnDEM(const std::string &file_name) { - GeoLib::Raster *raster(GeoLib::Raster::getRasterFromASCFile(file_name)); - if (! raster) { + this->_raster = GeoLib::Raster::getRasterFromASCFile(file_name); + if (! _raster) { ERR("GeoMapper::mapOnDEM(): failed to load %s", file_name.c_str()); return; } diff --git a/MeshLib/Node.h b/MeshLib/Node.h index 4b6921bfd91d698af164ec7e930b1449b2691840..215650b9def07e2bf1adb76997ca56e178c47e3e 100644 --- a/MeshLib/Node.h +++ b/MeshLib/Node.h @@ -58,6 +58,9 @@ public: /// Copy constructor Node(const Node &node); + /// Return all the nodes connected to this one + const std::vector<MeshLib::Node*>& getConnectedNodes() const { return _connected_nodes; }; + /// Get an element the node is part of. const Element* getElement(unsigned idx) const { return _elements[idx]; }; diff --git a/Utils/MeshEdit/CMakeLists.txt b/Utils/MeshEdit/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..e637d2cd66809d82561a472f4c9ef31eb3ef9b9a --- /dev/null +++ b/Utils/MeshEdit/CMakeLists.txt @@ -0,0 +1,35 @@ + +INCLUDE_DIRECTORIES( + ${CMAKE_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/BaseLib + ${CMAKE_SOURCE_DIR}/GeoLib + ${CMAKE_SOURCE_DIR}/FileIO + ${CMAKE_SOURCE_DIR}/FileIO/MeshIO + ${CMAKE_SOURCE_DIR}/MathLib + ${CMAKE_SOURCE_DIR}/MeshLib +) + +# Create executables +IF(QT4_FOUND) + + ADD_EXECUTABLE( removeMeshNodes removeMeshNodes.cpp ) + TARGET_LINK_LIBRARIES( removeMeshNodes + BaseLib + FileIO + MeshLib + ${QT_LIBRARIES} + ) + + ADD_EXECUTABLE( moveMeshNodes moveMeshNodes.cpp ) + TARGET_LINK_LIBRARIES( moveMeshNodes + BaseLib + FileIO + MathLib + MeshLib + ${QT_LIBRARIES} + ) + + SET_TARGET_PROPERTIES(moveMeshNodes removeMeshNodes + PROPERTIES FOLDER Utilities) + +ENDIF() # QT4_FOUND diff --git a/Utils/MeshEdit/moveMeshNodes.cpp b/Utils/MeshEdit/moveMeshNodes.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91940b92413ab03c9bd0ade21d5a700e26e8e864 --- /dev/null +++ b/Utils/MeshEdit/moveMeshNodes.cpp @@ -0,0 +1,198 @@ +/** + * \file removeMeshNodes.cpp + * 2012/03/07 KR Initial implementation + */ + +#include <QApplication> +#include "logog/include/logog.hpp" +#include "LogogSimpleFormatter.h" +#include "readMeshFromFile.h" +#include "Legacy/MeshIO.h" +#include "AABB.h" +#include "Mesh.h" +#include "Node.h" +#include "MshEditor.h" +#include "MathTools.h" + +int find_closest_point(MeshLib::Node const*const point, std::vector<MeshLib::Node*> const& nodes, double const& max_dist) +{ + const std::size_t nNodes (nodes.size()); + double sqr_shortest_dist (max_dist*2); + int idx = (sqr_shortest_dist<max_dist) ? 0 : -1; + const MeshLib::Node p (*point); + + for (unsigned i=0; i<nNodes; i++) + { + double sqr_dist ((p[0]-(*nodes[i])[0])*(p[0]-(*nodes[i])[0])); + if (sqr_dist < max_dist) + { + sqr_dist += ((p[1]-(*nodes[i])[1])*(p[1]-(*nodes[i])[1])); + if (sqr_dist < max_dist && sqr_dist < sqr_shortest_dist) + { + sqr_shortest_dist = sqr_dist; + idx = i; + } + } + } + + return idx; +} + +bool containsPoint(MeshLib::Node const& pnt, MeshLib::Node const& min, MeshLib::Node const& max) +{ + if (pnt[0] < min[0] || max[0] < pnt[0]) return false; + if (pnt[1] < min[1] || max[1] < pnt[1]) return false; + return true; +} + +int main (int argc, char* argv[]) +{ + QApplication app(argc, argv, false); + LOGOG_INITIALIZE(); + logog::Cout* logogCout = new logog::Cout; + BaseLib::LogogSimpleFormatter* formatter = new BaseLib::LogogSimpleFormatter; + logogCout->SetFormatter(*formatter); + + std::vector<std::string> keywords; + keywords.push_back("-ALL"); + keywords.push_back("-MESH"); + keywords.push_back("-LOWPASS"); + + if (argc < 3) + { + std::cout << "Moves mesh nodes and connected elements either by a given value or based on a list." << std::endl; + std::cout << std::endl; + std::cout << "Usage: " << argv[0] << " <msh-file.msh> <keyword> [<value1>] [<value2>]" << std::endl; + std::cout << "Available keywords:" << std::endl; + //for (size_t i=0; i<keywords.size(); i++) + std::cout << "\t" << "-ALL <value1> <value2> : changes the elevation of all mesh nodes by <value2> in direction <value1> [x,y,z]." << std::endl; + std::cout << "\t" << "-MESH <value1> <value2> : changes the elevation of mesh nodes based on a second mesh <value1> with a search range of <value2>." << std::endl; + std::cout << "\t" << "-LOWPASS : applies a lowpass filter over node elevation using directly connected nodes." << std::endl; + return -1; + } + + const std::string msh_name(argv[1]); + const std::string current_key(argv[2]); + //const std::string msh_name("D:\\rappbode-2013-03-03--30m_lowpass_new_new.msh"); + //const std::string current_key("-MESH"); + + if (msh_name.substr(msh_name.length()-4, 4).compare(".msh") != 0) + { + std::cout << "Error: Parameter 1 should be a msh-file" << std::endl; + std::cout << "Usage: " << argv[0] << " <msh-file.gml> <keyword> <value>" << std::endl; + return -1; + } + + bool is_keyword(false); + for (size_t i=0; i<keywords.size(); i++) + if (current_key.compare(keywords[i])==0) + { + is_keyword = true; + break; + } + + if (!is_keyword) + { + std::cout << "Keyword not recognised. Available keywords:" << std::endl; + for (size_t i=0; i<keywords.size(); i++) + std::cout << keywords[i] << std::endl; + return -1; + } + + MeshLib::Mesh* mesh (FileIO::readMeshFromFile(msh_name)); + //std::vector<size_t> del_nodes; + + // Start keyword-specific selection of nodes + + // moves the elevation of all nodes by value + if (current_key.compare("-ALL")==0) + { + if (argc < 5) + { + std::cout << "Missing parameter..." << std::endl; + return -1; + } + const std::string dir(argv[3]); + unsigned idx = (dir.compare("x") == 0) ? 0 : (dir.compare("y") == 0) ? 1 : 2; + const double value(strtod(argv[4],0)); + std::cout << "Moving all mesh nodes by " << value << " in direction " << idx << " (" << dir << ")..." << std::endl; + //double value(-10); + const size_t nNodes(mesh->getNNodes()); + std::vector<MeshLib::Node*> nodes (mesh->getNodes()); + for (size_t i=0; i<nNodes; i++) + { + (*nodes[i])[idx] += value; + } + } + + // maps the elevation of mesh nodes according to a ground truth mesh whenever nodes exist within max_dist + if (current_key.compare("-MESH")==0) + { + if (argc < 5) + { + std::cout << "Missing parameter..." << std::endl; + return -1; + } + const std::string value (argv[3]); + double max_dist(pow(strtod(argv[4],0), 2)); + //const std::string value("D:\\Rappbodevorsperre_elevation440m.msh"); + //double max_dist (25.0); // squared maximum distance at which reference points are used + double offset (0.0); // additional offset for elevation (should be 0) + MeshLib::Mesh* ground_truth (FileIO::readMeshFromFile(value)); + const std::vector<MeshLib::Node*> ground_truth_nodes (ground_truth->getNodes()); + GeoLib::AABB<MeshLib::Node> bounding_box(ground_truth_nodes.begin(), ground_truth_nodes.end()); + const MeshLib::Node min (bounding_box.getMinPoint()); + const MeshLib::Node max (bounding_box.getMaxPoint()); + + const size_t nNodes(mesh->getNNodes()); + std::vector<MeshLib::Node*> nodes (mesh->getNodes()); + + for (size_t i=0; i<nNodes; i++) + { + bool is_inside (containsPoint(*nodes[i], min, max)); + if (is_inside) + { + int idx = find_closest_point(nodes[i], ground_truth_nodes, max_dist); + if (idx>=0) + (*nodes[i])[2] = (*(ground_truth_nodes[idx]))[2]-offset; + } + } + } + + // a simple lowpass filter for the elevation of mesh nodes using the elevation of each node + // weighted by 2 and the elevation of each connected node weighted by 1 + if (current_key.compare("-LOWPASS")==0) + { + const size_t nNodes(mesh->getNNodes()); + std::vector<MeshLib::Node*> nodes (mesh->getNodes()); + + std::vector<double> elevation(nNodes); + for (size_t i=0; i<nNodes; i++) + elevation[i] = (*nodes[i])[2]; + + for (size_t i=0; i<nNodes; i++) + { + const std::vector<MeshLib::Node*> conn_nodes (nodes[i]->getConnectedNodes()); + const unsigned nConnNodes (conn_nodes.size()); + elevation[i] = (2*(*nodes[i])[2]); + for (size_t j=0; j<nConnNodes; ++j) + elevation[i] += (*conn_nodes[j])[2]; + elevation[i] /= (nConnNodes+2); + } + + for (size_t i=0; i<nNodes; i++) + (*nodes[i])[2] = elevation[i]; + } + /**** add other keywords here ****/ + + FileIO::MeshIO meshIO; + meshIO.setMesh(mesh); + meshIO.setPrecision(9); + meshIO.writeToFile(msh_name.substr(0, msh_name.length()-4) + "_new.msh"); + delete mesh; + return 1; + +} + + + diff --git a/Utils/MeshEdit/removeMeshNodes.cpp b/Utils/MeshEdit/removeMeshNodes.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0dd86b23f330f8ca8d8e15aef6b74007a156ee5e --- /dev/null +++ b/Utils/MeshEdit/removeMeshNodes.cpp @@ -0,0 +1,98 @@ +/** + * \file removeMeshNodes.cpp + * 2012/03/07 KR Initial implementation + */ + +#include <QApplication> +#include "readMeshFromFile.h" +#include "Legacy/MeshIO.h" +#include "Mesh.h" +#include "Node.h" +#include "MshEditor.h" + +int main (int argc, char* argv[]) +{ + QApplication app(argc, argv, false); + + std::vector<std::string> keywords; + keywords.push_back("-Z_ABOVE"); + keywords.push_back("-Z_BELOW"); + + if (argc != 4) + { + std::cout << "Removes mesh nodes and connected elements based on the given criterium." << std::endl; + std::cout << std::endl; + std::cout << "Usage: " << argv[0] << " <msh-file.msh> <keyword> <value>" << std::endl; + std::cout << "Available keywords:" << std::endl; + for (size_t i=0; i<keywords.size(); i++) + std::cout << "\t" << keywords[i] << std::endl; + return -1; + } + + const std::string msh_name(argv[1]); + const std::string current_key(argv[2]); + double value(strtod(argv[3],0)); + + if (msh_name.substr(msh_name.length()-4, 4).compare(".msh") != 0) + { + std::cout << "Error: Parameter 1 should be a msh-file" << std::endl; + std::cout << "Usage: " << argv[0] << " <msh-file.gml> <keyword> <value>" << std::endl; + return -1; + } + + bool is_keyword(false); + for (size_t i=0; i<keywords.size(); i++) + if (current_key.compare(keywords[i])==0) + { + is_keyword = true; + break; + } + + if (!is_keyword) + { + std::cout << "Keyword not recognised. Available keywords:" << std::endl; + for (size_t i=0; i<keywords.size(); i++) + std::cout << keywords[i] << std::endl; + return -1; + } + + MeshLib::Mesh* mesh (FileIO::readMeshFromFile(msh_name)); + std::vector<size_t> del_nodes; + + // Start keyword-specific selection of nodes + if ((current_key.compare("-Z_BELOW")==0) || (current_key.compare("-Z_ABOVE")==0)) + { + int factor(1); + if (current_key.compare("-Z_ABOVE")==0) + { + factor=-1; + value*=-1; + } + + const size_t nNodes(mesh->getNNodes()); + for (size_t i=0; i<nNodes; i++) + { + const MeshLib::Node* node = mesh->getNode(i); + const double* coords(node->getCoords()); + if ((factor*coords[2]) < value) + del_nodes.push_back(i); + } + } + + /**** add other keywords here ****/ + + // remove nodes and write new file + MeshLib::Mesh* new_mesh = MeshLib::MshEditor::removeMeshNodes(mesh, del_nodes); + + FileIO::MeshIO meshIO; + meshIO.setMesh(mesh); + meshIO.setPrecision(9); + meshIO.writeToFile(msh_name.substr(0, msh_name.length()-4) + "_new.msh"); + delete mesh; + delete new_mesh; + return 1; + +} + + +