Titan  v1.0
A high-performance CUDA-based physics simulation sandbox for robotics, physics, and reinforcement learning.
object.h
Go to the documentation of this file.
1 #ifndef TITAN_OBJECT_H
2 #define TITAN_OBJECT_H
3 
4 #include <vector>
5 #include "vec.h"
6 
7 #ifdef GRAPHICS
8 
9 #define GLM_FORCE_PURE
10 
11 // Include GLEW
12 #include <GL/glew.h>
13 
14 // Include GLFW
15 // TODO add SDL2 instead
16 #include <GLFW/glfw3.h>
17 
18 // Include GLM
19 #include <glm/glm.hpp>
20 #include <glm/gtc/matrix_transform.hpp>
21 #endif
22 
23 #ifdef CONSTRAINTS
24 #include <thrust/device_vector.h>
25 #endif
26 
27 namespace titan {
28 
29 struct CUDA_MASS;
30 class Spring;
31 class Mass;
32 
33 #ifdef __CUDACC__
34 #define CUDA_CALLABLE_MEMBER __host__ __device__
35 #else
36 #define CUDA_CALLABLE_MEMBER
37 #endif
38 
39 #ifdef __CUDACC__
40 #define CUDA_DEVICE __device__
41 #else
42 #define CUDA_DEVICE
43 #endif
44 
45 class Constraint { // constraint like plane or sphere which applies force to masses
46 public:
47  virtual ~Constraint() = default;
48 
49 #ifdef GRAPHICS
50  bool _initialized;
51  virtual void generateBuffers() = 0;
52  virtual void draw() = 0;
53 #endif
54 };
55 
56 struct Ball : public Constraint {
57  Ball(const Vec & center, double radius) {
58  _center = center;
59  _radius = radius;
60 
61 #ifdef GRAPHICS
62  _initialized = false;
63 #endif
64 
65  }
66 
67  double _radius;
69 
70 #ifdef GRAPHICS
71  ~Ball() {
72  glDeleteBuffers(1, &vertices);
73  glDeleteBuffers(1, &colors);
74  }
75 
76  void generateBuffers();
77  void draw();
78 
79  void subdivide(GLfloat * arr, GLfloat *v1, GLfloat *v2, GLfloat *v3, int depth);
80  void writeTriangle(GLfloat * arr, GLfloat *v1, GLfloat *v2, GLfloat *v3);
81  void normalize(GLfloat * v);
82 
83  int depth = 2;
84 
85  GLuint vertices;
86  GLuint colors;
87 #endif
88 };
89 
90 struct CudaBall {
91  CudaBall() = default;
92  CUDA_CALLABLE_MEMBER CudaBall(const Vec & center, double radius);
94 
96 
97  double _radius;
99 };
100 
101 struct ContactPlane : public Constraint {
102  ContactPlane(const Vec & normal, double offset) {
103  _normal = normal / normal.norm();
104  _offset = offset;
105 
106  _FRICTION_K = 0.0;
107  _FRICTION_S = 0.0;
108 
109 #ifdef GRAPHICS
110  _initialized = false;
111 #endif
112  }
113 
115  double _offset;
116 
117  double _FRICTION_K;
118  double _FRICTION_S;
119 
120 #ifdef GRAPHICS
121  ~ContactPlane() {
122  glDeleteBuffers(1, &vertices);
123  glDeleteBuffers(1, &colors);
124  }
125 
126  void generateBuffers();
127  void draw();
128 
129  GLuint vertices;
130  GLuint colors;
131 #endif
132 };
133 
135  CudaContactPlane() = default;
136  CUDA_CALLABLE_MEMBER CudaContactPlane(const Vec & normal, double offset);
138 
140 
142  double _offset;
143 
144  double _FRICTION_K;
145  double _FRICTION_S;
146 };
147 
148 
150  CudaConstraintPlane() = default;
151 
152  CUDA_CALLABLE_MEMBER CudaConstraintPlane(const Vec & normal, double friction);
153 
155 
157  double _friction;
158 };
159 
161  CudaDirection() = default;
162 
163  CUDA_CALLABLE_MEMBER CudaDirection(const Vec & tangent, double friction);
164 
166 
168  double _friction;
169 };
170 
174 
177 };
178 
179 
180 #ifdef CONSTRAINTS
181 struct LOCAL_CONSTRAINTS {
182  LOCAL_CONSTRAINTS();
183 
184  thrust::device_vector<CudaContactPlane> contact_plane;
185  thrust::device_vector<CudaConstraintPlane> constraint_plane;
186  thrust::device_vector<CudaBall> ball;
187  thrust::device_vector<CudaDirection> direction;
188 
189  CudaContactPlane * contact_plane_ptr;
190  CudaConstraintPlane * constraint_plane_ptr;
191  CudaBall * ball_ptr;
192  CudaDirection * direction_ptr;
193 
194  int num_contact_planes;
195  int num_balls;
196  int num_constraint_planes;
197  int num_directions; // if this is greater than 1, just make it fixed
198 
199  double drag_coefficient;
200  bool fixed; // move here from the class itself;
201 };
202 
203 struct CUDA_LOCAL_CONSTRAINTS {
204  CUDA_LOCAL_CONSTRAINTS() = default;
205 
206  CUDA_LOCAL_CONSTRAINTS(LOCAL_CONSTRAINTS & c);
207 
208  CudaContactPlane * contact_plane;
209  CudaConstraintPlane * constraint_plane;
210  CudaBall * ball;
211  CudaDirection * direction;
212 
213  int drag_coefficient;
214  bool fixed; // move here from the class itself;
215 
216  int num_contact_planes;
217  int num_balls;
218  int num_constraint_planes;
219  int num_directions; // if this is greater than 1, just make it fixed
220 };
221 
222 #endif
223 
224 #ifdef CONSTRAINTS
225 enum CONSTRAINT_TYPE {
226  CONSTRAINT_PLANE, CONTACT_PLANE, BALL, DIRECTION
227 };
228 #endif
229 
230 class Container { // contains and manipulates groups of masses and springs
231 public:
232  virtual ~Container() {};
233  void translate(const Vec & displ); // translate all masses by fixed amount
234  // rotate all masses around a fixed axis by a specified angle with respect to the center of mass.
235  // in radians
236  void rotate(const Vec & axis, double angle);
237  void setMassValues(double m); // set masses for all Mass objects
238  void setSpringConstants(double k); // set k for all Spring objects
239  void setRestLengths(double len); // set masses for all Mass objects
240 
241 #ifdef CONSTRAINTS
242  void addConstraint(CONSTRAINT_TYPE type, const Vec & v, double d);
243  void clearConstraints();
244 #endif
245 
246  void fix();
247 
248  void add(Mass * m);
249  void add(Spring * s);
250  void add(Container * c);
251 
252  // we can have more of these
253  std::vector<Mass *> masses;
254  std::vector<Spring *> springs;
255 };
256 
257 class Cube : public Container {
258 public:
259  ~Cube() {};
260 
261  Cube(const Vec & center, double side_length = 1.0);
262 
263  double _side_length;
265 };
266 
267 class Lattice : public Container {
268 public:
269  ~Lattice() {};
270 
271  Lattice(const Vec & center, const Vec & dims, int nx = 10, int ny = 10, int nz = 10);
272 
273  int nx, ny, nz;
275 };
276 
277 class Beam : public Container {
278 public:
279  ~Beam() {};
280 
281  Beam(const Vec & center, const Vec & dims, int nx = 10, int ny = 10, int nz = 10);
282 
283  int nx, ny, nz;
285 };
286 
287 
288 // typedef std::vector<std::vector<std::vector<std::vector<int>>>> cppn;
289 // class Robot : public Container {
290 // public:
291 // ~Robot() {};
292 
293 // // with known encoding
294 // Robot(const Vec & center, const cppn& encoding, double side_length, double omega=1.0, double k_soft=2e3, double k_stiff=2e5);
295 
296 // Vec _center;
297 // cppn _encoding;
298 // double _side_length;
299 // double _omega;
300 // double _k_soft;
301 // double _k_stiff;
302 // /* Vec _com; */
303 // /* double _score; */
304 // };
305 
306 } // namespace titan
307 
308 #endif //TITAN_OBJECT_H
titan::CUDA_GLOBAL_CONSTRAINTS::d_balls
CudaBall * d_balls
Definition: object.h:173
titan::Ball::_center
Vec _center
Definition: object.h:68
titan::ContactPlane::_offset
double _offset
Definition: object.h:115
titan::Container::fix
void fix()
titan::Container
Definition: object.h:230
titan
Definition: mass.h:11
titan::CudaContactPlane::applyForce
CUDA_CALLABLE_MEMBER void applyForce(CUDA_MASS *m)
titan::Beam::nx
int nx
Definition: object.h:283
titan::Ball::Ball
Ball(const Vec &center, double radius)
Definition: object.h:57
titan::Vec::norm
CUDA_CALLABLE_MEMBER double norm() const
Definition: vec.h:149
titan::CudaContactPlane::_FRICTION_K
double _FRICTION_K
Definition: object.h:144
titan::CudaBall::CudaBall
CUDA_CALLABLE_MEMBER CudaBall(const Ball &b)
titan::Container::~Container
virtual ~Container()
Definition: object.h:232
titan::CUDA_GLOBAL_CONSTRAINTS::num_planes
int num_planes
Definition: object.h:175
CUDA_CALLABLE_MEMBER
#define CUDA_CALLABLE_MEMBER
Definition: object.h:36
titan::Beam::_center
Vec _center
Definition: object.h:284
titan::CudaBall
Definition: object.h:90
titan::Cube::~Cube
~Cube()
Definition: object.h:259
titan::Container::rotate
void rotate(const Vec &axis, double angle)
titan::CudaConstraintPlane
Definition: object.h:149
titan::Container::translate
void translate(const Vec &displ)
titan::Container::setRestLengths
void setRestLengths(double len)
titan::Beam::~Beam
~Beam()
Definition: object.h:279
titan::Lattice::_dims
Vec _dims
Definition: object.h:274
titan::Beam::nz
int nz
Definition: object.h:283
titan::CudaDirection::_friction
double _friction
Definition: object.h:168
titan::ContactPlane::_normal
Vec _normal
Definition: object.h:114
titan::CUDA_GLOBAL_CONSTRAINTS::d_planes
CudaContactPlane * d_planes
Definition: object.h:172
titan::Constraint
Definition: object.h:45
titan::Spring
Definition: spring.h:19
titan::ContactPlane
Definition: object.h:101
titan::Lattice::nz
int nz
Definition: object.h:273
titan::CUDA_MASS
Definition: mass.h:78
titan::Vec
Definition: vec.h:33
titan::CudaConstraintPlane::applyForce
CUDA_CALLABLE_MEMBER void applyForce(CUDA_MASS *m)
titan::Cube::_side_length
double _side_length
Definition: object.h:263
titan::Container::add
void add(Mass *m)
titan::Container::masses
std::vector< Mass * > masses
Definition: object.h:253
titan::Lattice::Lattice
Lattice(const Vec &center, const Vec &dims, int nx=10, int ny=10, int nz=10)
titan::Ball
Definition: object.h:56
titan::CudaBall::CudaBall
CUDA_CALLABLE_MEMBER CudaBall(const Vec &center, double radius)
titan::Constraint::~Constraint
virtual ~Constraint()=default
titan::CudaDirection::CudaDirection
CudaDirection()=default
titan::CudaBall::_center
Vec _center
Definition: object.h:98
titan::CudaConstraintPlane::CudaConstraintPlane
CudaConstraintPlane()=default
titan::Beam::_dims
Vec _dims
Definition: object.h:284
titan::Beam::ny
int ny
Definition: object.h:283
titan::CudaContactPlane::_FRICTION_S
double _FRICTION_S
Definition: object.h:145
titan::CudaBall::_radius
double _radius
Definition: object.h:97
titan::CudaConstraintPlane::_normal
Vec _normal
Definition: object.h:156
titan::CUDA_GLOBAL_CONSTRAINTS::num_balls
int num_balls
Definition: object.h:176
titan::CudaDirection::applyForce
CUDA_CALLABLE_MEMBER void applyForce(CUDA_MASS *m)
titan::Ball::_radius
double _radius
Definition: object.h:67
titan::CUDA_GLOBAL_CONSTRAINTS
Definition: object.h:171
titan::CudaContactPlane::_normal
Vec _normal
Definition: object.h:141
titan::Lattice::ny
int ny
Definition: object.h:273
titan::CudaBall::CudaBall
CudaBall()=default
titan::Container::add
void add(Container *c)
titan::Lattice::_center
Vec _center
Definition: object.h:274
titan::Cube::Cube
Cube(const Vec &center, double side_length=1.0)
titan::CudaDirection::_tangent
Vec _tangent
Definition: object.h:167
titan::ContactPlane::_FRICTION_S
double _FRICTION_S
Definition: object.h:118
titan::Beam
Definition: object.h:277
titan::Beam::Beam
Beam(const Vec &center, const Vec &dims, int nx=10, int ny=10, int nz=10)
titan::Container::setMassValues
void setMassValues(double m)
titan::Container::springs
std::vector< Spring * > springs
Definition: object.h:254
titan::Mass
Definition: mass.h:16
vec.h
titan::CudaConstraintPlane::_friction
double _friction
Definition: object.h:157
titan::CudaContactPlane
Definition: object.h:134
titan::CudaContactPlane::CudaContactPlane
CUDA_CALLABLE_MEMBER CudaContactPlane(const Vec &normal, double offset)
titan::Lattice::~Lattice
~Lattice()
Definition: object.h:269
titan::ContactPlane::_FRICTION_K
double _FRICTION_K
Definition: object.h:117
titan::Cube
Definition: object.h:257
titan::Lattice
Definition: object.h:267
titan::CudaContactPlane::CudaContactPlane
CudaContactPlane(const ContactPlane &p)
titan::CudaDirection
Definition: object.h:160
titan::CudaConstraintPlane::CudaConstraintPlane
CUDA_CALLABLE_MEMBER CudaConstraintPlane(const Vec &normal, double friction)
titan::CudaDirection::CudaDirection
CUDA_CALLABLE_MEMBER CudaDirection(const Vec &tangent, double friction)
titan::Cube::_center
Vec _center
Definition: object.h:264
titan::CudaBall::applyForce
CUDA_CALLABLE_MEMBER void applyForce(CUDA_MASS *m)
titan::Container::add
void add(Spring *s)
titan::CudaContactPlane::CudaContactPlane
CudaContactPlane()=default
titan::ContactPlane::ContactPlane
ContactPlane(const Vec &normal, double offset)
Definition: object.h:102
titan::Lattice::nx
int nx
Definition: object.h:273
titan::Container::setSpringConstants
void setSpringConstants(double k)
titan::CudaContactPlane::_offset
double _offset
Definition: object.h:142