VTK世界坐标系转屏幕坐标系

方法一:直接使用WorldToDisplay函数转换

	int x = 110, y = 176, z = 107;
	double view3[3];
	renderer->SetWorldPoint(x, y, z, 1);//输入待换算三维点坐标
	renderer->WorldToDisplay();//执行坐标变换
	renderer->GetDisplayPoint(view3);//读取变换结果,u=view[0] v=view[1]

方法二:如果有大量点需要转换可以使用GetCompositeProjectionTransformMatrix获得投影变换矩阵,然后计算点在屏幕上的投影位置。

                参数GetTiledAspectRatio()表示渲染窗口的长宽比,这个参数通常设置为1.0,02是默认参数值,表示使用默认的near和far剪裁平面。

	float testpoint[4]{ x, y, z, 1 };
	float* view4;
	vtkMatrix4x4* matrix = ren->GetActiveCamera()->GetCompositeProjectionTransformMatrix(ren->GetTiledAspectRatio(), 0, 2);
	view4 = matrix->MultiplyPoint(testpoint);
	if (view4[3] != 0)
	{
		view4[0] /= view4[3];
		view4[1] /= view4[3];
		view4[2] /= view4[3];
	}
	int u = (view4[0] + 1.0) * _windowWidth / 2;//_windowWidth为RenderWindow的宽度
	int v = (view4[1] + 1.0) * _windowHeight / 2;

view4[0] + 1的原因为x_world和y_world的范围是[-1,1],那么将它们加1之后,范围就变成了[0,2],然后再除以2,就可以将范围归一化到[0,1]。

### 如何在 C++ 中使用 VTK 添加坐标轴或参考系 为了在基于 C++ 的应用程序中利用 VTK 库添加坐标轴,可以采用 `vtkAxesActor` 和 `vtkOrientationMarkerWidget` 组件来创建并配置坐标系。这使得开发者能够在渲染场景内指定位置展示一个直观的方向指示器。 #### 创建和配置坐标轴对象 通过实例化 `vtkAxesActor` 类可获得用于表示三维空间坐标的视觉元素。此组件允许自定义箭头长度、标签样式以及其他外观属性: ```cpp #include <vtkSmartPointer.h> #include <vtkAxesActor.h> // Create a vtkAxesActor object to represent the axes. auto axes = vtkSmartPointer<vtkAxesActor>::New(); axes->SetTotalLength(10, 10, 10); // Set length of each axis line segment ``` #### 将坐标轴集成到交互环境中 为了让用户能够更方便地查看坐标信息,在实际应用中通常会选择将上述构建好的坐标轴与特定的视图关联起来。借助于 `vtkOrientationMarkerWidget` 可以轻松完成这项工作,并支持调整其相对于父容器的位置布局方式: ```cpp #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> #include <vtkOrientationMarkerWidget.h> // Assuming 'renderer' is your existing vtkRenderer instance and // 'interactor' refers to an active vtkRenderWindowInteractor. // Instantiate orientation marker widget with our custom axes actor. auto widget = vtkSmartPointer<vtkOrientationMarkerWidget>::New(); widget->SetOutlineColor(0.93, 0.57, 0.13); widget->SetInteractor(interactor); // Positioning options are available via enum values defined within VTK. widget->SetViewport(0.0, 0.0, 0.4, 0.4); // Lower-left corner placement widget->SetEnabled(true); widget->InteractiveOn(); // Associate this widget's rendering behavior with the given renderer. widget->SetOrientationMarker(axes); ``` 以上代码片段展示了如何在一个典型的 Qt + VTK 开发环境下向 OpenGL 渲染窗口添加带有互动功能的小部件[^3]。值得注意的是,这里所使用的 API 版本可能因具体安装包的不同而有所差异;因此建议查阅官方文档获取最新指导说明。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

混元太极马保国

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值