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