LeapUtil.cpp
Go to the documentation of this file.
1 /******************************************************************************\
2 * Copyright (C) Leap Motion, Inc. 2011-2013. *
3 * Leap Motion proprietary and confidential. Not for distribution. *
4 * Use subject to the terms of the Leap Motion SDK Agreement available at *
5 * https://developer.leapmotion.com/sdk_agreement, or another agreement between *
6 * Leap Motion and you, your company or other organization. *
7 \******************************************************************************/
8 #include "LeapUtil.h"
9 
10 namespace LeapUtil {
11 
12 using namespace Leap;
13 
17 
18 void Camera::SetPOVLookAt( const Vector& vCameraPosition, const Vector& vTarget, Vector vUp )
19 {
20  Vector vZBasis = (vCameraPosition - vTarget).normalized();
21 
22  // if the zero vector was passed try to preserve the current camera up as much as possible
23  if ( vUp == Vector::zero() )
24  {
25  vUp = m_mtxPOV.yBasis;
26  }
27  else
28  {
29  vUp = vUp.normalized();
30  }
31 
32  // if the z direction is parallel to the desired up direction
33  // we need to pick another up.
34  if ( IsNearZero(1.0f - fabs(vZBasis.dot(vUp))) )
35  {
36  // make the new up perpendicular to the plane of the new z axis and the old right direction.
37  // it's a decent guess.
38  vUp = vZBasis.cross( m_mtxPOV.xBasis ).normalized();
39  }
40 
41  Vector vXBasis = vUp.cross(vZBasis).normalized();
42  Vector vYBasis = vZBasis.cross( vXBasis ).normalized();
43 
44  m_mtxPOV.xBasis = vXBasis;
45  m_mtxPOV.yBasis = vYBasis;
46  m_mtxPOV.zBasis = vZBasis;
47  m_mtxPOV.origin = vCameraPosition;
48 
50 }
51 
52 void Camera::RotateOrbit( float fDeltaMagnitude, float fDeltaLongitude, float fDeltaLatitude )
53 {
54  m_fOrbitDistance += fDeltaMagnitude;
55 
56  Vector vSphereZBasis = CartesianToSpherical( m_mtxPOV.zBasis );
57  float fNewLatitude = Clamp( vSphereZBasis.z + fDeltaLatitude,
60 
61  Vector vSphereNewZBasis( 1, vSphereZBasis.y + fDeltaLongitude, fNewLatitude );
62  Vector vSphereNewYBasis( 1, vSphereNewZBasis.y, vSphereNewZBasis.z + kfHalfPi );
63 
64  m_mtxPOV.zBasis = SphericalToCartesian( NormalizeSpherical( vSphereNewZBasis ) );
65  m_mtxPOV.yBasis = SphericalToCartesian( NormalizeSpherical( vSphereNewYBasis ) );
69 }
70 
71 void Camera::OnMouseWheel( float fDeltaZ )
72 {
73  static const float kfZScale = -0.15f;
74 
75  Move( m_mtxPOV.zBasis * fDeltaZ * kfZScale * m_fOrbitDistance );
77 }
78 
79 void Camera::OnMouseDown( const Vector& vMousePos )
80 {
81  m_vLastMousePos = vMousePos;
83 }
84 
85 void Camera::OnMouseMoveOrbit( const Vector& vMousePos )
86 {
87  static const float kfMouseOrbitAngleScale = 1/1024.0f;
88 
89  Vector vOrbitDelta = (vMousePos - m_vLastMousePos) * kfMouseOrbitAngleScale * kf2Pi;
90 
91  RotateOrbit( 0, vOrbitDelta.x, vOrbitDelta.y );
92 
93  m_vLastMousePos = vMousePos;
94 }
95 
96 }
void OnMouseDown(const Leap::Vector &vMousePos)
Definition: LeapUtil.cpp:79
Vector xBasis
Definition: LeapMath.h:1021
f
Vector cross(const Vector &other) const
Definition: LeapMath.h:360
bool IsNearZero(float fVal)
Definition: LeapUtil.h:84
Leap::Vector m_vLastMousePos
Definition: LeapUtil.h:416
T Clamp(T inVal, T minVal, T maxVal)
Definition: LeapUtil.h:58
float m_fMaxOrbitLatitude
Definition: LeapUtil.h:419
void OnMouseWheel(float fDeltaZ)
convenience methods for mapping mouse movements to orbit camera movements.
Definition: LeapUtil.cpp:71
static const float kf2Pi
Definition: LeapUtil.h:41
static const float kfHalfPi
Definition: LeapUtil.h:42
Definition: Leap.h:48
Leap::Matrix m_mtxPOV
Definition: LeapUtil.h:415
TFSIMD_FORCE_INLINE Vector3 normalized() const
void RotateOrbit(float fDeltaMagnitude, float fDeltaLongitude, float fDeltaLatitude)
Definition: LeapUtil.cpp:52
void OnMouseMoveOrbit(const Leap::Vector &vMousePos)
Definition: LeapUtil.cpp:85
void Move(const Leap::Vector &vDeltaPos)
translate the position of the camera (move it from its current position) by the specified offset ...
Definition: LeapUtil.h:319
Leap::Vector SphericalToCartesian(const Leap::Vector &vSpherical)
Definition: LeapUtil.h:196
Vector yBasis
Definition: LeapMath.h:1029
Vector origin
Definition: LeapMath.h:1045
float dot(const Vector &other) const
Definition: LeapMath.h:340
Vector zBasis
Definition: LeapMath.h:1037
void updateOrbitDistance()
Definition: LeapUtil.h:404
float m_fOrbitDistance
Definition: LeapUtil.h:418
Leap::Vector CartesianToSpherical(const Leap::Vector &vCartesian)
Definition: LeapUtil.h:168
static const Vector & zero()
Definition: LeapMath.h:92
Leap::Vector NormalizeSpherical(const Leap::Vector &vSpherical)
set magnitude to 1 and bring heading to (-Pi,Pi], elevation into [-Pi/2, Pi/2]
Definition: LeapUtil.h:174
void SetPOVLookAt(const Leap::Vector &vCameraPosition, const Leap::Vector &vTarget, Leap::Vector vUp=Leap::Vector::yAxis())
places the camera a the given position with it pointing towards the specified target position ...
Definition: LeapUtil.cpp:18


leap_motion
Author(s): Florian Lier , Mirza Shah , Isaac IY Saito
autogenerated on Tue Jun 2 2020 03:58:01