oculusVRSensorData.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/input/oculusVR/oculusVRSensorData.h"
  23. #include "platform/input/oculusVR/oculusVRUtil.h"
  24. #include "console/console.h"
  25. OculusVRSensorData::OculusVRSensorData()
  26. {
  27. reset();
  28. }
  29. void OculusVRSensorData::reset()
  30. {
  31. mDataSet = false;
  32. }
  33. void OculusVRSensorData::setData(OVR::SensorFusion& data, const F32& maxAxisRadius)
  34. {
  35. // Sensor rotation
  36. OVR::Quatf orientation;
  37. if(data.GetPredictionDelta() > 0)
  38. {
  39. orientation = data.GetPredictedOrientation();
  40. }
  41. else
  42. {
  43. orientation = data.GetOrientation();
  44. }
  45. OVR::Matrix4f orientMat(orientation);
  46. OculusVRUtil::convertRotation(orientMat.M, mRot);
  47. mRotQuat.set(mRot);
  48. // Sensor rotation in Euler format
  49. OculusVRUtil::convertRotation(orientation, mRotEuler);
  50. // Sensor rotation as axis
  51. OculusVRUtil::calculateAxisRotation(mRot, maxAxisRadius, mRotAxis);
  52. // Sensor raw values
  53. OVR::Vector3f accel = data.GetAcceleration();
  54. OculusVRUtil::convertAcceleration(accel, mAcceleration);
  55. OVR::Vector3f angVel = data.GetAngularVelocity();
  56. OculusVRUtil::convertAngularVelocity(angVel, mAngVelocity);
  57. OVR::Vector3f mag;
  58. if(data.HasMagCalibration() && data.IsYawCorrectionEnabled())
  59. {
  60. mag = data.GetCalibratedMagnetometer();
  61. }
  62. else
  63. {
  64. mag = data.GetMagnetometer();
  65. }
  66. OculusVRUtil::convertMagnetometer(mag, mMagnetometer);
  67. mDataSet = true;
  68. }
  69. void OculusVRSensorData::simulateData(const F32& maxAxisRadius)
  70. {
  71. // Sensor rotation
  72. mRot.identity();
  73. mRotQuat.identity();
  74. mRotEuler.zero();
  75. // Sensor rotation as axis
  76. OculusVRUtil::calculateAxisRotation(mRot, maxAxisRadius, mRotAxis);
  77. // Sensor raw values
  78. mAcceleration.zero();
  79. mAngVelocity.zero();
  80. mMagnetometer.zero();
  81. mDataSet = true;
  82. }
  83. U32 OculusVRSensorData::compare(OculusVRSensorData* other, bool doRawCompare)
  84. {
  85. S32 result = DIFF_NONE;
  86. // Check rotation
  87. if(mRotEuler.x != other->mRotEuler.x || mRotEuler.y != other->mRotEuler.y || mRotEuler.z != other->mRotEuler.z || !mDataSet)
  88. {
  89. result |= DIFF_ROT;
  90. }
  91. // Check rotation as axis
  92. if(mRotAxis.x != other->mRotAxis.x || !mDataSet)
  93. {
  94. result |= DIFF_ROTAXISX;
  95. }
  96. if(mRotAxis.y != other->mRotAxis.y || !mDataSet)
  97. {
  98. result |= DIFF_ROTAXISY;
  99. }
  100. // Check raw values
  101. if(doRawCompare)
  102. {
  103. if(mAcceleration.x != other->mAcceleration.x || mAcceleration.y != other->mAcceleration.y || mAcceleration.z != other->mAcceleration.z || !mDataSet)
  104. {
  105. result |= DIFF_ACCEL;
  106. }
  107. if(mAngVelocity.x != other->mAngVelocity.x || mAngVelocity.y != other->mAngVelocity.y || mAngVelocity.z != other->mAngVelocity.z || !mDataSet)
  108. {
  109. result |= DIFF_ANGVEL;
  110. }
  111. if(mMagnetometer.x != other->mMagnetometer.x || mMagnetometer.y != other->mMagnetometer.y || mMagnetometer.z != other->mMagnetometer.z || !mDataSet)
  112. {
  113. result |= DIFF_MAG;
  114. }
  115. }
  116. return result;
  117. }