-
Notifications
You must be signed in to change notification settings - Fork 0
/
SphericalBenchmarkBehaviour.cs
339 lines (283 loc) · 12.3 KB
/
SphericalBenchmarkBehaviour.cs
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using PupilLabs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Random = System.Random;
public class SphericalBenchmarkBehaviour : MonoBehaviour
{
public Camera sceneCamera;
private List<GameObject> sphereMarkers;
public GameObject wall;
// gaze
[Header("Gaze")]
public GazeController gazeController;
Vector3 localGazeDirection;
float gazeDistance;
private GazeListener gazeListener;
public Transform gazeOrigin;
public Transform projectionMarker;
public Transform gazeDirectionMarker;
public GazeVisualizer gazeVisualizer;
public float sphereCastRadius = 0.05f;
Vector3 direction = new Vector3(-1, -1, -1);
private float angularError = -1f;
private String currentSphere;
// Fixation detection
[Header("Fixation")]
public float maxDispersion = 1f; // Recommended: 1 degree
public float minDuration = 0.3f ; // Recommended: 300 ms
public SubscriptionsController subscriptionsController;
private RequestController requestController;
private bool fixation = false;
public bool annotationsOnlyDuringFixations = true;
// Exporting data
[Header("Exporting Data")]
public RecordingController recorder;
public AnnotationPublisher annotationPub;
public Transform head;
public bool sendHeadAsAnnotation = false;
public string pathForExperimentData;
private string FolderName;
private StreamWriter sw;
private int sampleIndex;
//Vectors
private Vector3 directioninWorldSpace;
private Vector3 relativespherePosition;
Vector3 sphereDirection = new Vector3(-1,-1,-1);
private Vector3 spherePosition;
private Vector3 sphereLocalPos;
private Vector3 centralRay;
private Vector3 origin;
// confidence
[Header("Confidence")]
public float confidenceThreshold = 0.6f;
float lastConfidence;
public Text debugText; // For debugging - displays pupil confidence
// Start is called before the first frame update
void Start()
{
origin = gazeOrigin.position;
// Hide benchmarking elements at beginning
sphereMarkers = GameObject.FindGameObjectsWithTag("Sphere").ToList();
centralRay = (sphereMarkers[0].transform.position - origin);// First sphere is centre sphere
Debug.Log(sphereMarkers[0].name + " distance from head: " + centralRay.magnitude);
Debug.Log(sphereMarkers[13].name + " distance from head: " + (sphereMarkers[13].transform.position - origin).magnitude);
Debug.Log(sphereMarkers[26].name + " distance from head: " + (sphereMarkers[26].transform.position - origin).magnitude);
foreach (GameObject sphere in sphereMarkers)
{
Debug.Log(sphere.name + " - " + Vector3.Angle(centralRay, sphere.transform.position - origin));
sphere.SetActive(false);
}
// Hide when not debugging
debugText.enabled = false;
requestController = subscriptionsController.requestCtrl;
wall.SetActive(true);
// Experiment Data
//sampleIndex = 1;
//FolderName = "Experiment_" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
//string path = pathForExperimentData + "/" + FolderName + "_Data.txt";
//sw = new StreamWriter(path, true);
//sw.WriteLine("SampleIndex Timestamp CamPos.x CamPos.y CamPos.z CamRot.x CamRot.y CamRot.z gazeDirection.x gazeDirection.y gazeDirection.z eyeCenter0.x eyeCenter0.y eyeCenter0.z eyeCenter1.x eyeCenter1.y eyeCenter1.z Confidence angularError spherePos.x spherePos.y SpherePos.z");
//sw.Flush();
}
// Update is called once per frame
void Update()
{
bool connected = recorder.requestCtrl.IsConnected;
UpdateText(connected);
// S key begins the experiment
if (Input.GetKeyDown(KeyCode.S))
{
// Set scene for benchmarking
foreach (GameObject elem in GameObject.FindGameObjectsWithTag("RoomElement"))
{
elem.SetActive(false);
}
// Start viewing spheres
StartCoroutine(displaySpheres());
if (requestController.IsConnected)
{
StartFixationSubscription();
}
}
// Get Gaze Data
gazeController.OnReceive3dGaze += ReceiveGaze;
// Visualise gaze ray in Unity scene mode
ShowProjected();
}
IEnumerator recordData(int sampleIndex, Vector3 eyeCenter0, Vector3 eyeCenter1, Vector3 gazeDirection, float conf, float angularErr)
{
yield return new WaitForSeconds(1f);
float timestamp = Time.realtimeSinceStartup;
sw.WriteLine(sampleIndex + " " + timestamp + " " + head.position.x + " " + head.position.y + " " + head.position.z + " " +
head.rotation.x + " " + head.rotation.y + " " + head.rotation.z + " " +
gazeDirection.x + " " + gazeDirection.y + " " + gazeDirection.z + " " + eyeCenter0.x + " " + eyeCenter0.y + " " + eyeCenter0.z + " " +
+ eyeCenter1.x + " " + eyeCenter1.y + " " + eyeCenter1.z + " " + conf + " " + angularErr + " " + spherePosition.x + " " + spherePosition.y + " " + spherePosition.z);
sw.Flush();
}
// Display spheres one by one for benchmarking
IEnumerator displaySpheres()
{
//Randomise order of spheres
Shuffle(sphereMarkers);
foreach (GameObject sphere in sphereMarkers)
{
sphere.SetActive(true);
relativespherePosition = sphere.transform.position - sceneCamera.transform.position;
directioninWorldSpace = gazeOrigin.TransformDirection(localGazeDirection);
sphereDirection = (sphere.transform.position - origin).normalized;
spherePosition = sphere.transform.position;
sphereLocalPos = sphere.transform.localPosition;
currentSphere = sphere.name;
yield return new WaitForSeconds(2);
sphere.SetActive(false);
}
}
void ReceiveGaze(GazeData gazeData)
{
if (gazeData.MappingContext != GazeData.GazeMappingContext.Binocular)
{
return;
}
lastConfidence = gazeData.Confidence;
if (gazeData.Confidence < confidenceThreshold)
{
return;
}
localGazeDirection = gazeData.GazeDirection;
gazeDistance = gazeData.GazeDistance;
// -- Uncomment to display confidence
// debugText.text = Math.Round((double)gazeData.Confidence, 2).ToString();
angularError = Vector3.Angle(direction, sphereDirection);
// -- This was to output data regularly (not just with fixations), too heavy though
//StartCoroutine(recordData(sampleIndex, gazeData.EyeCenter0, gazeData.EyeCenter1, gazeData.GazeDirection, gazeData.Confidence, angularError));
//sampleIndex++;
}
void ShowProjected()
{
direction = gazeOrigin.TransformDirection(localGazeDirection);
if (Physics.SphereCast(origin, sphereCastRadius, direction, out RaycastHit hit, Mathf.Infinity))
{
if (fixation)
{
Debug.DrawRay(origin, direction * hit.distance, Color.magenta);
}
projectionMarker.position = hit.point;
gazeDirectionMarker.position = origin + direction * hit.distance;
gazeDirectionMarker.LookAt(origin);
}
else
{
if (fixation)
{
Debug.DrawRay(origin, direction * 10, Color.blue);
}
}
}
void SendFloatAnnotation(String aLabel, float value)
{
Dictionary<string, object> data = new Dictionary<string, object>();
data[aLabel] = value;
annotationPub.SendAnnotation(label: aLabel, customData: data);
}
void SendStringAnnotation(String aLabel, String value)
{
Dictionary<string, object> data = new Dictionary<string, object>();
data[aLabel] = value;
annotationPub.SendAnnotation(label: aLabel, customData: data);
}
void SendVectorAnnotation(String aLabel, Vector3 value)
{
Dictionary<string, object> data = new Dictionary<string, object>();
data[aLabel + "_x"] = value.x;
data[aLabel + "_y"] = value.y;
data[aLabel + "_z"] = value.z;
annotationPub.SendAnnotation(label: aLabel, customData: data);
}
void SendBenchmarkAnnotation(float angularError, Vector3 gazeDir, Vector3 sphereDir, String sphereLabel, object duration, object confidence, Vector3 gazePoint, Vector2 normPos, object dispersion, Vector3 spherePos, object id)
{
Dictionary<string, object> data = new Dictionary<string, object>();
data["fixation_id"] = id;
data["angularError"] = angularError;
data["fixation_duration"] = duration;
data["dispersion"] = dispersion;
data["avg_pupil_confidence"] = confidence;
data["gaze_position_3d" + "_x"] = gazePoint.x;
data["gaze_position_3d" + "_y"] = gazePoint.y;
data["gaze_position_3d" + "_z"] = gazePoint.z;
data["norm_pos" + "_x"] = normPos.x;
data["norm_pos" + "_y"] = normPos.y;
data["head_world_x"] = head.position.x;
data["head_world_y"] = head.position.y;
data["head_world_z"] = head.position.z;
data["head_rot_x"] = head.rotation.x;
data["head_rot_y"] = head.rotation.y;
data["head_rot_z"] = head.rotation.z;
data["gaze_direction" + "_x"] = gazeDir.x;
data["gaze_direction" + "_y"] = gazeDir.y;
data["gaze_direction" + "_z"] = gazeDir.z;
data["sphere_direction" + "_x"] = sphereDir.x;
data["sphere_direction" + "_y"] = sphereDir.y;
data["sphere_direction" + "_z"] = sphereDir.z;
data["sphere_position" + "_x"] = spherePos.x;
data["sphere_position" + "_y"] = spherePos.y;
data["sphere_position" + "_z"] = spherePos.z;
data["sphere_label"] = sphereLabel;
data["sphere_angle"] = Vector3.Angle(centralRay, spherePos - origin);
annotationPub.SendAnnotation(label: "fixation_annotations", customData: data);
}
void StartFixationSubscription()
{
Debug.Log("StartFixationSubscription");
subscriptionsController.SubscribeTo("fixation", CustomReceiveData);
requestController.StartPlugin(
"Fixation_Detector",
new Dictionary<string, object> {
{ "max_dispersion", maxDispersion },
{ "min_duration", minDuration }
}
);
}
void CustomReceiveData(string topic, Dictionary<string, object> dictionary, byte[] thirdFrame = null)
{
if (dictionary.ContainsKey("timestamp"))
{
Debug.Log("Fixation detected: " + dictionary["timestamp"].ToString() + dictionary["gaze_point_3d"]);
Vector3 gazePoint = Helpers.Position(dictionary["gaze_point_3d"], false);
Vector2 normPos = Helpers.Position(dictionary["norm_pos"], false);
// Send annotations
SendBenchmarkAnnotation(angularError, direction, sphereDirection, currentSphere, dictionary["duration"], dictionary["confidence"], gazePoint, normPos, dictionary["dispersion"], spherePosition, dictionary["id"]);
Debug.DrawLine(head.transform.position, spherePosition, Color.green, 2f);
if (!fixation)
{
fixation = true;
}
}
}
void UpdateText(bool connected)
{
if (connected)
{
debugText.text = "Press R to Start/Stop the recording.";
var status = recorder.IsRecording ? "recording" : "not recording";
debugText.text = $"Status: {status}";
}
}
public static void Shuffle(List<GameObject> list)
{
Random rng = new Random();
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
GameObject value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}