EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_vector.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ vector headers
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2005
25 #
26 # Contributors: Manuel Stoeckl
27 # Reid Townson
28 #
29 ###############################################################################
30 */
31 
32 
39 #ifndef EGS_VECTOR_
40 #define EGS_VECTOR_
41 
42 #include "egs_functions.h"
43 #include "egs_libconfig.h"
44 
45 #include "egs_math.h"
46 
58 
59 public:
60 
61  EGS_Float x;
62  EGS_Float y;
63  EGS_Float z;
64 
65  EGS_Vector(EGS_Float xx, EGS_Float yy, EGS_Float zz) : x(xx), y(yy), z(zz) {};
66  EGS_Vector(const EGS_Vector &v) : x(v.x), y(v.y), z(v.z) {};
67  EGS_Vector() : x(0), y(0), z(0) {};
68 
69  EGS_Vector &operator=(const EGS_Vector &v) {
70  x = v.x;
71  y = v.y;
72  z = v.z;
73  return *this;
74  };
75 
76  // EGS_Vector additions
77  //
78  EGS_Vector operator+(const EGS_Vector &v) const {
79  return EGS_Vector(x+v.x, y+v.y, z+v.z);
80  };
81  EGS_Vector &operator+=(const EGS_Vector &v) {
82  x += v.x;
83  y += v.y;
84  z += v.z;
85  return *this;
86  };
87 
88  // EGS_Vector subtractions
89  //
90  EGS_Vector operator-(const EGS_Vector &v) const {
91  return EGS_Vector(x-v.x,y-v.y,z-v.z);
92  }
93  EGS_Vector &operator-=(const EGS_Vector &v) {
94  x -= v.x;
95  y -= v.y;
96  z -= v.z;
97  return *this;
98  };
99 
100  // EGS_Vector multiplications
101  //
102  EGS_Vector operator*(const EGS_Float f) const {
103  return EGS_Vector(x*f,y*f,z*f);
104  };
105  EGS_Vector &operator*=(const EGS_Float f) {
106  x *= f;
107  y *= f;
108  z *= f;
109  return *this;
110  };
111  friend EGS_Vector operator*(EGS_Float f, const EGS_Vector &v) {
112  return v*f;
113  };
114  EGS_Float operator*(const EGS_Vector &v) const {
115  return x*v.x + y*v.y + z*v.z;
116  };
117 
118  // vector product
119  EGS_Vector times(const EGS_Vector &v) const {
120  return EGS_Vector(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
121  };
122  EGS_Vector operator%(const EGS_Vector &v) const {
123  return EGS_Vector(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
124  };
125 
126  // scale
127  EGS_Vector getScaled(const EGS_Vector &s) const {
128  return EGS_Vector(x*s.x,y*s.y,z*s.z);
129  };
130  void scale(const EGS_Vector &s) {
131  x *= s.x;
132  y *= s.y;
133  z *= s.z;
134  };
135 
136  // Some other useful methods.
137  EGS_Float length() const {
138  return sqrt(x*x+y*y+z*z);
139  };
140  EGS_Float length2() const {
141  return x*x+y*y+z*z;
142  };
143  void normalize() {
144  EGS_Float tmp = 1./length();
145  x *= tmp;
146  y *= tmp;
147  z *= tmp;
148  };
149 
150  void rotate(EGS_Float cos_t, EGS_Float sin_t,
151  EGS_Float c_phi, EGS_Float s_phi) {
152  EGS_Float sin_z = x*x + y*y;
153  if (sin_z > epsilon) {
154  sin_z = sqrt(sin_z);
155  EGS_Float temp = sin_t/sin_z;
156  EGS_Float temp_phi = z*c_phi;
157  EGS_Float temp_x = x*cos_t;
158  register EGS_Float temp_y = y*cos_t;
159  EGS_Float temp_x1 = temp_phi*x-y*s_phi;
160  EGS_Float temp_y1 = temp_phi*y+x*s_phi;
161  x = temp*temp_x1+temp_x;
162  y = temp*temp_y1+temp_y;
163  z = z*cos_t-sin_z*sin_t*c_phi;
164  }
165  else {
166  x = sin_t*c_phi;
167  y = sin_t*s_phi;
168  z *= cos_t;
169  }
170  };
171 
172 };
173 
174 #endif
A class representing 3D vectors.
Definition: egs_vector.h:57
EGS_Float y
y-component
Definition: egs_vector.h:62
EGS_Float z
z-component
Definition: egs_vector.h:63
EGS_Float x
x-component
Definition: egs_vector.h:61
Global egspp functions header file.
Defines the EGS_EXPORT and EGS_LOCAL macros.
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
Attempts to fix broken math header files.
const EGS_Float epsilon
The epsilon constant for floating point comparisons.
Definition: egs_functions.h:62