Files
Renderive/render_2D/plottable/Constellation_Diagram.cpp
T
2026-08-12 09:01:25 +08:00

77 lines
3.6 KiB
C++

#include "Constellation_Diagram.h"
#include "Plottable_Real_Time_Data.h"
#include "Heatmap_Utils.h"
#include "../render/Blend2D_Cache.h"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <deque>
#include <numbers>
namespace renderive {
namespace detail {
namespace {
struct Timed_Point {
PointF point;
std::chrono::steady_clock::time_point time;
};
using Constellation_History = Plottable_History_Real_Time_Data<Timed_Point, std::deque<Timed_Point>>;
}
struct Constellation_Diagram_Control::Impl {
Impl(Constellation_Diagram_Control& owner, std::shared_ptr<Axis> i, std::shared_ptr<Axis> q)
: i_axis(std::move(i)), q_axis(std::move(q)), points(owner) {}
std::shared_ptr<Axis> i_axis;
std::shared_ptr<Axis> q_axis;
Constellation_History points;
};
Constellation_Diagram_Control::Constellation_Diagram_Control(Plot_Core& plot, const Constellation_Diagram_Properties& properties, std::shared_ptr<Axis> i_axis, std::shared_ptr<Axis> q_axis)
: Plottable_State(plot, properties), impl_(std::make_unique<Impl>(*this, std::move(i_axis), std::move(q_axis))) {}
Constellation_Diagram_Control::~Constellation_Diagram_Control() = default;
void Constellation_Diagram_Control::append_point(PointF point) {
const auto now = std::chrono::steady_clock::now();
const int lifetime = get<&Constellation_Diagram_Properties::point_lifetime_ms>();
const auto cutoff = now - std::chrono::milliseconds(lifetime);
const auto cutoff_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(cutoff.time_since_epoch()).count();
impl_->points.update({point, now});
impl_->points.discard_before_time_ns(static_cast<std::uint64_t>(cutoff_ns));
changed();
}
std::size_t Constellation_Diagram_Control::point_count() const {
return impl_->points.size();
}
void Constellation_Diagram_Control::fit_square_to_axes() {
const auto state = properties();
const double side = std::max(state.i_range.size(), state.q_range.size());
impl_->i_axis->set<&Axis_Properties::coordinates>(
Range{state.i_range.center() - side * 0.5, state.i_range.center() + side * 0.5});
impl_->q_axis->set<&Axis_Properties::coordinates>(
Range{state.q_range.center() + side * 0.5, state.q_range.center() - side * 0.5});
}
void Constellation_Diagram_Control::publish() {
publish_properties();
}
void Constellation_Diagram_Control::paint(Painter& painter, const Render_State_View& view) {
const auto& state = render_properties(view);
const auto& points = view.get(impl_->points);
const Axis_Transform x = impl_->i_axis->transform(view);
const Axis_Transform y = impl_->q_axis->transform(view);
const int count = static_cast<int>(state.type);
const double radius = std::min(state.i_range.size(), state.q_range.size()) * 0.4;
for(int index = 0; index < count; ++index) {
const double angle = state.phase_offset_radians + 2.0 * std::numbers::pi * index / count;
const PointF point{state.i_range.center() + std::cos(angle) * radius, state.q_range.center() + std::sin(angle) * radius};
painter.circle(mapped_point(x, point.x, y, point.y), 3.0,
Pen{state.anchor_color},
Brush{state.anchor_color, Brush_Style::Solid});
}
const auto cutoff = std::chrono::steady_clock::now() - std::chrono::milliseconds(state.point_lifetime_ms.get());
for(const auto& value : points) {
if(value.time < cutoff)
continue;
painter.circle(mapped_point(x, value.point.x, y, value.point.y), 2.0,
Pen{state.point_color},
Brush{state.point_color, Brush_Style::Solid});
}
}
}
}