BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
BasicVector3D.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Base/Vector/BasicVector3D.cpp
6 //! @brief Implements type-specific functions from template class BasicVector3D.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
17 #include "Base/Types/Exceptions.h"
18 
19 typedef std::complex<double> complex_t;
20 
21 // -----------------------------------------------------------------------------
22 // Quasi constructor
23 // -----------------------------------------------------------------------------
24 
25 BasicVector3D<double> vecOfLambdaAlphaPhi(double _lambda, double _alpha, double _phi)
26 {
27  double k = M_TWOPI / _lambda;
28  return BasicVector3D<double>(k * std::cos(_alpha) * std::cos(_phi),
29  -k * std::cos(_alpha) * std::sin(_phi), k * std::sin(_alpha));
30 }
31 
32 // -----------------------------------------------------------------------------
33 // Functions of this (with no further argument)
34 // -----------------------------------------------------------------------------
35 
36 //! Returns complex conjugate vector
38 {
39  return *this;
40 }
41 
43 {
44  return BasicVector3D<complex_t>(std::conj(v_[0]), std::conj(v_[1]), std::conj(v_[2]));
45 }
46 
47 //! Returns azimuth angle.
48 template <> double BasicVector3D<double>::phi() const
49 {
50  return x() == 0.0 && y() == 0.0 ? 0.0 : std::atan2(-y(), x());
51 }
52 
53 //! Returns polar angle.
54 template <> double BasicVector3D<double>::theta() const
55 {
56  return x() == 0.0 && y() == 0.0 && z() == 0.0 ? 0.0 : std::atan2(magxy(), z());
57 }
58 
59 //! Returns cosine of polar angle.
60 template <> double BasicVector3D<double>::cosTheta() const
61 {
62  return mag() == 0 ? 1 : z() / mag();
63 }
64 
65 //! Returns squared sine of polar angle.
66 template <> double BasicVector3D<double>::sin2Theta() const
67 {
68  return mag2() == 0 ? 0 : magxy2() / mag2();
69 }
70 
71 //! Returns this, trivially converted to complex type.
73 {
74  return BasicVector3D<complex_t>(v_[0], v_[1], v_[2]);
75 }
76 
77 //! Returns real parts.
79 {
80  return *this;
81 }
82 
84 {
85  return BasicVector3D<double>(v_[0].real(), v_[1].real(), v_[2].real());
86 }
87 
88 //! Returns unit vector in direction of this. Throws for null vector.
90 {
91  double len = mag();
92  if (len == 0.0)
93  throw Exceptions::DivisionByZeroException("Cannot normalize zero vector");
94  return BasicVector3D<double>(x() / len, y() / len, z() / len);
95 }
96 
98 {
99  double len = mag();
100  if (len == 0.0)
101  throw Exceptions::DivisionByZeroException("Cannot normalize zero vector");
102  return BasicVector3D<complex_t>(x() / len, y() / len, z() / len);
103 }
104 
105 // -----------------------------------------------------------------------------
106 // Combine two vectors
107 // -----------------------------------------------------------------------------
108 
109 //! Returns angle with respect to another vector.
110 template <> double BasicVector3D<double>::angle(const BasicVector3D<double>& v) const
111 {
112  double cosa = 0;
113  double ptot = mag() * v.mag();
114  if (ptot > 0) {
115  cosa = dot(v) / ptot;
116  if (cosa > 1)
117  cosa = 1;
118  if (cosa < -1)
119  cosa = -1;
120  }
121  return std::acos(cosa);
122 }
BasicVector3D< double > vecOfLambdaAlphaPhi(double _lambda, double _alpha, double _phi)
Creates a vector<double> as a wavevector with given wavelength and angles.
std::complex< double > complex_t
Declares and partly implements template class BasicVector3D.
Defines many exception classes in namespace Exceptionss.
Defines M_PI and some more mathematical constants.
#define M_TWOPI
Definition: MathConstants.h:49
double mag2() const
Returns magnitude squared of the vector.
BasicVector3D< T > conj() const
Returns complex conjugate vector.
auto dot(const BasicVector3D< U > &v) const
Returns dot product of vectors (antilinear in the first [=self] argument).
double sin2Theta() const
Returns squared sine of polar angle.
BasicVector3D< T > unit() const
Returns unit vector in direction of this. Throws for null vector.
double magxy2() const
Returns squared distance from z axis.
double mag() const
Returns magnitude of the vector.
double theta() const
Returns polar angle.
BasicVector3D< double > real() const
Returns real parts.
T z() const
Returns z-component in cartesian coordinate system.
Definition: BasicVector3D.h:68
T y() const
Returns y-component in cartesian coordinate system.
Definition: BasicVector3D.h:66
double phi() const
Returns azimuth angle.
T x() const
Returns x-component in cartesian coordinate system.
Definition: BasicVector3D.h:64
double magxy() const
Returns distance from z axis.
double angle(const BasicVector3D< T > &v) const
Returns angle with respect to another vector.
BasicVector3D< std::complex< double > > complex() const
Returns this, trivially converted to complex type.
double cosTheta() const
Returns cosine of polar angle.