KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mcmcda_track.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: Ernesto Brau
17  * =========================================================================== */
18 
19 #ifndef MCMCDA_TRACK_H_INCLUDED
20 #define MCMCDA_TRACK_H_INCLUDED
21 
22 #include <mcmcda_cpp/mcmcda_data.h>
23 #include <map>
24 #include <l_cpp/l_exception.h>
25 #include <m_cpp/m_vector.h>
27 #include <prob_cpp/prob_sample.h>
28 
29 namespace kjb {
30 namespace mcmcda {
31 
39 template<class E>
40 class Generic_track : private std::multimap<int, const E*>
41 {
42 public:
43  typedef E Element;
44 
45 private:
46  typedef std::multimap<int, const Element*> Parent;
47  mutable int chg_start_;
48  mutable int chg_end_;
49 
50 public:
54  Generic_track() : chg_start_(-1), chg_end_(-1) {}
55 
59  template<class Iterator>
60  Generic_track(Iterator first, Iterator last) :
61  Parent(first, last),
62  chg_start_(-1),
63  chg_end_(-1) {}
64 
65  // pass-throughs
66  using Parent::empty;
67  using Parent::begin;
68  using Parent::end;
69  using Parent::rbegin;
70  using Parent::rend;
71  using Parent::size;
72  using Parent::count;
73  using Parent::insert;
74  using Parent::clear;
75  using Parent::erase;
76  using Parent::find;
77  using Parent::equal_range;
78  using Parent::lower_bound;
79  using Parent::upper_bound;
80  typedef typename Parent::iterator iterator;
81  typedef typename Parent::const_iterator const_iterator;
82  typedef typename Parent::reverse_iterator reverse_iterator;
83  typedef typename Parent::const_reverse_iterator const_reverse_iterator;
84  typedef typename Parent::value_type value_type;
85  typedef typename Parent::key_type key_type;
86  typedef typename Parent::mapped_type mapped_type;
87  typedef typename Parent::reference reference;
88  typedef typename Parent::const_reference const_reference;
89 
93  int get_start_time() const
94  {
95  IFT(!empty(), Index_out_of_bounds, "get_start_time: track empty.");
96  return begin()->first;
97  }
98 
102  int get_end_time() const
103  {
104  IFT(!empty(), Index_out_of_bounds, "get_end_time: track empty.");
105  return rbegin()->first;
106  }
107 
111  int get_nth_time(int n) const
112  {
113  size_t rsz = real_size();
114  IFTD(!empty() && rsz > static_cast<size_t>(n) && n > 0,
116  "get_nth_time: cannot get %dth time, track has length %d.",
117  (n)(rsz));
118 
119  const_iterator cur = begin();
120  int k = 0;
121  int t = 0;
122  while(k < n)
123  {
124  if(cur->first != t)
125  {
126  k++;
127  t = cur->first;
128  }
129 
130  cur++;
131  }
132 
133  return t;
134  }
135 
139  int get_random_time() const
140  {
141  IFT(!empty(), Index_out_of_bounds, "get_random_time: track empty.");
142 
143  return get_nth_time(sample(
145  }
146 
150  int get_next_time(int t) const
151  {
152  const_iterator ub = this->upper_bound(t);
153  IFTD(ub != end(), Index_out_of_bounds,
154  "get_next_time: track has no after points time %d.", (t));
155 
156  return ub->first;
157  }
158 
162  int get_previous_time(int t) const
163  {
164  IFT(!empty(), Index_out_of_bounds, "get_previous_time: track is empty.");
165 
166  const_iterator ub = this->lower_bound(t);
167  IFTD(ub == begin(), Index_out_of_bounds,
168  "get_next_time: track has no before points time %d.", (t));
169  ub--;
170 
171  return ub->first;
172  }
173 
177  const Element& get_start_point() const
178  {
179  IFT(!empty(), Index_out_of_bounds, "get_start_point: track is empty.");
180  return *(begin()->second);
181  }
182 
186  const Element& get_end_point() const
187  {
188  IFT(!empty(), Index_out_of_bounds, "get_end_point: track is empty.");
189  return *(rbegin()->second);
190  }
191 
193  size_t real_size() const;
194 
196  int changed_start() const
197  {
198  return chg_start_;
199  }
200 
202  int changed_end() const
203  {
204  return chg_end_;
205  }
206 
208  void set_changed_start(int f) const
209  {
210  chg_start_ = f;
211  }
212 
214  void set_changed_end(int f) const
215  {
216  chg_end_ = f;
217  }
218 
222  bool is_valid
223  (
224  double v_bar,
225  int d_bar,
226  double sg,
227  const typename Data<Element>::Convert& to_vector
228  ) const;
229 
230  template<class Elem>
231  friend bool operator<
232  (
233  const Generic_track<Elem>& t1,
234  const Generic_track<Elem>& t2
235  );
236 
237  template<class Elem>
238  friend bool operator==
239  (
240  const Generic_track<Elem>& t1,
241  const Generic_track<Elem>& t2
242  );
243 
244  template<class Elem>
245  friend bool operator!=
246  (
247  const Generic_track<Elem>& t1,
248  const Generic_track<Elem>& t2
249  );
250 
252  {
253  using std::swap;
254  swap(t1.chg_start_, t2.chg_start_);
255 
256  Parent& m1 = t1;
257  Parent& m2 = t2;
258  swap(m1, m2);
259  }
260 };
261 
265 /*template<class Element>
266 inline Generic_track<Element> merge_tracks
267 (
268  const Generic_track<Element>& track1,
269  const Generic_track<Element>& track2,
270  int t_f,
271  int t_1
272 )
273 {
274  Generic_track<Element> result;
275  typename Generic_track<Element>::const_iterator it;
276 
277  it = track1.find(t_f);
278  IFTD(it != track1.end(), KJB_error,
279  "merge_tracks: track1 has no point at %d.", (t_f));
280  result.insert(track1.begin(), it);
281 
282  it = track2.find(t_1);
283  IFTD(it != track2.end(), KJB_error,
284  "merge_tracks: track2 has no point at %d.", (t_1));
285  result.insert(it, track2.end());
286 
287  return result;
288 }*/
289 
293 template<class Element>
294 void swap_tracks
295 (
296  Generic_track<Element>& track1,
297  Generic_track<Element>& track2,
298  int t1,
299  int tp1,
300  int t2,
301  int tq1
302 )
303 {
304  Generic_track<Element> temp_track = track1;
306 
307  it = track1.upper_bound(t1);
308  IFTD(it != track1.end(), KJB_error,
309  "swap_tracks: track1 has no point after time %d.", (t1));
310  track1.erase(it, track1.end());
311 
312  it = track2.find(tq1);
313  IFTD(it != track2.end(), KJB_error,
314  "swap_tracks: track2 has no point at time %d.", (tq1));
315  track1.insert(it, track2.end());
316 
317  it = track2.upper_bound(t2);
318  IFTD(it != track2.end(), KJB_error,
319  "swap_tracks: track2 has no point after time %d.", (t2));
320  track2.erase(it, track2.end());
321 
322  it = temp_track.find(tp1);
323  IFTD(it != temp_track.end(), KJB_error,
324  "swap_tracks: track1 has no point at time %d.", (tp1));
325  track2.insert(it, temp_track.end());
326 }
327 
328 /*============================================================================*
329  * MEMBER FUNCTION DEFINITIONS *
330  *----------------------------------------------------------------------------*/
331 
332 template<class Element>
334 {
335  size_t sz = 0;
336  int t = 0;
337  for(const_iterator pp = begin(); pp != end(); pp++)
338  {
339  if(pp->first != t)
340  {
341  sz++;
342  t = pp->first;
343  }
344  }
345 
346  return sz;
347 }
348 
349 template<class Element>
351 (
352  double v_bar,
353  int d_bar,
354  double sg,
355  const typename Data<Element>::Convert& to_vector
356 ) const
357 
358 {
359  if(real_size() < 2)
360  {
361  return false;
362  }
363 
364  typename Generic_track<Element>::const_iterator pair_p2 = begin();
365  for(typename Generic_track<Element>::const_iterator pair_p1 = pair_p2++;
366  pair_p2 != end();
367  pair_p1 = pair_p2++)
368  {
369  int d = pair_p2->first - pair_p1->first;
370  if(std::abs(d) > d_bar)
371  {
372  return false;
373  }
374 
375  /*if(!is_neighbor(*pair_p1->second, *pair_p2->second,
376  d, d_bar, v_bar, sg, to_vector))
377  {
378  return false;
379  }*/
380  }
381 
382  return true;
383 }
384 
388 template<class E>
389 inline
390 bool operator==(const Generic_track<E>& t1, const Generic_track<E>& t2)
391 {
392  const std::multimap<int, const E*>& m1 = t1;
393  const std::multimap<int, const E*>& m2 = t2;
394 
395  return m1 == m2;
396 }
397 
401 template<class E>
402 inline
403 bool operator!=(const Generic_track<E>& t1, const Generic_track<E>& t2)
404 {
405  const std::multimap<int, const E*>& m1 = t1;
406  const std::multimap<int, const E*>& m2 = t2;
407 
408  return m1 != m2;
409 }
410 
414 template<class Element>
415 inline
416 bool operator<
417 (
418  const Generic_track<Element>& t1,
419  const Generic_track<Element>& t2
420 )
421 {
422  const std::multimap<int, const Element*>& m1 = t1;
423  const std::multimap<int, const Element*>& m2 = t2;
424  return m1 < m2;
425 }
426 
427 }} //namespace kjb::mcmcda
428 
429 #endif /*MCMCDA_TRACK_H_INCLUDED */
430 
int get_end_time() const
Gets end time of track.
Definition: mcmcda_track.h:102
Parent::reference reference
Definition: mcmcda_track.h:87
Definition of various standard probability distributions.
Parent::key_type key_type
Definition: mcmcda_track.h:85
Object thrown when an index argument exceeds the size of a container.
Definition: l_exception.h:399
Parent::const_iterator const_iterator
Definition: mcmcda_track.h:81
int get_random_time() const
Gets a random time from track.
Definition: mcmcda_track.h:139
void set_changed_start(int f) const
Set the changed flag of this track.
Definition: mcmcda_track.h:208
Parent::mapped_type mapped_type
Definition: mcmcda_track.h:86
for k
Definition: APPgetLargeConnectedEdges.m:61
int get_start_time() const
Gets start time of track.
Definition: mcmcda_track.h:93
bool is_valid(double v_bar, int d_bar, double sg, const typename Data< Element >::Convert &to_vector) const
Determines whether this track is valid.
Definition: mcmcda_track.h:351
A class that represents a generic MCMCDA track.
Definition: mcmcda_track.h:40
E Element
Definition: mcmcda_track.h:43
friend void swap(Generic_track< Element > &t1, Generic_track< Element > &t2)
Definition: mcmcda_track.h:251
int get_previous_time(int t) const
Given a time, gets previous time.
Definition: mcmcda_track.h:162
Parent::iterator iterator
Definition: mcmcda_track.h:80
#define IFT(a, ex, msg)
Definition: l_exception.h:101
Parent::const_reverse_iterator const_reverse_iterator
Definition: mcmcda_track.h:83
size_t real_size() const
Get "real" size; i.e., number of frames with detections.
Definition: mcmcda_track.h:333
Parent::reverse_iterator reverse_iterator
Definition: mcmcda_track.h:82
int get_nth_time(int n) const
Gets nth time of track.
Definition: mcmcda_track.h:111
int changed_start() const
Returns changed start flag.
Definition: mcmcda_track.h:196
kjb_c::Pixel abs(const kjb_c::Pixel &p)
Take the channel-wise absolute value of a kjb_c::Pixel.
Definition: i_pixel.h:354
void swap_tracks(Generic_track< Element > &track1, Generic_track< Element > &track2, int t1, int tp1, int t2, int tq1)
Mergest two tracks.
Definition: mcmcda_track.h:295
Generic_track(Iterator first, Iterator last)
Range constructor.
Definition: mcmcda_track.h:60
boost::function1< Vector, const Element & > Convert
Definition: mcmcda_data.h:53
bool operator!=(const Generic_track< E > &t1, const Generic_track< E > &t2)
Compare two generic tracks.
Definition: mcmcda_track.h:403
std::pair< std::vector< Track >, std::vector< size_t > > sample(const Prior< Track > &prior, size_t T, const Track &def)
Sample an association from the prior.
Definition: mcmcda_prior.h:193
count
Definition: APPgetLargeConnectedEdges.m:71
const Element & get_start_point() const
Gets start point of track.
Definition: mcmcda_track.h:177
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
Exception often thrown when wrapped C functions return error codes.
Definition: l_exception.h:262
void set_changed_end(int f) const
Set the changed flag of this track.
Definition: mcmcda_track.h:214
Sampling functionality for the different distributions defined in "prob_distributions.h".
Parent::const_reference const_reference
Definition: mcmcda_track.h:88
Support for error handling exception classes in libKJB.
int get_next_time(int t) const
Given a time, gets next time.
Definition: mcmcda_track.h:150
Parent::value_type value_type
Definition: mcmcda_track.h:84
#define IFTD(a, ex, msg, params)
Definition: l_exception.h:112
const Element & get_end_point() const
Gets end point of track.
Definition: mcmcda_track.h:186
Generic_track()
Constructor.
Definition: mcmcda_track.h:54
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
bool operator==(const Generic_track< E > &t1, const Generic_track< E > &t2)
Compare two generic tracks.
Definition: mcmcda_track.h:390
int changed_end() const
Returns changed end flag.
Definition: mcmcda_track.h:202