trigonometry_utils.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_CORE_TRIGONOMETRY_UTILS_H
2 #define SLAM_CTOR_CORE_TRIGONOMETRY_UTILS_H
3 
4 #include <vector>
5 #include <cmath>
6 
7 // NB: polymorphic-provider soluction is cleaner from the OOP PoV.
8 // in case of performance issues related to 'virtual calls'
9 // can be replaced with 'ifs' in clients.
11 public:
12  virtual double sin(double angle_rad) const = 0;
13  virtual double cos(double angle_rad) const = 0;
14  virtual void set_base_angle(double angle_rad) = 0;
15 };
16 
18 public:
19  RawTrigonometryProvider() : _base_angle{0} {}
20 
21  double sin(double angle_rad) const override {
22  return std::sin(_base_angle + angle_rad);
23  }
24 
25  double cos(double angle_rad) const override {
26  return std::cos(_base_angle + angle_rad);
27  }
28 
29  void set_base_angle(double angle_rad) override {
30  _base_angle = angle_rad;
31  }
32 
33 private:
34  double _base_angle;
35 };
36 
38 public:
40  : _sin_base(0), _cos_base(0)
41  , _angle_min(0), _angle_max(0), _angle_delta(0) {
42  set_base_angle(0); // 'virtual call' is not virtual here, but it's ok.
43  }
44 
45  double sin(double angle_rad) const override {
46  // std::round is crucial to deal with inaccurate fp values
47  int angle_idx = std::round((angle_rad - _angle_min) / _angle_delta);
48  return _sin_base * _cos[angle_idx] + _cos_base * _sin[angle_idx];
49  }
50 
51  double cos(double angle_rad) const override {
52  // std::round is crucial to deal with inaccurate fp values
53  int angle_idx = std::round((angle_rad - _angle_min) / _angle_delta);
54  return _cos_base * _cos[angle_idx] - _sin_base * _sin[angle_idx];
55  }
56 
57  void set_base_angle(double angle_rad) override {
58  _sin_base = std::sin(angle_rad);
59  _cos_base = std::cos(angle_rad);
60  }
61 
62  void update(double a_min, double a_max, double a_inc) {
63  if (a_min == _angle_min && a_max == _angle_max && a_inc == _angle_delta)
64  return;
65  _sin.clear();
66  _cos.clear();
67  _angle_min = a_min;
68  _angle_max = a_max;
69  _angle_delta = a_inc;
70 
71  int angles_nm = (_angle_max - _angle_min) / _angle_delta + 1;
72  _sin.reserve(angles_nm);
73  _cos.reserve(angles_nm);
74  for(double angle = _angle_min; angle < _angle_max; angle += _angle_delta) {
75  _sin.push_back(std::sin(angle));
76  _cos.push_back(std::cos(angle));
77  }
78  }
79 
80 private:
81  std::vector<double> _sin, _cos;
82  double _sin_base, _cos_base;
83  double _angle_min, _angle_max, _angle_delta;
84 };
85 
86 #endif
void set_base_angle(double angle_rad) override
double cos(double angle_rad) const override
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
std::vector< double > _sin
void update(double a_min, double a_max, double a_inc)
void set_base_angle(double angle_rad) override
double sin(double angle_rad) const override
virtual void set_base_angle(double angle_rad)=0
virtual double cos(double angle_rad) const =0
double cos(double angle_rad) const override
double sin(double angle_rad) const override
virtual double sin(double angle_rad) const =0


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