#include <winsock2.h>
#include <ws2tcpip.h>
#include <tchar.h>
#include "EyeTrack.h"
#include "ECTrainer.h"
#include "KeepAlive.h"
#include "SceneCamera.h"
#include "MeanBuffer.h"
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
// コンストラクタ
EyeTrack::EyeTrack(ECTrainer* pEct)
: BaseProcess(pEct)
, _GazeV(ECTrainer::RINGBUFSIZE)
{
}
// メインループ
bool EyeTrack::MainLoop() {
int slen = _pEct->PKeepAlive()->GetSocketAddrLen();
char buf[KeepAlive::SOCKET_BUF_LEN];
int lastGidx = 0;
cv::Point2f gazePoint;
cv::Size sceneSize = _pEct->PSceneCamera()->GetSize();
//std::cout << "sceneSize: " << sceneSize.width << "," << sceneSize.height << std::endl;
MeanBuffer gpCx(MEAN_BUF_SIZE), gpCy(MEAN_BUF_SIZE);
while (!_pEct->PKeepAlive()->IsSent()) Sleep(1); // 最初のKeepAlive送信待ち
while (_pEct->IsAppRun()) {
// データ受信
memset(buf, '\0', KeepAlive::SOCKET_BUF_LEN);
if (recvfrom(_pEct->PKeepAlive()->GetSocket(), buf, KeepAlive::SOCKET_BUF_LEN,
0, _pEct->PKeepAlive()->GetSocketAddr(), &slen) == SOCKET_ERROR)
{
std::cerr << "Data Receive Error" << std::endl;
Sleep(10);
continue;
}
//std::cout << buf << std::endl;
// 受信データ解析
GazeData gd(buf);
if (gd.gidx > lastGidx) {
if (gazePoint.x > 0 && gazePoint.y > 0) {
gpCx.Push(gazePoint.x * sceneSize.width);
gpCy.Push(gazePoint.y * sceneSize.height);
_GazeV.Put(cv::Point2f(gpCx.Mean(), gpCy.Mean()));
}
else {
_GazeV.Put(gazePoint);
}
gazePoint = cv::Point2f(-1.F, -1.F);
lastGidx = gd.gidx;
}
if (gd.isGP && gd.s == 0) {
gazePoint = gd.gp;
}
if (gd.isPDR && gd.s == 0) _pupilD.r = gd.pdr;
}
return true;
}