1 // Copyright (C) 2007-2021 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>
48 vtkStandardNewMacro(SMESH_CellLabelActor)
53 SMESH_CellLabelActor::SMESH_CellLabelActor()
55 //Definition of cells numbering pipeline
56 //---------------------------------------
57 myCellsNumDataSet = vtkUnstructuredGrid::New();
59 myCellCenters = VTKViewer_CellCenters::New();
60 myCellCenters->SetInputData(myCellsNumDataSet);
62 myClsMaskPoints = vtkMaskPoints::New();
63 myClsMaskPoints->SetInputConnection(myCellCenters->GetOutputPort());
64 myClsMaskPoints->SetOnRatio(1);
66 myClsSelectVisiblePoints = vtkSelectVisiblePoints::New();
67 myClsSelectVisiblePoints->SetInputConnection(myClsMaskPoints->GetOutputPort());
68 myClsSelectVisiblePoints->SelectInvisibleOff();
69 myClsSelectVisiblePoints->SetTolerance(0.1);
71 myClsLabeledDataMapper = vtkLabeledDataMapper::New();
72 myClsLabeledDataMapper->SetInputConnection(myClsSelectVisiblePoints->GetOutputPort());
74 //myClsLabeledDataMapper->SetLabelFormat("%d");
75 myClsLabeledDataMapper->SetLabelModeToLabelScalars();
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);
86 myIsCellsLabeled = false;
88 myCellsLabels = vtkActor2D::New();
89 myCellsLabels->SetMapper(myClsLabeledDataMapper);
90 myCellsLabels->SetVisibility(myIsCellsLabeled);
92 vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
93 callBackCommand->SetClientData(this);
94 callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
96 myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
98 callBackCommand->Delete();
105 SMESH_CellLabelActor::~SMESH_CellLabelActor()
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();
118 myClsLabeledDataMapper->RemoveAllInputs();
119 myClsLabeledDataMapper->Delete();
120 // commented: porting to vtk 5.0
121 // myClsSelectVisiblePoints->UnRegisterAllOutputs();
122 myClsSelectVisiblePoints->Delete();
123 myClsTextProp->Delete();
127 void SMESH_CellLabelActor::SetFontProperties( SMESH::LabelFont family, int size,
128 bool bold, bool italic, bool shadow,
129 double r, double g, double b )
132 case SMESH::FntArial:
133 myClsTextProp->SetFontFamilyToArial(); break;
134 case SMESH::FntCourier:
135 myClsTextProp->SetFontFamilyToCourier(); break;
136 case SMESH::FntTimes:
138 myClsTextProp->SetFontFamilyToTimes(); break;
140 myClsTextProp->SetFontSize( size );
141 myClsTextProp->SetBold( bold );
142 myClsTextProp->SetItalic( italic );
143 myClsTextProp->SetShadow( shadow );
144 myClsTextProp->SetColor( r, g, b );
147 void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled)
149 myIsCellsLabeled = theIsCellsLabeled;
151 myCellsLabels->SetVisibility(false);
153 myTransformFilter->Update();
154 vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
156 if ( myIsCellsLabeled && aGrid )
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++)
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);
174 aDataSet->GetCellData()->SetScalars(anArray);
175 myCellCenters->SetInputData(aDataSet);
176 myCellsLabels->SetVisibility(GetVisibility());
180 void SMESH_CellLabelActor::SetVisibility(int theMode)
182 SMESH_DeviceActor::SetVisibility(theMode);
183 myCellsLabels->VisibilityOff();
184 if(myIsCellsLabeled && theMode)
185 myCellsLabels->VisibilityOn();
188 void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
190 SMESH_DeviceActor::AddToRender(theRenderer);
191 myClsSelectVisiblePoints->SetRenderer(theRenderer);
192 theRenderer->AddActor2D(myCellsLabels);
195 void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
197 theRenderer->RemoveActor(myCellsLabels);
198 SMESH_DeviceActor::RemoveFromRender(theRenderer);
201 void SMESH_CellLabelActor::UpdateLabels()
204 SetCellsLabeled(myIsCellsLabeled);
208 void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
209 unsigned long /*theEvent*/,
211 void* vtkNotUsed(theCallData))
213 SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
215 self->UpdateLabels();