-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunphysics.cpp
executable file
·197 lines (178 loc) · 7.17 KB
/
runphysics.cpp
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
192
193
194
195
196
197
/*
Copyright ©2013 The Regents of the University of California
(Regents). All Rights Reserved. Permission to use, copy, modify, and
distribute this software and its documentation for educational,
research, and not-for-profit purposes, without fee and without a
signed licensing agreement, is hereby granted, provided that the
above copyright notice, this paragraph and the following two
paragraphs appear in all copies, modifications, and
distributions. Contact The Office of Technology Licensing, UC
Berkeley, 2150 Shattuck Avenue, Suite 510, Berkeley, CA 94720-1620,
(510) 643-7201, for commercial licensing opportunities.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "runphysics.hpp"
#include "conf.hpp"
#include "io.hpp"
#include "misc.hpp"
#include "separateobs.hpp"
#include "simulation.hpp"
#include "timer.hpp"
#include "util.hpp"
#include <boost/filesystem.hpp>
#include <cstdio>
#include <fstream>
using namespace std;
static string outprefix;
static fstream timingfile;
Simulation sim;
int frame;
Timer fps;
void copy_file (const string &input, const string &output);
void init_physics (const string &json_file, string outprefix,
bool is_reloading) {
load_json(json_file, sim);
::outprefix = outprefix;
if (!outprefix.empty()) {
::timingfile.open(stringf("%s/timing", outprefix.c_str()).c_str(),
is_reloading ? ios::out|ios::app : ios::out);
// Make a copy of the config file for future use
copy_file(json_file.c_str(), stringf("%s/conf.json",outprefix.c_str()));
// And copy over all the obstacles
vector<Mesh*> base_meshes(sim.obstacles.size());
for (int o = 0; o < sim.obstacles.size(); o++)
base_meshes[o] = &sim.obstacles[o].base_mesh;
save_objs(base_meshes, stringf("%s/obs", outprefix.c_str()));
}
prepare(sim);
if (!is_reloading) {
separate_obstacles(sim.obstacle_meshes, sim.cloth_meshes);
relax_initial_state(sim);
}
}
static void save (const vector<Mesh*> &meshes, int frame, bool is_obs=false) {
if (!outprefix.empty() && frame < 10000)
// std::cout << "before saving cloth mesh" <<std::endl;
if(is_obs) save_objs(meshes, stringf("%s/obs_%04d", outprefix.c_str(), frame));
else save_objs(meshes, stringf("%s/%04d", outprefix.c_str(), frame));
// std::cout << "end saving cloth mesh" <<std::endl;
}
static void save_obstacle_transforms (const vector<Obstacle> &obs, int frame,
double time) {
if (!outprefix.empty() && frame < 10000) {
for (int o = 0; o < obs.size(); o++) {
Transformation trans = identity_();
if (obs[o].transform_spline) {
trans = get_dtrans(*obs[o].transform_spline, time).first;
// std::cout << "get dtrans finished, to save transformation" << std::endl;
save_transformation(trans, stringf("%s/%04dobs%02d.txt",
outprefix.c_str(), frame, o));
}else if(obs[o].smpl_motion){
trans = get_smpl_transformation(*obs[0].smpl_motion, time);
// std::cout << "get smpl transform finished, to save transformation" << std::endl;
// logic for saving smpl transform
save_smpl_transformation(trans, stringf("%s/%04dobs%02d.txt",
outprefix.c_str(), frame, o));
}
}
}
}
static void save_timings () {
static double old_totals[Simulation::nModules] = {};
if (!::timingfile)
return; // printing timing data to stdout is getting annoying
ostream &out = ::timingfile ? ::timingfile : cout;
for (int i = 0; i < Simulation::nModules; i++) {
out << sim.timers[i].total - old_totals[i] << " ";
old_totals[i] = sim.timers[i].total;
}
out << endl;
}
void save (const Simulation &sim, int frame) {
save(sim.cloth_meshes, frame);
// also to save the obstacles
save(sim.obstacle_meshes, frame, true);
save_obstacle_transforms(sim.obstacles, frame, sim.time);
// std::cout << "end saving obs transform" <<std::endl;
}
void sim_step() {
fps.tick();
advance_step(sim);
if (sim.step % sim.frame_steps == 0) {
save(sim, sim.frame);
save_timings();
}
fps.tock();
if (sim.time >= sim.end_time || sim.frame >= sim.end_frame)
exit(EXIT_SUCCESS);
}
void offline_loop() {
while (true)
sim_step();
}
void run_physics (const vector<string> &args) {
if (args.size() != 1 && args.size() != 2) {
cout << "Runs the simulation in batch mode." << endl;
cout << "Arguments:" << endl;
cout << " <scene-file>: JSON file describing the simulation setup"
<< endl;
cout << " <out-dir> (optional): Directory to save output in" << endl;
exit(EXIT_FAILURE);
}
string json_file = args[0];
string outprefix = args.size()>1 ? args[1] : "";
if (!outprefix.empty())
ensure_existing_directory(outprefix);
init_physics(json_file, outprefix, false);
if (!outprefix.empty())
save(sim, 0);
offline_loop();
}
void init_resume(const vector<string> &args) {
assert(args.size() == 2);
string outprefix = args[0];
string start_frame_str = args[1];
// Load like we would normally begin physics
init_physics(stringf("%s/conf.json", outprefix.c_str()), outprefix, true);
// Get the initialization information
sim.frame = atoi(start_frame_str.c_str());
sim.time = sim.frame * sim.frame_time;
sim.step = sim.frame * sim.frame_steps;
for(int i=0; i<sim.obstacles.size(); ++i)
sim.obstacles[i].get_mesh(sim.time);
load_objs(sim.cloth_meshes, stringf("%s/%04d",outprefix.c_str(),sim.frame));
prepare(sim); // re-prepare the new cloth meshes
separate_obstacles(sim.obstacle_meshes, sim.cloth_meshes);
}
void resume_physics (const vector<string> &args) {
if (args.size() != 2) {
cout << "Resumes an incomplete simulation in batch mode." << endl;
cout << "Arguments:" << endl;
cout << " <out-dir>: Directory containing simulation output files"
<< endl;
cout << " <resume-frame>: Frame number to resume from" << endl;
exit(EXIT_FAILURE);
}
init_resume(args);
offline_loop();
}
void copy_file (const string &input, const string &output) {
if(input == output) {
return;
}
if(boost::filesystem::exists(output)) {
boost::filesystem::remove(output);
}
boost::filesystem::copy_file(
input, output);
}