Salome HOME
DCQ : Merge with Ecole Ete a6.
[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_PassThroughFilter.h"
32 #include "utilities.h"
33  
34 // VTK Includes
35 #include <vtkProperty.h>
36 #include <vtkDataSetMapper.h>
37 #include <vtkShrinkPolyData.h>
38 #include <vtkGeometryFilter.h>
39 #include <vtkObjectFactory.h>
40
41 using namespace std;
42
43 static int MYVTKDEBUG = 0;
44
45 #ifdef _DEBUG_
46 static int MYDEBUG = 1;
47 static int MYDEBUGWITHFILES = 0;
48 #else
49 static int MYDEBUG = 0;
50 static int MYDEBUGWITHFILES = 0;
51 #endif
52
53 //=======================================================================
54
55 vtkStandardNewMacro(VISU_Actor);
56
57 VISU_Actor::VISU_Actor(){
58   SetDebug(MYVTKDEBUG);
59   myParent = this;
60   myPipeLine = NULL;
61   myPrs3d = NULL;
62
63   myStoreMapping = true;
64
65   myIsShrunk = false;
66   myIsShrinkable = false;
67   myShrinkFilter = vtkShrinkPolyData::New();
68
69   myIO = NULL;
70   myName = "";
71
72   ishighlighted = false;
73   IsHighlighting = true;
74   HighlightProperty = vtkProperty::New();
75   HighlightProperty->SetAmbient(0.5);            
76   HighlightProperty->SetDiffuse(0.3);            
77   HighlightProperty->SetSpecular(0.2);           
78   HighlightProperty->SetRepresentationToSurface();
79   HighlightProperty->SetAmbientColor(1, 1, 1);
80   HighlightProperty->SetDiffuseColor(1, 1, 1);
81   HighlightProperty->SetSpecularColor(0.5, 0.5, 0.5);
82
83   vtkProperty* aProperty = this->GetProperty();
84   aProperty->SetAmbient(0.5); 
85   aProperty->SetDiffuse(0.2);
86   aProperty->SetSpecular(0.2);
87   aProperty->SetAmbientColor(1, 1, 1);
88   aProperty->SetDiffuseColor(1, 1, 1);
89   aProperty->SetSpecularColor(0.5, 0.5, 0.5);
90   this->SetProperty(aProperty);
91 }
92
93 VISU_Actor::~VISU_Actor(){
94   SALOME_Actor::SetProperty(NULL);
95   HighlightProperty->Delete();
96   if(myPipeLine) myPipeLine->UnRegister(this);
97
98   myShrinkFilter->UnRegisterAllOutputs();
99   myShrinkFilter->Delete();
100 }
101
102 void VISU_Actor::setIO(const Handle(SALOME_InteractiveObject)& theIO){
103   SALOME_Actor::setIO(theIO); 
104   myName = theIO->getName(); 
105 }
106
107 void VISU_Actor::SetPrs3d(VISU::Prs3d_i* thePrs3d){ 
108   myPrs3d = thePrs3d;
109 }
110
111 void VISU_Actor::SetPipeLine(VISU_PipeLine* thePipeLine) {
112   if (myPipeLine != thePipeLine){
113     if (myPipeLine != NULL) myPipeLine->UnRegister(this);
114     myPipeLine = thePipeLine;
115     if (myPipeLine != NULL) myPipeLine->Register(this);
116     this->Modified();
117     vtkMapper *aMapper = myPipeLine->GetMapper();
118     vtkDataSet *aDataSet = aMapper->GetInput();
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     //Bug SAL4221:  Mesh with less than 10 cells : shrink mode disable
128     //SetShrinkable(aDataSet->GetNumberOfCells() > 10);
129     SetShrinkable(true);
130     //Now, we use vtkShrinkPolyData (not vtkShrinkFilter), 
131     //and the class there is no such limitation.
132     
133     vtkDataSetMapper* aNewMapper = vtkDataSetMapper::New();
134     aNewMapper->SetInput(aDataSet);
135     SetMapper(aNewMapper);
136     aNewMapper->Delete();
137   }
138 }
139
140 void VISU_Actor::SetParent(VISU_Actor* theParent){ 
141   myParent = theParent;
142 }
143
144 void VISU_Actor::SetRepresentation(int theMode) { 
145   SALOME_Actor::SetRepresentation(theMode);
146 }
147
148 void VISU_Actor::SetOpacity(float theValue){
149   GetProperty()->SetOpacity(theValue);
150 }
151
152 float VISU_Actor::GetOpacity(){ 
153   return GetProperty()->GetOpacity();
154 }
155
156 void VISU_Actor::SetShrinkable(bool theIsShrinkable){
157   myIsShrinkable = theIsShrinkable;
158 }
159
160 void VISU_Actor::SetShrink(float theFactor){
161   if(!myIsShrinkable) return;
162   if(vtkPolyData* aDataSet = myPassFilter.at(1)->GetPolyDataOutput()){
163     myShrinkFilter->SetShrinkFactor(theFactor);
164     myShrinkFilter->SetInput(aDataSet);
165     myPassFilter.at(2)->SetInput(myShrinkFilter->GetOutput());
166     myIsShrunk = true;
167   }
168 }
169
170 void VISU_Actor::UnShrink(){
171   if(!myIsShrunk) return;
172   if(vtkPolyData* aDataSet = myPassFilter.at(1)->GetPolyDataOutput()){
173     myPassFilter.at(2)->SetInput(aDataSet);
174     myPassFilter.at(2)->Modified();
175     myIsShrunk = false;
176   }
177 }
178
179 void VISU_Actor::SetVisibility(int theMode){
180   SALOME_Actor::SetVisibility(theMode);
181 }
182
183 int VISU_Actor::GetVisibility(){
184   return SALOME_Actor::GetVisibility();
185 }
186
187 void VISU_Actor::SetProperty(vtkProperty* theProperty){
188   SALOME_Actor::SetProperty(theProperty);
189 }
190
191 vtkProperty* VISU_Actor::GetProperty(){
192   return SALOME_Actor::GetProperty();
193 }
194
195 void VISU_Actor::SetLineWidth(float theLineWidth){
196   GetProperty()->SetLineWidth(theLineWidth);
197 }
198
199 float VISU_Actor::GetLineWidth(){
200   return GetProperty()->GetLineWidth();
201 }
202
203 void VISU_Actor::ShallowCopy(vtkProp *prop){
204   VISU_Actor *anActor = VISU_Actor::SafeDownCast(prop);
205   if(anActor != NULL){
206     setName(anActor->getName());
207     if(anActor->hasIO()) setIO(anActor->getIO());
208   }
209   SALOME_Actor::ShallowCopy(prop);
210 }
211
212 void VISU_Actor::highlight(Standard_Boolean highlight) {
213   if (this->IsHighlighting) {
214     if(highlight && !ishighlighted) {
215       ishighlighted=true;
216       this->SetProperty(HighlightProperty);
217     }else if (!highlight)
218       if(ishighlighted) ishighlighted=false;
219   }
220 }
221
222 int VISU_Actor::GetObjId(int theVtkID){
223   if (GetRepresentation() == 0) return -1; // points    
224   return myGeomFilter->GetObjId(theVtkID);
225 }
226
227 VISU_Actor::TVectorId VISU_Actor::GetVtkId(int theObjID){
228   return myGeomFilter->GetVtkId(theObjID);
229 }