Back to index

salome-smesh  6.5.0
SMESH_NodeLabelActor.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : SMESH_NodeLabelActor.cxx
00023 //  Author : Roman NIKOLAEV
00024 //  Module : SMESH
00025 //
00026 #include "SMESH_NodeLabelActor.h"
00027 
00028 #include <VTKViewer_TransformFilter.h>
00029 
00030 #include <vtkObjectFactory.h>
00031 #include <vtkCallbackCommand.h>
00032 #include <vtkMaskPoints.h>
00033 #include <vtkSelectVisiblePoints.h>
00034 #include <vtkLabeledDataMapper.h>
00035 #include <vtkActor2D.h>
00036 #include <vtkTextProperty.h>
00037 #include <vtkPointData.h>
00038 #include <vtkProperty2D.h>
00039 #include <vtkRenderer.h>
00040 #include <vtkUnstructuredGrid.h>
00041 
00042 vtkStandardNewMacro(SMESH_NodeLabelActor);
00043 
00047 SMESH_NodeLabelActor::SMESH_NodeLabelActor() {
00048   //Definition of points numbering pipeline
00049   //---------------------------------------
00050   myPointsNumDataSet = vtkUnstructuredGrid::New();
00051 
00052   myPtsMaskPoints = vtkMaskPoints::New();
00053   myPtsMaskPoints->SetInput(myPointsNumDataSet);
00054   myPtsMaskPoints->SetOnRatio(1);
00055 
00056   myPtsSelectVisiblePoints = vtkSelectVisiblePoints::New();
00057   myPtsSelectVisiblePoints->SetInput(myPtsMaskPoints->GetOutput());
00058   myPtsSelectVisiblePoints->SelectInvisibleOff();
00059   myPtsSelectVisiblePoints->SetTolerance(0.1);
00060     
00061   myPtsLabeledDataMapper = vtkLabeledDataMapper::New();
00062   myPtsLabeledDataMapper->SetInput(myPtsSelectVisiblePoints->GetOutput());
00063   myPtsLabeledDataMapper->SetLabelFormat("%d");
00064   myPtsLabeledDataMapper->SetLabelModeToLabelScalars();
00065     
00066   vtkTextProperty* aPtsTextProp = vtkTextProperty::New();
00067   aPtsTextProp->SetFontFamilyToTimes();
00068   static int aPointsFontSize = 10;
00069   aPtsTextProp->SetFontSize(aPointsFontSize);
00070   aPtsTextProp->SetBold(1);
00071   aPtsTextProp->SetItalic(0);
00072   aPtsTextProp->SetShadow(0);
00073   myPtsLabeledDataMapper->SetLabelTextProperty(aPtsTextProp);
00074   aPtsTextProp->Delete();
00075 
00076   myIsPointsLabeled = false;
00077 
00078   myPointLabels = vtkActor2D::New();
00079   myPointLabels->SetMapper(myPtsLabeledDataMapper);
00080   myPointLabels->GetProperty()->SetColor(1,1,1);
00081   myPointLabels->SetVisibility(myIsPointsLabeled);
00082 
00083   vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
00084   callBackCommand->SetClientData(this);
00085   callBackCommand->SetCallback(SMESH_NodeLabelActor::ProcessEvents);
00086 
00087   myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
00088                              callBackCommand);
00089   callBackCommand->Delete();
00090 }
00091 
00095 SMESH_NodeLabelActor::~SMESH_NodeLabelActor() {
00096   //Deleting of points numbering pipeline
00097   //---------------------------------------
00098   myPointsNumDataSet->Delete();
00099   
00100   // commented: porting to vtk 5.0
00101   //  myPtsLabeledDataMapper->RemoveAllInputs();
00102   myPtsLabeledDataMapper->Delete();
00103   
00104   // commented: porting to vtk 5.0
00105   //  myPtsSelectVisiblePoints->UnRegisterAllOutputs();
00106   myPtsSelectVisiblePoints->Delete();
00107   
00108   // commented: porting to vtk 5.0
00109   //  myPtsMaskPoints->UnRegisterAllOutputs();
00110   myPtsMaskPoints->Delete();
00111   myPointLabels->Delete();
00112 
00113 }
00114 
00115 void SMESH_NodeLabelActor::SetPointsLabeled(bool theIsPointsLabeled) {
00116   myTransformFilter->Update();
00117   vtkDataSet* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
00118 
00119   if(!aGrid)
00120     return;
00121     
00122   myIsPointsLabeled = theIsPointsLabeled && aGrid->GetNumberOfPoints();
00123 
00124   if ( myIsPointsLabeled )
00125   {
00126     myPointsNumDataSet->ShallowCopy(aGrid);
00127     vtkUnstructuredGrid *aDataSet = myPointsNumDataSet;
00128     
00129     int aNbElem = aDataSet->GetNumberOfPoints();
00130     
00131     vtkIntArray *anArray = vtkIntArray::New();
00132     anArray->SetNumberOfValues( aNbElem );
00133     
00134     for ( vtkIdType anId = 0; anId < aNbElem; anId++ )
00135     {
00136       int aSMDSId = myVisualObj->GetNodeObjId( anId );
00137       anArray->SetValue( anId, aSMDSId );
00138     }
00139     
00140     aDataSet->GetPointData()->SetScalars( anArray );
00141     myPtsMaskPoints->SetInput( aDataSet );
00142     myPointLabels->SetVisibility( GetVisibility() );
00143     anArray->Delete();
00144   }
00145   else
00146   {
00147     myPointLabels->SetVisibility( false );
00148   } 
00149 }
00150 
00151 
00152 void SMESH_NodeLabelActor::SetVisibility(int theMode)
00153 {
00154   SMESH_DeviceActor::SetVisibility(theMode);
00155   myPointLabels->VisibilityOff();
00156   if(myIsPointsLabeled && theMode)
00157     myPointLabels->VisibilityOn();
00158 }
00159 
00160 
00161 void SMESH_NodeLabelActor::AddToRender(vtkRenderer* theRenderer)
00162 {
00163   SMESH_DeviceActor::AddToRender(theRenderer);
00164   myPtsSelectVisiblePoints->SetRenderer(theRenderer);
00165   theRenderer->AddActor2D(myPointLabels);
00166 }
00167 
00168 void SMESH_NodeLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
00169 {
00170   theRenderer->RemoveActor(myPointLabels);
00171   SMESH_DeviceActor::RemoveFromRender(theRenderer);
00172 }
00173 
00174 void SMESH_NodeLabelActor::UpdateLabels() {
00175   if(myIsPointsLabeled)
00176     SetPointsLabeled(myIsPointsLabeled);
00177 }
00178 
00179 
00180 void SMESH_NodeLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
00181                                     unsigned long theEvent,
00182                                     void* theClientData,
00183                                     void* vtkNotUsed(theCallData)) {
00184   SMESH_NodeLabelActor* self = reinterpret_cast<SMESH_NodeLabelActor*>(theClientData);    
00185   if(self)
00186     self->UpdateLabels();
00187 }