surfaceTransformPoints.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 | Copyright (C) 2011-2016 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 {
60  (
61  "Transform (scale/rotate) a surface. "
62  "Like transformPoints but for surfaces."
63  );
65  argList::validArgs.append("surfaceFile");
66  argList::validArgs.append("output surfaceFile");
68  (
69  "translate",
70  "vector",
71  "translate by the specified <vector> - eg, '(1 0 0)'"
72  );
74  (
75  "rotate",
76  "(vectorA vectorB)",
77  "transform in terms of a rotation between <vectorA> and <vectorB> "
78  "- eg, '( (1 0 0) (0 0 1) )'"
79  );
81  (
82  "scale",
83  "vector",
84  "scale by the specified amount - eg, '(0.001 0.001 0.001)' for a "
85  "uniform [mm] to [m] scaling"
86  );
88  (
89  "rollPitchYaw",
90  "vector",
91  "transform in terms of '( roll pitch yaw )' in degrees"
92  );
94  (
95  "yawPitchRoll",
96  "vector",
97  "transform in terms of '( yaw pitch roll )' in degrees"
98  );
99  argList args(argc, argv);
100 
101  const fileName surfFileName = args[1];
102  const fileName outFileName = args[2];
103 
104  Info<< "Reading surf from " << surfFileName << " ..." << nl
105  << "Writing surf to " << outFileName << " ..." << endl;
106 
107  if (args.options().empty())
108  {
110  << "No options supplied, please use one or more of "
111  "-translate, -rotate or -scale options."
112  << exit(FatalError);
113  }
114 
115  meshedSurface surf1(surfFileName);
116 
117  pointField points(surf1.points());
118 
119  vector v;
120  if (args.optionReadIfPresent("translate", v))
121  {
122  Info<< "Translating points by " << v << endl;
123 
124  points += v;
125  }
126 
127  if (args.optionFound("rotate"))
128  {
129  Pair<vector> n1n2
130  (
131  args.optionLookup("rotate")()
132  );
133  n1n2[0] /= mag(n1n2[0]);
134  n1n2[1] /= mag(n1n2[1]);
135 
136  tensor T = rotationTensor(n1n2[0], n1n2[1]);
137 
138  Info<< "Rotating points by " << T << endl;
139 
140  points = transform(T, points);
141  }
142  else if (args.optionReadIfPresent("rollPitchYaw", v))
143  {
144  Info<< "Rotating points by" << nl
145  << " roll " << v.x() << nl
146  << " pitch " << v.y() << nl
147  << " yaw " << v.z() << nl;
148 
149  // Convert to radians
150  v *= pi/180.0;
151 
152  quaternion R(quaternion::rotationSequence::XYZ, v);
153 
154  Info<< "Rotating points by quaternion " << R << endl;
155  points = transform(R, points);
156  }
157  else if (args.optionReadIfPresent("yawPitchRoll", v))
158  {
159  Info<< "Rotating points by" << nl
160  << " yaw " << v.x() << nl
161  << " pitch " << v.y() << nl
162  << " roll " << v.z() << nl;
163 
164 
165  // Convert to radians
166  v *= pi/180.0;
167 
168  scalar yaw = v.x();
169  scalar pitch = v.y();
170  scalar roll = v.z();
171 
172  quaternion R = quaternion(vector(0, 0, 1), yaw);
173  R *= quaternion(vector(0, 1, 0), pitch);
174  R *= quaternion(vector(1, 0, 0), roll);
175 
176  Info<< "Rotating points by quaternion " << R << endl;
177  points = transform(R, points);
178  }
179 
180  if (args.optionReadIfPresent("scale", v))
181  {
182  Info<< "Scaling points by " << v << endl;
183 
187  }
188 
189  surf1.movePoints(points);
190  surf1.write(outFileName);
191 
192  Info<< "End\n" << endl;
193 
194  return 0;
195 }
196 
197 
198 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:69
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
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:198
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void noParallel()
Remove the parallel options.
Definition: argList.C:146
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
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:662
const Foam::HashTable< string > & options() const
Return options.
Definition: argListI.H:90
Spatial transformation functions for primitive fields.
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: contiguous.H:49
const pointField & points
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
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:93
Quaternion class used to perform rotations in 3D space.
Definition: quaternion.H:60
static const char nl
Definition: Ostream.H:262
tmp< Field< cmptType > > component(const direction) const
Return a component field of the field.
Definition: Field.C:650
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
#define R(A, B, C, D, E, F, K, M)
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:114
messageStream Info
dimensioned< scalar > mag(const dimensioned< Type > &)
static void addNote(const string &)
Add extra notes for the usage information.
Definition: argList.C:124
Foam::argList args(argc, argv)
Namespace for OpenFOAM.
dimensionSet transform(const dimensionSet &)
Definition: dimensionSet.C:465