利用 vtk 的控件画圆和矩形

博客介绍了作者如何修改vtkAffineRepresentation2D的源码,使得在2D平面上不仅能绘制圆和矩形,还增加了图形的缩放和移动功能。关键代码展示了绘制圆的细节,包括计算坐标、设置顶点和更新细胞数组。此外,还实现了显示文本和调整轴的代码段。
摘要由CSDN通过智能技术生成

代码改造了vtkAffineReprentation2D, 使其在2d平面上可以画圆,和矩形。vtkAffineReprentation2D虽然也能画圆和矩形,但是不能放大缩小,也不能移动。本人改造了vtk代码使其可以放大缩小,更可以移动。

关键代码如下:

圆:

void vtkEllipseRepresent2D::BuildRepresentation()
{
	if (this->GetMTime() > this->BuildTime ||
		(this->Renderer && this->Renderer->GetVTKWindow() &&
			this->Renderer->GetVTKWindow()->GetMTime() > this->BuildTime))
	{
		 
		vtkInteractorObserver::ComputeWorldToDisplay(
			this->Renderer, this->Origin[0], this->Origin[1], this->Origin[2], this->DisplayOrigin);

		int* size = this->Renderer->GetRenderWindow()->GetSize();
	 
		this->CurrentWidth = this->CircleWidth;
		this->CurrentWidth /= 2.0;
		double p1[3], p2[3], p3[3], p4[3];
		p1[0] = this->DisplayOrigin[0] - this->CurrentWidth;
		p1[1] = this->DisplayOrigin[1] - this->CurrentWidth;
		p1[2] = 0.0;
		p2[0] = this->DisplayOrigin[0] + this->CurrentWidth;
		p2[1] = this->DisplayOrigin[1] - this->CurrentWidth;
		p2[2] = 0.0;
		p3[0] = this->DisplayOrigin[0] + this->CurrentWidth;
		p3[1] = this->DisplayOrigin[1] + this->CurrentWidth;
		p3[2] = 0.0;
		p4[0] = this->DisplayOrigin[0] - this->CurrentWidth;
		p4[1] = this->DisplayOrigin[1] + this->CurrentWidth;
		p4[2] = 0.0;
		this->HBoxPoints->SetPoint(0, p1);
		this->HBoxPoints->SetPoint(1, p2);
		this->HBoxPoints->SetPoint(2, p3);
		this->HBoxPoints->SetPoint(3, p4);
		this->HBoxPoints->Modified();

		// draw the circle
		int i;
		double theta, delTheta = 2.0 * vtkMath::Pi() / VTK_CIRCLE_RESOLUTION;
		this->CurrentRadius = this->CurrentWidth ;//* 0.75
		this->CircleCellArray->Reset();
		this->CirclePoints->Reset();

		this->CirclePoints->SetNumberOfPoints(VTK_CIRCLE_RESOLUTION);
		//this->CircleCellArray->AllocateEstimate(1, VTK_CIRCLE_RESOLUTION + 1);
		
		this->CircleCellArray->InsertNextCell(VTK_CIRCLE_RESOLUTION + 1);
		for (i = 0; i < VTK_CIRCLE_RESOLUTION; i++)
		{
			theta = i * delTheta;
			p1[0] = this->DisplayOrigin[0] + this->CurrentRadius * cos(theta);
			p1[1] = this->DisplayOrigin[1] + this->CurrentRadius * sin(theta);
			this->CirclePoints->SetPoint(i, p1);
			this->CircleCellArray->InsertCellPoint(i);
		}
		this->CircleCellArray->InsertCellPoint(0);
		this->CircleCellArray->Modified();
		
		if (this->DisplayText)
		{
			char str[256];
			int maxValue, minValue;
			srand((unsigned)time(NULL));
			for (int i = 0; i < 10; i++)
			{
				maxValue += rand();
				minValue = rand();
			}
			maxValue = maxValue % 255;
			minValue = minValue % 50;
			if (maxValue < minValue)
				maxValue += 180;

			snprintf(str, sizeof(str), "max:%d\n, mean:%d\n, min:%d",
				maxValue, (maxValue + minValue) / 2, minValue);
			double eventPos[2] = { this->DisplayOrigin[0] + this->CurrentRadius ,this->DisplayOrigin[1] };
			this->UpdateText(str, eventPos);
		}

		// draw the translation axes
		this->CurrentAxesWidth = this->CurrentWidth * this->AxesWidth / this->CircleWidth;
		p1[0] = this->DisplayOrigin[0] - this->CurrentAxesWidth;
		p1[1] = this->DisplayOrigin[1];
		this->XAxis->GetPositionCoordinate()->SetValue(p1);
		p2[0] = this->DisplayOrigin[0] + this->CurrentAxesWidth;
		p2[1] = this->DisplayOrigin[1];
		this->XAxis->GetPosition2Coordinate()->SetValue(p2);

		p1[0] = this->DisplayOrigin[0];
		p1[1] = this->DisplayOrigin[1] - this->CurrentAxesWidth;
		this->YAxis->GetPositionCoordinate()->SetValue(p1);
		p2[0] = this->DisplayOrigin[0];
		p2[1] = this->DisplayOrigin[1] + this->CurrentAxesWidth;
		this->YAxis->GetPosition2Coordinate()->SetValue(p2);
		
		this->BuildTime.Modified();
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值