From d21091c7887ecf5f4953745fb034d42be14eddb7 Mon Sep 17 00:00:00 2001 From: mattijs Date: Thu, 8 Dec 2011 16:23:09 +0000 Subject: [PATCH] ENH: readSTLASCII: single precision point merging --- src/OpenFOAM/meshes/meshTools/mergePoints.C | 123 ++++++++++++------ src/OpenFOAM/meshes/meshTools/mergePoints.H | 36 +++-- .../Vector/floatVector/floatVector.C | 70 ++++++++++ .../Vector/floatVector/floatVector.H | 64 +++++++++ .../triSurface/interfaces/STL/readSTLASCII.L | 80 ++++++++---- .../triSurface/interfaces/STL/readSTLBINARY.C | 62 +++++---- 6 files changed, 340 insertions(+), 95 deletions(-) create mode 100644 src/OpenFOAM/primitives/Vector/floatVector/floatVector.C create mode 100644 src/OpenFOAM/primitives/Vector/floatVector/floatVector.H diff --git a/src/OpenFOAM/meshes/meshTools/mergePoints.C b/src/OpenFOAM/meshes/meshTools/mergePoints.C index 6ecb533527..954c6aa86e 100644 --- a/src/OpenFOAM/meshes/meshTools/mergePoints.C +++ b/src/OpenFOAM/meshes/meshTools/mergePoints.C @@ -23,25 +23,25 @@ License \*---------------------------------------------------------------------------*/ -#include "mergePoints.H" -#include "SortableList.H" #include "ListOps.H" +#include "point.H" +#include "Field.H" // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -bool Foam::mergePoints +template +Foam::label Foam::mergePoints ( - const UList& points, + const UList& points, const scalar mergeTol, const bool verbose, labelList& pointMap, - List& newPoints, - const point& origin + const Type& origin ) { - point compareOrigin = origin; + Type compareOrigin = origin; - if (origin == point(VGREAT, VGREAT, VGREAT)) + if (origin == Type::max) { if (points.size()) { @@ -53,12 +53,9 @@ bool Foam::mergePoints pointMap.setSize(points.size()); pointMap = -1; - // Storage for merged points - newPoints.setSize(points.size()); - if (points.empty()) { - return false; + return points.size(); } // We're comparing distance squared to origin first. @@ -70,33 +67,56 @@ bool Foam::mergePoints // 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 = sqr(mergeTol); + const scalar mergeTolSqr = Foam::sqr(scalar(mergeTol)); // Sort points by magSqr - const pointField d(points - compareOrigin); - SortableList sortedMagSqr(magSqr(d)); - scalarField sortedTol(points.size()); - forAll(sortedMagSqr.indices(), sortI) + const Field d(points - compareOrigin); + + List magSqrD(d.size()); + forAll(d, pointI) { - const point& pt = d[sortedMagSqr.indices()[sortI]]; + magSqrD[pointI] = magSqr(d[pointI]); + } + labelList order; + sortedOrder(magSqrD, order); + + + Field 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())); } - bool hasMerged = false; - label newPointI = 0; // Handle 0th point separately (is always unique) - label pointI = sortedMagSqr.indices()[0]; - pointMap[pointI] = newPointI; - newPoints[newPointI++] = points[pointI]; + label pointI = order[0]; + pointMap[pointI] = newPointI++; - for (label sortI = 1; sortI < sortedMagSqr.size(); sortI++) + for (label sortI = 1; sortI < order.size(); sortI++) { // Get original point index - label pointI = sortedMagSqr.indices()[sortI]; + 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; @@ -105,17 +125,19 @@ bool Foam::mergePoints ( label prevSortI = sortI - 1; prevSortI >= 0 - && mag - ( - sortedMagSqr[prevSortI] - - sortedMagSqr[sortI] - ) <= sortedTol[sortI]; + && (mag(magSqrD[order[prevSortI]] - mag2) <= sortedTol[sortI]); prevSortI-- ) { - label prevPointI = sortedMagSqr.indices()[prevSortI]; + label prevPointI = order[prevSortI]; + const point prevPt + ( + scalar(points[prevPointI].x()), + scalar(points[prevPointI].y()), + scalar(points[prevPointI].z()) + ); - if (magSqr(points[pointI] - points[prevPointI]) <= mergeTolSqr) + if (magSqr(pt - prevPt) <= mergeTolSqr) { // Found match. equalPointI = prevPointI; @@ -130,8 +152,6 @@ bool Foam::mergePoints // Same coordinate as equalPointI. Map to same new point. pointMap[pointI] = pointMap[equalPointI]; - hasMerged = true; - if (verbose) { Pout<< "Foam::mergePoints : Merging points " @@ -144,14 +164,41 @@ bool Foam::mergePoints else { // Differs. Store new point. - pointMap[pointI] = newPointI; - newPoints[newPointI++] = points[pointI]; + pointMap[pointI] = newPointI++; } } - newPoints.setSize(newPointI); + return newPointI; +} - return hasMerged; + +template +bool Foam::mergePoints +( + const UList& points, + const scalar mergeTol, + const bool verbose, + labelList& pointMap, + List& newPoints, + const Type& origin = Type::zero +) +{ + label nUnique = mergePoints + ( + points, + mergeTol, + verbose, + pointMap, + origin + ); + + newPoints.setSize(nUnique); + forAll(pointMap, pointI) + { + newPoints[pointMap[pointI]] = points[pointI]; + } + + return (nUnique != points.size()); } diff --git a/src/OpenFOAM/meshes/meshTools/mergePoints.H b/src/OpenFOAM/meshes/meshTools/mergePoints.H index d6a1bb1736..92f1f1071f 100644 --- a/src/OpenFOAM/meshes/meshTools/mergePoints.H +++ b/src/OpenFOAM/meshes/meshTools/mergePoints.H @@ -32,8 +32,8 @@ SourceFiles #ifndef mergePoints_H #define mergePoints_H -#include "scalarField.H" -#include "pointField.H" +#include "scalar.H" +#include "labelList.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -44,23 +44,41 @@ namespace Foam Function mergePoints Declaration \*---------------------------------------------------------------------------*/ -//- Sort & merge points. All points closer than/equal mergeTol get merged. -// Outputs the new unique points and a map from old to new. Returns -// true if anything merged, false otherwise. -bool mergePoints +//- 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 +label mergePoints ( - const UList& points, + const UList& points, const scalar mergeTol, const bool verbose, labelList& pointMap, - List& newPoints, - const point& origin = point::zero + const Type& origin = Type::zero +); + +//- Sorts and merges points. Determines new points. Returns true if anything +// merged (though newPoints still sorted even if not merged). +template +bool mergePoints +( + const UList& points, + const scalar mergeTol, + const bool verbose, + labelList& pointMap, + List& newPoints, + const Type& origin = Type::zero ); } // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +#ifdef NoRepository +# include "mergePoints.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + #endif // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/Vector/floatVector/floatVector.C b/src/OpenFOAM/primitives/Vector/floatVector/floatVector.C new file mode 100644 index 0000000000..d88ac70f1f --- /dev/null +++ b/src/OpenFOAM/primitives/Vector/floatVector/floatVector.C @@ -0,0 +1,70 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2011 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 . + +Description + Vector of floats. + +\*---------------------------------------------------------------------------*/ + +#include "floatVector.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +template<> +const char* const floatVector::typeName = "floatVector"; + +template<> +const char* floatVector::componentNames[] = {"x", "y", "z"}; + +template<> +const floatVector floatVector::zero(0, 0, 0); + +template<> +const floatVector floatVector::one(1, 1, 1); + +template<> +const floatVector floatVector::max +( + floatScalarVGREAT, + floatScalarVGREAT, + floatScalarVGREAT +); + +template<> +const floatVector floatVector::min +( + -floatScalarVGREAT, + -floatScalarVGREAT, + -floatScalarVGREAT +); + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/Vector/floatVector/floatVector.H b/src/OpenFOAM/primitives/Vector/floatVector/floatVector.H new file mode 100644 index 0000000000..438adc1776 --- /dev/null +++ b/src/OpenFOAM/primitives/Vector/floatVector/floatVector.H @@ -0,0 +1,64 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2011 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 . + +Typedef + Foam::floatVector + +Description + A float version of vector + +SourceFiles + floatVector.C + +\*---------------------------------------------------------------------------*/ + +#ifndef floatVector_H +#define floatVector_H + +#include "Vector.H" +#include "contiguous.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +typedef Vector floatVector; + + +//- Data associated with floatVector type are contiguous +template<> +inline bool contiguous() {return true;} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/triSurface/triSurface/interfaces/STL/readSTLASCII.L b/src/triSurface/triSurface/interfaces/STL/readSTLASCII.L index 393104a7b5..ddefb95a85 100644 --- a/src/triSurface/triSurface/interfaces/STL/readSTLASCII.L +++ b/src/triSurface/triSurface/interfaces/STL/readSTLASCII.L @@ -33,8 +33,10 @@ License #include "IFstream.H" #include "triSurface.H" -#include "STLpoint.H" +#include "floatVector.H" #include "OSspecific.H" +#include "mergePoints.H" +//#include "memInfo.H" using namespace Foam; @@ -77,8 +79,8 @@ class STLLexer label lineNo_; word startError_; - DynamicList STLpoints_; - //DynamicList STLnormals_; + DynamicList STLpoints_; + //DynamicList STLnormals_; DynamicList