Segmentation fault when implementing moving least squares method in ROS
Hi everyone,
I am trying to implement the moving least squares method in ROS. However, every time when I run the following code, there is a strange error saying "Segmentation fault (core dumped)". This occurs at line "movingLS.process(mlsPoints);". Here the code piece,
void moving_least_squares_node::MovingLeastSquares::processing(const sensor_msgs::PointCloud2ConstPtr input_msg) {
pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointNormal> movingLS;
pcl::PointCloud<pcl::PointNormal> mlsPoints;
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudFiltered(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointNormal>::Ptr cloudFilteredWithNormals(
new pcl::PointCloud<pcl::PointNormal>);
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
pcl::fromROSMsg(*input_msg, *cloud);
tree->setInputCloud(cloud);
movingLS.setInputCloud(cloud);
movingLS.setComputeNormals(true);
movingLS.setPolynomialOrder(3);
movingLS.setSearchMethod(tree);
movingLS.setSearchRadius(0.06);
// Reconstruct
movingLS.process(mlsPoints);
pcl::copyPointCloud(mlsPoints, *cloudFiltered);
pcl::copyPointCloud(mlsPoints, *cloudFilteredWithNormals);
pcl::toROSMsg(*cloudFiltered, output_msg);
pub.publish(output_msg);
}
Also, the same problem occurs in this code,
pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr output_cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointNormal>::Ptr output_cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>);
pcl::fromROSMsg(*input_msg, *input_cloud);
pcl::io::loadPCDFile<pcl::PointXYZ>("/home/jc/hull_abstraction/prototype/point_cloud_data/16_5.pcd", *input_cloud);
pcl::ApproximateVoxelGrid<pcl::PointXYZ> voxelGrid;
voxelGrid.setInputCloud(input_cloud);
voxelGrid.setLeafSize(0.01f, 0.01f, 0.01f); //Volume of voxel(AABB) is 1 cm3
voxelGrid.setDownsampleAllData(false); //No down sampling based on RGB information
voxelGrid.filter(*output_cloud);
pcl::toROSMsg(*output_cloud, output_msg);
output_msg.header.frame_id = "base";
pub.publish(output_msg);
Here is my CMakeLists.txt,
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(hull_abstraction)
find_package(PCL 1.10 REQUIRED) # find PCL library and define the required version
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
find_package(catkin REQUIRED COMPONENTS
pcl_conversions
pcl_msgs
rospy
sensor_msgs
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES hull_abstraction
# CATKIN_DEPENDS pcl_conversions pcl_msgs roscpp sensor_msgs std_msgs
# DEPENDS system_lib
)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
# Load pcd flie
SET(load_pcd
src/load_pcd_node.cpp
src/load_pcd_node/load_pcd.cpp
)
add_executable(load_pcd ${load_pcd})
target_link_libraries(load_pcd
${PCL_LIBRARIES}
${catkin_LIBRARIES}
)
# Moving least squares method
SET(moving_least_squares
src/moving_least_squares_node.cpp
src/moving_least_squares_node/moving_least_squares.cpp
src/hull_abstraction/functions.cpp
)
add_executable(moving_least_squares ${moving_least_squares})
target_link_libraries(moving_least_squares
${PCL_LIBRARIES}
${catkin_LIBRARIES}
)
Any clues and suggestions would be appreciated! Thanks for every help!!