offset_solver.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Created by zx on 2021/8/26.
  3. //
  4. #include "offset_solver.h"
  5. //
  6. // Created by zx on 2021/7/1.
  7. //
  8. #include <cppad/ipopt/solve.hpp>
  9. #include <glog/logging.h>
  10. #include <pcl/point_types.h>
  11. #include <pcl/point_cloud.h>
  12. #include <pcl/common/transforms.h>
  13. #include <pcl/filters/statistical_outlier_removal.h>
  14. #include <pcl/visualization/pcl_visualizer.h>
  15. using CppAD::AD;
  16. double CPPDA_PA=30.0;
  17. double CPPDA_PB=10.0;
  18. class affine_cppda_cost
  19. {
  20. public:
  21. pcl::PointCloud<pcl::PointXYZ>::Ptr m_cloud;
  22. double m_ox;
  23. double m_oy;
  24. affine_cppda_cost(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,double ox,double oy)
  25. :m_cloud(cloud),m_ox(ox),m_oy(oy)
  26. {}
  27. typedef CPPAD_TESTVECTOR( AD<double> ) ADvector;
  28. void operator()(ADvector& fg, const ADvector& x)
  29. {
  30. assert( fg.size() == 1 ); //误差
  31. assert( x.size() == 3);
  32. AD<double> cx = x[1];
  33. AD<double> cy = x[2];
  34. AD<double> ca = x[0];
  35. fg[0]=0;
  36. for(int i=0;i<m_cloud->size();++i)
  37. {
  38. pcl::PointXYZ p=m_cloud->points[i];
  39. double x=p.x-m_ox;
  40. double y=p.y-m_oy;
  41. AD<double> nx=CppAD::cos(ca)*x-CppAD::sin(ca)*y +cx;
  42. AD<double> ny=CppAD::sin(ca)*x+CppAD::cos(ca)*y +cy;
  43. fg[0]+=CppAD::pow(1.0/(1.0+CppAD::exp(CPPDA_PA*(nx+1.0)))
  44. +1.0/(1.0+CppAD::exp(-CPPDA_PA*(nx-1.0))),2);
  45. fg[0]+=CppAD::pow(1.0/(1.0+CppAD::exp(CPPDA_PB*(ny+2.4)))
  46. +1.0/(1.0+CppAD::exp(-CPPDA_PB*(ny-2.4))),2);
  47. }
  48. }
  49. };
  50. offset_solver::offset_solver(double minx,double maxx,double mina,double maxa)
  51. :m_minx(minx),m_maxx(maxx),m_mina(mina),m_maxa(maxa)
  52. {
  53. }
  54. Error_manager offset_solver::solve(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,
  55. pcl::PointXYZ rotate_center,float& cx,float& cy,float& theta)
  56. {
  57. Eigen::Vector4f centroid;
  58. pcl::compute3DCentroid(*cloud, centroid);
  59. LOG(INFO)<<" cloud size:"<<cloud->size()<<" , center :"<<-centroid[0]<<" ,"<<-centroid[1];
  60. double variable[] = {theta,cx,cy};
  61. bool ok = true;
  62. size_t i;
  63. typedef CPPAD_TESTVECTOR( double ) Dvector;
  64. // number of independent variables (domain dimension for f and g)
  65. size_t n_vars = 3;
  66. // number of constraints (range dimension for g)
  67. //size_t ng = 1;
  68. // initial value of the independent variables
  69. Dvector vars(n_vars);
  70. for (int i = 0; i < n_vars; i++) {
  71. vars[i] = 0.0;
  72. }
  73. Dvector vars_lowerbound(n_vars);
  74. Dvector vars_upperbound(n_vars);
  75. // Sets lower and upper limits for variables.
  76. // Set all non-actuators upper and lowerlimits
  77. // to the max negative and positive values.
  78. vars_lowerbound[0]=m_mina;
  79. vars_upperbound[0]=m_maxa;
  80. vars_lowerbound[1] = m_minx;
  81. vars_upperbound[1] = m_maxa;
  82. vars_lowerbound[2] = -1.0e19;
  83. vars_upperbound[2] = 1.0e19;
  84. affine_cppda_cost affine_cost(cloud,rotate_center.x,rotate_center.y);
  85. std::string options;
  86. // Uncomment this if you'd like more print information
  87. options += "Integer print_level 0\n";
  88. options += "Sparse true forward\n";
  89. options += "Sparse true reverse\n";
  90. options += "Numeric max_cpu_time 0.5\n";
  91. // place to return solution
  92. CppAD::ipopt::solve_result<Dvector> solution;
  93. // solve the problem
  94. CppAD::ipopt::solve<Dvector, affine_cppda_cost>(
  95. options, vars, vars_lowerbound, vars_upperbound, Dvector(),
  96. Dvector(), affine_cost, solution);
  97. // Check some of the solution values
  98. ok &= solution.status == CppAD::ipopt::solve_result<Dvector>::success;
  99. // Cost
  100. if(ok)
  101. {
  102. auto cost = solution.obj_value;
  103. theta = solution.x[0];
  104. cx = solution.x[1];
  105. cy = solution.x[2];
  106. printf(" cppad solver theta:%.3f , x;%.5f , y:%.5f\n",
  107. theta * 180.0 / M_PI, cx, cy);
  108. return SUCCESS;
  109. }
  110. return FAILED;
  111. }
  112. Error_manager offset_solver::solve(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,
  113. pcl::PointXYZ rotate_center,float& cx,float& cy,float& theta,
  114. pcl::visualization::PCLVisualizer& viewer)
  115. {
  116. Error_manager code=solve(cloud,rotate_center,cx,cy,theta);
  117. if(code!=SUCCESS)
  118. {
  119. viewer.addText(code.get_error_description(),50,50,1,0,0,"offset_solve_error_code");
  120. return code;
  121. }
  122. //先平移点云,后旋转,再平移
  123. Eigen::Affine3f transform_2 = Eigen::Affine3f::Identity();
  124. pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud(new pcl::PointCloud<pcl::PointXYZ>());
  125. transform_2.translation() << -rotate_center.x, -rotate_center.y, 0;
  126. pcl::transformPointCloud(*cloud, *transformed_cloud, transform_2);
  127. Eigen::Affine3f transform_r = Eigen::Affine3f::Identity();
  128. pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud1(new pcl::PointCloud<pcl::PointXYZ>());
  129. transform_r.rotate(Eigen::AngleAxisf(theta, Eigen::Vector3f::UnitZ()));
  130. pcl::transformPointCloud(*transformed_cloud, *transformed_cloud1, transform_r);
  131. pcl::PointCloud<pcl::PointXYZ>::Ptr transformed_cloud2(new pcl::PointCloud<pcl::PointXYZ>());
  132. Eigen::Affine3f transform_t = Eigen::Affine3f::Identity();
  133. transform_t.translation() << cx, 0, 0;
  134. pcl::transformPointCloud(*transformed_cloud1, *transformed_cloud2, transform_t);
  135. //创建坐标函数点云,用于显示
  136. pcl::PointCloud<pcl::PointXYZ>::Ptr viewer_cloud(new pcl::PointCloud<pcl::PointXYZ>);
  137. for (float x = -2; x < 2; x += 0.01)
  138. {
  139. float y = 4.0 * (1.0 / (1.0 + exp(CPPDA_PA * (x + 1.0)))
  140. + 1.0 / (1.0 + exp(-CPPDA_PA * (x - 1.0))) - 1) + 1;
  141. viewer_cloud->push_back(pcl::PointXYZ(x, y, 0));
  142. float zx = 4.0 * (1.0 / (1.0 + exp(CPPDA_PB * (3 * x + 2.4)))
  143. + 1.0 / (1.0 + exp(-CPPDA_PB * (3 * x - 2.4))) - 1) + 2.5;
  144. viewer_cloud->push_back(pcl::PointXYZ(zx, 3 * x, 0));
  145. }
  146. viewer.removeAllPointClouds();
  147. viewer.addPointCloud(transformed_cloud2, "cloud_");
  148. pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0, 255, 0); // green
  149. viewer.addPointCloud(viewer_cloud, single_color, "cloud_axis");
  150. viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "cloud_axis");
  151. viewer.spinOnce();
  152. return code;
  153. }