BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
Rotations.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Sample/Scattering/Rotations.cpp
6 //! @brief Implements IRotation classes.
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 "Base/Util/Assert.h"
17 #include "Base/Vector/RotMatrix.h"
18 
19 // ************************************************************************************************
20 // interface IRotation
21 // ************************************************************************************************
22 
23 IRotation::IRotation(const std::vector<double>& PValues)
24  : INode(PValues)
25 {
26 }
27 
29 {
30  if (matrix.isIdentity())
31  return new IdentityRotation;
32  if (std::optional<double> angle = matrix.angleAroundCoordAxis(0))
33  return new RotationX(angle.value());
34  if (std::optional<double> angle = matrix.angleAroundCoordAxis(1))
35  return new RotationY(angle.value());
36  if (std::optional<double> angle = matrix.angleAroundCoordAxis(2))
37  return new RotationZ(angle.value());
38  auto angles = matrix.zxzEulerAngles();
39  return new RotationEuler(angles[0], angles[1], angles[2]);
40 }
41 
42 R3 IRotation::transformed(const R3& v) const
43 {
44  return rotMatrix().transformed(v);
45 }
46 
48 {
49  return rotMatrix().isIdentity();
50 }
51 
53 {
54  return rotMatrix().isZRotation();
55 }
56 
57 //! Returns concatenated rotation (first right, then left).
58 
59 IRotation* createProduct(const IRotation& left, const IRotation& right)
60 {
61  RotMatrix tr_left = left.rotMatrix();
62  RotMatrix tr_right = right.rotMatrix();
63  IRotation* p_result = IRotation::createRotation(tr_left * tr_right);
64  return p_result;
65 }
66 
67 // ************************************************************************************************
68 // class IdentityRotation
69 // ************************************************************************************************
70 
72  : IRotation({})
73 {
74 }
75 
77 {
78  return {};
79 }
80 
81 // ************************************************************************************************
82 // class RotationX
83 // ************************************************************************************************
84 
85 //! Constructor of rotation around x-axis
86 RotationX::RotationX(const std::vector<double> P)
87  : IRotation(P)
88  , m_angle(m_P[0])
89 {
90  checkNodeArgs();
91 }
92 
93 RotationX::RotationX(double angle)
94  : RotationX(std::vector<double>{angle})
95 {
96 }
97 
99 {
100  return RotMatrix::AroundX(m_angle);
101 }
102 
103 // ************************************************************************************************
104 // class RotationY
105 // ************************************************************************************************
106 
107 //! Constructor of rotation around y-axis
108 RotationY::RotationY(const std::vector<double> P)
109  : IRotation(P)
110  , m_angle(m_P[0])
111 {
112  checkNodeArgs();
113 }
114 
115 RotationY::RotationY(double angle)
116  : RotationY(std::vector<double>{angle})
117 {
118 }
119 
121 {
122  return RotMatrix::AroundY(m_angle);
123 }
124 
125 // ************************************************************************************************
126 // class RotationZ
127 // ************************************************************************************************
128 
129 // --- RotationZ --------------------------------------------------------------
130 
131 //! Constructor of rotation around z-axis
132 RotationZ::RotationZ(const std::vector<double> P)
133  : IRotation(P)
134  , m_angle(m_P[0])
135 {
136  checkNodeArgs();
137 }
138 
139 RotationZ::RotationZ(double angle)
140  : RotationZ(std::vector<double>{angle})
141 {
142 }
143 
145 {
146  return RotMatrix::AroundZ(m_angle);
147 }
148 
149 // ************************************************************************************************
150 // class RotationEuler
151 // ************************************************************************************************
152 
153 RotationEuler::RotationEuler(const std::vector<double> P)
154  : IRotation(P)
155  , m_alpha(m_P[0])
156  , m_beta(m_P[1])
157  , m_gamma(m_P[2])
158 {
159  checkNodeArgs();
160 }
161 
162 RotationEuler::RotationEuler(double alpha, double beta, double gamma)
163  : RotationEuler(std::vector<double>{alpha, beta, gamma})
164 {
165 }
166 
168 {
169  RotMatrix inverse_transform(rotMatrix().Inverse());
170  return createRotation(inverse_transform);
171 }
172 
174 {
176 }
Defines the macro ASSERT.
Declares class RotMatrix.
IRotation * createProduct(const IRotation &left, const IRotation &right)
Returns concatenated rotation (first right, then left).
Definition: Rotations.cpp:59
Defines IRotation classes.
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:40
void checkNodeArgs() const
Raises exception if a parameter value is invalid.
Definition: INode.cpp:27
Abstract base class for rotations.
Definition: Rotations.h:29
IRotation(const std::vector< double > &PValues)
Definition: Rotations.cpp:23
virtual RotMatrix rotMatrix() const =0
Returns transformation.
R3 transformed(const R3 &v) const
Definition: Rotations.cpp:42
virtual bool isIdentity() const
Returns true if rotation matrix is identity matrix (no rotations)
Definition: Rotations.cpp:47
static IRotation * createRotation(const RotMatrix &transform)
Definition: Rotations.cpp:28
bool zInvariant() const
Definition: Rotations.cpp:52
The identity rotation, which leaves everything in place.
Definition: Rotations.h:58
RotMatrix rotMatrix() const override
Returns transformation.
Definition: Rotations.cpp:76
Rotation matrix in three dimensions. Represents group SO(3). Internal parameterization based on quate...
Definition: RotMatrix.h:25
static RotMatrix EulerZXZ(double alpha, double beta, double gamma)
Creates rotation defined by Euler angles.
Definition: RotMatrix.cpp:45
static RotMatrix AroundZ(double phi)
Creates rotation around z-axis.
Definition: RotMatrix.cpp:40
static RotMatrix AroundY(double phi)
Creates rotation around y-axis.
Definition: RotMatrix.cpp:35
static RotMatrix AroundX(double phi)
Creates rotation around x-axis.
Definition: RotMatrix.cpp:30
bool isIdentity() const
Determine if the transformation is trivial (identity)
Definition: RotMatrix.cpp:111
bool isZRotation() const
Definition: RotMatrix.cpp:126
std::optional< double > angleAroundCoordAxis(int iAxis) const
Definition: RotMatrix.cpp:131
std::array< double, 3 > zxzEulerAngles() const
Calculates the Euler angles corresponding to the rotation.
Definition: RotMatrix.cpp:53
T transformed(const T &v) const
Return transformed vector v.
Definition: RotMatrix.cpp:76
A sequence of rotations about the z-x'-z'' axes.
Definition: Rotations.h:146
double m_beta
Definition: Rotations.h:169
double beta() const
Definition: Rotations.h:163
RotationEuler(std::vector< double > P)
Definition: Rotations.cpp:153
double m_gamma
Definition: Rotations.h:169
IRotation * createInverse() const override
Returns a new IRotation object that is the current object's inverse.
Definition: Rotations.cpp:167
double m_alpha
Definition: Rotations.h:169
double gamma() const
Definition: Rotations.h:164
RotMatrix rotMatrix() const override
Returns transformation.
Definition: Rotations.cpp:173
double alpha() const
Definition: Rotations.h:162
A rotation about the x axis.
Definition: Rotations.h:74
double angle() const
Definition: Rotations.h:88
RotationX(std::vector< double > P)
Constructor of rotation around x-axis.
Definition: Rotations.cpp:86
RotMatrix rotMatrix() const override
Returns transformation.
Definition: Rotations.cpp:98
const double & m_angle
Definition: Rotations.h:93
A rotation about the y axis.
Definition: Rotations.h:98
RotationY(std::vector< double > P)
Constructor of rotation around y-axis.
Definition: Rotations.cpp:108
double angle() const
Definition: Rotations.h:112
RotMatrix rotMatrix() const override
Returns transformation.
Definition: Rotations.cpp:120
const double & m_angle
Definition: Rotations.h:117
A rotation about the z axis.
Definition: Rotations.h:122
const double & m_angle
Definition: Rotations.h:141
RotationZ(std::vector< double > P)
Constructor of rotation around z-axis.
Definition: Rotations.cpp:132
RotMatrix rotMatrix() const override
Returns transformation.
Definition: Rotations.cpp:144
double angle() const
Definition: Rotations.h:136
double beta(double z, double w)
double gamma(double x)
ABObj< sym, MatrixInverse< sym, ABObj< sym, LASymMatrix, double >, double >, double > Inverse(const ABObj< sym, LASymMatrix, double > &obj)
LAPACK Algebra functions specialize the Invert function for LASymMatrix.
Definition: LaInverse.h:27