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