]> git.sesse.net Git - casparcg/blob - common/diagnostics/graph.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / common / diagnostics / graph.cpp
1 #include "../stdafx.h"\r
2 \r
3 #include "graph.h"\r
4 #include "context.h"\r
5 \r
6 #pragma warning (disable : 4244)\r
7 \r
8 #include "../concurrency/executor.h"\r
9 #include "../utility/timer.h"\r
10 \r
11 #include <SFML/Graphics.hpp>\r
12 \r
13 #include <boost/foreach.hpp>\r
14 #include <boost/optional.hpp>\r
15 #include <boost/circular_buffer.hpp>\r
16 #include <boost/range/algorithm_ext/erase.hpp>\r
17 \r
18 #include <numeric>\r
19 #include <map>\r
20 #include <array>\r
21 \r
22 namespace caspar { namespace diagnostics {\r
23 \r
24 class guide : public drawable\r
25 {\r
26         float value_;\r
27         color c_;\r
28 public:\r
29         guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
30                 : value_(0.0f)\r
31                 , c_(c){}\r
32 \r
33         guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
34                 : value_(value)\r
35                 , c_(c){}\r
36                         \r
37         void set_color(color c) {c_ = c;}\r
38 \r
39         void render(sf::RenderTarget&)\r
40         {               \r
41                 glEnable(GL_LINE_STIPPLE);\r
42                 glLineStipple(3, 0xAAAA);\r
43                 glBegin(GL_LINE_STRIP); \r
44                         glColor4f(c_.red, c_.green, c_.blue+0.2f, c_.alpha);            \r
45                         glVertex3f(0.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);            \r
46                         glVertex3f(1.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);    \r
47                 glEnd();\r
48                 glDisable(GL_LINE_STIPPLE);\r
49         }\r
50 };\r
51 \r
52 class line : public drawable\r
53 {\r
54         boost::optional<diagnostics::guide> guide_;\r
55         boost::circular_buffer<std::pair<float, bool>> line_data_;\r
56 \r
57         std::vector<float>              tick_data_;\r
58         bool                                    tick_tag_;\r
59         color c_;\r
60 public:\r
61         line(size_t res = 600)\r
62                 : line_data_(res)\r
63                 , tick_tag_(false)\r
64                 , c_(1.0f, 1.0f, 1.0f)\r
65         {\r
66                 line_data_.push_back(std::make_pair(-1.0f, false));\r
67         }\r
68         \r
69         void update(float value)\r
70         {\r
71                 tick_data_.push_back(value);\r
72         }\r
73         \r
74         void tag()\r
75         {\r
76                 tick_tag_ = true;\r
77         }\r
78 \r
79         void guide(const guide& guide)\r
80         {\r
81                 guide_ = guide;\r
82                 guide_->set_color(c_);\r
83         }\r
84         \r
85         void set_color(color c)\r
86         {\r
87                 c_ = c;\r
88                 if(guide_)\r
89                         guide_->set_color(c_);\r
90         }\r
91 \r
92         color get_color() const { return c_; }\r
93         \r
94         void render(sf::RenderTarget& target)\r
95         {\r
96                 float dx = 1.0f/static_cast<float>(line_data_.capacity());\r
97                 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;\r
98 \r
99                 if(!tick_data_.empty())\r
100                 {\r
101                         float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();\r
102                         line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));\r
103                         tick_data_.clear();\r
104                 }\r
105                 else if(!line_data_.empty())\r
106                 {\r
107                         line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));\r
108                 }\r
109                 tick_tag_ = false;\r
110 \r
111                 if(guide_)\r
112                         target.Draw(*guide_);\r
113                 \r
114                 glBegin(GL_LINE_STRIP);\r
115                 glColor4f(c_.red, c_.green, c_.blue, 1.0f);             \r
116                 for(size_t n = 0; n < line_data_.size(); ++n)           \r
117                         if(line_data_[n].first > -0.5)\r
118                                 glVertex3f(x+n*dx, std::max(0.05f, std::min(0.95f, (1.0f-line_data_[n].first)*0.8f + 0.1f)), 0.0f);             \r
119                 glEnd();\r
120                                 \r
121                 glEnable(GL_LINE_STIPPLE);\r
122                 glLineStipple(3, 0xAAAA);\r
123                 for(size_t n = 0; n < line_data_.size(); ++n)   \r
124                 {\r
125                         if(line_data_[n].second)\r
126                         {\r
127                                 glBegin(GL_LINE_STRIP);\r
128                                 glColor4f(c_.red, c_.green, c_.blue, c_.alpha);                                 \r
129                                         glVertex3f(x+n*dx, 0.0f, 0.0f);                         \r
130                                         glVertex3f(x+n*dx, 1.0f, 0.0f);         \r
131                                 glEnd();\r
132                         }\r
133                 }\r
134                 glDisable(GL_LINE_STIPPLE);\r
135         }\r
136 };\r
137 \r
138 struct graph::implementation : public drawable\r
139 {\r
140         std::map<std::string, diagnostics::line> lines_;\r
141         std::string name_;\r
142         float height_scale_;\r
143 \r
144         implementation(const std::string& name) \r
145                 : name_(name)\r
146                 , height_scale_(0){}\r
147 \r
148         void update(const std::string& name, float value)\r
149         {\r
150                 context::begin_invoke([=]\r
151                 {\r
152                         lines_[name].update(value);\r
153                 });\r
154         }\r
155 \r
156         void tag(const std::string& name)\r
157         {\r
158                 context::begin_invoke([=]\r
159                 {\r
160                         lines_[name].tag();\r
161                 });\r
162         }\r
163 \r
164         void set_color(const std::string& name, color c)\r
165         {\r
166                 context::begin_invoke([=]\r
167                 {\r
168                         lines_[name].set_color(c);\r
169                 });\r
170         }\r
171         \r
172         void guide(const std::string& name, float value)\r
173         {\r
174                 context::begin_invoke([=]\r
175                 {\r
176                         lines_[name].guide(diagnostics::guide(value));\r
177                 });\r
178         }\r
179 \r
180 private:\r
181         void render(sf::RenderTarget& target)\r
182         {\r
183                 glPushMatrix();\r
184                         glScaled(1.0f, height_scale_, 1.0f);\r
185                         height_scale_ = std::min(1.0f, height_scale_+0.1f);\r
186 \r
187                         const size_t text_size = 15;\r
188                         const size_t text_margin = 2;\r
189                         const size_t text_offset = text_size+text_margin*2;\r
190 \r
191                         sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
192                         text.SetStyle(sf::String::Italic);\r
193                         text.Move(text_margin, text_margin);\r
194                 \r
195                         glPushMatrix();\r
196                                 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);\r
197                                 target.Draw(text);\r
198                                 float x_offset = text.GetPosition().x + text.GetRect().Right + text_margin*4;\r
199                                 for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
200                                 {                                               \r
201                                         sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);\r
202                                         line_text.SetPosition(x_offset, text_margin);\r
203                                         auto c = it->second.get_color();\r
204                                         line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));\r
205                                         target.Draw(line_text);\r
206                                         x_offset = line_text.GetRect().Right + text_margin*2;\r
207                                 }\r
208 \r
209                                 glDisable(GL_TEXTURE_2D);\r
210                         glPopMatrix();\r
211 \r
212                         glBegin(GL_QUADS);\r
213                                 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
214                                 glVertex2f(1.0f, 0.99f);\r
215                                 glVertex2f(0.0f, 0.99f);\r
216                                 glVertex2f(0.0f, 0.01f);        \r
217                                 glVertex2f(1.0f, 0.01f);        \r
218                         glEnd();\r
219 \r
220                         glPushMatrix();\r
221                                 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);\r
222                                 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);\r
223                 \r
224                                 target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
225                                 target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
226 \r
227                                 for(auto it = lines_.begin(); it != lines_.end(); ++it)         \r
228                                         target.Draw(it->second);\r
229                 \r
230                         glPopMatrix();\r
231 \r
232                 glPopMatrix();\r
233         }\r
234 \r
235         implementation(implementation&);\r
236         implementation& operator=(implementation&);\r
237 };\r
238         \r
239 graph::graph(const std::string& name) : impl_(new implementation(name))\r
240 {\r
241         context::register_drawable(impl_);\r
242 }\r
243 \r
244 void graph::update(const std::string& name, float value){impl_->update(name, value);}\r
245 void graph::tag(const std::string& name){impl_->tag(name);}\r
246 void graph::guide(const std::string& name, float value){impl_->guide(name, value);}\r
247 void graph::set_color(const std::string& name, color c){impl_->set_color(name, c);}\r
248 \r
249 safe_ptr<graph> create_graph(const std::string& name)\r
250 {\r
251         return safe_ptr<graph>(new graph(name));\r
252 }\r
253 \r
254 }}