Here is a example about how to use vtkCommand to listen for user’s key press event and change cell scalar to get different cell color.
The project is based on VTK9.2

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(vtk92Lut)

add_executable(${PROJECT_NAME} "main.cpp" )

find_package( VTK COMPONENTS vtkCommonCore vtkRenderingCore vtkInteractionStyle RenderingCore
    RenderingOpenGL2 FiltersSources CommonColor)

if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
else ()
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ${PROJECT_NAME}
    MODULES ${VTK_LIBRARIES}
    )
endif ()

target_link_libraries( ${PROJECT_NAME} ${VTK_LIBRARIES} )

main.cpp

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCellData.h>
#include <vtkNamedColors.h>
#include <vtkColorTransferFunction.h>

#define vtkSPtr vtkSmartPointer
#define vtkSPtrNew(Var, Type) vtkSPtr<Type> Var = vtkSPtr<Type>::New();

using namespace std;

void InsertPlane(vtkSPtr<vtkCellArray> cells, std::vector<vtkIdType> ptIds)
{
    /*vtkIdType ids1[3] = { ptIds[0], ptIds[1], ptIds[2] };
    vtkIdType ids2[3] = { ptIds[0], ptIds[2], ptIds[3] };
    cells->InsertNextCell( 3, ids1 );
    cells->InsertNextCell( 3, ids2 );*/

    vtkIdType ids[4] = { ptIds[0], ptIds[1], ptIds[2], ptIds[3] };
    cells->InsertNextCell( 4, ids );
}

class vtkKeyPressCB : public vtkCommand
{
public:
    static vtkKeyPressCB *New()
    { return new vtkKeyPressCB; }
    void Execute(vtkObject *caller, unsigned long, void*) override
    {
        vtkRenderWindowInteractor *interactor =
          reinterpret_cast<vtkRenderWindowInteractor*>(caller);
        std::string str = interactor->GetKeySym();
        std::cout << "str: " << str << std::endl;
        if( str == "y" )
        {
            m_PolyData->GetCellData()->GetScalars()->SetTuple1(2, 0);
            m_PolyData->GetCellData()->Modified();
            m_RenderWin->Render();
        }
        else if(str == "n")
        {
            m_PolyData->GetCellData()->GetScalars()->SetTuple1(2, 2);
            m_PolyData->GetCellData()->Modified();
            m_RenderWin->Render();
        }
    }

    void Setm_RenderWin(vtkSPtr<vtkRenderWindow> win){ m_RenderWin = win; };
    void Setm_PolyData(vtkSPtr<vtkPolyData> polyData){ m_PolyData = polyData; };
    vtkKeyPressCB(){};
protected:
    vtkSPtr<vtkPolyData> m_PolyData;
    vtkSPtr<vtkRenderWindow> m_RenderWin;
};


int main()
{
    const int ptsCount = 8;
    double coords[ptsCount][3] = { {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0},
                            {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1} };

    vtkSPtrNew( pts, vtkPoints );
    for( int i = 0; i < ptsCount; ++i )
    {
        pts->InsertPoint( i, coords[i] );
    }

    vtkSPtrNew( cells, vtkCellArray );
    std::vector<vtkIdType> ids = { 0, 1, 2, 3 };
    InsertPlane( cells, ids );
    ids = { 4, 5, 6, 7 };
    InsertPlane( cells, ids );
    ids = { 0, 4, 7, 3 };
    InsertPlane( cells, ids );
    ids = { 1, 5, 6, 2 };
    InsertPlane( cells, ids );
    ids = { 7, 6, 2, 3 };
    InsertPlane( cells, ids );
    ids = { 0, 1, 5, 4 };
    InsertPlane( cells, ids );

    vtkSPtrNew( polyData, vtkPolyData );
    polyData->SetPoints( pts );
    polyData->SetPolys( cells );

    vtkSPtrNew( scalars, vtkIntArray );
    scalars->SetNumberOfTuples( ptsCount );
    for( int i = 0; i < 6; ++i )
    {
        scalars->SetTuple1( i, i );
    }
    polyData->GetCellData()->SetScalars( scalars );

    vtkSPtrNew( lut, vtkColorTransferFunction );
    lut->SetClamping( 0 );
    vtkSPtrNew( colors, vtkNamedColors );

    string colorNames[6] = { "Gray", "Blue", "Red", "Cyan", "Yellow", "Lime" };
    for( int i = 0; i < 6; ++i )
    {
        double color[3];
        colors->GetColorRGB( colorNames[i].c_str(), color );
        lut->AddRGBPoint( i, color[0], color[1], color[2] );
    }

    vtkSPtrNew( mapper, vtkPolyDataMapper );
    mapper->SetInputData( polyData );
    mapper->SetScalarModeToUseCellData();
    mapper->SetLookupTable( lut );
    mapper->SetScalarRange( 0, 5 );

    vtkSPtrNew( actor, vtkActor );
    actor->SetMapper( mapper );

    vtkSPtrNew( renderer, vtkRenderer );
    renderer->AddActor(actor);
    renderer->SetBackground( 0, 0, 0 );

    vtkSPtrNew( renderWindow, vtkRenderWindow );
    renderWindow->AddRenderer( renderer );

    vtkSPtrNew( renderWindowInteractor, vtkRenderWindowInteractor );
    renderWindowInteractor->SetRenderWindow( renderWindow );
    vtkSPtrNew( cbObj, vtkKeyPressCB );
    cbObj->Setm_PolyData( polyData );
    cbObj->Setm_RenderWin( renderWindow );
    renderWindowInteractor->AddObserver( vtkCommand::KeyPressEvent, cbObj );

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;
}

Demo:

Categories: VTK

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
0
Would love your thoughts, please comment.x
()
x