blob: 6da086c27bbb157df3a9b968e0c660c601385689 [file] [log] [blame]
James Kuszmaul6271cf72019-09-18 20:10:08 -07001#ifndef FRC971_ANALYSIS_PLOTTING_WEBGL2_PLOTTER_H_
2#define FRC971_ANALYSIS_PLOTTING_WEBGL2_PLOTTER_H_
3
4#include <vector>
5
6#include <Eigen/Dense>
7#define GL_GLEXT_PROTOTYPES
8#include <GLES3/gl3.h>
9#include <GLES3/gl2ext.h>
10#include <GLES3/gl32.h>
11
12namespace frc971 {
13namespace plotting {
14
15struct Color {
16 float r;
17 float g;
18 float b;
19};
20
21class Line {
22 public:
23 virtual ~Line() {}
24 virtual void SetPoints(const std::vector<Eigen::Vector2d> &pts) = 0;
25 virtual void SetColor(const Color &color) = 0;
26 virtual void Draw() = 0;
27 virtual Eigen::Vector2d MaxValues() const = 0;
28 virtual Eigen::Vector2d MinValues() const = 0;
29 virtual void SetLineWidth(const float width) = 0;
30 virtual void SetPointSize(const float point_size) = 0;
31 virtual bool HasUpdate() = 0;
32};
33
34// TODO(james): Actually do something with this interface; originally, I'd meant
35// to look at writing some tests, but right now it's just extra boilerplate.
36class Plotter {
37 public:
38 virtual Line *AddLine() = 0;
39 virtual void SetScale(const Eigen::Vector2d &scale) = 0;
40 virtual Eigen::Vector2d GetScale() const = 0;
41 virtual void SetOffset(const Eigen::Vector2d &offset) = 0;
42 virtual Eigen::Vector2d GetOffset() const = 0;
43 virtual void Redraw() = 0;
44 virtual Eigen::Vector2d MaxValues() const = 0;
45 virtual Eigen::Vector2d MinValues() const = 0;
46 virtual void ClearZoomRectangle() = 0;
47 virtual void SetZoomRectangle(const Eigen::Vector2d &corner1,
48 const Eigen::Vector2d &corner2) = 0;
49 virtual void RecordState() = 0;
50 virtual void Undo() = 0;
51};
52
53class WebglCanvasPlotter : public Plotter {
54 public:
55 WebglCanvasPlotter(const std::string &canvas_id,
56 GLuint attribute_location = 0);
57 Line *AddLine() override;
58 void SetScale(const Eigen::Vector2d &scale) override;
59 Eigen::Vector2d GetScale() const override;
60 void SetOffset(const Eigen::Vector2d &offset) override;
61 Eigen::Vector2d GetOffset() const override;
62 void Redraw() override;
63 Eigen::Vector2d MaxValues() const override;
64 Eigen::Vector2d MinValues() const override;
65 void ClearZoomRectangle() override;
66 void SetZoomRectangle(const Eigen::Vector2d &corner1,
67 const Eigen::Vector2d &corner2) override;
68 void RecordState() override;
69 void Undo() override;
70
71 private:
72 std::vector<std::unique_ptr<Line>> lines_;
73 std::unique_ptr<Line> zoom_rectangle_;
74 Eigen::Vector2d scale_{1.0, 1.0};
75 Eigen::Vector2d offset_{0.0, 0.0};
76 std::vector<Eigen::Vector2d> old_scales_;
77 std::vector<Eigen::Vector2d> old_offsets_;
78 Eigen::Vector2d last_scale_{1.0, 1.0};
79 Eigen::Vector2d last_offset_{0.0, 0.0};
80 GLuint program_;
81 GLuint scale_uniform_location_;
82 GLuint offset_uniform_location_;
83 GLuint gl_buffer_;
84};
85
86} // namespace plotting
87} // namespace frc971
88#endif // FRC971_ANALYSIS_PLOTTING_WEBGL2_PLOTTER_H_