Garfield++ v1r0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
SolidBox.cc
Go to the documentation of this file.
1#include <iostream>
2#include <cmath>
3
4#include "SolidBox.hh"
7
8namespace Garfield {
9
10SolidBox::SolidBox(const double& cx, const double& cy, const double& cz,
11 const double& lx, const double& ly, const double& lz)
12 : Solid(),
13 m_cX(cx), m_cY(cy), m_cZ(cz),
14 m_lX(lx), m_lY(ly), m_lZ(lz),
15 m_dX(0.), m_dY(0.), m_dZ(1.),
16 m_cPhi(1.), m_sPhi(0.),
17 m_cTheta(1.), m_sTheta(0.) {
18
19 std::cout << "SolidBox:\n";
20 std::cout << " " << cx - lx << " < x [cm] < " << cx + lx << "\n";
21 std::cout << " " << cy - ly << " < y [cm] < " << cy + ly << "\n";
22 std::cout << " " << cz - lz << " < z [cm] < " << cz + lz << "\n";
23}
24
25SolidBox::SolidBox(const double& cx, const double& cy, const double& cz,
26 const double& lx, const double& ly, const double& lz,
27 const double& dx, const double& dy, const double& dz)
28 : Solid(),
29 m_cX(cx), m_cY(cy), m_cZ(cz),
30 m_lX(lx), m_lY(ly), m_lZ(lz),
31 m_dX(0.), m_dY(0.), m_dZ(1.),
32 m_cPhi(1.), m_sPhi(0.),
33 m_cTheta(1.), m_sTheta(0.) {
34
35 const double d = sqrt(dx * dx + dy * dy + dz * dz);
36 if (d < Small) {
37 std::cerr << "SolidBox: Direction vector has zero norm.\n";
38 } else {
39 m_dX = dx / d;
40 m_dY = dy / d;
41 m_dZ = dz / d;
42 double phi, theta;
43 const double dt = sqrt(m_dX * m_dX + m_dY * m_dY);
44 if (dt < Small) {
45 phi = 0.;
46 if (m_dZ > 0.) {
47 theta = 0.;
48 } else {
49 theta = Pi;
50 }
51 } else {
52 phi = atan2(m_dY, m_dX);
53 theta = atan2(dt, m_dZ);
54 }
55 m_cTheta = cos(theta);
56 m_sTheta = sin(theta);
57 m_cPhi = cos(phi);
58 m_sPhi = sin(phi);
59 }
60}
61
62bool SolidBox::IsInside(const double& x, const double& y,
63 const double& z) const {
64
65 // Transform the point to local coordinates
66 const double dx = x - m_cX;
67 const double dy = y - m_cY;
68 const double dz = z - m_cZ;
69 const double u = m_cPhi * m_cTheta * dx + m_sPhi * m_cTheta * dy - m_sTheta * dz;
70 const double v = -m_sPhi * dx + m_cPhi * dy;
71 const double w = m_cPhi * m_sTheta * dx + m_sPhi * m_sTheta * dy + m_cTheta * dz;
72
73 // See whether the point is inside
74 if (fabs(u) > m_lX || fabs(v) > m_lY || fabs(w) > m_lZ) {
75 if (m_debug) {
76 std::cout << "SolidBox::IsInside:\n";
77 std::cout << " (" << x << ", " << y << ", " << z << ") "
78 << " is outside.\n";
79 }
80 return false;
81 }
82
83 if (m_debug) {
84 std::cout << "SolidBox::IsInside:\n";
85 std::cout << " (" << x << ", " << y << ", " << z << ") "
86 << " is inside.\n";
87 }
88
89 return true;
90}
91
92bool SolidBox::GetBoundingBox(double& xmin, double& ymin, double& zmin,
93 double& xmax, double& ymax, double& zmax) const {
94
95 if (m_cTheta == 1. && m_cPhi == 1.) {
96 xmin = m_cX - m_lX;
97 xmax = m_cX + m_lX;
98 ymin = m_cY - m_lY;
99 ymax = m_cY + m_lY;
100 zmin = m_cZ - m_lZ;
101 zmax = m_cZ + m_lZ;
102 return true;
103 }
104
105 const double dd = sqrt(m_lX * m_lX + m_lY * m_lY + m_lZ * m_lZ);
106 xmin = m_cX - dd;
107 xmax = m_cX + dd;
108 ymin = m_cY - dd;
109 ymax = m_cY + dd;
110 zmin = m_cZ - dd;
111 zmax = m_cZ + dd;
112 return true;
113}
114
115bool SolidBox::GetCenter(double& x, double& y, double& z) const {
116
117 x = m_cX;
118 y = m_cY;
119 z = m_cZ;
120 return true;
121}
122
123bool SolidBox::GetDimensions(double& l1, double& l2, double& l3) const {
124
125 l1 = m_lX;
126 l2 = m_lY;
127 l3 = m_lZ;
128 return true;
129}
130
131bool SolidBox::GetOrientation(double& ctheta, double& stheta, double& cphi,
132 double& sphi) const {
133
134 ctheta = m_cTheta;
135 stheta = m_sTheta;
136 cphi = m_cPhi;
137 sphi = m_sPhi;
138 return true;
139}
140
141void SolidBox::SetHalfLengthX(const double& lx) {
142
143 if (lx > 0.) {
144 m_lX = lx;
145 } else {
146 std::cerr << "SolidBox::SetHalfLengthX:\n";
147 std::cerr << " Half-length must be > 0.\n";
148 }
149}
150
151void SolidBox::SetHalfLengthY(const double& ly) {
152
153 if (ly > 0.) {
154 m_lY = ly;
155 } else {
156 std::cerr << "SolidBox::SetHalfLengthY:\n";
157 std::cerr << " Half-length must be > 0.\n";
158 }
159}
160
161void SolidBox::SetHalfLengthZ(const double& lz) {
162
163 if (lz > 0.) {
164 m_lZ = lz;
165 } else {
166 std::cerr << "SolidBox::SetHalfLengthZ:\n";
167 std::cerr << " Half-length must be > 0.\n";
168 }
169}
170}
DoubleAc cos(const DoubleAc &f)
Definition: DoubleAc.cpp:431
DoubleAc sqrt(const DoubleAc &f)
Definition: DoubleAc.cpp:313
DoubleAc sin(const DoubleAc &f)
Definition: DoubleAc.cpp:383
DoubleAc fabs(const DoubleAc &f)
Definition: DoubleAc.h:616
void SetHalfLengthY(const double &ly)
Definition: SolidBox.cc:151
void SetHalfLengthX(const double &lx)
Definition: SolidBox.cc:141
virtual bool GetOrientation(double &ctheta, double &stheta, double &cphi, double &sphi) const
Definition: SolidBox.cc:131
virtual bool GetDimensions(double &l1, double &l2, double &l3) const
Definition: SolidBox.cc:123
virtual bool IsInside(const double &x, const double &y, const double &z) const
Definition: SolidBox.cc:62
virtual bool GetCenter(double &x, double &y, double &z) const
Definition: SolidBox.cc:115
SolidBox(const double &cx, const double &cy, const double &cz, const double &lx, const double &ly, const double &lz)
Definition: SolidBox.cc:10
void SetHalfLengthZ(const double &lz)
Definition: SolidBox.cc:161
virtual bool GetBoundingBox(double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax) const
Definition: SolidBox.cc:92
bool m_debug
Definition: Solid.hh:35