From 14cc288672091a0e1f08f6429f9e54ffd2da6baf Mon Sep 17 00:00:00 2001 From: Will Bainbridge Date: Tue, 6 Jun 2017 12:01:02 +0100 Subject: [PATCH] tetrahedron: triangle: Improved barycentric handling on tets and tris Updated the tetrahedron and triangle classes to use the barycentric primitives. Removed duplicate code for generating random positions in tets and tris, and fixed bug in tri random position. --- .../cellShapeControl/cellShapeControl.C | 8 +- .../cellShapeControlMesh.C | 4 +- .../cellShapeControlMesh.H | 4 +- .../nonUniformField/nonUniformField.C | 5 +- src/OpenFOAM/Make/files | 1 + .../primitiveShapes/tetrahedron/tetrahedron.H | 16 ++- .../tetrahedron/tetrahedronI.H | 88 +++++-------- .../primitiveShapes/triangle/triangle.H | 18 ++- .../primitiveShapes/triangle/triangleI.H | 52 ++++---- .../primitives/Barycentric2D/Barycentric2D.H | 114 +++++++++++++++++ .../primitives/Barycentric2D/Barycentric2DI.H | 120 ++++++++++++++++++ .../barycentric2D/barycentric2D.C | 75 +++++++++++ .../barycentric2D/barycentric2D.H | 74 +++++++++++ .../cellPointWeight/cellPointWeight.C | 15 +-- .../cellPointWeight/cellPointWeight.H | 15 ++- .../interpolationCellPointI.H | 9 +- .../interpolationCellPointWallModifiedI.H | 6 +- 17 files changed, 497 insertions(+), 127 deletions(-) create mode 100644 src/OpenFOAM/primitives/Barycentric2D/Barycentric2D.H create mode 100644 src/OpenFOAM/primitives/Barycentric2D/Barycentric2DI.H create mode 100644 src/OpenFOAM/primitives/Barycentric2D/barycentric2D/barycentric2D.C create mode 100644 src/OpenFOAM/primitives/Barycentric2D/barycentric2D/barycentric2D.H diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControl/cellShapeControl.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControl/cellShapeControl.C index 343ba46a6e..f84e010d04 100644 --- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControl/cellShapeControl.C +++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControl/cellShapeControl.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2012-2016 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2012-2017 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -97,7 +97,7 @@ Foam::scalarField Foam::cellShapeControl::cellSize Foam::scalar Foam::cellShapeControl::cellSize(const point& pt) const { - FixedList bary; + barycentric bary; cellShapeControlMesh::Cell_handle ch; shapeControlMesh_.barycentricCoords(pt, bary, ch); @@ -172,7 +172,7 @@ Foam::scalar Foam::cellShapeControl::cellSize(const point& pt) const Foam::tensor Foam::cellShapeControl::cellAlignment(const point& pt) const { - FixedList bary; + barycentric bary; cellShapeControlMesh::Cell_handle ch; shapeControlMesh_.barycentricCoords(pt, bary, ch); @@ -244,7 +244,7 @@ void Foam::cellShapeControl::cellSizeAndAlignment tensor& alignment ) const { - FixedList bary; + barycentric bary; cellShapeControlMesh::Cell_handle ch; shapeControlMesh_.barycentricCoords(pt, bary, ch); diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControlMesh/cellShapeControlMesh.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControlMesh/cellShapeControlMesh.C index 46140b7ae5..35734e40c5 100644 --- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControlMesh/cellShapeControlMesh.C +++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControlMesh/cellShapeControlMesh.C @@ -452,7 +452,7 @@ Foam::cellShapeControlMesh::~cellShapeControlMesh() void Foam::cellShapeControlMesh::barycentricCoords ( const Foam::point& pt, - FixedList& bary, + barycentric& bary, Cell_handle& ch ) const { @@ -472,7 +472,7 @@ void Foam::cellShapeControlMesh::barycentricCoords topoint(ch->vertex(3)->point()) ); - tet.barycentric(pt, bary); + bary = tet.pointToBarycentric(pt); } } diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControlMesh/cellShapeControlMesh.H b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControlMesh/cellShapeControlMesh.H index 7e4bc9041c..507809ce76 100644 --- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControlMesh/cellShapeControlMesh.H +++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellShapeControl/cellShapeControlMesh/cellShapeControlMesh.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2012-2016 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2012-2017 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -122,7 +122,7 @@ public: void barycentricCoords ( const Foam::point& pt, - FixedList& bary, + barycentric& bary, Cell_handle& ch ) const; diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellSizeControlSurfaces/surfaceCellSizeFunction/nonUniformField/nonUniformField.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellSizeControlSurfaces/surfaceCellSizeFunction/nonUniformField/nonUniformField.C index a478f9e513..a73243f760 100644 --- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellSizeControlSurfaces/surfaceCellSizeFunction/nonUniformField/nonUniformField.C +++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/cellSizeControlSurfaces/surfaceCellSizeFunction/nonUniformField/nonUniformField.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2012-2016 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2012-2017 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -118,8 +118,7 @@ Foam::scalar Foam::nonUniformField::interpolate pts[faceHitByPt[2]] ); - FixedList bary; - tri.barycentric(pt, bary); + const barycentric2D bary = tri.pointToBarycentric(pt); // return pointCellSize_[pMap[faceHitByPt[0]]]*bary[0] // + pointCellSize_[pMap[faceHitByPt[1]]]*bary[1] diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index f9b6810e54..ee25734b09 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -129,6 +129,7 @@ primitives/polynomialEqns/cubicEqn/cubicEqn.C primitives/polynomialEqns/quadraticEqn/quadraticEqn.C primitives/Barycentric/barycentric/barycentric.C +primitives/Barycentric2D/barycentric2D/barycentric2D.C containers/HashTables/HashTable/HashTableCore.C containers/HashTables/StaticHashTable/StaticHashTableCore.C diff --git a/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H b/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H index 80082ecce1..5f97ad2ac5 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H +++ b/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedron.H @@ -48,6 +48,7 @@ SourceFiles #include "UList.H" #include "triPointRef.H" #include "boundBox.H" +#include "barycentric.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -174,13 +175,18 @@ public: // uniform distribution inline Point randomPoint(cachedRandom& rndGen) const; - //- Calculate the barycentric coordinates of the given - // point, in the same order as a, b, c, d. Returns the - // determinant of the solution. - inline scalar barycentric + //- Calculate the point from the given barycentric coordinates. + inline Point barycentricToPoint(const barycentric& bary) const; + + //- Calculate the barycentric coordinates from the given point + inline barycentric pointToBarycentric(const point& pt) const; + + //- Calculate the barycentric coordinates from the given point. + // Returns the determinant. + inline scalar pointToBarycentric ( const point& pt, - FixedList& bary + barycentric& bary ) const; //- Return nearest point to p on tetrahedron. Is p itself diff --git a/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedronI.H b/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedronI.H index 08ed1816a9..a3293d3f00 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedronI.H +++ b/src/OpenFOAM/meshes/primitiveShapes/tetrahedron/tetrahedronI.H @@ -245,33 +245,7 @@ inline Point Foam::tetrahedron::randomPoint Random& rndGen ) const { - // Adapted from - // http://vcg.isti.cnr.it/activities/geometryegraphics/pointintetraedro.html - - scalar s = rndGen.scalar01(); - scalar t = rndGen.scalar01(); - scalar u = rndGen.scalar01(); - - if (s + t > 1.0) - { - s = 1.0 - s; - t = 1.0 - t; - } - - if (t + u > 1.0) - { - scalar tmp = u; - u = 1.0 - s - t; - t = 1.0 - tmp; - } - else if (s + t + u > 1.0) - { - scalar tmp = u; - u = s + t + u - 1.0; - s = 1.0 - t - tmp; - } - - return (1 - s - t - u)*a_ + s*b_ + t*c_ + u*d_; + return barycentricToPoint(barycentric01(rndGen)); } @@ -281,41 +255,37 @@ inline Point Foam::tetrahedron::randomPoint cachedRandom& rndGen ) const { - // Adapted from - // http://vcg.isti.cnr.it/activities/geometryegraphics/pointintetraedro.html - - scalar s = rndGen.sample01(); - scalar t = rndGen.sample01(); - scalar u = rndGen.sample01(); - - if (s + t > 1.0) - { - s = 1.0 - s; - t = 1.0 - t; - } - - if (t + u > 1.0) - { - scalar tmp = u; - u = 1.0 - s - t; - t = 1.0 - tmp; - } - else if (s + t + u > 1.0) - { - scalar tmp = u; - u = s + t + u - 1.0; - s = 1.0 - t - tmp; - } - - return (1 - s - t - u)*a_ + s*b_ + t*c_ + u*d_; + return barycentricToPoint(barycentric01(rndGen)); } template -Foam::scalar Foam::tetrahedron::barycentric +inline Point Foam::tetrahedron::barycentricToPoint +( + const barycentric& bary +) const +{ + return bary[0]*a_ + bary[1]*b_ + bary[2]*c_ + bary[3]*d_; +} + + +template +inline Foam::barycentric Foam::tetrahedron::pointToBarycentric +( + const point& pt +) const +{ + barycentric bary; + pointToBarycentric(pt, bary); + return bary; +} + + +template +inline Foam::scalar Foam::tetrahedron::pointToBarycentric ( const point& pt, - FixedList& bary + barycentric& bary ) const { // Reference: @@ -336,9 +306,9 @@ Foam::scalar Foam::tetrahedron::barycentric if (Foam::mag(detT) < SMALL) { - // Degenerate tetrahedron, returning 1/4 barycentric coordinates. + // Degenerate tetrahedron, returning 1/4 barycentric coordinates - bary = List(4, 0.25); + bary = barycentric(0.25, 0.25, 0.25, 0.25); return detT; } @@ -348,7 +318,7 @@ Foam::scalar Foam::tetrahedron::barycentric bary[0] = res.x(); bary[1] = res.y(); bary[2] = res.z(); - bary[3] = (1.0 - res.x() - res.y() - res.z()); + bary[3] = 1 - cmptSum(res); return detT; } diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H index d3838166d9..9e6073d46f 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H +++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -44,6 +44,7 @@ SourceFiles #include "FixedList.H" #include "UList.H" #include "linePointRef.H" +#include "barycentric2D.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -167,13 +168,18 @@ public: // distribution inline Point randomPoint(cachedRandom& rndGen) const; - //- Calculate the barycentric coordinates of the given - // point, in the same order as a, b, c. Returns the - // determinant of the solution. - inline scalar barycentric + //- Calculate the point from the given barycentric coordinates. + inline Point barycentricToPoint(const barycentric2D& bary) const; + + //- Calculate the barycentric coordinates from the given point + inline barycentric2D pointToBarycentric(const point& pt) const; + + //- Calculate the barycentric coordinates from the given point. + // Returns the determinant. + inline scalar pointToBarycentric ( const point& pt, - FixedList& bary + barycentric2D& bary ) const; //- Return point intersection with a ray. diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H index 9b9ec18b15..215cde621b 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H +++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangleI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -231,16 +231,7 @@ inline Foam::tensor Foam::triangle::inertia template inline Point Foam::triangle::randomPoint(Random& rndGen) const { - // Generating Random Points in Triangles - // by Greg Turk - // from "Graphics Gems", Academic Press, 1990 - // http://tog.acm.org/GraphicsGems/gems/TriPoints.c - - scalar s = rndGen.scalar01(); - - scalar t = sqrt(rndGen.scalar01()); - - return (1 - t)*a_ + (1 - s)*t*b_ + s*t*c_; + return barycentricToPoint(barycentric2D01(rndGen)); } @@ -250,24 +241,37 @@ inline Point Foam::triangle::randomPoint cachedRandom& rndGen ) const { - // Generating Random Points in Triangles - // by Greg Turk - // from "Graphics Gems", Academic Press, 1990 - // http://tog.acm.org/GraphicsGems/gems/TriPoints.c - - scalar s = rndGen.sample01(); - - scalar t = sqrt(rndGen.sample01()); - - return (1 - t)*a_ + (1 - s)*t*b_ + s*t*c_; + return barycentricToPoint(barycentric2D01(rndGen)); } template -Foam::scalar Foam::triangle::barycentric +inline Point Foam::triangle::barycentricToPoint +( + const barycentric2D& bary +) const +{ + return bary[0]*a_ + bary[1]*b_ + bary[2]*c_; +} + + +template +inline Foam::barycentric2D Foam::triangle::pointToBarycentric +( + const point& pt +) const +{ + barycentric2D bary; + pointToBarycentric(pt, bary); + return bary; +} + + +template +inline Foam::scalar Foam::triangle::pointToBarycentric ( const point& pt, - FixedList& bary + barycentric2D& bary ) const { // Reference: @@ -289,7 +293,7 @@ Foam::scalar Foam::triangle::barycentric { // Degenerate triangle, returning 1/3 barycentric coordinates. - bary = FixedList(1.0/3.0); + bary = barycentric2D(1.0/3.0, 1.0/3.0, 1.0/3.0); return denom; } diff --git a/src/OpenFOAM/primitives/Barycentric2D/Barycentric2D.H b/src/OpenFOAM/primitives/Barycentric2D/Barycentric2D.H new file mode 100644 index 0000000000..2d47bfeada --- /dev/null +++ b/src/OpenFOAM/primitives/Barycentric2D/Barycentric2D.H @@ -0,0 +1,114 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2017 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 . + +Class + Foam::Barycentric2D + +Description + Templated 2D Barycentric derived from VectorSpace. Has 3 components, one of + which is redundant. + +SourceFiles + Barycentric2DI.H + +\*---------------------------------------------------------------------------*/ + +#ifndef Barycentric2D_H +#define Barycentric2D_H + +#include "VectorSpace.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class Barycentric2D Declaration +\*---------------------------------------------------------------------------*/ + +template +class Barycentric2D +: + public VectorSpace, Cmpt, 3> +{ +public: + + //- Equivalent type of labels used for valid component indexing + typedef Barycentric2D