Salome HOME
Preferences
[modules/gui.git] / src / VTKViewer / VTKViewer_Actor.cxx
1 #include "VTKViewer_Actor.h"
2 #include "VTKViewer_Transform.h"
3 #include "VTKViewer_GeometryFilter.h"
4 #include "VTKViewer_TransformFilter.h"
5 #include "VTKViewer_PassThroughFilter.h"
6
7 // VTK Includes
8 #include <vtkObjectFactory.h>
9 #include <vtkDataSetMapper.h>
10 #include <vtkPolyDataMapper.h>
11 #include <vtkRenderer.h>
12 #include <vtkMapper.h>
13 #include <vtkPolyData.h>
14 #include <vtkCell.h>
15
16
17 /*
18 static void CopyPoints(vtkUnstructuredGrid* theGrid, vtkDataSet *theSourceDataSet){
19   vtkPoints *aPoints = vtkPoints::New();
20   vtkIdType iEnd = theSourceDataSet->GetNumberOfPoints();
21   aPoints->SetNumberOfPoints(iEnd);
22   for(vtkIdType i = 0; i < iEnd; i++){
23     aPoints->SetPoint(i,theSourceDataSet->GetPoint(i));
24   }
25   theGrid->SetPoints(aPoints);
26   aPoints->Delete();
27 }
28 */
29
30
31
32 vtkStandardNewMacro(VTKViewer_Actor);
33
34
35 VTKViewer_Actor::VTKViewer_Actor(){
36   myIsHighlighted = myIsPreselected = false;
37
38   myRepresentation = 1;
39   myDisplayMode = myRepresentation - 1;
40
41   myProperty = vtkProperty::New();
42   PreviewProperty = NULL;
43
44   myIsInfinite = false;
45   myIsResolveCoincidentTopology = true;
46
47   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
48                                                                  myPolygonOffsetUnits);
49   myStoreMapping = false;
50   myGeomFilter = VTKViewer_GeometryFilter::New();
51
52   myTransformFilter = VTKViewer_TransformFilter::New();
53
54   for(int i = 0; i < 6; i++)
55     myPassFilter.push_back(VTKViewer_PassThroughFilter::New());
56
57   Visibility = Pickable = true;
58 }
59
60
61 VTKViewer_Actor::~VTKViewer_Actor(){
62   SetPreviewProperty(NULL);
63
64   myGeomFilter->UnRegisterAllOutputs(); 
65   myGeomFilter->Delete();
66
67   myTransformFilter->UnRegisterAllOutputs();
68   myTransformFilter->Delete();
69
70   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++){
71     if(myPassFilter[i]){
72       myPassFilter[i]->UnRegisterAllOutputs(); 
73       myPassFilter[i]->Delete();
74     }
75   }
76   myProperty->Delete();
77 }
78
79 void VTKViewer_Actor::AddToRender(vtkRenderer* theRenderer){
80   theRenderer->AddActor(this);
81 }
82
83 void VTKViewer_Actor::RemoveFromRender(vtkRenderer* theRenderer){
84   theRenderer->RemoveActor(this);
85 }
86
87
88 void VTKViewer_Actor::SetTransform(VTKViewer_Transform* theTransform){
89   myTransformFilter->SetTransform(theTransform);
90 }
91
92
93 void VTKViewer_Actor::SetMapper(vtkMapper* theMapper){
94   InitPipeLine(theMapper);
95 }
96
97 void VTKViewer_Actor::InitPipeLine(vtkMapper* theMapper){
98   if(theMapper){
99     int anId = 0;
100     myPassFilter[ anId ]->SetInput( theMapper->GetInput() );
101     myPassFilter[ anId + 1]->SetInput( myPassFilter[ anId ]->GetOutput() );
102     
103     anId++; // 1
104     myGeomFilter->SetStoreMapping( myStoreMapping );
105     myGeomFilter->SetInput( myPassFilter[ anId ]->GetOutput() );
106
107     anId++; // 2
108     myPassFilter[ anId ]->SetInput( myGeomFilter->GetOutput() ); 
109     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
110
111     anId++; // 3
112     myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
113
114     anId++; // 4
115     myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
116     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
117
118     anId++; // 5
119     if(vtkDataSetMapper* aMapper = dynamic_cast<vtkDataSetMapper*>(theMapper)){
120       aMapper->SetInput(myPassFilter[anId]->GetOutput());
121     }else if(vtkPolyDataMapper* aMapper = dynamic_cast<vtkPolyDataMapper*>(theMapper)){
122       aMapper->SetInput(myPassFilter[anId]->GetPolyDataOutput());
123     }
124   }
125   vtkLODActor::SetMapper(theMapper);
126 }
127
128
129 void VTKViewer_Actor::Render(vtkRenderer *ren, vtkMapper* m){
130   if(myIsResolveCoincidentTopology){
131     int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
132     float aFactor, aUnit; 
133     vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
134     
135     vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
136     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
137                                                                    myPolygonOffsetUnits);
138     vtkLODActor::Render(ren,m);
139     
140     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
141     vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
142   }else{
143     vtkLODActor::Render(ren,m);
144   }
145 }
146
147
148 void VTKViewer_Actor::SetResolveCoincidentTopology(bool theIsResolve) {
149   myIsResolveCoincidentTopology = theIsResolve;
150 }
151
152 void VTKViewer_Actor::SetPolygonOffsetParameters(float factor, float units){
153   myPolygonOffsetFactor = factor;
154   myPolygonOffsetUnits = units;
155 }
156
157 void VTKViewer_Actor::GetPolygonOffsetParameters(float& factor, float& units){
158   factor = myPolygonOffsetFactor;
159   units = myPolygonOffsetUnits;
160 }
161
162
163 vtkDataSet* VTKViewer_Actor::GetInput(){
164   return myPassFilter.front()->GetOutput();
165 }
166
167
168 unsigned long int VTKViewer_Actor::GetMTime(){
169   unsigned long mTime = this->Superclass::GetMTime();
170   unsigned long time = myTransformFilter->GetMTime();
171   mTime = ( time > mTime ? time : mTime );
172   if(vtkDataSet *aDataSet = myPassFilter[0]->GetInput()){
173     time = aDataSet->GetMTime();
174     mTime = ( time > mTime ? time : mTime );
175   }
176   return mTime;
177 }
178
179
180 void VTKViewer_Actor::SetRepresentation(int theMode) { 
181   switch(myRepresentation){
182   case VTK_POINTS : 
183   case VTK_SURFACE : 
184     myProperty->DeepCopy(GetProperty());
185   }    
186   switch(theMode){
187   case VTK_POINTS : 
188   case VTK_SURFACE : 
189     GetProperty()->DeepCopy(myProperty);
190     break;
191   default:
192     break;
193     GetProperty()->SetAmbient(1.0);
194     GetProperty()->SetDiffuse(0.0);
195     GetProperty()->SetSpecular(0.0);
196   }
197   switch(theMode){
198   case 3 : 
199     myGeomFilter->SetInside(true);
200     GetProperty()->SetRepresentation(1);
201     break;
202   case VTK_POINTS : 
203     GetProperty()->SetPointSize(VTKViewer_POINT_SIZE);  
204   default :
205     GetProperty()->SetRepresentation(theMode);
206     myGeomFilter->SetInside(false);
207   }
208   myRepresentation = theMode;
209 }
210
211 int VTKViewer_Actor::GetRepresentation(){ 
212   return myRepresentation;
213 }
214
215
216 vtkCell* VTKViewer_Actor::GetElemCell(int theObjID){
217   return GetInput()->GetCell(theObjID);
218 }
219
220
221 float* VTKViewer_Actor::GetNodeCoord(int theObjID){
222   return GetInput()->GetPoint(theObjID);
223 }
224
225
226 //=================================================================================
227 // function : GetObjDimension
228 // purpose  : Return object dimension.
229 //            Virtual method shoulb be redifined by derived classes
230 //=================================================================================
231 int VTKViewer_Actor::GetObjDimension( const int theObjId )
232 {
233   if ( vtkCell* aCell = GetElemCell(theObjId) )
234     return aCell->GetCellDimension();
235   return 0;
236 }
237
238
239 bool VTKViewer_Actor::IsInfinitive(){ 
240   return myIsInfinite; 
241 }
242
243
244 void VTKViewer_Actor::SetOpacity(float theOpacity){ 
245   myOpacity = theOpacity;
246   GetProperty()->SetOpacity(theOpacity);
247 }
248
249 float VTKViewer_Actor::GetOpacity(){
250   return myOpacity;
251 }
252
253
254 void VTKViewer_Actor::SetColor(float r,float g,float b){
255   GetProperty()->SetColor(r,g,b);
256 }
257
258 void VTKViewer_Actor::GetColor(float& r,float& g,float& b){
259   float aColor[3];
260   GetProperty()->GetColor(aColor);
261   r = aColor[0];
262   g = aColor[1];
263   b = aColor[2];
264 }
265
266
267 int VTKViewer_Actor::getDisplayMode(){ 
268   return myDisplayMode; 
269 }
270
271 void VTKViewer_Actor::setDisplayMode(int theMode){ 
272   SetRepresentation(theMode+1); 
273   myDisplayMode = GetRepresentation() - 1;
274 }
275