Move image filling code in image class

This commit is contained in:
Rodolphe Houdas 2023-02-01 22:35:30 +00:00
parent 3c3bada62e
commit c049400c65
7 changed files with 47 additions and 56 deletions

View file

@ -154,4 +154,8 @@ void UltraleapDevice::close() {
void UltraleapDevice::on_frame_received(LEAP_TRACKING_EVENT* frame) {
UltraleapFrame::fill_frame_data(last_frame, frame);
emit_signal("frame_received", last_frame);
}
void UltraleapDevice::on_image_received(LEAP_IMAGE* image) {
UltraleapImage::fill_images_data(left_image_ref, right_image_ref, image);
}

View file

@ -67,6 +67,7 @@ public:
void tracking_mode_changed(UltraleapTypes::TrackingMode value);
void on_frame_received(LEAP_TRACKING_EVENT* frame);
void on_image_received(LEAP_IMAGE* image);
protected:
static void _bind_methods();

View file

@ -46,7 +46,6 @@ public:
bool _get(const StringName &p_name, Variant &r_ret) const;
Variant get_last_frame() { return last_frame; }
void set_last_frame(Variant value) { return; }
protected:
static void _bind_methods();

View file

@ -10,4 +10,31 @@ using namespace godot;
void UltraleapImage::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_data"), &UltraleapImage::get_data);
}
void UltraleapImage::fill_images_data(Ref<UltraleapImage> left, Ref<UltraleapImage> right, const LEAP_IMAGE* images) {
uint32_t width = images->properties.width;
uint32_t height = images->properties.height;
if (images->data == nullptr) {
UtilityFunctions::print("Oops null pointer for the image");
return;
}
PackedInt64Array temp_left = PackedInt64Array();
PackedInt64Array temp_right = PackedInt64Array();
uint32_t tot_pixels = height * width;
for (uint32_t i = 0; i < tot_pixels / 8; i++) {
uint64_t* data_left = (uint64_t*)((uint8_t*)images->data + images->offset);
uint64_t* data_right = (uint64_t*)((uint8_t*)(images + 1)->data + (images + 1)->offset);
uint64_t pixel_left = data_left[i];
uint64_t pixel_right = data_right[i];
temp_left.push_back(pixel_left);
temp_right.push_back(pixel_right);
}
left->data = temp_left.to_byte_array();
right->data = temp_right.to_byte_array();
}

View file

@ -9,6 +9,8 @@
#include <godot_cpp/core/binder_common.hpp>
#include <godot_cpp/core/class_db.hpp>
#include <LeapC.h>
using namespace godot;
class UltraleapImage : public Resource {
@ -21,6 +23,8 @@ public:
PackedByteArray get_data() { return data; }
static void fill_images_data(Ref<UltraleapImage> left, Ref<UltraleapImage> right, const LEAP_IMAGE* images);
protected:
static void _bind_methods();
};

View file

@ -186,6 +186,8 @@ void UltraleapHandTracking::serviceMessageLoop() {
handle_device_lost_event(msg.device_event);
break;
case eLeapEventType_DeviceFailure:
UtilityFunctions::print("A device failed!");
handle_device_lost_event(msg.device_event);
break;
case eLeapEventType_Tracking:
// TODO: SECURE THIS WITH A MUTEX OR SOMETHING
@ -225,14 +227,20 @@ void UltraleapHandTracking::serviceMessageLoop() {
case eLeapEventType_ConfigResponse:
break;
case eLeapEventType_Image:
// TODO: SECURE WITH A MUTEX
*last_image_event = *msg.image_event;
// Iterate through the devices and find which device this image
// came from. Then store it in the device.
for (int index = 0; index < devices->size(); index++) {
if (devices->get(index)->id == msg.device_id) {
fill_images_data(devices->get(index)->left_image, devices->get(index)->right_image, msg.image_event->image);
//fill_images_data(devices->get(index)->left_image, devices->get(index)->right_image, msg.image_event->image);
devices->get(index)->on_image_received(last_image_event->image);
}
}
if (devices->size() == 0) {
fill_images_data(left_image, right_image, msg.image_event->image);
UltraleapImage::fill_images_data(left_image, right_image, last_image_event->image);
}
else {
left_image = devices->get(0)->left_image;
@ -289,56 +297,6 @@ void UltraleapHandTracking::set_tracking_mode(UltraleapTypes::TrackingMode value
}
}
// Might be removed, could be replaced by fill_images_data which fills both images in one loop
void UltraleapHandTracking::fill_image_data(UltraleapImage* ul_image, const LEAP_IMAGE* image) {
uint32_t width = image->properties.width;
uint32_t height = image->properties.height;
if (image->data == nullptr) {
UtilityFunctions::print("Oops null pointer for the image");
return;
}
PackedInt64Array temp = PackedInt64Array();
uint32_t tot_pixels = height * width;
for (uint32_t i = 0; i < tot_pixels / 8; i++) {
uint64_t* data = (uint64_t*)((uint8_t*)image->data + image->offset);
uint64_t pixel = data[i];
temp.push_back(pixel);
}
ul_image->data = temp.to_byte_array();
}
void UltraleapHandTracking::fill_images_data(UltraleapImage* left, UltraleapImage* right, const LEAP_IMAGE* images) {
uint32_t width = images->properties.width;
uint32_t height = images->properties.height;
if (images->data == nullptr) {
UtilityFunctions::print("Oops null pointer for the image");
return;
}
PackedInt64Array temp_left = PackedInt64Array();
PackedInt64Array temp_right = PackedInt64Array();
uint32_t tot_pixels = height * width;
for (uint32_t i = 0; i < tot_pixels / 8; i++) {
uint64_t* data_left = (uint64_t*)((uint8_t*)images->data + images->offset);
uint64_t* data_right = (uint64_t*)((uint8_t*)(images + 1)->data + (images + 1)->offset);
uint64_t pixel_left = data_left[i];
uint64_t pixel_right = data_right[i];
temp_left.push_back(pixel_left);
temp_right.push_back(pixel_right);
}
left->data = temp_left.to_byte_array();
right->data = temp_right.to_byte_array();
}
void UltraleapHandTracking::handle_tracking_mode_event(const LEAP_TRACKING_MODE_EVENT* event, uint32_t device_id) {
eLeapTrackingMode current_tracking_mode = event->current_tracking_mode;
UltraleapTypes::TrackingMode tm = tracking_mode;

View file

@ -36,12 +36,10 @@ public:
void set_interpolate(bool value);
LEAP_TRACKING_EVENT* latest_frame = nullptr;
LEAP_IMAGE_EVENT* last_image_event = new LEAP_IMAGE_EVENT;
UltraleapDeviceList* devices = memnew(UltraleapDeviceList);
static void fill_image_data(UltraleapImage* ul_image, const LEAP_IMAGE* image);
static void fill_images_data(UltraleapImage* left, UltraleapImage* right, const LEAP_IMAGE* images);
UltraleapFrame* last_frame = memnew(UltraleapFrame);
Ref<UltraleapFrame> last_frame_ref = Ref<UltraleapFrame>(last_frame);