Back to index

salome-smesh  6.5.0
SMESH_CellLabelActor.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_CellLabelActor.cxx
00023 //  Author : Roman NIKOLAEV
00024 //  Module : SMESH
00025 //
00026 #include "SMESH_CellLabelActor.h"
00027 
00028 #include <VTKViewer_TransformFilter.h>
00029 #include <VTKViewer_CellCenters.h>
00030 
00031 #include <vtkObjectFactory.h>
00032 #include <vtkCallbackCommand.h>
00033 #include <vtkMaskPoints.h>
00034 #include <vtkSelectVisiblePoints.h>
00035 #include <vtkLabeledDataMapper.h>
00036 #include <vtkActor2D.h>
00037 #include <vtkTextProperty.h>
00038 #include <vtkPointData.h>
00039 #include <vtkProperty2D.h>
00040 #include <vtkRenderer.h>
00041 #include <vtkUnstructuredGrid.h>
00042 #include <vtkCellData.h>
00043 
00044 vtkStandardNewMacro(SMESH_CellLabelActor);
00045 
00049 SMESH_CellLabelActor::SMESH_CellLabelActor() {
00050     //Definition of cells numbering pipeline
00051   //---------------------------------------
00052   myCellsNumDataSet = vtkUnstructuredGrid::New();
00053 
00054   myCellCenters = VTKViewer_CellCenters::New();
00055   myCellCenters->SetInput(myCellsNumDataSet);
00056 
00057   myClsMaskPoints = vtkMaskPoints::New();
00058   myClsMaskPoints->SetInput(myCellCenters->GetOutput());
00059   myClsMaskPoints->SetOnRatio(1);
00060     
00061   myClsSelectVisiblePoints = vtkSelectVisiblePoints::New();
00062   myClsSelectVisiblePoints->SetInput(myClsMaskPoints->GetOutput());
00063   myClsSelectVisiblePoints->SelectInvisibleOff();
00064   myClsSelectVisiblePoints->SetTolerance(0.1);
00065     
00066   myClsLabeledDataMapper = vtkLabeledDataMapper::New();
00067   myClsLabeledDataMapper->SetInput(myClsSelectVisiblePoints->GetOutput());
00068 
00069   myClsLabeledDataMapper->SetLabelFormat("%d");
00070   myClsLabeledDataMapper->SetLabelModeToLabelScalars();
00071     
00072   vtkTextProperty* aClsTextProp = vtkTextProperty::New();
00073   aClsTextProp->SetFontFamilyToTimes();
00074   static int aCellsFontSize = 12;
00075   aClsTextProp->SetFontSize(aCellsFontSize);
00076   aClsTextProp->SetBold(1);
00077   aClsTextProp->SetItalic(0);
00078   aClsTextProp->SetShadow(0);
00079   myClsLabeledDataMapper->SetLabelTextProperty(aClsTextProp);
00080   aClsTextProp->Delete();
00081     
00082   myIsCellsLabeled = false;
00083 
00084   myCellsLabels = vtkActor2D::New();
00085   myCellsLabels->SetMapper(myClsLabeledDataMapper);
00086   myCellsLabels->GetProperty()->SetColor(0,1,0);
00087   myCellsLabels->SetVisibility(myIsCellsLabeled);
00088 
00089   vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
00090   callBackCommand->SetClientData(this);
00091   callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
00092 
00093   myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
00094                              callBackCommand);
00095   callBackCommand->Delete();
00096 }
00097 
00098 
00102 SMESH_CellLabelActor::~SMESH_CellLabelActor() {
00103   //Deleting of cells numbering pipeline
00104   //---------------------------------------
00105   myCellsNumDataSet->Delete();
00106 
00107   myClsLabeledDataMapper->RemoveAllInputs();
00108   myClsLabeledDataMapper->Delete();
00109   
00110   // commented: porting to vtk 5.0
00111   //  myClsSelectVisiblePoints->UnRegisterAllOutputs();
00112   myClsSelectVisiblePoints->Delete();
00113   
00114   // commented: porting to vtk 5.0
00115   //  myClsMaskPoints->UnRegisterAllOutputs();
00116   myClsMaskPoints->Delete();
00117   
00118   // commented: porting to vtk 5.0
00119   //  myCellCenters->UnRegisterAllOutputs();
00120   myCellCenters->Delete();
00121   
00122   myCellsLabels->Delete();
00123 }
00124 
00125 
00126 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled) {
00127   myTransformFilter->Update();
00128   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
00129   if(!aGrid)
00130     return;
00131 
00132   myIsCellsLabeled = theIsCellsLabeled && aGrid->GetNumberOfPoints();
00133   if(myIsCellsLabeled){
00134     myCellsNumDataSet->ShallowCopy(aGrid);
00135     vtkUnstructuredGrid *aDataSet = myCellsNumDataSet;
00136     int aNbElem = aDataSet->GetNumberOfCells();
00137     vtkIntArray *anArray = vtkIntArray::New();
00138     anArray->SetNumberOfValues(aNbElem);
00139     for(int anId = 0; anId < aNbElem; anId++){
00140       int aSMDSId = myVisualObj->GetElemObjId(anId);
00141       anArray->SetValue(anId,aSMDSId);
00142     }
00143     aDataSet->GetCellData()->SetScalars(anArray);
00144     myCellCenters->SetInput(aDataSet);
00145     myCellsLabels->SetVisibility(GetVisibility());
00146   }else{
00147     myCellsLabels->SetVisibility(false);
00148   }
00149 }
00150 
00151 void SMESH_CellLabelActor::SetVisibility(int theMode)
00152 {
00153   SMESH_DeviceActor::SetVisibility(theMode);
00154   myCellsLabels->VisibilityOff();
00155   if(myIsCellsLabeled && theMode)
00156     myCellsLabels->VisibilityOn();
00157 }
00158 
00159 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
00160 {
00161   SMESH_DeviceActor::AddToRender(theRenderer);
00162   myClsSelectVisiblePoints->SetRenderer(theRenderer);
00163   theRenderer->AddActor2D(myCellsLabels);
00164 }
00165 
00166 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
00167 {
00168   theRenderer->RemoveActor(myCellsLabels);
00169   SMESH_DeviceActor::RemoveFromRender(theRenderer);
00170 }
00171 
00172 void SMESH_CellLabelActor::UpdateLabels() {
00173   if(myIsCellsLabeled)
00174     SetCellsLabeled(myIsCellsLabeled);
00175 }
00176 
00177 
00178 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
00179                                     unsigned long theEvent,
00180                                     void* theClientData,
00181                                     void* vtkNotUsed(theCallData)) {
00182   SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
00183   if(self)
00184     self->UpdateLabels();
00185 }