Deal with trailing spaces and crlf

We ended up having corrupted meshes if the application which exported
the OBJ was adding trailing white spaces. Also make sure we got a test
case using crlf for end of lines.

Change-Id: Iace9dbc3d0d124fefe9e3350d396fdf26555cd17
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
This commit is contained in:
Kevin Ottens 2017-10-13 12:43:02 +02:00 committed by Sean Harmer
parent 294eefef0f
commit 391fe1d2c1
4 changed files with 40 additions and 1 deletions

View File

@ -102,6 +102,10 @@ bool ObjGeometryLoader::doLoad(QIODevice *ioDev, const QString &subMesh)
if (lineSize > 0 && line[0] != '#') {
if (line[lineSize - 1] == '\n')
--lineSize; // chop newline
if (line[lineSize - 1] == '\r')
--lineSize; // chop newline also for CRLF format
while (line[lineSize - 1] == ' ' || line[lineSize - 1] == '\t')
--lineSize; // chop trailing spaces
const ByteArraySplitter tokens(line, line + lineSize, ' ', QString::SkipEmptyParts);

View File

@ -0,0 +1,26 @@
# Blender v2.77 (sub 0) OBJ File: ''
# www.blender.org
mtllib cube.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
vn 1.0000 0.0000 0.0000
vn -0.0000 -0.0000 1.0000
vn -1.0000 -0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
usemtl Material
s off
f 1//1 2//1 3//1 4//1
f 5//2 8//2 7//2 6//2
f 1//3 5//3 6//3 2//3
f 2//4 6//4 7//4 3//4
f 3//5 7//5 8//5 4//5
f 5//6 1//6 4//6 8//6

View File

@ -1,6 +1,7 @@
<RCC>
<qresource prefix="/">
<file>cube.obj</file>
<file>cube2.obj</file>
<file>cube.ply</file>
<file>cube.stl</file>
<file>cube.gltf</file>

View File

@ -60,6 +60,7 @@ class tst_geometryloaders : public QObject
Q_OBJECT
private Q_SLOTS:
void testOBJLoader_data();
void testOBJLoader();
void testPLYLoader();
void testSTLLoader();
@ -69,6 +70,12 @@ private Q_SLOTS:
#endif
};
void tst_geometryloaders::testOBJLoader_data()
{
QTest::addColumn<QString>("fileName");
QTest::newRow("nominal case") << QStringLiteral(":/cube.obj");
QTest::newRow("trailing space + crlf") << QStringLiteral(":/cube2.obj");
}
void tst_geometryloaders::testOBJLoader()
{
QScopedPointer<QGeometryLoaderInterface> loader;
@ -77,7 +84,8 @@ void tst_geometryloaders::testOBJLoader()
if (!loader)
return;
QFile file(QStringLiteral(":/cube.obj"));
QFETCH(QString, fileName);
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug("Could not open test file for reading");
return;