Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/visu.git] / src / VISU_I / VISU_ScalarMapOnDeformedShape_i.cc
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 //  File   : VISU_ScalarMapOnDeformedShape_i.cc
24 //  Author : Eugeny Nikolaev
25 //  Module : VISU
26
27 #include "VISU_ScalarMapOnDeformedShapePL.hxx"
28 #include "VISU_Result_i.hh"
29 #include "VISU_ScalarMap_i.hh"
30 #include "VISU_ScalarMapOnDeformedShape_i.hh"
31 #include "VISU_Convertor.hxx"
32 #include "VISU_ScalarMapAct.h"
33
34 #include <vtkUnstructuredGrid.h>
35 #include <vtkProperty.h>
36 #include <vtkMapper.h>
37
38 using namespace VISU;
39 using namespace std;
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 0;
43 #else
44 static int MYDEBUG = 0;
45 #endif
46
47 static int INCMEMORY = 4+12;
48
49 int VISU::ScalarMapOnDeformedShape_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
50                                                  const char* theFieldName, int theIteration, int isMemoryCheck)
51 {
52   bool aResult = false;
53   try{
54     aResult = VISU::ScalarMap_i::IsPossible(theResult,theMeshName,theEntity,theFieldName,theIteration,false);
55     if(isMemoryCheck && aResult){
56       const VISU::PField aField = theResult->GetInput()->GetField(theMeshName,(VISU::TEntity)theEntity,theFieldName);
57       if(aField->myNbComp <= 1)
58         return false;
59       float aSize = INCMEMORY*
60         theResult->GetInput()->GetTimeStampSize(theMeshName,(VISU::TEntity)theEntity,theFieldName,theIteration);
61       aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
62       MESSAGE("ScalarMapOnDeformedShape_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<aResult);
63     }
64   }catch(std::exception& exc){
65     INFOS("Follow exception was occured :\n"<<exc.what());
66   }catch(...){
67     INFOS("Unknown exception was occured!");
68   }
69   return aResult;
70 }
71
72 int VISU::ScalarMapOnDeformedShape_i::myNbPresent = 0;
73 QString VISU::ScalarMapOnDeformedShape_i::GenerateName() { return VISU::GenerateName("ScalarDef.Shape",myNbPresent++);}
74
75 const string VISU::ScalarMapOnDeformedShape_i::myComment = "SCALARMAPONDEFORMEDSHAPE";
76 const char* VISU::ScalarMapOnDeformedShape_i::GetComment() const { return myComment.c_str();}
77
78 VISU::ScalarMapOnDeformedShape_i::
79 ScalarMapOnDeformedShape_i(Result_i* theResult, 
80           bool theAddToStudy) :
81   PrsObject_i(theResult->GetStudyDocument()), 
82   Prs3d_i(theResult,theAddToStudy),
83   ColoredPrs3d_i(theResult,theAddToStudy),
84   ScalarMap_i(theResult,theAddToStudy),
85   myScalarMapOnDeformedShapePL(NULL),
86   myScalarIteration(1)
87 {
88 }
89
90 VISU::ScalarMapOnDeformedShape_i::
91 ScalarMapOnDeformedShape_i(Result_i* theResult, 
92           SALOMEDS::SObject_ptr theSObject) :
93   PrsObject_i(theResult->GetStudyDocument()), 
94   Prs3d_i(theResult,theSObject),
95   ColoredPrs3d_i(theResult,theSObject),
96   ScalarMap_i(theResult,theSObject),
97   myScalarMapOnDeformedShapePL(NULL),
98   myScalarIteration(1)
99 {
100 }
101
102 VISU::Storable* VISU::ScalarMapOnDeformedShape_i::Create(const char* theMeshName, VISU::Entity theEntity, 
103                                                          const char* theFieldName, int theIteration)
104 {
105   if(MYDEBUG) cout << "VISU::ScalarMapOnDeformedShape_i::Create:"<<__LINE__<<endl;
106   myIsColored = true;
107   myColor.R = myColor.G = myColor.B = 0.5;
108   VISU::Storable* aRes = TSuperClass::Create(theMeshName,theEntity,theFieldName,theIteration);
109   SetScalarFieldName(theFieldName);
110   SetScalarIteration(theIteration);
111   SetScalarMeshName(theMeshName);
112   int anEntity = theEntity;
113   SetScalarEntity(VISU::TEntity(anEntity));
114   SetScalarField(myScalarMeshName.c_str(),myScalarFieldName.c_str(),myScalarIteration,myScalarEntity);
115   return aRes;
116 }
117
118 VISU::Storable* VISU::ScalarMapOnDeformedShape_i::Restore(const Storable::TRestoringMap& theMap)
119 {
120   if(MYDEBUG) cout << "VISU::ScalarMapOnDeformedShape_i::Restore:"<<__LINE__<<endl;
121   TSuperClass::Restore(theMap);
122   
123   SetScalarFieldName(VISU::Storable::FindValue(theMap,"myScalarFieldName"));
124   SetScalarMeshName(VISU::Storable::FindValue(theMap,"myScalarMeshName"));
125   SetScalarIteration(VISU::Storable::FindValue(theMap,"myScalarIteration").toInt());
126   SetScalarEntity(VISU::TEntity(VISU::Storable::FindValue(theMap,"myScalarEntity").toInt()));
127   SetScalarField(GetScalarMeshName().c_str(),
128                  GetScalarFieldName().c_str(),
129                  GetScalarIteration(),
130                  GetScalarEntity());
131   
132   SetScale(VISU::Storable::FindValue(theMap,"myFactor").toDouble());
133   myIsColored = VISU::Storable::FindValue(theMap,"myIsColored").toInt();
134   myColor.R = VISU::Storable::FindValue(theMap,"myColor.R").toDouble();
135   myColor.G = VISU::Storable::FindValue(theMap,"myColor.G").toDouble();
136   myColor.B = VISU::Storable::FindValue(theMap,"myColor.B").toDouble();
137   
138   return this;
139 }
140
141 void VISU::ScalarMapOnDeformedShape_i::ToStream(std::ostringstream& theStr)
142 {
143   if(MYDEBUG) cout << "VISU::ScalarMapOnDeformedShape_i::ToStream:"<<__LINE__<<endl;
144   TSuperClass::ToStream(theStr);
145   
146   Storable::DataToStream( theStr, "myScalarFieldName", myScalarFieldName.c_str());
147   Storable::DataToStream( theStr, "myScalarMeshName",  myScalarMeshName.c_str());
148   Storable::DataToStream( theStr, "myScalarIteration", myScalarIteration);
149   Storable::DataToStream( theStr, "myScalarEntity",    int(myScalarEntity));
150
151   Storable::DataToStream( theStr, "myFactor", GetScale() );
152   Storable::DataToStream( theStr, "myIsColored", myIsColored );
153   Storable::DataToStream( theStr, "myColor.R", myColor.R );
154   Storable::DataToStream( theStr, "myColor.G", myColor.G );
155   Storable::DataToStream( theStr, "myColor.B", myColor.B );
156
157 }
158
159 VISU::ScalarMapOnDeformedShape_i::~ScalarMapOnDeformedShape_i(){
160   if(MYDEBUG) MESSAGE("ScalarMapOnDeformedShape_i::~ScalarMapOnDeformedShape_i()");
161 }
162
163 void
164 VISU::ScalarMapOnDeformedShape_i
165 ::SameAs(const Prs3d_i* theOrigin)
166 {
167   if(MYDEBUG) cout << "VISU::ScalarMapOnDeformedShape_i::SameAs:"<<__LINE__<<endl;
168   int theScalarIter = this->GetScalarIteration();
169   string theScalarMeshName = this->GetScalarMeshName();
170   string theScalarFieldName = this->GetScalarFieldName();
171   if(MYDEBUG) cout << "SameAs:"<<endl
172                    << "\t"<<theScalarIter<<":"<<theScalarMeshName.c_str()<<":"<<theScalarFieldName.c_str()<<endl;
173
174   TSuperClass::SameAs(theOrigin);
175
176   if(const ScalarMapOnDeformedShape_i* aPrs3d = dynamic_cast<const ScalarMapOnDeformedShape_i*>(theOrigin)){
177     ScalarMapOnDeformedShape_i* anOrigin = const_cast<ScalarMapOnDeformedShape_i*>(aPrs3d);
178
179     this->SetScalarField(anOrigin->GetScalarMeshName().c_str(),
180                          anOrigin->GetScalarFieldName().c_str(),
181                          theScalarIter,
182                          anOrigin->GetScalarEntity());
183     Update();
184   }
185 }
186
187 void VISU::ScalarMapOnDeformedShape_i::SetScale(CORBA::Double theScale) { 
188   myScalarMapOnDeformedShapePL->SetScale(theScale);
189 }
190
191 CORBA::Double VISU::ScalarMapOnDeformedShape_i::GetScale(){
192   return myScalarMapOnDeformedShapePL->GetScale();
193 }
194
195 void VISU::ScalarMapOnDeformedShape_i::SetSourceRange(CORBA::Double theMinRange,CORBA::Double theMaxRange){
196   float aRange[2];
197   aRange[0] = float(theMinRange);
198   aRange[1] = float(theMaxRange);
199   myScalarMapOnDeformedShapePL->SetScalarRange(aRange);
200 }
201
202 CORBA::Double VISU::ScalarMapOnDeformedShape_i::GetSourceRangeMin(){
203   float aRange[2];
204   myScalarMapOnDeformedShapePL->GetSourceRange(aRange);
205   return aRange[0];
206 }
207
208 CORBA::Double VISU::ScalarMapOnDeformedShape_i::GetSourceRangeMax(){
209   float aRange[2];
210   myScalarMapOnDeformedShapePL->GetSourceRange(aRange);
211   return aRange[1];
212 }
213
214 void VISU::ScalarMapOnDeformedShape_i::DoHook()
215 {
216   if(MYDEBUG) cout << "VISU::ScalarMapOnDeformedShape_i::DoHook:"<<__LINE__<<endl;
217   if(!myPipeLine)
218     myPipeLine = VISU_ScalarMapOnDeformedShapePL::New();
219   
220   myScalarMapOnDeformedShapePL = dynamic_cast<VISU_ScalarMapOnDeformedShapePL*>(myPipeLine);
221   myPipeLine->GetMapper()->SetScalarVisibility(IsColored());
222
223   TSuperClass::DoHook();
224 }
225
226 VISU_Actor* VISU::ScalarMapOnDeformedShape_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO) 
227 {
228   if(MYDEBUG) cout << "VISU::ScalarMapOnDeformedShape_i::CreateActor:"<<__LINE__<<endl;
229   VISU_Actor* anActor = TSuperClass::CreateActor(theIO);
230   anActor->SetRepresentation(2);
231   anActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
232   anActor->SetVTKMapping(false);
233   return anActor;
234 }
235
236
237 void VISU::ScalarMapOnDeformedShape_i::UpdateActor(VISU_Actor* theActor) {
238   if(MYDEBUG) cout << "VISU::ScalarMapOnDeformedShape_i::UpdateActor:"<<__LINE__<<endl;
239   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
240     if(IsColored()){
241       anActor->SetBarVisibility(true);
242       myPipeLine->GetMapper()->SetScalarVisibility(1);
243     }else{
244       anActor->SetBarVisibility(false);
245       myPipeLine->GetMapper()->SetScalarVisibility(0);
246       anActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
247     }
248     TSuperClass::UpdateActor(theActor);
249   }
250 }
251
252 void VISU::ScalarMapOnDeformedShape_i::SetScalarField(const char* theMeshName,
253                                                       const char* theFieldName,
254                                                       const int   theIteration,
255                                                       const VISU::TEntity  theEntity)
256 {
257   if(MYDEBUG) cout << "VISU::ScalarMapOnDeformedShape_i::SetScalarField:"<<__LINE__<<endl;
258   if(MYDEBUG) cout << "\ttheFieldName="<<theFieldName<<endl;
259   if(MYDEBUG) cout << "\ttheIteration="<<theIteration<<endl;
260   myScalarField = myResult->GetInput()->GetField(theMeshName,theEntity,theFieldName);
261   TVTKOutput* aOut = myResult->GetInput()->GetTimeStampOnMesh(theMeshName,
262                                                               theEntity,
263                                                               theFieldName,
264                                                               theIteration)->GetVTKOutput();
265   if (myScalarMapOnDeformedShapePL){
266     myScalarMapOnDeformedShapePL->SetScalars(aOut);
267   }
268   this->SetScalarMeshName(theMeshName);
269   this->SetScalarFieldName(theFieldName);
270   this->SetScalarIteration(theIteration);
271   this->SetScalarEntity(theEntity);
272 }
273
274 void VISU::ScalarMapOnDeformedShape_i::SetScalarField(const char* theMeshName,
275                                                       const char* theFieldName,
276                                                       const long  theIteration,
277                                                       const VISU::Entity  theEntity)
278 {
279   this->SetScalarField(theMeshName,theFieldName,(int)theIteration,VISU::TEntity(theEntity));
280 }
281
282 void VISU::ScalarMapOnDeformedShape_i::SetScalarMeshName(const char* theName){
283   myScalarMeshName = theName;
284 }
285 std::string VISU::ScalarMapOnDeformedShape_i::GetScalarMeshName(){
286   return myScalarMeshName;
287 }
288
289 void VISU::ScalarMapOnDeformedShape_i::SetScalarFieldName(const char* theName){
290   myScalarFieldName = theName;
291 }
292 std::string VISU::ScalarMapOnDeformedShape_i::GetScalarFieldName(){
293   return myScalarFieldName;
294 }
295
296 void VISU::ScalarMapOnDeformedShape_i::SetScalarIteration(const int theValue){
297   myScalarIteration = theValue;
298 }
299
300
301 int VISU::ScalarMapOnDeformedShape_i::GetScalarIteration(){
302   return myScalarIteration;
303 }
304
305 void VISU::ScalarMapOnDeformedShape_i::SetScalarEntity(const VISU::TEntity theValue){
306   myScalarEntity = theValue;
307 }
308
309 VISU::TEntity VISU::ScalarMapOnDeformedShape_i::GetScalarEntity(){
310   return myScalarEntity;
311 }
312
313 void VISU::ScalarMapOnDeformedShape_i::SetScalarEEntity(const VISU::Entity theValue){
314   myScalarEntity = VISU::TEntity(theValue);
315 }
316
317 VISU::Entity VISU::ScalarMapOnDeformedShape_i::GetScalarEEntity(){
318   return VISU::Entity(myScalarEntity);
319 }
320
321 const VISU::PField&
322 VISU::ScalarMapOnDeformedShape_i
323 ::GetField() const 
324
325   return myScalarField;
326 }