SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSOrientation.h
Go to the documentation of this file.
1 #ifndef SENS_ORIENTATION_H
2 #define SENS_ORIENTATION_H
3 
4 #include <mutex>
5 #include <atomic>
6 #include <vector>
7 #include <thread>
8 #include <Utils.h>
9 
10 #include "SENS.h"
11 
13 
14 /*
15 
16  Android sensor coordinate system:
17  (https://developer.android.com/guide/topics/sensors/sensors_overview)
18 
19  Up = z North = y
20  | /
21  | /
22  |/
23  +------ East = x
24  +---------+
25  / +-----+ /
26  / / / /
27  / / / /
28  / +-----+ /
29  / 0 /
30 +---------+
31 
32  iOS sensor coordinate system:
33  (https://developer.apple.com/documentation/coremotion/getting_processed_device-motion_data/understanding_reference_frames_and_device_attitude)
34  In iOS we configure CMMotionManager with xMagneticNorthZVertical which means its a frame, where x points north, y points west and z points up (NWU).
35  In the iOS code, we add rotation of 90 deg. around z-axis to relate the sensor rotation to an ENU-frame (as in Android).
36 
37  Up = z West = y
38  | /
39  | /
40  |/
41  +------ North = x
42  +---------+
43  / +-----+ /
44  / / / /
45  / / / /
46  / +-----+ /
47  / 0 /
48 +---------+
49 
50  */
51 
53 {
54 public:
55  struct Quat
56  {
57  Quat() = default;
58  Quat(float x, float y, float z, float w)
59  : quatX(x),
60  quatY(y),
61  quatZ(z),
62  quatW(w)
63  {
64  }
65  float quatX = 0.f;
66  float quatY = 0.f;
67  float quatZ = 0.f;
68  float quatW = 0.f;
69  };
70 
71  virtual ~SENSOrientation() {}
72  //start gps sensor
73  virtual bool start() = 0;
74  virtual void stop() = 0;
75 
76  Quat getOrientation() const;
77 
78  bool isRunning() { return _running; }
79 
82 
83 protected:
84  void setOrientation(Quat orientation);
85 
86  bool _running = false;
87 
88 private:
91  mutable std::mutex _orientationMutex;
92 
93  std::vector<SENSOrientationListener*> _listeners;
94  std::mutex _listenerMutex;
95 };
96 
98 {
99 public:
101  virtual void onOrientation(const SENSTimePt& timePt, const SENSOrientation::Quat& ori) = 0;
102 };
103 
105 {
106 public:
107  ~SENSDummyOrientation() override {}
109  {
110  setOrientation(quat);
111  }
112 
113  bool start() override
114  {
115  _running = true;
116  return _running;
117  }
118 
119  void stop() override
120  {
121  }
122 };
123 
124 #endif
std::chrono::high_resolution_clock::time_point SENSTimePt
Definition: SENS.h:57
bool start() override
void setDummyQuat(SENSOrientation::Quat quat)
void stop() override
~SENSDummyOrientation() override
void setOrientation(Quat orientation)
void registerListener(SENSOrientationListener *listener)
SENSTimePt _timePt
void unregisterListener(SENSOrientationListener *listener)
virtual void stop()=0
virtual ~SENSOrientation()
Quat getOrientation() const
virtual bool start()=0
std::mutex _listenerMutex
std::vector< SENSOrientationListener * > _listeners
std::mutex _orientationMutex
virtual void onOrientation(const SENSTimePt &timePt, const SENSOrientation::Quat &ori)=0
Quat(float x, float y, float z, float w)