]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Extractor.cxx
Salome HOME
NRI : Remove dependence with KERNEL.
[modules/visu.git] / src / VISU_I / VISU_Extractor.cxx
1 using namespace std;
2 #include "VISU_Extractor.hxx"   
3
4 #include <vtkObjectFactory.h>
5 #include <vtkScalars.h>
6 #include <vtkVectors.h>
7 #include <vtkUnstructuredGridReader.h>
8
9 #ifdef DEBUG
10 static int MYDEBUG = 1;
11 #else
12 static int MYDEBUG = 0;
13 #endif
14
15 VISU_Extractor::VISU_Extractor(){
16   myNbComp = 0;
17   myScalarMode = 0;
18 }
19 VISU_Extractor::~VISU_Extractor() {}
20 VISU_Extractor* VISU_Extractor::New(){
21   vtkObject* ret = vtkObjectFactory::CreateInstance("VISU_Extractor");
22   if(ret) return (VISU_Extractor*)ret;
23   return new VISU_Extractor;
24 }  
25 void VISU_Extractor::Extract(vtkUnstructuredGridReader* theReader, 
26                              const string& theFieldName, int theScalarMode,
27                              int theNbComp, const VISU::TEntity& theEntity)
28 {
29   myScalarMode = theScalarMode;
30   myNbComp = theNbComp;
31   myEntity = theEntity;
32   if(myNbComp == 1)
33     theReader->SetScalarsName(theFieldName.c_str());
34   else
35     theReader->SetVectorsName(theFieldName.c_str());
36   SetInput(theReader->GetOutput());
37   Update();
38 }
39 template<typename TypeData> void
40 execute(int theNbComp, int theScalarMode, TypeData* theInputData, TypeData* theOutputData){
41   vtkVectors *inVectors = theInputData->GetVectors();
42   if ( !inVectors || theNbComp < 1 ) 
43     return; 
44   vtkScalars *newScalars = vtkScalars::New();
45   newScalars->SetNumberOfScalars(theNbComp);
46   for (int ptId = 0; ptId < theNbComp; ptId++) {
47     float v[3], s;
48     inVectors->GetVector(ptId,v);
49     if ( theScalarMode < 1 || theScalarMode > 3)
50       s = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
51     else
52       s = v[theScalarMode - 1];
53     newScalars->SetScalar(ptId, s);
54   }
55   theOutputData->SetScalars(newScalars);
56   newScalars->Delete();
57 }
58 void VISU_Extractor::Execute(){
59   vtkDataSet *input = this->GetInput(), *output = this->GetOutput();
60   output->CopyStructure(input);
61   output->GetPointData()->CopyAllOff();
62   output->GetCellData()->CopyAllOff();
63   if(myEntity == VISU::NODE_ENTITY){
64     output->GetPointData()->CopyVectorsOn();
65     int nbComp = input->GetNumberOfPoints();
66     vtkPointData *inData = input->GetPointData(), *outData = output->GetPointData();
67     if(myNbComp > 1)
68       execute(nbComp,myScalarMode,inData,outData);
69     else
70       output->GetPointData()->CopyScalarsOn();
71     outData->PassData(inData);
72   }else{
73     output->GetCellData()->CopyVectorsOn();
74     int nbComp = input->GetNumberOfCells();
75     vtkCellData *inData = input->GetCellData(), *outData = output->GetCellData();
76     if(myNbComp > 1)
77       execute(nbComp,myScalarMode,inData,outData);
78     else
79       output->GetCellData()->CopyScalarsOn();
80     outData->PassData(inData);
81   }
82 }
83
84