1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESH_CellLabelActor.cxx
23 // Author : Roman NIKOLAEV
26 #include "SMESH_CellLabelActor.h"
28 #include "SMESH_ExtractGeometry.h"
30 #include <VTKViewer_TransformFilter.h>
31 #include <VTKViewer_CellCenters.h>
32 #include <VTKViewer_ExtractUnstructuredGrid.h>
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>
47 vtkStandardNewMacro(SMESH_CellLabelActor);
52 SMESH_CellLabelActor::SMESH_CellLabelActor()
54 //Definition of cells numbering pipeline
55 //---------------------------------------
56 myCellsNumDataSet = vtkUnstructuredGrid::New();
58 myCellCenters = VTKViewer_CellCenters::New();
59 myCellCenters->SetInputData(myCellsNumDataSet);
61 myClsMaskPoints = vtkMaskPoints::New();
62 myClsMaskPoints->SetInputConnection(myCellCenters->GetOutputPort());
63 myClsMaskPoints->SetOnRatio(1);
65 myClsSelectVisiblePoints = vtkSelectVisiblePoints::New();
66 myClsSelectVisiblePoints->SetInputConnection(myClsMaskPoints->GetOutputPort());
67 myClsSelectVisiblePoints->SelectInvisibleOff();
68 myClsSelectVisiblePoints->SetTolerance(0.1);
70 myClsLabeledDataMapper = vtkLabeledDataMapper::New();
71 myClsLabeledDataMapper->SetInputConnection(myClsSelectVisiblePoints->GetOutputPort());
73 myClsLabeledDataMapper->SetLabelFormat("%d");
74 myClsLabeledDataMapper->SetLabelModeToLabelScalars();
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);
85 myIsCellsLabeled = false;
87 myCellsLabels = vtkActor2D::New();
88 myCellsLabels->SetMapper(myClsLabeledDataMapper);
89 myCellsLabels->SetVisibility(myIsCellsLabeled);
91 vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
92 callBackCommand->SetClientData(this);
93 callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
95 myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
97 callBackCommand->Delete();
104 SMESH_CellLabelActor::~SMESH_CellLabelActor()
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();
117 myClsLabeledDataMapper->RemoveAllInputs();
118 myClsLabeledDataMapper->Delete();
119 // commented: porting to vtk 5.0
120 // myClsSelectVisiblePoints->UnRegisterAllOutputs();
121 myClsSelectVisiblePoints->Delete();
122 myClsTextProp->Delete();
126 void SMESH_CellLabelActor::SetFontProperties( SMESH::LabelFont family, int size,
127 bool bold, bool italic, bool shadow,
128 double r, double g, double b )
131 case SMESH::FntArial:
132 myClsTextProp->SetFontFamilyToArial(); break;
133 case SMESH::FntCourier:
134 myClsTextProp->SetFontFamilyToCourier(); break;
135 case SMESH::FntTimes:
137 myClsTextProp->SetFontFamilyToTimes(); break;
139 myClsTextProp->SetFontSize( size );
140 myClsTextProp->SetBold( bold );
141 myClsTextProp->SetItalic( italic );
142 myClsTextProp->SetShadow( shadow );
143 myClsTextProp->SetColor( r, g, b );
146 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled)
148 myIsCellsLabeled = theIsCellsLabeled;
150 myCellsLabels->SetVisibility(false);
152 myTransformFilter->Update();
153 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
155 if ( myIsCellsLabeled && aGrid )
157 myCellsNumDataSet->ShallowCopy(aGrid);
158 vtkUnstructuredGrid *aDataSet = myCellsNumDataSet;
159 int aNbElem = aDataSet->GetNumberOfCells();
160 vtkIntArray *anArray = vtkIntArray::New();
161 anArray->SetNumberOfValues(aNbElem);
162 myExtractUnstructuredGrid->BuildOut2InMap();
163 for(int anId = 0; anId < aNbElem; anId++)
166 if(IsImplicitFunctionUsed())
167 id = myExtractGeometry->GetElemObjId(id);
168 id = myExtractUnstructuredGrid->GetInputId(id);
169 id = (id >=0) ? id : anId;
170 int aSMDSId = myVisualObj->GetElemObjId(id);
171 anArray->SetValue(anId,aSMDSId);
173 aDataSet->GetCellData()->SetScalars(anArray);
174 myCellCenters->SetInputData(aDataSet);
175 myCellsLabels->SetVisibility(GetVisibility());
179 void SMESH_CellLabelActor::SetVisibility(int theMode)
181 SMESH_DeviceActor::SetVisibility(theMode);
182 myCellsLabels->VisibilityOff();
183 if(myIsCellsLabeled && theMode)
184 myCellsLabels->VisibilityOn();
187 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
189 SMESH_DeviceActor::AddToRender(theRenderer);
190 myClsSelectVisiblePoints->SetRenderer(theRenderer);
191 theRenderer->AddActor2D(myCellsLabels);
194 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
196 theRenderer->RemoveActor(myCellsLabels);
197 SMESH_DeviceActor::RemoveFromRender(theRenderer);
200 void SMESH_CellLabelActor::UpdateLabels()
203 SetCellsLabeled(myIsCellsLabeled);
207 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
208 unsigned long theEvent,
210 void* vtkNotUsed(theCallData))
212 SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
214 self->UpdateLabels();