|
pure-cpp 1.0.0
A C++ physics simulation benchmark comparing performance with Python implementations
|
Class describing a space in which move several bodies. More...
#include <space.hpp>

Public Member Functions | |
| Space () | |
| Default constructor. More... | |
| Space (const Configuration::SimulationConfig &config) | |
| Initialise the space. More... | |
| ~Space () | |
| Destructor to clean up OpenMP locks. More... | |
| std::size_t | n () const |
| Number of bodies getter. More... | |
| BodyProxy | body (std::size_t i) |
| Get a proxy to the i-th body. More... | |
| ConstBodyProxy | body (std::size_t i) const |
| Get a const proxy to the i-th body. More... | |
| double | getPreviousTimeStep () const |
| Get the time step used in the last completed simulation frame. More... | |
| void | computeDynamics (std::size_t iteration) |
| Computes one full step of the simulation. More... | |
| std::tuple< double, double, double > | calculateSystemEnergy () const |
| Calculates the kinetic and potential energy of the system. More... | |
| std::tuple< Vector3dVec, QuaterniondVec > | getAllBodyTransforms () const |
| Retrieves the current positions and orientations of all bodies. More... | |
| std::tuple< Vector3dVec, QuaterniondVec, Vector3dVec, Vector3dVec > | getDataForDisplay () const |
| Retrieves all data required for display for all bodies. More... | |
| void | printProfilingReport () const |
| double | getG () const |
| Get the universal gravitational constant. More... | |
| void | setG (double g) |
| Set the universal gravitational constant. More... | |
| double | getEpsilon () const |
| Get the numerical precision threshold. More... | |
| void | setEpsilon (double eps) |
| Set the numerical precision threshold. More... | |
| double | getCoeffRestitution () const |
| Get the coefficient of restitution. More... | |
| void | setCoeffRestitution (double e) |
| Set the coefficient of restitution. More... | |
| double | getCoeffFriction () const |
| Get the coefficient of kinetic friction. More... | |
| void | setCoeffFriction (double mu) |
| Set the coefficient of kinetic friction. More... | |
| double | getCoeffStaticFriction () const |
| Get the coefficient of static friction. More... | |
| void | setCoeffStaticFriction (double mu_s) |
| Set the coefficient of static friction. More... | |
| double | getLinearDamping () const |
| Get the linear damping factor. More... | |
| void | setLinearDamping (double damping) |
| Set the linear damping factor. More... | |
| double | getAngularDamping () const |
| Get the angular damping factor. More... | |
| void | setAngularDamping (double damping) |
| Set the angular damping factor. More... | |
| double | getTimeStep () const |
| Get the current time step. More... | |
| void | setTimeStep (double dt) |
| Set the current time step. More... | |
| double | getPositionalCorrectionFactor () const |
| Get the positional correction factor. More... | |
| void | setPositionalCorrectionFactor (double factor) |
| Set the positional correction factor. More... | |
| bool | isDiagnosticsEnabled () const |
| Check if diagnostics are enabled. More... | |
| void | setDiagnosticsEnabled (bool enabled) |
| Enable or disable diagnostics. More... | |
| Bodies & | getBodiesForTest () |
| Provides direct access to the Bodies container for testing. More... | |
| std::optional< CollisionContext > | createCollisionContext (BodyProxy &b1, BodyProxy &b2) |
| Factory method to create a CollisionContext if two bodies are colliding. More... | |
| std::tuple< Vector3d, double, double > | applyRestitutionImpulse (CollisionContext &ctx) |
| Calculates and applies the normal impulse (restitution) for a collision. More... | |
| Vector3d | applyFrictionImpulse (const CollisionContext &ctx, double jn, const Vector3d &impulseN) |
| Calculates the tangential (frictional) impulse for a collision. More... | |
| void | colorGraph (const std::vector< std::vector< std::size_t > > &graph) |
| A greedy graph colouring algorithm to assign a colour (integer) to each body such that no two adjacent bodies (i.e., colliding bodies) share the same colour. More... | |
Private Member Functions | |
| void | handleCollision (BodyProxy &b1, BodyProxy &b2) |
| Handles collision detection and response between two bodies. More... | |
| void | initializeBodies (std::size_t n, double dens, unsigned int seed) |
| Initialises the bodies in the simulation with random properties. More... | |
| void | resolveInterpenetration (const CollisionContext &ctx) |
| Resolves interpenetration by moving bodies apart along the collision normal. More... | |
| double | getEffectiveMass (const CollisionContext &ctx, const Vector3d &direction_vec) |
| Calculates the effective mass of two colliding bodies in a given direction. More... | |
| void | logSystemEnergy (std::size_t iteration) |
| Logs system energy and checks for instability if diagnostics are enabled. More... | |
| void | buildCollisionGraph () |
| Builds the collision graph using broad-phase and narrow-phase detection. More... | |
| void | resolveCollisions () |
| Resolves all detected collisions using graph colouring. More... | |
| void | applyGravity () |
| Calculates and applies gravitational forces to all bodies. More... | |
| void | updateAdaptiveTimeStep () |
| Determines the optimal time step for the next frame based on current velocities and accelerations. More... | |
| void | applyDamping () |
| Applies linear and angular damping to all bodies. More... | |
Private Attributes | |
| double | dt_ |
| Time step (in s). More... | |
| double | previous_dt_ |
| Previous time step (in s). More... | |
| double | G_ |
| Universal gravitational constant (in m³ / kg /s²). More... | |
| double | epsilon_ |
| Numerical precision threshold. More... | |
| double | coeffRestitution_ |
| Coefficient of restitution for collisions. More... | |
| double | coeffFriction_ |
| Coefficient of kinetic (sliding) friction. More... | |
| double | coeffStaticFriction_ |
| Coefficient of static friction. More... | |
| double | positionalCorrectionFactor_ |
| The percentage of overlap to correct in each frame. More... | |
| double | linearDamping_ |
| Damping factor for linear velocity. More... | |
| double | angularDamping_ |
| Damping factor for angular velocity. More... | |
| bool | diagnosticsEnabled_ |
| Flag indicating if diagnostic output is enabled. More... | |
| std::size_t | logFreq_ |
| Frequency of energy log output (every N iterations). More... | |
| Bodies | bodies_ |
| SoA container for all bodies in the simulation. More... | |
| std::unique_ptr< KDTree > | kdTree_ |
| k-d tree for broad-phase collision detection. More... | |
| std::vector< std::vector< std::size_t > > | collisionGraph_ |
| Adjacency list for the collision graph. More... | |
| std::vector< int > | colors_ |
| Stores the colour of each body for parallel collision processing. More... | |
| std::vector< omp_lock_t > | graphLocks_ |
| OpenMP locks for thread-safe collision graph updates. More... | |
| std::vector< std::vector< std::pair< std::size_t, std::size_t > > > | thread_local_collision_pairs_ |
Thread-local storage for collision pairs found during broad-phase. This reduces lock contention on the global collisionGraph_ during parallel processing. More... | |
| double | lastTotalEnergy_ = std::numeric_limits<double>::infinity() |
| The total energy from the previous logged step. More... | |
| double | cached_max_radius_ = 0.0 |
| Cached maximum radius for collision detection optimization. This is recalculated only when bodies are added/removed, not every frame. More... | |
| double | profIntegration_ = 0.0 |
| Time spent in integration steps. More... | |
| double | profCollisionGraph_ = 0.0 |
| Time spent in collision graph building. More... | |
| double | profCollisionResponse_ = 0.0 |
| Time spent in collision response. More... | |
| double | profForceCalculation_ = 0.0 |
| Time spent in force calculation. More... | |
Friends | |
| class | ::SpaceTest |
|
explicit |
| Model::Space::~Space | ( | ) |
|
private |
| Model::Vector3d Model::Space::applyFrictionImpulse | ( | const CollisionContext & | ctx, |
| double | jn, | ||
| const Vector3d & | impulseN | ||
| ) |
Calculates the tangential (frictional) impulse for a collision.
This method is part of the internal collision handling API and is exposed to tests for unit testing collision logic.
| ctx | The collision context. |
| jn | The magnitude of the normal impulse, used for the friction limit. |
| impulseN | The normal impulse vector. |
Definition at line 548 of file space.cpp.

|
private |
| std::tuple< Model::Vector3d, double, double > Model::Space::applyRestitutionImpulse | ( | CollisionContext & | ctx | ) |
Calculates and applies the normal impulse (restitution) for a collision.
This method is part of the internal collision handling API and is exposed to tests for unit testing collision logic.
| ctx | The collision context. |
|
inline |
|
inline |
|
private |
Builds the collision graph using broad-phase and narrow-phase detection.
This method implements a two-phase collision detection algorithm to efficiently identify all colliding body pairs in the simulation. The result is stored in collisionGraph_, where collisionGraph_[i] contains the indices of all bodies colliding with body i.
Phase 1: Broad-Phase Detection (Spatial Acceleration)
Phase 2: Narrow-Phase Detection (Exact Collision Test)
After execution:
computeDynamics(). collisionGraph_[i] and i appears in collisionGraph_[j]. | std::tuple< double, double, double > Model::Space::calculateSystemEnergy | ( | ) | const |
| void Model::Space::colorGraph | ( | const std::vector< std::vector< std::size_t > > & | graph | ) |
A greedy graph colouring algorithm to assign a colour (integer) to each body such that no two adjacent bodies (i.e., colliding bodies) share the same colour.
This method implements a parallel greedy graph colouring algorithm used to partition collision pairs into independent sets. Bodies with the same colour can have their collisions resolved in parallel without conflicts.
The algorithm iterates until no conflicts remain.
After colouring, a possible result is:
colors_ vector. Reads of neighbour colours are protected by locks to avoid data races, while writes are minimal and lock-protected.| graph | The collision graph where graph[i] contains indices of bodies colliding with i. The graph should be undirected (if body i collides with j, then j should appear in graph[i] and i in graph[j]). |
| void Model::Space::computeDynamics | ( | std::size_t | iteration | ) |
Computes one full step of the simulation.
This method orchestrates the main simulation loop, including:
| iteration | The current simulation iteration number. |
| std::optional< Model::CollisionContext > Model::Space::createCollisionContext | ( | BodyProxy & | b1, |
| BodyProxy & | b2 | ||
| ) |
Factory method to create a CollisionContext if two bodies are colliding.
This method is part of the internal collision handling API and is exposed to tests for unit testing collision logic.
| b1 | First body. |
| b2 | Second body. |
std::nullopt. Definition at line 642 of file space.cpp.

| std::tuple< Model::Vector3dVec, Model::QuaterniondVec > Model::Space::getAllBodyTransforms | ( | ) | const |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
| std::tuple< Model::Vector3dVec, Model::QuaterniondVec, Model::Vector3dVec, Model::Vector3dVec > Model::Space::getDataForDisplay | ( | ) | const |
|
private |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
private |
|
inline |
|
private |
|
inline |
|
private |
|
private |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
private |
|
private |
|
private |
|
mutableprivate |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
mutableprivate |
|
mutableprivate |
|
mutableprivate |
|
mutableprivate |
|
private |