1 // Copyright (C) 2007-2013 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 myClsTextProp = vtkTextProperty::New();
73 myClsTextProp->SetFontFamilyToTimes();
74 myClsTextProp->SetFontSize(12);
75 myClsTextProp->SetBold(1);
76 myClsTextProp->SetItalic(0);
77 myClsTextProp->SetShadow(0);
78 myClsTextProp->SetColor( 0, 1, 0 );
79 myClsLabeledDataMapper->SetLabelTextProperty(myClsTextProp);
81 myIsCellsLabeled = false;
83 myCellsLabels = vtkActor2D::New();
84 myCellsLabels->SetMapper(myClsLabeledDataMapper);
85 myCellsLabels->SetVisibility(myIsCellsLabeled);
87 vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
88 callBackCommand->SetClientData(this);
89 callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
91 myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
93 callBackCommand->Delete();
100 SMESH_CellLabelActor::~SMESH_CellLabelActor() {
101 //Deleting of cells numbering pipeline
102 //---------------------------------------
103 myCellsNumDataSet->Delete();
104 myCellsLabels->Delete();
105 // commented: porting to vtk 5.0
106 // myClsMaskPoints->UnRegisterAllOutputs();
107 myClsMaskPoints->Delete();
108 // commented: porting to vtk 5.0
109 // myCellCenters->UnRegisterAllOutputs();
110 myCellCenters->Delete();
112 myClsLabeledDataMapper->RemoveAllInputs();
113 myClsLabeledDataMapper->Delete();
114 // commented: porting to vtk 5.0
115 // myClsSelectVisiblePoints->UnRegisterAllOutputs();
116 myClsSelectVisiblePoints->Delete();
117 myClsTextProp->Delete();
121 void SMESH_CellLabelActor::SetFontProperties( SMESH::LabelFont family, int size,
122 bool bold, bool italic, bool shadow,
123 vtkFloatingPointType r, vtkFloatingPointType g, vtkFloatingPointType b )
126 case SMESH::FntArial:
127 myClsTextProp->SetFontFamilyToArial(); break;
128 case SMESH::FntCourier:
129 myClsTextProp->SetFontFamilyToCourier(); break;
130 case SMESH::FntTimes:
132 myClsTextProp->SetFontFamilyToTimes(); break;
134 myClsTextProp->SetFontSize( size );
135 myClsTextProp->SetBold( bold );
136 myClsTextProp->SetItalic( italic );
137 myClsTextProp->SetShadow( shadow );
138 myClsTextProp->SetColor( r, g, b );
141 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled) {
142 myTransformFilter->Update();
143 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
147 myIsCellsLabeled = theIsCellsLabeled && aGrid->GetNumberOfPoints();
148 if(myIsCellsLabeled){
149 myCellsNumDataSet->ShallowCopy(aGrid);
150 vtkUnstructuredGrid *aDataSet = myCellsNumDataSet;
151 int aNbElem = aDataSet->GetNumberOfCells();
152 vtkIntArray *anArray = vtkIntArray::New();
153 anArray->SetNumberOfValues(aNbElem);
154 for(int anId = 0; anId < aNbElem; anId++){
155 int aSMDSId = myVisualObj->GetElemObjId(anId);
156 anArray->SetValue(anId,aSMDSId);
158 aDataSet->GetCellData()->SetScalars(anArray);
159 myCellCenters->SetInput(aDataSet);
160 myCellsLabels->SetVisibility(GetVisibility());
162 myCellsLabels->SetVisibility(false);
166 void SMESH_CellLabelActor::SetVisibility(int theMode)
168 SMESH_DeviceActor::SetVisibility(theMode);
169 myCellsLabels->VisibilityOff();
170 if(myIsCellsLabeled && theMode)
171 myCellsLabels->VisibilityOn();
174 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
176 SMESH_DeviceActor::AddToRender(theRenderer);
177 myClsSelectVisiblePoints->SetRenderer(theRenderer);
178 theRenderer->AddActor2D(myCellsLabels);
181 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
183 theRenderer->RemoveActor(myCellsLabels);
184 SMESH_DeviceActor::RemoveFromRender(theRenderer);
187 void SMESH_CellLabelActor::UpdateLabels() {
189 SetCellsLabeled(myIsCellsLabeled);
193 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
194 unsigned long theEvent,
196 void* vtkNotUsed(theCallData)) {
197 SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
199 self->UpdateLabels();