diff --git a/experimental/optimized-parse.cc b/experimental/optimized-parse.cc index 6f94db7..01df737 100644 --- a/experimental/optimized-parse.cc +++ b/experimental/optimized-parse.cc @@ -273,6 +273,8 @@ static inline int length_until_newline(const char *token, int n) { int len = 0; + assert(n < 4095); + // Assume token[n-1] = '\0' for (len = 0; len < n -1; len++) { if (token[len] == '\n') { @@ -643,7 +645,7 @@ struct CommandCount } }; -static bool parseLine(Command *command, const char *p, size_t p_len) +static bool parseLine(Command *command, const char *p, size_t p_len, bool triangulate = true) { char linebuf[4096]; assert(p_len < 4095); @@ -710,6 +712,8 @@ static bool parseLine(Command *command, const char *p, size_t p_len) //token += strspn(token, " \t"); skip_space(&token); + StackVector f; + while (!IS_NEW_LINE(token[0])) { vertex_index vi = parseRawTriple(&token); //printf("v = %d, %d, %d\n", vi.v_idx, vi.vn_idx, vi.vt_idx); @@ -720,11 +724,31 @@ static bool parseLine(Command *command, const char *p, size_t p_len) //token += n; skip_space_and_cr(&token); - command->f.push_back(vi); + f->push_back(vi); } command->type = COMMAND_F; + if (triangulate) { + vertex_index i0 = f[0]; + vertex_index i1(-1); + vertex_index i2 = f[1]; + + for (size_t k = 2; k < f->size(); k++) { + i1 = i2; + i2 = f[k]; + command->f.emplace_back(i0); + command->f.emplace_back(i1); + command->f.emplace_back(i2); + } + + } else { + + for (size_t k = 0; k < f->size(); k++) { + command->f.emplace_back(f[k]); + } + } + return true; } @@ -747,7 +771,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len) //command->material_name->push_back('\0'); skip_space(&token); command->material_name = token; - command->material_name_len = length_until_newline(token, p_len - (token - p)); + command->material_name_len = length_until_newline(token, p_len - (token - linebuf)); command->type = COMMAND_USEMTL; return true; @@ -808,7 +832,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len) token += 2; command->group_name = token; - command->group_name_len = length_until_newline(token, p_len - (token - p)); + command->group_name_len = length_until_newline(token, p_len - (token - linebuf)); command->type = COMMAND_G; return true; @@ -831,7 +855,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len) token += 2; command->object_name = token; - command->object_name_len = length_until_newline(token, p_len - (token - p)); + command->object_name_len = length_until_newline(token, p_len - (token - linebuf)); command->type = COMMAND_O; return true; @@ -969,8 +993,8 @@ bool parse(std::vector> &vertices, std::vector