2
0
Эх сурвалжийг харах

Fix a bug in importing binary PLY file (#1) (#6060)

* Correct IOStreamBuffer<T>::getNextLine() to prevent skipping too many '\n', which could be part of the BINARY part
* Add a regression test
yurik 3 сар өмнө
parent
commit
b97308683b

+ 4 - 1
include/assimp/IOStreamBuffer.h

@@ -318,7 +318,10 @@ AI_FORCE_INLINE bool IOStreamBuffer<T>::getNextLine(std::vector<T> &buffer) {
         }
     }
     buffer[i] = '\n';
-    while (m_cachePos < m_cacheSize && (m_cache[m_cachePos] == '\r' || m_cache[m_cachePos] == '\n')) {
+    if (m_cachePos < m_cacheSize && (m_cache[m_cachePos] == '\r')) {
+        ++m_cachePos;
+    }
+    if (m_cachePos < m_cacheSize && (m_cache[m_cachePos] == '\n')) {
         ++m_cachePos;
     }
 

BIN
test/models/PLY/cube_binary_starts_with_nl.ply


+ 21 - 0
test/unit/utPLYImportExport.cpp

@@ -141,6 +141,27 @@ TEST_F(utPLYImportExport, importBinaryPLYWithRNNewline) {
     EXPECT_EQ(2u, scene->mMeshes[0]->mFaces[0].mIndices[2]);
 }
 
+// Tests of a PLY file gets read with \n as the fist character in the BINARY part
+TEST_F(utPLYImportExport, importBinaryPLYWithNewlineInBinary) {
+    Assimp::Importer importer;
+    const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/cube_binary_starts_with_nl.ply", aiProcess_ValidateDataStructure);
+
+    ASSERT_NE(nullptr, scene);
+    ASSERT_NE(nullptr, scene->mMeshes[0]);
+    ASSERT_EQ(8u, scene->mMeshes[0]->mNumVertices);
+    // Make sure the first binary float was read correctly
+    ASSERT_FLOAT_EQ(5.967534f, scene->mMeshes[0]->mVertices[0][0]);
+    ASSERT_FLOAT_EQ(0, scene->mMeshes[0]->mVertices[0][1]);
+    ASSERT_FLOAT_EQ(0, scene->mMeshes[0]->mVertices[0][2]);
+
+    ASSERT_EQ(6u, scene->mMeshes[0]->mNumFaces);
+    // Also check if the indices were parsed correctly
+    ASSERT_EQ(4u, scene->mMeshes[0]->mFaces[0].mNumIndices);
+    EXPECT_EQ(0u, scene->mMeshes[0]->mFaces[0].mIndices[0]);
+    EXPECT_EQ(1u, scene->mMeshes[0]->mFaces[0].mIndices[1]);
+    EXPECT_EQ(2u, scene->mMeshes[0]->mFaces[0].mIndices[2]);
+}
+
 TEST_F(utPLYImportExport, vertexColorTest) {
     Assimp::Importer importer;
     const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/PLY/float-color.ply", aiProcess_ValidateDataStructure);