Skip to content

Commit

Permalink
app: enables output editor to save observations
Browse files Browse the repository at this point in the history
  • Loading branch information
quesnel committed Jun 11, 2024
1 parent 51ddab0 commit 61bb518
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 122 deletions.
32 changes: 0 additions & 32 deletions app/gui/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,38 +394,6 @@ static void application_manage_menu_action(application& app) noexcept
app.menu_save_as_project_file = false;
}
}

if (app.output_ed.write_output) {
// auto* obs = app.simulation_ed.plot_obs.try_to_get(
// app.simulation_ed.selected_sim_obs);

// if (obs) {
// if (auto* mdl = app.sim.models.try_to_get(obs->model); mdl) {
// if (auto* o = app.sim.observers.try_to_get(mdl->obs_id);
// o) {
// const char* title = "Select raw file path to save";
// const std::u8string_view default_filename =
// u8"filename.txt";
// const char8_t* filters[] = { u8".txt", nullptr };

// ImGui::OpenPopup(title);
// if (app.f_dialog.show_save_file(
// title, default_filename, filters)) {
// if (app.f_dialog.state ==
// file_dialog::status::ok) {
// obs->file = app.f_dialog.result;
// obs->write(*o, obs->file);
// }

// app.simulation_ed.selected_sim_obs =
// undefined<simulation_observation_id>();
// app.output_ed.write_output = false;
// app.f_dialog.clear();
// }
// }
// }
// }
}
}

static void application_show_windows(application& app) noexcept
Expand Down
24 changes: 15 additions & 9 deletions app/gui/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,6 @@ class plot_observation_widget
void show_plot_line(const observer& obs,
const variable_observer::type_options options,
const name_str& name) noexcept;

//! Write interpolate data
void write(application& app,
const std::filesystem::path& file_path) noexcept;

std::filesystem::path file;
};

/// Use to display a grid_observation_system into ImGui widget. An instance of
Expand Down Expand Up @@ -259,10 +253,22 @@ class output_editor

void show() noexcept;

ImPlotContext* implot_context = nullptr;
bool is_open = true;

void save_obs(const variable_observer_id vobs,
const variable_observer::sub_id svobs) noexcept;
void save_copy(const plot_copy_id id) noexcept;

private:
ImPlotContext* m_ctx = nullptr;

std::filesystem::path m_file;

plot_copy_id m_copy_id = undefined<plot_copy_id>();
variable_observer_id m_vobs_id = undefined<variable_observer_id>();
variable_observer::sub_id m_sub_id = undefined<variable_observer::sub_id>();

bool write_output = false;
bool is_open = true;
enum class save_option { none, copy, obs } m_need_save = save_option::none;
};

class grid_component_editor_data
Expand Down
149 changes: 135 additions & 14 deletions app/gui/output-editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ constexpr static inline const char* plot_type_str[] = { "None",
static void show_obervers_table(application& app) noexcept
{
for (auto& vobs : app.pj.variable_observers) {
auto to_copy = std::optional<variable_observer::sub_id>();
auto to_write = std::optional<variable_observer::sub_id>();
auto to_copy = std::optional<variable_observer::sub_id>();

vobs.for_each([&](const auto id) noexcept {
const auto idx = get_index(id);
Expand Down Expand Up @@ -67,9 +66,11 @@ static void show_obervers_table(application& app) noexcept
if (ImGui::Button("copy"))
to_copy = id;
ImGui::EndDisabled();

ImGui::SameLine();
if (ImGui::Button("write"))
to_write = id;
app.output_ed.save_obs(app.pj.variable_observers.get_id(vobs),
id);

ImGui::PopID();
});
Expand All @@ -82,13 +83,6 @@ static void show_obervers_table(application& app) noexcept
new_obs.name = vobs.get_names()[get_index(*to_copy)].sv();
new_obs.linear_outputs = obs->linearized_buffer;
}

if (to_write.has_value()) {
app.output_ed.write_output = true;
auto err = std::error_code{};
auto file_path = std::filesystem::current_path(err);
app.simulation_ed.plot_obs.write(app, file_path);
}
}
}

Expand Down Expand Up @@ -126,6 +120,9 @@ static void show_copy_table(irt::application& app) noexcept

if (ImGui::Button("del"))
to_del = id;
ImGui::SameLine();
if (ImGui::Button("write"))
app.output_ed.save_copy(id);

ImGui::PopID();
}
Expand Down Expand Up @@ -160,14 +157,104 @@ static void show_observation_table(application& app) noexcept
}
}

static void write(application& app,
std::ofstream& ofs,
const variable_observer& vobs,
const unsigned idx) noexcept
{
const auto* obs = app.sim.observers.try_to_get(vobs.get_obs_ids()[idx]);

ofs.imbue(std::locale::classic());
ofs << "t," << vobs.get_names()[idx].sv() << '\n';
for (auto& v : obs->linearized_buffer)
ofs << v.x << ',' << '\n';
}

static void write(application& app,
std::ofstream& ofs,
const variable_observer_id vobs_id,
const variable_observer::sub_id obs_id) noexcept
{
ofs.imbue(std::locale::classic());

if (const auto* vobs = app.pj.variable_observers.try_to_get(vobs_id);
vobs and vobs->exists(obs_id))
write(app, ofs, *vobs, get_index(obs_id));
else
app.notifications.try_insert(log_level::error,
[](auto& title, auto& msg) noexcept {
title = "Output editor";
msg = "Unknown observation";
});
}

static void write(application& app,
const std::filesystem::path& file_path,
const variable_observer_id vobs_id,
const variable_observer::sub_id obs_id) noexcept
{
if (auto ofs = std::ofstream{ file_path }; ofs.is_open())
write(app, ofs, vobs_id, obs_id);
else
app.notifications.try_insert(
log_level::error, [&](auto& title, auto& msg) noexcept {
title = "Output editor";
format(msg,
"Failed to open file `{}' to write observation",
file_path.string());
});
}

static void write(application& app,
std::ofstream& ofs,
const plot_copy& p) noexcept
{
ofs.imbue(std::locale::classic());

ofs << "t," << p.name.sv() << '\n';

for (auto& v : p.linear_outputs)
ofs << v.x << ',' << v.y << '\n';
}

static void write(application& app,
std::ofstream& ofs,
const plot_copy_id id) noexcept
{
if (auto* p = app.simulation_ed.copy_obs.try_to_get(id); p)
write(app, ofs, *p);
else
app.notifications.try_insert(log_level::error,
[](auto& title, auto& msg) noexcept {
title = "Output editor";
msg = "Unknown copy observation";
});
}

static void write(application& app,
const std::filesystem::path& file_path,
const plot_copy_id id) noexcept
{
if (auto ofs = std::ofstream{ file_path }; ofs.is_open())
write(app, ofs, id);
else
app.notifications.try_insert(
log_level::error, [&](auto& title, auto& msg) noexcept {
title = "Output editor";
format(msg,
"Failed to open file `{}' to write observation",
file_path.string());
});
}

output_editor::output_editor() noexcept
: implot_context{ ImPlot::CreateContext() }
: m_ctx{ ImPlot::CreateContext() }
{}

output_editor::~output_editor() noexcept
{
if (implot_context)
ImPlot::DestroyContext(implot_context);
if (m_ctx)
ImPlot::DestroyContext(m_ctx);
}

void output_editor::show() noexcept
Expand All @@ -185,7 +272,7 @@ void output_editor::show() noexcept

if (ImGui::CollapsingHeader("Plots outputs",
ImGuiTreeNodeFlags_DefaultOpen)) {
ImPlot::SetCurrentContext(app.output_ed.implot_context);
ImPlot::SetCurrentContext(app.output_ed.m_ctx);

if (ImPlot::BeginPlot("Plots", ImVec2(-1, -1))) {
ImPlot::PushStyleVar(ImPlotStyleVar_LineWeight, 1.f);
Expand Down Expand Up @@ -223,6 +310,40 @@ void output_editor::show() noexcept
}

ImGui::End();

if (m_need_save != save_option::none) {
const char* title = "Select file path to save observation";
const std::u8string_view default_filename = u8"example.txt";
const char8_t* filters[] = { u8".txt", u8".dat", u8".csv", nullptr };

ImGui::OpenPopup(title);
if (app.f_dialog.show_save_file(title, default_filename, filters)) {
if (app.f_dialog.state == file_dialog::status::ok) {
m_file = app.f_dialog.result;
if (m_need_save == save_option::copy)
write(app, m_file, m_copy_id);
else if (m_need_save == save_option::obs)
write(app, m_file, m_vobs_id, m_sub_id);
}

app.f_dialog.clear();
m_need_save = save_option::none;
}
}
}

void output_editor::save_obs(const variable_observer_id vobs,
const variable_observer::sub_id svobs) noexcept
{
m_vobs_id = vobs;
m_sub_id = svobs;
m_need_save = save_option::obs;
}

void output_editor::save_copy(const plot_copy_id id) noexcept
{
m_copy_id = id;
m_need_save = save_option::copy;
}

} // namespace irt
67 changes: 0 additions & 67 deletions app/gui/plot-observation-widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,71 +95,4 @@ void plot_observation_widget::show_plot_line(
ImGui::PopID();
}

static void plot_observation_widget_write(
plot_observation_widget& /*plot_widget*/,
application& /*app*/,
std::ofstream& /*ofs*/) noexcept
{
// ofs.imbue(std::locale::classic());

// bool first_column = true;
// int size = std::numeric_limits<int>::max();

// for_specified_data(
// app.sim.observers, plot_widget.observers, [&](auto& obs) noexcept {
// if (first_column) {
// ofs << "t," << obs.name.sv();
// first_column = false;
// } else {
// ofs << "," << obs.name.sv();
// }

// size = std::min(size, obs.linearized_buffer.ssize());
// });

// ofs << '\n';

// for (int i = 0; i < size; ++i) {
// first_column = true;

// for_specified_data(
// app.sim.observers, plot_widget.observers, [&](auto& obs)
// noexcept {
// auto idx = obs.linearized_buffer.index_from_begin(i);
// if (first_column) {
// ofs << obs.linearized_buffer[idx].x << ","
// << obs.linearized_buffer[idx].y;
// first_column = false;
// } else {
// ofs << "," << obs.linearized_buffer[idx].y;
// }
// });

// ofs << '\n';
// }
}

static void notification_fail_open_file(application& app,
const std::filesystem::path& file_path,
std::string_view title) noexcept
{
auto& n = app.notifications.alloc(log_level::error);
n.title = title;
format(
n.message, "The file `{}` is not openable", file_path.generic_string());
app.notifications.enable(n);
}

void plot_observation_widget::write(
application& app,
const std::filesystem::path& file_path) noexcept
{
auto ofs = std::ofstream{ file_path };

return ofs.is_open()
? plot_observation_widget_write(*this, app, ofs)
: notification_fail_open_file(
app, file_path, "Fail to open plot observation file");
}

} // irt

0 comments on commit 61bb518

Please sign in to comment.