Panda3D
pStatGraph.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file pStatGraph.cxx
10  * @author drose
11  * @date 2000-07-19
12  */
13 
14 #include "pStatGraph.h"
15 #include "pStatServer.h"
16 #include "pStatFrameData.h"
17 #include "pStatCollectorDef.h"
18 #include "string_utils.h"
19 #include "config_pstatclient.h"
20 
21 #include <stdio.h> // for sprintf
22 
23 using std::string;
24 
25 /**
26  *
27  */
28 PStatGraph::GuideBar::
29 GuideBar(double height, const string &label, PStatGraph::GuideBarStyle style) :
30  _height(height),
31  _label(label),
32  _style(style)
33 {
34 }
35 
36 /**
37  *
38  */
39 PStatGraph::GuideBar::
40 GuideBar(const PStatGraph::GuideBar &copy) :
41  _height(copy._height),
42  _label(copy._label),
43  _style(copy._style)
44 {
45 }
46 
47 /**
48  *
49  */
50 PStatGraph::
51 PStatGraph(PStatMonitor *monitor, int xsize, int ysize) :
52  _monitor(monitor),
53  _xsize(xsize),
54  _ysize(ysize)
55 {
56  _target_frame_rate = pstats_target_frame_rate;
57  _labels_changed = false;
58  _guide_bars_changed = false;
59  _guide_bar_units = GBU_ms;
60 }
61 
62 /**
63  *
64  */
65 PStatGraph::
66 ~PStatGraph() {
67 }
68 
69 /**
70  * Returns the number of horizontal guide bars that should be drawn, based on
71  * the indicated target frame rate. Not all of these may be visible; some may
72  * be off the top of the chart because of the vertical scale.
73  */
74 int PStatGraph::
76  return _guide_bars.size();
77 }
78 
79 /**
80  * Returns the nth horizontal guide bar. This should be drawn as a horizontal
81  * line across the chart at the y pixel location determined by
82  * height_to_pixel(bar._height).
83  *
84  * It is possible that this bar will be off the top of the chart.
85  */
87 get_guide_bar(int n) const {
88 #ifndef NDEBUG
89  static GuideBar bogus_bar(0.0, "bogus", GBS_normal);
90  nassertr(n >= 0 && n < (int)_guide_bars.size(), bogus_bar);
91 #endif
92  return _guide_bars[n];
93 }
94 
95 /**
96  * Returns the current number of user-defined guide bars. Not all of these
97  * may be visible.
98  */
99 int PStatGraph::
101  return _monitor->get_server()->get_num_user_guide_bars();
102 }
103 
104 /**
105  * Returns the nth user-defined guide bar.
106  */
108 get_user_guide_bar(int n) const {
109  double height = _monitor->get_server()->get_user_guide_bar_height(n);
110  return make_guide_bar(height, GBS_user);
111 }
112 
113 /**
114  * Adjusts the height of the nth user-defined guide bar.
115  */
116 void PStatGraph::
117 move_user_guide_bar(int n, double height) {
118  _monitor->get_server()->move_user_guide_bar(n, height);
119 }
120 
121 /**
122  * Creates a new user guide bar and returns its index number.
123  */
124 int PStatGraph::
125 add_user_guide_bar(double height) {
126  return _monitor->get_server()->add_user_guide_bar(height);
127 }
128 
129 /**
130  * Removes the user guide bar with the indicated index number. All subsequent
131  * index numbers are adjusted down one.
132  */
133 void PStatGraph::
135  _monitor->get_server()->remove_user_guide_bar(n);
136 }
137 
138 /**
139  * Returns the index number of the first user guide bar found whose height is
140  * within the indicated range, or -1 if no user guide bars fall within the
141  * range.
142  */
143 int PStatGraph::
144 find_user_guide_bar(double from_height, double to_height) const {
145  return _monitor->get_server()->find_user_guide_bar(from_height, to_height);
146 }
147 
148 
149 /**
150  * Returns a string representing the value nicely formatted for its range.
151  */
152 string PStatGraph::
153 format_number(double value) {
154  char buffer[128];
155 
156  if (value < 0.01) {
157  sprintf(buffer, "%0.4f", value);
158  } else if (value < 0.1) {
159  sprintf(buffer, "%0.3f", value);
160  } else if (value < 1.0) {
161  sprintf(buffer, "%0.2f", value);
162  } else if (value < 10.0) {
163  sprintf(buffer, "%0.1f", value);
164  } else {
165  sprintf(buffer, "%0.0f", value);
166  }
167 
168  return buffer;
169 }
170 
171 /**
172  * Returns a string representing the value nicely formatted for its range,
173  * including the units as indicated.
174  */
175 string PStatGraph::
176 format_number(double value, int guide_bar_units, const string &unit_name) {
177  string label;
178 
179  if ((guide_bar_units & GBU_named) != 0) {
180  // Units are whatever is specified by unit_name, not a time unit at all.
181  label = format_number(value);
182  if ((guide_bar_units & GBU_show_units) != 0 && !unit_name.empty()) {
183  label += " ";
184  label += unit_name;
185  }
186 
187  } else {
188  // Units are either milliseconds or hz, or both.
189  if ((guide_bar_units & GBU_ms) != 0) {
190  double ms = value * 1000.0;
191  label += format_number(ms);
192  if ((guide_bar_units & GBU_show_units) != 0) {
193  label += " ms";
194  }
195  }
196 
197  if ((guide_bar_units & GBU_hz) != 0) {
198  double hz = 1.0 / value;
199 
200  if ((guide_bar_units & GBU_ms) != 0) {
201  label += " (";
202  }
203  label += format_number(hz);
204  if ((guide_bar_units & GBU_show_units) != 0) {
205  label += " Hz";
206  }
207  if ((guide_bar_units & GBU_ms) != 0) {
208  label += ")";
209  }
210  }
211  }
212 
213  return label;
214 }
215 
216 /**
217  * Resets the list of guide bars.
218  */
219 void PStatGraph::
220 update_guide_bars(int num_bars, double scale) {
221  _guide_bars.clear();
222 
223  // We'd like to draw about num_bars bars on the chart. But we also want the
224  // bars to be harmonics of the target frame rate, so that the bottom bar is
225  // at tfrn or n * tfr, where n is an integer, and the upper bars are even
226  // multiples of that.
227 
228  // Choose a suitable harmonic of the target frame rate near the bottom part
229  // of the chart.
230 
231  double bottom = (double)num_bars / scale;
232 
233  double harmonic;
234  if (_target_frame_rate < bottom) {
235  // n * tfr
236  harmonic = floor(bottom / _target_frame_rate + 0.5) * _target_frame_rate;
237 
238  } else {
239  // tfr n
240  harmonic = _target_frame_rate / floor(_target_frame_rate / bottom + 0.5);
241  }
242 
243  // Now, make a few bars at k harmonic.
244  for (int k = 1; k / harmonic <= scale; k++) {
245  _guide_bars.push_back(make_guide_bar(k / harmonic));
246  }
247 
248  _guide_bars_changed = true;
249 }
250 
251 /**
252  * Makes a guide bar for the indicated elapsed time or level units.
253  */
254 PStatGraph::GuideBar PStatGraph::
255 make_guide_bar(double value, PStatGraph::GuideBarStyle style) const {
256  string label = format_number(value, _guide_bar_units, _unit_name);
257 
258  if ((style == GBS_normal) &&
259  (_guide_bar_units & GBU_named) == 0) {
260  // If it's a time unit, check to see if it matches our target frame rate.
261  double hz = 1.0 / value;
262  if (IS_THRESHOLD_EQUAL(hz, _target_frame_rate, 0.001)) {
263  style = GBS_target;
264  }
265  }
266 
267  return GuideBar(value, label, style);
268 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void move_user_guide_bar(int n, double height)
Adjusts the height of the nth user-defined guide bar.
Definition: pStatGraph.cxx:117
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int find_user_guide_bar(double from_height, double to_height) const
Returns the index number of the first user guide bar found whose height is within the indicated range...
Definition: pStatGraph.cxx:144
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is an abstract class that presents the interface to any number of different front-ends for the s...
Definition: pStatMonitor.h:39
const GuideBar & get_guide_bar(int n) const
Returns the nth horizontal guide bar.
Definition: pStatGraph.cxx:87
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_num_guide_bars() const
Returns the number of horizontal guide bars that should be drawn, based on the indicated target frame...
Definition: pStatGraph.cxx:75
int add_user_guide_bar(double height)
Creates a new user guide bar and returns its index number.
Definition: pStatGraph.cxx:125
static std::string format_number(double value)
Returns a string representing the value nicely formatted for its range.
Definition: pStatGraph.cxx:153
GuideBar get_user_guide_bar(int n) const
Returns the nth user-defined guide bar.
Definition: pStatGraph.cxx:108
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_num_user_guide_bars() const
Returns the current number of user-defined guide bars.
Definition: pStatGraph.cxx:100
void remove_user_guide_bar(int n)
Removes the user guide bar with the indicated index number.
Definition: pStatGraph.cxx:134