vtk绘制折线
- 引言
- 示例一
- 开发环境
- 项目结构
- 示例代码
- 运行结果
- 示例二
- 概况
- 示例代码
- 运行结果
引言
本文通过两种方式来绘制折线。示例一中通过vtkPolyLine来绘制折线;示例二中通过vtkLineSource来绘制折线,只不过示例二中折线是由一段一段的直线绘制,最后构成了一个折线。
示例一
开发环境
本文依旧采用QtCreator来创建空项目,其QtCreator的版本为4.11.2,Qt的版本是5.14.2。使用的vtk库版本为9.2。
项目结构
刚创建的空项目中只有一个空的pro文件。需要编写pro文件的内容,其内容为:
.pro
QT += core#greaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11 vtk9.2# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \main.cppSOUPDIR = $$PWD/../../SOUPdependency
vtk9.2 {contains(QT_ARCH, x86_64) {include($$SOUPDIR/vtk-9.2/vtk-9.2.pri)} else {include($$SOUPDIR/vtk-9.2-2017-omp-win32/vtk-9.2.pri)}DEFINES += vtkEventDataButton3D=vtkEventDataDevice3DDEFINES += vtkEventDataMove3D=vtkEventDataDevice3D
}# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
编写好pro文件之后,向项目中添加新文件main.cpp.
整个项目的结构如下:
示例代码
其main.cpp的代码如下:
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyLine.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLineSource.h>#include "vtkAutoInit.h"VTK_MODULE_INIT(vtkRenderingOpenGL2)//渲染
VTK_MODULE_INIT(vtkInteractionStyle)//交互样式
VTK_MODULE_INIT(vtkRenderingFreeType)//文本图像
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)//体素vtkNew<vtkPolyData> createBrokenLineByPolyLine()
{double dPoints[4][3] = {{3.2,7.4,7.0},{2.1,7.9,9.0},{12.3,6.4,7.5},{45.2,13.4,6.0}};vtkNew<vtkPoints> points;vtkNew<vtkPolyLine> polyLine;polyLine->GetPointIds()->SetNumberOfIds(4);for(int i = 0; i <4; ++i){points->InsertPoint(i,dPoints[i]);polyLine->GetPointIds()->SetId(i,i);}vtkNew<vtkCellArray> cellArray;cellArray->InsertNextCell(polyLine);vtkNew<vtkPolyData> polyData;polyData->SetPoints(points);polyData->SetLines(cellArray);return polyData;
}int main(int argc,char *argv[])
{vtkNew<vtkPolyData> polyData = createBrokenLineByPolyLine();vtkNew<vtkPolyDataMapper> mapper;mapper->SetInputData(polyData);vtkNew<vtkActor> actor;actor->SetMapper(mapper);actor->GetProperty()->SetColor(0.94,0.502,0.502);actor->GetProperty()->SetLineWidth(2.0);vtkNew<vtkRenderer> render;render->AddActor(actor);render->ResetCamera();vtkNew<vtkRenderWindow> renderWindow;renderWindow->AddRenderer(render);renderWindow->SetSize(400,400);renderWindow->Render();vtkNew<vtkRenderWindowInteractor> interactor;interactor->SetRenderWindow(renderWindow);interactor->Initialize();interactor->Start();return 0;
}
运行结果
示例二
概况
示例二也是创建的空项目,其开发环境同示例一,项目结构也同示例一。该示例实现的折线,由于是由三段的直线拼接而成,所以这里创建了三个vtkLineSource,将三个linesource分别设置到三个vtkPolyDataMapper中,又创建了三个vtkActor,将三个vtkPolyDataMapper指针对象分别设置到三个vtkActor指针对象中,其中三个vtkActor指针对象分别设置了其属性:直线的颜色。所以每段直线拥有自己的颜色。随后又将三个vtkActor对象一起添加到一个vtkRender中。
示例代码
这里只附上main.cpp的实现代码,其额外编写的pro文件内容同示例一。下面是main.cpp的实现代码:
main.cpp
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyLine.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLineSource.h>#include "vtkAutoInit.h"VTK_MODULE_INIT(vtkRenderingOpenGL2)//渲染
VTK_MODULE_INIT(vtkInteractionStyle)//交互样式
VTK_MODULE_INIT(vtkRenderingFreeType)//文本图像
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)//体素vtkNew<vtkRenderer> createBrokeLineByLineSource()
{double dPoints[4][3] = {{3.2,7.4,7.0},{2.1,7.9,9.0},{12.3,6.4,7.5},{45.2,13.4,6.0}};vtkNew<vtkLineSource> lineSource1;lineSource1->SetPoint1(dPoints[0]);lineSource1->SetPoint2(dPoints[1]);lineSource1->Update();vtkNew<vtkLineSource> lineSource2;lineSource2->SetPoint1(dPoints[1]);lineSource2->SetPoint2(dPoints[2]);lineSource2->Update();vtkNew<vtkLineSource> lineSource3;lineSource3->SetPoint1(dPoints[2]);lineSource2->SetPoint2(dPoints[3]);lineSource3->Update();vtkNew<vtkPolyDataMapper> mapper1;mapper1->SetInputData(lineSource1->GetOutput());vtkNew<vtkPolyDataMapper> mapper2;mapper2->SetInputData(lineSource2->GetOutput());vtkNew<vtkPolyDataMapper> mapper3;mapper3->SetInputData(lineSource3->GetOutput());vtkNew<vtkActor> actor1;actor1->SetMapper(mapper1);actor1->GetProperty()->SetColor(0.125,0.698,0.67);vtkNew<vtkActor> actor2;actor2->SetMapper(mapper2);actor2->GetProperty()->SetColor(1,0.714,0.757);vtkNew<vtkActor> actor3;actor3->SetMapper(mapper3);actor3->GetProperty()->SetColor(0.12,0.56,1);vtkNew<vtkRenderer> render;render->AddActor(actor1);render->AddActor(actor2);render->AddActor(actor3);render->ResetCamera();return render;
}int main(int argc,char *argv[])
{vtkNew<vtkRenderer> render = createBrokeLineByLineSource();vtkNew<vtkRenderWindow> renderWindow;renderWindow->AddRenderer(render);renderWindow->SetSize(400,400);renderWindow->Render();vtkNew<vtkRenderWindowInteractor> interactor;interactor->SetRenderWindow(renderWindow);interactor->Initialize();interactor->Start();return 0;
}