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
135 options.defines["PLOT_MODE"] = "1";
136 if (!rectangle_prog.build_program(ctx, "plot2d_rect.glpr", options, true)) {
137 std::cerr << "could not build GLSL program from plot2d_rect.glpr" << std::endl;
138 return false;
139 }
140 else
141 rectangle_prog.allow_context_to_set_color(false);
142 return plot_base::init(ctx);
143}
144
146{
147 aam_domain.destruct(ctx);
148 aam_domain_tick_labels.destruct(ctx);
149 point_prog.destruct(ctx);
150 line_prog.destruct(ctx);
151 rectangle_prog.destruct(ctx);
152 plot_base::clear(ctx);
153}
154
155bool plot2d::draw_point_plot(cgv::render::context& ctx, int i, int layer_idx)
156{
157 // skip unvisible and empty sub plots
158 if (!ref_sub_plot2d_config(i).show_plot)
159 return false;
160 GLsizei count = (GLsizei)enable_attributes(ctx, i, samples);
161 bool result = false;
162 if (count > 0) {
163 const plot2d_config& spc = ref_sub_plot2d_config(i);
164 if (spc.show_points && point_prog.is_linked()) {
165 set_plot_uniforms(ctx, point_prog);
166 set_mapping_uniforms(ctx, point_prog);
167 point_prog.set_uniform(ctx, "sub_plot_delta", float(i) * sub_plot_delta);
168 point_prog.set_uniform(ctx, "depth_offset", -layer_idx * layer_depth);
169 point_prog.set_uniform(ctx, "reference_point_size", get_domain_config_ptr()->reference_size);
170 point_prog.set_uniform(ctx, "blend_width_in_pixel", get_domain_config_ptr()->blend_width_in_pixel);
171 point_prog.set_uniform(ctx, "halo_width_in_pixel", 0.0f);
172 point_prog.set_uniform(ctx, "percentual_halo_width", spc.point_halo_width.size / spc.point_size.size);
173 point_prog.set_uniform(ctx, "viewport_height", (float)ctx.get_height());
174 point_prog.set_uniform(ctx, "measure_point_size_in_pixel", false);
175 point_prog.set_uniform(ctx, "screen_aligned", false);
176 point_prog.set_uniform(ctx, "color_index", spc.point_color.color_idx);
177 point_prog.set_uniform(ctx, "secondary_color_index", spc.point_halo_color.color_idx);
178 point_prog.set_uniform(ctx, "opacity_index", spc.point_color.opacity_idx);
179 point_prog.set_uniform(ctx, "secondary_opacity_index", spc.point_halo_color.opacity_idx);
180 point_prog.set_uniform(ctx, "size_index", spc.point_size.size_idx);
181 point_prog.set_uniform(ctx, "secondary_size_index", spc.point_halo_width.size_idx);
182 point_prog.set_attribute(ctx, point_prog.get_color_index(), spc.point_color.color);
183 point_prog.set_attribute(ctx, "secondary_color", spc.point_halo_color.color);
184 point_prog.set_attribute(ctx, "size", spc.point_size.size);
185 point_prog.enable(ctx);
186 draw_sub_plot_samples(count, spc);
187 point_prog.disable(ctx);
188 result = true;
189 }
190 }
191 disable_attributes(ctx, i);
192 return result;
193}
194bool plot2d::draw_line_plot(cgv::render::context& ctx, int i, int layer_idx)
195{
196 // skip unvisible and empty sub plots
197 if (!ref_sub_plot2d_config(i).show_plot)
198 return false;
199 GLsizei count = (GLsizei)enable_attributes(ctx, i, samples);
200 bool result = false;
201 if (count > 0) {
202 const plot2d_config& spc = ref_sub_plot2d_config(i);
203 if (spc.show_lines && line_prog.is_linked()) {
204 set_plot_uniforms(ctx, line_prog);
205 set_mapping_uniforms(ctx, line_prog);
206 line_prog.set_uniform(ctx, "sub_plot_delta", float(i) * sub_plot_delta);
207 line_prog.set_uniform(ctx, "depth_offset", -layer_idx * layer_depth);
208 line_prog.set_uniform(ctx, "reference_line_width", get_domain_config_ptr()->reference_size);
209 line_prog.set_uniform(ctx, "blend_width_in_pixel", get_domain_config_ptr()->blend_width_in_pixel);
210 line_prog.set_uniform(ctx, "halo_width_in_pixel", 0.0f);
211 line_prog.set_uniform(ctx, "halo_color_strength", 1.0f);
212 line_prog.set_uniform(ctx, "halo_color", spc.line_halo_color.color);
213 line_prog.set_uniform(ctx, "percentual_halo_width", spc.line_halo_width.size / spc.line_width.size);
214 line_prog.set_uniform(ctx, "viewport_height", (float)ctx.get_height());
215 line_prog.set_uniform(ctx, "measure_line_width_in_pixel", false);
216 line_prog.set_uniform(ctx, "screen_aligned", false);
217 line_prog.set_uniform(ctx, "color_index", spc.line_color.color_idx);
218 line_prog.set_uniform(ctx, "secondary_size_index", spc.line_halo_width.size_idx);
219 line_prog.set_uniform(ctx, "secondary_color_index", spc.line_halo_color.color_idx);
220 line_prog.set_uniform(ctx, "opacity_index", spc.line_color.opacity_idx);
221 line_prog.set_uniform(ctx, "secondary_opacity_index", spc.line_halo_color.opacity_idx);
222 line_prog.set_uniform(ctx, "size_index", spc.line_width.size_idx);
223 line_prog.set_attribute(ctx, line_prog.get_color_index(), spc.line_color.color);
224 line_prog.set_attribute(ctx, "secondary_color", spc.line_halo_color.color);
225 line_prog.set_attribute(ctx, "size", spc.line_width.size);
226 line_prog.enable(ctx);
227 if (strips[i].empty())
228 draw_sub_plot_samples(count, spc, true);
229 else {
230 unsigned fst = 0;
231 for (unsigned j = 0; j < strips[i].size(); ++j) {
232 glDrawArrays(GL_LINE_STRIP, fst, strips[i][j]);
233 fst += strips[i][j];
234 }
235 }
236 line_prog.disable(ctx);
237 result = true;
238 }
239 }
240 disable_attributes(ctx, i);
241 return result;
242}
243bool plot2d::draw_stick_plot(cgv::render::context& ctx, int i, int layer_idx)
244{
245 // skip unvisible and empty sub plots
246 if (!ref_sub_plot2d_config(i).show_plot)
247 return false;
248 GLsizei count = (GLsizei)enable_attributes(ctx, i, samples);
249 bool result = false;
250 if (count > 0) {
251 const plot2d_config& spc = ref_sub_plot2d_config(i);
252 if (spc.show_sticks && rectangle_prog.is_linked()) {
253 // configure vertex shader
254 rectangle_prog.set_uniform(ctx, "sub_plot_delta", float(i) * sub_plot_delta);
255 rectangle_prog.set_uniform(ctx, "color_index", spc.stick_color.color_idx);
256 rectangle_prog.set_uniform(ctx, "secondary_color_index", -1);
257 rectangle_prog.set_uniform(ctx, "opacity_index", spc.stick_color.opacity_idx);
258 rectangle_prog.set_uniform(ctx, "secondary_opacity_index", -1);
259 rectangle_prog.set_uniform(ctx, "size_index", spc.stick_width.size_idx);
260 rectangle_prog.set_uniform(ctx, "secondary_size_index", -1);
261 rectangle_prog.set_uniform(ctx, "rectangle_base_window", spc.stick_base_window);
262 rectangle_prog.set_uniform(ctx, "rectangle_coordinate_index", spc.stick_coordinate_index);
263 rectangle_prog.set_uniform(ctx, "rectangle_border_width", 0.0f);
264 rectangle_prog.set_attribute(ctx, rectangle_prog.get_color_index(), spc.stick_color.color);
265 rectangle_prog.set_attribute(ctx, "size", spc.stick_width.size * get_domain_config_ptr()->reference_size);
266 rectangle_prog.set_attribute(ctx, "depth_offset", -layer_idx * layer_depth);
267 // configure geometry shader
268 rectangle_prog.set_uniform(ctx, "border_mode", spc.stick_coordinate_index == 0 ? 2 : 1);
269
270 rectangle_prog.enable(ctx);
271 draw_sub_plot_samples(count, spc);
272 rectangle_prog.disable(ctx);
273 result = true;
274 }
275 }
276 disable_attributes(ctx, i);
277 return result;
278}
279void plot2d::configure_bar_plot(cgv::render::context& ctx)
280{
281 // prepare rectangle program to draw bar and stick plots
282 set_plot_uniforms(ctx, rectangle_prog);
283 set_mapping_uniforms(ctx, rectangle_prog);
284 // configure geometry shader
285 rectangle_prog.set_uniform(ctx, "pixel_blend", 2.0f);
286 rectangle_prog.set_uniform(ctx, "viewport_height", (float)ctx.get_height());
287 // configure fragment shader
288 rectangle_prog.set_uniform(ctx, "use_texture", false);
289 // configure side shader
290 rectangle_prog.set_uniform(ctx, "culling_mode", 0);
291 rectangle_prog.set_uniform(ctx, "map_color_to_material", 7);
292 rectangle_prog.set_uniform(ctx, "illumination_mode", 0);
293}
294
295bool plot2d::draw_bar_plot(cgv::render::context& ctx, int i, int layer_idx)
296{
297 // skip unvisible and empty sub plots
298 if (!ref_sub_plot2d_config(i).show_plot)
299 return false;
300 bool result = false;
301 GLsizei count = (GLsizei)enable_attributes(ctx, i, samples);
302 if (count > 0) {
303 const plot2d_config& spc = ref_sub_plot2d_config(i);
304 if (spc.show_bars && rectangle_prog.is_linked()) {
305 // configure vertex shader
306 rectangle_prog.set_uniform(ctx, "sub_plot_delta", float(i) * sub_plot_delta);
307 rectangle_prog.set_uniform(ctx, "color_index", spc.bar_color.color_idx);
308 rectangle_prog.set_uniform(ctx, "secondary_color_index", spc.bar_outline_color.color_idx);
309 rectangle_prog.set_uniform(ctx, "opacity_index", spc.bar_color.opacity_idx);
310 rectangle_prog.set_uniform(ctx, "secondary_opacity_index", spc.bar_outline_color.opacity_idx);
311 rectangle_prog.set_uniform(ctx, "size_index", spc.bar_percentual_width.size_idx);
312 rectangle_prog.set_uniform(ctx, "secondary_size_index", spc.bar_outline_width.size_idx);
313 rectangle_prog.set_uniform(ctx, "rectangle_base_window", spc.bar_base_window);
314 rectangle_prog.set_uniform(ctx, "rectangle_coordinate_index", spc.bar_coordinate_index);
315 rectangle_prog.set_uniform(ctx, "rectangle_border_width", spc.bar_outline_width.size * get_domain_config_ptr()->reference_size);
316 rectangle_prog.set_attribute(ctx, rectangle_prog.get_color_index(), spc.bar_color.color);
317 rectangle_prog.set_attribute(ctx, "secondary_color", spc.bar_outline_color.color);
318 rectangle_prog.set_attribute(ctx, "size", (count > 1 ? extent[0] / (count - 1) : extent[0]) * spc.bar_percentual_width.size);
319 rectangle_prog.set_attribute(ctx, "depth_offset", -layer_idx * layer_depth);
320 // configure geometry shader
321 rectangle_prog.set_uniform(ctx, "border_mode", spc.bar_coordinate_index == 0 ? 2 : 1);
322
323 rectangle_prog.enable(ctx);
324 draw_sub_plot_samples(count, spc);
325 rectangle_prog.disable(ctx);
326 result = true;
327 }
328 }
329 disable_attributes(ctx, i);
330 return result;
331}
332
333int plot2d::draw_sub_plots_jointly(cgv::render::context& ctx, int layer_idx)
334{
335 // first draw all bar plots
336 unsigned i;
337 for (i = 0; i < get_nr_sub_plots(); ++i) {
338 draw_bar_plot(ctx, i, layer_idx);
339 ++layer_idx;
340 }
341 // next draw stick plots
342 for (i = 0; i < get_nr_sub_plots(); ++i) {
343 draw_stick_plot(ctx, i, layer_idx);
344 ++layer_idx;
345 }
346 // then we draw line plots
347 for (i = 0; i < get_nr_sub_plots(); ++i) {
348 draw_line_plot(ctx, i, layer_idx);
349 ++layer_idx;
350 }
351 // finally draw point plots
352 for (i = 0; i < get_nr_sub_plots(); ++i) {
353 draw_point_plot(ctx, i, layer_idx);
354 ++layer_idx;
355 }
356 return layer_idx;
357}
358
359void plot2d::extract_domain_rectangles(std::vector<box2>& R, std::vector<rgb>& C, std::vector<float>& D)
360{
361 R.resize(5);
362 C.resize(5);
363 D.resize(5);
364 vec2 extent = vec2(this->extent[0], this->extent[1]);
366 R[0] = box2(-0.5f * extent, 0.5f * extent);
367 R[0].add_point(0.5f * extent + float(get_nr_sub_plots() - 1) * vec2(sub_plot_delta[0], sub_plot_delta[1]));
369 D[0] = 0.0f;
370 for (unsigned ai = 0; ai < 2; ++ai) {
371 axis_config& ac = get_domain_config_ptr()->axis_configs[ai];
372 axis_config& ao = get_domain_config_ptr()->axis_configs[1 - ai];
373 float lw = rs * ac.line_width;
375 vec2 axis_extent(0.0f);
376 axis_extent[ai] = extent[ai];
377 axis_extent[1 - ai] = lw;
378 box2 axis_box(-0.5f * axis_extent, 0.5f * axis_extent);
379 axis_box.ref_min_pnt()[1 - ai] += 0.5f * (extent[1 - ai] - lw);
380 axis_box.ref_max_pnt()[1 - ai] += 0.5f * (extent[1 - ai] - lw);
381 R[2 * ai + 1] = axis_box;
382 C[2 * ai + 1] = col;
383 D[2 * ai + 1] = -layer_depth;
384 axis_box.ref_min_pnt()[1 - ai] -= extent[1 - ai] - lw;
385 axis_box.ref_max_pnt()[1 - ai] -= extent[1 - ai] - lw;
386 R[2 * ai + 2] = axis_box;
387 C[2 * ai + 2] = col;
388 D[2 * ai + 2] = -layer_depth;
389 // axis line
390 if (ao.get_attribute_min() < 0 && ao.get_attribute_max() > 0) {
391 float axis_plot = ao.plot_space_from_attribute_space(0.0f);
392 axis_box.ref_min_pnt()[1 - ai] = axis_plot - 0.5f * lw;
393 axis_box.ref_max_pnt()[1 - ai] = axis_plot + 0.5f * lw;
394 R.push_back(axis_box);
395 C.push_back(ac.color);
396 D.push_back(-2 * layer_depth);
397 }
398 }
399}
400
401/*
402bool plot2d::extract_tick_rectangles_and_tick_labels(
403 std::vector<box2>& R, std::vector<rgb>& C, std::vector<float>& D,
404 std::vector<label_info>& tick_labels, int ai, int ti, float he, float z_plot)
405{
406 axis_config& ac = get_domain_config_ptr()->axis_configs[ai];
407 float acw = 1.5f * ac.line_width * get_domain_config_ptr()->reference_size;
408 tick_config& tc = ti == 0 ? ac.primary_ticks : ac.secondary_ticks;
409 if (tc.type == TT_NONE)
410 return false;
411 float min_tick = ac.tick_space_from_attribute_space(ac.get_attribute_min());
412 float max_tick = ac.tick_space_from_attribute_space(ac.get_attribute_max());
413 int min_i = (int)ceil(min_tick / tc.step - std::numeric_limits<float>::epsilon());
414 int max_i = (int)((max_tick - fmod(max_tick, tc.step)) / tc.step);
415 // ignore secondary ticks on domain boundary
416 if (ti == 1 && min_i * tc.step - min_tick < std::numeric_limits<float>::epsilon())
417 ++min_i;
418 if (ti == 1 && max_i * tc.step - max_tick > -std::numeric_limits<float>::epsilon())
419 --max_i;
420 float lw = 0.5f * get_domain_config_ptr()->reference_size * tc.line_width;
421 float dl = 0.5f * get_domain_config_ptr()->reference_size * tc.length;
422 float d = -int(ti + 3) * layer_depth;
423 for (int i = min_i; i <= max_i; ++i) {
424 float c_tick = (float)(i * tc.step);
425 float c_attr = ac.attribute_space_from_tick_space(c_tick);
426 // ignore secondary ticks on axes
427 if (fabs(c_attr) < std::numeric_limits<float>::epsilon())
428 continue;
429 // ignore secondary ticks on primary ticks
430 if (ti == 1 && fabs(fmod(c_tick, ac.primary_ticks.step)) < 0.00001f)
431 continue;
432 std::string label_str;
433 if (tc.label)
434 label_str = cgv::utils::to_string(c_attr);
435 float c_plot = ac.plot_space_from_window_space(ac.window_space_from_tick_space(c_tick));
436 vec2 mn, mx;
437 mn[ai] = c_plot - lw;
438 mx[ai] = c_plot + lw;
439 switch (tc.type) {
440 case TT_DASH:
441 mn[1 - ai] = -he;
442 mx[1 - ai] = -he + dl;
443 R.push_back(box2(mn, mx));
444 C.push_back(ac.color);
445 D.push_back(d);
446 if (!label_str.empty()) {
447 mx[1 - ai] += acw;
448 tick_labels.push_back(label_info(mx.to_vec(), label_str, ai == 0 ? cgv::render::TA_BOTTOM : cgv::render::TA_LEFT));
449 if (ti == 1)
450 tick_labels.back().scale = 0.75f;
451 }
452 mn[1 - ai] = he - dl;
453 mx[1 - ai] = he;
454 R.push_back(box2(mn, mx));
455 C.push_back(ac.color);
456 D.push_back(d);
457 if (!label_str.empty()) {
458 mn[1 - ai] -= acw;
459 tick_labels.push_back(label_info(mn.to_vec(), label_str, ai == 0 ? cgv::render::TA_TOP : cgv::render::TA_RIGHT));
460 if (ti == 1)
461 tick_labels.back().scale = 0.75f;
462 }
463 if (z_plot != std::numeric_limits<float>::quiet_NaN()) {
464 mn[1 - ai] = z_plot - dl;
465 mx[1 - ai] = z_plot + dl;
466 R.push_back(box2(mn, mx));
467 C.push_back(ac.color);
468 D.push_back(d);
469 }
470 break;
471 case TT_LINE:
472 case TT_PLANE:
473 mn[1 - ai] = -he;
474 mx[1 - ai] = he;
475 R.push_back(box2(mn, mx));
476 C.push_back(ac.color);
477 D.push_back(d);
478 if (!label_str.empty()) {
479 mn[1 - ai] += acw;
480 mx[1 - ai] -= acw;
481 mn[ai] += 2.5f * lw;
482 mx[ai] += 2.5f * lw;
483 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)));
484 if (ti == 1)
485 tick_labels.back().scale = 0.75f;
486 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)));
487 if (ti == 1)
488 tick_labels.back().scale = 0.75f;
489 }
490 break;
491 }
492 }
493 return true;
494}
495*/
496
497void plot2d::extract_domain_tick_rectangles_and_tick_labels(
498 std::vector<box2>& R, std::vector<rgb>& C, std::vector<float>& D,
499 std::vector<label_info>& tick_labels, std::vector<tick_batch_info>& tick_batches)
500{
501 vecn E = get_extent();
502 set_extent(vecn(2, &extent[0]));
503 tick_labels.clear();
504 tick_batches.clear();
505 for (unsigned ti = 0; ti < 2; ++ti) {
506 for (unsigned ai = 0; ai < 2; ++ai) {
507 axis_config& ac = get_domain_config_ptr()->axis_configs[ai];
508 axis_config& ao = get_domain_config_ptr()->axis_configs[1 - ai];
509 float z_plot = (ao.get_attribute_min() < 0.0f && ao.get_attribute_max() > 0.0f) ?
510 ao.plot_space_from_attribute_space(0.0f) : std::numeric_limits<float>::quiet_NaN();
511 tick_batch_info tbi(ai, 1 - ai, ti == 0, 0, (unsigned)tick_labels.size());
512 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)) {
513 if ((tbi.label_count = (unsigned)(tick_labels.size() - tbi.first_label)) > 0)
514 tick_batches.push_back(tbi);
515 }
516 }
517 }
518 set_extent(E);
519}
520
521void plot2d::draw_domain(cgv::render::context& ctx, int si, bool no_fill)
522{
523 std::vector<box2> R;
524 std::vector<rgb> C;
525 std::vector<float> D;
526 extract_domain_rectangles(R, C, D);
527 extract_domain_tick_rectangles_and_tick_labels(R, C, D, tick_labels, tick_batches);
528 draw_rectangles(ctx, aam_domain, R, C, D, (get_domain_config_ptr()->fill && !no_fill) ? 0 : 1);
529 draw_title(ctx, vec2::from_vec(get_domain_config_ptr()->title_pos), -3 * layer_depth, si);
530 draw_tick_labels(ctx, aam_domain_tick_labels, tick_labels, tick_batches, -4 * layer_depth);
531}
532
534{
536
537 // store to be changed opengl state
538 GLboolean line_smooth = glIsEnabled(GL_LINE_SMOOTH);
539 GLboolean blend = glIsEnabled(GL_BLEND);
540 GLboolean cull_face = glIsEnabled(GL_CULL_FACE);
541 GLenum blend_src, blend_dst, depth;
542 glGetIntegerv(GL_BLEND_DST, reinterpret_cast<GLint*>(&blend_dst));
543 glGetIntegerv(GL_BLEND_SRC, reinterpret_cast<GLint*>(&blend_src));
544 glGetIntegerv(GL_DEPTH_FUNC, reinterpret_cast<GLint*>(&depth));
545
546 // update state
547 glEnable(GL_LINE_SMOOTH);
548 glDisable(GL_CULL_FACE);
549 glEnable(GL_BLEND);
550 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
551 glDepthFunc(GL_LEQUAL);
552
553 // place plot with modelview matrix
555 mat4 R;
557 ctx.mul_modelview_matrix(cgv::math::translate4<float>(center_location) * R);
558
559 // configure bar prog only once
560 configure_bar_plot(ctx);
561 // draw all subplots jointly in one plane
562 if (sub_plot_delta[2] == 0.0f) {
563 if (get_domain_config_ptr()->show_domain)
564 draw_domain(ctx);
565 if (legend_components != LC_HIDDEN)
566 draw_legend(ctx, 5);
567 if (disable_depth_mask)
568 glDepthMask(GL_FALSE);
569 else
570 glDepthFunc(GL_LEQUAL);
571 draw_sub_plots_jointly(ctx, 8);
572 if (disable_depth_mask)
573 glDepthMask(GL_TRUE);
574 else
575 glDepthFunc(GL_LESS);
576 }
577 // draw subplots with offset in back to front order
578 else {
579 auto M = ctx.get_modelview_matrix();
580 double plot_pos_eye_z = M(2, 3);
581 double plot_z_eye_z = M(2, 2);
582 int i_begin = 0, i_end = get_nr_sub_plots(), i_delta = 1;
583 // check if we can not use default order
584 if (plot_pos_eye_z * plot_z_eye_z * sub_plot_delta[2] > 0) {
585 i_begin = i_end - 1;
586 i_end = -1;
587 i_delta = -1;
588 ctx.mul_modelview_matrix(cgv::math::translate4<float>(vec3(0, 0, (get_nr_sub_plots()-1) * sub_plot_delta[2])));
589 }
590 // traverse all subplots in back to front order
591 bool fst = true;
592 unsigned ai;
593 for (ai=0; ai<2+nr_attributes; ++ai)
594 if (multi_axis_modes[ai])
595 get_domain_config_ptr()->axis_configs[ai].backup_attribute_range();
596 for (int i = i_begin; i != i_end; i += i_delta) {
597 // adapt all axes in multi axis mode
598 for (ai = 0; ai < 2 + nr_attributes; ++ai) {
599 if (multi_axis_modes[ai]) {
600 auto& ac = get_domain_config_ptr()->axis_configs[ai];
601 float min_val, max_val;
602 ac.put_backup_attribute_range(min_val, max_val);
603 if (determine_axis_extent_from_subplot(ai, i, min_val, max_val)) {
604 if (fabs(max_val - min_val) < 10.0f * std::numeric_limits<float>::epsilon())
605 max_val = min_val + 1;
606 ac.set_attribute_range(min_val, max_val);
607 }
608
609 }
610 }
611 if (get_domain_config_ptr()->show_domain)
612 draw_domain(ctx, i, !fst);
613 if (legend_components != LC_HIDDEN)
614 draw_legend(ctx, 5, i == i_begin, multi_axis_modes);
615 if (disable_depth_mask)
616 glDepthMask(GL_FALSE);
617 else
618 glDepthFunc(GL_LEQUAL);
619 draw_bar_plot(ctx, i, 8);
620 draw_stick_plot(ctx, i, 9);
621 draw_line_plot(ctx, i, 10);
622 draw_point_plot(ctx, i, 11);
623 if (disable_depth_mask)
624 glDepthMask(GL_TRUE);
625 else
626 glDepthFunc(GL_LESS);
627 ctx.mul_modelview_matrix(cgv::math::translate4<float>(vec3(0, 0, i_delta* sub_plot_delta[2])));
628 fst = false;
629 }
630 for (ai = 0; ai < 2 + nr_attributes; ++ai)
631 if (multi_axis_modes[ai])
632 get_domain_config_ptr()->axis_configs[ai].restore_attribute_range();
633 }
634
636
637 // recover opengl state
638 if (!line_smooth)
639 glDisable(GL_LINE_SMOOTH);
640 if (!blend)
641 glDisable(GL_BLEND);
642 if (cull_face)
643 glEnable(GL_CULL_FACE);
644 glDepthFunc(depth);
645 glBlendFunc(blend_src, blend_dst);
646}
647
650{
651 auto& p2dc = reinterpret_cast<plot2d_config&>(pbc);
653}
654
657{
658 auto& p2dc = reinterpret_cast<plot2d_config&>(pbc);
660}
661
664{
665 auto& p2dc = reinterpret_cast<plot2d_config&>(pbc);
667}
668
673
675{
676 if (p.begin_tree_node("Multi Modes", multi_axis_modes, false, "level=3")) {
677 p.align("\a");
678 for (unsigned ai = 0; ai < 2 + nr_attributes; ++ai)
679 p.add_member_control(bp, get_domain_config_ptr()->axis_configs[ai].name, multi_axis_modes[ai], "toggle");
680 p.align("\b");
682 }
683 p.add_member_control(bp, "Delta X", sub_plot_delta[0], "value_slider", "min=-1;max=1;step=0.001;ticks=true");
684 p.add_member_control(bp, "Delta Y", sub_plot_delta[1], "value_slider", "min=-1;max=1;step=0.001;ticks=true");
685 p.add_member_control(bp, "Delta Z", sub_plot_delta[2], "value_slider", "min=-1;max=1;step=0.02;ticks=true");
687 p.add_member_control(bp, "Disable Depth Mask", disable_depth_mask, "toggle");
688}
689
690 }
691}
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:739
void put_homogeneous_matrix(hmat_type &M) const
compute equivalent homogeneous 4x4 rotation matrix
Definition quaternion.h:154
void create_stick_config_gui(cgv::base::base *bp, cgv::gui::provider &p, plot_base_config &pbc)
create the gui for a stick subplot
Definition plot2d.cxx:656
void clear(cgv::render::context &ctx)
destruct shader programs
Definition plot2d.cxx:145
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
void create_point_config_gui(cgv::base::base *bp, cgv::gui::provider &p, plot_base_config &pbc)
create the gui for a point subplot
Definition plot2d.cxx:649
std::vector< vec2 > & ref_sub_plot_samples(unsigned i=0)
return the samples of the i-th sub plot
Definition plot2d.cxx:106
void create_gui(cgv::base::base *bp, cgv::gui::provider &p)
create a gui for the plot with gui for all configs
Definition plot2d.cxx:674
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
void create_bar_config_gui(cgv::base::base *bp, cgv::gui::provider &p, plot_base_config &pbc)
create the gui for a bar subplot
Definition plot2d.cxx:663
void draw(cgv::render::context &ctx)
draw plot
Definition plot2d.cxx:533
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
void create_config_gui(cgv::base::base *bp, cgv::gui::provider &p, unsigned i)
create the gui for a subplot configuration
Definition plot2d.cxx:669
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
cgv::render::rectangle_render_style rrs
render style of rectangles
Definition plot_base.h:449
virtual void create_config_gui(cgv::base::base *bp, cgv::gui::provider &p, unsigned i)
create the gui for a subplot configuration
virtual void create_bar_config_gui(cgv::base::base *bp, cgv::gui::provider &p, plot_base_config &pbc)
create the gui for a bar subplot
const domain_config * get_domain_config_ptr() const
return const pointer to domain configuration
void clear(cgv::render::context &ctx)
destruct shader programs
unsigned get_nr_sub_plots() const
return current number of sub plots
LegendComponent legend_components
whether to show legend
Definition plot_base.h:366
std::vector< attribute_source_array > attribute_source_arrays
attribute sources
Definition plot_base.h:429
vecn get_extent() const
query the plot extend in 2D coordinates
bool determine_axis_extent_from_subplot(unsigned ai, unsigned i, float &sample_min, float &sample_max)
adjust the domain with respect to ai th axis to the i-th subplot
float layer_depth
depth offset of a single layer
Definition plot_base.h:323
quat orientation
orientiation quaternion mapping from domain to world coordinates
Definition plot_base.h:354
void set_mapping_uniforms(cgv::render::context &ctx, cgv::render::shader_program &prog)
set the uniforms for defining the mappings to visual variables
vec3 center_location
center location of domain in world coordinates
Definition plot_base.h:356
std::vector< plot_base_config * > configs
store one configuration per sub plot
Definition plot_base.h:348
std::vector< label_info > tick_labels
all tick labels
Definition plot_base.h:319
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
void prepare_extents()
prepare extents for drawing
virtual void create_gui(cgv::base::base *bp, cgv::gui::provider &p)
create a gui for the plot with gui for all configs
virtual void create_point_config_gui(cgv::base::base *bp, cgv::gui::provider &p, plot_base_config &pbc)
create the gui for a point subplot
std::vector< tick_batch_info > tick_batches
twice number of axis pairs with index of first tick label and number of tick labels for primary and s...
Definition plot_base.h:321
void set_extent(const vecn &new_extent)
set the plot extend in 2D coordinates
virtual void create_stick_config_gui(cgv::base::base *bp, cgv::gui::provider &p, plot_base_config &pbc)
create the gui for a stick subplot
void set_plot_uniforms(cgv::render::context &ctx, cgv::render::shader_program &prog)
set the uniforms for plot configurations
size_t enable_attributes(cgv::render::context &ctx, int i, const std::vector< std::vector< vec2 > > &samples)
set vertex shader input attributes based on attribute source information
bool init(cgv::render::context &ctx)
build legend prog and create aab
vec3 extent
extents used for drawing current
Definition plot_base.h:437
base class for all drawables, which is independent of the used rendering API.
Definition context.h:626
virtual void mul_modelview_matrix(const dmat4 &MV)
multiply given matrix from right to current modelview matrix
Definition context.cxx:1812
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:1818
void push_modelview_matrix()
push the current viewing matrix onto a matrix stack for viewing matrices.
Definition context.cxx:1806
bool enable(context &ctx)
enable the shader program
bool disable(context &ctx)
disable shader program and restore fixed functionality
bool set_uniform(const context &ctx, const std::string &name, const T &value, bool generate_error=false)
Set the value of a uniform by name, where the type can be any of int, unsigned, float,...
void destruct(const context &ctx)
destruct shader program
bool is_linked() const
return whether program is linked
bool set_attribute(const context &ctx, const std::string &name, const T &value)
set constant default value of a vertex attribute by attribute name, if name does not specify an attri...
bool build_program(const context &ctx, const std::string &file_name, bool show_error=false)
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
T min_value(ForwardIt first, ForwardIt last, T fallback)
Find the minimum value in the range [first, last) or return fallback if the range is empty.
Definition algorithm.h:59
T max_value(ForwardIt first, ForwardIt last, T fallback)
Find the maximum value in the range [first, last) or return fallback if the range is empty.
Definition algorithm.h:91
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:668
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:670
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
float reference_size
store size of virtual pixel based measurement
Definition plot_base.h:52
bool fill
whether to fill the domain
Definition plot_base.h:38
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
rgb color
color of the domain fill
Definition plot_base.h:48
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
holds options applied before and during shader compilation, such as preprocessor defines and code sni...
Definition shader_code.h:87
shader_define_map defines
map of pre-processor define names to values
Definition shader_code.h:89