small refactor

This commit is contained in:
2026-02-28 17:57:24 +01:00
parent 3f71603961
commit 36ddcb6925
34 changed files with 4138 additions and 3560 deletions

View File

@ -1,12 +1,11 @@
#ifndef __PHYSICS_HPP_
#define __PHYSICS_HPP_
#ifndef PHYSICS_HPP_
#define PHYSICS_HPP_
#include "config.hpp"
#include "octree.hpp"
#include <atomic>
#include <condition_variable>
#include <cstddef>
#include <mutex>
#include <queue>
#include <raylib.h>
@ -15,6 +14,8 @@
#include <variant>
#include <vector>
#include "util.hpp"
#ifdef THREADPOOL
#if defined(_WIN32)
#define NOGDI // All GDI defines and routines
@ -32,127 +33,129 @@
#include <tracy/Tracy.hpp>
#endif
class Mass {
class mass
{
public:
Vector3 position;
Vector3 previous_position; // for verlet integration
Vector3 velocity;
Vector3 force;
Vector3 position = Vector3Zero();
Vector3 previous_position = Vector3Zero(); // for verlet integration
Vector3 velocity = Vector3Zero();
Vector3 force = Vector3Zero();
public:
Mass(Vector3 _position)
: position(_position), previous_position(_position),
velocity(Vector3Zero()), force(Vector3Zero()) {}
mass() = delete;
explicit mass(const Vector3 _position)
: position(_position), previous_position(_position) {}
public:
auto ClearForce() -> void;
auto CalculateVelocity(const float delta_time) -> void;
auto CalculatePosition(const float delta_time) -> void;
auto VerletUpdate(const float delta_time) -> void;
auto clear_force() -> void;
auto calculate_velocity(float delta_time) -> void;
auto calculate_position(float delta_time) -> void;
auto verlet_update(float delta_time) -> void;
};
class Spring {
class spring
{
public:
std::size_t a;
std::size_t b;
size_t a;
size_t b;
public:
Spring(std::size_t _a, std::size_t _b) : a(_a), b(_b) {}
spring(const size_t _a, const size_t _b)
: a(_a), b(_b) {}
public:
auto CalculateSpringForce(Mass &_a, Mass &_b) const -> void;
static auto calculate_spring_force(mass& _a, mass& _b) -> void;
};
class MassSpringSystem {
class mass_spring_system
{
private:
#ifdef THREADPOOL
BS::thread_pool<BS::tp::none> threads;
#endif
#ifdef THREADPOOL
BS::thread_pool<> threads;
#endif
public:
Octree octree;
octree tree;
// This is the main ownership of all the states/masses/springs.
std::vector<Mass> masses;
std::vector<Spring> springs;
std::vector<mass> masses;
std::vector<spring> springs;
public:
MassSpringSystem()
#ifdef THREADPOOL
: threads(std::thread::hardware_concurrency() - 1, SetThreadName)
#endif
mass_spring_system()
#ifdef THREADPOOL
: threads(std::thread::hardware_concurrency() - 1, set_thread_name)
#endif
{
std::cout << std::format(
"Using Barnes-Hut + Octree repulsion force calculation.")
<< std::endl;
infoln("Using Barnes-Hut + Octree repulsion force calculation.");
#ifdef THREADPOOL
std::cout << std::format("Thread-pool: {} threads.",
threads.get_thread_count())
<< std::endl;
#else
std::cout << std::format("Thread-pool: Disabled.") << std::endl;
#endif
};
#ifdef THREADPOOL
infoln("Thread-pool: {} threads.", threads.get_thread_count());
#else
infoln("Thread-pool: Disabled.");
#endif
}
MassSpringSystem(const MassSpringSystem &copy) = delete;
MassSpringSystem &operator=(const MassSpringSystem &copy) = delete;
MassSpringSystem(MassSpringSystem &move) = delete;
MassSpringSystem &operator=(MassSpringSystem &&move) = delete;
mass_spring_system(const mass_spring_system& copy) = delete;
auto operator=(const mass_spring_system& copy) -> mass_spring_system& = delete;
mass_spring_system(mass_spring_system& move) = delete;
auto operator=(mass_spring_system&& move) -> mass_spring_system& = delete;
private:
#ifdef THREADPOOL
static auto SetThreadName(std::size_t idx) -> void;
#endif
#ifdef THREADPOOL
static auto set_thread_name(size_t idx) -> void;
#endif
auto BuildOctree() -> void;
auto build_octree() -> void;
public:
auto AddMass() -> void;
auto clear() -> void;
auto add_mass() -> void;
auto add_spring(size_t a, size_t b) -> void;
auto AddSpring(int a, int b) -> void;
auto clear_forces() -> void;
auto calculate_spring_forces() -> void;
auto calculate_repulsion_forces() -> void;
auto verlet_update(float delta_time) -> void;
auto Clear() -> void;
auto ClearForces() -> void;
auto CalculateSpringForces() -> void;
auto CalculateRepulsionForces() -> void;
auto VerletUpdate(float delta_time) -> void;
auto center_masses() -> void;
};
class ThreadedPhysics {
struct AddMass {};
struct AddSpring {
std::size_t a;
std::size_t b;
class threaded_physics
{
struct add_mass {};
struct add_spring
{
size_t a;
size_t b;
};
struct ClearGraph {};
using Command = std::variant<AddMass, AddSpring, ClearGraph>;
struct clear_graph {};
struct PhysicsState {
#ifdef TRACY
using command = std::variant<add_mass, add_spring, clear_graph>;
struct physics_state
{
#ifdef TRACY
TracyLockable(std::mutex, command_mtx);
#else
#else
std::mutex command_mtx;
#endif
std::queue<Command> pending_commands;
#endif
std::queue<command> pending_commands;
#ifdef TRACY
#ifdef TRACY
TracyLockable(std::mutex, data_mtx);
#else
#else
std::mutex data_mtx;
#endif
#endif
std::condition_variable_any data_ready_cnd;
std::condition_variable_any data_consumed_cnd;
Vector3 mass_center = Vector3Zero();
unsigned int ups = 0;
int ups = 0;
size_t mass_count = 0; // For debug
size_t spring_count = 0; // For debug
std::vector<Vector3> masses; // Read by renderer
bool data_ready = false;
bool data_consumed = true;
@ -164,17 +167,19 @@ private:
std::thread physics;
public:
PhysicsState state;
physics_state state;
public:
ThreadedPhysics() : physics(PhysicsThread, std::ref(state)) {}
threaded_physics()
: physics(physics_thread, std::ref(state)) {}
ThreadedPhysics(const ThreadedPhysics &copy) = delete;
ThreadedPhysics &operator=(const ThreadedPhysics &copy) = delete;
ThreadedPhysics(ThreadedPhysics &&move) = delete;
ThreadedPhysics &operator=(ThreadedPhysics &&move) = delete;
threaded_physics(const threaded_physics& copy) = delete;
auto operator=(const threaded_physics& copy) -> threaded_physics& = delete;
threaded_physics(threaded_physics&& move) = delete;
auto operator=(threaded_physics&& move) -> threaded_physics& = delete;
~ThreadedPhysics() {
~threaded_physics()
{
state.running = false;
state.data_ready_cnd.notify_all();
state.data_consumed_cnd.notify_all();
@ -182,23 +187,23 @@ public:
}
private:
static auto PhysicsThread(PhysicsState &state) -> void;
static auto physics_thread(physics_state& state) -> void;
public:
auto AddMassCmd() -> void;
auto add_mass_cmd() -> void;
auto AddSpringCmd(std::size_t a, std::size_t b) -> void;
auto add_spring_cmd(size_t a, size_t b) -> void;
auto ClearCmd() -> void;
auto clear_cmd() -> void;
auto AddMassSpringsCmd(
std::size_t num_masses,
const std::vector<std::pair<std::size_t, std::size_t>> &springs) -> void;
auto add_mass_springs_cmd(size_t num_masses, const std::vector<std::pair<size_t, size_t>>& springs) -> void;
};
// https://en.cppreference.com/w/cpp/utility/variant/visit
template <class... Ts> struct overloads : Ts... {
template <class... Ts>
struct overloads : Ts...
{
using Ts::operator()...;
};
#endif
#endif