Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / PIPELINE / VISU_Extractor.cxx
index f4bcfabba829819598bda4b8e59a597e6731cfd6..e44145e30dccbdaaa385401558b0beab5ad32c2a 100644 (file)
@@ -17,7 +17,7 @@
 //  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 //
 //  File   : VISU_Extractor.cxx
 
 #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(){
-  myScalarMode = 0;
+//----------------------------------------------------------------------------
+VISU_Extractor
+::VISU_Extractor():
+  myScalarMode(1)
+{
 }
 
-VISU_Extractor::~VISU_Extractor(){
-}
 
-void VISU_Extractor::SetScalarMode(int theScalarMode){
+//----------------------------------------------------------------------------
+VISU_Extractor
+::~VISU_Extractor()
+{}
+
+
+//----------------------------------------------------------------------------
+void
+VISU_Extractor
+::SetScalarMode(int theScalarMode)
+{
   if(myScalarMode != theScalarMode){
     myScalarMode = theScalarMode;
     Modified();
   }
 }
 
+//----------------------------------------------------------------------------
+int
+VISU_Extractor
+::GetScalarMode()
+{
+  return myScalarMode;
+}
 
-template<typename TypeData> void
-execute(int theNbComp, int theScalarMode, TypeData* theInputData, TypeData* theOutputData){
-  vtkDataArray *inVectors = theInputData->GetVectors();
-  if ( !inVectors || theNbComp < 1 )
-    return;
-  vtkFloatArray *newScalars = vtkFloatArray::New();
-  ostringstream aName;
-  aName<<inVectors->GetName();  aName<<", ";  aName<<theScalarMode;
-  newScalars->SetName(aName.str().c_str());
-  newScalars->SetNumberOfComponents(1);
-  newScalars->SetNumberOfTuples(theNbComp);
-  for (int ptId = 0; ptId < theNbComp; ptId++) {
-    float v[3], s;
-    inVectors->GetTuple(ptId,v);
-    if ( theScalarMode < 1 || theScalarMode > 3)
-      s = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
-    else
-      s = v[theScalarMode - 1];
-    newScalars->SetTuple1(ptId, s);
+
+//----------------------------------------------------------------------------
+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++;
   }
-  theOutputData->SetScalars(newScalars);
-  //theOutputData->SetActiveScalars(newScalars->GetName());
-  newScalars->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 nbComp = input->GetNumberOfPoints();
-    vtkPointData *inData = input->GetPointData(), *outData = output->GetPointData();
-    if(inData->GetAttribute(vtkDataSetAttributes::VECTORS))
-      execute(nbComp,myScalarMode,inData,outData);
-    else
-      output->GetPointData()->CopyScalarsOn();
-    outData->PassData(inData);
+//----------------------------------------------------------------------------
+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(theNbOfTuples < 1)
+    return;
+
+  vtkDataArray* aFieldArray = theInputData->GetArray("VISU_FIELD");
+  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{
-    output->GetCellData()->CopyVectorsOn();
-    int nbComp = input->GetNumberOfCells();
-    vtkCellData *inData = input->GetCellData(), *outData = output->GetCellData();
-    if(inData->GetAttribute(vtkDataSetAttributes::VECTORS))
-      execute(nbComp,myScalarMode,inData,outData);
-    else
-      output->GetCellData()->CopyScalarsOn();
-    outData->PassData(inData);
+    switch(anInputDataType){
+      vtkTemplateMacro5(Component2Scalars,
+                       aFieldArray,
+                       (VTK_TT *)(anInputPtr), 
+                       (VTK_TT *)(anOutputPtr),
+                       theNbOfTuples,
+                       theScalarMode - 1);
+    default:
+      break;
+    }
   }
+  
+  theOutputData->SetScalars(anOutputScalars);
+  anOutputScalars->Delete();
+}
+
+
+//---------------------------------------------------------------
+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;
 }