VTK :简单的2维图形绘制(点、线、矩形、多边形)

需要画出一个用于区域旋转的矩形,四个顶点、四条边可以拖动(像截图使用的矩形)。
2D 图形,不涉及3D
Qt5.12.7
vtk-9.2

看效果:
在这里插入图片描述

初始化矩形

定义四个顶点;
(0,1)(1,2)(2,3)(3,0)组成4条线

ps: GetColor3d_ 这些的返回值都是颜色数组[ double color[3] ]

	//点数据
	vtkSmartPointer<vtkPoints>				m_pPoints = nullptr;
	//每个点都有两个原型Actor组成(图中:白色圆圈和蓝色圆圈)
	vtkSmartPointer<vtkAssembly>			m_pPointAssembly[4]{ nullptr };
	//四条线的Actor
	vtkSmartPointer<vtkActor>				m_pLineActor[4]{ nullptr };
	//整个矩形
	vtkSmartPointer<vtkAssembly>			m_pQuadAssembly{ nullptr };


void InitQuad()
{
	m_pPoints = vtkSmartPointer<vtkPoints>::New();
	m_pQuadAssembly = vtkSmartPointer<vtkAssembly>::New();

	for (int i = 0; i < 4; ++i)
	{
		m_pPoints->InsertPoint(i, 0, 0, 0);
		m_pPointAssembly[i] = Generator_PointAssembly();//生成圆形actor
		m_pLineActor[i] = vtkSmartPointer<vtkActor>::New();

		//添加到 vtkPropPicker 的拾取列表,用于拾取Actor
		m_pPropPicker->AddPickList(m_pPointAssembly[i]);
		
		m_pQuadAssembly->AddPart(m_pPointAssembly[i]);
		m_pQuadAssembly->AddPart(m_pLineActor[i]);

		//绘制直线
		vtkNew<vtkLine> Line;
		Line->GetPointIds()->SetId(0, i);
		Line->GetPointIds()->SetId(1, i < 3 ? i + 1 : 0);
		vtkNew<vtkCellArray> Cell;
		Cell->InsertNextCell(Line);
		vtkNew<vtkPolyData> Polydata;
		Polydata->SetPoints(m_pPoints);//几何结构
		Polydata->SetLines(Cell);//拓扑结构
		vtkNew<vtkPolyDataMapper> Mapper;
		Mapper->SetInputData(Polydata);

		m_pLineActor[i]->SetMapper(Mapper);
		m_pLineActor[i]->SetPickable(false);
		m_pLineActor[i]->GetProperty()->SetColor(GetColor3d_default());
		m_pLineActor[i]->GetProperty()->SetLineWidth(1);
	}

//加到vtkRenderer 中
	DefaultRenderer->AddActor(m_pQuadAssembly);
}

//生成一个圆形Actor
vtkSmartPointer<vtkActor> Generator_PointActor(const double radius,  double* color)
{
	// 创建一个 vtkRegularPolygonSource 对象,用于表示点的形状(正多边形)
	vtkNew<vtkRegularPolygonSource> polygonSource;
	polygonSource->SetNumberOfSides(50); // 设置边数(即多边形的精度)
	polygonSource->SetRadius(radius);       // 设置点的半径

	// 创建一个 vtkPolyDataMapper 对象,并将图元连接到它
	vtkNew<vtkPolyDataMapper> mapper;
	mapper->SetInputConnection(polygonSource->GetOutputPort());
	VTK_MAPPER_TOPPING(mapper);//置顶

	// 设置边框样式和属性
	vtkNew<vtkActor> actor;
	actor->SetMapper(mapper);
	actor->GetProperty()->SetColor(color);

	return actor;
}


vtkSmartPointer<vtkAssembly> Generator_PointAssembly()
{
	vtkNew<vtkAssembly> Assembly;
	Assembly->AddPart(Generator_PointActor(6, GetColor3d_frame()));
	Assembly->AddPart(Generator_PointActor(5, GetColor3d_default()));

	return Assembly;
}

移动矩形

  1. 首先移动点对应的Assembly

	double picked[3]
	vtkNew<vtkAssembly> Assembly;
	
	//可以使vtkAssembly 中的若干个Actor同时移动
	Assembly->SetPosition(picked);
  1. 按照Assembly的位置重新调整矩形的位置信息
    刷新m_pPoints 内的坐标信息即可
void MoveQuad()
{
	for (int i = 0; i < 4; ++i)
		m_pPoints->SetPoint(i, m_pPointAssembly[i]->GetPosition());

	m_pPoints->Modified();
	m_pQuadAssembly->SetVisibility(true);


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
.05, false, false, false, 40), new Card(9, 6, 14, 0, 0.15, false, false, false, 55), new Card(7, 7, 15, 0, 0要在vtk.js中绘制,可以使用vtk.js中提供的vtkPoints和vtkGlyph3D类。以下.2, false, false, false, 50), new Card(11, 4, 16, 0, 0是一个简单的例子: ```javascript // 创建一个vtkPoints对象 const points = vtk.Common.Core.vtkPoints.newInstance(); .1, false, false, false, 45) ); $cards2 = array( new Card(8, 7, // 添加 points.insertNextPoint(0, 0, 0); points.insertNextPoint(1, 0, 014, 0, 0.1, false, false, false, 55), new Card(9, 5, ); points.insertNextPoint(0, 1, 0); points.insertNextPoint(0, 0, 1); // 创建15, 0, 0.2, false, false, false, 50), new Card(7, 8, 15, 0, 0.15, false, false, false, 60), new Card(11, 3, vtkGlyph3D对象 const glyph = vtk.Filters.General.vtkGlyph3D.newInstance({ input: points, }); // 设置glyph的16, 0, 0.05, false, false, false, 40), new Card(10, 4, 属性 glyph.setInputArrayToProcess(0, 0, 0, vtk.Common.DataModel.vtkDataObject.FIELD_ASSOCIATION_POINTS16, 0, 0.1, false, false, false, 45), new Card(12, 2, , 'Scalars'); glyph.setSourceConnection(vtk.Filters.Sources.vtkArrowSource.newInstance().getOutputPort()); glyph.setVectorModeToUse17, 0, 0.1, false, false, false, 35) ); // 创建队伍 $team1 =Normal(); // 创建vtkActor对象 const actor = vtk.Rendering.Core.vtkActor.newInstance(); actor.getProperty().setPointSize(5); // 设置 new Team($cards1); $team2 = new Team($cards2); // 开始战斗 $battle = new Battle($team1, $team2); $battle->start(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值