VTK使用vtkActor2D添加polyline

Posted theArcticOcean

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VTK使用vtkActor2D添加polyline相关的知识,希望对你有一定的参考价值。

具体实现如下:

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkActor2D.h>
#include <vtkProperty2D.h>

using namespace std;

int main()

    vtkSmartPointer<vtkConeSource> cone =
            vtkSmartPointer<vtkConeSource>::New();

    vtkSmartPointer<vtkPolyDataMapper> mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection( cone->GetOutputPort() );

    vtkSmartPointer<vtkActor> actor =
            vtkSmartPointer<vtkActor>::New();
    actor->SetMapper( mapper );

    vtkSmartPointer<vtkRenderer> renderer =
            vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->SetBackground( 0, 0, 0 );

    vtkSmartPointer<vtkRenderWindow> renderWindow =
            vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer( renderer );

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow( renderWindow );

    // ------------- start to add red line ----------------
    vtkSmartPointer<vtkPolyDataMapper2D> v1mmMapper = vtkSmartPointer<vtkPolyDataMapper2D>::New();
    vtkSmartPointer<vtkPolyData> v1mmPolyData = vtkSmartPointer<vtkPolyData>::New();
    vtkSmartPointer<vtkCellArray> v1mmLines = vtkSmartPointer<vtkCellArray>::New();
    vtkSmartPointer<vtkPoints> v1mmPoints = vtkSmartPointer<vtkPoints>::New();

    v1mmPolyData->SetPoints( v1mmPoints );
    v1mmPolyData->SetLines( v1mmLines );
    v1mmMapper->SetInputData( v1mmPolyData );

    vtkSmartPointer<vtkActor2D> v1mmLinesActor = vtkSmartPointer<vtkActor2D>::New();
    v1mmLinesActor->SetMapper( v1mmMapper );
    v1mmLinesActor->GetProperty()->SetColor( 1, 0, 0 );
    v1mmLinesActor->GetProperty()->SetLineWidth( 1 );

    vtkSmartPointer<vtkCoordinate> normCoords = vtkSmartPointer<vtkCoordinate>::New();
    normCoords->SetCoordinateSystemToView();
    v1mmMapper->SetTransformCoordinate( normCoords );

    // just for test
    vtkCoordinate *cd = v1mmMapper->GetTransformCoordinate();
    assert( nullptr != cd );
    printf( "GetCoordinateSystemAsString: %s\\n", cd->GetCoordinateSystemAsString() );

    double linePoint1[3] =  0, -1, 0.0 ;
    double linePoint2[3] =  0, 1, 0.0 ;
    vtkIdType pointId1 = v1mmPoints->InsertNextPoint(linePoint1);
    vtkIdType pointId2 = v1mmPoints->InsertNextPoint(linePoint2);
    vtkIdType lineIds[2] =  pointId1, pointId2 ;
    v1mmLines->InsertNextCell(2, lineIds);

    linePoint1[0] = -1; linePoint1[1] = 0; linePoint1[2] = 0;
    linePoint2[0] = 1; linePoint2[1] = 0; linePoint2[2] = 0;
    lineIds[0] = v1mmPoints->InsertNextPoint(linePoint1);
    lineIds[1] = v1mmPoints->InsertNextPoint(linePoint2);
    v1mmLines->InsertNextCell(2, lineIds);
    v1mmPolyData->Modified();
    // ------------- adding red line finished----------------

    renderer->ResetCamera();

    renderer->AddActor2D( v1mmLinesActor );

    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;

我们也可以使用viewport坐标系来绘图。
改动的代码只有vtkCoordinate的坐标系统设置以及线段两端点的位置设置。
因为view的坐标范围是[0,1],而viewport的坐标对应着像素值,所以我们需要使用renderWindow->GetSize();获取窗体的像素尺寸。

    vtkSmartPointer<vtkCoordinate> normCoords = vtkSmartPointer<vtkCoordinate>::New();
    normCoords->SetCoordinateSystemToViewport();
    v1mmMapper->SetTransformCoordinate( normCoords );

    // just for test
    vtkCoordinate *cd = v1mmMapper->GetTransformCoordinate();
    assert( nullptr != cd );
    printf( "GetCoordinateSystemAsString: %s\\n", cd->GetCoordinateSystemAsString() );

    renderWindow->Render(); //after render, window's size is not (0, 0) any more.

    int *size = renderWindow->GetSize();
    printf( "size: %d, %d\\n", size[0], size[1] );

    // viewport origin point is at left-bottom corner.
    double linePoint1[3] =  size[0]/2.0, 0, 0.0 ;
    double linePoint2[3] =  size[0]/2.0, size[1] * 1.0, 0.0 ;

    vtkIdType pointId1 = v1mmPoints->InsertNextPoint(linePoint1);
    vtkIdType pointId2 = v1mmPoints->InsertNextPoint(linePoint2);
    vtkIdType lineIds[2] =  pointId1, pointId2 ;
    v1mmLines->InsertNextCell(2, lineIds);

    linePoint1[0] = 0; linePoint1[1] = size[1] / 2; linePoint1[2] = 0;
    linePoint2[0] = size[0]; linePoint2[1] = size[1] / 2; linePoint2[2] = 0;
    lineIds[0] = v1mmPoints->InsertNextPoint(linePoint1);
    lineIds[1] = v1mmPoints->InsertNextPoint(linePoint2);
    v1mmLines->InsertNextCell(2, lineIds);
    v1mmPolyData->Modified();
    // ------------- adding red line finished----------------

显示结果和view系统一样。
图略。
在放大窗体后,view坐标里面的红线仍然布满整个窗体,但是viewport里的红线因为最初设置的寸尺比改变后的窗体寸尺小,所以就会有这样的放大效果:

如果不为render设置viewport,viewport和display的效果是一样的

    //normCoords->SetCoordinateSystemToViewport();
    normCoords->SetCoordinateSystemToDisplay();

但是,我们如果设置了viewport,那么差异就出现了:
在添加red line的代码块之前写上如下代码

    renderer->SetViewport( 0.0, 0.0, 0.5, 1.0 );

效果:

以上是关于VTK使用vtkActor2D添加polyline的主要内容,如果未能解决你的问题,请参考以下文章

添加单元格后更新 UnstructredGrid (VTK C++)

如何添加带有变量结尾(“扩展名”)的.gitignore文件,但开头是一样的

VTK基于MFC单文档的开发

求教VTK窗口渲染问题

使用OpenCV的函数polylines()绘制多条相连的线段和多边形;使用函数fillPoly()绘制带填充效果的多边形

Cmake动态编译VTK库(QVtkwidget)