Commit adadac76 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

gui: fix plot resize

parent 295cfee7
......@@ -993,22 +993,27 @@ editor::copy(const ImVector<int>& nodes) noexcept
status
editor::initialize(u32 id) noexcept
{
if (is_bad(sim.init(static_cast<unsigned>(kernel_model_cache),
static_cast<unsigned>(kernel_message_cache))) ||
is_bad(observation_outputs.init(sim.models.capacity())) ||
is_bad(observation_types.init(sim.models.capacity())) ||
is_bad(clusters.init(sim.models.capacity())) ||
is_bad(models_mapper.init(sim.models.capacity())) ||
is_bad(clusters_mapper.init(sim.models.capacity())) ||
is_bad(models_make_transition.init(sim.models.capacity())) ||
is_bad(top.init(static_cast<unsigned>(gui_node_cache))))
return status::gui_not_enough_memory;
irt_return_if_bad(sim.init(to_unsigned(kernel_model_cache),
to_unsigned(kernel_message_cache)));
irt_return_if_bad(clusters.init(sim.models.capacity()));
irt_return_if_bad(top.init(to_unsigned(gui_node_cache)));
positions.resize(sim.models.capacity() + clusters.capacity());
displacements.resize(sim.models.capacity() + clusters.capacity(),
ImVec2{ 0.f, 0.f });
try {
observation_outputs.resize(sim.models.capacity());
observation_types.resize(sim.models.capacity(),
observation_output::type::none);
models_mapper.resize(sim.models.capacity(), undefined<cluster_id>());
clusters_mapper.resize(sim.models.capacity(), undefined<cluster_id>());
models_make_transition.resize(sim.models.capacity(), false);
std::fill_n(models_make_transition.data(), sim.models.capacity(), false);
positions.resize(sim.models.capacity() + clusters.capacity(),
ImVec2{ 0.f, 0.f });
displacements.resize(sim.models.capacity() + clusters.capacity(),
ImVec2{ 0.f, 0.f });
} catch (const std::bad_alloc& /*e*/) {
return status::gui_not_enough_memory;
}
use_real_time = false;
synchronize_timestep = 0.;
......@@ -2905,7 +2910,7 @@ show_plot_box(bool* show_plot)
for (const auto& obs : ed->observation_outputs) {
if (obs.observation_type ==
observation_output::type::multiplot &&
obs.xs.data()) {
!obs.xs.empty()) {
ImPlot::PlotLine(obs.name.c_str(),
obs.xs.data(),
obs.ys.data(),
......
......@@ -225,18 +225,17 @@ struct observation_output
observation_output() = default;
observation_output(const char* name_)
observation_output(std::string_view name_)
: name(name_)
{}
std::ofstream ofs;
std::string name;
array<float> xs;
array<float> ys;
std::vector<float> xs;
std::vector<float> ys;
double tl = 0.0;
float min = -1.f;
float max = +1.f;
int id = 0;
type observation_type = type::none;
};
......@@ -266,15 +265,15 @@ struct editor
bool simulation_show_value = false;
bool stop = false;
vector<observation_output> observation_outputs;
array<observation_output::type> observation_types;
std::vector<observation_output> observation_outputs;
std::vector<observation_output::type> observation_types;
std::filesystem::path observation_directory;
data_array<cluster, cluster_id> clusters;
array<cluster_id> clusters_mapper; /* group per cluster_id */
array<cluster_id> models_mapper; /* group per model_id */
std::vector<cluster_id> clusters_mapper; /* group per cluster_id */
std::vector<cluster_id> models_mapper; /* group per model_id */
array<bool> models_make_transition;
std::vector<bool> models_make_transition;
ImVector<ImVec2> positions;
ImVector<ImVec2> displacements;
......
......@@ -34,12 +34,11 @@ observation_output_initialize(const irt::observer& obs,
if (match(output->observation_type,
observation_output::type::multiplot,
observation_output::type::both)) {
std::fill_n(output->xs.data(), output->xs.size(), 0.f);
std::fill_n(output->ys.data(), output->ys.size(), 0.f);
output->xs.clear();
output->ys.clear();
output->tl = t;
output->min = -1.f;
output->max = +1.f;
output->id = 0;
}
if (match(output->observation_type,
......@@ -74,11 +73,8 @@ observation_output_observe(const irt::observer& obs,
for (double to_fill = output->tl; to_fill < t;
to_fill += obs.time_step) {
if (static_cast<size_t>(output->id) < output->xs.size()) {
output->ys[output->id] = value;
output->xs[output->id] = static_cast<float>(t);
++output->id;
}
output->ys.emplace_back(value);
output->xs.emplace_back(static_cast<float>(t));
}
}
......@@ -113,20 +109,22 @@ initialize_observation(window_logger& log_w, irt::editor& ed) noexcept
ed.observation_outputs.clear();
observer* obs = nullptr;
while (ed.sim.observers.next(obs)) {
auto* output = ed.observation_outputs.emplace_back(obs->name.c_str());
auto& output = ed.observation_outputs.emplace_back(obs->name.sv());
const auto type =
ed.observation_types[get_index(ed.sim.observers.get_id(*obs))];
const auto diff = ed.simulation_end - ed.simulation_begin;
const auto freq = diff / obs->time_step;
const auto length = static_cast<size_t>(freq);
output->observation_type = type;
output.observation_type = type;
if (match(type,
observation_output::type::multiplot,
observation_output::type::both)) {
output->xs.init(length);
output->ys.init(length);
output.xs.clear();
output.ys.clear();
output.xs.reserve(length);
output.ys.reserve(length);
}
if (!obs->name.empty()) {
......@@ -135,7 +133,7 @@ initialize_observation(window_logger& log_w, irt::editor& ed) noexcept
if (type == observation_output::type::file ||
type == observation_output::type::both) {
if (output->ofs.open(obs_file_path); !output->ofs.is_open())
if (output.ofs.open(obs_file_path); !output.ofs.is_open())
log_w.log(4,
"Fail to open "
"observation file: %s in "
......@@ -153,7 +151,7 @@ initialize_observation(window_logger& log_w, irt::editor& ed) noexcept
obs->initialize = &observation_output_initialize;
obs->observe = &observation_output_observe;
obs->free = &observation_output_free;
obs->user_data = static_cast<void*>(output);
obs->user_data = static_cast<void*>(&output);
}
}
......@@ -297,8 +295,7 @@ simulation_init(window_logger& log_w, editor& ed)
ed.simulation_during_date = ed.simulation_begin;
ed.st = editor_status::initializing;
std::fill_n(
ed.models_make_transition.data(), ed.sim.models.max_used(), false);
ed.models_make_transition.resize(ed.sim.models.max_used(), false);
if (ed.sim_st = ed.sim.initialize(ed.simulation_current);
irt::is_bad(ed.sim_st)) {
......@@ -414,9 +411,7 @@ show_simulation_run_debug(window_logger& log_w, editor& ed)
const auto& l = ed.sim.sched.list_model_id();
std::fill_n(
ed.models_make_transition.data(), ed.sim.models.max_used(), false);
ed.models_make_transition.resize(ed.sim.models.max_used(), false);
for (auto it = l.begin(), e = l.end(); it != e; ++it)
ed.models_make_transition[get_index(*it)] = true;
} else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment