LeapUtil.h
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 
9 #ifndef __LeapUtil_h__
10 #define __LeapUtil_h__
11 
12 #include "Leap.h"
13 
14 // Define integer types for Visual Studio 2005
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;
20 #endif
21 
22 namespace LeapUtil {
23 
30 
39 
40 static const float kfPi = Leap::PI;
41 static const float kf2Pi = kfPi + kfPi;
42 static const float kfHalfPi = kfPi * 0.5f;
43 static const float kfEpsilon = 0.00001f;
44 
45 template<typename T>
46 T Min( T lhs, T rhs )
47 {
48  return lhs < rhs ? lhs : rhs;
49 }
50 
51 template<typename T>
52 T Max( T lhs, T rhs )
53 {
54  return lhs > rhs ? lhs : rhs;
55 }
56 
57 template<typename T>
58 T Clamp( T inVal, T minVal, T maxVal )
59 {
60  return (inVal < minVal) ? minVal : ((inVal > maxVal) ? maxVal : inVal);
61 }
62 
64 template<typename InterpType, typename ParamType>
65 InterpType Linterp( InterpType a, InterpType b, ParamType t )
66 {
67  return a + static_cast<InterpType>((b - a) * t);
68 }
69 
71 template<class Vec2>
72 Leap::Vector FromVector2( const Vec2& vIn, float fZ = 0.0f )
73 {
74  return Leap::Vector( static_cast<float>(vIn.x), static_cast<float>(vIn.y), fZ );
75 }
76 
78 template<class Vec3>
79 Leap::Vector FromVector3( const Vec3& vIn )
80 {
81  return Leap::Vector( static_cast<float>(vIn.x), static_cast<float>(vIn.y), static_cast<float>(vIn.z) );
82 }
83 
84 inline bool IsNearZero( float fVal )
85 {
86  return fabs(fVal) <= kfEpsilon;
87 }
88 
89 inline bool IsNearZero( const Leap::Vector& vVec )
90 {
91  return IsNearZero(vVec.x) && IsNearZero(vVec.y) && IsNearZero(vVec.z);
92 }
93 
95 template<typename T>
96 inline bool IsNearEqual( const T& a, const T& b )
97 {
98  return IsNearZero( a - b );
99 }
100 
102 inline Leap::Matrix ExtractRotation( const Leap::Matrix& mtxTransform )
103 {
104  return Leap::Matrix( mtxTransform.xBasis, mtxTransform.yBasis, mtxTransform.zBasis );
105 }
106 
109 {
110  return Leap::Matrix( Leap::Vector( mtxRot.xBasis.x, mtxRot.yBasis.x, mtxRot.zBasis.x ),
111  Leap::Vector( mtxRot.xBasis.y, mtxRot.yBasis.y, mtxRot.zBasis.y ),
112  Leap::Vector( mtxRot.xBasis.z, mtxRot.yBasis.z, mtxRot.zBasis.z ) );
113 }
114 
117 inline Leap::Matrix RigidInverse( const Leap::Matrix& mtxTransform )
118 {
119  Leap::Matrix rigidInverse = RotationInverse( mtxTransform );
120  rigidInverse.origin = rigidInverse.transformDirection( -mtxTransform.origin );
121  return rigidInverse;
122 }
123 
124 inline Leap::Vector ComponentWiseMin( const Leap::Vector& vLHS, const Leap::Vector& vRHS )
125 {
126  return Leap::Vector( Min( vLHS.x, vRHS.x ), Min( vLHS.y, vRHS.y ), Min( vLHS.z, vRHS.z ) );
127 }
128 
129 inline Leap::Vector ComponentWiseMax( const Leap::Vector& vLHS, const Leap::Vector& vRHS )
130 {
131  return Leap::Vector( Max( vLHS.x, vRHS.x ), Max( vLHS.y, vRHS.y ), Max( vLHS.z, vRHS.z ) );
132 }
133 
134 inline Leap::Vector ComponentWiseScale( const Leap::Vector& vLHS, const Leap::Vector& vRHS )
135 {
136  return Leap::Vector( vLHS.x * vRHS.x, vLHS.y * vRHS.y, vLHS.z * vRHS.z );
137 }
138 
140 {
141  return Leap::Vector( 1.0f/vVec.x, 1.0f/vVec.y, 1.0f/vVec.z );
142 }
143 
144 inline float MinComponent( const Leap::Vector& vVec )
145 {
146  return Min( vVec.x, Min( vVec.y, vVec.z ) );
147 }
148 
149 inline float MaxComponent( const Leap::Vector& vVec )
150 {
151  return Max( vVec.x, Max( vVec.y, vVec.z ) );
152 }
153 
155 inline float Heading( const Leap::Vector& vVec )
156 {
157  return atan2f( vVec.z, vVec.x );
158 }
159 
161 inline float Elevation( const Leap::Vector& vVec )
162 {
163  return atan2f( vVec.y, sqrtf(vVec.z * vVec.z + vVec.x * vVec.x) );
164 }
165 
168 inline Leap::Vector CartesianToSpherical( const Leap::Vector& vCartesian )
169 {
170  return Leap::Vector( vCartesian.magnitude(), Heading(vCartesian), Elevation(vCartesian) );
171 }
172 
174 inline Leap::Vector NormalizeSpherical( const Leap::Vector& vSpherical )
175 {
176  float fHeading = vSpherical.y;
177  float fElevation = vSpherical.z;
178 
179  for ( ; fElevation <= -kfPi; fElevation += kf2Pi );
180  for ( ; fElevation > kfPi; fElevation -= kf2Pi );
181 
182  if ( fabs(fElevation) > kfHalfPi )
183  {
184  fHeading += kfPi;
185  fElevation = fElevation > 0 ? (kfPi - fElevation) : -(kfPi + fElevation);
186  }
187 
188  for ( ; fHeading <= -kfPi; fHeading += kf2Pi );
189  for ( ; fHeading > kfPi; fHeading -= kf2Pi );
190 
191  return Leap::Vector( 1, fHeading, fElevation );
192 }
193 
196 inline Leap::Vector SphericalToCartesian( const Leap::Vector& vSpherical )
197 {
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 );
203 
204  return Leap::Vector( fCosHeading * fCosElevation * fMagnitude,
205  fSinElevation * fMagnitude,
206  fSinHeading * fCosElevation * fMagnitude);
207 }
208 
209 inline const char* BoolToStr( uint32_t bVal )
210 {
211  return bVal ? "On" : "Off";
212 }
213 
214 template<int _HistoryLength=256>
216 {
217 public:
218  enum
219  {
220  kHistoryLength = _HistoryLength
221  };
222 
223 public:
225 
226  void Reset()
227  {
228  m_uiCurIndex = 0;
229  m_fNumSamples = 0.0f;
230  m_fSum = 0.0f;
231  m_fAverage = 0.0f;
232  for ( int i = 0; i < kHistoryLength; m_afSamples[i++] = 0.0f );
233  }
234 
235  float AddSample( float fSample )
236  {
237  m_fNumSamples = Min( (m_fNumSamples + 1.0f), static_cast<float>(kHistoryLength) );
239  m_fSum += fSample;
240  m_fAverage = m_fSum * (1.0f/m_fNumSamples);
241 
242  m_afSamples[m_uiCurIndex] = fSample;
243 
244  m_uiCurIndex = static_cast<uint32_t>((m_uiCurIndex + 1) % kHistoryLength);
245 
246  return m_fAverage;
247  }
248 
249  float GetAverage() const { return m_fAverage; }
250  float GetSum() const { return m_fSum; }
251  uint32_t GetNumSamples() const { return static_cast<uint32_t>(m_fNumSamples); }
252 
254  float operator[](uint32_t uiIdx) const { return m_afSamples[(m_uiCurIndex + uiIdx) % kHistoryLength]; }
255 
256 private:
257  uint32_t m_uiCurIndex;
259  float m_fSum;
260  float m_fAverage;
262 };
263 
271 class Camera
272 {
273 public:
275  : m_fOrbitDistance(-1.0f),
276  m_fMaxOrbitLatitude( LeapUtil::kfPi * 0.375f ),
277  m_fNearClip( 0.1f ),
278  m_fFarClip( 128.0f ),
279  m_fVerticalFOVDegrees( 40.0f ),
280  m_fAspectRatio( 4.0f/3.0f )
281  {}
282 
284  const Leap::Matrix& GetPOV() const { return m_mtxPOV; }
285 
286  void SetPOV( const Leap::Matrix& mtxPOV ) { m_mtxPOV = mtxPOV; }
287 
290  Leap::Matrix GetView() const { return LeapUtil::RigidInverse(m_mtxPOV); }
291 
293  const Leap::Vector& GetPosition() const { return m_mtxPOV.origin; }
294 
295  void SetPosition( const Leap::Vector& vPos ) { m_mtxPOV.origin = vPos; }
296 
298  Leap::Matrix GetRotation() const { return LeapUtil::ExtractRotation(m_mtxPOV); }
299 
300  void SetRotation( const Leap::Vector& vAxis, float fRadians ) { m_mtxPOV.setRotation( vAxis, fRadians ); }
301 
302  void SetRotation( const Leap::Matrix& mtxRot )
303  {
304  m_mtxPOV.xBasis = mtxRot.xBasis;
305  m_mtxPOV.yBasis = mtxRot.yBasis;
306  m_mtxPOV.zBasis = mtxRot.zBasis;
307  }
308 
310  void SetPOVLookAt( const Leap::Vector& vCameraPosition, const Leap::Vector& vTarget, Leap::Vector vUp = Leap::Vector::yAxis() );
311 
313  void SetRotationLookAt( const Leap::Vector& vTarget, const Leap::Vector& vUp = Leap::Vector::yAxis() )
314  {
315  SetPOVLookAt( m_mtxPOV.origin, vTarget, vUp );
316  }
317 
319  void Move( const Leap::Vector& vDeltaPos )
320  {
321  m_mtxPOV.origin += vDeltaPos;
322  }
323 
325  void Rotate( const Leap::Matrix& mtxRotation )
326  {
327  m_mtxPOV = m_mtxPOV * LeapUtil::ExtractRotation(mtxRotation);
328  }
329 
330  void Rotate( const Leap::Vector& vAxis, float fRadians )
331  {
332  m_mtxPOV = m_mtxPOV * Leap::Matrix( vAxis, fRadians );
333  }
334 
337  {
338  return m_vOrbitTarget;
339  }
340 
341  void SetOrbitTarget( const Leap::Vector& vOrbitTarget)
342  {
343  m_vOrbitTarget = vOrbitTarget;
344  updateOrbitDistance();
345  }
346 
352  void RotateOrbit( float fDeltaMagnitude, float fDeltaLongitude, float fDeltaLatitude );
353 
356  float GetMaxOrbitLatitude() const
357  {
358  return m_fMaxOrbitLatitude;
359  }
360 
361  void SetMaxOrbitLatitude( float fMaxOrbitLatitude )
362  {
363  m_fMaxOrbitLatitude = fMaxOrbitLatitude;
364  }
365 
368  float CalcViewDepth( const Leap::Vector& vPos ) const
369  {
370  return m_mtxPOV.zBasis.dot( m_mtxPOV.origin - vPos );
371  }
372 
374 
375  void OnMouseWheel( float fDeltaZ );
376 
377  void OnMouseDown( const Leap::Vector& vMousePos );
378 
379  void OnMouseMoveOrbit( const Leap::Vector& vMousePos );
380 
381  float GetNearClip() const { return m_fNearClip; }
382 
383  float GetFarClip() const { return m_fFarClip; }
384 
385  void SetNearClip( float fNearClip ) { m_fNearClip = Max( 0.01f, fNearClip ); }
386 
387  void SetFarClip( float fFarClip ) { m_fFarClip = fFarClip; }
388 
389  void SetClipPlanes( float fNearClip, float fFarClip )
390  {
391  SetNearClip( fNearClip );
392  SetFarClip( fFarClip );
393  }
394 
395  float GetVerticalFOVDegrees() const { return m_fVerticalFOVDegrees; }
396 
397  void SetVerticalFOVDegrees( float fFOV ) { m_fVerticalFOVDegrees = fFOV; }
398 
399  float GetAspectRatio() const { return m_fAspectRatio; }
400 
401  void SetAspectRatio( float fAspectRatio ) { m_fAspectRatio = fAspectRatio; }
402 
403 private:
405  {
406  float fOrbitDistance = (m_vOrbitTarget - m_mtxPOV.origin).magnitude();
407 
408  if ( !IsNearZero( fOrbitDistance - m_fOrbitDistance ) )
409  {
410  m_fOrbitDistance = fOrbitDistance;
411  }
412  }
413 
414 private:
420  float m_fNearClip;
421  float m_fFarClip;
424 };
425 
426 
429 public:
431  : m_vDirection(Leap::Vector::yAxis()),
432  m_fScrollSize(512.0f),
433  m_fSpeed(0),
434  m_fMinSpeed(0.125f),
435  m_fDrag(0.4f),
436  m_fDragPower(2.0f),
437  m_fFixedTimeStep(1.0f/60.0f),
438  m_fPendingDeltaTime(0.0f)
439  {}
440 
445  const Leap::Vector& getPosition() const { return m_vPosition; }
446  void setPosition( const Leap::Vector& vPosition ) { m_vPosition = vPosition; }
447 
451  const Leap::Vector& getDirection() const { return m_vDirection; }
452  void setDirection( const Leap::Vector& vDirection ) { m_vDirection = vDirection.normalized(); }
453 
462  float getScrollSize() const { return m_fScrollSize; }
463  void setScrollSize( float fScrollSize ) { m_fScrollSize = (fScrollSize > 0.0f ? fScrollSize : 1.0f); }
464 
471  float getSpeed() const { return m_fSpeed; }
472  void setSpeed( float fSpeed ) { m_fSpeed = fSpeed; }
473 
478  float getMinSpeed() const { return m_fMinSpeed; }
479  void setMinSpeed( float fMinSpeed ) { m_fMinSpeed = fabs(fMinSpeed); }
480 
485  float getFixedTimeStep() const { return m_fMinSpeed; }
486  void setFixedTimeStep( float fFixedTimeStep ) { m_fFixedTimeStep = fabs(fFixedTimeStep); }
487 
491  float getDrag() const { return m_fDrag; }
492  void setDrag( float fDrag ) { m_fDrag = fabs(fDrag); }
493 
498  float getDragPower() const { return m_fDragPower; }
499  void setDragPower( float fDragPower ) { m_fDragPower = fDragPower; }
500 
503  const Leap::Vector getVelocity() const { return m_vDirection * m_fSpeed; }
504 
507  void setVelocity( const Leap::Vector& vDirection, float fSpeed )
508  {
509  setDirection( vDirection );
510  setSpeed( fSpeed );
511  }
512 
519  void update( float fDeltaTimeSeconds )
520  {
521  if ( fDeltaTimeSeconds <= 0.0f )
522  {
523  return;
524  }
525 
526  if ( fabs(m_fSpeed) > m_fMinSpeed )
527  {
528  if ( m_fDrag > 0 )
529  {
530  // only force to fixed time step if doing drag calculation
531 
532  const float fMinScrollSpeed = m_fMinSpeed / m_fScrollSize;
533  // fold the sign of speed into the scaling factor
534  const float fScrollScale = (m_fSpeed < 0.0f ? -1.0f : 1.0f) * m_fScrollSize;
535  // absolute value of scaled speed (_scrollScale has same sign as speed, guaranteeing positive value)
536  float fScrollSpeed = m_fSpeed / fScrollScale;
537 
538  // include any left over time from previous update in the time to process for this update.
539  fDeltaTimeSeconds += m_fPendingDeltaTime;
540 
541  // process time since last update in fixed chunks
542  for ( ; fDeltaTimeSeconds >= m_fFixedTimeStep; fDeltaTimeSeconds -= m_fFixedTimeStep )
543  {
544  const float fDragForce = powf( fScrollSpeed, m_fDragPower ) * m_fDrag;
545 
546  // decelerate unsigned/scaled speed by drag
547  fScrollSpeed -= fDragForce * m_fFixedTimeStep;
548 
549  // bail out if speed has hit minimum threshold
550  if ( fScrollSpeed <= fMinScrollSpeed )
551  {
552  m_fSpeed = 0.0f;
553  fDeltaTimeSeconds = 0.0f;
554  break;
555  }
556 
557  // update signed/unscaled speed
558  m_fSpeed = fScrollSpeed * fScrollScale;
559 
560  // step position by velocity
561  m_vPosition += m_vDirection * (m_fFixedTimeStep * m_fSpeed);
562  }
563 
564  // keep track of any leftover time not processed during this update.
565  m_fPendingDeltaTime = fDeltaTimeSeconds;
566  }
567  else
568  {
569  // simple position change with velocity and no drag - not affected by large time steps. no looping needed.
570  m_vPosition += m_vDirection * ((m_fPendingDeltaTime + fDeltaTimeSeconds) * m_fSpeed);
571  m_fPendingDeltaTime = 0.0f;
572  }
573  }
574  else
575  {
576  // enforce zeroing of speeds below minimum absolute value.
577  m_fSpeed = 0.0f;
578  m_fPendingDeltaTime = 0.0f;
579  }
580  }
581 
582 protected:
586  float m_fSpeed;
587  float m_fMinSpeed;
588  float m_fDrag;
592 };
593 
594 
596 template<typename T>
598 {
599 public:
600  static void Destroy( T* pPointer ) { delete pPointer; }
601 };
602 
604 template<typename T>
606 {
607 public:
608  static void Destroy( T* pPointer ) { delete[] pPointer; }
609 };
610 
624 template< typename T,
625  class Destructor = SmartInstanceDestructor<T>,
626  unsigned int ManagedPointerPoolSize = 512 >
628 {
629 public:
630  enum { kManagedPointerPoolSize = ManagedPointerPoolSize };
631 
632  typedef T ManagedType;
633 
634 private:
635  // a managed pointer is a pointer of the desired type plus a reference count.
637  {
638  ManagedType* m_pPointer;
639  uint32_t m_uiRefCount;
640  };
641 
642  // this private, embedded class manages a pool of smart pointers for the given type.
643  // there are separate pools for each created type of smart pointer.
644  // the pool is managed using a simple O(1) fixed size free list allocator
646  {
647  enum { kPoolSize = kManagedPointerPoolSize, kDeadPointer = 0xdeadbea7 };
648 
650  : m_uiNextFree(0),
651  m_uiNumAllocated(0)
652  {
653  for ( uint32_t i = 0; i < static_cast<uint32_t>(kPoolSize); i++ )
654  {
655  m_aEntryPool[i].m_pPointer = reinterpret_cast<ManagedType*>(kDeadPointer);
656  m_aEntryPool[i].m_uiRefCount = i + 1;
657  }
658  }
659 
660  // allocate a managed pointer entry for a raw pointer
661  ManagedPointerEntry* allocEntry( ManagedType* pPointer )
662  {
663  ManagedPointerEntry* pEntry = NULL;
664 
665  // this incurs a linear search penalty each time
666  // we create a new smart pointer from a raw pointer
667  // but adds significant safety - it prevents
668  // assigning the same raw pointer to two different
669  // member entries - this would result in double
670  // deletion when the 2nd one hit its end of life.
671  pEntry = findEntry( pPointer );
672 
673  if ( pEntry )
674  {
675  // we found an existing managed pointer entry, just increase the reference count
676  pEntry->m_uiRefCount++;
677  }
678  else if ( (m_uiNextFree < kPoolSize) && pPointer )
679  {
680  // grab the next free entry from the head of the free list
681  pEntry = m_aEntryPool + m_uiNextFree;
682 
683  // the head of the list now becomes the next free entry in the chain
684  // (m_uiRefCount is serving double-duty as next index member for entries that are still in the pool)
685  m_uiNextFree = pEntry->m_uiRefCount;
686 
687  // initialize the entry with the pointer and a reference count of 1.
688  pEntry->m_pPointer = pPointer;
689  pEntry->m_uiRefCount = 1;
690 
691  // book keeping.
692  m_uiNumAllocated++;
693  }
694 
695  return pEntry;
696  }
697 
698  // return an entry to the pool
700  {
701  // calculate the entry index as an offset from the start of the pool
702  const uint32_t entryIndex = static_cast<uint32_t>(pEntry - m_aEntryPool);
703 
704  // if this is a valid allocated entry (from this pool, and not already freed)
705  if ( (entryIndex < kPoolSize) &&
706  (m_uiNumAllocated != 0) &&
707  (pEntry->m_pPointer != reinterpret_cast<ManagedType*>(kDeadPointer)) )
708  {
709  // clear the entry
710  pEntry->m_pPointer = NULL;
711 
712  // push it to the head of the free list
713  pEntry->m_uiRefCount = m_uiNextFree;
714  m_uiNextFree = entryIndex;
715 
716  // book keeping.
717  m_uiNumAllocated--;
718  }
719  }
720 
721  // look for the managed pointer entry associated with a raw pointer. returns NULL
722  // if pPointer is NULL, dead or not found.
723  ManagedPointerEntry* findEntry( const ManagedType* pPointer )
724  {
725  // don't bother if the pointer is NULL or marked as our dead value
726  if ( pPointer && (pPointer != reinterpret_cast<ManagedType*>(kDeadPointer)) )
727  {
728  // linear search. stop after testing as many live entries as are allocated instead of always
729  // searching the entire pool.
730  for ( uint32_t i = 0, j = 0; j < m_uiNumAllocated && i < static_cast<uint32_t>(kPoolSize); i++ )
731  {
732  // if the entry is live (allocated)
733  if ( m_aEntryPool[i].m_pPointer != reinterpret_cast<ManagedType*>(kDeadPointer) )
734  {
735  // a match? return it.
736  if ( m_aEntryPool[i].m_pPointer == pPointer )
737  {
738  return m_aEntryPool + i;
739  }
740 
741  // not, increment the count of live entries encountered.
742  j++;
743  }
744  }
745  }
746 
747  return NULL;
748  }
749 
750  // number of allocated managed pointer entries
751  uint32_t getNumAllocated() const { return m_uiNumAllocated; }
752 
753  // number of available managed pointer entries.
754  uint32_t getNumFree() const { return static_cast<uint32_t>(kPoolSize) - m_uiNumAllocated; }
755 
756  // actual entry pool
757  ManagedPointerEntry m_aEntryPool[kPoolSize];
758 
759  // head of the free list
760  uint32_t m_uiNextFree;
761 
762  // number of entries that have been allocated from the free list
764  };
765 
766  // static instance of the managed pointer pool
767  static ManagedPointerPool& s_pool()
768  {
769  static ManagedPointerPool _s_pool;
770  return _s_pool;
771  }
772 
774  void refInc()
775  {
776  if ( m_pManagedPointer )
777  {
778  m_pManagedPointer->m_uiRefCount++;
779  }
780  }
781 
785  void refDec()
786  {
787  if ( m_pManagedPointer )
788  {
789  if ( m_pManagedPointer->m_uiRefCount )
790  {
791  m_pManagedPointer->m_uiRefCount--;
792  }
793 
794  if ( !m_pManagedPointer->m_uiRefCount )
795  {
796  Destructor::Destroy(m_pManagedPointer->m_pPointer);
797  s_pool().freeEntry(m_pManagedPointer);
798  }
799 
800  m_pManagedPointer = NULL;
801  }
802  }
803 
804 public:
807  : m_pManagedPointer(NULL)
808  {
809  }
810 
816  explicit SmartPointer( ManagedType* pManaged )
817  : m_pManagedPointer( s_pool().allocEntry( pManaged ) )
818  {
819  }
820 
823  : m_pManagedPointer( rhs.m_pManagedPointer )
824  {
825  refInc();
826  }
827 
830  const SmartPointer& operator =( const SmartPointer& rhs )
831  {
832  if ( m_pManagedPointer != rhs.m_pManagedPointer )
833  {
834  refDec();
835  m_pManagedPointer = rhs.m_pManagedPointer;
836  refInc();
837  }
838 
839  return *this;
840  }
841 
844  {
845  refDec();
846  }
847 
850  ManagedType* GetPointer() const { return m_pManagedPointer ? m_pManagedPointer->m_pPointer : NULL; }
851 
853  uint32_t GetRefCount() const { return m_pManagedPointer ? m_pManagedPointer->m_uiRefCount : 0u; }
854 
856  operator ManagedType*() const { return GetPointer(); }
857 
858  ManagedType* operator ->() const { return GetPointer(); }
859 
861  operator bool() const { return m_pManagedPointer != NULL; }
862 
863  bool operator !() const { return !m_pManagedPointer; }
864 
865  bool operator ==( const ManagedType* pPointer ) const { return GetPointer() == pPointer; }
866 
867  bool operator !=( const ManagedType* pPointer ) const { return GetPointer() != pPointer; }
868 
869  bool operator ==( const SmartPointer& rhs ) const { return m_pManagedPointer == rhs.m_pManagedPointer; }
870 
871  bool operator !=( const SmartPointer& rhs ) const { return m_pManagedPointer != rhs.m_pManagedPointer; }
872 
876  void Release()
877  {
878  refDec();
879  }
880 
882  static const SmartPointer& Null()
883  {
884  static SmartPointer _s_null;
885  return _s_null;
886  }
887 
889  static bool IsManaged( const ManagedType* pPointer )
890  {
891  return s_pool().findEntry( pPointer ) != NULL;
892  }
893 
896  static uint32_t GetNumManagedPointers() { return s_pool().getNumAllocated(); }
897 
898 private:
899  ManagedPointerEntry* m_pManagedPointer;
900 };
901 
902 } // namespace Leap
903 
904 #endif // __LeapUtil_h__
Leap::Vector m_vPosition
Definition: LeapUtil.h:583
Leap::Vector FromVector2(const Vec2 &vIn, float fZ=0.0f)
requires that the source vector type have direct member access with names x, y
Definition: LeapUtil.h:72
Vector xBasis
Definition: LeapMath.h:1021
ManagedPointerEntry * m_pManagedPointer
Definition: LeapUtil.h:899
float GetAverage() const
Definition: LeapUtil.h:249
static const float kfPi
Definition: LeapUtil.h:40
void SetVerticalFOVDegrees(float fFOV)
Definition: LeapUtil.h:397
static const SmartPointer & Null()
convenient static method for returning in cases where a null return value is needed.
Definition: LeapUtil.h:882
const Leap::Vector & GetPosition() const
position of the camera in world space
Definition: LeapUtil.h:293
const Leap::Vector getVelocity() const
Definition: LeapUtil.h:503
void SetPosition(const Leap::Vector &vPos)
Definition: LeapUtil.h:295
void SetRotation(const Leap::Vector &vAxis, float fRadians)
Definition: LeapUtil.h:300
float m_fNearClip
Definition: LeapUtil.h:420
static ManagedPointerPool & s_pool()
Definition: LeapUtil.h:767
float getMinSpeed() const
Definition: LeapUtil.h:478
bool IsNearEqual(const T &a, const T &b)
works with Vectors as well as floats
Definition: LeapUtil.h:96
float getScrollSize() const
Definition: LeapUtil.h:462
float GetFarClip() const
Definition: LeapUtil.h:383
void setDrag(float fDrag)
Definition: LeapUtil.h:492
f
Leap::Matrix RigidInverse(const Leap::Matrix &mtxTransform)
Definition: LeapUtil.h:117
float AddSample(float fSample)
Definition: LeapUtil.h:235
void setPosition(const Leap::Vector &vPosition)
Definition: LeapUtil.h:446
Vector transformDirection(const Vector &in) const
Definition: LeapMath.h:817
T Min(T lhs, T rhs)
Definition: LeapUtil.h:46
SmartPointer(const SmartPointer &rhs)
copy constructor. increases reference count for shared managed pointer entry
Definition: LeapUtil.h:822
bool IsNearZero(float fVal)
Definition: LeapUtil.h:84
float GetVerticalFOVDegrees() const
Definition: LeapUtil.h:395
Leap::Vector m_vLastMousePos
Definition: LeapUtil.h:416
Leap::Vector m_vDirection
Definition: LeapUtil.h:584
T Max(T lhs, T rhs)
Definition: LeapUtil.h:52
void SetPOV(const Leap::Matrix &mtxPOV)
Definition: LeapUtil.h:286
void Rotate(const Leap::Vector &vAxis, float fRadians)
Definition: LeapUtil.h:330
T Clamp(T inVal, T minVal, T maxVal)
Definition: LeapUtil.h:58
float MinComponent(const Leap::Vector &vVec)
Definition: LeapUtil.h:144
float getFixedTimeStep() const
Definition: LeapUtil.h:485
float GetMaxOrbitLatitude() const
Definition: LeapUtil.h:356
const Leap::Vector & GetOrbitTarget() const
the point of interest for the camera to orbit around
Definition: LeapUtil.h:336
Leap::Matrix RotationInverse(const Leap::Matrix &mtxRot)
returns a matrix representing the inverse rotation by simple transposition of the rotation block...
Definition: LeapUtil.h:108
float m_fAspectRatio
Definition: LeapUtil.h:423
InterpType Linterp(InterpType a, InterpType b, ParamType t)
works with Vector as well as floating point scalar types.
Definition: LeapUtil.h:65
void setSpeed(float fSpeed)
Definition: LeapUtil.h:472
void SetOrbitTarget(const Leap::Vector &vOrbitTarget)
Definition: LeapUtil.h:341
float operator[](uint32_t uiIdx) const
index 0 is the oldest sample, index kHistorySize - 1 is the newest.
Definition: LeapUtil.h:254
uint32_t GetRefCount() const
how many references are there to this managed pointer?
Definition: LeapUtil.h:853
float m_fMaxOrbitLatitude
Definition: LeapUtil.h:419
static const float kf2Pi
Definition: LeapUtil.h:41
Leap::Matrix GetRotation() const
orientation of the camera in world space
Definition: LeapUtil.h:298
const Leap::Vector & getPosition() const
Definition: LeapUtil.h:445
float GetNearClip() const
Definition: LeapUtil.h:381
static const float kfHalfPi
Definition: LeapUtil.h:42
float getDragPower() const
Definition: LeapUtil.h:498
void SetRotation(const Leap::Matrix &mtxRot)
Definition: LeapUtil.h:302
ManagedPointerEntry * allocEntry(ManagedType *pPointer)
Definition: LeapUtil.h:661
const Leap::Matrix & GetPOV() const
POV is the world space location and orientation of the camera.
Definition: LeapUtil.h:284
float m_fVerticalFOVDegrees
Definition: LeapUtil.h:422
Definition: Leap.h:48
Leap::Matrix m_mtxPOV
Definition: LeapUtil.h:415
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 ...
Definition: LeapUtil.h:313
alternative destructor for arrays of objects
Definition: LeapUtil.h:605
float Elevation(const Leap::Vector &vVec)
compute the spherical elevation of a vector direction in y above the z/x plane
Definition: LeapUtil.h:161
float m_afSamples[kHistoryLength]
Definition: LeapUtil.h:261
float MaxComponent(const Leap::Vector &vVec)
Definition: LeapUtil.h:149
float GetAspectRatio() const
Definition: LeapUtil.h:399
static void Destroy(T *pPointer)
Definition: LeapUtil.h:600
float m_fFarClip
Definition: LeapUtil.h:421
SmartPointer(ManagedType *pManaged)
Definition: LeapUtil.h:816
float getSpeed() const
Definition: LeapUtil.h:471
static uint32_t GetNumManagedPointers()
Definition: LeapUtil.h:896
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
void setMinSpeed(float fMinSpeed)
Definition: LeapUtil.h:479
void SetClipPlanes(float fNearClip, float fFarClip)
Definition: LeapUtil.h:389
ManagedType * GetPointer() const
Definition: LeapUtil.h:850
void refInc()
increment the reference count on our shared managed pointer entry (if any)
Definition: LeapUtil.h:774
Utility class for adding simple momentum to 2D or 3D UI elements.
Definition: LeapUtil.h:428
Leap::Vector SphericalToCartesian(const Leap::Vector &vSpherical)
Definition: LeapUtil.h:196
Vector yBasis
Definition: LeapMath.h:1029
void SetAspectRatio(float fAspectRatio)
Definition: LeapUtil.h:401
static const float PI
Definition: LeapMath.h:24
const Leap::Vector & getDirection() const
Definition: LeapUtil.h:451
Leap::Vector FromVector3(const Vec3 &vIn)
requires that the source vector type have direct member access with names x, y, z ...
Definition: LeapUtil.h:79
Leap::Vector ComponentWiseReciprocal(const Leap::Vector &vVec)
Definition: LeapUtil.h:139
void SetNearClip(float fNearClip)
Definition: LeapUtil.h:385
float getDrag() const
Definition: LeapUtil.h:491
void setScrollSize(float fScrollSize)
Definition: LeapUtil.h:463
static void Destroy(T *pPointer)
Definition: LeapUtil.h:608
static const Vector & yAxis()
Definition: LeapMath.h:113
Vector origin
Definition: LeapMath.h:1045
Vector zBasis
Definition: LeapMath.h:1037
~SmartPointer()
destructor decreases reference count for managed pointer entry.
Definition: LeapUtil.h:843
Leap::Vector ComponentWiseMax(const Leap::Vector &vLHS, const Leap::Vector &vRHS)
Definition: LeapUtil.h:129
ManagedPointerEntry * findEntry(const ManagedType *pPointer)
Definition: LeapUtil.h:723
static bool IsManaged(const ManagedType *pPointer)
returns true if the given raw pointer is managed by a smart pointer somewhere, false if not...
Definition: LeapUtil.h:889
void setDragPower(float fDragPower)
Definition: LeapUtil.h:499
void update(float fDeltaTimeSeconds)
Definition: LeapUtil.h:519
void updateOrbitDistance()
Definition: LeapUtil.h:404
float m_fOrbitDistance
Definition: LeapUtil.h:418
SmartPointer()
default constructor - equivalent of a NULL pointer.
Definition: LeapUtil.h:806
void SetMaxOrbitLatitude(float fMaxOrbitLatitude)
Definition: LeapUtil.h:361
Leap::Vector CartesianToSpherical(const Leap::Vector &vCartesian)
Definition: LeapUtil.h:168
Leap::Matrix ExtractRotation(const Leap::Matrix &mtxTransform)
create a new matrix with just the rotation block from the argument matrix
Definition: LeapUtil.h:102
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
Leap::Matrix GetView() const
Definition: LeapUtil.h:290
const char * BoolToStr(uint32_t bVal)
Definition: LeapUtil.h:209
void setDirection(const Leap::Vector &vDirection)
Definition: LeapUtil.h:452
static const float kfEpsilon
Definition: LeapUtil.h:43
void setFixedTimeStep(float fFixedTimeStep)
Definition: LeapUtil.h:486
uint32_t GetNumSamples() const
Definition: LeapUtil.h:251
float CalcViewDepth(const Leap::Vector &vPos) const
Definition: LeapUtil.h:368
void SetFarClip(float fFarClip)
Definition: LeapUtil.h:387
void freeEntry(ManagedPointerEntry *pEntry)
Definition: LeapUtil.h:699
Leap::Vector ComponentWiseMin(const Leap::Vector &vLHS, const Leap::Vector &vRHS)
Definition: LeapUtil.h:124
default destruction template class used by smart pointer.
Definition: LeapUtil.h:597
float GetSum() const
Definition: LeapUtil.h:250
Leap::Vector m_vOrbitTarget
Definition: LeapUtil.h:417
float Heading(const Leap::Vector &vVec)
compute the polar/spherical heading of a vector direction in z/x plane
Definition: LeapUtil.h:155
void Rotate(const Leap::Matrix &mtxRotation)
apply the specified rotation to the camera on top of its current rotation
Definition: LeapUtil.h:325
Leap::Vector ComponentWiseScale(const Leap::Vector &vLHS, const Leap::Vector &vRHS)
Definition: LeapUtil.h:134
void setVelocity(const Leap::Vector &vDirection, float fSpeed)
Definition: LeapUtil.h:507


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