Garfield++ 5.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 /// Set the viewing plane to z-x.
67 void SetPlaneZX();
68 /// Set the viewing plane to z-y.
69 void SetPlaneZY();
70
71 /// Switch on/off debugging output.
72 void EnableDebugging(const bool on = true) { m_debug = on; }
73
74 /// Find an unused function name.
75 static std::string FindUnusedFunctionName(const std::string& s);
76 /// Find an unused histogram name.
77 static std::string FindUnusedHistogramName(const std::string& s);
78 /// Find an unused canvas name.
79 static std::string FindUnusedCanvasName(const std::string& s);
80
81 protected:
82 std::string m_className = "ViewBase";
83
84 // Options
85 bool m_debug = false;
86
87 // Plot axis limits.
88 bool m_userPlotLimits = false;
89 double m_xMinPlot = -1., m_xMaxPlot = 1.;
90 double m_yMinPlot = -1., m_yMaxPlot = 1.;
91
92 // Bounding box.
93 bool m_userBox = false;
94 double m_xMinBox = -1., m_xMaxBox = 1.;
95 double m_yMinBox = -1., m_yMaxBox = 1.;
96 double m_zMinBox = -1., m_zMaxBox = 1.;
97
98 // Viewing plane (FPROJ).
99 // Default projection: x-y at z = 0.
100 std::array<std::array<double, 3>, 3> m_proj{{
101 {{1, 0, 0}}, {{0, 1, 0}}, {{0, 0, 0}}
102 }};
103 std::array<double, 4> m_plane{{0, 0, 1, 0}};
104 // Matrix used for projections (FPRMAT).
105 std::array<std::array<double, 3>, 3> m_prmat{{
106 {{1, 0, 0}}, {{0, 1, 0}}, {{0, 0, 1}}
107 }};
108
109 // Update and invert the projection matrix.
111 // Determine plane coordinates.
112 template<typename T>
113 void ToPlane(const T x, const T y, const T z, T& xp, T& yp) const {
114 xp = m_prmat[0][0] * x + m_prmat[0][1] * y + m_prmat[0][2] * z;
115 yp = m_prmat[1][0] * x + m_prmat[1][1] * y + m_prmat[1][2] * z;
116 }
117 // Determine whether a point is inside the bounding box.
118 template<typename T>
119 bool InBox(const std::array<T, 3>& x) const {
120 if (!m_userBox) return true;
121 if (x[0] < m_xMinBox || x[0] > m_xMaxBox ||
122 x[1] < m_yMinBox || x[1] > m_yMaxBox ||
123 x[2] < m_yMinBox || x[2] > m_zMaxBox) return false;
124 return true;
125 }
126 // Clip the line x0 - x1 to the extent of the bounding box.
127 void Clip(const std::array<float, 3>& x0,
128 const std::array<float, 3>& x1, std::array<float, 3>& xc) const;
129 // Draw the projection of a line onto the current viewing plane.
130 void DrawLine(const std::vector<std::array<float, 3> >& xl,
131 const short col, const short lw);
132
133 // X-axis label for the current viewing plane.
134 std::string LabelX();
135 // Y-axis label for the current viewing plane.
136 std::string LabelY();
137 // Description of the current viewing plane.
138 std::string PlaneDescription();
139
140 bool PlotLimits(Sensor* sensor, double& xmin, double& ymin,
141 double& xmax, double& ymax) const;
142 bool PlotLimits(Component* cmp, double& xmin, double& ymin,
143 double& xmax, double& ymax) const;
144 bool PlotLimitsFromUserBox(double& xmin, double& ymin,
145 double& xmax, double& ymax) const;
146 bool PlotLimits(std::array<double, 3>& bbmin,
147 std::array<double, 3>& bbmax,
148 double& xmin, double& ymin,
149 double& xmax, double& ymax) const;
150
151 static bool RangeSet(TVirtualPad*);
152 static void SetRange(TVirtualPad* pad, const double x0, const double y0,
153 const double x1, const double y1);
154 private:
155 // Current pad.
156 TPad* m_pad = nullptr;
157 std::unique_ptr<TCanvas> m_canvas;
158};
159}
160#endif
Abstract base class for components.
Definition Component.hh:13
std::array< double, 4 > m_plane
Definition ViewBase.hh:103
void EnableDebugging(const bool on=true)
Switch on/off debugging output.
Definition ViewBase.hh:72
std::string LabelY()
Definition ViewBase.cc:510
static void SetRange(TVirtualPad *pad, const double x0, const double y0, const double x1, const double y1)
Definition ViewBase.cc:93
std::string LabelX()
Definition ViewBase.cc:447
std::array< std::array< double, 3 >, 3 > m_prmat
Definition ViewBase.hh:105
void Clip(const std::array< float, 3 > &x0, const std::array< float, 3 > &x1, std::array< float, 3 > &xc) const
Definition ViewBase.cc:374
bool InBox(const std::array< T, 3 > &x) const
Definition ViewBase.hh:119
void DrawLine(const std::vector< std::array< float, 3 > > &xl, const short col, const short lw)
Definition ViewBase.cc:398
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:327
std::string m_className
Definition ViewBase.hh:82
void SetPlaneZX()
Set the viewing plane to z-x.
Definition ViewBase.cc:305
void SetPlaneYZ()
Set the viewing plane to y-z.
Definition ViewBase.cc:299
static std::string FindUnusedCanvasName(const std::string &s)
Find an unused canvas name.
Definition ViewBase.cc:337
std::array< std::array< double, 3 >, 3 > m_proj
Definition ViewBase.hh:100
TPad * GetCanvas()
Retrieve the canvas.
Definition ViewBase.cc:74
static bool RangeSet(TVirtualPad *)
Definition ViewBase.cc:83
void SetPlaneXZ()
Set the viewing plane to x-z.
Definition ViewBase.cc:293
bool PlotLimits(Sensor *sensor, double &xmin, double &ymin, double &xmax, double &ymax) const
Definition ViewBase.cc:635
void SetPlaneXY()
Set the viewing plane to x-y.
Definition ViewBase.cc:287
virtual ~ViewBase()=default
Destructor.
void ToPlane(const T x, const T y, const T z, T &xp, T &yp) const
Definition ViewBase.hh:113
void SetCanvas()
Unset an external canvas.
Definition ViewBase.hh:30
std::string PlaneDescription()
Definition ViewBase.cc:572
void SetPlaneZY()
Set the viewing plane to z-y.
Definition ViewBase.cc:311
bool PlotLimitsFromUserBox(double &xmin, double &ymin, double &xmax, double &ymax) const
Definition ViewBase.cc:690
static std::string FindUnusedFunctionName(const std::string &s)
Find an unused function name.
Definition ViewBase.cc:317
void Rotate(const double angle)
Rotate the viewing plane (angle in radian).
Definition ViewBase.cc:270
ViewBase()=delete
Default constructor.
void UpdateProjectionMatrix()
Definition ViewBase.cc:347
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