generated from libigl/libigl-example-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysicsHook.h
191 lines (167 loc) · 5.69 KB
/
PhysicsHook.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef PHYSICSHOOK_H
#define PHYSICSHOOK_H
#include <mutex>
#include <thread>
#include <igl/opengl/glfw/Viewer.h>
#include <igl/opengl/glfw/imgui/ImGuiMenu.h>
class PhysicsHook
{
public:
PhysicsHook() : sim_thread(NULL), please_pause(false), please_die(false), running(false)
{
}
virtual ~PhysicsHook()
{
killSimThread();
}
/*
* Runs when the user redraws/interacts with the GUI.
*/
virtual void drawGUI(igl::opengl::glfw::imgui::ImGuiMenu &menu) = 0;
/*
* Runs once when the simulation is initialized, and again each time the user resets the simulation.
*/
virtual void initSimulation() = 0;
/*
* Called every once in a while (and always before every simulation step) even if the simulation is paused.
* Use this to update the visualization in response to user input etc.
*/
virtual void tick() {};
/*
* Takes one simulation "step." You can do whatever you want here, but the granularity of your computation should
* be small enough that the user can view/pause/kill the simulation at interactive rates.
* This method *must* be thread-safe with respect to renderRenderGeometry() (easiest is to not touch any rendering
* data structures at all).
*/
virtual bool simulationStep() = 0;
/*
* Update the rendering data structures here. This method will be called in alternation with simulateOneStep().
* This method blocks rendering in the viewer, so do *not* do extensive computation here (leave it to
* simulateOneStep()).
*/
virtual void updateGeometry() = 0;
/*
* Perform any actual rendering here. This method *must* be thread-safe with respect to simulateOneStep().
* This method runs in the same thread as the viewer and blocks user IO, so there really should not be any
* extensive computation here or the UI will lag/become unresponsive (the whole reason the simulation itself
* is in its own thread.)
*/
virtual void renderGeometry(igl::opengl::glfw::Viewer &viewer) = 0;
/*
* Called when the user clicks on the simulation panel.
* This method is called in the *rendering thread*. If you need to make changes to the simulation state, you
* should stash the mouse click in a message queue and deal with it in the simulation thread.
*/
virtual bool mouseClicked(igl::opengl::glfw::Viewer &viewer, int button) { return false; }
/*
* Called when the user unclicks the mouse on the simulation panel.
* This method is called in the *rendering thread*. If you need to make changes to the simulation state, you
* should stash the mouse click in a message queue and deal with it in the simulation thread.
*/
virtual bool mouseReleased(igl::opengl::glfw::Viewer &viewer, int button) { return false; }
/*
* Called when the user drags the mouse on the simulation panel.
* This method is called in the *rendering thread*. If you need to make changes to the simulation state, you
* should stash the mouse click in a message queue and deal with it in the simulation thread.
*/
virtual bool mouseMoved(igl::opengl::glfw::Viewer &viewer, int button) { return false; }
/*
* Runs the simulation, if it has been paused (or never started).
*/
void run()
{
status_mutex.lock();
please_pause = false;
status_mutex.unlock();
}
/*
* Resets the simulation (and leaves it in a paused state; call run() to start it).
*/
void reset()
{
killSimThread();
please_die = running = false;
please_pause = true;
initSimulation();
updateGeometry();
sim_thread = new std::thread(&PhysicsHook::runSimThread, this);
}
/*
* Pause a running simulation. The simulation will pause at the end of its current "step"; this method will not
* interrupt simulateOneStep mid-processing.
*/
void pause()
{
status_mutex.lock();
please_pause = true;
status_mutex.unlock();
}
bool isPaused()
{
bool ret = false;
status_mutex.lock();
if(running && please_pause)
ret = true;
status_mutex.unlock();
return ret;
}
void render(igl::opengl::glfw::Viewer &viewer)
{
render_mutex.lock();
renderGeometry(viewer);
render_mutex.unlock();
}
protected:
void runSimThread()
{
status_mutex.lock();
running = true;
status_mutex.unlock();
bool done = false;
while (!done)
{
tick();
status_mutex.lock();
bool pausenow = please_pause;
status_mutex.unlock();
if (pausenow)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
else
{
render_mutex.lock();
updateGeometry();
done = simulationStep();
render_mutex.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
status_mutex.lock();
if (please_die)
done = true;
status_mutex.unlock();
}
status_mutex.lock();
running = false;
status_mutex.unlock();
}
void killSimThread()
{
if (sim_thread)
{
status_mutex.lock();
please_die = true;
status_mutex.unlock();
sim_thread->join();
delete sim_thread;
sim_thread = NULL;
}
}
std::thread *sim_thread;
bool please_pause;
bool please_die;
bool running;
std::mutex render_mutex;
std::mutex status_mutex;
};
#endif