Palm.cs 4.3 KB

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