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:
27 #
28 ###############################################################################
29 */
30 
31 
38 #ifndef EGS_VECTOR_
39 #define EGS_VECTOR_
40 
41 #include "egs_functions.h"
42 #include "egs_libconfig.h"
43 
44 #include "egs_math.h"
45 
57 
58 public:
59 
60  EGS_Float x;
61  EGS_Float y;
62  EGS_Float z;
63 
64  EGS_Vector(EGS_Float xx, EGS_Float yy, EGS_Float zz) : x(xx), y(yy), z(zz) {};
65  EGS_Vector(const EGS_Vector &v) : x(v.x), y(v.y), z(v.z) {};
66  EGS_Vector() : x(0), y(0), z(0) {};
67 
68  EGS_Vector &operator=(const EGS_Vector &v) {
69  x = v.x;
70  y = v.y;
71  z = v.z;
72  return *this;
73  };
74 
75  // EGS_Vector additions
76  //
77  EGS_Vector operator+(const EGS_Vector &v) const {
78  return EGS_Vector(x+v.x, y+v.y, z+v.z);
79  };
80  EGS_Vector &operator+=(const EGS_Vector &v) {
81  x += v.x;
82  y += v.y;
83  z += v.z;
84  return *this;
85  };
86 
87  // EGS_Vector subtractions
88  //
89  EGS_Vector operator-(const EGS_Vector &v) const {
90  return EGS_Vector(x-v.x,y-v.y,z-v.z);
91  }
92  EGS_Vector &operator-=(const EGS_Vector &v) {
93  x -= v.x;
94  y -= v.y;
95  z -= v.z;
96  return *this;
97  };
98 
99  // EGS_Vector multiplications
100  //
101  EGS_Vector operator*(const EGS_Float f) const {
102  return EGS_Vector(x*f,y*f,z*f);
103  };
104  EGS_Vector &operator*=(const EGS_Float f) {
105  x *= f;
106  y *= f;
107  z *= f;
108  return *this;
109  };
110  friend EGS_Vector operator*(EGS_Float f, const EGS_Vector &v) {
111  return v*f;
112  };
113  EGS_Float operator*(const EGS_Vector &v) const {
114  return x*v.x + y*v.y + z*v.z;
115  };
116 
117  // vector product
118  EGS_Vector times(const EGS_Vector &v) const {
119  return EGS_Vector(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
120  };
121  EGS_Vector operator%(const EGS_Vector &v) const {
122  return EGS_Vector(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
123  };
124 
125  // scale
126  EGS_Vector getScaled(const EGS_Vector &s) const {
127  return EGS_Vector(x*s.x,y*s.y,z*s.z);
128  };
129  void scale(const EGS_Vector &s) {
130  x *= s.x;
131  y *= s.y;
132  z *= s.z;
133  };
134 
135  // Some other useful methods.
136  EGS_Float length() const {
137  return sqrt(x*x+y*y+z*z);
138  };
139  EGS_Float length2() const {
140  return x*x+y*y+z*z;
141  };
142  void normalize() {
143  EGS_Float tmp = 1./length();
144  x *= tmp;
145  y *= tmp;
146  z *= tmp;
147  };
148 
149  void rotate(EGS_Float cos_t, EGS_Float sin_t,
150  EGS_Float c_phi, EGS_Float s_phi) {
151  EGS_Float sin_z = x*x + y*y;
152  if (sin_z > epsilon) {
153  sin_z = sqrt(sin_z);
154  EGS_Float temp = sin_t/sin_z;
155  EGS_Float temp_phi = z*c_phi;
156  EGS_Float temp_x = x*cos_t;
157  register EGS_Float temp_y = y*cos_t;
158  EGS_Float temp_x1 = temp_phi*x-y*s_phi;
159  EGS_Float temp_y1 = temp_phi*y+x*s_phi;
160  x = temp*temp_x1+temp_x;
161  y = temp*temp_y1+temp_y;
162  z = z*cos_t-sin_z*sin_t*c_phi;
163  }
164  else {
165  x = sin_t*c_phi;
166  y = sin_t*s_phi;
167  z *= cos_t;
168  }
169  };
170 
171 };
172 
173 #endif
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
EGS_Float y
y-component
Definition: egs_vector.h:61
A class representing 3D vectors.
Definition: egs_vector.h:56
Global egspp functions header file.
EGS_Float z
z-component
Definition: egs_vector.h:62
Attempts to fix broken math header files.
EGS_Float x
x-component
Definition: egs_vector.h:60
const EGS_Float epsilon
The epsilon constant for floating point comparisons.
Definition: egs_functions.h:61
Defines the EGS_EXPORT and EGS_LOCAL macros.