Salome HOME
Merge from V6_5_BR 05/06/2012
[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
107   myClsLabeledDataMapper->RemoveAllInputs();
108   myClsLabeledDataMapper->Delete();
109   
110   // commented: porting to vtk 5.0
111   //  myClsSelectVisiblePoints->UnRegisterAllOutputs();
112   myClsSelectVisiblePoints->Delete();
113   
114   // commented: porting to vtk 5.0
115   //  myClsMaskPoints->UnRegisterAllOutputs();
116   myClsMaskPoints->Delete();
117   
118   // commented: porting to vtk 5.0
119   //  myCellCenters->UnRegisterAllOutputs();
120   myCellCenters->Delete();
121   
122   myCellsLabels->Delete();
123 }
124
125
126 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled) {
127   myTransformFilter->Update();
128   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
129   if(!aGrid)
130     return;
131
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);
142     }
143     aDataSet->GetCellData()->SetScalars(anArray);
144     myCellCenters->SetInput(aDataSet);
145     myCellsLabels->SetVisibility(GetVisibility());
146   }else{
147     myCellsLabels->SetVisibility(false);
148   }
149 }
150
151 void SMESH_CellLabelActor::SetVisibility(int theMode)
152 {
153   SMESH_DeviceActor::SetVisibility(theMode);
154   myCellsLabels->VisibilityOff();
155   if(myIsCellsLabeled && theMode)
156     myCellsLabels->VisibilityOn();
157 }
158
159 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
160 {
161   SMESH_DeviceActor::AddToRender(theRenderer);
162   myClsSelectVisiblePoints->SetRenderer(theRenderer);
163   theRenderer->AddActor2D(myCellsLabels);
164 }
165
166 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
167 {
168   theRenderer->RemoveActor(myCellsLabels);
169   SMESH_DeviceActor::RemoveFromRender(theRenderer);
170 }
171
172 void SMESH_CellLabelActor::UpdateLabels() {
173   if(myIsCellsLabeled)
174     SetCellsLabeled(myIsCellsLabeled);
175 }
176
177
178 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
179                                          unsigned long theEvent,
180                                          void* theClientData,
181                                          void* vtkNotUsed(theCallData)) {
182   SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
183   if(self)
184     self->UpdateLabels();
185 }