Files
cpp-masssprings/include/physics.hpp

191 lines
4.1 KiB
C++

#ifndef __PHYSICS_HPP_
#define __PHYSICS_HPP_
#include <atomic>
#include <mutex>
#include <queue>
#include <raylib.h>
#include <raymath.h>
#include <thread>
#include <unordered_map>
#include <variant>
#include <vector>
#include "config.hpp"
#include "puzzle.hpp"
#ifdef BARNES_HUT
#include "octree.hpp"
#endif
#ifndef WEB
#include <BS_thread_pool.hpp>
#endif
class Mass {
public:
Vector3 position;
Vector3 previous_position; // for verlet integration
Vector3 velocity;
Vector3 force;
public:
Mass(Vector3 _position)
: position(_position), previous_position(_position),
velocity(Vector3Zero()), force(Vector3Zero()) {}
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;
};
class Spring {
public:
int mass_a;
int mass_b;
public:
Spring(int _mass_a, int _mass_b) : mass_a(_mass_a), mass_b(_mass_b) {}
public:
auto CalculateSpringForce(Mass &_mass_a, Mass &_mass_b) const -> void;
};
class MassSpringSystem {
private:
#ifdef BARNES_HUT
// Barnes-Hut
Octree octree;
#else
// Uniform grid
std::vector<int> mass_indices;
std::vector<int64_t> cell_ids;
int last_build;
int last_masses_count;
int last_springs_count;
#endif
#ifndef WEB
BS::thread_pool<BS::tp::none> threads;
#endif
public:
// This is the main ownership of all the states/masses/springs.
std::vector<Mass> masses;
std::unordered_map<State, int> state_masses;
std::vector<Spring> springs;
std::unordered_map<std::pair<State, State>, int> state_springs;
public:
MassSpringSystem() {
#ifndef BARNES_HUT
last_build = REPULSION_GRID_REFRESH;
std::cout << "Using uniform grid repulsion force calculation." << std::endl;
#else
std::cout << "Using Barnes-Hut + octree repulsion force calculation."
<< std::endl;
#endif
#ifndef WEB
std::cout << "Thread-Pool: " << threads.get_thread_count() << " threads."
<< std::endl;
#endif
};
MassSpringSystem(const MassSpringSystem &copy) = delete;
MassSpringSystem &operator=(const MassSpringSystem &copy) = delete;
MassSpringSystem(MassSpringSystem &move) = delete;
MassSpringSystem &operator=(MassSpringSystem &&move) = delete;
private:
#ifdef BARNES_HUT
auto BuildOctree() -> void;
#else
auto BuildUniformGrid() -> void;
#endif
public:
auto AddMass(const State &state) -> void;
auto GetMass(const State &state) -> Mass &;
auto GetMass(const State &state) const -> const Mass &;
auto AddSpring(const State &massA, const State &massB) -> void;
auto Clear() -> void;
auto ClearForces() -> void;
auto CalculateSpringForces() -> void;
auto CalculateRepulsionForces() -> void;
auto VerletUpdate(float delta_time) -> void;
#ifndef BARNES_HUT
auto InvalidateGrid() -> void;
#endif
};
class ThreadedPhysics {
struct AddMass {
State s;
};
struct AddSpring {
State a;
State b;
};
struct ClearGraph {};
using Command = std::variant<AddMass, AddSpring, ClearGraph>;
struct PhysicsState {
std::mutex command_mtx;
std::queue<Command> pending_commands;
std::mutex pos_mtx;
std::vector<Mass> masses; // Read by renderer
std::unordered_map<State, int> state_masses; // Read by renderer
std::vector<Spring> springs; // Read by renderer
std::unordered_map<std::pair<State, State>, int>
state_springs; // Read by renderer
std::atomic<bool> running{true};
};
private:
PhysicsState state;
std::thread physics;
public:
ThreadedPhysics() : physics(PhysicsThread, std::ref(state)) {}
ThreadedPhysics(const ThreadedPhysics &copy) = delete;
ThreadedPhysics &operator=(const ThreadedPhysics &copy) = delete;
ThreadedPhysics(ThreadedPhysics &&move) = delete;
ThreadedPhysics &operator=(ThreadedPhysics &&move) = delete;
~ThreadedPhysics() {
state.running = false;
physics.join();
}
private:
static auto PhysicsThread(PhysicsState &state) -> void;
public:
};
// https://en.cppreference.com/w/cpp/utility/variant/visit
template <class... Ts> struct overloads : Ts... {
using Ts::operator()...;
};
#endif