]> SALOME platform Git repositories - modules/visu.git/blob - src/OBJECT/VISU_Actor.cxx
Salome HOME
Merge with OCC-V2_1_0_deb
[modules/visu.git] / src / OBJECT / VISU_Actor.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
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   : VISU_Actor.cxx
25 //  Author : Laurent CORNABE with help of Nicolas REJNERI
26 //  Module : VISU
27 //  $Header$
28
29 #include "VISU_Actor.h"
30 #include "VISU_PipeLine.hxx"
31 #include "SALOME_ShrinkFilter.h"
32 #include "SALOME_GeometryFilter.h"
33 #include "SALOME_PassThroughFilter.h"
34  
35 #include <stdexcept>
36
37 // VTK Includes
38 #include <vtkProperty.h>
39
40 #include <vtkDataSet.h>
41 #include <vtkPolyData.h>
42 #include <vtkUnstructuredGrid.h>
43
44 #include <vtkShrinkFilter.h>
45 #include <vtkShrinkPolyData.h>
46
47 #include <vtkDataSetMapper.h>
48 #include <vtkGeometryFilter.h>
49 #include <vtkObjectFactory.h>
50
51 #include "utilities.h"
52
53 using namespace std;
54
55 static int MYVTKDEBUG = 0;
56
57 #ifdef _DEBUG_
58 static int MYDEBUG = 1;
59 static int MYDEBUGWITHFILES = 0;
60 #else
61 static int MYDEBUG = 0;
62 static int MYDEBUGWITHFILES = 0;
63 #endif
64
65 //=======================================================================
66
67 vtkStandardNewMacro(VISU_Actor);
68
69 VISU_Actor::VISU_Actor(){
70   SetDebug(MYVTKDEBUG);
71   myParent = this;
72   myPipeLine = NULL;
73   myPrs3d = NULL;
74
75   myStoreMapping = true;
76
77   myIsShrunk = false;
78   myIsShrinkable = false;
79   myShrinkFilter = SALOME_ShrinkFilter::New();
80   myShrinkFilter->SetStoreMapping(true);
81   SetShrinkFactor();
82   
83   myMapper = vtkDataSetMapper::New();
84
85   myIO = NULL;
86   myName = "";
87
88   vtkProperty* aProperty = GetProperty();
89   aProperty->SetAmbient(0.5); 
90   aProperty->SetDiffuse(0.2);
91   aProperty->SetSpecular(0.2);
92   aProperty->SetAmbientColor(1, 1, 1);
93   aProperty->SetDiffuseColor(1, 1, 1);
94   aProperty->SetSpecularColor(0.5, 0.5, 0.5);
95   this->SetProperty(aProperty);
96 }
97
98 VISU_Actor::~VISU_Actor(){
99   SALOME_Actor::SetProperty(NULL);
100
101   myMapper->RemoveAllInputs();
102   myMapper->Delete();
103
104   if(myPipeLine) 
105     myPipeLine->UnRegister(this);
106
107   myShrinkFilter->UnRegisterAllOutputs();
108   myShrinkFilter->Delete();
109 }
110
111 void VISU_Actor::setIO(const Handle(SALOME_InteractiveObject)& theIO){
112   SALOME_Actor::setIO(theIO); 
113   myName = theIO->getName(); 
114 }
115
116 void VISU_Actor::SetPrs3d(VISU::Prs3d_i* thePrs3d){ 
117   myPrs3d = thePrs3d;
118 }
119
120 void VISU_Actor::SetPipeLine(VISU_PipeLine* thePipeLine) {
121   if (myPipeLine != thePipeLine){
122     if (myPipeLine != NULL) myPipeLine->UnRegister(this);
123     myPipeLine = thePipeLine;
124     if (myPipeLine != NULL) myPipeLine->Register(this);
125     this->Modified();
126     vtkMapper *aMapper = myPipeLine->GetMapper();
127     vtkDataSet *aDataSet = aMapper->GetInput();
128     if(!aDataSet)
129       throw std::runtime_error("VISU_Actor::SetPipeLine >> There is no input data !!!");
130     aDataSet->Update();
131     static float eps = VTK_LARGE_FLOAT * 0.1 ;
132     if(aDataSet->GetLength() > eps)
133       throw std::runtime_error("VISU_Actor::SetPipeLine >> Diagonal of the actor is too large !!!");
134     if(!aDataSet->GetNumberOfCells())
135       throw std::runtime_error("VISU_Actor::SetPipeLine >> There is no visible elements");
136     //Bug SAL4221:  Mesh with less than 10 cells : shrink mode disable
137     //SetShrinkable(aDataSet->GetNumberOfCells() > 10);
138     SetShrinkable(true);
139     //Now, we use vtkShrinkPolyData (not vtkShrinkFilter), 
140     //and the class there is no such limitation.
141     
142     myMapper->SetInput(aDataSet);
143     SetMapper(myMapper);
144   }
145 }
146
147 void VISU_Actor::SetParent(VISU_Actor* theParent){ 
148   myParent = theParent;
149 }
150
151 void VISU_Actor::SetRepresentation(int theMode) { 
152   SALOME_Actor::SetRepresentation(theMode);
153   if(myRepresentation == VTK_POINTS)
154     UnShrink();
155 }
156
157 void VISU_Actor::SetOpacity(float theValue){
158   GetProperty()->SetOpacity(theValue);
159 }
160
161 float VISU_Actor::GetOpacity(){ 
162   return GetProperty()->GetOpacity();
163 }
164
165
166 void VISU_Actor::SetShrink(){
167   if(!myIsShrinkable) return;
168   if(vtkDataSet* aDataSet = myPassFilter[0]->GetOutput()){
169     myShrinkFilter->SetInput(aDataSet);
170     myPassFilter[1]->SetInput(myShrinkFilter->GetOutput());
171     myIsShrunk = true;
172   }
173 }
174
175 void VISU_Actor::UnShrink(){
176   if(!myIsShrunk) return;
177   if(vtkDataSet* aDataSet = myPassFilter[0]->GetOutput()){
178     myPassFilter[1]->SetInput(aDataSet);
179     myPassFilter[1]->Modified();
180     myIsShrunk = false;
181     Modified();
182   }
183 }
184
185 void VISU_Actor::SetShrinkable(bool theIsShrinkable){
186   myIsShrinkable = theIsShrinkable;
187 }
188
189 void VISU_Actor::SetShrinkFactor(float theValue){
190   myShrinkFilter->SetShrinkFactor(theValue);
191   Modified();
192 }
193
194 float VISU_Actor::GetShrinkFactor(){
195   return myShrinkFilter->GetShrinkFactor();
196 }
197
198
199 //----------------------------------------------------------------------------
200 void VISU_Actor::SetVisibility(int theMode){
201   SALOME_Actor::SetVisibility(theMode);
202 }
203
204 int VISU_Actor::GetVisibility(){
205   return SALOME_Actor::GetVisibility();
206 }
207
208 void VISU_Actor::SetProperty(vtkProperty* theProperty){
209   SALOME_Actor::SetProperty(theProperty);
210 }
211
212 vtkProperty* VISU_Actor::GetProperty(){
213   return SALOME_Actor::GetProperty();
214 }
215
216 void VISU_Actor::SetLineWidth(float theLineWidth){
217   GetProperty()->SetLineWidth(theLineWidth);
218 }
219
220 float VISU_Actor::GetLineWidth(){
221   return GetProperty()->GetLineWidth();
222 }
223
224 //----------------------------------------------------------------------------
225 void VISU_Actor::ShallowCopy(vtkProp *prop){
226   VISU_Actor *anActor = VISU_Actor::SafeDownCast(prop);
227   if(anActor != NULL){
228     setName(anActor->getName());
229     if(anActor->hasIO()) setIO(anActor->getIO());
230   }
231   SALOME_Actor::ShallowCopy(prop);
232 }
233
234 //----------------------------------------------------------------------------
235 int VISU_Actor::GetNodeObjId(int theVtkID){
236   if ( myIsShrunk ) 
237     return myShrinkFilter->GetNodeObjId(theVtkID);
238   return theVtkID;
239 }
240
241 int VISU_Actor::GetElemObjId(int theVtkID){
242   return myGeomFilter->GetElemObjId(theVtkID);
243 }