CellModules
spring.hpp
Go to the documentation of this file.
1#ifndef SPRING_HPP
2#define SPRING_HPP
4namespace MecaCell {
6// SPRING STRUCTURE
8// This is just a classic spring-damper
9
10struct Spring {
11 double k = 1.0; // stiffness
12 double c = 1.0; // damp coef
13 double restLength = 1.0; // rest length
14 double length = 1.0; // current length
15 double prevLength = 1.0;
16 double minLengthRatio = 0.5; // max compression
17
18 Spring(){};
19 Spring(const double &K, const double &C, const double &L)
20 : k(K), c(C), restLength(L), length(L){};
21
22 void updateLength(double l) {
24 length = l;
25 }
26
27 double computeForce(double dt) {
28 double v = (length - prevLength) / dt;
29 return 0.5 * (k * (length - restLength) + c * v);
30 }
31
32 template <typename A, typename B>
33 void applyForce(A &a, B &b, const Vector3D &direction, double dt) {
34 // direction = a -> b
35 double f = computeForce(dt);
36 a.receiveForce(direction * f);
37 b.receiveForce(-direction * f);
38 }
39};
40
42// JOINT STRUCTURE
44// flexible joint. Can be used for flexure (torque + force) or torsion (torque only)
45struct Joint {
46 double k = 1.0; // angular stiffness
47 double c = 1.0; // damp
48 double maxTeta = M_PI / 10.0; // maximum angle
49 Rotation<Vec> r; // rotation from node to joint
50 Rotation<Vec> delta; // current rotation
52 Vec direction; // current direction
53 Vec target; // targeted direction
54 bool maxTetaAutoCorrect = true; // do we need to handle maxTeta?
56 Joint(){};
57
58 Joint(const double &K, const double &C, const double &MTETA, bool handleMteta = true)
59 : k(K), c(C), maxTeta(MTETA), maxTetaAutoCorrect(handleMteta) {}
60
61 // current direction is computed using a reference Vector v rotated with rotation rot
62 void updateDirection(const Vec &v, const Rotation<Vec> &rot) {
63 direction = v.rotated(r.rotated(rot));
64 }
66};
67} // namespace MecaCell
68
69#endif
CGAL::Exact_predicates_inexact_constructions_kernel K
general purpose 3D vector/point class.
Definition: vector3D.h:20
static Rotation< Vector3D > getRotation(const Vector3D &v0, const Vector3D &v1)
computes the rotation from one vector to another
Definition: vector3D.h:322
Vector3D rotated(double angle, const Vector3D &vec) const
gives a rotated copy of the current vector
Definition: vector3D.h:277
this file contains various miscellanious utility functions & helpers *
bool maxTetaAutoCorrect
Definition: spring.hpp:54
double maxTeta
Definition: spring.hpp:48
Joint(const double &K, const double &C, const double &MTETA, bool handleMteta=true)
Definition: spring.hpp:58
Rotation< Vec > r
Definition: spring.hpp:49
bool targetUpdateEnabled
Definition: spring.hpp:55
void updateDirection(const Vec &v, const Rotation< Vec > &rot)
Definition: spring.hpp:62
void updateDelta()
Definition: spring.hpp:65
Rotation< Vec > delta
Definition: spring.hpp:50
Rotation< Vec > prevDelta
Definition: spring.hpp:51
double minLengthRatio
Definition: spring.hpp:16
double restLength
Definition: spring.hpp:13
void updateLength(double l)
Definition: spring.hpp:22
double computeForce(double dt)
Definition: spring.hpp:27
void applyForce(A &a, B &b, const Vector3D &direction, double dt)
Definition: spring.hpp:33
double prevLength
Definition: spring.hpp:15
double length
Definition: spring.hpp:14
Spring(const double &K, const double &C, const double &L)
Definition: spring.hpp:19