1#ifndef MECACELL_WORLD_H
2#define MECACELL_WORLD_H
5#include <unordered_set>
25template <
typename Cell,
typename Integrator = Euler>
class World {
49 template <
class,
class = MecaCell::
void_t<>>
72 for (
auto i = cells.begin(); i != cells.end();) {
84 double dt = 1.0 / 100.0;
95 DECLARE_HOOK(onAddCell, beginUpdate, preBehaviorUpdate, preDeleteDeadCellsUpdate,
96 postBehaviorUpdate, endUpdate, allForcesAppliedToCells, destructor)
168 template <
typename P,
typename... Rest>
171 registerPlugin(std::forward<P>(p))(
this);
181 for (
auto &h : hooks) h.clear();
204 Cell *c =
new Cell(std::forward<Args>(args)...);
234 for (
auto &f : hooks[
eToUI(Hooks::onAddCell)]) f(
this);
245 for (
auto &f : hooks[
eToUI(Hooks::allForcesAppliedToCells)]) f(
this);
285 std::unordered_set<ordered_pair<cell_t *>> res;
286 for (
auto &c : cells) {
287 for (
auto &connected : c->getConnectedCells())
291 for (
auto &r : res) vecRes.
push_back(std::make_pair(r.first, r.second));
301 const size_t MIN_CHUNK_SIZE = 2;
302 const double AVG_TASKS_PER_THREAD = 3.0;
305 [](
auto *c) { c->updateBehavior(); });
308 for (
auto &c : cells) c->updateBehavior();
324 for (
auto &f : hooks[
eToUI(Hooks::beginUpdate)]) f(
this);
325 for (
auto &f : hooks[
eToUI(Hooks::preBehaviorUpdate)]) f(
this);
328 for (
auto &f : hooks[
eToUI(Hooks::preDeleteDeadCellsUpdate)]) f(
this);
330 for (
auto &f : hooks[
eToUI(Hooks::postBehaviorUpdate)]) f(
this);
331 for (
auto &f : hooks[
eToUI(Hooks::endUpdate)]) f(
this);
339 for (
auto &f : hooks[
eToUI(Hooks::destructor)]) f(
this);
CellPlugin< cell_t > embedded_plugin_t
PrimoCell< CellBody > Cell
Where "everything" happens.
void registerPlugins(P &&p, Rest &&... otherPlugins)
Registers a plugin. A plugin is a class or struct that contains hooks.
void setUpdateBehaviorPeriod(size_t p)
sets the period at which the world must call the updateBehavior method of each cell....
size_t getNbUpdates() const
get the number of update since the creation of the world
void allForcesHaveBeenAppliedToCells()
this method triggers the allForcesAppliedToCells. It should be called by the embedded physics plugin ...
Cell * createCell(Args &&... args)
Creates a new cell and adds it through addCell()
void setDt(double d)
sets the amount by which time is increased at each update() call.
DECLARE_HOOK(onAddCell, beginUpdate, preBehaviorUpdate, preDeleteDeadCellsUpdate, postBehaviorUpdate, endUpdate, allForcesAppliedToCells, destructor) std World(size_t nThreads=0)
cells that are registered to be added
void setShuffleCells(bool s)
void addNewCells()
effectively adds the new cells that were registered by addCell triggers addCell hooks if there is som...
~World()
World's destructor. Triggers the destructor hooks and delete all cells.
void update()
main update method
size_t updtBhvPeriod
+1 on cell add. Used for cell's unique ids
EXPORTABLE(World, KV(frame), KV(cells))
@ignore
void addCell(Cell *c)
adds a cell to the new cells batch (which will be added to the main cells container at the end of the...
void setParallelUpdateBehavior(bool p)
enables or disables the parallelisation of the cells' updateBehavior methods Only has effect if nbThr...
std::vector< std::pair< cell_t *, cell_t * > > getConnectedCellsList()
returns a list of pair of connected cells
vector< Cell * > newCells
size_t nbAddedCells
+1 at each update. cf getNbUpdates()
void setNbThreads(size_t n)
void clearHooks()
end of recursion
void deleteDeadCells()
removes all cells marked dead
double dtMn
The amount by which time is increased every update.
bool parallelUpdateBehavior
period at which the world should call the cells updateBehavior method.
typename embedded_plugin_type< cell_t >::type cellPlugin_t
size_t getUpdateBehaviorPeriod() const
void registerHook(const Hooks &h, hook_t f)
register a single hook method. cf registerPlugins()
size_t getNbThreads()
the size of the threadpool that can be used by plugins to launch asynchronous jobs and also directly ...
void callUpdateBehavior()
calls the updateBehavior of each cell, potentially in parallel (see parallelUpdateBehavior and nbThre...
Represents a cell in the simulation.
void setWorld(World *w)
Sets the world for the cell.
Simple threadpool, largely inspired by github.com/progschj/ThreadPool.
void setNbThreads(size_t n)
void autoChunks(Container &v, size_t minChunkSize, double avgTasksPerThread, F f)
A simple vector class template.
void pop_back()
Removes the last element of the vector.
void push_back(const T &value)
Adds an element to the end of the vector.
iterator end()
Returns an iterator to the last element.
T & back()
Returns a reference to the last element.
void insert(iterator position, const T &value)
Inserts an element at the specified position.
bool empty() const
Checks if the vector is empty.
this file contains various miscellanious utility functions & helpers *
constexpr size_t eToUI(const T &t)
ordered_pair< T * > make_ordered_cell_pair(T *a, T *b)
typename make_void< Ts... >::type void_t
void shuffle(RandomIt first, RandomIt last)
Shuffles the elements in a range.
static random_engine_t & globalRand()
access to the static global random engine.This pseudo - random generator is* used in random 3D vector...
typename T::embedded_plugin_t type