Update.
[universe.git] / plotter.h
1
2 // Written and (C) by Francois Fleuret
3 // Contact <francois.fleuret@idiap.ch> for comments & bug reports
4
5 #ifndef PLOTTER_H
6 #define PLOTTER_H
7
8 #include <iostream>
9 #include <fstream>
10
11 using namespace std;
12
13 #include "misc.h"
14 #include "universe.h"
15
16 class Plotter {
17   int _width, _height;
18
19   class Tag {
20   public:
21     int index;
22     scalar_t red, green, blue;
23     Tag *next;
24   };
25
26   Tag **_tag_map;
27
28   // We have our own stack of tags to avoid numerous news and deletes
29   Tag *_tag_stack, *_tag_stack_top;
30   scalar_t *_red_imap, *_green_imap, *_blue_imap;
31   int _current_polygon;
32   int _scaling;
33
34   inline void protected_push_tag(int x, int y, scalar_t r, scalar_t g, scalar_t b) {
35     if(y >= 0 && y < _height) {
36
37       if(x < 0) { x = 0; }
38       else if(x >= _width) { x = _width - 1; }
39
40       int t = y * _width + x;
41
42       _tag_stack_top->next = _tag_map[t];
43       _tag_stack_top->index = _current_polygon;
44       _tag_stack_top->red = r;
45       _tag_stack_top->green = g;
46       _tag_stack_top->blue = b;
47       _tag_map[t] = _tag_stack_top;
48       _tag_stack_top++;
49     }
50   }
51
52   inline void push_tag(int x, int y, scalar_t r, scalar_t g, scalar_t b) {
53     int t = y * _width + x;
54     _tag_stack_top->next = _tag_map[t];
55     _tag_stack_top->index = _current_polygon;
56     _tag_stack_top->red = r;
57     _tag_stack_top->green = g;
58     _tag_stack_top->blue = b;
59     _tag_map[t] = _tag_stack_top;
60     _tag_stack_top++;
61   }
62
63   inline scalar_t red_sum(int xmin, int ymin, int xmax, int ymax) {
64     return _red_imap[xmax + (_width + 1) * ymax]
65       + _red_imap[xmin + (_width + 1) * ymin]
66       - _red_imap[xmax + (_width + 1) * ymin]
67       - _red_imap[xmin + (_width + 1) * ymax];
68   }
69
70   inline scalar_t green_sum(int xmin, int ymin, int xmax, int ymax) {
71     return _green_imap[xmax + (_width + 1) * ymax]
72       + _green_imap[xmin + (_width + 1) * ymin]
73       - _green_imap[xmax + (_width + 1) * ymin]
74       - _green_imap[xmin + (_width + 1) * ymax];
75   }
76
77   inline scalar_t blue_sum(int xmin, int ymin, int xmax, int ymax) {
78     return _blue_imap[xmax + (_width + 1) * ymax]
79       + _blue_imap[xmin + (_width + 1) * ymin]
80       - _blue_imap[xmax + (_width + 1) * ymin]
81       - _blue_imap[xmin + (_width + 1) * ymax];
82   }
83
84   void reset();
85
86   void draw_polygon(Polygon *p);
87   void fill();
88 public:
89
90   Plotter(int width, int height, int scaling);
91   ~Plotter();
92
93   void save_as_ppm(Universe *universe, const char *filename, int downscale);
94 };
95
96 #endif