gaze  0.1.0
Perform gaze tracking with common webcams.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
experiment.cpp
1 #include "where_people_look/experiment.h"
2 
3 #include <memory>
4 #include <mutex> // NOLINT
5 #include <string>
6 
7 #include "boost/filesystem.hpp"
8 #include "gaze/gaze.h"
9 #include "gtk/gtk.h"
10 
11 #include "where_people_look/config.h"
12 
13 
14 namespace wpl {
15 
16 void Experiment::init_gaze_tracker() {
17  this->gaze_tracker->init(this->config->get_subject_id());
18 }
19 
20 void Experiment::read_stimuli_list() {
21  boost::filesystem::directory_iterator
22  iter(this->config->get_stimuli_dir_path());
23  for (boost::filesystem::directory_entry entry : iter) {
24  if (boost::filesystem::is_regular_file(entry)
25  && !boost::filesystem::extension(entry).compare(".jpeg")) {
26  this->trials.push(entry.path().string());
27  }
28  }
29 }
30 
31 gboolean Experiment::experiment_stop_trial(gpointer experiment) {
32  Experiment* exp = static_cast<Experiment*>(experiment);
33  exp->gaze_tracker->stop_trial();
34  gtk_widget_hide(GTK_WIDGET(exp->image));
35  return false;
36 }
37 
38 gboolean Experiment::experiment_start_trial(gpointer experiment) {
39  Experiment* exp = static_cast<Experiment*>(experiment);
40  std::string image_file = exp->trials.front();
41  exp->trials.pop();
42  gtk_image_set_from_file(exp->image, image_file.c_str());
43  gtk_widget_show(GTK_WIDGET(exp->image));
44  exp->gaze_tracker->start_trial(image_file);
45  return false;
46 }
47 
48 Experiment::Experiment(GtkImage* const image, Config* const config)
49  : config(config),
50  gaze_tracker(new gaze::GazeTracker()),
51  image(image) {
52 }
53 
54 Experiment::~Experiment() {
55 }
56 
58  return this->config;
59 }
60 
62  std::lock_guard<std::mutex> lock(this->mutex);
63  if (this->is_prepared) {
64  return;
65  }
66  this->init_gaze_tracker();
67  this->read_stimuli_list();
68  this->is_prepared = true;
69 }
70 
72  std::lock_guard<std::mutex> lock(this->mutex);
73  if (this->is_started) {
74  return;
75  }
76  g_timeout_add(this->warmup, Experiment::experiment_trial, this);
77  this->is_started = true;
78 }
79 
81  std::lock_guard<std::mutex> lock(this->mutex);
82  if (this->is_calibrating) {
83  return true;
84  }
85  if (this->calibration_countdown-- == 0) {
86  this->is_calibrating = true;
87  this->calibration_countdown = this->calibration_after;
88  g_timeout_add(0, Experiment::experiment_calibrate, this);
89  g_timeout_add(500, Experiment::experiment_trial, this);
90  } else {
91  if (!this->trials.empty()) {
92  g_timeout_add(0, Experiment::experiment_start_trial, this);
93  g_timeout_add(this->trial_duration, Experiment::experiment_stop_trial,
94  this);
95  g_timeout_add(this->trial_duration + this->pause_duration,
97  } else {
98  g_timeout_add(this->cooldown, Experiment::experiment_quit_program, this);
99  }
100  }
101  return false;
102 }
103 
104 gboolean Experiment::experiment_calibrate(gpointer experiment) {
105  Experiment* exp = static_cast<Experiment*>(experiment);
106  exp->is_calibrating = false;
107  exp->gaze_tracker->calibrate();
108  return false;
109 }
110 
111 gboolean Experiment::experiment_quit_program(gpointer experiment) {
112  Experiment* exp = static_cast<Experiment*>(experiment);
113  GtkWidget* window = gtk_widget_get_ancestor(GTK_WIDGET(exp->image),
114  GTK_TYPE_WINDOW);
115  g_signal_emit_by_name(window, "destroy");
116  return false;
117 }
118 
119 gboolean Experiment::experiment_trial(gpointer experiment) {
120  return static_cast<Experiment*>(experiment)->trial();
121 }
122 
123 bool Experiment::experiment_prepare(const GtkWidget* const,
124  Experiment* const experiment) {
125  experiment->prepare();
126  return false;
127 }
128 
129 bool Experiment::experiment_start(const GtkWidget* const,
130  const GdkEventKey* const event_key,
131  Experiment* const experiment) {
132  if (event_key->keyval == GDK_KEY_space) {
133  if (!experiment->is_started) {
134  experiment->start();
135  }
136  }
137  return false;
138 }
139 
140 } // namespace wpl
Experiment(GtkImage *const image, Config *const config)
Definition: experiment.cpp:48
static gboolean experiment_trial(gpointer experiment)
Definition: experiment.cpp:119
static bool experiment_prepare(const GtkWidget *const assistant, Experiment *const experiment)
Definition: experiment.cpp:123
Config * get_config()
Definition: experiment.cpp:57
static bool experiment_start(const GtkWidget *const window, const GdkEventKey *const event_key, Experiment *const experiment)
Definition: experiment.cpp:129
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Holds all configuration options for the experiment.
Definition: config.h:15
Implements an experiment flow for Judd et al. (2009) .
Definition: experiment.h:45
static gboolean experiment_calibrate(gpointer experiment)
Definition: experiment.cpp:104
const char * c_str() const noexcept
static gboolean experiment_quit_program(gpointer experiment)
Definition: experiment.cpp:111