1、介绍
该功能主要示范点击鼠标左键,获取此刻鼠标的位置,并在控制台进行显示位置信息,并在显示界面显示一字符串。输入键盘上r,清空显示界面所有的字符
2、代码示例
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
using namespace std;
unsigned int text_id = 0;
void mouseEventOccurred(const pcl::visualization::MouseEvent &event, void* viewer_void)
{
pcl::visualization::PCLVisualizer *viewer = static_cast<pcl::visualization::PCLVisualizer *> (viewer_void);
if (event.getButton() == pcl::visualization::MouseEvent::LeftButton &&
event.getType() == pcl::visualization::MouseEvent::MouseButtonRelease)
{
std::cout << "Left mouse button released at position (" << event.getX() << ", " << event.getY() << ")" << std::endl;
char str[512];
sprintf(str, "text#%03d", text_id++);
viewer->addText("clicked here", event.getX(), event.getY(), str);
}
}
void keyboardEventOccurred(const pcl::visualization::KeyboardEvent &event, void* viewer_void)
{
pcl::visualization::PCLVisualizer *viewer = static_cast<pcl::visualization::PCLVisualizer *> (viewer_void);
if (event.getKeySym() == "r" && event.keyDown())
{
std::cout << "r was pressed => removing all text" << std::endl;
char str[512];
for (unsigned int i = 0; i < text_id; ++i)
{
sprintf(str, "text#%03d", i);
viewer->removeShape(str);
}
text_id = 0;
}
}
int main(){
//多数情况下,默认的鼠标和键盘交互设置不能满足用户的需求,用户想扩展函数的某一些功能,比如按下键盘时保存点云的信息,或者通过鼠标确定点云的位置,interactionCustomizationVis 函数进行演示如何捕捉鼠标和键盘事件,在窗口点击,将会显示一个2D的文本标签,按下r健出去文本
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
viewer->initCameraParameters();
//以上是实例化视窗的标准代码
//分别注册响应键盘和鼠标事件,keyboardEventOccurred mouseEventOccurred回调函数,需要将boost::shared_ptr强制转换为void*
viewer->registerKeyboardCallback(keyboardEventOccurred, (void*)viewer.get());
viewer->registerMouseCallback(mouseEventOccurred, (void*)viewer.get());
while (!viewer->wasStopped())
{
viewer->spinOnce();
}
return 0;
}