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