Salome HOME
Fix for the bug "21427: EDF 2024 SMESH: numbering does not take into account clipping".
[modules/smesh.git] / src / OBJECT / SMESH_NodeLabelActor.cxx
1 // Copyright (C) 2007-2011  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_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 <vtkPolyData.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 = vtkPolyData::New();
51
52   myPtsMaskPoints = vtkMaskPoints::New();
53   myPtsMaskPoints->SetInput(myPointsNumDataSet);
54   myPtsMaskPoints->SetOnRatio(1);
55
56   myPtsSelectVisiblePoints = vtkSelectVisiblePoints::New();
57   myPtsSelectVisiblePoints->SetInput(myPtsMaskPoints->GetOutput());
58   myPtsSelectVisiblePoints->SelectInvisibleOff();
59   myPtsSelectVisiblePoints->SetTolerance(0.1);
60     
61   myPtsLabeledDataMapper = vtkLabeledDataMapper::New();
62   myPtsLabeledDataMapper->SetInput(myPtsSelectVisiblePoints->GetOutput());
63   myPtsLabeledDataMapper->SetLabelFormat("%d");
64   myPtsLabeledDataMapper->SetLabelModeToLabelScalars();
65     
66   vtkTextProperty* aPtsTextProp = vtkTextProperty::New();
67   aPtsTextProp->SetFontFamilyToTimes();
68   static int aPointsFontSize = 10;
69   aPtsTextProp->SetFontSize(aPointsFontSize);
70   aPtsTextProp->SetBold(1);
71   aPtsTextProp->SetItalic(0);
72   aPtsTextProp->SetShadow(0);
73   myPtsLabeledDataMapper->SetLabelTextProperty(aPtsTextProp);
74   aPtsTextProp->Delete();
75
76   myIsPointsLabeled = false;
77
78   myPointLabels = vtkActor2D::New();
79   myPointLabels->SetMapper(myPtsLabeledDataMapper);
80   myPointLabels->GetProperty()->SetColor(1,1,1);
81   myPointLabels->SetVisibility(myIsPointsLabeled);
82
83   vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
84   callBackCommand->SetClientData(this);
85   callBackCommand->SetCallback(SMESH_NodeLabelActor::ProcessEvents);
86
87   myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
88                                  callBackCommand);
89   callBackCommand->Delete();
90 }
91
92 /*!
93   Destructor
94 */
95 SMESH_NodeLabelActor::~SMESH_NodeLabelActor() {
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
113 }
114
115 void SMESH_NodeLabelActor::SetPointsLabeled(bool theIsPointsLabeled) {
116   myTransformFilter->Update();
117   vtkDataSet* aGrid = vtkPolyData::SafeDownCast(myTransformFilter->GetOutput());
118
119   if(!aGrid)
120     return;
121     
122   myIsPointsLabeled = theIsPointsLabeled && aGrid->GetNumberOfPoints();
123
124   if ( myIsPointsLabeled )
125   {
126     myPointsNumDataSet->ShallowCopy(aGrid);
127     vtkDataSet *aDataSet = myPointsNumDataSet;
128     
129     int aNbElem = aDataSet->GetNumberOfPoints();
130     
131     vtkIntArray *anArray = vtkIntArray::New();
132     anArray->SetNumberOfValues( aNbElem );
133     
134     for ( vtkIdType anId = 0; anId < aNbElem; anId++ )
135     {
136       int aSMDSId = GetNodeObjId( anId );
137       anArray->SetValue( anId, aSMDSId );
138     }
139     
140     aDataSet->GetPointData()->SetScalars( anArray );
141     myPtsMaskPoints->SetInput( aDataSet );
142     myPointLabels->SetVisibility( GetVisibility() );
143     anArray->Delete();
144   }
145   else
146   {
147     myPointLabels->SetVisibility( false );
148   } 
149 }
150
151
152 void SMESH_NodeLabelActor::SetVisibility(int theMode)
153 {
154   SMESH_DeviceActor::SetVisibility(theMode);
155   myPointLabels->VisibilityOff();
156   if(myIsPointsLabeled && theMode)
157     myPointLabels->VisibilityOn();
158 }
159
160
161 void SMESH_NodeLabelActor::AddToRender(vtkRenderer* theRenderer)
162 {
163   SMESH_DeviceActor::AddToRender(theRenderer);
164   myPtsSelectVisiblePoints->SetRenderer(theRenderer);
165   theRenderer->AddActor2D(myPointLabels);
166 }
167
168 void SMESH_NodeLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
169 {
170   theRenderer->RemoveActor(myPointLabels);
171   SMESH_DeviceActor::RemoveFromRender(theRenderer);
172 }
173
174 void SMESH_NodeLabelActor::UpdateLabels() {
175   if(myIsPointsLabeled)
176     SetPointsLabeled(myIsPointsLabeled);
177 }
178
179
180 void SMESH_NodeLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
181                                          unsigned long theEvent,
182                                          void* theClientData,
183                                          void* vtkNotUsed(theCallData)) {
184   SMESH_NodeLabelActor* self = reinterpret_cast<SMESH_NodeLabelActor*>(theClientData);    
185   if(self)
186     self->UpdateLabels();
187 }