Debian Codegraph @

src/Base/Rotation.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25
26 #ifndef BASE_ROTATION_H
27 #define BASE_ROTATION_H
28
29 #include "Vector3D.h"
30 #ifndef FC_GLOBAL_H
31 # include <FCGlobal.h>
32 #endif
33
34 namespace Base
35 {
36
37 // forward declarations
38 class Matrix4D;
39
40 class BaseExport Rotation
41 {
42 public:
43 /** Construction. */
44 //@{
45 Rotation();
46 Rotation(const Vector3d& axis, double fAngle);
47 Rotation(const Matrix4D& matrix);
48 Rotation(const double q[4]);
49 Rotation(double q0, double q1, double q2, double q3);
50 Rotation(const Vector3d& rotateFrom, const Vector3d& rotateTo);
51 Rotation(const Rotation& rot) = default;
52 Rotation(Rotation&& rot) = default;
53 ~Rotation() = default;
54
55 enum EulerSequence
56 {
57 Invalid,
58
59 //! Classic Euler angles, alias to Intrinsic_ZXZ
60 EulerAngles,
61
62 //! Yaw Pitch Roll (or nautical) angles, alias to Intrinsic_ZYX
63 YawPitchRoll,
64
65 // Tait-Bryan angles (using three different axes)
66 Extrinsic_XYZ,
67 Extrinsic_XZY,
68 Extrinsic_YZX,
69 Extrinsic_YXZ,
70 Extrinsic_ZXY,
71 Extrinsic_ZYX,
72
73 Intrinsic_XYZ,
74 Intrinsic_XZY,
75 Intrinsic_YZX,
76 Intrinsic_YXZ,
77 Intrinsic_ZXY,
78 Intrinsic_ZYX,
79
80 // Proper Euler angles (using two different axes, first and third the same)
81 Extrinsic_XYX,
82 Extrinsic_XZX,
83 Extrinsic_YZY,
84 Extrinsic_YXY,
85 Extrinsic_ZYZ,
86 Extrinsic_ZXZ,
87
88 Intrinsic_XYX,
89 Intrinsic_XZX,
90 Intrinsic_YZY,
91 Intrinsic_YXY,
92 Intrinsic_ZXZ,
93 Intrinsic_ZYZ,
94
95 EulerSequenceLast,
96 };
97
98 /// Utility function to create Rotation based on direction / normal vector
99 /// Z base vector is assumed to represent the normal vector
100 static Rotation fromNormalVector(const Vector3d& normal);
101 /// Utility function to create Rotation based on euler angles
102 static Rotation fromEulerAngles(EulerSequence theOrder, double alpha, double beta, double gamma);
103 //@}
104
105 /** Methods to get or set rotations. */
106 //@{
107 const double* getValue() const;
108 void getValue(double& q0, double& q1, double& q2, double& q3) const;
109 void setValue(double q0, double q1, double q2, double q3);
110 /// If not a null quaternion then \a axis will be normalized
111 void getValue(Vector3d& axis, double& rfAngle) const;
112 /// Does the same as the method above unless normalizing the axis.
113 void getRawValue(Vector3d& axis, double& rfAngle) const;
114 void getValue(Matrix4D& matrix) const;
115 void setValue(const double q[4]);
116 void setValue(const Matrix4D& matrix);
117 void setValue(const Vector3d& axis, double fAngle);
118 void setValue(const Vector3d& rotateFrom, const Vector3d& rotateTo);
119 /// Euler angles in yaw,pitch,roll notation
120 void setYawPitchRoll(double y, double p, double r);
121 /// Euler angles in yaw,pitch,roll notation
122 void getYawPitchRoll(double& y, double& p, double& r) const;
123
124 static const char* eulerSequenceName(EulerSequence seq);
125 static EulerSequence eulerSequenceFromName(const char* name);
126 void getEulerAngles(EulerSequence theOrder, double& alpha, double& beta, double& gamma) const;
127 void setEulerAngles(EulerSequence theOrder, double alpha, double beta, double gamma);
128 bool isIdentity() const;
129 bool isIdentity(double tol) const;
130 bool isNull() const;
131 bool isSame(const Rotation&) const;
132 bool isSame(const Rotation&, double tol) const;
133 //@}
134
135 /** Invert rotations. */
136 //@{
137 Rotation& invert();
138 Rotation inverse() const;
139 //@}
140
141 /** Operators. */
142 //@{
143 Rotation& operator*=(const Rotation& q);
144 Rotation operator*(const Rotation& q) const;
145 bool operator==(const Rotation& q) const;
146 bool operator!=(const Rotation& q) const;
147 double& operator[](unsigned short usIndex)
148 {
149 return quat[usIndex];
150 }
151 const double& operator[](unsigned short usIndex) const
152 {
153 return quat[usIndex];
154 }
155 Rotation& operator=(const Rotation&) = default;
156 Rotation& operator=(Rotation&&) = default;
157
158 Rotation& multRight(const Base::Rotation& q);
159 Rotation& multLeft(const Base::Rotation& q);
160
161 void multVec(const Vector3d& src, Vector3d& dst) const;
162 Vector3d multVec(const Vector3d& src) const;
163 void multVec(const Vector3f& src, Vector3f& dst) const;
164 Vector3f multVec(const Vector3f& src) const;
165 void scaleAngle(double scaleFactor);
166 //@}
167
168 /** Specialty constructors */
169 static Rotation slerp(const Rotation& q0, const Rotation& q1, double t);
170 static Rotation identity();
171
172 /**
173 * @brief makeRotationByAxes(xdir, ydir, zdir, priorityOrder): creates a rotation
174 * that converts a vector in local cs with axes given as arguments, into a
175 * vector in global cs.
176 * @param xdir is wanted direction of local X axis
177 * @param ydir ...
178 * @param zdir
179 * @param priorityOrder sets which directions are followed. It is a string
180 * like "ZXY". This means, Z direction is followed precisely; X direction is
181 * corrected to be perpendicular to Z direction, and used; Y direction
182 * argument is ignored altogether (Y direction is generated from Z and X).
183 *
184 * If only one vector provided is nonzero, the other two directions are picked automatically.
185 */
186 static Rotation makeRotationByAxes(
187 Vector3d xdir,
188 Vector3d ydir,
189 Vector3d zdir,
190 const char* priorityOrder = "ZXY"
191 );
192
193 private:
194 void normalize();
195 void evaluateVector();
196 double quat[4];
197 Vector3d _axis; // the axis kept not to lose direction when angle is 0
198 double _angle; // this angle to keep the angle chosen by the user
199 };
200
201 } // namespace Base
202
203 #endif // BASE_ROTATION_H