BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSArrayOperatorIndexReflect.hh
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef BDSARRAYOPERATORCOORDREFLECT_H
20#define BDSARRAYOPERATORCOORDREFLECT_H
21#include "BDSArray4DCoords.hh"
22#include "BDSArrayInfo.hh"
23#include "BDSArrayOperatorIndex.hh"
24#include "BDSWarning.hh"
25
26#include "G4String.hh"
27#include "G4Types.hh"
28
29#include <array>
30#include <cmath>
31#include <string>
32
40{
41public:
43 BDSArrayOperatorIndex("Reflect(None)"),
44 xyzt{false, false, false, false},
45 zeroInArrayCoords{0,0,0,0},
46 dimensionInverted{false, false, false, false}
47 {;}
48 explicit BDSArrayOperatorIndexReflect(const std::array<G4bool,4>& xyztIn,
49 const BDSArrayInfo& arrayInfo):
51 {
52 operatesOn = xyztIn;
53 xyzt = xyztIn;
54 G4String newName = "Reflect(";
55 for (const auto& v : xyzt)
56 {newName += std::to_string(v);}
57 newName += ")";
58 name = newName;
59
60 auto zeroV = arrayInfo.zeroPoint;
61 auto nPoints = arrayInfo.nPoints;
62 for (G4int i = 0; i < 4; i++)
63 {
64 G4bool inverted = (zeroV[i] >= nPoints[i] - 1) && (nPoints[i] > 1);
65 dimensionInverted[i] = inverted;
66 zeroInArrayCoords[i] = inverted ? nPoints[i] - 1 : (G4int)std::floor(zeroV[i]);
67 }
68 }
70
71 virtual void Apply(G4int& x,
72 G4int& y,
73 G4int& z,
74 G4int& t) const
75 {
76 G4int* values[4] = {&x, &y, &z, &t};
77 for (G4int i = 0; i < 4; i++)
78 {
79 G4int v = *(values[i]);
80 if (dimensionInverted[i])
81 { *(values[i]) = xyzt[i] && v > zeroInArrayCoords[i] ? zeroInArrayCoords[i] - (v- zeroInArrayCoords[i]) : v;}
82 else
83 { *(values[i]) = xyzt[i] && v < zeroInArrayCoords[i] ? std::abs(v - zeroInArrayCoords[i]) : v; }
84 }
85 }
86
87 virtual void ApplyX(G4int& x) const {x = xyzt[0] ? std::abs(x) : x;}
88 virtual void ApplyY(G4int& y) const {y = xyzt[1] ? std::abs(y) : y;}
89 virtual void ApplyZ(G4int& z) const {z = xyzt[2] ? std::abs(z) : z;}
90 virtual void ApplyT(G4int& t) const {t = xyzt[3] ? std::abs(t) : t;}
91
92 virtual void TransformLimits(G4double& xMin, G4double& xMax,
93 G4double& yMin, G4double& yMax,
94 G4double& zMin, G4double& zMax,
95 G4double& tMin, G4double& tMax) const
96 {
97 xMin = -std::abs(xMax);
98 yMin = -std::abs(yMax);
99 zMin = -std::abs(zMax);
100 tMin = -std::abs(tMax);
101 }
102
103private:
104 std::array<G4bool,4> xyzt;
105 std::array<G4int, 4> zeroInArrayCoords;
106 std::array<G4bool,4> dimensionInverted;
107};
108
109#endif
Simple holder of information about an array.
Definition: BDSArrayInfo.hh:35
1D array for completeness in array system.
virtual void Apply(G4int &x, G4int &y, G4int &z, G4int &t) const
Interface for modifying by reference array indices.