transformPoints.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Application
28  transformPoints
29 
30 Group
31  grpMeshManipulationUtilities
32 
33 Description
34  Transforms the mesh points in the polyMesh directory according to the
35  translate, rotate and scale options.
36 
37 Usage
38  Options are:
39 
40  -time value
41  Specify the time to search from and apply the transformation
42  (default is latest)
43 
44  -translate vector
45  Translates the points by the given vector before rotations
46 
47  -rotate (vector vector)
48  Rotates the points from the first vector to the second,
49 
50  -rotate-angle (vector angle)
51  Rotate angle degrees about vector axis.
52 
53  or -yawPitchRoll (yawdegrees pitchdegrees rolldegrees)
54  or -rollPitchYaw (rolldegrees pitchdegrees yawdegrees)
55 
56  -scale scalar|vector
57  Scales the points by the given scalar or vector.
58 
59  The any or all of the three options may be specified and are processed
60  in the above order.
61 
62  With -rotateFields (in combination with -rotate/yawPitchRoll/rollPitchYaw)
63  it will also read & transform vector & tensor fields.
64 
65 Note
66  roll (rotation about x)
67  pitch (rotation about y)
68  yaw (rotation about z)
69 
70 \*---------------------------------------------------------------------------*/
71 
72 #include "argList.H"
73 #include "Time.H"
74 #include "fvMesh.H"
75 #include "volFields.H"
76 #include "surfaceFields.H"
77 #include "ReadFields.H"
78 #include "pointFields.H"
79 #include "transformField.H"
81 #include "axisAngleRotation.H"
83 
84 using namespace Foam;
85 using namespace Foam::coordinateRotations;
86 
87 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
88 
89 template<class GeoField>
90 void readAndRotateFields
91 (
92  PtrList<GeoField>& flds,
93  const fvMesh& mesh,
94  const tensor& rotT,
95  const IOobjectList& objects
96 )
97 {
98  ReadFields(mesh, objects, flds);
99  forAll(flds, i)
100  {
101  Info<< "Transforming " << flds[i].name() << endl;
102  const dimensionedTensor dimT("t", flds[i].dimensions(), rotT);
103  transform(flds[i], dimT, flds[i]);
104  }
105 }
106 
107 
108 void rotateFields(const argList& args, const Time& runTime, const tensor& T)
109 {
110  #include "createNamedMesh.H"
111 
112  // Read objects in time directory
113  IOobjectList objects(mesh, runTime.timeName());
114 
115  // Read vol fields.
116 
118  readAndRotateFields(vsFlds, mesh, T, objects);
119 
121  readAndRotateFields(vvFlds, mesh, T, objects);
122 
124  readAndRotateFields(vstFlds, mesh, T, objects);
125 
126  PtrList<volSymmTensorField> vsymtFlds;
127  readAndRotateFields(vsymtFlds, mesh, T, objects);
128 
130  readAndRotateFields(vtFlds, mesh, T, objects);
131 
132  // Read surface fields.
133 
135  readAndRotateFields(ssFlds, mesh, T, objects);
136 
138  readAndRotateFields(svFlds, mesh, T, objects);
139 
141  readAndRotateFields(sstFlds, mesh, T, objects);
142 
144  readAndRotateFields(ssymtFlds, mesh, T, objects);
145 
147  readAndRotateFields(stFlds, mesh, T, objects);
148 
149  mesh.write();
150 }
151 
152 
153 
154 int main(int argc, char *argv[])
155 {
157  (
158  "Transform (translate / rotate / scale) mesh points.\n"
159  "Note: roll=rotate about x, pitch=rotate about y, yaw=rotate about z"
160  );
162  (
163  "time",
164  "time",
165  "Specify the time to search from and apply the transformation"
166  " (default is latest)"
167  );
169  (
170  "translate",
171  "vector",
172  "Translate by specified <vector> - eg, '(1 0 0)' before rotations"
173  );
175  (
176  "origin",
177  "point",
178  "Use specified <point> as origin for rotations"
179  );
181  (
182  "rotate",
183  "(vectorA vectorB)",
184  "Rotate from <vectorA> to <vectorB> - eg, '((1 0 0) (0 0 1))'"
185  );
187  (
188  "rotate-angle",
189  "(vector scalar)",
190  "Rotate <angle> degrees about <vector> - eg, '((1 0 0) 45)'"
191  );
193  (
194  "rollPitchYaw",
195  "vector",
196  "Rotate by '(roll pitch yaw)' degrees"
197  );
199  (
200  "yawPitchRoll",
201  "vector",
202  "Rotate by '(yaw pitch roll)' degrees"
203  );
205  (
206  "rotateFields",
207  "Read and transform vector and tensor fields too"
208  );
210  (
211  "scale",
212  "scalar | vector",
213  "Scale by the specified amount - Eg, for uniform [mm] to [m] scaling "
214  "use either '(0.001 0.001 0.001)' or simply '0.001'"
215  );
216 
217  #include "addRegionOption.H"
218  #include "setRootCase.H"
219 
220  const bool doRotateFields = args.found("rotateFields");
221 
222  // Verify that an operation has been specified
223  {
224  const List<word> operationNames
225  {
226  "translate",
227  "rotate",
228  "rotate-angle",
229  "rollPitchYaw",
230  "yawPitchRoll",
231  "scale"
232  };
233 
234  if (!args.count(operationNames))
235  {
236  FatalError
237  << "No operation supplied, "
238  << "use at least one of the following:" << nl
239  << " ";
240 
241  for (const auto& opName : operationNames)
242  {
243  FatalError
244  << " -" << opName;
245  }
246 
247  FatalError
248  << nl << exit(FatalError);
249  }
250  }
251 
252  #include "createTime.H"
253 
255  fileName meshDir = polyMesh::meshSubDir;
256 
257  if (args.readIfPresent("region", regionName))
258  {
260  }
261 
262  if (args.found("time"))
263  {
264  if (args["time"] == "constant")
265  {
266  runTime.setTime(instant(0, "constant"), 0);
267  }
268  else
269  {
270  const scalar timeValue = args.get<scalar>("time");
271  runTime.setTime(instant(timeValue), 0);
272  }
273  }
274 
276  (
277  IOobject
278  (
279  "points",
280  runTime.findInstance(meshDir, "points"),
281  meshDir,
282  runTime,
285  false
286  )
287  );
288 
289  vector v;
290  if (args.readIfPresent("translate", v))
291  {
292  Info<< "Translating points by " << v << endl;
293 
294  points += v;
295  }
296 
297  vector origin;
298  const bool useOrigin = args.readIfPresent("origin", origin);
299  if (useOrigin)
300  {
301  Info<< "Set origin for rotations to " << origin << endl;
302  points -= origin;
303  }
304 
305  if (args.found("rotate"))
306  {
307  Pair<vector> n1n2
308  (
309  args.lookup("rotate")()
310  );
311  n1n2[0].normalise();
312  n1n2[1].normalise();
313 
314  const tensor rot(rotationTensor(n1n2[0], n1n2[1]));
315 
316  Info<< "Rotating points by " << rot << endl;
317  points = transform(rot, points);
318 
319  if (doRotateFields)
320  {
321  rotateFields(args, runTime, rot);
322  }
323  }
324  else if (args.found("rotate-angle"))
325  {
326  const Tuple2<vector, scalar> rotAxisAngle
327  (
328  args.lookup("rotate-angle")()
329  );
330 
331  const vector& axis = rotAxisAngle.first();
332  const scalar& angle = rotAxisAngle.second();
333 
334  Info<< "Rotating points " << nl
335  << " about " << axis << nl
336  << " angle " << angle << nl;
337 
338  const tensor rot(axisAngle::rotation(axis, angle, true));
339 
340  Info<< "Rotating points by " << rot << endl;
341  points = transform(rot, points);
342 
343  if (doRotateFields)
344  {
345  rotateFields(args, runTime, rot);
346  }
347  }
348  else if (args.readIfPresent("rollPitchYaw", v))
349  {
350  Info<< "Rotating points by" << nl
351  << " roll " << v.x() << nl
352  << " pitch " << v.y() << nl
353  << " yaw " << v.z() << nl;
354 
355  const tensor rot(euler::rotation(euler::eulerOrder::XYZ, v, true));
356 
357  Info<< "Rotating points by " << rot << endl;
358  points = transform(rot, points);
359 
360  if (doRotateFields)
361  {
362  rotateFields(args, runTime, rot);
363  }
364  }
365  else if (args.readIfPresent("yawPitchRoll", v))
366  {
367  Info<< "Rotating points by" << nl
368  << " yaw " << v.x() << nl
369  << " pitch " << v.y() << nl
370  << " roll " << v.z() << nl;
371 
372  const tensor rot(euler::rotation(euler::eulerOrder::ZYX, v, true));
373 
374  Info<< "Rotating points by " << rot << endl;
375  points = transform(rot, points);
376 
377  if (doRotateFields)
378  {
379  rotateFields(args, runTime, rot);
380  }
381  }
382 
383  List<scalar> scaling;
384  if (args.readListIfPresent("scale", scaling))
385  {
386  // readListIfPresent handles single or multiple values
387 
388  if (scaling.size() == 1)
389  {
390  Info<< "Scaling points uniformly by " << scaling[0] << nl;
391  points *= scaling[0];
392  }
393  else if (scaling.size() == 3)
394  {
395  Info<< "Scaling points by ("
396  << scaling[0] << " "
397  << scaling[1] << " "
398  << scaling[2] << ")" << nl;
399 
403  }
404  else
405  {
406  FatalError
407  << "-scale with 1 or 3 components only" << nl
408  << "given: " << args["scale"] << endl
409  << exit(FatalError);
410  }
411  }
412 
413  if (useOrigin)
414  {
415  Info<< "Unset origin for rotations from " << origin << endl;
416  points += origin;
417  }
418 
419 
420  // Set the precision of the points data to 10
422 
423  Info<< "Writing points into directory " << points.path() << nl << endl;
424  points.write();
425 
426  Info<< nl << "End" << nl << endl;
427 
428  return 0;
429 }
430 
431 
432 // ************************************************************************* //
Foam::argList::count
label count(const UList< word > &optionNames) const
Return how many of the specified options were used.
Definition: argList.C:1569
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::Tensor< scalar >
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::Vector::x
const Cmpt & x() const
Access to the vector x component.
Definition: VectorI.H:73
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::Vector< scalar >::Z
Definition: Vector.H:81
EulerCoordinateRotation.H
Foam::Vector< scalar >::Y
Definition: Vector.H:81
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IOField
A primitive field of type <T> with automated input and output.
Definition: foamVtkLagrangianWriter.H:61
Foam::fvMesh::write
virtual bool write(const bool valid=true) const
Write mesh using IO settings from time.
Definition: fvMesh.C:895
Foam::polyMesh::defaultRegion
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:312
Foam::ReadFields
wordList ReadFields(const typename GeoMesh::Mesh &mesh, const IOobjectList &objects, PtrList< GeometricField< Type, PatchField, GeoMesh >> &fields, const bool syncPar=true, const bool readOldTime=false)
Read Geometric fields of templated type.
transformGeometricField.H
Spatial transformation functions for GeometricField.
Foam::polyMesh::meshSubDir
static word meshSubDir
Return the mesh sub-directory name (usually "polyMesh")
Definition: polyMesh.H:315
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:785
Foam::argList::addNote
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:413
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:123
Foam::argList::readListIfPresent
bool readListIfPresent(const word &optName, List< T > &list) const
Definition: argListI.H:373
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
surfaceFields.H
Foam::surfaceFields.
Foam::transform
dimensionSet transform(const dimensionSet &ds)
Return the argument; transformations do not change the dimensions.
Definition: dimensionSet.C:519
Foam::argList::get
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:257
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:302
Foam::Vector::z
const Cmpt & z() const
Access to the vector z component.
Definition: VectorI.H:85
transformField.H
Spatial transformation functions for primitive fields.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
regionName
Foam::word regionName
Definition: createNamedDynamicFvMesh.H:1
Foam::argList::lookup
ITstream lookup(const word &optName) const
Return an input stream from the named option.
Definition: argListI.H:163
Foam::coordinateRotations::axisAngle::rotation
static tensor rotation(const vector &axis, const scalar angle, bool degrees=false)
The rotation tensor for given axis/angle.
Definition: axisAngleRotation.C:65
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::coordinateRotations
Namespace for coordinate system rotations.
Definition: axesRotation.C:37
Foam::Vector< scalar >::X
Definition: Vector.H:81
argList.H
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
addRegionOption.H
axisAngleRotation.H
Foam::Field::replace
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:563
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::FatalError
error FatalError
createNamedMesh.H
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::dimensioned
Generic dimensioned Type class.
Definition: dimensionedScalarFwd.H:43
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Field::component
tmp< Field< cmptType > > component(const direction) const
Return a component field of the field.
Definition: Field.C:551
Foam::IOobjectList
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:55
Foam::Time::findInstance
word findInstance(const fileName &dir, const word &name=word::null, const IOobject::readOption rOpt=IOobject::MUST_READ, const word &stopInstance=word::null) const
Definition: Time.C:802
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:325
Time.H
setRootCase.H
Foam::Vector::y
const Cmpt & y() const
Access to the vector y component.
Definition: VectorI.H:79
ReadFields.H
Field reading functions for post-processing utilities.
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::Pair
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: Pair.H:54
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:333
Foam::Vector< scalar >
Foam::List< word >
Foam::Time::setTime
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:1006
points
const pointField & points
Definition: gmvOutputHeader.H:1
createTime.H
Foam::coordinateRotations::euler::rotation
static tensor rotation(const vector &angles, bool degrees=false)
Definition: EulerCoordinateRotation.C:239
Foam::instant
An instant of time. Contains the time value and name.
Definition: instant.H:52
Foam::rotationTensor
tensor rotationTensor(const vector &n1, const vector &n2)
Rotational transformation tensor from vector n1 to n2.
Definition: transform.H:51
Foam::Tuple2< vector, scalar >
Foam::argList::addOption
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:336
args
Foam::argList args(argc, argv)
pointFields.H
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:157
Foam::IOobject::MUST_READ
Definition: IOobject.H:120