]> SALOME platform Git repositories - modules/visu.git/blob - src/PIPELINE/VISU_Extractor.cxx
Salome HOME
Merge with branch V2_2_0_VISU_improvement
[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
37 using namespace std;
38
39
40 vtkStandardNewMacro(VISU_Extractor);
41
42 VISU_Extractor::VISU_Extractor(){
43   myScalarMode = 0;
44 }
45
46 VISU_Extractor::~VISU_Extractor(){
47 }
48
49 void VISU_Extractor::SetScalarMode(int theScalarMode){
50   if(myScalarMode != theScalarMode){
51     myScalarMode = theScalarMode;
52     Modified();
53   }
54 }
55
56
57 template<typename TypeData> void
58 execute(int theNbComp, int theScalarMode, TypeData* theInputData, TypeData* theOutputData){
59   vtkDataArray *inVectors = theInputData->GetVectors();
60   if ( !inVectors || theNbComp < 1 )
61     return;
62   vtkFloatArray *newScalars = vtkFloatArray::New();
63   ostringstream aName;
64   aName<<inVectors->GetName();  aName<<", ";  aName<<theScalarMode;
65   newScalars->SetName(aName.str().c_str());
66   newScalars->SetNumberOfComponents(1);
67   newScalars->SetNumberOfTuples(theNbComp);
68   for (int ptId = 0; ptId < theNbComp; ptId++) {
69     float v[3], s;
70     inVectors->GetTuple(ptId,v);
71     if ( theScalarMode < 1 || theScalarMode > 3)
72       s = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
73     else
74       s = v[theScalarMode - 1];
75     newScalars->SetTuple1(ptId, s);
76   }
77   theOutputData->SetScalars(newScalars);
78   //theOutputData->SetActiveScalars(newScalars->GetName());
79   newScalars->Delete();
80 }
81
82
83 void VISU_Extractor::Execute(){
84   vtkDataSet *input = this->GetInput(), *output = this->GetOutput();
85   output->CopyStructure(input);
86   output->GetPointData()->CopyAllOff();
87   output->GetCellData()->CopyAllOff();
88   if(input->GetPointData()->GetNumberOfArrays()){
89     output->GetPointData()->CopyVectorsOn();
90     int nbComp = input->GetNumberOfPoints();
91     vtkPointData *inData = input->GetPointData(), *outData = output->GetPointData();
92     if(inData->GetAttribute(vtkDataSetAttributes::VECTORS))
93       execute(nbComp,myScalarMode,inData,outData);
94     else
95       output->GetPointData()->CopyScalarsOn();
96     outData->PassData(inData);
97   }else{
98     output->GetCellData()->CopyVectorsOn();
99     int nbComp = input->GetNumberOfCells();
100     vtkCellData *inData = input->GetCellData(), *outData = output->GetCellData();
101     if(inData->GetAttribute(vtkDataSetAttributes::VECTORS))
102       execute(nbComp,myScalarMode,inData,outData);
103     else
104       output->GetCellData()->CopyScalarsOn();
105     outData->PassData(inData);
106   }
107 }