SPH
RenderContext.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "gui/Utils.h"
4 #include "gui/objects/Bitmap.h"
5 #include "gui/objects/Color.h"
7 #include "sph/kernel/Kernel.h"
8 #include <wx/dc.h>
9 
11 
12 
13 enum class ColorFlag {
14  LINE = 1 << 0,
15  FILL = 1 << 1,
16  TEXT = 1 << 2,
17 };
18 
22 class IRenderContext : public Polymorphic {
23 public:
25  virtual Pixel size() const = 0;
26 
28  virtual void setColor(const Rgba& color, const Flags<ColorFlag> flags) = 0;
29 
31  virtual void setThickness(const float thickness) = 0;
32 
33  virtual void setFontSize(const int fontSize) = 0;
34 
38  virtual void fill(const Rgba& color) = 0;
39 
41  virtual void drawLine(const Coords p1, const Coords p2) = 0;
42 
44  virtual void drawCircle(const Coords center, const float radius) = 0;
45 
47  virtual void drawTriangle(const Coords p1, const Coords p2, const Coords p3) = 0;
48 
50  virtual void drawBitmap(const Coords p, const Bitmap<Rgba>& bitmap) = 0;
51 
52  virtual void drawText(const Coords p, const Flags<TextAlign> align, const std::string& s) = 0;
53 
54  virtual void drawText(const Coords p, const Flags<TextAlign> align, const std::wstring& s) = 0;
55 
57  return {};
58  }
59 };
60 
62  INLINE Rgba operator()(const Rgba&, const Rgba& color) const {
63  return color;
64  }
65 };
66 
67 struct OverPixelOp {
68  INLINE Rgba operator()(const Rgba& prev, const Rgba& color) const {
69  return color.over(prev);
70  }
71 };
72 
73 template <typename PixelOp>
75 protected:
76  PixelOp pixelOp;
77 
79 
81 
82  struct {
86  } colors;
87 
88  float thickness = 1._f;
89 
90  int fontSize = 9;
91 
92 public:
94  : bitmap(bitmap) {}
95 
96  virtual Pixel size() const override {
97  return bitmap.size();
98  }
99 
100  virtual void setColor(const Rgba& color, const Flags<ColorFlag> flags) override;
101 
102  virtual void setThickness(const float newThickness) override;
103 
104  virtual void setFontSize(const int newFontSize) override;
105 
106  virtual void fill(const Rgba& color) override;
107 
108  virtual void drawLine(const Coords p1, const Coords p2) override;
109 
110  virtual void drawCircle(const Coords center, const float radius) override;
111 
112  virtual void drawTriangle(const Coords p1, const Coords p2, const Coords p3) override;
113 
114  virtual void drawBitmap(const Coords p, const Bitmap<Rgba>& subBitmap) override;
115 
116  virtual void drawText(const Coords p, const Flags<TextAlign> align, const std::string& s) override;
117 
118  virtual void drawText(const Coords p, const Flags<TextAlign> align, const std::wstring& s) override;
119 
120  virtual Array<IRenderOutput::Label> getLabels() const override {
121  return labels.clone();
122  }
123 
124 private:
125  void drawSafe(const Pixel p, const Rgba c) {
126  if (p.x >= 0 && p.y >= 0 && p.x < bitmap.size().x && p.y < bitmap.size().y) {
127  bitmap[p] = pixelOp(bitmap[p], c);
128  }
129  }
130 
131  void drawSafe(const Coords p, const Rgba c) {
132  this->drawSafe(Pixel(p), c);
133  }
134 
135  void draw(const Pixel p, const Rgba c) {
136  bitmap[p] = pixelOp(bitmap[p], c);
137  }
138 
139  void draw(const Coords p, const Rgba c) {
140  this->draw(Pixel(p), c);
141  }
142 };
143 
146 class AntiAliasedRenderContext : public PreviewRenderContext<OverPixelOp> {
147 public:
150 
151  virtual void drawCircle(const Coords center, const float radius) override;
152 
153 protected:
154  void drawSafe(const Pixel p, const Rgba c) {
155  if (p.x >= 0 && p.y >= 0 && p.x < bitmap.size().x && p.y < bitmap.size().y) {
156  bitmap[Pixel(p)] = c.over(bitmap[Pixel(p)]);
157  }
158  }
159 };
160 
161 
163 private:
164  LutKernel<2> kernel;
165 
166 public:
169  , kernel(kernel) {}
170 
171  virtual void drawCircle(const Coords center, const float radius) override;
172 };
173 
174 
179 private:
180  wxDC& dc;
181  wxPen pen;
182  wxBrush brush;
183 
184 public:
185  WxRenderContext(wxDC& dc)
186  : dc(dc) {
187  pen = dc.GetPen();
188  brush = dc.GetBrush();
189  }
190 
191  virtual Pixel size() const override {
192  const wxSize s = dc.GetSize();
193  return Pixel(s.x, s.y);
194  }
195 
196  virtual void setColor(const Rgba& color, const Flags<ColorFlag> flags) override {
197  if (flags.has(ColorFlag::LINE)) {
198  pen.SetColour(wxColour(color));
199  dc.SetPen(pen);
200  }
201  if (flags.has(ColorFlag::FILL)) {
202  brush.SetColour(wxColour(color));
203  dc.SetBrush(brush);
204  }
205  if (flags.has(ColorFlag::TEXT)) {
206  dc.SetTextForeground(wxColour(color));
207  }
208  }
209 
210  virtual void setThickness(const float UNUSED(newThickness)) override {
211  }
212 
213  virtual void setFontSize(const int newFontSize) override {
214  wxFont font = dc.GetFont();
215  font.SetPointSize(newFontSize);
216  dc.SetFont(font);
217  }
218 
219  virtual void fill(const Rgba& color) override {
220  brush.SetColour(wxColour(color));
221  dc.SetBrush(brush);
222  dc.DrawRectangle(wxPoint(0, 0), dc.GetSize());
223  }
224 
225  virtual void drawLine(const Coords p1, const Coords p2) override {
226  dc.DrawLine(wxPoint(p1), wxPoint(p2));
227  }
228 
229  virtual void drawCircle(const Coords center, const float radius) override {
230  dc.DrawCircle(wxPoint(center), int(radius));
231  }
232 
233  virtual void drawTriangle(const Coords, const Coords, const Coords) override {
235  }
236 
237  virtual void drawBitmap(const Coords, const Bitmap<Rgba>&) override {
239  }
240 
241  virtual void drawText(const Coords p, const Flags<TextAlign> align, const std::string& s) override {
242  std::wstring ws(s.begin(), s.end());
243  this->drawText(p, align, ws);
244  }
245 
246  virtual void drawText(const Coords p, const Flags<TextAlign> align, const std::wstring& s) override {
247  IRenderOutput::Label label;
248  label.text = s;
249  label.align = align;
250  label.fontSize = dc.GetFont().GetPointSize();
251  label.color = Rgba(dc.GetTextForeground());
252  label.position = Pixel(p);
254  }
255 };
256 
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Wrapper of wxBitmap, will be possibly replaced by custom implementation.
const float radius
Definition: CurveDialog.cpp:18
Interface for renderers.
SPH kernels.
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
ColorFlag
Definition: RenderContext.h:13
void printLabels(wxDC &dc, ArrayView< const IRenderOutput::Label > labels)
Definition: Utils.cpp:186
Random utility functions for drawing stuff to DC.
void drawSafe(const Pixel p, const Rgba c)
virtual void drawCircle(const Coords center, const float radius) override
Draws a circle, given its center and a radius.
AntiAliasedRenderContext(Bitmap< Rgba > &bitmap)
Array clone() const
Performs a deep copy of all elements of the array.
Definition: Array.h:147
Pixel size() const
Definition: Bitmap.h:72
Wrapper of an integral value providing functions for reading and modifying individual bits.
Definition: Flags.h:20
constexpr INLINE bool has(const TEnum flag) const
Checks if the object has a given flag.
Definition: Flags.h:77
Abstraction of a device used for rendering.
Definition: RenderContext.h:22
virtual void setFontSize(const int fontSize)=0
virtual void drawCircle(const Coords center, const float radius)=0
Draws a circle, given its center and a radius.
virtual void drawLine(const Coords p1, const Coords p2)=0
Draws a line connecting two points.
virtual void drawText(const Coords p, const Flags< TextAlign > align, const std::string &s)=0
virtual void drawTriangle(const Coords p1, const Coords p2, const Coords p3)=0
Draws a triangle given three points.
virtual void drawText(const Coords p, const Flags< TextAlign > align, const std::wstring &s)=0
virtual void setColor(const Rgba &color, const Flags< ColorFlag > flags)=0
Selects the color for one or more drawing modes.
virtual Array< IRenderOutput::Label > getLabels() const
Definition: RenderContext.h:56
virtual void setThickness(const float thickness)=0
Modifies the thickness of the lines.
virtual void drawBitmap(const Coords p, const Bitmap< Rgba > &bitmap)=0
Draws a bitmap, given the position of its left-top corner.
virtual void fill(const Rgba &color)=0
Fills the whole canvas with given color.
virtual Pixel size() const =0
Returns the size of the canvas associated with the context.
Bitmap< Rgba > & bitmap
Definition: RenderContext.h:78
virtual void drawCircle(const Coords center, const float radius) override
Draws a circle, given its center and a radius.
virtual Array< IRenderOutput::Label > getLabels() const override
virtual void setColor(const Rgba &color, const Flags< ColorFlag > flags) override
Selects the color for one or more drawing modes.
virtual void fill(const Rgba &color) override
Fills the whole canvas with given color.
PreviewRenderContext(Bitmap< Rgba > &bitmap)
Definition: RenderContext.h:93
virtual void setThickness(const float newThickness) override
Modifies the thickness of the lines.
virtual void drawTriangle(const Coords p1, const Coords p2, const Coords p3) override
Draws a triangle given three points.
virtual void drawBitmap(const Coords p, const Bitmap< Rgba > &subBitmap) override
Draws a bitmap, given the position of its left-top corner.
Array< IRenderOutput::Label > labels
Definition: RenderContext.h:80
virtual void drawText(const Coords p, const Flags< TextAlign > align, const std::string &s) override
virtual void setFontSize(const int newFontSize) override
struct PreviewRenderContext::@28 colors
virtual Pixel size() const override
Returns the size of the canvas associated with the context.
Definition: RenderContext.h:96
virtual void drawLine(const Coords p1, const Coords p2) override
Draws a line connecting two points.
Definition: Color.h:8
static Rgba black()
Definition: Color.h:190
static Rgba white()
Definition: Color.h:194
Rgba over(const Rgba &other) const
Blends two colors together using "over" operation.
Definition: Color.h:120
SmoothedRenderContext(Bitmap< Rgba > &bitmap, const LutKernel< 2 > &kernel)
virtual void drawCircle(const Coords center, const float radius) override
Draws a circle, given its center and a radius.
Render context drawing directly into wxDC.
virtual void drawLine(const Coords p1, const Coords p2) override
Draws a line connecting two points.
WxRenderContext(wxDC &dc)
virtual void drawText(const Coords p, const Flags< TextAlign > align, const std::string &s) override
virtual void setColor(const Rgba &color, const Flags< ColorFlag > flags) override
Selects the color for one or more drawing modes.
virtual Pixel size() const override
Returns the size of the canvas associated with the context.
virtual void drawBitmap(const Coords, const Bitmap< Rgba > &) override
Draws a bitmap, given the position of its left-top corner.
virtual void drawTriangle(const Coords, const Coords, const Coords) override
Draws a triangle given three points.
virtual void setThickness(const float UNUSED(newThickness)) override
virtual void fill(const Rgba &color) override
Fills the whole canvas with given color.
virtual void drawText(const Coords p, const Flags< TextAlign > align, const std::wstring &s) override
virtual void setFontSize(const int newFontSize) override
virtual void drawCircle(const Coords center, const float radius) override
Draws a circle, given its center and a radius.
Definition: Point.h:115
std::wstring text
Definition: IRenderer.h:41
Flags< TextAlign > align
Definition: IRenderer.h:44
INLINE Rgba operator()(const Rgba &prev, const Rgba &color) const
Definition: RenderContext.h:68
INLINE Rgba operator()(const Rgba &, const Rgba &color) const
Definition: RenderContext.h:62
Definition: Point.h:101
Base class for all polymorphic objects.
Definition: Object.h:88