Merge branch 'upstream/OpenFOAM' into master
[freefoam.git] / applications / utilities / mesh / manipulation / transformPoints / transformPoints.C
blob80f6e05feea6a326e73117b9acf21aeb8378a683
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Application
26     transformPoints
28 Description
29     Transforms the mesh points in the polyMesh directory according to the
30     translate, rotate and scale options.
32 Usage
33     Options are:
35     -translate vector
36         Translates the points by the given vector,
38     -rotate (vector vector)
39         Rotates the points from the first vector to the second,
41      or -yawPitchRoll (yawdegrees pitchdegrees rolldegrees)
42      or -rollPitchYaw (rolldegrees pitchdegrees yawdegrees)
44     -scale vector
45         Scales the points by the given vector.
47     The any or all of the three options may be specified and are processed
48     in the above order.
50     With -rotateFields (in combination with -rotate/yawPitchRoll/rollPitchYaw)
51     it will also read & transform vector & tensor fields.
53 Usage
55     - transformPoints [OPTIONS]
57     @param -translate \<vector\>\n
58     Translate by given vector.
60     @param -rotateFields \n
61     Also rotate vector/tensor fields.
63     @param -rotate \<(vector vector)\>\n
64     Rotate from first to second direction.
66     @param -scale \<vector (sx sy sz)\>\n
67     Scaling factors in x, y and z direction.
69     @param -case \<dir\>\n
70     Case directory.
72     @param -parallel \n
73     Run in parallel.
75     @param -help \n
76     Display help message.
78     @param -doc \n
79     Display Doxygen API documentation page for this application.
81     @param -srcDoc \n
82     Display Doxygen source documentation page for this application.
84 Note
85     - yaw: rotation about z
86     - pitch: rotation about y
87     - roll: rotation about x
88 \*---------------------------------------------------------------------------*/
90 #include <OpenFOAM/argList.H>
91 #include <OpenFOAM/Time.H>
92 #include <finiteVolume/fvMesh.H>
93 #include <finiteVolume/volFields.H>
94 #include <finiteVolume/surfaceFields.H>
95 #include <OpenFOAM/ReadFields.H>
96 #include <OpenFOAM/pointFields.H>
97 #include <OpenFOAM/transformField.H>
98 #include <OpenFOAM/transformGeometricField.H>
99 #include <OpenFOAM/IStringStream.H>
101 using namespace Foam;
102 using namespace Foam::mathematicalConstant;
104 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
106 template<class GeoField>
107 void readAndRotateFields
109     PtrList<GeoField>& flds,
110     const fvMesh& mesh,
111     const tensor& T,
112     const IOobjectList& objects
115     ReadFields(mesh, objects, flds);
116     forAll(flds, i)
117     {
118         Info<< "Transforming " << flds[i].name() << endl;
119         dimensionedTensor dimT("t", flds[i].dimensions(), T);
120         transform(flds[i], dimT, flds[i]);
121     }
125 void rotateFields(const Time& runTime, const tensor& T)
127 #   include <OpenFOAM/createMesh.H>
129     // Read objects in time directory
130     IOobjectList objects(mesh, runTime.timeName());
132     // Read vol fields.
134     PtrList<volScalarField> vsFlds;
135     readAndRotateFields(vsFlds, mesh, T, objects);
137     PtrList<volVectorField> vvFlds;
138     readAndRotateFields(vvFlds, mesh, T, objects);
140     PtrList<volSphericalTensorField> vstFlds;
141     readAndRotateFields(vstFlds, mesh, T, objects);
143     PtrList<volSymmTensorField> vsymtFlds;
144     readAndRotateFields(vsymtFlds, mesh, T, objects);
146     PtrList<volTensorField> vtFlds;
147     readAndRotateFields(vtFlds, mesh, T, objects);
149     // Read surface fields.
151     PtrList<surfaceScalarField> ssFlds;
152     readAndRotateFields(ssFlds, mesh, T, objects);
154     PtrList<surfaceVectorField> svFlds;
155     readAndRotateFields(svFlds, mesh, T, objects);
157     PtrList<surfaceSphericalTensorField> sstFlds;
158     readAndRotateFields(sstFlds, mesh, T, objects);
160     PtrList<surfaceSymmTensorField> ssymtFlds;
161     readAndRotateFields(ssymtFlds, mesh, T, objects);
163     PtrList<surfaceTensorField> stFlds;
164     readAndRotateFields(stFlds, mesh, T, objects);
166     mesh.write();
170 //  Main program:
172 int main(int argc, char *argv[])
174     argList::validOptions.insert("translate", "vector");
175     argList::validOptions.insert("rotate", "(vector vector)");
176     argList::validOptions.insert("rollPitchYaw", "(roll pitch yaw)");
177     argList::validOptions.insert("yawPitchRoll", "(yaw pitch roll)");
178     argList::validOptions.insert("rotateFields", "");
179     argList::validOptions.insert("scale", "vector");
181 #   include <OpenFOAM/setRootCase.H>
182 #   include <OpenFOAM/createTime.H>
184     pointIOField points
185     (
186         IOobject
187         (
188             "points",
189             runTime.findInstance(polyMesh::meshSubDir, "points"),
190             polyMesh::meshSubDir,
191             runTime,
192             IOobject::MUST_READ,
193             IOobject::NO_WRITE,
194             false
195         )
196     );
199     if (args.options().empty())
200     {
201         FatalErrorIn(args.executable())
202             << "No options supplied, please use one or more of "
203                "-translate, -rotate or -scale options."
204             << exit(FatalError);
205     }
207     if (args.optionFound("translate"))
208     {
209         vector transVector(args.optionLookup("translate")());
211         Info<< "Translating points by " << transVector << endl;
213         points += transVector;
214     }
216     if (args.optionFound("rotate"))
217     {
218         Pair<vector> n1n2(args.optionLookup("rotate")());
219         n1n2[0] /= mag(n1n2[0]);
220         n1n2[1] /= mag(n1n2[1]);
221         tensor T = rotationTensor(n1n2[0], n1n2[1]);
223         Info<< "Rotating points by " << T << endl;
225         points = transform(T, points);
227         if (args.optionFound("rotateFields"))
228         {
229             rotateFields(runTime, T);
230         }
231     }
232     else if (args.optionFound("rollPitchYaw"))
233     {
234         vector v(args.optionLookup("rollPitchYaw")());
236         Info<< "Rotating points by" << nl
237             << "    roll  " << v.x() << nl
238             << "    pitch " << v.y() << nl
239             << "    yaw   " << v.z() << endl;
242         // Convert to radians
243         v *= pi/180.0;
245         quaternion R(v.x(), v.y(), v.z());
247         Info<< "Rotating points by quaternion " << R << endl;
248         points = transform(R, points);
250         if (args.optionFound("rotateFields"))
251         {
252             rotateFields(runTime, R.R());
253         }
254     }
255     else if (args.optionFound("yawPitchRoll"))
256     {
257         vector v(args.optionLookup("yawPitchRoll")());
259         Info<< "Rotating points by" << nl
260             << "    yaw   " << v.x() << nl
261             << "    pitch " << v.y() << nl
262             << "    roll  " << v.z() << endl;
265         // Convert to radians
266         v *= pi/180.0;
268         scalar yaw = v.x();
269         scalar pitch = v.y();
270         scalar roll = v.z();
272         quaternion R = quaternion(vector(0, 0, 1), yaw);
273         R *= quaternion(vector(0, 1, 0), pitch);
274         R *= quaternion(vector(1, 0, 0), roll);
276         Info<< "Rotating points by quaternion " << R << endl;
277         points = transform(R, points);
279         if (args.optionFound("rotateFields"))
280         {
281             rotateFields(runTime, R.R());
282         }
283     }
285     if (args.optionFound("scale"))
286     {
287         vector scaleVector(args.optionLookup("scale")());
289         Info<< "Scaling points by " << scaleVector << endl;
291         points.replace(vector::X, scaleVector.x()*points.component(vector::X));
292         points.replace(vector::Y, scaleVector.y()*points.component(vector::Y));
293         points.replace(vector::Z, scaleVector.z()*points.component(vector::Z));
294     }
296     // Set the precision of the points data to 10
297     IOstream::defaultPrecision(10);
299     Info << "Writing points into directory " << points.path() << nl << endl;
300     points.write();
302     return 0;
306 // ************************ vim: set sw=4 sts=4 et: ************************ //