Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/visu.git] / src / VISU_I / VISU_Prs3d_i.cc
index 5ae2f8ab245ca287d26c2c3837aa139b6b28eda1..1bdd88b8620cfb67340c3e95225f5072df705be1 100644 (file)
@@ -1,23 +1,23 @@
 //  VISU OBJECT : interactive object for VISU entities implementation
 //
 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
-// 
-//  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. 
-// 
-//  This library is distributed in the hope that it will be useful, 
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
-//  Lesser General Public License for more details. 
-// 
-//  You should have received a copy of the GNU Lesser General Public 
-//  License along with this library; if not, write to the Free Software 
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
-// 
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+//  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.
+//
+//  This library is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+//  Lesser General Public License for more details.
+//
+//  You should have received a copy of the GNU Lesser General Public
+//  License along with this library; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
 //
 //
 //  File   : VISU_Prs3d_i.cc
 //  Module : VISU
 
 #include "VISU_PipeLine.hxx"
-#include "VISU_Result_i.hh"
 #include "VISU_Prs3d_i.hh"
+
+#include "VISU_Result_i.hh"
 #include "VISU_Actor.h"
 
-#include <vtkGeometryFilter.h>
-#include <vtkDataSetMapper.h>
+#include "SALOME_Event.hxx"
+
+#include <vtkActorCollection.h>
+#include <vtkDataSet.h>
+#include <vtkMapper.h>
+
+#include <boost/bind.hpp>
 
 using namespace VISU;
 using namespace std;
 
 #ifdef _DEBUG_
-static int MYDEBUG = 0;
+static int MYDEBUG = 1;
 #else
 static int MYDEBUG = 0;
 #endif
 
-VISU::Prs3d_i::
-Prs3d_i(Result_i* theResult, 
-       SALOMEDS::SObject_ptr theSObject) : 
-  PrsObject_i(theResult->GetStudyDocument()), 
-  myResult(theResult),
+
+//----------------------------------------------------------------------------
+VISU::Prs3d_i
+::Prs3d_i(Result_i* theResult,
+         SALOMEDS::SObject_ptr theSObject) :
+  PrsObject_i(theResult->GetStudyDocument()),
   mySObject(SALOMEDS::SObject::_duplicate(theSObject)),
+  myActorCollection(vtkActorCollection::New()),
+  myResult(theResult),
   myAddToStudy(true),
   myPipeLine(NULL)
 {
+  if(MYDEBUG) MESSAGE("Prs3d_i::Prs3d_i - this = "<<this);
+  myOffset[0] = myOffset[1] = myOffset[2] = 0;
+  myActorCollection->Delete();
   myResult->Register();
 }
 
-VISU::Prs3d_i::
-Prs3d_i(Result_i* theResult, 
-       bool theAddToStudy) : 
-  PrsObject_i(theResult->GetStudyDocument()), 
-  myResult(theResult),
+VISU::Prs3d_i
+::Prs3d_i(Result_i* theResult,
+         bool theAddToStudy) :
+  PrsObject_i(theResult->GetStudyDocument()),
+  myActorCollection(vtkActorCollection::New()),
   mySObject(SALOMEDS::SObject::_nil()),
   myAddToStudy(theAddToStudy),
+  myResult(theResult),
   myPipeLine(NULL)
 {
+  if(MYDEBUG) MESSAGE("Prs3d_i::Prs3d_i - this = "<<this);
+  myOffset[0] = myOffset[1] = myOffset[2] = 0;
+  myActorCollection->Delete();
   myResult->Register();
 }
 
-void VISU::Prs3d_i::SameAs(const Prs3d_i* theOrigin)
+void
+VISU::Prs3d_i
+::SameAs(const Prs3d_i* theOrigin)
 {
-  Prs3d_i* aOrigin = const_cast<Prs3d_i*>(theOrigin);
-  myPipeLine->ShallowCopy(aOrigin->GetPL());
+  if (Prs3d_i* aOrigin = const_cast<Prs3d_i*>(theOrigin)) {
+    myPipeLine->SameAs(aOrigin->GetPL());
+    aOrigin->GetOffset(myOffset);
+  }
 }
 
-VISU::Prs3d_i::~Prs3d_i() {
-  if(MYDEBUG) MESSAGE("~Prs3d_i() - this = "<<this<<"; GetReferenceCount() = "<<myPipeLine->GetReferenceCount());
+VISU::Prs3d_i
+::~Prs3d_i() 
+{
+  if(MYDEBUG) MESSAGE("Prs3d_i::~Prs3d_i - this = "<<this);
+  myRemoveActorsFromRendererSignal();
   myPipeLine->Delete();
   myResult->Destroy();
 }
 
-void VISU::Prs3d_i::CreateActor(VISU_Actor* theActor, const Handle(SALOME_InteractiveObject)& theIO)
+
+//----------------------------------------------------------------------------
+VISU::Storable* 
+VISU::Prs3d_i
+::Restore(const Storable::TRestoringMap& theMap)
+{
+  myName = VISU::Storable::FindValue(theMap,"myName").latin1();
+  myOffset[0] = VISU::Storable::FindValue(theMap,"myOffset[0]").toFloat();
+  myOffset[1] = VISU::Storable::FindValue(theMap,"myOffset[1]").toFloat();
+  myOffset[2] = VISU::Storable::FindValue(theMap,"myOffset[2]").toFloat();
+  return this;
+}
+
+void
+VISU::Prs3d_i
+::ToStream(std::ostringstream& theStr)
+{
+  Storable::DataToStream( theStr, "myName",   myName.c_str() );
+  Storable::DataToStream( theStr, "myOffset[0]", myOffset[0] );
+  Storable::DataToStream( theStr, "myOffset[1]", myOffset[1] );
+  Storable::DataToStream( theStr, "myOffset[2]", myOffset[2] );
+}
+
+
+//----------------------------------------------------------------------------
+SALOMEDS::SObject_var 
+VISU::Prs3d_i
+::GetSObject()
 {
+  if(CORBA::is_nil(mySObject.in())){
+    const SALOMEDS::Study_var& aStudy = myResult->GetStudyDocument();
+    CORBA::String_var anIOR = GetID();
+    mySObject = aStudy->FindObjectIOR(anIOR);
+  }
+  return mySObject;
+}
+
+Result_i* 
+VISU::Prs3d_i
+::GetResult() const 
+{ 
+  return myResult;
+}
+
+const std::string& 
+VISU::Prs3d_i
+::GetMeshName() const 
+{ 
+  return myMeshName; 
+}
+
+
+//----------------------------------------------------------------------------
+void
+VISU::Prs3d_i
+::Update() 
+{
+  if(MYDEBUG) MESSAGE("Prs3d_i::Update - this = "<<this);
   try{
-    Update();
-    if(myAddToStudy){
-      Handle(SALOME_InteractiveObject) anIO = theIO;
-      if(anIO.IsNull()){
-       anIO = new SALOME_InteractiveObject(mySObject->GetID(),"VISU",GetName());
-      }
-      theActor->setIO(anIO); 
+    myPipeLine->Update();
+  }catch(...){
+    throw std::runtime_error("Prs3d_i::Update >> unexpected exception was caught!!!");
+  }
+}
+
+
+//----------------------------------------------------------------------------
+void
+VISU::Prs3d_i
+::CheckDataSet() 
+{
+  vtkMapper *aMapper = myPipeLine->GetMapper();
+  vtkDataSet *aDataSet = aMapper->GetInput();
+  if (!aDataSet)
+    throw std::runtime_error("There is no input data !!!");
+  aDataSet->Update();
+  static float eps = VTK_LARGE_FLOAT * 0.1 ;
+  if (!aDataSet->GetNumberOfCells())
+    throw std::runtime_error("There are no visible elements");
+  if (aDataSet->GetLength() > eps)
+    throw std::runtime_error("Diagonal of the actor is too large !!!");
+}
+
+void
+VISU::Prs3d_i
+::RemoveFromStudy() 
+{
+  struct TRemoveFromStudy: public SALOME_Event
+  {
+    VISU::Prs3d_i* myRemovable;
+    typedef boost::signal0<void> TRemoveFromStudySignal;
+    const TRemoveFromStudySignal& myRemoveFromStudySignal;
+
+    TRemoveFromStudy(VISU::Prs3d_i* theRemovable,
+                    const TRemoveFromStudySignal& theRemoveFromStudySignal):
+      myRemovable(theRemovable),
+      myRemoveFromStudySignal(theRemoveFromStudySignal)
+    {}
+    
+    virtual
+    void
+    Execute()
+    {
+      myRemoveFromStudySignal();
+      myRemovable->Destroy();
     }
-    theActor->SetPipeLine(GetPipeLine());
+  };
+
+  ProcessVoidEvent(new TRemoveFromStudy(this,myRemoveActorsFromRendererSignal));
+}
+
+
+//----------------------------------------------------------------------------
+VISU_PipeLine* 
+VISU::Prs3d_i
+::GetPipeLine()
+{
+  return GetPL();
+}
+
+VISU_PipeLine* 
+VISU::Prs3d_i
+::GetPL()
+{
+  return myPipeLine;
+}
+
+vtkUnstructuredGrid* 
+VISU::Prs3d_i::
+GetInput()
+{
+  return myPipeLine->GetInput();
+}
+
+
+//----------------------------------------------------------------------------
+void
+VISU::Prs3d_i
+::CreateActor(VISU_Actor* theActor, const Handle(SALOME_InteractiveObject)& theIO)
+{
+  try{
+    Handle(SALOME_InteractiveObject) anIO = theIO;
+    if(anIO.IsNull() && (!mySObject->_is_nil())){
+      anIO = new SALOME_InteractiveObject(mySObject->GetID(),"VISU",GetName());
+      theActor->setIO(anIO);
+    }
+
+    Update();
+    CheckDataSet();
+
     theActor->SetPrs3d(this);
+    theActor->SetShrinkFactor();
+    theActor->SetPosition(myOffset);
+    theActor->SetPipeLine(GetPipeLine());
+
+    theActor->SetFactory(this);
+    myUpdateActorsSignal.connect(boost::bind(&VISU_Actor::UpdateFromFactory,theActor));
+    myRemoveActorsFromRendererSignal.connect(boost::bind(&VISU_Actor::RemoveFromRender,theActor));
+
+    myActorCollection->AddItem(theActor);
+    theActor->Delete();
+
   }catch(std::bad_alloc& ex){
     throw std::runtime_error("CreateActor >> No enough memory");
     throw ex;
-  } catch(std::exception& ex){
-    throw ex;
+  } catch(std::exception&){
+    throw;
   }catch(...) {
     throw std::runtime_error("CreateActor >> unexpected exception was caught!!!");
-  } 
+  }
 }
 
-void VISU::Prs3d_i::UpdateActor(VISU_Actor* theActor) {
-  if(MYDEBUG) MESSAGE("Prs3d_i::UpdateActor() - this = "<<this);
-  theActor->GetMapper()->ShallowCopy(myPipeLine->GetMapper());
-  theActor->Modified();
+void
+VISU::Prs3d_i
+::RemoveActor(VISU_Actor* theActor) 
+{
+  if(MYDEBUG) MESSAGE("Prs3d_i::RemoveActor - this = "<<this<<"; theActor = "<<theActor);
+  myActorCollection->RemoveItem(theActor);
 }
 
-VISU::Storable* VISU::Prs3d_i::Restore(const Storable::TRestoringMap& theMap)
+void
+VISU::Prs3d_i
+::RemoveActors()
 {
-  myName = VISU::Storable::FindValue(theMap,"myName").latin1();
-  return this;
+  if(MYDEBUG) MESSAGE("Prs3d_i::RemoveActors - this = "<<this);
+  myRemoveActorsFromRendererSignal();
+  myActorCollection->RemoveAllItems();
 }
 
-void VISU::Prs3d_i::ToStream(std::ostringstream& theStr){
-  Storable::DataToStream( theStr, "myName",   myName.c_str() );
+void
+VISU::Prs3d_i
+::UpdateActor(VISU_Actor* theActor) 
+{
+  if(MYDEBUG) MESSAGE("Prs3d_i::UpdateActor - this = "<<this<<"; theActor = "<<theActor);
+  theActor->ShallowCopyPL(myPipeLine);
+  theActor->SetPosition(myOffset);
+  theActor->Modified();
 }
 
-void VISU::Prs3d_i::Update() {
-  if(MYDEBUG) MESSAGE("Prs3d_i::Update() - this = "<<this);
-  try{
-    myPipeLine->Update();
-  }catch(...){
-    throw std::runtime_error("Prs3d_i::Update >> unexpected exception was caught!!!");
-  }
+void
+VISU::Prs3d_i
+::UpdateActors()
+{
+  if(MYDEBUG) MESSAGE("Prs3d_i::UpdateActors - this = "<<this);
+  Update();
+  CheckDataSet();
+  myUpdateActorsSignal();
 }
 
-VISU_PipeLine* VISU::Prs3d_i::GetPipeLine(){
-  return GetPL();
+
+//----------------------------------------------------------------------------
+// Clipping planes
+void
+VISU::Prs3d_i
+::RemoveAllClippingPlanes()
+{
+  myPipeLine->RemoveAllClippingPlanes();
 }
 
-VISU_PipeLine* VISU::Prs3d_i::GetPL(){
-  return myPipeLine;
+bool
+VISU::Prs3d_i
+::AddClippingPlane(vtkPlane* thePlane)
+{
+  return myPipeLine->AddClippingPlane(thePlane);
+}
+
+vtkIdType
+VISU::Prs3d_i
+::GetNumberOfClippingPlanes() const
+{
+  return myPipeLine->GetNumberOfClippingPlanes();
 }
 
+vtkPlane* 
+VISU::Prs3d_i::
+GetClippingPlane(vtkIdType theID) const
+{
+  return myPipeLine->GetClippingPlane(theID);
+}
 
-SALOMEDS::SObject_var VISU::Prs3d_i::GetSObject(){
-  return mySObject;
+void
+VISU::Prs3d_i
+::SetPlaneParam (float theDir[3], float theDist, vtkPlane* thePlane) 
+{
+  myPipeLine->SetPlaneParam(theDir, theDist, thePlane);
 }
 
-void VISU::Prs3d_i::GetBounds(float aBounds[6]){
+
+//----------------------------------------------------------------------------
+void
+VISU::Prs3d_i
+::GetBounds(float aBounds[6])
+{
   myPipeLine->GetMapper()->GetBounds(aBounds);
 }
 
-VISU::Result_i* VISU::GetResult(SALOMEDS::SObject_ptr theSObject){
+void 
+VISU::Prs3d_i
+::SetOffset(const float* theOffsets)
+{
+  myOffset[0] = theOffsets[0];
+  myOffset[1] = theOffsets[1];
+  myOffset[2] = theOffsets[2];
+}
+
+void
+VISU::Prs3d_i
+::SetOffset(float theDx, float theDy, float theDz)
+{
+  myOffset[0] = theDx;
+  myOffset[1] = theDy;
+  myOffset[2] = theDz;
+}
+
+void
+VISU::Prs3d_i
+::GetOffset(float* theOffsets)
+{
+  theOffsets[0] = myOffset[0];
+  theOffsets[1] = myOffset[1];
+  theOffsets[2] = myOffset[2];
+}
+
+void 
+VISU::Prs3d_i
+::GetOffset(float& theDx, float& theDy, float& theDz)
+{
+  theDx = myOffset[0];
+  theDy = myOffset[1];
+  theDz = myOffset[2];
+}
+
+
+//----------------------------------------------------------------------------
+VISU::Result_i* 
+VISU::GetResult(SALOMEDS::SObject_ptr theSObject)
+{
   VISU::Result_var aResult = FindResult(theSObject);
   if(!aResult->_is_nil())
     return dynamic_cast<VISU::Result_i*>(VISU::GetServant(aResult.in()).in());
   return NULL;
 }
+