From 01720c57d9a4b30582e0f2f47ab9879dfdca9d3a Mon Sep 17 00:00:00 2001 From: Wim Vriend Date: Tue, 26 Feb 2013 16:42:25 +0000 Subject: Updating before branching. Last revision is v170 beta1. git-svn-id: svn+ssh://svn.code.sf.net/p/facetracknoir/code@271 19e81ba0-9b1a-49c3-bd6c-561e1906d5fb --- FTNoIR_Tracker_HT/ftnoir_tracker_ht.cpp | 246 +++++++++++++----------------- FTNoIR_Tracker_HT/ftnoir_tracker_ht.h | 50 +++--- FTNoIR_Tracker_HT/ftnoir_tracker_ht_dll.h | 7 +- FTNoIR_Tracker_HT/stdafx.h | 29 ---- 4 files changed, 133 insertions(+), 199 deletions(-) (limited to 'FTNoIR_Tracker_HT') diff --git a/FTNoIR_Tracker_HT/ftnoir_tracker_ht.cpp b/FTNoIR_Tracker_HT/ftnoir_tracker_ht.cpp index 6adad5db..336ede2d 100644 --- a/FTNoIR_Tracker_HT/ftnoir_tracker_ht.cpp +++ b/FTNoIR_Tracker_HT/ftnoir_tracker_ht.cpp @@ -1,19 +1,24 @@ #include "stdafx.h" -#include "..\ftnoir_tracker_base\ftnoir_tracker_base.h" +#include "../ftnoir_tracker_base/ftnoir_tracker_base.h" #include "headtracker-ftnoir.h" #include "ftnoir_tracker_ht.h" #include "ftnoir_tracker_ht_dll.h" -#include "UI_TRACKERCONTROLS.h" - -static TCHAR shmName[] = TEXT(HT_SHM_NAME); -static TCHAR mutexName[] = TEXT(HT_MUTEX_NAME); +#include "ui_trackercontrols.h" +#include "../facetracknoir/global-settings.h" #define WIDGET_WIDTH 250 -#define WIDGET_HEIGHT 170 +#define WIDGET_HEIGHT 188 + +#if defined(_WIN32) || defined(__WIN32) +#include +#else +#include +#endif // delicious copypasta static QList get_camera_names(void) { - QList ret; + QList ret; +#if defined(_WIN32) || defined(__WIN32) // Create the System Device Enumerator. HRESULT hr; ICreateDevEnum *pSysDevEnum = NULL; @@ -59,7 +64,18 @@ static QList get_camera_names(void) { pEnumCat->Release(); } pSysDevEnum->Release(); - return ret; +#else + for (int i = 0; i < 16; i++) { + char buf[128]; + sprintf(buf, "/dev/video%d", i); + if (access(buf, R_OK | W_OK) == 0) { + ret.append(buf); + } else { + break; + } + } +#endif + return ret; } typedef struct { @@ -68,6 +84,7 @@ typedef struct { } resolution_tuple; static resolution_tuple resolution_choices[] = { + { 0, 0 }, { 640, 480 }, { 320, 240 }, { 320, 200 }, @@ -94,14 +111,14 @@ static void load_settings(ht_config_t* config, Tracker* tracker) config->force_fps = iniFile.value("fps", 0).toInt(); config->camera_index = iniFile.value("camera-index", -1).toInt(); config->ransac_num_iters = 100; - config->ransac_max_reprojection_error = 5.5f; - config->ransac_max_inlier_error = 5.5f; - config->ransac_max_mean_error = 4.0f; - config->ransac_abs_max_mean_error = 6.0f; + config->ransac_max_reprojection_error = 6.5f; + config->ransac_max_inlier_error = 6.5f; + config->ransac_max_mean_error = 4.0f; + config->ransac_abs_max_mean_error = 7.0f; config->debug = 0; config->ransac_min_features = 0.75f; - int res = iniFile.value("resolution", 0).toInt(); - if (res < 0 || res >= sizeof(*resolution_choices) / sizeof(resolution_tuple)) + int res = iniFile.value("resolution", 0).toInt(); + if (res < 0 || res >= (int)(sizeof(*resolution_choices) / sizeof(resolution_tuple))) res = 0; resolution_tuple r = resolution_choices[res]; config->force_width = r.width; @@ -118,152 +135,95 @@ static void load_settings(ht_config_t* config, Tracker* tracker) iniFile.endGroup(); } -Tracker::Tracker() +Tracker::Tracker() : lck_shm(HT_SHM_NAME, HT_MUTEX_NAME, sizeof(ht_shm_t)), fresh(false) { videoWidget = NULL; layout = NULL; enableRX = enableRY = enableRZ = enableTX = enableTY = enableTZ = true; - hMutex = CreateMutex(NULL, false, mutexName); - hMapFile = CreateFileMapping( - INVALID_HANDLE_VALUE, - NULL, - PAGE_READWRITE, - 0, - sizeof(ht_shm_t), - shmName); - shm = (ht_shm_t*) MapViewOfFile(hMapFile, - FILE_MAP_READ | FILE_MAP_WRITE, - 0, - 0, - sizeof(ht_shm_t)); - paused = false; + shm = (ht_shm_t*) lck_shm.mem; + shm->terminate = 0; load_settings(&shm->config, this); + shm->result.filled = false; } Tracker::~Tracker() { + subprocess.kill(); + if (shm) + shm->terminate = true; if (layout) delete layout; if (videoWidget) delete videoWidget; - UnmapViewOfFile(shm); - CloseHandle(hMapFile); - CloseHandle(hMutex); } -void Tracker::Initialize(QFrame *videoframe) +void Tracker::StartTracker(QFrame* videoframe) { - videoframe->setAttribute(Qt::WA_NativeWindow); - videoframe->show(); - videoWidget = new VideoWidget(videoframe); - QHBoxLayout* layout = new QHBoxLayout(); - layout->setContentsMargins(0, 0, 0, 0); - layout->addWidget(videoWidget); - if (videoframe->layout()) - delete videoframe->layout(); - videoframe->setLayout(layout); - videoWidget->resize(WIDGET_WIDTH, WIDGET_HEIGHT); - videoWidget->show(); - this->layout = layout; + videoframe->setAttribute(Qt::WA_NativeWindow); + videoframe->show(); + videoWidget = new VideoWidget(videoframe); + QHBoxLayout* layout = new QHBoxLayout(); + layout->setContentsMargins(0, 0, 0, 0); + layout->addWidget(videoWidget); + if (videoframe->layout()) + delete videoframe->layout(); + videoframe->setLayout(layout); + videoWidget->resize(WIDGET_WIDTH, WIDGET_HEIGHT); + videoWidget->show(); + this->layout = layout; + load_settings(&shm->config, this); + shm->frame.channels = shm->frame.width = shm->frame.height = 0; + shm->pause = shm->terminate = shm->running = false; + shm->timer = 0; + subprocess.setWorkingDirectory(QCoreApplication::applicationDirPath() + "/tracker-ht"); +#if defined(_WIN32) || defined(__WIN32) + subprocess.start("\"" + QCoreApplication::applicationDirPath() + "/tracker-ht/headtracker-ftnoir" + "\""); +#else + subprocess.start(QCoreApplication::applicationDirPath() + "/tracker-ht/headtracker-ftnoir"); +#endif + connect(&timer, SIGNAL(timeout()), this, SLOT(paint_widget())); + timer.start(15); } -void Tracker::StartTracker(HWND parent) -{ - if (paused) - { - shm->pause = false; - } - else - { - load_settings(&shm->config, this); - shm->frame.channels = shm->frame.width = shm->frame.height = 0; - shm->pause = shm->terminate = shm->running = false; - shm->timer = 0; - subprocess.setWorkingDirectory(QCoreApplication::applicationDirPath() + "/tracker-ht"); - subprocess.start("\"" + QCoreApplication::applicationDirPath() + "/tracker-ht/headtracker-ftnoir.exe" + "\""); - } -} - -void Tracker::StopTracker(bool exit) -{ - if (exit) - { - shm->terminate = true; - subprocess.kill(); - } - else - { - shm->pause = true; - } +void Tracker::paint_widget() { + if (fresh) { + fresh = false; + videoWidget->update(); + } } bool Tracker::GiveHeadPoseData(THeadPoseData* data) { bool ret = false; - if (WaitForSingleObject(hMutex, 100) == WAIT_OBJECT_0) - { - shm->timer = 0; - if (WaitForSingleObject(videoWidget->hMutex, 10) == WAIT_OBJECT_0) - { - memcpy(&videoWidget->videoFrame, &shm->frame, sizeof(ht_video_t)); - ReleaseMutex(videoWidget->hMutex); - } - if (shm->result.filled) { - if (enableRX) - data->yaw = shm->result.rotx * 57.295781; - if (enableRY) - data->pitch = shm->result.roty * 57.295781; - if (enableRZ) - data->roll = shm->result.rotz * 57.295781; - if (enableTX) - data->x = shm->result.tx; - if (enableTY) - data->y = shm->result.ty; - if (enableTZ) - data->z = shm->result.tz; - ret = true; - } - ReleaseMutex(hMutex); - } + lck_shm.lock(); + shm->timer = 0; + if (shm->frame.width > 0) + { + videoWidget->updateImage(shm->frame.frame, shm->frame.width, shm->frame.height); + //memcpy(foo, shm->frame.frame, shm->frame.width * shm->frame.height * 3); + fresh = true; + } + if (shm->result.filled) { + if (enableRX) + data->yaw = shm->result.rotx; + if (enableRY) + data->pitch = shm->result.roty; + if (enableRZ) + data->roll = shm->result.rotz; + if (enableTX) + data->x = shm->result.tx; + if (enableTY) + data->y = shm->result.ty; + if (enableTZ) + data->z = shm->result.tz; + ret = true; + } + lck_shm.unlock(); return ret; } -VideoWidget::VideoWidget(QWidget* parent) : QWidget(parent) -{ - hMutex = CreateMutex(NULL, false, NULL); - videoFrame.channels = videoFrame.height = videoFrame.width = 0; - connect(&timer, SIGNAL(timeout()), this, SLOT(update())); - timer.start(35); -} - -VideoWidget::~VideoWidget() -{ - CloseHandle(hMutex); -} - -void VideoWidget::paintEvent(QPaintEvent *e) -{ - uchar* data = NULL; - if (WaitForSingleObject(hMutex, 10) == WAIT_OBJECT_0) - { - if (videoFrame.width > 0) - { - data = new uchar[videoFrame.width * videoFrame.height * 3]; - memcpy(data, videoFrame.frame, videoFrame.width * videoFrame.height * 3); - } - ReleaseMutex(hMutex); - } - if (data) - { - QImage image((uchar*) data, videoFrame.width, videoFrame.height, QImage::Format_RGB888); - QPainter painter(this); - painter.drawPixmap(e->rect(), QPixmap::fromImage(image.rgbSwapped()).scaled(WIDGET_WIDTH, WIDGET_HEIGHT, Qt::KeepAspectRatioByExpanding), e->rect()); - delete[] data; - } -} - //----------------------------------------------------------------------------- void TrackerDll::getFullName(QString *strToBeFilled) { @@ -282,23 +242,23 @@ void TrackerDll::getDescription(QString *strToBeFilled) void TrackerDll::getIcon(QIcon *icon) { - *icon = QIcon(":/images/HT.ico"); + *icon = QIcon(":/images/ht.png"); } //----------------------------------------------------------------------------- -#pragma comment(linker, "/export:GetTrackerDll=_GetTrackerDll@0") +//#pragma comment(linker, "/export:GetTrackerDll=_GetTrackerDll@0") -FTNOIR_TRACKER_BASE_EXPORT ITrackerDllPtr __stdcall GetTrackerDll() +extern "C" FTNOIR_TRACKER_BASE_EXPORT Metadata* CALLING_CONVENTION GetMetadata() { return new TrackerDll; } -#pragma comment(linker, "/export:GetTracker=_GetTracker@0") +//#pragma comment(linker, "/export:GetTracker=_GetTracker@0") -FTNOIR_TRACKER_BASE_EXPORT ITrackerPtr __stdcall GetTracker() +extern "C" FTNOIR_TRACKER_BASE_EXPORT void* CALLING_CONVENTION GetConstructor() { - return new Tracker; + return (ITracker*) new Tracker; } //////////////////////////////////////////////////////////////////////////////// @@ -308,11 +268,11 @@ FTNOIR_TRACKER_BASE_EXPORT ITrackerPtr __stdcall GetTracker() // GetTrackerDialog - Undecorated name, which can be easily used with GetProcAddress // Win32 API function. // _GetTrackerDialog@0 - Common name decoration for __stdcall functions in C language. -#pragma comment(linker, "/export:GetTrackerDialog=_GetTrackerDialog@0") +//#pragma comment(linker, "/export:GetTrackerDialog=_GetTrackerDialog@0") -FTNOIR_TRACKER_BASE_EXPORT ITrackerDialogPtr __stdcall GetTrackerDialog( ) +extern "C" FTNOIR_TRACKER_BASE_EXPORT void* CALLING_CONVENTION GetDialog( ) { - return new TrackerControls; + return (ITrackerDialog*) new TrackerControls; } TrackerControls::TrackerControls() @@ -382,7 +342,7 @@ void TrackerControls::loadSettings() ui.tx->setCheckState(iniFile.value("enable-tx", true).toBool() ? Qt::Checked : Qt::Unchecked); ui.ty->setCheckState(iniFile.value("enable-ty", true).toBool() ? Qt::Checked : Qt::Unchecked); ui.tz->setCheckState(iniFile.value("enable-tz", true).toBool() ? Qt::Checked : Qt::Unchecked); - ui.resolution->setCurrentIndex(iniFile.value("resolution", 0).toInt()); + ui.resolution->setCurrentIndex(iniFile.value("resolution", 0).toInt()); iniFile.endGroup(); settingsDirty = false; } @@ -459,4 +419,4 @@ void TrackerControls::doCancel() else { this->close(); } -} \ No newline at end of file +} diff --git a/FTNoIR_Tracker_HT/ftnoir_tracker_ht.h b/FTNoIR_Tracker_HT/ftnoir_tracker_ht.h index 30d62f32..1a449dca 100644 --- a/FTNoIR_Tracker_HT/ftnoir_tracker_ht.h +++ b/FTNoIR_Tracker_HT/ftnoir_tracker_ht.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2013 Stanisław Halik +/* Copyright (c) 2013 Stanislaw Halik * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -9,41 +9,43 @@ #define FTNOIR_TRACKER_HT_H #include "stdafx.h" -#include "..\ftnoir_tracker_base\ftnoir_tracker_base.h" +#include "../ftnoir_tracker_base/ftnoir_tracker_base.h" #include "headtracker-ftnoir.h" -#include "ui_TrackerControls.h" +#include "ui_trackercontrols.h" +#include "video_widget.h" +#include "../compat/compat.h" +#include +#include -class VideoWidget : public QWidget -{ - Q_OBJECT -public: - VideoWidget(QWidget* parent); - ~VideoWidget(); - ht_video_t videoFrame; - HANDLE hMutex; -protected: - void paintEvent(QPaintEvent* e); -private: - QTimer timer; -}; - -class Tracker : public ITracker +class Tracker : public QObject, public ITracker { + Q_OBJECT public: Tracker(); ~Tracker(); - void Initialize(QFrame *videoframe); - void StartTracker(HWND parent_window); - void StopTracker(bool exit); - bool GiveHeadPoseData(THeadPoseData *data); + void StartTracker(QFrame* frame); + bool GiveHeadPoseData(THeadPoseData *data); bool enableTX, enableTY, enableTZ, enableRX, enableRY, enableRZ; ht_shm_t* shm; + bool NeedsTimeToFinish() { + return true; + } + void WaitForExit() { + if (shm) { + shm->terminate = true; + subprocess.waitForFinished(5000); + } + subprocess.kill(); + } private: - HANDLE hMutex, hMapFile; - bool paused; + QTimer timer; + PortableLockedShm lck_shm; QProcess subprocess; VideoWidget* videoWidget; QHBoxLayout* layout; + volatile bool fresh; +private slots: + void paint_widget(); }; // Widget that has controls for FTNoIR protocol client-settings. diff --git a/FTNoIR_Tracker_HT/ftnoir_tracker_ht_dll.h b/FTNoIR_Tracker_HT/ftnoir_tracker_ht_dll.h index 01b4cfa0..f3bfd381 100644 --- a/FTNoIR_Tracker_HT/ftnoir_tracker_ht_dll.h +++ b/FTNoIR_Tracker_HT/ftnoir_tracker_ht_dll.h @@ -5,10 +5,11 @@ * copyright notice and this permission notice appear in all copies. */ -#include "..\ftnoir_tracker_base\ftnoir_tracker_base.h" +#include "../ftnoir_tracker_base/ftnoir_tracker_base.h" +#include "../facetracknoir/global-settings.h" //----------------------------------------------------------------------------- -class TrackerDll : public ITrackerDll +class TrackerDll : public Metadata { // ITrackerDll interface void Initialize() {} @@ -16,4 +17,4 @@ class TrackerDll : public ITrackerDll void getShortName(QString *strToBeFilled); void getDescription(QString *strToBeFilled); void getIcon(QIcon *icon); -}; \ No newline at end of file +}; diff --git a/FTNoIR_Tracker_HT/stdafx.h b/FTNoIR_Tracker_HT/stdafx.h index 9cbe1001..0e532c9f 100644 --- a/FTNoIR_Tracker_HT/stdafx.h +++ b/FTNoIR_Tracker_HT/stdafx.h @@ -1,31 +1,3 @@ -// stdafx.h : include file for standard system include files, -// or project specific include files that are used frequently, but -// are changed infrequently -// - -#pragma once - -// Modify the following defines if you have to target a platform prior to the ones specified below. -// Refer to MSDN for the latest info on corresponding values for different platforms. -#ifndef WINVER // Allow use of features specific to Windows XP or later. -#define WINVER 0x0501 // Change this to the appropriate value to target other versions of Windows. -#endif - -#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. -#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. -#endif - -#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later. -#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later. -#endif - -#ifndef _WIN32_IE // Allow use of features specific to IE 6.0 or later. -#define _WIN32_IE 0x0600 // Change this to the appropriate value to target other versions of IE. -#endif - -#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers -// Windows Header Files: -#include #include #include #include @@ -40,4 +12,3 @@ #include #include #include -#include \ No newline at end of file -- cgit v1.2.3