axisAngleRotation.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2018-2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "axisAngleRotation.H"
29 #include "dictionary.H"
30 #include "quaternion.H"
31 #include "unitConversion.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace coordinateRotations
39 {
40 
41  defineTypeName(axisAngle);
43  (
44  coordinateRotation,
45  axisAngle,
46  dictionary
47  );
48 
49 } // End namespace coordinateRotation
50 } // End namespace Foam
51 
52 
53 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
54 
55 void Foam::coordinateRotations::axisAngle::checkSpec()
56 {
57  if (mag(angle_) < VSMALL || mag(axis_) < SMALL)
58  {
59  clear(); // identity rotation
60  }
61 }
62 
63 
64 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
65 
67 (
68  const vector& axis,
69  const scalar angle,
70  bool degrees
71 )
72 {
73  if (mag(angle) < VSMALL || mag(axis) < SMALL)
74  {
75  return sphericalTensor::I; // identity rotation
76  }
77 
78  return quaternion(axis, (degrees ? degToRad(angle) : angle)).R();
79 }
80 
81 
83 (
84  const vector::components axis,
85  const scalar angle,
86  bool degrees
87 )
88 {
89  vector rotAxis(Zero);
90  rotAxis[axis] = 1;
91 
92  return axisAngle::rotation(rotAxis, angle, degrees);
93 }
94 
95 
96 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
97 
99 :
101  axis_ (0,0,1), // e3 = global Z
102  angle_(Zero),
103  degrees_(true)
104 {}
105 
106 
108 :
110  axis_(crot.axis_),
111  angle_(crot.angle_),
112  degrees_(crot.degrees_)
113 {
114  checkSpec();
115 }
116 
117 
119 (
120  const vector& axis,
121  scalar angle,
122  bool degrees
123 )
124 :
126  axis_(axis),
127  angle_(angle),
128  degrees_(degrees)
129 {
130  checkSpec();
131 }
132 
133 
135 (
136  const vector::components axis,
137  scalar angle,
138  bool degrees
139 )
140 :
142  axis_(Zero),
143  angle_(angle),
144  degrees_(degrees)
145 {
146  axis_[axis] = 1;
147 }
148 
149 
151 :
152  axisAngle
153  (
154  dict.get<vector>("axis"),
155  dict.get<scalar>("angle"),
156  dict.getOrDefault("degrees", true)
157  )
158 {}
159 
160 
161 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
162 
164 {
165  axis_ = vector(0,0,1); // e3 = global Z
166  angle_ = Zero;
167 }
168 
171 {
172  return rotation(axis_, angle_, degrees_);
173 }
174 
175 
177 {
178  os << "rotation axis: " << axis_
179  << " angle(" << (degrees_ ? "deg" : "rad") << "): " << angle_;
180 }
181 
182 
184 (
185  const word& keyword,
186  Ostream& os
187 ) const
188 {
189  os.beginBlock(keyword);
190 
191  os.writeEntry("type", type());
192  os.writeEntry("axis", axis_);
193  os.writeEntry("angle", angle_);
194  if (!degrees_)
195  {
196  os.writeEntry("degrees", "false");
197  }
198 
199  os.endBlock();
200 }
201 
202 
203 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
virtual void write(Ostream &os) const
Write information.
User specification of a coordinate rotation.
dictionary dict
tensor R() const
The rotation tensor corresponding to the quaternion.
Definition: quaternionI.H:352
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Unit conversion functions.
Macros for easy insertion into run-time selection tables.
virtual void clear()
Reset specification.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
addToRunTimeSelectionTable(coordinateRotation, axisAngle, dictionary)
A class for handling words, derived from Foam::string.
Definition: word.H:63
Quaternion class used to perform rotations in 3D space.
Definition: quaternion.H:53
Vector< scalar > vector
Definition: vector.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
OBJstream os(runTime.globalPath()/outputName)
static const SphericalTensor I
axisAngle()
Default construct. Axis = Z, angle = 0.
static tensor rotation(const vector &axis, const scalar angle, bool degrees=false)
The rotation tensor for given axis/angle.
components
Component labeling enumeration.
Definition: Vector.H:83
A coordinateRotation specified by a rotation axis and a rotation angle about that axis...
virtual void writeEntry(const word &keyword, Ostream &os) const
Write dictionary entry.
Tensor of scalars, i.e. Tensor<scalar>.
constexpr scalar degToRad(const scalar deg) noexcept
Conversion from degrees to radians.
Namespace for OpenFOAM.
virtual tensor R() const
Calculate and return the rotation tensor calculated from axis and angle.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127