Salome HOME
21948: EDF SMESH : Memory is not freed when deleting a mesh
[modules/smesh.git] / src / OBJECT / SMESH_CellLabelActor.cxx
1 // Copyright (C) 2007-2012  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->SetInput(myCellsNumDataSet);
56
57   myClsMaskPoints = vtkMaskPoints::New();
58   myClsMaskPoints->SetInput(myCellCenters->GetOutput());
59   myClsMaskPoints->SetOnRatio(1);
60     
61   myClsSelectVisiblePoints = vtkSelectVisiblePoints::New();
62   myClsSelectVisiblePoints->SetInput(myClsMaskPoints->GetOutput());
63   myClsSelectVisiblePoints->SelectInvisibleOff();
64   myClsSelectVisiblePoints->SetTolerance(0.1);
65     
66   myClsLabeledDataMapper = vtkLabeledDataMapper::New();
67   myClsLabeledDataMapper->SetInput(myClsSelectVisiblePoints->GetOutput());
68
69   myClsLabeledDataMapper->SetLabelFormat("%d");
70   myClsLabeledDataMapper->SetLabelModeToLabelScalars();
71     
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();
81     
82   myIsCellsLabeled = false;
83
84   myCellsLabels = vtkActor2D::New();
85   myCellsLabels->SetMapper(myClsLabeledDataMapper);
86   myCellsLabels->GetProperty()->SetColor(0,1,0);
87   myCellsLabels->SetVisibility(myIsCellsLabeled);
88
89   vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
90   callBackCommand->SetClientData(this);
91   callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
92
93   myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
94                                  callBackCommand);
95   callBackCommand->Delete();
96 }
97
98
99 /*!
100   Destructor.
101 */
102 SMESH_CellLabelActor::~SMESH_CellLabelActor() {
103   //Deleting of cells numbering pipeline
104   //---------------------------------------
105   myCellsNumDataSet->Delete();
106   myCellsLabels->Delete();
107   // commented: porting to vtk 5.0
108   //  myClsMaskPoints->UnRegisterAllOutputs();
109   myClsMaskPoints->Delete();
110   // commented: porting to vtk 5.0
111   //  myCellCenters->UnRegisterAllOutputs();
112   myCellCenters->Delete();
113
114   myClsLabeledDataMapper->RemoveAllInputs();
115   myClsLabeledDataMapper->Delete();
116   // commented: porting to vtk 5.0
117   //  myClsSelectVisiblePoints->UnRegisterAllOutputs();
118   myClsSelectVisiblePoints->Delete();
119   
120   
121   
122 }
123
124
125 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled) {
126   myTransformFilter->Update();
127   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
128   if(!aGrid)
129     return;
130
131   myIsCellsLabeled = theIsCellsLabeled && aGrid->GetNumberOfPoints();
132   if(myIsCellsLabeled){
133     myCellsNumDataSet->ShallowCopy(aGrid);
134     vtkUnstructuredGrid *aDataSet = myCellsNumDataSet;
135     int aNbElem = aDataSet->GetNumberOfCells();
136     vtkIntArray *anArray = vtkIntArray::New();
137     anArray->SetNumberOfValues(aNbElem);
138     for(int anId = 0; anId < aNbElem; anId++){
139       int aSMDSId = myVisualObj->GetElemObjId(anId);
140       anArray->SetValue(anId,aSMDSId);
141     }
142     aDataSet->GetCellData()->SetScalars(anArray);
143     myCellCenters->SetInput(aDataSet);
144     myCellsLabels->SetVisibility(GetVisibility());
145   }else{
146     myCellsLabels->SetVisibility(false);
147   }
148 }
149
150 void SMESH_CellLabelActor::SetVisibility(int theMode)
151 {
152   SMESH_DeviceActor::SetVisibility(theMode);
153   myCellsLabels->VisibilityOff();
154   if(myIsCellsLabeled && theMode)
155     myCellsLabels->VisibilityOn();
156 }
157
158 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
159 {
160   SMESH_DeviceActor::AddToRender(theRenderer);
161   myClsSelectVisiblePoints->SetRenderer(theRenderer);
162   theRenderer->AddActor2D(myCellsLabels);
163 }
164
165 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
166 {
167   theRenderer->RemoveActor(myCellsLabels);
168   SMESH_DeviceActor::RemoveFromRender(theRenderer);
169 }
170
171 void SMESH_CellLabelActor::UpdateLabels() {
172   if(myIsCellsLabeled)
173     SetCellsLabeled(myIsCellsLabeled);
174 }
175
176
177 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
178                                          unsigned long theEvent,
179                                          void* theClientData,
180                                          void* vtkNotUsed(theCallData)) {
181   SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
182   if(self)
183     self->UpdateLabels();
184 }