2010-05-12 13:27:55 +00:00
|
|
|
/*---------------------------------------------------------------------------*\
|
|
|
|
========= |
|
2013-12-11 16:09:41 +00:00
|
|
|
\\ / F ield | foam-extend: Open Source CFD
|
2015-05-17 13:32:07 +00:00
|
|
|
\\ / O peration | Version: 3.2
|
|
|
|
\\ / A nd | Web: http://www.foam-extend.org
|
|
|
|
\\/ M anipulation | For copyright notice see file Copyright
|
2010-05-12 13:27:55 +00:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
License
|
2013-12-11 16:09:41 +00:00
|
|
|
This file is part of foam-extend.
|
2010-05-12 13:27:55 +00:00
|
|
|
|
2013-12-11 16:09:41 +00:00
|
|
|
foam-extend is free software: you can redistribute it and/or modify it
|
2010-05-12 13:27:55 +00:00
|
|
|
under the terms of the GNU General Public License as published by the
|
2013-12-11 16:09:41 +00:00
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
2010-05-12 13:27:55 +00:00
|
|
|
option) any later version.
|
|
|
|
|
2013-12-11 16:09:41 +00:00
|
|
|
foam-extend 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.
|
2010-05-12 13:27:55 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2013-12-11 16:09:41 +00:00
|
|
|
along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
|
2010-05-12 13:27:55 +00:00
|
|
|
|
|
|
|
Description
|
|
|
|
Read obj line (not surface!) file and convert into vtk.
|
|
|
|
|
|
|
|
\*---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include "argList.H"
|
|
|
|
#include "OFstream.H"
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include "IStringStream.H"
|
|
|
|
#include "point.H"
|
|
|
|
#include "DynamicList.H"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace Foam;
|
|
|
|
|
|
|
|
|
|
|
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
|
|
|
|
|
|
|
string getLine(std::ifstream& is)
|
|
|
|
{
|
|
|
|
string line;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
std::getline(is, line);
|
|
|
|
}
|
2010-08-25 21:42:57 +00:00
|
|
|
while (line.size() && line[0] == '#');
|
2010-05-12 13:27:55 +00:00
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Read space-separated vertices (with optional '/' arguments)
|
|
|
|
labelList parseVertices(const string& line)
|
|
|
|
{
|
|
|
|
DynamicList<label> verts;
|
|
|
|
|
|
|
|
// Assume 'l' is followed by space.
|
2010-08-25 21:42:57 +00:00
|
|
|
string::size_type endNum = 1;
|
2010-05-12 13:27:55 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2010-08-25 21:42:57 +00:00
|
|
|
string::size_type startNum = line.find_first_not_of(' ', endNum);
|
2010-05-12 13:27:55 +00:00
|
|
|
|
2010-08-25 21:42:57 +00:00
|
|
|
if (startNum == string::npos)
|
2010-05-12 13:27:55 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
endNum = line.find(' ', startNum);
|
|
|
|
|
|
|
|
string vertexSpec;
|
2010-08-25 21:42:57 +00:00
|
|
|
if (endNum != string::npos)
|
2010-05-12 13:27:55 +00:00
|
|
|
{
|
|
|
|
vertexSpec = line.substr(startNum, endNum-startNum);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vertexSpec = line.substr(startNum, line.size() - startNum);
|
|
|
|
}
|
|
|
|
|
2010-08-25 21:42:57 +00:00
|
|
|
string::size_type slashPos = vertexSpec.find('/');
|
2010-05-12 13:27:55 +00:00
|
|
|
|
|
|
|
label vertI = 0;
|
2010-08-25 21:42:57 +00:00
|
|
|
if (slashPos != string::npos)
|
2010-05-12 13:27:55 +00:00
|
|
|
{
|
|
|
|
IStringStream intStream(vertexSpec.substr(0, slashPos));
|
|
|
|
|
|
|
|
intStream >> vertI;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IStringStream intStream(vertexSpec);
|
|
|
|
|
|
|
|
intStream >> vertI;
|
|
|
|
}
|
|
|
|
verts.append(vertI - 1);
|
|
|
|
}
|
|
|
|
while (true);
|
|
|
|
|
|
|
|
return verts.shrink();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Main program:
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
argList::noParallel();
|
|
|
|
argList::validArgs.clear();
|
|
|
|
argList::validArgs.append("OBJ file");
|
|
|
|
argList::validArgs.append("output VTK file");
|
2010-08-25 21:42:57 +00:00
|
|
|
argList args(argc, argv);
|
2010-05-12 13:27:55 +00:00
|
|
|
|
|
|
|
fileName objName(args.additionalArgs()[0]);
|
|
|
|
fileName outName(args.additionalArgs()[1]);
|
|
|
|
|
|
|
|
std::ifstream OBJfile(objName.c_str());
|
|
|
|
|
|
|
|
if (!OBJfile.good())
|
|
|
|
{
|
|
|
|
FatalErrorIn(args.executable())
|
|
|
|
<< "Cannot read file " << objName << exit(FatalError);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Points and lines
|
|
|
|
DynamicList<point> points;
|
|
|
|
DynamicList<labelList> polyLines;
|
|
|
|
DynamicList<labelList> polygons;
|
|
|
|
|
|
|
|
bool hasWarned = false;
|
|
|
|
|
|
|
|
label lineNo = 0;
|
|
|
|
while (OBJfile.good())
|
|
|
|
{
|
|
|
|
string line = getLine(OBJfile);
|
|
|
|
lineNo++;
|
|
|
|
|
|
|
|
// 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 == "l")
|
|
|
|
{
|
|
|
|
polyLines.append(parseVertices(line));
|
|
|
|
}
|
|
|
|
else if (cmd == "f")
|
|
|
|
{
|
|
|
|
polygons.append(parseVertices(line));
|
|
|
|
}
|
|
|
|
else if (cmd != "")
|
|
|
|
{
|
|
|
|
if (!hasWarned)
|
|
|
|
{
|
|
|
|
hasWarned = true;
|
|
|
|
|
|
|
|
WarningIn(args.executable())
|
|
|
|
<< "Unrecognized OBJ command " << cmd << nl
|
|
|
|
<< "In line " << lineStream.str()
|
|
|
|
<< " at linenumber " << lineNo << nl
|
|
|
|
<< "Only recognized commands are 'v' and 'l'.\n"
|
|
|
|
<< "If this is a surface command use surfaceConvert instead"
|
|
|
|
<< " to convert to a file format that can be read by VTK"
|
|
|
|
<< endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Write as vtk 'polydata' file
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
OFstream outFile(outName);
|
|
|
|
|
|
|
|
outFile
|
|
|
|
<< "# vtk DataFile Version 2.0\n"
|
|
|
|
<< objName << nl
|
|
|
|
<< "ASCII\n"
|
|
|
|
<< "DATASET POLYDATA\n"
|
|
|
|
<< "POINTS " << points.size() << " float\n";
|
|
|
|
|
|
|
|
forAll(points, i)
|
|
|
|
{
|
|
|
|
const point& pt = points[i];
|
|
|
|
|
|
|
|
outFile << pt.x() << ' ' << pt.y() << ' ' << pt.z() << nl;
|
|
|
|
}
|
|
|
|
|
|
|
|
label nItems = 0;
|
|
|
|
forAll(polyLines, polyI)
|
|
|
|
{
|
|
|
|
nItems += polyLines[polyI].size() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
outFile
|
|
|
|
<< "LINES " << polyLines.size() << ' ' << nItems << nl;
|
|
|
|
|
|
|
|
forAll(polyLines, polyI)
|
|
|
|
{
|
|
|
|
const labelList& line = polyLines[polyI];
|
|
|
|
|
|
|
|
outFile << line.size();
|
|
|
|
|
|
|
|
forAll(line, i)
|
|
|
|
{
|
|
|
|
outFile << ' ' << line[i];
|
|
|
|
}
|
|
|
|
outFile << nl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nItems = 0;
|
|
|
|
forAll(polygons, polyI)
|
|
|
|
{
|
|
|
|
nItems += polygons[polyI].size() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
outFile
|
|
|
|
<< "POLYGONS " << polygons.size() << ' ' << nItems << nl;
|
|
|
|
|
|
|
|
forAll(polygons, polyI)
|
|
|
|
{
|
|
|
|
const labelList& line = polygons[polyI];
|
|
|
|
|
|
|
|
outFile << line.size();
|
|
|
|
|
|
|
|
forAll(line, i)
|
|
|
|
{
|
|
|
|
outFile << ' ' << line[i];
|
|
|
|
}
|
|
|
|
outFile << nl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
outFile
|
|
|
|
<< "POINT_DATA " << points.size() << nl
|
|
|
|
<< "SCALARS pointID float 1\n"
|
|
|
|
<< "LOOKUP_TABLE default\n";
|
|
|
|
|
|
|
|
forAll(points, i)
|
|
|
|
{
|
|
|
|
outFile << i;
|
|
|
|
|
|
|
|
if ((i % 10) == 1)
|
|
|
|
{
|
|
|
|
outFile << nl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outFile << ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Info << "End\n" << endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ************************************************************************* //
|