BornAgain  1.19.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 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/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 getTransform3D().transformed(v);
56 }
57 
59 {
60  return getTransform3D().isIdentity();
61 }
62 
64 {
65  return getTransform3D().isZRotation();
66 }
67 
68 //! Returns concatenated rotation (first right, then left).
69 
70 IRotation* createProduct(const IRotation& left, const IRotation& right)
71 {
72  Transform3D tr_left = left.getTransform3D();
73  Transform3D tr_right = right.getTransform3D();
74  IRotation* p_result = IRotation::createRotation(tr_left * tr_right);
75  return p_result;
76 }
77 
78 // ************************************************************************************************
79 // class IdentityRotation
80 // ************************************************************************************************
81 
83  : IRotation({"IdentityRotation", "Identity rotation, does nothing", {}}, {})
84 {
85 }
86 
88 {
89  return {};
90 }
91 
92 // ************************************************************************************************
93 // class RotationX
94 // ************************************************************************************************
95 
96 //! Constructor of rotation around x-axis
97 RotationX::RotationX(const std::vector<double> P)
98  : IRotation(
99  {"XRotation", "class_tooltip", {{"Angle", "rad", "Angle around x axis", -INF, +INF, 0}}}, P)
100  , m_angle(m_P[0])
101 {
102 }
103 
104 RotationX::RotationX(double angle) : RotationX(std::vector<double>{angle}) {}
105 
107 {
109 }
110 
111 // ************************************************************************************************
112 // class RotationY
113 // ************************************************************************************************
114 
115 //! Constructor of rotation around y-axis
116 RotationY::RotationY(const std::vector<double> P)
117  : IRotation(
118  {"YRotation", "class_tooltip", {{"Angle", "rad", "Angle around y axis", -INF, +INF, 0}}}, P)
119  , m_angle(m_P[0])
120 {
121 }
122 
123 RotationY::RotationY(double angle) : RotationY(std::vector<double>{angle}) {}
124 
126 {
128 }
129 
130 // ************************************************************************************************
131 // class RotationZ
132 // ************************************************************************************************
133 
134 // --- RotationZ --------------------------------------------------------------
135 
136 //! Constructor of rotation around z-axis
137 RotationZ::RotationZ(const std::vector<double> P)
138  : IRotation(
139  {"ZRotation", "class_tooltip", {{"Angle", "rad", "Angle around z axis", -INF, +INF, 0}}}, P)
140  , m_angle(m_P[0])
141 {
142 }
143 
144 RotationZ::RotationZ(double angle) : RotationZ(std::vector<double>{angle}) {}
145 
147 {
149 }
150 
151 // ************************************************************************************************
152 // class RotationEuler
153 // ************************************************************************************************
154 
155 RotationEuler::RotationEuler(const std::vector<double> P)
156  : IRotation({"EulerRotation",
157  "Sequence of three rotations around z-x'-z''",
158  {{"Alpha", "rad", "First Euler angle, rotation around z axis", -INF, +INF, 0},
159  {"Beta", "rad", "Second Euler angle, rotation around x' axis", -INF, +INF, 0},
160  {"Gamma", "rad", "Third Euler angle, rotation around z'' axis", -INF, +INF, 0}}},
161  P)
162  , m_alpha(m_P[0])
163  , m_beta(m_P[1])
164  , m_gamma(m_P[2])
165 {
166 }
167 
168 RotationEuler::RotationEuler(double alpha, double beta, double gamma)
169  : RotationEuler(std::vector<double>{alpha, beta, gamma})
170 {
171 }
172 
174 {
175  Transform3D inverse_transform(getTransform3D().getInverse());
176  return createRotation(inverse_transform);
177 }
178 
180 {
182 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
const double INF
Definition: INode.h:25
IRotation * createProduct(const IRotation &left, const IRotation &right)
Returns concatenated rotation (first right, then left).
Definition: Rotations.cpp:70
Defines IRotation classes.
Declares class Transform3D.
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
Abstract base class for rotations.
Definition: Rotations.h:28
IRotation(const NodeMeta &meta, const std::vector< double > &PValues)
Definition: Rotations.cpp:23
kvector_t transformed(const kvector_t &v) const
Definition: Rotations.cpp:53
virtual bool isIdentity() const
Returns true if rotation matrix is identity matrix (no rotations)
Definition: Rotations.cpp:58
virtual Transform3D getTransform3D() const =0
Returns transformation.
static IRotation * createRotation(const Transform3D &transform)
Definition: Rotations.cpp:28
bool zInvariant() const
Definition: Rotations.cpp:63
Transform3D getTransform3D() const
Returns transformation.
Definition: Rotations.cpp:87
A sequence of rotations about the z-x'-z'' axes.
Definition: Rotations.h:133
double m_beta
Definition: Rotations.h:150
RotationEuler(const std::vector< double > P)
Definition: Rotations.cpp:155
Transform3D getTransform3D() const
Returns transformation.
Definition: Rotations.cpp:179
double m_gamma
Definition: Rotations.h:150
double m_alpha
Definition: Rotations.h:150
IRotation * createInverse() const
Returns a new IRotation object that is the current object's inverse.
Definition: Rotations.cpp:173
A rotation about the x axis.
Definition: Rotations.h:73
Transform3D getTransform3D() const
Returns transformation.
Definition: Rotations.cpp:106
const double & m_angle
Definition: Rotations.h:88
RotationX(const std::vector< double > P)
Constructor of rotation around x-axis.
Definition: Rotations.cpp:97
A rotation about the y axis.
Definition: Rotations.h:93
Transform3D getTransform3D() const
Returns transformation.
Definition: Rotations.cpp:125
const double & m_angle
Definition: Rotations.h:108
RotationY(const std::vector< double > P)
Constructor of rotation around y-axis.
Definition: Rotations.cpp:116
A rotation about the z axis.
Definition: Rotations.h:113
const double & m_angle
Definition: Rotations.h:128
Transform3D getTransform3D() const
Returns transformation.
Definition: Rotations.cpp:146
RotationZ(const std::vector< double > P)
Constructor of rotation around z-axis.
Definition: Rotations.cpp:137
Vector transformations in three dimensions.
Definition: Transform3D.h:26
static Transform3D createRotateX(double phi)
Creates rotation around x-axis.
Definition: Transform3D.cpp:29
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:94
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:76
static Transform3D createRotateEuler(double alpha, double beta, double gamma)
Creates rotation defined by Euler angles.
Definition: Transform3D.cpp:68
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:89
static Transform3D createRotateZ(double phi)
Creates rotation around z-axis.
Definition: Transform3D.cpp:55
double calculateRotateZAngle() const
Calculates the rotation angle for a rotation around the z-axis alone Only meaningfull if the actual r...
Definition: Transform3D.cpp:99
static Transform3D createRotateY(double phi)
Creates rotation around y-axis.
Definition: Transform3D.cpp:42
Definition: filesystem.h:81
Metadata of one model node.
Definition: INode.h:38