在PCLVisualizer中添加坐标轴和图片和颜色表

pcl 专栏收录该内容
1 篇文章 0 订阅

继承PCL可视化类,实现坐标轴、PNG图片、颜色表的显示

pcl::visualization::PCLVisualizer是pcl中的可视化类,支持点云显示,点云拾取,点云框选等,但是无法添加图片和颜色表。通过继承PCLVisualizer类可以灵活添加坐标轴、图片和颜色表。

参考了pcl源码之后实现了上面几个功能,效果如下。

左下角添加了坐标轴,其位置通过参数给出,固定不可移动
右上角添加了一个PNG图片显示
右下角添加了一个颜色条
在这里插入图片描述

实现代码

main.cpp

#include <vtkRenderWindow.h>
#include <vtkPNGReader.h>
#include <vtkImageData.h>
#include <vtkLogoRepresentation.h>
#include <vtkLogoWidget.h>
#include <vtkAxesActor.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkScalarBarWidget.h>
#include <vtkScalarBarActor.h>
#include <vtkScalarBarRepresentation.h>
#include <vtkColorTransferFunction.h>

#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/point_cloud_color_handlers.h>

using namespace std;

/*继承了PCL的可视化类 pcl::visualization::PCLVisualizer,实现坐标轴/LOGO图片/颜色表的显示*/
class My_View :public pcl::visualization::PCLVisualizer
{
public:
	My_View(string name, bool initIterator) :pcl::visualization::PCLVisualizer(name, initIterator)
	{
		;
	}

	/** \brief Adds a widget which shows an Logo image display
	*  \param[in] interactor - Pointer to the vtk interactor object used by the PCLVisualizer window
	*  \param[in] name - imagefilename
	*  \param[in] x - x-position
	*  \param[in] y - y-position
	*  \param[in] x_wide - x-scalar
	*  \param[in] y_wide - y-scalar
	*  \param[in] opaticy - opaticy
	*/
	void addLogoWidgetToview(vtkRenderWindowInteractor* interactor, const std::string &name, double &x, double &y, double &x_wide, double &y_wide, double &opaticy);

	/** \brief Adds a widget which shows an vtkScalarBarWidget
	*  \param[in] interactor - Pointer to the vtk interactor object used by the PCLVisualizer window
	*  \param[in] lut - lookuptable
	*  \param[in] x - x-position
	*  \param[in] y - y-position
	*  \param[in] x_wide - x-scalar
	*  \param[in] y_wide - y-scalar
	*/
	void addScalarBarWidgetToView(vtkRenderWindowInteractor* interactor, vtkLookupTable* lut, double &x, double &y, double &x_wide, double &y_wide);
	vtkSmartPointer<vtkScalarBarWidget> getScalarBarWidgetMember();

	/** \brief Adds a widget which shows an interactive axes display for orientation
	*  \param[in] interactor - Pointer to the vtk interactor object used by the PCLVisualizer window
	*  \param[in] x - x-position
	*  \param[in] y - y-position
	*  \param[in] x_wide - x-scalar
	*  \param[in] y_wide - y-scalar
	*/
	void addOrientationMarkerWidgetAxesToview(vtkRenderWindowInteractor* interactor, double x, double y, double x_wide, double y_wide);

private:

	/** \brief Internal pointer to widget which contains a logo_Widget_ */
	vtkSmartPointer<vtkLogoWidget> logo_Widget_member_;

	/** \brief Internal pointer to widget which contains a scalarbar_Widget_ */
	vtkSmartPointer<vtkScalarBarWidget> scalarbar_Widget_member_;

	/** \brief Internal pointer to widget which contains a set of axes */
	vtkSmartPointer<vtkOrientationMarkerWidget> axes_widget_member_;

};

vtkSmartPointer<vtkScalarBarWidget> My_View::getScalarBarWidgetMember()
{
	return scalarbar_Widget_member_;
}

void My_View::addLogoWidgetToview(vtkRenderWindowInteractor* interactor, const std::string &name, double &x, double &y, double &x_wide, double &y_wide, double &opaticy)
{
	if (!logo_Widget_member_)
	{
		vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New();
		reader->SetFileName(name.c_str());/*no multiple coding format*/
		reader->Update();
		vtkSmartPointer<vtkLogoRepresentation> logoRepresentation = vtkSmartPointer<vtkLogoRepresentation>::New();
		logoRepresentation->SetImage(reader->GetOutput());
		logoRepresentation->SetPosition(x, y);
		logoRepresentation->SetPosition2(x_wide, y_wide);/*0.12, 0.05*/
		logoRepresentation->GetImageProperty()->SetOpacity(opaticy);/*0.9*/

		logoRepresentation->SetShowBorderToOff();/*no edgeline*/
		logo_Widget_member_ = vtkSmartPointer<vtkLogoWidget>::New();
		logo_Widget_member_->SetInteractor(interactor);
		logo_Widget_member_->SetRepresentation(logoRepresentation);

		logo_Widget_member_->SetEnabled(true);/*enable show*/
		logo_Widget_member_->ProcessEventsOff();/*disable move*/
	}
	else
	{
		logo_Widget_member_->SetEnabled(true);
		pcl::console::print_warn(stderr, "LogoWidget Widget already exists, just enabling it");
	}
}


void My_View::addScalarBarWidgetToView(vtkRenderWindowInteractor* interactor, vtkLookupTable* lut, double &x, double &y, double &x_wide, double &y_wide)
{
	if (!scalarbar_Widget_member_)
	{
		/*Scalarbar Actor*/
		vtkSmartPointer< vtkScalarBarActor > scalarBarActor = vtkSmartPointer< vtkScalarBarActor >::New();
		scalarBarActor->SetOrientationToHorizontal();
		scalarBarActor->SetLookupTable(lut);

		/*ScalarBar Widget*/
		scalarbar_Widget_member_ = vtkSmartPointer< vtkScalarBarWidget >::New();
		scalarbar_Widget_member_->SetInteractor(interactor);
		scalarbar_Widget_member_->SetScalarBarActor(scalarBarActor);
		//scalarBarWidget->set
		scalarbar_Widget_member_->On();
	}
	else
	{
		/*Scalarbar Actor*/
		vtkSmartPointer< vtkScalarBarActor > scalarBarActor = vtkSmartPointer< vtkScalarBarActor >::New();
		scalarBarActor->SetOrientationToHorizontal();
		scalarBarActor->SetLookupTable(lut);

		/*ScalarBar Widget*/
		//scalarbar_Widget_.
		scalarbar_Widget_member_ = vtkSmartPointer< vtkScalarBarWidget >::New();
		scalarbar_Widget_member_->SetInteractor(interactor);
		scalarbar_Widget_member_->SetScalarBarActor(scalarBarActor);
		//scalarBarWidget->set
		scalarbar_Widget_member_->On();
	}
}

void My_View::addOrientationMarkerWidgetAxesToview(vtkRenderWindowInteractor* interactor, double x, double y, double x_wide, double y_wide)
{
	if (!axes_widget_member_)
	{
		vtkSmartPointer<vtkAxesActor> axes = vtkSmartPointer<vtkAxesActor>::New();

		axes_widget_member_ = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
		axes_widget_member_->SetOutlineColor(0.9300, 0.5700, 0.1300);
		axes_widget_member_->SetOrientationMarker(axes);
		axes_widget_member_->SetInteractor(interactor);
		axes_widget_member_->SetViewport(x, y, x_wide, y_wide);
		axes_widget_member_->SetEnabled(true);
		axes_widget_member_->InteractiveOn();
		axes_widget_member_->InteractiveOff();
	}
	else
	{
		axes_widget_member_->SetEnabled(true);
		pcl::console::print_warn(stderr, "Orientation Widget Axes already exists, just enabling it");
	}
}


int main(int argc, char *argv[])
{
	/*初始化视图*/
	boost::shared_ptr<My_View> viewer_;
	viewer_.reset(new My_View("viewer", true));

	/*添加Logo图片到视图左上角*/
	std::string logofilename = "png.png";
	double x = 0.80, y = 0.80/*0.93*/, x_wide = 0.2/*0.12*/, y_wide = 0.1/*0.05*/, opacity = 0.9;
	viewer_->addLogoWidgetToview(viewer_->getRenderWindow()->GetInteractor(), logofilename, x, y, x_wide, y_wide, opacity);

	/*添加坐标轴到视图的左下角*/
	viewer_->addOrientationMarkerWidgetAxesToview(viewer_->getRenderWindow()->GetInteractor(), 0.0, 0.0, 0.2, 0.2);

	/*添加颜色列表到视图右下角*/
	vtkSmartPointer<vtkColorTransferFunction> color = vtkSmartPointer<vtkColorTransferFunction>::New();
	color->AddRGBPoint(0.0, 1.0,0.0,0.0);/*red*/
	color->AddRGBPoint(0.5, 0.0, 1.0, 0.0);/*green*/
	color->AddRGBPoint(1.0, 0.0, 0.0, 1.0);/*blue*/
	color->SetColorSpaceToRGB();
	color->SetNanColor(1.0, 1.0, 0);
	vtkSmartPointer< vtkLookupTable > lut = vtkSmartPointer< vtkLookupTable >::New();
	lut->SetNumberOfColors(64);
	for (int i = 0; i < 64; i++)
	{
		double d[3] = { 0.0 };
		double *pd = color->GetColor(i / 64.0);
		d[0] = pd[0]; d[1] = pd[1]; d[2] = pd[2];
		lut->SetTableValue(i, d[0], d[1], d[2]);
	}
	lut->SetTableRange(0.0, 1.0);
	lut->Build();

	double x1 = 0.9, y1 = 0.0,/*0.93*/ x_wide1 = 0.08/*0.12*/, y_wide1 = 0.35;/*0.05*/
	int maxsize[2] = { 50, 100 };
	int minsize[2] = { 50, 100 };

	vtkSmartPointer<vtkScalarBarRepresentation > scalarBarRep = vtkSmartPointer<vtkScalarBarRepresentation >::New();
	scalarBarRep->SetPosition(x1, y1);/*在pcl visual中的位置*/
	scalarBarRep->SetPosition2(x_wide1, y_wide1);/*0.2, 0.2等效于上面的两行代码*/
	//scalarBarRep->SetMaximumSize(20, 50);/*最大*/	//scalarBarRep->SetMinimumSize(minsize);/*最小*/

	scalarBarRep->SetShowBorderToOn();/*Invalid*/
	scalarBarRep->SetShowBorder(true); //scalarBarRep->SetShowBorderToActive();
	//scalarBarRep->SetShowHorizontalBorder(true); //scalarBarRep->SetShowVerticalBorder(true);

	viewer_->addScalarBarWidgetToView(viewer_->getRenderWindow()->GetInteractor(), lut, x1, y1, x_wide1, y_wide1);/*添加Widget到View*/
	scalarBarRep->SetScalarBarActor(viewer_->getScalarBarWidgetMember()->GetScalarBarActor());/*设置代理对象为Widget*/
	viewer_->getScalarBarWidgetMember()->SetRepresentation(scalarBarRep);/*为Widget设置代理*/
	
	viewer_->getScalarBarWidgetMember()->GetScalarBarActor()->SetLabelFormat("%5.2f");/*尺度的刻度值*/
	viewer_->getScalarBarWidgetMember()->GetScalarBarActor()->SetMaximumWidthInPixels(70);

	viewer_->spin();
	
}

CMakeLists

cmake_minimum_required (VERSION 2.6 FATAL_ERROR)

project(pcl_visualization_test)
find_package (VTK REQUIRED)
find_package (PCL 1.8.1 REQUIRED)

include_directories(${Boost_INCLUDE_DIRS} ${PCL_INCLUDE_DIRS})
link_directories    (${PCL_LIBRARY_DIRS})
add_definitions     (${PCL_DEFINITIONS})

ADD_DEFINITIONS(-D _XKEYCHECK_H)
ADD_EXECUTABLE  (Demo main.cpp)

TARGET_LINK_LIBRARIES (Demo ${PCL_LIBRARIES} ${VTK_LIBRARIES})

参考资料

VTK的数据结构

  • 4
    点赞
  • 16
    评论
  • 23
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值