48string getLine(std::ifstream& is)
53 std::getline(is,
line);
55 while (
line.starts_with(
'#'));
73 if (startNum == string::npos)
78 endNum =
line.find(
' ', startNum);
81 if (endNum != string::npos)
83 vertexSpec =
line.substr(startNum, endNum-startNum);
87 vertexSpec =
line.substr(startNum,
line.size() - startNum);
93 if (slashPos != string::npos)
114int main(
int argc,
char *argv[])
118 "Read obj line (not surface) file and convert into legacy VTK file"
121 argList::noParallel();
122 argList::addArgument(
"obj-file",
"The input obj line file");
123 argList::addArgument(
"vtk-file",
"The output vtk file");
129 std::ifstream OBJfile(objName);
131 Info<<
"Processing file " << objName <<
endl;
136 <<
"Cannot read file " << objName <<
exit(FatalError);
145 bool hasWarned =
false;
148 while (OBJfile.good())
150 const string line = getLine(OBJfile);
153 if (
line.empty())
continue;
157 word cmd(lineStream);
163 lineStream >>
x >>
y >> z;
167 else if (cmd ==
"vn")
171 lineStream >>
x >>
y >> z;
190 <<
"Unrecognized OBJ command " << cmd <<
nl
191 <<
"In line " << lineStream.str()
192 <<
" at linenumber " << lineNo <<
nl
193 <<
"Only recognized commands are 'v' and 'l'.\n"
194 <<
"If this is a surface command use surfaceConvert instead"
195 <<
" to convert to a file format that can be read by VTK"
210 <<
"# vtk DataFile Version 2.0\n"
213 <<
"DATASET POLYDATA\n"
214 <<
"POINTS " <<
points.size() <<
" double\n";
219 << float(pt.x()) <<
' '
220 << float(pt.y()) <<
' '
221 << float(pt.z()) <<
nl;
225 <<
"VERTICES " <<
points.size() <<
' ' << (2 *
points.size()) << nl;
229 outFile << 1 <<
' ' << i <<
nl;
235 nItems += polyLines[polyI].
size() + 1;
239 <<
"LINES " << polyLines.
size() <<
' ' << nItems <<
nl;
245 outFile <<
line.size();
249 outFile <<
' ' <<
line[i];
258 nItems += polygons[polyI].
size() + 1;
262 <<
"POLYGONS " << polygons.
size() <<
' ' << nItems <<
nl;
268 outFile <<
line.size();
272 outFile <<
' ' <<
line[i];
279 <<
"POINT_DATA " <<
points.size() <<
nl
280 <<
"SCALARS pointID double 1\n"
281 <<
"LOOKUP_TABLE default\n";
297 if (!pointNormals.
empty())
299 outFile <<
nl <<
"NORMALS pointNormals double\n";
301 for(
const vector&
n : pointNormals)
304 << float(
n.x()) <<
' '
305 << float(
n.y()) <<
' '
306 << float(
n.z()) <<
nl;
Input/output from string buffers.
label size_type
The type to represent the size of a buffer.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
DynamicList< T, SizeMin > & shrink()
Shrink the allocated space to the number of elements used.
void append(const T &val)
Copy append an element to the end of this list.
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Output to file stream, using an OSstream.
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
void size(const label n)
Older name for setAddressableSize.
Extract command arguments and options from the supplied argc and argv parameters.
T get(const label index) const
Get a value from the argument at index.
A class for handling file names.
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
A class for handling words, derived from Foam::string.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
#define WarningInFunction
Report a warning using Foam::Warning.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
errorManipArg< error, int > exit(error &err, const int errNo=1)
constexpr char nl
The newline '\n' character (0x0a)
Foam::argList args(argc, argv)
#define forAll(list, i)
Loop across all elements in list.