TODO(syoyo): refactor function.
| 1399 | |
| 1400 | // TODO(syoyo): refactor function. |
| 1401 | static bool exportGroupsToShape(shape_t *shape, const PrimGroup &prim_group, |
| 1402 | const std::vector<tag_t> &tags, |
| 1403 | const int material_id, const std::string &name, |
| 1404 | bool triangulate, const std::vector<real_t> &v, |
| 1405 | std::string *warn) { |
| 1406 | if (prim_group.IsEmpty()) { |
| 1407 | return false; |
| 1408 | } |
| 1409 | |
| 1410 | shape->name = name; |
| 1411 | |
| 1412 | // polygon |
| 1413 | if (!prim_group.faceGroup.empty()) { |
| 1414 | // Flatten vertices and indices |
| 1415 | for (size_t i = 0; i < prim_group.faceGroup.size(); i++) { |
| 1416 | const face_t &face = prim_group.faceGroup[i]; |
| 1417 | |
| 1418 | size_t npolys = face.vertex_indices.size(); |
| 1419 | |
| 1420 | if (npolys < 3) { |
| 1421 | // Face must have 3+ vertices. |
| 1422 | if (warn) { |
| 1423 | (*warn) += "Degenerated face found\n."; |
| 1424 | } |
| 1425 | continue; |
| 1426 | } |
| 1427 | |
| 1428 | if (triangulate) { |
| 1429 | if (npolys == 4) { |
| 1430 | vertex_index_t i0 = face.vertex_indices[0]; |
| 1431 | vertex_index_t i1 = face.vertex_indices[1]; |
| 1432 | vertex_index_t i2 = face.vertex_indices[2]; |
| 1433 | vertex_index_t i3 = face.vertex_indices[3]; |
| 1434 | |
| 1435 | size_t vi0 = size_t(i0.v_idx); |
| 1436 | size_t vi1 = size_t(i1.v_idx); |
| 1437 | size_t vi2 = size_t(i2.v_idx); |
| 1438 | size_t vi3 = size_t(i3.v_idx); |
| 1439 | |
| 1440 | if (((3 * vi0 + 2) >= v.size()) || ((3 * vi1 + 2) >= v.size()) || |
| 1441 | ((3 * vi2 + 2) >= v.size()) || ((3 * vi3 + 2) >= v.size())) { |
| 1442 | // Invalid triangle. |
| 1443 | // FIXME(syoyo): Is it ok to simply skip this invalid triangle? |
| 1444 | if (warn) { |
| 1445 | (*warn) += "Face with invalid vertex index found.\n"; |
| 1446 | } |
| 1447 | continue; |
| 1448 | } |
| 1449 | |
| 1450 | real_t v0x = v[vi0 * 3 + 0]; |
| 1451 | real_t v0y = v[vi0 * 3 + 1]; |
| 1452 | real_t v0z = v[vi0 * 3 + 2]; |
| 1453 | real_t v1x = v[vi1 * 3 + 0]; |
| 1454 | real_t v1y = v[vi1 * 3 + 1]; |
| 1455 | real_t v1z = v[vi1 * 3 + 2]; |
| 1456 | real_t v2x = v[vi2 * 3 + 0]; |
| 1457 | real_t v2y = v[vi2 * 3 + 1]; |
| 1458 | real_t v2z = v[vi2 * 3 + 2]; |