Pārlūkot izejas kodu

Merge pull request #316 from tgnottingham/master

Allow numbers starting with decimal in fast_atof. Allow several spaces between numbers in OBJ files.
Alexander Gessler 11 gadi atpakaļ
vecāks
revīzija
b7d46b29ad

+ 7 - 6
code/ObjFileParser.cpp

@@ -113,8 +113,8 @@ void ObjFileParser::parseFile()
 					getVector3(m_pModel->m_Vertices);
 				} else if (*m_DataIt == 't') {
 					// read in texture coordinate ( 2D or 3D )
-                    ++m_DataIt;
-                    getVector( m_pModel->m_TextureCoord );
+                                        ++m_DataIt;
+                                        getVector( m_pModel->m_TextureCoord );
 				} else if (*m_DataIt == 'n') {
 					// Read in normal vector definition
 					++m_DataIt;
@@ -233,12 +233,13 @@ void ObjFileParser::copyNextLine(char *pBuffer, size_t length)
 // -------------------------------------------------------------------
 void ObjFileParser::getVector( std::vector<aiVector3D> &point3d_array ) {
     size_t numComponents( 0 );
-    DataArrayIt tmp( m_DataIt );
+    const char* tmp( &m_DataIt[0] );
     while( !IsLineEnd( *tmp ) ) {
-        if( *tmp == ' ' ) {
-            ++numComponents;
+        if ( !SkipSpaces( &tmp ) ) {
+            break;
         }
-        tmp++;
+        SkipToken( tmp );
+        ++numComponents;
     }
     float x, y, z;
     if( 2 == numComponents ) {

+ 14 - 2
code/fast_atof.h

@@ -229,14 +229,26 @@ inline uint64_t strtoul10_64( const char* in, const char** out=0, unsigned int*
 template <typename Real>
 inline const char* fast_atoreal_move( const char* c, Real& out, bool check_comma = true)
 {
-	Real f;
+	Real f = 0;
 
 	bool inv = (*c == '-');
 	if (inv || *c == '+') {
 		++c;
 	}
 
-	f = static_cast<Real>( strtoul10_64 ( c, &c) );
+        if (!(c[0] >= '0' && c[0] <= '9') &&
+                !(c[0] == '.' && c[1] >= '0' && c[1] <= '9'))
+        {
+                throw std::invalid_argument("Cannot parse string "
+                        "as real number: does not start with digit "
+                        "or decimal point followed by digit.");
+        }
+
+        if (*c != '.')
+        {
+                f = static_cast<Real>( strtoul10_64 ( c, &c) );
+        }
+
 	if ((*c == '.' || (check_comma && c[0] == ',')) && c[1] >= '0' && c[1] <= '9')
 	{
 		++c;

+ 11 - 0
test/models/OBJ/multiple_spaces.obj

@@ -0,0 +1,11 @@
+v   1.0 2.0 3.0   
+v 2.0   3.0 1.0
+v 3.0 1.0   2.0
+v   1.0    2.0    3.0
+
+vt   1.0 2.0 3.0   
+vt 2.0   3.0 1.0
+vt 3.0 1.0   2.0
+vt   1.0    2.0    3.0
+
+f 1  2   3    

+ 23 - 0
test/models/OBJ/number_formats.obj

@@ -0,0 +1,23 @@
+v 0 0 0
+
+v 1 2. 3.0
+v +1 +2. +3.0
+v -1 -2. -3.0
+
+v 1e2 2.e1 3.1e2
+v +1e2 +2.e1 +3.1e2
+v -1e2 -2.e1 -3.1e2
+
+v 1e+2 2.e+1 3.1+e2
+v +1e+2 +2.e+1 +3.1+e2
+v -1e+2 -2.e+1 -3.1+e2
+
+v 1e-2 2.e-1 3.1-e2
+v +1e-2 +2.e-1 +3.1-e2
+v -1e-2 -2.e-1 -3.1-e2
+
+v 1e10 2.e01 3.1e10
+
+v 1E2 2.E1 3.1E2
+
+f 1 2 4