node-editor.cpp 100 KB
Newer Older
1
2
3
4
// Copyright (c) 2020 INRA Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

Gauthier Quesnel's avatar
Gauthier Quesnel committed
5
6
7
8
9
#ifdef _WIN32
#define NOMINMAX
#define WINDOWS_LEAN_AND_MEAN
#endif

10
#include "node-editor.hpp"
Gauthier Quesnel's avatar
Gauthier Quesnel committed
11
12
#include "gui.hpp"
#include "imnodes.hpp"
13
#include "implot.h"
Gauthier Quesnel's avatar
Gauthier Quesnel committed
14

15
#include <cinttypes>
16
#include <fstream>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
17
#include <string>
18

19
#include <fmt/format.h>
20
#include <irritator/core.hpp>
21
#include <irritator/examples.hpp>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
22
#include <irritator/io.hpp>
23
24
25

namespace irt {

Gauthier Quesnel's avatar
Gauthier Quesnel committed
26
27
static ImVec4
operator*(const ImVec4& lhs, const float rhs) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
28
29
30
31
{
    return ImVec4(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs, lhs.w * rhs);
}

32
33
void
editor::settings_manager::compute_colors() noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
34
35
36
37
38
39
{
    gui_hovered_model_color =
      ImGui::ColorConvertFloat4ToU32(gui_model_color * 1.25f);
    gui_selected_model_color =
      ImGui::ColorConvertFloat4ToU32(gui_model_color * 1.5f);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
40
41
42
43
44
    gui_hovered_model_transition_color =
      ImGui::ColorConvertFloat4ToU32(gui_model_transition_color * 1.25f);
    gui_selected_model_transition_color =
      ImGui::ColorConvertFloat4ToU32(gui_model_transition_color * 1.5f);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
45
46
47
48
49
50
    gui_hovered_cluster_color =
      ImGui::ColorConvertFloat4ToU32(gui_cluster_color * 1.25f);
    gui_selected_cluster_color =
      ImGui::ColorConvertFloat4ToU32(gui_cluster_color * 1.5f);
}

51
52
53
template<size_t N, typename... Args>
void
format(small_string<N>& str, const char* fmt, const Args&... args)
54
{
55
56
    auto ret = fmt::format_to_n(str.begin(), N, fmt, args...);
    str.size(ret.size);
57
58
}

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
template<typename DataArray, typename Container, typename Function>
void
for_each(DataArray& d_array, Container& container, Function f) noexcept
{
    using identifier_type = typename DataArray::identifier_type;

    static_assert(
      std::is_same<identifier_type, typename Container::value_type>::value,
      "Container must store same identifier_type as DataArray");

    auto first = std::begin(container);
    [[maybe_unused]] auto previous = first;
    auto last = std::end(container);

    while (first != last) {
        if (auto* ptr = d_array.try_to_get(*first); ptr) {
            f(*ptr, *first);

            if constexpr (std::is_same_v<std::vector<identifier_type>,
                                         std::remove_cvref_t<Container>>) {
                ++first;
            } else if constexpr (std::is_same_v<
                                   flat_list<identifier_type>,
                                   std::remove_cvref_t<Container>>) {
                previous = first++;
            } else {
                abort();
            }
        } else {
            if constexpr (std::is_same_v<std::vector<identifier_type>,
                                         std::remove_cvref_t<Container>>) {
                std::swap(*first, container.back());
                container.pop_back();
                last = std::end(container);
            } else if constexpr (std::is_same_v<
                                   flat_list<identifier_type>,
                                   std::remove_cvref_t<Container>>) {
                if (previous == first) {
                    container.pop_front();
                    first = container.begin();
                    previous = first;
                } else {
                    first = container.erase_after(previous);
                }
            } else {
                abort();
            }
        }
    }
}

110
111
void
editor::clear() noexcept
112
{
113
114
    clusters.clear();
    sim.clear();
115

116
117
118
    std::fill(std::begin(clusters_mapper),
              std::end(clusters_mapper),
              undefined<cluster_id>());
119

120
121
122
    std::fill(std::begin(models_mapper),
              std::end(models_mapper),
              undefined<cluster_id>());
123

124
125
    top.clear();
}
126

127
128
cluster_id
editor::ancestor(const child_id child) const noexcept
129
{
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    if (child.index() == 0) {
        const auto mdl_id = std::get<model_id>(child);
        auto parent = models_mapper[get_index(mdl_id)];
        auto ret = parent;

        while (parent != undefined<cluster_id>()) {
            ret = parent;
            parent = clusters_mapper[get_index(parent)];
        }

        return ret;
    } else {
        const auto gp_id = std::get<cluster_id>(child);
        auto parent = clusters_mapper[get_index(gp_id)];
        auto ret = parent;
145

146
147
148
149
150
151
152
        while (parent != undefined<cluster_id>()) {
            ret = parent;
            parent = clusters_mapper[get_index(parent)];
        }

        return ret;
    }
153
}
154

155
156
int
editor::get_top_group_ref(const child_id child) const noexcept
157
{
158
159
160
161
162
    const auto top_ref = ancestor(child);

    return top_ref == undefined<cluster_id>() ? top.get_index(child)
                                              : top.get_index(top_ref);
}
163

164
165
166
167
bool
editor::is_in_hierarchy(const cluster& group,
                        const cluster_id group_to_search) const noexcept
{
168
169
170
    if (clusters.get_id(group) == group_to_search) {
        log_w.log(7, "clusters.get_id(group) == group_to_search\n");
        return true;
171
172
    }

173
174
175
176
    // TODO: Derecursive this part of the function.
    for (const auto elem : group.children) {
        if (elem.index() == 1) {
            const auto id = std::get<cluster_id>(elem);
177

178
179
180
181
            if (id == group_to_search) {
                log_w.log(7, "id == group_to_search\n");
                return true;
            }
182

183
            if (const auto* gp = clusters.try_to_get(id); gp) {
184
                if (is_in_hierarchy(*gp, group_to_search)) {
185
186
187
188
189
                    log_w.log(7, "is_in_hierarchy = true\n");
                    return true;
                }
            }
        }
190
191
    }

192
193
    return false;
}
194

195
196
197
198
199
200
void
editor::group(const ImVector<int>& nodes) noexcept
{
    if (!clusters.can_alloc(1)) {
        log_w.log(5, "Fail to allocate a new group.");
        return;
201
202
    }

203
204
    /* First, move children models and groups from the current cluster into the
       newly allocated cluster. */
205

206
207
208
209
    auto& new_cluster = clusters.alloc();
    auto new_cluster_id = clusters.get_id(new_cluster);
    format(new_cluster.name, "Group {}", new_cluster_id);
    parent(new_cluster_id, undefined<cluster_id>());
210

211
212
    for (int i = 0, e = nodes.size(); i != e; ++i) {
        if (auto index = top.get_index(nodes[i]); index != not_found) {
213
            new_cluster.children.push_back(top.children[index].first);
214
            top.pop(index);
215
216
        }
    }
217

218
    top.emplace_back(new_cluster_id);
219

220
221
222
223
224
225
226
227
228
    for (const auto child : new_cluster.children) {
        if (child.index() == 0) {
            const auto id = std::get<model_id>(child);
            parent(id, new_cluster_id);
        } else {
            const auto id = std::get<cluster_id>(child);
            parent(id, new_cluster_id);
        }
    }
229

230
231
232
233
234
    /* For all input and output ports of the remaining models in the current
       cluster, we try to detect if the corresponding model is or is not in the
       same cluster. */

    for (const auto child : top.children) {
235
236
        if (child.first.index() == 0) {
            const auto child_id = std::get<model_id>(child.first);
237
238
239

            if (auto* model = sim.models.try_to_get(child_id); model) {
                sim.for_all_input_port(
240
241
242
                  *model,
                  [this, &new_cluster](const input_port& port,
                                       input_port_id /*pid*/) {
243
244
245
                      for (const auto id : port.connections) {
                          if (auto* p = this->sim.output_ports.try_to_get(id);
                              p)
246
                              if (is_in_hierarchy(new_cluster,
247
248
249
250
251
252
                                                  this->parent(p->model)))
                                  new_cluster.output_ports.emplace_back(id);
                      }
                  });

                sim.for_all_output_port(
253
254
255
                  *model,
                  [this, &new_cluster](const output_port& port,
                                       output_port_id /*pid*/) {
256
257
                      for (const auto id : port.connections) {
                          if (auto* p = this->sim.input_ports.try_to_get(id); p)
258
                              if (is_in_hierarchy(new_cluster,
259
260
261
262
263
264
                                                  this->parent(p->model)))
                                  new_cluster.input_ports.emplace_back(id);
                      }
                  });
            }
        } else {
265
            const auto child_id = std::get<cluster_id>(child.first);
266
267
268
269
270
271
272

            if (auto* group = clusters.try_to_get(child_id); group) {
                for (const auto id : group->input_ports) {
                    if (auto* p = sim.input_ports.try_to_get(id); p) {
                        for (const auto d_id : p->connections) {
                            if (auto* d_p = sim.output_ports.try_to_get(d_id);
                                d_p) {
273
                                if (is_in_hierarchy(new_cluster,
274
275
276
277
                                                    this->parent(d_p->model)))
                                    new_cluster.output_ports.emplace_back(d_id);
                            }
                        }
278
279
280
                    }
                }

281
282
283
284
285
                for (const auto id : group->output_ports) {
                    if (auto* p = sim.output_ports.try_to_get(id); p) {
                        for (const auto d_id : p->connections) {
                            if (auto* d_p = sim.input_ports.try_to_get(d_id);
                                d_p) {
286
                                if (is_in_hierarchy(new_cluster,
287
288
289
290
291
                                                    this->parent(d_p->model)))
                                    new_cluster.input_ports.emplace_back(d_id);
                            }
                        }
                    }
292
                }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
293
            }
294
        }
295
296
    }
}
297

298
299
300
301
void
editor::ungroup(const int node) noexcept
{
    const auto index = top.get_index(node);
302

303
304
305
306
    if (index == not_found) {
        log_w.log(5, "ungroup model not in top\n");
        return;
    }
307

308
    if (top.children[index].first.index() == 0) {
309
310
311
        log_w.log(5, "node is not a group\n");
        return;
    }
312

313
    auto* group =
314
      clusters.try_to_get(std::get<cluster_id>(top.children[index].first));
315
316
317
318
    if (!group) {
        log_w.log(5, "group does not exist\n");
        return;
    }
319

320
321
    const auto group_id = clusters.get_id(*group);
    top.pop(index);
322

323
324
325
    for (size_t i = 0, e = group->children.size(); i != e; ++i) {
        if (group->children[i].index() == 0) {
            const auto id = std::get<model_id>(group->children[i]);
326
            if (auto* mdl = sim.models.try_to_get(id); mdl) {
327
328
329
330
331
                parent(id, undefined<cluster_id>());
                top.emplace_back(group->children[i]);
            }
        } else {
            auto id = std::get<cluster_id>(group->children[i]);
332
            if (auto* gp = clusters.try_to_get(id); gp) {
333
334
335
336
337
                parent(id, undefined<cluster_id>());
                top.emplace_back(group->children[i]);
            }
        }
    }
338

339
340
341
    clusters.free(*group);
    parent(group_id, undefined<cluster_id>());
}
342

343
344
345
346
347
348
349
350
351
352
void
editor::free_group(cluster& group) noexcept
{
    const auto group_id = clusters.get_id(group);

    for (const auto child : group.children) {
        if (child.index() == 0) {
            auto id = std::get<model_id>(child);
            models_mapper[get_index(id)] = undefined<cluster_id>();
            if (auto* mdl = sim.models.try_to_get(id); mdl) {
353
                log_w.log(7, "delete model %" PRIu64 "\n", id);
354
                sim.deallocate(id);
355

356
357
358
359
360
                observation_dispatch(
                  get_index(id),
                  [this](auto& outs, auto id) { outs.free(id); });

                observation_outputs[get_index(id)] = std::monostate{};
361
362
363
364
365
            }
        } else {
            auto id = std::get<cluster_id>(child);
            clusters_mapper[get_index(id)] = undefined<cluster_id>();
            if (auto* gp = clusters.try_to_get(id); gp) {
366
                log_w.log(7, "delete group %" PRIu64 "\n", gp);
367
368
369
370
                free_group(*gp);
            }
        }
    }
371

372
373
374
    parent(group_id, undefined<cluster_id>());
    clusters.free(group);
}
375

376
void
377
378
379
380
381
382
383
384
385
editor::free_children(const ImVector<int>& nodes) noexcept
{
    for (int i = 0, e = nodes.size(); i != e; ++i) {
        const auto index = top.get_index(nodes[i]);
        if (index == not_found)
            continue;

        const auto child = top.children[index];

386
387
        if (child.first.index() == 0) {
            const auto id = std::get<model_id>(child.first);
388
389
            if (auto* mdl = sim.models.try_to_get(id); mdl) {
                models_mapper[get_index(id)] = undefined<cluster_id>();
390
                log_w.log(7, "delete %" PRIu64 "\n", id);
391
392
                parent(id, undefined<cluster_id>());
                sim.deallocate(id);
393

394
395
396
397
398
                observation_dispatch(
                  get_index(id),
                  [this](auto& outs, const auto id) { outs.free(id); });

                observation_outputs[get_index(id)] = std::monostate{};
399
400
            }
        } else {
401
            const auto id = std::get<cluster_id>(child.first);
402
403
            if (auto* gp = clusters.try_to_get(id); gp) {
                clusters_mapper[get_index(id)] = undefined<cluster_id>();
404
                log_w.log(7, "delete group %" PRIu64 "\n", id);
405
406
407
                free_group(*gp);
            }
        }
408

409
410
411
        top.pop(index);
    }
}
412

413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
struct copier
{
    struct copy_model
    {
        copy_model() = default;

        copy_model(const model_id src_, const model_id dst_) noexcept
          : src(src_)
          , dst(dst_)
        {}

        model_id src, dst;
    };

    struct copy_cluster
    {
        copy_cluster() = default;

        copy_cluster(const cluster_id src_, const cluster_id dst_) noexcept
          : src(src_)
          , dst(dst_)
        {}

        cluster_id src, dst;
    };

    struct copy_input_port
    {
        copy_input_port() = default;

        copy_input_port(const input_port_id src_,
                        const input_port_id dst_) noexcept
          : src(src_)
          , dst(dst_)
        {}

        input_port_id src, dst;
    };

    struct copy_output_port
    {
        copy_output_port() = default;

        copy_output_port(const output_port_id src_,
                         const output_port_id dst_) noexcept
          : src(src_)
          , dst(dst_)
        {}

        output_port_id src, dst;
    };

    std::vector<copy_model> c_models;
    std::vector<copy_cluster> c_clusters;
    std::vector<copy_input_port> c_input_ports;
    std::vector<copy_output_port> c_output_ports;

    void sort() noexcept
    {
        std::sort(std::begin(c_models),
                  std::end(c_models),
                  [](const auto left, const auto right) {
                      return static_cast<u64>(left.src) <
                             static_cast<u64>(right.src);
                  });

        std::sort(std::begin(c_clusters),
                  std::end(c_clusters),
                  [](const auto left, const auto right) {
                      return static_cast<u64>(left.src) <
                             static_cast<u64>(right.src);
                  });

        std::sort(std::begin(c_input_ports),
                  std::end(c_input_ports),
                  [](const auto left, const auto right) {
                      return static_cast<u64>(left.src) <
                             static_cast<u64>(right.src);
                  });

        std::sort(std::begin(c_output_ports),
                  std::end(c_output_ports),
                  [](const auto left, const auto right) {
                      return static_cast<u64>(left.src) <
                             static_cast<u64>(right.src);
                  });
    }

    template<typename Container, typename T>
    static int get(const Container& c, const T src) noexcept
    {
        const typename Container::value_type val = { src, undefined<T>() };

        auto it = std::lower_bound(std::begin(c),
                                   std::end(c),
                                   val,
                                   [](const auto& left, const auto& right) {
                                       return static_cast<u64>(left.src) <
                                              static_cast<u64>(right.src);
                                   });

        return (it != std::end(c) &&
                static_cast<u64>(src) == static_cast<u64>(it->src))
                 ? static_cast<int>(std::distance(std::begin(c), it))
                 : not_found;
    }

    int get_model(const model_id src) const noexcept
    {
        return get(c_models, src);
    }

    int get_cluster(const cluster_id src) const noexcept
    {
        return get(c_clusters, src);
    }

    int get_input_port(const input_port_id src) const noexcept
    {
        return get(c_input_ports, src);
    }

    int get_output_port(const output_port_id src) const noexcept
    {
        return get(c_output_ports, src);
    }

    status copy(editor& ed,
                const size_t models_to_merge_with_top,
                const size_t clusters_to_merge_with_top)
    {
        auto& sim = ed.sim;

        for (size_t i = 0, e = std::size(c_models); i != e; ++i) {
            auto* mdl = sim.models.try_to_get(c_models[i].src);
            auto* mdl_id_dst = &c_models[i].dst;

550
            auto ret = sim.dispatch(
551
              mdl->type,
Gauthier Quesnel's avatar
Gauthier Quesnel committed
552
553
554
              [this, &sim, mdl, &mdl_id_dst]<typename DynamicsM>(
                DynamicsM& dynamics_models) -> status {
                  using Dynamics = typename DynamicsM::value_type;
555

Gauthier Quesnel's avatar
Gauthier Quesnel committed
556
557
                  irt_return_if_fail(dynamics_models.can_alloc(1),
                                     status::dynamics_not_enough_memory);
558

Gauthier Quesnel's avatar
Gauthier Quesnel committed
559
560
                  auto* dyn_ptr = dynamics_models.try_to_get(mdl->id);
                  irt_return_if_fail(dyn_ptr, status::dynamics_unknown_id);
561

Gauthier Quesnel's avatar
Gauthier Quesnel committed
562
563
                  auto& new_dyn = dynamics_models.alloc(*dyn_ptr);
                  auto new_dyn_id = dynamics_models.get_id(new_dyn);
564

Gauthier Quesnel's avatar
Gauthier Quesnel committed
565
566
567
568
                  if constexpr (is_detected_v<has_input_port_t, Dynamics>)
                      std::fill_n(new_dyn.x,
                                  std::size(new_dyn.x),
                                  static_cast<input_port_id>(0));
569

Gauthier Quesnel's avatar
Gauthier Quesnel committed
570
571
572
573
                  if constexpr (is_detected_v<has_output_port_t, Dynamics>)
                      std::fill_n(new_dyn.y,
                                  std::size(new_dyn.y),
                                  static_cast<output_port_id>(0));
574

575
                  irt_return_if_bad(sim.alloc(new_dyn, new_dyn_id));
576

Gauthier Quesnel's avatar
Gauthier Quesnel committed
577
                  *mdl_id_dst = new_dyn.id;
578

Gauthier Quesnel's avatar
Gauthier Quesnel committed
579
580
581
582
583
                  if constexpr (is_detected_v<has_input_port_t, Dynamics>)
                      for (size_t j = 0, ej = std::size(new_dyn.x); j != ej;
                           ++j)
                          this->c_input_ports.emplace_back(dyn_ptr->x[j],
                                                           new_dyn.x[j]);
584

Gauthier Quesnel's avatar
Gauthier Quesnel committed
585
586
587
588
589
                  if constexpr (is_detected_v<has_output_port_t, Dynamics>)
                      for (size_t j = 0, ej = std::size(new_dyn.y); j != ej;
                           ++j)
                          this->c_output_ports.emplace_back(dyn_ptr->y[j],
                                                            new_dyn.y[j]);
590

Gauthier Quesnel's avatar
Gauthier Quesnel committed
591
592
                  return status::success;
              });
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693

            irt_return_if_bad(ret);
        }

        for (size_t i = 0, e = std::size(c_clusters); i != e; ++i) {
            auto* gp_src = ed.clusters.try_to_get(c_clusters[i].src);
            auto& gp_dst = ed.clusters.alloc(*gp_src);
            c_clusters[i].dst = ed.clusters.get_id(gp_dst);
        }

        sort();

        for (size_t i = 0, e = std::size(c_clusters); i != e; ++i) {
            auto* gp_src = ed.clusters.try_to_get(c_clusters[i].src);
            auto* gp_dst = ed.clusters.try_to_get(c_clusters[i].dst);

            for (size_t j = 0, ej = gp_src->children.size(); j != ej; ++j) {
                if (gp_src->children[j].index() == 0) {
                    const auto id = std::get<model_id>(gp_src->children[j]);
                    const auto index = get_model(id);
                    gp_dst->children[j] = c_models[index].dst;
                } else {
                    const auto id = std::get<cluster_id>(gp_src->children[j]);
                    const auto index = get_cluster(id);
                    gp_dst->children[j] = c_clusters[index].dst;
                }
            }

            for (size_t j = 0, ej = gp_src->input_ports.size(); j != ej; ++j) {
                const auto index = get_input_port(gp_src->input_ports[j]);
                gp_dst->input_ports[j] = c_input_ports[index].dst;
            }

            for (size_t j = 0, ej = gp_src->output_ports.size(); j != ej; ++j) {
                const auto index = get_output_port(gp_src->output_ports[j]);
                gp_dst->output_ports[j] = c_output_ports[index].dst;
            }
        }

        for (size_t i = 0, e = std::size(c_input_ports); i != e; ++i) {
            const auto* src = sim.input_ports.try_to_get(c_input_ports[i].src);
            auto* dst = sim.input_ports.try_to_get(c_input_ports[i].dst);

            assert(dst->connections.empty());

            for (const auto port : src->connections) {
                const auto index = get_output_port(port);
                dst->connections.emplace_front(c_output_ports[index].dst);
            }
        }

        for (size_t i = 0, e = std::size(c_output_ports); i != e; ++i) {
            const auto* src =
              sim.output_ports.try_to_get(c_output_ports[i].src);
            auto* dst = sim.output_ports.try_to_get(c_output_ports[i].dst);

            assert(dst->connections.empty());

            for (const auto port : src->connections) {
                const auto index = get_input_port(port);
                dst->connections.emplace_front(c_input_ports[index].dst);
            }
        }

        for (size_t i = 0, e = std::size(c_models); i != e; ++i) {
            const auto parent_src = ed.parent(c_models[i].src);
            const auto index = get_cluster(parent_src);

            if (index == not_found)
                ed.parent(c_models[i].dst, parent_src);
            else
                ed.parent(c_models[i].dst, c_clusters[index].dst);
        }

        for (size_t i = 0, e = std::size(c_clusters); i != e; ++i) {
            const auto parent_src = ed.parent(c_clusters[i].src);
            const auto index = get_cluster(parent_src);

            if (index == not_found)
                ed.parent(c_models[i].dst, parent_src);
            else
                ed.parent(c_models[i].dst, c_clusters[index].dst);
        }

        /* Finally, merge clusters and models from user selection into the
           editor.top structure. */

        for (size_t i = 0; i != models_to_merge_with_top; ++i) {
            ed.top.emplace_back(c_models[i].dst);
            ed.parent(c_models[i].dst, undefined<cluster_id>());
        }

        for (size_t i = 0; i != clusters_to_merge_with_top; ++i) {
            ed.top.emplace_back(c_clusters[i].dst);
            ed.parent(c_clusters[i].dst, undefined<cluster_id>());
        }

        return status::success;
    }
};

694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
static void
compute_connection_distance(output_port& port, editor& ed, const float k)
{
    for_each(ed.sim.input_ports,
             port.connections,
             [&](const auto& i_port, const auto /*i_id*/) {
                 const auto v = ed.get_top_group_ref(port.model);
                 const auto u = ed.get_top_group_ref(i_port.model);

                 const float dx = ed.positions[v].x - ed.positions[u].x;
                 const float dy = ed.positions[v].y - ed.positions[u].y;
                 if (dx && dy) {
                     const float d2 = dx * dx / dy * dy;
                     const float coeff = std::sqrt(d2) / k;

                     ed.displacements[v].x -= dx * coeff;
                     ed.displacements[v].y -= dy * coeff;
                     ed.displacements[u].x += dx * coeff;
                     ed.displacements[u].y += dy * coeff;
                 }
             });
}

void
718
editor::compute_grid_layout() noexcept
719
720
{
    const auto size = length(top.children);
721
722
723
724

    if (size == 0)
        return;

725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
    const auto tmp = std::sqrt(size);
    const auto column = static_cast<int>(tmp);
    auto line = column;
    auto remaining = size - (column * line);

    while (remaining > column) {
        ++line;
        remaining -= column;
    }

    const auto panning = imnodes::EditorContextGetPanning();
    auto new_pos = panning;

    int elem = 0;

    for (int i = 0; i < column; ++i) {
741
742
        new_pos.y =
          panning.y + static_cast<float>(i) * settings.grid_layout_y_distance;
743
        for (int j = 0; j < line; ++j) {
744
745
            new_pos.x = panning.x +
                        static_cast<float>(j) * settings.grid_layout_x_distance;
746
            imnodes::SetNodeGridSpacePos(top.children[elem].second, new_pos);
747
748
749
750
751
752
753
            positions[elem].x = new_pos.x;
            positions[elem].y = new_pos.y;
            ++elem;
        }
    }

    new_pos.x = panning.x;
754
755
    new_pos.y =
      panning.y + static_cast<float>(column) * settings.grid_layout_y_distance;
756
    for (int j = 0; j < remaining; ++j) {
757
758
        new_pos.x =
          panning.x + static_cast<float>(j) * settings.grid_layout_x_distance;
759
        imnodes::SetNodeGridSpacePos(top.children[elem].second, new_pos);
760
761
762
763
        positions[elem].x = new_pos.x;
        positions[elem].y = new_pos.y;
        ++elem;
    }
764

765
    imnodes::EditorContextResetPanning(positions[0]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
766
}
767

Gauthier Quesnel's avatar
Gauthier Quesnel committed
768
void
769
editor::compute_automatic_layout() noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
770
{
771
772
    /* See. Graph drawing by Forced-directed Placement by Thomas M. J.
       Fruchterman and Edward M. Reingold in Software--Pratice and
Gauthier Quesnel's avatar
Gauthier Quesnel committed
773
774
775
776
777
778
779
780
781
782
783
784
785
       Experience, Vol. 21(1 1), 1129-1164 (november 1991).
       */

    const auto size = length(top.children);
    const auto tmp = std::sqrt(size);
    const auto column = static_cast<int>(tmp);
    auto line = column;
    auto remaining = size - (column * line);

    while (remaining > column) {
        ++line;
        remaining -= column;
    }
786

787
788
789
790
    const float W =
      static_cast<float>(column) * settings.automatic_layout_x_distance;
    const float L =
      line + (remaining > 0) ? settings.automatic_layout_y_distance : 0.f;
791
792
793
794
    const float area = W * L;
    const float k_square = area / static_cast<float>(top.children.size());
    const float k = std::sqrt(k_square);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
795
796
797
798
    // float t = 1.f - static_cast<float>(iteration) /
    //                   static_cast<float>(automatic_layout_iteration_limit);
    // t *= t;

799
800
    float t =
      1.f - 1.f / static_cast<float>(settings.automatic_layout_iteration_limit);
801

802
803
    for (int iteration = 0;
         iteration < settings.automatic_layout_iteration_limit;
804
805
806
         ++iteration) {
        for (int i_v = 0; i_v < size; ++i_v) {
            const int v = i_v;
807

808
            displacements[v].x = displacements[v].y = 0.f;
809

810
811
            for (int i_u = 0; i_u < size; ++i_u) {
                const int u = i_u;
812

813
814
815
                if (u != v) {
                    const ImVec2 delta{ positions[v].x - positions[u].x,
                                        positions[v].y - positions[u].y };
816

817
818
819
                    if (delta.x && delta.y) {
                        const float d2 = delta.x * delta.x + delta.y * delta.y;
                        const float coeff = k_square / d2;
820

821
822
823
                        displacements[v].x += coeff * delta.x;
                        displacements[v].y += coeff * delta.y;
                    }
824
825
826
                }
            }
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
827

828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
        for (size_t i = 0, e = top.children.size(); i != e; ++i) {
            if (top.children[i].first.index() == 0) {
                const auto id = std::get<model_id>(top.children[i].first);
                if (const auto* mdl = sim.models.try_to_get(id); mdl) {
                    sim.for_all_output_port(
                      *mdl,
                      [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);
                if (auto* gp = clusters.try_to_get(id); gp) {
                    for_each(
                      sim.output_ports,
                      gp->output_ports,
                      [this, k](output_port& port, output_port_id /*id*/) {
                          compute_connection_distance(port, *this, k);
                      });
                }
848
849
850
            }
        }

851
852
853
        auto sum = 0.f;
        for (int i_v = 0; i_v < size; ++i_v) {
            const int v = i_v;
854

855
856
857
            const float d2 = displacements[v].x * displacements[v].x +
                             displacements[v].y * displacements[v].y;
            const float d = std::sqrt(d2);
858

859
860
861
862
863
864
865
866
            if (d > t) {
                const float coeff = t / d;
                displacements[v].x *= coeff;
                displacements[v].y *= coeff;
                sum += t;
            } else {
                sum += d;
            }
867

868
869
            positions[v].x += displacements[v].x;
            positions[v].y += displacements[v].y;
870

871
872
            imnodes::SetNodeGridSpacePos(top.children[v].second, positions[v]);
        }
873
    }
874

875
    imnodes::EditorContextResetPanning(positions[0]);
876
877
}

878
879
status
editor::copy(const ImVector<int>& nodes) noexcept
880
{
881
882
883
884
885
886
887
888
889
890
891
    copier cp;

    std::vector<cluster_id> copy_stack;

    for (int i = 0, e = nodes.size(); i != e; ++i) {
        const auto index = top.get_index(nodes[i]);
        if (index == not_found)
            continue;

        const auto child = top.children[index];

892
893
        if (child.first.index() == 0) {
            const auto id = std::get<model_id>(child.first);
894
895
896
            if (auto* mdl = sim.models.try_to_get(id); mdl)
                cp.c_models.emplace_back(id, undefined<model_id>());
        } else {
897
            const auto id = std::get<cluster_id>(child.first);
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
            if (auto* gp = clusters.try_to_get(id); gp) {
                cp.c_clusters.emplace_back(id, undefined<cluster_id>());
                copy_stack.emplace_back(id);
            }
        }
    }

    const auto models_to_merge_with_top = std::size(cp.c_models);
    const auto clusters_to_merge_with_top = std::size(cp.c_clusters);

    while (!copy_stack.empty()) {
        const auto gp_id = copy_stack.back();
        copy_stack.pop_back();

        if (auto* gp = clusters.try_to_get(gp_id); gp) {
            for (const auto child : gp->children) {
                if (child.index() == 0) {
                    const auto id = std::get<model_id>(child);
                    if (auto* mdl = sim.models.try_to_get(id); mdl)
                        cp.c_models.emplace_back(id, undefined<model_id>());
                } else {
                    const auto id = std::get<cluster_id>(child);
                    if (auto* gp = clusters.try_to_get(id); gp) {
                        cp.c_clusters.emplace_back(id, undefined<cluster_id>());
                        copy_stack.emplace_back(id);
                    }
                }
            }
        }
    }

    return cp.copy(*this, models_to_merge_with_top, clusters_to_merge_with_top);
930
}
931

932
933
934
status
editor::initialize(u32 id) noexcept
{
935
936
    irt_return_if_bad(sim.init(to_unsigned(settings.kernel_model_cache),
                               to_unsigned(settings.kernel_message_cache)));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
937
    irt_return_if_bad(clusters.init(sim.models.capacity()));
938
939
940
    irt_return_if_bad(top.init(to_unsigned(settings.gui_node_cache)));
    irt_return_if_bad(plot_outs.init(to_unsigned(settings.kernel_model_cache)));
    irt_return_if_bad(file_outs.init(to_unsigned(settings.kernel_model_cache)));
941
942
    irt_return_if_bad(
      file_discrete_outs.init(to_unsigned(settings.kernel_model_cache)));
943

Gauthier Quesnel's avatar
Gauthier Quesnel committed
944
945
946
947
948
    try {
        observation_outputs.resize(sim.models.capacity());
        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);
949

Gauthier Quesnel's avatar
Gauthier Quesnel committed
950
951
952
953
        positions.resize(sim.models.capacity() + clusters.capacity(),
                         ImVec2{ 0.f, 0.f });
        displacements.resize(sim.models.capacity() + clusters.capacity(),
                             ImVec2{ 0.f, 0.f });
954
955
956

        observation_directory = std::filesystem::current_path();

Gauthier Quesnel's avatar
Gauthier Quesnel committed
957
958
959
    } catch (const std::bad_alloc& /*e*/) {
        return status::gui_not_enough_memory;
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
960

961
962
963
    use_real_time = false;
    synchronize_timestep = 0.;

964
    format(name, "Editor {}", id);
965

966
    initialized = true;
967

968
969
    return status::success;
}
970

971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
status
editor::add_lotka_volterra() noexcept
{
    if (!sim.adder_2_models.can_alloc(2) || !sim.mult_2_models.can_alloc(2) ||
        !sim.integrator_models.can_alloc(2) ||
        !sim.quantifier_models.can_alloc(2) || !sim.models.can_alloc(10))
        return status::simulation_not_enough_model;

    auto& sum_a = sim.adder_2_models.alloc();
    auto& sum_b = sim.adder_2_models.alloc();
    auto& product = sim.mult_2_models.alloc();
    auto& integrator_a = sim.integrator_models.alloc();
    auto& integrator_b = sim.integrator_models.alloc();
    auto& quantifier_a = sim.quantifier_models.alloc();
    auto& quantifier_b = sim.quantifier_models.alloc();

    integrator_a.default_current_value = 18.0;

    quantifier_a.default_adapt_state = irt::quantifier::adapt_state::possible;
    quantifier_a.default_zero_init_offset = true;
    quantifier_a.default_step_size = 0.01;
    quantifier_a.default_past_length = 3;

    integrator_b.default_current_value = 7.0;

    quantifier_b.default_adapt_state = irt::quantifier::adapt_state::possible;
    quantifier_b.default_zero_init_offset = true;
    quantifier_b.default_step_size = 0.01;
    quantifier_b.default_past_length = 3;

    product.default_input_coeffs[0] = 1.0;
    product.default_input_coeffs[1] = 1.0;
    sum_a.default_input_coeffs[0] = 2.0;
    sum_a.default_input_coeffs[1] = -0.4;
    sum_b.default_input_coeffs[0] = -1.0;
    sum_b.default_input_coeffs[1] = 0.1;

1008
1009
1010
    irt_return_if_bad(sim.alloc(sum_a, sim.adder_2_models.get_id(sum_a)));
    irt_return_if_bad(sim.alloc(sum_b, sim.adder_2_models.get_id(sum_b)));
    irt_return_if_bad(sim.alloc(product, sim.mult_2_models.get_id(product)));
1011
    irt_return_if_bad(
1012
      sim.alloc(integrator_a, sim.integrator_models.get_id(integrator_a)));
1013
    irt_return_if_bad(
1014
      sim.alloc(integrator_b, sim.integrator_models.get_id(integrator_b)));
1015
    irt_return_if_bad(
1016
1017
1018
      sim.alloc(quantifier_a, sim.quantifier_models.get_id(quantifier_a)));
    irt_return_if_bad(
      sim.alloc(quantifier_b, sim.quantifier_models.get_id(quantifier_b)));
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054

    irt_return_if_bad(sim.connect(sum_a.y[0], integrator_a.x[1]));
    irt_return_if_bad(sim.connect(sum_b.y[0], integrator_b.x[1]));

    irt_return_if_bad(sim.connect(integrator_a.y[0], sum_a.x[0]));
    irt_return_if_bad(sim.connect(integrator_b.y[0], sum_b.x[0]));

    irt_return_if_bad(sim.connect(integrator_a.y[0], product.x[0]));
    irt_return_if_bad(sim.connect(integrator_b.y[0], product.x[1]));

    irt_return_if_bad(sim.connect(product.y[0], sum_a.x[1]));
    irt_return_if_bad(sim.connect(product.y[0], sum_b.x[1]));

    irt_return_if_bad(sim.connect(quantifier_a.y[0], integrator_a.x[0]));
    irt_return_if_bad(sim.connect(quantifier_b.y[0], integrator_b.x[0]));
    irt_return_if_bad(sim.connect(integrator_a.y[0], quantifier_a.x[0]));
    irt_return_if_bad(sim.connect(integrator_b.y[0], quantifier_b.x[0]));

    top.emplace_back(sum_a.id);
    top.emplace_back(sum_b.id);
    top.emplace_back(product.id);
    top.emplace_back(integrator_a.id);
    top.emplace_back(integrator_b.id);
    top.emplace_back(quantifier_a.id);
    top.emplace_back(quantifier_b.id);

    parent(sum_a.id, undefined<cluster_id>());
    parent(sum_b.id, undefined<cluster_id>());
    parent(product.id, undefined<cluster_id>());
    parent(integrator_a.id, undefined<cluster_id>());
    parent(integrator_b.id, undefined<cluster_id>());
    parent(quantifier_a.id, undefined<cluster_id>());
    parent(quantifier_b.id, undefined<cluster_id>());

    return status::success;
}
1055

1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
status
editor::add_izhikevitch() noexcept
{
    if (!sim.constant_models.can_alloc(3) || !sim.adder_2_models.can_alloc(3) ||
        !sim.adder_4_models.can_alloc(1) || !sim.mult_2_models.can_alloc(1) ||
        !sim.integrator_models.can_alloc(2) ||
        !sim.quantifier_models.can_alloc(2) || !sim.cross_models.can_alloc(2) ||
        !sim.models.can_alloc(14))
        return status::simulation_not_enough_model;

    auto& constant = sim.constant_models.alloc();
    auto& constant2 = sim.constant_models.alloc();
    auto& constant3 = sim.constant_models.alloc();
    auto& sum_a = sim.adder_2_models.alloc();
    auto& sum_b = sim.adder_2_models.alloc();
    auto& sum_c = sim.adder_4_models.alloc();
    auto& sum_d = sim.adder_2_models.alloc();
    auto& product = sim.mult_2_models.alloc();
    auto& integrator_a = sim.integrator_models.alloc();
    auto& integrator_b = sim.integrator_models.alloc();
    auto& quantifier_a = sim.quantifier_models.alloc();
    auto& quantifier_b = sim.quantifier_models.alloc();
    auto& cross = sim.cross_models.alloc();
    auto& cross2 = sim.cross_models.alloc();

    double a = 0.2;
    double b = 2.0;
    double c = -56.0;
    double d = -16.0;
    double I = -99.0;
    double vt = 30.0;

    constant.default_value = 1.0;
    constant2.default_value = c;
    constant3.default_value = I;

    cross.default_threshold = vt;
    cross2.default_threshold = vt;

    integrator_a.default_current_value = 0.0;

    quantifier_a.default_adapt_state = irt::quantifier::adapt_state::possible;
    quantifier_a.default_zero_init_offset = true;
    quantifier_a.default_step_size = 0.01;
    quantifier_a.default_past_length = 3;

    integrator_b.default_current_value = 0.0;

    quantifier_b.default_adapt_state = irt::quantifier::adapt_state::possible;
    quantifier_b.default_zero_init_offset = true;
    quantifier_b.default_step_size = 0.01;
    quantifier_b.default_past_length = 3;

    product.default_input_coeffs[0] = 1.0;
    product.default_input_coeffs[1] = 1.0;

    sum_a.default_input_coeffs[0] = 1.0;
    sum_a.default_input_coeffs[1] = -1.0;
    sum_b.default_input_coeffs[0] = -a;
    sum_b.default_input_coeffs[1] = a * b;
    sum_c.default_input_coeffs[0] = 0.04;
    sum_c.default_input_coeffs[1] = 5.0;
    sum_c.default_input_coeffs[2] = 140.0;
    sum_c.default_input_coeffs[3] = 1.0;
    sum_d.default_input_coeffs[0] = 1.0;
    sum_d.default_input_coeffs[1] = d;

    irt_return_if_bad(
1124
      sim.alloc(constant3, sim.constant_models.get_id(constant3)));
1125
    irt_return_if_bad(
1126
      sim.alloc(constant, sim.constant_models.get_id(constant)));
1127
    irt_return_if_bad(
1128
      sim.alloc(constant2, sim.constant_models.get_id(constant2)));
1129

1130
1131
1132
1133
    irt_return_if_bad(sim.alloc(sum_a, sim.adder_2_models.get_id(sum_a)));
    irt_return_if_bad(sim.alloc(sum_b, sim.adder_2_models.get_id(sum_b)));
    irt_return_if_bad(sim.alloc(sum_c, sim.adder_4_models.get_id(sum_c)));
    irt_return_if_bad(sim.alloc(sum_d, sim.adder_2_models.get_id(sum_d)));
1134

1135
1136
1137
    irt_return_if_bad(sim.alloc(product, sim.mult_2_models.get_id(product)));
    irt_return_if_bad(
      sim.alloc(integrator_a, sim.integrator_models.get_id(integrator_a)));
1138
    irt_return_if_bad(
1139
      sim.alloc(integrator_b, sim.integrator_models.get_id(integrator_b)));
1140
    irt_return_if_bad(
1141
      sim.alloc(quantifier_a, sim.quantifier_models.get_id(quantifier_a)));
1142
    irt_return_if_bad(
1143
1144
1145
      sim.alloc(quantifier_b, sim.quantifier_models.get_id(quantifier_b)));
    irt_return_if_bad(sim.alloc(cross, sim.cross_models.get_id(cross)));
    irt_return_if_bad(sim.alloc(cross2, sim.cross_models.get_id(cross2)));
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211

    irt_return_if_bad(sim.connect(integrator_a.y[0], cross.x[0]));
    irt_return_if_bad(sim.connect(constant2.y[0], cross.x[1]));
    irt_return_if_bad(sim.connect(integrator_a.y[0], cross.x[2]));

    irt_return_if_bad(sim.connect(cross.y[0], quantifier_a.x[0]));
    irt_return_if_bad(sim.connect(cross.y[0], product.x[0]));
    irt_return_if_bad(sim.connect(cross.y[0], product.x[1]));
    irt_return_if_bad(sim.connect(product.y[0], sum_c.x[0]));
    irt_return_if_bad(sim.connect(cross.y[0], sum_c.x[1]));
    irt_return_if_bad(sim.connect(cross.y[0], sum_b.x[1]));

    irt_return_if_bad(sim.connect(constant.y[0], sum_c.x[2]));
    irt_return_if_bad(sim.connect(constant3.y[0], sum_c.x[3]));

    irt_return_if_bad(sim.connect(sum_c.y[0], sum_a.x[0]));
    irt_return_if_bad(sim.connect(integrator_b.y[0], sum_a.x[1]));
    irt_return_if_bad(sim.connect(cross2.y[0], sum_a.x[1]));
    irt_return_if_bad(sim.connect(sum_a.y[0], integrator_a.x[1]));
    irt_return_if_bad(sim.connect(cross.y[0], integrator_a.x[2]));
    irt_return_if_bad(sim.connect(quantifier_a.y[0], integrator_a.x[0]));

    irt_return_if_bad(sim.connect(cross2.y[0], quantifier_b.x[0]));
    irt_return_if_bad(sim.connect(cross2.y[0], sum_b.x[0]));
    irt_return_if_bad(sim.connect(quantifier_b.y[0], integrator_b.x[0]));
    irt_return_if_bad(sim.connect(sum_b.y[0], integrator_b.x[1]));

    irt_return_if_bad(sim.connect(cross2.y[0], integrator_b.x[2]));
    irt_return_if_bad(sim.connect(integrator_a.y[0], cross2.x[0]));
    irt_return_if_bad(sim.connect(integrator_b.y[0], cross2.x[2]));
    irt_return_if_bad(sim.connect(sum_d.y[0], cross2.x[1]));
    irt_return_if_bad(sim.connect(integrator_b.y[0], sum_d.x[0]));
    irt_return_if_bad(sim.connect(constant.y[0], sum_d.x[1]));

    top.emplace_back(constant.id);
    top.emplace_back(constant2.id);
    top.emplace_back(constant3.id);
    top.emplace_back(sum_a.id);
    top.emplace_back(sum_b.id);
    top.emplace_back(sum_c.id);
    top.emplace_back(sum_d.id);
    top.emplace_back(product.id);
    top.emplace_back(integrator_a.id);
    top.emplace_back(integrator_b.id);
    top.emplace_back(quantifier_a.id);
    top.emplace_back(quantifier_b.id);
    top.emplace_back(cross.id);
    top.emplace_back(cross2.id);

    parent(constant.id, undefined<cluster_id>());
    parent(constant2.id, undefined<cluster_id>());
    parent(constant3.id, undefined<cluster_id>());
    parent(sum_a.id, undefined<cluster_id>());
    parent(sum_b.id, undefined<cluster_id>());
    parent(sum_c.id, undefined<cluster_id>());
    parent(sum_d.id, undefined<cluster_id>());
    parent(product.id, undefined<cluster_id>());
    parent(integrator_a.id, undefined<cluster_id>());
    parent(integrator_b.id, undefined<cluster_id>());
    parent(quantifier_a.id, undefined<cluster_id>());
    parent(quantifier_b.id, undefined<cluster_id>());
    parent(cross.id, undefined<cluster_id>());
    parent(cross2.id, undefined<cluster_id>());

    return status::success;
}
1212

1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
static int
show_connection(output_port& port,
                output_port_id id,
                data_array<input_port, input_port_id>& input_ports,
                int connection_id)
{
    for_each(input_ports,
             port.connections,
             [&connection_id, id](const auto& /*i_port*/, const auto i_id) {
                 imnodes::Link(
                   connection_id++, editor::get_out(id), editor::get_in(i_id));
             });

    return connection_id;
}

1229
1230
1231
1232
1233
1234
void
editor::show_connections() noexcept
{
    int connection_id = 0;

    for (size_t i = 0, e = top.children.size(); i != e; ++i) {
1235
1236
        if (top.children[i].first.index() == 0) {
            const auto id = std::get<model_id>(top.children[i].first);
1237
1238
            if (const auto* mdl = sim.models.try_to_get(id); mdl) {
                sim.for_all_output_port(
1239
1240
1241
1242
1243
1244
1245
1246
                  *mdl,
                  [this, &connection_id](output_port& port,
                                         output_port_id /*id*/) {
                      connection_id =
                        show_connection(port,
                                        this->sim.output_ports.get_id(port),
                                        this->sim.input_ports,
                                        connection_id);
1247
1248
                  });
            }
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
        } else {
            const auto id = std::get<cluster_id>(top.children[i].first);
            if (auto* gp = clusters.try_to_get(id); gp) {
                for_each(sim.output_ports,
                         gp->output_ports,
                         [this, &connection_id](output_port& port,
                                                output_port_id /*id*/) {
                             connection_id = show_connection(
                               port,
                               this->sim.output_ports.get_id(port),
                               this->sim.input_ports,
                               connection_id);
                         });
            }
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
        }
    }
}

void
editor::show_model_cluster(cluster& mdl) noexcept
{
    {
        auto it = mdl.input_ports.begin();
        auto end = mdl.input_ports.end();
1273

1274
1275
        while (it != end) {
            if (auto* port = sim.input_ports.try_to_get(*it); port) {
1276
1277
                imnodes::BeginInputAttribute(get_in(*it),
                                             imnodes::PinShape_TriangleFilled);
1278
                ImGui::TextUnformatted("");
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1279
                imnodes::EndInputAttribute();
1280
1281
1282
                ++it;
            } else {
                it = mdl.input_ports.erase(it);
1283
            }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1284
        }
1285
1286
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1287
    {
1288
1289
        auto it = mdl.output_ports.begin();
        auto end = mdl.output_ports.end();
1290

1291
1292
        while (it != end) {
            if (auto* port = sim.output_ports.try_to_get(*it); port) {
Gauthier Quesnel's avatar