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) Royal Holloway,
3University of London 2001 - 2023.
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 BDSTHREEVECTOR_H
20#define BDSTHREEVECTOR_H
21#include "BDSException.hh"
22
23#include "globals.hh" // geant4 types / globals
24
25#include <cmath>
26#include <ostream>
27
43template <typename T>
45{
46public:
49 {values[0] = 0; values[1] = 0; values[2] = 0;}
50
52 BDSThreeVector(T xIn, T yIn, T zIn)
53 {values[0] = xIn; values[1] = yIn; values[2] = zIn;}
54
57 {
58 for (G4int i : {0, 1, 2})
59 {values[i] = other.values[i];}
60 }
61
64 {
65 if (&rhs != this)
66 {
67 for (G4int i : {0, 1, 2})
68 {values[i] = rhs.values[i];}
69 }
70 return *this;
71 }
72
74 T& operator[](const G4int index)
75 {
76 if (index > 2)
77 {throw BDSException("BDSThreeVector::operator[]> index outside array");}
78 return values[index];
79 }
80
82 const T& operator[](const G4int index) const
83 {
84 if (index > 2)
85 {throw BDSException("BDSThreeVector::operator[]> index outside array");}
86 return values[index];
87 }
88
90 inline const T& x() const {return values[0];}
91 inline const T& y() const {return values[1];}
92 inline const T& z() const {return values[2];}
94
96 friend std::ostream& operator<< (std::ostream& out, BDSThreeVector const &v)
97 {out << "(" << v.values[0] << ", " << v.values[1] << ", " << v.values[2] << ")"; return out;}
98
99 T dot(const BDSThreeVector& b) const
100 {return x()*b.x() + y()*b.y() + z()*b.z();}
101
103 inline T mag() const {return std::sqrt(values[0]*values[0] + values[1]*values[1] + values[2]*values[2]);}
104
106 inline friend BDSThreeVector operator * (const BDSThreeVector& a, const double& b)
107 {return BDSThreeVector(b*a.x(), b*a.y(), b*a.z());}
108 inline friend BDSThreeVector operator * (const double& b, const BDSThreeVector& a)
109 {return BDSThreeVector(b*a.x(), b*a.y(), b*a.z());}
110 inline friend T operator * (const BDSThreeVector& a, const BDSThreeVector& b)
111 {return a.dot(b);}
113
115 inline friend BDSThreeVector operator + (const BDSThreeVector& a, const G4double& b)
116 {return BDSThreeVector(a.x() + b, a.y() + b, a.z() + b);}
117 inline friend BDSThreeVector operator + (const BDSThreeVector& a, const BDSThreeVector& b)
118 {return BDSThreeVector(a.x() + b.x(), a.y() + b.y(), a.z() + b.z());}
119 inline friend BDSThreeVector operator - (const BDSThreeVector& a, const G4double& b)
120 {return BDSThreeVector(a.x() - b, a.y() - b, a.z() - b);}
121 inline friend BDSThreeVector operator - (const BDSThreeVector& a, const BDSThreeVector& b)
122 {return BDSThreeVector(a.x() - b.x(), a.y() - b.y(), a.z() - b.z());}
124
125 inline BDSThreeVector& operator *= (G4double a)
126 {
127 values[0] *= a;
128 values[1] *= a;
129 values[2] *= a;
130 return *this;
131 }
132
133private:
135 T values[3];
136};
137
138#endif
General exception with possible name of object and message.
Definition: BDSException.hh:35
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.