SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLDeviceRotation.cpp
Go to the documentation of this file.
1 /**
2  * \file SLDeviceRotation.cpp
3  * \authors Marcus Hudritsch
4  * \date November 2017
5  * \authors Marcus Hudritsch
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #include <SLDeviceRotation.h>
12 
13 //-----------------------------------------------------------------------------
15 {
16  init();
17 }
18 //-----------------------------------------------------------------------------
20 {
22  _pitchRAD = 0.0f;
23  _yawRAD = 0.0f;
24  _rollRAD = 0.0f;
25  _rotationAvg.init(3, SLMat3f());
26  _zeroYawAtStart = true;
27  _startYawRAD = 0.0f;
28  _isFirstSensorValue = false;
29  _isUsed = false;
31  _updateRPY = true;
32 }
33 //-----------------------------------------------------------------------------
34 /*! onRotationQUAT: Event handler for rotation change of a mobile device from a
35  rotation quaternion. This function will only be called in an Android or iOS
36  project. See e.g. onSensorChanged in GLES3Activity.java in the Android project.
37  This handler is only called if the flag SLScene::_usesRotation is true. If so
38  the mobile device turns on it's IMU sensor system. The device rotation is so
39  far only used in SLCamera::setView if the cameras animation is on CA_deciveRotYUp.
40  If _zeroYawAfterStart is true the start yaw value is subtracted. This means
41  that the magnetic north will be ignored.
42  The angles should be:\n
43  Roll from -halfpi (ccw) to zero (horizontal) to +halfpi (clockwise)\n
44  Pitch from -halfpi (down) to zero (horizontal) to +halfpi (up)\n
45  Yaw from -pi (south) to zero (north) to +pi (south)\n
46 */
48  SLfloat quatY,
49  SLfloat quatZ,
50  SLfloat quatW)
51 {
52  _quaternion = SLQuat4f(quatX, quatY, quatZ, quatW);
55 
56  if (_updateRPY)
58 
59  /*
60  Android sensor coordinate system:
61  (https://developer.android.com/guide/topics/sensors/sensors_overview)
62 
63  Up = z North = y
64  | /
65  | /
66  |/
67  +------ East = x
68  +---------+
69  / +-----+ /
70  / / / /
71  / / / /
72  / +-----+ /
73  / 0 /
74  +---------+
75 
76  iOS sensor coordinate system:
77  (https://developer.apple.com/documentation/coremotion/getting_processed_device-motion_data/understanding_reference_frames_and_device_attitude)
78  In iOS we configure CMMotionManager with xMagneticNorthZVertical which means its a frame, where x points north, y points west and z points up (NWU).
79  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).
80 
81  Up = z West = y
82  | /
83  | /
84  |/
85  +------ North = x
86  +---------+
87  / +-----+ /
88  / / / /
89  / / / /
90  / +-----+ /
91  / 0 /
92  +---------+
93 
94  */
95 
97  {
98  // store initial rotation in yaw for referencing of initial alignment
100  _isFirstSensorValue = false;
101  }
102 }
103 //-----------------------------------------------------------------------------
104 //! Setter that turns on the device rotation sensor
106 {
107  if (!_isUsed && use == true)
108  _isFirstSensorValue = true;
109 
110  _isUsed = use;
111 }
112 //------------------------------------------------------------------------------
113 //! Returns the device rotation averaged over multple frames
115 {
116  assert(numAvg > 0 && "Num. of averaged values must be greater than zero");
117  _rotationAvg.init(numAvg, _rotationAvg.average());
118 }
119 //------------------------------------------------------------------------------
120 //! Returns the rotation offset mode as string
122 {
123  switch (_offsetMode)
124  {
125  case ROM_none: return "None";
126  case ROM_oneFingerX: return "OneFingerX";
127  case ROM_oneFingerXY: return "OneFingerXY";
128  default: return "Unknown";
129  }
130 }
131 //------------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
bool SLbool
Definition: SL.h:175
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
Mobile device rotation class declaration.
@ ROM_oneFingerX
@ ROM_none
@ ROM_oneFingerXY
SLMat3< SLfloat > SLMat3f
Definition: SLMat3.h:746
SLQuat4< SLfloat > SLQuat4f
Definition: SLQuat4.h:846
SLMat3f _rotation
Mobile device rotation as matrix.
SLfloat _rollRAD
Device roll angle in radians.
SLbool _isFirstSensorValue
Flag for the first sensor values.
SLbool isUsed() const
SLfloat _startYawRAD
Initial yaw angle after _zeroYawAfterSec in radians.
SLfloat _yawRAD
Device yaw angle in radians.
SLbool _updateRPY
Calculate roll pitch yaw in onRotationQUAT.
SLbool _zeroYawAtStart
Quaternion rotation that is set by IMU.
SLfloat _pitchRAD
Device pitch angle in radians.
SLRotOffsetMode _offsetMode
Rotation offset mode.
Averaged< SLMat3f > _rotationAvg
Component wise averaged rotation matrix.
SLbool _isUsed
Flag if device rotation is used.
void onRotationQUAT(SLfloat quatX, SLfloat quatY, SLfloat quatZ, SLfloat quatW)
SLstring offsetModeStr() const
Returns the rotation offset mode as string.
void identity()
Definition: SLMat3.h:329
SLMat3< T > toMat3() const
Definition: SLQuat4.h:318
void toEulerAnglesXYZ(T &xRotRAD, T &yRotRAD, T &zRotRAD) const
Definition: SLQuat4.h:407