BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Rotations.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
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/Utils/Assert.h"
18 
19 // ************************************************************************** //
20 // interface IRotation
21 // ************************************************************************** //
22 
23 IRotation::IRotation(const NodeMeta& meta, const std::vector<double>& PValues)
24  : INode(meta, PValues)
25 {
26 }
27 
29 {
30  auto rot_type = transform.getRotationType();
31  switch (rot_type) {
32  case Transform3D::XAXIS: {
33  double angle = transform.calculateRotateXAngle();
34  return new RotationX(angle);
35  }
36  case Transform3D::YAXIS: {
37  double angle = transform.calculateRotateYAngle();
38  return new RotationY(angle);
39  }
40  case Transform3D::ZAXIS: {
41  double angle = transform.calculateRotateZAngle();
42  return new RotationZ(angle);
43  }
44  case Transform3D::EULER: {
45  double alpha, beta, gamma;
46  transform.calculateEulerAngles(&alpha, &beta, &gamma);
47  return new RotationEuler(alpha, beta, gamma);
48  }
49  }
50  ASSERT(0); // impossible case
51 }
52 
54 {
55  return new RotationZ(0.0);
56 }
57 
59 {
60  return getTransform3D().transformed(v);
61 }
62 
64 {
65  return getTransform3D().isIdentity();
66 }
67 
69 {
70  return getTransform3D().isZRotation();
71 }
72 
73 //! Returns concatenated rotation (first right, then left).
74 
75 IRotation* createProduct(const IRotation& left, const IRotation& right)
76 {
77  Transform3D tr_left = left.getTransform3D();
78  Transform3D tr_right = right.getTransform3D();
79  IRotation* p_result = IRotation::createRotation(tr_left * tr_right);
80  return p_result;
81 }
82 
83 // ************************************************************************** //
84 // class IdentityRotation
85 // ************************************************************************** //
86 
88  : IRotation({"IdentityRotation", "Identity rotation, does nothing", {}}, {})
89 {
90 }
91 
93 {
95 }
96 
97 // ************************************************************************** //
98 // class RotationX
99 // ************************************************************************** //
100 
101 //! Constructor of rotation around x-axis
102 RotationX::RotationX(const std::vector<double> P)
103  : IRotation(
104  {"XRotation", "class_tooltip", {{"Angle", "rad", "Angle around x axis", -INF, +INF, 0}}},
105  P),
106  m_angle(m_P[0])
107 {
108 }
109 
110 RotationX::RotationX(double angle) : RotationX(std::vector<double>{angle}) {}
111 
113 {
115 }
116 
117 // ************************************************************************** //
118 // class RotationY
119 // ************************************************************************** //
120 
121 //! Constructor of rotation around y-axis
122 RotationY::RotationY(const std::vector<double> P)
123  : IRotation(
124  {"YRotation", "class_tooltip", {{"Angle", "rad", "Angle around y axis", -INF, +INF, 0}}},
125  P),
126  m_angle(m_P[0])
127 {
128 }
129 
130 RotationY::RotationY(double angle) : RotationY(std::vector<double>{angle}) {}
131 
133 {
135 }
136 
137 // ************************************************************************** //
138 // class RotationZ
139 // ************************************************************************** //
140 
141 // --- RotationZ --------------------------------------------------------------
142 
143 //! Constructor of rotation around z-axis
144 RotationZ::RotationZ(const std::vector<double> P)
145  : IRotation(
146  {"ZRotation", "class_tooltip", {{"Angle", "rad", "Angle around z axis", -INF, +INF, 0}}},
147  P),
148  m_angle(m_P[0])
149 {
150 }
151 
152 RotationZ::RotationZ(double angle) : RotationZ(std::vector<double>{angle}) {}
153 
155 {
157 }
158 
159 // ************************************************************************** //
160 // class RotationEuler
161 // ************************************************************************** //
162 
163 RotationEuler::RotationEuler(const std::vector<double> P)
164  : IRotation({"EulerRotation",
165  "Sequence of three rotations around z-x'-z''",
166  {{"Alpha", "rad", "First Euler angle, rotation around z axis", -INF, +INF, 0},
167  {"Beta", "rad", "Second Euler angle, rotation around x' axis", -INF, +INF, 0},
168  {"Gamma", "rad", "Third Euler angle, rotation around z'' axis", -INF, +INF, 0}}},
169  P),
170  m_alpha(m_P[0]), m_beta(m_P[1]), m_gamma(m_P[2])
171 {
172 }
173 
174 RotationEuler::RotationEuler(double alpha, double beta, double gamma)
175  : RotationEuler(std::vector<double>{alpha, beta, gamma})
176 {
177 }
178 
180 {
181  Transform3D inverse_transform(getTransform3D().getInverse());
182  return createRotation(inverse_transform);
183 }
184 
186 {
188 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:26
const double INF
Definition: INode.h:24
IRotation * createProduct(const IRotation &left, const IRotation &right)
Returns concatenated rotation (first right, then left).
Definition: Rotations.cpp:75
Defines IRotation classes.
Declares class Transform3D.
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
Pure virtual interface for rotations.
Definition: Rotations.h:27
IRotation(const NodeMeta &meta, const std::vector< double > &PValues)
Definition: Rotations.cpp:23
static IRotation * createIdentity()
Definition: Rotations.cpp:53
kvector_t transformed(const kvector_t &v) const
Definition: Rotations.cpp:58
virtual bool isIdentity() const
Returns true if rotation matrix is identity matrix (no rotations)
Definition: Rotations.cpp:63
virtual Transform3D getTransform3D() const =0
Returns transformation.
static IRotation * createRotation(const Transform3D &transform)
Definition: Rotations.cpp:28
bool zInvariant() const
Definition: Rotations.cpp:68
Transform3D getTransform3D() const
Returns transformation.
Definition: Rotations.cpp:92
A sequence of rotations about the z-x'-z'' axes.
Definition: Rotations.h:135
double m_beta
Definition: Rotations.h:152
RotationEuler(const std::vector< double > P)
Definition: Rotations.cpp:163
Transform3D getTransform3D() const
Returns transformation.
Definition: Rotations.cpp:185
double m_gamma
Definition: Rotations.h:152
double m_alpha
Definition: Rotations.h:152
IRotation * createInverse() const
Returns a new IRotation object that is the current object's inverse.
Definition: Rotations.cpp:179
A rotation about the x axis.
Definition: Rotations.h:72
Transform3D getTransform3D() const
Returns transformation.
Definition: Rotations.cpp:112
const double & m_angle
Definition: Rotations.h:87
RotationX(const std::vector< double > P)
Constructor of rotation around x-axis.
Definition: Rotations.cpp:102
A rotation about the y axis.
Definition: Rotations.h:93
Transform3D getTransform3D() const
Returns transformation.
Definition: Rotations.cpp:132
const double & m_angle
Definition: Rotations.h:108
RotationY(const std::vector< double > P)
Constructor of rotation around y-axis.
Definition: Rotations.cpp:122
A rotation about the z axis.
Definition: Rotations.h:114
const double & m_angle
Definition: Rotations.h:129
Transform3D getTransform3D() const
Returns transformation.
Definition: Rotations.cpp:154
RotationZ(const std::vector< double > P)
Constructor of rotation around z-axis.
Definition: Rotations.cpp:144
Vector transformations in three dimensions.
Definition: Transform3D.h:28
static Transform3D createRotateX(double phi)
Creates rotation around x-axis.
Definition: Transform3D.cpp:34
bool isIdentity() const
Determine if the transformation is trivial (identity)
double calculateRotateYAngle() const
Calculates the rotation angle for a rotation around the y-axis alone Only meaningfull if the actual r...
Definition: Transform3D.cpp:99
static Transform3D createIdentity()
Creates identity transformation (default)
Definition: Transform3D.cpp:29
ivector_t transformed(const ivector_t &v) const
Return transformed vector v.
void calculateEulerAngles(double *p_alpha, double *p_beta, double *p_gamma) const
Calculates the Euler angles corresponding to the rotation.
Definition: Transform3D.cpp:81
static Transform3D createRotateEuler(double alpha, double beta, double gamma)
Creates rotation defined by Euler angles.
Definition: Transform3D.cpp:73
bool isZRotation() const
ERotationType getRotationType() const
Retrieve the rotation type (general, around x, y or z-axis)
double calculateRotateXAngle() const
Calculates the rotation angle for a rotation around the x-axis alone Only meaningfull if the actual r...
Definition: Transform3D.cpp:94
static Transform3D createRotateZ(double phi)
Creates rotation around z-axis.
Definition: Transform3D.cpp:60
double calculateRotateZAngle() const
Calculates the rotation angle for a rotation around the z-axis alone Only meaningfull if the actual r...
static Transform3D createRotateY(double phi)
Creates rotation around y-axis.
Definition: Transform3D.cpp:47
Metadata of one model node.
Definition: INode.h:37