Salome HOME
DCQ: prepare V2.0.0
[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 <vtkObjectFactory.h>
45 #include <vtkDataSetMapper.h>
46 #include <vtkPolyDataMapper.h>
47
48 using namespace std;
49
50 #if defined __GNUC__
51   #if __GNUC__ == 2
52     #define __GNUC_2__
53   #endif
54 #endif
55
56 int SALOME_POINT_SIZE = 3;
57
58
59 vtkStandardNewMacro(SALOME_Actor);
60
61
62 SALOME_Actor::SALOME_Actor(){
63   PreviewProperty = NULL;
64   ispreselected = Standard_False;
65   myProperty = vtkProperty::New();
66
67   myRepresentation = 2;
68
69   myIsInfinite = false;
70
71   myStoreMapping = false;
72   myGeomFilter = SALOME_GeometryFilter::New();
73
74   myTransformFilter = SALOME_TransformFilter::New();
75
76   for(int i = 0; i < 6; i++)
77     myPassFilter.push_back(SALOME_PassThroughFilter::New());
78 }
79
80
81 SALOME_Actor::~SALOME_Actor(){
82   SetPreviewProperty(NULL);
83
84   myGeomFilter->UnRegisterAllOutputs(); 
85   myGeomFilter->Delete();
86
87   myTransformFilter->UnRegisterAllOutputs();
88   myTransformFilter->Delete();
89
90   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++){
91     if(myPassFilter[i]){
92       myPassFilter[i]->UnRegisterAllOutputs(); 
93       myPassFilter[i]->Delete();
94     }
95   }
96
97   myProperty->Delete();
98 }
99
100
101 void SALOME_Actor::AddToRender(vtkRenderer* theRenderer){
102   theRenderer->AddActor(this);
103 }
104
105 void SALOME_Actor::RemoveFromRender(vtkRenderer* theRenderer){
106   theRenderer->RemoveActor(this);
107 }
108
109
110 void SALOME_Actor::SetTransform(SALOME_Transform* theTransform){
111   myTransformFilter->SetTransform(theTransform);
112 }
113
114 void SALOME_Actor::SetMapper(vtkMapper* theMapper){
115   if(theMapper){
116     int anId = 0;
117 #if defined __GNUC_2__
118     myPassFilter[anId]->SetInput(theMapper->GetInput());
119 #else
120     myPassFilter.at(anId)->SetInput(theMapper->GetInput());
121 #endif
122     myGeomFilter->SetStoreMapping(myStoreMapping);
123 #if defined __GNUC_2__
124     myGeomFilter->SetInput(myPassFilter[anId]->GetOutput());
125 #else
126     myGeomFilter->SetInput(myPassFilter.at(anId)->GetOutput());
127 #endif
128
129     anId++;
130
131 #if defined __GNUC_2__
132     myPassFilter[anId]->SetInput(myGeomFilter->GetOutput());
133     myPassFilter[anId+1]->SetInput(myPassFilter[anId]->GetOutput());
134 #else
135     myPassFilter.at(anId)->SetInput(myGeomFilter->GetOutput());
136     myPassFilter.at(anId+1)->SetInput(myPassFilter.at(anId)->GetOutput());
137 #endif
138
139     anId++;
140 #if defined __GNUC_2__
141     myPassFilter[anId+1]->SetInput(myPassFilter[anId]->GetOutput());
142 #else
143     myPassFilter.at(anId+1)->SetInput(myPassFilter.at(anId)->GetOutput());
144 #endif
145
146     anId++;
147 #if defined __GNUC_2__
148     myTransformFilter->SetInput(myPassFilter[anId]->GetPolyDataOutput());
149 #else
150     myTransformFilter->SetInput(myPassFilter.at(anId)->GetPolyDataOutput());
151 #endif
152
153     anId++;
154 #if defined __GNUC_2__
155     myPassFilter[anId]->SetInput(myTransformFilter->GetOutput());
156     myPassFilter[anId+1]->SetInput(myPassFilter[anId]->GetOutput());
157 #else
158     myPassFilter.at(anId)->SetInput(myTransformFilter->GetOutput());
159     myPassFilter.at(anId+1)->SetInput(myPassFilter.at(anId)->GetOutput());
160 #endif
161
162     anId++;
163     if(vtkDataSetMapper* aMapper = dynamic_cast<vtkDataSetMapper*>(theMapper))
164 #if defined __GNUC_2__
165       aMapper->SetInput(myPassFilter[anId]->GetOutput());
166 #else
167       aMapper->SetInput(myPassFilter.at(anId)->GetOutput());
168 #endif
169     else if(vtkPolyDataMapper* aMapper = dynamic_cast<vtkPolyDataMapper*>(theMapper))
170 #if defined __GNUC_2__
171       aMapper->SetInput(myPassFilter[anId]->GetPolyDataOutput());
172 #else
173       aMapper->SetInput(myPassFilter.at(anId)->GetPolyDataOutput());
174 #endif
175   }
176   vtkLODActor::SetMapper(theMapper);
177 }
178
179 vtkPolyData* SALOME_Actor::GetPolyDataInput(){
180   return myPassFilter.back()->GetPolyDataOutput();
181 }
182
183
184 unsigned long int SALOME_Actor::GetMTime(){
185   unsigned long mTime = this->Superclass::GetMTime();
186   unsigned long time = myTransformFilter->GetMTime();
187   mTime = ( time > mTime ? time : mTime );
188   if(vtkDataSet *aDataSet = myPassFilter[0]->GetInput()){
189     time = aDataSet->GetMTime();
190     mTime = ( time > mTime ? time : mTime );
191   }
192   return mTime;
193 }
194
195
196 void SALOME_Actor::SetRepresentation(int theMode) { 
197   switch(myRepresentation){
198   case 0 : 
199   case 2 : 
200     myProperty->DeepCopy(GetProperty());
201   }    
202   switch(theMode){
203   case 0 : 
204   case 2 : 
205     GetProperty()->DeepCopy(myProperty);
206     break;
207   default:
208     break;
209     GetProperty()->SetAmbient(1.0);
210     GetProperty()->SetDiffuse(0.0);
211     GetProperty()->SetSpecular(0.0);
212   }
213   switch(theMode){
214   case 3 : 
215     myGeomFilter->SetInside(true);
216     GetProperty()->SetRepresentation(1);
217     break;
218   case 0 : 
219     GetProperty()->SetPointSize(SALOME_POINT_SIZE);  
220   default :
221     GetProperty()->SetRepresentation(theMode);
222     myGeomFilter->SetInside(false);
223   }
224   myRepresentation = theMode;
225 }
226
227 int SALOME_Actor::GetRepresentation(){ 
228   return myRepresentation;
229 }
230
231 //=================================================================================
232 // function : GetObjDimension
233 // purpose  : Return object dimension.
234 //            Virtual method shoulb be redifined by derived classes
235 //=================================================================================
236 int SALOME_Actor::GetObjDimension( const int theObjId )
237 {
238   if ( theObjId < 0 )
239     return 0;
240     
241   std::vector<int> aVtkList = GetVtkId( theObjId );
242   int nbVtk = aVtkList.size();
243   
244   if ( nbVtk == 0 )
245     return 0;
246   else if ( nbVtk > 1 )
247     return 3;
248   else //  nbVtk = 1
249   {
250     if ( vtkDataSet* aGrid = GetMapper()->GetInput() )
251     {
252       if ( vtkCell* aCell = aGrid->GetCell( aVtkList.front() ) )
253         return aCell->GetCellDimension();
254     }
255   }
256   return 0;
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275