pose_sampling_helpers.cpp
Go to the documentation of this file.
1 //
2 // Created by yijiangh on 4/9/18.
3 //
4 
6 
7 // opened for external usage without linking to library
8 namespace descartes_planner
9 {
10 
11 std::vector<Eigen::Vector3d> discretizePositions(const Eigen::Vector3d& start, const Eigen::Vector3d& stop, const double ds)
12 {
13  auto dist = (stop - start).norm();
14 
15  size_t n_intermediate_points = 0;
16  if (dist > ds)
17  {
18  n_intermediate_points = static_cast<size_t>(std::lround(dist / ds));
19  }
20 
21  const auto total_points = 2 + n_intermediate_points;
22 
23  std::vector<Eigen::Vector3d> result;
24  result.reserve(total_points);
25 
26  for (std::size_t i = 0; i < total_points; ++i)
27  {
28  const double r = i / static_cast<double>(total_points - 1);
29  Eigen::Vector3d point = start + (stop - start) * r;
30  result.push_back(point);
31  }
32  return result;
33 }
34 
35 Eigen::Affine3d makePose(const Eigen::Vector3d& position, const Eigen::Matrix3d& orientation,
36  const double z_axis_angle)
37 {
38  Eigen::Affine3d m = Eigen::Affine3d::Identity();
39  m.matrix().block<3,3>(0,0) = orientation;
40  m.matrix().col(3).head<3>() = position;
41 
42  Eigen::AngleAxisd z_rot (z_axis_angle, Eigen::Vector3d::UnitZ());
43 
44  return m * z_rot;
45 }
46 
47 Eigen::Affine3d makePose(const Eigen::Vector3d& position, const Eigen::Matrix3d& orientation)
48 {
49  Eigen::Affine3d m = Eigen::Affine3d::Identity();
50  m.matrix().block<3,3>(0,0) = orientation;
51  m.matrix().col(3).head<3>() = position;
52 
53  return m;
54 }
55 
56 int randomSampleInt(int lower, int upper)
57 {
58  std::random_device rd;
59  std::mt19937 gen(rd());
60 
61  if(upper > lower)
62  {
63  std::uniform_int_distribution<int> int_distr(lower, upper);
64  return int_distr(gen);
65  }
66  else
67  {
68  return lower;
69  }
70 }
71 
72 double randomSampleDouble(double lower, double upper)
73 {
74  std::random_device rd;
75  std::mt19937 gen(rd());
76 
77  if(upper > lower)
78  {
79  std::uniform_real_distribution<double> double_distr(lower, upper);
80  return double_distr(gen);
81  }
82  else
83  {
84  return lower;
85  }
86 }
87 
88 // TODO: original generate sampling function used only in spatial extrusion
89 std::vector<Eigen::Affine3d> generateSample(const descartes_planner::CapRung& cap_rung,
91 {
92  // sample int for orientation
93  int o_sample = randomSampleInt(0, cap_rung.orientations_[0].size()-1);
94 
95  assert(cap_rung.orientations_.size() > 0);
96  assert(cap_rung.orientations_[0].size() > o_sample);
97 
98  Eigen::Matrix3d orientation_sample = cap_rung.orientations_[0][o_sample];
99 
100  // sample [0,1] for axis, z_axis_angle = b_rand * 2 * Pi
101 
102  double x_axis_sample = randomSampleDouble(0.0, 1.0) * 2 * M_PI;
103 
104  assert(cap_rung.path_pts_.size() > 0);
105  assert(cap_rung.path_pts_[0].size() > 0);
106 
107  std::vector<Eigen::Affine3d> poses;
108  poses.reserve(cap_rung.path_pts_[0].size());
109  for(const auto& pt : cap_rung.path_pts_[0])
110  {
111  poses.push_back(makePose(pt, orientation_sample, x_axis_sample));
112  }
113 
114  // TODO: should be a std::vector too?
115  cap_vert.z_axis_angle_ = x_axis_sample;
116 
117  // spatial extrusion only gets one kinematics segment
118  cap_vert.orientation_.resize(1);
119  cap_vert.orientation_[0] = orientation_sample;
120 
121  return poses;
122 }
123 
124 // TODO: should be more universal, not only for pnp
125 std::vector<std::vector<Eigen::Affine3d>> generateSamplePickNPlace(const descartes_planner::CapRung& cap_rung,
126  descartes_planner::CapVert& cap_vert)
127 {
128  // sample int for orientation
129  int o_sample = randomSampleInt(0, cap_rung.orientations_[0].size()-1);
130 
131  // this index is applied to orientation candidates in each kinematics family
132  assert(cap_rung.path_pts_.size() == cap_rung.sub_segment_ids_.size());
133 
134  cap_vert.orientation_.resize(cap_rung.path_pts_.size());
135  std::vector<std::vector<Eigen::Affine3d>> poses(cap_rung.path_pts_.size());
136 
137  // for each kinematics family
138  for(int i=0; i<cap_rung.path_pts_.size(); i++)
139  {
140  assert(cap_rung.orientations_[0].size() == cap_rung.orientations_[i].size());
141 
142  const auto orient = cap_rung.orientations_[i][o_sample];
143 
144  cap_vert.orientation_[i] = orient;
145 
146  poses[i].reserve(cap_rung.path_pts_[0].size());
147 
148  for(const auto &pt : cap_rung.path_pts_[i])
149  {
150  poses[i].push_back(makePose(pt, orient));
151  }
152  }
153 
154  return poses;
155 }
156 
157 }
std::vector< Eigen::Vector3d > discretizePositions(const Eigen::Vector3d &start, const Eigen::Vector3d &stop, const double ds)
#define M_PI
std::vector< std::vector< Eigen::Matrix3d > > orientations_
std::vector< std::vector< Eigen::Affine3d > > generateSamplePickNPlace(const descartes_planner::CapRung &cap_rung, descartes_planner::CapVert &cap_vert)
std::vector< std::vector< Eigen::Vector3d > > path_pts_
std::vector< Eigen::Matrix3d > orientation_
int randomSampleInt(int lower, int upper)
Eigen::Affine3d makePose(const Eigen::Vector3d &position, const Eigen::Matrix3d &orientation, const double z_axis_angle)
r
std::vector< Eigen::Affine3d > generateSample(const descartes_planner::CapRung &cap_rung, descartes_planner::CapVert &cap_vert)
double randomSampleDouble(double lower, double upper)


choreo_descartes_planner
Author(s): Yijiang Huang , Jonathan Meyer
autogenerated on Thu Jul 18 2019 03:58:35