From d1ec81eb09bbe8464af5a20893076d7bc3f96f7c Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Mon, 11 Sep 2023 16:23:38 +0200 Subject: [PATCH] add and delete on simulation thread --- src/MainWindow.cpp | 9 +++++---- src/MainWindow.h | 5 ++++- src/Simulation.cpp | 18 +++++++++++++----- src/Simulation.h | 12 +++++------- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 53d1cbc..a17517f 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -12,7 +12,6 @@ #include #include #include -#include "Pendulum.h" #include "Button.h" #include #include @@ -297,6 +296,7 @@ QWidget * MainWindow::buildAddUI() { removeBtn->setStyleSheet("background-color: #ffaaaa"); connect(addBtn, &QPushButton::clicked, this, &MainWindow::add); + connect(this, &MainWindow::pendulaCreated, simulation, &Simulation::addPendula); connect(removeBtn, &QPushButton::clicked, simulation, &Simulation::clearPendula); btnLyt->addWidget(addBtn); @@ -393,8 +393,8 @@ void MainWindow::normalizeLengths() { } void MainWindow::add() { + std::vector addPendula; if (multiple){ - for (int i = 0; i < count; i++){ auto M = std::vector(masses.begin(), masses.begin() + segments); auto L = std::vector(lengths.begin(), lengths.begin() + segments); @@ -417,13 +417,14 @@ void MainWindow::add() { auto hue = float(progress + 0.5); c = QColor::fromHsvF(hue, 1, 1); } - simulation->pendula.push_back(new Pendulum(M, L, c, angle)); + addPendula.push_back(new Pendulum(M, L, c, angle)); } } else { auto M = std::vector(masses.begin(), masses.begin() + segments); auto L = std::vector(lengths.begin(), lengths.begin() + segments); - simulation->pendula.push_back(new Pendulum(M, L, color, startingAngle)); + addPendula.push_back(new Pendulum(M, L, color, startingAngle)); } + emit pendulaCreated(addPendula); } void MainWindow::resetSimulationControl() { diff --git a/src/MainWindow.h b/src/MainWindow.h index 37cd6d2..433dcce 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -1,6 +1,7 @@ #include #include #include "Slider.h" +#include "Pendulum.h" class Simulation; class QGridLayout; @@ -9,6 +10,7 @@ class GLWidget; enum Property {Angle, Mass, Length}; class MainWindow : public QWidget { + Q_OBJECT public: explicit MainWindow(); protected: @@ -39,7 +41,8 @@ private: Slider * gravitySlider = nullptr, * timescaleSlider = nullptr; Slider<> * substepsSlider = nullptr; - +signals: + void pendulaCreated(const std::vector &add); public slots: void resetMasses(); void resetLengths(); diff --git a/src/Simulation.cpp b/src/Simulation.cpp index fd2c3bd..a0d8a6b 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -14,7 +14,7 @@ Simulation::Simulation() { lastUpdate = high_resolution_clock::now(); }; -void Simulation::draw(QPainter *p, int screenSize) const { +void Simulation::draw(QPainter *p, int screenSize) { double scale = screenSize * 0.95 / size; for (const auto pendulum : pendula) @@ -35,14 +35,22 @@ void Simulation::update() { h /= substeps; #pragma omp parallel for - for (const auto pendulum : pendula) - for (int i = 0; i < substeps; i++) + for (const auto & pendulum : pendula) + for (int k = 0; k < substeps; k++) pendulum->update(h, gravity); } void Simulation::clearPendula() { - for (auto p : pendula) - delete p; + auto deleteLater = pendula; pendula.clear(); + pendula.shrink_to_fit(); + QThread::usleep(500000); + for (auto p : deleteLater) + delete p; +} + +void Simulation::addPendula(const std::vector &add) { + for (auto p : add) + pendula.push_back(p); } diff --git a/src/Simulation.h b/src/Simulation.h index e5df659..315d764 100644 --- a/src/Simulation.h +++ b/src/Simulation.h @@ -19,19 +19,17 @@ public: double timescale {}; int substeps {}; - int updateInterval = 17; - bool isPlaying = false; - std::vector pendula; - - QTimer * timer; - - void draw(QPainter*, int) const; + void draw(QPainter*, int); public slots: void clearPendula(); + void addPendula(const std::vector &add); private slots: void update(); private: + QTimer * timer; + int updateInterval = 17; + std::vector pendula; time_point lastUpdate; }; \ No newline at end of file