CellModules
connectablecell.hpp
Go to the documentation of this file.
1#ifndef MECACELL_CONNECTABLECELL_HPP
2#define MECACELL_CONNECTABLECELL_HPP
3#include <array>
4#include <cmath>
5#include <cstdlib>
6#include <iostream>
7#include <memory>
8#include <sstream>
9#include <vector>
12#include "movable.h"
13#include "orientable.h"
17
18namespace MecaCell {
19
33template <class Derived, template <class> class Body = ContactSurfaceBody>
35 public:
36 using body_t = Body<Derived>;
38 // using vec_t = decltype((body_t) nullptr->getPosition());
39 friend body_t;
41
42 protected:
43 body_t body; // core implementation
44 bool dead = false;
45
46 // viewer related attributes. They probably could be moved elswhere but are clearly
47 // harmless and greatly simplify basic usage of a simple viewer. They might also be used
48 // toconvey information in headless mode
49 bool isVisible = true; // should we display this cell? (cosmetic only - cell is still
50 // present and active -)
51 array<double, 3> color = {
52 {0.75, 0.12, 0.07}}; // cell's color (interpreted as RGB by viewer)
53
54 unique_vector<Derived *> connectedCells; // list of currently connected cells
55
56 // helpers & shortcuts
57 inline Derived *selfptr() { return static_cast<Derived *>(this); }
58 inline Derived &self() { return static_cast<Derived &>(*this); }
59 const Derived &selfconst() const { return static_cast<const Derived &>(*this); }
60
61 public:
69 void eraseConnectedCell(Derived *cell) { connectedCells.erase(cell); }
70 void addConnectedCell(Derived *c) { connectedCells.insert(c); }
72 bool isConnectedTo(Derived *c) { return connectedCells.count(c); }
73
74 size_t id = 0; // mostly for debugging, num of cell by order of addition in world
75 size_t getId() const { return id; }
76 ConnectableCell(const Derived &c)
77 : body(static_cast<Derived *>(this)), dead(false), color(c.color) {}
78
80 : ConnectableCell(static_cast<const Derived &>(c)) {}
81
87 ConnectableCell(const Derived *c) : ConnectableCell(*c) {}
88
94 ConnectableCell(Vec pos) : body(static_cast<Derived *>(this), pos) {}
95
97
107 ConnectableCell(const Derived &c, const Vec &translation)
108 : Movable(c.getPosition() + translation, c.mass),
109 body(static_cast<Derived *>(this)),
110 dead(false),
111 color(c.color) {}
112
113 /*************** UPDATES **************/
114
118 void die() { dead = true; }
119 bool isDead() { return dead; }
120 Vector3D getPosition() const { return body.getPosition(); }
121
122 void setColorRGB(size_t r, size_t g, size_t b) {
123 color = {{static_cast<double>(r) / 255.0, static_cast<double>(g) / 255.0,
124 static_cast<double>(b) / 255.0}};
125 }
126 void setColorRGB(std::array<int, 3> rgb) { setColorRGB(rgb[0], rgb[1], rgb[2]); }
127 void setColorRGB(std::array<double, 3> rgb) { color = rgb; }
128 void setColorHSV(double H, double S, double V) { color = hsvToRgb(H, S, V); }
129 void setColorHSV(std::array<double, 3> hsv) { setColorHSV(hsv[0], hsv[1], hsv[2]); }
130
136 void setVisible(bool v) { isVisible = v; }
137
143 string toString() {
144 stringstream s;
145 s << "Cell " << id << " :" << std::endl;
146 s << " position = " << getPosition() << std::endl;
147 s << " nbConnections = " << connectedCells.size();
148 return s.str();
149 }
150
151 /************** GET ******************/
152 body_t &getBody() { return body; }
153 const body_t &getConstBody() const { return body; }
154 double getBoundingBoxRadius() const { return body.getBoundingBoxRadius(); }
155 double getColor(unsigned int i) const {
156 if (i < 3) return color[i];
157 return 0;
158 }
161 }
162 bool getVisible() { return isVisible; }
163 int getNbConnections() const { return connectedCells.size(); }
164
166};
167} // namespace MecaCell
168#endif
MecaCell::Vec pos
Definition: CellBody.hpp:34
CellPlugin< cell_t > embedded_plugin_t
Definition: CellBody.hpp:39
Basis for every cell a user might want to use.
array< double, 3 > color
ConnectableCell(const Derived *c)
Copy constructor.
unique_vector< Derived * > connectedCells
void setColorHSV(double H, double S, double V)
void setColorRGB(std::array< double, 3 > rgb)
const body_t & getConstBody() const
void addConnectedCell(Derived *c)
ConnectableCell(const Derived &c, const Vec &translation)
Copy constructor with translation.
void setColorRGB(std::array< int, 3 > rgb)
bool isConnectedTo(Derived *c)
void setColorRGB(size_t r, size_t g, size_t b)
double getColor(unsigned int i) const
void setVisible(bool v)
should a viewer displpay this cell ?
const std::vector< Derived * > & getConnectedCells() const
void eraseConnectedCell(Derived *cell)
disconnect a neighboring cell
string toString()
dumps internal infos
ConnectableCell(const Derived &c)
ConnectableCell(Vec pos)
Constructor.
EXPORTABLE(ConnectableCell, KV(body), KV(id))
const Derived & selfconst() const
void die()
flags the cell as dead so it can be cleanly removed from the world
double getBoundingBoxRadius() const
typename body_t::embedded_plugin_t embedded_plugin_t
ConnectableCell(const ConnectableCell &c)
void setColorHSV(std::array< double, 3 > hsv)
general purpose 3D vector/point class.
Definition: vector3D.h:20
void erase(const T &t)
void insert(U &&u)
const std::vector< T > & getUnderlyingVector() const
size_t count(const T &t) const
size_t size() const
#define KV(p)
Definition: exportable.hpp:30
this file contains various miscellanious utility functions & helpers *
std::array< double, 3 > hsvToRgb(double h, double s, double v)
transform hsv color space to rgb
Definition: utils.hpp:97
iostream endl
End-of-line manipulator.
Definition: std.hpp:274