Salome HOME
Update version to 3.2.0a1
[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   myIsColored = true;
106   myColor.R = myColor.G = myColor.B = 0.5;
107   VISU::Storable* aRes = TSuperClass::Create(theMeshName,theEntity,theFieldName,theIteration);
108   SetScalarFieldName(theFieldName);
109   SetScalarIteration(theIteration);
110   SetScalarMeshName(theMeshName);
111   int anEntity = theEntity;
112   SetScalarEntity(VISU::TEntity(anEntity));
113   SetScalarField(myScalarMeshName.c_str(),myScalarFieldName.c_str(),myScalarIteration,myScalarEntity);
114   return aRes;
115 }
116
117 VISU::Storable* VISU::ScalarMapOnDeformedShape_i::Restore(const Storable::TRestoringMap& theMap)
118 {
119   TSuperClass::Restore(theMap);
120   
121   SetScalarFieldName(VISU::Storable::FindValue(theMap,"myScalarFieldName"));
122   SetScalarMeshName(VISU::Storable::FindValue(theMap,"myScalarMeshName"));
123   SetScalarIteration(VISU::Storable::FindValue(theMap,"myScalarIteration").toInt());
124   SetScalarEntity(VISU::TEntity(VISU::Storable::FindValue(theMap,"myScalarEntity").toInt()));
125   SetScalarField(GetScalarMeshName().c_str(),
126                  GetScalarFieldName().c_str(),
127                  GetScalarIteration(),
128                  GetScalarEntity());
129   
130   SetScale(VISU::Storable::FindValue(theMap,"myFactor").toDouble());
131   myIsColored = VISU::Storable::FindValue(theMap,"myIsColored").toInt();
132   myColor.R = VISU::Storable::FindValue(theMap,"myColor.R").toDouble();
133   myColor.G = VISU::Storable::FindValue(theMap,"myColor.G").toDouble();
134   myColor.B = VISU::Storable::FindValue(theMap,"myColor.B").toDouble();
135   
136   return this;
137 }
138
139 void VISU::ScalarMapOnDeformedShape_i::ToStream(std::ostringstream& theStr)
140 {
141   TSuperClass::ToStream(theStr);
142   
143   Storable::DataToStream( theStr, "myScalarFieldName", myScalarFieldName.c_str());
144   Storable::DataToStream( theStr, "myScalarMeshName",  myScalarMeshName.c_str());
145   Storable::DataToStream( theStr, "myScalarIteration", myScalarIteration);
146   Storable::DataToStream( theStr, "myScalarEntity",    int(myScalarEntity));
147
148   Storable::DataToStream( theStr, "myFactor", GetScale() );
149   Storable::DataToStream( theStr, "myIsColored", myIsColored );
150   Storable::DataToStream( theStr, "myColor.R", myColor.R );
151   Storable::DataToStream( theStr, "myColor.G", myColor.G );
152   Storable::DataToStream( theStr, "myColor.B", myColor.B );
153
154 }
155
156 VISU::ScalarMapOnDeformedShape_i::~ScalarMapOnDeformedShape_i(){
157   if(MYDEBUG) MESSAGE("ScalarMapOnDeformedShape_i::~ScalarMapOnDeformedShape_i()");
158 }
159
160 void
161 VISU::ScalarMapOnDeformedShape_i
162 ::SameAs(const Prs3d_i* theOrigin)
163 {
164   TSuperClass::SameAs(theOrigin);
165
166   if(const ScalarMapOnDeformedShape_i* aPrs3d = dynamic_cast<const ScalarMapOnDeformedShape_i*>(theOrigin)){
167     ScalarMapOnDeformedShape_i* anOrigin = const_cast<ScalarMapOnDeformedShape_i*>(aPrs3d);
168
169     // increment the scalar iteration for using in VISU_TimeAnimation class implementation.
170     this->SetScalarField(anOrigin->GetScalarMeshName().c_str(),
171                          anOrigin->GetScalarFieldName().c_str(),
172                          anOrigin->GetScalarIteration() + 1,
173                          anOrigin->GetScalarEntity());
174     Update();
175   }
176 }
177
178 void VISU::ScalarMapOnDeformedShape_i::SetScale(CORBA::Double theScale) { 
179   myScalarMapOnDeformedShapePL->SetScale(theScale);
180 }
181
182 CORBA::Double VISU::ScalarMapOnDeformedShape_i::GetScale(){
183   return myScalarMapOnDeformedShapePL->GetScale();
184 }
185
186 void VISU::ScalarMapOnDeformedShape_i::SetSourceRange(CORBA::Double theMinRange,CORBA::Double theMaxRange){
187   float aRange[2];
188   aRange[0] = float(theMinRange);
189   aRange[1] = float(theMaxRange);
190   myScalarMapOnDeformedShapePL->SetScalarRange(aRange);
191 }
192
193 CORBA::Double VISU::ScalarMapOnDeformedShape_i::GetSourceRangeMin(){
194   float aRange[2];
195   myScalarMapOnDeformedShapePL->GetSourceRange(aRange);
196   return aRange[0];
197 }
198
199 CORBA::Double VISU::ScalarMapOnDeformedShape_i::GetSourceRangeMax(){
200   float aRange[2];
201   myScalarMapOnDeformedShapePL->GetSourceRange(aRange);
202   return aRange[1];
203 }
204
205 void VISU::ScalarMapOnDeformedShape_i::DoHook()
206 {
207   if(!myPipeLine)
208     myPipeLine = VISU_ScalarMapOnDeformedShapePL::New();
209   
210   myScalarMapOnDeformedShapePL = dynamic_cast<VISU_ScalarMapOnDeformedShapePL*>(myPipeLine);
211   myPipeLine->GetMapper()->SetScalarVisibility(IsColored());
212
213   TSuperClass::DoHook();
214 }
215
216 VISU_Actor* VISU::ScalarMapOnDeformedShape_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO) 
217 {
218   VISU_Actor* anActor = TSuperClass::CreateActor(theIO);
219   anActor->SetRepresentation(2);
220   anActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
221   anActor->SetVTKMapping(false);
222   return anActor;
223 }
224
225
226 void VISU::ScalarMapOnDeformedShape_i::UpdateActor(VISU_Actor* theActor) {
227   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
228     if(IsColored()){
229       anActor->SetBarVisibility(true);
230       myPipeLine->GetMapper()->SetScalarVisibility(1);
231     }else{
232       anActor->SetBarVisibility(false);
233       myPipeLine->GetMapper()->SetScalarVisibility(0);
234       anActor->GetProperty()->SetColor(myColor.R,myColor.G,myColor.B);
235     }
236     TSuperClass::UpdateActor(theActor);
237   }
238 }
239
240 void VISU::ScalarMapOnDeformedShape_i::SetScalarField(const char* theMeshName,
241                                                       const char* theFieldName,
242                                                       const int   theIteration,
243                                                       const VISU::TEntity  theEntity)
244 {
245   if (myScalarMapOnDeformedShapePL){
246     myField = myResult->GetInput()->GetField(theMeshName,theEntity,theFieldName);
247     TVTKOutput* aOut = myResult->GetInput()->GetTimeStampOnMesh(theMeshName,
248                                                                 theEntity,
249                                                                 theFieldName,
250                                                                 theIteration)->GetVTKOutput();
251     myScalarMapOnDeformedShapePL->SetScalars(aOut);
252   }
253   this->SetScalarMeshName(theMeshName);
254   this->SetScalarFieldName(theFieldName);
255   this->SetScalarIteration(theIteration);
256   this->SetScalarEntity(theEntity);
257 }
258
259
260 void VISU::ScalarMapOnDeformedShape_i::SetScalarMeshName(const char* theName){
261   myScalarMeshName = theName;
262 }
263 std::string VISU::ScalarMapOnDeformedShape_i::GetScalarMeshName(){
264   return myScalarMeshName;
265 }
266
267 void VISU::ScalarMapOnDeformedShape_i::SetScalarFieldName(const char* theName){
268   myScalarFieldName = theName;
269 }
270 std::string VISU::ScalarMapOnDeformedShape_i::GetScalarFieldName(){
271   return myScalarFieldName;
272 }
273
274 void VISU::ScalarMapOnDeformedShape_i::SetScalarIteration(const int theValue){
275   myScalarIteration = theValue;
276 }
277 int VISU::ScalarMapOnDeformedShape_i::GetScalarIteration(){
278   return myScalarIteration;
279 }
280
281 void VISU::ScalarMapOnDeformedShape_i::SetScalarEntity(const VISU::TEntity theValue){
282   myScalarEntity = theValue;
283 }
284 VISU::TEntity VISU::ScalarMapOnDeformedShape_i::GetScalarEntity(){
285   return myScalarEntity;
286 }