Preparing to make mtp be a command line using an input file.
[mtp.git] / tracker.cc
index 675ba2d..4d12e61 100644 (file)
 
 using namespace std;
 
-Tracker::Tracker(int nb_time_steps, int nb_locations) {
+void Tracker::free() {
+  delete[] _edge_lengths;
+  delete _graph;
+  deallocate_array<scalar_t>(detection_scores);
+  deallocate_array<int>(allowed_motion);
+  delete[] exits;
+  delete[] entrances;
+}
+
+void Tracker::allocate(int nb_time_steps, int nb_locations) {
+  free();
+
   _nb_locations = nb_locations;
   _nb_time_steps = nb_time_steps;
 
@@ -32,10 +43,10 @@ Tracker::Tracker(int nb_time_steps, int nb_locations) {
   entrances = new int[_nb_locations];
   exits = new int[_nb_locations];
 
-  for(int l = 0; l < nb_locations; l++) {
+  for(int l = 0; l < _nb_locations; l++) {
     entrances[l] = 0;
     exits[l] = 0;
-    for(int m = 0; m < nb_locations; m++) {
+    for(int m = 0; m < _nb_locations; m++) {
       allowed_motion[l][m] = 0;
     }
   }
@@ -50,6 +61,97 @@ Tracker::Tracker(int nb_time_steps, int nb_locations) {
   _graph = 0;
 }
 
+void Tracker::write(ostream *os) {
+  (*os) << _nb_locations << " " << _nb_time_steps <<endl;
+
+  (*os) << endl;
+
+  for(int l = 0; l < _nb_locations; l++) {
+    for(int m = 0; m < _nb_locations; m++) {
+      (*os) << allowed_motion[l][m];
+      if(m < _nb_locations - 1) (*os) << " "; else (*os) << endl;
+    }
+  }
+
+  (*os) << endl;
+
+  for(int l = 0; l < _nb_locations; l++) {
+    (*os) << entrances[l];
+    if(l < _nb_locations - 1) (*os) << " "; else (*os) << endl;
+  }
+
+  (*os) << endl;
+
+  for(int l = 0; l < _nb_locations; l++) {
+    (*os) << exits[l];
+    if(l < _nb_locations - 1) (*os) << " "; else (*os) << endl;
+  }
+
+  (*os) << endl;
+
+  for(int t = 0; t < _nb_time_steps; t++) {
+    for(int l = 0; l < _nb_locations; l++) {
+      (*os) << detection_scores[t][l];
+      if(l < _nb_locations - 1) (*os) << " "; else (*os) << endl;
+    }
+  }
+}
+
+void Tracker::read(istream *is) {
+  int nb_locations, nb_time_steps;
+
+  (*is) >> nb_locations >> nb_time_steps;
+
+  allocate(nb_time_steps, nb_locations);
+
+  for(int l = 0; l < _nb_locations; l++) {
+    for(int m = 0; m < _nb_locations; m++) {
+      (*is) >> allowed_motion[l][m];
+    }
+  }
+
+  for(int l = 0; l < _nb_locations; l++) {
+    (*is) >> entrances[l];
+  }
+
+  for(int l = 0; l < _nb_locations; l++) {
+    (*is) >> exits[l];
+  }
+
+  for(int t = 0; t < _nb_time_steps; t++) {
+    for(int l = 0; l < _nb_locations; l++) {
+      (*is) >> detection_scores[t][l];
+    }
+  }
+}
+
+void Tracker::write_trajectories(ostream *os) {
+  for(int t = 0; t < nb_trajectories(); t++) {
+    (*os) << t
+         << " " << trajectory_entrance_time(t)
+         << " " << trajectory_duration(t)
+         << " " << trajectory_score(t);
+    for(int u = 0; u < trajectory_duration(t); u++) {
+      (*os) << " " << trajectory_location(t, u);
+    }
+    (*os) << endl;
+  }
+}
+
+Tracker::Tracker() {
+  _nb_locations = 0;
+  _nb_time_steps = 0;
+
+  detection_scores = 0;
+  allowed_motion = 0;
+
+  entrances = 0;
+  exits = 0;
+
+  _edge_lengths = 0;
+  _graph = 0;
+}
+
 Tracker::~Tracker() {
   delete[] _edge_lengths;
   delete _graph;
@@ -180,6 +282,8 @@ void Tracker::print_graph_dot(ostream *os) {
 }
 
 void Tracker::track() {
+  ASSERT(_graph);
+
   int e = 0;
   for(int t = 0; t < _nb_time_steps; t++) {
     for(int l = 0; l < _nb_locations; l++) {