#include <winsock2.h>
#include <ws2tcpip.h>
#include "BaseProcess.h"
#include "EyeTrack.h"
#include "ECTrainer.h"
#include "KeepAlive.h"
#include "SceneCamera.h"
#include "nkcWinUtils.h"
#include "HPTimer.h"
#include "Worker.h"
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
// コンストラクタ
EyeTrack::EyeTrack(ECTrainer* pEct) : BaseProcess(pEct)
, _GazeV()
, _lastGidx(0)
, _gazePoint(cv::Point2f(-1.F, -1.F))
, _gpCx(MEAN_BUF_SIZE)
, _gpCy(MEAN_BUF_SIZE)
{
_pupilD.l = _pupilD.r = 0;
_wtimer = ::CreateWaitableTimer(NULL, TRUE, NULL);
}
// デストラクタ
EyeTrack::~EyeTrack() {
CloseHandle(_wtimer);
}
// メインループ
bool EyeTrack::Routine() {
#if defined(EYEDEVICE_GLASS2)
// 最初のKeepAlive送信待ち
if (!Ect()->PKeepAlive()->IsSent()) {
Sleep(1);
return true;
}
// データ受信
int slen = Ect()->PKeepAlive()->GetSocketAddrLen();
char buf[KeepAlive::SOCKET_BUF_LEN];
memset(buf, '\0', KeepAlive::SOCKET_BUF_LEN);
if (recvfrom(Ect()->PKeepAlive()->GetSocket(), buf, KeepAlive::SOCKET_BUF_LEN,
0, Ect()->PKeepAlive()->GetSocketAddr(), &slen) == SOCKET_ERROR) {
std::cerr << "Data Receive Error" << std::endl;
Sleep(10);
return true;
}
//std::cout << buf << std::endl;
// 受信データ解析
bool rv = false;
GazeData gd(buf);
if (gd.gidx > _lastGidx) {
if (_gazePoint.x > 0 && _gazePoint.y > 0) {
_gpCx.Push(_gazePoint.x * Ect()->PSceneCamera()->GetSize().width);
_gpCy.Push(_gazePoint.y * Ect()->PSceneCamera()->GetSize().height);
_GazeV.Put(cv::Point2f(_gpCx.Mean(), _gpCy.Mean()));
} else {
_GazeV.Put(_gazePoint);
}
_gazePoint = cv::Point2f(-1.F, -1.F);
_lastGidx = gd.gidx;
rv = true;
}
if (gd.isGP && gd.s == 0) {
_gazePoint = gd.gp;
}
if (gd.isPDR && gd.s == 0) _pupilD.r = gd.pdr;
return rv;
#elif defined(EYEDEVICE_NONE)
_GazeV.Put(cv::Point2f(-1.F, -1.F));
Ect()->PWorker()->SetTrigger();
::Sleep(10);
return true;
#endif
}
// FPS表示
void EyeTrack::FPS(double fps) {
nkc::wut::DebugPrintf(_T("[EyeTrack] %.1f fps\n"), fps);
}