+
+/*
+ * dyncnn is a deep-learning algorithm for the prediction of
+ * interacting object dynamics
+ *
+ * Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
+ * Written by Francois Fleuret <francois.fleuret@idiap.ch>
+ *
+ * This file is part of dyncnn.
+ *
+ * dyncnn is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * dyncnn is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with dyncnn. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <iostream>
+
+using namespace std;
+
+#include <cmath>
+#include "polygon.h"
+
+static const scalar_t dl = 20.0;
+static const scalar_t repulsion_constant = 0.2;
+static const scalar_t dissipation = 0.5;
+
+Polygon::Polygon(scalar_t mass,
+ scalar_t red, scalar_t green, scalar_t blue,
+ scalar_t *x, scalar_t *y,
+ int nv) : _mass(mass),
+ _moment_of_inertia(0), _radius(0),
+ _relative_x(new scalar_t[nv]), _relative_y(new scalar_t[nv]),
+ _total_nb_dots(0),
+ _nb_dots(new int[nv]),
+ _effecting_edge(0),
+ _length(new scalar_t[nv]),
+ _triangles(new Triangle[nv-2]),
+ _initialized(false), _nailed(false),
+ _nb_vertices(nv),
+ _x(new scalar_t[nv]), _y(new scalar_t[nv]),
+ _red(red), _green(green), _blue(blue) {
+ _last_center_x = 0;
+ _last_center_y = 0;
+ _last_theta = 0;
+
+ if(x) for(int i = 0; i < nv; i++) _relative_x[i] = x[i];
+ if(y) for(int i = 0; i < nv; i++) _relative_y[i] = y[i];
+}
+
+Polygon::~Polygon() {
+ delete[] _relative_x;
+ delete[] _relative_y;
+ delete[] _x;
+ delete[] _y;
+ delete[] _nb_dots;
+ delete[] _length;
+ delete[] _triangles;
+ delete[] _effecting_edge;
+}
+
+Polygon *Polygon::clone() {
+ return new Polygon(_mass, _red, _green, _blue, _relative_x, _relative_y, _nb_vertices);
+}
+
+#ifdef XFIG_SUPPORT
+void Polygon::color_xfig(XFigTracer *tracer) {
+ tracer->add_color(int(255 * _red), int(255 * _green), int(255 * _blue));
+}
+
+void Polygon::print_xfig(XFigTracer *tracer) {
+ tracer->draw_polygon(int(255 * _red), int(255 * _green), int(255 * _blue),
+ _nb_vertices, _x, _y);
+}
+#endif
+
+#ifdef X11_SUPPORT
+void Polygon::draw(SimpleWindow *window) {
+ window->color(_red, _green, _blue);
+ int x[_nb_vertices], y[_nb_vertices];
+ for(int n = 0; n < _nb_vertices; n++) {
+ x[n] = int(_x[n]);
+ y[n] = int(_y[n]);
+ }
+ window->fill_polygon(_nb_vertices, x, y);
+}
+
+void Polygon::draw_contours(SimpleWindow *window) {
+ int x[_nb_vertices], y[_nb_vertices];
+ for(int n = 0; n < _nb_vertices; n++) {
+ x[n] = int(_x[n]);
+ y[n] = int(_y[n]);
+ }
+ window->color(0.0, 0.0, 0.0);
+ // window->color(1.0, 1.0, 1.0);
+ for(int n = 0; n < _nb_vertices; n++) {
+ window->draw_line(x[n], y[n], x[(n+1)%_nb_vertices], y[(n+1)%_nb_vertices]);
+ }
+}
+#endif
+
+void Polygon::draw(Canvas *canvas) {
+ canvas->set_drawing_color(_red, _green, _blue);
+ canvas->draw_polygon(1, _nb_vertices, _x, _y);
+}
+
+void Polygon::draw_contours(Canvas *canvas) {
+ canvas->set_drawing_color(0.0, 0.0, 0.0);
+ canvas->draw_polygon(0, _nb_vertices, _x, _y);
+}
+
+void Polygon::set_vertex(int k, scalar_t x, scalar_t y) {
+ _relative_x[k] = x;
+ _relative_y[k] = y;
+}
+
+void Polygon::set_position(scalar_t center_x, scalar_t center_y, scalar_t theta) {
+ _center_x = center_x;
+ _center_y = center_y;
+ _theta = theta;
+}
+
+void Polygon::set_speed(scalar_t dcenter_x, scalar_t dcenter_y, scalar_t dtheta) {
+ _dcenter_x = dcenter_x;
+ _dcenter_y = dcenter_y;
+ _dtheta = dtheta;
+}
+
+bool Polygon::contain(scalar_t x, scalar_t y) {
+ for(int t = 0; t < _nb_vertices-2; t++) {
+ scalar_t xa = _x[_triangles[t].a], ya = _y[_triangles[t].a];
+ scalar_t xb = _x[_triangles[t].b], yb = _y[_triangles[t].b];
+ scalar_t xc = _x[_triangles[t].c], yc = _y[_triangles[t].c];
+ if(prod_vect(x - xa, y - ya, xb - xa, yb - ya) <= 0 &&
+ prod_vect(x - xb, y - yb, xc - xb, yc - yb) <= 0 &&
+ prod_vect(x - xc, y - yc, xa - xc, ya - yc) <= 0) return true;
+ }
+ return false;
+}
+
+void Polygon::triangularize(int &nt, int nb, int *index) {
+ if(nb == 3) {
+
+ if(nt >= _nb_vertices-2) {
+ cerr << "Error type #1 in triangularization." << endl;
+ exit(1);
+ }
+
+ _triangles[nt].a = index[0];
+ _triangles[nt].b = index[1];
+ _triangles[nt].c = index[2];
+ nt++;
+
+ } else {
+ int best_m = -1, best_n = -1;
+ scalar_t best_split = -1, det, s = -1, t = -1;
+
+ for(int n = 0; n < nb; n++) for(int m = 0; m < n; m++) if(n > m+1 && m+nb > n+1) {
+ bool no_intersection = true;
+ for(int k = 0; no_intersection & (k < nb); k++)
+ if(k != n && k != m && (k+1)%nb != n && (k+1)%nb != m) {
+ intersection(_relative_x[index[n]], _relative_y[index[n]],
+ _relative_x[index[m]], _relative_y[index[m]],
+ _relative_x[index[k]], _relative_y[index[k]],
+ _relative_x[index[(k+1)%nb]], _relative_y[index[(k+1)%nb]], det, s, t);
+ no_intersection = det == 0 || s < 0 || s > 1 || t < 0 || t > 1;
+ }
+
+ if(no_intersection) {
+ scalar_t a1 = 0, a2 = 0;
+ for(int k = 0; k < nb; k++) if(k >= m && k < n)
+ a1 += prod_vect(_relative_x[index[k]] - _relative_x[index[m]],
+ _relative_y[index[k]] - _relative_y[index[m]],
+ _relative_x[index[k+1]] - _relative_x[index[m]],
+ _relative_y[index[k+1]] - _relative_y[index[m]]);
+ else
+ a2 += prod_vect(_relative_x[index[k]] - _relative_x[index[m]],
+ _relative_y[index[k]] - _relative_y[index[m]],
+ _relative_x[index[(k+1)%nb]] - _relative_x[index[m]],
+ _relative_y[index[(k+1)%nb]] - _relative_y[index[m]]);
+
+ if((a1 * a2 > 0 && best_split < 0) || (abs(a1 - a2) < best_split)) {
+ best_n = n; best_m = m;
+ best_split = abs(a1 - a2);
+ }
+ }
+ }
+
+ if(best_n >= 0 && best_m >= 0) {
+ int index_neg[nb], index_pos[nb];
+ int neg = 0, pos = 0;
+ for(int k = 0; k < nb; k++) {
+ if(k >= best_m && k <= best_n) index_pos[pos++] = index[k];
+ if(k <= best_m || k >= best_n) index_neg[neg++] = index[k];
+ }
+ if(pos < 3 || neg < 3) {
+ cerr << "Error type #2 in triangularization." << endl;
+ exit(1);
+ }
+ triangularize(nt, pos, index_pos);
+ triangularize(nt, neg, index_neg);
+ } else {
+ cerr << "Error type #3 in triangularization." << endl;
+ exit(1);
+ }
+ }
+}
+
+void Polygon::initialize(int nb_polygons) {
+ scalar_t a;
+
+ _nb_polygons = nb_polygons;
+
+ a = _relative_x[_nb_vertices - 1] * _relative_y[0]
+ - _relative_x[0] * _relative_y[_nb_vertices - 1];
+
+ for(int n = 0; n < _nb_vertices - 1; n++)
+ a += _relative_x[n] * _relative_y[n+1] - _relative_x[n+1] * _relative_y[n];
+ a *= 0.5;
+
+ // Reorder the vertices
+
+ if(a < 0) {
+ a = -a;
+ scalar_t x, y;
+ for(int n = 0; n < _nb_vertices / 2; n++) {
+ x = _relative_x[n];
+ y = _relative_y[n];
+ _relative_x[n] = _relative_x[_nb_vertices - 1 - n];
+ _relative_y[n] = _relative_y[_nb_vertices - 1 - n];
+ _relative_x[_nb_vertices - 1 - n] = x;
+ _relative_y[_nb_vertices - 1 - n] = y;
+ }
+ }
+
+ // Compute the center of mass and moment of inertia
+
+ scalar_t sx, sy, w;
+ w = 0;
+ sx = 0;
+ sy = 0;
+ for(int n = 0; n < _nb_vertices; n++) {
+ int np = (n+1)%_nb_vertices;
+ w =_relative_x[n] * _relative_y[np] - _relative_x[np] * _relative_y[n];
+ sx += (_relative_x[n] + _relative_x[np]) * w;
+ sy += (_relative_y[n] + _relative_y[np]) * w;
+ }
+ sx /= 6 * a;
+ sy /= 6 * a;
+
+ _radius = 0;
+ for(int n = 0; n < _nb_vertices; n++) {
+ _relative_x[n] -= sx;
+ _relative_y[n] -= sy;
+ scalar_t r = sqrt(sq(_relative_x[n]) + sq(_relative_y[n]));
+ if(r > _radius) _radius = r;
+ }
+
+ scalar_t num = 0, den = 0;
+ for(int n = 0; n < _nb_vertices; n++) {
+ int np = (n+1)%_nb_vertices;
+ den += abs(prod_vect(_relative_x[np], _relative_y[np], _relative_x[n], _relative_y[n]));
+ num += abs(prod_vect(_relative_x[np], _relative_y[np], _relative_x[n], _relative_y[n])) *
+ (_relative_x[np] * _relative_x[np] + _relative_y[np] * _relative_y[np] +
+ _relative_x[np] * _relative_x[n] + _relative_y[np] * _relative_y[n] +
+ _relative_x[n] * _relative_x[n] + _relative_y[n] * _relative_y[n]);
+ }
+
+ _moment_of_inertia = num / (6 * den);
+
+ scalar_t vx = cos(_theta), vy = sin(_theta);
+
+ for(int n = 0; n < _nb_vertices; n++) {
+ _x[n] = _center_x + _relative_x[n] * vx + _relative_y[n] * vy;
+ _y[n] = _center_y - _relative_x[n] * vy + _relative_y[n] * vx;
+ }
+
+ scalar_t length;
+ _total_nb_dots = 0;
+ for(int n = 0; n < _nb_vertices; n++) {
+ length = sqrt(sq(_relative_x[n] - _relative_x[(n+1)%_nb_vertices]) +
+ sq(_relative_y[n] - _relative_y[(n+1)%_nb_vertices]));
+ _length[n] = length;
+ _nb_dots[n] = int(length / dl + 1);
+ _total_nb_dots += _nb_dots[n];
+ }
+
+ delete[] _effecting_edge;
+ _effecting_edge = new int[_nb_polygons * _total_nb_dots];
+ for(int p = 0; p < _nb_polygons * _total_nb_dots; p++) _effecting_edge[p] = -1;
+
+ int nt = 0;
+ int index[_nb_vertices];
+ for(int n = 0; n < _nb_vertices; n++) index[n] = n;
+ triangularize(nt, _nb_vertices, index);
+
+ _initialized = true;
+}
+
+bool Polygon::update(scalar_t dt) {
+ if(!_nailed) {
+ _center_x += _dcenter_x * dt;
+ _center_y += _dcenter_y * dt;
+ _theta += _dtheta * dt;
+ }
+
+ scalar_t d = exp(log(dissipation) * dt);
+ _dcenter_x *= d;
+ _dcenter_y *= d;
+ _dtheta *= d;
+
+ scalar_t vx = cos(_theta), vy = sin(_theta);
+
+ for(int n = 0; n < _nb_vertices; n++) {
+ _x[n] = _center_x + _relative_x[n] * vx + _relative_y[n] * vy;
+ _y[n] = _center_y - _relative_x[n] * vy + _relative_y[n] * vx;
+ }
+
+ if(abs(_center_x - _last_center_x) +
+ abs(_center_y - _last_center_y) +
+ abs(_theta - _last_theta) * _radius > 0.1) {
+ _last_center_x = _center_x;
+ _last_center_y = _center_y;
+ _last_theta = _theta;
+ return true;
+ } else return false;
+}
+
+scalar_t Polygon::relative_x(scalar_t ax, scalar_t ay) {
+ return (ax - _center_x) * cos(_theta) - (ay - _center_y) * sin(_theta);
+}
+
+scalar_t Polygon::relative_y(scalar_t ax, scalar_t ay) {
+ return (ax - _center_x) * sin(_theta) + (ay - _center_y) * cos(_theta);
+}
+
+scalar_t Polygon::absolute_x(scalar_t rx, scalar_t ry) {
+ return _center_x + rx * cos(_theta) + ry * sin(_theta);
+}
+
+scalar_t Polygon::absolute_y(scalar_t rx, scalar_t ry) {
+ return _center_y - rx * sin(_theta) + ry * cos(_theta);
+}
+
+void Polygon::apply_force(scalar_t dt, scalar_t x, scalar_t y, scalar_t fx, scalar_t fy) {
+ _dcenter_x += fx / _mass * dt;
+ _dcenter_y += fy / _mass * dt;
+ _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) {
+ for(int v = 0; v < _nb_vertices; v++) {
+ int vp = (v+1)%_nb_vertices;
+ for(int d = 0; d < _nb_dots[v]; d++) {
+ scalar_t s = scalar_t(d * dl)/_length[v];
+ 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;
+ else if(x > xmax) vx = x - xmax;
+ if(y < 0) vy = y;
+ else if(y > ymax) vy = y - ymax;
+ apply_force(dt, x, y, - dl * vx * repulsion_constant, - dl * vy * repulsion_constant);
+ }
+ }
+}
+
+void Polygon::apply_collision_forces(scalar_t dt, int n_polygon, Polygon *p) {
+ scalar_t closest_x[_total_nb_dots], closest_y[_total_nb_dots];
+ bool inside[_total_nb_dots];
+ scalar_t distance[_total_nb_dots];
+ int _new_effecting_edge[_total_nb_dots];
+
+ int first_dot = 0;
+
+ for(int v = 0; v < _nb_vertices; v++) {
+ int vp = (v+1)%_nb_vertices;
+ scalar_t x = _x[v], y = _y[v], xp = _x[vp], yp = _y[vp];
+
+ for(int d = 0; d < _nb_dots[v]; d++) {
+ inside[d] = false;
+ distance[d] = FLT_MAX;
+ }
+
+ // First, we tag the dots located inside the polygon p
+
+ 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];
+ scalar_t xc = p->_x[p->_triangles[t].c], yc = p->_y[p->_triangles[t].c];
+ scalar_t den, num;
+
+ const scalar_t eps = 1e-6;
+
+ den = prod_vect(xp - x, yp - y, xb - xa, yb - ya);
+ num = prod_vect(xa - x, ya - y, xb - xa, yb - ya);
+ if(den > eps) {
+ if(num / den < max) max = num / den;
+ } else if(den < -eps) {
+ if(num / den > min) min = num / den;
+ } else {
+ if(num < 0) { min = 1; max = 0; }
+ }
+
+ den = prod_vect(xp - x, yp - y, xc - xb, yc - yb);
+ num = prod_vect(xb - x, yb - y, xc - xb, yc - yb);
+ if(den > eps) {
+ if(num / den < max) max = num / den;
+ } else if(den < -eps) {
+ if(num / den > min) min = num / den;
+ } else {
+ if(num < 0) { min = 1; max = 0; }
+ }
+
+ den = prod_vect(xp - x, yp - y, xa - xc, ya - yc);
+ num = prod_vect(xc - x, yc - y, xa - xc, ya - yc);
+ if(den > eps) {
+ if(num / den < max) max = num / den;
+ } else if(den < -eps) {
+ if(num / den > min) min = num / den;
+ } else {
+ if(num < 0) { min = 1; max = 0; }
+ }
+
+ for(int d = 0; d < _nb_dots[v]; d++) {
+ scalar_t s = scalar_t(d * dl)/_length[v];
+ if(s >= min && s <= max) inside[d] = true;
+ }
+ }
+
+ // Then, we compute for each dot what is the closest point on
+ // the boundary of p
+
+ for(int m = 0; m < p->_nb_vertices; m++) {
+ int mp = (m+1)%p->_nb_vertices;
+ scalar_t xa = p->_x[m], ya = p->_y[m];
+ scalar_t xb = p->_x[mp], yb = p->_y[mp];
+ scalar_t gamma0 = ((x - xa) * (xb - xa) + (y - ya) * (yb - ya)) / sq(p->_length[m]);
+ scalar_t gamma1 = ((xp - x) * (xb - xa) + (yp - y) * (yb - ya)) / sq(p->_length[m]);
+ scalar_t delta0 = (prod_vect(xb - xa, yb - ya, x - xa, y - ya)) / p->_length[m];
+ scalar_t delta1 = (prod_vect(xb - xa, yb - ya, xp - x, yp - y)) / p->_length[m];
+
+ for(int d = 0; d < _nb_dots[v]; d++) if(inside[d]) {
+ int r = _effecting_edge[(first_dot + d) * _nb_polygons + n_polygon];
+
+ // If there is already a spring, we look only at the
+ // vertices next to the current one
+
+ if(r < 0 || m == r || m == (r+1)%p->_nb_vertices || (m+1)%p->_nb_vertices == r) {
+
+ scalar_t s = scalar_t(d * dl)/_length[v];
+ scalar_t delta = abs(s * delta1 + delta0);
+ if(delta < distance[d]) {
+ scalar_t gamma = s * gamma1 + gamma0;
+ if(gamma < 0) {
+ scalar_t l = sqrt(sq(x * (1 - s) + xp * s - xa) + sq(y * (1 - s) + yp * s - ya));
+ if(l < distance[d]) {
+ distance[d] = l;
+ closest_x[d] = xa;
+ closest_y[d] = ya;
+ _new_effecting_edge[first_dot + d] = m;
+ }
+ } else if(gamma > 1) {
+ scalar_t l = sqrt(sq(x * (1 - s) + xp * s - xb) + sq(y * (1 - s) + yp * s - yb));
+ if(l < distance[d]) {
+ distance[d] = l;
+ closest_x[d] = xb;
+ closest_y[d] = yb;
+ _new_effecting_edge[first_dot + d] = m;
+ }
+ } else {
+ distance[d] = delta;
+ closest_x[d] = xa * (1 - gamma) + xb * gamma;
+ closest_y[d] = ya * (1 - gamma) + yb * gamma;
+ _new_effecting_edge[first_dot + d] = m;
+ }
+ }
+ }
+ } else _new_effecting_edge[first_dot + d] = -1;
+ }
+
+ // Apply forces
+
+ for(int d = 0; d < _nb_dots[v]; d++) if(inside[d]) {
+ scalar_t s = scalar_t(d * dl)/_length[v];
+ scalar_t x = _x[v] * (1 - s) + _x[vp] * s;
+ scalar_t y = _y[v] * (1 - s) + _y[vp] * s;
+ scalar_t vx = x - closest_x[d];
+ scalar_t vy = y - closest_y[d];
+
+ p->apply_force(dt,
+ closest_x[d], closest_y[d],
+ dl * vx * repulsion_constant, dl * vy * repulsion_constant);
+
+ apply_force(dt,
+ x, y,
+ - dl * vx * repulsion_constant, - dl * vy * repulsion_constant);
+ }
+
+ first_dot += _nb_dots[v];
+ }
+
+ for(int d = 0; d < _total_nb_dots; d++)
+ _effecting_edge[d * _nb_polygons + n_polygon] = _new_effecting_edge[d];
+
+}
+
+bool Polygon::collide(Polygon *p) {
+ for(int n = 0; n < _nb_vertices; n++) {
+ int np = (n+1)%_nb_vertices;
+ for(int m = 0; m < p->_nb_vertices; m++) {
+ int mp = (m+1)%p->_nb_vertices;
+ scalar_t det, s = -1, t = -1;
+ intersection(_x[n], _y[n], _x[np], _y[np],
+ p->_x[m], p->_y[m], p->_x[mp], p->_y[mp], det, s, t);
+ if(det != 0 && s>= 0 && s <= 1&& t >= 0 && t <= 1) return true;
+ }
+ }
+
+ for(int n = 0; n < _nb_vertices; n++) if(p->contain(_x[n], _y[n])) return true;
+ for(int n = 0; n < p->_nb_vertices; n++) if(contain(p->_x[n], p->_y[n])) return true;
+
+ return false;
+}