Skip to content

Commit 776344b

Browse files
committed
Fix parsing line primitive.
1 parent ba70318 commit 776344b

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

models/line-prim.obj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ v 2.000000 2.000000 0.000000
1313
g g0
1414
usemtl white
1515
l 1 2 3 4
16-
l 5 6 7
16+
l 5 6 7 8

tests/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
CXX ?= g++
44
CXXFLAGS ?= -g -O2
55

6-
tester: tester.cc
7-
$(CXX) $(CXXFLAGS) -o tester tester.cc
6+
tester: tester.cc ../tiny_obj_loader.h
7+
$(CXX) $(CXXFLAGS) -fsanitize=address -o tester tester.cc
88

99
all: tester
1010

1111
check: tester
12-
./tester
12+
./tester
1313

1414
clean:
1515
rm -rf tester

tests/tester.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,8 @@ TEST_CASE("line-primitive", "[line]") {
957957

958958
REQUIRE(true == ret);
959959
REQUIRE(1 == shapes.size());
960-
REQUIRE(6 == shapes[0].path.indices.size());
960+
REQUIRE(8 == shapes[0].lines.indices.size());
961+
REQUIRE(2 == shapes[0].lines.num_line_vertices.size());
961962
}
962963

963964
TEST_CASE("multiple-group-names", "[group]") {
@@ -1060,7 +1061,7 @@ TEST_CASE("colorspace", "[Issue184]") {
10601061
// Just check if it does not crash.
10611062
// Disable by default since Windows filesystem can't create filename of afl
10621063
// testdata
1063-
#if 0
1064+
#if 0
10641065

10651066
TEST_CASE("afl000000", "[AFL]") {
10661067
tinyobj::attrib_t attrib;

tiny_obj_loader.h

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ typedef struct {
260260
typedef struct {
261261
// Linear flattened indices.
262262
std::vector<index_t> indices; // indices for vertices(poly lines)
263-
std::vector<int> num_line_vertces; // The number of vertices per line.
263+
std::vector<int> num_line_vertices; // The number of vertices per line.
264264
} lines_t;
265265

266266
typedef struct {
@@ -592,6 +592,10 @@ struct PrimGroup {
592592
pointGroup.clear();
593593
}
594594

595+
bool IsEmpty() const {
596+
return faceGroup.empty() && lineGroup.empty() && pointGroup.empty();
597+
}
598+
595599
// TODO(syoyo): bspline, surface, ...
596600
};
597601

@@ -1221,10 +1225,13 @@ static bool exportGroupsToShape(shape_t *shape, const PrimGroup &prim_group,
12211225
const int material_id, const std::string &name,
12221226
bool triangulate,
12231227
const std::vector<real_t> &v) {
1224-
if (prim_group.faceGroup.empty() && prim_group.lineGroup.empty()) {
1228+
if (prim_group.IsEmpty()) {
12251229
return false;
12261230
}
12271231

1232+
shape->name = name;
1233+
1234+
// polygon
12281235
if (!prim_group.faceGroup.empty()) {
12291236
// Flatten vertices and indices
12301237
for (size_t i = 0; i < prim_group.faceGroup.size(); i++) {
@@ -1464,13 +1471,28 @@ static bool exportGroupsToShape(shape_t *shape, const PrimGroup &prim_group,
14641471
}
14651472
}
14661473

1467-
shape->name = name;
14681474
shape->mesh.tags = tags;
14691475
}
14701476

1471-
// if (!lineGroup.empty()) {
1472-
// shape->lines.indices.swap(lineGroup);
1473-
//}
1477+
// line
1478+
if (!prim_group.lineGroup.empty()) {
1479+
// Flatten indices
1480+
for (size_t i = 0; i < prim_group.lineGroup.size(); i++) {
1481+
for (size_t j = 0; j < prim_group.lineGroup[i].vertex_indices.size(); j++) {
1482+
1483+
const vertex_index_t &vi = prim_group.lineGroup[i].vertex_indices[j];
1484+
1485+
index_t idx;
1486+
idx.vertex_index = vi.v_idx;
1487+
idx.normal_index = vi.vn_idx;
1488+
idx.texcoord_index = vi.vt_idx;
1489+
1490+
shape->lines.indices.push_back(idx);
1491+
}
1492+
1493+
shape->lines.num_line_vertices.push_back(int(prim_group.lineGroup[i].vertex_indices.size()));
1494+
}
1495+
}
14741496

14751497
return true;
14761498
}

0 commit comments

Comments
 (0)