1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // VISU OBJECT : interactive object for VISU entities implementation
24 // File : VISU_PrsObject_i.cxx
25 // Author : Alexey PETROV
28 #include "VISU_ScalarMap_i.hh"
29 #include "VISU_Prs3dUtils.hh"
31 #include "VISU_Result_i.hh"
32 #include "VISU_ResultUtils.hh"
34 #include "VISU_ScalarMapAct.h"
35 #include "VISU_ScalarMapPL.hxx"
36 #include "VISU_LookupTable.hxx"
37 #include "VISU_PipeLineUtils.hxx"
38 #include "VISU_ScalarBarActor.hxx"
39 #include "VISU_Convertor.hxx"
41 #include "SUIT_ResourceMgr.h"
43 #include "SUIT_Session.h"
44 #include "SALOME_Event.h"
45 #include "SalomeApp_Study.h"
46 #include "SalomeApp_Application.h"
48 #include <vtkDataSetMapper.h>
49 #include <vtkTextProperty.h>
52 static int MYDEBUG = 0;
54 static int MYDEBUG = 0;
57 static int INCMEMORY = 4;
61 //----------------------------------------------------------------------------
64 ::IsPossible(Result_i* theResult,
65 const std::string& theMeshName,
66 VISU::Entity theEntity,
67 const std::string& theFieldName,
68 CORBA::Long theTimeStampNumber,
69 bool theIsMemoryCheck)
74 bool anIsEstimated = true;
75 VISU::Result_i::PInput anInput = theResult->GetInput(theMeshName,
79 size_t aSize = anInput->GetTimeStampOnMeshSize(theMeshName,
80 (VISU::TEntity)theEntity,
88 aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
90 MESSAGE("ScalarMap_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<aResult);
93 }catch(std::exception& exc){
94 INFOS("Follow exception was occured :\n"<<exc.what());
96 INFOS("Unknown exception was occured!");
101 //----------------------------------------------------------------------------
105 IsSameColor(const SALOMEDS::Color& theLeft,
106 const SALOMEDS::Color& theRight)
109 VISU::CheckIsSameValue(theLeft.R, theRight.R) &&
110 VISU::CheckIsSameValue(theLeft.G, theRight.G) &&
111 VISU::CheckIsSameValue(theLeft.B, theRight.B);
115 //----------------------------------------------------------------------------
116 int VISU::ScalarMap_i::myNbPresent = 0;
118 //----------------------------------------------------------------------------
123 return VISU::GenerateName("ScalarMap",myNbPresent++);
126 //----------------------------------------------------------------------------
127 const string VISU::ScalarMap_i::myComment = "SCALARMAP";
129 //----------------------------------------------------------------------------
134 return myComment.c_str();
137 //----------------------------------------------------------------------------
143 return "ICON_TREE_SCALAR_MAP";
145 return "ICON_TREE_SCALAR_MAP_GROUPS";
148 //----------------------------------------------------------------------------
153 SALOMEDS::SObject_var aSObject = GetSObject();
154 if(CORBA::is_nil(aSObject))
157 SALOMEDS::Study_var aStudyDocument = GetStudyDocument();
158 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudyDocument->NewBuilder();
160 // Check if the icon needs to be updated, update if necessary
161 SALOMEDS::GenericAttribute_var anAttr =
162 aStudyBuilder->FindOrCreateAttribute(aSObject, "AttributePixMap");
163 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
165 CORBA::String_var aPixMapName = aPixmap->GetPixMap();
166 if(strcmp(GetIconName(), aPixMapName.in()) != 0)
167 aPixmap->SetPixMap(GetIconName());
169 // Update Object Browser
170 //rnv: to fix issue 0020167 (crash then AddMeshOnGroup called from
171 //python script). Solution: update object browser using UpdateObjBrowser event.
173 SUIT_Session* aSession = SUIT_Session::session();
174 QList<SUIT_Application*> anApplications = aSession->applications();
175 QList<SUIT_Application*>::Iterator anIter = anApplications.begin();
176 while( anIter != anApplications.end() ){
177 SUIT_Application* aSApp = *anIter;
178 if(SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aSApp)){
179 if(SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(anApp->activeStudy())){
180 if(_PTR(Study) aCStudy = aStudy->studyDS()){
181 if(aStudyDocument->StudyId() == aCStudy->StudyId()){
182 anApp->updateObjectBrowser();
192 ProcessVoidEvent(new TUpdateObjBrowser(aStudyDocument->StudyId(),&done));
195 //----------------------------------------------------------------------------
197 ScalarMap_i(EPublishInStudyMode thePublishInStudyMode) :
198 ColoredPrs3d_i(thePublishInStudyMode),
202 //----------------------------------------------------------------------------
208 //----------------------------------------------------------------------------
211 ::SameAs(const Prs3d_i* theOrigin)
213 TSuperClass::SameAs(theOrigin);
215 if(const ScalarMap_i* aPrs3d = dynamic_cast<const ScalarMap_i*>(theOrigin)){
216 ScalarMap_i* anOrigin = const_cast<ScalarMap_i*>(aPrs3d);
218 SetScaling(anOrigin->GetScaling());
219 SetGaussMetric(anOrigin->GetGaussMetric());
220 SetBarVisible(anOrigin->IsBarVisible());
226 //----------------------------------------------------------------------------
228 * Creates Scalar Map and initialises it from resources
232 ::Create(const std::string& theMeshName,
233 VISU::Entity theEntity,
234 const std::string& theFieldName,
235 CORBA::Long theTimeStampNumber)
237 TSuperClass::Create(theMeshName,theEntity,theFieldName,theTimeStampNumber);
239 SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
241 if( aResourceMgr->booleanValue("VISU", "scalar_bar_logarithmic", false) )
242 SetScaling(VISU::LOGARITHMIC);
244 SetScaling(VISU::LINEAR);
246 QColor aColor = aResourceMgr->colorValue( "VISU", "edge_color", QColor( 255, 255, 255 ) );
247 SALOMEDS::Color aLinkColor;
248 aLinkColor.R = aColor.red()/255.;
249 aLinkColor.G = aColor.green()/255.;
250 aLinkColor.B = aColor.blue()/255.;
251 SetLinkColor(aLinkColor);
253 int aGaussMetric = aResourceMgr->integerValue("VISU", "scalar_gauss_metric", 0);
254 SetGaussMetric((VISU::GaussMetric)aGaussMetric);
260 //----------------------------------------------------------------------------
265 return TSuperClass::GetMemorySize();
268 //----------------------------------------------------------------------------
271 ::Restore(SALOMEDS::SObject_ptr theSObject,
272 const Storable::TRestoringMap& theMap)
274 if(!TSuperClass::Restore(theSObject, theMap))
277 SetScaling(VISU::Scaling(VISU::Storable::FindValue(theMap,"myScaling").toInt()));
279 QString aVal = VISU::Storable::FindValue(theMap,"myShowBar", "1");
280 SetBarVisible((aVal.toInt() == 1)? true : false);
282 SetGaussMetric(VISU::GaussMetric(VISU::Storable::FindValue(theMap,"myGaussMetric").toInt()));
284 SALOMEDS::Color aLinkColor;
285 aLinkColor.R = VISU::Storable::FindValue(theMap,"myLinkColor.R").toDouble();
286 aLinkColor.G = VISU::Storable::FindValue(theMap,"myLinkColor.G").toDouble();
287 aLinkColor.B = VISU::Storable::FindValue(theMap,"myLinkColor.B").toDouble();
288 SetLinkColor(aLinkColor);
290 // Check if the icon needs to be updated, update if necessary
291 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
292 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
293 SALOMEDS::GenericAttribute_var anAttr =
294 aStudyBuilder->FindOrCreateAttribute(theSObject, "AttributePixMap");
295 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
297 CORBA::String_var aPixMapName = aPixmap->GetPixMap();
298 if(strcmp(GetIconName(), aPixMapName.in()) != 0)
299 aPixmap->SetPixMap(GetIconName());
304 //----------------------------------------------------------------------------
307 ::ToStream(std::ostringstream& theStr)
309 TSuperClass::ToStream(theStr);
311 Storable::DataToStream( theStr, "myScaling", GetScaling() );
312 Storable::DataToStream( theStr, "myShowBar", (IsBarVisible()? 1:0) );
313 Storable::DataToStream( theStr, "myGaussMetric", int(GetGaussMetric()) );
315 SALOMEDS::Color aLinkColor = GetLinkColor();
316 Storable::DataToStream( theStr, "myLinkColor.R", aLinkColor.R );
317 Storable::DataToStream( theStr, "myLinkColor.G", aLinkColor.G );
318 Storable::DataToStream( theStr, "myLinkColor.B", aLinkColor.B );
322 //----------------------------------------------------------------------------
327 return VISU::Scaling(GetSpecificPL()->GetScaling());
330 //----------------------------------------------------------------------------
333 ::SetScaling(VISU::Scaling theScaling)
335 VISU::TSetModified aModified(this);
337 ProcessVoidEvent(new TVoidMemFun1ArgEvent<VISU_ScalarMapPL, int>
338 (GetSpecificPL(), &VISU_ColoredPL::SetScaling, theScaling));
341 //----------------------------------------------------------------------------
344 ::SetLinkColor(const SALOMEDS::Color& theColor)
346 if(IsSameColor(myLinkColor, theColor))
349 VISU::TSetModified aModified(this);
351 myLinkColor = theColor;
352 myParamsTime.Modified();
355 //----------------------------------------------------------------------------
363 //----------------------------------------------------------------------------
368 return VISU::GaussMetric(GetSpecificPL()->GetGaussMetric());
371 //----------------------------------------------------------------------------
374 ::SetGaussMetric(VISU::GaussMetric theGaussMetric)
376 VISU::TSetModified aModified(this);
378 ProcessVoidEvent(new TVoidMemFun1ArgEvent<VISU_ScalarMapPL, VISU::TGaussMetric>
379 (GetSpecificPL(), &VISU_ScalarMapPL::SetGaussMetric, (VISU::TGaussMetric)theGaussMetric));
382 //----------------------------------------------------------------------------
385 ::SetSourceGeometry()
387 int aNbGroups = myGroupNames.size();
389 GetSpecificPL()->SetSourceGeometry();
390 myGroupNames.clear();
395 //----------------------------------------------------------------------------
398 ::AddMeshOnGroup(const char* theGroupName)
400 VISU::Result_i::PInput anInput = GetCResult()->GetInput();
401 VISU::PUnstructuredGridIDMapper anIDMapper = anInput->GetMeshOnGroup(GetCMeshName(), theGroupName);
403 int aNbGroups = myGroupNames.size();
404 if(myGroupNames.find(theGroupName) == myGroupNames.end()){
405 GetSpecificPL()->AddGeometry(anIDMapper->GetOutput(), theGroupName);
406 myGroupNames.insert(theGroupName);
409 // To update scalar range according to the new input (IPAL21305)
417 //----------------------------------------------------------------------------
422 int aNbGroups = myGroupNames.size();
423 GetSpecificPL()->ClearGeometry();
424 myGroupNames.clear();
430 //----------------------------------------------------------------------------
433 ::DoSetInput(bool theIsInitilizePipe, bool theReInit)
435 VISU::Result_i::PInput anInput = GetCResult()->GetInput(GetCMeshName(),
438 GetTimeStampNumber());
440 throw std::runtime_error("Mesh_i::Build - GetCResult()->GetInput() == NULL !!!");
442 SetField(anInput->GetField(GetCMeshName(),GetTEntity(),GetCFieldName()));
444 throw std::runtime_error("There is no Field with the parameters !!!");
446 VISU::PUnstructuredGridIDMapper anIDMapper =
447 anInput->GetTimeStampOnMesh(GetCMeshName(),GetTEntity(),GetCFieldName(),GetTimeStampNumber());
450 throw std::runtime_error("There is no TimeStamp with the parameters !!!");
452 GetSpecificPL()->SetUnstructuredGridIDMapper(anIDMapper);
456 //----------------------------------------------------------------------------
461 return IsPossible(GetCResult(),GetCMeshName(),GetEntity(),GetCFieldName(),GetTimeStampNumber(),true);
464 void VISU::ScalarMap_i::SetBarVisible(CORBA::Boolean theVisible)
466 if (myShowBar == theVisible)
468 VISU::TSetModified aModified(this);
469 myShowBar = theVisible;
470 myParamsTime.Modified();
473 //----------------------------------------------------------------------------
476 ::CreatePipeLine(VISU_PipeLine* thePipeLine)
478 if(MYDEBUG) MESSAGE("ScalarMap_i::CreatePipeLine() - "<<thePipeLine);
480 myScalarMapPL = VISU_ScalarMapPL::New();
481 myScalarMapPL->GetMapper()->SetScalarVisibility(1);
483 myScalarMapPL = dynamic_cast<VISU_ScalarMapPL*>(thePipeLine);
485 TSuperClass::CreatePipeLine(myScalarMapPL);
488 //----------------------------------------------------------------------------
491 ::CreateActor(bool toSupressShrinking)
493 VISU_ScalarMapAct* anActor = VISU_ScalarMapAct::New();
495 TSuperClass::CreateActor(anActor);
496 anActor->SetBarVisibility(myShowBar);
497 SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
498 int aDispMode = aResourceMgr->integerValue("VISU", "scalar_map_represent", 2);
499 bool toShrink = aResourceMgr->booleanValue("VISU", "scalar_map_shrink", false);
500 bool toUseShading = aResourceMgr->booleanValue("VISU", "represent_shading", false);
501 anActor->SetRepresentation(aDispMode);
502 if (toShrink && !toSupressShrinking) anActor->SetShrink();
503 anActor->SetShading(toUseShading);
505 anActor->SetFeatureEdgesAngle( aResourceMgr->doubleValue("VISU", "feature_edges_angle", 0.0) );
506 anActor->SetFeatureEdgesFlags( aResourceMgr->booleanValue("VISU", "show_feature_edges", false),
507 aResourceMgr->booleanValue("VISU", "show_boundary_edges", false),
508 aResourceMgr->booleanValue("VISU", "show_manifold_edges", false),
509 aResourceMgr->booleanValue("VISU", "show_non_manifold_edges", false) );
510 anActor->SetFeatureEdgesColoring( aResourceMgr->booleanValue("VISU", "feature_edges_coloring", false) );
512 anActor->SetQuadratic2DRepresentation(VISU_Actor::EQuadratic2DRepresentation(aResourceMgr->integerValue( "VISU",
516 UpdateActor(anActor);
524 //----------------------------------------------------------------------------
529 return CreateActor(false);
532 //----------------------------------------------------------------------------
535 ::UpdateActor(VISU_ActorBase* theActor)
537 if(VISU_ScalarMapAct* anActor = dynamic_cast<VISU_ScalarMapAct*>(theActor)){
538 VISU_LookupTable * aLookupTable = GetSpecificPL()->GetBarTable();
540 bool anIsScalarFilterUsed = IsScalarFilterUsed();
541 if ( anIsScalarFilterUsed || aLookupTable->HasMarkedValues() )
542 aLookupTable->ForceBuild();
544 if ( anIsScalarFilterUsed ) {
545 static unsigned char MARK_COLOR[] = { 255, 255, 255 };
546 aLookupTable->MarkValueByColor( GetScalarFilterMin(), MARK_COLOR );
547 aLookupTable->MarkValueByColor( GetScalarFilterMax(), MARK_COLOR );
550 VISU_ScalarBarActor *aScalarBar = anActor->GetScalarBar();
551 aScalarBar->SetLookupTable( aLookupTable );
553 aScalarBar->SetDistribution( GetSpecificPL()->GetDistribution() );
554 aScalarBar->SetDistributionVisibility( GetIsDistributionVisible() );
556 aScalarBar->SetTitle(GetScalarBarTitle().c_str());
557 aScalarBar->SetOrientation(GetBarOrientation());
558 aScalarBar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
559 aScalarBar->GetPositionCoordinate()->SetValue(GetPosX(),GetPosY());
560 aScalarBar->SetWidth(GetWidth());
561 aScalarBar->SetHeight(GetHeight());
562 aScalarBar->SetRatios(GetTitleSize(), GetLabelSize(),
563 GetBarWidth(), GetBarHeight());
564 aScalarBar->SetNumberOfLabels(GetLabels());
565 aScalarBar->SetLabelFormat(GetLabelsFormat());
569 vtkTextProperty* aTitleProp = aScalarBar->GetTitleTextProperty();
570 aTitleProp->SetFontFamily(GetTitFontType());
572 GetTitleColor(anRGB[0],anRGB[1],anRGB[2]);
573 aTitleProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
575 IsBoldTitle()? aTitleProp->BoldOn() : aTitleProp->BoldOff();
576 IsItalicTitle()? aTitleProp->ItalicOn() : aTitleProp->ItalicOff();
577 IsShadowTitle()? aTitleProp->ShadowOn() : aTitleProp->ShadowOff();
579 vtkTextProperty* aLabelProp = aScalarBar->GetLabelTextProperty();
580 aLabelProp->SetFontFamily(GetLblFontType());
582 GetLabelColor(anRGB[0],anRGB[1],anRGB[2]);
583 aLabelProp->SetColor(anRGB[0],anRGB[1],anRGB[2]);
585 IsBoldLabel()? aLabelProp->BoldOn() : aLabelProp->BoldOff();
586 IsItalicLabel()? aLabelProp->ItalicOn() : aLabelProp->ItalicOff();
587 IsShadowLabel()? aLabelProp->ShadowOn() : aLabelProp->ShadowOff();
589 anActor->SetBarVisibility(myShowBar);
591 aScalarBar->Modified();
593 // Update values labels
595 vtkTextProperty* aProp = anActor->GetsValLabelsProps();
598 aProp->SetFontFamily( GetValLblFontType() );
599 aProp->SetFontSize( GetValLblFontSize() );
600 aProp->SetBold( IsBoldValLbl() );
601 aProp->SetItalic( IsItalicValLbl() );
602 aProp->SetShadow( IsShadowValLbl() );
605 GetValLblFontColor( anRGB[ 0 ], anRGB[ 1 ], anRGB[ 2 ] );
606 aProp->SetColor( anRGB[ 0 ], anRGB[ 1 ], anRGB[ 2 ] );
609 // Update edge property
610 SALOMEDS::Color aLinkColor = GetLinkColor();
611 anActor->GetEdgeProperty()->SetColor(aLinkColor.R, aLinkColor.G, aLinkColor.B);
613 TSuperClass::UpdateActor(theActor);
616 //----------------------------------------------------------------------------
617 struct TGetComponentMin: public SALOME_Event
619 VISU::ColoredPrs3d_i* myColoredPrs3d;
622 typedef CORBA::Double TResult;
625 TGetComponentMin( VISU::ColoredPrs3d_i* theColoredPrs3d,
626 vtkIdType theCompID ):
627 myColoredPrs3d( theColoredPrs3d ),
628 myCompID( theCompID )
635 VISU::TNames aGroupNames;
636 VISU::GaussMetric aGaussMetric = VISU::AVERAGE;
637 if(VISU::ScalarMap_i* aPrs3d = dynamic_cast<VISU::ScalarMap_i*>(myColoredPrs3d)) {
638 aGroupNames = aPrs3d->GetSpecificPL()->GetGeometryNames();
639 aGaussMetric = aPrs3d->GetGaussMetric();
642 VISU::PMinMaxController aMinMaxController = myColoredPrs3d->GetMinMaxController();
643 if ( aMinMaxController ) {
644 myResult = aMinMaxController->GetComponentMin( myCompID );
646 VISU::TMinMax aTMinMax;
647 if(myColoredPrs3d->GetScalarField()->myIsELNO)
648 aTMinMax = myColoredPrs3d->GetScalarField()->GetMinMax( myCompID, aGroupNames, (VISU::TGaussMetric)aGaussMetric );
650 aTMinMax = myColoredPrs3d->GetScalarField()->GetAverageMinMax( myCompID, aGroupNames, (VISU::TGaussMetric)aGaussMetric );
651 myResult = aTMinMax.first;
657 //----------------------------------------------------------------------------
660 ::GetComponentMin(vtkIdType theCompID)
662 return ProcessEvent( new TGetComponentMin( this, theCompID ) );
665 //----------------------------------------------------------------------------
666 struct TGetComponentMax: public SALOME_Event
668 VISU::ColoredPrs3d_i* myColoredPrs3d;
671 typedef CORBA::Double TResult;
674 TGetComponentMax( VISU::ColoredPrs3d_i* theColoredPrs3d,
675 vtkIdType theCompID ):
676 myColoredPrs3d( theColoredPrs3d ),
677 myCompID( theCompID )
684 VISU::TNames aGroupNames;
685 VISU::GaussMetric aGaussMetric = VISU::AVERAGE;
686 if(VISU::ScalarMap_i* aPrs3d = dynamic_cast<VISU::ScalarMap_i*>(myColoredPrs3d)) {
687 aGroupNames = aPrs3d->GetSpecificPL()->GetGeometryNames();
688 aGaussMetric = aPrs3d->GetGaussMetric();
691 VISU::PMinMaxController aMinMaxController = myColoredPrs3d->GetMinMaxController();
692 if ( aMinMaxController ) {
693 myResult = aMinMaxController->GetComponentMax( myCompID );
695 VISU::TMinMax aTMinMax;
696 if(myColoredPrs3d->GetScalarField()->myIsELNO)
697 aTMinMax = myColoredPrs3d->GetScalarField()->GetMinMax( myCompID, aGroupNames, (VISU::TGaussMetric)aGaussMetric );
699 aTMinMax = myColoredPrs3d->GetScalarField()->GetAverageMinMax( myCompID, aGroupNames, (VISU::TGaussMetric)aGaussMetric );
700 myResult = aTMinMax.second;
706 //----------------------------------------------------------------------------
709 ::GetComponentMax(vtkIdType theCompID)
711 return ProcessEvent( new TGetComponentMax( this, theCompID ) );