10 #define __LeapUtil_h__ 15 #if defined(_MSC_VER) && (_MSC_VER < 1600) 16 typedef __int8 int8_t;
17 typedef __int16 int16_t;
18 typedef unsigned __int8 uint8_t;
19 typedef unsigned __int16 uint16_t;
48 return lhs < rhs ? lhs : rhs;
54 return lhs > rhs ? lhs : rhs;
58 T
Clamp( T inVal, T minVal, T maxVal )
60 return (inVal < minVal) ? minVal : ((inVal > maxVal) ? maxVal : inVal);
64 template<
typename InterpType,
typename ParamType>
65 InterpType
Linterp( InterpType a, InterpType b, ParamType t )
67 return a +
static_cast<InterpType
>((b - a) * t);
74 return Leap::Vector( static_cast<float>(vIn.x), static_cast<float>(vIn.y), fZ );
81 return Leap::Vector( static_cast<float>(vIn.x), static_cast<float>(vIn.y), static_cast<float>(vIn.z) );
146 return Min( vVec.
x,
Min( vVec.
y, vVec.
z ) );
151 return Max( vVec.
x,
Max( vVec.
y, vVec.
z ) );
157 return atan2f( vVec.
z, vVec.
x );
163 return atan2f( vVec.
y, sqrtf(vVec.
z * vVec.
z + vVec.
x * vVec.
x) );
176 float fHeading = vSpherical.
y;
177 float fElevation = vSpherical.
z;
179 for ( ; fElevation <= -
kfPi; fElevation +=
kf2Pi );
180 for ( ; fElevation >
kfPi; fElevation -=
kf2Pi );
182 if ( fabs(fElevation) > kfHalfPi )
185 fElevation = fElevation > 0 ? (kfPi - fElevation) : -(kfPi + fElevation);
188 for ( ; fHeading <= -
kfPi; fHeading +=
kf2Pi );
189 for ( ; fHeading >
kfPi; fHeading -=
kf2Pi );
198 const float fMagnitude = vSpherical.
x;
199 const float fCosHeading = cosf( vSpherical.
y );
200 const float fSinHeading = sinf( vSpherical.
y );
201 const float fCosElevation = cosf( vSpherical.
z );
202 const float fSinElevation = sinf( vSpherical.
z );
204 return Leap::Vector( fCosHeading * fCosElevation * fMagnitude,
205 fSinElevation * fMagnitude,
206 fSinHeading * fCosElevation * fMagnitude);
211 return bVal ?
"On" :
"Off";
214 template<
int _HistoryLength=256>
275 : m_fOrbitDistance(-1.0
f),
276 m_fMaxOrbitLatitude(
LeapUtil::kfPi * 0.375
f ),
278 m_fFarClip( 128.0
f ),
279 m_fVerticalFOVDegrees( 40.0
f ),
280 m_fAspectRatio( 4.0
f/3.0
f )
304 m_mtxPOV.xBasis = mtxRot.
xBasis;
305 m_mtxPOV.yBasis = mtxRot.
yBasis;
306 m_mtxPOV.zBasis = mtxRot.
zBasis;
315 SetPOVLookAt( m_mtxPOV.origin, vTarget, vUp );
321 m_mtxPOV.origin += vDeltaPos;
338 return m_vOrbitTarget;
343 m_vOrbitTarget = vOrbitTarget;
344 updateOrbitDistance();
352 void RotateOrbit(
float fDeltaMagnitude,
float fDeltaLongitude,
float fDeltaLatitude );
358 return m_fMaxOrbitLatitude;
363 m_fMaxOrbitLatitude = fMaxOrbitLatitude;
370 return m_mtxPOV.zBasis.dot( m_mtxPOV.origin - vPos );
375 void OnMouseWheel(
float fDeltaZ );
391 SetNearClip( fNearClip );
392 SetFarClip( fFarClip );
406 float fOrbitDistance = (m_vOrbitTarget - m_mtxPOV.origin).magnitude();
408 if ( !
IsNearZero( fOrbitDistance - m_fOrbitDistance ) )
410 m_fOrbitDistance = fOrbitDistance;
432 m_fScrollSize(512.0
f),
437 m_fFixedTimeStep(1.0
f/60.0
f),
438 m_fPendingDeltaTime(0.0
f)
463 void setScrollSize(
float fScrollSize ) { m_fScrollSize = (fScrollSize > 0.0f ? fScrollSize : 1.0f); }
472 void setSpeed(
float fSpeed ) { m_fSpeed = fSpeed; }
479 void setMinSpeed(
float fMinSpeed ) { m_fMinSpeed = fabs(fMinSpeed); }
492 void setDrag(
float fDrag ) { m_fDrag = fabs(fDrag); }
509 setDirection( vDirection );
521 if ( fDeltaTimeSeconds <= 0.0
f )
526 if ( fabs(m_fSpeed) > m_fMinSpeed )
532 const float fMinScrollSpeed = m_fMinSpeed / m_fScrollSize;
534 const float fScrollScale = (m_fSpeed < 0.0f ? -1.0f : 1.0f) * m_fScrollSize;
536 float fScrollSpeed = m_fSpeed / fScrollScale;
539 fDeltaTimeSeconds += m_fPendingDeltaTime;
542 for ( ; fDeltaTimeSeconds >= m_fFixedTimeStep; fDeltaTimeSeconds -= m_fFixedTimeStep )
544 const float fDragForce = powf( fScrollSpeed, m_fDragPower ) * m_fDrag;
547 fScrollSpeed -= fDragForce * m_fFixedTimeStep;
550 if ( fScrollSpeed <= fMinScrollSpeed )
553 fDeltaTimeSeconds = 0.0f;
558 m_fSpeed = fScrollSpeed * fScrollScale;
561 m_vPosition += m_vDirection * (m_fFixedTimeStep * m_fSpeed);
565 m_fPendingDeltaTime = fDeltaTimeSeconds;
570 m_vPosition += m_vDirection * ((m_fPendingDeltaTime + fDeltaTimeSeconds) * m_fSpeed);
571 m_fPendingDeltaTime = 0.0f;
578 m_fPendingDeltaTime = 0.0f;
600 static void Destroy( T* pPointer ) {
delete pPointer; }
608 static void Destroy( T* pPointer ) {
delete[] pPointer; }
624 template<
typename T,
626 unsigned int ManagedPointerPoolSize = 512 >
630 enum { kManagedPointerPoolSize = ManagedPointerPoolSize };
647 enum { kPoolSize = kManagedPointerPoolSize, kDeadPointer = 0xdeadbea7 };
653 for ( uint32_t i = 0; i < static_cast<uint32_t>(kPoolSize); i++ )
655 m_aEntryPool[i].m_pPointer =
reinterpret_cast<ManagedType*
>(kDeadPointer);
656 m_aEntryPool[i].m_uiRefCount = i + 1;
671 pEntry = findEntry( pPointer );
678 else if ( (m_uiNextFree < kPoolSize) && pPointer )
681 pEntry = m_aEntryPool + m_uiNextFree;
702 const uint32_t entryIndex =
static_cast<uint32_t
>(pEntry - m_aEntryPool);
705 if ( (entryIndex < kPoolSize) &&
706 (m_uiNumAllocated != 0) &&
707 (pEntry->
m_pPointer != reinterpret_cast<ManagedType*>(kDeadPointer)) )
714 m_uiNextFree = entryIndex;
726 if ( pPointer && (pPointer != reinterpret_cast<ManagedType*>(kDeadPointer)) )
730 for ( uint32_t i = 0, j = 0; j < m_uiNumAllocated && i < static_cast<uint32_t>(kPoolSize); i++ )
733 if ( m_aEntryPool[i].m_pPointer != reinterpret_cast<ManagedType*>(kDeadPointer) )
736 if ( m_aEntryPool[i].m_pPointer == pPointer )
738 return m_aEntryPool + i;
754 uint32_t
getNumFree()
const {
return static_cast<uint32_t
>(kPoolSize) - m_uiNumAllocated; }
769 static ManagedPointerPool _s_pool;
776 if ( m_pManagedPointer )
778 m_pManagedPointer->m_uiRefCount++;
787 if ( m_pManagedPointer )
789 if ( m_pManagedPointer->m_uiRefCount )
791 m_pManagedPointer->m_uiRefCount--;
794 if ( !m_pManagedPointer->m_uiRefCount )
796 Destructor::Destroy(m_pManagedPointer->m_pPointer);
797 s_pool().freeEntry(m_pManagedPointer);
800 m_pManagedPointer = NULL;
807 : m_pManagedPointer(NULL)
817 : m_pManagedPointer( s_pool().allocEntry( pManaged ) )
823 : m_pManagedPointer( rhs.m_pManagedPointer )
850 ManagedType*
GetPointer()
const {
return m_pManagedPointer ? m_pManagedPointer->m_pPointer : NULL; }
853 uint32_t
GetRefCount()
const {
return m_pManagedPointer ? m_pManagedPointer->m_uiRefCount : 0u; }
856 operator ManagedType*()
const {
return GetPointer(); }
858 ManagedType* operator ->()
const {
return GetPointer(); }
861 operator bool()
const {
return m_pManagedPointer != NULL; }
863 bool operator !()
const {
return !m_pManagedPointer; }
865 bool operator ==(
const ManagedType* pPointer )
const {
return GetPointer() == pPointer; }
867 bool operator !=(
const ManagedType* pPointer )
const {
return GetPointer() != pPointer; }
891 return s_pool().findEntry( pPointer ) != NULL;
904 #endif // __LeapUtil_h__
Leap::Vector FromVector2(const Vec2 &vIn, float fZ=0.0f)
requires that the source vector type have direct member access with names x, y
ManagedPointerEntry * m_pManagedPointer
void SetVerticalFOVDegrees(float fFOV)
static const SmartPointer & Null()
convenient static method for returning in cases where a null return value is needed.
const Leap::Vector & GetPosition() const
position of the camera in world space
void SetPosition(const Leap::Vector &vPos)
void SetRotation(const Leap::Vector &vAxis, float fRadians)
static ManagedPointerPool & s_pool()
bool IsNearEqual(const T &a, const T &b)
works with Vectors as well as floats
Leap::Matrix RigidInverse(const Leap::Matrix &mtxTransform)
float AddSample(float fSample)
Vector transformDirection(const Vector &in) const
SmartPointer(const SmartPointer &rhs)
copy constructor. increases reference count for shared managed pointer entry
bool IsNearZero(float fVal)
float GetVerticalFOVDegrees() const
Leap::Vector m_vLastMousePos
void SetPOV(const Leap::Matrix &mtxPOV)
void Rotate(const Leap::Vector &vAxis, float fRadians)
T Clamp(T inVal, T minVal, T maxVal)
float MinComponent(const Leap::Vector &vVec)
float GetMaxOrbitLatitude() const
const Leap::Vector & GetOrbitTarget() const
the point of interest for the camera to orbit around
Leap::Matrix RotationInverse(const Leap::Matrix &mtxRot)
returns a matrix representing the inverse rotation by simple transposition of the rotation block...
InterpType Linterp(InterpType a, InterpType b, ParamType t)
works with Vector as well as floating point scalar types.
void SetOrbitTarget(const Leap::Vector &vOrbitTarget)
float operator[](uint32_t uiIdx) const
index 0 is the oldest sample, index kHistorySize - 1 is the newest.
uint32_t GetRefCount() const
how many references are there to this managed pointer?
float m_fMaxOrbitLatitude
Leap::Matrix GetRotation() const
orientation of the camera in world space
uint32_t m_uiNumAllocated
float GetNearClip() const
static const float kfHalfPi
void SetRotation(const Leap::Matrix &mtxRot)
ManagedPointerEntry * allocEntry(ManagedType *pPointer)
const Leap::Matrix & GetPOV() const
POV is the world space location and orientation of the camera.
float m_fVerticalFOVDegrees
void SetRotationLookAt(const Leap::Vector &vTarget, const Leap::Vector &vUp=Leap::Vector::yAxis())
maintains the current camera position and turns it to look at the specified target position ...
uint32_t getNumAllocated() const
alternative destructor for arrays of objects
float Elevation(const Leap::Vector &vVec)
compute the spherical elevation of a vector direction in y above the z/x plane
float m_afSamples[kHistoryLength]
float MaxComponent(const Leap::Vector &vVec)
float GetAspectRatio() const
static void Destroy(T *pPointer)
SmartPointer(ManagedType *pManaged)
static uint32_t GetNumManagedPointers()
void Move(const Leap::Vector &vDeltaPos)
translate the position of the camera (move it from its current position) by the specified offset ...
void SetClipPlanes(float fNearClip, float fFarClip)
ManagedType * GetPointer() const
void refInc()
increment the reference count on our shared managed pointer entry (if any)
Leap::Vector SphericalToCartesian(const Leap::Vector &vSpherical)
uint32_t getNumFree() const
void SetAspectRatio(float fAspectRatio)
Leap::Vector FromVector3(const Vec3 &vIn)
requires that the source vector type have direct member access with names x, y, z ...
Leap::Vector ComponentWiseReciprocal(const Leap::Vector &vVec)
void SetNearClip(float fNearClip)
static void Destroy(T *pPointer)
static const Vector & yAxis()
~SmartPointer()
destructor decreases reference count for managed pointer entry.
Leap::Vector ComponentWiseMax(const Leap::Vector &vLHS, const Leap::Vector &vRHS)
ManagedPointerEntry * findEntry(const ManagedType *pPointer)
static bool IsManaged(const ManagedType *pPointer)
returns true if the given raw pointer is managed by a smart pointer somewhere, false if not...
void updateOrbitDistance()
SmartPointer()
default constructor - equivalent of a NULL pointer.
void SetMaxOrbitLatitude(float fMaxOrbitLatitude)
Leap::Vector CartesianToSpherical(const Leap::Vector &vCartesian)
Leap::Matrix ExtractRotation(const Leap::Matrix &mtxTransform)
create a new matrix with just the rotation block from the argument matrix
Leap::Vector NormalizeSpherical(const Leap::Vector &vSpherical)
set magnitude to 1 and bring heading to (-Pi,Pi], elevation into [-Pi/2, Pi/2]
Leap::Matrix GetView() const
const char * BoolToStr(uint32_t bVal)
static const float kfEpsilon
uint32_t GetNumSamples() const
float CalcViewDepth(const Leap::Vector &vPos) const
void SetFarClip(float fFarClip)
void freeEntry(ManagedPointerEntry *pEntry)
Leap::Vector ComponentWiseMin(const Leap::Vector &vLHS, const Leap::Vector &vRHS)
default destruction template class used by smart pointer.
Leap::Vector m_vOrbitTarget
float Heading(const Leap::Vector &vVec)
compute the polar/spherical heading of a vector direction in z/x plane
void Rotate(const Leap::Matrix &mtxRotation)
apply the specified rotation to the camera on top of its current rotation
Leap::Vector ComponentWiseScale(const Leap::Vector &vLHS, const Leap::Vector &vRHS)