leapMotionUtil.cpp
Engine/source/platform/input/leapMotion/leapMotionUtil.cpp
Namespaces:
namespace
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/input/leapMotion/leapMotionUtil.h" 25 26namespace LeapMotionUtil 27{ 28 29void convertPosition(const Leap::Vector& inPosition, F32& x, F32& y, F32& z) 30{ 31 // Convert to Torque coordinates. The conversion is: 32 // 33 // Motion Torque 34 // x y z --> x -z y 35 x = inPosition.x; // x = x 36 y = -inPosition.z; // y = -z 37 z = inPosition.y; // z = y; 38} 39 40void convertPosition(const Leap::Vector& inPosition, Point3F& outPosition) 41{ 42 // Convert to Torque coordinates. The conversion is: 43 // 44 // Motion Torque 45 // x y z --> x -z y 46 outPosition.x = inPosition.x; // x = x 47 outPosition.y = -inPosition.z; // y = -z 48 outPosition.z = inPosition.y; // z = y; 49} 50 51void convertHandRotation(const Leap::Hand& hand, MatrixF& outRotation) 52{ 53 // We need to convert from Motion coordinates to 54 // Torque coordinates. The conversion is: 55 // 56 // Motion Torque 57 // a b c a b c a -c b 58 // d e f --> -g -h -i --> -g i -h 59 // g h i d e f d -f e 60 const Leap::Vector& handToFingers = hand.direction(); 61 Leap::Vector handFront = -handToFingers; 62 const Leap::Vector& handDown = hand.palmNormal(); 63 Leap::Vector handUp = -handDown; 64 Leap::Vector handRight = handUp.cross(handFront); 65 66 outRotation.setColumn(0, Point4F( handRight.x, -handRight.z, handRight.y, 0.0f)); 67 outRotation.setColumn(1, Point4F( -handFront.x, handFront.z, -handFront.y, 0.0f)); 68 outRotation.setColumn(2, Point4F( handUp.x, -handUp.z, handUp.y, 0.0f)); 69 outRotation.setPosition(Point3F::Zero); 70} 71 72void calculateHandAxisRotation(const MatrixF& handRotation, const F32& maxHandAxisRadius, Point2F& outRotation) 73{ 74 const VectorF& controllerUp = handRotation.getUpVector(); 75 outRotation.x = controllerUp.x; 76 outRotation.y = controllerUp.y; 77 78 // Limit the axis angle to that given to us 79 if(outRotation.len() > maxHandAxisRadius) 80 { 81 outRotation.normalize(maxHandAxisRadius); 82 } 83 84 // Renormalize to the range of 0..1 85 if(maxHandAxisRadius != 0.0f) 86 { 87 outRotation /= maxHandAxisRadius; 88 } 89} 90 91void convertPointableRotation(const Leap::Pointable& pointable, MatrixF& outRotation) 92{ 93 // We need to convert from Motion coordinates to 94 // Torque coordinates. The conversion is: 95 // 96 // Motion Torque 97 // a b c a b c a -c b 98 // d e f --> -g -h -i --> -g i -h 99 // g h i d e f d -f e 100 Leap::Vector pointableFront = -pointable.direction(); 101 Leap::Vector pointableRight = Leap::Vector::up().cross(pointableFront); 102 Leap::Vector pointableUp = pointableFront.cross(pointableRight); 103 104 outRotation.setColumn(0, Point4F( pointableRight.x, -pointableRight.z, pointableRight.y, 0.0f)); 105 outRotation.setColumn(1, Point4F( -pointableFront.x, pointableFront.z, -pointableFront.y, 0.0f)); 106 outRotation.setColumn(2, Point4F( pointableUp.x, -pointableUp.z, pointableUp.y, 0.0f)); 107 outRotation.setPosition(Point3F::Zero); 108} 109 110} 111