MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
position.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mbgl/math/angles.hpp>
5 
6 #include <array>
7 
8 namespace mbgl {
9 namespace style {
10 class Position {
11 public:
12  Position() = default;
13  Position(std::array<float, 3>& position_)
14  : radial(position_[0]), azimuthal(position_[1]), polar(position_[2]) {
15  calculateCartesian();
16  };
17 
18  friend bool operator==(const Position& lhs, const Position& rhs) {
19  return lhs.radial == rhs.radial && lhs.azimuthal == rhs.azimuthal && lhs.polar == rhs.polar;
20  // TODO this doesn't address wrapping, which would be better addressed by comparing cartesian coordinates but being calculated floats are ont to be trusted.
21  }
22 
23  friend bool operator!=(const Position& lhs, const Position& rhs) {
24  return !(lhs == rhs);
25  }
26 
27  std::array<float, 3> getCartesian() const { return {{x, y, z}}; };
28 
29  std::array<float, 3> getSpherical() const { return {{radial, azimuthal, polar}}; };
30 
31  void set(std::array<float, 3>& position_) {
32  radial = position_[0];
33  azimuthal = position_[1];
34  polar = position_[2];
35  calculateCartesian();
36  };
37 
39  void setCartesian(std::array<float, 3>& position_) {
40  x = position_[0];
41  y = position_[1];
42  z = position_[2];
43  }
44 
45 private:
46  float radial;
47  float azimuthal;
48  float polar;
49  float x;
50  float y;
51  float z;
52 
53  void calculateCartesian() {
54  // We abstract "north"/"up" (compass-wise) to be 0° when really this is 90° (π/2): we
55  // correct for that here
56  const float _a = util::deg2radf(azimuthal + 90);
57  const float _p = util::deg2radf(polar);
58 
59  x = radial * std::cos(_a) * std::sin(_p);
60  y = radial * std::sin(_a) * std::sin(_p);
61  z = radial * std::cos(_p);
62  };
63 };
64 } // namespace style
65 } // namespace mbgl
Position(std::array< float, 3 > &position_)
Definition: position.hpp:13
friend bool operator==(const Position &lhs, const Position &rhs)
Definition: position.hpp:18
void set(std::array< float, 3 > &position_)
Definition: position.hpp:31
std::array< float, 3 > getSpherical() const
Definition: position.hpp:29
std::array< float, 3 > getCartesian() const
Definition: position.hpp:27
void setCartesian(std::array< float, 3 > &position_)
Utility function to be used only during interpolation; this leaves spherical coordinates undefined.
Definition: position.hpp:39
friend bool operator!=(const Position &lhs, const Position &rhs)
Definition: position.hpp:23
constexpr float deg2radf(float deg) noexcept
Converts degrees to radians.
Definition: angles.hpp:24
Definition: actor.hpp:15