1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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>
30 #include <VTKViewer_ExtractUnstructuredGrid.h>
32 #include <vtkObjectFactory.h>
33 #include <vtkCallbackCommand.h>
34 #include <vtkMaskPoints.h>
35 #include <vtkSelectVisiblePoints.h>
36 #include <vtkLabeledDataMapper.h>
37 #include <vtkActor2D.h>
38 #include <vtkTextProperty.h>
39 #include <vtkPointData.h>
40 #include <vtkProperty2D.h>
41 #include <vtkRenderer.h>
42 #include <vtkUnstructuredGrid.h>
43 #include <vtkCellData.h>
45 vtkStandardNewMacro(SMESH_CellLabelActor);
50 SMESH_CellLabelActor::SMESH_CellLabelActor() {
51 //Definition of cells numbering pipeline
52 //---------------------------------------
53 myCellsNumDataSet = vtkUnstructuredGrid::New();
55 myCellCenters = VTKViewer_CellCenters::New();
56 myCellCenters->SetInputData(myCellsNumDataSet);
58 myClsMaskPoints = vtkMaskPoints::New();
59 myClsMaskPoints->SetInputConnection(myCellCenters->GetOutputPort());
60 myClsMaskPoints->SetOnRatio(1);
62 myClsSelectVisiblePoints = vtkSelectVisiblePoints::New();
63 myClsSelectVisiblePoints->SetInputConnection(myClsMaskPoints->GetOutputPort());
64 myClsSelectVisiblePoints->SelectInvisibleOff();
65 myClsSelectVisiblePoints->SetTolerance(0.1);
67 myClsLabeledDataMapper = vtkLabeledDataMapper::New();
68 myClsLabeledDataMapper->SetInputConnection(myClsSelectVisiblePoints->GetOutputPort());
70 myClsLabeledDataMapper->SetLabelFormat("%d");
71 myClsLabeledDataMapper->SetLabelModeToLabelScalars();
73 myClsTextProp = vtkTextProperty::New();
74 myClsTextProp->SetFontFamilyToTimes();
75 myClsTextProp->SetFontSize(12);
76 myClsTextProp->SetBold(1);
77 myClsTextProp->SetItalic(0);
78 myClsTextProp->SetShadow(0);
79 myClsTextProp->SetColor( 0, 1, 0 );
80 myClsLabeledDataMapper->SetLabelTextProperty(myClsTextProp);
82 myIsCellsLabeled = false;
84 myCellsLabels = vtkActor2D::New();
85 myCellsLabels->SetMapper(myClsLabeledDataMapper);
86 myCellsLabels->SetVisibility(myIsCellsLabeled);
88 vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
89 callBackCommand->SetClientData(this);
90 callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
92 myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
94 callBackCommand->Delete();
101 SMESH_CellLabelActor::~SMESH_CellLabelActor() {
102 //Deleting of cells numbering pipeline
103 //---------------------------------------
104 myCellsNumDataSet->Delete();
105 myCellsLabels->Delete();
106 // commented: porting to vtk 5.0
107 // myClsMaskPoints->UnRegisterAllOutputs();
108 myClsMaskPoints->Delete();
109 // commented: porting to vtk 5.0
110 // myCellCenters->UnRegisterAllOutputs();
111 myCellCenters->Delete();
113 myClsLabeledDataMapper->RemoveAllInputs();
114 myClsLabeledDataMapper->Delete();
115 // commented: porting to vtk 5.0
116 // myClsSelectVisiblePoints->UnRegisterAllOutputs();
117 myClsSelectVisiblePoints->Delete();
118 myClsTextProp->Delete();
122 void SMESH_CellLabelActor::SetFontProperties( SMESH::LabelFont family, int size,
123 bool bold, bool italic, bool shadow,
124 double r, double g, double b )
127 case SMESH::FntArial:
128 myClsTextProp->SetFontFamilyToArial(); break;
129 case SMESH::FntCourier:
130 myClsTextProp->SetFontFamilyToCourier(); break;
131 case SMESH::FntTimes:
133 myClsTextProp->SetFontFamilyToTimes(); break;
135 myClsTextProp->SetFontSize( size );
136 myClsTextProp->SetBold( bold );
137 myClsTextProp->SetItalic( italic );
138 myClsTextProp->SetShadow( shadow );
139 myClsTextProp->SetColor( r, g, b );
142 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled) {
143 myTransformFilter->Update();
144 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
148 myIsCellsLabeled = theIsCellsLabeled && aGrid->GetNumberOfPoints();
149 if(myIsCellsLabeled){
150 myCellsNumDataSet->ShallowCopy(aGrid);
151 vtkUnstructuredGrid *aDataSet = myCellsNumDataSet;
152 int aNbElem = aDataSet->GetNumberOfCells();
153 vtkIntArray *anArray = vtkIntArray::New();
154 anArray->SetNumberOfValues(aNbElem);
155 for(int anId = 0; anId < aNbElem; anId++){
156 vtkIdType id = myExtractUnstructuredGrid->GetInputId(anId);
157 id = (id >=0) ? id : anId;
158 int aSMDSId = myVisualObj->GetElemObjId(id);
159 anArray->SetValue(anId,aSMDSId);
161 aDataSet->GetCellData()->SetScalars(anArray);
162 myCellCenters->SetInputData(aDataSet);
163 myCellsLabels->SetVisibility(GetVisibility());
165 myCellsLabels->SetVisibility(false);
169 void SMESH_CellLabelActor::SetVisibility(int theMode)
171 SMESH_DeviceActor::SetVisibility(theMode);
172 myCellsLabels->VisibilityOff();
173 if(myIsCellsLabeled && theMode)
174 myCellsLabels->VisibilityOn();
177 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
179 SMESH_DeviceActor::AddToRender(theRenderer);
180 myClsSelectVisiblePoints->SetRenderer(theRenderer);
181 theRenderer->AddActor2D(myCellsLabels);
184 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
186 theRenderer->RemoveActor(myCellsLabels);
187 SMESH_DeviceActor::RemoveFromRender(theRenderer);
190 void SMESH_CellLabelActor::UpdateLabels() {
192 SetCellsLabeled(myIsCellsLabeled);
196 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
197 unsigned long theEvent,
199 void* vtkNotUsed(theCallData)) {
200 SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
202 self->UpdateLabels();