Salome HOME
950bf83cdea1969d856c259951820933af252084
[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.at( anId )->SetInput( theGrid );
145     myPassFilter.at( anId + 1)->SetInput( myPassFilter.at( anId )->GetOutput() );
146     
147     anId++; // 1
148     myGeomFilter->SetStoreMapping( myStoreMapping );
149     myGeomFilter->SetInput( myPassFilter.at( anId )->GetOutput() );
150
151     anId++; // 2
152     myPassFilter.at( anId )->SetInput( myGeomFilter->GetOutput() ); 
153     myPassFilter.at( anId + 1 )->SetInput( myPassFilter.at( anId )->GetOutput() );
154
155     anId++; // 3
156     myTransformFilter->SetInput( myPassFilter.at( anId )->GetPolyDataOutput() );
157
158     anId++; // 4
159     myPassFilter.at( anId )->SetInput( myTransformFilter->GetOutput() );
160     myPassFilter.at( anId + 1 )->SetInput( myPassFilter.at( anId )->GetOutput() );
161
162     anId++; // 5
163     myMapper->SetInput( myPassFilter.at( anId )->GetPolyDataOutput() );
164
165     vtkLODActor::SetMapper( myMapper );
166     Modified();
167   }
168 }
169
170
171 SALOME_ExtractUnstructuredGrid* SMESH_DeviceActor::GetExtractUnstructuredGrid(){
172   return myExtractUnstructuredGrid;
173 }
174
175
176 vtkUnstructuredGrid* SMESH_DeviceActor::GetUnstructuredGrid(){
177   myExtractUnstructuredGrid->Update();
178   return myExtractUnstructuredGrid->GetOutput();
179 }
180
181
182 vtkMergeFilter* SMESH_DeviceActor::GetMergeFilter(){
183   return myMergeFilter;
184 }
185
186
187 vtkPolyData* SMESH_DeviceActor::GetPolyDataInput(){
188   return myPassFilter.back()->GetPolyDataOutput();
189 }
190
191
192 unsigned long int SMESH_DeviceActor::GetMTime(){
193   unsigned long mTime = this->Superclass::GetMTime();
194   mTime = max(mTime,myExtractUnstructuredGrid->GetMTime());
195   mTime = max(mTime,myMergeFilter->GetMTime());
196   mTime = max(mTime,myGeomFilter->GetMTime());
197   mTime = max(mTime,myTransformFilter->GetMTime());
198   return mTime;
199 }
200
201
202 void SMESH_DeviceActor::SetTransform(SALOME_Transform* theTransform){
203   myTransformFilter->SetTransform(theTransform);
204 }
205
206
207 void SMESH_DeviceActor::SetShrink()
208 {
209   if ( !myIsShrinkable ) return;
210   if ( vtkDataSet* aDataSet = myPassFilter.at( 0 )->GetOutput() )
211   {
212     myShrinkFilter->SetInput( aDataSet );
213     myPassFilter.at( 1 )->SetInput( myShrinkFilter->GetOutput() );
214     myIsShrunk = true;
215   }
216 }
217
218 void SMESH_DeviceActor::UnShrink()
219 {
220   if ( !myIsShrunk ) return;
221   if ( vtkDataSet* aDataSet = myPassFilter.at( 0 )->GetOutput() )
222   {
223     myPassFilter.at( 1 )->SetInput( aDataSet );
224     myPassFilter.at( 1 )->Modified();
225     myIsShrunk = false;
226     Modified();
227   }
228 }
229
230
231 void SMESH_DeviceActor::SetRepresentation(EReperesent theMode){ 
232   switch(theMode){
233   case ePoint: 
234     myGeomFilter->SetInside(true);
235     GetProperty()->SetRepresentation(0);
236     break;
237   case eInsideframe: 
238     myGeomFilter->SetInside(true);
239     GetProperty()->SetRepresentation(1);
240     break;
241   default :
242     GetProperty()->SetRepresentation(theMode);
243     myGeomFilter->SetInside(false);
244   }
245   myRepresentation = theMode;
246   myMapper->Modified();
247   Modified();
248 }
249
250
251 void SMESH_DeviceActor::SetVisibility(int theMode){
252   if(!myExtractUnstructuredGrid->GetInput() || GetUnstructuredGrid()->GetNumberOfCells()){
253     vtkLODActor::SetVisibility(theMode);
254   }else{
255     vtkLODActor::SetVisibility(false);
256   }
257 }
258
259
260 int SMESH_DeviceActor::GetVisibility(){
261   if(!GetUnstructuredGrid()->GetNumberOfCells()){
262     vtkLODActor::SetVisibility(false);
263   }
264   return vtkLODActor::GetVisibility();
265 }
266
267
268 int SMESH_DeviceActor::GetObjId(int theVtkID){
269   if (GetRepresentation() == ePoint){
270     return GetNodeObjId(theVtkID);
271   }else{
272     return GetElemObjId(theVtkID);
273   }
274 }
275
276
277 SMESH_DeviceActor::TVectorId SMESH_DeviceActor::GetVtkId(int theObjID){
278   if (GetRepresentation() == ePoint){
279     return GetNodeVtkId(theObjID);
280   }else{
281     return GetElemVtkId(theObjID);
282   }
283 }
284
285
286 int SMESH_DeviceActor::GetNodeObjId(int theVtkID){
287   vtkIdType aRetID = myVisualObj->GetNodeObjId(theVtkID);
288   if(MYDEBUG) MESSAGE("GetNodeObjId - theVtkID = "<<theVtkID<<"; aRetID = "<<aRetID);
289   return aRetID;
290 }
291
292 SMESH_DeviceActor::TVectorId SMESH_DeviceActor::GetNodeVtkId(int theObjID){
293   SMESH_DeviceActor::TVectorId aVecId;
294   vtkIdType anID = myVisualObj->GetNodeVTKId(theObjID);
295   if(anID < 0) 
296     return aVecId;
297   aVecId.push_back(anID);
298   return aVecId;
299 }
300
301
302 int SMESH_DeviceActor::GetElemObjId(int theVtkID){
303   vtkIdType aGridID = myGeomFilter->GetObjId(theVtkID);
304   if(aGridID < 0) 
305     return -1;
306   vtkIdType anExtractID = myExtractUnstructuredGrid->GetOutId(aGridID);
307   if(anExtractID < 0) 
308     return -1;
309   vtkIdType aRetID = myVisualObj->GetElemObjId(anExtractID);
310   if(MYDEBUG) 
311     MESSAGE("GetElemObjId - theVtkID = "<<theVtkID<<"; anExtractID = "<<anExtractID<<"; aGridID = "<<aGridID<<"; aRetID = "<<aRetID);
312   return aRetID;
313 }
314
315 SMESH_DeviceActor::TVectorId SMESH_DeviceActor::GetElemVtkId(int theObjID){
316   TVectorId aVecId;
317   vtkIdType aGridID = myVisualObj->GetElemVTKId(theObjID);
318   if(aGridID < 0) 
319     return aVecId;
320   aVecId = myGeomFilter->GetVtkId(aGridID);
321   if(MYDEBUG) 
322     MESSAGE("GetElemVtkId - theObjID = "<<theObjID<<"; aGridID = "<<aGridID<<"; aGridID = "<<aGridID<<"; aVecId[0] = "<<aVecId[0]);
323   return aVecId;
324 }
325
326
327 float SMESH_DeviceActor::GetShrinkFactor(){
328   return myShrinkFilter->GetShrinkFactor();
329 }
330
331 void SMESH_DeviceActor::SetShrinkFactor(float theValue){
332   theValue = theValue > 0.1? theValue: 0.8;
333   myShrinkFilter->SetShrinkFactor(theValue);
334   Modified();
335 }
336
337
338 void SMESH_DeviceActor::Render(vtkRenderer *ren, vtkMapper* m){
339   int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
340   float aFactor, aUnit; 
341   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
342
343   vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
344   vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
345                                                                  myPolygonOffsetUnits);
346   vtkLODActor::Render(ren,m);
347
348   vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
349   vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
350 }
351
352
353 void SMESH_DeviceActor::SetPolygonOffsetParameters(float factor, float units){
354   myPolygonOffsetFactor = factor;
355   myPolygonOffsetUnits = units;
356 }
357