vtkImageFlip can be used to flip axis. So we can get mirror image, it’s so cool.

#include <vtkImageData.h>
#include <vtkImageCast.h>
#include <vtkSmartPointer.h>
#include <vtkImageMapper3D.h>
#include <vtkImageFlip.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageMandelbrotSource.h>
#include <vtkImageActor.h>
#include <vtkPNGReader.h>

int main(int, char *[])
{
    // Create an image
    vtkSmartPointer<vtkPNGReader> reader =
        vtkSmartPointer<vtkPNGReader>::New();
    reader->SetFileName ( "/Users/weiyang/Desktop/pic2.png" );
    reader->Update();

    vtkSmartPointer<vtkImageCast> castSource =
        vtkSmartPointer<vtkImageCast>::New();
    castSource->SetOutputScalarTypeToUnsignedChar();
    castSource->SetInputConnection( reader->GetOutputPort() );
    castSource->Update();

    vtkSmartPointer<vtkImageFlip> flipXFilter =
        vtkSmartPointer<vtkImageFlip>::New();
    flipXFilter->SetFilteredAxis( 0 ); // flip x axis
    flipXFilter->SetInputConnection( reader->GetOutputPort() );
    flipXFilter->Update();

    vtkSmartPointer<vtkImageCast> castXFilter =
        vtkSmartPointer<vtkImageCast>::New();
    castXFilter->SetOutputScalarTypeToUnsignedChar();
    castXFilter->SetInputConnection( flipXFilter->GetOutputPort() );
    castXFilter->Update();

    // Create actors
    vtkSmartPointer<vtkImageActor> inputActor =
        vtkSmartPointer<vtkImageActor>::New();
    inputActor->GetMapper()->SetInputConnection( castSource->GetOutputPort() );

    vtkSmartPointer<vtkImageActor> xActor =
        vtkSmartPointer<vtkImageActor>::New();
    xActor->GetMapper()->SetInputConnection( castXFilter->GetOutputPort() );

    // Define viewport ranges
    // (xmin, ymin, xmax, ymax)
    double inputViewport[4] = {0.0, 0.0, 0.5, 1.0};
    double xViewport[4] = {0.5, 0.0, 1.0, 1.0};

    // Setup renderers
    vtkSmartPointer<vtkRenderer> inputRenderer =
        vtkSmartPointer<vtkRenderer>::New();
    inputRenderer->SetViewport( inputViewport );
    inputRenderer->AddActor( inputActor );
    inputRenderer->ResetCamera();
    inputRenderer->SetBackground( .4, .5, .9 );

    vtkSmartPointer<vtkRenderer> xRenderer =
        vtkSmartPointer<vtkRenderer>::New();
    xRenderer->SetViewport( xViewport );
    xRenderer->AddActor( xActor );
    xRenderer->ResetCamera();
    xRenderer->SetBackground( .4, .5, .6 );

    // Setup render window
    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->SetSize( 400, 400 );
    renderWindow->AddRenderer( inputRenderer );
    renderWindow->AddRenderer( xRenderer );

    // Setup render window interactor
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleImage> style =
        vtkSmartPointer<vtkInteractorStyleImage>::New();

    renderWindowInteractor->SetInteractorStyle( style );

    // Render and start interaction
    renderWindowInteractor->SetRenderWindow( renderWindow );
    renderWindowInteractor->Initialize();

    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

 

分类: C plus plus

发表评论

电子邮件地址不会被公开。 必填项已用*标注