Make bones a linked list

This commit is contained in:
rodolpheh 2023-11-21 08:38:58 +00:00
parent 38c11c67fc
commit bbe54db220
4 changed files with 40 additions and 34 deletions

View File

@ -137,29 +137,15 @@ void UltraleapBone::_bind_methods() {
BIND_ENUM_CONSTANT(None);
}
void UltraleapBone::fill_bone_data(Ref<UltraleapBone> ul_bone, LEAP_BONE* bone, BoneType type, LEAP_BONE* previous_bone, LEAP_PALM* palm) {
ul_bone->type = type;
ul_bone->prev_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&bone->prev_joint);
ul_bone->next_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&bone->next_joint);
ul_bone->width = bone->width / 1000;
ul_bone->orientation = UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&bone->rotation);
void UltraleapBone::fill_bone_data(Ref<UltraleapBone> bone, Ref<UltraleapBone> previous_bone, LEAP_BONE* leap_bone, BoneType type, LEAP_PALM* palm) {
bone->type = type;
bone->prev_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&leap_bone->prev_joint);
bone->next_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&leap_bone->next_joint);
bone->width = leap_bone->width / 1000;
bone->orientation = UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&leap_bone->rotation);
// If previous_bone is defined, we compute the transform from the previous bone to the current bone
if (previous_bone != NULL) {
Transform3D previous_bone_transform = Transform3D(
Basis(UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&previous_bone->rotation)),
UltraleapTypes::ultraleap_vector3_to_godot_vector3(&previous_bone->prev_joint)
);
Transform3D current_bone_transform = Transform3D(
Basis(ul_bone->orientation),
ul_bone->prev_joint
);
Transform3D relative_bone_transform = previous_bone_transform.inverse() * current_bone_transform;
ul_bone->rotation = relative_bone_transform.basis.get_quaternion();
ul_bone->translation = relative_bone_transform.origin;
}
previous_bone->next_bone = bone;
bone->previous_bone = previous_bone;
// If palm is defined, this bone is either a metacarpal or an arm. We still compute relative transform though.
if (palm != NULL) {
@ -169,17 +155,34 @@ void UltraleapBone::fill_bone_data(Ref<UltraleapBone> ul_bone, LEAP_BONE* bone,
);
Transform3D current_bone_transform = Transform3D(
Basis(ul_bone->orientation),
type == UltraleapBone::BoneType::Arm ? ul_bone->next_joint : ul_bone->prev_joint
Basis(bone->orientation),
type == UltraleapBone::BoneType::Arm ? bone->next_joint : bone->prev_joint
);
Transform3D relative_bone_transform = previous_bone_transform.inverse() * current_bone_transform;
ul_bone->rotation = relative_bone_transform.basis.get_quaternion();
ul_bone->translation = relative_bone_transform.origin;
bone->rotation = relative_bone_transform.basis.get_quaternion();
bone->translation = relative_bone_transform.origin;
}
}
void UltraleapBone::compute_bone_data(Ref<UltraleapBone> bone) {
bone->center = (bone->prev_joint + bone->next_joint) / 2.0f;
bone->length = (bone->prev_joint - bone->next_joint).length();
// If previous_bone is defined, we compute the transform from the previous bone to the current bone
if (bone->previous_bone != NULL) {
Transform3D previous_bone_transform = Transform3D(
Basis(bone->previous_bone->orientation),
bone->previous_bone->prev_joint
);
Transform3D current_bone_transform = Transform3D(
Basis(bone->orientation),
bone->prev_joint
);
Transform3D relative_bone_transform = previous_bone_transform.inverse() * current_bone_transform;
bone->rotation = relative_bone_transform.basis.get_quaternion();
bone->translation = relative_bone_transform.origin;
}
}

View File

@ -42,6 +42,9 @@ public:
Quaternion orientation;
Quaternion rotation;
Ref<UltraleapBone> previous_bone;
Ref<UltraleapBone> next_bone;
// Getters / Setters
BoneType get_type() { return type; }
void set_type(BoneType value) { type = value; }
@ -70,7 +73,7 @@ public:
Quaternion get_rotation() { return rotation; }
void set_rotation(Quaternion value) { rotation = value; }
static void fill_bone_data(Ref<UltraleapBone> ul_bone, LEAP_BONE* bone, BoneType type, LEAP_BONE* previous_bone, LEAP_PALM* palm);
static void fill_bone_data(Ref<UltraleapBone> bone, Ref<UltraleapBone> previous_bone, LEAP_BONE* leap_bone, BoneType type, LEAP_PALM* palm = NULL);
static void compute_bone_data(Ref<UltraleapBone> bone);
protected:
static void _bind_methods();

View File

@ -139,16 +139,16 @@ void UltraleapDigit::fill_digit_data(Ref<UltraleapDigit> ul_digit, LEAP_DIGIT* d
// We need some extra logic here because the metacarpal doesn't exist in Ultraleap's hand data
if (type == UltraleapDigit::FingerType::Thumb) {
UltraleapBone::fill_bone_data(ul_digit->proximal_ref, &digit->proximal, UltraleapBone::BoneType::Proximal, NULL, palm);
UltraleapBone::fill_bone_data(ul_digit->proximal_ref, NULL, &digit->proximal, UltraleapBone::BoneType::Proximal, palm);
// Still filling it out but shouldn't be used
UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal, NULL, palm);
UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, NULL, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal, palm);
}
else {
UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal, NULL, palm);
UltraleapBone::fill_bone_data(ul_digit->proximal_ref, &digit->proximal, UltraleapBone::BoneType::Proximal, &digit->metacarpal, NULL);
UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, NULL, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal, palm);
UltraleapBone::fill_bone_data(ul_digit->proximal_ref, ul_digit->metacarpal_ref, &digit->proximal, UltraleapBone::BoneType::Proximal);
}
UltraleapBone::fill_bone_data(ul_digit->intermediate_ref, &digit->intermediate, UltraleapBone::BoneType::Intermediate, &digit->proximal, NULL);
UltraleapBone::fill_bone_data(ul_digit->distal_ref, &digit->distal, UltraleapBone::BoneType::Distal, &digit->intermediate, NULL);
UltraleapBone::fill_bone_data(ul_digit->intermediate_ref, ul_digit->proximal_ref, &digit->intermediate, UltraleapBone::BoneType::Intermediate);
UltraleapBone::fill_bone_data(ul_digit->distal_ref, ul_digit->intermediate_ref, &digit->distal, UltraleapBone::BoneType::Distal);
}

View File

@ -266,7 +266,7 @@ void UltraleapHand::fill_hand_data(Ref<UltraleapHand> ul_hand, LEAP_HAND* hand)
UltraleapDigit::fill_digit_data(ul_hand->get_ring(), &hand->ring, UltraleapDigit::FingerType::Ring, &hand->palm);
UltraleapDigit::fill_digit_data(ul_hand->get_pinky(), &hand->pinky, UltraleapDigit::FingerType::Pinky, &hand->palm);
UltraleapBone::fill_bone_data(ul_hand->arm_ref, &hand->arm, UltraleapBone::BoneType::Arm, NULL, &hand->palm);
UltraleapBone::fill_bone_data(ul_hand->arm_ref, NULL, &hand->arm, UltraleapBone::BoneType::Arm, &hand->palm);
ul_hand->wrist = Transform3D(Basis(ul_hand->palm_ref->orientation), ul_hand->arm_ref->next_joint);
}