From: Anthony Geay Date: Wed, 23 Jun 2021 12:58:50 +0000 (+0200) Subject: [EDF23711] : ContactReader is now ready to deal with time X-Git-Tag: V9_8_0a1~5 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=28e9b75e9e66410dae51b327f677da713256e96b;p=tools%2Fparavisaddons_common.git [EDF23711] : ContactReader is now ready to deal with time --- diff --git a/src/ContactReader/plugin/ContactReaderModule/vtkContactReader.cxx b/src/ContactReader/plugin/ContactReaderModule/vtkContactReader.cxx index e3839d3..fc4c0ff 100644 --- a/src/ContactReader/plugin/ContactReaderModule/vtkContactReader.cxx +++ b/src/ContactReader/plugin/ContactReaderModule/vtkContactReader.cxx @@ -43,6 +43,9 @@ #include #include #include +#include + +constexpr char INST_TAG[] = "INST"; class MyException : public std::exception { @@ -87,6 +90,12 @@ private: T *_ptr; }; +bool PresenceOf(const std::vector &arr, const std::string &what) +{ + auto pos = std::find(arr.begin(), arr.end(), what); + return pos!=arr.end(); +} + vtkIdType PosOf(const std::vector &arr, const std::string &what) { auto pos = std::find(arr.begin(), arr.end(), what); @@ -99,22 +108,38 @@ vtkIdType PosOf(const std::vector &arr, const std::string &what) return std::distance(arr.begin(), pos); } -vtkStandardNewMacro(vtkContactReader); - -vtkContactReader::vtkContactReader() : FileName(NULL), ScaleFactor(0.02) +double InstValueOf(vtkTable *table, vtkIdType iRow, vtkIdType iCol) { - this->SetNumberOfInputPorts(0); + vtkVariantArray *row(table->GetRow(iRow)); + vtkVariant *elt(row->GetPointer(iCol)); + return elt->ToDouble(); } -vtkContactReader::~vtkContactReader() +vtkSmartPointer LoadDataFromFile(const char *FileName) { + vtkNew reader; + reader->SetFileName(FileName); + reader->SetDetectNumericColumns(true); + reader->SetUseStringDelimiter(true); + reader->SetHaveHeaders(true); + reader->SetFieldDelimiterCharacters(" "); + reader->SetAddTabFieldDelimiter(true); + reader->SetMergeConsecutiveDelimiters(true); + reader->Update(); + vtkTable *table(reader->GetOutput()); + vtkSmartPointer ret(table); + return ret; } -int vtkContactReader::RequestInformation(vtkInformation *vtkNotUsed(request), - vtkInformationVector **vtkNotUsed(inputVector), - vtkInformationVector *outputVector) +std::vector GetColumnNames(vtkTable *table) { - return 1; + vtkIdType nbCols(table->GetNumberOfColumns()); + std::vector colNames(nbCols); + for (vtkIdType iCol = 0; iCol < nbCols; iCol++) + { + colNames[iCol] = table->GetColumnName(iCol); + } + return colNames; } void FillValue(vtkVariantArray *row, double *ptToFeed, std::size_t ipos, vtkIdType pos) @@ -130,6 +155,48 @@ void FillValue(vtkVariantArray *row, double *ptToFeed, std::size_t ipos, vtkIdTy } } +vtkStandardNewMacro(vtkContactReader); + +vtkContactReader::vtkContactReader() : FileName(NULL), ScaleFactor(0.02),IsTimed(false) +{ + this->SetNumberOfInputPorts(0); +} + +vtkContactReader::~vtkContactReader() +{ +} + +int vtkContactReader::RequestInformation(vtkInformation *vtkNotUsed(request), + vtkInformationVector **vtkNotUsed(inputVector), + vtkInformationVector *outputVector) +{ + vtkInformation *outInfo(outputVector->GetInformationObject(0)); + // + vtkSmartPointer table(LoadDataFromFile(this->FileName)); + std::vector colNames(GetColumnNames(table)); + this->IsTimed = PresenceOf(colNames,INST_TAG); + vtkIdType nbRows(table->GetNumberOfRows()); + if(!this->IsTimed) + return 1; + vtkIdType InstPos = PosOf(colNames, INST_TAG); + std::set allInsts; + for (vtkIdType iRow = 0; iRow < nbRows; iRow++) + { + vtkVariantArray *row(table->GetRow(iRow)); + vtkVariant *elt(row->GetPointer(InstPos)); + allInsts.insert(elt->ToDouble()); + } + std::vector allInstsV(allInsts.begin(),allInsts.end()); + double timeRange[2]; + timeRange[0]=allInstsV.front(); + timeRange[1]=allInstsV.back(); + outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),allInstsV.data(),(int)allInstsV.size()); + outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(),timeRange,2); + std::for_each(colNames.begin(),colNames.end(),[](const std::string& elt) { std::cerr << elt << " ";}); + return 1; +} + + int vtkContactReader::RequestData(vtkInformation *vtkNotUsed(request), vtkInformationVector **vtkNotUsed(inputVector), vtkInformationVector *outputVector) @@ -139,32 +206,36 @@ int vtkContactReader::RequestData(vtkInformation *vtkNotUsed(request), // try { - vtkNew reader; - reader->SetFileName(this->FileName); - reader->SetDetectNumericColumns(true); - reader->SetUseStringDelimiter(true); - reader->SetHaveHeaders(true); - reader->SetFieldDelimiterCharacters(" "); - reader->SetAddTabFieldDelimiter(true); - reader->SetMergeConsecutiveDelimiters(true); - reader->Update(); - vtkTable *table(reader->GetOutput()); + vtkSmartPointer table(LoadDataFromFile(this->FileName)); vtkIdType nbRows(table->GetNumberOfRows()), nbCols(table->GetNumberOfColumns()); - std::vector colNames(nbCols); - for (vtkIdType iCol = 0; iCol < nbCols; iCol++) - { - colNames[iCol] = table->GetColumnName(iCol); - } + std::vector colNames(GetColumnNames(table)); vtkIdType XPos(PosOf(colNames, "X")), YPos(PosOf(colNames, "Y")), ZPos(PosOf(colNames, "Z")), DXPos(PosOf(colNames, "DX")), DYPos(PosOf(colNames, "DY")), DZPos(PosOf(colNames, "DZ")); + // check of presence of INST. If yes -> it means that input file is on different time steps. + vtkIdType InstPos(std::numeric_limits::max()); // vtkSmartPointer coords(vtkSmartPointer::New()), vectArr(vtkSmartPointer::New()); + // + vtkIdType nbTuples(nbRows),startRow(0); + double reqTS(0.0); + if(this->IsTimed) + { + InstPos = PosOf(colNames, INST_TAG); + if(outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP())) + reqTS=outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()); + vtkIdType iRow = 0; + for (; iRow < nbRows && InstValueOf(table,iRow,InstPos)!=reqTS; iRow++); + startRow = iRow; + for (; iRow < nbRows && InstValueOf(table,iRow,InstPos)==reqTS; iRow++); + nbTuples = iRow-startRow; + } + // vectArr->SetNumberOfComponents(3); coords->SetNumberOfComponents(3); - coords->SetNumberOfTuples(nbRows); - vectArr->SetNumberOfTuples(nbRows); + coords->SetNumberOfTuples(nbTuples); + vectArr->SetNumberOfTuples(nbTuples); double *ptToFeed1(coords->Begin()), *ptToFeed2(vectArr->Begin()); const vtkIdType POS[3] = {XPos, YPos, ZPos}, DX[3] = {DXPos, DYPos, DZPos}; - for (vtkIdType iRow = 0; iRow < nbRows; iRow++, ptToFeed1 += 3, ptToFeed2 += 3) + for (vtkIdType iRow = startRow; iRow < startRow+nbTuples; iRow++, ptToFeed1 += 3, ptToFeed2 += 3) { vtkVariantArray *row(table->GetRow(iRow)); for (std::size_t ipos = 0; ipos < 3; ipos++) @@ -199,6 +270,8 @@ int vtkContactReader::RequestData(vtkInformation *vtkNotUsed(request), glyph->SetScaleFactor(this->ScaleFactor); glyph->Update(); output->ShallowCopy(glyph->GetOutput()); + if(this->IsTimed) + output->GetInformation()->Set(vtkDataObject::DATA_TIME_STEP(),reqTS); output->GetPointData()->SetActiveAttribute(0, vtkDataSetAttributes::SCALARS); //output->ShallowCopy(ret); } diff --git a/src/ContactReader/plugin/ContactReaderModule/vtkContactReader.h b/src/ContactReader/plugin/ContactReaderModule/vtkContactReader.h index 1195eeb..c4b8d35 100644 --- a/src/ContactReader/plugin/ContactReaderModule/vtkContactReader.h +++ b/src/ContactReader/plugin/ContactReaderModule/vtkContactReader.h @@ -43,6 +43,7 @@ protected: char *FileName; double ScaleFactor; + bool IsTimed; private: vtkContactReader(const vtkContactReader &) = delete; diff --git a/src/ContactReader/plugin/sources.xml b/src/ContactReader/plugin/sources.xml index ef82977..41c2e5a 100644 --- a/src/ContactReader/plugin/sources.xml +++ b/src/ContactReader/plugin/sources.xml @@ -25,6 +25,15 @@ This property specifies the scale factor applied to the size of arrows. + + + + Available timestep values. + +