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