vtk绘制线段

效果如下:

在这里插入图片描述
代码:

#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkLine.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkUnsignedCharArray.h>

#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif

#ifdef vtkGenericDataArray_h
#define InsertNextTupleValue InsertNextTypedTuple
#endif

int main(void)
{
	vtkNew<vtkPolyData> linesPolyData;

	double origin[3] = { 0.0, 0.0, 0.0 };
	double p0[3] = { 1.0, 0.0, 0.0 };
	double p1[3] = { 0.0, 1.0, 0.0 };

	vtkNew<vtkPoints> pts;
	pts->InsertNextPoint(origin);
	pts->InsertNextPoint(p0);
	pts->InsertNextPoint(p1);

	linesPolyData->SetPoints(pts);

	vtkNew<vtkLine> line0;
	line0->GetPointIds()->SetId(0, 0);
	line0->GetPointIds()->SetId(1, 1);

	vtkNew<vtkLine> line1;
	line1->GetPointIds()->SetId(0, 0);
	line1->GetPointIds()->SetId(1, 2);

	vtkNew<vtkCellArray> lines;
	lines->InsertNextCell(line0);
	lines->InsertNextCell(line1);

	linesPolyData->SetLines(lines);

	vtkNew<vtkNamedColors> namedColors;

	vtkNew<vtkUnsignedCharArray> colors;
	colors->SetNumberOfComponents(3);
	colors->InsertNextTupleValue(namedColors->GetColor3ub("Tomato").GetData());
	colors->InsertNextTupleValue(namedColors->GetColor3ub("Mint").GetData());

	linesPolyData->GetCellData()->SetScalars(colors);

	vtkNew<vtkPolyDataMapper> mapper;
	mapper->SetInputData(linesPolyData);

	vtkNew<vtkActor> actor;
	actor->SetMapper(mapper);
	actor->GetProperty()->SetLineWidth(4);

	vtkNew<vtkRenderer> renderer;
	renderer->AddActor(actor);
	renderer->SetBackground(namedColors->GetColor3d("SlateGray").GetData());

	vtkNew<vtkRenderWindow> window;
	window->SetWindowName("Colored Lines");
	window->AddRenderer(renderer);

	vtkNew<vtkRenderWindowInteractor> interactor;
	interactor->SetRenderWindow(window);

	window->Render();
	interactor->Start();

	system("pause");

	return EXIT_SUCCESS;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值