pure-cpp 1.0.0
A C++ physics simulation benchmark comparing performance with Python implementations
physics_worker.hpp
Go to the documentation of this file.
1#ifndef PHYSICS_WORKER_HPP
2#define PHYSICS_WORKER_HPP
3
4/**
5 * \file physics_worker.hpp
6 * \brief Worker object to run the physics simulation in a separate thread.
7 * \author Le Bars, Yoann
8 *
9 * This file is part of the pure C++ benchmark.
10 */
11
12#include <QObject>
13#include <QVector>
14#include <tuple>
15
16#include "body.hpp"
17#include "config.hpp"
18
19Q_DECLARE_METATYPE(Model::Vector3dVec);
20Q_DECLARE_METATYPE(Model::QuaterniondVec);
21
22namespace Model {
23 // Forward-declare Space to avoid including the full header.
24 class Space;
25
26 /**
27 * \brief Runs the physics simulation in a separate thread to keep the UI
28 * responsive.
29 *
30 * This worker object owns the `Space` instance and communicates with the
31 * main (GUI) thread via signals and slots.
32 * \ingroup PhysicsCore
33 */
34 class PhysicsWorker : public QObject {
35 Q_OBJECT
36
37 public:
38 /**
39 * \brief Constructs the physics worker.
40 *
41 * \param config The simulation configuration.
42 * \param in_parent The parent QObject, if any.
43 */
44 explicit PhysicsWorker(const Configuration::SimulationConfig& config,
45 QObject* in_parent = nullptr);
46
47 /// \brief Destructor.
48 ~PhysicsWorker() override;
49
50 /**
51 * \brief Triggers cleanup tasks before the worker is destroyed.
52 *
53 * This is primarily used to instruct the `Space` object to print its
54 * final profiling report.
55 */
56 void cleanup();
57
58 /**
59 * \brief Provides read-only access to the initial list of body
60 * proxies.
61 * \return A vector of const proxies to the initially created bodies.
62 */
63 [[nodiscard]] std::vector<ConstBodyProxy> getInitialBodies() const;
64
65 signals:
66 /**
67 * \brief Emitted after each simulation step with the updated state of
68 * all bodies.
69 * \param positions A vector of all body positions.
70 * \param quaternions A vector of all body orientations.
71 * \param torques A vector of all body torques.
72 * \param alphas A vector of all body angular accelerations.
73 * \param iteration The current simulation iteration number.
74 */
75 void updatedBodyData(const Vector3dVec& positions,
76 const QuaterniondVec& quaternions,
77 const Vector3dVec& torques, // NOLINT
78 const Vector3dVec& alphas, // NOLINT
79 std::size_t iteration);
80
81 /// \brief Emitted when the simulation has completed all iterations.
83
84 public slots:
85 /**
86 * \brief Starts the simulation loop.
87 *
88 * This slot is connected to the QThread's `started` signal.
89 */
90 void startSimulation();
91 /**
92 * \brief Performs a single step of the physics simulation and emits the
93 * results.
94 */
95 void performSingleStep();
96
97 /**
98 * \brief Stops the simulation loop.
99 * This should be called before cleanup to prevent new steps from being scheduled.
100 */
101 void stopSimulation();
102
103 private:
104 /// \brief The simulation space containing all bodies and physics logic.
105 std::unique_ptr<Space> space_;
106 /// \brief The current simulation iteration count.
107 std::size_t iterationCount_;
108 /// \brief The maximum number of iterations to run.
109 std::size_t maxIterations_;
110 /// \brief A flag to control the execution of the simulation loop.
112 };
113} // namespace Model
114
115#endif // PHYSICS_WORKER_HPP
SoA container for simulation bodies and proxies for AoS-like access.
Runs the physics simulation in a separate thread to keep the UI responsive.
void cleanup()
Triggers cleanup tasks before the worker is destroyed.
void updatedBodyData(const Vector3dVec &positions, const QuaterniondVec &quaternions, const Vector3dVec &torques, const Vector3dVec &alphas, std::size_t iteration)
Emitted after each simulation step with the updated state of all bodies.
void startSimulation()
Starts the simulation loop.
std::unique_ptr< Space > space_
The simulation space containing all bodies and physics logic.
std::size_t iterationCount_
The current simulation iteration count.
std::vector< ConstBodyProxy > getInitialBodies() const
Provides read-only access to the initial list of body proxies.
void performSingleStep()
Performs a single step of the physics simulation and emits the results.
std::size_t maxIterations_
The maximum number of iterations to run.
void simulationFinished()
Emitted when the simulation has completed all iterations.
void stopSimulation()
Stops the simulation loop. This should be called before cleanup to prevent new steps from being sched...
~PhysicsWorker() override
Destructor.
PhysicsWorker(const Configuration::SimulationConfig &config, QObject *in_parent=nullptr)
Constructs the physics worker.
bool isRunning_
A flag to control the execution of the simulation loop.