cgv
Loading...
Searching...
No Matches
device_color_scale.cxx
1#include "device_color_scale.h"
2
3namespace cgv {
4namespace render {
5
7 const cgv::media::color_scale* color_scale = get_color_scale();
9 // Set arguments that are available to every color scale through the base class
10 // and then update the implementation-specific fields.
11 arguments.unknown_color = color_scale->get_unknown_color();
12 arguments.domain = color_scale->get_domain();
13 if(color_scale->is_clamped())
14 arguments.mapping_options |= DeviceColorScaleMappingOptions::kClamped;
15 update_color_scale_specific_arguments(arguments);
16 return arguments;
17}
18
19std::vector<cgv::rgba> device_color_scale::get_texture_data(size_t texture_resolution) const {
20 auto color_scale = get_color_scale();
21 if(color_scale)
22 return color_scale->quantize(texture_resolution);
23 else
24 return std::vector<cgv::rgba>(texture_resolution, { 0.0f });
25}
26
27void device_continuous_color_scale::update_color_scale_specific_arguments(device_color_scale_arguments& out_arguments) const {
28 out_arguments.transform = static_cast<uint16_t>(color_scale->get_transform());
29 if(color_scale->is_diverging())
30 out_arguments.mapping_options |= DeviceColorScaleMappingOptions::kDiverging;
31 out_arguments.midpoint = color_scale->get_midpoint();
32 out_arguments.exponent = color_scale->get_pow_exponent();
33
34 // Continuous color scales with logarithmic transform use precomputed values to make mapping calculations more efficient.
35 // However, these invariants are private so we need to recompute them here.
36 if(color_scale->get_transform() == cgv::media::ContinuousMappingTransform::kLog) {
37 vec2 domain = color_scale->get_domain();
38 out_arguments.log_sign = domain[0] < 0.0f && domain[1] < 0.0f ? -1.0f : 1.0f;
39 out_arguments.log_base = std::log(color_scale->get_log_base());
40 out_arguments.log_midpoint = std::log(out_arguments.log_sign * color_scale->get_midpoint()) / out_arguments.log_base;
41 out_arguments.log_lower_bound = std::log(out_arguments.log_sign * domain[0]) / out_arguments.log_base;
42 out_arguments.log_upper_bound = std::log(out_arguments.log_sign * domain[1]) / out_arguments.log_base;
43 }
44}
45
46void device_discrete_color_scale::update_color_scale_specific_arguments(device_color_scale_arguments& out_arguments) const {
47 size_t count = std::min(color_scale->get_indexed_color_count(), device_color_scale_arguments::k_max_indexed_color_count);
48 out_arguments.indexed_color_count = static_cast<uint8_t>(count);
49 out_arguments.sample_mode = DeviceColorScaleSampleMode::kDiscrete;
50}
51
52} // namespace render
53} // namespace cgv
Base class defining an interface for color scales.
Definition color_scale.h:22
cgv::rgba get_unknown_color() const
Get the color returned for scalars outside the domain if clamping is disabled.
Definition color_scale.h:93
cgv::vec2 get_domain() const
Get the input domain of scalars that will be mapped.
Definition color_scale.h:52
bool is_clamped() const
Get whether the input values are clamped to the domain before mapping.
Definition color_scale.h:64
std::shared_ptr< cgv::media::continuous_color_scale > color_scale
The underlying host-side color scale.
device_color_scale_arguments get_arguments() const
Get the color scale arguments of the underlying color scale implementation.
std::vector< cgv::rgba > get_texture_data(size_t texture_resolution) const
Get the texture data of the underlying color scales's color ramp or indexed colors.
this header is dependency free
Definition print.h:11
Color scale arguments as used by the graphics device or shader program.
uint16_t transform
The next three fields "transform, mapping_options and sample_mode" take up 4 bytes in total.