From 5d69e4ebaa6d941f4dc520658200f22298c37210 Mon Sep 17 00:00:00 2001 From: mattijs <mattijs> Date: Thu, 26 Nov 2015 12:23:58 +0000 Subject: [PATCH] ENH: triSurface: reading obj files with <cr> ending - read nastran (NAS) surfaces in comma-separated free format --- .../triSurface/interfaces/NAS/readNAS.C | 84 ++++++--- .../triSurface/interfaces/OBJ/readOBJ.C | 159 +++++++++--------- 2 files changed, 142 insertions(+), 101 deletions(-) diff --git a/src/triSurface/triSurface/interfaces/NAS/readNAS.C b/src/triSurface/triSurface/interfaces/NAS/readNAS.C index 3e39579d052..129ce49610c 100644 --- a/src/triSurface/triSurface/interfaces/NAS/readNAS.C +++ b/src/triSurface/triSurface/interfaces/NAS/readNAS.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -26,7 +26,7 @@ Description - Uses the Ansa "$ANSA_NAME" or the Hypermesh "$HMNAME COMP" extensions to obtain patch names. - - Handles Nastran short and long formats, but not free format. + - Handles Nastran short, long, and comma-separated free formats. - Properly handles the Nastran compact floating point notation: \n \verbatim GRID 28 10.20269-.030265-2.358-8 @@ -69,6 +69,32 @@ static scalar parseNASCoord(const string& s) } +// Read a column of a given width from either a fixed-format NAS file, or a +// comma-separated free-format NAS file +static std::string readNASToken +( + const string& line, + const size_t& width, + size_t& index +) +{ + size_t indexStart, indexEnd; + + indexStart = index; + + indexEnd = line.find(',', indexStart); + index = indexEnd + 1; + + if (indexEnd == std::string::npos) + { + indexEnd = indexStart + width; + index = indexEnd; + } + + return line.substr(indexStart, indexEnd - indexStart); +} + + bool triSurface::readNAS(const fileName& fName) { IFstream is(fName); @@ -104,6 +130,7 @@ bool triSurface::readNAS(const fileName& fName) while (is.good()) { + size_t linei = 0; string line; is.getLine(line); @@ -197,17 +224,16 @@ bool triSurface::readNAS(const fileName& fName) } // Read first word - IStringStream lineStream(line); - word cmd; - lineStream >> cmd; + word cmd(IStringStream(readNASToken(line, 8, linei))()); if (cmd == "CTRIA3") { - label groupId = readLabel(IStringStream(line.substr(16,8))()); - label a = readLabel(IStringStream(line.substr(24,8))()); - label b = readLabel(IStringStream(line.substr(32,8))()); - label c = readLabel(IStringStream(line.substr(40,8))()); - + readNASToken(line, 8, linei); + label groupId = + readLabel(IStringStream(readNASToken(line, 8, linei))()); + label a = readLabel(IStringStream(readNASToken(line, 8, linei))()); + label b = readLabel(IStringStream(readNASToken(line, 8, linei))()); + label c = readLabel(IStringStream(readNASToken(line, 8, linei))()); // Convert group into patch Map<label>::const_iterator iter = groupToPatch.find(groupId); @@ -228,11 +254,13 @@ bool triSurface::readNAS(const fileName& fName) } else if (cmd == "CQUAD4") { - label groupId = readLabel(IStringStream(line.substr(16,8))()); - label a = readLabel(IStringStream(line.substr(24,8))()); - label b = readLabel(IStringStream(line.substr(32,8))()); - label c = readLabel(IStringStream(line.substr(40,8))()); - label d = readLabel(IStringStream(line.substr(48,8))()); + readNASToken(line, 8, linei); + label groupId = + readLabel(IStringStream(readNASToken(line, 8, linei))()); + label a = readLabel(IStringStream(readNASToken(line, 8, linei))()); + label b = readLabel(IStringStream(readNASToken(line, 8, linei))()); + label c = readLabel(IStringStream(readNASToken(line, 8, linei))()); + label d = readLabel(IStringStream(readNASToken(line, 8, linei))()); // Convert group into patch Map<label>::const_iterator iter = groupToPatch.find(groupId); @@ -255,7 +283,8 @@ bool triSurface::readNAS(const fileName& fName) else if (cmd == "PSHELL") { // Read shell type since group gives patchnames - label groupId = readLabel(IStringStream(line.substr(8,8))()); + label groupId = + readLabel(IStringStream(readNASToken(line, 8, linei))()); if (groupId == ansaId && ansaType == "PSHELL") { groupToName.insert(groupId, string::validate<word>(ansaName)); @@ -264,10 +293,12 @@ bool triSurface::readNAS(const fileName& fName) } else if (cmd == "GRID") { - label index = readLabel(IStringStream(line.substr(8,8))()); - scalar x = parseNASCoord(line.substr(24, 8)); - scalar y = parseNASCoord(line.substr(32, 8)); - scalar z = parseNASCoord(line.substr(40, 8)); + label index = + readLabel(IStringStream(readNASToken(line, 8, linei))()); + readNASToken(line, 8, linei); + scalar x = parseNASCoord(readNASToken(line, 8, linei)); + scalar y = parseNASCoord(readNASToken(line, 8, linei)); + scalar z = parseNASCoord(readNASToken(line, 8, linei)); indices.append(index); points.append(point(x, y, z)); @@ -279,11 +310,13 @@ bool triSurface::readNAS(const fileName& fName) // Typical line (spaces compacted) // GRID* 126 0 -5.55999875E+02 -5.68730474E+02 // * 2.14897901E+02 + label index = + readLabel(IStringStream(readNASToken(line, 8, linei))()); + readNASToken(line, 8, linei); + scalar x = parseNASCoord(readNASToken(line, 16, linei)); + scalar y = parseNASCoord(readNASToken(line, 16, linei)); - label index = readLabel(IStringStream(line.substr(8,16))()); - scalar x = parseNASCoord(line.substr(40, 16)); - scalar y = parseNASCoord(line.substr(56, 16)); - + linei = 0; is.getLine(line); if (line[0] != '*') { @@ -295,7 +328,8 @@ bool triSurface::readNAS(const fileName& fName) << " line:" << is.lineNumber() << exit(FatalError); } - scalar z = parseNASCoord(line.substr(8, 16)); + readNASToken(line, 8, linei); + scalar z = parseNASCoord(readNASToken(line, 16, linei)); indices.append(index); points.append(point(x, y, z)); diff --git a/src/triSurface/triSurface/interfaces/OBJ/readOBJ.C b/src/triSurface/triSurface/interfaces/OBJ/readOBJ.C index 004661ab3cf..dd57ffb94f6 100644 --- a/src/triSurface/triSurface/interfaces/OBJ/readOBJ.C +++ b/src/triSurface/triSurface/interfaces/OBJ/readOBJ.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -53,105 +53,112 @@ bool Foam::triSurface::readOBJ(const fileName& OBJfileName) label sz = line.size(); - if (sz && line[sz-1] == '\\') + if (sz) { - line.substr(0, sz-1); - line += getLineNoComment(OBJfile); - } - - // Read first word - IStringStream lineStream(line); - word cmd; - lineStream >> cmd; - - if (cmd == "v") - { - scalar x, y, z; - - lineStream >> x >> y >> z; - - points.append(point(x, y, z)); - } - else if (cmd == "g") - { - word group; - - lineStream >> group; - - HashTable<label>::const_iterator findGroup = - groupToPatch.find(group); - - if (findGroup != groupToPatch.end()) + if (line[sz-1] == '\\') { - groupID = findGroup(); + line.substr(0, sz-1); + line += getLineNoComment(OBJfile); } - else - { - groupID = maxGroupID; - groupToPatch.insert(group, groupID); + // Read first word + IStringStream lineStream(line); + word cmd; + lineStream >> cmd; - maxGroupID++; - } - } - else if (cmd == "f") - { - DynamicList<label> verts; + if (cmd == "v") + { + scalar x, y, z; - // Assume 'f' is followed by space. - string::size_type endNum = 1; + lineStream >> x >> y >> z; - while (true) + points.append(point(x, y, z)); + } + else if (cmd == "g") { - string::size_type startNum = - line.find_first_not_of(" \r", endNum); + word group; - if (startNum == string::npos) - { - break; - } + lineStream >> group; - endNum = line.find(' ', startNum); + HashTable<label>::const_iterator findGroup = + groupToPatch.find(group); - string vertexSpec; - if (endNum != string::npos) + if (findGroup != groupToPatch.end()) { - vertexSpec = line.substr(startNum, endNum-startNum); + groupID = findGroup(); } else { - vertexSpec = line.substr(startNum, line.size() - startNum); - } + groupID = maxGroupID; - string::size_type slashPos = vertexSpec.find('/'); + groupToPatch.insert(group, groupID); - label vertI = 0; - if (slashPos != string::npos) - { - IStringStream intStream(vertexSpec.substr(0, slashPos)); - - intStream >> vertI; + maxGroupID++; } - else - { - IStringStream intStream(vertexSpec); + } + else if (cmd == "f") + { + DynamicList<label> verts; - intStream >> vertI; + // Assume 'f' is followed by space. + string::size_type endNum = 1; + + while (true) + { + string::size_type startNum = + line.find_first_not_of(" \r", endNum); + + if (startNum == string::npos) + { + break; + } + + endNum = line.find(' ', startNum); + + string vertexSpec; + if (endNum != string::npos) + { + vertexSpec = line.substr(startNum, endNum-startNum); + } + else + { + vertexSpec = line.substr + ( + startNum, + line.size() - startNum + ); + } + + string::size_type slashPos = vertexSpec.find('/'); + + label vertI = 0; + if (slashPos != string::npos) + { + IStringStream intStream(vertexSpec.substr(0, slashPos)); + + intStream >> vertI; + } + else + { + IStringStream intStream(vertexSpec); + + intStream >> vertI; + } + verts.append(vertI - 1); } - verts.append(vertI - 1); - } - verts.shrink(); + verts.shrink(); - // Do simple face triangulation around f[0]. - // Cannot use face::triangulation since no complete points yet. - for (label fp = 1; fp < verts.size() - 1; fp++) - { - label fp1 = verts.fcIndex(fp); + // Do simple face triangulation around f[0]. + // Cannot use face::triangulation since no complete points yet. + for (label fp = 1; fp < verts.size() - 1; fp++) + { + label fp1 = verts.fcIndex(fp); - labelledTri tri(verts[0], verts[fp], verts[fp1], groupID); + labelledTri tri(verts[0], verts[fp], verts[fp1], groupID); - faces.append(tri); + faces.append(tri); + } } } } -- GitLab