Salome HOME
0172da00b49ebf2a048accfd735cf504e61fa84d
[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   myTransformFilter->Update();
149   vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
150   if ( !aGrid )
151     return;
152
153   myIsCellsLabeled = theIsCellsLabeled && aGrid->GetNumberOfPoints();
154   if ( myIsCellsLabeled )
155   {
156     myCellsNumDataSet->ShallowCopy(aGrid);
157     vtkUnstructuredGrid *aDataSet = myCellsNumDataSet;
158     int aNbElem = aDataSet->GetNumberOfCells();
159     vtkIntArray *anArray = vtkIntArray::New();
160     anArray->SetNumberOfValues(aNbElem);
161     myExtractUnstructuredGrid->BuildOut2InMap();
162     for(int anId = 0; anId < aNbElem; anId++)
163     {
164       vtkIdType id = anId;
165       if(IsImplicitFunctionUsed())
166         id = myExtractGeometry->GetElemObjId(id);
167       id = myExtractUnstructuredGrid->GetInputId(id);
168       id = (id >=0) ? id : anId;
169       int aSMDSId = myVisualObj->GetElemObjId(id);
170       anArray->SetValue(anId,aSMDSId);
171     }
172     aDataSet->GetCellData()->SetScalars(anArray);
173     myCellCenters->SetInputData(aDataSet);
174     myCellsLabels->SetVisibility(GetVisibility());
175   }else{
176     myCellsLabels->SetVisibility(false);
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 }