Leap Motion - 近端骨与掌骨的角度(左右运动)

Leap Motion - Angle of proximal bone to metacarpal (side to side movement)

我正在尝试获取骨骼之间的角度,例如掌骨和近端骨(左右移动手指的角度,例如食指与拇指尽可能靠近时的角度你可以移动它,然后当你的食指尽可能靠近你的中指时移动它的角度。

我尝试过使用 Vector3.Angle 和骨骼的方向,但这不起作用,因为它包括手指的弯曲,所以如果手握拳,它会给出完全不同的值张开手。

我真正想要的是一种我可以 "normalize" 的方法(我知道归一化不是正确的术语,但这是我能想到的最好的)骨骼的方向,这样即使手指是弯曲,方向向量仍然指向前方而不是向下,但会指向手指的方向(从一侧到另一侧)。

我在下面添加了一张图表来尝试说明我的意思。

在第二张图中,蓝色代表我目前使用骨骼方向得到的结果,绿色是掌骨方向,红色是我想要的(从侧面看)。第一张图显示了我从自上而下的视图中寻找的内容。蓝线是掌骨方向,在这个例子中红线是近端骨方向,绿色污迹代表我正在寻找的角度。

要得到这个值,需要"uncurl"手指方向根据当前掌骨方向。最后有点牵扯;你必须构建一些基础向量才能沿着右轴展开手。希望此示例脚本中的注释能够解释一切。

using Leap;
using Leap.Unity;
using UnityEngine;

public class MeasureIndexSplay : MonoBehaviour {

  // Update is called once per frame
  void Update () {
    var hand = Hands.Get(Chirality.Right);
    if (hand != null) {
      Debug.Log(GetIndexSplayAngle(hand));
    }
  }

  // Some member variables for drawing gizmos.
  private Ray _metacarpalRay;
  private Ray _proximalRay;
  private Ray _uncurledRay;

  /// <summary>
  /// This method returns the angle of the proximal bone of the index finger relative to
  /// its metacarpal, when ignoring any angle due to the curling of the finger.
  /// 
  /// In other words, this method measures the "side-to-side" angle of the finger.
  /// </summary>
  public float GetIndexSplayAngle(Hand h) {
    var index = h.GetIndex();

    // These are the directions we care about.
    var metacarpalDir = index.bones[0].Direction.ToVector3();
    var proximalDir   = index.bones[1].Direction.ToVector3();

    // Let's start with the palm basis vectors.
    var distalAxis = h.DistalAxis(); // finger axis
    var radialAxis = h.RadialAxis(); // thumb axis
    var palmarAxis = h.PalmarAxis(); // palm axis

    // We need a basis whose forward direction is aligned to the metacarpal, so we can
    // uncurl the finger with the proper uncurling axis. The hand's palm basis is close,
    // but not aligned with any particular finger, so let's fix that.
    //
    // We construct a rotation from the palm "finger axis" to align it to the metacarpal
    // direction. Then we apply that same rotation to the other two basis vectors so
    // that we still have a set of orthogonal basis vectors.
    var metacarpalRotation = Quaternion.FromToRotation(distalAxis, metacarpalDir);
    distalAxis = metacarpalRotation * distalAxis;
    radialAxis = metacarpalRotation * radialAxis;
    palmarAxis = metacarpalRotation * palmarAxis;

    // Note: At this point, we don't actually need the distal axis anymore, and we
    // don't need to use the palmar axis, either. They're included above to clarify that
    // we're able to apply the aligning rotation to each axis to maintain a set of
    // orthogonal basis vectors, in case we wanted a complete "metacarpal-aligned basis"
    // for performing other calculations.

    // The radial axis, which has now been rotated a bit to be orthogonal to our
    // metacarpal, is the axis pointing generally towards the thumb. This is our curl
    // axis.
    // If you're unfamiliar with using directions as rotation axes, check out the images
    // here: https://en.wikipedia.org/wiki/Right-hand_rule
    var curlAxis = radialAxis;

    // We want to "uncurl" the proximal bone so that it is in line with the metacarpal,
    // when considered only on the radial plane -- this is the plane defined by the
    // direction approximately towards the thumb, and after the above step, it's also
    // orthogonal to the direction our metacarpal is facing.
    var proximalOnRadialPlane = Vector3.ProjectOnPlane(proximalDir, radialAxis);
    var curlAngle = Vector3.SignedAngle(metacarpalDir, proximalOnRadialPlane,
                                        curlAxis);

    // Construct the uncurling rotation from the axis and angle and apply it to the
    // *original* bone direction. We determined the angle of positive curl, so our
    // rotation flips its sign to rotate the other direction -- to _un_curl.
    var uncurlingRotation = Quaternion.AngleAxis(-curlAngle, curlAxis);
    var uncurledProximal = uncurlingRotation * proximalDir;

    // Upload some data for gizmo drawing (optional).
    _metacarpalRay = new Ray(index.bones[0].PrevJoint.ToVector3(),
                             index.bones[0].Direction.ToVector3());
    _proximalRay = new Ray(index.bones[1].PrevJoint.ToVector3(),
                           index.bones[1].Direction.ToVector3());
    _uncurledRay = new Ray(index.bones[1].PrevJoint.ToVector3(),
                           uncurledProximal);

    // This final direction is now uncurled and can be compared against the direction
    // of the metacarpal under the assumption it was constructed from an open hand.
    return Vector3.Angle(metacarpalDir, uncurledProximal);
  }

  // Draw some gizmos for debugging purposes.
  public void OnDrawGizmos() {
    Gizmos.color = Color.white;
    Gizmos.DrawRay(_metacarpalRay.origin, _metacarpalRay.direction);

    Gizmos.color = Color.blue;
    Gizmos.DrawRay(_proximalRay.origin, _proximalRay.direction);

    Gizmos.color = Color.red;
    Gizmos.DrawRay(_uncurledRay.origin, _uncurledRay.direction);
  }
}

值得一提的是,虽然食指是卷曲的,但跟踪的 Leap 手在这个轴上没有太多的灵活性。