KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pt_scene_viewer.h
Go to the documentation of this file.
1 /* =========================================================================== *
2 |
3 | Copyright (c) 1994-2008 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 | Authors:
17 | Ernesto Brau
18 |
19 * =========================================================================== */
20 
21 /* $Id$ */
22 
23 #ifndef PT_SCENE_VIEWER
24 #define PT_SCENE_VIEWER
25 
26 #ifdef KJB_HAVE_OPENGL
34 #include <m_cpp/m_vector.h>
35 #include <gr_cpp/gr_glut.h>
36 #include <gr_cpp/gr_offscreen.h>
37 #include <video_cpp/video.h>
38 #include <l_cpp/l_exception.h>
39 #include <map>
40 #include <boost/bind.hpp>
41 #include <boost/optional.hpp>
42 
43 namespace kjb {
44 
45 // This should be moved to gr_opengl.h. Soon I will move it
46 namespace opengl {
47 
49 void render_arrow(const Vector& src, const Vector& dest);
50 
52 void render_x(const Vector& center, double sz);
53 
54 } // namespace kjb::opengl
55 
56 namespace pt {
57 
58 class Scene_viewer
59 {
60 private:
61  struct Render_style
62  {
63  Vector color;
64  double line_width;
65 
66  Render_style(const Vector& col, double lw) :
67  color(col), line_width(lw) {}
68  };
69 
70 public:
71  static const Render_style DEFAULT_GROUND_STYLE;
72  static const Render_style DEFAULT_TARGET_STYLE;
73  static const Render_style DEFAULT_DATA_STYLE;
74  static const Vector DEFAULT_TEXT_COLOR;
75  static const size_t DEFAULT_WIDTH = 300;
76  static const size_t DEFAULT_HEIGHT = 300;
77 
78 public:
80  Scene_viewer
81  (
82  const Scene& scene,
83  double width,
84  double height,
85  bool show_window = true
86  );
87 
88 #ifdef KJB_HAVE_GLUT
89  ~Scene_viewer()
90  {
91  if(m_glwin)
92  {
93  delete m_glwin;
94  }
95  }
96 #endif
97 
98 public:
100  void set_scene(const Scene& scene);
101 
103  const Scene& scene() { return *m_scene_p; }
104 
106  void resize(size_t width, size_t height);
107 
109  double width() const;
110 
112  double height() const;
113 
115  template<class Func>
116  void set_key_callback(const Func& cb)
117  {
118 #ifdef KJB_HAVE_GLUT
119  if(m_glwin)
120  {
121  m_glwin->set_keyboard_callback(cb);
122  }
123 #endif
124  }
125 
127  void set_facemarks(const Facemark_data& fm_data)
128  {
129  IFT(fm_data.size() == scene_length(), Illegal_argument,
130  "Facemark data has incorrect number of frames");
131 
132  m_facemark_data_p = &fm_data;
133  }
134 
136  void set_flows
137  (
138  const std::vector<Integral_flow>& flx,
139  const std::vector<Integral_flow>& fly
140  )
141  {
142  IFT(flx.size() == fly.size() && flx.size() == scene_length() - 1,
143  Illegal_argument, "Flows have incorrect number of frames");
144 
145  m_flowsx_p = &flx;
146  m_flowsy_p = &fly;
147  }
148 
150  template <class InputIterator>
151  void set_text_lines(InputIterator first, InputIterator last)
152  {
153  m_text_lines.resize(std::distance(first, last));
154  std::copy(first, last, m_text_lines.begin());
155  }
156 
158  void add_text_line(const std::string& str) { m_text_lines.push_back(str); }
159 
161  void clear_text_lines() { m_text_lines.clear(); }
162 
164  template <class InputIterator>
165  void set_frame_images(InputIterator first, InputIterator last)
166  {
167  IFT(std::distance(first, last) == scene_length(),
168  Illegal_argument, "Incorrect number of frame images.");
169 
170  m_video.load_images(first, last);
171  //m_ground_style.color[3] = 0.1;
172  resize(m_video.get_width(), m_video.get_height());
173  }
174 
176  void clear_frame_images()
177  {
178  m_video = Video();
179  //m_ground_style.color[3] = 1.0;
180  }
181 
183  void set_frame(size_t f);
184 
186  void advance_frame(size_t df = 1);
187 
189  void rewind_frame(size_t df = 1);
190 
192  double frame() const { return m_frame; }
193 
195  void set_alternative_camera(const Perspective_camera& cam)
196  {
197  *m_alt_camera = cam;
198  }
199 
201  void set_overhead_view();
202 
204  void clear_alternative_camera()
205  {
206  m_alt_camera = boost::none;
207  }
208 
209  void write_images (const std::vector<std::string>& out_fps);
210 
211 public:
213  void draw_images(bool di) { m_draw_images = di; }
214 
216  void draw_ground(bool dg) { m_draw_ground = dg; }
217 
219  void draw_cylinders(bool dc) { m_draw_cylinders = dc; }
220 
222  void draw_bodies(bool db) { m_draw_bodies = db; }
223 
225  void draw_heads(bool dh) { m_draw_heads = dh; }
226 
228  void draw_faces(bool df) { m_draw_faces = df; }
229 
231  void draw_bottoms(bool db) { m_draw_bottoms = db; }
232 
234  void draw_trails(bool dt) { m_draw_trails = dt; }
235 
237  void draw_full_boxes(bool dfb) { m_draw_full_boxes = dfb; }
238 
240  void draw_body_boxes(bool dbb) { m_draw_body_boxes = dbb; }
241 
243  void draw_face_boxes(bool dfb) { m_draw_face_boxes = dfb; }
244 
246  void draw_face_features(bool dff) { m_draw_face_features = dff; }
247 
249  void draw_model_vectors(bool dmv) { m_draw_model_vectors = dmv; }
250 
252  void draw_face_vectors(bool dfv) { m_draw_face_vectors = dfv; }
253 
255  void draw_data_boxes(bool ddb) { m_draw_data_boxes = ddb; }
256 
258  void draw_facemarks(bool dfm) { m_draw_facemarks = dfm; }
259 
261  void draw_flow_vectors(bool dfv) { m_draw_flow_vectors = dfv; }
262 
264  void draw_flow_face_vectors(bool dffv) { m_draw_flow_face_vectors = dffv; }
265 
267  void draw_text(bool dt) { m_draw_text = dt; }
268 
270  void weigh_data_box_color(bool wbc) { m_weigh_box_color = wbc; }
271 
273  void draw_3d_mode();
274 
276  void draw_2d_mode();
277 
279  void draw_solid(bool ds) { m_draw_solid = ds; }
280 
281 public:
283  void set_target_style
284  (
285  const Target& target,
286  const Vector& color,
287  double line_width,
288  bool only = false
289  );
290 
292  void set_target_data_style
293  (
294  const Target& target,
295  const Vector& color,
296  double line_width,
297  bool only = false
298  );
299 
301  void set_target_style(const Vector& color, double line_width)
302  {
303  m_target_style.color = color;
304  m_target_style.line_width = line_width;
305  }
306 
308  void clear_target_styles() { m_target_styles.clear(); }
309 
311  void set_data_box_style
312  (
313  const Detection_box& dbox,
314  const Vector& color,
315  double line_width,
316  bool only = false
317  );
318 
320  void set_facemark_style
321  (
322  const Deva_facemark& fmark,
323  const Vector& color,
324  double line_width,
325  bool only = false
326  );
327 
329  void set_data_style(const Vector& color, double line_width)
330  {
331  m_data_style.color = color;
332  m_data_style.line_width = line_width;
333  }
334 
336  void clear_data_box_styles() { m_data_box_styles.clear(); }
337 
339  void clear_facemark_styles() { m_facemark_styles.clear(); }
340 
342  void set_text_color(const Vector& color) { m_text_color = color; }
343 
345 #ifdef KJB_HAVE_GLUT
346  void redisplay() const { if(m_glwin) m_glwin->redisplay(); }
347 #endif
348 
349 private:
351  void prepare_for_rendering_2d() const;
352 
354  void init_gl();
355 
357  void init_lighting() const;
358 
360  void reshape_scene(int w, int h) const;
361 
363  void render_scene() const;
364 
365  /* @brief Renders the ground plane. */
366  void render_ground() const;
367 
368  /* @brief Renders the cylinders. */
369  void render_cylinders() const;
370 
371  /* @brief Renders the bodies. */
372  void render_heads() const;
373 
374  /* @brief Renders the bottoms of the cylinders. */
375  void render_bottoms() const;
376 
377  /* @brief Renders the trails. */
378  void render_trails() const;
379 
380  /* @brief Renders the model boxes. */
381  void render_model_boxes() const;
382 
383  /* @brief Renders the face boxes. */
384  void render_face_boxes() const;
385 
386  /* @brief Renders the model direction vectors. */
387  void render_model_vectors() const;
388 
389  /* @brief Renders the data boxes. */
390  void render_data_boxes() const;
391 
392  /* @brief Renders the facemarks. */
393  void render_facemarks() const;
394 
395  /* @brief Renders the flow vectors. */
396  void render_flow_vectors() const;
397 
398  /* @brief Renders the text. */
399  void render_text() const;
400 
401  /* @brief Render flow arrow; helper function. */
402  void render_flow_arrow
403  (
404  const Bbox& box,
405  const Visibility& vis,
406  const Integral_flow& fx,
407  const Integral_flow& fy
408  ) const;
409 
410  /* @brief Set current style. */
411  void set_current_style(const Target& tg) const;
412 
413  /* @brief Gets the color for a specific target. */
414  const Vector& get_target_color(const Target& target) const;
415 
416  /* @brief Gets the line width for a specific target. */
417  double get_target_lwidth(const Target& target) const;
418 
419  /* @brief Gets the color for a specific data box. */
420  const Vector& get_data_box_color(const Detection_box& dbox) const;
421 
422  /* @brief Gets the line width for a specific data box. */
423  double get_data_box_lwidth(const Detection_box& dbox) const;
424 
425  /* @brief Gets the color for a specific facemark. */
426  const Vector& get_facemark_color(const Deva_facemark& fmark) const;
427 
428  /* @brief Gets the line width for a specific facemark. */
429  double get_facemark_lwidth(const Deva_facemark& fmark) const;
430 
431  /* @brief Gets the color for a particular string. */
432  Vector get_text_line_color(const std::string& str) const;
433 
434  /* @brief Helper function: returns the number of frames in the scene. */
435  size_t scene_length() const
436  {
437  return m_scene_p->association.get_data().size();
438  }
439 
440 private:
441  // scene-releated members
442 #ifdef KJB_HAVE_GLUT
443  opengl::Glut_window* m_glwin;
444 #endif
445  const Scene* m_scene_p;
446  const Facemark_data* m_facemark_data_p;
447  const std::vector<Integral_flow>* m_flowsx_p;
448  const std::vector<Integral_flow>* m_flowsy_p;
449  std::vector<std::string> m_text_lines;
450  Video m_video;
451  size_t m_frame;
452  boost::optional<Perspective_camera> m_alt_camera;
453 
454  // what to render
455  bool m_draw_images;
456  bool m_draw_ground;
457  bool m_draw_cylinders;
458  bool m_draw_bodies;
459  bool m_draw_heads;
460  bool m_draw_faces;
461  bool m_draw_bottoms;
462  bool m_draw_trails;
463  bool m_draw_full_boxes;
464  bool m_draw_body_boxes;
465  bool m_draw_face_boxes;
466  bool m_draw_face_features;
467  bool m_draw_model_vectors;
468  bool m_draw_face_vectors;
469  bool m_draw_data_boxes;
470  bool m_draw_facemarks;
471  bool m_draw_flow_vectors;
472  bool m_draw_flow_face_vectors;
473  bool m_draw_text;
474  bool m_weigh_box_color;
475  bool m_draw_solid;
476 
477  // styles
478  Render_style m_ground_style;
479  Render_style m_target_style;
480  Render_style m_data_style;
481  Vector m_text_color;
482  std::map<const Target*, Render_style> m_target_styles;
483  std::map<const Detection_box*, Render_style> m_data_box_styles;
484  std::map<const Deva_facemark*, Render_style> m_facemark_styles;
485 
486 };
487 
488 }} //namespace kjb::pt
489 
490 #endif /* KJB_HAVE_OPENGL */
491 
492 #endif /*PT_SCENE_VIEWER */
493 
height
Definition: APPgetLargeConnectedEdges.m:33
Axis_aligned_rectangle_2d Bbox
Definition: d_bbox.h:29
Offscreen rendering buffer for X11 or Mac OS X.
#define IFT(a, ex, msg)
Definition: l_exception.h:101
std::vector< std::vector< Deva_facemark > > Facemark_data
Definition: pt_data.h:129
#define dest(triedge, pointptr)
Definition: triangle.c:938
Support for error handling exception classes in libKJB.
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...