diff --git a/Applications/Utils/ModelPreparation/CMakeLists.txt b/Applications/Utils/ModelPreparation/CMakeLists.txt index 1d8e282ea56814621d4c70c2b6a3161d5c894755..042ee6a9a05a8790584218fd7a258576d78ee5d1 100644 --- a/Applications/Utils/ModelPreparation/CMakeLists.txt +++ b/Applications/Utils/ModelPreparation/CMakeLists.txt @@ -19,4 +19,16 @@ target_link_libraries(createNeumannBc ${OGS_VTK_REQUIRED_LIBS} ) +add_executable(convertVtkDataArrayToVtkDataArray ConvertVtkDataArrayToVtkDataArray.cpp ) +target_link_libraries(convertVtkDataArrayToVtkDataArray MeshLib) +set_target_properties(convertVtkDataArrayToVtkDataArray PROPERTIES FOLDER Utilities) + add_subdirectory(PartitionMesh) + +#################### +### Installation ### +#################### +install(TARGETS + convertVtkDataArrayToVtkDataArray + RUNTIME DESTINATION bin COMPONENT Utilities +) diff --git a/Applications/Utils/ModelPreparation/ConvertVtkDataArrayToVtkDataArray.cpp b/Applications/Utils/ModelPreparation/ConvertVtkDataArrayToVtkDataArray.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33b04b398f935138152d6f536f1bc150748188d1 --- /dev/null +++ b/Applications/Utils/ModelPreparation/ConvertVtkDataArrayToVtkDataArray.cpp @@ -0,0 +1,158 @@ +/** + * + * \copyright + * Copyright (c) 2012-2016, 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 <algorithm> +#include <cmath> +#include <memory> +#include <numeric> + +#include <tclap/CmdLine.h> + +#include "Applications/ApplicationsLib/LogogSetup.h" + +#include "BaseLib/BuildInfo.h" + +#include "MeshLib/IO/readMeshFromFile.h" +#include "MeshLib/IO/writeMeshToFile.h" + +#include "MeshLib/Mesh.h" + +template <typename T1, typename T2> +std::pair<bool, std::string> castPropertyVectorToPropertyVector( + MeshLib::Properties& properties, + std::string const& property_vector_name_in, + std::string const& property_vector_name_out) +{ + auto const* const orig_pv = properties.getPropertyVector<T1>( + property_vector_name_in, MeshLib::MeshItemType::Cell, 1); + if (!orig_pv) + return std::make_pair(false, + "Original property vector '" + + property_vector_name_in + "' not found."); + auto* new_pv = properties.createNewPropertyVector<T2>( + property_vector_name_out, MeshLib::MeshItemType::Cell, 1); + if (!new_pv) + return std::make_pair(false, + "Could not create new property vector '" + + property_vector_name_in + "' not found."); + new_pv->resize(orig_pv->getNumberOfTuples()); + for (std::size_t i(0); i < new_pv->getNumberOfTuples(); ++i) + { + (*new_pv)[i] = static_cast<T2>((*orig_pv)[i]); + } + return std::make_pair(true, ""); +} + +int main(int argc, char* argv[]) +{ + ApplicationsLib::LogogSetup logog_setup; + + TCLAP::CmdLine cmd( + "Converts a double or floating point cell data array of a vtk " + "unstructured grid into a int or double cell data array.\n\n" + "OpenGeoSys-6 software, version " + + BaseLib::BuildInfo::git_describe + + ".\n" + "Copyright (c) 2012-2018, OpenGeoSys Community " + "(http://www.opengeosys.org)", + ' ', BaseLib::BuildInfo::git_describe); + + TCLAP::ValueArg<std::string> new_property_data_type_arg( + "t", + "new-property-data-type", + "the name of the data type as string (int or double)", + false, + "int", + "data type as string"); + cmd.add(new_property_data_type_arg); + + TCLAP::ValueArg<std::string> new_property_arg( + "n", + "new-property-name", + "the name of the new cell data array (PropertyVector) the values are " + "stored", + false, + "MaterialIDs", + "name of the new cell data array (PropertyVector) as string"); + cmd.add(new_property_arg); + + TCLAP::ValueArg<std::string> out_mesh_arg( + "o", "out-mesh", "output mesh file name", true, "", "file name"); + cmd.add(out_mesh_arg); + + TCLAP::ValueArg<std::string> property_arg( + "e", + "existing-property-name", + "the name of the existing cell data array (PropertyVector)", + true, + "", + "property name as string"); + cmd.add(property_arg); + + TCLAP::ValueArg<std::string> mesh_arg( + "i", "in-mesh", "input mesh file name", true, "", "file name"); + cmd.add(mesh_arg); + + cmd.parse(argc, argv); + + std::unique_ptr<MeshLib::Mesh> mesh( + MeshLib::IO::readMeshFromFile(mesh_arg.getValue())); + + if (!mesh) + { + return -1; + } + + bool success = false; + std::string err_msg = "Could not find cell data array '" + + property_arg.getValue() + "' in the mesh '" + + mesh_arg.getValue() + "'"; + + if (new_property_data_type_arg.getValue() == "int") + { + if (mesh->getProperties().existsPropertyVector<double>( + property_arg.getValue(), MeshLib::MeshItemType::Cell, 1)) + { + std::tie(success, err_msg) = + castPropertyVectorToPropertyVector<double, int>( + mesh->getProperties(), + property_arg.getValue(), + new_property_arg.getValue()); + } + + if (mesh->getProperties().existsPropertyVector<float>( + property_arg.getValue(), MeshLib::MeshItemType::Cell, 1)) + std::tie(success, err_msg) = + castPropertyVectorToPropertyVector<float, int>( + mesh->getProperties(), + property_arg.getValue(), + new_property_arg.getValue()); + } + if (new_property_data_type_arg.getValue() == "double") + { + if (mesh->getProperties().existsPropertyVector<float>( + property_arg.getValue(), MeshLib::MeshItemType::Cell, 1)) + std::tie(success, err_msg) = + castPropertyVectorToPropertyVector<float, double>( + mesh->getProperties(), + property_arg.getValue(), + new_property_arg.getValue()); + } + + if (!success) + { + ERR("%s", err_msg.c_str()); + return -1; + } + + MeshLib::IO::writeMeshToFile(*mesh, out_mesh_arg.getValue()); + + return EXIT_SUCCESS; +} diff --git a/MeshLib/MeshGenerators/VtkMeshConverter.cpp b/MeshLib/MeshGenerators/VtkMeshConverter.cpp index f2208aff7ed40d3f6e91955a013471b6af337b85..8d928387bb3bfddf3019850135095efcd9a2cfc4 100644 --- a/MeshLib/MeshGenerators/VtkMeshConverter.cpp +++ b/MeshLib/MeshGenerators/VtkMeshConverter.cpp @@ -22,6 +22,7 @@ #include <vtkBitArray.h> #include <vtkCharArray.h> #include <vtkDoubleArray.h> +#include <vtkFloatArray.h> #include <vtkImageData.h> #include <vtkIntArray.h> #include <vtkPointData.h> @@ -272,6 +273,12 @@ void VtkMeshConverter::convertArray(vtkDataArray& array, return; } + if (vtkFloatArray::SafeDownCast(&array)) + { + VtkMeshConverter::convertTypedArray<float>(array, properties, type); + return; + } + if (vtkIntArray::SafeDownCast(&array)) { VtkMeshConverter::convertTypedArray<int>(array, properties, type); diff --git a/MeshLib/Vtk/VtkMappedMeshSource.cpp b/MeshLib/Vtk/VtkMappedMeshSource.cpp index 7253f750cdc70aa8e8edce5224e028114e8ff8fe..a76b55354b6fb52788c9cd536bf2aefa426efe55 100644 --- a/MeshLib/Vtk/VtkMappedMeshSource.cpp +++ b/MeshLib/Vtk/VtkMappedMeshSource.cpp @@ -135,6 +135,8 @@ int VtkMappedMeshSource::RequestData(vtkInformation*, { if (addProperty<double>(properties, *name)) continue; + if (addProperty<float>(properties, *name)) + continue; if (addProperty<int>(properties, *name)) continue; if (addProperty<unsigned>(properties, *name)) diff --git a/web/content/docs/tools/doubleValuedMaterialIDs.vtu b/web/content/docs/tools/doubleValuedMaterialIDs.vtu new file mode 100755 index 0000000000000000000000000000000000000000..6fa6ad250020cfde76ee9c5cde9041ef4d4bf236 --- /dev/null +++ b/web/content/docs/tools/doubleValuedMaterialIDs.vtu @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e2fd55e6555445f3ae5a3dfb97f9a7d238fad6cb90f24b335e5906d04fa7a01 +size 1633116 diff --git a/web/content/docs/tools/model-preparation/convert-data-array-to-data-array/index.pandoc b/web/content/docs/tools/model-preparation/convert-data-array-to-data-array/index.pandoc new file mode 100644 index 0000000000000000000000000000000000000000..607a90d372df48ef8354320db6bd29ab15853235 --- /dev/null +++ b/web/content/docs/tools/model-preparation/convert-data-array-to-data-array/index.pandoc @@ -0,0 +1,52 @@ ++++ +date = "2018-12-17T10:56:57+01:00" +title = "Convert vtk data array to another vtk data array" +author = "Thomas Fischer" + +[menu] + [menu.tools] + parent = "model-preparation" ++++ + +## General + +Often, meshes contain geometrical data in common with data used for process +simulation. Usually, such data used by the process simulation is associated to +the mesh nodes or to the mesh cells. In the vtk unstructured grid file format +the geometrical and the process data information is stored in one file - in so +called data arrays. + +Some tools, for instance paraview, export data arrays always using a floating +point data type. OpenGeoSys expects the 'MaterialIDs' cell data array to have +int data-type. + +Other tools, for instance Gocad, export data associated with cells or nodes +sometimes as float. The tool can convert the cell data arrays to double +data-type. + +## Usage + +``` +convertVtkDataArrayToVtkDataArray + --in-mesh <file name> + --existing-property-name <name of the cell data array as string> + --out-mesh <filename for the output mesh> + --new-property-name <name of the new cell data array> + --new-property-data-type <data type as string> +``` + +## Example + +``` +convertVtkDataArrayToVtkDataArray + -i doubleValuedMaterialIDs.vtu + -e MaterialIDs_double + --new-property-data-type int + -n MaterialIDs + -o intValuedMaterialIDs.vtu +``` + +::: {.note} +### Example Files +[doubleValuedMaterialIDs.vtu](doubleValuedMaterialIDs.vtu) +:::