cgv
Loading...
Searching...
No Matches
plot_base.h
1#pragma once
2
3#include <vector>
4
5#include <cgv/base/base.h>
6#include <cgv/render/attribute_array_binding.h>
7#include <cgv/render/color_scale_adapter.h>
8#include <cgv/render/drawable.h>
9#include <cgv/render/shader_program.h>
10#include <cgv/render/vertex_buffer.h>
11#include <cgv/render/view.h>
12#include <cgv/media/axis_aligned_box.h>
13#include <cgv/media/color.h>
14#include <cgv/media/color_scale.h>
15#include <cgv/media/color_scheme.h>
16#include <cgv/media/font/font.h>
17#include <cgv/gui/provider.h>
18#include <libs/cgv_gl/rectangle_renderer.h>
19
20#include "axis_config.h"
21
22#include "lib_begin.h"
23
24namespace cgv {
25 namespace plot {
26
30enum VisualVariable
31{
32 VV_COLOR,
33 VV_OPACITY,
34 VV_SIZE
35};
36
76
78enum ChartType
79{
80 CT_POINT,
81 CT_LINE_CHART,
82 CT_BAR_CHART
83};
84
86{
87 rgb color;
88 int color_idx;
89 mapped_rgb(const rgb& c = rgb(1,1,1)) : color(c), color_idx(-1) {}
90};
91
93{
94 float opacity;
95 int opacity_idx;
96 mapped_opacity(float o = 1.0f) : opacity(o), opacity_idx(-1) {}
97};
98
100{
101 float size;
102 int size_idx;
103 mapped_size(float s = 1.0f) : size(s), size_idx(-1) {}
104};
105
107{
108 rgba color;
109 int color_idx;
110 int opacity_idx;
111 mapped_rgba(const rgba& c = rgba(1, 1, 1, 1)) : color(c), color_idx(-1), opacity_idx(-1) {}
112};
113
114extern CGV_API void add_mapped_size_control(cgv::gui::provider& p, cgv::base::base* bp, const std::string& name, mapped_size& ms, std::string options = "");
115extern CGV_API void add_mapped_rgb_control(cgv::gui::provider& p, cgv::base::base* bp, const std::string& name, mapped_rgb& ms);
116extern CGV_API void add_mapped_rgba_control(cgv::gui::provider& p, cgv::base::base* bp, const std::string& name, mapped_rgba& ms);
117extern CGV_API void add_mapped_opacity_control(cgv::gui::provider& p, cgv::base::base* bp, const std::string& name, mapped_opacity& ms);
118
119enum SubPlotInfluence
120{
121 SPI_NONE = 0,
122 SPI_POINT = 1,
123 SPI_POINT_HALO = 2,
124 SPI_LINE = 4,
125 SPI_LINE_HALO = 8,
126 SPI_STICK = 16,
127 SPI_BAR = 32,
128 SPI_BAR_OUTLINE = 64,
129 SPI_ALL = 127
130};
131
133struct CGV_API plot_base_config
134{
136 std::string name;
137
142
145
147 SubPlotInfluence sub_plot_influence;
154
159
166
177
188
194 float bar_base_window;
203
205 plot_base_config(const std::string& _name, unsigned dim);
207 virtual void configure_chart(ChartType chart_type);
209 virtual void set_colors(const rgb& base_color);
210 virtual void set_color_indices(int idx);
212 virtual void set_opacities(float _opa);
213 virtual void set_opacity_indices(int idx);
215 virtual void set_sizes(float _size);
216 virtual void set_size_indices(int idx);
218 virtual ~plot_base_config();
219};
220
222enum AttributeSource
223{
224 AS_NONE,
225 AS_SAMPLE_CONTAINER,
226 AS_POINTER,
227 AS_VBO
228};
229
231struct CGV_API attribute_source
232{
233 AttributeSource source;
234 union {
235 int sub_plot_index; // index of other subplot or -1 for current subplot
236 const float* pointer; // pointer to external data
237 const cgv::render::vertex_buffer* vbo_ptr; // pointer to vbo
238 };
239 size_t offset; // offset into vbo or coordinate axis into sample container
240 size_t count;
241 size_t stride; // stride in all representations
245 attribute_source(int sub_plot_index, size_t ai, size_t _count, size_t _stride);
247 attribute_source(const float* _pointer, size_t _count, size_t _stride);
249 attribute_source(const cgv::render::vertex_buffer* _vbo_ptr, size_t _offset, size_t _count, size_t _stride);
252};
253
256{
257 bool samples_out_of_date;
258 bool sources_out_of_date;
259 size_t count;
262 std::vector<attribute_source> attribute_sources;
264};
266enum LegendComponent
267{
268 LC_HIDDEN = 0,
269 LC_PRIMARY_COLOR = 1,
270 LC_SECONDARY_COLOR = 2,
271 LC_PRIMARY_OPACITY = 4,
272 LC_SECONDARY_OPACITY = 8,
273 LC_PRIMARY_SIZE = 16,
274 LC_SECONDARY_SIZE = 32,
275 LC_ANY = 63
276};
277
279{
280 virtual size_t size(unsigned i) const = 0;
281 virtual float operator() (unsigned i, unsigned k, unsigned o) const = 0;
282};
283
285class CGV_API plot_base : public cgv::render::drawable, virtual public cgv::signal::tacker
286{
289private:
291 cgv::render::shader_program legend_prog;
292protected:
293 cgv::render::view* view_ptr;
296 {
297 vecn position;
298 std::string label;
300 float scale;
301 label_info(const vecn& _position, const std::string& _label, cgv::render::TextAlignment _align)
302 : position(_position), label(_label), align(_align), scale(1.0f) {}
303 };
306 {
308 int ai, aj;
310 bool primary;
312 unsigned first_vertex;
314 unsigned vertex_count;
316 unsigned first_label;
318 unsigned label_count;
320 tick_batch_info(int _ai, int _aj, bool _primary, unsigned _first_vertex = 0, unsigned _first_label = 0);
321 };
323 std::vector<label_info> tick_labels, legend_tick_labels;
325 std::vector<tick_batch_info> tick_batches, legend_tick_batches;
328
332 static std::vector<const char*> font_names;
334 static std::string font_name_enum_def;
335public:
337 void ensure_font_names();
338protected:
340
344 unsigned dim;
352 std::vector<plot_base_config*> configs;
354
362 vec3 world_space_from_plot_space(const vecn& pnt_plot) const;
364 vec3 transform_to_world(const vecn& pnt_attr) const;
366public:
370 LegendComponent legend_components;
380
385 static const unsigned MAX_NR_COLOR_MAPPINGS = 2;
387 int color_mapping[MAX_NR_COLOR_MAPPINGS];
389 bool color_scale_is_bipolar[MAX_NR_COLOR_MAPPINGS];
391 float color_scale_gamma[MAX_NR_COLOR_MAPPINGS];
393 float window_zero_position[MAX_NR_COLOR_MAPPINGS];
395 static const unsigned MAX_NR_OPACITY_MAPPINGS = 2;
397 int opacity_mapping[MAX_NR_OPACITY_MAPPINGS];
399 float opacity_gamma[MAX_NR_OPACITY_MAPPINGS];
401 bool opacity_is_bipolar[MAX_NR_OPACITY_MAPPINGS];
403 float opacity_window_zero_position[MAX_NR_OPACITY_MAPPINGS];
405 float opacity_min[MAX_NR_OPACITY_MAPPINGS];
407 float opacity_max[MAX_NR_OPACITY_MAPPINGS];
408
410 static const unsigned MAX_NR_SIZE_MAPPINGS = 2;
411 static const unsigned color_scale_texture_unit = 0;
413 int size_mapping[MAX_NR_SIZE_MAPPINGS];
415 float size_gamma[MAX_NR_SIZE_MAPPINGS];
417 float size_min[MAX_NR_SIZE_MAPPINGS], size_max[MAX_NR_SIZE_MAPPINGS];
419protected:
423 std::array<std::shared_ptr<cgv::render::device_continuous_color_scale>, MAX_NR_COLOR_MAPPINGS> color_scales;
427 int color_scheme_index[MAX_NR_COLOR_MAPPINGS];
429 bool reversed[2] = { false, false };
431
444 std::vector<attribute_source_array> attribute_source_arrays;
446 void on_legend_axis_change(cgv::gui::provider& p, cgv::gui::control<int>& ctrl);
448 void on_font_selection();
450 void on_font_face_selection();
454 void prepare_extents();
456 void set_plot_uniforms(cgv::render::context& ctx, cgv::render::shader_program& prog);
458 void set_mapping_uniforms(cgv::render::context& ctx, cgv::render::shader_program& prog);
460 void update_color_scales();
461private:
463 size_t enable_attributes(cgv::render::context& ctx, int i, const sample_access& sa);
464protected:
467 cgv::render::attribute_array_manager aam_legend, aam_legend_ticks, aam_title;
469 void draw_rectangles(cgv::render::context& ctx, cgv::render::attribute_array_manager& aam,
470 std::vector<box2>& R, std::vector<rgb>& C, std::vector<float>& D, size_t offset = 0);
472 void draw_tick_labels(cgv::render::context& ctx, cgv::render::attribute_array_manager& aam_ticks,
473 std::vector<label_info>& tick_labels, std::vector<tick_batch_info>& tick_batches, float depth);
475 size_t enable_attributes(cgv::render::context& ctx, int i, const std::vector<std::vector<vec2>>& samples);
477 size_t enable_attributes(cgv::render::context& ctx, int i, const std::vector<std::vector<vec3>>& samples);
479 void disable_attributes(cgv::render::context& ctx, int i);
481 void update_samples_out_of_date_flag();
483 virtual bool compute_sample_coordinate_interval(int i, int ai, float& samples_min, float& samples_max) = 0;
485 void draw_sub_plot_samples(int count, const plot_base_config& spc, bool strip = false);
487 void draw_title(cgv::render::context& ctx, vec2 pos, float depth, int si = -1);
489 void draw_legend(cgv::render::context& ctx, int layer_idx = 0, bool is_first = true, bool* multi_axis_modes = 0);
491 bool extract_tick_rectangles_and_tick_labels(
492 std::vector<box2>& R, std::vector<rgb>& C, std::vector<float>& D,
493 std::vector<label_info>& tick_labels, int ai, int ci, int ti, float he,
494 float z_plot, float plot_scale = 1.0f, vec2 plot_offset = vec2(0.0f,0.0f), float d = 0.0f, bool multi_axis = true);
496 void extract_legend_tick_rectangles_and_tick_labels(
497 std::vector<box2>& R, std::vector<rgb>& C, std::vector<float>& D,
498 std::vector<label_info>& tick_labels, std::vector<tick_batch_info>& tick_batches, float d,
499 bool clear_cache = false, bool is_first = true, bool* multi_axis_modes = 0);
500
501public:
503 plot_base(unsigned dim, unsigned nr_attributes = 0);
505 unsigned get_dim() const { return dim; }
507 void set_view_ptr(cgv::render::view* _view_ptr);
511 void set_label_font(float font_size, cgv::media::font::FontFaceAttributes ffa = cgv::media::font::FFA_REGULAR, const std::string& font_name = "");
513 const domain_config* get_domain_config_ptr() const;
515 domain_config* get_domain_config_ptr();
517 void set_domain_config_ptr(domain_config* _new_ptr);
519
523 const box2 get_domain() const;
525 const box3 get_domain3() const;
527 void set_domain(const box2& dom);
529 void set_domain3(const box3& dom);
531 void set_extent(const vecn& new_extent);
533 vecn get_extent() const;
535
537 void set_extent_scaling(float x_scale, float y_scale, float z_scale = 0);
539 void set_width(float new_width, bool constrained = true);
541 void set_height(float new_height, bool constrained = true);
543 void set_orientation(const quat& _orientation);
545 void place_origin(const vec3& new_origin_location);
547 void place_center(const vec3& new_center_location);
549 void place_corner(unsigned corner_index, const vec3& new_corner_location);
551 vec3 get_origin() const;
553 const quat& get_orientation() const;
555 const vec3& get_center() const;
557 vec3 get_corner(unsigned i) const;
559 const vec3 get_axis_direction(unsigned ai) const;
561
565 bool determine_axis_extent_from_subplot(unsigned ai, unsigned i, float& sample_min, float& sample_max);
567 void adjust_domain_axis_to_data(unsigned ai, bool adjust_min = true, bool adjust_max = true, bool only_visible = true);
569 void adjust_domain_to_data(bool only_visible = true);
571 void include_axis_to_domain(unsigned ai);
573 void adjust_tick_marks(unsigned max_nr_secondary_ticks = 20, bool adjust_to_attribute_ranges = true);
575 void adjust_extent_to_domain_aspect_ratio(int preserve_ai = 0);
577
581 unsigned get_nr_sub_plots() const;
583 virtual unsigned add_sub_plot(const std::string& name) = 0;
585 virtual void delete_sub_plot(unsigned i) = 0;
587 plot_base_config& ref_sub_plot_config(unsigned i);
589 void set_samples_out_of_date(unsigned i);
591 void set_sub_plot_colors(unsigned i, const rgb& base_color);
593 void set_sub_plot_attribute(unsigned i, unsigned ai, int subplot_index, size_t aj);
595 void set_sub_plot_attribute(unsigned i, unsigned ai, const float* _pointer, size_t count, size_t stride);
597 void set_sub_plot_attribute(unsigned i, unsigned ai, const cgv::render::vertex_buffer* _vbo_ptr, size_t _offset, size_t _count, size_t _stride);
599
603 bool set_color_scale(int mapping_index, int color_scheme_index);
605 bool set_color_scale(int mapping_index, const std::string& color_scheme_name);
607
609 bool init(cgv::render::context& ctx);
611 void clear(cgv::render::context& ctx);
612
615protected:
616 void update_ref_opacity(unsigned i, cgv::gui::provider& p);
617 void update_ref_opacity_index(unsigned i, cgv::gui::provider& p);
618 void update_ref_size(unsigned i, cgv::gui::provider& p);
619 void update_ref_size_index(unsigned i, cgv::gui::provider& p);
620 void update_ref_color(unsigned i, cgv::gui::provider& p);
621 void update_ref_color_index(unsigned i, cgv::gui::provider& p);
622
623public:
625 virtual void create_plot_gui(cgv::base::base* bp, cgv::gui::provider& p);
627 virtual void create_base_config_gui(cgv::base::base* bp, cgv::gui::provider& p, unsigned i);
629 virtual void create_point_config_gui(cgv::base::base* bp, cgv::gui::provider& p, plot_base_config& pbc);
631 virtual void create_line_config_gui(cgv::base::base* bp, cgv::gui::provider& p, plot_base_config& pbc);
633 virtual void create_stick_config_gui(cgv::base::base* bp, cgv::gui::provider& p, plot_base_config& pbc);
635 virtual void create_bar_config_gui(cgv::base::base* bp, cgv::gui::provider& p, plot_base_config& pbc);
637 virtual void create_config_gui(cgv::base::base* bp, cgv::gui::provider& p, unsigned i);
639 virtual void create_gui(cgv::base::base* bp, cgv::gui::provider& p);
641};
642
643 }
644}
645
646#include <cgv/config/lib_end.h>
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
base class for plot2d and plot3d, which can have several sub plots each
Definition plot_base.h:286
unsigned get_dim() const
return nr dimensions of plot
Definition plot_base.h:505
virtual unsigned add_sub_plot(const std::string &name)=0
add sub plot and return sub plot index
cgv::render::rectangle_render_style rrs
render style of rectangles
Definition plot_base.h:466
static std::string font_name_enum_def
concatenate font names to enum definition for dropdown control
Definition plot_base.h:334
static std::vector< const char * > font_names
store a vector with all fonts on the system
Definition plot_base.h:332
rgba legend_color
color and opacity of legend
Definition plot_base.h:378
LegendComponent legend_components
whether to show legend
Definition plot_base.h:370
std::vector< attribute_source_array > attribute_source_arrays
attribute sources
Definition plot_base.h:444
cgv::render::vertex_buffer vbo_legend
vbo for legend drawing
Definition plot_base.h:440
float layer_depth
depth offset of a single layer
Definition plot_base.h:327
vec2 legend_extent
width of legend
Definition plot_base.h:374
quat orientation
orientiation quaternion mapping from domain to world coordinates
Definition plot_base.h:358
unsigned dim
dimension of plot
Definition plot_base.h:344
domain_config dom_cfg
domain configuration
Definition plot_base.h:348
vec3 center_location
center location of domain in world coordinates
Definition plot_base.h:360
std::vector< plot_base_config * > configs
store one configuration per sub plot
Definition plot_base.h:352
ivec4 out_of_range_mode
handling of values that are out of range
Definition plot_base.h:383
std::vector< label_info > tick_labels
all tick labels
Definition plot_base.h:323
cgv::render::attribute_array_binding aab_legend
manage attributes for legend drawing
Definition plot_base.h:442
domain_config * dom_cfg_ptr
pointer to currently used domain config
Definition plot_base.h:350
virtual void delete_sub_plot(unsigned i)=0
delete the i-th sub plot
unsigned nr_attributes
number of additional attributes
Definition plot_base.h:346
cgv::media::font::font_face_ptr title_font_face
store pointer to title font face
Definition plot_base.h:438
int legend_axis
coordinate direction along which to draw legend
Definition plot_base.h:376
vec3 legend_location
center location of legend in domain coordinates
Definition plot_base.h:372
cgv::media::font::font_ptr title_font
store pointer to title font
Definition plot_base.h:436
std::array< std::shared_ptr< cgv::render::device_continuous_color_scale >, MAX_NR_COLOR_MAPPINGS > color_scales
color scales used for primary and secondary color mapping
Definition plot_base.h:423
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:325
cgv::render::color_scale_adapter color_scale_adapter
adapter to enable using color scales in shader programs
Definition plot_base.h:425
cgv::media::font::font_ptr label_font
store pointer to label font
Definition plot_base.h:432
cgv::media::font::font_face_ptr label_font_face
store pointer to label font face
Definition plot_base.h:434
vec3 extent
extents used for drawing current
Definition plot_base.h:452
the attribute_array_binding allows to define vertex attributes (i.e.
attribute array manager used to upload arrays to gpu
Manages graphics resources to enable using device_color_scale in shader programs.
base class for all drawables, which is independent of the used rendering API.
Definition context.h:668
base class for all drawables, which is independent of the used rendering API.
Definition drawable.h:15
a shader program combines several shader code fragments to a complete definition of the shading pipel...
a vertex buffer is an unstructured memory block on the GPU.
defines a symmetric view with the following quantities:
Definition view.h:22
FontFaceAttributes
declaration of supported attributes of font faces
Definition font.h:21
TextAlignment
different text alignments
Definition context.h:329
this header is dependency free
Definition print.h:11
cgv::media::color< float, cgv::media::RGB, cgv::media::OPACITY > rgba
declare rgba color type with 32 bit components
Definition color.h:898
cgv::media::color< float, cgv::media::RGB > rgb
declare rgb color type with 32 bit components
Definition color.h:896
struct that manages attribute sources and corresponding gpu objects per subplot
Definition plot_base.h:256
store source of a single plot attribute (one coordinate axis or one float attribute)
Definition plot_base.h:232
rgba title_color
color of the title
Definition plot_base.h:54
bool show_domain
whether to show the coordinate axes including tickmarks and labels
Definition plot_base.h:40
unsigned title_font_index
store index of selected title font
Definition plot_base.h:68
float title_font_size
store selected title font size
Definition plot_base.h:70
float reference_size
store size of virtual pixel based measurement
Definition plot_base.h:56
unsigned label_font_index
store index of selected label font
Definition plot_base.h:62
bool fill
whether to fill the domain
Definition plot_base.h:42
std::string title
plot title
Definition plot_base.h:44
vecn title_pos
position of title
Definition plot_base.h:50
cgv::media::font::FontFaceAttributes label_ffa
store selected label font face attributes
Definition plot_base.h:66
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:60
cgv::media::font::FontFaceAttributes title_ffa
store selected title font face attributes
Definition plot_base.h:72
rgb color
color of the domain fill
Definition plot_base.h:52
float label_font_size
store selected label font size
Definition plot_base.h:64
float blend_width_in_pixel
store blend width in screen pixels used for antialiasing
Definition plot_base.h:58
bool show_title
whether to show the plot title
Definition plot_base.h:46
bool show_sub_plot_names
whether to show the sub plot names
Definition plot_base.h:48
render information stored per label
Definition plot_base.h:296
unsigned first_vertex
index of first tick vertex in batch
Definition plot_base.h:312
int ai
indices of coordinate axis used for definition of 2d points
Definition plot_base.h:308
plot independent configuration parameters of one sub plot in a 2d or 3d plot
Definition plot_base.h:134
mapped_size line_halo_width
width of line halo in pixel
Definition plot_base.h:174
int bar_coordinate_index
extended bar information
Definition plot_base.h:192
mapped_size line_width
line width
Definition plot_base.h:170
mapped_size bar_percentual_width
percentual width of bar computed assuming a uniform y-sampling distance
Definition plot_base.h:198
std::string name
name of sub plot
Definition plot_base.h:136
float stick_base_window
base window position of stick
Definition plot_base.h:183
bool show_lines
whether to connect data points with lines
Definition plot_base.h:168
mapped_rgba stick_color
color of the stick line
Definition plot_base.h:187
bool show_bars
whether to show bars
Definition plot_base.h:190
mapped_size bar_outline_width
line width of bar outlines
Definition plot_base.h:196
mapped_opacity ref_opacity
reference opacity, when changed, all opcities are adapted with set_opacity()
Definition plot_base.h:151
bool show_points
whether to show data points
Definition plot_base.h:156
mapped_size stick_width
line width of stick
Definition plot_base.h:185
mapped_rgba bar_color
bar fill color
Definition plot_base.h:200
SubPlotInfluence sub_plot_influence
store bit field to define which sub plots are influenced by reference values
Definition plot_base.h:147
mapped_rgba line_halo_color
color of line halo
Definition plot_base.h:176
mapped_rgba point_color
point color
Definition plot_base.h:161
mapped_size ref_size
reference size, when changed, all sizes are adapted with set_size()
Definition plot_base.h:153
size_t begin_sample
offset into samples defaults to 0, if larger than end_sample vector is split into two parts
Definition plot_base.h:139
int stick_coordinate_index
extended stick information
Definition plot_base.h:181
bool show_plot
whether to show sub plot
Definition plot_base.h:144
bool show_sticks
whether to show straight lines to the bottom of the plot, which are called sticks
Definition plot_base.h:179
mapped_rgba line_color
line color
Definition plot_base.h:172
mapped_rgb ref_color
reference color, when changed, all colors are adapted with set_colors()
Definition plot_base.h:149
mapped_size point_halo_width
width of point halo in pixel
Definition plot_base.h:163
mapped_rgba bar_outline_color
bar outline color
Definition plot_base.h:202
mapped_size point_size
point size in pixels
Definition plot_base.h:158
mapped_rgba point_halo_color
color of point halo
Definition plot_base.h:165
size_t end_sample
defaults to -1 and effectively is always the end of the sample vector
Definition plot_base.h:141
configuration of rectangle renderer