Titan  v1.0
A high-performance CUDA-based physics simulation sandbox for robotics, physics, and reinforcement learning.
sim.h
Go to the documentation of this file.
1 //
2 // Created by Jacob Austin on 5/17/18.
3 //
4 
5 #ifndef TITAN_SIM_H
6 #define TITAN_SIM_H
7 
8 #ifdef GRAPHICS
9 
10 #include "graphics.h"
11 #include "shader.h"
12 
13 #endif
14 
15 #include <thrust/host_vector.h>
16 #include <thrust/device_vector.h>
17 
18 #include <algorithm>
19 #include <list>
20 #include <vector>
21 #include <set>
22 #include <thread>
23 
24 #include "spring.h"
25 #include "mass.h"
26 #include "object.h"
27 #include "vec.h"
28 
29 namespace titan {
30 
31 #define MAX_BLOCKS 65535 // max number of CUDA blocks
32 #define THREADS_PER_BLOCK 1024
33 
34 #ifndef GRAPHICS
35 #endif
36 
37 class Simulation {
38 public:
39  // Create
41  Mass * createMass(const Vec & pos);
42 
44  Spring * createSpring(Mass * m1, Mass * m2);
45 
46  // Delete
47  void deleteMass(Mass * m);
48  void deleteSpring(Spring * s);
49 
50  void get(Mass *m);
51  void get(Spring *s); // not really useful, no commands change springs
52  void get(Container *c);
53 
54  void set(Mass * m);
55  void set(Spring *s);
56  void set(Container * c);
57 
58  void getAll();
59  void setAll();
60 
61  // Global constraints (can be rendered)
62  void createPlane(const Vec & abc, double d ); // creates half-space ax + by + cz < d
63  void createPlane(const Vec &abc, double d, double FRICTION_K, double FRICTION_S); // creates half-space ax + by + cz < d
64 
65  void createBall(const Vec & center, double r ); // creates ball with radius r at position center
66 
67  void clearConstraints(); // clears global constraints only
68 
69  // Containers
72 
73  Cube * createCube(const Vec & center, double side_length); // creates cube
74  Lattice * createLattice(const Vec & center, const Vec & dims, int nx = 10, int ny = 10, int nz = 10);
75  // Robot * createRobot(const Vec & center, const cppn& encoding, double side_length, double omega=1.0, double k_soft=2e3, double k_stiff=2e5);
76  Beam * createBeam(const Vec & center, const Vec & dims, int nx = 10, int ny = 10, int nz = 10);
77  Container * importFromSTL(const std::string & path, double density = 10.0, int num_rays = 5); // density is vertices / volume
78 
79  // Bulk modifications, only update CPU
81  void setAllMassValues(double m);
82  void setTimeStep(double delta_t);
83  void setGlobalAcceleration(const Vec & global_acc);
85 
86  // Control
87  void start(); // start simulation
88 
89  void stop(); // stop simulation while paused, free all memory.
90  void stop(double time); // stop simulation at time
91 
92  void pause(double t); // pause at time t, block CPU until t.
93  void resume();
94 
95  void reset(); // reset the simulation
96 
97  void setBreakpoint(double time); // tell the program to stop at a fixed time (doesn't hang).
98 
99  void wait(double t); // wait fixed time without stopping simulation
100  void waitUntil(double t); // wait until time without stopping simulation
101  void waitForEvent(); // wait until event (e.g. breakpoint)
102 
103  double time();
104  bool running();
105 
107 
110 
114 
115  std::vector<Mass *> masses;
116  std::vector<Spring *> springs;
117  std::vector<Container *> containers;
118 
119 #ifdef GRAPHICS
120  void setViewport(const Vec & camera_position, const Vec & target_location, const Vec & up_vector);
121  void moveViewport(const Vec & displacement);
122  glm::mat4 & getProjectionMatrix();
123 #endif
124 
125 private:
126  void freeGPU();
127  void _run();
128 
129  void execute(); // same as above but w/out reset
130 
131  //Prints
132  void printSprings();
133 
134  Mass * createMass(Mass * m); // utility
135  Spring * createSpring(Spring * s); // utility
136 
137  double dt; // set to 0 by default, when start is called will be set to min(mass dt) unless previously set
138  double T; // global simulation time
139 
140  static bool RUNNING;
141  static bool STARTED;
142  static bool ENDED;
143  static bool FREED;
144  static bool GPU_DONE;
145 
146  std::vector<Constraint *> constraints;
147 
148  thrust::device_vector<CUDA_MASS *> d_masses;
149  thrust::device_vector<CUDA_SPRING *> d_springs;
150 
151  thrust::device_vector<CudaContactPlane> d_planes; // used for constraints
152  thrust::device_vector<CudaBall> d_balls; // used for constraints
153 
154  CUDA_GLOBAL_CONSTRAINTS d_constraints;
155  bool update_constraints;
156 
157  void updateCudaParameters();
158 
159  std::set<double> bpts; // list of breakpoints
160 
161  CUDA_MASS ** d_mass;
162  CUDA_SPRING ** d_spring;
163 
164  int massBlocksPerGrid;
165  int springBlocksPerGrid;
166 
167  CUDA_MASS ** massToArray();
168  CUDA_SPRING ** springToArray();
169  void constraintsToArray();
170  void toArray();
171 
172  void massFromArray();
173  void springFromArray();
174  void constraintsFromArray();
175  void fromArray();
176 
177  std::thread gpu_thread;
178  Vec _global_acc; // global acceleration
179 
180 #ifdef GRAPHICS
181 
182 #ifdef SDL2
183  static SDL_Window * window;
184  static SDL_GLContext context;
185  void createSDLWindow();
186 #else
187  static GLFWwindow * window;
188  void createGLFWWindow();
189 #endif
190 
191  static GLuint VertexArrayID;
192  static GLuint programID;
193  static GLuint MatrixID;
194  static glm::mat4 MVP;
195 
196  static GLuint vertices;
197  static GLuint colors;
198  static GLuint indices;
199 
200  void clearScreen();
201  void renderScreen();
202  void updateBuffers();
203  void generateBuffers();
204  void resizeBuffers();
205  void draw();
206 
207  static bool update_indices;
208  static bool update_colors;
209  static bool resize_buffers;
210 
211  static int lineWidth;
212  static int pointSize;
213 
214  static Vec camera;
215  static Vec looks_at;
216  static Vec up;
217 #endif
218 };
219 
220 } // namespace titan
221 
222 #endif //TITAN_SIM_H
titan::Simulation::waitForEvent
void waitForEvent()
titan::Simulation::wait
void wait(double t)
titan::Container
Definition: object.h:230
titan
Definition: mass.h:11
graphics.h
titan::Simulation::createSpring
Spring * createSpring(Mass *m1, Mass *m2)
titan::Simulation
Definition: sim.h:37
titan::Simulation::~Simulation
~Simulation()
titan::Simulation::createBall
void createBall(const Vec &center, double r)
titan::Simulation::createPlane
void createPlane(const Vec &abc, double d)
titan::Simulation::deleteContainer
void deleteContainer(Container *c)
titan::Simulation::getContainerByIndex
Container * getContainerByIndex(int i)
titan::Simulation::set
void set(Spring *s)
titan::Simulation::reset
void reset()
titan::Simulation::createPlane
void createPlane(const Vec &abc, double d, double FRICTION_K, double FRICTION_S)
titan::Simulation::importFromSTL
Container * importFromSTL(const std::string &path, double density=10.0, int num_rays=5)
titan::Simulation::resume
void resume()
titan::Simulation::printPositions
void printPositions()
titan::Simulation::containers
std::vector< Container * > containers
Definition: sim.h:117
titan::Simulation::getSpringByIndex
Spring * getSpringByIndex(int i)
titan::Simulation::masses
std::vector< Mass * > masses
Definition: sim.h:115
titan::Simulation::pause
void pause(double t)
titan::Simulation::setAllMassValues
void setAllMassValues(double m)
titan::Simulation::createBeam
Beam * createBeam(const Vec &center, const Vec &dims, int nx=10, int ny=10, int nz=10)
titan::Simulation::getMassByIndex
Mass * getMassByIndex(int i)
titan::Simulation::deleteMass
void deleteMass(Mass *m)
titan::Spring
Definition: spring.h:19
titan::Simulation::setGlobalAcceleration
void setGlobalAcceleration(const Vec &global_acc)
spring.h
mass.h
titan::Simulation::get
void get(Spring *s)
titan::Simulation::get
void get(Mass *m)
titan::Simulation::setAllSpringConstantValues
void setAllSpringConstantValues(double k)
titan::CUDA_MASS
Definition: mass.h:78
titan::Vec
Definition: vec.h:33
titan::Simulation::createSpring
Spring * createSpring()
titan::Simulation::createCube
Cube * createCube(const Vec &center, double side_length)
titan::Simulation::waitUntil
void waitUntil(double t)
titan::Simulation::time
double time()
titan::Simulation::stop
void stop(double time)
titan::Simulation::deleteSpring
void deleteSpring(Spring *s)
titan::Simulation::setBreakpoint
void setBreakpoint(double time)
titan::Simulation::createContainer
Container * createContainer()
titan::CUDA_GLOBAL_CONSTRAINTS
Definition: object.h:171
titan::Simulation::clearConstraints
void clearConstraints()
titan::Simulation::get
void get(Container *c)
titan::CUDA_SPRING
Definition: spring.h:68
titan::Simulation::createMass
Mass * createMass()
titan::Simulation::springs
std::vector< Spring * > springs
Definition: sim.h:116
titan::Beam
Definition: object.h:277
titan::Simulation::createMass
Mass * createMass(const Vec &pos)
titan::Simulation::getAll
void getAll()
titan::Simulation::stop
void stop()
titan::Mass
Definition: mass.h:16
titan::Simulation::start
void start()
titan::Simulation::defaultRestLengths
void defaultRestLengths()
vec.h
shader.h
titan::Cube
Definition: object.h:257
titan::Lattice
Definition: object.h:267
titan::Simulation::Simulation
Simulation()
titan::Simulation::createLattice
Lattice * createLattice(const Vec &center, const Vec &dims, int nx=10, int ny=10, int nz=10)
titan::Simulation::set
void set(Mass *m)
titan::Simulation::running
bool running()
titan::Simulation::setTimeStep
void setTimeStep(double delta_t)
object.h
titan::Simulation::set
void set(Container *c)
titan::Simulation::setAll
void setAll()