[vtkusers] Contourf plot with dynamic resize

Mattia M l1q1d at yahoo.it
Mon Jul 24 05:35:25 EDT 2017


Hi to all, this is my first post in the mailing list. I'm trying to develop a plot application thant is able to create the controuf similar to matlab https://it.mathworks.com/help/matlab/ref/contourf.html <https://it.mathworks.com/help/matlab/ref/contourf.html>

I succeed in creating something like it but my actual issues are place the plot in the center and resize dynamically with the window. This is the code that I'm using so far:


void CreateBaseData(vtkImageData* image)

{



    datastructure * dataInput = new datastructure(FILEPATH);



    image->SetDimensions(dataInput->shape()->Xsize, dataInput->shape()->Ysize, 1);

//    image->SetSpacing(dataInput->shape()->Xres,dataInput->shape()->Yres,0);



//    image->SetOrigin(dataInput->shape()->Xmin,dataInput->shape()->Ymin,0);



    image->AllocateScalars(VTK_DOUBLE,1);



    for(unsigned int x = 0; x <  dataInput->shape()->Xsize; x++)

    {

        for(unsigned int y = 0; y < dataInput->shape()->Ysize; y++)

        {

            double* pixel = static_cast<double *>(image->GetScalarPointer(x,y,0));

            pixel[0] = dataInput->z[y][x];

        }

    }

    delete dataInput;



    //image->Modified();

}



void RenderWindowUISingleInheritance::ModifiedHandler()

{

    ui->qvtkWidget->GetRenderWindow()->Render();

}





// Constructor

RenderWindowUISingleInheritance::RenderWindowUISingleInheritance(QWidget *parent) :

    QMainWindow(parent),

    ui(new Ui::RenderWindowUISingleInheritanceUI)

{

    ui->setupUi(this);



    vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New();



    ui->qvtkWidget->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style);



    int numberOfContours = 15;



    double scalarRange[2];



    float levels[] = { 0.4, 1.0, 2.0, 4, 10.0, 20.0, 40, 100, 200, 400, 1000, 2000, 4000, 10000, 20000, 40000 };



    vtkSmartPointer<vtkImageData> colorImage = vtkSmartPointer<vtkImageData>::New();

    vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();

    vtkSmartPointer<vtkImageDataGeometryFilter> convert = vtkSmartPointer<vtkImageDataGeometryFilter>::New();

    vtkSmartPointer<vtkBandedPolyDataContourFilter> bf = vtkSmartPointer<vtkBandedPolyDataContourFilter>::New();

    vtkSmartPointer<vtkPolyDataMapper2D> filledContourMapper = vtkSmartPointer<vtkPolyDataMapper2D>::New();

    vtkSmartPointer<vtkPolyDataMapper2D> contourLineMapperer =vtkSmartPointer<vtkPolyDataMapper2D>::New();



    vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();



    vtkSmartPointer<vtkActor2D> filledContourActor =vtkSmartPointer<vtkActor2D>::New();

    vtkSmartPointer<vtkActor2D> contourLineActor = vtkSmartPointer<vtkActor2D>::New();

    vtkSmartPointer<vtkCubeAxesActor> cubeAxesActor = vtkSmartPointer<vtkCubeAxesActor>::New();

    vtkSmartPointer<vtkScalarBarActor> scalarBarActor =vtkSmartPointer<vtkScalarBarActor>::New();

    CreateBaseData(colorImage);



    colorImage->GetScalarRange(scalarRange);



    int l_min;

    int l_max;

    for (l_min=numberOfContours-1;l_min>=0;l_min--) {

        if (levels[l_min] <= scalarRange[0]) break;

    }



    for (l_max=0;l_max<numberOfContours;l_max++) {



        if (levels[l_max] > scalarRange[1]) break;

    }

    numberOfContours = l_max - l_min;



//    colormap::setColorMap(lut,1,1);



    char label[10];

    for (vtkIdType i = l_min; i < numberOfContours+1; ++i)

    {

        //std::cout << lut->GetAnnotatedValue(i).ToString() << std::endl;



        sprintf(label,"%4.2f",levels[i]);

        lut->SetAnnotation(i, label);

    }



    convert->SetInputData(colorImage);

    bf->SetInputConnection(convert->GetOutputPort());

    bf->SetNumberOfContours(numberOfContours);

    for (int i=l_min;i<l_max;i++){



        bf->SetValue(i,levels[i]);

    }



    bf->GenerateContourEdgesOn();



    filledContourMapper->SetInputConnection(bf->GetOutputPort());

    filledContourMapper->SetScalarModeToUseCellData();

    //    filledContourMapper->SetColorModeToMapScalars();

    filledContourMapper->SetScalarRange(0,numberOfContours);

    filledContourMapper->SetLookupTable(lut);



    filledContourActor->SetMapper(filledContourMapper);

    filledContourActor->PickableOn();

    filledContourActor->GetPosition2Coordinate()->SetValue(0.17, 0.8);

    filledContourActor->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();

    filledContourActor->GetPositionCoordinate()->SetValue(0.82, 0.1);


    filledContourActor->SetWidth( 0.7 );

    filledContourActor->SetHeight( 0.1 );

    filledContourActor->SetPosition( 0.1, 0.01 );


    contourLineMapperer->SetInputData(bf->GetContourEdgesOutput());


    contourLineMapperer->ScalarVisibilityOn();


    contourLineActor->SetMapper(contourLineMapperer);

    contourLineActor->GetProperty()->SetLineWidth(0.5);

    contourLineActor->GetProperty()->SetColor(0., 0., 0.);


    scalarBarActor->SetLookupTable(filledContourMapper->GetLookupTable());

    scalarBarActor->SetNumberOfLabels(numberOfContours);

    scalarBarActor->SetOrientationToHorizontal();

    scalarBarActor->SetPosition(0.05,0.2);

    scalarBarActor->SetWidth(0.9);

    scalarBarActor->SetHeight(0.05);



    scalarBarActor->SetDrawTickLabels(0);

    scalarBarActor->SetDrawAnnotations(1);

    scalarBarActor->GetAnnotationTextProperty()->SetFontFamilyToCourier();

    //scalarBar->GetAnnotationTextProperty()->SetVerticalJustificationToCentered();

    scalarBarActor->GetAnnotationTextProperty()->BoldOff();

    scalarBarActor->GetAnnotationTextProperty()->ItalicOff();

    scalarBarActor->GetAnnotationTextProperty()->ShadowOff() ;

    scalarBarActor->GetAnnotationTextProperty()->SetColor(0, 0, 0);

    scalarBarActor->GetAnnotationTextProperty()->SetJustificationToRight();


    scalarBarActor->SetUseBounds(true);


//    cubeAxesActor->SetBounds(filledContourMapper->GetBounds());

    cubeAxesActor->SetCamera(ren1->GetActiveCamera());

    cubeAxesActor->GetTitleTextProperty(0)->SetColor(0.0, 0.0, 0.0);

    cubeAxesActor->GetLabelTextProperty(0)->SetColor(0.0, 0.0, 0.0);

    cubeAxesActor->GetTitleTextProperty(1)->SetColor(0.0, 0.0, 0.0);

    cubeAxesActor->GetLabelTextProperty(1)->SetColor(0.0, 0.0, 0.0);

    cubeAxesActor->DrawXGridlinesOn();

    cubeAxesActor->DrawYGridlinesOn();

    cubeAxesActor->DrawZGridlinesOff();

    cubeAxesActor->GetXAxesLinesProperty()->SetColor(0.0, 0.0,0.0);

    cubeAxesActor->GetYAxesLinesProperty()->SetColor(0.0, 0.0,0.0);

    cubeAxesActor->GetYAxesGridlinesProperty ()->SetColor(0,0,0);

    cubeAxesActor->GetXAxesGridlinesProperty ()->SetColor(0,0,0);



    cubeAxesActor->SetTickLocationToOutside();

    //    cubeAxesActor->SetGridLineLocation(cubeAxesActor->VTK_GRID_LINES_CLOSEST);

    ren1->SetBackground(1, 1, 1);

    // Add the actors

    ren1->AddActor(filledContourActor);
    ren1->AddActor(contourLineActor);
    ren1->AddActor(scalarBarActor);

//    ren1->AddActor(cubeAxesActor);
    ren1->ResetCamera();
    // VTK/Qt wedded
    ui->qvtkWidget->GetRenderWindow()->AddRenderer(ren1);

    ui->qvtkWidget->GetRenderWindow()->AddObserver(vtkCommand::ModifiedEvent,this,&RenderWindowUISingleInheritance::ModifiedHandler);

    // Set up action signals and slots

    connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(slotExit()));

}



void RenderWindowUISingleInheritance::slotExit() 

{

    qApp->exit();

}



RenderWindowUISingleInheritance::~RenderWindowUISingleInheritance() {

    delete ui;

};

Do you have any suggestion?

Thanks

Mattia

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170724/b9b8d571/attachment-0001.html>


More information about the vtkusers mailing list