1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESH_CellLabelActor.cxx
23 // Author : Roman NIKOLAEV
26 #include "SMESH_CellLabelActor.h"
28 #include <VTKViewer_TransformFilter.h>
29 #include <VTKViewer_CellCenters.h>
31 #include <vtkObjectFactory.h>
32 #include <vtkCallbackCommand.h>
33 #include <vtkMaskPoints.h>
34 #include <vtkSelectVisiblePoints.h>
35 #include <vtkLabeledDataMapper.h>
36 #include <vtkActor2D.h>
37 #include <vtkTextProperty.h>
38 #include <vtkPointData.h>
39 #include <vtkProperty2D.h>
40 #include <vtkRenderer.h>
41 #include <vtkUnstructuredGrid.h>
42 #include <vtkCellData.h>
44 vtkStandardNewMacro(SMESH_CellLabelActor);
49 SMESH_CellLabelActor::SMESH_CellLabelActor() {
50 //Definition of cells numbering pipeline
51 //---------------------------------------
52 myCellsNumDataSet = vtkUnstructuredGrid::New();
54 myCellCenters = VTKViewer_CellCenters::New();
55 myCellCenters->SetInput(myCellsNumDataSet);
57 myClsMaskPoints = vtkMaskPoints::New();
58 myClsMaskPoints->SetInput(myCellCenters->GetOutput());
59 myClsMaskPoints->SetOnRatio(1);
61 myClsSelectVisiblePoints = vtkSelectVisiblePoints::New();
62 myClsSelectVisiblePoints->SetInput(myClsMaskPoints->GetOutput());
63 myClsSelectVisiblePoints->SelectInvisibleOff();
64 myClsSelectVisiblePoints->SetTolerance(0.1);
66 myClsLabeledDataMapper = vtkLabeledDataMapper::New();
67 myClsLabeledDataMapper->SetInput(myClsSelectVisiblePoints->GetOutput());
69 myClsLabeledDataMapper->SetLabelFormat("%d");
70 myClsLabeledDataMapper->SetLabelModeToLabelScalars();
72 vtkTextProperty* aClsTextProp = vtkTextProperty::New();
73 aClsTextProp->SetFontFamilyToTimes();
74 static int aCellsFontSize = 12;
75 aClsTextProp->SetFontSize(aCellsFontSize);
76 aClsTextProp->SetBold(1);
77 aClsTextProp->SetItalic(0);
78 aClsTextProp->SetShadow(0);
79 myClsLabeledDataMapper->SetLabelTextProperty(aClsTextProp);
80 aClsTextProp->Delete();
82 myIsCellsLabeled = false;
84 myCellsLabels = vtkActor2D::New();
85 myCellsLabels->SetMapper(myClsLabeledDataMapper);
86 myCellsLabels->GetProperty()->SetColor(0,1,0);
87 myCellsLabels->SetVisibility(myIsCellsLabeled);
89 vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
90 callBackCommand->SetClientData(this);
91 callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
93 myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
95 callBackCommand->Delete();
102 SMESH_CellLabelActor::~SMESH_CellLabelActor() {
103 //Deleting of cells numbering pipeline
104 //---------------------------------------
105 myCellsNumDataSet->Delete();
107 myClsLabeledDataMapper->RemoveAllInputs();
108 myClsLabeledDataMapper->Delete();
110 // commented: porting to vtk 5.0
111 // myClsSelectVisiblePoints->UnRegisterAllOutputs();
112 myClsSelectVisiblePoints->Delete();
114 // commented: porting to vtk 5.0
115 // myClsMaskPoints->UnRegisterAllOutputs();
116 myClsMaskPoints->Delete();
118 // commented: porting to vtk 5.0
119 // myCellCenters->UnRegisterAllOutputs();
120 myCellCenters->Delete();
122 myCellsLabels->Delete();
126 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled) {
127 myTransformFilter->Update();
128 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
132 myIsCellsLabeled = theIsCellsLabeled && aGrid->GetNumberOfPoints();
133 if(myIsCellsLabeled){
134 myCellsNumDataSet->ShallowCopy(aGrid);
135 vtkUnstructuredGrid *aDataSet = myCellsNumDataSet;
136 int aNbElem = aDataSet->GetNumberOfCells();
137 vtkIntArray *anArray = vtkIntArray::New();
138 anArray->SetNumberOfValues(aNbElem);
139 for(int anId = 0; anId < aNbElem; anId++){
140 int aSMDSId = myVisualObj->GetElemObjId(anId);
141 anArray->SetValue(anId,aSMDSId);
143 aDataSet->GetCellData()->SetScalars(anArray);
144 myCellCenters->SetInput(aDataSet);
145 myCellsLabels->SetVisibility(GetVisibility());
147 myCellsLabels->SetVisibility(false);
151 void SMESH_CellLabelActor::SetVisibility(int theMode)
153 SMESH_DeviceActor::SetVisibility(theMode);
154 myCellsLabels->VisibilityOff();
155 if(myIsCellsLabeled && theMode)
156 myCellsLabels->VisibilityOn();
159 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
161 SMESH_DeviceActor::AddToRender(theRenderer);
162 myClsSelectVisiblePoints->SetRenderer(theRenderer);
163 theRenderer->AddActor2D(myCellsLabels);
166 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
168 theRenderer->RemoveActor(myCellsLabels);
169 SMESH_DeviceActor::RemoveFromRender(theRenderer);
172 void SMESH_CellLabelActor::UpdateLabels() {
174 SetCellsLabeled(myIsCellsLabeled);
178 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
179 unsigned long theEvent,
181 void* vtkNotUsed(theCallData)) {
182 SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
184 self->UpdateLabels();