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

#include "BaseProcess.h"
#include "nkcOpenCV.h"
#include "RingBuffer.h"
#include "MeanBuffer.h"

// LRテンプレート
template <class T>
class LR {
public:
	T l;
	T r;
};

class EyeTrack : public BaseProcess
{
	static const int MEAN_BUF_SIZE = 20;	// 注視点データ移動平均サイズ

	RingBuffer<cv::Point2f> _GazeV; // 視野画像中の注視点座標
	LR<float> _pupilD;
	int _lastGidx;	// 前回のデータインデックス
	cv::Point2f _gazePoint;
	MeanBuffer _gpCx, _gpCy;	// 移動平均バッファ

	//std::string ClassName() { return "EyeTrack"; }	// FPS表示

public:
	// コンストラクタ
	EyeTrack(ECTrainer* pEct);
	// メインループ
	bool Routine();
	// 注視点の更新状況
	bool IsNewGazeV() { return _GazeV.IsNew(); }
	// 注視点を取得
	cv::Point2f GetGazeV() { return _GazeV.Get(); }
};


// トラッカーデータ解析クラス
struct GazeData {
	long ts = 0;
	int s = 0;
	int gidx = 0;
	int l = 0;
	cv::Point2f gp;	// 注視点
	cv::Point3f gdr, gdl; // 視線ベクトル L,R
	float pdr, pdl;	// 瞳孔径 L,R
	bool isGP = false;
	bool isGDR = false;
	bool isGDL = false;
	bool isPDR = false;
	bool isPDL = 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);
			}
		}

		char* ppd = strstr(pgidx, "\"pd\"");
		if (ppd) {
			if (strstr(ppd, "right") != NULL) {
				isPDR = true;
				pdr = (float)atof(ppd + 5);
			} else {
				isPDL = true;
				pdl = (float)atof(ppd + 5);
			}
		}
	}
};