CellModules
elasticbody.hpp
Go to the documentation of this file.
1#ifndef ELASTICBODY_HPP
2#define ELASTICBODY_HPP
6#include "integrators.hpp"
7#include "orientable.h"
8#include "spring.hpp"
13
14namespace MecaCell {
15template <typename H> struct GhostCenter : public OrientedParticle {
16 H &host;
17 double fK = 3.0;
18 double tK = 1.0;
19 double prevDist = 0;
21 this->mass = host.getMass();
22 this->position = h.getPosition();
23 this->orientation = h.getOrientation();
24 this->orientationRotation = h.getOrientationRotation();
25 }
26 template <typename Integrator = Euler> void update(double dt, double momentOfInertia) {
27 this->mass = host.getMass();
28 logger<DBG>("GhostCenter force = ", this->force);
29 Integrator::updatePosition(*this, dt);
30 Integrator::updateOrientation(*this, momentOfInertia, dt);
31 auto dir = this->position - host.getPosition();
32 double l = dir.length();
33 if (l > 0) {
34 dir = dir / l;
35 double f = std::min(1.0 / dt, l * fK);
36 host.receiveForce(dir * f);
37 host.receiveForce(-host.getVelocity() * 0.05);
38 logger<DBG>("Host total force = ", host.getForce(), " (sent = ", dir * f, ")");
39 }
40 prevDist = l;
41 // auto rot = Vector3D::getRotation(this->getOrientation(), host.getOrientation());
42 // host.receiveTorque(rot.n * rot.teta * tK);
43 this->resetForce();
44 this->resetTorque();
45 }
46};
47
48template <typename Cell> class ElasticBody {
50
51 Cell *cell = nullptr;
53 double restRadius = Config::DEFAULT_CELL_RADIUS; // radiius of the cell when at rest
56
57 public:
61
64 void setRestRadius(double r) { restRadius = r; }
65 double getBoundingBoxRadius() const { return restRadius; };
66 double getYoungModulus() const { return youngModulus; }
67 double getPoissonCoef() const { return poissonCoef; }
68 void setYoungModulus(double y) { youngModulus = y; }
69 void setPoissonCoef(double p) { poissonCoef = p; }
70 void setMass(double m) { realCenter.setMass(m); }
74 double getMass() { return realCenter.getMass(); }
75 double getMomentOfInertia() const {
76 return 0.4 * realCenter.getMass() * restRadius * restRadius;
77 }
79 void receiveTorque(const Vec &t) { ghostCenter.receiveTorque(t); }
80 void receiveForce(const Vec &f) { ghostCenter.receiveForce(f); }
81
82 template <typename Integrator = Euler> void updatePositionsAndOrientations(double dt) {
83 Integrator::updatePosition(realCenter, dt);
84 }
85
86 std::tuple<Cell *, double> getConnectedCellAndMembraneDistance(const Vec &d) const {
87 // /!\ assumes that d is normalized
88 Cell *closestCell = nullptr;
89 double closestDist = restRadius;
90 for (auto &con : cellConnections) {
91 auto normal = cell == con->cells.first ? -con->direction : con->direction;
92 double dot = normal.dot(d);
93 if (dot < 0) {
94 const auto &midpoint =
95 cell == con->cells.first ? con->midpoint.first : con->midpoint.second;
96 double l = -midpoint / dot;
97 if (l < closestDist) {
98 closestDist = l;
99 closestCell = con->cells.first == cell ? con->cells.second : con->cells.first;
100 }
101 }
102 }
103 return std::make_tuple(closestCell, closestDist);
104 }
107
108 inline Cell *getConnectedCell(const Vec &d) const {
109 return get<0>(getConnectedCellAndMembraneDistance(d));
110 }
111
112 // required by GenericConnection Plugin
113 inline double getPreciseMembraneDistance(const Vec &d) const {
114 return get<1>(getConnectedCellAndMembraneDistance(d));
115 }
116
117 void moveTo(Vector3D newpos) {
118 ghostCenter.setPosition(newpos);
119 ghostCenter.setVelocity(Vec::zero());
120 }
121
122 void updateInternals(double dt) {
123 auto moi = getMomentOfInertia();
124 ghostCenter.update(dt, moi);
125 }
126};
127}
128#endif
MecaCell::Vec pos
Definition: CellBody.hpp:34
void setRestRadius(double r)
Definition: elasticbody.hpp:64
std::vector< ElasticConnection< Cell > * > cellConnections
Definition: elasticbody.hpp:52
ElasticBody(Cell *c, Vector3D pos=Vector3D::zero())
Definition: elasticbody.hpp:62
std::tuple< Cell *, double > getConnectedCellAndMembraneDistance(const Vec &d) const
Definition: elasticbody.hpp:86
void updatePositionsAndOrientations(double dt)
Definition: elasticbody.hpp:82
void setPoissonCoef(double p)
Definition: elasticbody.hpp:69
void receiveForce(const Vec &f)
Definition: elasticbody.hpp:80
double getPoissonCoef() const
Definition: elasticbody.hpp:67
double getYoungModulus() const
Definition: elasticbody.hpp:66
void updateInternals(double dt)
void setMass(double m)
Definition: elasticbody.hpp:70
void setYoungModulus(double y)
Definition: elasticbody.hpp:68
double getPreciseMembraneDistance(const Vec &d) const
void moveTo(Vector3D newpos)
Cell * getConnectedCell(const Vec &d) const
GhostCenter< OrientedParticle > ghostCenter
Definition: elasticbody.hpp:60
OrientedParticle realCenter
Definition: elasticbody.hpp:59
double getMomentOfInertia() const
Definition: elasticbody.hpp:75
double getBoundingBoxRadius() const
Definition: elasticbody.hpp:65
void receiveTorque(const Vec &t)
Definition: elasticbody.hpp:79
double getMass() const
Definition: movable.h:30
void resetForce()
Definition: movable.h:46
void setMass(const double m)
Definition: movable.h:35
Vec getVelocity() const
Definition: movable.h:28
Vec getPosition() const
Definition: movable.h:26
Vec getForce() const
Definition: movable.h:29
Basis< Vec > orientation
Definition: orientable.h:9
Rotation< Vec > orientationRotation
Definition: orientable.h:10
Vec getTorque() const
Definition: orientable.h:22
general purpose 3D vector/point class.
Definition: vector3D.h:20
double length() const
compute the length of the vector
Definition: vector3D.h:257
static Vector3D zero()
constructs a zero vector
Definition: vector3D.h:170
Represents a cell in the simulation.
Definition: PrimoCell.hpp:53
A simple vector class template.
Definition: std.hpp:290
this file contains various miscellanious utility functions & helpers *
static constexpr double DEFAULT_CELL_POISSONCOEF
Definition: config.hpp:16
static constexpr double DEFAULT_CELL_RADIUS
Definition: config.hpp:12
static constexpr double DEFAULT_CELL_YOUNGMOD
Definition: config.hpp:15
An Elastic Connection is a connection between two cells.
void update(double dt, double momentOfInertia)
Definition: elasticbody.hpp:26