KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bbb_data.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 /* $Id$ */
20 
21 #ifndef B3_DATA_H
22 #define B3_DATA_H
23 
24 #include <bbb_cpp/bbb_trajectory.h>
25 #include <l_cpp/l_functors.h>
26 #include <algorithm>
27 #include <vector>
28 #include <iterator>
29 #include <string>
30 #include <cstring>
31 #include <functional>
32 #include <boost/bind.hpp>
33 
34 namespace kjb {
35 namespace bbb {
36 
37 class Data
38 {
39 public:
41  Data() {}
42 
44  template<class TrIt>
45  Data(TrIt first, TrIt last) : trajectories_(first, last)
46  {
47  ids_.resize(trajectories_.size());
48  std::generate(ids_.begin(), ids_.end(), Increment<size_t>(0));
49  }
50 
52  template<class TrIt, class IdIt>
53  Data(TrIt first, TrIt last, IdIt first_id) : trajectories_(first, last)
54  {
55  IdIt last_id = first_id;
56  std::advance(last_id, std::distance(first, last));
57  ids_.assign(first_id, last_id);
58  }
59 
61  template<class TrIt, class IdIt>
62  void set(TrIt first, TrIt last, IdIt first_id)
63  {
64  trajectories_.assign(first, last);
65 
66  IdIt last_id = first_id;
67  std::advance(last_id, std::distance(first, last));
68  ids_.assign(first_id, last_id);
69  }
70 
71 public:
73  size_t size() const
74  {
75  return trajectories_.size();
76  }
77 
79  const Trajectory& trajectory(size_t k) const
80  {
81  return trajectories_[k];
82  }
83 
85  size_t id(size_t k) const
86  {
87  return ids_[k];
88  }
89 
91  size_t index(size_t id) const
92  {
93  std::vector<size_t>::const_iterator sz_p;
94  sz_p = std::find(ids_.begin(), ids_.end(), id);
95  return sz_p == ids_.end() ? size() : *sz_p;
96  }
97 
99  std::vector<Trajectory>::const_iterator begin() const
100  {
101  return trajectories_.begin();
102  }
103 
105  std::vector<Trajectory>::const_iterator end() const
106  {
107  return trajectories_.end();
108  }
109 
111  std::vector<size_t>::const_iterator ibegin() const
112  {
113  return ids_.begin();
114  }
115 
117  std::vector<size_t>::const_iterator iend() const
118  {
119  return ids_.end();
120  }
121 
123  size_t dimensions() const
124  {
125  if(trajectories_.empty()) return 0;
126 
127  return begin()->dimensions();
128  }
129 
131  size_t start_frame() const
132  {
133  IFT(!trajectories_.empty(), Runtime_error, "Data is empty.");
134 
135  std::vector<Trajectory>::const_iterator tr_p;
136  tr_p = min_element(
137  trajectories_.begin(),
138  trajectories_.end(),
139  boost::bind(
140  std::less<size_t>(),
141  boost::bind(&Trajectory::start, _1),
142  boost::bind(&Trajectory::start, _2)));
143 
144  return tr_p->start();
145  }
146 
148  size_t end_frame() const
149  {
150  std::vector<Trajectory>::const_iterator tr_p;
151  tr_p = max_element(
152  trajectories_.begin(),
153  trajectories_.end(),
154  boost::bind(
155  std::less<size_t>(),
156  boost::bind(&Trajectory::end, _1),
157  boost::bind(&Trajectory::end, _1)));
158 
159  return tr_p->end();
160  }
161 
162 private:
163  std::vector<Trajectory> trajectories_;
164  std::vector<size_t> ids_;
165 };
166 
168 void read(Data& data, const std::string& fname);
169 
171 void write(const Data& data, const std::string& fname);
172 
173 }} // namespace kjb::bbb
174 
175 #endif /*B3_DATA_H */
176 
size_t index(size_t id) const
Get kth trajectory.
Definition: bbb_data.h:91
size_t start_frame() const
Start frame of the data.
Definition: bbb_data.h:131
size_t end() const
Gets the end frame of this person.
Definition: bbb_trajectory.h:115
for k
Definition: APPgetLargeConnectedEdges.m:61
std::vector< Trajectory >::const_iterator begin() const
Iterator to first trajectory.
Definition: bbb_data.h:99
size_t end_frame() const
End frame of the data.
Definition: bbb_data.h:148
std::vector< size_t >::const_iterator iend() const
Iterator to ID at one-past-the end.
Definition: bbb_data.h:117
Generator that increments (++) its state everytime it is called. Useful for creating sequences of con...
Definition: l_functors.h:39
size_t id(size_t k) const
Get kth trajectory.
Definition: bbb_data.h:85
Data(TrIt first, TrIt last)
Data set containing given trajectories.
Definition: bbb_data.h:45
const Trajectory & trajectory(size_t k) const
Get kth trajectory.
Definition: bbb_data.h:79
#define IFT(a, ex, msg)
Definition: l_exception.h:101
std::vector< size_t >::const_iterator ibegin() const
Iterator to first ID.
Definition: bbb_data.h:111
Definition: bbb_trajectory.h:41
std::vector< Trajectory >::const_iterator end() const
Iterator to trajectory at one-past-the end.
Definition: bbb_data.h:105
void write(const Data &data, const std::string &fname)
Write to file.
Definition: bbb_data.cpp:123
Definition: bbb_data.h:37
size_t dimensions() const
Dimensionality of the data.
Definition: bbb_data.h:123
void read(Data &data, const std::string &fname)
Read from file.
Definition: bbb_data.cpp:37
void set(TrIt first, TrIt last, IdIt first_id)
Set trajectories.
Definition: bbb_data.h:62
Data()
Empty data set.
Definition: bbb_data.h:41
size_t start() const
Gets the start frame of this person.
Definition: bbb_trajectory.h:112
size_t size() const
Number of trajectories.
Definition: bbb_data.h:73
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321
Data(TrIt first, TrIt last, IdIt first_id)
Data set containing given trajectories.
Definition: bbb_data.h:53