SPH
SvgContext.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "gui/objects/Point.h"
9 #include "io/Path.h"
10 #include "post/Plot.h"
11 #include <wx/dcsvg.h>
12 
14 
15 class SvgPath : public IDrawPath {
16 private:
17  wxSVGFileDC& dc;
18  AffineMatrix2 matrix;
19 
20  PlotPoint prev{ -1, -1 };
21  PlotPoint first;
22 
23 public:
24  SvgPath(wxSVGFileDC& dc, const AffineMatrix2& matrix)
25  : dc(dc)
26  , matrix(matrix) {}
27 
28  virtual void addPoint(const PlotPoint& point) override {
29  if (prev == PlotPoint{ -1, -1 }) {
30  first = point;
31  } else {
32  const PlotPoint p1 = matrix.transformPoint(prev);
33  const PlotPoint p2 = matrix.transformPoint(point);
34  dc.DrawLine({ int(p1.x), int(p1.y) }, { int(p2.x), int(p2.y) });
35  }
36  prev = point;
37  }
38 
39  virtual void closePath() override {
40  const PlotPoint p1 = matrix.transformPoint(prev);
41  const PlotPoint p2 = matrix.transformPoint(first);
42  dc.DrawLine({ int(p1.x), int(p1.y) }, { int(p2.x), int(p2.y) });
43  }
44 
45  virtual void endPath() override {
46  // do nothing
47  }
48 };
49 
50 class SvgContext : public IDrawingContext {
51 private:
52  wxSVGFileDC dc;
53  int pointSize = 3;
54  AffineMatrix2 matrix;
55 
56 public:
57  SvgContext(const Path& path, const Pixel size, const double dpi = 72)
58  : dc(path.native(), size.x, size.y, dpi) {}
59 
60  virtual void drawPoint(const PlotPoint& point) override {
61  const PlotPoint p = matrix.transformPoint(point);
62  dc.DrawCircle(int(p.x), int(p.y), pointSize);
63  }
64 
65  virtual void drawErrorPoint(const ErrorPlotPoint& point) override {
66  const PlotPoint p = matrix.transformPoint(point);
67  dc.DrawCircle(int(p.x), int(p.y), pointSize);
68  }
69 
70  virtual void drawLine(const PlotPoint& from, const PlotPoint& to) override {
71  const PlotPoint p1 = matrix.transformPoint(from);
72  const PlotPoint p2 = matrix.transformPoint(to);
73  dc.DrawLine({ int(p1.x), int(p1.y) }, { int(p2.x), int(p2.y) });
74  }
75 
76  virtual AutoPtr<IDrawPath> drawPath() override {
77  return makeAuto<SvgPath>(dc, matrix);
78  }
79 
80  virtual void setStyle(const Size UNUSED(index)) override {
81  // currently not implemented
82  }
83 
84  virtual void setTransformMatrix(const AffineMatrix2& newMatrix) override {
85  matrix = newMatrix;
86  }
87 };
88 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
Object representing a path on a filesystem, similar to std::filesystem::path in c++17.
Drawing quantity values as functions of time or spatial coordinates.
2D affine matrix
Definition: Point.h:50
PlotPoint transformPoint(const PlotPoint &p) const
Applies the affine transform on given point.
Definition: Point.h:92
Wrapper of pointer that deletes the resource from destructor.
Definition: AutoPtr.h:15
Definition: Plot.h:19
Abstraction of a drawing context.
Definition: Plot.h:34
Object representing a path on a filesystem.
Definition: Path.h:17
virtual void drawErrorPoint(const ErrorPlotPoint &point) override
Adds a point with error bars to the plot.
Definition: SvgContext.h:65
SvgContext(const Path &path, const Pixel size, const double dpi=72)
Definition: SvgContext.h:57
virtual void setTransformMatrix(const AffineMatrix2 &newMatrix) override
Applies the given tranformation matrix on all primitives.
Definition: SvgContext.h:84
virtual void drawPoint(const PlotPoint &point) override
Adds a single point to the plot.
Definition: SvgContext.h:60
virtual void drawLine(const PlotPoint &from, const PlotPoint &to) override
Draws a line connecting two points.
Definition: SvgContext.h:70
virtual void setStyle(const Size UNUSED(index)) override
Definition: SvgContext.h:80
virtual AutoPtr< IDrawPath > drawPath() override
Draws a path connecting points.
Definition: SvgContext.h:76
virtual void closePath() override
Closes the path, connecting to the first point on the path.
Definition: SvgContext.h:39
SvgPath(wxSVGFileDC &dc, const AffineMatrix2 &matrix)
Definition: SvgContext.h:24
virtual void addPoint(const PlotPoint &point) override
Adds a next point on the path.
Definition: SvgContext.h:28
virtual void endPath() override
Finalizes the path. Does not connect the last point to anything.
Definition: SvgContext.h:45
Simple 2D vector with integer coordinates. Provides conversion from and to wxPoint.
Point with error bars.
Definition: Point.h:43
Definition: Point.h:101
Point in 2D plot.
Definition: Point.h:16
Float y
Definition: Point.h:17
Float x
Definition: Point.h:17