Palm.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using BBIWARG.Recognition.HandRecognition;
  2. using BBIWARG.Recognition.Tracking;
  3. using BBIWARG.Utility;
  4. using System;
  5. namespace BBIWARG.Recognition.PalmRecognition
  6. {
  7. /// <summary>
  8. /// The handedness of the palm.
  9. /// </summary>
  10. public enum HandSide
  11. {
  12. Undefined = 0,
  13. Right = 1,
  14. Left = 2
  15. }
  16. /// <summary>
  17. /// Represents a palm (each hand with one finger (thumb) has palm)
  18. /// </summary>
  19. public class Palm : TrackableObject
  20. {
  21. /// <summary>
  22. /// the position of the fingers lower (bottom right for left hand, bottom left for right hand)
  23. /// </summary>
  24. public Vector2D FingersLower { get; private set; }
  25. /// <summary>
  26. /// the position of the upper fingers (top right for left hand, top left for left hand)
  27. /// </summary>
  28. public Vector2D FingersUpper { get; private set; }
  29. /// <summary>
  30. /// the hand belonging to this palm
  31. /// </summary>
  32. public Hand Hand { get; private set; }
  33. /// <summary>
  34. /// the handedness
  35. /// </summary>
  36. public HandSide HandSide { get; private set; }
  37. /// <summary>
  38. /// the quadrangle of the four palm points
  39. /// </summary>
  40. public Quadrangle Quad { get; private set; }
  41. /// <summary>
  42. /// the thumb's convexity defect
  43. /// </summary>
  44. public ConvexityDefect ThumbDefect { get; private set; }
  45. /// <summary>
  46. /// the position of the wrist lower (bottom left for left hand, bottom right for right hand)
  47. /// </summary>
  48. public Vector2D WristLower { get; private set; }
  49. /// <summary>
  50. /// the position of the upper wrist (top left corner for left hand, top right for right hand)
  51. /// </summary>
  52. public Vector2D WristUpper { get; private set; }
  53. /// <summary>
  54. /// Initializes a new instance of the Palm class.
  55. /// </summary>
  56. /// <param name="hand">The hand.</param>
  57. /// <param name="thumbDefect">The thumb defect.</param>
  58. /// <param name="handSide">The handedness.</param>
  59. /// <param name="wristUpper">The wrist upper position.</param>
  60. /// <param name="fingersUpper">The fingers upper position.</param>
  61. /// <param name="fingersLower">The fingers lower position.</param>
  62. /// <param name="wristLower">The wrist lower position.</param>
  63. public Palm(Hand hand, ConvexityDefect thumbDefect, HandSide handSide, Vector2D wristUpper, Vector2D fingersUpper, Vector2D fingersLower, Vector2D wristLower)
  64. {
  65. Hand = hand;
  66. ThumbDefect = thumbDefect;
  67. HandSide = handSide;
  68. WristUpper = wristUpper;
  69. FingersUpper = fingersUpper;
  70. FingersLower = fingersLower;
  71. WristLower = wristLower;
  72. hand.Palm = this;
  73. createQuad();
  74. }
  75. /// <summary>
  76. /// Gets the relative position [0-1;0-1] from an absolute position.
  77. /// </summary>
  78. /// <param name="absolutePosition">the absolute position</param>
  79. /// <returns>the relative position</returns>
  80. public Vector2D getRelativePosition(Vector2D absolutePosition)
  81. {
  82. Vector2D relativePosition = Quad.getRelativePosition(absolutePosition);
  83. float x = Math.Max(0, Math.Min(1, relativePosition.X));
  84. float y = Math.Max(0, Math.Min(1, relativePosition.Y));
  85. return new Vector2D(x, y);
  86. }
  87. /// <summary>
  88. /// Checks if the position is inside the palm (with a tolerance)
  89. /// </summary>
  90. /// <param name="position">the absolute position</param>
  91. /// <returns>whether the position is inside the palm</returns>
  92. public bool isInside(Vector2D position)
  93. {
  94. return Quad.isInside(position, Parameters.PalmInsideTolerance);
  95. }
  96. /// <summary>
  97. /// Creates the palm quadrangle
  98. /// </summary>
  99. private void createQuad()
  100. {
  101. if (HandSide == HandSide.Left)
  102. Quad = new Quadrangle(WristUpper, FingersUpper, FingersLower, WristLower);
  103. else
  104. Quad = new Quadrangle(FingersUpper, WristUpper, WristLower, FingersLower);
  105. }
  106. }
  107. }