summaryrefslogtreecommitdiffhomepage
path: root/FaceTrackNoIR/tracker.h
blob: dd8a961bd4acc8382533f5b3ae77e5700cb7ca15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/********************************************************************************
* FaceTrackNoIR		This program is a private project of the some enthusiastic	*
*					gamers from Holland, who don't like to pay much for			*
*					head-tracking.												*
*																				*
* Copyright (C) 2010	Wim Vriend (Developing)									*
*						Ron Hendriks (Researching and Testing)					*
*																				*
* Homepage																		*																				*
* This program is free software; you can redistribute it and/or modify it		*
* under the terms of the GNU General Public License as published by the			*
* Free Software Foundation; either version 3 of the License, or (at your		*
* option) any later version.													*
*																				*
* This program is distributed in the hope that it will be useful, but			*
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY	*
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for	*
* more details.																	*
*																				*
* You should have received a copy of the GNU General Public License along		*
* with this program; if not, see <http://www.gnu.org/licenses/>.				*
*********************************************************************************/
#ifndef __TRACKER_H__
#define __TRACKER_H__

#include <QThread>
#include <QMessageBox>
#include <QLineEdit>
#include <QThread>
#include <QPoint>
#include <QWaitCondition>
#include <QList>
#include <QPainterPath>

#define DIRECTINPUT_VERSION 0x0800
#include <Dinput.h>

#include "ExcelServer.h"			// Excel-server (for analysing purposes)
#include "FTNoIR_cxx_protocolserver.h"

#include "..\ftnoir_tracker_base\FTNoIR_Tracker_base.h"
#include "..\ftnoir_protocol_base\FTNoIR_Protocol_base.h"
#include "..\ftnoir_filter_base\FTNoIR_Filter_base.h"
#include "AutoClosePtr.h"

// 1a. COM-Like usage with smart pointer.
// No need to call `ITracker::Release'; the instance will
// be released automatically in destructor of the smart pointer.
typedef AutoClosePtr<ITracker, void, &ITracker::Release> ITrackerPtr;
typedef ITracker *(WINAPI *importGetTracker)(void);
typedef AutoClosePtr<IProtocol, void, &IProtocol::Release> IProtocolPtr;
typedef IProtocol *(WINAPI *importGetProtocol)(void);
typedef AutoClosePtr<IFilter, void, &IFilter::Release> IFilterPtr;
typedef IFilter *(WINAPI *importGetFilter)(void);

// include the DirectX Library files
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")

enum AngleName {
	PITCH = 0,
	YAW = 1,
	ROLL = 2,
	X = 3,
	Y = 4,
	Z = 5
};

enum FTNoIR_Client {
	FREE_TRACK = 0,
	FLIGHTGEAR = 1,
	FTNOIR = 2,
	PPJOY = 3,
	TRACKIR = 4,
	SIMCONNECT = 5,
	FSUIPC = 6,
	MOUSE = 7
};

enum FTNoIR_Face_Tracker {
	FT_SM_FACEAPI = 0,
	FT_FTNOIR = 1,
	FT_VISAGE = 2
};

enum FTNoIR_Tracker_Status {
	TRACKER_OFF = 0,
	TRACKER_ON = 1
};

class FaceTrackNoIR;				// pre-define parent-class to avoid circular includes

//
// Structure to hold all variables concerning one of 6 DOF's
//
class THeadPoseDOF {
public:
	void initHeadPoseData(){
		headPos = 0.0f;
		offset_headPos = 0.0f;
		invert = 0.0f;
		red = 0.0f;
		rawList.clear();
		maxItems = 10.0f;
		prevPos = 0.0f;
		prevRawPos = 0.0f;
		NeutralZone = 0;
		MaxInput = 0;
		confidence = 0.0f;
		newSample = FALSE;
	}
	float headPos;					// Current position (from faceTracker, radials or meters)
	float offset_headPos;			// Offset for centering
	float invert;					// Invert measured value (= 1.0f or -1.0f)
	float red;						// Reduction factor (used for EWMA-filtering, between 0.0f and 1.0f)
	QList<float> rawList;			// List of 'n' headPos values (used for moving average)
	int maxItems;					// Maximum number of elements in rawList
	float prevPos;					// Previous Position
	float prevRawPos;				// Previous Raw Position
	QPainterPath curve;				// Bezier curve to translate input -> output
	int NeutralZone;				// Neutral zone
	int MaxInput;					// Maximum raw input
	float confidence;				// Current confidence
	bool newSample;					// Indicate new sample from tracker
};

//
// Structure to hold keycode and CTRL, SHIFT, ALT for shortkeys
//
struct TShortKey {
	BYTE keycode;					// Required Key
	bool shift;						// Modifiers to examine
	bool ctrl;
	bool alt;
	bool doPitch;					// Modifiers to act on axis
	bool doYaw;
	bool doRoll;
	bool doX;
	bool doY;
	bool doZ;
};

class Tracker : public QThread {
	Q_OBJECT

private:
	// Handles to neatly terminate thread...
	HANDLE m_StopThread;
	HANDLE m_WaitThread;

	FTNoIR_Client selectedClient;
	FTNoIR_Face_Tracker selectedTracker;

	static T6DOF current_camera;					// Used for filtering
	static T6DOF target_camera;
	static T6DOF new_camera;
	static T6DOF output_camera;

	static ITrackerPtr pTracker;					// Pointer to Tracker instance (in DLL)
	static IProtocolPtr pProtocol;					// Pointer to Protocol instance (in DLL)
	static IFilterPtr pFilter;						// Pointer to Filter instance (in DLL)

	static void addHeadPose( THeadPoseData head_pose );
	static void addRaw2List ( QList<float> *rawList, float maxIndex, float raw );
//	static float lowPassFilter ( float newvalue, float *oldvalue, float dt, float coeff);
//	static float rateLimiter ( float newvalue, float *oldvalue, float dt, float max_rate);
//	static float getCorrectedNewRaw ( float NewRaw, float rotNeutral );

	/** static member variables for saving the head pose **/
	static THeadPoseDOF Pitch;						// Head-rotation X-direction (Up/Down)
	static THeadPoseDOF Yaw;						// Head-rotation Y-direction ()
	static THeadPoseDOF Roll;						// Head-rotation Z-direction ()
	static THeadPoseDOF X;							// Head-movement X-direction (Left/Right)
	static THeadPoseDOF Y;							// Head-movement Y-direction (Up/Down)
	static THeadPoseDOF Z;							// Head-movement Z-direction (To/From camera)

	static TShortKey CenterKey;						// ShortKey to Center headposition
	static TShortKey StartStopKey;					// ShortKey to Start/stop tracking
	static TShortKey InhibitKey;					// ShortKey to disable one or more axis during tracking
	static TShortKey GameZeroKey;					// ShortKey to Set Game Zero
//	static TShortKey AxisReverseKey;				// ShortKey to reverse axis during tracking

	// Flags to start/stop/reset tracking
	static bool confid;								// Tracker data is OK
	static bool do_tracking;						// Start/stop tracking, using the shortkey
	static bool do_center;							// Center head-position, using the shortkey
	static bool do_inhibit;							// Inhibit DOF-axis, using the shortkey
	static bool do_game_zero;						// Set in-game zero, using the shortkey
	static bool do_axis_reverse;					// Axis reverse, using the shortkey

	static HANDLE hTrackMutex;						// Prevent reading/writing the headpose simultaneously

	static bool useFilter;							// Use EWMA-filtering
	static bool setZero;							// Set to zero's, when OFF (one-shot)
	static bool setEngineStop;						// Stop tracker->engine, when OFF

	static bool useAxisReverse;						// Use Axis Reverse
	static float YawAngle4ReverseAxis;				// Axis Reverse settings
	static float Z_Pos4ReverseAxis;
	static float Z_PosWhenReverseAxis;

	FaceTrackNoIR *mainApp;

	QSharedPointer<ProtocolServerBase> debug_Client;	// Protocol Server to log debug-data

protected:
	// qthread override run method 
	void run();

public:
	Tracker( int clientID, int facetrackerID, FaceTrackNoIR *parent );
	~Tracker();

	void setup();

//	void registerHeadPoseCallback();
	bool handleGameCommand ( int command );
	QString getGameProgramName();					// Get the ProgramName from the game and display it.
	void loadSettings();							// Load settings from the INI-file
	bool isShortKeyPressed( TShortKey *key, BYTE *keystate );

	static bool getTrackingActive() { return do_tracking && confid; }
	static bool getAxisReverse() { return do_axis_reverse; }

	static bool getConfid() { return confid; }

	static void setInvertPitch(bool invert) { Pitch.invert = invert?-1.0f:+1.0f; }
	static void setInvertYaw(bool invert) { Yaw.invert = invert?-1.0f:+1.0f; }
	static void setInvertRoll(bool invert) { Roll.invert = invert?-1.0f:+1.0f; }
	static void setInvertX(bool invert) { X.invert = invert?-1.0f:+1.0f; }
	static void setInvertY(bool invert) { Y.invert = invert?-1.0f:+1.0f; }
	static void setInvertZ(bool invert) { Z.invert = invert?-1.0f:+1.0f; }

	static void setUseFilter(bool set) { useFilter = set; }
	static void setMinSmooth(int x);
	static void setMaxSmooth(int x);
	static void setPowCurve(int x);

	static void getHeadPose(THeadPoseData *data);				// Return the current headpose data
	static void getOutputHeadPose(THeadPoseData *data);			// Return the current (processed) headpose data

	static float getSmoothFromList ( QList<float> *rawList );
	static float getDegreesFromRads ( float rads ) { return (rads * 57.295781f); }
	static float getRadsFromDegrees ( float degrees ) { return (degrees * 0.017453f); }
	static float getOutputFromCurve ( QPainterPath *curve, float input, float neutralzone, float maxinput );

	// For now, use one slider for all
	void setSmoothing(int x) { 
		Pitch.maxItems = x;
		Yaw.maxItems = x;
		Roll.maxItems = x;
		X.maxItems = x;
		Y.maxItems = x;
		Z.maxItems = x;
	}

};

#endif