197 lines
9.1 KiB
C++
197 lines
9.1 KiB
C++
#include "Waterfall.h"
|
|
#include "Heatmap_Utils.h"
|
|
#include "Plottable_Real_Time_Data.h"
|
|
#include "../renderable/Render_Partition.h"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <deque>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
namespace renderive {
|
|
namespace detail {
|
|
namespace {
|
|
struct Waterfall_Row {
|
|
int tick{};
|
|
std::vector<double> values;
|
|
};
|
|
struct Waterfall_Interaction_Base {};
|
|
struct Waterfall_Interaction {
|
|
Hover_Tooltip_Runtime tooltip;
|
|
};
|
|
using Waterfall_History = Plottable_History_Real_Time_Data<Waterfall_Row, std::deque<Waterfall_Row>>;
|
|
using Waterfall_Interaction_State = Double_State_Strategy<Waterfall_Interaction_Base, Waterfall_Interaction>;
|
|
struct Waterfall_Render_Frame {
|
|
Adaptive_Render_Partitioner partitioner;
|
|
Axis_Raster_Layout layout;
|
|
Frequency_Columns columns;
|
|
std::vector<Pixel> pixels;
|
|
int source_width{};
|
|
int source_height{};
|
|
int active_partitions{1};
|
|
std::size_t work_size{};
|
|
bool valid{};
|
|
};
|
|
}
|
|
struct Waterfall_Control::Impl {
|
|
Impl(Waterfall_Control& owner, std::shared_ptr<Frequency_Axis> frequency, std::shared_ptr<Time_Axis> time)
|
|
: frequency_axis(std::move(frequency)), time_axis(std::move(time)), rows(owner) {}
|
|
std::shared_ptr<Frequency_Axis> frequency_axis;
|
|
std::shared_ptr<Time_Axis> time_axis;
|
|
Waterfall_History rows;
|
|
Waterfall_Interaction_State interaction;
|
|
Waterfall_Render_Frame render_frame;
|
|
};
|
|
Waterfall_Control::Waterfall_Control(Plot_Core& plot, const Waterfall_Properties& properties, std::shared_ptr<Frequency_Axis> frequency_axis, std::shared_ptr<Time_Axis> time_axis)
|
|
: Plottable_State(plot, properties), impl_(std::make_unique<Impl>(*this, std::move(frequency_axis), std::move(time_axis))) {}
|
|
Waterfall_Control::~Waterfall_Control() = default;
|
|
void Waterfall_Control::append_row(int tick, std::span<const double> values) {
|
|
const std::size_t limit = static_cast<std::size_t>(
|
|
std::max(2, impl_->time_axis->get<&Time_Axis_Properties::visible_count>()));
|
|
if(get<&Waterfall_Properties::frequency_bin_count>() <= 0)
|
|
set<&Waterfall_Properties::frequency_bin_count>(static_cast<int>(values.size()));
|
|
impl_->rows.update({tick, {values.begin(), values.end()}}, limit);
|
|
changed();
|
|
}
|
|
void Waterfall_Control::append_row(int tick, std::pmr::vector<double>&& values) {
|
|
append_row(tick, std::span<const double>(values.data(), values.size()));
|
|
}
|
|
void Waterfall_Control::append_row(Time_Of_Day time, std::span<const double> values) {
|
|
append_row(impl_->time_axis->append_time(time), values);
|
|
}
|
|
void Waterfall_Control::append_row(Time_Of_Day time, std::pmr::vector<double>&& values) {
|
|
append_row(time, std::span<const double>(values.data(), values.size()));
|
|
}
|
|
std::size_t Waterfall_Control::row_count() const {
|
|
return impl_->rows.size();
|
|
}
|
|
std::size_t Waterfall_Control::stored_point_count() const {
|
|
const auto rows = impl_->rows.snapshot();
|
|
std::size_t count{};
|
|
for(const auto& row : rows)
|
|
count += row.values.size();
|
|
return count;
|
|
}
|
|
std::size_t Waterfall_Control::rendered_cell_count() const {
|
|
const auto state = properties();
|
|
const auto rows = impl_->rows.snapshot();
|
|
if(rows.empty())
|
|
return 0;
|
|
const int source_width = std::min(state.frequency_bin_count.get(), static_cast<int>(std::min_element(rows.begin(), rows.end(), [](const auto& left, const auto& right) { return left.values.size() < right.values.size(); })->values.size()));
|
|
if(source_width <= 0)
|
|
return 0;
|
|
const auto columns = frequency_columns(
|
|
state.frequency_range,
|
|
impl_->frequency_axis->get<&Axis_Properties::coordinates>(),
|
|
source_width,
|
|
state.visible_range_only);
|
|
return columns ? static_cast<std::size_t>(columns->last - columns->first + 1) * rows.size() : 0;
|
|
}
|
|
void Waterfall_Control::handle_event(const Event& event) {
|
|
bool updated{};
|
|
impl_->interaction.update([&](Waterfall_Interaction& interaction) { updated = update_hover_tooltip(interaction.tooltip, event); });
|
|
if(updated)
|
|
changed();
|
|
}
|
|
void Waterfall_Control::publish() {
|
|
publish_properties();
|
|
impl_->interaction.publish();
|
|
}
|
|
|
|
void Waterfall_Control::build_paint_task_graph(Renderable_Task_Graph& graph) {
|
|
const auto prepare = graph.emplace([this](const Scene_Render_Context&) {
|
|
prepare_render_frame(render_state_view());
|
|
}, "prepare waterfall");
|
|
std::array<Renderable_Task_Graph::Task, maximum_render_partitions> partitions;
|
|
for (int index = 0; index < maximum_render_partitions; ++index) {
|
|
partitions[static_cast<std::size_t>(index)] = graph.emplace(
|
|
[this, index](const Scene_Render_Context&) {
|
|
render_partition(render_state_view(), index);
|
|
},
|
|
"raster waterfall partition");
|
|
graph.precede(prepare, partitions[static_cast<std::size_t>(index)]);
|
|
}
|
|
const auto compose = add_paint_task(
|
|
graph, "compose waterfall",
|
|
[this](Painter& painter, const Render_State_View& view) { paint(painter, view); });
|
|
for (const auto partition : partitions)
|
|
graph.precede(partition, compose);
|
|
}
|
|
|
|
void Waterfall_Control::prepare_render_frame(const Render_State_View& view) {
|
|
const auto& state = render_properties(view);
|
|
const auto& rows = view.get(impl_->rows);
|
|
auto& output = impl_->render_frame;
|
|
output.valid = false;
|
|
output.work_size = 0;
|
|
if (rows.empty())
|
|
return;
|
|
const int source_width = std::min(state.frequency_bin_count.get(), static_cast<int>(std::min_element(rows.begin(), rows.end(), [](const auto& left, const auto& right) { return left.values.size() < right.values.size(); })->values.size()));
|
|
const int height = static_cast<int>(rows.size());
|
|
if (source_width <= 0 || height <= 0)
|
|
return;
|
|
const Axis_Transform frequency_axis = impl_->frequency_axis->transform(view);
|
|
const Axis_Transform time_axis = impl_->time_axis->transform(view);
|
|
const auto columns = frequency_columns(state.frequency_range, frequency_axis.coordinate_range,
|
|
source_width, state.visible_range_only);
|
|
if (!columns)
|
|
return;
|
|
const int width = columns->last - columns->first + 1;
|
|
const Range time_range = rows.size() == 1
|
|
? time_axis.coordinate_range
|
|
: Range{static_cast<double>(rows.front().tick),
|
|
static_cast<double>(rows.back().tick)};
|
|
const auto layout = axis_raster_layout(frequency_axis, time_axis, columns->range,
|
|
time_range, width, height);
|
|
if (!layout.valid())
|
|
return;
|
|
output.layout = layout;
|
|
output.columns = *columns;
|
|
output.source_width = width;
|
|
output.source_height = height;
|
|
output.work_size = static_cast<std::size_t>(width) * height;
|
|
output.pixels.resize(output.work_size);
|
|
output.active_partitions =
|
|
output.partitioner.begin(state.partition_count.get(), output.work_size);
|
|
output.valid = true;
|
|
}
|
|
|
|
void Waterfall_Control::render_partition(const Render_State_View& view, int partition_index) {
|
|
auto& output = impl_->render_frame;
|
|
if (!output.valid || partition_index >= output.active_partitions)
|
|
return;
|
|
const auto& state = render_properties(view);
|
|
const auto& rows = view.get(impl_->rows);
|
|
const auto range = render_partition_range(output.work_size, partition_index,
|
|
output.active_partitions);
|
|
for (std::size_t cell = range.first; cell < range.last; ++cell) {
|
|
const int y = static_cast<int>(cell / static_cast<std::size_t>(output.source_width));
|
|
const int x = static_cast<int>(cell % static_cast<std::size_t>(output.source_width));
|
|
const auto& row = rows[static_cast<std::size_t>(y)].values;
|
|
output.pixels[output.layout.index(x, y, output.source_width, output.source_height)] =
|
|
state.color_map.at_normalized(normalized_value(
|
|
row[static_cast<std::size_t>(output.columns.first + x)], state.power_range));
|
|
}
|
|
}
|
|
|
|
void Waterfall_Control::paint(Painter& painter, const Render_State_View& view) {
|
|
const auto& state = render_properties(view);
|
|
const auto& interaction = view.get(impl_->interaction);
|
|
auto& output = impl_->render_frame;
|
|
if (!output.valid)
|
|
return;
|
|
painter.heatmap(output.layout.target, output.layout.width, output.layout.height,
|
|
output.pixels, state.interpolation_mode);
|
|
output.partitioner.finish(output.work_size);
|
|
if(state.tooltip_enabled && interaction.tooltip.active && output.layout.target.contains(interaction.tooltip.position)) {
|
|
const Axis_Transform frequency_axis = impl_->frequency_axis->transform(view);
|
|
const double frequency = frequency_axis.point_to_coord(interaction.tooltip.position);
|
|
std::ostringstream text;
|
|
text << std::fixed << std::setprecision(2) << frequency << " Hz";
|
|
const RectF box{interaction.tooltip.position.x + 8.0, interaction.tooltip.position.y + 8.0, 110.0, 24.0};
|
|
painter.rect(box, Pen{state.tooltip_text_pen.color}, state.tooltip_background_brush);
|
|
painter.text({box.x + 4.0, box.y + 3.0}, text.str(), state.tooltip_font, state.tooltip_text_pen);
|
|
}
|
|
}
|
|
}
|
|
}
|