Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / OBJECT / VISU_Actor.cxx
index 6a2ccc1087e0a5583857781406e14788ddffdc7e..42591ab305f39bbb0f6c9d6362942850453e9c22 100644 (file)
 //
 //
 //
-//  File   : VISU_Actor.cxx
-//  Author : Laurent CORNABE with help of Nicolas REJNERI
+//  File   : 
+//  Author : 
 //  Module : VISU
 //  $Header$
 
 #include "VISU_Actor.h"
 #include "VISU_PipeLine.hxx"
-#include "SALOME_PassThroughFilter.h"
+#include "VTKViewer_ShrinkFilter.h"
+#include "VTKViewer_GeometryFilter.h"
+#include "VTKViewer_PassThroughFilter.h"
  
+#include <stdexcept>
+#include <sstream>
+
 // VTK Includes
 #include <vtkProperty.h>
-#include <vtkDataSetMapper.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>
+
+#include <vtkShrinkFilter.h>
 #include <vtkShrinkPolyData.h>
+
+#include <vtkDataSetMapper.h>
+#include <vtkGeometryFilter.h>
 #include <vtkObjectFactory.h>
 
+#include <boost/bind.hpp>
+
+#include "utilities.h"
+
 using namespace std;
 
-//=======================================================================
+static int MYVTKDEBUG = 0;
+
+#ifdef _DEBUG_
+static int MYDEBUG = 1;
+#else
+static int MYDEBUG = 0;
+#endif
+
+//#define ENABLE_ANNOTATION
 
+//----------------------------------------------------------------------------
 vtkStandardNewMacro(VISU_Actor);
 
-VISU_Actor::VISU_Actor(){
-  myIsShrunk = false;
-  myIsShrinkable = false;
-  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;
+
+  myShrinkFilter->SetStoreMapping(true);
 
-  myIO = NULL;
-  myName = "";
+  myAnnotationMapper->Delete();
+  myAnnotationActor->SetMapper(myAnnotationMapper.GetPointer());
 
-  ishighlighted = false;
-  IsHighlighting = true;
-  HighlightProperty = vtkProperty::New();
-  HighlightProperty->SetAmbient(0.5);
-  HighlightProperty->SetDiffuse(0.3);
-  HighlightProperty->SetSpecular(0.2);
-  HighlightProperty->SetRepresentationToSurface();
-  HighlightProperty->SetAmbientColor(1, 1, 1);
-  HighlightProperty->SetDiffuseColor(1, 1, 1);
-  HighlightProperty->SetSpecularColor(0.5, 0.5, 0.5);
+  myAnnotationActor->Delete();
+  myAnnotationActor->SetVisibility(0);
 }
 
-VISU_Actor::~VISU_Actor(){
-  HighlightProperty->Delete();
+//----------------------------------------------------------------------------
+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);
+}
+
+void
+VISU_Actor
+::ShallowCopyPL(VISU_PipeLine* thePipeLine)
+{
+  myPipeLine->ShallowCopy(thePipeLine);
+
+  vtkDataSet* aDatsSet = myMapper->GetInput();
+  GetMapper()->ShallowCopy(thePipeLine->GetMapper());
+
+  // 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) throw(std::runtime_error&){
-  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");
-    SetShrinkable(aDataSet->GetNumberOfCells() > 10);
-
-    vtkDataSetMapper* aNewMapper = vtkDataSetMapper::New();
-    aNewMapper->SetInput(aDataSet);
-    aMapper->ShallowCopy(aMapper);
-    SetMapper(aNewMapper);
-    aNewMapper->Delete();
+VISU::Prs3d_i* 
+VISU_Actor
+::GetPrs3d()
+{ 
+  return myPrs3d;
+}
+
+//----------------------------------------------------------------------------
+VISU::TActorFactory* 
+VISU_Actor
+::GetFactory()
+{ 
+  return myActorFactory;
+}
+
+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
+::UpdateFromFactory()
+{
+  myActorFactory->UpdateActor(this);
+  Update();
+}
+
+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();
 }
 
-void VISU_Actor::SetParent(VISU_Actor* theParent){ 
-  myParent = theParent;
+VISU_PipeLine* 
+VISU_Actor
+::GetPipeLine() 
+{ 
+  return myPipeLine.GetPointer();
 }
 
-void VISU_Actor::SetShrinkable(bool theIsShrinkable){
-  myIsShrinkable = theIsShrinkable;
+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;
+  if(vtkDataSet* aDataSet = myPassFilter[0]->GetOutput()){
+    myShrinkFilter->SetInput(aDataSet);
+    myPassFilter[1]->SetInput(myShrinkFilter->GetOutput());
+    myIsShrunk = true;
+  }
+}
+
+void
+VISU_Actor
+::UnShrink()
+{
+  if(!myIsShrunk) 
+    return;
+  if(vtkDataSet* aDataSet = myPassFilter[0]->GetOutput()){
+    myPassFilter[1]->SetInput(aDataSet);
+    myPassFilter[1]->Modified();
+    myIsShrunk = false;
+    Modified();
+  }
+}
+
+bool
+VISU_Actor
+::IsShrunk()
+{
+  return myIsShrunk;
 }
 
-void VISU_Actor::SetShrink(float theFactor){
-  if(!myIsShrinkable || myIsShrunk) return;
-  vtkShrinkPolyData *aShrinkFilter = vtkShrinkPolyData::New();
-  aShrinkFilter->SetShrinkFactor(theFactor);
-  // use for post-filtering
-  aShrinkFilter->SetInput(myPassFilter[2]->GetPolyDataOutput());
-  myPassFilter[3]->SetInput(aShrinkFilter->GetOutput());
-  aShrinkFilter->Register(myPassFilter[3]);
-  aShrinkFilter->Delete();
-  myIsShrunk = true;
+void
+VISU_Actor
+::SetShrinkable(bool theIsShrinkable)
+{
+  myIsShrinkable = theIsShrinkable;
 }
 
-void VISU_Actor::UnShrink(){
-  if(!myIsShrunk) return;
-  myPassFilter[3]->SetInput(myPassFilter[2]->GetPolyDataOutput());
-  myPassFilter[3]->Modified();
-  myIsShrunk = false;
+bool
+VISU_Actor
+::IsShrunkable() 
+{ 
+  return myIsShrinkable;
 }
 
-void VISU_Actor::SetVisibility(int theMode){
-  SALOME_Actor::SetVisibility(theMode);
+void
+VISU_Actor
+::SetShrinkFactor(float theValue)
+{
+  myShrinkFilter->SetShrinkFactor(theValue);
+  Modified();
 }
 
-int VISU_Actor::GetVisibility(){
-  return SALOME_Actor::GetVisibility();
+float
+VISU_Actor
+::GetShrinkFactor()
+{
+  return myShrinkFilter->GetShrinkFactor();
 }
 
-void VISU_Actor::SetProperty(vtkProperty* theProperty){
-  SALOME_Actor::SetProperty(theProperty);
+
+//----------------------------------------------------------------------------
+void
+VISU_Actor
+::SetOpacity(float theValue)
+{
+  GetProperty()->SetOpacity(theValue);
 }
 
-vtkProperty* VISU_Actor::GetProperty(){
-  return SALOME_Actor::GetProperty();
+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();
 }
 
-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);
+//==================================================================
+// 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
+::SetVTKMapping(bool theIsVTKMapping)
+{
+  myIsVTKMapping = theIsVTKMapping;
+}
+
+bool 
+VISU_Actor
+::IsVTKMapping() const
+{
+  return myIsVTKMapping;
+}
+
+//----------------------------------------------------------------------------
+vtkDataSet* 
+VISU_Actor
+::GetInput()
+{
+  if(myIsVTKMapping)
+    return Superclass::GetInput();
+
+  return GetCurrentPL()->GetOutput();
 }
 
-void VISU_Actor::highlight(Standard_Boolean highlight) {
-  if (this->IsHighlighting) {
-    if(highlight && !ishighlighted) {
-      ishighlighted=true;
-      this->SetProperty(HighlightProperty);
-    }else if (!highlight)
-      if(ishighlighted)        ishighlighted=false;
+//----------------------------------------------------------------------------
+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;
 }