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