Fixed the interpreter.
[flatland.git] / polygon.h
1
2 /*
3
4    flatland is a simple 2d physical simulator
5
6    Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
7    Written by Francois Fleuret <francois.fleuret@idiap.ch>
8
9    This file is part of flatland
10
11    flatland is free software: you can redistribute it and/or modify it
12    under the terms of the GNU General Public License version 3 as
13    published by the Free Software Foundation.
14
15    flatland is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with flatland.  If not, see <http://www.gnu.org/licenses/>.
22
23 */
24
25 #ifndef POLYGON_H
26 #define POLYGON_H
27
28 #include "misc.h"
29 #include "canvas.h"
30
31 #ifdef XFIG_SUPPORT
32 #include "xfig_tracer.h"
33 #endif
34
35 #ifdef X11_SUPPORT
36 #include "simple_window.h"
37 #endif
38
39 class Polygon {
40   struct Triangle {
41     int a, b, c;
42   };
43
44   int _nb_max_polygons;
45   int _nb_polygons; // We need to know the total number of polygons in
46                     // the universe
47
48   scalar_t _mass, _moment_of_inertia, _radius;
49
50   scalar_t *_relative_x, *_relative_y;
51   scalar_t _last_center_x, _last_center_y, _last_theta;
52
53   int _total_nb_dots;
54   int *_nb_dots;
55   int *_effecting_edge;
56
57   scalar_t *_length;
58   Triangle *_triangles;
59
60   inline void intersection(scalar_t xa2, scalar_t ya2,
61                            scalar_t xa1, scalar_t ya1,
62                            scalar_t xb2, scalar_t yb2,
63                            scalar_t xb1, scalar_t yb1,
64                            scalar_t &det, scalar_t &s1, scalar_t &s2) {
65     scalar_t m11, m12, m21, m22, n11, n12, n21, n22, v1, v2;
66     m11 = xa1 - xa2; m12 = xb2 - xb1; m21 = ya1 - ya2; m22 = yb2 - yb1;
67     det = m11 * m22 - m12 * m21;
68     if(det != 0) {
69       n11 = + m22 / det; n12 = - m12 / det; n21 = - m21 / det; n22 = + m11 / det;
70       v1 = xb2 - xa2; v2 = yb2 - ya2;
71       s1 = n11 * v1 + n12 * v2; s2 = n21 * v1 + n22 * v2;
72     }
73   }
74
75 public:
76   bool _initialized;
77   bool _nailed;
78   int _nb_vertices;
79   scalar_t *_x, *_y;
80   scalar_t _red, _green, _blue;
81   scalar_t _center_x, _center_y, _theta;
82   scalar_t _dcenter_x, _dcenter_y, _dtheta;
83
84   // The x and y parameters are ignored if null. Useful to initialize
85   // directly.
86
87   Polygon(scalar_t mass,
88           scalar_t red, scalar_t green, scalar_t blue,
89           scalar_t *x, scalar_t *y,
90           int nv);
91
92   ~Polygon();
93
94   Polygon *clone();
95
96 #ifdef XFIG_SUPPORT
97   void color_xfig(XFigTracer *tracer);
98   void print_xfig(XFigTracer *tracer);
99 #endif
100
101 #ifdef X11_SUPPORT
102   void draw(SimpleWindow *window);
103   void draw_contours(SimpleWindow *window);
104 #endif
105
106   void draw(Canvas *canvas);
107   void draw_contours(Canvas *canvas);
108
109   void set_vertex(int k, scalar_t x, scalar_t y);
110   void set_position(scalar_t center_x, scalar_t center_y, scalar_t theta);
111   void set_speed(scalar_t dcenter_x, scalar_t dcenter_y, scalar_t dtheta);
112   bool contain(scalar_t x, scalar_t y);
113   void triangularize(int &nt, int nb, int *index);
114   void initialize(int nb_polygons);
115   bool update(scalar_t dt);
116   scalar_t relative_x(scalar_t ax, scalar_t ay);
117   scalar_t relative_y(scalar_t ax, scalar_t ay);
118   scalar_t absolute_x(scalar_t rx, scalar_t ry);
119   scalar_t absolute_y(scalar_t rx, scalar_t ry);
120
121   void apply_force(scalar_t dt, scalar_t x, scalar_t y, scalar_t fx, scalar_t fy);
122   void apply_border_forces(scalar_t dt, scalar_t xmin, scalar_t ymin, scalar_t xmax, scalar_t ymax);
123   void apply_collision_forces(scalar_t dt, int n_polygon, Polygon *p);
124
125   bool collide_with_borders(scalar_t xmin, scalar_t ymin,
126                             scalar_t xmax, scalar_t ymax);
127
128   bool collide(Polygon *p);
129 };
130
131 #endif