Salome HOME
Update Help for VISU module.
[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 "VTKViewer_ShrinkFilter.h"
32 #include "VTKViewer_GeometryFilter.h"
33 #include "VTKViewer_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 #else
60 static int MYDEBUG = 0;
61 #endif
62
63 //=======================================================================
64
65 vtkStandardNewMacro(VISU_Actor);
66
67 VISU_Actor::VISU_Actor(){
68   SetDebug(MYVTKDEBUG);
69   myParent = this;
70   myPipeLine = NULL;
71   myPrs3d = NULL;
72
73   myStoreMapping = true;
74
75   myIsShrunk = false;
76   myIsShrinkable = false;
77   myShrinkFilter = VTKViewer_ShrinkFilter::New();
78   myShrinkFilter->SetStoreMapping(true);
79   SetShrinkFactor();
80   
81   myMapper = vtkDataSetMapper::New();
82
83   myIO = NULL;
84   myName = "";
85 }
86
87 VISU_Actor::~VISU_Actor(){
88   SALOME_Actor::SetProperty(NULL);
89
90   myMapper->RemoveAllInputs();
91   myMapper->Delete();
92
93   if(myPipeLine) 
94     myPipeLine->UnRegister(this);
95
96   myShrinkFilter->UnRegisterAllOutputs();
97   myShrinkFilter->Delete();
98 }
99
100 void VISU_Actor::setIO(const Handle(SALOME_InteractiveObject)& theIO){
101   SALOME_Actor::setIO(theIO); 
102   myName = theIO->getName(); 
103 }
104
105 void VISU_Actor::SetPrs3d(VISU::Prs3d_i* thePrs3d){ 
106   myPrs3d = thePrs3d;
107 }
108
109 void VISU_Actor::SetPipeLine(VISU_PipeLine* thePipeLine) {
110   if (myPipeLine != thePipeLine){
111     if (myPipeLine != NULL) myPipeLine->UnRegister(this);
112     myPipeLine = thePipeLine;
113     if (myPipeLine != NULL) myPipeLine->Register(this);
114     this->Modified();
115     vtkMapper *aMapper = myPipeLine->GetMapper();
116     vtkDataSet *aDataSet = aMapper->GetInput();
117
118     //This code has been moved into VISU::Prs3d_i::UpdateActor() for bug 9808
119     //if(!aDataSet)
120     //  throw std::runtime_error("VISU_Actor::SetPipeLine >> There is no input data !!!");
121     //aDataSet->Update();
122     //static float eps = VTK_LARGE_FLOAT * 0.1 ;
123     //if(aDataSet->GetLength() > eps)
124     //  throw std::runtime_error("VISU_Actor::SetPipeLine >> Diagonal of the actor is too large !!!");
125     //if(!aDataSet->GetNumberOfCells())
126     //  throw std::runtime_error("VISU_Actor::SetPipeLine >> There is no visible elements");
127
128     //Bug SAL4221:  Mesh with less than 10 cells : shrink mode disable
129     //SetShrinkable(aDataSet->GetNumberOfCells() > 10);
130     SetShrinkable(thePipeLine->IsShrinkable());
131     //Now, we use vtkShrinkPolyData (not vtkShrinkFilter), 
132     //and the class there is no such limitation.
133
134     myMapper->SetInput(aDataSet);
135     SetMapper(myMapper);
136   }
137 }
138
139 void VISU_Actor::SetParent(VISU_Actor* theParent){ 
140   myParent = theParent;
141 }
142
143 void VISU_Actor::SetRepresentation(int theMode) { 
144   SALOME_Actor::SetRepresentation(theMode);
145   if(myRepresentation == VTK_POINTS)
146     UnShrink();
147 }
148
149 void VISU_Actor::SetOpacity(float theValue){
150   GetProperty()->SetOpacity(theValue);
151 }
152
153 float VISU_Actor::GetOpacity(){ 
154   return GetProperty()->GetOpacity();
155 }
156
157
158 void VISU_Actor::SetShrink(){
159   if(!myIsShrinkable) return;
160   if(vtkDataSet* aDataSet = myPassFilter[0]->GetOutput()){
161     myShrinkFilter->SetInput(aDataSet);
162     myPassFilter[1]->SetInput(myShrinkFilter->GetOutput());
163     myIsShrunk = true;
164   }
165 }
166
167 void VISU_Actor::UnShrink(){
168   if(!myIsShrunk) return;
169   if(vtkDataSet* aDataSet = myPassFilter[0]->GetOutput()){
170     myPassFilter[1]->SetInput(aDataSet);
171     myPassFilter[1]->Modified();
172     myIsShrunk = false;
173     Modified();
174   }
175 }
176
177 void VISU_Actor::SetShrinkable(bool theIsShrinkable){
178   myIsShrinkable = theIsShrinkable;
179 }
180
181 void VISU_Actor::SetShrinkFactor(float theValue){
182   myShrinkFilter->SetShrinkFactor(theValue);
183   Modified();
184 }
185
186 float VISU_Actor::GetShrinkFactor(){
187   return myShrinkFilter->GetShrinkFactor();
188 }
189
190
191 //----------------------------------------------------------------------------
192 void VISU_Actor::SetVisibility(int theMode){
193   SALOME_Actor::SetVisibility(theMode);
194 }
195
196 int VISU_Actor::GetVisibility(){
197   return SALOME_Actor::GetVisibility();
198 }
199
200 void VISU_Actor::SetLineWidth(float theLineWidth){
201   GetProperty()->SetLineWidth(theLineWidth);
202 }
203
204 float VISU_Actor::GetLineWidth(){
205   return GetProperty()->GetLineWidth();
206 }
207
208 //----------------------------------------------------------------------------
209 void VISU_Actor::ShallowCopy(vtkProp *prop){
210   VISU_Actor *anActor = VISU_Actor::SafeDownCast(prop);
211   if(anActor != NULL){
212     setName(anActor->getName());
213     if(anActor->hasIO()) setIO(anActor->getIO());
214   }
215   SALOME_Actor::ShallowCopy(prop);
216 }
217
218 //----------------------------------------------------------------------------
219 int VISU_Actor::GetNodeObjId(int theVtkID){
220   if ( myIsShrunk ) 
221     return myShrinkFilter->GetNodeObjId(theVtkID);
222   return theVtkID;
223 }
224
225 int VISU_Actor::GetElemObjId(int theVtkID){
226   return myGeomFilter->GetElemObjId(theVtkID);
227 }