Salome HOME
Merge branch 'V8_4_BR'
[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 "SMESH_ExtractGeometry.h"
29
30 #include <VTKViewer_TransformFilter.h>
31 #include <VTKViewer_CellCenters.h>
32 #include <VTKViewer_ExtractUnstructuredGrid.h>
33
34 #include <vtkObjectFactory.h>
35 #include <vtkCallbackCommand.h>
36 #include <vtkMaskPoints.h>
37 #include <vtkSelectVisiblePoints.h>
38 #include <vtkLabeledDataMapper.h>
39 #include <vtkActor2D.h>
40 #include <vtkTextProperty.h>
41 #include <vtkPointData.h>
42 #include <vtkProperty2D.h>
43 #include <vtkRenderer.h>
44 #include <vtkUnstructuredGrid.h>
45 #include <vtkCellData.h>
46
47 vtkStandardNewMacro(SMESH_CellLabelActor);
48
49 /*!
50   Constructor.
51 */
52 SMESH_CellLabelActor::SMESH_CellLabelActor()
53 {
54   //Definition of cells numbering pipeline
55   //---------------------------------------
56   myCellsNumDataSet = vtkUnstructuredGrid::New();
57
58   myCellCenters = VTKViewer_CellCenters::New();
59   myCellCenters->SetInputData(myCellsNumDataSet);
60
61   myClsMaskPoints = vtkMaskPoints::New();
62   myClsMaskPoints->SetInputConnection(myCellCenters->GetOutputPort());
63   myClsMaskPoints->SetOnRatio(1);
64
65   myClsSelectVisiblePoints = vtkSelectVisiblePoints::New();
66   myClsSelectVisiblePoints->SetInputConnection(myClsMaskPoints->GetOutputPort());
67   myClsSelectVisiblePoints->SelectInvisibleOff();
68   myClsSelectVisiblePoints->SetTolerance(0.1);
69
70   myClsLabeledDataMapper = vtkLabeledDataMapper::New();
71   myClsLabeledDataMapper->SetInputConnection(myClsSelectVisiblePoints->GetOutputPort());
72
73   myClsLabeledDataMapper->SetLabelFormat("%d");
74   myClsLabeledDataMapper->SetLabelModeToLabelScalars();
75
76   myClsTextProp = vtkTextProperty::New();
77   myClsTextProp->SetFontFamilyToTimes();
78   myClsTextProp->SetFontSize(12);
79   myClsTextProp->SetBold(1);
80   myClsTextProp->SetItalic(0);
81   myClsTextProp->SetShadow(0);
82   myClsTextProp->SetColor( 0, 1, 0 );
83   myClsLabeledDataMapper->SetLabelTextProperty(myClsTextProp);
84     
85   myIsCellsLabeled = false;
86
87   myCellsLabels = vtkActor2D::New();
88   myCellsLabels->SetMapper(myClsLabeledDataMapper);
89   myCellsLabels->SetVisibility(myIsCellsLabeled);
90
91   vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
92   callBackCommand->SetClientData(this);
93   callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
94
95   myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
96                                  callBackCommand);
97   callBackCommand->Delete();
98 }
99
100
101 /*!
102   Destructor.
103 */
104 SMESH_CellLabelActor::~SMESH_CellLabelActor()
105 {
106   //Deleting of cells numbering pipeline
107   //---------------------------------------
108   myCellsNumDataSet->Delete();
109   myCellsLabels->Delete();
110   // commented: porting to vtk 5.0
111   //  myClsMaskPoints->UnRegisterAllOutputs();
112   myClsMaskPoints->Delete();
113   // commented: porting to vtk 5.0
114   //  myCellCenters->UnRegisterAllOutputs();
115   myCellCenters->Delete();
116
117   myClsLabeledDataMapper->RemoveAllInputs();
118   myClsLabeledDataMapper->Delete();
119   // commented: porting to vtk 5.0
120   //  myClsSelectVisiblePoints->UnRegisterAllOutputs();
121   myClsSelectVisiblePoints->Delete();
122   myClsTextProp->Delete();
123 }
124
125
126 void SMESH_CellLabelActor::SetFontProperties( SMESH::LabelFont family, int size,
127                                               bool bold, bool italic, bool shadow,
128                                               double r, double g, double b  )
129 {
130   switch ( family ) {
131   case SMESH::FntArial:
132     myClsTextProp->SetFontFamilyToArial(); break;
133   case SMESH::FntCourier:
134     myClsTextProp->SetFontFamilyToCourier(); break;
135   case SMESH::FntTimes:
136   default:
137     myClsTextProp->SetFontFamilyToTimes(); break;
138   }    
139   myClsTextProp->SetFontSize( size );
140   myClsTextProp->SetBold( bold );
141   myClsTextProp->SetItalic( italic );
142   myClsTextProp->SetShadow( shadow );
143   myClsTextProp->SetColor( r, g, b );
144 }
145
146 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled)
147 {
148   myIsCellsLabeled = theIsCellsLabeled;
149
150   myCellsLabels->SetVisibility(false);
151
152   myTransformFilter->Update();
153   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
154
155   if ( myIsCellsLabeled && aGrid )
156   {
157     myCellsNumDataSet->ShallowCopy(aGrid);
158     vtkUnstructuredGrid *aDataSet = myCellsNumDataSet;
159     int aNbElem = aDataSet->GetNumberOfCells();
160     vtkIntArray *anArray = vtkIntArray::New();
161     anArray->SetNumberOfValues(aNbElem);
162     myExtractUnstructuredGrid->BuildOut2InMap();
163     for(int anId = 0; anId < aNbElem; anId++)
164     {
165       vtkIdType id = anId;
166       if(IsImplicitFunctionUsed())
167         id = myExtractGeometry->GetElemObjId(id);
168       id = myExtractUnstructuredGrid->GetInputId(id);
169       id = (id >=0) ? id : anId;
170       int aSMDSId = myVisualObj->GetElemObjId(id);
171       anArray->SetValue(anId,aSMDSId);
172     }
173     aDataSet->GetCellData()->SetScalars(anArray);
174     myCellCenters->SetInputData(aDataSet);
175     myCellsLabels->SetVisibility(GetVisibility());
176   }
177 }
178
179 void SMESH_CellLabelActor::SetVisibility(int theMode)
180 {
181   SMESH_DeviceActor::SetVisibility(theMode);
182   myCellsLabels->VisibilityOff();
183   if(myIsCellsLabeled && theMode)
184     myCellsLabels->VisibilityOn();
185 }
186
187 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
188 {
189   SMESH_DeviceActor::AddToRender(theRenderer);
190   myClsSelectVisiblePoints->SetRenderer(theRenderer);
191   theRenderer->AddActor2D(myCellsLabels);
192 }
193
194 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
195 {
196   theRenderer->RemoveActor(myCellsLabels);
197   SMESH_DeviceActor::RemoveFromRender(theRenderer);
198 }
199
200 void SMESH_CellLabelActor::UpdateLabels()
201 {
202   if(myIsCellsLabeled)
203     SetCellsLabeled(myIsCellsLabeled);
204 }
205
206
207 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
208                                          unsigned long theEvent,
209                                          void* theClientData,
210                                          void* vtkNotUsed(theCallData))
211 {
212   SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
213   if(self)
214     self->UpdateLabels();
215 }