From 3edca81a75eb74950beba4bd8368d90c2b95acbb Mon Sep 17 00:00:00 2001 From: Holden Green Date: Wed, 4 Jul 2018 21:21:56 -0700 Subject: [PATCH 1/7] Added line paths. --- tiny_obj_loader.h | 70 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 9e43a7d..4045404 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -236,9 +236,14 @@ typedef struct { std::vector tags; // SubD tag } mesh_t; +typedef struct { + std::vector indices; // pairs of indices for lines +} path_t; + typedef struct { std::string name; mesh_t mesh; + path_t path; } shape_t; // Vertex attributes @@ -401,6 +406,11 @@ struct face_t { face_t() : smoothing_group_id(0) {} }; +struct line_t { + int idx0; + int idx1; +}; + struct tag_sizes { tag_sizes() : num_ints(0), num_reals(0), num_strings(0) {} int num_ints; @@ -1015,15 +1025,17 @@ static int pnpoly(int nvert, T *vertx, T *verty, T testx, } // TODO(syoyo): refactor function. -static bool exportFaceGroupToShape(shape_t *shape, +static bool exportGroupsToShape(shape_t *shape, const std::vector &faceGroup, + const std::vector &lineGroup, const std::vector &tags, const int material_id, const std::string &name, bool triangulate, const std::vector &v) { - if (faceGroup.empty()) { - return false; - } + + + + if (!faceGroup.empty()) { // Flatten vertices and indices for (size_t i = 0; i < faceGroup.size(); i++) { @@ -1031,10 +1043,11 @@ static bool exportFaceGroupToShape(shape_t *shape, size_t npolys = face.vertex_indices.size(); + /* if (npolys < 3) { // Face must have 3+ vertices. continue; - } + }*/ vertex_index_t i0 = face.vertex_indices[0]; vertex_index_t i1(-1); @@ -1254,6 +1267,12 @@ static bool exportFaceGroupToShape(shape_t *shape, shape->name = name; shape->mesh.tags = tags; + } + + + if(!lineGroup.empty()){ + shape->path.indices = std::move(lineGroup); + } return true; } @@ -1765,6 +1784,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, std::vector vc; std::vector tags; std::vector faceGroup; + std::vector lineGroup; std::string name; // material @@ -1842,6 +1862,32 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, continue; } + // line + if (token[0] == 'l' && IS_SPACE((token[1]))){ + token += 2; + + line_t line_cache; + bool end_line_bit = 0; + while(!IS_NEW_LINE(token[0])){ + //get index from string + int idx; + fixIndex(parseInt(&token), 0, &idx); + // move to next space or end of string (\0 / \n) + token += strcspn(token, " \t\r")+1; + + if(!end_line_bit){ + line_cache.idx0 = idx; + } else { + line_cache.idx1 = idx; + lineGroup.push_back(line_cache.idx0); + lineGroup.push_back(line_cache.idx1); + line_cache = line_t(); + } + end_line_bit = !end_line_bit; + } + + continue; + } // face if (token[0] == 'f' && IS_SPACE((token[1]))) { token += 2; @@ -1892,7 +1938,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, // Create per-face material. Thus we don't add `shape` to `shapes` at // this time. // just clear `faceGroup` after `exportFaceGroupToShape()` call. - exportFaceGroupToShape(&shape, faceGroup, tags, material, name, + exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material, name, triangulate, v); faceGroup.clear(); material = newMaterialId; @@ -1947,7 +1993,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, // group name if (token[0] == 'g' && IS_SPACE((token[1]))) { // flush previous face group. - bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name, + bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material, name, triangulate, v); (void)ret; // return value not used. @@ -1984,7 +2030,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, // object name if (token[0] == 'o' && IS_SPACE((token[1]))) { // flush previous face group. - bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name, + bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material, name, triangulate, v); if (ret) { shapes->push_back(shape); @@ -2092,7 +2138,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, // Ignore unknown command. } - bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name, + bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material, name, triangulate, v); // exportFaceGroupToShape return false when `usemtl` is called in the last // line. @@ -2103,6 +2149,12 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, } faceGroup.clear(); // for safety + + for(int i=0;i Date: Wed, 4 Jul 2018 21:44:09 -0700 Subject: [PATCH 2/7] Removed print message. --- tiny_obj_loader.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 4045404..13b7471 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -2149,12 +2149,6 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, } faceGroup.clear(); // for safety - - for(int i=0;i Date: Wed, 4 Jul 2018 21:46:42 -0700 Subject: [PATCH 3/7] Fixed comments --- tiny_obj_loader.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 13b7471..4dcbe90 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1937,7 +1937,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, if (newMaterialId != material) { // Create per-face material. Thus we don't add `shape` to `shapes` at // this time. - // just clear `faceGroup` after `exportFaceGroupToShape()` call. + // just clear `faceGroup` after `exportGroupsToShape()` call. exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material, name, triangulate, v); faceGroup.clear(); @@ -2140,7 +2140,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material, name, triangulate, v); - // exportFaceGroupToShape return false when `usemtl` is called in the last + // exportGroupsToShape return false when `usemtl` is called in the last // line. // we also add `shape` to `shapes` when `shape.mesh` has already some // faces(indices) From 7befd59de4398d1aae9f1e9af5af5782545ba723 Mon Sep 17 00:00:00 2001 From: Holden Green Date: Wed, 4 Jul 2018 21:59:56 -0700 Subject: [PATCH 4/7] Removed comment --- tiny_obj_loader.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 4dcbe90..06bb03a 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1043,11 +1043,10 @@ static bool exportGroupsToShape(shape_t *shape, size_t npolys = face.vertex_indices.size(); - /* if (npolys < 3) { // Face must have 3+ vertices. continue; - }*/ + } vertex_index_t i0 = face.vertex_indices[0]; vertex_index_t i1(-1); From 6650dbf397705630b3114fc61ac59cd2f4a686cb Mon Sep 17 00:00:00 2001 From: Holden Green Date: Wed, 4 Jul 2018 22:10:45 -0700 Subject: [PATCH 5/7] Used swap in favor of move --- tiny_obj_loader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 06bb03a..cc894d6 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1270,7 +1270,7 @@ static bool exportGroupsToShape(shape_t *shape, if(!lineGroup.empty()){ - shape->path.indices = std::move(lineGroup); + shape->path.indices.swap(lineGroup); } return true; From 3cbf45a5720069aab361b0b44f651856031a8bb5 Mon Sep 17 00:00:00 2001 From: Holden Green Date: Wed, 4 Jul 2018 22:13:39 -0700 Subject: [PATCH 6/7] Non const --- tiny_obj_loader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index cc894d6..2a0f66f 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1027,7 +1027,7 @@ static int pnpoly(int nvert, T *vertx, T *verty, T testx, // TODO(syoyo): refactor function. static bool exportGroupsToShape(shape_t *shape, const std::vector &faceGroup, - const std::vector &lineGroup, + std::vector &lineGroup, const std::vector &tags, const int material_id, const std::string &name, bool triangulate, From a4b115a584bf966262dc21985886b4f36fdd6fb7 Mon Sep 17 00:00:00 2001 From: Holden Green Date: Tue, 24 Jul 2018 23:47:59 -0700 Subject: [PATCH 7/7] Hopefully fixed compiling error --- tiny_obj_loader.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 2a0f66f..d459047 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1035,6 +1035,10 @@ static bool exportGroupsToShape(shape_t *shape, + if (faceGroup.empty() && lineGroup.empty()) { + return false; + } + if (!faceGroup.empty()) { // Flatten vertices and indices