From: Francois Fleuret Date: Wed, 1 Mar 2017 08:28:50 +0000 (+0100) Subject: Seems to work quite well. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=2cd32038873961c8ff3861efb218fad75fbcbf69;p=flatland.git Seems to work quite well. --- diff --git a/flatland.cc b/flatland.cc index e41e565..abfc644 100644 --- a/flatland.cc +++ b/flatland.cc @@ -222,7 +222,7 @@ extern "C" void fl_generate_sequences(int nb_sequences, scalar_t xf = grabbed_polygon->absolute_x(grab_relative_x, grab_relative_y); scalar_t yf = grabbed_polygon->absolute_y(grab_relative_x, grab_relative_y); grabbed_polygon->apply_force(dt, xf, yf, 0.0, -1.0); - universe->update(dt); + universe->update(dt, 1.0 / scaling); } } } @@ -238,17 +238,6 @@ extern "C" void fl_generate_sequences(int nb_sequences, } } } - - /* - { - cout << canvases[0]->_actual_width << " "<< canvases[1]->_actual_height << endl; - cout << "Writing /tmp/sanity.png" << endl; - CanvasCairo main_canvas(scaling, nb_images_per_sequence, 1, canvases); - FILE *file = safe_fopen("/tmp/sanity.png", "w"); - main_canvas.write_png(file); - fclose(file); - } - */ } for(int t = 0; t < 2 * nb_saved_frames; t++) { diff --git a/mylib.c b/mylib.c index 61a461d..6d62c24 100644 --- a/mylib.c +++ b/mylib.c @@ -19,8 +19,7 @@ #include "flatland.h" -int generate_sequence(THByteTensor *output) { - long nb_sequences = 1; +int generate_sequence(long nb_sequences, THByteTensor *output) { long nb_images_per_sequence = 5; long depth = 3; long width = 64; diff --git a/mylib.h b/mylib.h index 9c9208b..a0a255f 100644 --- a/mylib.h +++ b/mylib.h @@ -1,2 +1,2 @@ -int generate_sequence(THByteTensor *output); +int generate_sequence(long nb_sequences, THByteTensor *output); diff --git a/polygon.cc b/polygon.cc index 69b125e..b64fbe4 100644 --- a/polygon.cc +++ b/polygon.cc @@ -356,7 +356,9 @@ void Polygon::apply_force(scalar_t dt, scalar_t x, scalar_t y, scalar_t fx, scal _dtheta -= prod_vect(x - _center_x, y - _center_y, fx, fy) / (_mass * _moment_of_inertia) * dt; } -void Polygon::apply_border_forces(scalar_t dt, scalar_t xmax, scalar_t ymax) { +void Polygon::apply_border_forces(scalar_t dt, + scalar_t xmin, scalar_t ymin, + scalar_t xmax, scalar_t ymax) { for(int v = 0; v < _nb_vertices; v++) { int vp = (v+1)%_nb_vertices; for(int d = 0; d < _nb_dots[v]; d++) { @@ -364,9 +366,9 @@ void Polygon::apply_border_forces(scalar_t dt, scalar_t xmax, scalar_t ymax) { scalar_t x = _x[v] * (1 - s) + _x[vp] * s; scalar_t y = _y[v] * (1 - s) + _y[vp] * s; scalar_t vx = 0, vy = 0; - if(x < 0) vx = x; + if(x < xmin) vx = xmin - x; else if(x > xmax) vx = x - xmax; - if(y < 0) vy = y; + if(y < ymin) vy = ymin - y; else if(y > ymax) vy = y - ymax; apply_force(dt, x, y, - dl * vx * repulsion_constant, - dl * vy * repulsion_constant); } @@ -390,9 +392,10 @@ void Polygon::apply_collision_forces(scalar_t dt, int n_polygon, Polygon *p) { distance[d] = FLT_MAX; } - // First, we tag the dots located inside the polygon p + // First, we tag the dots located inside the polygon p by looping + // through the _nb_vertices - 2 triangles from the decomposition - for(int t = 0; t < p->_nb_vertices-2; t++) { + for(int t = 0; t < p->_nb_vertices - 2; t++) { scalar_t min = 0, max = 1; scalar_t xa = p->_x[p->_triangles[t].a], ya = p->_y[p->_triangles[t].a]; scalar_t xb = p->_x[p->_triangles[t].b], yb = p->_y[p->_triangles[t].b]; diff --git a/polygon.h b/polygon.h index 8e23a70..557fdd6 100644 --- a/polygon.h +++ b/polygon.h @@ -119,7 +119,7 @@ public: scalar_t absolute_y(scalar_t rx, scalar_t ry); void apply_force(scalar_t dt, scalar_t x, scalar_t y, scalar_t fx, scalar_t fy); - void apply_border_forces(scalar_t dt, scalar_t xmax, scalar_t ymax); + void apply_border_forces(scalar_t dt, scalar_t xmin, scalar_t ymin, scalar_t xmax, scalar_t ymax); void apply_collision_forces(scalar_t dt, int n_polygon, Polygon *p); bool collide(Polygon *p); diff --git a/test.py b/test.py index d6d2df1..de408aa 100755 --- a/test.py +++ b/test.py @@ -10,10 +10,11 @@ x = torch.ByteTensor(4, 5).fill_(0) print(x.size()) -mylib.generate_sequence(x) +mylib.generate_sequence(8, x) print(x.size()) x = x.float().sub_(128).div_(128) -torchvision.utils.save_image(x[0], 'example.png') +for s in range(0, x.size(0)): + torchvision.utils.save_image(x[s], 'example_' + str(s) + '.png') diff --git a/universe.cc b/universe.cc index 2b1383d..b897d0c 100644 --- a/universe.cc +++ b/universe.cc @@ -122,11 +122,13 @@ void Universe::compute_pseudo_collisions(int nb_axis, int *nb_colliding_axis) { } } -bool Universe::update(scalar_t dt) { +bool Universe::update(scalar_t dt, scalar_t padding) { bool result = false; apply_collision_forces(dt); for(int n = 0; n < _nb_polygons; n++) if(_polygons[n]) { - _polygons[n]->apply_border_forces(dt, _width, _height); + _polygons[n]->apply_border_forces(dt, + padding, padding, + _width - padding, _height - padding); result |= _polygons[n]->update(dt); } return result; diff --git a/universe.h b/universe.h index 11dacc7..c80bf2c 100644 --- a/universe.h +++ b/universe.h @@ -56,7 +56,7 @@ public: // axis to speed up the computation void compute_pseudo_collisions(int nb_axis, int *nb_colliding_axis); void apply_collision_forces(scalar_t dt); - bool update(scalar_t dt); + bool update(scalar_t dt, scalar_t padding); Polygon *pick_polygon(scalar_t x, scalar_t y);