Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / OBJECT / VISU_Actor.cxx
index ac8298d3fc4822ccd69b9b6e03281121d150bb5c..42591ab305f39bbb0f6c9d6362942850453e9c22 100644 (file)
@@ -21,8 +21,8 @@
 //
 //
 //
-//  File   : VISU_Actor.cxx
-//  Author : Laurent CORNABE with help of Nicolas REJNERI
+//  File   : 
+//  Author : 
 //  Module : VISU
 //  $Header$
 
 #include "VTKViewer_PassThroughFilter.h"
  
 #include <stdexcept>
+#include <sstream>
 
 // VTK Includes
 #include <vtkProperty.h>
-
+#include <vtkSmartPointer.h>
+#include <vtkTextMapper.h>
+#include <vtkTextActor.h>
+#include <vtkProperty2D.h>
+#include <vtkRenderer.h>
+#include <vtkCellPicker.h>
+#include <vtkCell.h>
+#include <vtkPointPicker.h>
+#include <vtkPoints.h>
+#include <vtkInteractorStyle.h>
 #include <vtkDataSet.h>
 #include <vtkPolyData.h>
 #include <vtkUnstructuredGrid.h>
@@ -48,6 +58,8 @@
 #include <vtkGeometryFilter.h>
 #include <vtkObjectFactory.h>
 
+#include <boost/bind.hpp>
+
 #include "utilities.h"
 
 using namespace std;
@@ -60,100 +72,199 @@ static int MYDEBUG = 1;
 static int MYDEBUG = 0;
 #endif
 
-//=======================================================================
+//#define ENABLE_ANNOTATION
 
+//----------------------------------------------------------------------------
 vtkStandardNewMacro(VISU_Actor);
 
-VISU_Actor::VISU_Actor(){
-  SetDebug(MYVTKDEBUG);
-  myParent = this;
-  myPipeLine = NULL;
-  myPrs3d = NULL;
+//----------------------------------------------------------------------------
+VISU_Actor
+::VISU_Actor():
+  myIsVTKMapping(false),
+  myPrs3d(NULL),
+  myActorFactory(NULL),
+  myMapper(vtkDataSetMapper::New()),
+  myIsShrunk(false),
+  myIsShrinkable(false),
+  myShrinkFilter(VTKViewer_ShrinkFilter::New()),
+  myAnnotationMapper(vtkTextMapper::New()),
+  myAnnotationActor(vtkTextActor::New())
+{
+  if(MYDEBUG) MESSAGE("VISU_Actor::VISU_Actor - this = "<<this);
+
+  myMapper->Delete();
+  myShrinkFilter->Delete();
 
   myStoreMapping = true;
 
-  myIsShrunk = false;
-  myIsShrinkable = false;
-  myShrinkFilter = VTKViewer_ShrinkFilter::New();
   myShrinkFilter->SetStoreMapping(true);
-  SetShrinkFactor();
-  
-  myMapper = vtkDataSetMapper::New();
 
-  myIO = NULL;
-  myName = "";
+  myAnnotationMapper->Delete();
+  myAnnotationActor->SetMapper(myAnnotationMapper.GetPointer());
+
+  myAnnotationActor->Delete();
+  myAnnotationActor->SetVisibility(0);
 }
 
-VISU_Actor::~VISU_Actor(){
-  SALOME_Actor::SetProperty(NULL);
+//----------------------------------------------------------------------------
+void
+VISU_Actor
+::ShallowCopy(vtkProp *prop)
+{
+  VISU_Actor *anActor = VISU_Actor::SafeDownCast(prop);
+  if(anActor != NULL){
+    setName(anActor->getName());
+    if(anActor->hasIO()) setIO(anActor->getIO());
+  }
+  Superclass::ShallowCopy(prop);
+}
 
-  myMapper->RemoveAllInputs();
-  myMapper->Delete();
+void
+VISU_Actor
+::ShallowCopyPL(VISU_PipeLine* thePipeLine)
+{
+  myPipeLine->ShallowCopy(thePipeLine);
 
-  if(myPipeLine) 
-    myPipeLine->UnRegister(this);
+  vtkDataSet* aDatsSet = myMapper->GetInput();
+  GetMapper()->ShallowCopy(thePipeLine->GetMapper());
 
-  myShrinkFilter->UnRegisterAllOutputs();
-  myShrinkFilter->Delete();
+  // To restore mapper input from pipeline
+  myMapper->SetInput(aDatsSet);
 }
 
-void VISU_Actor::setIO(const Handle(SALOME_InteractiveObject)& theIO){
-  SALOME_Actor::setIO(theIO); 
+//----------------------------------------------------------------------------
+VISU_Actor
+::~VISU_Actor()
+{
+  if(MYDEBUG) MESSAGE("~VISU_Actor() - this = "<<this);
+  Superclass::SetProperty(NULL);
+  SetDebug(MYVTKDEBUG);
+}
+
+//----------------------------------------------------------------------------
+void 
+VISU_Actor
+::setIO(const Handle(SALOME_InteractiveObject)& theIO)
+{
+  Superclass::setIO(theIO); 
   myName = theIO->getName(); 
 }
 
-void VISU_Actor::SetPrs3d(VISU::Prs3d_i* thePrs3d){ 
+//----------------------------------------------------------------------------
+void 
+VISU_Actor
+::SetPrs3d(VISU::Prs3d_i* thePrs3d)
+{ 
   myPrs3d = thePrs3d;
 }
 
-void VISU_Actor::SetPipeLine(VISU_PipeLine* thePipeLine) {
-  if (myPipeLine != thePipeLine){
-    if (myPipeLine != NULL) myPipeLine->UnRegister(this);
-    myPipeLine = thePipeLine;
-    if (myPipeLine != NULL) myPipeLine->Register(this);
-    this->Modified();
-    vtkMapper *aMapper = myPipeLine->GetMapper();
-    vtkDataSet *aDataSet = aMapper->GetInput();
-    if(!aDataSet)
-      throw std::runtime_error("VISU_Actor::SetPipeLine >> There is no input data !!!");
-    aDataSet->Update();
-    static float eps = VTK_LARGE_FLOAT * 0.1 ;
-    if(aDataSet->GetLength() > eps)
-      throw std::runtime_error("VISU_Actor::SetPipeLine >> Diagonal of the actor is too large !!!");
-    if(!aDataSet->GetNumberOfCells())
-      throw std::runtime_error("VISU_Actor::SetPipeLine >> There is no visible elements");
-    //Bug SAL4221:  Mesh with less than 10 cells : shrink mode disable
-    //SetShrinkable(aDataSet->GetNumberOfCells() > 10);
-    SetShrinkable(thePipeLine->IsShrinkable());
-    //Now, we use vtkShrinkPolyData (not vtkShrinkFilter), 
-    //and the class there is no such limitation.
-
-    myMapper->SetInput(aDataSet);
-    SetMapper(myMapper);
-  }
+VISU::Prs3d_i* 
+VISU_Actor
+::GetPrs3d()
+{ 
+  return myPrs3d;
 }
 
-void VISU_Actor::SetParent(VISU_Actor* theParent){ 
-  myParent = theParent;
+//----------------------------------------------------------------------------
+VISU::TActorFactory* 
+VISU_Actor
+::GetFactory()
+{ 
+  return myActorFactory;
 }
 
-void VISU_Actor::SetRepresentation(int theMode) { 
-  SALOME_Actor::SetRepresentation(theMode);
-  if(myRepresentation == VTK_POINTS)
-    UnShrink();
+void
+VISU_Actor
+::SetFactory(VISU::TActorFactory* theActorFactory)
+{ 
+  using namespace VISU;
+
+  if(myActorFactory == theActorFactory)
+    return;
+  
+  if(theActorFactory)
+    myDestroySignal.connect(boost::bind(&TActorFactory::RemoveActor,
+                                       theActorFactory,
+                                       _1));
+
+  myActorFactory = theActorFactory;
 }
 
-void VISU_Actor::SetOpacity(float theValue){
-  GetProperty()->SetOpacity(theValue);
+//----------------------------------------------------------------------------
+void
+VISU_Actor
+::UpdateFromFactory()
+{
+  myActorFactory->UpdateActor(this);
+  Update();
 }
 
-float VISU_Actor::GetOpacity(){ 
-  return GetProperty()->GetOpacity();
+void
+VISU_Actor
+::RemoveFromRender()
+{
+  RemoveFromRender(GetRenderer());
+}
+
+//----------------------------------------------------------------------------
+void
+VISU_Actor
+::SetMapperInput(vtkDataSet* theDataSet) 
+{
+  myMapper->SetInput(theDataSet);
+  SetMapper(myMapper.GetPointer());
+}
+
+void
+VISU_Actor
+::SetPipeLine(VISU_PipeLine* thePipeLine) 
+{
+  myPipeLine = thePipeLine;
+  if(thePipeLine){
+    if(vtkMapper *aMapper = myPipeLine->GetMapper()){
+      if(vtkDataSet *aDataSet = aMapper->GetInput()){
+       SetShrinkable(thePipeLine->IsShrinkable());
+
+       SetMapperInput(aDataSet);
+      }
+    }
+  }
+  this->Modified();
+}
+
+VISU_PipeLine* 
+VISU_Actor
+::GetPipeLine() 
+{ 
+  return myPipeLine.GetPointer();
+}
+
+VISU_PipeLine* 
+VISU_Actor
+::GetCurrentPL() 
+{ 
+  return GetPipeLine();
+}
+
+
+//----------------------------------------------------------------------------
+void
+VISU_Actor
+::SetRepresentation(int theMode) 
+{ 
+  Superclass::SetRepresentation(theMode);
+  if(myRepresentation == VTK_POINTS)
+    UnShrink();
 }
 
 
-void VISU_Actor::SetShrink(){
-  if(!myIsShrinkable) return;
+//----------------------------------------------------------------------------
+void
+VISU_Actor
+::SetShrink()
+{
+  if(!myIsShrinkable) 
+    return;
   if(vtkDataSet* aDataSet = myPassFilter[0]->GetOutput()){
     myShrinkFilter->SetInput(aDataSet);
     myPassFilter[1]->SetInput(myShrinkFilter->GetOutput());
@@ -161,8 +272,12 @@ void VISU_Actor::SetShrink(){
   }
 }
 
-void VISU_Actor::UnShrink(){
-  if(!myIsShrunk) return;
+void
+VISU_Actor
+::UnShrink()
+{
+  if(!myIsShrunk) 
+    return;
   if(vtkDataSet* aDataSet = myPassFilter[0]->GetOutput()){
     myPassFilter[1]->SetInput(aDataSet);
     myPassFilter[1]->Modified();
@@ -171,54 +286,298 @@ void VISU_Actor::UnShrink(){
   }
 }
 
-void VISU_Actor::SetShrinkable(bool theIsShrinkable){
+bool
+VISU_Actor
+::IsShrunk()
+{
+  return myIsShrunk;
+}
+
+void
+VISU_Actor
+::SetShrinkable(bool theIsShrinkable)
+{
   myIsShrinkable = theIsShrinkable;
 }
 
-void VISU_Actor::SetShrinkFactor(float theValue){
+bool
+VISU_Actor
+::IsShrunkable() 
+{ 
+  return myIsShrinkable;
+}
+
+void
+VISU_Actor
+::SetShrinkFactor(float theValue)
+{
   myShrinkFilter->SetShrinkFactor(theValue);
   Modified();
 }
 
-float VISU_Actor::GetShrinkFactor(){
+float
+VISU_Actor
+::GetShrinkFactor()
+{
   return myShrinkFilter->GetShrinkFactor();
 }
 
 
 //----------------------------------------------------------------------------
-void VISU_Actor::SetVisibility(int theMode){
-  SALOME_Actor::SetVisibility(theMode);
+void
+VISU_Actor
+::SetOpacity(float theValue)
+{
+  GetProperty()->SetOpacity(theValue);
 }
 
-int VISU_Actor::GetVisibility(){
-  return SALOME_Actor::GetVisibility();
+float
+VISU_Actor
+::GetOpacity()
+{ 
+  return GetProperty()->GetOpacity();
 }
 
-void VISU_Actor::SetLineWidth(float theLineWidth){
+void
+VISU_Actor
+::SetLineWidth(float theLineWidth)
+{
   GetProperty()->SetLineWidth(theLineWidth);
 }
 
-float VISU_Actor::GetLineWidth(){
+float
+VISU_Actor
+::GetLineWidth()
+{
   return GetProperty()->GetLineWidth();
 }
 
+//==================================================================
+// function: AddToRender
+// purpose :
+//==================================================================
+void
+VISU_Actor
+::AddToRender(vtkRenderer* theRenderer)
+{
+  Superclass::AddToRender(theRenderer);
+  theRenderer->AddActor(myAnnotationActor.GetPointer());
+}
+
+//==================================================================
+// function: RemoveFromRender
+// purpose :
+//==================================================================
+void
+VISU_Actor
+::RemoveFromRender(vtkRenderer* theRenderer)
+{
+  theRenderer->RemoveActor(myAnnotationActor.GetPointer());
+  Superclass::RemoveFromRender(theRenderer); 
+  myDestroySignal(this);
+}
+
 //----------------------------------------------------------------------------
-void VISU_Actor::ShallowCopy(vtkProp *prop){
-  VISU_Actor *anActor = VISU_Actor::SafeDownCast(prop);
-  if(anActor != NULL){
-    setName(anActor->getName());
-    if(anActor->hasIO()) setIO(anActor->getIO());
-  }
-  SALOME_Actor::ShallowCopy(prop);
+void 
+VISU_Actor
+::SetVTKMapping(bool theIsVTKMapping)
+{
+  myIsVTKMapping = theIsVTKMapping;
+}
+
+bool 
+VISU_Actor
+::IsVTKMapping() const
+{
+  return myIsVTKMapping;
 }
 
 //----------------------------------------------------------------------------
-int VISU_Actor::GetNodeObjId(int theVtkID){
-  if ( myIsShrunk ) 
-    return myShrinkFilter->GetNodeObjId(theVtkID);
-  return theVtkID;
+vtkDataSet* 
+VISU_Actor
+::GetInput()
+{
+  if(myIsVTKMapping)
+    return Superclass::GetInput();
+
+  return GetCurrentPL()->GetOutput();
 }
 
-int VISU_Actor::GetElemObjId(int theVtkID){
-  return myGeomFilter->GetElemObjId(theVtkID);
+//----------------------------------------------------------------------------
+vtkIdType
+VISU_Actor
+::GetNodeObjId(vtkIdType theID)
+{
+  if(myIsVTKMapping)
+    return Superclass::GetNodeObjId(theID);
+
+  vtkIdType anID = myGeomFilter->GetNodeObjId(theID);
+
+  if(myIsShrunk)
+   anID = myShrinkFilter->GetNodeObjId(anID);
+
+  return GetCurrentPL()->GetNodeObjID(anID);
+}
+
+vtkIdType
+VISU_Actor
+::GetNodeVTKID(vtkIdType theID)
+{
+  if(myIsVTKMapping)
+    return theID;
+
+  return GetCurrentPL()->GetNodeVTKID(theID);
+}
+
+float*
+VISU_Actor
+::GetNodeCoord(int theObjID)
+{
+  if(myIsVTKMapping)
+    return Superclass::GetNodeCoord(theObjID);
+
+  return GetCurrentPL()->GetNodeCoord(theObjID);
+}
+
+
+//----------------------------------------------------------------------------
+vtkIdType
+VISU_Actor
+::GetElemObjId(vtkIdType theID)
+{
+  if(myIsVTKMapping)
+    return Superclass::GetElemObjId(theID);
+
+  vtkIdType anID = myGeomFilter->GetElemObjId(theID);
+
+  if(myIsShrunk)
+   anID = myShrinkFilter->GetElemObjId(anID);
+
+  return GetCurrentPL()->GetElemObjID(anID);
+}
+
+vtkIdType
+VISU_Actor
+::GetElemVTKID(vtkIdType theID)
+{
+  if(myIsVTKMapping)
+    return theID;
+
+  return GetCurrentPL()->GetElemVTKID(theID);
+}
+
+vtkCell* 
+VISU_Actor
+::GetElemCell(vtkIdType theObjID)
+{
+  if(myIsVTKMapping)
+    return Superclass::GetElemCell(theObjID);
+
+  return GetCurrentPL()->GetElemCell(theObjID);
+}
+
+
+//----------------------------------------------------------------------------
+bool
+VISU_Actor
+::PreHighlight(vtkInteractorStyle* theInteractorStyle, 
+              SVTK_SelectionEvent* theSelectionEvent,
+              bool theIsHighlight)
+{
+  bool aRet = Superclass::PreHighlight(theInteractorStyle,
+                                      theSelectionEvent,
+                                      theIsHighlight);
+#ifndef ENABLE_ANNOTATION
+  return aRet;
+#endif   
+  //
+  myAnnotationActor->SetVisibility(0);
+  if(theIsHighlight){
+    switch(mySelectionMode){
+    case CellSelection:{ 
+      vtkRenderer* aRenderer = theInteractorStyle->GetCurrentRenderer();
+      myCellPicker->Pick(theSelectionEvent->myX, 
+                        theSelectionEvent->myY, 
+                        0.0, 
+                        aRenderer);
+
+      if(myCellPicker->GetActor() != this)
+       return false;
+      
+      vtkIdType aVTKId = myCellPicker->GetCellId();
+      if(aVTKId >= 0  && mySelector->IsValid(this,aVTKId,true) && hasIO()){
+       vtkIdType anObjId = GetElemObjId(aVTKId);
+       if(vtkCell* aCell = GetElemCell(anObjId)){
+         vtkPoints* aPts = aCell->GetPoints();
+         if(int aNbPts = aCell->GetNumberOfPoints()){
+           float aCoord[3] = {0.0, 0.0, 0.0};
+           for(int i = 0; i < aNbPts; i++){
+             float *aPntCoord = aPts->GetPoint(i); 
+             aCoord[0] += aPntCoord[0];
+             aCoord[1] += aPntCoord[1];
+             aCoord[2] += aPntCoord[2];
+           }
+           // Display coordinates
+           float aWorldCoord[4] = {aCoord[0]/aNbPts, aCoord[1]/aNbPts, aCoord[2]/aNbPts, 1.0};
+           aRenderer->SetWorldPoint(aWorldCoord);
+           aRenderer->WorldToDisplay();
+           float aSelectionPoint[3];
+           aRenderer->GetDisplayPoint(aSelectionPoint);
+           myAnnotationActor->SetPosition(aSelectionPoint);
+           //
+           // To prepare the annotation text
+           std::ostringstream aStr;
+           aStr<<"Cell ID: "<< anObjId;
+           std::string aString = aStr.str();
+           myAnnotationMapper->SetInput(aString.c_str());
+           
+           myAnnotationActor->SetVisibility(1);
+           return true;
+         }
+       }
+      }
+      break;
+    }
+    case NodeSelection:{ 
+      vtkRenderer* aRenderer = theInteractorStyle->GetCurrentRenderer();
+      myPointPicker->Pick(theSelectionEvent->myX, 
+                         theSelectionEvent->myY, 
+                         0.0, 
+                         aRenderer);
+      
+      if(myPointPicker->GetActor() != this)
+       return false;
+
+      vtkIdType aVtkId = myPointPicker->GetPointId();
+      if(aVtkId >= 0  && mySelector->IsValid(this,aVtkId,true) && hasIO()){
+       vtkIdType anObjId = GetNodeObjId( aVtkId );
+       if(float* aCoord = GetNodeCoord(anObjId)){
+         // Display coordinates
+         float aWorldCoord[4] = {aCoord[0], aCoord[1], aCoord[2], 1.0};
+         aRenderer->SetWorldPoint(aWorldCoord);
+         aRenderer->WorldToDisplay();
+         float aSelectionPoint[3];
+         aRenderer->GetDisplayPoint(aSelectionPoint);
+         myAnnotationActor->SetPosition(aSelectionPoint);
+         //
+         // To prepare the annotation text
+         std::ostringstream aStr;
+         aStr<<"Node ID: "<< anObjId;
+         std::string aString = aStr.str();
+         myAnnotationMapper->SetInput(aString.c_str());
+         
+         myAnnotationActor->SetVisibility(1);
+         return true;
+       }
+      }
+      break;
+    }
+    case EdgeOfCellSelection:
+      break;
+    default:
+      break;
+    }
+  }
+
+  return aRet;
 }