Skip parsing incomplete or invalid face definition(e.g. f definition only contains 1 or 2 indices).

This commit is contained in:
Syoyo Fujita
2018-02-23 20:25:13 +09:00
parent e060b4f4aa
commit b85714b4cf
4 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
newmtl white
Ka 0 0 0
Kd 1 1 1
Ks 0 0 0
newmtl red
Ka 0 0 0
Kd 1 0 0
Ks 0 0 0
newmtl green
Ka 0 0 0
Kd 0 1 0
Ks 0 0 0
newmtl blue
Ka 0 0 0
Kd 0 0 1
Ks 0 0 0
newmtl light
Ka 20 20 20
Kd 1 1 1
Ks 0 0 0

View File

@@ -0,0 +1,18 @@
mtllib invalid-face-definition.mtl
v 0.000000 2.000000 2.000000
v 0.000000 0.000000 2.000000
v 2.000000 0.000000 2.000000
v 2.000000 2.000000 2.000000
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.000000 0.000000
v 2.000000 2.000000 0.000000
# 8 vertices
g front cube
usemtl white
f 1
g back cube
# expects white material
f 8 7

View File

@@ -751,6 +751,24 @@ TEST_CASE("smoothing-group", "[Issue162]") {
}
TEST_CASE("invalid-face-definition", "[face]") {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/invalid-face-definition.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "[face] " << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(1 == shapes.size());
REQUIRE(0 == shapes[0].mesh.indices.size());
}
#if 0
int
main(

View File

@@ -1023,6 +1023,11 @@ static bool exportFaceGroupToShape(shape_t *shape,
for (size_t i = 0; i < faceGroup.size(); i++) {
const face_t &face = faceGroup[i];
if (face.vertex_indices.size() < 3) {
// Face must have 3+ vertices.
continue;
}
vertex_index_t i0 = face.vertex_indices[0];
vertex_index_t i1(-1);
vertex_index_t i2 = face.vertex_indices[1];