1 // Copyright (C) 2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
21 #include "vtkSimpleMode.h"
23 #include "vtkAdjacentVertexIterator.h"
24 #include "vtkDataSetSurfaceFilter.h"
25 #include "vtkIntArray.h"
26 #include "vtkCellData.h"
27 #include "vtkPointData.h"
29 #include "vtkStreamingDemandDrivenPipeline.h"
30 #include "vtkUnstructuredGrid.h"
31 #include "vtkPolyData.h"
32 #include "vtkMultiBlockDataSet.h"
34 #include "vtkInformationStringKey.h"
35 #include "vtkAlgorithmOutput.h"
36 #include "vtkObjectFactory.h"
37 #include "vtkMutableDirectedGraph.h"
38 #include "vtkMultiBlockDataSet.h"
39 #include "vtkDataSet.h"
40 #include "vtkInformationVector.h"
41 #include "vtkInformation.h"
42 #include "vtkDataArraySelection.h"
43 #include "vtkTimeStamp.h"
44 #include "vtkInEdgeIterator.h"
45 #include "vtkInformationDataObjectKey.h"
46 #include "vtkExecutive.h"
47 #include "vtkVariantArray.h"
48 #include "vtkStringArray.h"
49 #include "vtkDoubleArray.h"
50 #include "vtkCharArray.h"
51 #include "vtkUnsignedCharArray.h"
52 #include "vtkDataSetAttributes.h"
53 #include "vtkDemandDrivenPipeline.h"
54 #include "vtkDataObjectTreeIterator.h"
55 #include "vtkWarpScalar.h"
56 #include "vtkWarpVector.h"
57 #include "vtkMultiBlockDataGroupFilter.h"
58 #include "vtkCompositeDataToUnstructuredGridFilter.h"
61 #define _USE_MATH_DEFINES
69 vtkStandardNewMacro(vtkSimpleMode);
71 static const char ZE_DISPLACEMENT_NAME1[]="@@ForReal?@@";
73 static const char ZE_DISPLACEMENT_NAME2[]="@@ForImag?@@";
75 static const char ZE_DISPLACEMENT_NAME3[]="MagnitudeOfCpxDisp";
77 static const double EPS=1e-12;
81 class MZCException : public std::exception
84 MZCException(const std::string& s):_reason(s) { }
85 virtual const char *what() const throw() { return _reason.c_str(); }
86 virtual ~MZCException() throw() { }
91 vtkSmartPointer<vtkDoubleArray> ForceTo3Compo(vtkDoubleArray *arr)
94 return vtkSmartPointer<vtkDoubleArray>();
95 int nbCompo(arr->GetNumberOfComponents()),nbTuples(arr->GetNumberOfTuples());
98 vtkSmartPointer<vtkDoubleArray> ret(arr);
104 vtkSmartPointer<vtkDoubleArray> ret(vtkSmartPointer<vtkDoubleArray>::New());
105 ret->SetNumberOfComponents(3);
106 ret->SetNumberOfTuples(nbTuples);
107 const double *srcPt(arr->Begin());
108 double *destPt(ret->Begin());
109 for(int i=0;i<nbTuples;i++,destPt+=3,srcPt+=6)
110 std::copy(srcPt,srcPt+3,destPt);
113 throw MZCException("ForceTo3Compo : internal error ! 6 or 3 compo arrays expected !");
116 std::vector< std::string > GetPossibleArrayNames(vtkDataSet *dataset)
119 throw MZCException("The input dataset is null !");
120 std::vector< std::string > ret;
121 vtkPointData *att(dataset->GetPointData());
122 for(int i=0;i<att->GetNumberOfArrays();i++)
124 vtkDataArray *locArr(att->GetArray(i));
125 int nbComp(locArr->GetNumberOfComponents());
126 if(nbComp!=3 && nbComp!=6)
128 std::string s(locArr->GetName());
134 std::string FindTheBest(const std::vector<std::string>& arrNames, const std::string& key0, const std::string& key1)
140 for(std::vector<std::string>::const_iterator it=arrNames.begin();it!=arrNames.end();it++)
143 if((*it).find(key0,0)!=std::string::npos)
145 if((*it).find(key1,0)!=std::string::npos)
156 std::string FindBestRealAmong(const std::vector<std::string>& arrNames)
158 static const char KEY1[]="DEPL";
159 static const char KEY2[]="REEL";
160 return FindTheBest(arrNames,KEY1,KEY2);
163 std::string FindBestImagAmong(const std::vector<std::string>& arrNames)
165 static const char KEY1[]="DEPL";
166 static const char KEY2[]="IMAG";
167 return FindTheBest(arrNames,KEY1,KEY2);
170 vtkUnstructuredGrid *ExtractInfo1(vtkInformationVector *inputVector)
172 vtkInformation *inputInfo(inputVector->GetInformationObject(0));
173 vtkDataSet *input(0);
174 vtkDataSet *input0(vtkDataSet::SafeDownCast(inputInfo->Get(vtkDataObject::DATA_OBJECT())));
175 vtkMultiBlockDataSet *input1(vtkMultiBlockDataSet::SafeDownCast(inputInfo->Get(vtkDataObject::DATA_OBJECT())));
181 throw MZCException("Input dataSet must be a DataSet or single elt multi block dataset expected !");
182 if(input1->GetNumberOfBlocks()!=1)
183 throw MZCException("Input dataSet is a multiblock dataset with not exactly one block ! Use MergeBlocks or ExtractBlocks filter before calling this filter !");
184 vtkDataObject *input2(input1->GetBlock(0));
186 throw MZCException("Input dataSet is a multiblock dataset with exactly one block but this single element is NULL !");
187 vtkDataSet *input2c(vtkDataSet::SafeDownCast(input2));
189 throw MZCException("Input dataSet is a multiblock dataset with exactly one block but this single element is not a dataset ! Use MergeBlocks or ExtractBlocks filter before calling this filter !");
193 throw MZCException("Input data set is NULL !");
194 vtkUnstructuredGrid *usgIn(vtkUnstructuredGrid::SafeDownCast(input));
196 throw MZCException("Input data set is not an unstructured mesh ! This filter works only on unstructured meshes !");
200 void ExtractInfo3(vtkDataSet *ds, const std::string& arrName, vtkDataArray *& arr, int& idx)
202 vtkPointData *att(ds->GetPointData());
204 throw MZCException("Input dataset has no point data attribute ! Impossible to move mesh !");
205 vtkDataArray *zeArr(0);
207 for(;i<att->GetNumberOfArrays();i++)
209 vtkDataArray *locArr(att->GetArray(i));
210 std::string s(locArr->GetName());
219 std::ostringstream oss;
220 oss << "Impossible to locate the array called \"" << arrName << "\" used to move mesh !";
221 throw MZCException(oss.str());
228 void ExtractInfo2(vtkDataSet *ds, const std::string& arrName, vtkDoubleArray *& arr)
230 vtkDataArray *zeArr(0);
232 ExtractInfo3(ds,arrName,zeArr,dummy);
233 arr=vtkDoubleArray::SafeDownCast(zeArr);
236 std::ostringstream oss;
237 oss << "Array called \"" << arrName << "\" has been located but this is NOT a float64 array !";
238 throw MZCException(oss.str());
240 if(arr->GetNumberOfComponents()!=3 && arr->GetNumberOfComponents()!=6)
242 std::ostringstream oss;
243 oss << "Float64 array called \"" << arrName << "\" has been located but this array has not exactly 3 or 6 components as it should !";
244 throw MZCException(oss.str());
246 if(arr->GetNumberOfTuples()!=ds->GetNumberOfPoints())
248 std::ostringstream oss;
249 oss << "Float64-1 components array called \"" << arrName << "\" has been located but the number of tuples is invalid ! Should be " << ds->GetNumberOfPoints() << " instead of " << arr->GetNumberOfTuples() << " !";
250 throw MZCException(oss.str());
256 class vtkSimpleMode::vtkSimpleModeInternal
259 vtkSimpleModeInternal():_surface(0) { }
260 vtkPolyData *performConnection(vtkDataSet *ds);
261 void setFieldForReal(const std::string& st) { _real=st; }
262 std::string getFieldForReal() const { return _real; }
263 ~vtkSimpleModeInternal();
265 vtkDataSetSurfaceFilter *_surface;
270 vtkPolyData *vtkSimpleMode::vtkSimpleModeInternal::performConnection(vtkDataSet *ds)
274 _surface=vtkDataSetSurfaceFilter::New();
275 _surface->SetInputData(ds);
278 return _surface->GetOutput();
281 vtkSimpleMode::vtkSimpleModeInternal::~vtkSimpleModeInternal()
287 vtkSimpleMode::vtkSimpleMode():Factor(1.),AnimationTime(0.),Internal(new vtkSimpleMode::vtkSimpleModeInternal)
289 //this->SetInputArrayToProcess(0,0,0,vtkDataObject::FIELD_ASSOCIATION_POINTS,vtkDataSetAttributes::VECTORS);
292 vtkSimpleMode::~vtkSimpleMode()
294 delete this->Internal;
297 void vtkSimpleMode::SetInputArrayToProcess(int idx, int port, int connection, int ff, const char *name)
300 this->Internal->setFieldForReal(name);
301 vtkPolyDataAlgorithm::SetInputArrayToProcess(idx,port,connection,ff,name);
304 double GetOptimalRatioFrom(vtkUnstructuredGrid *dataset, vtkDoubleArray *array)
306 if(!dataset || !array)
307 throw MZCException("The input dataset and or array is null !");
308 vtkDataArray *coords(dataset->GetPoints()->GetData());
309 vtkDoubleArray *coords2(vtkDoubleArray::SafeDownCast(coords));
311 throw MZCException("Input coordinates are not float64 !");
312 int nbCompo(array->GetNumberOfComponents());
313 if(coords2->GetNumberOfComponents()!=3 || (nbCompo!=3 && nbCompo!=6))
314 throw MZCException("Input coordinates do not have 3 components as it should !");
315 int nbPts(dataset->GetNumberOfPoints());
316 const double *srcPt1(array->Begin());
317 dataset->ComputeBounds();
318 double *minmax1(dataset->GetBounds());
319 double minmax2[3]={0.,0.,0.};
320 for(int i=0;i<nbPts;i++,srcPt1+=nbCompo)
322 minmax2[0]=std::max(fabs(srcPt1[0]),minmax2[0]);
323 minmax2[1]=std::max(fabs(srcPt1[1]),minmax2[1]);
324 minmax2[2]=std::max(fabs(srcPt1[2]),minmax2[2]);
326 double maxDispDelta(*std::max_element(minmax2,minmax2+3));
330 minmax2[i]=minmax1[2*i+1]-minmax1[2*i];
331 double maxGeoDelta(*std::max_element(minmax2,minmax2+3));
334 return maxGeoDelta/maxDispDelta;
337 int vtkSimpleMode::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
339 //std::cerr << "########################################## vtkSimpleMode::RequestInformation ##########################################" << std::endl;
342 if(this->Internal->getFieldForReal().empty())
344 /*vtkUnstructuredGrid *usgIn(0);
345 vtkDoubleArray *arr(0);
346 ExtractInfo(inputVector[0],usgIn,this->Internal->getFieldForReal(),arr);
347 std::vector<std::string> candidatesArrName(GetPossibleArrayNames(usgIn));
349 double ratio(GetOptimalRatioFrom(usgIn,arr));
350 std::string optArrNameForReal(FindBestRealAmong(candidatesArrName));
351 std::string optArrNameForImag(FindBestImagAmong(candidatesArrName));*/
352 //std::cerr << ratio << std::endl;
353 //std::cerr << optArrNameForReal << " * " << optArrNameForImag << std::endl;
355 catch(MZCException& e)
357 std::ostringstream oss;
358 oss << "Exception has been thrown in vtkSimpleMode::RequestInformation : " << e.what() << std::endl;
359 if(this->HasObserver("ErrorEvent") )
360 this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
362 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
363 vtkObject::BreakOnError();
369 int vtkSimpleMode::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
371 //std::cerr << "########################################## vtkSimpleMode::RequestData ##########################################" << std::endl;
374 vtkUnstructuredGrid *usgIn(0);
375 usgIn=ExtractInfo1(inputVector[0]);
377 int nbPts(usgIn->GetNumberOfPoints());
378 vtkPolyData *outSurface(this->Internal->performConnection(usgIn));
379 vtkDoubleArray *arrRealBase(0);
380 ExtractInfo2(outSurface,this->Internal->getFieldForReal(),arrRealBase);
381 vtkSmartPointer<vtkDoubleArray> arrReal(ForceTo3Compo(arrRealBase));
382 vtkSmartPointer<vtkDoubleArray> arr1(vtkSmartPointer<vtkDoubleArray>::New());
383 arr1->SetName(ZE_DISPLACEMENT_NAME1);
384 arr1->SetNumberOfComponents(3);
385 arr1->SetNumberOfTuples(nbPts);
386 double *ptToFeed1(arr1->Begin());
387 const double *srcPt1(arrReal->Begin());
388 double cst1(Factor*sin(AnimationTime*2*M_PI));
389 std::transform(srcPt1,srcPt1+3*nbPts,ptToFeed1,std::bind2nd(std::multiplies<double>(),cst1));
390 int idx1(outSurface->GetPointData()->AddArray(arr1));
391 outSurface->GetPointData()->SetActiveAttribute(idx1,vtkDataSetAttributes::VECTORS);
394 vtkSmartPointer<vtkWarpVector> ws(vtkSmartPointer<vtkWarpVector>::New());//vtkNew
395 ws->SetInputData(outSurface);
396 ws->SetScaleFactor(1.);
397 ws->SetInputArrayToProcess(idx1,0,0,"vtkDataObject::FIELD_ASSOCIATION_POINTS",ZE_DISPLACEMENT_NAME1);
399 vtkSmartPointer<vtkDataSet> ds(ws->GetOutput());
400 ds->GetPointData()->RemoveArray(idx1);
404 vtkDataArray *dummy(0);
405 ExtractInfo3(ds,this->Internal->getFieldForReal(),dummy,idx2);
407 ds->GetPointData()->SetActiveAttribute(idx2,vtkDataSetAttributes::SCALARS);
409 vtkInformation *outInfo(outputVector->GetInformationObject(0));
410 vtkPolyData *output(vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
411 output->ShallowCopy(ds);
414 catch(MZCException& e)
416 std::ostringstream oss;
417 oss << "Exception has been thrown in vtkSimpleMode::RequestInformation : " << e.what() << std::endl;
418 if(this->HasObserver("ErrorEvent") )
419 this->InvokeEvent("ErrorEvent",const_cast<char *>(oss.str().c_str()));
421 vtkOutputWindowDisplayErrorText(const_cast<char *>(oss.str().c_str()));
422 vtkObject::BreakOnError();
428 void vtkSimpleMode::PrintSelf(ostream& os, vtkIndent indent)
430 this->Superclass::PrintSelf(os, indent);