Commit 010d01b0 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

gui: update automatic layout menu item

parent d152c2bb
Pipeline #14779 passed with stage
in 1 minute and 54 seconds
...@@ -321,8 +321,6 @@ editor::group(const ImVector<int>& nodes) noexcept ...@@ -321,8 +321,6 @@ editor::group(const ImVector<int>& nodes) noexcept
return; return;
} }
iteration = 0;
/* First, move children models and groups from the current cluster into the /* First, move children models and groups from the current cluster into the
newly allocated cluster. */ newly allocated cluster. */
...@@ -440,8 +438,6 @@ editor::ungroup(const int node) noexcept ...@@ -440,8 +438,6 @@ editor::ungroup(const int node) noexcept
return; return;
} }
iteration = 0;
const auto group_id = clusters.get_id(*group); const auto group_id = clusters.get_id(*group);
top.pop(index); top.pop(index);
...@@ -830,7 +826,7 @@ compute_connection_distance(output_port& port, editor& ed, const float k) ...@@ -830,7 +826,7 @@ compute_connection_distance(output_port& port, editor& ed, const float k)
} }
void void
editor::reorder() noexcept editor::compute_grid_layout() noexcept
{ {
const auto size = length(top.children); const auto size = length(top.children);
const auto tmp = std::sqrt(size); const auto tmp = std::sqrt(size);
...@@ -872,7 +868,7 @@ editor::reorder() noexcept ...@@ -872,7 +868,7 @@ editor::reorder() noexcept
} }
void void
editor::compute_automatic_layout_iteration() noexcept editor::compute_automatic_layout() noexcept
{ {
/* See. Graph drawing by Forced-directed Placement by Thomas M. J. /* See. Graph drawing by Forced-directed Placement by Thomas M. J.
Fruchterman and Edward M. Reingold in Software--Pratice and Fruchterman and Edward M. Reingold in Software--Pratice and
...@@ -902,74 +898,77 @@ editor::compute_automatic_layout_iteration() noexcept ...@@ -902,74 +898,77 @@ editor::compute_automatic_layout_iteration() noexcept
float t = 1.f - 1.f / static_cast<float>(automatic_layout_iteration_limit); float t = 1.f - 1.f / static_cast<float>(automatic_layout_iteration_limit);
for (int i_v = 0; i_v < size; ++i_v) { for (int iteration = 0; iteration < automatic_layout_iteration_limit;
const int v = i_v; ++iteration) {
for (int i_v = 0; i_v < size; ++i_v) {
const int v = i_v;
displacements[v].x = displacements[v].y = 0.f; displacements[v].x = displacements[v].y = 0.f;
for (int i_u = 0; i_u < size; ++i_u) { for (int i_u = 0; i_u < size; ++i_u) {
const int u = i_u; const int u = i_u;
if (u != v) { if (u != v) {
const ImVec2 delta{ positions[v].x - positions[u].x, const ImVec2 delta{ positions[v].x - positions[u].x,
positions[v].y - positions[u].y }; positions[v].y - positions[u].y };
if (delta.x && delta.y) { if (delta.x && delta.y) {
const float d2 = delta.x * delta.x + delta.y * delta.y; const float d2 = delta.x * delta.x + delta.y * delta.y;
const float coeff = k_square / d2; const float coeff = k_square / d2;
displacements[v].x += coeff * delta.x; displacements[v].x += coeff * delta.x;
displacements[v].y += coeff * delta.y; displacements[v].y += coeff * delta.y;
}
} }
} }
} }
}
for (size_t i = 0, e = top.children.size(); i != e; ++i) { for (size_t i = 0, e = top.children.size(); i != e; ++i) {
if (top.children[i].first.index() == 0) { if (top.children[i].first.index() == 0) {
const auto id = std::get<model_id>(top.children[i].first); const auto id = std::get<model_id>(top.children[i].first);
if (const auto* mdl = sim.models.try_to_get(id); mdl) { if (const auto* mdl = sim.models.try_to_get(id); mdl) {
sim.for_all_output_port( sim.for_all_output_port(
*mdl, [this, k](output_port& port, output_port_id /*id*/) { *mdl,
compute_connection_distance(port, *this, k); [this, k](output_port& port, output_port_id /*id*/) {
}); compute_connection_distance(port, *this, k);
} });
} else { }
const auto id = std::get<cluster_id>(top.children[i].first); } else {
if (auto* gp = clusters.try_to_get(id); gp) { const auto id = std::get<cluster_id>(top.children[i].first);
for_each(sim.output_ports, if (auto* gp = clusters.try_to_get(id); gp) {
gp->output_ports, for_each(
[this, k](output_port& port, output_port_id /*id*/) { sim.output_ports,
compute_connection_distance(port, *this, k); gp->output_ports,
}); [this, k](output_port& port, output_port_id /*id*/) {
compute_connection_distance(port, *this, k);
});
}
} }
} }
}
auto sum = 0.f; auto sum = 0.f;
for (int i_v = 0; i_v < size; ++i_v) { for (int i_v = 0; i_v < size; ++i_v) {
const int v = i_v; const int v = i_v;
const float d2 = displacements[v].x * displacements[v].x + const float d2 = displacements[v].x * displacements[v].x +
displacements[v].y * displacements[v].y; displacements[v].y * displacements[v].y;
const float d = std::sqrt(d2); const float d = std::sqrt(d2);
if (d > t) { if (d > t) {
const float coeff = t / d; const float coeff = t / d;
displacements[v].x *= coeff; displacements[v].x *= coeff;
displacements[v].y *= coeff; displacements[v].y *= coeff;
sum += t; sum += t;
} else { } else {
sum += d; sum += d;
} }
positions[v].x += displacements[v].x; positions[v].x += displacements[v].x;
positions[v].y += displacements[v].y; positions[v].y += displacements[v].y;
imnodes::SetNodeGridSpacePos(top.children[v].second, positions[v]); imnodes::SetNodeGridSpacePos(top.children[v].second, positions[v]);
}
} }
++iteration;
} }
status status
...@@ -1135,8 +1134,6 @@ editor::add_lotka_volterra() noexcept ...@@ -1135,8 +1134,6 @@ editor::add_lotka_volterra() noexcept
parent(quantifier_a.id, undefined<cluster_id>()); parent(quantifier_a.id, undefined<cluster_id>());
parent(quantifier_b.id, undefined<cluster_id>()); parent(quantifier_b.id, undefined<cluster_id>());
iteration = 0;
return status::success; return status::success;
} }
...@@ -1301,8 +1298,6 @@ editor::add_izhikevitch() noexcept ...@@ -1301,8 +1298,6 @@ editor::add_izhikevitch() noexcept
parent(cross.id, undefined<cluster_id>()); parent(cross.id, undefined<cluster_id>());
parent(cross2.id, undefined<cluster_id>()); parent(cross2.id, undefined<cluster_id>());
iteration = 0;
return status::success; return status::success;
} }
...@@ -2106,9 +2101,9 @@ editor::show_editor() noexcept ...@@ -2106,9 +2101,9 @@ editor::show_editor() noexcept
if (ImGui::MenuItem("Clear")) if (ImGui::MenuItem("Clear"))
clear(); clear();
if (ImGui::MenuItem("Grid Reorder")) if (ImGui::MenuItem("Grid Reorder"))
reorder(); compute_grid_layout();
if (ImGui::MenuItem("Automatic Layout", nullptr, &automatic_layout)) if (ImGui::MenuItem("Automatic Layout"))
iteration = 0; compute_automatic_layout();
ImGui::EndMenu(); ImGui::EndMenu();
} }
...@@ -2195,12 +2190,6 @@ editor::show_editor() noexcept ...@@ -2195,12 +2190,6 @@ editor::show_editor() noexcept
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8.f, 8.f)); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8.f, 8.f));
if (imnodes::IsAnyAttributeActive(nullptr))
iteration = 0;
if (automatic_layout && iteration < automatic_layout_iteration_limit)
compute_automatic_layout_iteration();
if (!ImGui::IsAnyItemHovered() && ImGui::IsMouseClicked(1)) if (!ImGui::IsAnyItemHovered() && ImGui::IsMouseClicked(1))
ImGui::OpenPopup("Context menu"); ImGui::OpenPopup("Context menu");
...@@ -2222,7 +2211,6 @@ editor::show_editor() noexcept ...@@ -2222,7 +2211,6 @@ editor::show_editor() noexcept
parent(new_model, undefined<cluster_id>()); parent(new_model, undefined<cluster_id>());
imnodes::SetNodeScreenSpacePos(top.emplace_back(new_model), imnodes::SetNodeScreenSpacePos(top.emplace_back(new_model),
click_pos); click_pos);
iteration = 0;
} }
} }
......
...@@ -251,8 +251,6 @@ struct editor ...@@ -251,8 +251,6 @@ struct editor
imnodes::EditorContext* context = nullptr; imnodes::EditorContext* context = nullptr;
bool initialized = false; bool initialized = false;
bool show = true; bool show = true;
bool automatic_layout = false;
int iteration = 0;
simulation sim; simulation sim;
double simulation_begin = 0.0; double simulation_begin = 0.0;
...@@ -285,8 +283,8 @@ struct editor ...@@ -285,8 +283,8 @@ struct editor
void free_children(const ImVector<int>& nodes) noexcept; void free_children(const ImVector<int>& nodes) noexcept;
status copy(const ImVector<int>& nodes) noexcept; status copy(const ImVector<int>& nodes) noexcept;
void reorder() noexcept; void compute_grid_layout() noexcept;
void compute_automatic_layout_iteration() noexcept; void compute_automatic_layout() noexcept;
bool is_in_hierarchy(const cluster& group, bool is_in_hierarchy(const cluster& group,
const cluster_id group_to_search) const noexcept; const cluster_id group_to_search) const noexcept;
......
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