Salome HOME
ptv, do not send mouse release event just after mouse double click
[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 /*!Constructor.Initialize default parameters.*/
36 VTKViewer_Actor::VTKViewer_Actor(){
37   myIsHighlighted = myIsPreselected = false;
38
39   myRepresentation = 1;
40   myDisplayMode = myRepresentation - 1;
41
42   myProperty = vtkProperty::New();
43   PreviewProperty = NULL;
44
45   myIsInfinite = false;
46   myIsResolveCoincidentTopology = true;
47
48   vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
49                                                                  myPolygonOffsetUnits);
50   myStoreMapping = false;
51   myGeomFilter = VTKViewer_GeometryFilter::New();
52
53   myTransformFilter = VTKViewer_TransformFilter::New();
54
55   for(int i = 0; i < 6; i++)
56     myPassFilter.push_back(VTKViewer_PassThroughFilter::New());
57
58   Visibility = Pickable = true;
59 }
60
61
62 /*!Destructor.*/
63 VTKViewer_Actor::~VTKViewer_Actor(){
64   SetPreviewProperty(NULL);
65
66   myGeomFilter->UnRegisterAllOutputs(); 
67   myGeomFilter->Delete();
68
69   myTransformFilter->UnRegisterAllOutputs();
70   myTransformFilter->Delete();
71
72   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++){
73     if(myPassFilter[i]){
74       myPassFilter[i]->UnRegisterAllOutputs(); 
75       myPassFilter[i]->Delete();
76     }
77   }
78   myProperty->Delete();
79 }
80
81 /*!Add VTKViewer_Actor to renderer.
82  *\param theRenderer - vtkRenderer
83  */
84 void VTKViewer_Actor::AddToRender(vtkRenderer* theRenderer){
85   theRenderer->AddActor(this);
86 }
87
88 /*!Remove VTKViewer_Actor from renderer.
89  *\param theRenderer - vtkRenderer
90  */
91 void VTKViewer_Actor::RemoveFromRender(vtkRenderer* theRenderer){
92   theRenderer->RemoveActor(this);
93 }
94
95 /*!Add transformation to transform filter.
96  *\param theTransform - transformation.
97  */
98 void VTKViewer_Actor::SetTransform(VTKViewer_Transform* theTransform){
99   myTransformFilter->SetTransform(theTransform);
100 }
101
102 /*!Set mapper to pipeline.\n
103  *Call method for pipeline initialization.
104  *\param theMapper - mapper
105  */
106 void VTKViewer_Actor::SetMapper(vtkMapper* theMapper){
107   InitPipeLine(theMapper);
108 }
109
110 /*!Initialize sequence of filters for mapper, if \a theMapper is not null.
111  *\param theMapper - mapper
112  */
113 void VTKViewer_Actor::InitPipeLine(vtkMapper* theMapper){
114   if(theMapper){
115     int anId = 0;
116     myPassFilter[ anId ]->SetInput( theMapper->GetInput() );
117     myPassFilter[ anId + 1]->SetInput( myPassFilter[ anId ]->GetOutput() );
118     
119     anId++; // 1
120     myGeomFilter->SetStoreMapping( myStoreMapping );
121     myGeomFilter->SetInput( myPassFilter[ anId ]->GetOutput() );
122
123     anId++; // 2
124     myPassFilter[ anId ]->SetInput( myGeomFilter->GetOutput() ); 
125     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
126
127     anId++; // 3
128     myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
129
130     anId++; // 4
131     myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
132     myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
133
134     anId++; // 5
135     if(vtkDataSetMapper* aMapper = dynamic_cast<vtkDataSetMapper*>(theMapper)){
136       aMapper->SetInput(myPassFilter[anId]->GetOutput());
137     }else if(vtkPolyDataMapper* aMapper = dynamic_cast<vtkPolyDataMapper*>(theMapper)){
138       aMapper->SetInput(myPassFilter[anId]->GetPolyDataOutput());
139     }
140   }
141   vtkLODActor::SetMapper(theMapper);
142 }
143
144 /*!*/
145 void VTKViewer_Actor::Render(vtkRenderer *ren, vtkMapper* m){
146   if(myIsResolveCoincidentTopology){
147     int aResolveCoincidentTopology = vtkMapper::GetResolveCoincidentTopology();
148     float aFactor, aUnit; 
149     vtkMapper::GetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
150     
151     vtkMapper::SetResolveCoincidentTopologyToPolygonOffset();
152     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(myPolygonOffsetFactor,
153                                                                    myPolygonOffsetUnits);
154     vtkLODActor::Render(ren,m);
155     
156     vtkMapper::SetResolveCoincidentTopologyPolygonOffsetParameters(aFactor,aUnit);
157     vtkMapper::SetResolveCoincidentTopology(aResolveCoincidentTopology);
158   }else{
159     vtkLODActor::Render(ren,m);
160   }
161 }
162
163 /*!Set flag myIsResolveCoincidentTopology to \a theIsResolve.
164  *\param theIsResolve - bool flag.
165  */
166 void VTKViewer_Actor::SetResolveCoincidentTopology(bool theIsResolve) {
167   myIsResolveCoincidentTopology = theIsResolve;
168 }
169
170 /*!Sets polygon offset factor and polygon offset units.
171  *\param factor - float factor
172  *\param units  - float units
173  */
174 void VTKViewer_Actor::SetPolygonOffsetParameters(float factor, float units){
175   myPolygonOffsetFactor = factor;
176   myPolygonOffsetUnits = units;
177 }
178
179 /*!Gets polygon offset factor and polygon offset units.
180  *\param factor - output float
181  *\param units  - output float
182  */
183 void VTKViewer_Actor::GetPolygonOffsetParameters(float& factor, float& units){
184   factor = myPolygonOffsetFactor;
185   units = myPolygonOffsetUnits;
186 }
187
188
189 /*!Get input data set.
190  *\retval vtkDataSet pointer.
191  */
192 vtkDataSet* VTKViewer_Actor::GetInput(){
193   return myPassFilter.front()->GetOutput();
194 }
195
196 /*!Get modification time.
197  *\retval time - unsigned long.
198  */
199 unsigned long int VTKViewer_Actor::GetMTime(){
200   unsigned long mTime = this->Superclass::GetMTime();
201   unsigned long time = myTransformFilter->GetMTime();
202   mTime = ( time > mTime ? time : mTime );
203   if(vtkDataSet *aDataSet = myPassFilter[0]->GetInput()){
204     time = aDataSet->GetMTime();
205     mTime = ( time > mTime ? time : mTime );
206   }
207   return mTime;
208 }
209
210 /*!Set representation mode.
211  *\param theMode - int.
212  */
213 void VTKViewer_Actor::SetRepresentation(int theMode) { 
214   switch(myRepresentation){
215   case VTK_POINTS : 
216   case VTK_SURFACE : 
217     myProperty->DeepCopy(GetProperty());
218   }    
219   switch(theMode){
220   case VTK_POINTS : 
221   case VTK_SURFACE : 
222     GetProperty()->DeepCopy(myProperty);
223     break;
224   default:
225     break;
226     GetProperty()->SetAmbient(1.0);
227     GetProperty()->SetDiffuse(0.0);
228     GetProperty()->SetSpecular(0.0);
229   }
230   switch(theMode){
231   case 3 : 
232     myGeomFilter->SetInside(true);
233     GetProperty()->SetRepresentation(1);
234     break;
235   case VTK_POINTS : 
236     GetProperty()->SetPointSize(VTKViewer_POINT_SIZE);  
237   default :
238     GetProperty()->SetRepresentation(theMode);
239     myGeomFilter->SetInside(false);
240   }
241   myRepresentation = theMode;
242 }
243
244 /*!Get representation.
245  *\retval representation mode.
246  */
247 int VTKViewer_Actor::GetRepresentation(){ 
248   return myRepresentation;
249 }
250
251 /*!Get VTK cell by object ID.
252  *\param theObjID - object ID.
253  *\retval vtkCell pointer.
254  */
255 vtkCell* VTKViewer_Actor::GetElemCell(int theObjID){
256   return GetInput()->GetCell(theObjID);
257 }
258
259 /*!Get node coordinates by node ID.
260  *\param theObjID - node ID.
261  *\retval float array of coordinates.
262  * \li array[0] - X coordinate.
263  * \li array[1] - Y coordinate.
264  * \li array[2] - Z coordinate.
265  */
266 float* VTKViewer_Actor::GetNodeCoord(int theObjID){
267   return GetInput()->GetPoint(theObjID);
268 }
269
270
271 //=================================================================================
272 // function : GetObjDimension
273 /*! purpose  : Return object dimension.\n
274  *            Virtual method shoulb be redifined by derived classes
275  *\param theObjId - object ID.
276  */
277 //=================================================================================
278 int VTKViewer_Actor::GetObjDimension( const int theObjId )
279 {
280   if ( vtkCell* aCell = GetElemCell(theObjId) )
281     return aCell->GetCellDimension();
282   return 0;
283 }
284
285 /*!Get infinite flag*/
286 bool VTKViewer_Actor::IsInfinitive(){ 
287   return myIsInfinite; 
288 }
289
290 /*!Set property - opacity.
291  *\param theOpacity - new apacity
292  */
293 void VTKViewer_Actor::SetOpacity(float theOpacity){ 
294   myOpacity = theOpacity;
295   GetProperty()->SetOpacity(theOpacity);
296 }
297
298 /*!Get property - opacity.
299  *\retval float value.
300  */
301 float VTKViewer_Actor::GetOpacity(){
302   return myOpacity;
303 }
304
305 /*!Set property - color
306  *\param r - float Red value
307  *\param g - float Green value
308  *\param b - float Blue value
309  */
310 void VTKViewer_Actor::SetColor(float r,float g,float b){
311   GetProperty()->SetColor(r,g,b);
312 }
313
314 /*!Get property - color
315  *\param r - output float Red value
316  *\param g - output float Green value
317  *\param b - output float Blue value
318  */
319 void VTKViewer_Actor::GetColor(float& r,float& g,float& b){
320   float aColor[3];
321   GetProperty()->GetColor(aColor);
322   r = aColor[0];
323   g = aColor[1];
324   b = aColor[2];
325 }
326
327 /*!Get display mode.
328  *\retval int value
329  */
330 int VTKViewer_Actor::getDisplayMode(){ 
331   return myDisplayMode; 
332 }
333
334 /*!Set display mode
335  *\param theMode - integer value.
336  */
337 void VTKViewer_Actor::setDisplayMode(int theMode){ 
338   SetRepresentation(theMode+1); 
339   myDisplayMode = GetRepresentation() - 1;
340 }
341