Skip to content

Instantly share code, notes, and snippets.

@keineahnung2345
Last active September 15, 2021 13:24
Show Gist options
  • Save keineahnung2345/f3f323e4c643770036fcdf59a648e007 to your computer and use it in GitHub Desktop.
Save keineahnung2345/f3f323e4c643770036fcdf59a648e007 to your computer and use it in GitHub Desktop.
pcl mls upsampling test revised from https://github.com/PointCloudLibrary/pcl/issues/1958 for PCL 1.12
set (CMAKE_CXX_STANDARD 14)
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(MY_GRAND_PROJECT)
# specify the components required
#find_package(PCL 1.10 REQUIRED COMPONENTS common io)
# or use all available components
find_package(PCL 1.12 PATHS "$ENV{HOME}/Documents/pcl/build")
#find_package(PCL 1.10 REQUIRED)
include_directories("/usr/include/eigen3")
include_directories("$ENV{HOME}/Documents/pcl")
include_directories("$ENV{HOME}/Documents/pcl/common/include")
include_directories("$ENV{HOME}/Documents/pcl/surface/include")
include_directories("$ENV{HOME}/Documents/pcl/io/include")
include_directories("$ENV{HOME}/Documents/pcl/kdtree/include")
include_directories("$ENV{HOME}/Documents/pcl/search/include")
include_directories("$ENV{HOME}/Documents/pcl/build/include")
link_directories("$ENV{HOME}/Documents/pcl/build/lib")
add_definitions(${PCL_DEFINITIONS})
add_definitions("-DDISABLE_PCAP -DDISABLE_PNG -DDISABLE_LIBUSB_1_0")
message(STATUS "include: ${PCL_INCLUDE_DIRS}")
message(STATUS "def: ${PCL_DEFINITIONS}")
message(STATUS "lib: ${PCL_LIBRARIES}")
add_executable(random_upsampling random_upsampling.cpp)
target_link_libraries(random_upsampling pcl_common pcl_surface pcl_io pcl_kdtree pcl_search ${PCL_LIBRARIES})
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/vtk_io.h>
#include <pcl/surface/mls.h>
#include <pcl/search/kdtree.h>
using namespace pcl;
using namespace pcl::io;
int
main (int argc, char** argv)
{
PointCloud<PointXYZ>::Ptr cloud (new PointCloud<PointXYZ>);
PointCloud<PointXYZ>::Ptr cloud2 (new PointCloud<PointXYZ>);
search::KdTree<PointXYZ>::Ptr tree;
int gridSize = 50;
for(unsigned int x = 0; x < gridSize; x++)
{
for(unsigned int y = 0; y < gridSize; y++)
{
double d = 0.001 * ( (double)rand() / (double)RAND_MAX );
pcl::PointXYZ pt(x / 10.0 , y / 10.0 , 0.5 * cos(double(x)/10.0) - 0.5 * sin(double(y)/10.0) + d);
cloud->push_back(pt);
if(std::abs((int)x+(int)y-50)<=10){
pcl::PointXYZ pt2(x / 10.0 , y / 10.0 , 0.5 * sin(double(x)/10.0) - 0.5 * cos(double(y)/10.0) + d);
cloud2->push_back(pt2);
}
}
}
// cloud->is_dense = false;
if (!cloud->empty())
{
std::string file_path = "cloud.pcd";
pcl::io::savePCDFile(file_path, *cloud);
PCL_INFO("Wrote file: %s\n", file_path.c_str());
}
if (!cloud2->empty())
{
std::string file_path = "cloud2.pcd";
pcl::io::savePCDFile(file_path, *cloud2);
PCL_INFO("Wrote file: %s\n", file_path.c_str());
}
// Create search tree
tree.reset (new search::KdTree<PointXYZ> (false));
tree->setInputCloud (cloud);
// Init objects
PointCloud<PointXYZ> mls_points;
PointCloud<PointNormal>::Ptr mls_normals (new PointCloud<PointNormal> ());
MovingLeastSquares<PointXYZ, PointNormal> mls;
// Set parameters
mls.setInputCloud (cloud);
mls.setComputeNormals (true);
mls.setPolynomialOrder (2);
mls.setSearchMethod (tree);
mls.setSearchRadius (0.5);
// Reconstruct
mls.process (*mls_normals);
std::cout << "processed size: " << mls_normals->size() << std::endl;
if (!mls_normals->empty())
{
std::string file_path = "none.pcd";
pcl::io::savePCDFile(file_path, *mls_normals);
PCL_INFO("Wrote file: %s\n", file_path.c_str());
}
// Testing upsampling
MovingLeastSquares<PointXYZ, PointNormal> mls_sample_local_plane;
// Set parameters
mls_sample_local_plane.setInputCloud (cloud);
mls_sample_local_plane.setComputeNormals (true);
mls_sample_local_plane.setPolynomialOrder (2);
mls_sample_local_plane.setSearchMethod (tree);
mls_sample_local_plane.setSearchRadius (0.5);
mls_sample_local_plane.setUpsamplingMethod (MovingLeastSquares<PointXYZ, PointNormal>::SAMPLE_LOCAL_PLANE);
mls_sample_local_plane.setUpsamplingRadius (0.1);
mls_sample_local_plane.setUpsamplingStepSize (0.05);
mls_normals->clear ();
PointCloud<PointNormal>::Ptr cloud_sample_local_plane (new PointCloud<PointNormal> ());
mls_sample_local_plane.process (*cloud_sample_local_plane);
std::cout << "processed size: " << cloud_sample_local_plane->size() << std::endl;
if (!cloud_sample_local_plane->empty())
{
std::string file_path = "sample_local_plane.pcd";
pcl::io::savePCDFile(file_path, *cloud_sample_local_plane);
PCL_INFO("Wrote file: %s\n", file_path.c_str());
}
MovingLeastSquares<PointXYZ, PointNormal> mls_voxel_grid_dilation;
PointCloud<PointNormal>::Ptr cloud_voxel_grid_dilation (new PointCloud<PointNormal> ());
mls_voxel_grid_dilation.setInputCloud (cloud);
mls_voxel_grid_dilation.setComputeNormals (true);
mls_voxel_grid_dilation.setPolynomialOrder (2);
mls_voxel_grid_dilation.setSearchMethod (tree);
mls_voxel_grid_dilation.setSearchRadius (0.5);
mls_voxel_grid_dilation.setUpsamplingMethod (MovingLeastSquares<PointXYZ, PointNormal>::VOXEL_GRID_DILATION);
mls_voxel_grid_dilation.setDilationIterations (0);
mls_voxel_grid_dilation.setDilationVoxelSize (0.2f);
mls_voxel_grid_dilation.process (*cloud_voxel_grid_dilation);
std::cout << "processed size: " << cloud_voxel_grid_dilation->size() << std::endl;
if (!cloud_voxel_grid_dilation->empty())
{
std::string file_path = "voxel_grid_dilation.pcd";
pcl::io::savePCDFile(file_path, *cloud_voxel_grid_dilation);
PCL_INFO("Wrote file: %s\n", file_path.c_str());
}
PointCloud<PointNormal>::Ptr cloud_distinct_cloud (new PointCloud<PointNormal> ());
MovingLeastSquares<PointXYZ, PointNormal> mls_distinct_cloud;
mls_distinct_cloud.setInputCloud (cloud);
mls_distinct_cloud.setComputeNormals (true);
mls_distinct_cloud.setPolynomialOrder (2);
mls_distinct_cloud.setSearchMethod (tree);
mls_distinct_cloud.setSearchRadius (0.5);
mls_distinct_cloud.setUpsamplingMethod(pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointNormal>::DISTINCT_CLOUD);
mls_distinct_cloud.setDistinctCloud(cloud2);
mls_distinct_cloud.process(*cloud_distinct_cloud);
std::cout << "processed size: " << cloud_distinct_cloud->size() << std::endl;
if (!cloud_distinct_cloud->empty())
{
std::string file_path = "distinct_cloud.pcd";
pcl::io::savePCDFile(file_path, *cloud_distinct_cloud);
PCL_INFO("Wrote file: %s\n", file_path.c_str());
}
MovingLeastSquares<PointXYZ, PointNormal> mls_random_uniform_density;
PointCloud<PointNormal>::Ptr cloud_random_uniform_density (new PointCloud<PointNormal> ());
mls_random_uniform_density.setComputeNormals(true);
mls_random_uniform_density.setInputCloud(cloud);
mls_random_uniform_density.setPolynomialOrder(2);
mls_random_uniform_density.setSearchMethod(tree);
mls_random_uniform_density.setSearchRadius(0.5);
mls_random_uniform_density.setUpsamplingMethod(pcl::MovingLeastSquares<pcl::PointXYZ, pcl::PointNormal>::RANDOM_UNIFORM_DENSITY);
mls_random_uniform_density.setPointDensity(500);
mls_random_uniform_density.process(*cloud_random_uniform_density);
std::cout << "processed size: " << cloud_random_uniform_density->size() << std::endl;
if (!cloud_random_uniform_density->empty())
{
std::string file_path = "random_uniform_density.pcd";
pcl::io::savePCDFile(file_path, *cloud_random_uniform_density);
PCL_INFO("Wrote file: %s\n", file_path.c_str());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment