Newer
Older
PrismSoftware / ECTrainer2 / EyeTrack.h
#pragma once

#include "BaseProcess.h"
#include "myOpenCV.h"

class Marker;

class EyeTrack : public BaseProcess
{
	static const int PORT = 49152;	// The port on which to listen for incoming data
	static const int BUFLEN = 512;	// Socket通信バッファサイズ
	static const int DATA_MEAN_SIZE = 20;	// 注視点データ移動平均サイズ

	Marker* _pMarker;
	cv::Point _GazeV;	// 視野画像中の注視点座標
	cv::Point2f _GazeI;	// 刺激画像中の注視点座標(正規化座標系)
	SOCKET _socket;
	struct sockaddr_in _socketAddr;
	bool _KeepAliveSignal;

public:
	EyeTrack(ECTrainer* pEct, Marker* pMarker);
	bool Init();
	bool MainLoop();
	bool KeepAliveLoop();
	void SetGazeV(cv::Point gazeV) { _GazeV = gazeV; }
	cv::Point GetGazeV() { return _GazeV; }
	cv::Point2f GetGazeI() { return _GazeI; }
};

// トラッカーデータ解析クラス
struct GazeData {
	long ts = 0;
	int s = 0;
	int gidx = 0;
	int l = 0;
	cv::Point2f gp;
	cv::Point3f gdr;
	cv::Point3f gdl;
	bool isGP = false;
	bool isGDR = false;
	bool isGDL = false;

	GazeData(char* str) {
		char* pts = strstr(str, "\"ts\"");
		if (pts) ts = atol(pts + 5);
		else pts = str;

		char* ps = strstr(pts + 5, "\"s\"");
		if (ps) s = atoi(ps + 4);
		else ps = str;

		char* pgidx = strstr(ps + 4, "\"gidx\"");
		if (pgidx) gidx = atoi(pgidx + 7);
		else pgidx = str;

		char* pl = strstr(pgidx + 7, "\"l\"");
		if (pl) {
			l = atoi(pl + 4);

			char* pgp = strstr(pl + 4, "\"gp\"");
			if (pgp) {
				isGP = true;
				gp.x = (float)atof(pgp + 6);
				pgp = strchr(pgp + 6, ',');
				if (pgp) gp.y = (float)atof(pgp + 1);
			}
		}

		char* pgd = strstr(pgidx, "\"gd\"");
		if (pgd) {
			if (strstr(pgd, "right") != NULL) {
				isGDR = true;
				gdr.x = (float)atof(pgd + 6);
				pgd = strchr(pgd + 6, ',');
				if (pgd) gdr.y = (float)atof(pgd + 1);
				else pgd = pgidx;
				pgd = strchr(pgd + 1, ',');
				if (pgd) gdr.z = (float)atof(pgd + 1);
			} else {
				isGDL = true;
				gdl.x = (float)atof(pgd + 6);
				pgd = strchr(pgd + 6, ',');
				if (pgd) gdl.y = (float)atof(pgd + 1);
				pgd = strchr(pgd + 1, ',');
				if (pgd) gdl.z = (float)atof(pgd + 1);
			}
		}
	}
};