KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bbb_visualizer.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_VISUALIZER_H
22 #define B3_VISUALIZER_H
23 
25 #include <bbb_cpp/bbb_data.h>
26 #include <bbb_cpp/bbb_trajectory.h>
28 #include <gr_cpp/gr_glut.h>
29 #include <l_cpp/l_exception.h>
30 #include <m_cpp/m_vector.h>
32 #include <video_cpp/video.h>
34 #include <prob_cpp/prob_sample.h>
35 #include <algorithm>
36 #include <boost/optional.hpp>
37 
38 namespace kjb {
39 namespace bbb {
40 
42 
44 inline
45 double trajectory_min_x(const Trajectory& traj)
46 {
47  const Trajectory::vec_t& v = traj.dim(0);
48  return *std::min_element(v.begin(), v.end());
49 }
50 
52 inline
53 double trajectory_max_x(const Trajectory& traj)
54 {
55  const Trajectory::vec_t& v = traj.dim(0);
56  return *std::max_element(v.begin(), v.end());
57 }
58 
60 inline
61 double trajectory_min_z(const Trajectory& traj)
62 {
63  const Trajectory::vec_t& v = traj.dim(1);
64  return *std::min_element(v.begin(), v.end());
65 }
66 
68 inline
69 double trajectory_max_z(const Trajectory& traj)
70 {
71  const Trajectory::vec_t& v = traj.dim(1);
72  return *std::max_element(v.begin(), v.end());
73 }
74 
77 {
78 public:
84  (
85  const Description& description,
86  const Data& data,
87  const Perspective_camera& camera,
88  const Activity_library& library
89  ) :
90 #ifdef KJB_HAVE_OPENGL
91  glwin_(DEFAULT_WIDTH, DEFAULT_HEIGHT, "Visualize description"),
92 #endif /* KJB_HAVE_OPENGL */
93  desc_p_(&description),
94  data_p_(&data),
95  cam_p_(&camera),
96  lib_p_(&library),
97  frame_(1),
98  draw_images_(false)
99  {
100  init_gl();
101  }
102 
107  Visualizer
108  (
109  const Description& description,
110  const Data& data,
111  const Activity_library& library
112  ) :
113 #ifdef KJB_HAVE_OPENGL
114  glwin_(DEFAULT_WIDTH, DEFAULT_HEIGHT, "Visualize description"),
115 #endif /* KJB_HAVE_OPENGL */
116  desc_p_(&description),
117  data_p_(&data),
118  cam_p_(0),
119  lib_p_(&library),
120  frame_(1),
121  draw_images_(false)
122  {
123  init_gl();
125  }
126 
127 public:
129  template <class InputIterator>
130  void set_frame_images(InputIterator first, InputIterator last)
131  {
132  IFT(std::distance(first, last) >= desc_p_->end(),
133  Illegal_argument, "Not enough frames");
134 
135  video_.load_images(first, last);
136  resize(video_.get_width(), video_.get_height());
137  }
138 
141  {
142  alt_camera_ = cam;
143  }
144 
146  void set_overhead_view();
147 
150 
152  void resize(size_t width, size_t height)
153  {
154 #ifdef KJB_HAVE_OPENGL
155  glwin_.set_size(width, height);
156 #else /* KJB_HAVE_OPENGL */
158 #endif /* KJB_HAVE_OPENGL */
159  }
160 
162  void set_frame(size_t f)
163  {
164  IFT(f >= 1 && f <= desc_p_->end(),
165  Illegal_argument, "Cannot set frame: value outside of range.");
166 
167  frame_ = f;
168  }
169 
171  void advance_frame(size_t df = 1)
172  {
173  frame_ += df;
174 
175  if(frame_ > desc_p_->end())
176  {
177  size_t ndf = frame_ - desc_p_->end() - 1;
178  frame_ = 1;
179  advance_frame(ndf);
180  }
181  }
182 
184  void rewind_frame(size_t df = 1)
185  {
186  if(df >= frame_)
187  {
188  size_t ndf = df - frame_;
189  frame_ = desc_p_->end();
190  rewind_frame(ndf);
191  }
192  else
193  {
194  frame_ -= df;
195  }
196  }
197 
199  template<class Func>
200  void set_key_callback(const Func& cb)
201  {
202 #ifdef KJB_HAVE_OPENGL
203  glwin_.set_keyboard_callback(cb);
204 #else /* KJB_HAVE_OPENGL */
206 #endif /* KJB_HAVE_OPENGL */
207  }
208 
210  void draw_images(bool di);
211 
212 public:
214  double width() const
215  {
216 #ifdef KJB_HAVE_OPENGL
217  return glwin_.get_width();
218 #else /* KJB_HAVE_OPENGL */
220 #endif /* KJB_HAVE_OPENGL */
221  }
222 
224  double height() const {
225 #ifdef KJB_HAVE_OPENGL
226  return glwin_.get_height();
227 #else /* KJB_HAVE_OPENGL */
229 #endif /* KJB_HAVE_OPENGL */
230  }
231 
232 private:
234  void init_gl();
235 
237  void render_scene() const;
238 
240  void render_trajectory(const Trajectory& traj) const;
241 
243  Vector random_color() const
244  {
245  static Uniform_distribution U;
246  return Vector(sample(U), sample(U), sample(U));
247  }
248 
249 private:
250 #ifdef KJB_HAVE_OPENGL
251  opengl::Glut_window glwin_;
252 #endif /* KJB_HAVE_OPENGL */
253  const Description* desc_p_;
254  const Data* data_p_;
255  const Perspective_camera* cam_p_;
256  const Activity_library* lib_p_;
257  Video video_;
258  size_t frame_;
259  boost::optional<Perspective_camera> alt_camera_;
260  bool draw_images_;
261 
262  typedef std::map<const Physical_activity*, Vector> Color_map;
263  mutable Color_map cur_colors_;
264 
265 private:
266  // constants
267  static const size_t DEFAULT_WIDTH = 800;
268  static const size_t DEFAULT_HEIGHT = 800;
269 };
270 
271 }} // namespace kjb::bbb
272 
273 #endif /*B3_VISUALIZER_H */
274 
Activity_sequence sample(const As_prior &prior)
Sample an activity sequence from the given prior.
Definition: bbb_activity_sequence_prior.cpp:90
void set_frame_images(InputIterator first, InputIterator last)
Set frame images from files.
Definition: bbb_visualizer.h:130
Definition of various standard probability distributions.
double trajectory_max_z(const Trajectory &traj)
Compute the max in the Z-direction.
Definition: bbb_visualizer.h:69
double trajectory_min_z(const Trajectory &traj)
Compute the min in the Z-direction.
Definition: bbb_visualizer.h:61
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
void set_overhead_view()
Set an overhead view.
Definition: bbb_visualizer.cpp:68
void load_images(const std::vector< Image > &images, float)
Definition: video.h:274
void clear_alternative_camera()
Return to the scene's camera.
Definition: bbb_visualizer.cpp:43
Definition: bbb_description.h:62
void set_key_callback(const Func &cb)
Set the key handling callback.
Definition: bbb_visualizer.h:200
#define IFT(a, ex, msg)
Definition: l_exception.h:101
St_perspective_camera for modeling a perspective camera using the classic Forsyth and Ponce parametri...
void advance_frame(size_t df=1)
Advance the current frame by the given value.
Definition: bbb_visualizer.h:171
Visualize a description and the corresponding data.
Definition: bbb_visualizer.h:76
void set_frame(size_t f)
Set the current frame to the given value.
Definition: bbb_visualizer.h:162
const vec_t & dim(size_t d) const
Gets the trajectory of this person.
Definition: bbb_trajectory.h:93
void resize(size_t width, size_t height)
Resize this viewer.
Definition: bbb_visualizer.h:152
iterator end()
Definition: m_vector.h:557
iterator begin()
Definition: m_vector.h:537
Definition: bbb_trajectory.h:41
Definition: perspective_camera.h:93
size_t get_height() const
Definition: video.h:336
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
Definition: bbb_data.h:37
size_t get_width() const
Definition: video.h:331
Sampling functionality for the different distributions defined in "prob_distributions.h".
double width() const
Get this viewer's width.
Definition: bbb_visualizer.h:214
double trajectory_min_x(const Trajectory &traj)
Compute the min in the X-direction.
Definition: bbb_visualizer.h:45
double trajectory_max_x(const Trajectory &traj)
Compute the max in the X-direction.
Definition: bbb_visualizer.h:53
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
Visualizer(const Description &description, const Data &data, const Perspective_camera &camera, const Activity_library &library)
Construct a visualizer with the given description, data, and camera.
Definition: bbb_visualizer.h:84
void draw_images(bool di)
Definition: bbb_visualizer.cpp:55
boost::math::uniform Uniform_distribution
Definition: prob_distribution.h:70
Support for error handling exception classes in libKJB.
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539
Definition: video.h:82
double height() const
Get this viewer's height.
Definition: bbb_visualizer.h:224
void set_alternative_camera(const Perspective_camera &cam)
Set an overhead view.
Definition: bbb_visualizer.h:140
Definition: bbb_activity_library.h:38
void rewind_frame(size_t df=1)
Rewind (move backwards) the current frame by the given value.
Definition: bbb_visualizer.h:184
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
size_t end() const
Get the end time of this description.
Definition: bbb_description.h:133