PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/applications/utilities/mesh/manipulation/transformPoints/transformPoints.C

https://github.com/lakeatmt/OpenFOAM-1.7.x
C | 288 lines | 165 code | 57 blank | 66 comment | 12 complexity | ee5145cc2298b21143606cd98b3494be MD5 | raw file
  1. /*---------------------------------------------------------------------------*\
  2. ========= |
  3. \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
  4. \\ / O peration |
  5. \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
  6. \\/ M anipulation |
  7. -------------------------------------------------------------------------------
  8. License
  9. This file is part of OpenFOAM.
  10. OpenFOAM is free software: you can redistribute it and/or modify it
  11. under the terms of the GNU General Public License as published by
  12. the Free Software Foundation, either version 3 of the License, or
  13. (at your option) any later version.
  14. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
  15. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
  20. Application
  21. transformPoints
  22. Description
  23. Transforms the mesh points in the polyMesh directory according to the
  24. translate, rotate and scale options.
  25. Usage
  26. Options are:
  27. -translate vector
  28. Translates the points by the given vector,
  29. -rotate (vector vector)
  30. Rotates the points from the first vector to the second,
  31. or -yawPitchRoll (yawdegrees pitchdegrees rolldegrees)
  32. or -rollPitchYaw (rolldegrees pitchdegrees yawdegrees)
  33. -scale vector
  34. Scales the points by the given vector.
  35. The any or all of the three options may be specified and are processed
  36. in the above order.
  37. With -rotateFields (in combination with -rotate/yawPitchRoll/rollPitchYaw)
  38. it will also read & transform vector & tensor fields.
  39. Note:
  40. yaw (rotation about z)
  41. pitch (rotation about y)
  42. roll (rotation about x)
  43. \*---------------------------------------------------------------------------*/
  44. #include "argList.H"
  45. #include "Time.H"
  46. #include "fvMesh.H"
  47. #include "volFields.H"
  48. #include "surfaceFields.H"
  49. #include "ReadFields.H"
  50. #include "pointFields.H"
  51. #include "transformField.H"
  52. #include "transformGeometricField.H"
  53. #include "IStringStream.H"
  54. using namespace Foam;
  55. using namespace Foam::mathematicalConstant;
  56. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
  57. template<class GeoField>
  58. void readAndRotateFields
  59. (
  60. PtrList<GeoField>& flds,
  61. const fvMesh& mesh,
  62. const tensor& T,
  63. const IOobjectList& objects
  64. )
  65. {
  66. ReadFields(mesh, objects, flds);
  67. forAll(flds, i)
  68. {
  69. Info<< "Transforming " << flds[i].name() << endl;
  70. dimensionedTensor dimT("t", flds[i].dimensions(), T);
  71. transform(flds[i], dimT, flds[i]);
  72. }
  73. }
  74. void rotateFields(const argList& args, const Time& runTime, const tensor& T)
  75. {
  76. # include "createNamedMesh.H"
  77. // Read objects in time directory
  78. IOobjectList objects(mesh, runTime.timeName());
  79. // Read vol fields.
  80. PtrList<volScalarField> vsFlds;
  81. readAndRotateFields(vsFlds, mesh, T, objects);
  82. PtrList<volVectorField> vvFlds;
  83. readAndRotateFields(vvFlds, mesh, T, objects);
  84. PtrList<volSphericalTensorField> vstFlds;
  85. readAndRotateFields(vstFlds, mesh, T, objects);
  86. PtrList<volSymmTensorField> vsymtFlds;
  87. readAndRotateFields(vsymtFlds, mesh, T, objects);
  88. PtrList<volTensorField> vtFlds;
  89. readAndRotateFields(vtFlds, mesh, T, objects);
  90. // Read surface fields.
  91. PtrList<surfaceScalarField> ssFlds;
  92. readAndRotateFields(ssFlds, mesh, T, objects);
  93. PtrList<surfaceVectorField> svFlds;
  94. readAndRotateFields(svFlds, mesh, T, objects);
  95. PtrList<surfaceSphericalTensorField> sstFlds;
  96. readAndRotateFields(sstFlds, mesh, T, objects);
  97. PtrList<surfaceSymmTensorField> ssymtFlds;
  98. readAndRotateFields(ssymtFlds, mesh, T, objects);
  99. PtrList<surfaceTensorField> stFlds;
  100. readAndRotateFields(stFlds, mesh, T, objects);
  101. mesh.write();
  102. }
  103. // Main program:
  104. int main(int argc, char *argv[])
  105. {
  106. # include "addRegionOption.H"
  107. argList::validOptions.insert("translate", "vector");
  108. argList::validOptions.insert("rotate", "(vector vector)");
  109. argList::validOptions.insert("rollPitchYaw", "(roll pitch yaw)");
  110. argList::validOptions.insert("yawPitchRoll", "(yaw pitch roll)");
  111. argList::validOptions.insert("rotateFields", "");
  112. argList::validOptions.insert("scale", "vector");
  113. # include "setRootCase.H"
  114. # include "createTime.H"
  115. word regionName = polyMesh::defaultRegion;
  116. fileName meshDir;
  117. if (args.optionReadIfPresent("region", regionName))
  118. {
  119. meshDir = regionName/polyMesh::meshSubDir;
  120. }
  121. else
  122. {
  123. meshDir = polyMesh::meshSubDir;
  124. }
  125. pointIOField points
  126. (
  127. IOobject
  128. (
  129. "points",
  130. runTime.findInstance(meshDir, "points"),
  131. meshDir,
  132. runTime,
  133. IOobject::MUST_READ,
  134. IOobject::NO_WRITE,
  135. false
  136. )
  137. );
  138. if (args.options().empty())
  139. {
  140. FatalErrorIn(args.executable())
  141. << "No options supplied, please use one or more of "
  142. "-translate, -rotate or -scale options."
  143. << exit(FatalError);
  144. }
  145. if (args.optionFound("translate"))
  146. {
  147. vector transVector(args.optionLookup("translate")());
  148. Info<< "Translating points by " << transVector << endl;
  149. points += transVector;
  150. }
  151. if (args.optionFound("rotate"))
  152. {
  153. Pair<vector> n1n2(args.optionLookup("rotate")());
  154. n1n2[0] /= mag(n1n2[0]);
  155. n1n2[1] /= mag(n1n2[1]);
  156. tensor T = rotationTensor(n1n2[0], n1n2[1]);
  157. Info<< "Rotating points by " << T << endl;
  158. points = transform(T, points);
  159. if (args.optionFound("rotateFields"))
  160. {
  161. rotateFields(args, runTime, T);
  162. }
  163. }
  164. else if (args.optionFound("rollPitchYaw"))
  165. {
  166. vector v(args.optionLookup("rollPitchYaw")());
  167. Info<< "Rotating points by" << nl
  168. << " roll " << v.x() << nl
  169. << " pitch " << v.y() << nl
  170. << " yaw " << v.z() << endl;
  171. // Convert to radians
  172. v *= pi/180.0;
  173. quaternion R(v.x(), v.y(), v.z());
  174. Info<< "Rotating points by quaternion " << R << endl;
  175. points = transform(R, points);
  176. if (args.optionFound("rotateFields"))
  177. {
  178. rotateFields(args, runTime, R.R());
  179. }
  180. }
  181. else if (args.optionFound("yawPitchRoll"))
  182. {
  183. vector v(args.optionLookup("yawPitchRoll")());
  184. Info<< "Rotating points by" << nl
  185. << " yaw " << v.x() << nl
  186. << " pitch " << v.y() << nl
  187. << " roll " << v.z() << endl;
  188. // Convert to radians
  189. v *= pi/180.0;
  190. scalar yaw = v.x();
  191. scalar pitch = v.y();
  192. scalar roll = v.z();
  193. quaternion R = quaternion(vector(0, 0, 1), yaw);
  194. R *= quaternion(vector(0, 1, 0), pitch);
  195. R *= quaternion(vector(1, 0, 0), roll);
  196. Info<< "Rotating points by quaternion " << R << endl;
  197. points = transform(R, points);
  198. if (args.optionFound("rotateFields"))
  199. {
  200. rotateFields(args, runTime, R.R());
  201. }
  202. }
  203. if (args.optionFound("scale"))
  204. {
  205. vector scaleVector(args.optionLookup("scale")());
  206. Info<< "Scaling points by " << scaleVector << endl;
  207. points.replace(vector::X, scaleVector.x()*points.component(vector::X));
  208. points.replace(vector::Y, scaleVector.y()*points.component(vector::Y));
  209. points.replace(vector::Z, scaleVector.z()*points.component(vector::Z));
  210. }
  211. // Set the precision of the points data to 10
  212. IOstream::defaultPrecision(10);
  213. Info << "Writing points into directory " << points.path() << nl << endl;
  214. points.write();
  215. return 0;
  216. }
  217. // ************************************************************************* //