SPH
GraphicsContext.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "gui/objects/Color.h"
9 #include "post/Plot.h"
10 #include "post/Point.h"
11 #include <wx/dcclient.h>
12 #include <wx/dcmemory.h>
13 #include <wx/graphics.h>
14 
16 
17 class GraphicsPath : public IDrawPath {
18 private:
20  wxGraphicsMatrix matrix;
21  wxGraphicsPath path;
22  bool first;
23 
24 public:
25  explicit GraphicsPath(const SharedPtr<wxGraphicsContext>& gc, const wxGraphicsMatrix& matrix)
26  : gc(gc)
27  , matrix(matrix) {
28  path = gc->CreatePath();
29  first = true;
30  }
31 
32  virtual void addPoint(const PlotPoint& point) override {
33  double x = point.x, y = point.y;
34  matrix.TransformPoint(&x, &y);
35  if (first) {
36  path.MoveToPoint(x, y);
37  first = false;
38  } else {
39  path.AddLineToPoint(x, y);
40  }
41  }
42 
43  virtual void closePath() override {
44  path.CloseSubpath();
45  gc->StrokePath(path);
46  }
47 
48  virtual void endPath() override {
49  gc->StrokePath(path);
50  }
51 };
52 
55 private:
57 
59  wxGraphicsMatrix matrix;
60 
62  const Float ps = 3._f;
63 
65  Rgba color;
66 
67 public:
69  GraphicsContext(wxPaintDC& dc, const Rgba color)
70  : gc(wxGraphicsContext::Create(dc))
71  , color(color) {
72  this->setStyle(0);
73  matrix = gc->CreateMatrix();
74  }
75 
77  GraphicsContext(wxMemoryDC& dc, const Rgba color)
78  : gc(wxGraphicsContext::Create(dc))
79  , color(color) {
80  this->setStyle(0);
81  matrix = gc->CreateMatrix();
82  }
83 
84  virtual void drawPoint(const PlotPoint& point) override {
85  double x = point.x, y = point.y;
86  matrix.TransformPoint(&x, &y);
87  gc->DrawEllipse(x - ps / 2, y - ps / 2, ps, ps);
88  }
89 
90  virtual void drawErrorPoint(const ErrorPlotPoint& point) override {
91  this->drawPoint(point);
92  }
93 
94  virtual void drawLine(const PlotPoint& from, const PlotPoint& to) override {
95  double x1 = from.x, y1 = from.y;
96  matrix.TransformPoint(&x1, &y1);
97  double x2 = to.x, y2 = to.y;
98  matrix.TransformPoint(&x2, &y2);
99  gc->StrokeLine(x1, y1, x2, y2);
100  }
101 
102  virtual AutoPtr<IDrawPath> drawPath() override {
103  return makeAuto<GraphicsPath>(gc, matrix);
104  }
105 
106  virtual void setStyle(const Size index) override {
107  // we currently only distinguish between style 0 and any positive value; this can be generalized in
108  // the future if needed.
109  wxPen pen;
110  if (index == 0) {
111  // selected color
112  pen.SetColour(wxColour(color));
113  } else {
114  // black
115  pen.SetColour(*wxBLACK);
116  }
117  gc->SetPen(pen);
118  }
119 
120  virtual void setTransformMatrix(const AffineMatrix2& m) override {
121  matrix.Set(m(0, 0), m(0, 1), m(1, 0), m(1, 1), m(0, 2), m(1, 2));
122  }
123 };
124 
125 
127 /*class IInterpolation : public Polymorphic {
128 protected:
129  Array<PlotPoint> points;
130 
131 public:
132  IInterpolation(Array<PlotPoint>&& points)
133  : points(std::move(points)) {
134  auto compare = [](PlotPoint& p1, PlotPoint& p2) { return p1.x < p2.x; };
135  std::sort(this->points.begin(), this->points.end(), compare);
136  }
137 
138  virtual Array<PlotPoint> interpolate(const Interval& range, const Float step) const = 0;
139 };
140 
141 
142 class LinearInterpolation : public IInterpolation {
143 public:
144  LinearInterpolation(Array<PlotPoint>&& points)
145  : IInterpolation(std::move(points)) {}
146 
147  virtual Array<PlotPoint> interpolate(const Interval& range, const Float step) const {
148  Array<PlotPoint> result;
149  Size index = 0;
150  const Float x0 = points[index].x;
151  if (range.lower() < x0) {
152  // extend using tangent of two left-most points
153  }
154  for (Float x = range.lower(); x < range.upper(); x += step) {
155 
156  // if ()
157  }
158  // find index
159  // const Size i0;
160  return result;
161  }
162 };*/
163 
164 /*class CatmullRomInterpolation : public IInterpolation {
165 public:
166  Float operator()() const {
167  PlotPoint p0, p1, p2, p3;
168  // compute curve parameters, using centripetal Catmull-Rom parametrization
169  const Float t1 = getParam(p0, p1);
170  const Float t2 = getParam(p1, p2) + t1;
171  const Float t3 = getParam(p2, p3) + t2;
172  return t3;
173  }
174 
175 private:
176  INLINE Float getParam(const PlotPoint p1, const PlotPoint p2) const {
177  return root<4>(sqr(p2.x - p1.x) + sqr(p2.y - p1.y));
178  }
179 };*/
180 
181 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
#define NAMESPACE_SPH_END
Definition: Object.h:12
Drawing quantity values as functions of time or spatial coordinates.
2D affine matrix
Definition: Point.h:50
Wrapper of pointer that deletes the resource from destructor.
Definition: AutoPtr.h:15
Drawing context using wxWidgets implementation of Cairo backend.
GraphicsContext(wxMemoryDC &dc, const Rgba color)
Constructs the drawing context from wxMemoryDC.
virtual void drawLine(const PlotPoint &from, const PlotPoint &to) override
Draws a line connecting two points.
GraphicsContext(wxPaintDC &dc, const Rgba color)
Constructs the drawing context from wxPaintDC.
virtual void drawPoint(const PlotPoint &point) override
Adds a single point to the plot.
virtual void drawErrorPoint(const ErrorPlotPoint &point) override
Adds a point with error bars to the plot.
virtual void setTransformMatrix(const AffineMatrix2 &m) override
Applies the given tranformation matrix on all primitives.
virtual void setStyle(const Size index) override
Changes the current drawing style.
virtual AutoPtr< IDrawPath > drawPath() override
Draws a path connecting points.
virtual void endPath() override
Finalizes the path. Does not connect the last point to anything.
GraphicsPath(const SharedPtr< wxGraphicsContext > &gc, const wxGraphicsMatrix &matrix)
virtual void closePath() override
Closes the path, connecting to the first point on the path.
virtual void addPoint(const PlotPoint &point) override
Adds a next point on the path.
Definition: Plot.h:19
Abstraction of a drawing context.
Definition: Plot.h:34
Definition: Color.h:8
2D point and other primitives for 2D geometry
Point with error bars.
Definition: Point.h:43
Point in 2D plot.
Definition: Point.h:16
Float y
Definition: Point.h:17
Float x
Definition: Point.h:17