VTK多线条显示

1 开发环境

    计算机系统:Win8.1(64位)

    Qt版本:5.2.1

    Qt Creator版本:3.0.1

    VTK版本:6.2.0

2 问题描述

    VTK中可以是实现画线条的类有vtkLInevtkPolyLine。画单条线比较容易实现,但是要画多根线,使其构成树形,就比较麻烦。根据现有的资料,达到这个目的的方法有a)多个Actor,为每根线条创建一个Actor;b)单个Actor

    由于单个Actor比较简洁,避免了为不同的线创建不同的的Actor,因此本文重点介绍此方法。

3 具体代码

void RSAMeasurement3D::skeletonShow(QList < QList< QPoint3D > > groups,
                                      vtkActor *actor, double color[3])
{
    vtkSmartPointer < vtkPoints > points =
        vtkSmartPointer < vtkPoints >::New();

    QList < vtkIdList *> ids;

    int pointNum = 0;
    for( int g = 0; g < groups.length(); g++) {
        QList< QPoint3D > grp = groups.at(g);

        vtkIdList *id = vtkIdList::New();

        for (int i = 0; i < grp.length(); i++) {
            points->InsertNextPoint(grp[i].x(), grp[i].y(), grp[i].z());
            id->InsertNextId( pointNum );
            pointNum++;
        }

        ids.append( id );
    }


    //PolyLine
    vtkSmartPointer< vtkPolyLine >aPolyLine =
            vtkSmartPointer< vtkPolyLine >::New();

    aPolyLine->GetPointIds()->SetNumberOfIds( points->GetNumberOfPoints() );
   
    for (int i = 0; i < points->GetNumberOfPoints(); i++) {
        aPolyLine->GetPointIds()->SetId(i, i);
    }

    // vtkGrid
    vtkSmartPointer< vtkUnstructuredGrid > aPolyLineGrid =
            vtkSmartPointer< vtkUnstructuredGrid >::New();
    aPolyLineGrid->Allocate(1, 1);

    // Cells, very important
    for( int i = 0; i < ids.length(); i++) { 
        vtkIdList *id = ids.at(i);
        aPolyLineGrid->InsertNextCell(aPolyLine->GetCellType(), id);
        id->Delete();
    }

    aPolyLineGrid->SetPoints( points );


    // Mapper
    vtkSmartPointer< vtkDataSetMapper > aPolyLineMapper =
            vtkSmartPointer< vtkDataSetMapper >::New();
    aPolyLineMapper->SetInputData(aPolyLineGrid);

    // Actor
    vtkSmartPointer< vtkActor > aPolyLineActor =
            vtkSmartPointer< vtkActor >::New();
    aPolyLineActor->SetMapper(aPolyLineMapper);

    aPolyLineActor->GetProperty()->SetColor(1, 0, 0);

    m_pvtkRenderer3D->AddActor( aPolyLineActor );

    ui->qvtkWidget3D->repaint();

}

说明:

    上面代码是我最近项目中的一个函数,主要用于将骨架点云显示成骨架线。

参数:

groups——点云的组,每组中包含了若干个点

actor——暂时没有使用

color——暂时没有使用

数据类型:

QPoint3D——我自己定义的一个类型,主要用于存储3D点数据(QPoint只至此2D的)

    显示效果如下图:

VTK多线条显示_第1张图片


参考资料

[1]VTK中画线的代码

[2]3D plot of multiple lines between 2 known points

[3]Draw a multiple lines set with VTK

[3]VTK/Examples/Cxx/GeometricObjects/ColoredLines

[4]how to draw multiple lines simultaneously with vtk?

你可能感兴趣的:(VTK多线条显示)