Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / PIPELINE / VISU_Extractor.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //  File   : VISU_Extractor.cxx
24 //  Module : VISU
25
26 #include "VISU_Extractor.hxx"
27 #include "VISU_PipeLineUtils.hxx"
28
29 #include <sstream>
30
31 #include <vtkObjectFactory.h>
32 #include <vtkUnstructuredGrid.h>
33 #include <vtkFloatArray.h>
34 #include <vtkPointData.h>
35 #include <vtkCellData.h>
36 #include <vector>
37
38 using namespace std;
39
40
41 vtkStandardNewMacro(VISU_Extractor);
42
43 VISU_Extractor::VISU_Extractor()
44 {
45   myScalarMode = 1;
46 }
47
48 VISU_Extractor::~VISU_Extractor()
49 {}
50
51 void VISU_Extractor::SetScalarMode(int theScalarMode)
52 {
53   if(myScalarMode != theScalarMode){
54     myScalarMode = theScalarMode;
55     Modified();
56   }
57 }
58
59 template<typename TypeData> void
60 execute(int theNbElems, 
61         int theScalarMode, 
62         TypeData* theInputData, 
63         TypeData* theOutputData)
64 {
65   if(theNbElems < 1 )
66     return;
67   vtkDataArray* aFieldArray = theInputData->GetArray("VISU_FIELD");
68   if(vtkFloatArray *aFloatArray = dynamic_cast<vtkFloatArray*>(aFieldArray)){
69     int aNbComp = aFloatArray->GetNumberOfComponents();
70     std::vector<float> anArray(aNbComp < 3? 3: aNbComp);
71     //
72     vtkFloatArray *aScalars = vtkFloatArray::New();
73     aScalars->SetNumberOfTuples(theNbElems);
74     aScalars->SetNumberOfComponents(1);
75     //
76     if(!theScalarMode){
77       for(int anId = 0; anId < theNbElems; anId++){
78         aFloatArray->GetTuple(anId,&anArray[0]);
79         float aVector[3] = {anArray[0], anArray[1], anArray[2]};
80         float aScalar = sqrt(aVector[0]*aVector[0] + aVector[1]*aVector[1] + aVector[2]*aVector[2]);
81         aScalars->SetTuple1(anId,aScalar);
82       }
83     }else{
84       for(int anId = 0; anId < theNbElems; anId++){
85         aFloatArray->GetTuple(anId,&anArray[0]);
86         aScalars->SetTuple1(anId,anArray[theScalarMode - 1]);
87       }
88     }
89     theOutputData->SetScalars(aScalars);
90     aScalars->Delete();
91   }
92 }
93
94 void VISU_Extractor::Execute(){
95   vtkDataSet *input = this->GetInput(), *output = this->GetOutput();
96   output->CopyStructure(input);
97   output->GetPointData()->CopyAllOff();
98   output->GetCellData()->CopyAllOff();
99   if(input->GetPointData()->GetNumberOfArrays()){
100     output->GetPointData()->CopyVectorsOn();
101     int aNbElems = input->GetNumberOfPoints();
102     vtkPointData *inData = input->GetPointData(), *outData = output->GetPointData();
103     if(inData->GetAttribute(vtkDataSetAttributes::VECTORS))
104       execute(aNbElems,myScalarMode,inData,outData);
105     else
106       output->GetPointData()->CopyScalarsOn();
107     outData->PassData(inData);
108     outData->AddArray(inData->GetArray("VISU_FIELD"));
109   }else{
110     output->GetCellData()->CopyVectorsOn();
111     int aNbElems = input->GetNumberOfCells();
112     vtkCellData *inData = input->GetCellData(), *outData = output->GetCellData();
113     if(inData->GetAttribute(vtkDataSetAttributes::VECTORS))
114       execute(aNbElems,myScalarMode,inData,outData);
115     else
116       output->GetCellData()->CopyScalarsOn();
117     outData->PassData(inData);
118     outData->AddArray(inData->GetArray("VISU_FIELD"));
119   }
120 }