Titan  v1.0
A high-performance CUDA-based physics simulation sandbox for robotics, physics, and reinforcement learning.
spring.h
Go to the documentation of this file.
1 //
2 // Created by Jacob Austin on 5/17/18.
3 //
4 
5 #ifndef TITAN_SPRING_H
6 #define TITAN_SPRING_H
7 
8 #include "mass.h"
9 #include "vec.h"
10 
11 namespace titan {
12 
13 class Mass;
14 struct CUDA_SPRING;
15 struct CUDA_MASS;
16 
18 
19 class Spring {
20 public:
21  Spring() : _left(nullptr), _right(nullptr), _k(10000.0), _rest(1.0), _type(PASSIVE_SOFT),
22  _omega(0.0), _damping(0.0), arrayptr(nullptr) {}
23 
24  Spring(Mass * left, Mass * right) : _left(left), _right(right), _k(10000.0), _type(PASSIVE_SOFT),
25  _omega(0.0), _damping(0.0), arrayptr(nullptr)
26  {
27  this -> defaultLength();
28  }
29 
30  Spring(Mass * left, Mass * right, double k, double rest_length) : _left(left), _right(right),
31  _k(k), _rest(rest_length), _type(PASSIVE_SOFT), _omega(0.0), _damping(0.0) {}
32 
33  Spring(Mass * left, Mass * right, double k, double rest_length, SpringType type, double omega) :
34  _left(left), _right(right), _k(k), _rest(rest_length), _type(type), _omega(omega), _damping(0.0) {};
35 
36  void update(const CUDA_SPRING & spr);
37  void setRestLength(double rest_length) { _rest = rest_length; } //sets Rest length
38  void defaultLength(); //sets rest length
39  void changeType(SpringType type, double omega) { _type = type; _omega = omega;}
40  void addDamping(double constant) { _damping = constant; }
41 
42  void setLeft(Mass * left); // sets left mass (attaches spring to mass 1)
43  void setRight(Mass * right);
44 
45  void setMasses(Mass * left, Mass * right) { _left = left; _right = right; } //sets both right and left masses
46 
47  Mass * _left; // pointer to left mass object // private
48  Mass * _right; // pointer to right mass object
49 
50  double _k; // spring constant (N/m)
51  double _rest; // spring rest length (meters)
52 
53  SpringType _type; // 0-3, for oscillating springs
54  double _omega; // frequency of oscillation
55  double _damping; // damping on the masses.
56 
57 private:
58  CUDA_SPRING *arrayptr; //Pointer to struct version for GPU cudaMalloc
59 
60  friend class Simulation;
61  friend struct CUDA_SPRING;
62  friend class Container;
63  friend class Lattice;
64  friend class Cube;
65  friend class Beam;
66 };
67 
68 struct CUDA_SPRING {
70  CUDA_SPRING(const Spring & s);
71 
72  CUDA_SPRING(const Spring & s, CUDA_MASS * left, CUDA_MASS * right);
73 
74  CUDA_MASS * _left; // pointer to left mass object
75  CUDA_MASS * _right; // pointer to right mass object
76 
77  double _k; // spring constant (N/m)
78  double _rest; // spring rest length (meters)
79 
80  // Breathing
82  double _omega;
83  double _damping;
84 };
85 
86 } // namespace titan
87 
88 #endif //TITAN_SPRING_H
titan::Spring::defaultLength
void defaultLength()
titan::Spring::_rest
double _rest
Definition: spring.h:51
titan::Container
Definition: object.h:230
titan
Definition: mass.h:11
titan::Simulation
Definition: sim.h:37
titan::CUDA_SPRING::_rest
double _rest
Definition: spring.h:78
titan::Spring::changeType
void changeType(SpringType type, double omega)
Definition: spring.h:39
titan::ACTIVE_EXPAND_THEN_CONTRACT
@ ACTIVE_EXPAND_THEN_CONTRACT
Definition: spring.h:17
titan::Spring::_type
SpringType _type
Definition: spring.h:53
titan::CUDA_SPRING::_type
SpringType _type
Definition: spring.h:81
titan::Spring::Spring
Spring(Mass *left, Mass *right, double k, double rest_length, SpringType type, double omega)
Definition: spring.h:33
titan::Spring
Definition: spring.h:19
mass.h
titan::Spring::setMasses
void setMasses(Mass *left, Mass *right)
Definition: spring.h:45
titan::CUDA_MASS
Definition: mass.h:78
titan::Spring::Spring
Spring()
Definition: spring.h:21
titan::Spring::addDamping
void addDamping(double constant)
Definition: spring.h:40
titan::SpringType
SpringType
Definition: spring.h:17
titan::Spring::setLeft
void setLeft(Mass *left)
titan::CUDA_SPRING
Definition: spring.h:68
titan::ACTIVE_CONTRACT_THEN_EXPAND
@ ACTIVE_CONTRACT_THEN_EXPAND
Definition: spring.h:17
titan::Spring::setRestLength
void setRestLength(double rest_length)
Definition: spring.h:37
titan::Spring::_k
double _k
Definition: spring.h:50
titan::CUDA_SPRING::CUDA_SPRING
CUDA_SPRING(const Spring &s)
titan::Spring::Spring
Spring(Mass *left, Mass *right, double k, double rest_length)
Definition: spring.h:30
titan::PASSIVE_STIFF
@ PASSIVE_STIFF
Definition: spring.h:17
titan::Spring::_right
Mass * _right
Definition: spring.h:48
titan::Beam
Definition: object.h:277
titan::CUDA_SPRING::_damping
double _damping
Definition: spring.h:83
titan::Mass
Definition: mass.h:16
vec.h
titan::CUDA_SPRING::_k
double _k
Definition: spring.h:77
titan::Spring::Spring
Spring(Mass *left, Mass *right)
Definition: spring.h:24
titan::CUDA_SPRING::_omega
double _omega
Definition: spring.h:82
titan::Cube
Definition: object.h:257
titan::Lattice
Definition: object.h:267
titan::CUDA_SPRING::CUDA_SPRING
CUDA_SPRING(const Spring &s, CUDA_MASS *left, CUDA_MASS *right)
titan::Spring::_damping
double _damping
Definition: spring.h:55
titan::PASSIVE_SOFT
@ PASSIVE_SOFT
Definition: spring.h:17
titan::CUDA_SPRING::_right
CUDA_MASS * _right
Definition: spring.h:75
titan::Spring::_left
Mass * _left
Definition: spring.h:47
titan::CUDA_SPRING::CUDA_SPRING
CUDA_SPRING()
Definition: spring.h:69
titan::Spring::update
void update(const CUDA_SPRING &spr)
titan::CUDA_SPRING::_left
CUDA_MASS * _left
Definition: spring.h:74
titan::Spring::_omega
double _omega
Definition: spring.h:54
titan::Spring::setRight
void setRight(Mass *right)