sensor_data.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_CORE_SENSOR_DATA_H
2 #define SLAM_CTOR_CORE_SENSOR_DATA_H
3 
4 #include <cassert>
5 #include <cmath>
6 #include <memory>
7 #include <vector>
8 #include <iostream>
9 
10 #include "state_data.h"
11 #include "robot_pose.h"
12 #include "../geometry_utils.h"
13 
14 struct ScanPoint2D {
15 public:
16  enum class PointType {Polar, Cartesian};
17 public: // methods
18 
19  static ScanPoint2D make_polar(double range, double angle, bool is_occ) {
20  return ScanPoint2D{PointType::Polar, range, angle, is_occ};
21  }
22 
23  static ScanPoint2D make_cartesian(const Point2D &p, bool is_occ) {
24  return ScanPoint2D{PointType::Cartesian, p.x, p.y, is_occ};
25  }
26 
27  ScanPoint2D(PointType type, double x_or_range, double y_or_angle, bool is_occ)
28  : _type{type}, _factor{1.0}, _is_occupied{is_occ} {
29 
30  switch (_type) {
31  case PointType::Polar:
32  _data.polar.range = x_or_range;
33  _data.polar.angle = y_or_angle;
34  break;
36  _data.cartesian.x = x_or_range;
37  _data.cartesian.y = y_or_angle;
38  break;
39  default:
40  assert(0 && "Unknown point type");
41  }
42  }
43 
44  ScanPoint2D(double range = 0, double ang = 0, bool is_occ = true)
45  : ScanPoint2D{PointType::Polar, range, ang, is_occ} {}
46 
47  double range() const {
48  if (_type == PointType::Polar) {
49  return _data.polar.range;
50  }
51  return std::sqrt(std::pow(_data.cartesian.x, 2) +
52  std::pow(_data.cartesian.y, 2));
53  }
54  double angle() const {
55  if (_type == PointType::Polar) {
56  return _data.polar.angle;
57  }
58  return std::atan2(_data.cartesian.y, _data.cartesian.x);
59  }
60  double x() const {
61  if (_type == PointType::Cartesian) {
62  return _data.cartesian.x;
63  }
64  return _data.polar.range * std::cos(_data.polar.angle);
65  }
66  double y() const {
67  if (_type == PointType::Cartesian) {
68  return _data.cartesian.y;
69  }
70  return _data.polar.range * std::sin(_data.polar.angle);
71  }
72  bool is_occupied() const { return _is_occupied; }
73 
74  ScanPoint2D& set_factor(double factor) { _factor = factor; return *this; }
75  double factor() const { return _factor; }
76 
77  ScanPoint2D to_cartesian(std::shared_ptr<TrigonometryProvider> tp) const {
78  auto point = move_origin(0, 0, tp);
79  return ScanPoint2D{PointType::Cartesian, point.x, point.y, _is_occupied};
80  }
81 
82  // NB: a rotation is preset in a given trigonometry provider
83  Point2D move_origin(double d_x, double d_y,
84  std::shared_ptr<TrigonometryProvider> tp) const {
85  return Point2D{d_x + range() * tp->cos(angle()),
86  d_y + range() * tp->sin(angle())};
87 
88  }
89 
91  std::shared_ptr<TrigonometryProvider> tp) const {
92  return move_origin(p.x, p.y, tp);
93 
94  }
95 
96  Point2D move_origin(double d_x, double d_y, double d_angle) const {
97  auto patched = ScanPoint2D{PointType::Polar,
98  range(), angle() + d_angle,
99  is_occupied()};
100  return Point2D{patched.x() + d_x, patched.y() + d_y};
101  }
102 
103  Point2D move_origin(double d_x, double d_y) const {
104  return Point2D{x() + d_x, y() + d_y};
105  }
106 
107  ScanPoint2D to_cartesian(double d_angle = 0, double d_range = 0) const {
108  auto patched = ScanPoint2D{PointType::Polar,
109  range() + d_range, angle() + d_angle,
110  is_occupied()};
111  return ScanPoint2D{PointType::Cartesian, patched.x(), patched.y(),
112  patched.is_occupied()};
113  }
114 
115  ScanPoint2D to_polar(double d_x = 0, double d_y = 0) const {
116  auto patched = ScanPoint2D{PointType::Cartesian,
117  x() + d_x, y() + d_y,
118  is_occupied()};
119  return ScanPoint2D{PointType::Polar, patched.range(), patched.angle(),
120  patched.is_occupied()};
121  }
122 
123 private: // data structs
124  union PointData {
125  PointData() : cartesian{0, 0} {}
126  struct PolarPoint {
127  double range, angle;
128  } polar;
130  };
131 
132 private: // data
135  double _factor;
137 };
138 
139 inline std::ostream& operator<<(std::ostream &osm, const ScanPoint2D &sp) {
140  osm << "ScanPoint2D{ r: " << sp.range() << "; a: " << sp.angle() << " <-> ";
141  return osm << "x: " << sp.x() << ", y: " << sp.y() << "}";
142 }
143 
144 struct LaserScan2D {
145 public:
146  using Points = std::vector<ScanPoint2D>;
147 public:
148  const Points& points() const { return _points; }
150  return const_cast<Points&>(
151  static_cast<const LaserScan2D*>(this)->points());
152  }
153 
155  LaserScan2D cartsn_scan;
156  cartsn_scan.points().reserve(_points.size());
157  cartsn_scan.trig_provider = trig_provider;
158 
159  cartsn_scan.trig_provider->set_base_angle(angle);
160  for (auto &sp : _points) {
161  auto cartesian_sp = sp.to_cartesian(cartsn_scan.trig_provider);
162  cartsn_scan.points().push_back(cartesian_sp);
163  }
164  return cartsn_scan;
165  }
166 
167 public:
168  // TODO: create simple and effective way
169  // to translate LS to world by a given pose
170  // Move the provider to LaserScan2D.
171  std::shared_ptr<TrigonometryProvider> trig_provider;
172 private:
174 };
175 
178 
180  double quality; // 0 - low, 1 - fine
181 };
182 
183 #endif
static ScanPoint2D make_polar(double range, double angle, bool is_occ)
Definition: sensor_data.h:19
Points _points
Definition: sensor_data.h:173
ScanPoint2D to_cartesian(double d_angle=0, double d_range=0) const
Definition: sensor_data.h:107
ScanPoint2D(double range=0, double ang=0, bool is_occ=true)
Definition: sensor_data.h:44
Points & points()
Definition: sensor_data.h:149
bool _is_occupied
Definition: sensor_data.h:136
PointType _type
Definition: sensor_data.h:133
Point2D move_origin(double d_x, double d_y, double d_angle) const
Definition: sensor_data.h:96
ScanPoint2D(PointType type, double x_or_range, double y_or_angle, bool is_occ)
Definition: sensor_data.h:27
double _factor
Definition: sensor_data.h:135
LaserScan2D to_cartesian(double angle) const
Definition: sensor_data.h:154
ScanPoint2D to_cartesian(std::shared_ptr< TrigonometryProvider > tp) const
Definition: sensor_data.h:77
bool is_occupied() const
Definition: sensor_data.h:72
Point2D move_origin(double d_x, double d_y, std::shared_ptr< TrigonometryProvider > tp) const
Definition: sensor_data.h:83
ScanPoint2D & set_factor(double factor)
Definition: sensor_data.h:74
struct ScanPoint2D::PointData::PolarPoint polar
double factor() const
Definition: sensor_data.h:75
Point2D move_origin(const Point2D &p, std::shared_ptr< TrigonometryProvider > tp) const
Definition: sensor_data.h:90
double x() const
Definition: sensor_data.h:60
static ScanPoint2D make_cartesian(const Point2D &p, bool is_occ)
Definition: sensor_data.h:23
double angle() const
Definition: sensor_data.h:54
ScanPoint2D to_polar(double d_x=0, double d_y=0) const
Definition: sensor_data.h:115
const Points & points() const
Definition: sensor_data.h:148
Point2D move_origin(double d_x, double d_y) const
Definition: sensor_data.h:103
RobotPoseDelta pose_delta
Definition: sensor_data.h:177
std::shared_ptr< TrigonometryProvider > trig_provider
Definition: sensor_data.h:171
double range() const
Definition: sensor_data.h:47
std::vector< ScanPoint2D > Points
Definition: sensor_data.h:146
PointData _data
Definition: sensor_data.h:134
std::ostream & operator<<(std::ostream &osm, const ScanPoint2D &sp)
Definition: sensor_data.h:139
double y() const
Definition: sensor_data.h:66


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25