Garfield 0.3
Toolkit for the detailed simulation of particle detectors based on ionization measurement in gases and semiconductors
Loading...
Searching...
No Matches
ViewBase.hh
Go to the documentation of this file.
1#ifndef G_VIEW_BASE
2#define G_VIEW_BASE
3
4#include <array>
5#include <memory>
6#include <string>
7
8// #include <TPad.h>
9#include <TCanvas.h>
10
11class TPad;
12class TVirtualPad;
13
14namespace Garfield {
15
16class Sensor;
17class Component;
18
20
21class ViewBase {
22 public:
24 ViewBase() = delete;
26 ViewBase(const std::string& name);
28 virtual ~ViewBase() = default;
29
31 void SetCanvas(TPad* pad) { m_pad = pad; }
33 void SetCanvas() { m_pad = nullptr; }
35 TPad* GetCanvas();
36
39 void SetArea(const double xmin, const double ymin, const double xmax,
40 const double ymax);
42 virtual void SetArea(const double xmin, const double ymin, const double zmin,
43 const double xmax, const double ymax, const double zmax);
46 void SetArea() {
47 m_userBox = false;
48 m_userPlotLimits = false;
49 }
50
55 virtual void SetPlane(const double fx, const double fy, const double fz,
56 const double x0, const double y0, const double z0);
58 virtual void SetPlane(const double fx, const double fy, const double fz,
59 const double x0, const double y0, const double z0,
60 const double hx, const double hy, const double hz);
62 void Rotate(const double angle);
64 void SetPlaneXY();
66 void SetPlaneXZ();
68 void SetPlaneYZ();
70 void SetPlaneZX();
72 void SetPlaneZY();
73
75 void EnableDebugging(const bool on = true) { m_debug = on; }
76
78 static std::string FindUnusedFunctionName(const std::string& s);
80 static std::string FindUnusedHistogramName(const std::string& s);
82 static std::string FindUnusedCanvasName(const std::string& s);
83
84 protected:
85 std::string m_className = "ViewBase";
86
87 // Options
88 bool m_debug = false;
89
90 // Plot axis limits.
91 bool m_userPlotLimits = false;
92 double m_xMinPlot = -1., m_xMaxPlot = 1.;
93 double m_yMinPlot = -1., m_yMaxPlot = 1.;
94
95 // Bounding box.
96 bool m_userBox = false;
97 double m_xMinBox = -1., m_xMaxBox = 1.;
98 double m_yMinBox = -1., m_yMaxBox = 1.;
99 double m_zMinBox = -1., m_zMaxBox = 1.;
100
101 // Viewing plane (FPROJ).
102 // Default projection: x-y at z = 0.
103 std::array<std::array<double, 3>, 3> m_proj{
104 {{{1, 0, 0}}, {{0, 1, 0}}, {{0, 0, 0}}}};
105 std::array<double, 4> m_plane{{0, 0, 1, 0}};
106 // Matrix used for projections (FPRMAT).
107 std::array<std::array<double, 3>, 3> m_prmat{
108 {{{1, 0, 0}}, {{0, 1, 0}}, {{0, 0, 1}}}};
109
110 // Update and invert the projection matrix.
112 // Determine plane coordinates.
113 template <typename T>
114 void ToPlane(const T x, const T y, const T z, T& xp, T& yp) const {
115 xp = m_prmat[0][0] * x + m_prmat[0][1] * y + m_prmat[0][2] * z;
116 yp = m_prmat[1][0] * x + m_prmat[1][1] * y + m_prmat[1][2] * z;
117 }
118 // Determine whether a point is inside the bounding box.
119 template <typename T>
120 bool InBox(const std::array<T, 3>& x) const {
121 if (!m_userBox) return true;
122 if (x[0] < m_xMinBox || x[0] > m_xMaxBox || x[1] < m_yMinBox ||
123 x[1] > m_yMaxBox || x[2] < m_yMinBox || x[2] > m_zMaxBox)
124 return false;
125 return true;
126 }
127 // Clip the line x0 - x1 to the extent of the bounding box.
128 void Clip(const std::array<float, 3>& x0, const std::array<float, 3>& x1,
129 std::array<float, 3>& xc) const;
130 // Draw the projection of a line onto the current viewing plane.
131 void DrawLine(const std::vector<std::array<float, 3> >& xl, const short col,
132 const short lw);
133
134 // X-axis label for the current viewing plane.
135 std::string LabelX();
136 // Y-axis label for the current viewing plane.
137 std::string LabelY();
138 // Description of the current viewing plane.
139 std::string PlaneDescription();
140
141 bool PlotLimits(Sensor* sensor, double& xmin, double& ymin, double& xmax,
142 double& ymax) const;
143 bool PlotLimits(Component* cmp, double& xmin, double& ymin, double& xmax,
144 double& ymax) const;
145 bool PlotLimitsFromUserBox(double& xmin, double& ymin, double& xmax,
146 double& ymax) const;
147 bool PlotLimits(std::array<double, 3>& bbmin, std::array<double, 3>& bbmax,
148 double& xmin, double& ymin, double& xmax, double& ymax) const;
149
150 static bool RangeSet(TVirtualPad*);
151 static void SetRange(TVirtualPad* pad, const double x0, const double y0,
152 const double x1, const double y1);
153
154 private:
155 // Current pad.
156 TPad* m_pad = nullptr;
157 std::unique_ptr<TCanvas> m_canvas;
158};
159} // namespace Garfield
160#endif
std::array< double, 4 > m_plane
Definition ViewBase.hh:105
void EnableDebugging(const bool on=true)
Switch on/off debugging output.
Definition ViewBase.hh:75
std::string LabelY()
std::string LabelX()
std::array< std::array< double, 3 >, 3 > m_prmat
Definition ViewBase.hh:107
std::unique_ptr< TCanvas > m_canvas
Definition ViewBase.hh:157
void Clip(const std::array< float, 3 > &x0, const std::array< float, 3 > &x1, std::array< float, 3 > &xc) const
static void SetRange(TVirtualPad *pad, const double x0, const double y0, const double x1, const double y1)
static std::string FindUnusedCanvasName(const std::string &s)
Find an unused canvas name.
bool InBox(const std::array< T, 3 > &x) const
Definition ViewBase.hh:120
static bool RangeSet(TVirtualPad *)
void DrawLine(const std::vector< std::array< float, 3 > > &xl, const short col, const short lw)
void SetCanvas(TPad *pad)
Set the canvas to be painted on.
Definition ViewBase.hh:31
std::string m_className
Definition ViewBase.hh:85
void SetPlaneZX()
Set the viewing plane to z-x.
void SetPlaneYZ()
Set the viewing plane to y-z.
static std::string FindUnusedHistogramName(const std::string &s)
Find an unused histogram name.
virtual void SetArea(const double xmin, const double ymin, const double zmin, const double xmax, const double ymax, const double zmax)
Set a bounding box (if applicable).
bool PlotLimits(std::array< double, 3 > &bbmin, std::array< double, 3 > &bbmax, double &xmin, double &ymin, double &xmax, double &ymax) const
std::array< std::array< double, 3 >, 3 > m_proj
Definition ViewBase.hh:103
void SetArea()
Use default x- and y-axis limits (based on the bounding box of the sensor/component,...
Definition ViewBase.hh:46
bool PlotLimits(Component *cmp, double &xmin, double &ymin, double &xmax, double &ymax) const
TPad * GetCanvas()
Retrieve the canvas.
void SetPlaneXZ()
Set the viewing plane to x-z.
virtual void SetPlane(const double fx, const double fy, const double fz, const double x0, const double y0, const double z0)
Set the projection (viewing plane), if applicable.
void SetArea(const double xmin, const double ymin, const double xmax, const double ymax)
Set the x- and y-axis limits (in local coordinates of the current viewing plane, if applicable).
bool PlotLimits(Sensor *sensor, double &xmin, double &ymin, double &xmax, double &ymax) const
void SetPlaneXY()
Set the viewing plane to x-y.
virtual void SetPlane(const double fx, const double fy, const double fz, const double x0, const double y0, const double z0, const double hx, const double hy, const double hz)
Set the projection plane specifying a hint for the in-plane x axis.
virtual ~ViewBase()=default
Destructor.
static std::string FindUnusedFunctionName(const std::string &s)
Find an unused function name.
void ToPlane(const T x, const T y, const T z, T &xp, T &yp) const
Definition ViewBase.hh:114
void SetCanvas()
Unset an external canvas.
Definition ViewBase.hh:33
std::string PlaneDescription()
void SetPlaneZY()
Set the viewing plane to z-y.
ViewBase(const std::string &name)
Constructor.
bool PlotLimitsFromUserBox(double &xmin, double &ymin, double &xmax, double &ymax) const
void Rotate(const double angle)
Rotate the viewing plane (angle in radian).
ViewBase()=delete
Default constructor.
void UpdateProjectionMatrix()