leapMotionFrame.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #ifndef _LEAPMOTIONFRAME_H_
  23. #define _LEAPMOTIONFRAME_H_
  24. #include "console/simObject.h"
  25. #include "math/mPoint3.h"
  26. #include "math/mMatrix.h"
  27. #include "math/mQuat.h"
  28. #include "Leap.h"
  29. class LeapMotionFrame : public SimObject
  30. {
  31. typedef SimObject Parent;
  32. public:
  33. enum PointableType
  34. {
  35. PT_UNKNOWN = -1,
  36. PT_FINGER = 0,
  37. PT_TOOL,
  38. };
  39. protected:
  40. static U32 smNextInternalFrameId;
  41. // Frame
  42. bool mFrameValid;
  43. U64 mFrameId;
  44. U64 mFrameTimeStamp;
  45. // Torque 3D frame information
  46. U32 mFrameInternalId;
  47. S32 mFrameSimTime;
  48. S32 mFrameRealTime;
  49. // Hands
  50. U32 mHandCount;
  51. Vector<bool> mHandValid;
  52. Vector<S32> mHandId;
  53. Vector<Point3F> mHandRawPos;
  54. Vector<Point3I> mHandPos;
  55. Vector<MatrixF> mHandRot;
  56. Vector<QuatF> mHandRotQuat;
  57. Vector<Point2F> mHandRotAxis;
  58. Vector<U32> mHandPointablesCount;
  59. // Pointables
  60. U32 mPointableCount;
  61. Vector<bool> mPointableValid;
  62. Vector<S32> mPointableId;
  63. Vector<S32> mPointableHandIndex;
  64. Vector<PointableType> mPointableType;
  65. Vector<Point3F> mPointableRawPos;
  66. Vector<Point3I> mPointablePos;
  67. Vector<MatrixF> mPointableRot;
  68. Vector<QuatF> mPointableRotQuat;
  69. Vector<F32> mPointableLength;
  70. Vector<F32> mPointableWidth;
  71. protected:
  72. void copyFromFrameHands(const Leap::HandList& hands, const F32& maxHandAxisRadius);
  73. void copyFromFramePointables(const Leap::PointableList& pointables);
  74. public:
  75. LeapMotionFrame();
  76. virtual ~LeapMotionFrame();
  77. static void initPersistFields();
  78. virtual bool onAdd();
  79. virtual void onRemove();
  80. void clear();
  81. /// Copy a Leap Frame into our data structures
  82. void copyFromFrame(const Leap::Frame& frame, const F32& maxHandAxisRadius);
  83. // Frame
  84. bool isFrameValid() const { return mFrameValid; }
  85. U32 getFrameInternalId() const { return mFrameInternalId; }
  86. S32 getFrameSimTime() const { return mFrameSimTime; }
  87. S32 getFrameRealTime() const { return mFrameRealTime; }
  88. // Hands
  89. U32 getHandCount() const { return mHandCount; }
  90. bool getHandValid(U32 index) const;
  91. S32 getHandId(U32 index) const;
  92. const Point3F& getHandRawPos(U32 index) const;
  93. const Point3I& getHandPos(U32 index) const;
  94. const MatrixF& getHandRot(U32 index) const;
  95. const QuatF& getHandRotQuat(U32 index) const;
  96. const Point2F& getHandRotAxis(U32 index) const;
  97. U32 getHandPointablesCount(U32 index) const;
  98. // Pointables
  99. U32 getPointablesCount() const { return mPointableCount; }
  100. bool getPointableValid(U32 index) const;
  101. S32 getPointableId(U32 index) const;
  102. S32 getPointableHandIndex(U32 index) const;
  103. PointableType getPointableType(U32 index) const;
  104. const Point3F& getPointableRawPos(U32 index) const;
  105. const Point3I& getPointablePos(U32 index) const;
  106. const MatrixF& getPointableRot(U32 index) const;
  107. const QuatF& getPointableRotQuat(U32 index) const;
  108. F32 getPointableLength(U32 index) const;
  109. F32 getPointableWidth(U32 index) const;
  110. DECLARE_CONOBJECT(LeapMotionFrame);
  111. };
  112. typedef LeapMotionFrame::PointableType LeapMotionFramePointableType;
  113. DefineEnumType( LeapMotionFramePointableType );
  114. //-----------------------------------------------------------------------------
  115. inline bool LeapMotionFrame::getHandValid(U32 index) const
  116. {
  117. return (index < mHandCount && mHandValid[index]);
  118. }
  119. inline S32 LeapMotionFrame::getHandId(U32 index) const
  120. {
  121. return (index >= mHandCount) ? -1 : mHandId[index];
  122. }
  123. inline const Point3F& LeapMotionFrame::getHandRawPos(U32 index) const
  124. {
  125. return (index >= mHandCount) ? Point3F::Zero : mHandRawPos[index];
  126. }
  127. inline const Point3I& LeapMotionFrame::getHandPos(U32 index) const
  128. {
  129. return (index >= mHandCount) ? Point3I::Zero : mHandPos[index];
  130. }
  131. inline const MatrixF& LeapMotionFrame::getHandRot(U32 index) const
  132. {
  133. return (index >= mHandCount) ? MatrixF::Identity : mHandRot[index];
  134. }
  135. inline const QuatF& LeapMotionFrame::getHandRotQuat(U32 index) const
  136. {
  137. return (index >= mHandCount) ? QuatF::Identity : mHandRotQuat[index];
  138. }
  139. inline const Point2F& LeapMotionFrame::getHandRotAxis(U32 index) const
  140. {
  141. return (index >= mHandCount) ? Point2F::Zero : mHandRotAxis[index];
  142. }
  143. inline U32 LeapMotionFrame::getHandPointablesCount(U32 index) const
  144. {
  145. return (index >= mHandCount) ? 0 : mHandPointablesCount[index];
  146. }
  147. inline bool LeapMotionFrame::getPointableValid(U32 index) const
  148. {
  149. return (index < mPointableCount && mPointableValid[index]);
  150. }
  151. inline S32 LeapMotionFrame::getPointableId(U32 index) const
  152. {
  153. return (index >= mPointableCount) ? -1 : mPointableId[index];
  154. }
  155. inline S32 LeapMotionFrame::getPointableHandIndex(U32 index) const
  156. {
  157. return (index >= mPointableCount) ? -1 : mPointableHandIndex[index];
  158. }
  159. inline LeapMotionFrame::PointableType LeapMotionFrame::getPointableType(U32 index) const
  160. {
  161. return (index >= mPointableCount) ? PT_UNKNOWN : mPointableType[index];
  162. }
  163. inline const Point3F& LeapMotionFrame::getPointableRawPos(U32 index) const
  164. {
  165. return (index >= mPointableCount) ? Point3F::Zero : mPointableRawPos[index];
  166. }
  167. inline const Point3I& LeapMotionFrame::getPointablePos(U32 index) const
  168. {
  169. return (index >= mPointableCount) ? Point3I::Zero : mPointablePos[index];
  170. }
  171. inline const MatrixF& LeapMotionFrame::getPointableRot(U32 index) const
  172. {
  173. return (index >= mPointableCount) ? MatrixF::Identity : mPointableRot[index];
  174. }
  175. inline const QuatF& LeapMotionFrame::getPointableRotQuat(U32 index) const
  176. {
  177. return (index >= mPointableCount) ? QuatF::Identity : mPointableRotQuat[index];
  178. }
  179. inline F32 LeapMotionFrame::getPointableLength(U32 index) const
  180. {
  181. return (index >= mPointableCount) ? 0.0f : mPointableLength[index];
  182. }
  183. inline F32 LeapMotionFrame::getPointableWidth(U32 index) const
  184. {
  185. return (index >= mPointableCount) ? 0.0f : mPointableWidth[index];
  186. }
  187. #endif // _LEAPMOTIONFRAME_H_