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