BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSThreeVector.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 BDSTHREEVECTOR_H
19#define BDSTHREEVECTOR_H
20#include "BDSException.hh"
21
22#include "globals.hh" // geant4 types / globals
23
24#include <cmath>
25#include <ostream>
26
42template <typename T>
44{
45public:
48 {values[0] = 0; values[1] = 0; values[2] = 0;}
49
51 BDSThreeVector(T xIn, T yIn, T zIn)
52 {values[0] = xIn; values[1] = yIn; values[2] = zIn;}
53
56 {
57 for (G4int i : {0, 1, 2})
58 {values[i] = other.values[i];}
59 }
60
63 {
64 if (&rhs != this)
65 {
66 for (G4int i : {0, 1, 2})
67 {values[i] = rhs.values[i];}
68 }
69 return *this;
70 }
71
73 T& operator[](const G4int index)
74 {
75 if (index > 2)
76 {throw BDSException("BDSThreeVector::operator[]> index outside array");}
77 return values[index];
78 }
79
81 const T& operator[](const G4int index) const
82 {
83 if (index > 2)
84 {throw BDSException("BDSThreeVector::operator[]> index outside array");}
85 return values[index];
86 }
87
89 inline const T& x() const {return values[0];}
90 inline const T& y() const {return values[1];}
91 inline const T& z() const {return values[2];}
93
95 friend std::ostream& operator<< (std::ostream& out, BDSThreeVector const &v)
96 {out << "(" << v.values[0] << ", " << v.values[1] << ", " << v.values[2] << ")"; return out;}
97
98 T dot(const BDSThreeVector& b) const
99 {return x()*b.x() + y()*b.y() + z()*b.z();}
100
102 inline T mag() const {return std::sqrt(values[0]*values[0] + values[1]*values[1] + values[2]*values[2]);}
103
105 inline friend BDSThreeVector operator * (const BDSThreeVector& a, const double& b)
106 {return BDSThreeVector(b*a.x(), b*a.y(), b*a.z());}
107 inline friend BDSThreeVector operator * (const double& b, const BDSThreeVector& a)
108 {return BDSThreeVector(b*a.x(), b*a.y(), b*a.z());}
109 inline friend T operator * (const BDSThreeVector& a, const BDSThreeVector& b)
110 {return a.dot(b);}
112
114 inline friend BDSThreeVector operator + (const BDSThreeVector& a, const G4double& b)
115 {return BDSThreeVector(a.x() + b, a.y() + b, a.z() + b);}
116 inline friend BDSThreeVector operator + (const BDSThreeVector& a, const BDSThreeVector& b)
117 {return BDSThreeVector(a.x() + b.x(), a.y() + b.y(), a.z() + b.z());}
118 inline friend BDSThreeVector operator - (const BDSThreeVector& a, const G4double& b)
119 {return BDSThreeVector(a.x() - b, a.y() - b, a.z() - b);}
120 inline friend BDSThreeVector operator - (const BDSThreeVector& a, const BDSThreeVector& b)
121 {return BDSThreeVector(a.x() - b.x(), a.y() - b.y(), a.z() - b.z());}
123
124 inline BDSThreeVector& operator *= (G4double a)
125 {
126 values[0] *= a;
127 values[1] *= a;
128 values[2] *= a;
129 return *this;
130 }
131
132private:
134 T values[3];
135};
136
137#endif
General exception with possible name of object and message.
Simple three vector that's templated so the right type can be used.
BDSThreeVector()
Default constructor.
BDSThreeVector(const BDSThreeVector &other)
Copy constructor.
BDSThreeVector & operator=(const BDSThreeVector &rhs)
Assignment operator.
T values[3]
Member data - x,y,z.
T & operator[](const G4int index)
Access / set a single element.
friend BDSThreeVector operator*(const BDSThreeVector &a, const double &b)
Multiplication.
T mag() const
Get the magnitude of it.
friend std::ostream & operator<<(std::ostream &out, BDSThreeVector const &v)
Output stream.
const T & z() const
Accessor by name.
const T & x() const
Accessor by name.
friend BDSThreeVector operator+(const BDSThreeVector &a, const G4double &b)
Addition, subtraction.
const T & y() const
Accessor by name.
const T & operator[](const G4int index) const
Access a single element.
BDSThreeVector(T xIn, T yIn, T zIn)
Alternate constructor.
friend BDSThreeVector operator-(const BDSThreeVector &a, const G4double &b)
Addition, subtraction.