surfaceTransformPoints: Updated to use the new transformer class

Description
    Transform (translate, rotate, scale) a surface.

Usage
    \b surfaceTransformPoints "\<transformations\>" \<input\> \<output\>
    Supported transformations:
      - \par translate=<translation vector>
        Translational transformation by given vector
      - \par rotate=(\<n1 vector\> \<n2 vector\>)
        Rotational transformation from unit vector n1 to n2
      - \par Rx=\<angle [deg] about x-axis\>
        Rotational transformation by given angle about x-axis
      - \par Ry=\<angle [deg] about y-axis\>
        Rotational transformation by given angle about y-axis
      - \par Rz=\<angle [deg] about z-axis\>
        Rotational transformation by given angle about z-axis
      - \par Ra=\<axis vector\> \<angle [deg] about axis\>
        Rotational transformation by given angle about given axis
      - \par scale=\<x-y-z scaling vector\>
        Anisotropic scaling by the given vector in the x, y, z
        coordinate directions

    Example usage:
        surfaceTransformPoints \
            "translate=(-0.586 0 -0.156), \
            Ry=3.485, \
            translate=(0.586 0 0.156)" \
            constant/geometry/w3_orig.stl constant/geometry/w3.stl
This commit is contained in:
Henry Weller
2021-03-29 16:14:48 +01:00
parent 45dca30c51
commit 0d679d926a
4 changed files with 87 additions and 56 deletions

View File

@ -27,30 +27,39 @@ Application
Description Description
Transform (translate, rotate, scale) a surface. Transform (translate, rotate, scale) a surface.
The rollPitchYaw option takes three angles (degrees):
- roll (rotation about x) followed by
- pitch (rotation about y) followed by
- yaw (rotation about z)
The yawPitchRoll does yaw followed by pitch followed by roll.
Usage Usage
\b surfaceTransformPoints "\<transformations\>" \<input\> \<output\> \b surfaceTransformPoints "\<transformations\>" \<input\> \<output\>
Supported transformations:
- \par translate=<translation vector>
Translational transformation by given vector
- \par rotate=(\<n1 vector\> \<n2 vector\>)
Rotational transformation from unit vector n1 to n2
- \par Rx=\<angle [deg] about x-axis\>
Rotational transformation by given angle about x-axis
- \par Ry=\<angle [deg] about y-axis\>
Rotational transformation by given angle about y-axis
- \par Rz=\<angle [deg] about z-axis\>
Rotational transformation by given angle about z-axis
- \par Ra=\<axis vector\> \<angle [deg] about axis\>
Rotational transformation by given angle about given axis
- \par scale=\<x-y-z scaling vector\>
Anisotropic scaling by the given vector in the x, y, z
coordinate directions
Example usage: Example usage:
surfaceTransformPoints \ surfaceTransformPoints \
"translate=(-0.586 0 -0.156), \ "translate=(-0.586 0 -0.156), \
rollPitchYaw=(0 -3.485 0), \ Ry=3.485, \
translate=(0.586 0 0.156)" \ translate=(0.586 0 0.156)" \
constant/geometry/w3_orig.stl constant/geometry/w3.stl constant/geometry/w3_orig.stl constant/geometry/w3.stl
See also See also
Foam::transformer
transformPoints transformPoints
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "argList.H" #include "argList.H"
#include "quaternion.H"
#include "unitConversion.H" #include "unitConversion.H"
#include "MeshedSurfaces.H" #include "MeshedSurfaces.H"
@ -62,10 +71,27 @@ int main(int argc, char *argv[])
{ {
#include "removeCaseOptions.H" #include "removeCaseOptions.H"
argList::addNote const wordList supportedTransformations
( (
"Transform (translate, rotate, scale) a surface." {"translate", "rotate", "Rx", "Ry", "Rz", "Ra", "scale"}
); );
{
OStringStream supportedTransformationsStr;
supportedTransformationsStr << supportedTransformations << endl;
argList::addNote
(
"Transforms a surface e.g.\n"
"surfaceTransformPoints "
"\"translate=(-0.586 0 -0.156), "
"Ry=3.485, "
"translate=(0.586 0 0.156)\" "
"surf.stl tranformedSurf.obj\n\n"
"Supported transformations " + supportedTransformationsStr.str()
);
}
argList::noParallel(); argList::noParallel();
argList::validArgs.append("transformations"); argList::validArgs.append("transformations");
argList::validArgs.append("surface file"); argList::validArgs.append("surface file");
@ -84,17 +110,14 @@ int main(int argc, char *argv[])
List<Tuple2<word, string>> transformations; List<Tuple2<word, string>> transformations;
dictArgList(transformationString, simpleTransformations, transformations); dictArgList(transformationString, simpleTransformations, transformations);
meshedSurface surf1(surfFileName); transformer transforms;
pointField points(surf1.points());
forAll(transformations, i) forAll(transformations, i)
{ {
if (transformations[i].first() == "translate") if (transformations[i].first() == "translate")
{ {
const vector v(IStringStream(transformations[i].second())()); const vector v(IStringStream(transformations[i].second())());
Info<< "Translating points by " << v << endl; transforms = transformer::translation(v) & transforms;
points += v;
} }
else if (transformations[i].first() == "rotate") else if (transformations[i].first() == "rotate")
{ {
@ -103,52 +126,60 @@ int main(int argc, char *argv[])
n1n2[0] /= mag(n1n2[0]); n1n2[0] /= mag(n1n2[0]);
n1n2[1] /= mag(n1n2[1]); n1n2[1] /= mag(n1n2[1]);
const tensor T = rotationTensor(n1n2[0], n1n2[1]); transforms =
transformer::rotation(rotationTensor(n1n2[0], n1n2[1]))
Info<< "Rotating points by " << T << endl; & transforms;
points = transform(T, points);
} }
else if (transformations[i].first() == "rollPitchYaw") else if (transformations[i].first() == "Rx")
{ {
const vector v(IStringStream(transformations[i].second())()); const scalar a
Info<< "Rotating points by "
<< " roll = " << v.x()
<< ", pitch = " << v.y()
<< ", yaw = " << v.z() << nl;
const quaternion R(quaternion::rotationSequence::XYZ, degToRad(v));
points = transform(R, points);
}
else if (transformations[i].first() == "yawPitchRoll")
{
const vector v(IStringStream(transformations[i].second())());
Info<< "Rotating points by "
<< " yaw " << v.x()
<< ", pitch " << v.y()
<< ", roll " << v.z() << nl;
const quaternion R
( (
quaternion::rotationSequence::ZYX, readScalar(IStringStream(transformations[i].second())())
degToRad(vector(v.z(), v.y(), v.x()))
); );
points = transform(R, points); transforms = transformer::rotation(Rx(degToRad(a))) & transforms;
}
else if (transformations[i].first() == "Ry")
{
const scalar a
(
readScalar(IStringStream(transformations[i].second())())
);
transforms = transformer::rotation(Ry(degToRad(a))) & transforms;
}
else if (transformations[i].first() == "Rz")
{
const scalar a
(
readScalar(IStringStream(transformations[i].second())())
);
transforms = transformer::rotation(Rz(degToRad(a))) & transforms;
}
else if (transformations[i].first() == "Ra")
{
IStringStream istr(transformations[i].second());
const vector v(istr);
const scalar a(readScalar(istr));
transforms = transformer::rotation(Ra(v, degToRad(a))) & transforms;
} }
else if (transformations[i].first() == "scale") else if (transformations[i].first() == "scale")
{ {
const vector v(IStringStream(transformations[i].second())()); const vector v(IStringStream(transformations[i].second())());
transforms =
Info<< "Scaling points by " << v << endl; transformer::scaling(diagTensor(v.x(), v.y(), v.z()))
& transforms;
points.replace(vector::X, v.x()*points.component(vector::X)); }
points.replace(vector::Y, v.y()*points.component(vector::Y)); else
points.replace(vector::Z, v.z()*points.component(vector::Z)); {
args.printUsage();
FatalErrorInFunction
<< "Unknown transformation " << transformations[i].first()
<< exit(FatalError);
} }
} }
meshedSurface surf1(surfFileName);
pointField points(surf1.points());
transforms.transformPosition(points, points);
surf1.movePoints(points); surf1.movePoints(points);
surf1.write(outFileName); surf1.write(outFileName);

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -25,8 +25,8 @@ Class
Foam::transformer Foam::transformer
Description Description
Vector-tensor class used to perform translations and rotations in Vector-tensor class used to perform translations, rotations and scaling
3D space. operations in 3D space.
SourceFiles SourceFiles
transformerI.H transformerI.H

View File

@ -6,7 +6,7 @@ cd ${0%/*} || exit 1 # Run from this directory
runApplication surfaceTransformPoints \ runApplication surfaceTransformPoints \
"translate=(-0.586 0 -0.156), \ "translate=(-0.586 0 -0.156), \
rollPitchYaw=(0 -3.485 0), \ Ry=3.485, \
translate=(0.586 0 0.156)" \ translate=(0.586 0 0.156)" \
constant/geometry/w3_orig.stl constant/geometry/w3.stl constant/geometry/w3_orig.stl constant/geometry/w3.stl

View File

@ -6,7 +6,7 @@ cd ${0%/*} || exit 1 # Run from this directory
runApplication surfaceTransformPoints \ runApplication surfaceTransformPoints \
"translate=(-0.586 0 -0.156), \ "translate=(-0.586 0 -0.156), \
rollPitchYaw=(0 -3.485 0), \ Ry=3.485, \
translate=(0.586 0 0.156)" \ translate=(0.586 0 0.156)" \
constant/geometry/w3_orig.stl constant/geometry/w3.stl constant/geometry/w3_orig.stl constant/geometry/w3.stl