Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / PIPELINE / VISU_Extractor.cxx
index 82eec5dc8f20800ae485bb562de52e91caafd486..e44145e30dccbdaaa385401558b0beab5ad32c2a 100644 (file)
 
 #include "VISU_Extractor.hxx"
 #include "VISU_PipeLineUtils.hxx"
+#include "VISU_ConvertorUtils.hxx"
 
 #include <sstream>
 
 #include <vtkObjectFactory.h>
 #include <vtkUnstructuredGrid.h>
-#include <vtkFloatArray.h>
 #include <vtkPointData.h>
 #include <vtkCellData.h>
 #include <vector>
+#include <vtkInformation.h>
+#include <vtkInformationVector.h>
 
-using namespace std;
 
 
+//----------------------------------------------------------------------------
 vtkStandardNewMacro(VISU_Extractor);
 
-VISU_Extractor::VISU_Extractor()
+//----------------------------------------------------------------------------
+VISU_Extractor
+::VISU_Extractor():
+  myScalarMode(1)
 {
-  myScalarMode = 1;
 }
 
-VISU_Extractor::~VISU_Extractor()
+
+//----------------------------------------------------------------------------
+VISU_Extractor
+::~VISU_Extractor()
 {}
 
-void VISU_Extractor::SetScalarMode(int theScalarMode)
+
+//----------------------------------------------------------------------------
+void
+VISU_Extractor
+::SetScalarMode(int theScalarMode)
 {
   if(myScalarMode != theScalarMode){
     myScalarMode = theScalarMode;
@@ -56,65 +67,129 @@ void VISU_Extractor::SetScalarMode(int theScalarMode)
   }
 }
 
-template<typename TypeData> void
-execute(int theNbElems, 
-       int theScalarMode, 
-       TypeData* theInputData, 
-       TypeData* theOutputData)
+//----------------------------------------------------------------------------
+int
+VISU_Extractor
+::GetScalarMode()
+{
+  return myScalarMode;
+}
+
+
+//----------------------------------------------------------------------------
+template<typename TValueType> 
+void
+Module2Scalars(vtkDataArray *theInputDataArray,
+              TValueType* theOutputPtr,
+              vtkIdType theNbOfTuples)
+{
+  vtkIdType aNbComp = theInputDataArray->GetNumberOfComponents();
+  std::vector<vtkFloatingPointType> anArray(aNbComp < 3? 3: aNbComp);
+  for(vtkIdType aTupleId = 0; aTupleId < theNbOfTuples; aTupleId++){
+    theInputDataArray->GetTuple(aTupleId, &anArray[0]);
+    vtkFloatingPointType aVector[3] = {anArray[0], anArray[1], anArray[2]};
+    vtkFloatingPointType aScalar = sqrt(aVector[0]*aVector[0] + 
+                                       aVector[1]*aVector[1] + 
+                                       aVector[2]*aVector[2]);
+    *theOutputPtr = TValueType(aScalar);
+    theOutputPtr++;
+  }
+}
+
+
+//----------------------------------------------------------------------------
+template<typename TValueType> 
+void
+Component2Scalars(vtkDataArray *theInputDataArray,
+                 TValueType* theInputPtr,
+                 TValueType* theOutputPtr,
+                 vtkIdType theNbOfTuples,
+                 vtkIdType theComponentId)
+{
+  vtkIdType aNbComp = theInputDataArray->GetNumberOfComponents();
+  for(vtkIdType aTupleId = 0; aTupleId < theNbOfTuples; aTupleId++){
+    *theOutputPtr = *(theInputPtr + theComponentId);
+    theInputPtr += aNbComp;
+    theOutputPtr++;
+  }
+}
+
+
+//----------------------------------------------------------------------------
+template<typename TDataSetAttributesType> void
+ExecuteScalars(vtkIdType theNbOfTuples, 
+              vtkIdType theScalarMode, 
+              TDataSetAttributesType* theInputData, 
+              TDataSetAttributesType* theOutputData)
 {
-  if(theNbElems < 1 )
+  if(theNbOfTuples < 1)
     return;
+
   vtkDataArray* aFieldArray = theInputData->GetArray("VISU_FIELD");
-  if(vtkFloatArray *aFloatArray = dynamic_cast<vtkFloatArray*>(aFieldArray)){
-    int aNbComp = aFloatArray->GetNumberOfComponents();
-    std::vector<vtkFloatingPointType> anArray(aNbComp < 3? 3: aNbComp);
-    //
-    vtkFloatArray *aScalars = vtkFloatArray::New();
-    aScalars->SetNumberOfTuples(theNbElems);
-    aScalars->SetNumberOfComponents(1);
-    //
-    if(!theScalarMode){
-      for(int anId = 0; anId < theNbElems; anId++){
-       aFloatArray->GetTuple(anId,&anArray[0]);
-       vtkFloatingPointType aVector[3] = {anArray[0], anArray[1], anArray[2]};
-       vtkFloatingPointType aScalar = sqrt(aVector[0]*aVector[0] + aVector[1]*aVector[1] + aVector[2]*aVector[2]);
-       aScalars->SetTuple1(anId,aScalar);
-      }
-    }else{
-      for(int anId = 0; anId < theNbElems; anId++){
-       aFloatArray->GetTuple(anId,&anArray[0]);
-       aScalars->SetTuple1(anId,anArray[theScalarMode - 1]);
-      }
+  vtkIdType anInputDataType = aFieldArray->GetDataType();
+  vtkDataArray *anOutputScalars = vtkDataArray::CreateDataArray(anInputDataType);
+  anOutputScalars->SetNumberOfComponents(1);
+  anOutputScalars->SetNumberOfTuples(theNbOfTuples);
+
+  void *anInputPtr = aFieldArray->GetVoidPointer(0);
+  void *anOutputPtr = anOutputScalars->GetVoidPointer(0);
+
+  if(theScalarMode == 0){
+    switch(anInputDataType){
+      vtkTemplateMacro3(Module2Scalars,
+                       aFieldArray,
+                       (VTK_TT *)(anOutputPtr), 
+                       theNbOfTuples);
+    default:
+      break;
+    }
+  }else{
+    switch(anInputDataType){
+      vtkTemplateMacro5(Component2Scalars,
+                       aFieldArray,
+                       (VTK_TT *)(anInputPtr), 
+                       (VTK_TT *)(anOutputPtr),
+                       theNbOfTuples,
+                       theScalarMode - 1);
+    default:
+      break;
     }
-    theOutputData->SetScalars(aScalars);
-    aScalars->Delete();
   }
+  
+  theOutputData->SetScalars(anOutputScalars);
+  anOutputScalars->Delete();
 }
 
-void VISU_Extractor::Execute(){
-  vtkDataSet *input = this->GetInput(), *output = this->GetOutput();
-  output->CopyStructure(input);
-  output->GetPointData()->CopyAllOff();
-  output->GetCellData()->CopyAllOff();
-  if(input->GetPointData()->GetNumberOfArrays()){
-    output->GetPointData()->CopyVectorsOn();
-    int aNbElems = input->GetNumberOfPoints();
-    vtkPointData *inData = input->GetPointData(), *outData = output->GetPointData();
-    if(inData->GetAttribute(vtkDataSetAttributes::VECTORS))
-      execute(aNbElems,myScalarMode,inData,outData);
-    else
-      output->GetPointData()->CopyScalarsOn();
-    outData->PassData(inData);
-    outData->AddArray(inData->GetArray("VISU_FIELD"));
-  }else{
-    output->GetCellData()->CopyVectorsOn();
-    int aNbElems = input->GetNumberOfCells();
-    vtkCellData *inData = input->GetCellData(), *outData = output->GetCellData();
-    if(inData->GetAttribute(vtkDataSetAttributes::VECTORS))
-      execute(aNbElems,myScalarMode,inData,outData);
-    else
-      output->GetCellData()->CopyScalarsOn();
-    outData->PassData(inData);
-    outData->AddArray(inData->GetArray("VISU_FIELD"));
+
+//---------------------------------------------------------------
+int
+VISU_Extractor
+::RequestData(vtkInformation *theRequest,
+             vtkInformationVector **theInputVector,
+             vtkInformationVector *theOutputVector)
+{
+  vtkDataSet *anInput = VISU::GetInput(theInputVector, 0);
+  vtkDataSet *anOutput = VISU::GetOutput(theOutputVector);
+
+  anOutput->CopyStructure(anInput);
+
+  vtkPointData *anInputPointData = anInput->GetPointData();
+  vtkPointData *anOutputPointData = anOutput->GetPointData();
+  anOutputPointData->PassData(anInputPointData); 
+  if(VISU::IsDataOnPoints(anInput)){
+    int aNbElems = anInput->GetNumberOfPoints();
+    if(anInputPointData->GetAttribute(vtkDataSetAttributes::VECTORS))
+      ExecuteScalars(aNbElems, myScalarMode, anInputPointData, anOutputPointData);
+  }
+  
+  vtkCellData *anInputCellData = anInput->GetCellData();
+  vtkCellData *anOutputCellData = anOutput->GetCellData();
+  anOutputCellData->PassData(anInputCellData); 
+  if(VISU::IsDataOnCells(anInput)){
+    int aNbElems = anInput->GetNumberOfCells();
+    if(anInputCellData->GetAttribute(vtkDataSetAttributes::VECTORS))
+      ExecuteScalars(aNbElems, myScalarMode, anInputCellData, anOutputCellData);
   }
+
+  return 1;
 }