0

I am trying to generate mesh from a point cloud using Marching Cubes algorithm implemented in PCL. I am adapting the code given at https://github.com/atduskgreg/pcl-marching-squares-example/blob/master/marching_cubes.cpp to my point cloud (my code is given below). Visual Studio 2015 successfully builds but when I run it I get an error message about debug assertion fail. The expression is "Vector subscript out of range". Can you help me about this error message? My another question is if I can successfully generate the polygon mesh, how can I export it in .obj, .ply or .wrl format? I am looking forward to hearing from you Regards

#include <iostream>
#include <pcl/point_cloud.h>
#include <pcl/octree/octree.h>
#include<conio.h>
#include <iostream>
#include <vector>
#include <ctime>
#include <pcl/io/pcd_io.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <pcl/surface/3rdparty/poisson4/geometry.h>
#include <pcl/registration/distances.h>
#include <pcl/common/distances.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/search/kdtree.h>
#include <pcl/features/normal_3d_omp.h>
#include <pcl/surface/marching_cubes_rbf.h>
#include <pcl/surface/marching_cubes_hoppe.h>
#include <pcl/surface/marching_cubes.h>

int 
main(int argc, char** argv) 
{ 
        srand((unsigned int)time(NULL)); 
        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); 
        //pcd'ye dönüştürülen dosyanın okunması 
        pcl::io::loadPCDFile<pcl::PointXYZ>("silindir_arka_ENTIRE_DATA_ELIMINATED_REG_PCD.pcd", *cloud); 
        pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne; 
        ne.setInputCloud(cloud); 
        pcl::search::KdTree<pcl::PointXYZ>::Ptr tree1(new pcl::search::KdTree<pcl::PointXYZ>()); 
        tree1->setInputCloud(cloud); 
        ne.setInputCloud(cloud); 
        ne.setSearchMethod(tree1); 
        ne.setKSearch(20); 
        pcl::PointCloud<pcl::Normal>::Ptr cloud_normals(new pcl::PointCloud<pcl::Normal>); 
        ne.compute(*cloud_normals); 

        pcl::PointCloud<pcl::PointNormal>::Ptr cloud_with_normals(new pcl::PointCloud<pcl::PointNormal>); 
        pcl::concatenateFields(*cloud, *cloud_normals, *cloud_with_normals); 
        cloud_with_normals->points[0].normal_x; 

        std::cout << cloud_with_normals->points[0].normal_x << " " << cloud_with_normals->points[0].normal_y << " " << cloud_with_normals->points[0].normal_z << std::endl; 
        pcl::search::KdTree<pcl::PointNormal>::Ptr tree(new pcl::search::KdTree<pcl::PointNormal>()); 
        tree->setInputCloud(cloud_with_normals); 
        std::cout << "begin marching cubes reconstruction" << std::endl; 

        pcl::MarchingCubesHoppe<pcl::PointNormal> mc; 
        pcl::PolygonMesh::Ptr triangles(new pcl::PolygonMesh); 

        std::cout << "111" << std::endl; 
        mc.setInputCloud(cloud_with_normals); 
        std::cout << "222" << std::endl; 
        mc.setSearchMethod(tree); 
        std::cout << "333" << std::endl; 
        mc.reconstruct(*triangles); 

        std::cout << triangles->polygons.size() << " triangles created" << std::endl; 

        return(0); 

} 
mozendi
  • 85
  • 1
  • 11

1 Answers1

0

It looks like the vector subscript out of range comes from indexing into cloud_with_normals at [0]. I would check the size of cloud and cloud_normals and see if concatenateFields() has the output you expect. Since you are using Visual Studio, you can use the debugging tools to see when the unexpected behavior starts.

brad
  • 930
  • 9
  • 22