pure-cpp 1.0.0
A C++ physics simulation benchmark comparing performance with Python implementations
physics_worker.cpp
Go to the documentation of this file.
1/**
2 * \file physics_worker.cpp
3 * \brief Implementation of the worker object that runs the physics simulation
4 * in a separate thread.
5 * \author Le Bars, Yoann
6 *
7 * This file is part of the pure C++ benchmark.
8 */
9
10#include "physics_worker.hpp"
11
12#include <memory>
13
14#include "space.hpp"
15
16namespace Model {
17
18 /**
19 * \brief Constructs the physics worker.
20 */
21 PhysicsWorker::PhysicsWorker(const Configuration::SimulationConfig& config,
22 QObject* in_parent)
23 : QObject(in_parent),
24 space_(std::make_unique<Space>(config)),
25 iterationCount_(0),
26 maxIterations_(config.n_iter),
27 isRunning_(false) {
28 // Register the custom vector types for use in signals and slots.
29 qRegisterMetaType<Vector3dVec>("Model::Vector3dVec");
30 qRegisterMetaType<QuaterniondVec>("Model::QuaterniondVec");
31 }
32
33 /**
34 * \brief Destructor. The definition must be in the .cpp file where Space is
35 * fully defined.
36 */
38
39 /**
40 * \brief Instructs the `Space` object to print its final profiling
41 * report.
42 */
44 if (space_) {
45 space_->printProfilingReport();
46 }
47 }
48
49 /**
50 * \brief Provides read-only access to the initial list of body proxies.
51 */
52 std::vector<ConstBodyProxy> PhysicsWorker::getInitialBodies() const {
53 std::vector<ConstBodyProxy> bodies;
54 if (space_) {
55 bodies.reserve(space_->n());
56 for (std::size_t i = 0; i < space_->n(); ++i) {
57 bodies.emplace_back(space_->body(i));
58 }
59 }
60 return bodies;
61 }
62
63 /**
64 * \brief Starts the simulation loop.
65 */
67 isRunning_ = true;
69 performSingleStep(); // Start the first step.
70 }
71
72 /**
73 * \brief Performs a single step of the physics simulation and emits the
74 * results.
75 */
78 space_->computeDynamics(iterationCount_);
79
80 Vector3dVec positions;
81 QuaterniondVec quaternions;
82 Vector3dVec torques;
83 Vector3dVec alphas;
84 std::tie(positions, quaternions, torques, alphas) =
85 space_->getDataForDisplay();
86
87 emit updatedBodyData(positions, quaternions, torques,
88 alphas, // NOLINT
89 iterationCount_); // NOLINT
90
93 emit simulationFinished();
94 isRunning_ = false;
95 }
96 }
97 }
98
100 isRunning_ = false;
101 }
102
103} // namespace Model
104
105/* The MOC compiler needs to see the implementation of the class that uses
106 signals and slots. We include the generated moc file here.
107 This explicit inclusion ensures that the Meta-Object Compiler’s output is
108 compiled with the rest of the implementation, which is a robust pattern
109 for handling Qt’s meta-object system. */
110#include "moc_physics_worker.cpp"
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.
Class describing a space in which move several bodies.
Definition: space.hpp:89
Worker object to run the physics simulation in a separate thread.
N-body simulation space with gravitational interaction and collision response.