bench_ransac_plane_estimation.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2015, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 #include <iostream>
37 #include <pcl/point_cloud.h>
38 #include <pcl/point_types.h>
39 #include <pcl/sample_consensus/method_types.h>
40 #include <pcl/sample_consensus/model_types.h>
41 #include <pcl/segmentation/sac_segmentation.h>
42 #include <ctime>
43 #include <boost/random.hpp>
44 #include <string>
45 #include <typeinfo>
46 #include <sstream>
47 #include<stdio.h>
48 #include <sys/time.h>
49 
50 void usage(char** argv)
51 {
52  std::cerr << "Usage: " << argv[0] << " Iteration Number-of-points Max-iterations Gaussian-noise-variance Outlier-threshold" << std::endl;
53 }
54 
55 pcl::PointCloud<pcl::PointXYZ>::Ptr generatePoints(const int num_points, const double variance)
56 {
57  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
58  boost::mt19937 gen = boost::mt19937(static_cast<unsigned long>(time(0)));
59  boost::normal_distribution<> dst(0.0, sqrt(variance));
60  boost::variate_generator<
61  boost::mt19937&,
62  boost::normal_distribution<> > rand(gen, dst);
63 
64  const double dx = 0.01;
65  cloud->points.resize(num_points * num_points);
66  for (size_t i = 0; i < num_points; i++) {
67  for (size_t j = 0; j < num_points; j++) {
68  pcl::PointXYZ p;
69  p.x = i * dx;
70  p.y = j * dx;
71  p.z = rand();
72  cloud->points[i * num_points + j] = p;
73  }
74  }
75  return cloud;
76 }
77 
78 int toInt(char* argv)
79 {
80  std::stringstream ss(argv);
81  int val = 0;
82  ss >> val;
83  return val;
84 }
85 
86 double toDouble(char* argv)
87 {
88  std::stringstream ss(argv);
89  double val = 0;
90  ss >> val;
91  return val;
92 }
93 
94 int main(int argc, char** argv)
95 {
96  if (argc != 6) {
97  usage(argv);
98  return 1;
99  }
100  int iteration = toInt(argv[1]);
101  int number_of_points = toInt(argv[2]);
102  int max_iterations = toInt(argv[3]);
103  double variance = toDouble(argv[4]);
104  double outlier_threshold = toDouble(argv[5]);
105  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud = generatePoints(number_of_points, variance);
106  pcl::SACSegmentation<pcl::PointXYZ> seg;
107  seg.setOptimizeCoefficients (true);
108  seg.setRadiusLimits(0.0, std::numeric_limits<double>::max ()); // 0.3?
109  seg.setMethodType (pcl::SAC_RANSAC);
110  seg.setDistanceThreshold (outlier_threshold);
111  seg.setInputCloud(cloud);
112  seg.setMaxIterations (max_iterations);
113  seg.setModelType (pcl::SACMODEL_PLANE);
114  pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
115  pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
116  struct timeval s, e;
117  gettimeofday(&s, NULL);
118  for (size_t i = 0; i < iteration; i++) {
119  seg.segment (*inliers, *coefficients);
120  }
121  gettimeofday(&e, NULL);
122  // Total, each, iteration, number_of_points, max_iterations, variance, outlier_threshold
123  double time = (e.tv_sec - s.tv_sec) + (e.tv_usec - s.tv_usec)*1.0E-6;
124  printf("%lf,%lf,%d,%d,%d,%f,%f\n", time, time / iteration,
125  iteration, number_of_points * number_of_points, max_iterations, variance, outlier_threshold);
126  std::cerr << "Took " << time << " sec (" << time / iteration << " sec for each)" << std::endl;
127  return 0;
128 }
double max(const OneDataStat &d)
wrapper function for max method for boost::function
coefficients
void usage(char **argv)
double toDouble(char *argv)
int main(int argc, char **argv)
double sqrt()
pcl::PointCloud< pcl::PointXYZ >::Ptr generatePoints(const int num_points, const double variance)
int toInt(char *argv)
p
#define NULL
cloud


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:46