From 3e9fab3bfc6f869c0d7be02e50ebe27818784707 Mon Sep 17 00:00:00 2001 From: Tyson Grant Nottingham Date: Mon, 21 Jul 2014 22:43:13 -0700 Subject: [PATCH 1/6] Allow numbers starting with decimal in fast_atof. --- code/fast_atof.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/code/fast_atof.h b/code/fast_atof.h index ddda1f29e..a7f0cb23f 100644 --- a/code/fast_atof.h +++ b/code/fast_atof.h @@ -229,14 +229,25 @@ inline uint64_t strtoul10_64( const char* in, const char** out=0, unsigned int* template 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( strtoul10_64 ( c, &c) ); + if (!isdigit(*c) && !(*c == '.' && isdigit(c[1]))) + { + 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( strtoul10_64 ( c, &c) ); + } + if ((*c == '.' || (check_comma && c[0] == ',')) && c[1] >= '0' && c[1] <= '9') { ++c; From 36c82fe5b05bfb15bc3b999d521b9ca26367992e Mon Sep 17 00:00:00 2001 From: Tyson Grant Nottingham Date: Mon, 21 Jul 2014 23:03:08 -0700 Subject: [PATCH 2/6] Allow several spaces between numbers in OBJ files. --- code/ObjFileParser.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index f2e65db71..e9a1dd9ef 100644 --- a/code/ObjFileParser.cpp +++ b/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; @@ -234,11 +234,17 @@ void ObjFileParser::copyNextLine(char *pBuffer, size_t length) void ObjFileParser::getVector( std::vector &point3d_array ) { size_t numComponents( 0 ); DataArrayIt tmp( m_DataIt ); - while( !IsLineEnd( *tmp ) ) { - if( *tmp == ' ' ) { - ++numComponents; + while( true ) { + while( isspace(*tmp) && !IsLineEnd(*tmp) ) { + tmp++; } - tmp++; + if( IsLineEnd(*tmp) ) { + break; + } + while( !isspace(*tmp) ) { + tmp++; + } + ++numComponents; } float x, y, z; if( 2 == numComponents ) { From 79c56adea40686bb822c75ec12a72a945d079f41 Mon Sep 17 00:00:00 2001 From: Tyson Grant Nottingham Date: Tue, 22 Jul 2014 21:58:41 -0700 Subject: [PATCH 3/6] Don't use isdigit() in fast_atof. --- code/fast_atof.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code/fast_atof.h b/code/fast_atof.h index a7f0cb23f..4ff666b38 100644 --- a/code/fast_atof.h +++ b/code/fast_atof.h @@ -236,7 +236,8 @@ inline const char* fast_atoreal_move( const char* c, Real& out, bool check_comma ++c; } - if (!isdigit(*c) && !(*c == '.' && isdigit(c[1]))) + 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 " From 0c5605d07df4f3faa0be7b55cc197ff979f35d84 Mon Sep 17 00:00:00 2001 From: Tyson Grant Nottingham Date: Tue, 22 Jul 2014 21:59:23 -0700 Subject: [PATCH 4/6] Don't use isspace() in OBJ file parser. --- code/ObjFileParser.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/code/ObjFileParser.cpp b/code/ObjFileParser.cpp index e9a1dd9ef..6e73f0bf4 100644 --- a/code/ObjFileParser.cpp +++ b/code/ObjFileParser.cpp @@ -233,17 +233,12 @@ void ObjFileParser::copyNextLine(char *pBuffer, size_t length) // ------------------------------------------------------------------- void ObjFileParser::getVector( std::vector &point3d_array ) { size_t numComponents( 0 ); - DataArrayIt tmp( m_DataIt ); - while( true ) { - while( isspace(*tmp) && !IsLineEnd(*tmp) ) { - tmp++; - } - if( IsLineEnd(*tmp) ) { + const char* tmp( &m_DataIt[0] ); + while( !IsLineEnd( *tmp ) ) { + if ( !SkipSpaces( &tmp ) ) { break; } - while( !isspace(*tmp) ) { - tmp++; - } + SkipToken( tmp ); ++numComponents; } float x, y, z; From 084ab9d82e8415c8c4e5bafca6343fedc85a49b2 Mon Sep 17 00:00:00 2001 From: Tyson Grant Nottingham Date: Tue, 22 Jul 2014 22:00:47 -0700 Subject: [PATCH 5/6] Add test OBJ containing various number formats. --- test/models/OBJ/number_formats.obj | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 test/models/OBJ/number_formats.obj diff --git a/test/models/OBJ/number_formats.obj b/test/models/OBJ/number_formats.obj new file mode 100644 index 000000000..12b21bf44 --- /dev/null +++ b/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 From f96375b5f6c039caf4abe95f97dcdfc1c2f8f614 Mon Sep 17 00:00:00 2001 From: Tyson Grant Nottingham Date: Tue, 22 Jul 2014 22:01:18 -0700 Subject: [PATCH 6/6] Add test OBJ w/ multiple spaces between elements. --- test/models/OBJ/multiple_spaces.obj | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test/models/OBJ/multiple_spaces.obj diff --git a/test/models/OBJ/multiple_spaces.obj b/test/models/OBJ/multiple_spaces.obj new file mode 100644 index 000000000..4f27a0342 --- /dev/null +++ b/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