diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C index f10fb9c056..2d6bdfac2e 100644 --- a/applications/test/string/Test-string.C +++ b/applications/test/string/Test-string.C @@ -191,7 +191,12 @@ int main(int argc, char *argv[]) Info<<"camel-case => " << (word("camel") & "case") << nl; for (const auto& s : { " text with \"spaces'", "08/15 value" }) { - Info<<"validated \"" << s << "\" => " + // Character sequence + + Info<<"validated 5 chars from \" => " + << word::validate(s, s+5, true) << nl; + + Info<<"validated (via string convert) \"" << s << "\" => " << word::validate(s, true) << nl; } Info<< nl; diff --git a/applications/test/surfaceReading/Make/files b/applications/test/surfaceReading/Make/files new file mode 100644 index 0000000000..60efef416d --- /dev/null +++ b/applications/test/surfaceReading/Make/files @@ -0,0 +1,3 @@ +Test-surfaceReading.C + +EXE = $(FOAM_APPBIN)/Test-surfaceReading diff --git a/applications/test/surfaceReading/Make/options b/applications/test/surfaceReading/Make/options new file mode 100644 index 0000000000..14486e1758 --- /dev/null +++ b/applications/test/surfaceReading/Make/options @@ -0,0 +1,6 @@ +EXE_INC = \ + -I$(LIB_SRC)/fileFormats/lnInclude \ + -I$(LIB_SRC)/surfMesh/lnInclude + +EXE_LIBS = \ + -lsurfMesh diff --git a/applications/test/surfaceReading/Test-surfaceReading.C b/applications/test/surfaceReading/Test-surfaceReading.C new file mode 100644 index 0000000000..41b69c1c14 --- /dev/null +++ b/applications/test/surfaceReading/Test-surfaceReading.C @@ -0,0 +1,145 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ 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 . + +Application + Test-surfaceReading + +Description + Test basic surface format reading capabilities (and speeds) + +Note + The filename extensions are used to determine the file format type. + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "Time.H" +#include "clockTime.H" +#include "triSurface.H" +#include "MeshedSurfaces.H" +#include "UnsortedMeshedSurfaces.H" +#include "STLReader.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +int main(int argc, char *argv[]) +{ + argList::addNote + ( + "Test basic surface format reading capabilities (and speeds)" + ); + + argList::noParallel(); + argList::addArgument("inputFile"); + + argList::addBoolOption + ( + "triSurface", + "Use triSurface for read" + ); + argList::addBoolOption + ( + "triFace", + "Use triFace instead of face" + ); + argList::addBoolOption + ( + "unsorted", + "Use UnsortedMeshedSurface instead of MeshedSurface, " + "or unsorted output (with -triSurface option)" + ); + + argList::addOption + ( + "ext", + "name", + "Force alternative extension" + ); + + argList::addOption + ( + "stl-parser", + "N", + "ASCII parser type: 0=Flex, 1=Ragel, 2=Manual" + ); + + #include "setRootCase.H" + + const fileName importName = args[1]; + + word ext; + if (!args.readIfPresent("ext", ext)) + { + ext = importName.ext(); + if (ext == "gz") + { + ext = importName.lessExt().ext(); + } + } + + args.readIfPresent("stl-parser", fileFormats::STLReader::parserType); + + clockTime timing; + + if (args.found("triSurface")) + { + triSurface surf(importName, ext); + + Info<< "Read surface:" << endl; + surf.writeStats(Info); + Info<< "Area : " << sum(surf.magSf()) << nl << endl; + } + else if (args.found("triFace")) + { + MeshedSurface surf(importName, ext); + + Info<< "Read surface:" << endl; + surf.writeStats(Info); + Info<< "Area : " << sum(surf.magSf()) << nl << endl; + } + else if (args.found("unsorted")) + { + UnsortedMeshedSurface surf(importName, ext); + + Info<< "Read surface:" << endl; + surf.writeStats(Info); + Info<< "Area : " << sum(surf.magSf()) << nl << endl; + } + else + { + MeshedSurface surf(importName, ext); + + Info<< "Read surface:" << endl; + surf.writeStats(Info); + Info<< "Area : " << sum(surf.magSf()) << nl << endl; + } + + Info<< nl << "Reading took " << timing.elapsedTime() << "s" << nl + << "\nEnd\n" << endl; + + return 0; +} + +// ************************************************************************* // diff --git a/etc/controlDict b/etc/controlDict index 2be965fb94..b64ec0ecf1 100644 --- a/etc/controlDict +++ b/etc/controlDict @@ -121,8 +121,12 @@ OptimisationSwitches // Force dumping (at next timestep) upon signal (-1 to disable) and exit stopAtWriteNowSignal -1; + + //- Choose STL ASCII parser: 0=Flex, 1=Ragel, 2=Manual + fileFormats::stl 0; } + /* Can specify fallback profiling settings profiling { diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index 57796f027f..f77302f521 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -50,7 +50,7 @@ Foam::fileName Foam::fileName::validate out.resize(s.size()); char prev = 0; - std::string::size_type count = 0; + std::string::size_type len = 0; // Largely as per stripInvalid for (auto iter = s.cbegin(); iter != s.cend(); ++iter) @@ -66,17 +66,17 @@ Foam::fileName Foam::fileName::validate } // Only track valid chars - out[count++] = prev = c; + out[len++] = prev = c; } } - if (doClean && prev == '/' && count > 1) + if (doClean && prev == '/' && len > 1) { // Avoid trailing '/' - --count; + --len; } - out.resize(count); + out.resize(len); return out; } diff --git a/src/OpenFOAM/primitives/strings/string/stringI.H b/src/OpenFOAM/primitives/strings/string/stringI.H index 533c733afb..f8d7e4b9f4 100644 --- a/src/OpenFOAM/primitives/strings/string/stringI.H +++ b/src/OpenFOAM/primitives/strings/string/stringI.H @@ -222,17 +222,18 @@ inline String Foam::string::validate(const std::string& str) String out; out.resize(str.size()); - size_type count = 0; + size_type len = 0; for (auto iter = str.cbegin(); iter != str.cend(); ++iter) { const char c = *iter; if (String::valid(c)) { - out[count++] = c; + out[len] = c; + ++len; } } - out.resize(count); + out.resize(len); return out; } diff --git a/src/OpenFOAM/primitives/strings/word/word.C b/src/OpenFOAM/primitives/strings/word/word.C index c51d489c91..ae751a58d0 100644 --- a/src/OpenFOAM/primitives/strings/word/word.C +++ b/src/OpenFOAM/primitives/strings/word/word.C @@ -41,7 +41,7 @@ Foam::word Foam::word::validate(const std::string& s, const bool prefix) word out; out.resize(s.size() + (prefix ? 1 : 0)); - std::string::size_type count = 0; + std::string::size_type len = 0; // As per validate, but optionally detect if the first character // is a digit, which we'd like to avoid having since this will @@ -52,17 +52,51 @@ Foam::word Foam::word::validate(const std::string& s, const bool prefix) if (word::valid(c)) { - if (!count && prefix && isdigit(c)) + if (!len && prefix && isdigit(c)) { // First valid character was a digit - prefix with '_' - out[count++] = '_'; + out[len++] = '_'; } - out[count++] = c; + out[len++] = c; } } - out.resize(count); + out.resize(len); + + return out; +} + + +Foam::word Foam::word::validate +( + const char* first, + const char* last, + const bool prefix +) +{ + std::string::size_type len = (last - first) + (prefix ? 1 : 0); + + word out; + out.resize(len); + + for (len=0; first != last; ++first) + { + const char c = *first; + + if (word::valid(c)) + { + if (!len && prefix && isdigit(c)) + { + // First valid character was a digit - prefix with '_' + out[len++] = '_'; + } + + out[len++] = c; + } + } + + out.resize(len); return out; } diff --git a/src/OpenFOAM/primitives/strings/word/word.H b/src/OpenFOAM/primitives/strings/word/word.H index f0997d64fd..4accabc805 100644 --- a/src/OpenFOAM/primitives/strings/word/word.H +++ b/src/OpenFOAM/primitives/strings/word/word.H @@ -148,6 +148,16 @@ public: // that work nicely as dictionary keywords. static word validate(const std::string& s, const bool prefix=false); + //- Construct validated word (no invalid characters) from a sequence + //- of characters in the range [first,last), + // Optionally prefix any leading digit with '_'. + static word validate + ( + const char* first, + const char* last, + const bool prefix=false + ); + // File-like Functions diff --git a/src/fileFormats/Make/files b/src/fileFormats/Make/files index 1dd9b3469a..989048f579 100644 --- a/src/fileFormats/Make/files +++ b/src/fileFormats/Make/files @@ -12,7 +12,9 @@ fire/FIRECore.C starcd/STARCDCore.C stl/STLCore.C stl/STLReader.C -stl/STLReaderASCII.L +stl/STLAsciiParseFlex.L +stl/STLAsciiParseManual.C +stl/STLAsciiParseRagel.C vtk/core/foamVtkCore.C vtk/core/foamVtkPTraits.C diff --git a/src/fileFormats/stl/STLAsciiParse.H b/src/fileFormats/stl/STLAsciiParse.H new file mode 100644 index 0000000000..48c9a159e0 --- /dev/null +++ b/src/fileFormats/stl/STLAsciiParse.H @@ -0,0 +1,153 @@ +/*--------------------------------*- C++ -*----------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ 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::Detail::STLAsciiParse + +Description + Internal class used when parsing STL ASCII format + +SourceFiles + STLAsciiParse.C + +\*---------------------------------------------------------------------------*/ + +#ifndef STLAsciiParse_H +#define STLAsciiParse_H + +#include "DynamicList.H" +#include "HashTable.H" +#include "STLpoint.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace Detail +{ + +/*---------------------------------------------------------------------------*\ + Class Detail::STLAsciiParse Declaration +\*---------------------------------------------------------------------------*/ + +class STLAsciiParse +{ +protected: + + // Protected Data + + bool sorted_; + label groupId_; // The current solid group + label lineNum_; + + //- The number of local points on the current facet + int nFacetPoints_; + + //- Current vertex component when reading 'vertex' + int nVertexCmpt_; + + //- Scratch space for reading 'vertex' + STLpoint currVertex_; + + DynamicList points_; + DynamicList