KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tracking_trajectory.h
Go to the documentation of this file.
1 /* =========================================================================== *
2  |
3  | Copyright (c) 1994-2011 by Kobus Barnard (author)
4  |
5  | Personal and educational use of this code is granted, provided that this
6  | header is kept intact, and that the authorship is not misrepresented, that
7  | its use is acknowledged in publications, and relevant papers are cited.
8  |
9  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
10  |
11  | Please note that the code in this file has not necessarily been adequately
12  | tested. Naturally, there is no guarantee of performance, support, or fitness
13  | for any particular task. Nonetheless, I am interested in hearing about
14  | problems that you encounter.
15  |
16  | Author: Kyle Simek, Ernesto Brau, Jinyan Guan
17  * =========================================================================== */
18 
19 /* $Id: tracking_trajectory.h 18658 2015-03-18 14:44:28Z ernesto $ */
20 
21 #ifndef TRACKING_TRAJECTORY_H
22 #define TRACKING_TRAJECTORY_H
23 
25 #include <l_cpp/l_exception.h>
26 #include <l_cpp/l_word_list.h>
27 #include <l/l_sys_io.h>
28 #include <m_cpp/m_vector.h>
29 #include <string>
30 #include <map>
31 #include <algorithm>
32 #include <vector>
33 #include <fstream>
34 #include <iterator>
35 #include <boost/optional.hpp>
36 #include <boost/foreach.hpp>
37 #include <boost/lambda/lambda.hpp>
38 #include <boost/algorithm/string.hpp>
39 #include <boost/format.hpp>
40 #include <boost/lexical_cast.hpp>
41 
44 namespace kjb {
45 namespace tracking {
46 
47 // forward declarations
48 template <class T>
51 
52 template <class T>
55 
56 
60 template <class T>
62 {
64 
65  Generic_trajectory_element(const T& value_) : value(value_) {}
66 
70  bool parse(const std::string& line)
71  {
72  // NOT_IMPLEMENTED IN GENERAL
74  }
75 
79  void write(std::ofstream& ofs) const
80  {
81  // Implementer must specialize this function
83  }
84 
88  static void write_invalid(std::ofstream& ofs)
89  {
90  // Implementer must specialize this function
92  }
93 
95  friend
96  void swap
97  (
100  )
101  {
102  using std::swap;
103  swap(t1.value, t2.value);
104  }
105 
106  T value;
107 };
108 
109 
113 template <class T>
114 class Generic_trajectory :
115  public std::vector<boost::optional<Generic_trajectory_element<T> > >
116 {
117 public:
119  typedef std::vector<boost::optional<Trajectory_element> > Base;
120 
121 public:
122 
124 
125  Generic_trajectory(size_t size) : Base(size) {}
126 
127  Generic_trajectory(double h, double w, double g) :
128  height(h), width(w), girth(g) {}
129 
130  Generic_trajectory(size_t sz, double h, double w, double g) :
131  Base(sz), height(h), width(w), girth(g) {}
132 
133  template <class F>
134  Canonical_trajectory to_canonical(F to_vector) const;
135 
137  size_t parse(std::ifstream& ifs);
138 
142  void write(const std::string& filename) const;
143 
145  bool parse_header(const std::string& /*line*/)
146  {
147  return false;
148  }
149 
154  void write_header(std::ofstream& /*ofs*/) const {}
155 
159  int start_time() const;
160 
164  int end_time() const;
165 
169  std::vector<T> get_values() const
170  {
171  std::vector<T> vals;
172  transform(std::back_inserter(vals), boost::lambda::_1);
173  return vals;
174  }
175 
179  template <class OutIt, class Trans>
180  void transform(OutIt outp, Trans f) const;
181 
184  {
185  using std::swap;
186  swap(t1.id, t2.id);
187  swap(t1.height, t2.height);
188  swap(t1.width, t2.width);
189  swap(t1.girth, t2.girth);
190  swap(static_cast<Base&>(t1), static_cast<Base&>(t2));
191  }
192 
193 public:
195  double height;
196  double width;
197  double girth;
198 };
199 
201 
202 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
203 
204 template <class T>
205 template <class F>
208 {
209  //typedef typename Base::value_type Map_element;
210  //typedef typename Canonical_trajectory::Base::value_type Out_element;
211 
213  this->size(),
214  this->height,
215  this->width,
216  this->girth);
217  out.id = this->id;
218 
219  for(size_t i = 0; i < this->size(); ++i)
220  {
221  if((*this)[i])
222  out[i] = to_vector((*this)[i]->value);
223  }
224 
225  return out;
226 }
227 
228 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
229 
230 template <class T>
231 size_t Generic_trajectory<T>::parse(std::ifstream& ifs)
232 {
233  std::string line;
234  while(std::getline(ifs, line))
235  {
236  if(line == "")
237  {
238  continue;
239  }
240 
241  if(!parse_header(line))
242  {
243  Trajectory_element elem;
244  if(elem.parse(line))
245  {
246  this->push_back(elem);
247  }
248  else
249  {
250  this->push_back(false);
251  }
252  }
253  }
254 
255  return this->size();
256 }
257 
258 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
259 
260 template <class T>
261 void Generic_trajectory<T>::write(const std::string& filename) const
262 {
263  std::ofstream ofs(filename.c_str());
264  if(!ofs)
265  {
266  KJB_THROW_3(IO_error, "Couldn't write to file: %s", (filename.c_str()));
267  }
268 
269  write_header(ofs);
270 
271  for(size_t f = 0; f < this->size(); f++)
272  {
273  if((*this)[f])
274  {
275  (*this)[f]->write(ofs);
276  }
277  else
278  {
280  }
281  }
282 
283  ofs.close();
284 }
285 
286 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
287 
288 template <class T>
290 {
291  size_t sz = this->size();
292  for(size_t i = 0; i < sz; i++)
293  {
294  if((*this)[i])
295  {
296  return i + 1;
297  }
298  }
299 
300  return -1;
301 }
302 
303 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
304 
305 template <class T>
307 {
308  size_t sz = this->size();
309  for(int i = static_cast<int>(sz) - 1; i >= 0; i--)
310  {
311  if((*this)[i])
312  {
313  return i + 1;
314  }
315  }
316 
317  return -1;
318 }
319 
320 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
321 
322 template <class T>
323 template <class OutIt, class Trans>
324 void Generic_trajectory<T>::transform(OutIt outp, Trans f) const
325 {
326  int stime = start_time();
327  int etime = end_time();
328 
329  if(stime == -1 || etime == -1)
330  {
331  return;
332  }
333 
334  size_t stm = stime;
335  size_t etm = etime;
336  for(size_t i = stm - 1; i <= etm - 1; i++)
337  {
338  assert((*this)[i]);
339  *outp++ = f((*this)[i]->value);
340  }
341 }
342 
343 
348 template <class T>
350  public std::map<Entity_id, Generic_trajectory<T> >
351 {
352 public:
355  typedef std::map<Entity_id, Trajectory> Base;
356 
357 public:
359  Generic_trajectory_map(size_t duration = 0) : duration_(duration) {}
360 
362  template <class F>
363  Canonical_trajectory_map to_canonical(F to_vector) const;
364 
366  void parse(const std::string& path, const std::string& entity);
367 
372  void write(const std::string& dirname) const;
373 
375  size_t duration() const
376  {
377  return duration_;
378  }
379 
381  size_t& duration()
382  {
383  return duration_;
384  }
385 
387  friend
389  {
390  using std::swap;
391  swap(t1.duration_, t2.duration_);
392  swap(static_cast<Base&>(t1), static_cast<Base&>(t2));
393  }
394 
395 private:
396  size_t duration_;
397 };
398 typedef Generic_trajectory_map<kjb::Vector> Canonical_trajectory_map;
399 
400 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
401 
402 template <class T>
403 template <class F>
406 {
407  typedef typename Base::value_type Map_element;
408  //typedef typename Canonical_trajectory_map::Base::value_type Out_map_element;
409 
410  Canonical_trajectory_map out_map(this->duration_);
411 
412  BOOST_FOREACH(const Map_element& element, *this)
413  {
414  out_map[element.first] = element.second.to_canonical(to_vector);
415  }
416 
417  return out_map;
418 }
419 
420 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
421 
422 template <class T>
424 (
425  const std::string& path,
426  const std::string& entity
427 )
428 {
429  using namespace boost;
430 
431  if(entity.empty()) return;
432 
433  std::string pattern = path + "/" + entity + "_*.txt";
434  Word_list word_list(pattern.c_str(), NULL);
435 
436  for(size_t i = 0; i < word_list.size(); i++)
437  {
438  std::string path = word_list[i];
439 
440  // file name format is <type>_<index>.txt
441  // use this format to extract the entity_id
442  std::string::size_type fname_st = path.rfind('/') + 1;
443  std::string fname = path.substr(fname_st);
444 
445  std::vector<std::string> parts;
446  split(parts, fname, is_any_of("_."), token_compress_on);
447 
448  IFTD(parts.size() == 3, Illegal_argument,
449  "Invalid filename format for track file:", (fname.c_str()));
450 
451  Entity_type type = boost::lexical_cast<Entity_type>(parts[0]);
452  size_t index = boost::lexical_cast<size_t>(parts[1]);
453 
454  Entity_id id(type, index);
455  IFT(this->count(id) == 0, Illegal_argument, "Duplicate entity");
456 
457  std::ifstream ifs(path.c_str());
458  IFTD(ifs, IO_error, "File not found: %s", (path.c_str()));
459 
460  Trajectory traj;
461  traj.id = id;
465 
466  if(this->size() == 0)
467  {
468  //set_duration(traj.parse(ifs));
469  duration() = traj.parse(ifs);
470  }
471  else
472  {
473  size_t num_frames = traj.parse(ifs);
474  IFT(duration() == num_frames, Runtime_error,
475  "Trajectories have differing duration.");
476  }
477 
478  (*this)[id] = traj;
479  }
480 }
481 
482 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
483 
484 template <class T>
485 void Generic_trajectory_map<T>::write(const std::string& dirname) const
486 {
487  kjb_c::kjb_mkdir(dirname.c_str());
488  for(typename Generic_trajectory_map<T>::const_iterator it = Base::begin();
489  it != Base::end();
490  it++)
491  {
492  boost::format fmt("%s/%s_%d.txt");
493  std::string filename = (
494  fmt % dirname
495  % boost::lexical_cast<std::string>(it->first.type)
496  % it->first.index).str();
497 
498  assert(duration() == it->second.size());
499  it->second.write(filename);
500  }
501 }
502 
503 }} //namespace kjb::tracking
504 
505 #endif /*TRACKING_TRAJECTORY_H */
506 
friend void swap(Generic_trajectory< T > &t1, Generic_trajectory< T > &t2)
Swap two trajectories.
Definition: tracking_trajectory.h:183
Generic_trajectory_map(size_t duration=0)
Constructor.
Definition: tracking_trajectory.h:359
Generic_trajectory_map< kjb::Vector > Canonical_trajectory_map
Definition: tracking_trajectory.h:53
std::vector< boost::optional< Trajectory_element > > Base
Definition: tracking_trajectory.h:119
double height
Definition: tracking_trajectory.h:195
double get_entity_type_average_height(Entity_type type)
Get the average height of an entity.
Definition: tracking_entity.cpp:110
Represents an element of a trajectory of a particular entity.
Definition: tracking_trajectory.h:61
double get_entity_type_average_girth(Entity_type type)
Get the average girth of an entity.
Definition: tracking_entity.cpp:138
Canonical_trajectory to_canonical(F to_vector) const
Definition: tracking_trajectory.h:207
Canonical_trajectory_map to_canonical(F to_vector) const
convert to a "canonical" trajectory of kjb::Vectors. Used for evaluation.
Definition: tracking_trajectory.h:405
std::vector< T > get_values() const
Get a vector of the values of this trajectory.
Definition: tracking_trajectory.h:169
height
Definition: APPgetLargeConnectedEdges.m:33
Wrapper for the libKJB type Word_list (useful for globs).
Definition: l_word_list.h:60
#define KJB_THROW(ex)
Definition: l_exception.h:46
Entity_id id
Definition: tracking_trajectory.h:194
friend void swap(Generic_trajectory_element< T > &t1, Generic_trajectory_element< T > &t2)
Swap two trajectories.
Definition: tracking_trajectory.h:97
Wrapped version of Word_list integrated with kjb_glob.
size_t & duration()
Returns the duration of this set of trajectories.
Definition: tracking_trajectory.h:381
void write(std::ofstream &ofs) const
Writes this element to a stream.
Definition: tracking_trajectory.h:79
Generic_trajectory_element< T > Trajectory_element
Definition: tracking_trajectory.h:118
#define IFT(a, ex, msg)
Definition: l_exception.h:101
void parse(const std::string &path, const std::string &entity)
Reads all trajectories in a directory.
Definition: tracking_trajectory.h:424
Entity + index; used for file I/O.
Definition: tracking_entity.h:82
int end_time() const
Returns index of last valid element.
Definition: tracking_trajectory.h:306
std::map< Entity_id, Trajectory > Base
Definition: tracking_trajectory.h:355
Generic_trajectory< kjb::Vector > Canonical_trajectory
Definition: tracking_trajectory.h:49
end get the endpoints of the long edges and an image of the long edges for id
Definition: APPgetLargeConnectedEdges.m:96
Generic_trajectory_element< T > Trajectory_element
Definition: tracking_trajectory.h:354
Represents a set of trajectories; it is a map from entity to trajectory.
Definition: tracking_trajectory.h:53
void write(const std::string &filename) const
Writes a single trajectory to a file.
Definition: tracking_trajectory.h:261
count
Definition: APPgetLargeConnectedEdges.m:71
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
double get_entity_type_average_width(Entity_type type)
Get the average width of an entity.
Definition: tracking_entity.cpp:124
#define KJB_THROW_3(ex, fmt, params)
Definition: l_exception.h:56
bool parse(const std::string &line)
Reads an element from a line of a file.
Definition: tracking_trajectory.h:70
T value
Definition: tracking_trajectory.h:106
void write(const std::string &dirname) const
Writes this trajectory map to files in directory given by the string.
Definition: tracking_trajectory.h:485
static void write_invalid(std::ofstream &ofs)
Writes this element to a stream.
Definition: tracking_trajectory.h:88
Generic_trajectory(size_t sz, double h, double w, double g)
Definition: tracking_trajectory.h:130
Represents a trajectory. Vector of optionals to trajectory elements.
Definition: tracking_trajectory.h:49
int getline(FILE *fp, std::string *line, char EOL= '\n')
Like C's fgets but with std::string, or C++'s getline but with FILE*.
Generic_trajectory(double h, double w, double g)
Definition: tracking_trajectory.h:127
friend void swap(Generic_trajectory_map< T > &t1, Generic_trajectory_map< T > &t2)
Swap two trajectories.
Definition: tracking_trajectory.h:388
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
Object thrown when attempting to use unimplemented functionality.
Definition: l_exception.h:281
Entity_type
Definition: tracking_entity.h:40
bool parse_header(const std::string &)
Parses the header information from a header line.
Definition: tracking_trajectory.h:145
void write_header(std::ofstream &) const
Writes the header of a trajectory. Does nothing by default. Please specialize.
Definition: tracking_trajectory.h:154
Generic_trajectory_element(const T &value_)
Definition: tracking_trajectory.h:65
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
size_t parse(std::ifstream &ifs)
Reads a single trajectory from the given file.
Definition: tracking_trajectory.h:231
Generic_trajectory(size_t size)
Definition: tracking_trajectory.h:125
Object thrown when input or output fails.
Definition: l_exception.h:496
Support for error handling exception classes in libKJB.
void transform(OutIt outp, Trans f) const
Fill a sequence of values obtained from this trajectory.
Definition: tracking_trajectory.h:324
double girth
Definition: tracking_trajectory.h:197
#define IFTD(a, ex, msg, params)
Definition: l_exception.h:112
Generic_trajectory< T > Trajectory
Definition: tracking_trajectory.h:353
size_t duration() const
Returns the duration of this set of trajectories.
Definition: tracking_trajectory.h:375
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
Generic_trajectory()
Definition: tracking_trajectory.h:123
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321
Generic_trajectory_element()
Definition: tracking_trajectory.h:63
double width
Definition: tracking_trajectory.h:196
int start_time() const
Returns index of first valid element.
Definition: tracking_trajectory.h:289