From 51b249025802ec7cd5dd5a43938dca2abd3b221a Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 22 Oct 2020 13:00:48 +0200 Subject: [PATCH] ENH: additional options for transformPoints and surfaceRefineRedGreen - add -recentre option to shift points according to the bounding box centre before performing other operations - add -auto-origin to use the bounding box centre as the origin for rotations. Has lower priority than the -origin option. - surfaceTransformPoints now has distinct -read-scale, -write-scale options (as per surfaceMeshConvert). Silently accepts -scale as equivalent to -write-scale, for backwards compatibility and similarity with transformPoints - add -steps option for surfaceRefineRedGreen for successive refinement --- .../transformPoints/transformPoints.C | 158 ++++++++++++---- .../surfaceRefineRedGreen.C | 56 ++++-- .../surfaceTransformPoints.C | 176 +++++++++++++----- .../rhoSimpleFoam/squareBend/Allclean | 11 ++ .../rhoSimpleFoam/squareBend/Allrun | 27 +++ 5 files changed, 325 insertions(+), 103 deletions(-) create mode 100755 tutorials/compressible/rhoSimpleFoam/squareBend/Allclean create mode 100755 tutorials/compressible/rhoSimpleFoam/squareBend/Allrun diff --git a/applications/utilities/mesh/manipulation/transformPoints/transformPoints.C b/applications/utilities/mesh/manipulation/transformPoints/transformPoints.C index 7f695abffb..769f3e79d9 100644 --- a/applications/utilities/mesh/manipulation/transformPoints/transformPoints.C +++ b/applications/utilities/mesh/manipulation/transformPoints/transformPoints.C @@ -41,11 +41,14 @@ Usage Specify the time to search from and apply the transformation (default is latest) + -recentre + Recentre using the bounding box centre before other operations + -translate vector - Translates the points by the given vector before rotations + Translate the points by the given vector before rotations -rotate (vector vector) - Rotates the points from the first vector to the second, + Rotate the points from the first vector to the second -rotate-angle (vector angle) Rotate angle degrees about vector axis. @@ -54,7 +57,7 @@ Usage or -rollPitchYaw (rolldegrees pitchdegrees yawdegrees) -scale scalar|vector - Scales the points by the given scalar or vector. + Scale the points by the given scalar or vector on output. The any or all of the three options may be specified and are processed in the above order. @@ -150,6 +153,85 @@ void rotateFields(const argList& args, const Time& runTime, const tensor& T) } +// Retrieve scaling option +// - size 0 : no scaling +// - size 1 : uniform scaling +// - size 3 : non-uniform scaling +List getScalingOpt(const word& optName, const argList& args) +{ + // readListIfPresent handles single or multiple values + // - accept 1 or 3 values + + List scaling; + args.readListIfPresent(optName, scaling); + + if (scaling.size() == 1) + { + // Uniform scaling + } + else if (scaling.size() == 3) + { + // Non-uniform, but may actually be uniform + if + ( + equal(scaling[0], scaling[1]) + && equal(scaling[0], scaling[2]) + ) + { + scaling.resize(1); + } + } + else if (!scaling.empty()) + { + FatalError + << "Incorrect number of components, must be 1 or 3." << nl + << " -" << optName << ' ' << args[optName].c_str() << endl + << exit(FatalError); + } + + if (scaling.size() == 1 && equal(scaling[0], 1)) + { + // Scale factor 1 == no scaling + scaling.clear(); + } + + for (const scalar scale : scaling) + { + if (scale <= 0) + { + FatalError + << "Invalid scaling value, must be positive." << nl + << " -" << optName << ' ' << args[optName].c_str() << endl + << exit(FatalError); + } + } + + return scaling; +} + + +void applyScaling(pointField& points, const List& scaling) +{ + if (scaling.size() == 1) + { + Info<< "Scaling points uniformly by " << scaling[0] << nl; + points *= scaling[0]; + } + else if (scaling.size() == 3) + { + Info<< "Scaling points by (" + << scaling[0] << ' ' + << scaling[1] << ' ' + << scaling[2] << ')' << nl; + + points.replace(vector::X, scaling[0]*points.component(vector::X)); + points.replace(vector::Y, scaling[1]*points.component(vector::Y)); + points.replace(vector::Z, scaling[2]*points.component(vector::Z)); + } +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // int main(int argc, char *argv[]) { @@ -165,11 +247,21 @@ int main(int argc, char *argv[]) "Specify the time to search from and apply the transformation" " (default is latest)" ); + argList::addBoolOption + ( + "recentre", + "Recentre the bounding box before other operations" + ); argList::addOption ( "translate", "vector", - "Translate by specified - eg, '(1 0 0)' before rotations" + "Translate by specified before rotations" + ); + argList::addBoolOption + ( + "auto-origin", + "Use bounding box centre as origin for rotations" ); argList::addOption ( @@ -214,6 +306,9 @@ int main(int argc, char *argv[]) "use either '(0.001 0.001 0.001)' or simply '0.001'" ); + // Compatibility with surfaceTransformPoints + argList::addOptionCompat("scale", {"write-scale", 0}); + #include "addRegionOption.H" #include "setRootCase.H" @@ -223,6 +318,7 @@ int main(int argc, char *argv[]) { const List operationNames { + "recentre", "translate", "rotate", "rotate-angle", @@ -286,16 +382,31 @@ int main(int argc, char *argv[]) ) ); + + // Begin operations + vector v; + if (args.found("recentre")) + { + v = boundBox(points).centre(); + Info<< "Adjust centre " << v << " -> (0 0 0)" << endl; + points -= v; + } + if (args.readIfPresent("translate", v)) { Info<< "Translating points by " << v << endl; - points += v; } vector origin; - const bool useOrigin = args.readIfPresent("origin", origin); + bool useOrigin = args.readIfPresent("origin", origin); + if (args.found("auto-origin") && !useOrigin) + { + useOrigin = true; + origin = boundBox(points).centre(); + } + if (useOrigin) { Info<< "Set origin for rotations to " << origin << endl; @@ -328,8 +439,8 @@ int main(int argc, char *argv[]) args.lookup("rotate-angle")() ); - const vector& axis = rotAxisAngle.first(); - const scalar& angle = rotAxisAngle.second(); + const vector& axis = rotAxisAngle.first(); + const scalar angle = rotAxisAngle.second(); Info<< "Rotating points " << nl << " about " << axis << nl @@ -380,35 +491,8 @@ int main(int argc, char *argv[]) } } - List scaling; - if (args.readListIfPresent("scale", scaling)) - { - // readListIfPresent handles single or multiple values - - if (scaling.size() == 1) - { - Info<< "Scaling points uniformly by " << scaling[0] << nl; - points *= scaling[0]; - } - else if (scaling.size() == 3) - { - Info<< "Scaling points by (" - << scaling[0] << " " - << scaling[1] << " " - << scaling[2] << ")" << nl; - - points.replace(vector::X, scaling[0]*points.component(vector::X)); - points.replace(vector::Y, scaling[1]*points.component(vector::Y)); - points.replace(vector::Z, scaling[2]*points.component(vector::Z)); - } - else - { - FatalError - << "-scale with 1 or 3 components only" << nl - << "given: " << args["scale"] << endl - << exit(FatalError); - } - } + // Output scaling + applyScaling(points, getScalingOpt("scale", args)); if (useOrigin) { diff --git a/applications/utilities/surface/surfaceRefineRedGreen/surfaceRefineRedGreen.C b/applications/utilities/surface/surfaceRefineRedGreen/surfaceRefineRedGreen.C index b5b6b0cd30..b92418e1d4 100644 --- a/applications/utilities/surface/surfaceRefineRedGreen/surfaceRefineRedGreen.C +++ b/applications/utilities/surface/surfaceRefineRedGreen/surfaceRefineRedGreen.C @@ -6,6 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2013 OpenFOAM Foundation + Copyright (C) 2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -63,33 +64,52 @@ int main(int argc, char *argv[]) argList::noParallel(); argList::addArgument("input", "The input surface file"); argList::addArgument("output", "The output surface file"); + argList::addOption + ( + "steps", + "N", + "Number of refinement steps (default: 1)" + ); argList args(argc, argv); - const fileName surfFileName = args[1]; - const fileName outFileName = args[2]; + const fileName surfFileName(args[1]); + const fileName outFileName(args[2]); Info<< "Reading surface from " << surfFileName << " ..." << endl; - triSurface surf1(surfFileName); + triSurface surf(surfFileName); - // Refine - triSurface surf2 = triSurfaceTools::redGreenRefine - ( - surf1, - identity(surf1.size()) //Hack: refine all - ); - - Info<< "Original surface:" << endl - << " triangles :" << surf1.size() << endl - << " vertices(used):" << surf1.nPoints() << endl - << "Refined surface:" << endl - << " triangles :" << surf2.size() << endl - << " vertices(used):" << surf2.nPoints() << endl << endl; + Info<< "Original surface:" << nl + << " triangles :" << surf.size() << nl + << " vertices(used):" << surf.nPoints() << endl; - Info<< "Writing refined surface to " << outFileName << " ..." << endl; + const label nsteps = + args.getCheckOrDefault