ENH: mergePoints now with template for list types

- makes it possible to use list types other than UList.
  For example, UIndirectList<point>
This commit is contained in:
Mark Olesen
2017-05-30 18:21:01 +02:00
parent 104f43583f
commit 408b6c7e6b
6 changed files with 38 additions and 281 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -29,16 +29,18 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
template<class PointList>
Foam::label Foam::mergePoints
(
const UList<Type>& points,
const PointList& points,
const scalar mergeTol,
const bool verbose,
labelList& pointMap,
const Type& origin
typename PointList::const_reference origin
)
{
typedef typename PointList::value_type point_type;
// Create a old to new point mapping array
pointMap.setSize(points.size());
pointMap = -1;
@ -49,10 +51,10 @@ Foam::label Foam::mergePoints
}
// Explicitly convert to Field to support various list types
tmp<Field<Type>> tPoints(new Field<Type>(points));
tmp<Field<point_type>> tPoints(new Field<point_type>(points));
Type compareOrigin = origin;
if (origin == Type::max)
point_type compareOrigin = origin;
if (origin == point_type::max)
{
compareOrigin = sum(tPoints())/points.size();
}
@ -69,7 +71,7 @@ Foam::label Foam::mergePoints
const scalar mergeTolSqr = Foam::sqr(scalar(mergeTol));
// Sort points by magSqr
const Field<Type> d(tPoints - compareOrigin);
const Field<point_type> d(tPoints - compareOrigin);
List<scalar> magSqrD(d.size());
forAll(d, pointi)
@ -77,15 +79,16 @@ Foam::label Foam::mergePoints
magSqrD[pointi] = magSqr(d[pointi]);
}
labelList order;
sortedOrder(magSqrD, order);
Foam::sortedOrder(magSqrD, order);
Field<scalar> sortedTol(points.size());
forAll(order, sortI)
{
label pointi = order[sortI];
const label pointi = order[sortI];
// Convert to scalar precision
// NOTE: not yet using point_type template parameter
const point pt
(
scalar(d[pointi].x()),
@ -104,9 +107,11 @@ Foam::label Foam::mergePoints
for (label sortI = 1; sortI < order.size(); sortI++)
{
// Get original point index
label pointi = order[sortI];
const label pointi = order[sortI];
const scalar mag2 = magSqrD[order[sortI]];
// Convert to scalar precision
// NOTE: not yet using point_type template parameter
const point pt
(
scalar(points[pointi].x()),
@ -126,7 +131,10 @@ Foam::label Foam::mergePoints
prevSortI--
)
{
label prevPointi = order[prevSortI];
const label prevPointi = order[prevSortI];
// Convert to scalar precision
// NOTE: not yet using point_type template parameter
const point prevPt
(
scalar(points[prevPointi].x()),
@ -169,18 +177,18 @@ Foam::label Foam::mergePoints
}
template<class Type>
template<class PointList>
bool Foam::mergePoints
(
const UList<Type>& points,
const PointList& points,
const scalar mergeTol,
const bool verbose,
labelList& pointMap,
List<Type>& newPoints,
const Type& origin
List<typename PointList::value_type>& newPoints,
typename PointList::const_reference origin
)
{
label nUnique = mergePoints
const label nUnique = mergePoints
(
points,
mergeTol,

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -46,27 +46,28 @@ namespace Foam
//- Sorts and merges points. All points closer than/equal mergeTol get merged.
// Returns the number of unique points and a map from old to new.
template<class Type>
template<class PointList>
label mergePoints
(
const UList<Type>& points,
const PointList& points,
const scalar mergeTol,
const bool verbose,
labelList& pointMap,
const Type& origin = Type::zero
typename PointList::const_reference origin = PointList::value_type::zero
);
//- Sorts and merges points. Determines new points. Returns true if anything
// merged (though newPoints still sorted even if not merged).
template<class Type>
template<class PointList>
bool mergePoints
(
const UList<Type>& points,
const PointList& points,
const scalar mergeTol,
const bool verbose,
labelList& pointMap,
List<Type>& newPoints,
const Type& origin = Type::zero
List<typename PointList::value_type>& newPoints,
typename PointList::const_reference origin = PointList::value_type::zero
);
} // End namespace Foam

View File

@ -11,6 +11,4 @@ writer/ccmWriter.C
writer/ccmWriterMesh.C
writer/ccmWriterSolution.C
/* misc/mergePoints1.C */
LIB = $(FOAM_LIBBIN)/libccm

View File

@ -1,176 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
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 "ListOps.H"
#include "point.H"
#include "Field.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
// template<class Type, template<class> class ListType=UList>
template<class Type>
Foam::label Foam::mergePoints
(
const bool dummy,
const UIndirectList<Type>& points,
const scalar mergeTol,
const bool verbose,
labelList& pointMap,
const Type& origin
)
{
// Create a old to new point mapping array
pointMap.setSize(points.size());
pointMap = -1;
if (points.empty())
{
return 0;
}
// Explicitly convert to Field to support various list types
tmp<Field<Type>> tPoints(new Field<Type>(points));
Type compareOrigin = origin;
if (origin == Type::max)
{
compareOrigin = sum(tPoints())/points.size();
}
// We're comparing distance squared to origin first.
// Say if starting from two close points:
// x, y, z
// x+mergeTol, y+mergeTol, z+mergeTol
// Then the magSqr of both will be
// x^2+y^2+z^2
// x^2+y^2+z^2 + 2*mergeTol*(x+z+y) + mergeTol^2*...
// so the difference will be 2*mergeTol*(x+y+z)
const scalar mergeTolSqr = Foam::sqr(scalar(mergeTol));
// Sort points by magSqr
const Field<Type> d(tPoints - compareOrigin);
List<scalar> magSqrD(d.size());
forAll(d, pointi)
{
magSqrD[pointi] = magSqr(d[pointi]);
}
labelList order;
sortedOrder(magSqrD, order);
Field<scalar> sortedTol(points.size());
forAll(order, sortI)
{
label pointi = order[sortI];
// Convert to scalar precision
const point pt
(
scalar(d[pointi].x()),
scalar(d[pointi].y()),
scalar(d[pointi].z())
);
sortedTol[sortI] = 2*mergeTol*(mag(pt.x())+mag(pt.y())+mag(pt.z()));
}
label newPointi = 0;
// Handle 0th point separately (is always unique)
label pointi = order[0];
pointMap[pointi] = newPointi++;
for (label sortI = 1; sortI < order.size(); sortI++)
{
// Get original point index
label pointi = order[sortI];
const scalar mag2 = magSqrD[order[sortI]];
// Convert to scalar precision
const point pt
(
scalar(points[pointi].x()),
scalar(points[pointi].y()),
scalar(points[pointi].z())
);
// Compare to previous points to find equal one.
label equalPointi = -1;
for
(
label prevSortI = sortI - 1;
prevSortI >= 0
&& (mag(magSqrD[order[prevSortI]] - mag2) <= sortedTol[sortI]);
prevSortI--
)
{
label prevPointi = order[prevSortI];
const point prevPt
(
scalar(points[prevPointi].x()),
scalar(points[prevPointi].y()),
scalar(points[prevPointi].z())
);
if (magSqr(pt - prevPt) <= mergeTolSqr)
{
// Found match.
equalPointi = prevPointi;
break;
}
}
if (equalPointi != -1)
{
// Same coordinate as equalPointi. Map to same new point.
pointMap[pointi] = pointMap[equalPointi];
if (verbose)
{
Pout<< "Foam::mergePoints : Merging points "
<< pointi << " and " << equalPointi
<< " with coordinates:" << points[pointi]
<< " and " << points[equalPointi]
<< endl;
}
}
else
{
// Differs. Store new point.
pointMap[pointi] = newPointi++;
}
}
return newPointi;
}
// ************************************************************************* //

View File

@ -1,73 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
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/>.
Description
Merge points. See below.
SourceFiles
mergePoints.C
\*---------------------------------------------------------------------------*/
#ifndef mergePoints1_H
#define mergePoints1_H
#include "scalar.H"
#include "labelList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Function mergePoints Declaration
\*---------------------------------------------------------------------------*/
//- Sorts and merges points. All points closer than/equal mergeTol get merged.
// Returns the number of unique points and a map from old to new.
//template<class Type, template<class> class ListType=UList>
template<class Type>
label mergePoints
(
const bool dummy,
const UIndirectList<Type>& points,
const scalar mergeTol,
const bool verbose,
labelList& pointMap,
const Type& origin = Type::zero
);
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "mergePoints1.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -37,7 +37,7 @@ License
#include "PackedList.H"
#include "uindirectPrimitivePatch.H"
#include "SortableList.H"
#include "mergePoints1.H"
#include "mergePoints.H"
#include "ListOps.H"
#include "ccmInternal.H" // include last to avoid any strange interactions
@ -2002,9 +2002,8 @@ void Foam::ccm::reader::mergeInplaceInterfaces()
Info<< " patch " << patch0 << "," << patch1 << ": ("
<< nPatch0Faces << " and " << nPatch1Faces << " faces) " << flush;
label nMerged = mergePoints
const label nMerged = mergePoints
(
true,
pointsToMerge,
option().mergeTol(),
false,
@ -2017,9 +2016,9 @@ void Foam::ccm::reader::mergeInplaceInterfaces()
if (nMerged)
{
// Transcribe local to global addressing
forAll(mergedPointMap, lookupI)
forAll(mergedPointMap, i)
{
oldToNew[addr[lookupI]] = addr[mergedPointMap[lookupI]];
oldToNew[addr[i]] = addr[mergedPointMap[i]];
}
interfacesToMerge.append(interI);