From 409d0997fb3dc053e0207e96d67f7924c856389a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?David=20K=C3=BCgler?= <david.kuegler@gris.tu-darmstadt.de>
Date: Tue, 21 Nov 2017 14:37:16 +0100
Subject: [PATCH 2/2] 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.
Remove c++11 features
---
 Modules/IO/Mesh/include/itkOBJMeshIO.h |   2 +
 Modules/IO/Mesh/src/itkOBJMeshIO.cxx   | 192 ++++++++++++++-------------------
 2 files changed, 84 insertions(+), 110 deletions(-)

diff --git a/Modules/IO/Mesh/include/itkOBJMeshIO.h b/Modules/IO/Mesh/include/itkOBJMeshIO.h
index 5c91079..ca5e915 100644
--- a/Modules/IO/Mesh/include/itkOBJMeshIO.h
+++ b/Modules/IO/Mesh/include/itkOBJMeshIO.h
@@ -151,6 +151,8 @@ protected:
       }
   }
 
+  static bool SplitLine(const std::string& line, std::string& type, std::string& content);
+
 protected:
   OBJMeshIO();
   virtual ~OBJMeshIO() ITK_OVERRIDE;
diff --git a/Modules/IO/Mesh/src/itkOBJMeshIO.cxx b/Modules/IO/Mesh/src/itkOBJMeshIO.cxx
index 4ae5cbe..3a0a4ff 100644
--- a/Modules/IO/Mesh/src/itkOBJMeshIO.cxx
+++ b/Modules/IO/Mesh/src/itkOBJMeshIO.cxx
@@ -105,6 +105,31 @@ OBJMeshIO
     }
 }
 
+bool
+OBJMeshIO
+::SplitLine(const std::string& line, std::string& type, std::string& content)
+{
+  std::locale   loc;
+  std::string::const_iterator start = line.begin();
+
+  while (start != line.end() && std::isspace(*start, loc))
+    ++start;
+  // start should be at the first non-empty sign.
+
+  std::string::const_iterator it = start;
+  while (it != line.end() && !std::isspace(*it, loc))
+    ++it;
+
+  if (it == line.end())
+    return false;
+
+  // else anyways
+  type = line.substr(start - line.begin(), it - start);
+  content = line.substr(it - line.begin());
+
+  return true;
+}
+
 void
 OBJMeshIO
 ::ReadMeshInformation()
@@ -123,21 +148,9 @@ OBJMeshIO
   std::locale   loc;
   while (std::getline(m_InputFile, line, '\n'))
   {
-    inputLine.clear();
-
-    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())
+    if (!SplitLine(line, type, inputLine))
       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")
@@ -160,7 +173,6 @@ OBJMeshIO
         this->m_NumberOfPointPixels++;
         this->m_UpdatePointData = true;
       }
-
     }
   }
 
@@ -226,33 +238,20 @@ OBJMeshIO
   std::string type;
   std::locale loc;
   while ( std::getline(m_InputFile, line, '\n') )
-    {
-    inputLine.clear();
-    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())
+  {
+    if (!SplitLine(line, type, inputLine))
       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")
-        {
+      {
         std::stringstream ss(inputLine);
         for ( unsigned int ii = 0; ii < this->m_PointDimension; ii++ )
-          {
           ss >> data[index++];
-          }
-        }
       }
     }
+  }
 
   CloseFile();
 }
@@ -273,53 +272,39 @@ OBJMeshIO
   std::string type;
   std::locale loc;
   while ( std::getline(m_InputFile, line, '\n') )
-    {
-    inputLine.clear();
-
-    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())
+  {
+    if (!SplitLine(line, type, inputLine))
       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 == "f" )
-        {
+      {
         std::stringstream   ss(inputLine);
         std::string         item;
         std::vector< long > idList;
         while ( ss >> item )
-          {
+        {
           long                   id;
           std::string::size_type pos = item.find('/');
           while ( pos != std::string::npos )
-            {
+          {
             item.replace(pos, 1, " ");
             pos = item.find('/', pos);
-            }
+          }
 
           std::stringstream st(item);
           st >> id;
 
           idList.push_back(id);
-          }
+        }
 
         data[index++] = static_cast<long>( idList.size() );
         for ( std::vector< long >::const_iterator it = idList.begin(); it != idList.end(); ++it )
-          {
           data[index++] = ( *it - 1 );
-          }
-        }
       }
     }
+  }
 
   CloseFile();
 
@@ -346,33 +331,20 @@ OBJMeshIO
   std::string type;
   std::locale loc;
   while ( std::getline(m_InputFile, line, '\n') )
-    {
-    inputLine.clear();
-    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())
+  {
+    if (!SplitLine(line, type, inputLine))
       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 == "vn" )
-        {
+      {
         std::stringstream ss(inputLine);
         for ( unsigned int ii = 0; ii < this->m_PointDimension; ii++ )
-          {
           ss >> data[index++];
-          }
-        }
       }
     }
+  }
 
   CloseFile();
 }
@@ -389,9 +361,9 @@ OBJMeshIO
 {
   // Check file name
   if ( this->m_FileName == "" )
-    {
+  {
     itkExceptionMacro("No Input FileName");
-    }
+  }
 
   // Define output stream and open it
   std::ofstream outputFile;
@@ -399,10 +371,10 @@ OBJMeshIO
 
   // Test whether input file was opened successfuly
   if ( !outputFile.is_open() )
-    {
+  {
     itkExceptionMacro("Unable to open file\n"
                       "outputFilename= " << this->m_FileName);
-    }
+  }
 
   // write comments
   outputFile << "# OBJ file generated by ITK\n";
@@ -419,9 +391,9 @@ WritePoints(void *buffer)
 {
   // Check file name
   if ( this->m_FileName == "" )
-    {
+  {
     itkExceptionMacro("No Input FileName");
-    }
+  }
 
   // Define output stream and open it
   std::ofstream outputFile;
@@ -429,96 +401,96 @@ WritePoints(void *buffer)
 
   // Test whether input file was opened successfuly
   if ( !outputFile.is_open() )
-    {
+  {
     itkExceptionMacro("Unable to open file\n"
                       "outputFilename= " << this->m_FileName);
-    }
+  }
 
   // Write points
   switch ( this->m_PointComponentType )
-    {
+  {
     case UCHAR:
-      {
+    {
       WritePoints(static_cast< unsigned char * >( buffer ), outputFile);
       break;
-      }
+    }
     case CHAR:
-      {
+    {
       WritePoints(static_cast< char * >( buffer ), outputFile);
 
       break;
-      }
+    }
     case USHORT:
-      {
+    {
       WritePoints(static_cast< unsigned short * >( buffer ), outputFile);
 
       break;
-      }
+     }
     case SHORT:
-      {
+    {
       WritePoints(static_cast< short * >( buffer ), outputFile);
 
       break;
-      }
+    }
     case UINT:
-      {
+    {
       WritePoints(static_cast< unsigned int * >( buffer ), outputFile);
 
       break;
-      }
+    }
     case INT:
-      {
+    {
       WritePoints(static_cast< int * >( buffer ), outputFile);
 
       break;
-      }
+    }
     case ULONG:
-      {
+    {
       WritePoints(static_cast< unsigned long * >( buffer ), outputFile);
 
       break;
-      }
+    }
     case LONG:
-      {
+    {
       WritePoints(static_cast< long * >( buffer ), outputFile);
 
       break;
-      }
+    }
     case ULONGLONG:
-      {
+    {
       WritePoints(static_cast< unsigned long long * >( buffer ), outputFile);
 
       break;
-      }
+    }
     case LONGLONG:
-      {
+    {
       WritePoints(static_cast< long long * >( buffer ), outputFile);
 
       break;
-      }
+    }
     case FLOAT:
-      {
+    {
       WritePoints(static_cast< float * >( buffer ), outputFile);
 
       break;
-      }
+    }
     case DOUBLE:
-      {
+    {
       WritePoints(static_cast< double * >( buffer ), outputFile);
 
       break;
-      }
+    }
     case LDOUBLE:
-      {
+    {
       WritePoints(static_cast< long double * >( buffer ), outputFile);
 
       break;
-      }
+    }
     default:
-      {
+    {
       itkExceptionMacro(<< "Unknown point component type" << std::endl);
-      }
     }
+  }
 
   outputFile.close();
 }
-- 
2.8.1.windows.1

