smesh/src/OBJECT/SMESH_CellLabelActor.cxx

217 lines
6.9 KiB
C++
Raw Normal View History

2023-06-20 02:11:37 +05:00
// Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE
2012-08-09 16:03:55 +06:00
//
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
2014-02-20 18:25:37 +06:00
// version 2.1 of the License, or (at your option) any later version.
2012-08-09 16:03:55 +06:00
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// File : SMESH_CellLabelActor.cxx
// Author : Roman NIKOLAEV
// Module : SMESH
//
#include "SMESH_CellLabelActor.h"
#include "SMESH_ExtractGeometry.h"
2012-08-09 16:03:55 +06:00
#include <VTKViewer_TransformFilter.h>
#include <VTKViewer_CellCenters.h>
#include <VTKViewer_ExtractUnstructuredGrid.h>
2012-08-09 16:03:55 +06:00
#include <vtkObjectFactory.h>
#include <vtkCallbackCommand.h>
#include <vtkMaskPoints.h>
#include <vtkSelectVisiblePoints.h>
#include <vtkLabeledDataMapper.h>
#include <vtkActor2D.h>
#include <vtkTextProperty.h>
#include <vtkPointData.h>
#include <vtkProperty2D.h>
#include <vtkRenderer.h>
#include <vtkUnstructuredGrid.h>
#include <vtkCellData.h>
#include <iostream>
2012-08-09 16:03:55 +06:00
2020-08-21 10:05:05 +05:00
vtkStandardNewMacro(SMESH_CellLabelActor)
2012-08-09 16:03:55 +06:00
/*!
Constructor.
*/
SMESH_CellLabelActor::SMESH_CellLabelActor()
{
//Definition of cells numbering pipeline
2012-08-09 16:03:55 +06:00
//---------------------------------------
myCellsNumDataSet = vtkUnstructuredGrid::New();
myCellCenters = VTKViewer_CellCenters::New();
2013-03-01 19:13:25 +06:00
myCellCenters->SetInputData(myCellsNumDataSet);
2012-08-09 16:03:55 +06:00
myClsMaskPoints = vtkMaskPoints::New();
2013-03-01 19:13:25 +06:00
myClsMaskPoints->SetInputConnection(myCellCenters->GetOutputPort());
2012-08-09 16:03:55 +06:00
myClsMaskPoints->SetOnRatio(1);
2012-08-09 16:03:55 +06:00
myClsSelectVisiblePoints = vtkSelectVisiblePoints::New();
2013-03-01 19:13:25 +06:00
myClsSelectVisiblePoints->SetInputConnection(myClsMaskPoints->GetOutputPort());
2012-08-09 16:03:55 +06:00
myClsSelectVisiblePoints->SelectInvisibleOff();
myClsSelectVisiblePoints->SetTolerance(0.1);
2012-08-09 16:03:55 +06:00
myClsLabeledDataMapper = vtkLabeledDataMapper::New();
2013-03-01 19:13:25 +06:00
myClsLabeledDataMapper->SetInputConnection(myClsSelectVisiblePoints->GetOutputPort());
2012-08-09 16:03:55 +06:00
//myClsLabeledDataMapper->SetLabelFormat("%d");
2012-08-09 16:03:55 +06:00
myClsLabeledDataMapper->SetLabelModeToLabelScalars();
2013-02-12 20:37:44 +06:00
myClsTextProp = vtkTextProperty::New();
myClsTextProp->SetFontFamilyToTimes();
myClsTextProp->SetFontSize(12);
myClsTextProp->SetBold(1);
myClsTextProp->SetItalic(0);
myClsTextProp->SetShadow(0);
myClsTextProp->SetColor( 0, 1, 0 );
myClsLabeledDataMapper->SetLabelTextProperty(myClsTextProp);
2012-08-09 16:03:55 +06:00
myIsCellsLabeled = false;
myCellsLabels = vtkActor2D::New();
myCellsLabels->SetMapper(myClsLabeledDataMapper);
myCellsLabels->SetVisibility(myIsCellsLabeled);
vtkCallbackCommand* callBackCommand = vtkCallbackCommand::New();
callBackCommand->SetClientData(this);
callBackCommand->SetCallback(SMESH_CellLabelActor::ProcessEvents);
myTransformFilter->AddObserver("VTKViewer_TransformFilter::TransformationFinished",
2013-02-12 20:37:44 +06:00
callBackCommand);
2012-08-09 16:03:55 +06:00
callBackCommand->Delete();
}
/*!
Destructor.
*/
SMESH_CellLabelActor::~SMESH_CellLabelActor()
{
2012-08-09 16:03:55 +06:00
//Deleting of cells numbering pipeline
//---------------------------------------
myCellsNumDataSet->Delete();
2013-02-12 20:37:44 +06:00
myCellsLabels->Delete();
2012-08-09 16:03:55 +06:00
// commented: porting to vtk 5.0
// myClsMaskPoints->UnRegisterAllOutputs();
myClsMaskPoints->Delete();
// commented: porting to vtk 5.0
// myCellCenters->UnRegisterAllOutputs();
myCellCenters->Delete();
2013-02-12 20:37:44 +06:00
myClsLabeledDataMapper->RemoveAllInputs();
myClsLabeledDataMapper->Delete();
// commented: porting to vtk 5.0
// myClsSelectVisiblePoints->UnRegisterAllOutputs();
myClsSelectVisiblePoints->Delete();
myClsTextProp->Delete();
2012-08-09 16:03:55 +06:00
}
2013-02-12 20:37:44 +06:00
void SMESH_CellLabelActor::SetFontProperties( SMESH::LabelFont family, int size,
bool bold, bool italic, bool shadow,
2013-03-01 19:13:25 +06:00
double r, double g, double b )
2013-02-12 20:37:44 +06:00
{
switch ( family ) {
case SMESH::FntArial:
myClsTextProp->SetFontFamilyToArial(); break;
case SMESH::FntCourier:
myClsTextProp->SetFontFamilyToCourier(); break;
case SMESH::FntTimes:
default:
myClsTextProp->SetFontFamilyToTimes(); break;
}
myClsTextProp->SetFontSize( size );
myClsTextProp->SetBold( bold );
myClsTextProp->SetItalic( italic );
myClsTextProp->SetShadow( shadow );
myClsTextProp->SetColor( r, g, b );
2013-02-12 20:37:44 +06:00
}
void SMESH_CellLabelActor::SetCellsLabeled(bool theIsCellsLabeled)
{
myIsCellsLabeled = theIsCellsLabeled;
myCellsLabels->SetVisibility(false);
2012-08-09 16:03:55 +06:00
myTransformFilter->Update();
vtkUnstructuredGrid* aGrid = vtkUnstructuredGrid::SafeDownCast(myTransformFilter->GetOutput());
if ( myIsCellsLabeled && aGrid )
{
2012-08-09 16:03:55 +06:00
myCellsNumDataSet->ShallowCopy(aGrid);
vtkUnstructuredGrid *aDataSet = myCellsNumDataSet;
vtkIdType aNbElem = aDataSet->GetNumberOfCells();
vtkIdTypeArray *anArray = vtkIdTypeArray::New();
2012-08-09 16:03:55 +06:00
anArray->SetNumberOfValues(aNbElem);
myExtractUnstructuredGrid->BuildOut2InMap();
for(vtkIdType anId = 0; anId < aNbElem; anId++)
{
vtkIdType id = anId;
if(IsImplicitFunctionUsed())
id = myExtractGeometry->GetElemObjId(id);
id = myExtractUnstructuredGrid->GetInputId(id);
id = (id >=0) ? id : anId;
vtkIdType aSMDSId = myVisualObj->GetElemObjId(id);
2012-08-09 16:03:55 +06:00
anArray->SetValue(anId,aSMDSId);
}
aDataSet->GetCellData()->SetScalars(anArray);
2013-03-01 19:13:25 +06:00
myCellCenters->SetInputData(aDataSet);
2012-08-09 16:03:55 +06:00
myCellsLabels->SetVisibility(GetVisibility());
}
}
void SMESH_CellLabelActor::SetVisibility(int theMode)
{
SMESH_DeviceActor::SetVisibility(theMode);
myCellsLabels->VisibilityOff();
if(myIsCellsLabeled && theMode)
myCellsLabels->VisibilityOn();
}
void SMESH_CellLabelActor::AddToRender(vtkRenderer* theRenderer)
{
SMESH_DeviceActor::AddToRender(theRenderer);
myClsSelectVisiblePoints->SetRenderer(theRenderer);
theRenderer->AddActor2D(myCellsLabels);
}
void SMESH_CellLabelActor::RemoveFromRender(vtkRenderer* theRenderer)
{
theRenderer->RemoveActor(myCellsLabels);
SMESH_DeviceActor::RemoveFromRender(theRenderer);
}
void SMESH_CellLabelActor::UpdateLabels()
{
2012-08-09 16:03:55 +06:00
if(myIsCellsLabeled)
SetCellsLabeled(myIsCellsLabeled);
}
void SMESH_CellLabelActor::ProcessEvents(vtkObject* vtkNotUsed(theObject),
2020-08-21 10:05:05 +05:00
unsigned long /*theEvent*/,
2013-02-12 20:37:44 +06:00
void* theClientData,
void* vtkNotUsed(theCallData))
{
2012-08-09 16:03:55 +06:00
SMESH_CellLabelActor* self = reinterpret_cast<SMESH_CellLabelActor*>(theClientData);
if(self)
self->UpdateLabels();
}