Salome HOME
IPAL52939: SIGSEGV when Compute a mesh with closed VTK Viewer
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_VTKUtils.cxx
index 229d7f809518fb67cbb493a59ff6cddc8be115f5..031335ee5128420f32877bbcfb8effb59c6b94d1 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 #include "SMESHGUI_VTKUtils.h"
 
 #include "SMESHGUI.h"
-#include "SMESHGUI_Utils.h"
 #include "SMESHGUI_Filter.h"
+#include "SMESHGUI_Utils.h"
+#include "SMDS_Mesh.hxx"
+#include "SMESH_Actor.h"
+#include "SMESH_ActorUtils.h"
+#include "SMESH_CellLabelActor.h"
 #include "SMESH_ControlsDef.hxx"
-
-#include <SMESH_Actor.h>
-#include <SMESH_ActorUtils.h>
 #include "SMESH_NodeLabelActor.h"
-#include "SMESH_CellLabelActor.h"
-#include <SMESH_ObjectDef.h>
-#include <SMDS_Mesh.hxx>
+#include "SMESH_ObjectDef.h"
 
 // SALOME GUI includes
 #include <SUIT_Desktop.h>
@@ -47,7 +46,6 @@
 #include <SUIT_ResourceMgr.h>
 
 #include <SALOME_ListIO.hxx>
-#include <SALOME_ListIteratorOfListIO.hxx>
 
 #include <SVTK_Selector.h>
 #include <SVTK_ViewModel.h>
@@ -775,6 +773,7 @@ namespace SMESH
               anActor->SetVisibility(false);
               aStudy->setVisibilityState(theEntry, Qtx::HiddenState);
               break;
+            default:;
           }
         } else {
           switch (theAction) {
@@ -801,6 +800,7 @@ namespace SMESH
               }
               break;
             }
+          default:;
           }
         }
       }
@@ -810,16 +810,20 @@ namespace SMESH
   }
 
 
-  bool UpdateView(EDisplaing theAction, const char* theEntry){
+  bool UpdateView(EDisplaing theAction, const char* theEntry) {
         //MESSAGE("UpdateView");
     SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
-    SUIT_ViewWindow *aWnd = app->activeViewManager()->getActiveView();
-    return UpdateView(aWnd,theAction,theEntry);
+    if ( SUIT_ViewManager* vm = app->activeViewManager() )
+    {
+      SUIT_ViewWindow *aWnd = vm->getActiveView();
+      return UpdateView(aWnd,theAction,theEntry);
+    }
+    return false;
   }
 
   void UpdateView(){
-    if(SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()){
+    if ( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()) {
       LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
       SALOME_ListIO selected; mgr->selectedObjects( selected );
 
@@ -850,7 +854,7 @@ namespace SMESH
 
   bool Update(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
   {
-        MESSAGE("Update");
+    MESSAGE("Update");
     _PTR(Study) aStudy = GetActiveStudyDocument();
     CORBA::Long anId = aStudy->StudyId();
     if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry())) {
@@ -863,7 +867,7 @@ namespace SMESH
 
   bool UpdateNulData(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
   {
-        MESSAGE("UpdateNulData");
+    MESSAGE("UpdateNulData");
     _PTR(Study) aStudy = GetActiveStudyDocument();
     CORBA::Long anId = aStudy->StudyId();
     if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry(), true)) {
@@ -906,12 +910,12 @@ namespace SMESH
            aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
 
     int aElem0DSize = mgr->integerValue("SMESH", "elem0d_size", 5);
-    int aBallSize   = mgr->integerValue("SMESH", "ball_elem_size", 5);
+   // int aBallSize   = mgr->integerValue("SMESH", "ball_elem_size", 5);
     int aLineWidth  = mgr->integerValue("SMESH", "element_width", 1);
     int maxSize = aElem0DSize;
     if (aElem0DSize > maxSize) maxSize = aElem0DSize;
     if (aLineWidth > maxSize) maxSize = aLineWidth;
-    if (aBallSize > maxSize) maxSize = aBallSize;
+  //  if (aBallSize > maxSize) maxSize = aBallSize;
 
     double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
            SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 ),
@@ -961,7 +965,7 @@ namespace SMESH
     SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
     if ( !mgr ) return;
     //
-    vtkFloatingPointType anRGBNd[3] = {1,1,1};
+    double anRGBNd[3] = {1,1,1};
     SMESH::GetColor( "SMESH", "numbering_node_color", anRGBNd[0], anRGBNd[1], anRGBNd[2], QColor( 255, 255, 255 ) );
     int aSizeNd = 10;
     SMESH::LabelFont aFamilyNd = SMESH::FntTimes;
@@ -980,7 +984,7 @@ namespace SMESH
       aSizeNd    = f.pointSize();
     }
     //
-    vtkFloatingPointType anRGBEl[3] = {0,1,0};
+    double anRGBEl[3] = {0,1,0};
     SMESH::GetColor( "SMESH", "numbering_elem_color", anRGBEl[0], anRGBEl[1], anRGBEl[2], QColor( 0, 255, 0 ) );
     int aSizeEl = 12;
     SMESH::LabelFont aFamilyEl = SMESH::FntTimes;
@@ -991,7 +995,7 @@ namespace SMESH
     if ( mgr->hasValue( "SMESH", "numbering_elem_font" ) ) {
       QFont f = mgr->fontValue( "SMESH", "numbering_elem_font" );
 
-      if ( f.family()      == "Arial" )          aFamilyEl = SMESH::FntArial;
+      if ( f.family()      == "Arial" )   aFamilyEl = SMESH::FntArial;
       else if ( f.family() == "Courier" ) aFamilyEl = SMESH::FntCourier;
       else if ( f.family() == "Times" )   aFamilyEl = SMESH::FntTimes;    
       aBoldEl    = f.bold();
@@ -1005,23 +1009,23 @@ namespace SMESH
     foreach ( SUIT_ViewManager* vm, vmList ) {
       QVector<SUIT_ViewWindow*> views = vm->getViews();
       foreach ( SUIT_ViewWindow* vw, views ) {
-       // update VTK viewer properties
-       if ( SVTK_ViewWindow* aVtkView = GetVtkViewWindow( vw ) ) {
-         // update actors
-         vtkRenderer* aRenderer = aVtkView->getRenderer();
-         VTK::ActorCollectionCopy aCopy( aRenderer->GetActors() );
-         vtkActorCollection* aCollection = aCopy.GetActors();
-         aCollection->InitTraversal();
-         while ( vtkActor* anAct = aCollection->GetNextActor() ) {
-           if ( SMESH_NodeLabelActor* anActor = dynamic_cast< SMESH_NodeLabelActor* >( anAct ) ) {
-             anActor->SetFontProperties( aFamilyNd, aSizeNd, aBoldNd, anItalicNd, aShadowNd, anRGBNd[0], anRGBNd[1], anRGBNd[2] );
-           }
-           else if ( SMESH_CellLabelActor* anActor = dynamic_cast< SMESH_CellLabelActor* >( anAct ) ) {
-             anActor->SetFontProperties( aFamilyEl, aSizeEl, aBoldEl, anItalicEl, aShadowEl, anRGBEl[0], anRGBEl[1], anRGBEl[2] );
-           }
-         }
-         aVtkView->Repaint( false ); 
-       }
+        // update VTK viewer properties
+        if ( SVTK_ViewWindow* aVtkView = GetVtkViewWindow( vw ) ) {
+          // update actors
+          vtkRenderer* aRenderer = aVtkView->getRenderer();
+          VTK::ActorCollectionCopy aCopy( aRenderer->GetActors() );
+          vtkActorCollection* aCollection = aCopy.GetActors();
+          aCollection->InitTraversal();
+          while ( vtkActor* anAct = aCollection->GetNextActor() ) {
+            if ( SMESH_NodeLabelActor* anActor = dynamic_cast< SMESH_NodeLabelActor* >( anAct ) ) {
+              anActor->SetFontProperties( aFamilyNd, aSizeNd, aBoldNd, anItalicNd, aShadowNd, anRGBNd[0], anRGBNd[1], anRGBNd[2] );
+            }
+            else if ( SMESH_CellLabelActor* anActor = dynamic_cast< SMESH_CellLabelActor* >( anAct ) ) {
+              anActor->SetFontProperties( aFamilyEl, aSizeEl, aBoldEl, anItalicEl, aShadowEl, anRGBEl[0], anRGBEl[1], anRGBEl[2] );
+            }
+          }
+          aVtkView->Repaint( false ); 
+        }
       }
     }
   }
@@ -1323,21 +1327,21 @@ namespace SMESH
 
   //----------------------------------------------------------------------------
   // internal function
-  void ComputeBoundsParam( vtkFloatingPointType theBounds[6],
-                           vtkFloatingPointType theDirection[3],
-                           vtkFloatingPointType theMinPnt[3],
-                           vtkFloatingPointType& theMaxBoundPrj,
-                           vtkFloatingPointType& theMinBoundPrj )
+  void ComputeBoundsParam( double theBounds[6],
+                           double theDirection[3],
+                           double theMinPnt[3],
+                           double& theMaxBoundPrj,
+                           double& theMinBoundPrj )
   {
     //Enlarge bounds in order to avoid conflicts of precision
     for(int i = 0; i < 6; i += 2){
       static double EPS = 1.0E-3;
-      vtkFloatingPointType aDelta = (theBounds[i+1] - theBounds[i])*EPS;
+      double aDelta = (theBounds[i+1] - theBounds[i])*EPS;
       theBounds[i] -= aDelta;
       theBounds[i+1] += aDelta;
     }
 
-    vtkFloatingPointType aBoundPoints[8][3] = { {theBounds[0],theBounds[2],theBounds[4]},
+    double aBoundPoints[8][3] = { {theBounds[0],theBounds[2],theBounds[4]},
                                                 {theBounds[1],theBounds[2],theBounds[4]},
                                                 {theBounds[0],theBounds[3],theBounds[4]},
                                                 {theBounds[1],theBounds[3],theBounds[4]},
@@ -1350,7 +1354,7 @@ namespace SMESH
     theMaxBoundPrj = vtkMath::Dot(theDirection,aBoundPoints[aMaxId]);
     theMinBoundPrj = theMaxBoundPrj;
     for(int i = 1; i < 8; i++){
-      vtkFloatingPointType aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
+      double aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
       if(theMaxBoundPrj < aTmp){
         theMaxBoundPrj = aTmp;
         aMaxId = i;
@@ -1359,43 +1363,57 @@ namespace SMESH
         theMinBoundPrj = aTmp;
       }
     }
-    vtkFloatingPointType *aMinPnt = aBoundPoints[aMaxId];
+    double *aMinPnt = aBoundPoints[aMaxId];
     theMinPnt[0] = aMinPnt[0];
     theMinPnt[1] = aMinPnt[1];
     theMinPnt[2] = aMinPnt[2];
   }
 
   // internal function
-  void DistanceToPosition( vtkFloatingPointType theBounds[6],
-                           vtkFloatingPointType theDirection[3],
-                           vtkFloatingPointType theDist,
-                           vtkFloatingPointType thePos[3] )
+  void DistanceToPosition( double theBounds[6],
+                           double theDirection[3],
+                           double theDist,
+                           double thePos[3] )
   {
-    vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
+    double aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
     ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
-    vtkFloatingPointType aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
+    double aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
     thePos[0] = aMinPnt[0]-theDirection[0]*aLength;
     thePos[1] = aMinPnt[1]-theDirection[1]*aLength;
     thePos[2] = aMinPnt[2]-theDirection[2]*aLength;
   }
 
   // internal function (currently unused, left just in case)
-  void PositionToDistance( vtkFloatingPointType theBounds[6],
-                           vtkFloatingPointType theDirection[3],
-                           vtkFloatingPointType thePos[3],
-                           vtkFloatingPointType& theDist )
+  void PositionToDistance( double theBounds[6],
+                           double theDirection[3],
+                           double thePos[3],
+                           double& theDist )
   {
-    vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
+    double aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
     ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
-    vtkFloatingPointType aPrj = vtkMath::Dot(theDirection,thePos);
+    double aPrj = vtkMath::Dot(theDirection,thePos);
     theDist = (aPrj-aMinBoundPrj)/(aMaxBoundPrj-aMinBoundPrj);
   }
 
   bool ComputeClippingPlaneParameters( std::list<vtkActor*> theActorList,
-                                       vtkFloatingPointType theNormal[3],
-                                       vtkFloatingPointType theDist,
-                                       vtkFloatingPointType theBounds[6],
-                                       vtkFloatingPointType theOrigin[3] )
+                                       double theNormal[3],
+                                       double theDist,
+                                       double theBounds[6],
+                                       double theOrigin[3] )
+  {
+    bool anIsOk = false;
+    anIsOk = ComputeBounds( theActorList, theBounds );
+
+
+    if( !anIsOk )
+      return false;
+
+    DistanceToPosition( theBounds, theNormal, theDist, theOrigin );
+    return true;
+  }
+
+  bool ComputeBounds( std::list<vtkActor*> theActorList,
+                      double theBounds[6])
   {
     bool anIsOk = false;
     theBounds[0] = theBounds[2] = theBounds[4] = VTK_DOUBLE_MAX;
@@ -1404,7 +1422,7 @@ namespace SMESH
     for( ; anIter != theActorList.end(); anIter++ ) {
       if( vtkActor* aVTKActor = *anIter ) {
         if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
-          vtkFloatingPointType aBounds[6];
+          double aBounds[6];
           anActor->GetUnstructuredGrid()->GetBounds( aBounds );
           theBounds[0] = std::min( theBounds[0], aBounds[0] );
           theBounds[1] = std::max( theBounds[1], aBounds[1] );
@@ -1416,12 +1434,7 @@ namespace SMESH
         }
       }
     }
-
-    if( !anIsOk )
-      return false;
-    
-    DistanceToPosition( theBounds, theNormal, theDist, theOrigin );
-    return true;
+    return anIsOk;
   }
 
 #ifndef DISABLE_PLOT2DVIEWER