This commit is contained in:
2026-03-05 21:47:48 +01:00
parent c8d6541221
commit 9de0d06806
12 changed files with 289 additions and 233 deletions

View File

@ -13,7 +13,7 @@
#include <variant>
#include <vector>
class threaded_physics
class cpu_layout_engine
{
struct add_mass
{};
@ -64,17 +64,17 @@ public:
physics_state state;
public:
explicit threaded_physics(
explicit cpu_layout_engine(
const std::optional<BS::thread_pool<>* const> _thread_pool = std::nullopt)
: thread_pool(_thread_pool), physics(physics_thread, std::ref(state), std::ref(thread_pool))
{}
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;
cpu_layout_engine(const cpu_layout_engine& copy) = delete;
auto operator=(const cpu_layout_engine& copy) -> cpu_layout_engine& = delete;
cpu_layout_engine(cpu_layout_engine&& move) = delete;
auto operator=(cpu_layout_engine&& move) -> cpu_layout_engine& = delete;
~threaded_physics()
~cpu_layout_engine()
{
state.running = false;
state.data_ready_cnd.notify_all();
@ -98,4 +98,4 @@ public:
const std::vector<std::pair<size_t, size_t>>& springs) -> void;
};
#endif
#endif

View File

@ -7,7 +7,7 @@
#include <optional>
#include <raylib.h>
class mass_spring_system
class cpu_spring_system
{
public:
class spring
@ -36,12 +36,12 @@ public:
std::vector<spring> springs;
public:
mass_spring_system() {}
cpu_spring_system() {}
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;
cpu_spring_system(const cpu_spring_system& copy) = delete;
auto operator=(const cpu_spring_system& copy) -> cpu_spring_system& = delete;
cpu_spring_system(cpu_spring_system& move) = delete;
auto operator=(cpu_spring_system&& move) -> cpu_spring_system& = delete;
public:
auto clear() -> void;

View File

@ -1,6 +1,8 @@
#ifndef OCTREE_HPP_
#define OCTREE_HPP_
#include "util.hpp"
#include <array>
#include <vector>
@ -21,7 +23,7 @@ class octree
bool leaf = true;
public:
[[nodiscard]] auto child_count() const -> int;
node(const Vector3& _box_min, const Vector3& _box_max) : box_min(_box_min), box_max(_box_max) {}
};
public:
@ -29,23 +31,88 @@ public:
std::vector<node> nodes;
// This approach is actually slower than the array of nodes
// beacuse we access all the attributes in the same function
// std::vector<Vector3> mass_centers;
// std::vector<float> mass_totals;
// std::vector<Vector3> box_mins;
// std::vector<Vector3> box_maxs;
// std::vector<std::array<int, 8>> childrens;
// std::vector<int> mass_ids;
// std::vector<uint8_t> leafs; // bitpacked std::vector<bool> is a lot slower
public:
octree() = default;
// octree(const octree& copy) = delete;
// auto operator=(const octree& copy) -> octree& = delete;
// octree(octree&& move) = delete;
// auto operator=(octree&& move) -> octree& = delete;
octree(const octree& copy) = delete;
auto operator=(const octree& copy) -> octree& = delete;
octree(octree&& move) = delete;
auto operator=(octree&& move) -> octree& = delete;
public:
[[nodiscard]] auto get_octant(int node_idx, const Vector3& pos) const -> int;
[[nodiscard]] auto get_child_bounds(int node_idx, int octant) const
-> std::pair<Vector3, Vector3>;
auto create_empty_leaf(const Vector3& box_min, const Vector3& box_max) -> int;
auto clear() -> void;
auto reserve(size_t count) -> void;
[[nodiscard]] auto empty() const -> bool;
[[nodiscard]] INLINE static inline auto get_octant(const Vector3& box_min,
const Vector3& box_max,
const Vector3& pos) -> int;
[[nodiscard]] INLINE static inline auto get_child_bounds(const Vector3& box_min,
const Vector3& box_max,
int octant) -> std::pair<Vector3, Vector3>;
INLINE inline auto create_empty_leaf(const Vector3& box_min, const Vector3& box_max) -> int;
[[nodiscard]] auto get_child_count(int node_idx) const -> int;
auto insert(int node_idx, int mass_id, const Vector3& pos, float mass, int depth) -> void;
static auto build_octree(octree& t, const std::vector<Vector3>& positions) -> void;
[[nodiscard]] auto calculate_force(int node_idx, const Vector3& pos) const -> Vector3;
};
INLINE inline auto octree::get_octant(const Vector3& box_min, const Vector3& box_max, const Vector3& pos) -> int
{
auto [cx, cy, cz] = (box_min + box_max) / 2.0f;
// The octant is encoded as a 3-bit integer "zyx". The node area is split
// along all 3 axes, if a position is right of an axis, this bit is set to 1.
// If a position is right of the x-axis and y-axis and left of the z-axis, the
// encoded octant is "011".
return (pos.x >= cx) | ((pos.y >= cy) << 1) | ((pos.z >= cz) << 2);
}
INLINE inline auto octree::get_child_bounds(const Vector3& box_min,
const Vector3& box_max,
const int octant) -> std::pair<Vector3, Vector3>
{
auto [cx, cy, cz] = (box_min + box_max) / 2.0f;
Vector3 min = Vector3Zero();
Vector3 max = Vector3Zero();
// If (octant & 1), the octant is to the right of the node region's x-axis
// (see GetOctant). This means the left bound is the x-axis and the right
// bound the node's region max.
min.x = octant & 1 ? cx : box_min.x;
max.x = octant & 1 ? box_max.x : cx;
min.y = octant & 2 ? cy : box_min.y;
max.y = octant & 2 ? box_max.y : cy;
min.z = octant & 4 ? cz : box_min.z;
max.z = octant & 4 ? box_max.z : cz;
return std::make_pair(min, max);
}
INLINE inline auto octree::create_empty_leaf(const Vector3& box_min, const Vector3& box_max) -> int
{
nodes.emplace_back(box_min, box_max);
return static_cast<int>(nodes.size() - 1);
// mass_centers.emplace_back(Vector3Zero());
// mass_totals.emplace_back(0);
// box_mins.emplace_back(box_min);
// box_maxs.emplace_back(box_max);
// childrens.push_back({-1, -1, -1, -1, -1, -1, -1, -1});
// mass_ids.emplace_back(-1);
// leafs.emplace_back(true);
}
#endif

View File

@ -3,7 +3,7 @@
#include "graph_distances.hpp"
#include "load_save.hpp"
#include "threaded_physics.hpp"
#include "cpu_layout_engine.hpp"
#include "puzzle.hpp"
#include <boost/unordered/unordered_flat_map.hpp>
@ -12,7 +12,7 @@
class state_manager
{
private:
threaded_physics& physics;
cpu_layout_engine& physics;
std::string preset_file;
size_t current_preset = 0;
@ -42,7 +42,7 @@ private:
bool edited = false;
public:
state_manager(threaded_physics& _physics, const std::string& _preset_file)
state_manager(cpu_layout_engine& _physics, const std::string& _preset_file)
: physics(_physics), preset_file(_preset_file)
{
reload_preset_file();