Files
Renderive/render_2D/plottable/Spectrum.cpp
T
2026-08-11 17:48:32 +08:00

253 lines
13 KiB
C++

#include "Spectrum.h"
#include "Curve_Sampling.h"
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <sstream>
namespace renderive {
namespace detail {
namespace {
struct Spectrum_Runtime_Base {};
struct Spectrum_Runtime {
std::vector<double> samples;
std::vector<double> maxima;
std::vector<double> minima;
std::vector<double> markers;
int selected_marker = -1;
Hover_Tooltip_Runtime tooltip;
};
using Spectrum_Runtime_State = Double_State_Strategy<Spectrum_Runtime_Base, Spectrum_Runtime>;
RectF axes_rect(const Axis_Transform& horizontal, const Axis_Transform& vertical) {
const double x1 = horizontal.coord_to_pixel(horizontal.coordinate_range.origin);
const double x2 = horizontal.coord_to_pixel(horizontal.coordinate_range.target);
const double y1 = vertical.coord_to_pixel(vertical.coordinate_range.origin);
const double y2 = vertical.coord_to_pixel(vertical.coordinate_range.target);
return {std::min(x1, x2), std::min(y1, y2), std::abs(x2 - x1), std::abs(y2 - y1)};
}
void draw_curve(Painter& painter, std::span<const double> values, Range domain, const Axis_Transform& x_axis, const Axis_Transform& y_axis, bool visible_only, Line_Interpolation_Mode interpolation, const Pen& pen, const Brush& brush) {
auto points = curve_points(values, domain, x_axis, y_axis, visible_only, interpolation);
if(points.size() < 2)
return;
if(brush.enabled()) {
std::vector<PointF> polygon;
polygon.reserve(points.size() + 2);
polygon.push_back({points.front().x, y_axis.coord_to_pixel(y_axis.coordinate_range.target)});
polygon.insert(polygon.end(), points.begin(), points.end());
polygon.push_back({points.back().x, y_axis.coord_to_pixel(y_axis.coordinate_range.target)});
painter.polygon(polygon, Pen{.style = Line_Style::None}, brush);
}
painter.polyline(points, pen);
}
double spectrum_power_at(const Spectrum_Properties& properties, const Spectrum_Runtime& runtime, double frequency, bool& ok) {
ok = false;
if(runtime.samples.empty() || !properties.frequency_range.contains(frequency) || properties.frequency_range.length() == 0.0)
return 0.0;
const double normalized = (frequency - properties.frequency_range.origin) / properties.frequency_range.length();
const double position = std::clamp(normalized, 0.0, 1.0) * static_cast<double>(runtime.samples.size() - 1);
const auto lower = static_cast<std::size_t>(std::floor(position));
const auto upper = std::min(lower + 1, runtime.samples.size() - 1);
const double fraction = position - static_cast<double>(lower);
ok = true;
return runtime.samples[lower] * (1.0 - fraction) + runtime.samples[upper] * fraction;
}
}
struct Spectrum_Control::Impl {
Impl(std::shared_ptr<Frequency_Axis> frequency, std::shared_ptr<Axis> power) : frequency_axis(std::move(frequency)), power_axis(std::move(power)) {}
std::shared_ptr<Frequency_Axis> frequency_axis;
std::shared_ptr<Axis> power_axis;
Spectrum_Runtime_State runtime;
};
Spectrum_Control::Spectrum_Control(Plot_Core& plot, const Spectrum_Properties& properties, std::shared_ptr<Frequency_Axis> frequency_axis, std::shared_ptr<Axis> power_axis)
: Plottable_State(plot, properties), impl_(std::make_unique<Impl>(std::move(frequency_axis), std::move(power_axis))) {}
Spectrum_Control::~Spectrum_Control() = default;
void Spectrum_Control::update_samples(std::span<const double> values) {
if(get<&Spectrum_Properties::frequency_point_size>() <= 0)
set<&Spectrum_Properties::frequency_point_size>(static_cast<int>(values.size()));
impl_->runtime.update([values](Spectrum_Runtime& runtime) {
runtime.samples.assign(values.begin(), values.end());
if(runtime.maxima.size() != values.size())
runtime.maxima.assign(values.begin(), values.end());
else
for(std::size_t index = 0; index < values.size(); ++index)
runtime.maxima[index] = std::max(runtime.maxima[index], values[index]);
if(runtime.minima.size() != values.size())
runtime.minima.assign(values.begin(), values.end());
else
for(std::size_t index = 0; index < values.size(); ++index)
runtime.minima[index] = std::min(runtime.minima[index], values[index]);
});
changed();
}
void Spectrum_Control::update_samples(std::pmr::vector<double>&& values) {
update_samples(std::span<const double>(values.data(), values.size()));
}
std::size_t Spectrum_Control::sample_count() const {
return impl_->runtime.read([](const Spectrum_Runtime& runtime) { return runtime.samples.size(); });
}
std::size_t Spectrum_Control::rendered_point_count() const {
const auto state = properties();
const auto runtime = impl_->runtime.read([](const Spectrum_Runtime& value) { return value; });
return curve_points(runtime.samples, state.frequency_range, impl_->frequency_axis->transform(), impl_->power_axis->transform(), state.visible_range_only, state.interpolation_mode).size();
}
double Spectrum_Control::power_at(double frequency, bool& ok) const {
const auto state = properties();
return impl_->runtime.read([&](const Spectrum_Runtime& runtime) { return spectrum_power_at(state, runtime, frequency, ok); });
}
void Spectrum_Control::add_custom_marker(double frequency) {
add_custom_line_marker(frequency);
}
void Spectrum_Control::add_custom_line_marker(double frequency) {
impl_->runtime.update([frequency](Spectrum_Runtime& runtime) { runtime.markers.push_back(frequency); });
changed();
}
void Spectrum_Control::remove_custom_marker(double frequency) {
bool removed{};
impl_->runtime.update([&](Spectrum_Runtime& runtime) {
if(runtime.markers.empty())
return;
auto closest = std::min_element(runtime.markers.begin(), runtime.markers.end(), [frequency](double left, double right) { return std::abs(left - frequency) < std::abs(right - frequency); });
const int removed_index = static_cast<int>(std::distance(runtime.markers.begin(), closest));
runtime.markers.erase(closest);
if(runtime.selected_marker == removed_index)
runtime.selected_marker = -1;
else if(runtime.selected_marker > removed_index)
--runtime.selected_marker;
removed = true;
});
if(removed)
changed();
}
void Spectrum_Control::remove_selected_marker() {
bool removed{};
impl_->runtime.update([&](Spectrum_Runtime& runtime) {
if(runtime.selected_marker < 0 || runtime.selected_marker >= static_cast<int>(runtime.markers.size()))
return;
runtime.markers.erase(runtime.markers.begin() + runtime.selected_marker);
runtime.selected_marker = -1;
removed = true;
});
if(removed)
changed();
}
void Spectrum_Control::clear_custom_markers() {
impl_->runtime.update([](Spectrum_Runtime& runtime) {
runtime.markers.clear();
runtime.selected_marker = -1;
});
changed();
}
int Spectrum_Control::selectable_line_marker_count() const {
return impl_->runtime.read([](const Spectrum_Runtime& runtime) { return static_cast<int>(runtime.markers.size()); });
}
int Spectrum_Control::selected_marker_index() const {
return impl_->runtime.get<&Spectrum_Runtime::selected_marker>();
}
void Spectrum_Control::set_selected_marker_index(int index) {
impl_->runtime.update([index](Spectrum_Runtime& runtime) { runtime.selected_marker = index >= 0 && index < static_cast<int>(runtime.markers.size()) ? index : -1; });
changed();
}
void Spectrum_Control::select_next_marker() {
impl_->runtime.update([](Spectrum_Runtime& runtime) {
if(runtime.markers.empty())
runtime.selected_marker = -1;
else
runtime.selected_marker = (runtime.selected_marker + 1) % static_cast<int>(runtime.markers.size());
});
changed();
}
void Spectrum_Control::select_previous_marker() {
impl_->runtime.update([](Spectrum_Runtime& runtime) {
if(runtime.markers.empty())
runtime.selected_marker = -1;
else
runtime.selected_marker = (runtime.selected_marker <= 0 ? static_cast<int>(runtime.markers.size()) : runtime.selected_marker) - 1;
});
changed();
}
void Spectrum_Control::clear_marker_selection() {
set_selected_marker_index(-1);
}
double Spectrum_Control::marker_frequency(int index) const {
return impl_->runtime.read([index](const Spectrum_Runtime& runtime) { return index >= 0 && index < static_cast<int>(runtime.markers.size()) ? runtime.markers[index] : 0.0; });
}
void Spectrum_Control::set_marker_frequency(int index, double frequency) {
bool updated{};
impl_->runtime.update([&](Spectrum_Runtime& runtime) {
if(index < 0 || index >= static_cast<int>(runtime.markers.size()))
return;
runtime.markers[index] = frequency;
updated = true;
});
if(updated)
changed();
}
void Spectrum_Control::set_current_marker_frequency(double frequency) {
const int index = selected_marker_index();
if(index >= 0)
set_marker_frequency(index, frequency);
}
void Spectrum_Control::handle_event(const Event& event) {
bool updated{};
impl_->runtime.update([&](Spectrum_Runtime& runtime) { updated = update_hover_tooltip(runtime.tooltip, event); });
if(updated)
changed();
}
void Spectrum_Control::publish() {
publish_properties();
impl_->runtime.publish();
}
void Spectrum_Control::paint(Painter& painter) {
const auto state = render_properties();
const auto runtime = impl_->runtime.render_use_state();
const Axis_Transform horizontal = impl_->frequency_axis->transform();
const Axis_Transform vertical = impl_->power_axis->transform();
const RectF content = axes_rect(horizontal, vertical);
if(state.sweep_region_visible) {
const double first = horizontal.coord_to_pixel(state.sweep_frequency_range.origin);
const double last = horizontal.coord_to_pixel(state.sweep_frequency_range.target);
painter.rect({std::min(first, last), content.y, std::abs(last - first), content.height}, Pen{.style = Line_Style::None}, state.sweep_region_brush);
}
if(state.max_hold_visible)
draw_curve(painter, runtime.maxima, state.frequency_range, horizontal, vertical, state.visible_range_only, state.interpolation_mode, state.max_pen, state.max_brush);
if(state.min_hold_visible)
draw_curve(painter, runtime.minima, state.frequency_range, horizontal, vertical, state.visible_range_only, state.interpolation_mode, state.min_pen, state.min_brush);
draw_curve(painter, runtime.samples, state.frequency_range, horizontal, vertical, state.visible_range_only, state.interpolation_mode, state.current_pen, state.current_brush);
if(state.middle_frequency_pen.enabled()) {
const double x = horizontal.coord_to_pixel(state.center_frequency);
painter.line({x, content.y}, {x, content.bottom()}, state.middle_frequency_pen);
}
for(std::size_t index = 0; index < runtime.markers.size(); ++index) {
const double x = horizontal.coord_to_pixel(runtime.markers[index]);
painter.line({x, content.y}, {x, content.bottom()}, static_cast<int>(index) == runtime.selected_marker ? state.selected_marker_pen : state.marker_pen);
}
if(!runtime.samples.empty() && (state.max_marker_visible || state.use_min_marker)) {
const auto draw_extreme = [&](bool maximum) {
auto iterator = maximum ? std::max_element(runtime.samples.begin(), runtime.samples.end()) : std::min_element(runtime.samples.begin(), runtime.samples.end());
const std::size_t index = static_cast<std::size_t>(std::distance(runtime.samples.begin(), iterator));
const double denominator = runtime.samples.size() > 1 ? runtime.samples.size() - 1.0 : 1.0;
const double frequency = state.frequency_range.origin + state.frequency_range.length() * index / denominator;
const PointF point{horizontal.coord_to_pixel(frequency), vertical.coord_to_pixel(*iterator)};
const Pen& pen = maximum ? state.max_pen : state.min_pen;
painter.circle(point, 3.0, pen, Brush{pen.color, Brush_Style::Solid});
};
if(state.max_marker_visible)
draw_extreme(true);
if(state.use_min_marker)
draw_extreme(false);
}
if(state.tooltip_enabled && runtime.tooltip.active && content.contains(runtime.tooltip.position)) {
const double frequency = horizontal.pixel_to_coord(runtime.tooltip.position.x);
bool ok{};
const double power = spectrum_power_at(state, runtime, frequency, ok);
if(ok) {
std::ostringstream text;
text << std::fixed << std::setprecision(2) << frequency << " Hz " << power;
const RectF box{runtime.tooltip.position.x + 8.0, runtime.tooltip.position.y + 8.0, 170.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);
}
}
}
}
}