Skip to content

Commit

Permalink
rotatedBoxToFace: New faceSource, the face equivalent of rotatedBoxTo…
Browse files Browse the repository at this point in the history
…Cell

Description
    A topoSetSource to select faces based on cell centres inside a rotated
    and/or skewed box.

    The box can be defined with an origin and three vectors; i, j, and k. The
    origin is one corner of the box, and the vectors are the edges connected to
    that corner.

    For example, the following defines a box rotated 45 degrees around the
    z-axis, with width and depth of 0.2, height of 200, and with a bottom left
    corner at (0.4 0.4 -100):

    \verbatim
        origin  (0.4 0.4 -100);
        i       (0.141421 0.141421 0);
        j       (-0.141421 0.141421 0);
        k       (0 0 200);
    \endverbatim

    Alternatively, the box can be defined using a non-rotated box and details
    of how it should be rotated. This syntax is triggered by the presence of
    the keyword "box". A standard bounding box is supplied, along with a centre
    of rotation and two vectors, n1 and n2. The rotation is taken to be that
    which transforms n1 onto n2.

    The above example can be equivalently specified in this alternative form as
    follows:

    \verbatim
        box     (0.4 0.4 -100) (0.6 0.6 100);
        centre  (0.4 0.4 0);
        n1      (1 0 0);
        n2      (1 1 0);
    \endverbatim
  • Loading branch information
Henry Weller committed Mar 21, 2022
1 parent 87855d8 commit acd5528
Show file tree
Hide file tree
Showing 3 changed files with 365 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/meshTools/sets/cellSources/rotatedBoxToCell/rotatedBoxToCell.C
Expand Up @@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
Expand Down Expand Up @@ -43,26 +43,26 @@ namespace Foam
void Foam::rotatedBoxToCell::combine(topoSet& set, const bool add) const
{
// Define a cell for the box
pointField boxPoints(8);
boxPoints[0] = origin_;
boxPoints[1] = origin_ + i_;
boxPoints[2] = origin_ + i_ + j_;
boxPoints[3] = origin_ + j_;
boxPoints[4] = origin_ + k_;
boxPoints[5] = origin_ + k_ + i_;
boxPoints[6] = origin_ + k_ + i_ + j_;
boxPoints[7] = origin_ + k_ + j_;

labelList boxVerts(8);
forAll(boxVerts, i)
{
boxVerts[i] = i;
}
const pointField boxPoints
(
{
origin_,
origin_ + i_,
origin_ + i_ + j_,
origin_ + j_,
origin_ + k_,
origin_ + k_ + i_,
origin_ + k_ + i_ + j_,
origin_ + k_ + j_
}
);

const labelList boxVerts({0, 1, 2, 3, 4, 5, 6, 7});

const cellModel& hex = *(cellModeller::lookup("hex"));

// Get outwards pointing faces.
faceList boxFaces(cellShape(hex, boxVerts).faces());
const faceList boxFaces(cellShape(hex, boxVerts).faces());

// Precalculate normals
vectorField boxFaceNormals(boxFaces.size());
Expand All @@ -81,9 +81,11 @@ void Foam::rotatedBoxToCell::combine(topoSet& set, const bool add) const

forAll(boxFaces, i)
{
const face& f = boxFaces[i];

if (((ctrs[celli] - boxPoints[f[0]]) & boxFaceNormals[i]) > 0)
if
(
((ctrs[celli] - boxPoints[boxFaces[i][0]]) & boxFaceNormals[i])
> 0
)
{
inside = false;
break;
Expand Down
188 changes: 188 additions & 0 deletions src/meshTools/sets/faceSources/rotatedBoxToFace/rotatedBoxToFace.C
@@ -0,0 +1,188 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2022 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/

#include "rotatedBoxToFace.H"
#include "polyMesh.H"
#include "cellModeller.H"
#include "transform.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
defineTypeNameAndDebug(rotatedBoxToFace, 0);
addToRunTimeSelectionTable(topoSetSource, rotatedBoxToFace, word);
}


// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //

void Foam::rotatedBoxToFace::combine(topoSet& set, const bool add) const
{
// Define a cell for the box
const pointField boxPoints
(
{
origin_,
origin_ + i_,
origin_ + i_ + j_,
origin_ + j_,
origin_ + k_,
origin_ + k_ + i_,
origin_ + k_ + i_ + j_,
origin_ + k_ + j_
}
);

const labelList boxVerts({0, 1, 2, 3, 4, 5, 6, 7});

const cellModel& hex = *(cellModeller::lookup("hex"));

// Get outwards pointing faces.
const faceList boxFaces(cellShape(hex, boxVerts).faces());

// Precalculate normals
vectorField boxFaceNormals(boxFaces.size());
forAll(boxFaces, i)
{
boxFaceNormals[i] = boxFaces[i].area(boxPoints);
}

// Check whether face centre is inside all faces of box.

const pointField& ctrs = mesh_.faceCentres();

forAll(ctrs, facei)
{
bool inside = true;

forAll(boxFaces, i)
{
if
(
((ctrs[facei] - boxPoints[boxFaces[i][0]]) & boxFaceNormals[i])
> 0
)
{
inside = false;
break;
}
}

if (inside)
{
addOrDelete(set, facei, add);
}
}
}


// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //

Foam::rotatedBoxToFace::rotatedBoxToFace
(
const polyMesh& mesh,
const vector& origin,
const vector& i,
const vector& j,
const vector& k
)
:
topoSetSource(mesh),
origin_(origin),
i_(i),
j_(j),
k_(k)
{}


Foam::rotatedBoxToFace::rotatedBoxToFace
(
const polyMesh& mesh,
const dictionary& dict
)
:
topoSetSource(mesh),
origin_(),
i_(),
j_(),
k_()
{
if (dict.found("box"))
{
const boundBox bb(dict.lookup("box"));
const vector c(dict.lookupOrDefault<vector>("centre", bb.midpoint()));
const vector n1(normalised(dict.lookup<vector>("n1")));
const vector n2(normalised(dict.lookup<vector>("n2")));

const tensor R(rotationTensor(n1, n2));
const pointField bbPoints(bb.points());

origin_ = (R & (bb.min() - c)) + c;
i_ = R & (bbPoints[1] - bb.min());
j_ = R & (bbPoints[3] - bb.min());
k_ = R & (bbPoints[4] - bb.min());
}
else
{
origin_ = dict.lookup<point>("origin");
i_ = dict.lookup<vector>("i");
j_ = dict.lookup<vector>("j");
k_ = dict.lookup<vector>("k");
}
}


// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //

Foam::rotatedBoxToFace::~rotatedBoxToFace()
{}


// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //

void Foam::rotatedBoxToFace::applyToSet
(
const topoSetSource::setAction action,
topoSet& set
) const
{
if ((action == topoSetSource::NEW) || (action == topoSetSource::ADD))
{
Info<< " Adding faces with center within rotated box " << endl;

combine(set, true);
}
else if (action == topoSetSource::DELETE)
{
Info<< " Removing faces with center within rotated box " << endl;

combine(set, false);
}
}


// ************************************************************************* //

0 comments on commit acd5528

Please sign in to comment.