KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sample2_recorder.h
Go to the documentation of this file.
1 /* $Id: sample2_recorder.h 18301 2014-11-26 19:17:13Z ksimek $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2012 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 
22 #include <gui_cpp/gui_graph.h>
24 #include <sample_cpp/sample_base.h>
25 #include <gui_cpp/gui_viewer.h>
26 #include <gui_cpp/gui_graph.h>
27 #include <gr_cpp/gr_opengl.h>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/make_shared.hpp>
30 
31 
32 #ifdef KJB_HAVE_OPENGL
33 template <class Model>
34 class Target_plot_recorder
35 {
36 private:
37  Target_plot_recorder(const Target_plot_recorder&){}
38  Target_plot_recorder& operator=(const Target_plot_recorder&){ return *this;}
39 public:
40  typedef Model Model_type;
41  typedef kjb::gui::Plot Value_type;
42  typedef std::map<double, double> Data_map;
43 
44  const kjb::gui::Plot& get() const{ return plot_;}
45 
46  Target_plot_recorder(size_t width, size_t height) :
47  wnd_(),
48  viewer_(),
49  plot_(0,0,width, height),
50  cull_begin_(0),
51  cull_end_(0)
52  {
53  typedef kjb::opengl::Glut_window Glut_window;
54 
55  kjb::opengl::Glut::push_current_window();
56 
57  wnd_ = boost::shared_ptr<Glut_window>(new Glut_window(width, height, "Target Distribution vs. Iteration"));
58  wnd_->set_current();
59 
60  viewer_ = boost::make_shared<kjb::gui::Viewer>(width, height);
61 
62  viewer_->attach(*wnd_);
63  viewer_->attach_overlay(&plot_);
64  dataset_ = plot_.add_dataset(Data_map());
65  plot_.auto_axis(false, true);
66  GL_ETX();
67 
68  kjb::opengl::Glut::pop_current_window();
69  }
70 
71  void include_y_origin(bool b)
72  {
73  plot_.auto_axis(false, b);
74  }
75 
85  void set_culling(size_t cull_begin, size_t cull_end = 0)
86  {
87  cull_begin_ = cull_begin;
88  if(cull_end == 0)
89  cull_end_ = cull_begin_;
90  else
91  cull_end_ = cull_end;
92  }
93 
94  void operator()(const Model& model, const Step_log<Model>& step_log) const
95  {
96  typedef Data_map::iterator Iterator;
97  // almost always, step_log will have only one entry.
98  // In the rare case when there's multiple, this will only
99  // take the last one
100  double lt = step_log.back().lt;
101 
102  // append one value
103  plot_.append_dataset(
104  dataset_,
105  &lt,
106  &lt + 1);
107 
108  if(cull_begin_ && dataset_->data.size() > cull_begin_)
109  {
110  Data_map& data = dataset_->data;
111  // UNTESTED
112  int remove_count = (int) data.size() - (int) cull_end_;
113  Iterator remove_begin = data.begin();
114  Iterator remove_end = data.begin();
115  std::advance(remove_end, remove_count);
116 
117  Iterator it = remove_begin;
118  while(it != remove_end)
119  data.erase(it++);
120  dataset_->update_data_range();
121  }
122 
123  GL_ETX();
124  wnd_->redisplay();
125  glFlush();
126  GL_ETX();
127  }
128 
129 private:
130  boost::shared_ptr<kjb::opengl::Glut_window> wnd_;
131  boost::shared_ptr<kjb::gui::Viewer> viewer_;
132  mutable kjb::gui::Plot plot_;
133  kjb::gui::Plot::Data_set_iterator dataset_;
134  size_t cull_begin_;
135  size_t cull_end_;
136 };
137 #endif /* KJB_HAVE_OPENGL */
height
Definition: APPgetLargeConnectedEdges.m:33
Definition: sample_base.h:160