cgv
Loading...
Searching...
No Matches
plot2d.cxx
1#include "plot2d.h"
2#include <libs/cgv_gl/gl/gl.h>
3#include <cgv/media/color_scale.h>
4#include <cgv/render/attribute_array_binding.h>
5#include <cgv/math/ftransform.h>
6
7namespace cgv {
8 namespace plot {
9
11plot2d_config::plot2d_config(const std::string& _name) : plot_base_config(_name, 2)
12{
13 configure_chart(CT_LINE_CHART);
14};
15
17void plot2d_config::configure_chart(ChartType chart_type)
18{
20 show_lines = chart_type == CT_LINE_CHART;
21}
22
24plot2d::plot2d(const std::string& title, unsigned nr_attributes) : plot_base(2, nr_attributes)
25{
26 multi_axis_modes = new bool[2 + nr_attributes];
27 std::fill(multi_axis_modes, multi_axis_modes+(2+nr_attributes), false);
28 sub_plot_delta = vec3(0.0f);
29 disable_depth_mask = false;
30 //legend_components = LC_ANY;
33 acs[0].name = "x";
34 acs[1].name = "y";
35 for (unsigned ai =0; ai < nr_attributes; ++ai)
36 acs[ai+2].name = std::string("attribute_")+cgv::utils::to_string(ai);
37}
38
39plot2d::~plot2d()
40{
41 delete[] multi_axis_modes;
42}
43
44bool plot2d::compute_sample_coordinate_interval(int i, int ai, float& samples_min, float& samples_max)
45{
46 // compute bounding box
47 bool found_sample = false;
48 float min_value, max_value;
49 for (unsigned j = 0; j < samples[i].size(); ++j) {
50 if (found_sample) {
51 min_value = std::min(min_value, samples[i][j](ai));
52 max_value = std::max(max_value, samples[i][j](ai));
53 }
54 else {
55 min_value = samples[i][j](ai);
56 max_value = samples[i][j](ai);
57 found_sample = true;
58 }
59 }
60 if (found_sample) {
61 samples_min = min_value;
62 samples_max = max_value;
63 return true;
64 }
65 return false;
66}
67
68unsigned plot2d::add_sub_plot(const std::string& name)
69{
70 // determine index of new sub plot
71 unsigned i = get_nr_sub_plots();
72
73 // create new config
74 if (i == 0)
75 configs.push_back(new plot2d_config(name));
76 else {
77 configs.push_back(new plot2d_config(ref_sub_plot2d_config(i - 1)));
78 ref_sub_plot_config(i).name = name;
79 }
80
81 // create new point container
82 samples.push_back(std::vector<vec2>());
83 strips.push_back(std::vector<unsigned>());
85 attribute_source_arrays.back().attribute_sources.push_back(attribute_source(i, 0, 0, 2 * sizeof(float)));
86 attribute_source_arrays.back().attribute_sources.push_back(attribute_source(i, 1, 0, 2 * sizeof(float)));
87 // return sub plot index
88 return i;
89}
90
92{
93 delete configs[i];
94 configs[i] = 0;
95 configs.erase(configs.begin() + i);
96 samples.erase(samples.begin() + i);
97 strips.erase(strips.begin() + i);
98}
99
102{
103 return static_cast<plot2d_config&>(ref_sub_plot_config(i));
104}
105
106std::vector<vec2>& plot2d::ref_sub_plot_samples(unsigned i)
107{
108 return samples[i];
109}
110
112std::vector<unsigned>& plot2d::ref_sub_plot_strips(unsigned i)
113{
114 return strips[i];
115}
116
118{
119 aam_domain.init(ctx);
120 aam_domain_tick_labels.init(ctx);
121 if (!line_prog.build_program(ctx, "plot2d_line.glpr", true)) {
122 std::cerr << "could not build GLSL program from plot2d_line.glpr" << std::endl;
123 return false;
124 }
125 else
126 line_prog.allow_context_to_set_color(false);
127 if (!point_prog.build_program(ctx, "plot2d_point.glpr", true)) {
128 std::cerr << "could not build GLSL program from plot2d_point.glpr" << std::endl;
129 return false;
130 }
131 else
132 point_prog.allow_context_to_set_color(false);
133 if (!rectangle_prog.build_program(ctx, "plot2d_rect.glpr", true, { {"PLOT_MODE", "1"} })) {
134 std::cerr << "could not build GLSL program from plot2d_rect.glpr" << std::endl;
135 return false;
136 }
137 else
138 rectangle_prog.allow_context_to_set_color(false);
139 return plot_base::init(ctx);
140}
141
142void plot2d::clear(cgv::render::context& ctx)
143{
144 aam_domain.destruct(ctx);
145 aam_domain_tick_labels.destruct(ctx);
146 point_prog.destruct(ctx);
147 line_prog.destruct(ctx);
148 rectangle_prog.destruct(ctx);
149 plot_base::clear(ctx);
150}
151
152bool plot2d::draw_point_plot(cgv::render::context& ctx, int i, int layer_idx)
153{
154 // skip unvisible and empty sub plots
155 if (!ref_sub_plot2d_config(i).show_plot)
156 return false;
157 GLsizei count = (GLsizei)enable_attributes(ctx, i, samples);
158 bool result = false;
159 if (count > 0) {
160 const plot2d_config& spc = ref_sub_plot2d_config(i);
161 if (spc.show_points && point_prog.is_linked()) {
162 set_plot_uniforms(ctx, point_prog);
163 set_mapping_uniforms(ctx, point_prog);
164 point_prog.set_uniform(ctx, "sub_plot_delta", float(i) * sub_plot_delta);
165 point_prog.set_uniform(ctx, "depth_offset", -layer_idx * layer_depth);
166 point_prog.set_uniform(ctx, "reference_point_size", get_domain_config_ptr()->reference_size);
167 point_prog.set_uniform(ctx, "blend_width_in_pixel", get_domain_config_ptr()->blend_width_in_pixel);
168 point_prog.set_uniform(ctx, "halo_width_in_pixel", 0.0f);
169 point_prog.set_uniform(ctx, "percentual_halo_width", spc.point_halo_width.size / spc.point_size.size);
170 point_prog.set_uniform(ctx, "viewport_height", (float)ctx.get_height());
171 point_prog.set_uniform(ctx, "measure_point_size_in_pixel", false);
172 point_prog.set_uniform(ctx, "screen_aligned", false);
173 point_prog.set_uniform(ctx, "color_index", spc.point_color.color_idx);
174 point_prog.set_uniform(ctx, "secondary_color_index", spc.point_halo_color.color_idx);
175 point_prog.set_uniform(ctx, "opacity_index", spc.point_color.opacity_idx);
176 point_prog.set_uniform(ctx, "secondary_opacity_index", spc.point_halo_color.opacity_idx);
177 point_prog.set_uniform(ctx, "size_index", spc.point_size.size_idx);
178 point_prog.set_uniform(ctx, "secondary_size_index", spc.point_halo_width.size_idx);
179 point_prog.set_attribute(ctx, point_prog.get_color_index(), spc.point_color.color);
180 point_prog.set_attribute(ctx, "secondary_color", spc.point_halo_color.color);
181 point_prog.set_attribute(ctx, "size", spc.point_size.size);
182 point_prog.enable(ctx);
183 draw_sub_plot_samples(count, spc);
184 point_prog.disable(ctx);
185 result = true;
186 }
187 }
188 disable_attributes(ctx, i);
189 return result;
190}
191bool plot2d::draw_line_plot(cgv::render::context& ctx, int i, int layer_idx)
192{
193 // skip unvisible and empty sub plots
194 if (!ref_sub_plot2d_config(i).show_plot)
195 return false;
196 GLsizei count = (GLsizei)enable_attributes(ctx, i, samples);
197 bool result = false;
198 if (count > 0) {
199 const plot2d_config& spc = ref_sub_plot2d_config(i);
200 if (spc.show_lines && line_prog.is_linked()) {
201 set_plot_uniforms(ctx, line_prog);
202 set_mapping_uniforms(ctx, line_prog);
203 line_prog.set_uniform(ctx, "sub_plot_delta", float(i) * sub_plot_delta);
204 line_prog.set_uniform(ctx, "depth_offset", -layer_idx * layer_depth);
205 line_prog.set_uniform(ctx, "reference_line_width", get_domain_config_ptr()->reference_size);
206 line_prog.set_uniform(ctx, "blend_width_in_pixel", get_domain_config_ptr()->blend_width_in_pixel);
207 line_prog.set_uniform(ctx, "halo_width_in_pixel", 0.0f);
208 line_prog.set_uniform(ctx, "halo_color_strength", 1.0f);
209 line_prog.set_uniform(ctx, "halo_color", spc.line_halo_color.color);
210 line_prog.set_uniform(ctx, "percentual_halo_width", spc.line_halo_width.size / spc.line_width.size);
211 line_prog.set_uniform(ctx, "viewport_height", (float)ctx.get_height());
212 line_prog.set_uniform(ctx, "measure_line_width_in_pixel", false);
213 line_prog.set_uniform(ctx, "screen_aligned", false);
214 line_prog.set_uniform(ctx, "color_index", spc.line_color.color_idx);
215 line_prog.set_uniform(ctx, "secondary_size_index", spc.line_halo_width.size_idx);
216 line_prog.set_uniform(ctx, "secondary_color_index", spc.line_halo_color.color_idx);
217 line_prog.set_uniform(ctx, "opacity_index", spc.line_color.opacity_idx);
218 line_prog.set_uniform(ctx, "secondary_opacity_index", spc.line_halo_color.opacity_idx);
219 line_prog.set_uniform(ctx, "size_index", spc.line_width.size_idx);
220 line_prog.set_attribute(ctx, line_prog.get_color_index(), spc.line_color.color);
221 line_prog.set_attribute(ctx, "secondary_color", spc.line_halo_color.color);
222 line_prog.set_attribute(ctx, "size", spc.line_width.size);
223 line_prog.enable(ctx);
224 if (strips[i].empty())
225 draw_sub_plot_samples(count, spc, true);
226 else {
227 unsigned fst = 0;
228 for (unsigned j = 0; j < strips[i].size(); ++j) {
229 glDrawArrays(GL_LINE_STRIP, fst, strips[i][j]);
230 fst += strips[i][j];
231 }
232 }
233 line_prog.disable(ctx);
234 result = true;
235 }
236 }
237 disable_attributes(ctx, i);
238 return result;
239}
240bool plot2d::draw_stick_plot(cgv::render::context& ctx, int i, int layer_idx)
241{
242 // skip unvisible and empty sub plots
243 if (!ref_sub_plot2d_config(i).show_plot)
244 return false;
245 GLsizei count = (GLsizei)enable_attributes(ctx, i, samples);
246 bool result = false;
247 if (count > 0) {
248 const plot2d_config& spc = ref_sub_plot2d_config(i);
249 if (spc.show_sticks && rectangle_prog.is_linked()) {
250 // configure vertex shader
251 rectangle_prog.set_uniform(ctx, "sub_plot_delta", float(i) * sub_plot_delta);
252 rectangle_prog.set_uniform(ctx, "color_index", spc.stick_color.color_idx);
253 rectangle_prog.set_uniform(ctx, "secondary_color_index", -1);
254 rectangle_prog.set_uniform(ctx, "opacity_index", spc.stick_color.opacity_idx);
255 rectangle_prog.set_uniform(ctx, "secondary_opacity_index", -1);
256 rectangle_prog.set_uniform(ctx, "size_index", spc.stick_width.size_idx);
257 rectangle_prog.set_uniform(ctx, "secondary_size_index", -1);
258 rectangle_prog.set_uniform(ctx, "rectangle_base_window", spc.stick_base_window);
259 rectangle_prog.set_uniform(ctx, "rectangle_coordinate_index", spc.stick_coordinate_index);
260 rectangle_prog.set_uniform(ctx, "rectangle_border_width", 0.0f);
261 rectangle_prog.set_attribute(ctx, rectangle_prog.get_color_index(), spc.stick_color.color);
262 rectangle_prog.set_attribute(ctx, "size", spc.stick_width.size * get_domain_config_ptr()->reference_size);
263 rectangle_prog.set_attribute(ctx, "depth_offset", -layer_idx * layer_depth);
264 // configure geometry shader
265 rectangle_prog.set_uniform(ctx, "border_mode", spc.stick_coordinate_index == 0 ? 2 : 1);
266
267 rectangle_prog.enable(ctx);
268 draw_sub_plot_samples(count, spc);
269 rectangle_prog.disable(ctx);
270 result = true;
271 }
272 }
273 disable_attributes(ctx, i);
274 return result;
275}
276void plot2d::configure_bar_plot(cgv::render::context& ctx)
277{
278 // prepare rectangle program to draw bar and stick plots
279 set_plot_uniforms(ctx, rectangle_prog);
280 set_mapping_uniforms(ctx, rectangle_prog);
281 // configure geometry shader
282 rectangle_prog.set_uniform(ctx, "pixel_blend", 2.0f);
283 rectangle_prog.set_uniform(ctx, "viewport_height", (float)ctx.get_height());
284 // configure fragment shader
285 rectangle_prog.set_uniform(ctx, "use_texture", false);
286 // configure side shader
287 rectangle_prog.set_uniform(ctx, "culling_mode", 0);
288 rectangle_prog.set_uniform(ctx, "map_color_to_material", 7);
289 rectangle_prog.set_uniform(ctx, "illumination_mode", 0);
290}
291
292bool plot2d::draw_bar_plot(cgv::render::context& ctx, int i, int layer_idx)
293{
294 // skip unvisible and empty sub plots
295 if (!ref_sub_plot2d_config(i).show_plot)
296 return false;
297 bool result = false;
298 GLsizei count = (GLsizei)enable_attributes(ctx, i, samples);
299 if (count > 0) {
300 const plot2d_config& spc = ref_sub_plot2d_config(i);
301 if (spc.show_bars && rectangle_prog.is_linked()) {
302 // configure vertex shader
303 rectangle_prog.set_uniform(ctx, "sub_plot_delta", float(i) * sub_plot_delta);
304 rectangle_prog.set_uniform(ctx, "color_index", spc.bar_color.color_idx);
305 rectangle_prog.set_uniform(ctx, "secondary_color_index", spc.bar_outline_color.color_idx);
306 rectangle_prog.set_uniform(ctx, "opacity_index", spc.bar_color.opacity_idx);
307 rectangle_prog.set_uniform(ctx, "secondary_opacity_index", spc.bar_outline_color.opacity_idx);
308 rectangle_prog.set_uniform(ctx, "size_index", spc.bar_percentual_width.size_idx);
309 rectangle_prog.set_uniform(ctx, "secondary_size_index", spc.bar_outline_width.size_idx);
310 rectangle_prog.set_uniform(ctx, "rectangle_base_window", spc.bar_base_window);
311 rectangle_prog.set_uniform(ctx, "rectangle_coordinate_index", spc.bar_coordinate_index);
312 rectangle_prog.set_uniform(ctx, "rectangle_border_width", spc.bar_outline_width.size * get_domain_config_ptr()->reference_size);
313 rectangle_prog.set_attribute(ctx, rectangle_prog.get_color_index(), spc.bar_color.color);
314 rectangle_prog.set_attribute(ctx, "secondary_color", spc.bar_outline_color.color);
315 rectangle_prog.set_attribute(ctx, "size", (count > 1 ? extent[0] / (count - 1) : extent[0]) * spc.bar_percentual_width.size);
316 rectangle_prog.set_attribute(ctx, "depth_offset", -layer_idx * layer_depth);
317 // configure geometry shader
318 rectangle_prog.set_uniform(ctx, "border_mode", spc.bar_coordinate_index == 0 ? 2 : 1);
319
320 rectangle_prog.enable(ctx);
321 draw_sub_plot_samples(count, spc);
322 rectangle_prog.disable(ctx);
323 result = true;
324 }
325 }
326 disable_attributes(ctx, i);
327 return result;
328}
329
330int plot2d::draw_sub_plots_jointly(cgv::render::context& ctx, int layer_idx)
331{
332 // first draw all bar plots
333 unsigned i;
334 for (i = 0; i < get_nr_sub_plots(); ++i) {
335 draw_bar_plot(ctx, i, layer_idx);
336 ++layer_idx;
337 }
338 // next draw stick plots
339 for (i = 0; i < get_nr_sub_plots(); ++i) {
340 draw_stick_plot(ctx, i, layer_idx);
341 ++layer_idx;
342 }
343 // then we draw line plots
344 for (i = 0; i < get_nr_sub_plots(); ++i) {
345 draw_line_plot(ctx, i, layer_idx);
346 ++layer_idx;
347 }
348 // finally draw point plots
349 for (i = 0; i < get_nr_sub_plots(); ++i) {
350 draw_point_plot(ctx, i, layer_idx);
351 ++layer_idx;
352 }
353 return layer_idx;
354}
355
356void plot2d::extract_domain_rectangles(std::vector<box2>& R, std::vector<rgb>& C, std::vector<float>& D)
357{
358 R.resize(5);
359 C.resize(5);
360 D.resize(5);
361 vec2 extent = vec2(this->extent[0], this->extent[1]);
362 float rs = get_domain_config_ptr()->reference_size;
363 R[0] = box2(-0.5f * extent, 0.5f * extent);
364 R[0].add_point(0.5f * extent + float(get_nr_sub_plots() - 1) * vec2(sub_plot_delta[0], sub_plot_delta[1]));
365 C[0] = get_domain_config_ptr()->color;
366 D[0] = 0.0f;
367 for (unsigned ai = 0; ai < 2; ++ai) {
368 axis_config& ac = get_domain_config_ptr()->axis_configs[ai];
369 axis_config& ao = get_domain_config_ptr()->axis_configs[1 - ai];
370 float lw = rs * ac.line_width;
371 rgb col = get_domain_config_ptr()->fill ? ac.color : get_domain_config_ptr()->color;
372 vec2 axis_extent(0.0f);
373 axis_extent[ai] = extent[ai];
374 axis_extent[1 - ai] = lw;
375 box2 axis_box(-0.5f * axis_extent, 0.5f * axis_extent);
376 axis_box.ref_min_pnt()[1 - ai] += 0.5f * (extent[1 - ai] - lw);
377 axis_box.ref_max_pnt()[1 - ai] += 0.5f * (extent[1 - ai] - lw);
378 R[2 * ai + 1] = axis_box;
379 C[2 * ai + 1] = col;
380 D[2 * ai + 1] = -layer_depth;
381 axis_box.ref_min_pnt()[1 - ai] -= extent[1 - ai] - lw;
382 axis_box.ref_max_pnt()[1 - ai] -= extent[1 - ai] - lw;
383 R[2 * ai + 2] = axis_box;
384 C[2 * ai + 2] = col;
385 D[2 * ai + 2] = -layer_depth;
386 // axis line
387 if (ao.get_attribute_min() < 0 && ao.get_attribute_max() > 0) {
388 float axis_plot = ao.plot_space_from_attribute_space(0.0f);
389 axis_box.ref_min_pnt()[1 - ai] = axis_plot - 0.5f * lw;
390 axis_box.ref_max_pnt()[1 - ai] = axis_plot + 0.5f * lw;
391 R.push_back(axis_box);
392 C.push_back(ac.color);
393 D.push_back(-2 * layer_depth);
394 }
395 }
396}
397
398/*
399bool plot2d::extract_tick_rectangles_and_tick_labels(
400 std::vector<box2>& R, std::vector<rgb>& C, std::vector<float>& D,
401 std::vector<label_info>& tick_labels, int ai, int ti, float he, float z_plot)
402{
403 axis_config& ac = get_domain_config_ptr()->axis_configs[ai];
404 float acw = 1.5f * ac.line_width * get_domain_config_ptr()->reference_size;
405 tick_config& tc = ti == 0 ? ac.primary_ticks : ac.secondary_ticks;
406 if (tc.type == TT_NONE)
407 return false;
408 float min_tick = ac.tick_space_from_attribute_space(ac.get_attribute_min());
409 float max_tick = ac.tick_space_from_attribute_space(ac.get_attribute_max());
410 int min_i = (int)ceil(min_tick / tc.step - std::numeric_limits<float>::epsilon());
411 int max_i = (int)((max_tick - fmod(max_tick, tc.step)) / tc.step);
412 // ignore secondary ticks on domain boundary
413 if (ti == 1 && min_i * tc.step - min_tick < std::numeric_limits<float>::epsilon())
414 ++min_i;
415 if (ti == 1 && max_i * tc.step - max_tick > -std::numeric_limits<float>::epsilon())
416 --max_i;
417 float lw = 0.5f * get_domain_config_ptr()->reference_size * tc.line_width;
418 float dl = 0.5f * get_domain_config_ptr()->reference_size * tc.length;
419 float d = -int(ti + 3) * layer_depth;
420 for (int i = min_i; i <= max_i; ++i) {
421 float c_tick = (float)(i * tc.step);
422 float c_attr = ac.attribute_space_from_tick_space(c_tick);
423 // ignore secondary ticks on axes
424 if (fabs(c_attr) < std::numeric_limits<float>::epsilon())
425 continue;
426 // ignore secondary ticks on primary ticks
427 if (ti == 1 && fabs(fmod(c_tick, ac.primary_ticks.step)) < 0.00001f)
428 continue;
429 std::string label_str;
430 if (tc.label)
431 label_str = cgv::utils::to_string(c_attr);
432 float c_plot = ac.plot_space_from_window_space(ac.window_space_from_tick_space(c_tick));
433 vec2 mn, mx;
434 mn[ai] = c_plot - lw;
435 mx[ai] = c_plot + lw;
436 switch (tc.type) {
437 case TT_DASH:
438 mn[1 - ai] = -he;
439 mx[1 - ai] = -he + dl;
440 R.push_back(box2(mn, mx));
441 C.push_back(ac.color);
442 D.push_back(d);
443 if (!label_str.empty()) {
444 mx[1 - ai] += acw;
445 tick_labels.push_back(label_info(mx.to_vec(), label_str, ai == 0 ? cgv::render::TA_BOTTOM : cgv::render::TA_LEFT));
446 if (ti == 1)
447 tick_labels.back().scale = 0.75f;
448 }
449 mn[1 - ai] = he - dl;
450 mx[1 - ai] = he;
451 R.push_back(box2(mn, mx));
452 C.push_back(ac.color);
453 D.push_back(d);
454 if (!label_str.empty()) {
455 mn[1 - ai] -= acw;
456 tick_labels.push_back(label_info(mn.to_vec(), label_str, ai == 0 ? cgv::render::TA_TOP : cgv::render::TA_RIGHT));
457 if (ti == 1)
458 tick_labels.back().scale = 0.75f;
459 }
460 if (z_plot != std::numeric_limits<float>::quiet_NaN()) {
461 mn[1 - ai] = z_plot - dl;
462 mx[1 - ai] = z_plot + dl;
463 R.push_back(box2(mn, mx));
464 C.push_back(ac.color);
465 D.push_back(d);
466 }
467 break;
468 case TT_LINE:
469 case TT_PLANE:
470 mn[1 - ai] = -he;
471 mx[1 - ai] = he;
472 R.push_back(box2(mn, mx));
473 C.push_back(ac.color);
474 D.push_back(d);
475 if (!label_str.empty()) {
476 mn[1 - ai] += acw;
477 mx[1 - ai] -= acw;
478 mn[ai] += 2.5f * lw;
479 mx[ai] += 2.5f * lw;
480 tick_labels.push_back(label_info(mx.to_vec(), label_str, cgv::render::TextAlignment(ai == 0 ? cgv::render::TA_TOP + cgv::render::TA_LEFT : cgv::render::TA_RIGHT + cgv::render::TA_BOTTOM)));
481 if (ti == 1)
482 tick_labels.back().scale = 0.75f;
483 tick_labels.push_back(label_info(mn.to_vec(), label_str, cgv::render::TextAlignment(ai == 0 ? cgv::render::TA_BOTTOM + cgv::render::TA_LEFT : cgv::render::TA_LEFT + cgv::render::TA_BOTTOM)));
484 if (ti == 1)
485 tick_labels.back().scale = 0.75f;
486 }
487 break;
488 }
489 }
490 return true;
491}
492*/
493
494void plot2d::extract_domain_tick_rectangles_and_tick_labels(
495 std::vector<box2>& R, std::vector<rgb>& C, std::vector<float>& D,
496 std::vector<label_info>& tick_labels, std::vector<tick_batch_info>& tick_batches)
497{
498 vecn E = get_extent();
499 set_extent(vecn(2, &extent[0]));
500 tick_labels.clear();
501 tick_batches.clear();
502 for (unsigned ti = 0; ti < 2; ++ti) {
503 for (unsigned ai = 0; ai < 2; ++ai) {
504 axis_config& ac = get_domain_config_ptr()->axis_configs[ai];
505 axis_config& ao = get_domain_config_ptr()->axis_configs[1 - ai];
506 float z_plot = (ao.get_attribute_min() < 0.0f && ao.get_attribute_max() > 0.0f) ?
507 ao.plot_space_from_attribute_space(0.0f) : std::numeric_limits<float>::quiet_NaN();
508 tick_batch_info tbi(ai, 1 - ai, ti == 0, 0, (unsigned)tick_labels.size());
509 if (extract_tick_rectangles_and_tick_labels(R, C, D, tick_labels, ai, ai, ti, 0.5f * ao.extent, z_plot, 1.0f, vec2(0.0f), -3 * layer_depth, ac.multi_axis_ticks)) {
510 if ((tbi.label_count = (unsigned)(tick_labels.size() - tbi.first_label)) > 0)
511 tick_batches.push_back(tbi);
512 }
513 }
514 }
515 set_extent(E);
516}
517
518void plot2d::draw_domain(cgv::render::context& ctx, int si, bool no_fill)
519{
520 std::vector<box2> R;
521 std::vector<rgb> C;
522 std::vector<float> D;
523 extract_domain_rectangles(R, C, D);
524 extract_domain_tick_rectangles_and_tick_labels(R, C, D, tick_labels, tick_batches);
525 draw_rectangles(ctx, aam_domain, R, C, D, (get_domain_config_ptr()->fill && !no_fill) ? 0 : 1);
526 draw_title(ctx, vec2::from_vec(get_domain_config_ptr()->title_pos), -3 * layer_depth, si);
527 draw_tick_labels(ctx, aam_domain_tick_labels, tick_labels, tick_batches, -4 * layer_depth);
528}
529
530void plot2d::draw(cgv::render::context& ctx)
531{
532 prepare_extents();
533
534 // store to be changed opengl state
535 GLboolean line_smooth = glIsEnabled(GL_LINE_SMOOTH);
536 GLboolean blend = glIsEnabled(GL_BLEND);
537 GLboolean cull_face = glIsEnabled(GL_CULL_FACE);
538 GLenum blend_src, blend_dst, depth;
539 glGetIntegerv(GL_BLEND_DST, reinterpret_cast<GLint*>(&blend_dst));
540 glGetIntegerv(GL_BLEND_SRC, reinterpret_cast<GLint*>(&blend_src));
541 glGetIntegerv(GL_DEPTH_FUNC, reinterpret_cast<GLint*>(&depth));
542
543 // update state
544 glEnable(GL_LINE_SMOOTH);
545 glDisable(GL_CULL_FACE);
546 glEnable(GL_BLEND);
547 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
548 glDepthFunc(GL_LEQUAL);
549
550 // place plot with modelview matrix
552 mat4 R;
553 orientation.put_homogeneous_matrix(R);
554 ctx.mul_modelview_matrix(cgv::math::translate4<float>(center_location) * R);
555
556 // configure bar prog only once
557 configure_bar_plot(ctx);
558 // draw all subplots jointly in one plane
559 if (sub_plot_delta[2] == 0.0f) {
560 if (get_domain_config_ptr()->show_domain)
561 draw_domain(ctx);
562 if (legend_components != LC_HIDDEN)
563 draw_legend(ctx, 5);
564 if (disable_depth_mask)
565 glDepthMask(GL_FALSE);
566 else
567 glDepthFunc(GL_LEQUAL);
568 draw_sub_plots_jointly(ctx, 8);
569 if (disable_depth_mask)
570 glDepthMask(GL_TRUE);
571 else
572 glDepthFunc(GL_LESS);
573 }
574 // draw subplots with offset in back to front order
575 else {
576 auto M = ctx.get_modelview_matrix();
577 double plot_pos_eye_z = M(2, 3);
578 double plot_z_eye_z = M(2, 2);
579 int i_begin = 0, i_end = get_nr_sub_plots(), i_delta = 1;
580 // check if we can not use default order
581 if (plot_pos_eye_z * plot_z_eye_z * sub_plot_delta[2] > 0) {
582 i_begin = i_end - 1;
583 i_end = -1;
584 i_delta = -1;
585 ctx.mul_modelview_matrix(cgv::math::translate4<float>(vec3(0, 0, (get_nr_sub_plots()-1) * sub_plot_delta[2])));
586 }
587 // traverse all subplots in back to front order
588 bool fst = true;
589 unsigned ai;
590 for (ai=0; ai<2+nr_attributes; ++ai)
591 if (multi_axis_modes[ai])
592 get_domain_config_ptr()->axis_configs[ai].backup_attribute_range();
593 for (int i = i_begin; i != i_end; i += i_delta) {
594 // adapt all axes in multi axis mode
595 for (ai = 0; ai < 2 + nr_attributes; ++ai) {
596 if (multi_axis_modes[ai]) {
597 auto& ac = get_domain_config_ptr()->axis_configs[ai];
598 float min_val, max_val;
599 ac.put_backup_attribute_range(min_val, max_val);
600 if (determine_axis_extent_from_subplot(ai, i, min_val, max_val)) {
601 if (fabs(max_val - min_val) < 10.0f * std::numeric_limits<float>::epsilon())
602 max_val = min_val + 1;
603 ac.set_attribute_range(min_val, max_val);
604 }
605
606 }
607 }
608 if (get_domain_config_ptr()->show_domain)
609 draw_domain(ctx, i, !fst);
610 if (legend_components != LC_HIDDEN)
611 draw_legend(ctx, 5, i == i_begin, multi_axis_modes);
612 if (disable_depth_mask)
613 glDepthMask(GL_FALSE);
614 else
615 glDepthFunc(GL_LEQUAL);
616 draw_bar_plot(ctx, i, 8);
617 draw_stick_plot(ctx, i, 9);
618 draw_line_plot(ctx, i, 10);
619 draw_point_plot(ctx, i, 11);
620 if (disable_depth_mask)
621 glDepthMask(GL_TRUE);
622 else
623 glDepthFunc(GL_LESS);
624 ctx.mul_modelview_matrix(cgv::math::translate4<float>(vec3(0, 0, i_delta* sub_plot_delta[2])));
625 fst = false;
626 }
627 for (ai = 0; ai < 2 + nr_attributes; ++ai)
628 if (multi_axis_modes[ai])
629 get_domain_config_ptr()->axis_configs[ai].restore_attribute_range();
630 }
631
633
634 // recover opengl state
635 if (!line_smooth)
636 glDisable(GL_LINE_SMOOTH);
637 if (!blend)
638 glDisable(GL_BLEND);
639 if (cull_face)
640 glEnable(GL_CULL_FACE);
641 glDepthFunc(depth);
642 glBlendFunc(blend_src, blend_dst);
643}
644
646void plot2d::create_point_config_gui(cgv::base::base* bp, cgv::gui::provider& p, plot_base_config& pbc)
647{
648 auto& p2dc = reinterpret_cast<plot2d_config&>(pbc);
649 plot_base::create_point_config_gui(bp, p, pbc);
650}
651
653void plot2d::create_stick_config_gui(cgv::base::base* bp, cgv::gui::provider& p, plot_base_config& pbc)
654{
655 auto& p2dc = reinterpret_cast<plot2d_config&>(pbc);
656 plot_base::create_stick_config_gui(bp, p, pbc);
657}
658
660void plot2d::create_bar_config_gui(cgv::base::base* bp, cgv::gui::provider& p, plot_base_config& pbc)
661{
662 auto& p2dc = reinterpret_cast<plot2d_config&>(pbc);
663 plot_base::create_bar_config_gui(bp, p, pbc);
664}
665
666void plot2d::create_config_gui(cgv::base::base* bp, cgv::gui::provider& p, unsigned i)
667{
668 plot_base::create_config_gui(bp, p, i);
669}
670
671void plot2d::create_gui(cgv::base::base* bp, cgv::gui::provider& p)
672{
673 if (p.begin_tree_node("Multi Modes", multi_axis_modes, false, "level=3")) {
674 p.align("\a");
675 for (unsigned ai = 0; ai < 2 + nr_attributes; ++ai)
676 p.add_member_control(bp, get_domain_config_ptr()->axis_configs[ai].name, multi_axis_modes[ai], "toggle");
677 p.align("\b");
678 p.end_tree_node(rrs);
679 }
680 p.add_member_control(bp, "Delta X", sub_plot_delta[0], "value_slider", "min=-1;max=1;step=0.001;ticks=true");
681 p.add_member_control(bp, "Delta Y", sub_plot_delta[1], "value_slider", "min=-1;max=1;step=0.001;ticks=true");
682 p.add_member_control(bp, "Delta Z", sub_plot_delta[2], "value_slider", "min=-1;max=1;step=0.02;ticks=true");
683 plot_base::create_gui(bp, p);
684 p.add_member_control(bp, "Disable Depth Mask", disable_depth_mask, "toggle");
685}
686
687 }
688}
base class for all classes that can be registered with support for dynamic properties (see also secti...
Definition base.h:75
derive from this class to provide a gui to the current viewer
Definition provider.h:64
void align(const std::string &_align)
send pure alignment information
Definition provider.cxx:36
bool begin_tree_node(const std::string &label, const T &value, bool initial_visibility=false, const std::string &options="", gui_group_ptr ggp=gui_group_ptr())
Begin a sub tree of a tree structured gui.
Definition provider.h:212
data::ref_ptr< control< T > > add_member_control(cgv::base::base *base_ptr, const std::string &label, T &value, const std::string &gui_type="", const std::string &options="", const std::string &align="\n")
add control with callback to cgv::base::on_set method on cgv::gui::control::value_change
Definition provider.h:137
void end_tree_node(const T &value)
template specialization that allows to specify value reference plus node_instance by using the result...
Definition provider.h:222
static fvec< float, N > from_vec(const vec< float > &)
conversion from vector
Definition fvec.h:738
color()
standard constructor does not initialize components
Definition color.h:589
std::vector< std::vector< unsigned > > strips
allow to split series into connected strips that are represented by the number of contained samples
Definition plot2d.h:49
std::vector< vec2 > & ref_sub_plot_samples(unsigned i=0)
return the samples of the i-th sub plot
Definition plot2d.cxx:106
cgv::render::attribute_array_manager aam_domain
attribute managers for domain rectangles and domain tick labels
Definition plot2d.h:51
void delete_sub_plot(unsigned i)
delete the i-th sub plot
Definition plot2d.cxx:91
std::vector< unsigned > & ref_sub_plot_strips(unsigned i=0)
return the strip definition of the i-th sub plot
Definition plot2d.cxx:112
plot2d_config & ref_sub_plot2d_config(unsigned i=0)
return a reference to the plot1d configuration of the i-th plot
Definition plot2d.cxx:101
bool * multi_axis_modes
whether to manage separate axes for each sub plot
Definition plot2d.h:55
vec3 sub_plot_delta
offset in between sub plots in x, y and z direction
Definition plot2d.h:57
unsigned add_sub_plot(const std::string &name)
add sub plot and return sub plot index
Definition plot2d.cxx:68
plot2d(const std::string &title, unsigned nr_attributes=0)
construct 2D plot with given number of additional attributes and default parameters
Definition plot2d.cxx:24
std::vector< std::vector< vec2 > > samples
store 2d samples for data series
Definition plot2d.h:47
bool init(cgv::render::context &ctx)
construct shader programs
Definition plot2d.cxx:117
base class for plot2d and plot3d, which can have several sub plots each
Definition plot_base.h:282
const domain_config * get_domain_config_ptr() const
return const pointer to domain configuration
unsigned get_nr_sub_plots() const
return current number of sub plots
std::vector< attribute_source_array > attribute_source_arrays
attribute sources
Definition plot_base.h:429
std::vector< plot_base_config * > configs
store one configuration per sub plot
Definition plot_base.h:348
plot_base_config & ref_sub_plot_config(unsigned i)
return a reference to the plot base configuration of the i-th plot
unsigned nr_attributes
number of additional attributes
Definition plot_base.h:342
bool init(cgv::render::context &ctx)
build legend prog and create aab
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
virtual void mul_modelview_matrix(const dmat4 &MV)
multiply given matrix from right to current modelview matrix
Definition context.cxx:1808
virtual unsigned int get_height() const =0
return the height of the window
virtual dmat4 get_modelview_matrix() const =0
return homogeneous 4x4 viewing matrix, which transforms from world to eye space
void pop_modelview_matrix()
see push_V for an explanation
Definition context.cxx:1814
void push_modelview_matrix()
push the current viewing matrix onto a matrix stack for viewing matrices.
Definition context.cxx:1802
bool build_program(const context &ctx, const std::string &file_name, bool show_error=false, const shader_define_map &defines=shader_define_map())
successively calls create, attach_program and link.
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
the cgv namespace
Definition print.h:11
cgv::math::vec< float > vecn
declare type of single precision floating point vector with varying dimension
Definition vec.h:1187
cgv::media::color< float, cgv::media::RGB > rgb
declare rgb color type with 32 bit components
Definition color.h:853
cgv::math::fvec< float, 2 > vec2
declare type of 2d single precision floating point vectors
Definition fvec.h:667
cgv::media::axis_aligned_box< float, 2 > box2
declare type of 2d single precision floating point axis-aligned boxes
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Definition fvec.h:669
struct that manages attribute sources and corresponding gpu objects per subplot
Definition plot_base.h:252
store source of a single plot attribute (one coordinate axis or one float attribute)
Definition plot_base.h:228
std::string title
plot title
Definition plot_base.h:40
std::vector< axis_config > axis_configs
store a vector of axis configurations (2/3 for plot2/3d plus several attribute axes)
Definition plot_base.h:56
extend common plot configuration with parameters specific to 2d plot
Definition plot2d.h:15
void configure_chart(ChartType chart_type)
configure the sub plot to a specific chart type
Definition plot2d.cxx:17
plot2d_config(const std::string &_name)
set default values
Definition plot2d.cxx:11
plot independent configuration parameters of one sub plot in a 2d or 3d plot
Definition plot_base.h:130
virtual void configure_chart(ChartType chart_type)
configure the sub plot to a specific chart type
Definition plot_base.cxx:68
std::string name
name of sub plot
Definition plot_base.h:132
bool show_lines
whether to connect data points with lines
Definition plot_base.h:164
bool show_points
whether to show data points
Definition plot_base.h:152
mapped_rgba point_color
point color
Definition plot_base.h:157
mapped_size point_halo_width
width of point halo in pixel
Definition plot_base.h:159
mapped_size point_size
point size in pixels
Definition plot_base.h:154
mapped_rgba point_halo_color
color of point halo
Definition plot_base.h:161