KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bbb_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: Ernesto Brau
17  * =========================================================================== */
18 
19 /* $Id$ */
20 
21 #ifndef B3_TRAJECTORY_H
22 #define B3_TRAJECTORY_H
23 
24 #include <m_cpp/m_vector.h>
25 #include <l_cpp/l_functors.h>
26 #include <l_cpp/l_exception.h>
27 #include <algorithm>
28 #include <vector>
29 #include <iterator>
30 #include <iostream>
31 #include <boost/bind.hpp>
32 
33 namespace kjb {
34 namespace bbb {
35 
42 {
43 public:
44  typedef Vector vec_t;
45 
46 public:
48  Trajectory() : start_(0) {}
49 
51  template<class VecIter>
52  void set_positions(size_t st, VecIter first, VecIter last);
53 
55  template<class VecIter>
56  void set_dimensions(size_t st, VecIter first, VecIter last);
57 
59  template<class VecIter>
60  void append_positions(VecIter first, VecIter last);
61 
63  template<class VecIter>
64  void append_dimensions(VecIter first, VecIter last);
65 
67  vec_t pos(size_t frame) const
68  {
70  "Cannot get position: trajector is empty.");
71 
72  IFT(frame >= start() && frame <= end(), Illegal_argument,
73  "Cannot get position: frame out of range.");
74 
75  size_t t = frame - start();
76  vec_t x(static_cast<int>(dimensions()), 0.0);
77  std::transform(
78  trajectory_.begin(),
79  trajectory_.end(),
80  x.begin(),
81  boost::bind(
82  static_cast<const double&(vec_t::*)(int) const>(&vec_t::at),
83  _1, t));
84 
85  return x;
86  }
87 
89  template<class VecIter>
90  void copy(VecIter out) const;
91 
93  const vec_t& dim(size_t d) const
94  {
96  "Cannot get dim: trajectory is empty.");
97 
99  "Cannot get dth dimension of trajectory; d is too large.");
100 
101  return trajectory_[d];
102  }
103 
105  size_t dimensions() const
106  {
107  if(trajectory_.empty()) return 0;
108  return trajectory_.size();
109  }
110 
112  size_t start() const { return start_; }
113 
115  size_t end() const { return start() + size() - 1; }
116 
118  size_t size() const
119  {
120  if(dimensions() == 0) return 0;
121  return trajectory_[0].size();
122  }
123 
124 private:
125  size_t start_;
126  std::vector<vec_t> trajectory_;
127 };
128 
129 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
130 
131 template<class VecIter>
132 void Trajectory::set_positions(size_t st, VecIter first, VecIter last)
133 {
134  typedef typename std::iterator_traits<VecIter>::value_type Vec;
135 
136  //IFT(st > 0, Runtime_error, "Cannot set ponts: start is 0.");
137 
138  size_t sz = std::distance(first, last);
139  IFT(sz > 0, Runtime_error, "Cannot set ponts: size is 0.");
140 
141  size_t dim = first->size();
142  IFT(dim > 0, Runtime_error, "Cannot set ponts: dimension is 0.");
143 
144  start_ = st;
145  trajectory_.resize(dim, vec_t(static_cast<int>(sz)));
146  for(size_t d = 0; d < dim; d++)
147  {
148  std::transform(
149  first, last,
150  trajectory_[d].begin(),
151  boost::bind(
152  static_cast<const double&(Vec::*)(int) const>(&Vec::at),
153  _1, d));
154  }
155 }
156 
157 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
158 
159 template<class VecIter>
160 void Trajectory::set_dimensions(size_t st, VecIter first, VecIter last)
161 {
162  //IFT(st > 0, Runtime_error, "Cannot set dims: start is 0.");
163 
164  size_t dim = std::distance(first, last);
165  IFT(dim > 0, Runtime_error, "Cannot set dims: dimension is 0.");
166 
167  size_t sz = first->size();
168  IFT(sz > 0, Runtime_error, "Cannot set dims: size is 0.");
169 
170  start_ = st;
171  trajectory_.resize(dim);
172  std::copy(first, last, trajectory_.begin());
173 
174  // check for inconsistencies
175  for(size_t d = 0; d < dim; d++)
176  {
177  IFT(trajectory_[d].size() == size(), Illegal_argument,
178  "Cannot set trajectory dimensions; they must be same size.");
179  }
180 }
181 
182 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
183 
184 template<class VecIter>
185 void Trajectory::append_positions(VecIter first, VecIter last)
186 {
187  IFT(dimensions() != 0, Runtime_error,
188  "Cannot append positions: trajectory empty.");
189 
190  typedef typename std::iterator_traits<VecIter>::value_type Vec;
191 
192  size_t sz = std::distance(first, last);
193  if(sz == 0) return;
194 
195  size_t dim = first->size();
196  IFT(dim == dimensions(), Runtime_error,
197  "Cannot append positions: dimension mismatch");
198 
199  size_t new_sz = size() + sz;
200  for(size_t d = 0; d < dim; d++)
201  {
202  trajectory_[d].reserve(new_sz);
203  std::transform(
204  first, last,
205  std::back_inserter(trajectory_[d]),
206  boost::bind(
207  static_cast<const double&(Vec::*)(int) const>(&Vec::at),
208  _1, d));
209 
210  assert(trajectory_[d].size() == size());
211  }
212 }
213 
214 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
215 
216 template<class VecIter>
217 void Trajectory::append_dimensions(VecIter first, VecIter last)
218 {
219  IFT(dimensions() != 0, Runtime_error,
220  "Cannot append positions: trajectory empty.");
221 
222  size_t dim = std::distance(first, last);
223  IFT(dim == dimensions(), Runtime_error,
224  "Cannot append positions: dimension mismatch");
225 
226  size_t sz = first->size();
227  if(sz == 0) return;
228 
229  size_t new_sz = size() + sz;
230  for(size_t d = 0; d < dim; d++, first++)
231  {
232  trajectory_[d].reserve(new_sz);
233  std::copy(
234  first->begin(),
235  first->end(),
236  std::back_inserter(trajectory_[d]));
237 
238  assert(trajectory_[d].size() == size());
239  }
240 }
241 
242 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
243 
244 template<class VecIter>
245 void Trajectory::copy(VecIter out) const
246 {
247  IFT(dimensions() != 0, Runtime_error, "Cannot copy: empty trajectory.");
248 
249  const size_t s = start();
250  const size_t e = end();
251  for(size_t t = s; t <= e; t++)
252  {
253  *out++ = pos(t);
254  }
255 }
256 
258 std::ostream& operator<<(std::ostream& ost, const Trajectory& traj);
259 
260 }} // namespace kjb::bbb
261 
262 #endif /*B3_TRAJECTORY_H */
263 
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: m_vector.h:510
size_t size() const
Gets the size of this person.
Definition: bbb_trajectory.h:118
size_t end() const
Gets the end frame of this person.
Definition: bbb_trajectory.h:115
Vector vec_t
Definition: bbb_trajectory.h:44
Value_type & at(int i)
Safely subscript vector, e.g., A.at(10), to get an lvalue.
Definition: m_vector.h:991
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
std::ostream & operator<<(std::ostream &ost, const Activity_sequence &aseq)
Push an activity sequence to an output stream.
#define IFT(a, ex, msg)
Definition: l_exception.h:101
const vec_t & dim(size_t d) const
Gets the trajectory of this person.
Definition: bbb_trajectory.h:93
iterator begin()
Definition: m_vector.h:537
Definition: bbb_trajectory.h:41
x
Definition: APPgetLargeConnectedEdges.m:100
void set_positions(size_t st, VecIter first, VecIter last)
Set points of this trajectory.
Definition: bbb_trajectory.h:132
vec_t pos(size_t frame) const
Gets the position of this person at the given frame.
Definition: bbb_trajectory.h:67
void copy(VecIter out) const
Gets the trajectory of this person.
Definition: bbb_trajectory.h:245
void append_dimensions(VecIter first, VecIter last)
Append by dimension.
Definition: bbb_trajectory.h:217
Trajectory()
Create an empty trajectory.
Definition: bbb_trajectory.h:48
void append_positions(VecIter first, VecIter last)
Append by position.
Definition: bbb_trajectory.h:185
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
void set_dimensions(size_t st, VecIter first, VecIter last)
Set dimensions of this trajectory.
Definition: bbb_trajectory.h:160
size_t start() const
Gets the start frame of this person.
Definition: bbb_trajectory.h:112
Support for error handling exception classes in libKJB.
size_t dimensions() const
Gets the dimensionality of this person's trajectory.
Definition: bbb_trajectory.h:105
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321