Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <coverage_3d_planning/TSPSolver.h>
00010 #include <coverage_3d_planning/TSPSearch.h>
00011 #include <coverage_3d_tools/conversions.h>
00012
00013 TSPSolver::TSPSolver() {
00014 dist_measure_="DIST";
00015 max_adjacency_=500;
00016 }
00017 TSPSolver::TSPSolver(double max_adjacency) {
00018 dist_measure_="DIST";
00019 max_adjacency_=max_adjacency;
00020 }
00021
00022 void TSPSolver::getSolution(std::vector<std::vector<double> >& joint_path, std::vector<tf::Pose>& pose_path,
00023 std::vector<unsigned int>& action, std::vector<unsigned int>& res_traj) {
00024
00025 joint_path.clear();
00026 pose_path.clear();
00027 action.clear();
00028
00029
00030 std::map<int, int> index_map;
00031 Eigen::MatrixXf weights;
00032 computeWeightMatrix(weights);
00033
00034 TSPSearch tsp;
00035 tsp.writeToTSPFile(weights);
00036 tsp.callTSPSolver();
00037 std::vector<unsigned int> traj;
00038 tsp.readResultTSPFile(traj);
00039
00040 traj.erase(traj.begin());
00041 std::vector<SurfacePatch> res_patch;
00042 for (unsigned int i = 0; i < traj.size(); ++i) {
00043 traj[i] = traj[i] - 1;
00044 res_patch.push_back(patches_[traj[i]]);
00045 }
00046 std::vector<unsigned int> actions;
00047 getActions(traj, actions);
00048
00049 std::vector<std::vector<std::vector<double> > > ik_solutions_to_optimize;
00050
00051 for (unsigned int i = 0; i < traj.size(); ++i) {
00052
00053 int curr_node = traj[i];
00054 int next_node = traj[i + 1];
00055
00056 if (i < traj.size() - 1) {
00057
00058 if ((adjacency_(curr_node, next_node) == 1)) {
00059
00060
00061 int local_edge_nb = -1;
00062 for (int j = 0; j <= next_node; ++j) {
00063 local_edge_nb += adjacency_(curr_node, j);
00064 }
00065
00066 ik_solutions_to_optimize.push_back(coll_free_ik_[curr_node][local_edge_nb]);
00067
00068 pose_path.push_back(coll_free_poses_[curr_node][local_edge_nb]);
00069 } else {
00070 ROS_WARN("TSPSolver::getSolution Path planning edge taken --> should usually not happen.");
00071 std::vector<std::vector<std::vector<double> > > tmp = coll_free_ik_[traj[i]];
00072 std::vector<std::vector<double> > tmp_joints;
00073 for (unsigned int j = 0; j < tmp.size(); ++j) {
00074 for (unsigned int k = 0; k < tmp[j].size(); ++k) {
00075 tmp_joints.push_back(tmp[j][k]);
00076 }
00077
00078 }
00079 ik_solutions_to_optimize.push_back(tmp_joints);
00080 pose_path.push_back(coll_free_poses_[curr_node][0]);
00081 }
00082 } else {
00083 std::vector<std::vector<std::vector<double> > > tmp = coll_free_ik_[traj[i]];
00084 std::vector<std::vector<double> > tmp_joints;
00085 for (unsigned int j = 0; j < tmp.size(); ++j) {
00086 for (unsigned int k = 0; k < tmp[j].size(); ++k) {
00087 tmp_joints.push_back(tmp[j][k]);
00088 }
00089
00090 }
00091 ik_solutions_to_optimize.push_back(tmp_joints);
00092 pose_path.push_back(coll_free_poses_[curr_node][0]);
00093 }
00094 }
00095 std::vector<std::vector<double> > tmp_joint_start;
00096 tmp_joint_start.push_back(start_joint_);
00097 ik_solutions_to_optimize.insert(ik_solutions_to_optimize.begin(), tmp_joint_start);
00098 ik_solutions_to_optimize.push_back(tmp_joint_start);
00099
00100 std::vector<std::vector<double> > path;
00101 std::vector<bool> valid_poses(ik_solutions_to_optimize.size(), true);
00102 traj_planner_.computeDijkstraPath(ik_solutions_to_optimize, valid_poses, path,dist_measure_);
00103 joint_path = path;
00104
00105 res_traj = traj;
00106 pose_path.push_back(start_pose_);
00107 pose_path.insert(pose_path.begin(), start_pose_);
00108 actions.push_back(1);
00109 actions.insert(actions.begin(), 1);
00110 action = actions;
00111
00112 }
00113
00114 void TSPSolver::computeWeightMatrix(Eigen::MatrixXf& weigths) {
00115
00116
00117 Eigen::Vector3f start = PoseTF2EigenVector3(start_pose_);
00118 weigths = Eigen::MatrixXf::Ones(adjacency_.rows() + 1, adjacency_.rows() + 1) * max_adjacency_;
00119 for (int i = 1; i < weigths.rows(); ++i) {
00120
00121 weigths(i, 0) = (start - surface_points_[i - 1].getVector3fMap()).norm();
00122 weigths(0, i) = weigths(i, 0);
00123 }
00124
00125 for (int i = 1; i < weigths.rows(); ++i) {
00126 for (int j = 1; j < weigths.cols(); ++j) {
00127 if (adjacency_(i - 1, j - 1) == 1) {
00128 weigths(i, j)
00129 = (surface_points_[i - 1].getVector3fMap() - surface_points_[j - 1].getVector3fMap()).norm();
00130 } else {
00131 weigths(i, j)
00132 += (surface_points_[i - 1].getVector3fMap() - surface_points_[j - 1].getVector3fMap()).norm();
00133
00134 }
00135 }
00136 }
00137 }
00138
00139 void TSPSolver::getActions(const std::vector<unsigned int>& traj, std::vector<unsigned int>& actions) {
00140
00141 actions.clear();
00142
00143 for (unsigned int i = 0; i < traj.size(); ++i) {
00144
00145 if (i < (traj.size() - 1)) {
00146
00147 if (adjacency_(traj[i], traj[i + 1]) == 1) {
00148 actions.push_back(0);
00149 } else {
00150 actions.push_back(1);
00151 }
00152 } else {
00153 actions.push_back(1);
00154 }
00155 }
00156 }