leapMotionUtil.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/leapMotion/leapMotionUtil.h"
  23. namespace LeapMotionUtil
  24. {
  25. void convertPosition(const Leap::Vector& inPosition, F32& x, F32& y, F32& z)
  26. {
  27. // Convert to Torque coordinates. The conversion is:
  28. //
  29. // Motion Torque
  30. // x y z --> x -z y
  31. x = inPosition.x; // x = x
  32. y = -inPosition.z; // y = -z
  33. z = inPosition.y; // z = y;
  34. }
  35. void convertPosition(const Leap::Vector& inPosition, Point3F& outPosition)
  36. {
  37. // Convert to Torque coordinates. The conversion is:
  38. //
  39. // Motion Torque
  40. // x y z --> x -z y
  41. outPosition.x = inPosition.x; // x = x
  42. outPosition.y = -inPosition.z; // y = -z
  43. outPosition.z = inPosition.y; // z = y;
  44. }
  45. void convertHandRotation(const Leap::Hand& hand, MatrixF& outRotation)
  46. {
  47. // We need to convert from Motion coordinates to
  48. // Torque coordinates. The conversion is:
  49. //
  50. // Motion Torque
  51. // a b c a b c a -c b
  52. // d e f --> -g -h -i --> -g i -h
  53. // g h i d e f d -f e
  54. const Leap::Vector& handToFingers = hand.direction();
  55. Leap::Vector handFront = -handToFingers;
  56. const Leap::Vector& handDown = hand.palmNormal();
  57. Leap::Vector handUp = -handDown;
  58. Leap::Vector handRight = handUp.cross(handFront);
  59. outRotation.setColumn(0, Point4F( handRight.x, -handRight.z, handRight.y, 0.0f));
  60. outRotation.setColumn(1, Point4F( -handFront.x, handFront.z, -handFront.y, 0.0f));
  61. outRotation.setColumn(2, Point4F( handUp.x, -handUp.z, handUp.y, 0.0f));
  62. outRotation.setPosition(Point3F::Zero);
  63. }
  64. void calculateHandAxisRotation(const MatrixF& handRotation, const F32& maxHandAxisRadius, Point2F& outRotation)
  65. {
  66. const VectorF& controllerUp = handRotation.getUpVector();
  67. outRotation.x = controllerUp.x;
  68. outRotation.y = controllerUp.y;
  69. // Limit the axis angle to that given to us
  70. if(outRotation.len() > maxHandAxisRadius)
  71. {
  72. outRotation.normalize(maxHandAxisRadius);
  73. }
  74. // Renormalize to the range of 0..1
  75. if(maxHandAxisRadius != 0.0f)
  76. {
  77. outRotation /= maxHandAxisRadius;
  78. }
  79. }
  80. void convertPointableRotation(const Leap::Pointable& pointable, MatrixF& outRotation)
  81. {
  82. // We need to convert from Motion coordinates to
  83. // Torque coordinates. The conversion is:
  84. //
  85. // Motion Torque
  86. // a b c a b c a -c b
  87. // d e f --> -g -h -i --> -g i -h
  88. // g h i d e f d -f e
  89. Leap::Vector pointableFront = -pointable.direction();
  90. Leap::Vector pointableRight = Leap::Vector::up().cross(pointableFront);
  91. Leap::Vector pointableUp = pointableFront.cross(pointableRight);
  92. outRotation.setColumn(0, Point4F( pointableRight.x, -pointableRight.z, pointableRight.y, 0.0f));
  93. outRotation.setColumn(1, Point4F( -pointableFront.x, pointableFront.z, -pointableFront.y, 0.0f));
  94. outRotation.setColumn(2, Point4F( pointableUp.x, -pointableUp.z, pointableUp.y, 0.0f));
  95. outRotation.setPosition(Point3F::Zero);
  96. }
  97. }