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