BornAgain  1.19.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 reflection and scattering
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 
16 #include <stdexcept>
17 
18 typedef std::complex<double> complex_t;
19 
20 // -----------------------------------------------------------------------------
21 // Functions of this (with no further argument)
22 // -----------------------------------------------------------------------------
23 
24 //! Returns complex conjugate vector
26 {
27  return *this;
28 }
29 
31 {
32  return BasicVector3D<complex_t>(std::conj(v_[0]), std::conj(v_[1]), std::conj(v_[2]));
33 }
34 
35 //! Returns azimuth angle.
36 template <> double BasicVector3D<double>::phi() const
37 {
38  return x() == 0.0 && y() == 0.0 ? 0.0 : std::atan2(-y(), x());
39 }
40 
41 //! Returns polar angle.
42 template <> double BasicVector3D<double>::theta() const
43 {
44  return x() == 0.0 && y() == 0.0 && z() == 0.0 ? 0.0 : std::atan2(magxy(), z());
45 }
46 
47 //! Returns cosine of polar angle.
48 template <> double BasicVector3D<double>::cosTheta() const
49 {
50  return mag() == 0 ? 1 : z() / mag();
51 }
52 
53 //! Returns squared sine of polar angle.
54 template <> double BasicVector3D<double>::sin2Theta() const
55 {
56  return mag2() == 0 ? 0 : magxy2() / mag2();
57 }
58 
59 //! Returns this, trivially converted to complex type.
61 {
62  return BasicVector3D<complex_t>(v_[0], v_[1], v_[2]);
63 }
64 
65 //! Returns real parts.
67 {
68  return *this;
69 }
70 
72 {
73  return BasicVector3D<double>(v_[0].real(), v_[1].real(), v_[2].real());
74 }
75 
76 //! Returns unit vector in direction of this. Throws for null vector.
78 {
79  double len = mag();
80  if (len == 0.0)
81  throw std::runtime_error("Cannot normalize zero vector");
82  return BasicVector3D<double>(x() / len, y() / len, z() / len);
83 }
84 
86 {
87  double len = mag();
88  if (len == 0.0)
89  throw std::runtime_error("Cannot normalize zero vector");
90  return BasicVector3D<complex_t>(x() / len, y() / len, z() / len);
91 }
92 
93 // -----------------------------------------------------------------------------
94 // Combine two vectors
95 // -----------------------------------------------------------------------------
96 
97 //! Returns angle with respect to another vector.
98 template <> double BasicVector3D<double>::angle(const BasicVector3D<double>& v) const
99 {
100  double cosa = 0;
101  double ptot = mag() * v.mag();
102  if (ptot > 0) {
103  cosa = dot(v) / ptot;
104  if (cosa > 1)
105  cosa = 1;
106  if (cosa < -1)
107  cosa = -1;
108  }
109  return std::acos(cosa);
110 }
std::complex< double > complex_t
Declares and partly implements template class BasicVector3D.
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< complex_t > complex() const
Returns this, trivially converted to complex type.
BasicVector3D< double > real() const
Returns real parts.
T z() const
Returns z-component in cartesian coordinate system.
Definition: BasicVector3D.h:67
T y() const
Returns y-component in cartesian coordinate system.
Definition: BasicVector3D.h:65
double phi() const
Returns azimuth angle.
T x() const
Returns x-component in cartesian coordinate system.
Definition: BasicVector3D.h:63
double magxy() const
Returns distance from z axis.
double angle(const BasicVector3D< T > &v) const
Returns angle with respect to another vector.
double cosTheta() const
Returns cosine of polar angle.