Salome HOME
PR : merge branch V1_2c dans branche principale pour V1_3_0_b1
[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_TransformFilter.h"
37 #include "SALOME_PassThroughFilter.h"
38 #include "SALOME_GeometryFilter.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 #include <vtkTransformPolyDataFilter.h>
48
49 using namespace std;
50
51 int SALOME_POINT_SIZE = 3;
52
53 void SALOME_Actor::Render(vtkRenderer *ren, vtkMapper *Mapper )
54 {
55   if (this->Mapper == NULL) {
56     MESSAGE ("No mapper for actor.")
57     return;
58   }
59   
60   vtkMapper *bestMapper;
61   bestMapper = this->Mapper;
62
63   /* render the property */
64   if (!this->Property) {
65     // force creation of a property
66     this->GetProperty();
67   }
68
69   this->Property->Render(this, ren);
70   if (this->BackfaceProperty) {
71     this->BackfaceProperty->BackfaceRender(this, ren);
72     this->Device->SetBackfaceProperty(this->BackfaceProperty);
73   }
74   this->Device->SetProperty(this->Property);
75   
76   
77   /* render the texture */
78   if (this->Texture) {
79     this->Texture->Render(ren);
80   }
81   
82   
83   // Store information on time it takes to render.
84   // We might want to estimate time from the number of polygons in mapper.
85   this->Device->Render(ren,bestMapper);
86   this->EstimatedRenderTime = bestMapper->GetTimeToDraw();
87 }
88
89 int SALOME_Actor::RenderOpaqueGeometry(vtkViewport *vp)
90 {
91   int renderedSomething = 0; 
92   vtkRenderer      *ren = (vtkRenderer *)vp;
93
94   if ( ! this->Mapper ) {
95     return 0;
96   }
97
98   // make sure we have a property
99   if (!this->Property) {
100     // force creation of a property
101     this->GetProperty();
102   }
103
104   // is this actor opaque ?
105   if (this->GetIsOpaque()) {
106     this->Property->Render(this, ren);
107     
108     // render the backface property
109     if (this->BackfaceProperty) {
110       this->BackfaceProperty->BackfaceRender(this, ren);
111     }
112     
113     // render the texture 
114     if (this->Texture) {
115       this->Texture->Render(ren);
116     }
117     this->Render(ren,this->Mapper);
118     
119     renderedSomething = 1;
120   }
121   
122   return renderedSomething; 
123 }
124
125 void SALOME_Actor::ReleaseGraphicsResources(vtkWindow *renWin)
126 {
127   vtkActor::ReleaseGraphicsResources(renWin);
128   this->Mapper->ReleaseGraphicsResources(renWin);
129 }
130
131
132 void SALOME_Actor::AddToRender(vtkRenderer* theRenderer){
133   theRenderer->AddActor(this);
134 }
135 void SALOME_Actor::RemoveFromRender(vtkRenderer* theRenderer){
136   theRenderer->RemoveActor(this);
137 }
138
139
140 vtkPolyData* SALOME_Actor::GetPolyDataInput(){
141   return myPassFilter[3]->GetPolyDataOutput();
142 }
143
144 void SALOME_Actor::SetMapper(vtkMapper* theMapper){
145   if(theMapper){
146     myPassFilter[0]->SetInput(theMapper->GetInput());
147     myPassFilter[1]->SetInput(myPassFilter[0]->GetPolyDataOutput());
148     myTransformFilter->SetInput(myPassFilter[1]->GetPolyDataOutput());
149     myPassFilter[2]->SetInput(myTransformFilter->GetOutput());
150     myPassFilter[3]->SetInput(myPassFilter[2]->GetPolyDataOutput());
151     if(vtkDataSetMapper* aMapper = dynamic_cast<vtkDataSetMapper*>(theMapper))
152       aMapper->SetInput(myPassFilter[3]->GetPolyDataOutput());
153     else if(vtkPolyDataMapper* aMapper = dynamic_cast<vtkPolyDataMapper*>(theMapper))
154       aMapper->SetInput(myPassFilter[3]->GetPolyDataOutput());
155   }
156   vtkLODActor::SetMapper(theMapper);
157 }
158
159 void SALOME_Actor::SetTransform(SALOME_Transform* theTransform){
160   myTransformFilter->SetTransform(theTransform);
161   myTransformFilter->Modified();
162 }
163
164
165 void SALOME_Actor::SetRepresentation(int theMode) { 
166   myRepresentation = theMode;
167   switch(theMode){
168   case 3 : 
169     myPassFilter[0]->SetInside(true);
170     GetProperty()->SetRepresentation(1);
171     break;
172   case 0 : 
173     GetProperty()->SetPointSize(SALOME_POINT_SIZE);  
174   default :
175     GetProperty()->SetRepresentation(myRepresentation);
176     myPassFilter[0]->SetInside(false);
177   }
178 }
179 int SALOME_Actor::GetRepresentation(){ 
180   return myRepresentation;
181 }
182
183
184 SALOME_Actor::SALOME_Actor(){
185   PreviewProperty = NULL;
186   myRepresentation = 2;
187   myTransformFilter = SALOME_TransformFilter::New();
188   myPassFilter.push_back(SALOME_PassThroughFilter::New());
189   myPassFilter.push_back(SALOME_PassThroughFilter::New());
190   myPassFilter.push_back(SALOME_PassThroughFilter::New());
191   myPassFilter.push_back(SALOME_PassThroughFilter::New());
192 }
193
194 SALOME_Actor::~SALOME_Actor(){
195   myTransformFilter->Delete();
196   SetPreviewProperty(NULL);
197   for(int i = 0, iEnd = myPassFilter.size(); i < iEnd; i++)
198     if(myPassFilter[i] != NULL) 
199       myPassFilter[i]->Delete();
200 }