surfaceTransformPoints.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  surfaceTransformPoints
26 
27 Description
28  Transform (scale/rotate) a surface.
29  Like transformPoints but for surfaces.
30 
31  The rollPitchYaw option takes three angles (degrees):
32  - roll (rotation about x) followed by
33  - pitch (rotation about y) followed by
34  - yaw (rotation about z)
35 
36  The yawPitchRoll does yaw followed by pitch followed by roll.
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #include "argList.H"
41 #include "OFstream.H"
42 #include "IFstream.H"
43 #include "boundBox.H"
44 #include "transformField.H"
45 #include "Pair.H"
46 #include "quaternion.H"
47 #include "mathematicalConstants.H"
48 
49 #include "MeshedSurfaces.H"
50 
51 using namespace Foam;
52 using namespace Foam::constant::mathematical;
53 
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 int main(int argc, char *argv[])
58 {
59  #include "removeCaseOptions.H"
60 
62  (
63  "Transform (scale/rotate) a surface. "
64  "Like transformPoints but for surfaces."
65  );
67  argList::validArgs.append("surface file");
68  argList::validArgs.append("output surface file");
70  (
71  "translate",
72  "vector",
73  "translate by the specified <vector> - eg, '(1 0 0)'"
74  );
76  (
77  "rotate",
78  "(vectorA vectorB)",
79  "transform in terms of a rotation between <vectorA> and <vectorB> "
80  "- eg, '( (1 0 0) (0 0 1) )'"
81  );
83  (
84  "scale",
85  "vector",
86  "scale by the specified amount - eg, '(0.001 0.001 0.001)' for a "
87  "uniform [mm] to [m] scaling"
88  );
90  (
91  "rollPitchYaw",
92  "vector",
93  "transform in terms of '( roll pitch yaw )' in degrees"
94  );
96  (
97  "yawPitchRoll",
98  "vector",
99  "transform in terms of '( yaw pitch roll )' in degrees"
100  );
101  argList args(argc, argv);
102 
103  const fileName surfFileName = args[1];
104  const fileName outFileName = args[2];
105 
106  Info<< "Reading surf from " << surfFileName << " ..." << nl
107  << "Writing surf to " << outFileName << " ..." << endl;
108 
109  if (args.options().empty())
110  {
112  << "No options supplied, please use one or more of "
113  "-translate, -rotate or -scale options."
114  << exit(FatalError);
115  }
116 
117  meshedSurface surf1(surfFileName);
118 
119  pointField points(surf1.points());
120 
121  vector v;
122  if (args.optionReadIfPresent("translate", v))
123  {
124  Info<< "Translating points by " << v << endl;
125 
126  points += v;
127  }
128 
129  if (args.optionFound("rotate"))
130  {
131  Pair<vector> n1n2
132  (
133  args.optionLookup("rotate")()
134  );
135  n1n2[0] /= mag(n1n2[0]);
136  n1n2[1] /= mag(n1n2[1]);
137 
138  tensor T = rotationTensor(n1n2[0], n1n2[1]);
139 
140  Info<< "Rotating points by " << T << endl;
141 
142  points = transform(T, points);
143  }
144  else if (args.optionReadIfPresent("rollPitchYaw", v))
145  {
146  Info<< "Rotating points by" << nl
147  << " roll " << v.x() << nl
148  << " pitch " << v.y() << nl
149  << " yaw " << v.z() << nl;
150 
151  // Convert to radians
152  v *= pi/180.0;
153 
154  quaternion R(quaternion::rotationSequence::XYZ, v);
155 
156  Info<< "Rotating points by quaternion " << R << endl;
157  points = transform(R, points);
158  }
159  else if (args.optionReadIfPresent("yawPitchRoll", v))
160  {
161  Info<< "Rotating points by" << nl
162  << " yaw " << v.x() << nl
163  << " pitch " << v.y() << nl
164  << " roll " << v.z() << nl;
165 
166 
167  // Convert to radians
168  v *= pi/180.0;
169 
170  scalar yaw = v.x();
171  scalar pitch = v.y();
172  scalar roll = v.z();
173 
174  quaternion R = quaternion(vector(0, 0, 1), yaw);
175  R *= quaternion(vector(0, 1, 0), pitch);
176  R *= quaternion(vector(1, 0, 0), roll);
177 
178  Info<< "Rotating points by quaternion " << R << endl;
179  points = transform(R, points);
180  }
181 
182  if (args.optionReadIfPresent("scale", v))
183  {
184  Info<< "Scaling points by " << v << endl;
185 
189  }
190 
191  surf1.movePoints(points);
192  surf1.write(outFileName);
193 
194  Info<< "End\n" << endl;
195 
196  return 0;
197 }
198 
199 
200 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:79
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
static void noParallel()
Remove the parallel options.
Definition: argList.C:174
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:153
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
tensor rotationTensor(const vector &n1, const vector &n2)
Rotational transformation tensor from vector n1 to n2.
Definition: transform.H:47
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:472
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
Spatial transformation functions for primitive fields.
const Foam::HashTable< string > & options() const
Return options.
Definition: argListI.H:90
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: contiguous.H:49
const pointField & points
mathematical constants.
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:127
Quaternion class used to perform rotations in 3D space.
Definition: quaternion.H:60
tmp< Field< cmptType > > component(const direction) const
Return a component field of the field.
Definition: Field.C:460
static const char nl
Definition: Ostream.H:265
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
#define R(A, B, C, D, E, F, K, M)
messageStream Info
dimensioned< scalar > mag(const dimensioned< Type > &)
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:158
Foam::argList args(argc, argv)
Namespace for OpenFOAM.
dimensionSet transform(const dimensionSet &)
Definition: dimensionSet.C:477
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:114