Empirical
canvas_utils.h
Go to the documentation of this file.
1 
13 #ifndef EMP_WEB_CANVAS_UTILS_H
14 #define EMP_WEB_CANVAS_UTILS_H
15 
16 #include "Canvas.h"
17 
18 #include "../base/vector.h"
19 #include "../Evolve/StateGrid.h"
20 #include "../geometry/Circle2D.h"
21 #include "../geometry/Surface2D.h"
22 #include "../tools/BitMatrix.h"
23 #include "color_map.h"
24 
25 namespace emp {
26 namespace web {
27 
29  void Draw(Canvas canvas, const emp::Circle & circle,
30  const std::string & fill="",
31  const std::string & line="")
32  {
33  canvas.Clear();
34  canvas.Draw(circle, fill, line);
35  }
36 
37 
39  template <size_t COLS, size_t ROWS>
40  void Draw(Canvas canvas, const BitMatrix<COLS,ROWS> & matrix, double w, double h)
41  {
42  canvas.Clear();
43 
44  double cell_w = w / (double) COLS;
45  double cell_h = h / (double) ROWS;
46 
47  for (size_t x = 0; x < COLS; x++) {
48  for (size_t y = 0; y < ROWS; y++) {
49  if (matrix.Get(x,y)) {
50  canvas.Rect({x*cell_w, y*cell_h}, cell_w, cell_h, "black");
51  }
52  }
53  }
54  }
55 
56 
62  template <typename BODY_TYPE>
63  void Draw(Canvas canvas,
64  const Surface2D<BODY_TYPE> & surface,
65  const emp::vector<std::string> & color_map)
66  {
67  canvas.Clear();
68 
69  const double w = surface.GetWidth();
70  const double h = surface.GetHeight();
71 
72  // Setup a black background for the surface
73  canvas.Rect({0,0}, w, h, "black");
74 
75  // Draw the circles.
76  const auto & body_set = surface.GetConstBodySet();
77  for (auto body : body_set) {
78  //canvas.Circle(body->GetPerimeter(), color_map[body->GetColorID()], "white");
79  canvas.Circle(body->GetPerimeter(), "", color_map[body->GetColorID()]);
80  //canvas.Circle(body->GetPerimeter(), "", "white");
81  }
82  }
83 
89  template <typename BODY_TYPE>
90  void Draw(Canvas canvas, const Surface2D<BODY_TYPE> & surface, size_t num_colors)
91  {
92  Draw(canvas, surface, GetHueMap(num_colors));
93  }
94 
95 
105  void Draw(Canvas canvas,
106  const emp::vector<emp::vector<size_t>> & grid,
107  const emp::vector<std::string> & color_map,
108  std::string line_color,
109  double cell_width, double cell_height,
110  double offset_x, double offset_y)
111  {
112  canvas.Clear();
113 
114  // Setup a black background for the grid.
115  canvas.Rect({0,0}, canvas.GetWidth(), canvas.GetHeight(), "black");
116 
117  // Fill out the grid!
118  const size_t grid_rows = grid.size();
119  const size_t grid_cols = grid[0].size();
120  for (size_t row = 0; row < grid_rows; row++) {
121  const double cur_y = offset_y + row*cell_height;
122  for (size_t col = 0; col < grid_cols; col++) {
123  const double cur_x = offset_x + col*cell_width;
124  const std::string & cur_color = color_map[grid[row][col]];
125  canvas.Rect({cur_x,cur_y}, cell_width, cell_height, cur_color, line_color);
126  }
127  }
128  }
129 
137  void Draw(Canvas canvas,
138  const emp::vector<emp::vector<size_t>> & grid,
139  const emp::vector<std::string> & color_map,
140  std::string line_color,
141  double cell_w, double cell_h)
142  {
143  const double canvas_w = canvas.GetWidth();
144  const double canvas_h = canvas.GetHeight();
145  const double grid_w = cell_w * grid[0].size();
146  const double grid_h = cell_h * grid.size();
147 
148  // Center the grid on the canvas if there's extra room.
149  const double offset_x = (canvas_w <= grid_w) ? 0 : (canvas_w - grid_w) / 2;
150  const double offset_y = (canvas_h <= grid_h) ? 0 : (canvas_h - grid_h) / 2;
151 
152  // Call Draw with all of the extra details.
153  Draw(canvas, grid, color_map, line_color, cell_w, cell_h, offset_x, offset_y);
154  }
155 
161  void Draw(Canvas canvas,
162  const emp::vector<emp::vector<size_t>> & grid,
163  const emp::vector<std::string> & color_map,
164  std::string line_color="black")
165  {
166  // Determine the cell width & height
167  const double cell_w = canvas.GetWidth() / grid[0].size();
168  const double cell_h = canvas.GetHeight() / grid.size();
169 
170  Draw(canvas, grid, color_map, line_color, cell_w, cell_h);
171  }
172 
183  void Draw(Canvas canvas,
184  const emp::vector<size_t> & grid,
185  size_t grid_cols,
186  const emp::vector<std::string> & color_map,
187  std::string line_color,
188  double cell_width, double cell_height,
189  double offset_x, double offset_y)
190  {
191  canvas.Clear();
192 
193  // Setup a black background for the grid.
194  canvas.Rect({0,0}, canvas.GetWidth(), canvas.GetHeight(), "black");
195 
196  // Fill out the grid!
197  const size_t grid_rows = grid.size() / grid_cols;
198  size_t id = 0;
199  for (size_t row = 0; row < grid_rows; row++) {
200  const double cur_y = offset_y + row*cell_height;
201  for (size_t col = 0; col < grid_cols; col++) {
202  const double cur_x = offset_x + col*cell_width;
203  const std::string & cur_color = color_map[grid[id++]];
204  canvas.Rect({cur_x,cur_y}, cell_width, cell_height, cur_color, line_color);
205  }
206  }
207  }
208 
214  void Draw(Canvas canvas,
215  const StateGrid & state_grid,
216  const emp::vector<std::string> & color_map,
217  std::string line_color="black")
218  {
219  // Determine the canvas info.
220  const double canvas_w = canvas.GetWidth();
221  const double canvas_h = canvas.GetHeight();
222 
223  // Determine the cell width & height.
224  const double cell_w = canvas_w / state_grid.GetWidth();
225  const double cell_h = canvas_h / state_grid.GetHeight();
226 
227  // Determine the realized grid width and height on the canvas.
228  const double grid_w = cell_w * state_grid.GetWidth();
229  const double grid_h = cell_h * state_grid.GetHeight();
230 
231  // Center the grid on the canvas if there's extra room.
232  const double offset_x = (canvas_w <= grid_w) ? 0 : (canvas_w - grid_w) / 2;
233  const double offset_y = (canvas_h <= grid_h) ? 0 : (canvas_h - grid_h) / 2;
234 
235  canvas.Clear();
236 
237  // Setup a black background for the grid.
238  canvas.Rect({0,0}, canvas.GetWidth(), canvas.GetHeight(), line_color);
239 
240  // Fill out the grid!
241  size_t id = 0;
242  for (size_t row = 0; row < state_grid.GetHeight(); row++) {
243  const double cur_y = offset_y + row*cell_h;
244  for (size_t col = 0; col < state_grid.GetWidth(); col++) {
245  const double cur_x = offset_x + col*cell_w;
246  const int state = state_grid.GetStates()[id++];
247  if (state < 0) continue; // leave negative-number squares blank...
248  const std::string & cur_color = color_map[(size_t) state];
249  canvas.Rect({cur_x,cur_y}, cell_w, cell_h, cur_color, line_color);
250  }
251  }
252  }
253 
261  void DrawGridBG(Canvas canvas, size_t rows, size_t cols,
262  const std::string & bg_color, const std::string & line_color) {
263  canvas.Clear(bg_color);
264 
265  const double canvas_x = (double) canvas.GetWidth();
266  const double canvas_y = (double) canvas.GetHeight();
267  const double cell_width = canvas_x / cols;
268  const double cell_height = canvas_y / rows;
269 
270  for (size_t i = 0; i <= cols; i++) {
271  double x = cell_width * i;
272  canvas.Line( {x,0}, {x,canvas_y}, line_color);
273  }
274  for (size_t i = 0; i <= rows; i++) {
275  double y = cell_height * i;
276  canvas.Line( {0,y}, {canvas_x,y}, line_color);
277  }
278  }
279 
280  template <typename CONTAINER_T, typename POINT_FUN_T, typename COLOR_FUN_T>
281  void DrawPoints(Canvas canvas, CONTAINER_T && container, double radius,
282  POINT_FUN_T && point_fun, COLOR_FUN_T && color_fun,
283  const std::string & line_color="black")
284  {
285  // Draw all of the organisms
286  for (auto obj : container) {
287  const auto pos = point_fun(obj);
288  const auto color = color_fun(obj);
289  canvas.Circle(pos, radius, color, line_color);
290  }
291  }
292 
293 
294 }
295 }
296 
297 #endif
Canvas & Clear()
Clear everything off of this canvas.
Definition: Canvas.h:254
Canvas & Circle(Point center, double _r, Ts &&...vals)
Definition: Canvas.h:131
const emp::vector< Ptr< BODY_TYPE > > & GetConstBodySet() const
Definition: Surface2D.h:53
bool Get(size_t col, size_t row) const
Definition: BitMatrix.h:101
Definition: Surface2D.h:38
Definition: Circle2D.h:16
A StateGrid describes a map of grid positions to the current state of each position.
Definition: StateGrid.h:105
const emp::vector< std::string > & GetHueMap(size_t map_size, double min_h=0.0, double max_h=360.0, int s=100, int l=50)
Definition: color_map.h:65
double GetWidth() const
Definition: Surface2D.h:48
size_t size() const
Definition: vector.h:151
Manage an HTML canvas object.
Canvas & Line(double x1, double y1, double x2, double y2, Ts &&...vals)
Add a Line from x1,y1 to x2,y2. Optional face color and line color.
Definition: Canvas.h:172
A simple class to manage a COLS x ROWS matrix of bits.
Definition: BitMatrix.h:38
Tools to dynamically build (and cache) color maps.
double GetHeight() const
Definition: Surface2D.h:49
Manage an HTML Canvas object.
Definition: Canvas.h:27
If we are in emscripten, make sure to include the header.
Definition: array.h:37
void Draw(Canvas canvas, const emp::Circle &circle, const std::string &fill="", const std::string &line="")
Draw a Circle onto the canvas.
Definition: canvas_utils.h:29
double GetWidth() const
Get the pixel width of this Canvas.
Definition: Canvas.h:119
size_t GetHeight() const
Definition: StateGrid.h:126
const emp::vector< int > GetStates() const
Definition: StateGrid.h:128
void DrawPoints(Canvas canvas, CONTAINER_T &&container, double radius, POINT_FUN_T &&point_fun, COLOR_FUN_T &&color_fun, const std::string &line_color="black")
Definition: canvas_utils.h:281
double GetHeight() const
Get the pixel height of this Canvas.
Definition: Canvas.h:120
Canvas & Draw(const emp::Circle &circle, const std::string &fc="", const std::string &lc="")
Draw a circle onto this canvas.
Definition: Canvas.h:229
size_t GetWidth() const
Definition: StateGrid.h:125
void DrawGridBG(Canvas canvas, size_t rows, size_t cols, const std::string &bg_color, const std::string &line_color)
Definition: canvas_utils.h:261
Canvas & Rect(Point corner, double w, double h, Ts &&...vals)
Definition: Canvas.h:145