ed_sensor_integration
beam_model.cpp
Go to the documentation of this file.
1 #include "ed/kinect/beam_model.h"
2 
3 // ----------------------------------------------------------------------------------------------------
4 
5 BeamModel::BeamModel() : half_num_beams_(0) {}
6 
7 // ----------------------------------------------------------------------------------------------------
8 
9 BeamModel::BeamModel(double w, unsigned int num_beams)
10 {
12 }
13 
14 // ----------------------------------------------------------------------------------------------------
15 
16 void BeamModel::initialize(double w, unsigned int num_beams)
17 {
19  fx_ = 2 * num_beams / w;
20 
21  rays_.resize(num_beams);
22  for(unsigned int i = 0; i < num_beams; ++i)
23  rays_[i] = geo::Vec2(((double)(i) - half_num_beams_) / fx_, 1);
24 }
25 
26 // ----------------------------------------------------------------------------------------------------
27 
28 void BeamModel::RenderModel(const std::vector<std::vector<geo::Vec2> >& contours, const geo::Transform2& pose, int identifier,
29  std::vector<double>& ranges, std::vector<int>& identifiers) const
30 {
31  double near_plane = 0.01;
32 
33  geo::Vec2 p1_temp, p2_temp;
34 
35  for(std::vector<std::vector<geo::Vec2> >::const_iterator it_contour = contours.begin(); it_contour != contours.end(); ++it_contour)
36  {
37  const std::vector<geo::Vec2>& model = *it_contour;
38 
39  std::vector<geo::Vec2> t_vertices(model.size());
40  for(unsigned int i = 0; i < model.size(); ++i)
41  t_vertices[i] = pose * model[i];
42 
43  int nbeams = num_beams();
44 
45  for(unsigned int i = 0; i < model.size(); ++i)
46  {
47  unsigned int j = (i + 1) % model.size();
48  const geo::Vec2* p1 = &t_vertices[i];
49  const geo::Vec2* p2 = &t_vertices[j];
50 
51  // If p1 is behind the near plane, clip it
52  if (p1->y < near_plane)
53  {
54  // if p2 is also behind the near plane, skip the line
55  if (p2->y < near_plane)
56  continue;
57 
58  double r = (near_plane - p1->y) / (p2->y - p1->y);
59  p1_temp.x = p1->x + r * (p2->x - p1->x);
60  p1_temp.y = near_plane;
61  p1 = &p1_temp;
62  }
63 
64  // If p2 is behind the near plane, clip it
65  if (p2->y < near_plane)
66  {
67  double r = (near_plane - p2->y) / (p1->y - p2->y);
68  p2_temp.x = p2->x + r * (p1->x - p2->x);
69  p2_temp.y = near_plane;
70  p2 = &p2_temp;
71  }
72 
73  // Calculate the beam numbers corresponding to p1 and p2
74  int i1 = CalculateBeam(p1->x, p1->y) + 1;
75  int i2 = CalculateBeam(p2->x, p2->y);
76 
77  // If i2 < i1, we are looking at the back face of the line, so skip it (back face culling)
78  // If i2 < 0 or i1 >= nbeams, the whole line is out of view, so skip it
79  if (i2 < i1 || i2 < 0 || i1 >= nbeams)
80  continue;
81 
82  // Clip i1 and i2 to be between 0 and nbeams
83  i1 = std::max(0, i1);
84  i2 = std::min(i2, nbeams - 1);
85 
86  geo::Vec2 s = *p2 - *p1;
87  double t = p1->x * s.y - p1->y * s.x;
88 
89  for(int i_beam = i1; i_beam <= i2; ++i_beam)
90  {
91  const geo::Vec2& r = rays_[i_beam];
92 
93  // calculate depth of intersection between line (p1, p2) and r
94  double d = t / (r.x * s.y - r.y * s.x);
95 
96  double& depth_old = ranges[i_beam];
97  if (d > 0 && (d < depth_old || depth_old == 0))
98  {
99  depth_old = d;
100  identifiers[i_beam] = identifier;
101  }
102  }
103  }
104  }
105 }
106 
BeamModel::rays_
std::vector< geo::Vec2 > rays_
Definition: beam_model.h:54
BeamModel::BeamModel
BeamModel()
Definition: beam_model.cpp:5
t
Timer t
geo::Vec2T::y
T y
BeamModel::RenderModel
void RenderModel(const std::vector< std::vector< geo::Vec2 > > &contours, const geo::Transform2 &pose, int identifier, std::vector< double > &ranges, std::vector< int > &identifiers) const
Definition: beam_model.cpp:28
BeamModel::fx_
double fx_
Definition: beam_model.h:52
std::vector
std::vector::size
T size(T... args)
geo::Vec2T::x
T x
BeamModel::initialize
void initialize(double w, unsigned int num_beams)
Definition: beam_model.cpp:16
BeamModel::CalculateBeam
int CalculateBeam(double x, double depth) const
Definition: beam_model.h:19
BeamModel::half_num_beams_
unsigned int half_num_beams_
Definition: beam_model.h:53
geo::Transform2T
std::min
T min(T... args)
BeamModel::num_beams
unsigned int num_beams() const
Definition: beam_model.h:45
std::vector::begin
T begin(T... args)
beam_model.h
std::max
T max(T... args)
geo::Vec2T