From 71b1204987fa7093271480ee2b9554efc52a1415 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Tue, 5 Dec 2017 11:49:53 +0100 Subject: [PATCH] Make the medwriter engine usable from the outside --- src/Plugins/MEDWriter/CMakeLists.txt | 5 + src/Plugins/MEDWriter/IO/CMakeLists.txt | 12 +- src/Plugins/MEDWriter/IO/VTKToMEDMem.cxx | 948 ++++++++++++++++++++++ src/Plugins/MEDWriter/IO/VTKToMEDMem.hxx | 86 ++ src/Plugins/MEDWriter/IO/vtkMEDWriter.cxx | 933 +-------------------- 5 files changed, 1062 insertions(+), 922 deletions(-) create mode 100644 src/Plugins/MEDWriter/IO/VTKToMEDMem.cxx create mode 100644 src/Plugins/MEDWriter/IO/VTKToMEDMem.hxx diff --git a/src/Plugins/MEDWriter/CMakeLists.txt b/src/Plugins/MEDWriter/CMakeLists.txt index add77d45..2c132b00 100644 --- a/src/Plugins/MEDWriter/CMakeLists.txt +++ b/src/Plugins/MEDWriter/CMakeLists.txt @@ -41,6 +41,9 @@ OPTION(BUILD_SHARED_LIBS "Build with shared libraries." ${VTK_BUILD_SHARED_LIBS} SET(VTK_INSTALL_RUNTIME_DIR lib/salome) SET(VTK_INSTALL_LIBRARY_DIR lib/salome) SET(VTK_INSTALL_ARCHIVE_DIR lib/salome) +SET(MEDWRITER_INSTALL_CMAKE_LOCAL adm_local/cmake_files CACHE PATH "Install path: local SALOME CMake files") +SET(MEDWRITER_INSTALL_HEADERS include/salome CACHE PATH "Install path: local include files") + SET(MEDCOUPLING_ROOT_DIR $ENV{MEDCOUPLING_ROOT_DIR} CACHE PATH "MEDCOUPLING_ROOT_DIR") LIST(APPEND CMAKE_MODULE_PATH "${MEDCOUPLING_ROOT_DIR}/cmake_files") @@ -52,3 +55,5 @@ FIND_PACKAGE(SalomeMEDFile REQUIRED) PV_PROCESS_MODULES() ADD_SUBDIRECTORY(ParaViewPlugin) + +INSTALL(EXPORT ${PROJECT_NAME}TargetGroup DESTINATION "${MEDWRITER_INSTALL_CMAKE_LOCAL}" FILE ${PROJECT_NAME}Targets.cmake) diff --git a/src/Plugins/MEDWriter/IO/CMakeLists.txt b/src/Plugins/MEDWriter/IO/CMakeLists.txt index 90c3b710..16dbc23b 100644 --- a/src/Plugins/MEDWriter/IO/CMakeLists.txt +++ b/src/Plugins/MEDWriter/IO/CMakeLists.txt @@ -23,6 +23,14 @@ INCLUDE_DIRECTORIES( ${MEDFILE_INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS}) +ADD_LIBRARY(VTKToMEDMem SHARED VTKToMEDMem.cxx) +TARGET_LINK_LIBRARIES(VTKToMEDMem vtkPVVTKExtensionsRendering vtkFiltersGeneral vtkFiltersCore ${PARAVIEW_LIBRARIES} ${MEDCoupling_medloader} ${MEDFILE_C_LIBRARIES}) VTK_MODULE_LIBRARY(vtkMEDWriter vtkMEDWriter.cxx) -TARGET_LINK_LIBRARIES(vtkMEDWriter vtkPVVTKExtensionsRendering vtkFiltersGeneral vtkFiltersCore ${PARAVIEW_LIBRARIES} ${MEDCoupling_medloader} ${MEDFILE_C_LIBRARIES}) -INSTALL(TARGETS vtkMEDWriter RUNTIME DESTINATION ${VTK_INSTALL_RUNTIME_DIR} LIBRARY DESTINATION ${VTK_INSTALL_LIBRARY_DIR} ARCHIVE DESTINATION ${VTK_INSTALL_ARCHIVE_DIR}) +TARGET_LINK_LIBRARIES(vtkMEDWriter VTKToMEDMem) +INSTALL(TARGETS vtkMEDWriter RUNTIME DESTINATION ${VTK_INSTALL_RUNTIME_DIR} LIBRARY DESTINATION ${VTK_INSTALL_LIBRARY_DIR} ARCHIVE DESTINATION ${VTK_INSTALL_ARCHIVE_DIR} ) +INSTALL(TARGETS VTKToMEDMem EXPORT ${PROJECT_NAME}TargetGroup RUNTIME DESTINATION ${VTK_INSTALL_RUNTIME_DIR} LIBRARY DESTINATION ${VTK_INSTALL_LIBRARY_DIR} ARCHIVE DESTINATION ${VTK_INSTALL_ARCHIVE_DIR} ) + +INSTALL(FILES VTKToMEDMem.hxx DESTINATION ${MEDWRITER_INSTALL_HEADERS}) + +SET(_${PROJECT_NAME}_exposed_targets VTKToMEDMem) +EXPORT(TARGETS ${_${PROJECT_NAME}_exposed_targets} FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake) diff --git a/src/Plugins/MEDWriter/IO/VTKToMEDMem.cxx b/src/Plugins/MEDWriter/IO/VTKToMEDMem.cxx new file mode 100644 index 00000000..c3d20a9e --- /dev/null +++ b/src/Plugins/MEDWriter/IO/VTKToMEDMem.cxx @@ -0,0 +1,948 @@ +// Copyright (C) 2017 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author : Anthony Geay (EDF R&D) + +#include "VTKToMEDMem.hxx" + +#include "vtkAdjacentVertexIterator.h" +#include "vtkIntArray.h" +#include "vtkLongArray.h" +#include "vtkCellData.h" +#include "vtkPointData.h" +#include "vtkFloatArray.h" +#include "vtkCellArray.h" + +#include "vtkStreamingDemandDrivenPipeline.h" +#include "vtkInformationDataObjectMetaDataKey.h" +#include "vtkUnstructuredGrid.h" +#include "vtkMultiBlockDataSet.h" +#include "vtkRectilinearGrid.h" +#include "vtkInformationStringKey.h" +#include "vtkAlgorithmOutput.h" +#include "vtkObjectFactory.h" +#include "vtkMutableDirectedGraph.h" +#include "vtkMultiBlockDataSet.h" +#include "vtkPolyData.h" +#include "vtkDataSet.h" +#include "vtkInformationVector.h" +#include "vtkInformation.h" +#include "vtkDataArraySelection.h" +#include "vtkTimeStamp.h" +#include "vtkInEdgeIterator.h" +#include "vtkInformationDataObjectKey.h" +#include "vtkExecutive.h" +#include "vtkVariantArray.h" +#include "vtkStringArray.h" +#include "vtkDoubleArray.h" +#include "vtkCharArray.h" +#include "vtkUnsignedCharArray.h" +#include "vtkDataSetAttributes.h" +#include "vtkDemandDrivenPipeline.h" +#include "vtkDataObjectTreeIterator.h" +#include "vtkWarpScalar.h" + +#include +#include +#include +#include + +using VTKToMEDMem::Grp; +using VTKToMEDMem::Fam; + +using MEDCoupling::MEDFileData; +using MEDCoupling::MEDFileMesh; +using MEDCoupling::MEDFileCMesh; +using MEDCoupling::MEDFileUMesh; +using MEDCoupling::MEDFileFields; +using MEDCoupling::MEDFileMeshes; + +using MEDCoupling::MEDFileIntField1TS; +using MEDCoupling::MEDFileField1TS; +using MEDCoupling::MEDFileIntFieldMultiTS; +using MEDCoupling::MEDFileFieldMultiTS; +using MEDCoupling::MEDFileAnyTypeFieldMultiTS; +using MEDCoupling::DataArray; +using MEDCoupling::DataArrayInt; +using MEDCoupling::DataArrayFloat; +using MEDCoupling::DataArrayDouble; +using MEDCoupling::MEDCouplingMesh; +using MEDCoupling::MEDCouplingUMesh; +using MEDCoupling::MEDCouplingCMesh; +using MEDCoupling::MEDCouplingFieldDouble; +using MEDCoupling::MEDCouplingFieldFloat; +using MEDCoupling::MEDCouplingFieldInt; +using MEDCoupling::MCAuto; +using MEDCoupling::Traits; +using MEDCoupling::MLFieldTraits; + +/////////////////// + +Fam::Fam(const std::string& name) +{ + static const char ZE_SEP[]="@@][@@"; + std::size_t pos(name.find(ZE_SEP)); + std::string name0(name.substr(0,pos)),name1(name.substr(pos+strlen(ZE_SEP))); + std::istringstream iss(name1); + iss >> _id; + _name=name0; +} + +/////////////////// + +template +class MEDFileVTKTraits +{ +public: + typedef void VtkType; + typedef void MCType; +}; + +template<> +class MEDFileVTKTraits +{ +public: + typedef vtkIntArray VtkType; + typedef MEDCoupling::DataArrayInt MCType; +}; + +template<> +class MEDFileVTKTraits +{ +public: + typedef vtkFloatArray VtkType; + typedef MEDCoupling::DataArrayFloat MCType; +}; + +template<> +class MEDFileVTKTraits +{ +public: + typedef vtkDoubleArray VtkType; + typedef MEDCoupling::DataArrayDouble MCType; +}; + +std::map ComputeMapOfType() +{ + std::map ret; + int nbOfTypesInMC(sizeof(MEDCouplingUMesh::MEDCOUPLING2VTKTYPETRADUCER)/sizeof(int)); + for(int i=0;i& context) +{ + static const char DFT_MESH_NAME[]="Mesh"; + if(context.empty()) + return DFT_MESH_NAME; + std::ostringstream oss; oss << DFT_MESH_NAME; + for(std::vector::const_iterator it=context.begin();it!=context.end();it++) + oss << "_" << *it; + return oss.str(); +} + +DataArrayInt *ConvertVTKArrayToMCArrayInt(vtkDataArray *data) +{ + if(!data) + throw MZCException("ConvertVTKArrayToMCArrayInt : internal error !"); + int nbTuples(data->GetNumberOfTuples()),nbComp(data->GetNumberOfComponents()); + std::size_t nbElts(nbTuples*nbComp); + MCAuto ret(DataArrayInt::New()); + ret->alloc(nbTuples,nbComp); + for(int i=0;iGetComponentName(i)); + if(comp) + ret->setInfoOnComponent(i,comp); + } + int *ptOut(ret->getPointer()); + vtkIntArray *d0(vtkIntArray::SafeDownCast(data)); + if(d0) + { + const int *pt(d0->GetPointer(0)); + std::copy(pt,pt+nbElts,ptOut); + return ret.retn(); + } + vtkLongArray *d1(vtkLongArray::SafeDownCast(data)); + if(d1) + { + const long *pt(d1->GetPointer(0)); + std::copy(pt,pt+nbElts,ptOut); + return ret.retn(); + } + vtkUnsignedCharArray *d2(vtkUnsignedCharArray::SafeDownCast(data)); + if(d2) + { + const unsigned char *pt(d2->GetPointer(0)); + std::copy(pt,pt+nbElts,ptOut); + return ret.retn(); + } + std::ostringstream oss; + oss << "ConvertVTKArrayToMCArrayInt : unrecognized array \"" << typeid(*data).name() << "\" type !"; + throw MZCException(oss.str()); +} + +template +typename Traits::ArrayType *ConvertVTKArrayToMCArrayDouble(vtkDataArray *data) +{ + if(!data) + throw MZCException("ConvertVTKArrayToMCArrayDouble : internal error !"); + int nbTuples(data->GetNumberOfTuples()),nbComp(data->GetNumberOfComponents()); + std::size_t nbElts(nbTuples*nbComp); + MCAuto< typename Traits::ArrayType > ret(Traits::ArrayType::New()); + ret->alloc(nbTuples,nbComp); + for(int i=0;iGetComponentName(i)); + if(comp) + ret->setInfoOnComponent(i,comp); + else + { + if(nbComp>1 && nbComp<=3) + { + char tmp[2]; + tmp[0]='X'+i; tmp[1]='\0'; + ret->setInfoOnComponent(i,tmp); + } + } + } + T *ptOut(ret->getPointer()); + typename MEDFileVTKTraits::VtkType *d0(MEDFileVTKTraits::VtkType::SafeDownCast(data)); + if(d0) + { + const T *pt(d0->GetPointer(0)); + for(std::size_t i=0;iGetClassName() << "\" type !"; + throw MZCException(oss.str()); +} + +DataArrayDouble *ConvertVTKArrayToMCArrayDoubleForced(vtkDataArray *data) +{ + if(!data) + throw MZCException("ConvertVTKArrayToMCArrayDoubleForced : internal error 0 !"); + vtkFloatArray *d0(vtkFloatArray::SafeDownCast(data)); + if(d0) + { + MCAuto ret(ConvertVTKArrayToMCArrayDouble(data)); + MCAuto ret2(ret->convertToDblArr()); + return ret2.retn(); + } + vtkDoubleArray *d1(vtkDoubleArray::SafeDownCast(data)); + if(d1) + return ConvertVTKArrayToMCArrayDouble(data); + throw MZCException("ConvertVTKArrayToMCArrayDoubleForced : unrecognized type of data for double !"); +} + +DataArray *ConvertVTKArrayToMCArray(vtkDataArray *data) +{ + if(!data) + throw MZCException("ConvertVTKArrayToMCArray : internal error !"); + vtkFloatArray *d0(vtkFloatArray::SafeDownCast(data)); + if(d0) + return ConvertVTKArrayToMCArrayDouble(data); + vtkDoubleArray *d1(vtkDoubleArray::SafeDownCast(data)); + if(d1) + return ConvertVTKArrayToMCArrayDouble(data); + vtkIntArray *d2(vtkIntArray::SafeDownCast(data)); + vtkLongArray *d3(vtkLongArray::SafeDownCast(data)); + vtkUnsignedCharArray *d4(vtkUnsignedCharArray::SafeDownCast(data)); + if(d2 || d3 || d4) + return ConvertVTKArrayToMCArrayInt(data); + std::ostringstream oss; + oss << "ConvertVTKArrayToMCArray : unrecognized array \"" << typeid(*data).name() << "\" type !"; + throw MZCException(oss.str()); +} + +MEDCouplingUMesh *BuildMeshFromCellArray(vtkCellArray *ca, DataArrayDouble *coords, int meshDim, INTERP_KERNEL::NormalizedCellType type) +{ + MCAuto subMesh(MEDCouplingUMesh::New("",meshDim)); + subMesh->setCoords(coords); subMesh->allocateCells(); + int nbCells(ca->GetNumberOfCells()); + if(nbCells==0) + return 0; + vtkIdType nbEntries(ca->GetNumberOfConnectivityEntries()); + const vtkIdType *conn(ca->GetPointer()); + for(int i=0;i conn2(sz); + for(int jj=0;jjinsertNextCell(type,sz,&conn2[0]); + conn+=sz; + } + return subMesh.retn(); +} + +MEDCouplingUMesh *BuildMeshFromCellArrayTriangleStrip(vtkCellArray *ca, DataArrayDouble *coords, MCAuto& ids) +{ + MCAuto subMesh(MEDCouplingUMesh::New("",2)); + subMesh->setCoords(coords); subMesh->allocateCells(); + int nbCells(ca->GetNumberOfCells()); + if(nbCells==0) + return 0; + vtkIdType nbEntries(ca->GetNumberOfConnectivityEntries()); + const vtkIdType *conn(ca->GetPointer()); + ids=DataArrayInt::New() ; ids->alloc(0,1); + for(int i=0;i0) + { + for(int j=0;jinsertNextCell(INTERP_KERNEL::NORM_TRI3,3,conn2); + ids->pushBackSilent(i); + } + } + else + { + std::ostringstream oss; oss << "BuildMeshFromCellArrayTriangleStrip : on cell #" << i << " the triangle stip looks bab !"; + throw MZCException(oss.str()); + } + conn+=sz; + } + return subMesh.retn(); +} + +class MicroField +{ +public: + MicroField(const MCAuto& m, const std::vector >& cellFs):_m(m),_cellFs(cellFs) { } + MicroField(const std::vector< MicroField >& vs); + void setNodeFields(const std::vector >& nf) { _nodeFs=nf; } + MCAuto getMesh() const { return _m; } + std::vector > getCellFields() const { return _cellFs; } +private: + MCAuto _m; + std::vector > _cellFs; + std::vector > _nodeFs; +}; + +MicroField::MicroField(const std::vector< MicroField >& vs) +{ + std::size_t sz(vs.size()); + std::vector vs2(sz); + std::vector< std::vector< MCAuto > > arrs2(sz); + int nbElts(-1); + for(std::size_t ii=0;ii arrsTmp(sz); + for(std::size_t jj=0;jj +void AppendToFields(MEDCoupling::TypeOfField tf, MEDCouplingMesh *mesh, const DataArrayInt *n2oPtr, typename MEDFileVTKTraits::MCType *dadPtr, MEDFileFields *fs, double timeStep, int tsId) +{ + std::string fieldName(dadPtr->getName()); + MCAuto< typename Traits::FieldType > f(Traits::FieldType::New(tf)); + f->setTime(timeStep,tsId,0); + { + std::string fieldNameForChuckNorris(MEDCoupling::MEDFileAnyTypeField1TSWithoutSDA::FieldNameToMEDFileConvention(fieldName)); + f->setName(fieldNameForChuckNorris); + } + if(!n2oPtr) + f->setArray(dadPtr); + else + { + MCAuto< typename Traits::ArrayType > dad2(dadPtr->selectByTupleId(n2oPtr->begin(),n2oPtr->end())); + f->setArray(dad2); + } + f->setMesh(mesh); + MCAuto< typename MLFieldTraits::FMTSType > fmts(MLFieldTraits::FMTSType::New()); + MCAuto< typename MLFieldTraits::F1TSType > f1ts(MLFieldTraits::F1TSType::New()); + f1ts->setFieldNoProfileSBT(f); + fmts->pushBackTimeStep(f1ts); + fs->pushField(fmts); +} + +void AppendMCFieldFrom(MEDCoupling::TypeOfField tf, MEDCouplingMesh *mesh, MEDFileData *mfd, MCAuto da, const DataArrayInt *n2oPtr, double timeStep, int tsId) +{ + static const char FAMFIELD_FOR_CELLS[]="FamilyIdCell"; + static const char FAMFIELD_FOR_NODES[]="FamilyIdNode"; + if(!da || !mesh || !mfd) + throw MZCException("AppendMCFieldFrom : internal error !"); + MEDFileFields *fs(mfd->getFields()); + MEDFileMeshes *ms(mfd->getMeshes()); + if(!fs || !ms) + throw MZCException("AppendMCFieldFrom : internal error 2 !"); + MCAuto dad(MEDCoupling::DynamicCast(da)); + if(dad.isNotNull()) + { + AppendToFields(tf,mesh,n2oPtr,dad,fs,timeStep,tsId); + return ; + } + MCAuto daf(MEDCoupling::DynamicCast(da)); + if(daf.isNotNull()) + { + AppendToFields(tf,mesh,n2oPtr,daf,fs,timeStep,tsId); + return ; + } + MCAuto dai(MEDCoupling::DynamicCast(da)); + if(dai.isNotNull()) + { + std::string fieldName(dai->getName()); + if((fieldName!=FAMFIELD_FOR_CELLS || tf!=MEDCoupling::ON_CELLS) && (fieldName!=FAMFIELD_FOR_NODES || tf!=MEDCoupling::ON_NODES)) + { + AppendToFields(tf,mesh,n2oPtr,dai,fs,timeStep,tsId); + return ; + } + else if(fieldName==FAMFIELD_FOR_CELLS && tf==MEDCoupling::ON_CELLS) + { + MEDFileMesh *mm(ms->getMeshWithName(mesh->getName())); + if(!mm) + throw MZCException("AppendMCFieldFrom : internal error 3 !"); + if(!n2oPtr) + mm->setFamilyFieldArr(mesh->getMeshDimension()-mm->getMeshDimension(),dai); + else + { + MCAuto dai2(dai->selectByTupleId(n2oPtr->begin(),n2oPtr->end())); + mm->setFamilyFieldArr(mesh->getMeshDimension()-mm->getMeshDimension(),dai2); + } + } + else if(fieldName==FAMFIELD_FOR_NODES || tf==MEDCoupling::ON_NODES) + { + MEDFileMesh *mm(ms->getMeshWithName(mesh->getName())); + if(!mm) + throw MZCException("AppendMCFieldFrom : internal error 4 !"); + if(!n2oPtr) + mm->setFamilyFieldArr(1,dai); + else + { + MCAuto dai2(dai->selectByTupleId(n2oPtr->begin(),n2oPtr->end())); + mm->setFamilyFieldArr(1,dai2); + } + } + } +} + +void PutAtLevelDealOrder(MEDFileData *mfd, int meshDimRel, const MicroField& mf, double timeStep, int tsId) +{ + if(!mfd) + throw MZCException("PutAtLevelDealOrder : internal error !"); + MEDFileMesh *mm(mfd->getMeshes()->getMeshAtPos(0)); + MEDFileUMesh *mmu(dynamic_cast(mm)); + if(!mmu) + throw MZCException("PutAtLevelDealOrder : internal error 2 !"); + MCAuto mesh(mf.getMesh()); + mesh->setName(mfd->getMeshes()->getMeshAtPos(0)->getName()); + MCAuto o2n(mesh->sortCellsInMEDFileFrmt()); + const DataArrayInt *o2nPtr(o2n); + MCAuto n2o; + mmu->setMeshAtLevel(meshDimRel,mesh); + const DataArrayInt *n2oPtr(0); + if(o2n) + { + n2o=o2n->invertArrayO2N2N2O(mesh->getNumberOfCells()); + n2oPtr=n2o; + if(n2oPtr && n2oPtr->isIota(mesh->getNumberOfCells())) + n2oPtr=0; + if(n2oPtr) + mm->setRenumFieldArr(meshDimRel,n2o); + } + // + std::vector > cells(mf.getCellFields()); + for(std::vector >::const_iterator it=cells.begin();it!=cells.end();it++) + { + MCAuto da(*it); + AppendMCFieldFrom(MEDCoupling::ON_CELLS,mesh,mfd,da,n2oPtr,timeStep,tsId); + } +} + +void AssignSingleGTMeshes(MEDFileData *mfd, const std::vector< MicroField >& ms, double timeStep, int tsId) +{ + if(!mfd) + throw MZCException("AssignSingleGTMeshes : internal error !"); + MEDFileMesh *mm0(mfd->getMeshes()->getMeshAtPos(0)); + MEDFileUMesh *mm(dynamic_cast(mm0)); + if(!mm) + throw MZCException("AssignSingleGTMeshes : internal error 2 !"); + int meshDim(-std::numeric_limits::max()); + std::map > ms2; + for(std::vector< MicroField >::const_iterator it=ms.begin();it!=ms.end();it++) + { + const MEDCouplingUMesh *elt((*it).getMesh()); + if(elt) + { + int myMeshDim(elt->getMeshDimension()); + meshDim=std::max(meshDim,myMeshDim); + ms2[myMeshDim].push_back(*it); + } + } + if(ms2.empty()) + return ; + for(std::map >::const_iterator it=ms2.begin();it!=ms2.end();it++) + { + const std::vector< MicroField >& vs((*it).second); + if(vs.size()==1) + { + PutAtLevelDealOrder(mfd,(*it).first-meshDim,vs[0],timeStep,tsId); + } + else + { + MicroField merge(vs); + PutAtLevelDealOrder(mfd,(*it).first-meshDim,merge,timeStep,tsId); + } + } +} + +DataArrayDouble *BuildCoordsFrom(vtkPointSet *ds) +{ + if(!ds) + throw MZCException("BuildCoordsFrom : internal error !"); + vtkPoints *pts(ds->GetPoints()); + if(!pts) + throw MZCException("BuildCoordsFrom : internal error 2 !"); + vtkDataArray *data(pts->GetData()); + if(!data) + throw MZCException("BuildCoordsFrom : internal error 3 !"); + return ConvertVTKArrayToMCArrayDoubleForced(data); +} + +void AddNodeFields(MEDFileData *mfd, vtkDataSetAttributes *dsa, double timeStep, int tsId) +{ + if(!mfd || !dsa) + throw MZCException("AddNodeFields : internal error !"); + MEDFileMesh *mm(mfd->getMeshes()->getMeshAtPos(0)); + MEDFileUMesh *mmu(dynamic_cast(mm)); + if(!mmu) + throw MZCException("AddNodeFields : internal error 2 !"); + MCAuto mesh; + if(!mmu->getNonEmptyLevels().empty()) + mesh=mmu->getMeshAtLevel(0); + else + { + mesh=MEDCouplingUMesh::Build0DMeshFromCoords(mmu->getCoords()); + mesh->setName(mmu->getName()); + } + int nba(dsa->GetNumberOfArrays()); + for(int i=0;iGetArray(i)); + const char *name(arr->GetName()); + if(!arr) + continue; + MCAuto da(ConvertVTKArrayToMCArray(arr)); + da->setName(name); + AppendMCFieldFrom(MEDCoupling::ON_NODES,mesh,mfd,da,NULL,timeStep,tsId); + } +} + +std::vector > AddPartFields(const DataArrayInt *part, vtkDataSetAttributes *dsa) +{ + std::vector< MCAuto > ret; + if(!dsa) + return ret; + int nba(dsa->GetNumberOfArrays()); + for(int i=0;iGetArray(i)); + if(!arr) + continue; + const char *name(arr->GetName()); + int nbCompo(arr->GetNumberOfComponents()); + vtkIdType nbTuples(arr->GetNumberOfTuples()); + MCAuto mcarr(ConvertVTKArrayToMCArray(arr)); + if(part) + mcarr=mcarr->selectByTupleId(part->begin(),part->end()); + mcarr->setName(name); + ret.push_back(mcarr); + } + return ret; +} + +std::vector > AddPartFields2(int bg, int end, vtkDataSetAttributes *dsa) +{ + std::vector< MCAuto > ret; + if(!dsa) + return ret; + int nba(dsa->GetNumberOfArrays()); + for(int i=0;iGetArray(i)); + if(!arr) + continue; + const char *name(arr->GetName()); + int nbCompo(arr->GetNumberOfComponents()); + vtkIdType nbTuples(arr->GetNumberOfTuples()); + MCAuto mcarr(ConvertVTKArrayToMCArray(arr)); + mcarr=mcarr->selectByTupleIdSafeSlice(bg,end,1); + mcarr->setName(name); + ret.push_back(mcarr); + } + return ret; +} + +void ConvertFromRectilinearGrid(MEDFileData *ret, vtkRectilinearGrid *ds, const std::vector& context, double timeStep, int tsId) +{ + if(!ds || !ret) + throw MZCException("ConvertFromRectilinearGrid : internal error !"); + // + MCAuto meshes(MEDFileMeshes::New()); + ret->setMeshes(meshes); + MCAuto fields(MEDFileFields::New()); + ret->setFields(fields); + // + MCAuto cmesh(MEDFileCMesh::New()); + meshes->pushMesh(cmesh); + MCAuto cmeshmc(MEDCouplingCMesh::New()); + vtkDataArray *cx(ds->GetXCoordinates()),*cy(ds->GetYCoordinates()),*cz(ds->GetZCoordinates()); + if(cx) + { + MCAuto arr(ConvertVTKArrayToMCArrayDoubleForced(cx)); + cmeshmc->setCoordsAt(0,arr); + } + if(cy) + { + MCAuto arr(ConvertVTKArrayToMCArrayDoubleForced(cy)); + cmeshmc->setCoordsAt(1,arr); + } + if(cz) + { + MCAuto arr(ConvertVTKArrayToMCArrayDoubleForced(cz)); + cmeshmc->setCoordsAt(2,arr); + } + std::string meshName(GetMeshNameWithContext(context)); + cmeshmc->setName(meshName); + cmesh->setMesh(cmeshmc); + std::vector > cellFs(AddPartFields(0,ds->GetCellData())); + for(std::vector >::const_iterator it=cellFs.begin();it!=cellFs.end();it++) + { + MCAuto da(*it); + AppendMCFieldFrom(MEDCoupling::ON_CELLS,cmeshmc,ret,da,NULL,timeStep,tsId); + } + std::vector > nodeFs(AddPartFields(0,ds->GetPointData())); + for(std::vector >::const_iterator it=nodeFs.begin();it!=nodeFs.end();it++) + { + MCAuto da(*it); + AppendMCFieldFrom(MEDCoupling::ON_NODES,cmeshmc,ret,da,NULL,timeStep,tsId); + } +} + +void ConvertFromPolyData(MEDFileData *ret, vtkPolyData *ds, const std::vector& context, double timeStep, int tsId) +{ + if(!ds || !ret) + throw MZCException("ConvertFromPolyData : internal error !"); + // + MCAuto meshes(MEDFileMeshes::New()); + ret->setMeshes(meshes); + MCAuto fields(MEDFileFields::New()); + ret->setFields(fields); + // + MCAuto umesh(MEDFileUMesh::New()); + meshes->pushMesh(umesh); + MCAuto coords(BuildCoordsFrom(ds)); + umesh->setCoords(coords); + umesh->setName(GetMeshNameWithContext(context)); + // + int offset(0); + std::vector< MicroField > ms; + vtkCellArray *cd(ds->GetVerts()); + if(cd) + { + MCAuto subMesh(BuildMeshFromCellArray(cd,coords,0,INTERP_KERNEL::NORM_POINT1)); + if((const MEDCouplingUMesh *)subMesh) + { + std::vector > cellFs(AddPartFields2(offset,offset+subMesh->getNumberOfCells(),ds->GetCellData())); + offset+=subMesh->getNumberOfCells(); + ms.push_back(MicroField(subMesh,cellFs)); + } + } + vtkCellArray *cc(ds->GetLines()); + if(cc) + { + MCAuto subMesh(BuildMeshFromCellArray(cc,coords,1,INTERP_KERNEL::NORM_SEG2)); + if((const MEDCouplingUMesh *)subMesh) + { + std::vector > cellFs(AddPartFields2(offset,offset+subMesh->getNumberOfCells(),ds->GetCellData())); + offset+=subMesh->getNumberOfCells(); + ms.push_back(MicroField(subMesh,cellFs)); + } + } + vtkCellArray *cb(ds->GetPolys()); + if(cb) + { + MCAuto subMesh(BuildMeshFromCellArray(cb,coords,2,INTERP_KERNEL::NORM_POLYGON)); + if((const MEDCouplingUMesh *)subMesh) + { + std::vector > cellFs(AddPartFields2(offset,offset+subMesh->getNumberOfCells(),ds->GetCellData())); + offset+=subMesh->getNumberOfCells(); + ms.push_back(MicroField(subMesh,cellFs)); + } + } + vtkCellArray *ca(ds->GetStrips()); + if(ca) + { + MCAuto ids; + MCAuto subMesh(BuildMeshFromCellArrayTriangleStrip(ca,coords,ids)); + if((const MEDCouplingUMesh *)subMesh) + { + std::vector > cellFs(AddPartFields(ids,ds->GetCellData())); + offset+=subMesh->getNumberOfCells(); + ms.push_back(MicroField(subMesh,cellFs)); + } + } + AssignSingleGTMeshes(ret,ms,timeStep,tsId); + AddNodeFields(ret,ds->GetPointData(),timeStep,tsId); +} + +void ConvertFromUnstructuredGrid(MEDFileData *ret, vtkUnstructuredGrid *ds, const std::vector& context, double timeStep, int tsId) +{ + if(!ds || !ret) + throw MZCException("ConvertFromUnstructuredGrid : internal error !"); + // + MCAuto meshes(MEDFileMeshes::New()); + ret->setMeshes(meshes); + MCAuto fields(MEDFileFields::New()); + ret->setFields(fields); + // + MCAuto umesh(MEDFileUMesh::New()); + meshes->pushMesh(umesh); + MCAuto coords(BuildCoordsFrom(ds)); + umesh->setCoords(coords); + umesh->setName(GetMeshNameWithContext(context)); + vtkIdType nbCells(ds->GetNumberOfCells()); + vtkCellArray *ca(ds->GetCells()); + if(!ca) + return ; + vtkIdType nbEnt(ca->GetNumberOfConnectivityEntries()); + vtkIdType *caPtr(ca->GetPointer()); + vtkUnsignedCharArray *ct(ds->GetCellTypesArray()); + if(!ct) + throw MZCException("ConvertFromUnstructuredGrid : internal error"); + vtkIdTypeArray *cla(ds->GetCellLocationsArray()); + const vtkIdType *claPtr(cla->GetPointer(0)); + if(!cla) + throw MZCException("ConvertFromUnstructuredGrid : internal error 2"); + const unsigned char *ctPtr(ct->GetPointer(0)); + std::map m(ComputeMapOfType()); + MCAuto lev(DataArrayInt::New()) ; lev->alloc(nbCells,1); + int *levPtr(lev->getPointer()); + for(vtkIdType i=0;i::iterator it(m.find(ctPtr[i])); + if(it!=m.end()) + { + const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)(*it).second)); + levPtr[i]=cm.getDimension(); + } + else + { + std::ostringstream oss; oss << "ConvertFromUnstructuredGrid : at pos #" << i << " unrecognized VTK cell with type =" << ctPtr[i]; + throw MZCException(oss.str()); + } + } + int dummy(0); + MCAuto levs(lev->getDifferentValues()); + std::vector< MicroField > ms; + vtkIdTypeArray *faces(ds->GetFaces()),*faceLoc(ds->GetFaceLocations()); + for(const int *curLev=levs->begin();curLev!=levs->end();curLev++) + { + MCAuto m0(MEDCouplingUMesh::New("",*curLev)); + m0->setCoords(coords); m0->allocateCells(); + MCAuto cellIdsCurLev(lev->findIdsEqual(*curLev)); + for(const int *cellId=cellIdsCurLev->begin();cellId!=cellIdsCurLev->end();cellId++) + { + std::map::iterator it(m.find(ctPtr[*cellId])); + vtkIdType offset(claPtr[*cellId]); + vtkIdType sz(caPtr[offset]); + INTERP_KERNEL::NormalizedCellType ct((INTERP_KERNEL::NormalizedCellType)(*it).second); + if(ct!=INTERP_KERNEL::NORM_POLYHED) + { + std::vector conn2(sz); + for(int kk=0;kkinsertNextCell(ct,sz,&conn2[0]); + } + else + { + if(!faces || !faceLoc) + throw MZCException("ConvertFromUnstructuredGrid : faces are expected when there are polyhedra !"); + const vtkIdType *facPtr(faces->GetPointer(0)),*facLocPtr(faceLoc->GetPointer(0)); + std::vector conn; + int off0(facLocPtr[*cellId]); + int nbOfFaces(facPtr[off0++]); + for(int k=0;kinsertNextCell(ct,conn.size(),&conn[0]); + } + } + std::vector > cellFs(AddPartFields(cellIdsCurLev,ds->GetCellData())); + ms.push_back(MicroField(m0,cellFs)); + } + AssignSingleGTMeshes(ret,ms,timeStep,tsId); + AddNodeFields(ret,ds->GetPointData(),timeStep,tsId); +} + +/////////////////// + +void WriteMEDFileFromVTKDataSet(MEDFileData *mfd, vtkDataSet *ds, const std::vector& context, double timeStep, int tsId) +{ + if(!ds || !mfd) + throw MZCException("Internal error in WriteMEDFileFromVTKDataSet."); + vtkPolyData *ds2(vtkPolyData::SafeDownCast(ds)); + vtkUnstructuredGrid *ds3(vtkUnstructuredGrid::SafeDownCast(ds)); + vtkRectilinearGrid *ds4(vtkRectilinearGrid::SafeDownCast(ds)); + if(ds2) + { + ConvertFromPolyData(mfd,ds2,context,timeStep,tsId); + } + else if(ds3) + { + ConvertFromUnstructuredGrid(mfd,ds3,context,timeStep,tsId); + } + else if(ds4) + { + ConvertFromRectilinearGrid(mfd,ds4,context,timeStep,tsId); + } + else + throw MZCException("Unrecognized vtkDataSet ! Sorry ! Try to convert it to UnstructuredGrid to be able to write it !"); +} + +void WriteMEDFileFromVTKMultiBlock(MEDFileData *mfd, vtkMultiBlockDataSet *ds, const std::vector& context, double timeStep, int tsId) +{ + if(!ds || !mfd) + throw MZCException("Internal error in WriteMEDFileFromVTKMultiBlock."); + int nbBlocks(ds->GetNumberOfBlocks()); + if(nbBlocks==1 && context.empty()) + { + vtkDataObject *uniqueElt(ds->GetBlock(0)); + if(!uniqueElt) + throw MZCException("Unique elt in multiblock is NULL !"); + vtkDataSet *uniqueEltc(vtkDataSet::SafeDownCast(uniqueElt)); + if(uniqueEltc) + { + WriteMEDFileFromVTKDataSet(mfd,uniqueEltc,context,timeStep,tsId); + return ; + } + } + for(int i=0;iGetBlock(i)); + std::vector context2; + context2.push_back(i); + if(!elt) + { + std::ostringstream oss; oss << "In context "; + std::copy(context.begin(),context.end(),std::ostream_iterator(oss," ")); + oss << " at pos #" << i << " elt is NULL !"; + throw MZCException(oss.str()); + } + vtkDataSet *elt1(vtkDataSet::SafeDownCast(elt)); + if(elt1) + { + WriteMEDFileFromVTKDataSet(mfd,elt1,context,timeStep,tsId); + continue; + } + vtkMultiBlockDataSet *elt2(vtkMultiBlockDataSet::SafeDownCast(elt)); + if(elt2) + { + WriteMEDFileFromVTKMultiBlock(mfd,elt2,context,timeStep,tsId); + continue; + } + std::ostringstream oss; oss << "In context "; + std::copy(context.begin(),context.end(),std::ostream_iterator(oss," ")); + oss << " at pos #" << i << " elt not recognized data type !"; + throw MZCException(oss.str()); + } +} + +void WriteMEDFileFromVTKGDS(MEDFileData *mfd, vtkDataObject *input, double timeStep, int tsId) +{ + if(!input || !mfd) + throw MZCException("WriteMEDFileFromVTKGDS : internal error !"); + std::vector context; + vtkDataSet *input1(vtkDataSet::SafeDownCast(input)); + if(input1) + { + WriteMEDFileFromVTKDataSet(mfd,input1,context,timeStep,tsId); + return ; + } + vtkMultiBlockDataSet *input2(vtkMultiBlockDataSet::SafeDownCast(input)); + if(input2) + { + WriteMEDFileFromVTKMultiBlock(mfd,input2,context,timeStep,tsId); + return ; + } + throw MZCException("WriteMEDFileFromVTKGDS : not recognized data type !"); +} + +void PutFamGrpInfoIfAny(MEDFileData *mfd, const std::string& meshName, const std::vector& groups, const std::vector& fams) +{ + if(!mfd) + return ; + if(meshName.empty()) + return ; + MEDFileMeshes *meshes(mfd->getMeshes()); + if(!meshes) + return ; + if(meshes->getNumberOfMeshes()!=1) + return ; + MEDFileMesh *mm(meshes->getMeshAtPos(0)); + if(!mm) + return ; + mm->setName(meshName); + for(std::vector::const_iterator it=fams.begin();it!=fams.end();it++) + mm->setFamilyId((*it).getName(),(*it).getID()); + for(std::vector::const_iterator it=groups.begin();it!=groups.end();it++) + mm->setFamiliesOnGroup((*it).getName(),(*it).getFamilies()); + MEDFileFields *fields(mfd->getFields()); + if(!fields) + return ; + for(int i=0;igetNumberOfFields();i++) + { + MEDFileAnyTypeFieldMultiTS *fmts(fields->getFieldAtPos(i)); + if(!fmts) + continue; + fmts->setMeshName(meshName); + } +} diff --git a/src/Plugins/MEDWriter/IO/VTKToMEDMem.hxx b/src/Plugins/MEDWriter/IO/VTKToMEDMem.hxx new file mode 100644 index 00000000..5a0def7d --- /dev/null +++ b/src/Plugins/MEDWriter/IO/VTKToMEDMem.hxx @@ -0,0 +1,86 @@ +// Copyright (C) 2017 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// Author : Anthony Geay (EDF R&D) + +#ifndef __VTKTOMEDMEM_HXX__ +#define __VTKTOMEDMEM_HXX__ + +#include "MEDFileMesh.hxx" +#include "MEDFileField.hxx" +#include "MEDFileData.hxx" +#include "MEDCouplingMemArray.hxx" +#include "MEDCouplingFieldInt.hxx" +#include "MEDCouplingFieldFloat.hxx" +#include "MEDCouplingFieldDouble.hxx" +#include "MEDCouplingRefCountObject.hxx" +#include "MEDLoaderTraits.hxx" + +#include +#include + +/////////////////// + +class vtkDataSet; + +class MZCException : public std::exception +{ +public: + MZCException(const std::string& s):_reason(s) { } + virtual const char *what() const throw() { return _reason.c_str(); } + virtual ~MZCException() throw() { } +private: + std::string _reason; +}; + +namespace VTKToMEDMem +{ + class Grp + { + public: + Grp(const std::string& name):_name(name) { } + void setFamilies(const std::vector& fams) { _fams=fams; } + std::string getName() const { return _name; } + std::vector getFamilies() const { return _fams; } + private: + std::string _name; + std::vector _fams; + }; + + class Fam + { + public: + Fam(const std::string& name); + std::string getName() const { return _name; } + int getID() const { return _id; } + private: + std::string _name; + int _id; + }; +} + +class vtkDataObject; + +void WriteMEDFileFromVTKDataSet(MEDCoupling::MEDFileData *mfd, vtkDataSet *ds, const std::vector& context, double timeStep, int tsId); + +void WriteMEDFileFromVTKGDS(MEDCoupling::MEDFileData *mfd, vtkDataObject *input, double timeStep, int tsId); + +void PutFamGrpInfoIfAny(MEDCoupling::MEDFileData *mfd, const std::string& meshName, const std::vector& groups, const std::vector& fams); + +#endif + diff --git a/src/Plugins/MEDWriter/IO/vtkMEDWriter.cxx b/src/Plugins/MEDWriter/IO/vtkMEDWriter.cxx index 9367b114..65537b3e 100644 --- a/src/Plugins/MEDWriter/IO/vtkMEDWriter.cxx +++ b/src/Plugins/MEDWriter/IO/vtkMEDWriter.cxx @@ -19,6 +19,7 @@ // Author : Anthony Geay (EDF R&D) #include "vtkMEDWriter.h" +#include "VTKToMEDMem.hxx" #include "vtkAdjacentVertexIterator.h" #include "vtkIntArray.h" @@ -71,775 +72,12 @@ #include #include -using MEDCoupling::MEDFileData; -using MEDCoupling::MEDFileMesh; -using MEDCoupling::MEDFileCMesh; -using MEDCoupling::MEDFileUMesh; -using MEDCoupling::MEDFileFields; -using MEDCoupling::MEDFileMeshes; - -using MEDCoupling::MEDFileIntField1TS; -using MEDCoupling::MEDFileField1TS; -using MEDCoupling::MEDFileIntFieldMultiTS; -using MEDCoupling::MEDFileFieldMultiTS; -using MEDCoupling::MEDFileAnyTypeFieldMultiTS; -using MEDCoupling::DataArray; -using MEDCoupling::DataArrayInt; -using MEDCoupling::DataArrayFloat; -using MEDCoupling::DataArrayDouble; -using MEDCoupling::MEDCouplingMesh; -using MEDCoupling::MEDCouplingUMesh; -using MEDCoupling::MEDCouplingCMesh; -using MEDCoupling::MEDCouplingFieldDouble; -using MEDCoupling::MEDCouplingFieldFloat; -using MEDCoupling::MEDCouplingFieldInt; -using MEDCoupling::MCAuto; -using MEDCoupling::Traits; -using MEDCoupling::MLFieldTraits; - vtkStandardNewMacro(vtkMEDWriter); -/////////////////// - -class MZCException : public std::exception -{ -public: - MZCException(const std::string& s):_reason(s) { } - virtual const char *what() const throw() { return _reason.c_str(); } - virtual ~MZCException() throw() { } -private: - std::string _reason; -}; - -template -class MEDFileVTKTraits -{ -public: - typedef void VtkType; - typedef void MCType; -}; - -template<> -class MEDFileVTKTraits -{ -public: - typedef vtkIntArray VtkType; - typedef MEDCoupling::DataArrayInt MCType; -}; - -template<> -class MEDFileVTKTraits -{ -public: - typedef vtkFloatArray VtkType; - typedef MEDCoupling::DataArrayFloat MCType; -}; - -template<> -class MEDFileVTKTraits -{ -public: - typedef vtkDoubleArray VtkType; - typedef MEDCoupling::DataArrayDouble MCType; -}; - -/////////////////// - -std::map ComputeMapOfType() -{ - std::map ret; - int nbOfTypesInMC(sizeof(MEDCouplingUMesh::MEDCOUPLING2VTKTYPETRADUCER)/sizeof(int)); - for(int i=0;i& context) -{ - static const char DFT_MESH_NAME[]="Mesh"; - if(context.empty()) - return DFT_MESH_NAME; - std::ostringstream oss; oss << DFT_MESH_NAME; - for(std::vector::const_iterator it=context.begin();it!=context.end();it++) - oss << "_" << *it; - return oss.str(); -} - -DataArrayInt *ConvertVTKArrayToMCArrayInt(vtkDataArray *data) -{ - if(!data) - throw MZCException("ConvertVTKArrayToMCArrayInt : internal error !"); - int nbTuples(data->GetNumberOfTuples()),nbComp(data->GetNumberOfComponents()); - std::size_t nbElts(nbTuples*nbComp); - MCAuto ret(DataArrayInt::New()); - ret->alloc(nbTuples,nbComp); - for(int i=0;iGetComponentName(i)); - if(comp) - ret->setInfoOnComponent(i,comp); - } - int *ptOut(ret->getPointer()); - vtkIntArray *d0(vtkIntArray::SafeDownCast(data)); - if(d0) - { - const int *pt(d0->GetPointer(0)); - std::copy(pt,pt+nbElts,ptOut); - return ret.retn(); - } - vtkLongArray *d1(vtkLongArray::SafeDownCast(data)); - if(d1) - { - const long *pt(d1->GetPointer(0)); - std::copy(pt,pt+nbElts,ptOut); - return ret.retn(); - } - vtkUnsignedCharArray *d2(vtkUnsignedCharArray::SafeDownCast(data)); - if(d2) - { - const unsigned char *pt(d2->GetPointer(0)); - std::copy(pt,pt+nbElts,ptOut); - return ret.retn(); - } - std::ostringstream oss; - oss << "ConvertVTKArrayToMCArrayInt : unrecognized array \"" << typeid(*data).name() << "\" type !"; - throw MZCException(oss.str()); -} - -template -typename Traits::ArrayType *ConvertVTKArrayToMCArrayDouble(vtkDataArray *data) -{ - if(!data) - throw MZCException("ConvertVTKArrayToMCArrayDouble : internal error !"); - int nbTuples(data->GetNumberOfTuples()),nbComp(data->GetNumberOfComponents()); - std::size_t nbElts(nbTuples*nbComp); - MCAuto< typename Traits::ArrayType > ret(Traits::ArrayType::New()); - ret->alloc(nbTuples,nbComp); - for(int i=0;iGetComponentName(i)); - if(comp) - ret->setInfoOnComponent(i,comp); - else - { - if(nbComp>1 && nbComp<=3) - { - char tmp[2]; - tmp[0]='X'+i; tmp[1]='\0'; - ret->setInfoOnComponent(i,tmp); - } - } - } - T *ptOut(ret->getPointer()); - typename MEDFileVTKTraits::VtkType *d0(MEDFileVTKTraits::VtkType::SafeDownCast(data)); - if(d0) - { - const T *pt(d0->GetPointer(0)); - for(std::size_t i=0;iGetClassName() << "\" type !"; - throw MZCException(oss.str()); -} - -DataArrayDouble *ConvertVTKArrayToMCArrayDoubleForced(vtkDataArray *data) -{ - if(!data) - throw MZCException("ConvertVTKArrayToMCArrayDoubleForced : internal error 0 !"); - vtkFloatArray *d0(vtkFloatArray::SafeDownCast(data)); - if(d0) - { - MCAuto ret(ConvertVTKArrayToMCArrayDouble(data)); - MCAuto ret2(ret->convertToDblArr()); - return ret2.retn(); - } - vtkDoubleArray *d1(vtkDoubleArray::SafeDownCast(data)); - if(d1) - return ConvertVTKArrayToMCArrayDouble(data); - throw MZCException("ConvertVTKArrayToMCArrayDoubleForced : unrecognized type of data for double !"); -} - -DataArray *ConvertVTKArrayToMCArray(vtkDataArray *data) -{ - if(!data) - throw MZCException("ConvertVTKArrayToMCArray : internal error !"); - vtkFloatArray *d0(vtkFloatArray::SafeDownCast(data)); - if(d0) - return ConvertVTKArrayToMCArrayDouble(data); - vtkDoubleArray *d1(vtkDoubleArray::SafeDownCast(data)); - if(d1) - return ConvertVTKArrayToMCArrayDouble(data); - vtkIntArray *d2(vtkIntArray::SafeDownCast(data)); - vtkLongArray *d3(vtkLongArray::SafeDownCast(data)); - vtkUnsignedCharArray *d4(vtkUnsignedCharArray::SafeDownCast(data)); - if(d2 || d3 || d4) - return ConvertVTKArrayToMCArrayInt(data); - std::ostringstream oss; - oss << "ConvertVTKArrayToMCArray : unrecognized array \"" << typeid(*data).name() << "\" type !"; - throw MZCException(oss.str()); -} - -MEDCouplingUMesh *BuildMeshFromCellArray(vtkCellArray *ca, DataArrayDouble *coords, int meshDim, INTERP_KERNEL::NormalizedCellType type) -{ - MCAuto subMesh(MEDCouplingUMesh::New("",meshDim)); - subMesh->setCoords(coords); subMesh->allocateCells(); - int nbCells(ca->GetNumberOfCells()); - if(nbCells==0) - return 0; - vtkIdType nbEntries(ca->GetNumberOfConnectivityEntries()); - const vtkIdType *conn(ca->GetPointer()); - for(int i=0;i conn2(sz); - for(int jj=0;jjinsertNextCell(type,sz,&conn2[0]); - conn+=sz; - } - return subMesh.retn(); -} - -MEDCouplingUMesh *BuildMeshFromCellArrayTriangleStrip(vtkCellArray *ca, DataArrayDouble *coords, MCAuto& ids) -{ - MCAuto subMesh(MEDCouplingUMesh::New("",2)); - subMesh->setCoords(coords); subMesh->allocateCells(); - int nbCells(ca->GetNumberOfCells()); - if(nbCells==0) - return 0; - vtkIdType nbEntries(ca->GetNumberOfConnectivityEntries()); - const vtkIdType *conn(ca->GetPointer()); - ids=DataArrayInt::New() ; ids->alloc(0,1); - for(int i=0;i0) - { - for(int j=0;jinsertNextCell(INTERP_KERNEL::NORM_TRI3,3,conn2); - ids->pushBackSilent(i); - } - } - else - { - std::ostringstream oss; oss << "BuildMeshFromCellArrayTriangleStrip : on cell #" << i << " the triangle stip looks bab !"; - throw MZCException(oss.str()); - } - conn+=sz; - } - return subMesh.retn(); -} - -class MicroField -{ -public: - MicroField(const MCAuto& m, const std::vector >& cellFs):_m(m),_cellFs(cellFs) { } - MicroField(const std::vector< MicroField >& vs); - void setNodeFields(const std::vector >& nf) { _nodeFs=nf; } - MCAuto getMesh() const { return _m; } - std::vector > getCellFields() const { return _cellFs; } -private: - MCAuto _m; - std::vector > _cellFs; - std::vector > _nodeFs; -}; - -MicroField::MicroField(const std::vector< MicroField >& vs) -{ - std::size_t sz(vs.size()); - std::vector vs2(sz); - std::vector< std::vector< MCAuto > > arrs2(sz); - int nbElts(-1); - for(std::size_t ii=0;ii arrsTmp(sz); - for(std::size_t jj=0;jj -void AppendToFields(MEDCoupling::TypeOfField tf, MEDCouplingMesh *mesh, const DataArrayInt *n2oPtr, typename MEDFileVTKTraits::MCType *dadPtr, MEDFileFields *fs, double timeStep, int tsId) -{ - std::string fieldName(dadPtr->getName()); - MCAuto< typename Traits::FieldType > f(Traits::FieldType::New(tf)); - f->setTime(timeStep,tsId,0); - { - std::string fieldNameForChuckNorris(MEDCoupling::MEDFileAnyTypeField1TSWithoutSDA::FieldNameToMEDFileConvention(fieldName)); - f->setName(fieldNameForChuckNorris); - } - if(!n2oPtr) - f->setArray(dadPtr); - else - { - MCAuto< typename Traits::ArrayType > dad2(dadPtr->selectByTupleId(n2oPtr->begin(),n2oPtr->end())); - f->setArray(dad2); - } - f->setMesh(mesh); - MCAuto< typename MLFieldTraits::FMTSType > fmts(MLFieldTraits::FMTSType::New()); - MCAuto< typename MLFieldTraits::F1TSType > f1ts(MLFieldTraits::F1TSType::New()); - f1ts->setFieldNoProfileSBT(f); - fmts->pushBackTimeStep(f1ts); - fs->pushField(fmts); -} - -void AppendMCFieldFrom(MEDCoupling::TypeOfField tf, MEDCouplingMesh *mesh, MEDFileData *mfd, MCAuto da, const DataArrayInt *n2oPtr, double timeStep, int tsId) -{ - static const char FAMFIELD_FOR_CELLS[]="FamilyIdCell"; - static const char FAMFIELD_FOR_NODES[]="FamilyIdNode"; - if(!da || !mesh || !mfd) - throw MZCException("AppendMCFieldFrom : internal error !"); - MEDFileFields *fs(mfd->getFields()); - MEDFileMeshes *ms(mfd->getMeshes()); - if(!fs || !ms) - throw MZCException("AppendMCFieldFrom : internal error 2 !"); - MCAuto dad(MEDCoupling::DynamicCast(da)); - if(dad.isNotNull()) - { - AppendToFields(tf,mesh,n2oPtr,dad,fs,timeStep,tsId); - return ; - } - MCAuto daf(MEDCoupling::DynamicCast(da)); - if(daf.isNotNull()) - { - AppendToFields(tf,mesh,n2oPtr,daf,fs,timeStep,tsId); - return ; - } - MCAuto dai(MEDCoupling::DynamicCast(da)); - if(dai.isNotNull()) - { - std::string fieldName(dai->getName()); - if((fieldName!=FAMFIELD_FOR_CELLS || tf!=MEDCoupling::ON_CELLS) && (fieldName!=FAMFIELD_FOR_NODES || tf!=MEDCoupling::ON_NODES)) - { - AppendToFields(tf,mesh,n2oPtr,dai,fs,timeStep,tsId); - return ; - } - else if(fieldName==FAMFIELD_FOR_CELLS && tf==MEDCoupling::ON_CELLS) - { - MEDFileMesh *mm(ms->getMeshWithName(mesh->getName())); - if(!mm) - throw MZCException("AppendMCFieldFrom : internal error 3 !"); - if(!n2oPtr) - mm->setFamilyFieldArr(mesh->getMeshDimension()-mm->getMeshDimension(),dai); - else - { - MCAuto dai2(dai->selectByTupleId(n2oPtr->begin(),n2oPtr->end())); - mm->setFamilyFieldArr(mesh->getMeshDimension()-mm->getMeshDimension(),dai2); - } - } - else if(fieldName==FAMFIELD_FOR_NODES || tf==MEDCoupling::ON_NODES) - { - MEDFileMesh *mm(ms->getMeshWithName(mesh->getName())); - if(!mm) - throw MZCException("AppendMCFieldFrom : internal error 4 !"); - if(!n2oPtr) - mm->setFamilyFieldArr(1,dai); - else - { - MCAuto dai2(dai->selectByTupleId(n2oPtr->begin(),n2oPtr->end())); - mm->setFamilyFieldArr(1,dai2); - } - } - } -} - -void PutAtLevelDealOrder(MEDFileData *mfd, int meshDimRel, const MicroField& mf, double timeStep, int tsId) -{ - if(!mfd) - throw MZCException("PutAtLevelDealOrder : internal error !"); - MEDFileMesh *mm(mfd->getMeshes()->getMeshAtPos(0)); - MEDFileUMesh *mmu(dynamic_cast(mm)); - if(!mmu) - throw MZCException("PutAtLevelDealOrder : internal error 2 !"); - MCAuto mesh(mf.getMesh()); - mesh->setName(mfd->getMeshes()->getMeshAtPos(0)->getName()); - MCAuto o2n(mesh->sortCellsInMEDFileFrmt()); - const DataArrayInt *o2nPtr(o2n); - MCAuto n2o; - mmu->setMeshAtLevel(meshDimRel,mesh); - const DataArrayInt *n2oPtr(0); - if(o2n) - { - n2o=o2n->invertArrayO2N2N2O(mesh->getNumberOfCells()); - n2oPtr=n2o; - if(n2oPtr && n2oPtr->isIota(mesh->getNumberOfCells())) - n2oPtr=0; - if(n2oPtr) - mm->setRenumFieldArr(meshDimRel,n2o); - } - // - std::vector > cells(mf.getCellFields()); - for(std::vector >::const_iterator it=cells.begin();it!=cells.end();it++) - { - MCAuto da(*it); - AppendMCFieldFrom(MEDCoupling::ON_CELLS,mesh,mfd,da,n2oPtr,timeStep,tsId); - } -} - -void AssignSingleGTMeshes(MEDFileData *mfd, const std::vector< MicroField >& ms, double timeStep, int tsId) -{ - if(!mfd) - throw MZCException("AssignSingleGTMeshes : internal error !"); - MEDFileMesh *mm0(mfd->getMeshes()->getMeshAtPos(0)); - MEDFileUMesh *mm(dynamic_cast(mm0)); - if(!mm) - throw MZCException("AssignSingleGTMeshes : internal error 2 !"); - int meshDim(-std::numeric_limits::max()); - std::map > ms2; - for(std::vector< MicroField >::const_iterator it=ms.begin();it!=ms.end();it++) - { - const MEDCouplingUMesh *elt((*it).getMesh()); - if(elt) - { - int myMeshDim(elt->getMeshDimension()); - meshDim=std::max(meshDim,myMeshDim); - ms2[myMeshDim].push_back(*it); - } - } - if(ms2.empty()) - return ; - for(std::map >::const_iterator it=ms2.begin();it!=ms2.end();it++) - { - const std::vector< MicroField >& vs((*it).second); - if(vs.size()==1) - { - PutAtLevelDealOrder(mfd,(*it).first-meshDim,vs[0],timeStep,tsId); - } - else - { - MicroField merge(vs); - PutAtLevelDealOrder(mfd,(*it).first-meshDim,merge,timeStep,tsId); - } - } -} - -DataArrayDouble *BuildCoordsFrom(vtkPointSet *ds) -{ - if(!ds) - throw MZCException("BuildCoordsFrom : internal error !"); - vtkPoints *pts(ds->GetPoints()); - if(!pts) - throw MZCException("BuildCoordsFrom : internal error 2 !"); - vtkDataArray *data(pts->GetData()); - if(!data) - throw MZCException("BuildCoordsFrom : internal error 3 !"); - return ConvertVTKArrayToMCArrayDoubleForced(data); -} - -void AddNodeFields(MEDFileData *mfd, vtkDataSetAttributes *dsa, double timeStep, int tsId) -{ - if(!mfd || !dsa) - throw MZCException("AddNodeFields : internal error !"); - MEDFileMesh *mm(mfd->getMeshes()->getMeshAtPos(0)); - MEDFileUMesh *mmu(dynamic_cast(mm)); - if(!mmu) - throw MZCException("AddNodeFields : internal error 2 !"); - MCAuto mesh; - if(!mmu->getNonEmptyLevels().empty()) - mesh=mmu->getMeshAtLevel(0); - else - { - mesh=MEDCouplingUMesh::Build0DMeshFromCoords(mmu->getCoords()); - mesh->setName(mmu->getName()); - } - int nba(dsa->GetNumberOfArrays()); - for(int i=0;iGetArray(i)); - const char *name(arr->GetName()); - if(!arr) - continue; - MCAuto da(ConvertVTKArrayToMCArray(arr)); - da->setName(name); - AppendMCFieldFrom(MEDCoupling::ON_NODES,mesh,mfd,da,NULL,timeStep,tsId); - } -} - -std::vector > AddPartFields(const DataArrayInt *part, vtkDataSetAttributes *dsa) -{ - std::vector< MCAuto > ret; - if(!dsa) - return ret; - int nba(dsa->GetNumberOfArrays()); - for(int i=0;iGetArray(i)); - if(!arr) - continue; - const char *name(arr->GetName()); - int nbCompo(arr->GetNumberOfComponents()); - vtkIdType nbTuples(arr->GetNumberOfTuples()); - MCAuto mcarr(ConvertVTKArrayToMCArray(arr)); - if(part) - mcarr=mcarr->selectByTupleId(part->begin(),part->end()); - mcarr->setName(name); - ret.push_back(mcarr); - } - return ret; -} - -std::vector > AddPartFields2(int bg, int end, vtkDataSetAttributes *dsa) -{ - std::vector< MCAuto > ret; - if(!dsa) - return ret; - int nba(dsa->GetNumberOfArrays()); - for(int i=0;iGetArray(i)); - if(!arr) - continue; - const char *name(arr->GetName()); - int nbCompo(arr->GetNumberOfComponents()); - vtkIdType nbTuples(arr->GetNumberOfTuples()); - MCAuto mcarr(ConvertVTKArrayToMCArray(arr)); - mcarr=mcarr->selectByTupleIdSafeSlice(bg,end,1); - mcarr->setName(name); - ret.push_back(mcarr); - } - return ret; -} - -void ConvertFromRectilinearGrid(MEDFileData *ret, vtkRectilinearGrid *ds, const std::vector& context, double timeStep, int tsId) -{ - if(!ds || !ret) - throw MZCException("ConvertFromRectilinearGrid : internal error !"); - // - MCAuto meshes(MEDFileMeshes::New()); - ret->setMeshes(meshes); - MCAuto fields(MEDFileFields::New()); - ret->setFields(fields); - // - MCAuto cmesh(MEDFileCMesh::New()); - meshes->pushMesh(cmesh); - MCAuto cmeshmc(MEDCouplingCMesh::New()); - vtkDataArray *cx(ds->GetXCoordinates()),*cy(ds->GetYCoordinates()),*cz(ds->GetZCoordinates()); - if(cx) - { - MCAuto arr(ConvertVTKArrayToMCArrayDoubleForced(cx)); - cmeshmc->setCoordsAt(0,arr); - } - if(cy) - { - MCAuto arr(ConvertVTKArrayToMCArrayDoubleForced(cy)); - cmeshmc->setCoordsAt(1,arr); - } - if(cz) - { - MCAuto arr(ConvertVTKArrayToMCArrayDoubleForced(cz)); - cmeshmc->setCoordsAt(2,arr); - } - std::string meshName(GetMeshNameWithContext(context)); - cmeshmc->setName(meshName); - cmesh->setMesh(cmeshmc); - std::vector > cellFs(AddPartFields(0,ds->GetCellData())); - for(std::vector >::const_iterator it=cellFs.begin();it!=cellFs.end();it++) - { - MCAuto da(*it); - AppendMCFieldFrom(MEDCoupling::ON_CELLS,cmeshmc,ret,da,NULL,timeStep,tsId); - } - std::vector > nodeFs(AddPartFields(0,ds->GetPointData())); - for(std::vector >::const_iterator it=nodeFs.begin();it!=nodeFs.end();it++) - { - MCAuto da(*it); - AppendMCFieldFrom(MEDCoupling::ON_NODES,cmeshmc,ret,da,NULL,timeStep,tsId); - } -} - -void ConvertFromPolyData(MEDFileData *ret, vtkPolyData *ds, const std::vector& context, double timeStep, int tsId) -{ - if(!ds || !ret) - throw MZCException("ConvertFromPolyData : internal error !"); - // - MCAuto meshes(MEDFileMeshes::New()); - ret->setMeshes(meshes); - MCAuto fields(MEDFileFields::New()); - ret->setFields(fields); - // - MCAuto umesh(MEDFileUMesh::New()); - meshes->pushMesh(umesh); - MCAuto coords(BuildCoordsFrom(ds)); - umesh->setCoords(coords); - umesh->setName(GetMeshNameWithContext(context)); - // - int offset(0); - std::vector< MicroField > ms; - vtkCellArray *cd(ds->GetVerts()); - if(cd) - { - MCAuto subMesh(BuildMeshFromCellArray(cd,coords,0,INTERP_KERNEL::NORM_POINT1)); - if((const MEDCouplingUMesh *)subMesh) - { - std::vector > cellFs(AddPartFields2(offset,offset+subMesh->getNumberOfCells(),ds->GetCellData())); - offset+=subMesh->getNumberOfCells(); - ms.push_back(MicroField(subMesh,cellFs)); - } - } - vtkCellArray *cc(ds->GetLines()); - if(cc) - { - MCAuto subMesh(BuildMeshFromCellArray(cc,coords,1,INTERP_KERNEL::NORM_SEG2)); - if((const MEDCouplingUMesh *)subMesh) - { - std::vector > cellFs(AddPartFields2(offset,offset+subMesh->getNumberOfCells(),ds->GetCellData())); - offset+=subMesh->getNumberOfCells(); - ms.push_back(MicroField(subMesh,cellFs)); - } - } - vtkCellArray *cb(ds->GetPolys()); - if(cb) - { - MCAuto subMesh(BuildMeshFromCellArray(cb,coords,2,INTERP_KERNEL::NORM_POLYGON)); - if((const MEDCouplingUMesh *)subMesh) - { - std::vector > cellFs(AddPartFields2(offset,offset+subMesh->getNumberOfCells(),ds->GetCellData())); - offset+=subMesh->getNumberOfCells(); - ms.push_back(MicroField(subMesh,cellFs)); - } - } - vtkCellArray *ca(ds->GetStrips()); - if(ca) - { - MCAuto ids; - MCAuto subMesh(BuildMeshFromCellArrayTriangleStrip(ca,coords,ids)); - if((const MEDCouplingUMesh *)subMesh) - { - std::vector > cellFs(AddPartFields(ids,ds->GetCellData())); - offset+=subMesh->getNumberOfCells(); - ms.push_back(MicroField(subMesh,cellFs)); - } - } - AssignSingleGTMeshes(ret,ms,timeStep,tsId); - AddNodeFields(ret,ds->GetPointData(),timeStep,tsId); -} - -void ConvertFromUnstructuredGrid(MEDFileData *ret, vtkUnstructuredGrid *ds, const std::vector& context, double timeStep, int tsId) -{ - if(!ds || !ret) - throw MZCException("ConvertFromUnstructuredGrid : internal error !"); - // - MCAuto meshes(MEDFileMeshes::New()); - ret->setMeshes(meshes); - MCAuto fields(MEDFileFields::New()); - ret->setFields(fields); - // - MCAuto umesh(MEDFileUMesh::New()); - meshes->pushMesh(umesh); - MCAuto coords(BuildCoordsFrom(ds)); - umesh->setCoords(coords); - umesh->setName(GetMeshNameWithContext(context)); - vtkIdType nbCells(ds->GetNumberOfCells()); - vtkCellArray *ca(ds->GetCells()); - if(!ca) - return ; - vtkIdType nbEnt(ca->GetNumberOfConnectivityEntries()); - vtkIdType *caPtr(ca->GetPointer()); - vtkUnsignedCharArray *ct(ds->GetCellTypesArray()); - if(!ct) - throw MZCException("ConvertFromUnstructuredGrid : internal error"); - vtkIdTypeArray *cla(ds->GetCellLocationsArray()); - const vtkIdType *claPtr(cla->GetPointer(0)); - if(!cla) - throw MZCException("ConvertFromUnstructuredGrid : internal error 2"); - const unsigned char *ctPtr(ct->GetPointer(0)); - std::map m(ComputeMapOfType()); - MCAuto lev(DataArrayInt::New()) ; lev->alloc(nbCells,1); - int *levPtr(lev->getPointer()); - for(vtkIdType i=0;i::iterator it(m.find(ctPtr[i])); - if(it!=m.end()) - { - const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel((INTERP_KERNEL::NormalizedCellType)(*it).second)); - levPtr[i]=cm.getDimension(); - } - else - { - std::ostringstream oss; oss << "ConvertFromUnstructuredGrid : at pos #" << i << " unrecognized VTK cell with type =" << ctPtr[i]; - throw MZCException(oss.str()); - } - } - int dummy(0); - MCAuto levs(lev->getDifferentValues()); - std::vector< MicroField > ms; - vtkIdTypeArray *faces(ds->GetFaces()),*faceLoc(ds->GetFaceLocations()); - for(const int *curLev=levs->begin();curLev!=levs->end();curLev++) - { - MCAuto m0(MEDCouplingUMesh::New("",*curLev)); - m0->setCoords(coords); m0->allocateCells(); - MCAuto cellIdsCurLev(lev->findIdsEqual(*curLev)); - for(const int *cellId=cellIdsCurLev->begin();cellId!=cellIdsCurLev->end();cellId++) - { - std::map::iterator it(m.find(ctPtr[*cellId])); - vtkIdType offset(claPtr[*cellId]); - vtkIdType sz(caPtr[offset]); - INTERP_KERNEL::NormalizedCellType ct((INTERP_KERNEL::NormalizedCellType)(*it).second); - if(ct!=INTERP_KERNEL::NORM_POLYHED) - { - std::vector conn2(sz); - for(int kk=0;kkinsertNextCell(ct,sz,&conn2[0]); - } - else - { - if(!faces || !faceLoc) - throw MZCException("ConvertFromUnstructuredGrid : faces are expected when there are polyhedra !"); - const vtkIdType *facPtr(faces->GetPointer(0)),*facLocPtr(faceLoc->GetPointer(0)); - std::vector conn; - int off0(facLocPtr[*cellId]); - int nbOfFaces(facPtr[off0++]); - for(int k=0;kinsertNextCell(ct,conn.size(),&conn[0]); - } - } - std::vector > cellFs(AddPartFields(cellIdsCurLev,ds->GetCellData())); - ms.push_back(MicroField(m0,cellFs)); - } - AssignSingleGTMeshes(ret,ms,timeStep,tsId); - AddNodeFields(ret,ds->GetPointData(),timeStep,tsId); -} - -/////////////////// - -vtkMEDWriter::vtkMEDWriter():WriteAllTimeSteps(0),NumberOfTimeSteps(0),CurrentTimeIndex(0),FileName(0) -{ -} - -vtkMEDWriter::~vtkMEDWriter() -{ -} +using MEDCoupling::MEDFileData; +using MEDCoupling::MCAuto; +using VTKToMEDMem::Grp; +using VTKToMEDMem::Fam; vtkInformationDataObjectMetaDataKey *GetMEDReaderMetaDataIfAny() { @@ -879,37 +117,6 @@ bool IsInformationOK(vtkInformation *info) return false; } -class Grp -{ -public: - Grp(const std::string& name):_name(name) { } - void setFamilies(const std::vector& fams) { _fams=fams; } - std::string getName() const { return _name; } - std::vector getFamilies() const { return _fams; } -private: - std::string _name; - std::vector _fams; -}; - -class Fam -{ -public: - Fam(const std::string& name) - { - static const char ZE_SEP[]="@@][@@"; - std::size_t pos(name.find(ZE_SEP)); - std::string name0(name.substr(0,pos)),name1(name.substr(pos+strlen(ZE_SEP))); - std::istringstream iss(name1); - iss >> _id; - _name=name0; - } - std::string getName() const { return _name; } - int getID() const { return _id; } -private: - std::string _name; - int _id; -}; - void LoadFamGrpMapInfo(vtkMutableDirectedGraph *sil, std::string& meshName, std::vector& groups, std::vector& fams) { if(!sil) @@ -975,6 +182,14 @@ void LoadFamGrpMapInfo(vtkMutableDirectedGraph *sil, std::string& meshName, std: it0->Delete(); } +vtkMEDWriter::vtkMEDWriter():WriteAllTimeSteps(0),NumberOfTimeSteps(0),CurrentTimeIndex(0),FileName(0) +{ +} + +vtkMEDWriter::~vtkMEDWriter() +{ +} + int vtkMEDWriter::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) { //std::cerr << "########################################## vtkMEDWriter::RequestInformation ########################################## " << (const char *) this->FileName << std::endl; @@ -997,128 +212,6 @@ int vtkMEDWriter::RequestUpdateExtent(vtkInformation* vtkNotUsed(request), vtkIn return 1; } -void WriteMEDFileFromVTKDataSet(MEDFileData *mfd, vtkDataSet *ds, const std::vector& context, double timeStep, int tsId) -{ - if(!ds || !mfd) - throw MZCException("Internal error in WriteMEDFileFromVTKDataSet."); - vtkPolyData *ds2(vtkPolyData::SafeDownCast(ds)); - vtkUnstructuredGrid *ds3(vtkUnstructuredGrid::SafeDownCast(ds)); - vtkRectilinearGrid *ds4(vtkRectilinearGrid::SafeDownCast(ds)); - if(ds2) - { - ConvertFromPolyData(mfd,ds2,context,timeStep,tsId); - } - else if(ds3) - { - ConvertFromUnstructuredGrid(mfd,ds3,context,timeStep,tsId); - } - else if(ds4) - { - ConvertFromRectilinearGrid(mfd,ds4,context,timeStep,tsId); - } - else - throw MZCException("Unrecognized vtkDataSet ! Sorry ! Try to convert it to UnstructuredGrid to be able to write it !"); -} - -void WriteMEDFileFromVTKMultiBlock(MEDFileData *mfd, vtkMultiBlockDataSet *ds, const std::vector& context, double timeStep, int tsId) -{ - if(!ds || !mfd) - throw MZCException("Internal error in WriteMEDFileFromVTKMultiBlock."); - int nbBlocks(ds->GetNumberOfBlocks()); - if(nbBlocks==1 && context.empty()) - { - vtkDataObject *uniqueElt(ds->GetBlock(0)); - if(!uniqueElt) - throw MZCException("Unique elt in multiblock is NULL !"); - vtkDataSet *uniqueEltc(vtkDataSet::SafeDownCast(uniqueElt)); - if(uniqueEltc) - { - WriteMEDFileFromVTKDataSet(mfd,uniqueEltc,context,timeStep,tsId); - return ; - } - } - for(int i=0;iGetBlock(i)); - std::vector context2; - context2.push_back(i); - if(!elt) - { - std::ostringstream oss; oss << "In context "; - std::copy(context.begin(),context.end(),std::ostream_iterator(oss," ")); - oss << " at pos #" << i << " elt is NULL !"; - throw MZCException(oss.str()); - } - vtkDataSet *elt1(vtkDataSet::SafeDownCast(elt)); - if(elt1) - { - WriteMEDFileFromVTKDataSet(mfd,elt1,context,timeStep,tsId); - continue; - } - vtkMultiBlockDataSet *elt2(vtkMultiBlockDataSet::SafeDownCast(elt)); - if(elt2) - { - WriteMEDFileFromVTKMultiBlock(mfd,elt2,context,timeStep,tsId); - continue; - } - std::ostringstream oss; oss << "In context "; - std::copy(context.begin(),context.end(),std::ostream_iterator(oss," ")); - oss << " at pos #" << i << " elt not recognized data type !"; - throw MZCException(oss.str()); - } -} - -void WriteMEDFileFromVTKGDS(MEDFileData *mfd, vtkDataObject *input, double timeStep, int tsId) -{ - if(!input || !mfd) - throw MZCException("WriteMEDFileFromVTKGDS : internal error !"); - std::vector context; - vtkDataSet *input1(vtkDataSet::SafeDownCast(input)); - if(input1) - { - WriteMEDFileFromVTKDataSet(mfd,input1,context,timeStep,tsId); - return ; - } - vtkMultiBlockDataSet *input2(vtkMultiBlockDataSet::SafeDownCast(input)); - if(input2) - { - WriteMEDFileFromVTKMultiBlock(mfd,input2,context,timeStep,tsId); - return ; - } - throw MZCException("WriteMEDFileFromVTKGDS : not recognized data type !"); -} - -void PutFamGrpInfoIfAny(MEDFileData *mfd, const std::string& meshName, const std::vector& groups, const std::vector& fams) -{ - if(!mfd) - return ; - if(meshName.empty()) - return ; - MEDFileMeshes *meshes(mfd->getMeshes()); - if(!meshes) - return ; - if(meshes->getNumberOfMeshes()!=1) - return ; - MEDFileMesh *mm(meshes->getMeshAtPos(0)); - if(!mm) - return ; - mm->setName(meshName); - for(std::vector::const_iterator it=fams.begin();it!=fams.end();it++) - mm->setFamilyId((*it).getName(),(*it).getID()); - for(std::vector::const_iterator it=groups.begin();it!=groups.end();it++) - mm->setFamiliesOnGroup((*it).getName(),(*it).getFamilies()); - MEDFileFields *fields(mfd->getFields()); - if(!fields) - return ; - for(int i=0;igetNumberOfFields();i++) - { - MEDFileAnyTypeFieldMultiTS *fmts(fields->getFieldAtPos(i)); - if(!fmts) - continue; - fmts->setMeshName(meshName); - } -} - int vtkMEDWriter::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) { //std::cerr << "########################################## vtkMEDWriter::RequestData ########################################## " << (const char *) this->FileName << std::endl; -- 2.39.2