TODO(syoyo): refactor function.
| 1491 | |
| 1492 | // TODO(syoyo): refactor function. |
| 1493 | static bool exportGroupsToShape(shape_t *shape, const PrimGroup &prim_group, |
| 1494 | const std::vector<tag_t> &tags, |
| 1495 | const int material_id, const std::string &name, |
| 1496 | bool triangulate, const std::vector<real_t> &v, |
| 1497 | std::string *warn) { |
| 1498 | if (prim_group.IsEmpty()) { |
| 1499 | return false; |
| 1500 | } |
| 1501 | |
| 1502 | shape->name = name; |
| 1503 | |
| 1504 | // polygon |
| 1505 | if (!prim_group.faceGroup.empty()) { |
| 1506 | // Flatten vertices and indices |
| 1507 | for (size_t i = 0; i < prim_group.faceGroup.size(); i++) { |
| 1508 | const face_t &face = prim_group.faceGroup[i]; |
| 1509 | |
| 1510 | size_t npolys = face.vertex_indices.size(); |
| 1511 | |
| 1512 | if (npolys < 3) { |
| 1513 | // Face must have 3+ vertices. |
| 1514 | if (warn) { |
| 1515 | (*warn) += "Degenerated face found\n."; |
| 1516 | } |
| 1517 | continue; |
| 1518 | } |
| 1519 | |
| 1520 | if (triangulate && npolys != 3) { |
| 1521 | if (npolys == 4) { |
| 1522 | vertex_index_t i0 = face.vertex_indices[0]; |
| 1523 | vertex_index_t i1 = face.vertex_indices[1]; |
| 1524 | vertex_index_t i2 = face.vertex_indices[2]; |
| 1525 | vertex_index_t i3 = face.vertex_indices[3]; |
| 1526 | |
| 1527 | size_t vi0 = size_t(i0.v_idx); |
| 1528 | size_t vi1 = size_t(i1.v_idx); |
| 1529 | size_t vi2 = size_t(i2.v_idx); |
| 1530 | size_t vi3 = size_t(i3.v_idx); |
| 1531 | |
| 1532 | if (((3 * vi0 + 2) >= v.size()) || ((3 * vi1 + 2) >= v.size()) || |
| 1533 | ((3 * vi2 + 2) >= v.size()) || ((3 * vi3 + 2) >= v.size())) { |
| 1534 | // Invalid triangle. |
| 1535 | // FIXME(syoyo): Is it ok to simply skip this invalid triangle? |
| 1536 | if (warn) { |
| 1537 | (*warn) += "Face with invalid vertex index found.\n"; |
| 1538 | } |
| 1539 | continue; |
| 1540 | } |
| 1541 | |
| 1542 | real_t v0x = v[vi0 * 3 + 0]; |
| 1543 | real_t v0y = v[vi0 * 3 + 1]; |
| 1544 | real_t v0z = v[vi0 * 3 + 2]; |
| 1545 | real_t v1x = v[vi1 * 3 + 0]; |
| 1546 | real_t v1y = v[vi1 * 3 + 1]; |
| 1547 | real_t v1z = v[vi1 * 3 + 2]; |
| 1548 | real_t v2x = v[vi2 * 3 + 0]; |
| 1549 | real_t v2y = v[vi2 * 3 + 1]; |
| 1550 | real_t v2z = v[vi2 * 3 + 2]; |
no test coverage detected