Make bones a linked list
This commit is contained in:
parent
38c11c67fc
commit
bbe54db220
55
src/bone.cpp
55
src/bone.cpp
|
@ -137,29 +137,15 @@ void UltraleapBone::_bind_methods() {
|
||||||
BIND_ENUM_CONSTANT(None);
|
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) {
|
void UltraleapBone::fill_bone_data(Ref<UltraleapBone> bone, Ref<UltraleapBone> previous_bone, LEAP_BONE* leap_bone, BoneType type, LEAP_PALM* palm) {
|
||||||
ul_bone->type = type;
|
bone->type = type;
|
||||||
ul_bone->prev_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&bone->prev_joint);
|
bone->prev_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&leap_bone->prev_joint);
|
||||||
ul_bone->next_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&bone->next_joint);
|
bone->next_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&leap_bone->next_joint);
|
||||||
ul_bone->width = bone->width / 1000;
|
bone->width = leap_bone->width / 1000;
|
||||||
ul_bone->orientation = UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&bone->rotation);
|
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
|
previous_bone->next_bone = bone;
|
||||||
if (previous_bone != NULL) {
|
bone->previous_bone = previous_bone;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If palm is defined, this bone is either a metacarpal or an arm. We still compute relative transform though.
|
// If palm is defined, this bone is either a metacarpal or an arm. We still compute relative transform though.
|
||||||
if (palm != NULL) {
|
if (palm != NULL) {
|
||||||
|
@ -169,17 +155,34 @@ void UltraleapBone::fill_bone_data(Ref<UltraleapBone> ul_bone, LEAP_BONE* bone,
|
||||||
);
|
);
|
||||||
|
|
||||||
Transform3D current_bone_transform = Transform3D(
|
Transform3D current_bone_transform = Transform3D(
|
||||||
Basis(ul_bone->orientation),
|
Basis(bone->orientation),
|
||||||
type == UltraleapBone::BoneType::Arm ? ul_bone->next_joint : ul_bone->prev_joint
|
type == UltraleapBone::BoneType::Arm ? bone->next_joint : bone->prev_joint
|
||||||
);
|
);
|
||||||
|
|
||||||
Transform3D relative_bone_transform = previous_bone_transform.inverse() * current_bone_transform;
|
Transform3D relative_bone_transform = previous_bone_transform.inverse() * current_bone_transform;
|
||||||
ul_bone->rotation = relative_bone_transform.basis.get_quaternion();
|
bone->rotation = relative_bone_transform.basis.get_quaternion();
|
||||||
ul_bone->translation = relative_bone_transform.origin;
|
bone->translation = relative_bone_transform.origin;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UltraleapBone::compute_bone_data(Ref<UltraleapBone> bone) {
|
void UltraleapBone::compute_bone_data(Ref<UltraleapBone> bone) {
|
||||||
bone->center = (bone->prev_joint + bone->next_joint) / 2.0f;
|
bone->center = (bone->prev_joint + bone->next_joint) / 2.0f;
|
||||||
bone->length = (bone->prev_joint - bone->next_joint).length();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -42,6 +42,9 @@ public:
|
||||||
Quaternion orientation;
|
Quaternion orientation;
|
||||||
Quaternion rotation;
|
Quaternion rotation;
|
||||||
|
|
||||||
|
Ref<UltraleapBone> previous_bone;
|
||||||
|
Ref<UltraleapBone> next_bone;
|
||||||
|
|
||||||
// Getters / Setters
|
// Getters / Setters
|
||||||
BoneType get_type() { return type; }
|
BoneType get_type() { return type; }
|
||||||
void set_type(BoneType value) { type = value; }
|
void set_type(BoneType value) { type = value; }
|
||||||
|
@ -70,7 +73,7 @@ public:
|
||||||
Quaternion get_rotation() { return rotation; }
|
Quaternion get_rotation() { return rotation; }
|
||||||
void set_rotation(Quaternion value) { rotation = value; }
|
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);
|
static void compute_bone_data(Ref<UltraleapBone> bone);
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
|
@ -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
|
// We need some extra logic here because the metacarpal doesn't exist in Ultraleap's hand data
|
||||||
if (type == UltraleapDigit::FingerType::Thumb) {
|
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
|
// 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 {
|
else {
|
||||||
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);
|
||||||
UltraleapBone::fill_bone_data(ul_digit->proximal_ref, &digit->proximal, UltraleapBone::BoneType::Proximal, &digit->metacarpal, NULL);
|
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->intermediate_ref, ul_digit->proximal_ref, &digit->intermediate, UltraleapBone::BoneType::Intermediate);
|
||||||
UltraleapBone::fill_bone_data(ul_digit->distal_ref, &digit->distal, UltraleapBone::BoneType::Distal, &digit->intermediate, NULL);
|
UltraleapBone::fill_bone_data(ul_digit->distal_ref, ul_digit->intermediate_ref, &digit->distal, UltraleapBone::BoneType::Distal);
|
||||||
}
|
}
|
|
@ -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_ring(), &hand->ring, UltraleapDigit::FingerType::Ring, &hand->palm);
|
||||||
UltraleapDigit::fill_digit_data(ul_hand->get_pinky(), &hand->pinky, UltraleapDigit::FingerType::Pinky, &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);
|
ul_hand->wrist = Transform3D(Basis(ul_hand->palm_ref->orientation), ul_hand->arm_ref->next_joint);
|
||||||
}
|
}
|
Loading…
Reference in a new issue