viny_grid_cell.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_SLAM_VINY_GRID_CELL_H
2 #define SLAM_CTOR_SLAM_VINY_GRID_CELL_H
3 
4 #include "../../core/maps/grid_cell.h"
5 #include <ostream>
6 
7 class BaseTBM {
8 private:
9  // TODO: use enum class
10  static const int UNKNOWN = 0b00, EMPTY = 0b01, OCCUPIED = 0b10,
11  CONFLICT = 0b11, NM = 4;
12 public:
13  BaseTBM() {}
14  BaseTBM(const BaseTBM&) = default;
15  BaseTBM& operator=(const BaseTBM&) = default;
16  BaseTBM& operator=(BaseTBM&&) = default;
17 
19  if (!aoo.occupancy.is_valid()) { return; }
20 
21  // TODO: consider other conversion schemas
22  double prob_occ = aoo.occupancy.prob_occ;
23  double est_qual = aoo.occupancy.estimation_quality * aoo.quality;
24 
25  _occupied = prob_occ * est_qual;
26  _empty = (1 - prob_occ) * est_qual;
27  _unknown = 1 - _occupied - _empty;
28  /*
29  if (aoo.is_occupied) {
30  _occupied = prob_occ * est_qual;
31  _unknown = 1 - _occupied;
32  } else {
33  _empty = (1 - prob_occ) * est_qual;
34  _unknown = 1 - _empty;
35  }
36  */
37  }
38 
39  BaseTBM(double occupied, double empty, double unknown, double conflict)
40  : _occupied(occupied), _empty(empty)
41  , _unknown(unknown), _conflict(conflict) {}
42 
43  BaseTBM& operator+=(const BaseTBM& rhs) {
44  auto result = BaseTBM();
45  result.reset();
46 
47  for (int this_id = 0; this_id < NM; ++this_id) {
48  for (int that_id = 0; that_id < NM; ++that_id) {
49  result.belief_by_id(this_id | that_id) +=
50  belief_by_id(this_id) * rhs.belief_by_id(that_id);
51  }
52  }
53 
54  *this = result;
55  normalize();
56  return *this;
57  }
58 
59  BaseTBM& operator-=(const BaseTBM& rhs) {
60  auto result = BaseTBM();
61  result.reset();
62 
63  for (int this_id = 0; this_id < NM; ++this_id) {
64  for (int that_id = 0; that_id < NM; ++that_id) {
65  result.belief_by_id(this_id & that_id) +=
66  belief_by_id(this_id) * rhs.belief_by_id(that_id);
67  }
68  }
69 
70  *this = result;
71  normalize();
72  return *this;
73  }
74 
75  explicit operator Occupancy() {
76  auto occ = Occupancy{_occupied + 0.5 * _unknown, 1.0};
77  return occ;
78  }
79 
81  double weight = _occupied + _empty + _unknown;
82  _occupied /= weight;
83  _empty /= weight;
84  _unknown /= weight;
85  _conflict = 0;
86  }
87 
88  double conflict() const { return _conflict; }
89  double occupied() const { return _occupied; }
90  double empty() const { return _empty; }
91  double unknown() const { return _unknown; }
92 private:
93  void normalize() {
94  double tot_weight = _occupied + _empty + _conflict + _unknown;
95  if (tot_weight == 0.0) {
96  _unknown = 1.0;
97  return;
98  }
99  _conflict /= tot_weight;
100  _occupied /= tot_weight;
101  _empty /= tot_weight;
102  _unknown /= tot_weight;
103  }
104 
105  void reset() { _occupied = _empty = _unknown = _conflict = 0; }
106 
107  double& belief_by_id(int id) {
108  return const_cast<double&>(
109  static_cast<const BaseTBM*>(this)->belief_by_id(id));
110  }
111 
112  const double& belief_by_id(int id) const {
113  switch (id) {
114  case OCCUPIED: return _occupied;
115  case EMPTY: return _empty;
116  case UNKNOWN: return _unknown;
117  default: return _conflict;
118  }
119  }
120 private:
121  double _occupied = 0.0;
122  double _empty = 0.0;
123  double _unknown = 1.0;
124  double _conflict = 0.0;
125 };
126 
127 
128 
129 class VinyDSCell : public GridCell {
130 public:
132 
133  std::unique_ptr<GridCell> clone() const override {
134  return std::make_unique<VinyDSCell>(*this);
135  }
136 
137  void operator+=(const AreaOccupancyObservation &aoo) override {
138  if (!aoo.occupancy.is_valid()) { return; }
139 
140  _belief += aoo;
141  _belief.normalize_conflict();
142  _occupancy.prob_occ = Occupancy(_belief).prob_occ;
143  }
144 
145  double discrepancy(const AreaOccupancyObservation &aoo) const override {
146  auto that_belief = BaseTBM{aoo};
147  auto total_unknown = that_belief.unknown() + _belief.unknown();
148  auto d_occ = std::abs(that_belief.occupied() - _belief.occupied());
149  auto combined_belief = that_belief;
150  combined_belief += _belief;
151  /* return combined_belief.conflict() + d_occ + total_unknown; */
152 
153  // original "combined.conflict() + d_occ + total_unknown" was replaced
154  // with the following rule to put the discrepancy in [0; 1].
155  auto unknown = total_unknown / 2.0;
156  auto known = 1 - unknown;
157  auto known_discrepancy = known * (combined_belief.conflict() + d_occ) / 2.0;
158  return unknown / 2 + known_discrepancy;
159  }
160 
161  std::vector<char> serialize() const override {
163  s << _belief.occupied() << _belief.empty()
164  << _belief.unknown() << _belief.conflict();
165  return s.result();
166  }
167 
168  std::size_t deserialize(const std::vector<char>& data,
169  std::size_t pos = 0) override {
170  Deserializer d(data, GridCell::deserialize(data, pos));
171  double o, e, u, c;
172  d >> o >> e >> u >> c;
173  _belief = BaseTBM(o, e, u, c);
174  return d.pos();
175  }
176 
177  const BaseTBM& belief() const { return _belief; }
178 private:
180 };
181 
182 #endif
double discrepancy(const AreaOccupancyObservation &aoo) const override
d
double conflict() const
double _conflict
double prob_occ
Definition: state_data.h:8
static const int OCCUPIED
size_t pos() const
Definition: serialization.h:73
XmlRpcServer s
const double & belief_by_id(int id) const
BaseTBM _belief
double _unknown
void reset()
BaseTBM & operator=(const BaseTBM &)=default
static const int NM
static const int CONFLICT
double occupied() const
std::unique_ptr< GridCell > clone() const override
std::size_t deserialize(const std::vector< char > &data, std::size_t pos=0) override
bool is_valid() const
Definition: state_data.h:23
void operator+=(const AreaOccupancyObservation &aoo) override
double estimation_quality
Definition: state_data.h:9
BaseTBM & operator-=(const BaseTBM &rhs)
void normalize_conflict()
BaseTBM(double occupied, double empty, double unknown, double conflict)
double unknown() const
static const int EMPTY
double _occupied
const BaseTBM & belief() const
double & belief_by_id(int id)
static const int UNKNOWN
virtual size_t deserialize(const std::vector< char > &data, size_t pos=0)
Definition: grid_cell.h:40
BaseTBM & operator+=(const BaseTBM &rhs)
virtual std::vector< char > serialize() const
Definition: grid_cell.h:35
BaseTBM(const AreaOccupancyObservation &aoo)
std::vector< char > result() const
Definition: serialization.h:24
void normalize()
double empty() const
double _empty
std::vector< char > serialize() const override


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