-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconf.cpp
executable file
·949 lines (866 loc) · 34 KB
/
conf.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
/*
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 "conf.hpp"
#include "io.hpp"
#include "magic.hpp"
#include "mot_parser.hpp"
#include "util.hpp"
#include <cassert>
#include <cfloat>
#include <fstream>
#include <json/json.h>
//#include <png.h>
#include "hylc/hylc_conf.hpp"
#include "sstream"
using namespace std;
void parse(bool &, const Json::Value &);
void parse(int &, const Json::Value &);
void parse(double &, const Json::Value &);
void parse(string &, const Json::Value &);
void complain(const Json::Value &json, const string &expected);
template <int n> void parse(Vec<n> &v, const Json::Value &json) {
if (!json.isArray())
complain(json, "array");
assert(json.size() == n);
for (int i = 0; i < n; i++)
parse(v[i], json[i]);
}
template <typename T> void parse(T &x, const Json::Value &json, const T &x0) {
if (json.isNull())
x = x0;
else
parse(x, json);
}
template <typename T> void parse(vector<T> &v, const Json::Value &json) {
if (!json.isArray())
complain(json, "array");
v.resize(json.size());
for (int i = 0; i < json.size(); i++)
parse(v[i], json[i]);
}
void parse(Cloth &, const Json::Value &);
void parse_motions(vector<Motion> &, const Json::Value &);
void parse_smpl_motions(vector<Motion> &, const Json::Value &, bool &, Eigen::VectorXf &);
void parse_handles(vector<Handle *> &, const Json::Value &,
const vector<Cloth> &, const vector<Motion> &);
void parse_handlebars(vector<Handle *> &, const Json::Value &,
const vector<Cloth> &, const vector<Motion> &);
void parse_handlespheres(vector<Handle *> &, const Json::Value &,
const vector<Cloth> &, const vector<Motion> &);
void parse_obstacles(vector<Obstacle> &, const Json::Value &,
const vector<Motion> &, bool, const Simulation &);
void parse_morphs(vector<Morph> &, const Json::Value &, const vector<Cloth> &);
void parse(Wind &, const Json::Value &);
void parse(Magic &, const Json::Value &);
void load_json(const string &configFilename, Simulation &sim) {
Json::Value json;
Json::Reader reader;
ifstream file(configFilename.c_str());
bool parsingSuccessful = reader.parse(file, json);
if (!parsingSuccessful) {
fprintf(stderr, "Error reading file: %s\n", configFilename.c_str());
fprintf(stderr, "%s", reader.getFormatedErrorMessages().c_str());
abort();
}
file.close();
// Gather general data
if (!json["frame_time"].empty()) {
parse(sim.frame_time, json["frame_time"]);
parse(sim.frame_steps, json["frame_steps"], 1);
sim.step_time = sim.frame_time / sim.frame_steps;
parse(sim.end_time, json["end_time"], infinity);
parse(sim.end_frame, json["end_frame"], infinity);
} else if (!json["timestep"].empty()) {
parse(sim.step_time, json["timestep"]);
parse(sim.frame_steps, json["save_frames"], 1);
sim.frame_time = sim.step_time * sim.frame_steps;
parse(sim.end_time, json["duration"], infinity);
sim.end_frame = infinity;
}
sim.time = 0;
hylc::parse_hylc(json["hylc"]);
parse(sim.is_smpl, json["is_smpl"], false);
parse(sim.cloths, json["cloths"]);
parse_motions(sim.motions, json["motions"]);
parse(sim.smpl_model_path, json["smpl_model_path"], std::string ());
if(sim.is_smpl)
parse_smpl_motions(sim.smpl_motions, json["motions"], sim.smpl_gender, sim.smpl_betas);
parse_handles(sim.handles, json["handles"], sim.cloths, sim.motions);
parse_handlebars(sim.handles, json["handlebars"], sim.cloths, sim.motions); // NOTE does not work with resume
parse_handlespheres(sim.handles, json["handlespheres"], sim.cloths, sim.motions); // NOTE does not work with resume
parse_obstacles(sim.obstacles, json["obstacles"], sim.is_smpl ? sim.smpl_motions : sim.motions , sim.is_smpl, sim);
std::cout << "obstacled parsed" << std::endl;
parse_morphs(sim.morphs, json["morphs"], sim.cloths);
parse(sim.gravity, json["gravity"], Vec3(0));
parse(sim.implicit_integration, json["implicit"], true);
parse(sim.explicit_massdamping, json["explicit_massdamping"], 0.0);
parse(sim.explicit_stretchdamping, json["explicit_stretchdamping"], 0.0);
parse(sim.wind, json["wind"]);
parse(sim.friction, json["friction"], 0.6);
parse(sim.obs_friction, json["obs_friction"], 0.3);
// std::cout << "obstacled parsed" << std::endl;
string module_names[] = {"proximity", "physics", "strainlimiting",
"collision", "remeshing", "separation",
"popfilter", "plasticity"};
for (int i = 0; i < Simulation::nModules; i++) {
sim.enabled[i] = true;
for (int j = 0; j < json["disable"].size(); j++)
if (json["disable"][j] == module_names[i])
sim.enabled[i] = false;
}
parse(::magic, json["magic"]);
// disable strain limiting and plasticity if not needed
bool has_strain_limits = false, has_plasticity = false;
for (int c = 0; c < sim.cloths.size(); c++)
for (int m = 0; m < sim.cloths[c].materials.size(); m++) {
Cloth::Material *mat = sim.cloths[c].materials[m];
if (finite(mat->strain_min) || finite(mat->strain_max))
has_strain_limits = true;
if (finite(mat->yield_curv))
has_plasticity = true;
}
if (!has_strain_limits)
sim.enabled[Simulation::StrainLimiting] = false;
if (!has_plasticity)
sim.enabled[Simulation::Plasticity] = false;
}
// Basic data types
void complain(const Json::Value &json, const string &expected) {
cout << "Expected " << expected << ", found " << json << " instead" << endl;
abort();
}
void parse(bool &b, const Json::Value &json) {
if (!json.isBool())
complain(json, "boolean");
b = json.asBool();
}
void parse(int &n, const Json::Value &json) {
if (!json.isIntegral())
complain(json, "integer");
n = json.asInt();
}
void parse(double &x, const Json::Value &json) {
if (!json.isNumeric())
complain(json, "real");
x = json.asDouble();
}
void parse(string &s, const Json::Value &json) {
if (!json.isString())
complain(json, "string");
s = json.asString();
}
struct Range {
double &min, &max;
Range(double &min, double &max) : min(min), max(max) {}
};
void parse(Range range, const Json::Value &json, Vec2 range0) {
if (json.isNull()) {
range.min = range0[0];
range.max = range0[1];
return;
}
assert(json.size() == 2);
parse(range.min, json[0u]);
parse(range.max, json[1]);
}
struct Box {
Vec2 umin, umax;
Box() {}
Box(const Vec2 &umin, const Vec2 &umax) : umin(umin), umax(umax) {}
};
void parse(Box &box, const Json::Value &json, const Box &box0) {
if (json.isNull()) {
box = box0;
return;
}
assert(json.size() == 2);
parse(box.umin, json[0u]);
parse(box.umax, json[1]);
}
// Cloth
void parse(Transformation &, const Json::Value &);
void parse(Cloth::Material *&, const Json::Value &);
void parse(Cloth::Remeshing &, const Json::Value &);
struct Velocity {
Vec3 v, w;
Vec3 o;
};
void parse(Velocity &, const Json::Value &);
void apply_velocity(Mesh &mesh, const Velocity &vel);
void parse(Cloth &cloth, const Json::Value &json) {
string filename;
parse(filename, json["mesh"]);
load_obj(cloth.mesh, filename);
Transformation transform;
parse(transform, json["transform"]);
if (transform.scale != 1)
for (int v = 0; v < cloth.mesh.verts.size(); v++)
cloth.mesh.verts[v]->u *= transform.scale;
compute_ms_data(cloth.mesh);
apply_transformation(cloth.mesh, transform);
Velocity velocity;
parse(velocity, json["velocity"]);
apply_velocity(cloth.mesh, velocity);
parse(cloth.materials, json["materials"]);
parse(cloth.remeshing, json["remeshing"]);
}
void parse(Transformation &transform, const Json::Value &json) {
Vec<4> rot(0);
parse(transform.translation, json["translate"], Vec3(0));
parse(transform.scale, json["scale"], 1.);
parse(rot, json["rotate"], Vec<4>(0));
transform.rotation = Quaternion::from_axisangle(Vec3(rot[1], rot[2], rot[3]),
rot[0] * M_PI / 180);
}
void parse(Velocity &velocity, const Json::Value &json) {
parse(velocity.v, json["linear"], Vec3(0));
parse(velocity.w, json["angular"], Vec3(0));
parse(velocity.o, json["origin"], Vec3(0));
}
void apply_velocity(Mesh &mesh, const Velocity &vel) {
for (int n = 0; n < mesh.nodes.size(); n++)
mesh.nodes[n]->v = vel.v + cross(vel.w, mesh.nodes[n]->x - vel.o);
}
void load_material_data(Cloth::Material &, const string &filename);
void parse(Cloth::Material *&material, const Json::Value &json) {
string filename;
parse(filename, json["data"]);
material = new Cloth::Material;
memset(material, 0, sizeof(Cloth::Material));
load_material_data(*material, filename);
double density_mult, stretching_mult, bending_mult, thicken;
parse(density_mult, json["density_mult"], 1.);
parse(stretching_mult, json["stretching_mult"], 1.);
parse(bending_mult, json["bending_mult"], 1.);
parse(thicken, json["thicken"], 1.);
density_mult *= thicken;
stretching_mult *= thicken;
bending_mult *= thicken;
material->density *= density_mult;
for (int i = 0; i < sizeof(material->stretching.s) / sizeof(Vec4); i++)
((Vec4 *)&material->stretching.s)[i] *= stretching_mult;
for (int i = 0; i < sizeof(material->bending.d) / sizeof(double); i++)
((double *)&material->bending.d)[i] *= bending_mult;
parse(material->damping, json["damping"], 0.);
parse(Range(material->strain_min, material->strain_max),
json["strain_limits"], Vec2(-infinity, infinity));
parse(material->yield_curv, json["yield_curv"], infinity);
parse(material->weakening, json["weakening"], 0.);
}
void parse(Cloth::Remeshing &remeshing, const Json::Value &json) {
parse(remeshing.refine_angle, json["refine_angle"], infinity);
parse(remeshing.refine_compression, json["refine_compression"], infinity);
parse(remeshing.refine_velocity, json["refine_velocity"], infinity);
parse(Range(remeshing.size_min, remeshing.size_max), json["size"],
Vec2(-infinity, infinity));
parse(remeshing.size_mult_boundary, json["boundary_detail_mult"], 1.0);
parse(remeshing.aspect_min, json["aspect_min"], -infinity);
}
// Other things
void parse(Motion &, const Json::Value &);
void parse_motions(vector<Motion> &motions, const Json::Value &json) {
if (json.isObject() && !json.isNull()) {
string filename;
double fps;
Transformation trans;
parse(filename, json["motfile"]);
parse(fps, json["fps"]);
parse(trans, json["transform"]);
motions = load_mot(filename, fps);
for (int m = 0; m < motions.size(); m++) {
clean_up_quaternions(motions[m]);
for (int p = 0; p < motions[m].points.size(); p++)
motions[m].points[p].x = trans * motions[m].points[p].x;
for (int p = 0; p < motions[m].points.size(); p++)
fill_in_velocity(motions[m], p);
}
} else
parse(motions, json);
}
void parse_smpl_motions(vector<Motion> &motions, const Json::Value &json, bool& smpl_gender, Eigen::VectorXf& smpl_betas){
string filename;
double fps;
// frame for interpolation
int initialization_frames = 0;
parse(filename, json["smpl_motfile"]);
parse(fps, json["smpl_fps"]);
parse(initialization_frames, json["smpl_initialization_frames"], 0);
int num_frames = 0, gender = 0;
std::ifstream fin(filename, std::ios::in);
fin >> gender >> num_frames;
std::cout << gender << " " << num_frames << std::endl;
smpl_gender = gender;
Eigen::MatrixXf betas, thetas, trans;
betas.resize(num_frames, 10);
thetas.resize(num_frames, 72);
trans.resize(num_frames, 3);
float beta;
smpl_betas.resize(10);
for(int i=0;i<10;i++){
if(fin.eof()) throw exception();
fin >> beta;
smpl_betas(i) = beta;
}
float tmp;
for(int fid=0;fid<num_frames;fid++){
for(int i=0;i<72;i++){
if(fin.eof()) throw exception();
fin >> tmp;
thetas(fid, i) = tmp;
}
for(int i=0;i<3;i++){
if(fin.eof()) throw exception();
fin >> tmp;
trans(fid, i) = tmp;
}
}
int keep_frames = 10;
// thetas now have F x 72 in axis-angle representations
// should parse thetas into one motion
// motion is Spline<transformation>
// only one part for SMPL
motions.resize(1);
// points is a structure with x and v, where x is the transformation
motions[0].points.resize(num_frames + initialization_frames + keep_frames);
// consider the interpolation process
for(int fid = 0;fid < initialization_frames;fid++){
double scale = double(fid) / double(initialization_frames);
// interpolation for poses
motions[0].points[fid].v = Transformation();
for(int pid = 0;pid < 24; pid++){
// the motion in the first valid frame
float thetax = thetas(0,3 * pid), thetay = thetas(0,3 * pid + 1), thetaz = thetas(0,3 * pid + 2);
// calculate the normalize angle and axis
float angle = sqrt(thetax * thetax + thetay * thetay + thetaz * thetaz) + 1e-8;
Vec3 axis = Vec3(thetax / angle, thetay / angle, thetaz / angle);
Quaternion q = Quaternion::from_axisangle(axis, angle);
Quaternion rest = Quaternion();
rest.s = 1.;
rest.v = Vec3(0., 0., 0.);
/*
if(pid == 0){
rest.s = 0.707106;
rest.v[0] = 0.707106;
}
*/
motions[0].points[fid].x.rotations.push_back(rest * (1 - scale) + q * scale);
// nonsense
motions[0].points[fid].x.rotation = q;
motions[0].points[fid].v.rotations.emplace_back(Quaternion());
}
// interpolation for dynamic betas
for(int i=0;i<10;i++){
// if(i == 1){
// motions[0].points[fid].x.dynamic_betas[i] = smpl_betas[i] * scale + (1 - scale);
// }else{
motions[0].points[fid].x.dynamic_betas[i] = smpl_betas[i] * scale;
// }
// if(i == 1) motions[0].points[fid].x.dynamic_betas[i] = 2;
}
motions[0].points[fid].t = (fid / fps) - 0;
}
for(int fid = initialization_frames; fid < initialization_frames + keep_frames; fid++){
motions[0].points[fid].x.translation = Vec3(trans(0, 0), trans(0, 1), trans(0, 2));
for(int pid = 0;pid < 24; pid++){
float thetax = thetas(0,3 * pid), thetay = thetas(0,3 * pid + 1), thetaz = thetas(0,3 * pid + 2);
// calculate the normalize angle and axis
float angle = sqrt(thetax * thetax + thetay * thetay + thetaz * thetaz) + 1e-8;
Vec3 axis = Vec3(thetax / angle, thetay / angle, thetaz / angle);
Quaternion q = Quaternion::from_axisangle(axis, angle);
// rotations contain 24 parts
motions[0].points[fid].x.rotations.push_back(q);
// setup the translation
// nonsense
motions[0].points[fid].x.rotation = q;
motions[0].points[fid].v.rotations.emplace_back(Quaternion());
}
motions[0].points[fid].v = Transformation();
motions[0].points[fid].x.dynamic_betas.resize(10);
for(int i=0;i<10;i++){
motions[0].points[fid].x.dynamic_betas[i] = smpl_betas[i];
}
motions[0].points[fid].t = (fid / fps) - 0; // here 0 indicates the start time
}
for (int fid = initialization_frames + keep_frames; fid < num_frames + initialization_frames + keep_frames; fid++) {
int fid2 = fid - initialization_frames - keep_frames;
motions[0].points[fid].x.translation = Vec3(trans(fid2, 0), trans(fid2, 1), trans(fid2, 2));
for(int pid = 0;pid < 24; pid++){
float thetax = thetas(fid2,3 * pid), thetay = thetas(fid2,3 * pid + 1), thetaz = thetas(fid2,3 * pid + 2);
// calculate the normalize angle and axis
float angle = sqrt(thetax * thetax + thetay * thetay + thetaz * thetaz) + 1e-8;
Vec3 axis = Vec3(thetax / angle, thetay / angle, thetaz / angle);
Quaternion q = Quaternion::from_axisangle(axis, angle);
// rotations contain 24 parts
motions[0].points[fid].x.rotations.push_back(q);
// setup the translation
// nonsense
motions[0].points[fid].x.rotation = q;
motions[0].points[fid].v.rotations.emplace_back(Quaternion());
}
motions[0].points[fid].v = Transformation();
motions[0].points[fid].x.dynamic_betas.resize(10);
for(int i=0;i<10;i++){
motions[0].points[fid].x.dynamic_betas[i] = smpl_betas[i];
}
// std::cout << "smpl dynamic betas set" << std::endl;
motions[0].points[fid].t = (fid / fps) - 0; // here 0 indicates the start time
}
// fill in the velocity
for(int fid=0;fid<num_frames+initialization_frames+keep_frames;fid++){
if(fid == 0 || fid == num_frames+initialization_frames+keep_frames-1){
motions[0].points[fid].v = motions[0].points[fid].x * 0;
}else{
motions[0].points[fid].v = (motions[0].points[fid + 1].x - motions[0].points[fid-1].x) / (motions[0].points[fid+1].t - motions[0].points[fid-1].t);
}
}
Eigen::MatrixXf theta_mat;
theta_mat.resize(24, 3);
for(int i=0;i<24;i++){
theta_mat(i, 0) = thetas(0,3 * i);
theta_mat(i, 1) = thetas(0,3 * i + 1);
theta_mat(i, 2) = thetas(0,3 * i + 2);
}
// std::cout << thetas << std::endl;
// std::cout << theta_mat << std::endl;
// exit(0);
// }
std::cout << "smpl motion parsed" << std::endl;
}
void parse(Motion::Point &, const Json::Value &);
void parse(Motion &motion, const Json::Value &json) {
parse(motion.points, json);
for (int p = 0; p < motion.points.size(); p++)
if (motion.points[p].v.scale == infinity) // no velocity specified
fill_in_velocity(motion, p);
}
void parse(Motion::Point &mp, const Json::Value &json) {
parse(mp.t, json["time"]);
parse(mp.x, json["transform"]);
if (json["velocity"].isNull())
mp.v.scale = infinity; // raise a flag
else {
parse(mp.v, json["velocity"]);
if (mp.v.scale == 1)
mp.v.scale = 0;
if (mp.v.rotation.s == 1) {
mp.v.rotation.s = 0;
mp.v.rotation.v = Vec3(0);
}
}
}
void parse_handle(vector<Handle *> &, const Json::Value &,
const vector<Cloth> &, const vector<Motion> &);
void parse_handlebar(vector<Handle *> &, const Json::Value &,
const vector<Cloth> &, const vector<Motion> &,
const Vec2 &umin, const Vec2 &umax);
void parse_handlesphere(vector<Handle *> &, const Json::Value &,
const vector<Cloth> &, const vector<Motion> &);
void parse_handles(vector<Handle *> &hans, const Json::Value &jsons,
const vector<Cloth> &cloths, const vector<Motion> &motions) {
for (int j = 0; j < jsons.size(); j++)
parse_handle(hans, jsons[j], cloths, motions);
}
void parse_handlebars(vector<Handle *> &hans, const Json::Value &jsons,
const vector<Cloth> &cloths,
const vector<Motion> &motions) {
// get bounds in material space
Vec2 umin, umax;
bool first = true;
for (const auto &cloth : cloths) {
const Mesh &mesh = cloth.mesh;
for (int i = 0; i < (int)mesh.verts.size(); i++) {
Vec2 &u = mesh.verts[i]->u;
if (first) {
umin = u;
umax = u;
first = false;
} else {
for (int k = 0; k < 2; ++k) {
umin(k) = min(u(k), umin(k));
umax(k) = max(u(k), umax(k));
}
}
}
}
for (int j = 0; j < jsons.size(); j++)
parse_handlebar(hans, jsons[j], cloths, motions, umin, umax);
}
void parse_handlespheres(vector<Handle *> &hans, const Json::Value &jsons,
const vector<Cloth> &cloths,
const vector<Motion> &motions) {
for (int j = 0; j < jsons.size(); j++)
parse_handlesphere(hans, jsons[j], cloths, motions);
}
void parse_node_handle(vector<Handle *> &hans, const Json::Value &json,
const vector<Cloth> &cloths,
const vector<Motion> &motions);
void parse_circle_handle(vector<Handle *> &hans, const Json::Value &json,
const vector<Cloth> &cloths,
const vector<Motion> &motions);
void parse_glue_handle(vector<Handle *> &hans, const Json::Value &json,
const vector<Cloth> &cloths,
const vector<Motion> &motions);
void parse_handle(vector<Handle *> &hans, const Json::Value &json,
const vector<Cloth> &cloths, const vector<Motion> &motions) {
string type;
parse(type, json["type"], string("node"));
int nhans = hans.size();
if (type == "node")
parse_node_handle(hans, json, cloths, motions);
else if (type == "circle")
parse_circle_handle(hans, json, cloths, motions);
else if (type == "glue")
parse_glue_handle(hans, json, cloths, motions);
else {
cout << "Unknown handle type " << type << endl;
abort();
}
double start_time, end_time, fade_time;
parse(start_time, json["start_time"], 0.);
parse(end_time, json["end_time"], infinity);
parse(fade_time, json["fade_time"], 0.);
for (int h = nhans; h < hans.size(); h++) {
hans[h]->start_time = start_time;
hans[h]->end_time = end_time;
hans[h]->fade_time = fade_time;
}
}
void parse_handlebar(vector<Handle *> &hans, const Json::Value &json,
const vector<Cloth> &cloths, const vector<Motion> &motions,
const Vec2 &umin, const Vec2 &umax) {
int side;
parse(side, json["side"], 0); // 0-left, 1-bottom, 2-right, 3-top
int nhans = hans.size();
double widthp,lengthp,insetp;
parse(widthp,json["widthp"],0.0); // % of other side have as size
parse(lengthp,json["lengthp"],1.0); // % of side to cover
parse(insetp,json["insetp"],0.0); // % of other side to inset
double offset = 0.5 * (1.0 - lengthp); // how much to offset start and stop
// widthp = max(widthp, 1e-5); // min size to numerically cover at least a line
Vec2 extents = umax - umin;
Vec2 from, to;
if (side == 0) { // left
from(0) = umin(0) + insetp * extents(0) - 0.5 * widthp * extents(0);
from(1) = umin(1) + offset * extents(1);
to(0) = umin(0) + insetp * extents(0) + 0.5 * widthp * extents(0);
to(1) = umax(1) - offset * extents(1);
} else if (side == 1) { // bottom
from(0) = umin(0) + offset * extents(0);
from(1) = umin(1) + insetp * extents(1) - 0.5 * widthp * extents(1);
to(0) = umax(0) - offset * extents(0);
to(1) = umin(1) + insetp * extents(1) + 0.5 * widthp * extents(1);
} else if (side == 2) { // right
from(0) = umax(0) - insetp * extents(0) - 0.5 * widthp * extents(0);
from(1) = umin(1) + offset * extents(1);
to(0) = umax(0) - insetp * extents(0) + 0.5 * widthp * extents(0);
to(1) = umax(1) - offset * extents(1);
} else { // top
from(0) = umin(0) + offset * extents(0);
from(1) = umax(1) - insetp * extents(1) - 0.5 * widthp * extents(1);
to(0) = umax(0) - offset * extents(0);
to(1) = umax(1) - insetp * extents(1) + 0.5 * widthp * extents(1);
}
// fatten numerically
double w = 1e-5;
from(0) -= w;
from(1) -= w;
to(0) += w;
to(1) += w;
// depending on side get list of vertex indices for that side
// then make a modified json that instead of the side info
// has a nodes info with the respective indices
Json::Value cpy(json); // copy with motion info etc.
// remove side and replace with nodes
cpy.removeMember("side");
Json::Value &cpynodes = cpy["nodes"];
// add respective indices to nodes
for (const auto &cloth : cloths) {
const Mesh &mesh = cloth.mesh;
for (int i = 0; i < (int)mesh.verts.size(); i++) { // maybe nodes instead?
Vec2 &u = mesh.verts[i]->u;
if (u(0) > from(0) && u(0) < to(0) && u(1) > from(1) && u(1) < to(1))
cpynodes.append(mesh.verts[i]->node->index);
}
}
// redirect to default node handle parsing with altered json
parse_node_handle(hans, cpy, cloths, motions);
double start_time, end_time, fade_time;
parse(start_time, json["start_time"], 0.);
parse(end_time, json["end_time"], infinity);
parse(fade_time, json["fade_time"], 0.);
for (int h = nhans; h < hans.size(); h++) {
hans[h]->start_time = start_time;
hans[h]->end_time = end_time;
hans[h]->fade_time = fade_time;
}
}
void parse_handlesphere(vector<Handle *> &hans, const Json::Value &json,
const vector<Cloth> &cloths, const vector<Motion> &motions) {
int nhans = hans.size();
Vec3 loc(0);
double radius;
parse(loc, json["center"]);
parse(radius, json["radius"]);
Json::Value cpy(json); // copy with motion info etc.
// remove side and replace with nodes
cpy.removeMember("center");
cpy.removeMember("radius");
Json::Value &cpynodes = cpy["nodes"];
// add respective indices to nodes
for (const auto &cloth : cloths) {
const Mesh &mesh = cloth.mesh;
for (int i = 0; i < (int)mesh.verts.size(); i++) { // maybe nodes instead?
Vec3 &x = mesh.verts[i]->node->x;
if (norm2(x-loc) <= radius*radius)
cpynodes.append(mesh.verts[i]->node->index);
}
}
// redirect to default node handle parsing with altered json
parse_node_handle(hans, cpy, cloths, motions);
double start_time, end_time, fade_time;
parse(start_time, json["start_time"], 0.);
parse(end_time, json["end_time"], infinity);
parse(fade_time, json["fade_time"], 0.);
for (int h = nhans; h < hans.size(); h++) {
hans[h]->start_time = start_time;
hans[h]->end_time = end_time;
hans[h]->fade_time = fade_time;
}
}
void parse_node_handle(vector<Handle *> &hans, const Json::Value &json,
const vector<Cloth> &cloths,
const vector<Motion> &motions) {
int c, l, m;
vector<int> ns;
parse(c, json["cloth"], 0);
parse(l, json["label"], -1);
if (l == -1)
parse(ns, json["nodes"]);
parse(m, json["motion"], -1);
const Mesh &mesh = cloths[c].mesh;
const Motion *motion = (m != -1) ? &motions[m] : NULL;
if (l != -1) {
for (int n = 0; n < mesh.nodes.size(); n++) {
if (mesh.nodes[n]->label != l)
continue;
NodeHandle *han = new NodeHandle;
han->node = mesh.nodes[n];
han->node->preserve = true;
han->motion = motion;
hans.push_back(han);
}
}
if (!ns.empty()) {
for (int i = 0; i < ns.size(); i++) {
NodeHandle *han = new NodeHandle;
han->node = mesh.nodes[ns[i]];
han->node->preserve = true;
han->motion = motion;
hans.push_back(han);
}
}
}
void parse_circle_handle(vector<Handle *> &hans, const Json::Value &json,
const vector<Cloth> &cloths,
const vector<Motion> &motions) {
CircleHandle *han = new CircleHandle;
int c, m;
parse(c, json["cloth"], 0);
han->mesh = (Mesh *)&cloths[c].mesh;
parse(han->label, json["label"]);
parse(m, json["motion"], -1);
han->motion = (m != -1) ? &motions[m] : NULL;
parse(han->c, json["circumference"]);
parse(han->u, json["u"]);
parse(han->xc, json["center"]);
parse(han->dx0, json["axis0"]);
parse(han->dx1, json["axis1"]);
hans.push_back(han);
}
void parse_glue_handle(vector<Handle *> &hans, const Json::Value &json,
const vector<Cloth> &cloths,
const vector<Motion> &motions) {
GlueHandle *han = new GlueHandle;
int c;
vector<int> ns;
parse(c, json["cloth"], 0);
parse(ns, json["nodes"]);
if (ns.size() != 2) {
cout << "Must glue exactly two nodes together" << endl;
abort();
}
const Mesh &mesh = cloths[c].mesh;
han->nodes[0] = (Node *)mesh.nodes[ns[0]];
han->nodes[1] = (Node *)mesh.nodes[ns[1]];
hans.push_back(han);
}
void parse_obstacle(Obstacle &, const Json::Value &, const vector<Motion> &);
void parse_obstacles(vector<Obstacle> &obstacles, const Json::Value &json,
const vector<Motion> &motions, bool is_smpl, const Simulation& sim) {
if (json.isString()) {
string fmt;
parse(fmt, json);
string last_filename;
for (int i = 0; true; i++) {
string filename = stringf(fmt, i);
if (!fstream(filename.c_str(), ios::in) or (!last_filename.empty() and filename == last_filename))
break;
std::cout << filename << std::endl;
Obstacle obs;
load_obj(obs.base_mesh, filename);
if(!is_smpl) {
obs.transform_spline = (i < motions.size()) ? &motions[i] : NULL;
obs.smpl_motion = NULL;
obs.start_time = 0;
obs.end_time = infinity;
obs.get_mesh(0);
}else{
obs.smpl_motion = (i < motions.size()) ? &motions[i] : NULL;
obs.transform_spline = NULL;
obs.gender = sim.smpl_gender;
obs.betas = sim.smpl_betas;
// create the smpl model
obs.smpl = new SMPL();
// std::cout << "smpl model loaded:" << sim.smpl_model_path << std::endl;
obs.smpl->loadModelFromJSONFile(sim.smpl_model_path);
// exit(0);
obs.get_smpl_mesh(0);
}
obstacles.push_back(obs);
last_filename = filename;
}
} else {
obstacles.resize(json.size());
for (int j = 0; j < json.size(); j++)
parse_obstacle(obstacles[j], json[j], motions);
}
}
void parse_obstacle(Obstacle &obstacle, const Json::Value &json,
const vector<Motion> &motions) {
string filename;
parse(filename, json["mesh"]);
load_obj(obstacle.base_mesh, filename);
Transformation transform;
parse(transform, json["transform"]);
apply_transformation(obstacle.base_mesh, transform);
int m;
parse(m, json["motion"], -1);
obstacle.transform_spline = (m != -1) ? &motions[m] : NULL;
parse(obstacle.start_time, json["start_time"], 0.);
parse(obstacle.end_time, json["end_time"], infinity);
obstacle.get_mesh(0);
}
void parse_morph(Morph &, const Json::Value &, const vector<Cloth> &);
void parse(Spline<Morph::Weights>::Point &, const Json::Value &);
void parse_morphs(vector<Morph> &morphs, const Json::Value &json,
const vector<Cloth> &cloths) {
morphs.resize(json.size());
for (int j = 0; j < json.size(); j++)
parse_morph(morphs[j], json[j], cloths);
}
void parse_morph(Morph &morph, const Json::Value &json,
const vector<Cloth> &cloths) {
int c;
parse(c, json["cloth"], 0);
morph.mesh = (Mesh *)&cloths[c].mesh;
morph.targets.resize(json["targets"].size());
for (int j = 0; j < json["targets"].size(); j++) {
string filename;
parse(filename, json["targets"][j]);
load_obj(morph.targets[j], filename);
}
int nk = json["spline"].size();
morph.weights.points.resize(nk);
morph.log_stiffness.points.resize(nk);
for (int k = 0; k < nk; k++) {
const Json::Value &j = json["spline"][k];
double t;
parse(t, j["time"]);
morph.weights.points[k].t = morph.log_stiffness.points[k].t = t;
int m;
parse(m, j["target"]);
morph.weights.points[k].x.assign(morph.targets.size(), 0);
morph.weights.points[k].x[m] = 1;
double s;
parse(s, j["stiffness"]);
morph.log_stiffness.points[k].x = log(s);
}
for (int k = 0; k < nk; k++) {
fill_in_velocity(morph.weights, k);
fill_in_velocity(morph.log_stiffness, k);
}
}
void parse(Wind &wind, const Json::Value &json) {
parse(wind.density, json["density"], 1.);
parse(wind.velocity, json["velocity"], Vec3(0));
parse(wind.drag, json["drag"], 0.);
}
void parse(Magic &magic, const Json::Value &json) {
#define PARSE_MAGIC(param) parse(magic.param, json[#param], magic.param)
PARSE_MAGIC(fixed_high_res_mesh);
PARSE_MAGIC(handle_stiffness);
PARSE_MAGIC(collision_stiffness);
PARSE_MAGIC(repulsion_thickness);
parse(magic.projection_thickness, json["projection_thickness"],
0.1 * magic.repulsion_thickness);
PARSE_MAGIC(edge_flip_threshold);
PARSE_MAGIC(rib_stiffening);
PARSE_MAGIC(combine_tensors);
PARSE_MAGIC(preserve_creases);
PARSE_MAGIC(relax_initial_state);
PARSE_MAGIC(display_scale);
#undef PARSE_MAGIC
}
// JSON materials
void parse(StretchingSamples &, const Json::Value &);
void parse(BendingData &, const Json::Value &);
void load_material_data(Cloth::Material &material, const string &filename) {
Json::Value json;
Json::Reader reader;
ifstream file(filename.c_str());
bool parsingSuccessful = reader.parse(file, json);
if (!parsingSuccessful) {
fprintf(stderr, "Error reading file: %s\n", filename.c_str());
fprintf(stderr, "%s", reader.getFormatedErrorMessages().c_str());
abort();
}
file.close();
parse(material.density, json["density"]);
parse(material.stretching, json["stretching"]);
parse(material.bending, json["bending"]);
}
void parse(StretchingSamples &samples, const Json::Value &json) {
StretchingData data;
parse(data.d[0][0], json[0u]);
for (int i = 1; i < 5; i++)
data.d[0][i] = data.d[0][0];
for (int i = 0; i < 5; i++)
parse(data.d[1][i], json[i + 1]);
evaluate_stretching_samples(samples, data);
}
void parse(BendingData &data, const Json::Value &json) {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 5; j++)
parse(data.d[i][j], json[i][j]);
}