Salome HOME
Make it run with int64 configuration with non regression tests
[modules/paravis.git] / src / Plugins / SimpleMode / IO / vtkSimpleMode.cxx
index 404180ed0b16dd84a134308ccef34d1f24a32184..2626e79de9bb6dbf99b5f0eba548e0d4f6da7d0a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2017  CEA/DEN, EDF R&D
+// Copyright (C) 2017-2019  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
@@ -28,7 +28,7 @@
 
 #include "vtkStreamingDemandDrivenPipeline.h"
 #include "vtkUnstructuredGrid.h"
-#include "vtkPolyData.h"
+#include "vtkDataSet.h"
 #include  "vtkMultiBlockDataSet.h"
 
 #include "vtkInformationStringKey.h"
@@ -47,6 +47,7 @@
 #include "vtkVariantArray.h"
 #include "vtkStringArray.h"
 #include "vtkDoubleArray.h"
+#include "vtkFloatArray.h"
 #include "vtkCharArray.h"
 #include "vtkUnsignedCharArray.h"
 #include "vtkDataSetAttributes.h"
 #include "vtkMultiBlockDataGroupFilter.h"
 #include "vtkCompositeDataToUnstructuredGridFilter.h"
 
+#ifdef WIN32
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <functional>
+#endif
+
 #include <map>
 #include <deque>
 #include <sstream>
@@ -83,29 +90,69 @@ private:
   std::string _reason;
 };
 
-vtkSmartPointer<vtkDoubleArray> ForceTo3Compo(vtkDoubleArray *arr)
+//ValueTypeT
+
+/*template<class T>
+struct ArrayTraits
 {
+  typedef T EltType;
+  };*/
+
+template<class VTK_ARRAY_T>
+vtkSmartPointer<VTK_ARRAY_T> ForceTo3CompoImpl(VTK_ARRAY_T *arr)
+{
+  using ELT_TYPE = typename VTK_ARRAY_T::ValueType;
   if(!arr)
-    return vtkSmartPointer<vtkDoubleArray>();
-  int nbCompo(arr->GetNumberOfComponents()),nbTuples(arr->GetNumberOfTuples());
+    return vtkSmartPointer<VTK_ARRAY_T>();
+  vtkIdType nbCompo(arr->GetNumberOfComponents()),nbTuples(arr->GetNumberOfTuples());
   if(nbCompo==3)
     {
-      vtkSmartPointer<vtkDoubleArray> ret(arr);
-      arr->Register(0);
+      vtkSmartPointer<VTK_ARRAY_T> ret(arr);
       return ret;
     }
   if(nbCompo==6)
     {
-      vtkSmartPointer<vtkDoubleArray> ret(vtkSmartPointer<vtkDoubleArray>::New());
+      vtkSmartPointer<VTK_ARRAY_T> ret(vtkSmartPointer<VTK_ARRAY_T>::New());
       ret->SetNumberOfComponents(3);
       ret->SetNumberOfTuples(nbTuples);
-      const double *srcPt(arr->Begin());
-      double *destPt(ret->Begin());
-      for(int i=0;i<nbTuples;i++,destPt+=3,srcPt+=6)
+      const ELT_TYPE *srcPt(arr->Begin());
+      ELT_TYPE *destPt(ret->Begin());
+      for(vtkIdType i=0;i<nbTuples;i++,destPt+=3,srcPt+=6)
         std::copy(srcPt,srcPt+3,destPt);
       return ret;
     }
-  throw MZCException("ForceTo3Compo : internal error ! 6 or 3 compo arrays expected !");
+  throw MZCException("ForceTo3CompoImpl : internal error ! 6 or 3 compo arrays expected !");
+}
+
+vtkSmartPointer<vtkDataArray> ForceTo3Compo(vtkDataArray *arr)
+{
+  vtkDoubleArray *arr0(vtkDoubleArray::SafeDownCast(arr));
+  if(arr0)
+    return ForceTo3CompoImpl<vtkDoubleArray>(arr0);
+  vtkFloatArray *arr1(vtkFloatArray::SafeDownCast(arr));
+  if(arr1)
+    return ForceTo3CompoImpl<vtkFloatArray>(arr1);
+  throw MZCException("ForceTo3Compo : array is NEITHER float64 NOR float32 array !");
+}
+
+template<class VTK_ARRAY_T>
+void FeedDataInternal(VTK_ARRAY_T *arrReal, double cst1, double *ptToFeed1)
+{
+  vtkIdType nbTuples(arrReal->GetNumberOfTuples());
+  using ELT_TYPE = typename VTK_ARRAY_T::ValueType;
+  const ELT_TYPE *srcPt1(arrReal->Begin());
+  std::for_each(srcPt1,srcPt1+3*nbTuples,[&ptToFeed1,cst1](const ELT_TYPE& elt) { *ptToFeed1 = (double)elt * cst1; ptToFeed1++; });
+}
+
+void FeedData(vtkDataArray *arr, double cst1, double *ptToFeed1)
+{
+  vtkDoubleArray *arr0(vtkDoubleArray::SafeDownCast(arr));
+  if(arr0)
+    return FeedDataInternal<vtkDoubleArray>(arr0,cst1,ptToFeed1);
+  vtkFloatArray *arr1(vtkFloatArray::SafeDownCast(arr));
+  if(arr1)
+    return FeedDataInternal<vtkFloatArray>(arr1,cst1,ptToFeed1);
+  throw MZCException("FeedData : array is NEITHER float64 NOR float32 array !");
 }
 
 std::vector< std::string > GetPossibleArrayNames(vtkDataSet *dataset)
@@ -220,16 +267,16 @@ void ExtractInfo3(vtkDataSet *ds, const std::string& arrName, vtkDataArray *& ar
 }
 
   
-void ExtractInfo2(vtkDataSet *ds, const std::string& arrName, vtkDoubleArray *& arr)
+void ExtractInfo2(vtkDataSet *ds, const std::string& arrName, vtkDataArray *&arr)
 {
-  vtkDataArray *zeArr(0);
   int dummy;
-  ExtractInfo3(ds,arrName,zeArr,dummy);
-  arr=vtkDoubleArray::SafeDownCast(zeArr);
-  if(!arr)
+  ExtractInfo3(ds,arrName,arr,dummy);
+  vtkDoubleArray *arr1(vtkDoubleArray::SafeDownCast(arr));
+  vtkFloatArray *arr2(vtkFloatArray::SafeDownCast(arr));
+  if(!arr1 && !arr2)
     {
       std::ostringstream oss;
-      oss << "Array called \"" << arrName << "\" has been located but this is NOT a float64 array !";
+      oss << "Array called \"" << arrName << "\" has been located but this is NEITHER float64 NOR float32 array !";
       throw MZCException(oss.str());
     }
   if(arr->GetNumberOfComponents()!=3 && arr->GetNumberOfComponents()!=6)
@@ -293,7 +340,7 @@ void vtkSimpleMode::SetInputArrayToProcess(int idx, int port, int connection, in
 {
   if(idx==0)
     this->Internal->setFieldForReal(name);
-  vtkPolyDataAlgorithm::SetInputArrayToProcess(idx,port,connection,ff,name);
+  vtkDataSetAlgorithm::SetInputArrayToProcess(idx,port,connection,ff,name);
 }
 
 double GetOptimalRatioFrom(vtkUnstructuredGrid *dataset, vtkDoubleArray *array)
@@ -329,6 +376,12 @@ double GetOptimalRatioFrom(vtkUnstructuredGrid *dataset, vtkDoubleArray *array)
   return maxGeoDelta/maxDispDelta;
 }
 
+int vtkSimpleMode::FillOutputPortInformation( int vtkNotUsed(port), vtkInformation* info)
+{
+  info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData");
+  return 1;
+}
+
 int vtkSimpleMode::RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
 { 
   //std::cerr << "########################################## vtkSimpleMode::RequestInformation ##########################################" << std::endl;
@@ -371,17 +424,16 @@ int vtkSimpleMode::RequestData(vtkInformation *request, vtkInformationVector **i
       //
       int nbPts(usgIn->GetNumberOfPoints());
       vtkPolyData *outSurface(this->Internal->performConnection(usgIn));
-      vtkDoubleArray *arrRealBase(0);
+      vtkDataArray *arrRealBase(nullptr);
       ExtractInfo2(outSurface,this->Internal->getFieldForReal(),arrRealBase);
-      vtkSmartPointer<vtkDoubleArray> arrReal(ForceTo3Compo(arrRealBase));
+      vtkSmartPointer<vtkDataArray> arrReal(ForceTo3Compo(arrRealBase));
       vtkSmartPointer<vtkDoubleArray> arr1(vtkSmartPointer<vtkDoubleArray>::New());
       arr1->SetName(ZE_DISPLACEMENT_NAME1);
       arr1->SetNumberOfComponents(3);
       arr1->SetNumberOfTuples(nbPts);
       double *ptToFeed1(arr1->Begin());
-      const double *srcPt1(arrReal->Begin());
-      double cst1(Factor*sin(AnimationTime*2*M_PI));
-      std::transform(srcPt1,srcPt1+3*nbPts,ptToFeed1,std::bind2nd(std::multiplies<double>(),cst1));
+      double cst1(Factor*cos(AnimationTime*2*M_PI));
+      FeedData(arrReal,cst1,ptToFeed1);
       int idx1(outSurface->GetPointData()->AddArray(arr1));
       outSurface->GetPointData()->SetActiveAttribute(idx1,vtkDataSetAttributes::VECTORS);
       //
@@ -399,12 +451,30 @@ int vtkSimpleMode::RequestData(vtkInformation *request, vtkInformationVector **i
         vtkDataArray *dummy(0);
         ExtractInfo3(ds,this->Internal->getFieldForReal(),dummy,idx2);
       }
-      ds->GetPointData()->SetActiveAttribute(idx2,vtkDataSetAttributes::SCALARS);
       //
       vtkInformation *outInfo(outputVector->GetInformationObject(0));
       vtkPolyData *output(vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT())));
       output->ShallowCopy(ds);
-      
+      output->GetPointData()->DeepCopy(ds->GetPointData());
+      //
+      for(int i=0;i<output->GetPointData()->GetNumberOfArrays();i++)
+        {
+          vtkDataArray *arr(output->GetPointData()->GetArray(i));
+          vtkDoubleArray *arr2(vtkDoubleArray::SafeDownCast(arr));
+          if(!arr2)
+            continue;
+          vtkIdType nbCompo(arr2->GetNumberOfComponents()),nbTuples(arr2->GetNumberOfTuples());
+          if(nbCompo!=3 && nbCompo!=2)
+            continue;
+          double *arrPtr(arr2->GetPointer(0));
+          std::transform(arrPtr,arrPtr+nbCompo*nbTuples,arrPtr,std::bind2nd(std::multiplies<double>(),cos(AnimationTime*2*M_PI)));
+        }
+      //
+      vtkDataArray* array = output->GetPointData()->GetArray(idx2);
+      vtkSmartPointer<vtkDataArray> result = vtkSmartPointer<vtkDataArray>::Take(vtkDataArray::CreateDataArray(array->GetDataType()));
+      result->ShallowCopy(array);
+      result->SetName("__NormalModesAnimation__");
+      output->GetPointData()->SetScalars(result);            
     }
   catch(MZCException& e)
     {