From d3d5ce544dd543bdb5813e4a4b97036ffe6a244b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?David=20K=C3=BCgler?=
 <david.kuegler@gris.informatik.tu-darmstadt.de>
Date: Mon, 20 Nov 2017 16:26:02 +0100
Subject: [PATCH] BUG: itkOBJMeshIO did only load meshes properly, if the
 vectornormals were all placed before the vectors.

https://issues.itk.org/jira/browse/ITK-2883

Fix the loading of the file loading vectornormals independently of the vectors.
---
 Modules/IO/Mesh/src/itkOBJMeshIO.cxx | 117 ++++++++++++++++++++---------------
 1 file changed, 66 insertions(+), 51 deletions(-)

diff --git a/Modules/IO/Mesh/src/itkOBJMeshIO.cxx b/Modules/IO/Mesh/src/itkOBJMeshIO.cxx
index 40a33cd..4ae5cbe 100644
--- a/Modules/IO/Mesh/src/itkOBJMeshIO.cxx
+++ b/Modules/IO/Mesh/src/itkOBJMeshIO.cxx
@@ -121,43 +121,48 @@ OBJMeshIO
   std::string   inputLine;
   std::string   type;
   std::locale   loc;
-  while ( std::getline(m_InputFile, line, '\n') )
-    {
+  while (std::getline(m_InputFile, line, '\n'))
+  {
     inputLine.clear();
-    for ( unsigned int ii = 0; ii < line.size(); ii++ )
-      {
-      if ( !std::isspace(line[ii], loc) )
-        {
-        type = line[ii];
-        inputLine = line.substr(ii + 1);
-        break;
-        }
-      }
 
-    if ( !inputLine.empty() )
+    auto starttype = std::find_if_not(line.begin(), line.end(), [&loc](const char l) { return std::isspace(l, loc); });
+    // only has spaces
+    if (starttype == line.end())
+      continue;
+
+    type = *starttype;
+    if (++starttype != line.end() && !std::isspace(*starttype, loc))
+    {
+      type = type + *starttype; ++starttype;
+    }
+
+    inputLine = line.substr(starttype - line.begin());
+
+    if (!inputLine.empty())
+    {
+      if (type == "v")
       {
-      if ( type == "v" )
-        {
         this->m_NumberOfPoints++;
-        }
-      else if ( type == "f" )
-        {
+      }
+      else if (type == "f")
+      {
         this->m_NumberOfCells++;
 
         std::stringstream ss(inputLine);
         std::string       item;
-        while ( ss >> item )
-          {
+        while (ss >> item)
+        {
           numberOfCellPoints++;
-          }
         }
-      else if ( type == "vn" )
-        {
+      }
+      else if (type == "vn")
+      {
         this->m_NumberOfPointPixels++;
         this->m_UpdatePointData = true;
-        }
       }
+
     }
+  }
 
   this->m_PointDimension = 3;
 
@@ -223,19 +228,22 @@ OBJMeshIO
   while ( std::getline(m_InputFile, line, '\n') )
     {
     inputLine.clear();
-    for ( unsigned int ii = 0; ii < line.size(); ii++ )
-      {
-      if ( !std::isspace(line[ii], loc) )
-        {
-        type = line[ii];
-        inputLine = line.substr(ii + 1);
-        break;
-        }
-      }
+    auto starttype = std::find_if_not(line.begin(), line.end(), [&loc](const char l) { return std::isspace(l, loc); });
+    // only has spaces
+    if (starttype == line.end())
+      continue;
+
+    type = *starttype;
+    if (++starttype != line.end() && !std::isspace(*starttype, loc))
+    {
+      type = type + *starttype; ++starttype;
+    }
+
+    inputLine = line.substr(starttype - line.begin());
 
     if ( !inputLine.empty() )
       {
-      if ( type == "v" )
+      if ( type == "v")
         {
         std::stringstream ss(inputLine);
         for ( unsigned int ii = 0; ii < this->m_PointDimension; ii++ )
@@ -267,15 +275,19 @@ OBJMeshIO
   while ( std::getline(m_InputFile, line, '\n') )
     {
     inputLine.clear();
-    for ( unsigned int ii = 0; ii < line.size(); ii++ )
-      {
-      if ( !std::isspace(line[ii], loc) )
-        {
-        type = line[ii];
-        inputLine = line.substr(ii + 1);
-        break;
-        }
-      }
+
+    auto starttype = std::find_if_not(line.begin(), line.end(), [&loc](const char l) { return std::isspace(l, loc); });
+    // only has spaces
+    if (starttype == line.end())
+      continue;
+
+    type = *starttype;
+    if (++starttype != line.end() && !std::isspace(*starttype, loc))
+    {
+      type = type + *starttype; ++starttype;
+    }
+
+    inputLine = line.substr(starttype - line.begin());
 
     if ( !inputLine.empty() )
       {
@@ -336,15 +348,18 @@ OBJMeshIO
   while ( std::getline(m_InputFile, line, '\n') )
     {
     inputLine.clear();
-    for ( unsigned int ii = 0; ii < line.size(); ii++ )
-      {
-      if ( !std::isspace(line[ii], loc) )
-        {
-        type = line[ii];
-        inputLine = line.substr(ii + 1);
-        break;
-        }
-      }
+    auto starttype = std::find_if_not(line.begin(), line.end(), [&loc](const char l) { return std::isspace(l, loc); });
+    // only has spaces
+    if (starttype == line.end())
+      continue;
+
+    type = *starttype;
+    if (++starttype != line.end() && !std::isspace(*starttype, loc))
+    {
+      type = type + *starttype; ++starttype;
+    }
+
+    inputLine = line.substr(starttype - line.begin());
 
     if ( !inputLine.empty() )
       {
-- 
2.8.1.windows.1

