点云视窗类CloudViewer是简单显示点云的可视化工具,可以让用户用尽可能少的代码查看点云。点云视窗类不能应用于多线程应用程序中。
/** \brief Construct a cloud viewer, with a window name.
* \param window_name This is displayed at the top of the window
*/
CloudViewer (const std::string& window_name);
/** \brief Show a cloud, with an optional key for multiple clouds.
* \param[in] cloud XYZ point cloud
* \param[in] cloudname a key for the point cloud, use the same name if you would like to overwrite the existing cloud.
*/
void
showCloud (const MonochromeCloud::ConstPtr &cloud, const std::string& cloudname = "cloud");
/** Visualization callable function, may be used for running things on the UI thread.
*/
using VizCallable = std::function<void (pcl::visualization::PCLVisualizer&)>;
/** \brief Run a callbable object on the UI thread. This will run once and be removed
* @param x Use boost::ref(x) for a function object that you would like to not copy
*/
void
runOnVisualizationThreadOnce (VizCallable x);
/** \brief Run a callbable object on the UI thread. Will persist until removed
* @param x Use boost::ref(x) for a function object that you would like to not copy
* \param key The key for the callable -- use the same key to overwrite.
*/
void
runOnVisualizationThread (VizCallable x, const std::string& key = "callable");
/** \brief Check if the gui was quit, you should quit also
* \param millis_to_wait This will request to "spin" for the number of milliseconds, before exiting.
* \return true if the user signaled the gui to stop
*/
bool
wasStopped (int millis_to_wait = 1);
/** \brief PCL Visualizer constructor.
* \param[in] name the window name (empty by default)
* \param[in] create_interactor if true (default), create an interactor, false otherwise
*/
PCLVisualizer (const std::string &name = "", const bool create_interactor = true);
/** \brief Set the viewport's background color.
* \param[in] r the red component of the RGB color
* \param[in] g the green component of the RGB color
* \param[in] b the blue component of the RGB color
* \param[in] viewport the view port (default: all)
*/
void
setBackgroundColor (const double &r, const double &g, const double &b, int viewport = 0);
/** \brief Add a sphere shape from a point and a radius
* \param[in] center the center of the sphere
* \param[in] radius the radius of the sphere
* \param[in] id the sphere id/name (default: "sphere")
* \param[in] viewport (optional) the id of the new viewport (default: 0)
*/
template <typename PointT> bool
addSphere (const PointT ¢er, double radius, const std::string &id = "sphere",
int viewport = 0);
/** \brief Removes an added shape from screen (line, polygon, etc.), based on a given ID
* \note This methods also removes PolygonMesh objects and PointClouds, if they match the ID
* \param[in] id the shape object id (i.e., given on \a addLine etc.)
* \param[in] viewport view port from where the Point Cloud should be removed (default: all)
*/
bool
removeShape (const std::string &id = "cloud", int viewport = 0);
/** \brief Add a text to screen
* \param[in] text the text to add
* \param[in] xpos the X position on screen where the text should be added
* \param[in] ypos the Y position on screen where the text should be added
* \param[in] id the text object id (default: equal to the "text" parameter)
* \param[in] viewport the view port (default: all)
*/
bool
addText (const std::string &text,
int xpos, int ypos,
const std::string &id = "", int viewport = 0);
完整程序:
#include //点云视窗类:CloudViewer头文件声明
#include
#include
#include
int user_data;
void
viewerOneOff (pcl::visualization::PCLVisualizer& viewer) // 设置回调函数viewerOneOff
{
viewer.setBackgroundColor (0.0, 1.0, 0.0); // 设置背景颜色
pcl::PointXYZ o;
o.x = 1.0;
o.y = 0;
o.z = 0;
viewer.addSphere (o, 0.25, "sphere", 0); // 添加一个球体
std::cout << "i only run once" << std::endl;
}
void
viewerPsycho (pcl::visualization::PCLVisualizer& viewer) // 设置回调函数viewerPsycho
{
static unsigned count = 0;
std::stringstream ss;
ss << "Once per viewer loop: " << count++;
viewer.removeShape ("text", 0); // 移走旧文字
viewer.addText (ss.str(), 200, 300, "text", 0); // 添加新文字
user_data++;
}
int
main ()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); // 创建一个PointCloud boost共享指针,并进行实例化为cloud
pcl::io::loadPCDFile ("my_point_cloud.pcd", *cloud); // 读取点云文件
pcl::visualization::CloudViewer viewer("Cloud Viewer"); // 创建一个可视化窗口
viewer.showCloud(cloud); // 在窗口中显示点云
viewer.runOnVisualizationThreadOnce (viewerOneOff); // 调用一次viewerOnOff
viewer.runOnVisualizationThread (viewerPsycho); // 持续调用viewerPsycho
while (!viewer.wasStopped ())
{
user_data++;
}
return 0;
}
如果您有修改意见或问题,欢迎留言或者通过邮箱和我联系。
手打很辛苦,如果我的文章对您有帮助,转载请注明出处。