Newer
Older
PrismSoftware / ECTrainer2 / EyeTrack.cpp
#include <winsock2.h>
#include <ws2tcpip.h>
#include "EyeTrack.h"
#include "ECTrainer.h"
#include "KeepAlive.h"
#include "SceneCamera.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)
{
}

// メインループ
bool EyeTrack::Routine() {

#if defined(EYEDEVICE_GLASS2)
	// 最初のKeepAlive送信待ち
	if (!_pEct->PKeepAlive()->IsSent()) {
		Sleep(1);
		return true;
	}

	// データ受信
	int slen = _pEct->PKeepAlive()->GetSocketAddrLen();
	char buf[KeepAlive::SOCKET_BUF_LEN];
	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);
		return true;
}
	//std::cout << buf << std::endl;

	// 受信データ解析
	GazeData gd(buf);
	if (gd.gidx > _lastGidx) {
		if (_gazePoint.x > 0 && _gazePoint.y > 0) {
			_gpCx.Push(_gazePoint.x * _pEct->PSceneCamera()->GetSize().width);
			_gpCy.Push(_gazePoint.y * _pEct->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;
	}
	if (gd.isGP && gd.s == 0) {
		_gazePoint = gd.gp;
	}
	if (gd.isPDR && gd.s == 0) _pupilD.r = gd.pdr;

#elif defined(EYEDEVICE_NONE)
	_GazeV.Put(cv::Point2f(-1.F, -1.F));
	Sleep(10);
#endif

	return true;
}