]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_ScalarMap_i.cc
Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[modules/visu.git] / src / VISU_I / VISU_ScalarMap_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_PrsObject_i.cxx
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_ScalarMapPL.hxx"
28 #include "VISU_Result_i.hh"
29 #include "VISU_ScalarMap_i.hh"
30 #include "VISU_ScalarMapAct.h"
31 #include "QAD_Config.h"
32
33 using namespace VISU;
34 using namespace std;
35
36 #ifdef _DEBUG_
37 static int MYDEBUG = 0;
38 static int MYDEBUGWITHFILES = 0;
39 #else
40 static int MYDEBUG = 0;
41 static int MYDEBUGWITHFILES = 0;
42 #endif
43
44 static int INCMEMORY = 4;
45
46 int VISU::ScalarMap_i::IsPossible(Result_i* theResult, const char* theMeshName, VISU::Entity theEntity, 
47                                   const char* theFieldName, int theIteration)
48 {
49   try{
50     vtkIdType aSize = INCMEMORY*
51       theResult->GetInput()->GetTimeStampSize(theMeshName,(VISU::TEntity)theEntity,theFieldName,theIteration);
52     if(MYDEBUG) MESSAGE("ScalarMap_i::IsPossible - CheckAvailableMemory = "<<aSize);
53     if(VISU_PipeLine::CheckAvailableMemory(aSize)) return true;
54     return false;
55   }catch(std::runtime_error& exc){
56     INFOS("Follow exception was accured :\n"<<exc.what());
57   }catch(...){
58     INFOS("Unknown exception was accured!");
59   }
60   return 0;
61 }
62
63 int VISU::ScalarMap_i::myNbPresent = 0;
64 QString VISU::ScalarMap_i::GenerateName() { return VISU::GenerateName("ScalarMap",myNbPresent++);}
65
66 const string VISU::ScalarMap_i::myComment = "SCALARMAP";
67 const char* VISU::ScalarMap_i::GetComment() const { return myComment.c_str();}
68
69 VISU::ScalarMap_i::ScalarMap_i(Result_i* theResult, bool theAddToStudy) : 
70        PrsObject_i(theResult->GetStudyDocument()), 
71        Prs3d_i(theResult)
72 {
73   myAddToStudy = theAddToStudy;
74   myScalarMapPL = NULL;
75 }
76
77 void VISU::ScalarMap_i::Destroy(){
78   if(myAddToStudy){
79     SALOMEDS::SObject_var aSObj = myStudy->FindObjectID(GetEntry());
80     if(!aSObj->_is_nil()){
81       SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
82       aStudyBuilder->RemoveObject(aSObj);
83     }
84   }
85   Prs3d_i::Destroy();
86 }
87
88
89 void VISU::ScalarMap_i::SameAs(const ScalarMap_i* theOrigin)
90 {
91   ScalarMap_i* aScalarMap = const_cast<ScalarMap_i*>(theOrigin);
92   DoHook();
93   VISU::Prs3d_i::SameAs(aScalarMap);
94   myField = aScalarMap->GetField();
95   myMeshName = myField->myMeshName;
96   myEntity = myField->myEntity;
97   myIteration = aScalarMap->GetIteration();
98   myFieldName = aScalarMap->GetFieldName();
99
100   SetScalarMode(aScalarMap->GetScalarMode());
101
102   SetRange(aScalarMap->GetMin(), aScalarMap->GetMax());
103   myIsFixedRange = aScalarMap->IsRangeFixed();
104
105   SetScaling(aScalarMap->GetScaling());
106
107   SetOrientation(aScalarMap->GetOrientation());
108   SetPosition(aScalarMap->GetPosX(), aScalarMap->GetPosY());
109   SetSize(aScalarMap->GetWidth(), aScalarMap->GetHeight());
110   SetNbColors(aScalarMap->GetNbColors());
111   SetLabels(aScalarMap->GetLabels());
112   SetTitle(aScalarMap->GetTitle());
113
114   Build(-1);
115   Update();
116 }
117
118
119 /**
120  * Creates Scalar Map and initialises it from resources
121  */
122 VISU::Storable* VISU::ScalarMap_i::Create(const char* theMeshName, VISU::Entity theEntity, 
123                                           const char* theFieldName, int theIteration)
124 {
125   DoHook();
126
127   // Orientation
128   QString aOrient = QAD_CONFIG->getSetting("Visu:SBOrientation");
129   if ( !aOrient.isEmpty() ) 
130     myOrientation = (VISU::ScalarMap::Orientation) aOrient.toInt();
131   else
132     myOrientation = VISU::ScalarMap::VERTICAL;
133
134   // Scalar Bar origin
135   QString aXorigin = QAD_CONFIG->getSetting("Visu:SBXorigin");
136   if ( !aXorigin.isEmpty() )
137     myPosition[0] = aXorigin.toFloat();
138   else {
139     if(myOrientation == VISU::ScalarMap::VERTICAL) {
140       myPosition[0] = 0.03;
141     } else {
142       myPosition[0] = 0.2;
143     }
144   }
145   
146   QString aYorigin = QAD_CONFIG->getSetting("Visu:SBYorigin");
147   if ( !aYorigin.isEmpty() )
148     myPosition[1] = aYorigin.toFloat();
149   else {
150     if(myOrientation == VISU::ScalarMap::VERTICAL) {
151       myPosition[1] = 0.1;
152     } else {
153       myPosition[1] = 0.012;
154     }
155   }
156
157   // Scalar Bar size
158   QString aWidth = QAD_CONFIG->getSetting("Visu:SBWidth");
159   if ( !aWidth.isEmpty() )
160     myWidth = aWidth.toFloat();
161   else {
162     myWidth =(myOrientation == VISU::ScalarMap::VERTICAL)? 0.17:0.6;
163   }
164   QString aHeight = QAD_CONFIG->getSetting("Visu:SBHeight");
165   if ( !aHeight.isEmpty() )
166     myHeight = aHeight.toFloat();
167   else {
168     myHeight =(myOrientation == VISU::ScalarMap::VERTICAL)? 0.8:0.12;
169   }
170
171   // Nb of Colors
172   QString aColors = QAD_CONFIG->getSetting("Visu:SBNumberOfColors");
173   int aNumberOfColors = (aColors.isEmpty())? 64 : aColors.toInt();
174   if (aNumberOfColors > 64) 
175     aNumberOfColors = 64;
176   SetNbColors(aNumberOfColors);
177
178   // Nb of Labels
179   QString aLabels = QAD_CONFIG->getSetting("Visu:SBNumberOfLabels");
180   myNumberOfLabels = (aLabels.isEmpty())? 5 : aLabels.toInt();
181   if (myNumberOfLabels > 65) 
182     myNumberOfLabels = 65;
183   
184   // Scalar Range
185   QString aFixRange = QAD_CONFIG->getSetting("Visu:SBImposeRange");
186   myIsFixedRange = (aFixRange.compare("true") == 0);
187
188   if(myIsFixedRange){
189     float aScalarRange[2];
190     QString aRangeMin = QAD_CONFIG->getSetting("Visu:SBMinimumValue");
191     float aMin = (aRangeMin.isEmpty())? 0 : aRangeMin.toFloat();
192     QString aRangeMax = QAD_CONFIG->getSetting("Visu:SBMaximumValue");
193     float aMax = (aRangeMax.isEmpty())? 0 : aRangeMax.toFloat();
194     SetRange(aMin,aMax);
195   }
196
197   QString aScaling = QAD_CONFIG->getSetting("Visu:SBScaling");
198   if(aScaling.compare("LOGARITHMIC") == 0) 
199     SetScaling(VISU::LOGARITHMIC);
200   else 
201     SetScaling(VISU::LINEAR);
202
203   myMeshName = theMeshName;
204   myEntity = (VISU::TEntity)theEntity;
205   myFieldName =theFieldName;
206   myIteration = theIteration;
207
208   return Build(false);
209 }
210
211
212 VISU::Storable* VISU::ScalarMap_i::Restore(const Storable::TRestoringMap& theMap)
213      throw(std::logic_error&)
214 {
215   DoHook();
216
217   Prs3d_i::Restore(theMap);
218
219   myAddToStudy = false; //SRN Added 21/06/2003 SAL2983: to avoid addition of the new ScalarMap to study.
220
221   myMeshName = VISU::Storable::FindValue(theMap,"myMeshName").latin1();
222   myEntity = (VISU::TEntity)VISU::Storable::FindValue(theMap,"myEntity").toInt();
223   myFieldName = VISU::Storable::FindValue(theMap,"myFieldName").latin1();
224   myIteration = VISU::Storable::FindValue(theMap,"myIteration").toInt();
225
226   SetScalarMode(VISU::Storable::FindValue(theMap,"myScalarMode").toInt());
227   SetScaling(VISU::Scaling(VISU::Storable::FindValue(theMap,"myScaling").toInt()));
228   float aMin = VISU::Storable::FindValue(theMap,"myScalarRange[0]").toDouble();
229   float aMax = VISU::Storable::FindValue(theMap,"myScalarRange[1]").toDouble();
230   SetRange(aMin,aMax);
231   myIsFixedRange = VISU::Storable::FindValue(theMap,"myIsFixedRange").toInt();
232
233   myTitle = VISU::Storable::FindValue(theMap,"myTitle").latin1();
234   myOrientation = (VISU::ScalarMap::Orientation)VISU::Storable::FindValue(theMap,"myOrientation").toInt();
235   SetNbColors(VISU::Storable::FindValue(theMap,"myNumberOfColors").toInt());
236   myNumberOfLabels = VISU::Storable::FindValue(theMap,"myNumberOfLabels").toInt();
237   myPosition[0] = VISU::Storable::FindValue(theMap,"myPosition[0]").toDouble();
238   myPosition[1] = VISU::Storable::FindValue(theMap,"myPosition[1]").toDouble();
239   myWidth = VISU::Storable::FindValue(theMap,"myWidth").toDouble();
240   myHeight = VISU::Storable::FindValue(theMap,"myHeight").toDouble();
241
242   return Build(true);
243 }
244
245
246 void VISU::ScalarMap_i::ToStream(std::ostringstream& theStr){
247   Prs3d_i::ToStream(theStr);
248
249   Storable::DataToStream( theStr, "myMeshName",       myMeshName.c_str() );
250   Storable::DataToStream( theStr, "myEntity",         myEntity );
251   Storable::DataToStream( theStr, "myFieldName",      myFieldName.c_str() );
252   Storable::DataToStream( theStr, "myIteration",      myIteration );
253
254   Storable::DataToStream( theStr, "myScalarMode",     int(GetScalarMode()) );
255   Storable::DataToStream( theStr, "myScalarRange[0]", GetMin() );
256   Storable::DataToStream( theStr, "myScalarRange[1]", GetMax() );
257   Storable::DataToStream( theStr, "myIsFixedRange",   myIsFixedRange );
258   Storable::DataToStream( theStr, "myScaling",        GetScaling() );
259
260   Storable::DataToStream( theStr, "myTitle",          myTitle.c_str() );
261   Storable::DataToStream( theStr, "myOrientation",    myOrientation );
262   Storable::DataToStream( theStr, "myNumberOfColors", int(GetNbColors()) );
263   Storable::DataToStream( theStr, "myNumberOfLabels", myNumberOfLabels );
264   Storable::DataToStream( theStr, "myPosition[0]",    myPosition[0] );
265   Storable::DataToStream( theStr, "myPosition[1]",    myPosition[1] );
266   Storable::DataToStream( theStr, "myWidth",          myWidth );
267   Storable::DataToStream( theStr, "myHeight",         myHeight );
268 }
269
270
271 CORBA::Long VISU::ScalarMap_i::GetScalarMode(){ 
272   return myScalarMapPL->GetScalarMode();
273 }
274 void VISU::ScalarMap_i::SetScalarMode(CORBA::Long theScalarMode) { 
275   myScalarMapPL->SetScalarMode(theScalarMode);
276 }
277
278
279 VISU::Scaling VISU::ScalarMap_i::GetScaling(){
280   return VISU::Scaling(myScalarMapPL->GetScaling());
281 }
282 void VISU::ScalarMap_i::SetScaling(VISU::Scaling theScaling){
283   myScalarMapPL->SetScaling(theScaling);
284 }
285
286
287 void VISU::ScalarMap_i::SetRange(CORBA::Double theMin, CORBA::Double theMax){ 
288   if(theMin > theMax) return;
289   float aScalarRange[2] = {theMin, theMax};
290   myScalarMapPL->SetScalarRange(aScalarRange);
291   myIsFixedRange = true;
292 }
293
294 CORBA::Double VISU::ScalarMap_i::GetMin(){
295   return myScalarMapPL->GetScalarRange()[0];
296 }
297
298 CORBA::Double VISU::ScalarMap_i::GetMax(){
299   return myScalarMapPL->GetScalarRange()[1];
300 }
301
302    
303 void VISU::ScalarMap_i::SetNbColors(CORBA::Long theNbColors){
304   myScalarMapPL->SetNbColors(theNbColors);
305 }
306
307 CORBA::Long VISU::ScalarMap_i::GetNbColors(){
308   return myScalarMapPL->GetNbColors();
309 }
310
311
312 VISU::Storable* VISU::ScalarMap_i::Restore(SALOMEDS::SObject_ptr theSObject, 
313                                            const string& thePrefix, const Storable::TRestoringMap& theMap)
314   throw(std::logic_error&)
315 {
316   if(VISU::Result_i* pResult = GetResult(theSObject)){
317     VISU::ScalarMap_i* pResent = new VISU::ScalarMap_i(pResult);
318     return pResent->Restore(theMap);
319   }
320   return NULL;
321 }
322
323
324 VISU::ScalarMap_i::~ScalarMap_i(){
325   if(MYDEBUG) MESSAGE("ScalarMap_i::~ScalarMap_i()");
326 }
327
328
329 VISU::Storable* VISU::ScalarMap_i::Build(int theRestoring){
330   if(MYDEBUG) 
331     MESSAGE("ScalarMap_i::Build - "<<myFieldName<<"; theRestoring = "<<theRestoring);
332   try{
333     if(myResult->GetInput() == NULL) 
334       throw std::runtime_error("Mesh_i::Build - myResult->GetInput() == NULL !!!");
335     myField = &(myResult->GetInput()->GetField(myMeshName,myEntity,myFieldName));
336     if(myField == NULL) throw std::runtime_error("There is no Field with the parameters !!!");
337     VISU_Convertor::TOutput *anOutput = 
338       myResult->GetInput()->GetTimeStampOnMesh(myMeshName,myEntity,myFieldName,myIteration);
339     if(anOutput == NULL) throw std::runtime_error("There is no TimeStamp with the parameters !!!");
340     myScalarMapPL->SetInput(anOutput);
341     myScalarMapPL->Build();
342     QString aComment;
343     myName = "NoName";
344     if(theRestoring <= 0){
345       if(theRestoring == 0) myScalarMapPL->Init();
346       if(!myIsFixedRange) myScalarMapPL->SetSourceRange();
347       const VISU::TField::TValField& aValField = myField->myValField;
348       const VISU::TField::TValForTime& aValForTime = aValField.find(myIteration)->second;
349       aComment.sprintf("%s %s",myFieldName.c_str(),VISU_Convertor::GenerateName(aValForTime.myTime).c_str());
350       myTitle = aComment.simplifyWhiteSpace().latin1();
351     }
352     if(myAddToStudy){
353       myName = GenerateName().latin1();
354       aComment.sprintf("myComment=TIMESTAMP;myType=%d;myMeshName=%s;myEntityId=%d;myFieldName=%s;myTimeStampId=%d;myNumComponent=%d",
355                        VISU::TTIMESTAMP,myMeshName.c_str(),myEntity,myFieldName.c_str(),myIteration,myField->myNbComp);
356       string aResultEntry = myResult->GetEntry();
357       string aRefFatherEntry = myResult->GetRefFatherEntry();
358       string anEntry = FindEntryWithComment(myStudy,aResultEntry.c_str(),aComment.latin1());
359       if(anEntry == "") throw std::runtime_error("There is no Entry for binding the presentation !!!");
360       aComment.sprintf("myComment=%s;myMeshName=%s;myEntityId=%d;myFieldName=%s;myTimeStampId=%d;myNumComponent=%d",
361                        GetComment(),myMeshName.c_str(),myEntity,myFieldName.c_str(),myIteration,myField->myNbComp);
362       CORBA::String_var anIOR = GetID();
363       CreateAttributes(myStudy,anEntry.c_str(),aRefFatherEntry.c_str(),anIOR,myName.c_str(),"",aComment.latin1(),true);
364     }
365     return this;
366   }catch(std::runtime_error& exc){
367     INFOS("Follow exception was accured :\n"<<exc.what());
368   }catch(...){
369     INFOS("Unknown exception was accured!");
370   }
371   return NULL;
372 }
373
374
375 void VISU::ScalarMap_i::DoHook(){
376   if(MYDEBUG)  MESSAGE("ScalarMap_i::DoHook() - "<<myPipeLine);
377   if(!myPipeLine) {
378     myPipeLine = VISU_ScalarMapPL::New();
379     myPipeLine->GetMapper()->SetScalarVisibility(1);
380   }
381   myScalarMapPL = dynamic_cast<VISU_ScalarMapPL*>(myPipeLine);
382 }
383
384 void VISU::ScalarMap_i::Update(){
385   VISU::Prs3d_i::Update();
386 }
387
388 void VISU::ScalarMap_i::SetMapScale(double theMapScale){
389   myScalarMapPL->SetMapScale(theMapScale);
390 }
391
392 VISU_Actor* VISU::ScalarMap_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO)
393   throw (std::runtime_error&)
394 {
395   VISU_ScalarMapAct* anActor = VISU_ScalarMapAct::New();
396   try{
397     VISU::Prs3d_i::CreateActor(anActor,theIO);
398     anActor->SetBarVisibility(true);
399     anActor->SetRepresentation(2);
400     anActor->myFieldName = myFieldName;
401     UpdateActor(anActor);
402   }catch(std::runtime_error& exc){
403     anActor->Delete();
404     throw exc; 
405   }
406   return anActor;
407 }
408
409 void VISU::ScalarMap_i::UpdateActor(VISU_Actor* theActor){
410   if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
411     VISU::Prs3d_i::UpdateActor(theActor);
412     VISU_ScalarBarActor *aScalarBar = anActor->GetScalarBar();
413     aScalarBar->SetLookupTable(myScalarMapPL->GetBarTable());
414     aScalarBar->SetTitle(myTitle.c_str());
415     aScalarBar->SetOrientation(myOrientation);
416     aScalarBar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
417     aScalarBar->GetPositionCoordinate()->SetValue(myPosition[0],myPosition[1]);
418     aScalarBar->SetWidth(myWidth);
419     aScalarBar->SetHeight(myHeight);
420     aScalarBar->SetNumberOfLabels(myNumberOfLabels);
421     aScalarBar->Modified();
422   }
423 }
424
425
426 void VISU::ScalarMap_i::SetSourceRange(){
427   myScalarMapPL->SetSourceRange();
428   myIsFixedRange = false;
429 }