Salome HOME
DCQ:prepare 2.0.0
[modules/smesh.git] / src / OBJECT / SMESH_DeviceActor.cxx
1 //  SMESH OBJECT : interactive object for SMESH visualization
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 //
24 //  File   : SMESH_Actor.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SMESH
27 //  $Header$
28
29
30 #include "SMESH_DeviceActor.h"
31
32 #include "SALOME_Transform.h"
33 #include "SALOME_TransformFilter.h"
34 #include "SALOME_PassThroughFilter.h"
35 #include "SALOME_ExtractUnstructuredGrid.h"
36
37 #include "utilities.h"
38
39 // VTK Includes
40 #include <vtkObjectFactory.h>
41 #include <vtkShrinkFilter.h>
42 #include <vtkShrinkPolyData.h>
43
44 #include <vtkProperty.h>
45 #include <vtkPolyData.h>
46 #include <vtkMergeFilter.h>
47 #include <vtkPolyDataMapper.h>
48 #include <vtkUnstructuredGrid.h>
49
50 #ifdef _DEBUG_
51 static int MYDEBUG = 0;
52 static int MYDEBUGWITHFILES = 0;
53 #else
54 static int MYDEBUG = 0;
55 static int MYDEBUGWITHFILES = 0;
56 #endif
57
58 using namespace std;
59
60
61 vtkStandardNewMacro(SMESH_DeviceActor);
62
63
64 SMESH_DeviceActor::SMESH_DeviceActor(){
65   if(MYDEBUG) MESSAGE("SMESH_DeviceActor");
66   myIsShrunk = false;
67   myIsShrinkable = false;
68   myRepresentation = eSurface;
69
70   myProperty = vtkProperty::New();
71   myMapper = vtkPolyDataMapper::New();
72
73   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
74                                                                  myPolygonOffsetUnits);
75   //myMapper->SetResolveCoincidentTopologyToShiftZBuffer();
76   //myMapper->SetResolveCoincidentTopologyZShift(0.02);
77   
78   myMapper->UseLookupTableScalarRangeOn();
79   myMapper->SetColorModeToMapScalars();
80
81   myShrinkFilter = vtkShrinkFilter::New();
82
83   myExtractUnstructuredGrid = SALOME_ExtractUnstructuredGrid::New();
84   myExtractUnstructuredGrid->SetStoreMapping(true);
85
86   myMergeFilter = vtkMergeFilter::New();
87
88   myStoreMapping = false;
89   myGeomFilter = SALOME_GeometryFilter::New();
90
91   myTransformFilter = SALOME_TransformFilter::New();
92
93   for(int i = 0; i < 6; i++)
94     myPassFilter.push_back(SALOME_PassThroughFilter::New());
95 }
96
97
98 SMESH_DeviceActor::~SMESH_DeviceActor(){
99   if(MYDEBUG) MESSAGE("~SMESH_DeviceActor");
100   myProperty->Delete();
101
102   myMapper->RemoveAllInputs();
103   myMapper->Delete();
104
105   myShrinkFilter->UnRegisterAllOutputs();
106   myShrinkFilter->Delete();
107
108   myExtractUnstructuredGrid->UnRegisterAllOutputs();
109   myExtractUnstructuredGrid->Delete();
110
111   myMergeFilter->UnRegisterAllOutputs();
112   myMergeFilter->Delete();
113
114   myGeomFilter->UnRegisterAllOutputs();
115   myGeomFilter->Delete();
116
117   myTransformFilter->UnRegisterAllOutputs();
118   myTransformFilter->Delete();
119
120   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++){
121     myPassFilter[i]->UnRegisterAllOutputs(); 
122     myPassFilter[i]->Delete();
123   }
124 }
125
126
127 void SMESH_DeviceActor::SetStoreMapping(int theStoreMapping){
128   myStoreMapping = theStoreMapping;
129   Modified();
130 }
131
132
133 void SMESH_DeviceActor::SetUnstructuredGrid(vtkUnstructuredGrid* theGrid){
134   if(theGrid){
135     //myIsShrinkable = theGrid->GetNumberOfCells() > 10;
136     myIsShrinkable = true;
137
138     myExtractUnstructuredGrid->SetInput(theGrid);
139     myMergeFilter->SetGeometry(myExtractUnstructuredGrid->GetOutput());
140
141     theGrid = static_cast<vtkUnstructuredGrid*>(myMergeFilter->GetOutput());
142
143     int anId = 0;
144     myPassFilter[ anId ]->SetInput( theGrid );
145     myPassFilter[ anId + 1]->SetInput( myPassFilter[ anId ]->GetOutput() );
146     
147     anId++; // 1
148     myGeomFilter->SetStoreMapping( myStoreMapping );
149     myGeomFilter->SetInput( myPassFilter[ anId ]->GetOutput() );
150
151     anId++; // 2
152     myPassFilter[ anId ]->SetInput( myGeomFilter->GetOutput() ); 
153     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
154
155     anId++; // 3
156     myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
157     myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
158
159     anId++; // 4
160     myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
161     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
162     myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
163     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
164
165     anId++; // 5
166     myMapper->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
167     myMapper->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
168
169     vtkLODActor::SetMapper( myMapper );
170     Modified();
171   }
172 }
173
174
175 SALOME_ExtractUnstructuredGrid* SMESH_DeviceActor::GetExtractUnstructuredGrid(){
176   return myExtractUnstructuredGrid;
177 }
178
179
180 vtkUnstructuredGrid* SMESH_DeviceActor::GetUnstructuredGrid(){
181   myExtractUnstructuredGrid->Update();
182   return myExtractUnstructuredGrid->GetOutput();
183 }
184
185
186 vtkMergeFilter* SMESH_DeviceActor::GetMergeFilter(){
187   return myMergeFilter;
188 }
189
190
191 vtkPolyData* SMESH_DeviceActor::GetPolyDataInput(){
192   return myPassFilter.back()->GetPolyDataOutput();
193 }
194
195
196 unsigned long int SMESH_DeviceActor::GetMTime(){
197   unsigned long mTime = this->Superclass::GetMTime();
198   mTime = max(mTime,myExtractUnstructuredGrid->GetMTime());
199   mTime = max(mTime,myMergeFilter->GetMTime());
200   mTime = max(mTime,myGeomFilter->GetMTime());
201   mTime = max(mTime,myTransformFilter->GetMTime());
202   return mTime;
203 }
204
205
206 void SMESH_DeviceActor::SetTransform(SALOME_Transform* theTransform){
207   myTransformFilter->SetTransform(theTransform);
208 }
209
210
211 void SMESH_DeviceActor::SetShrink()
212 {
213   if ( !myIsShrinkable ) return;
214   if ( vtkDataSet* aDataSet = myPassFilter[ 0 ]->GetOutput() )
215   {
216     myShrinkFilter->SetInput( aDataSet );
217     myPassFilter[ 1 ]->SetInput( myShrinkFilter->GetOutput() );
218     myIsShrunk = true;
219   }
220 }
221
222 void SMESH_DeviceActor::UnShrink()
223 {
224   if ( !myIsShrunk ) return;
225   if ( vtkDataSet* aDataSet = myPassFilter[ 0 ]->GetOutput() )
226   {    
227     myPassFilter[ 1 ]->SetInput( aDataSet );
228     myPassFilter[ 1 ]->Modified();
229     myIsShrunk = false;
230     Modified();
231   }
232 }
233
234
235 void SMESH_DeviceActor::SetRepresentation(EReperesent theMode){ 
236   switch(theMode){
237   case ePoint: 
238     myGeomFilter->SetInside(true);
239     GetProperty()->SetRepresentation(0);
240     break;
241   case eInsideframe: 
242     myGeomFilter->SetInside(true);
243     GetProperty()->SetRepresentation(1);
244     break;
245   default :
246     GetProperty()->SetRepresentation(theMode);
247     myGeomFilter->SetInside(false);
248   }
249   myRepresentation = theMode;
250   myMapper->Modified();
251   Modified();
252 }
253
254
255 void SMESH_DeviceActor::SetVisibility(int theMode){
256   if(!myExtractUnstructuredGrid->GetInput() || GetUnstructuredGrid()->GetNumberOfCells()){
257     vtkLODActor::SetVisibility(theMode);
258   }else{
259     vtkLODActor::SetVisibility(false);
260   }
261 }
262
263
264 int SMESH_DeviceActor::GetVisibility(){
265   if(!GetUnstructuredGrid()->GetNumberOfCells()){
266     vtkLODActor::SetVisibility(false);
267   }
268   return vtkLODActor::GetVisibility();
269 }
270
271
272 int SMESH_DeviceActor::GetObjId(int theVtkID){
273   if (GetRepresentation() == ePoint){
274     return GetNodeObjId(theVtkID);
275   }else{
276     return GetElemObjId(theVtkID);
277   }
278 }
279
280
281 SMESH_DeviceActor::TVectorId SMESH_DeviceActor::GetVtkId(int theObjID){
282   if (GetRepresentation() == ePoint){
283     return GetNodeVtkId(theObjID);
284   }else{
285     return GetElemVtkId(theObjID);
286   }
287 }
288
289
290 int SMESH_DeviceActor::GetNodeObjId(int theVtkID){
291   vtkIdType aRetID = myVisualObj->GetNodeObjId(theVtkID);
292   if(MYDEBUG) MESSAGE("GetNodeObjId - theVtkID = "<<theVtkID<<"; aRetID = "<<aRetID);
293   return aRetID;
294 }
295
296 SMESH_DeviceActor::TVectorId SMESH_DeviceActor::GetNodeVtkId(int theObjID){
297   SMESH_DeviceActor::TVectorId aVecId;
298   vtkIdType anID = myVisualObj->GetNodeVTKId(theObjID);
299   if(anID < 0) 
300     return aVecId;
301   aVecId.push_back(anID);
302   return aVecId;
303 }
304
305
306 int SMESH_DeviceActor::GetElemObjId(int theVtkID){
307   vtkIdType aGridID = myGeomFilter->GetObjId(theVtkID);
308   if(aGridID < 0) 
309     return -1;
310   vtkIdType anExtractID = myExtractUnstructuredGrid->GetOutId(aGridID);
311   if(anExtractID < 0) 
312     return -1;
313   vtkIdType aRetID = myVisualObj->GetElemObjId(anExtractID);
314   if(MYDEBUG) 
315     MESSAGE("GetElemObjId - theVtkID = "<<theVtkID<<"; anExtractID = "<<anExtractID<<"; aGridID = "<<aGridID<<"; aRetID = "<<aRetID);
316   return aRetID;
317 }
318
319 SMESH_DeviceActor::TVectorId SMESH_DeviceActor::GetElemVtkId(int theObjID){
320   TVectorId aVecId;
321   vtkIdType aGridID = myVisualObj->GetElemVTKId(theObjID);
322   if(aGridID < 0) 
323     return aVecId;
324   aVecId = myGeomFilter->GetVtkId(aGridID);
325   if(MYDEBUG) 
326     MESSAGE("GetElemVtkId - theObjID = "<<theObjID<<"; aGridID = "<<aGridID<<"; aGridID = "<<aGridID<<"; aVecId[0] = "<<aVecId[0]);
327   return aVecId;
328 }
329
330
331 float SMESH_DeviceActor::GetShrinkFactor(){
332   return myShrinkFilter->GetShrinkFactor();
333 }
334
335 void SMESH_DeviceActor::SetShrinkFactor(float theValue){
336   theValue = theValue > 0.1? theValue: 0.8;
337   myShrinkFilter->SetShrinkFactor(theValue);
338   Modified();
339 }
340
341
342 void SMESH_DeviceActor::Render(vtkRenderer *ren, vtkMapper* m){
343   int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
344   float aFactor, aUnit; 
345   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
346
347   vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
348   vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
349                                                                  myPolygonOffsetUnits);
350   vtkLODActor::Render(ren,m);
351
352   vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
353   vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
354 }
355
356
357 void SMESH_DeviceActor::SetPolygonOffsetParameters(float factor, float units){
358   myPolygonOffsetFactor = factor;
359   myPolygonOffsetUnits = units;
360 }
361