]> SALOME platform Git repositories - modules/visu.git/blob - src/PIPELINE/VISU_Extractor.cxx
Salome HOME
Fix of issue 0017328: EDF 593 VISU : min and max scalar map of results given at gauss...
[modules/visu.git] / src / PIPELINE / VISU_Extractor.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  VISU OBJECT : interactive object for VISU entities implementation
23 //  File   : VISU_Extractor.cxx
24 //  Module : VISU
25 //
26 #include "VISU_Extractor.hxx"
27 #include "VISU_PipeLineUtils.hxx"
28 #include "VISU_ConvertorUtils.hxx"
29 #include "VISU_MeshValue.hxx"
30
31 #include <sstream>
32
33 #include <vtkObjectFactory.h>
34 #include <vtkUnstructuredGrid.h>
35 #include <vtkPointData.h>
36 #include <vtkCellData.h>
37 #include <vector>
38 #include <vtkInformation.h>
39 #include <vtkInformationVector.h>
40
41
42
43 //----------------------------------------------------------------------------
44 vtkStandardNewMacro(VISU_Extractor);
45
46 //----------------------------------------------------------------------------
47 VISU_Extractor
48 ::VISU_Extractor():
49   myScalarMode(1),
50   myGaussMetric(VISU::AVERAGE_METRIC)
51 {
52 }
53
54
55 //----------------------------------------------------------------------------
56 VISU_Extractor
57 ::~VISU_Extractor()
58 {}
59
60
61 //----------------------------------------------------------------------------
62 void
63 VISU_Extractor
64 ::SetScalarMode(int theScalarMode)
65 {
66   if(myScalarMode != theScalarMode){
67     myScalarMode = theScalarMode;
68     Modified();
69   }
70 }
71
72 //----------------------------------------------------------------------------
73 int
74 VISU_Extractor
75 ::GetScalarMode()
76 {
77   return myScalarMode;
78 }
79
80
81 //----------------------------------------------------------------------------
82 void
83 VISU_Extractor
84 ::SetGaussMetric(VISU::TGaussMetric theGaussMetric)
85 {
86   if(myGaussMetric != theGaussMetric){
87     myGaussMetric = theGaussMetric;
88     Modified();
89   }
90 }
91
92 //----------------------------------------------------------------------------
93 VISU::TGaussMetric
94 VISU_Extractor
95 ::GetGaussMetric()
96 {
97   return myGaussMetric;
98 }
99
100
101 //----------------------------------------------------------------------------
102 template<typename TValueType> 
103 void
104 Module2Scalars(vtkDataArray *theInputDataArray,
105                TValueType* theOutputPtr,
106                vtkIdType theNbOfTuples)
107 {
108   vtkIdType aNbComp = theInputDataArray->GetNumberOfComponents();
109   std::vector<vtkFloatingPointType> anArray(aNbComp < 3? 3: aNbComp);
110   for(vtkIdType aTupleId = 0; aTupleId < theNbOfTuples; aTupleId++){
111     theInputDataArray->GetTuple(aTupleId, &anArray[0]);
112     vtkFloatingPointType aVector[3] = {anArray[0], anArray[1], anArray[2]};
113     vtkFloatingPointType aScalar = sqrt(aVector[0]*aVector[0] + 
114                                         aVector[1]*aVector[1] + 
115                                         aVector[2]*aVector[2]);
116     *theOutputPtr = TValueType(aScalar);
117     theOutputPtr++;
118   }
119 }
120
121
122 //----------------------------------------------------------------------------
123 template<typename TValueType> 
124 void
125 Component2Scalars(vtkDataArray *theInputDataArray,
126                   TValueType* theInputPtr,
127                   TValueType* theOutputPtr,
128                   vtkIdType theNbOfTuples,
129                   vtkIdType theComponentId)
130 {
131   vtkIdType aNbComp = theInputDataArray->GetNumberOfComponents();
132   for(vtkIdType aTupleId = 0; aTupleId < theNbOfTuples; aTupleId++){
133     *theOutputPtr = *(theInputPtr + theComponentId);
134     theInputPtr += aNbComp;
135     theOutputPtr++;
136   }
137 }
138
139
140 //----------------------------------------------------------------------------
141 template<typename TDataSetAttributesType> void
142 ExecuteScalars(vtkIdType theNbOfTuples, 
143                vtkIdType theScalarMode, 
144                VISU::TGaussMetric theGaussMetric, 
145                TDataSetAttributesType* theInputData, 
146                TDataSetAttributesType* theOutputData)
147 {
148   if(theNbOfTuples < 1)
149     return;
150
151   vtkDataArray* aFieldArray = NULL;
152   switch( theGaussMetric ) {
153     case VISU::AVERAGE_METRIC: aFieldArray = theInputData->GetArray("VISU_FIELD"); break;
154     case VISU::MINIMUM_METRIC: aFieldArray = theInputData->GetArray("VISU_FIELD_GAUSS_MIN"); break;
155     case VISU::MAXIMUM_METRIC: aFieldArray = theInputData->GetArray("VISU_FIELD_GAUSS_MAX"); break;
156   }
157   if( !aFieldArray )
158     return;
159
160   vtkIdType anInputDataType = aFieldArray->GetDataType();
161   vtkDataArray *anOutputScalars = vtkDataArray::CreateDataArray(anInputDataType);
162   anOutputScalars->SetNumberOfComponents(1);
163   anOutputScalars->SetNumberOfTuples(theNbOfTuples);
164
165   void *anInputPtr = aFieldArray->GetVoidPointer(0);
166   void *anOutputPtr = anOutputScalars->GetVoidPointer(0);
167
168   if(theScalarMode == 0){
169     switch(anInputDataType){
170       vtkTemplateMacro3(Module2Scalars,
171                         aFieldArray,
172                         (VTK_TT *)(anOutputPtr), 
173                         theNbOfTuples);
174     default:
175       break;
176     }
177   }else{
178     switch(anInputDataType){
179       vtkTemplateMacro5(Component2Scalars,
180                         aFieldArray,
181                         (VTK_TT *)(anInputPtr), 
182                         (VTK_TT *)(anOutputPtr),
183                         theNbOfTuples,
184                         theScalarMode - 1);
185     default:
186       break;
187     }
188   }
189   
190   theOutputData->SetScalars(anOutputScalars);
191   anOutputScalars->Delete();
192 }
193
194
195 //---------------------------------------------------------------
196 int
197 VISU_Extractor
198 ::RequestData(vtkInformation *theRequest,
199               vtkInformationVector **theInputVector,
200               vtkInformationVector *theOutputVector)
201 {
202   vtkDataSet *anInput = VISU::GetInput( theInputVector, 0 );
203   vtkDataSet *anOutput = VISU::GetOutput( theOutputVector );
204
205   anOutput->CopyStructure( anInput );
206
207   vtkPointData *anInputPointData = anInput->GetPointData();
208   vtkPointData *anOutputPointData = anOutput->GetPointData();
209   anOutputPointData->PassData( anInputPointData ); 
210   if ( VISU::IsDataOnPoints( anInput ) ) {
211     int aNbElems = anInput->GetNumberOfPoints();
212     if ( anInputPointData->GetAttribute( vtkDataSetAttributes::VECTORS ) )
213       ExecuteScalars( aNbElems, myScalarMode, myGaussMetric, anInputPointData, anOutputPointData );
214   }
215   
216   vtkCellData *anInputCellData = anInput->GetCellData();
217   vtkCellData *anOutputCellData = anOutput->GetCellData();
218   anOutputCellData->PassData( anInputCellData ); 
219   if ( VISU::IsDataOnCells( anInput ) ) {
220     int aNbElems = anInput->GetNumberOfCells();
221     if ( anInputCellData->GetAttribute( vtkDataSetAttributes::VECTORS ) )
222       ExecuteScalars( aNbElems, myScalarMode, myGaussMetric, anInputCellData, anOutputCellData );
223   }
224
225   return 1;
226 }