Salome HOME
5bca6fc0a1e126666f4a4a406067ea0017bd102c
[modules/smesh.git] / src / OBJECT / SMESH_CellLabelActor.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 #include <VTKViewer_ExtractUnstructuredGrid.h>
31
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>
44
45 vtkStandardNewMacro(SMESH_CellLabelActor);
46
47 /*!
48   Constructor.
49 */
50 SMESH_CellLabelActor::SMESH_CellLabelActor() {
51     //Definition of cells numbering pipeline
52   //---------------------------------------
53   myCellsNumDataSet = vtkUnstructuredGrid::New();
54
55   myCellCenters = VTKViewer_CellCenters::New();
56   myCellCenters->SetInputData(myCellsNumDataSet);
57
58   myClsMaskPoints = vtkMaskPoints::New();
59   myClsMaskPoints->SetInputConnection(myCellCenters->GetOutputPort());
60   myClsMaskPoints->SetOnRatio(1);
61     
62   myClsSelectVisiblePoints = vtkSelectVisiblePoints::New();
63   myClsSelectVisiblePoints->SetInputConnection(myClsMaskPoints->GetOutputPort());
64   myClsSelectVisiblePoints->SelectInvisibleOff();
65   myClsSelectVisiblePoints->SetTolerance(0.1);
66     
67   myClsLabeledDataMapper = vtkLabeledDataMapper::New();
68   myClsLabeledDataMapper->SetInputConnection(myClsSelectVisiblePoints->GetOutputPort());
69
70   myClsLabeledDataMapper->SetLabelFormat("%d");
71   myClsLabeledDataMapper->SetLabelModeToLabelScalars();
72     
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);
81     
82   myIsCellsLabeled = false;
83
84   myCellsLabels = vtkActor2D::New();
85   myCellsLabels->SetMapper(myClsLabeledDataMapper);
86   myCellsLabels->SetVisibility(myIsCellsLabeled);
87
88   vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
89   callBackCommand->SetClientData(this);
90   callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
91
92   myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
93                                  callBackCommand);
94   callBackCommand->Delete();
95 }
96
97
98 /*!
99   Destructor.
100 */
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();
112
113   myClsLabeledDataMapper->RemoveAllInputs();
114   myClsLabeledDataMapper->Delete();
115   // commented: porting to vtk 5.0
116   //  myClsSelectVisiblePoints->UnRegisterAllOutputs();
117   myClsSelectVisiblePoints->Delete();
118   myClsTextProp->Delete();
119 }
120
121
122 void SMESH_CellLabelActor::SetFontProperties( SMESH::LabelFont family, int size,
123                                               bool bold, bool italic, bool shadow,
124                                               double r, double g, double b  )
125 {
126   switch ( family ) {
127   case SMESH::FntArial:
128     myClsTextProp->SetFontFamilyToArial(); break;
129   case SMESH::FntCourier:
130     myClsTextProp->SetFontFamilyToCourier(); break;
131   case SMESH::FntTimes:
132   default:
133     myClsTextProp->SetFontFamilyToTimes(); break;
134   }    
135   myClsTextProp->SetFontSize( size );
136   myClsTextProp->SetBold( bold );
137   myClsTextProp->SetItalic( italic );
138   myClsTextProp->SetShadow( shadow );
139   myClsTextProp->SetColor( r, g, b ); 
140 }
141
142 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled) {
143   myTransformFilter->Update();
144   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
145   if(!aGrid)
146     return;
147
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);
160     }
161     aDataSet->GetCellData()->SetScalars(anArray);
162     myCellCenters->SetInputData(aDataSet);
163     myCellsLabels->SetVisibility(GetVisibility());
164   }else{
165     myCellsLabels->SetVisibility(false);
166   }
167 }
168
169 void SMESH_CellLabelActor::SetVisibility(int theMode)
170 {
171   SMESH_DeviceActor::SetVisibility(theMode);
172   myCellsLabels->VisibilityOff();
173   if(myIsCellsLabeled && theMode)
174     myCellsLabels->VisibilityOn();
175 }
176
177 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
178 {
179   SMESH_DeviceActor::AddToRender(theRenderer);
180   myClsSelectVisiblePoints->SetRenderer(theRenderer);
181   theRenderer->AddActor2D(myCellsLabels);
182 }
183
184 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
185 {
186   theRenderer->RemoveActor(myCellsLabels);
187   SMESH_DeviceActor::RemoveFromRender(theRenderer);
188 }
189
190 void SMESH_CellLabelActor::UpdateLabels() {
191   if(myIsCellsLabeled)
192     SetCellsLabeled(myIsCellsLabeled);
193 }
194
195
196 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
197                                          unsigned long theEvent,
198                                          void* theClientData,
199                                          void* vtkNotUsed(theCallData)) {
200   SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
201   if(self)
202     self->UpdateLabels();
203 }