Salome HOME
90d28e162d73ebae310393adcd0c33b62b6539f0
[modules/smesh.git] / src / OBJECT / SMESH_CellLabelActor.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SMESH_CellLabelActor.cxx
23 //  Author : Roman NIKOLAEV
24 //  Module : SMESH
25 //
26 #include "SMESH_CellLabelActor.h"
27
28 #include <VTKViewer_TransformFilter.h>
29 #include <VTKViewer_CellCenters.h>
30
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>
43
44 vtkStandardNewMacro(SMESH_CellLabelActor);
45
46 /*!
47   Constructor.
48 */
49 SMESH_CellLabelActor::SMESH_CellLabelActor() {
50     //Definition of cells numbering pipeline
51   //---------------------------------------
52   myCellsNumDataSet = vtkUnstructuredGrid::New();
53
54   myCellCenters = VTKViewer_CellCenters::New();
55   myCellCenters->SetInputData(myCellsNumDataSet);
56
57   myClsMaskPoints = vtkMaskPoints::New();
58   myClsMaskPoints->SetInputConnection(myCellCenters->GetOutputPort());
59   myClsMaskPoints->SetOnRatio(1);
60     
61   myClsSelectVisiblePoints = vtkSelectVisiblePoints::New();
62   myClsSelectVisiblePoints->SetInputConnection(myClsMaskPoints->GetOutputPort());
63   myClsSelectVisiblePoints->SelectInvisibleOff();
64   myClsSelectVisiblePoints->SetTolerance(0.1);
65     
66   myClsLabeledDataMapper = vtkLabeledDataMapper::New();
67   myClsLabeledDataMapper->SetInputConnection(myClsSelectVisiblePoints->GetOutputPort());
68
69   myClsLabeledDataMapper->SetLabelFormat("%d");
70   myClsLabeledDataMapper->SetLabelModeToLabelScalars();
71     
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);
80     
81   myIsCellsLabeled = false;
82
83   myCellsLabels = vtkActor2D::New();
84   myCellsLabels->SetMapper(myClsLabeledDataMapper);
85   myCellsLabels->SetVisibility(myIsCellsLabeled);
86
87   vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
88   callBackCommand->SetClientData(this);
89   callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
90
91   myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
92                                  callBackCommand);
93   callBackCommand->Delete();
94 }
95
96
97 /*!
98   Destructor.
99 */
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();
111
112   myClsLabeledDataMapper->RemoveAllInputs();
113   myClsLabeledDataMapper->Delete();
114   // commented: porting to vtk 5.0
115   //  myClsSelectVisiblePoints->UnRegisterAllOutputs();
116   myClsSelectVisiblePoints->Delete();
117   myClsTextProp->Delete();
118 }
119
120
121 void SMESH_CellLabelActor::SetFontProperties( SMESH::LabelFont family, int size,
122                                               bool bold, bool italic, bool shadow,
123                                               double r, double g, double b  )
124 {
125   switch ( family ) {
126   case SMESH::FntArial:
127     myClsTextProp->SetFontFamilyToArial(); break;
128   case SMESH::FntCourier:
129     myClsTextProp->SetFontFamilyToCourier(); break;
130   case SMESH::FntTimes:
131   default:
132     myClsTextProp->SetFontFamilyToTimes(); break;
133   }    
134   myClsTextProp->SetFontSize( size );
135   myClsTextProp->SetBold( bold );
136   myClsTextProp->SetItalic( italic );
137   myClsTextProp->SetShadow( shadow );
138   myClsTextProp->SetColor( r, g, b ); 
139 }
140
141 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled) {
142   myTransformFilter->Update();
143   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
144   if(!aGrid)
145     return;
146
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);
157     }
158     aDataSet->GetCellData()->SetScalars(anArray);
159     myCellCenters->SetInputData(aDataSet);
160     myCellsLabels->SetVisibility(GetVisibility());
161   }else{
162     myCellsLabels->SetVisibility(false);
163   }
164 }
165
166 void SMESH_CellLabelActor::SetVisibility(int theMode)
167 {
168   SMESH_DeviceActor::SetVisibility(theMode);
169   myCellsLabels->VisibilityOff();
170   if(myIsCellsLabeled && theMode)
171     myCellsLabels->VisibilityOn();
172 }
173
174 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
175 {
176   SMESH_DeviceActor::AddToRender(theRenderer);
177   myClsSelectVisiblePoints->SetRenderer(theRenderer);
178   theRenderer->AddActor2D(myCellsLabels);
179 }
180
181 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
182 {
183   theRenderer->RemoveActor(myCellsLabels);
184   SMESH_DeviceActor::RemoveFromRender(theRenderer);
185 }
186
187 void SMESH_CellLabelActor::UpdateLabels() {
188   if(myIsCellsLabeled)
189     SetCellsLabeled(myIsCellsLabeled);
190 }
191
192
193 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
194                                          unsigned long theEvent,
195                                          void* theClientData,
196                                          void* vtkNotUsed(theCallData)) {
197   SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
198   if(self)
199     self->UpdateLabels();
200 }