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